PR rtl-optimization/79386
[official-gcc.git] / gcc / ada / sem_ch12.adb
blobc43533603bec6fa36859942a3dbd6708feef12cd
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Rident; use Rident;
47 with Restrict; use Restrict;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Sinput; use Sinput;
72 with Sinput.L; use Sinput.L;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
75 with Uname; use Uname;
76 with Table;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Warnsw; use Warnsw;
82 with GNAT.HTable;
84 package body Sem_Ch12 is
86 ----------------------------------------------------------
87 -- Implementation of Generic Analysis and Instantiation --
88 ----------------------------------------------------------
90 -- GNAT implements generics by macro expansion. No attempt is made to share
91 -- generic instantiations (for now). Analysis of a generic definition does
92 -- not perform any expansion action, but the expander must be called on the
93 -- tree for each instantiation, because the expansion may of course depend
94 -- on the generic actuals. All of this is best achieved as follows:
96 -- a) Semantic analysis of a generic unit is performed on a copy of the
97 -- tree for the generic unit. All tree modifications that follow analysis
98 -- do not affect the original tree. Links are kept between the original
99 -- tree and the copy, in order to recognize non-local references within
100 -- the generic, and propagate them to each instance (recall that name
101 -- resolution is done on the generic declaration: generics are not really
102 -- macros). This is summarized in the following diagram:
104 -- .-----------. .----------.
105 -- | semantic |<--------------| generic |
106 -- | copy | | unit |
107 -- | |==============>| |
108 -- |___________| global |__________|
109 -- references | | |
110 -- | | |
111 -- .-----|--|.
112 -- | .-----|---.
113 -- | | .----------.
114 -- | | | generic |
115 -- |__| | |
116 -- |__| instance |
117 -- |__________|
119 -- b) Each instantiation copies the original tree, and inserts into it a
120 -- series of declarations that describe the mapping between generic formals
121 -- and actuals. For example, a generic In OUT parameter is an object
122 -- renaming of the corresponding actual, etc. Generic IN parameters are
123 -- constant declarations.
125 -- c) In order to give the right visibility for these renamings, we use
126 -- a different scheme for package and subprogram instantiations. For
127 -- packages, the list of renamings is inserted into the package
128 -- specification, before the visible declarations of the package. The
129 -- renamings are analyzed before any of the text of the instance, and are
130 -- thus visible at the right place. Furthermore, outside of the instance,
131 -- the generic parameters are visible and denote their corresponding
132 -- actuals.
134 -- For subprograms, we create a container package to hold the renamings
135 -- and the subprogram instance itself. Analysis of the package makes the
136 -- renaming declarations visible to the subprogram. After analyzing the
137 -- package, the defining entity for the subprogram is touched-up so that
138 -- it appears declared in the current scope, and not inside the container
139 -- package.
141 -- If the instantiation is a compilation unit, the container package is
142 -- given the same name as the subprogram instance. This ensures that
143 -- the elaboration procedure called by the binder, using the compilation
144 -- unit name, calls in fact the elaboration procedure for the package.
146 -- Not surprisingly, private types complicate this approach. By saving in
147 -- the original generic object the non-local references, we guarantee that
148 -- the proper entities are referenced at the point of instantiation.
149 -- However, for private types, this by itself does not insure that the
150 -- proper VIEW of the entity is used (the full type may be visible at the
151 -- point of generic definition, but not at instantiation, or vice-versa).
152 -- In order to reference the proper view, we special-case any reference
153 -- to private types in the generic object, by saving both views, one in
154 -- the generic and one in the semantic copy. At time of instantiation, we
155 -- check whether the two views are consistent, and exchange declarations if
156 -- necessary, in order to restore the correct visibility. Similarly, if
157 -- the instance view is private when the generic view was not, we perform
158 -- the exchange. After completing the instantiation, we restore the
159 -- current visibility. The flag Has_Private_View marks identifiers in the
160 -- the generic unit that require checking.
162 -- Visibility within nested generic units requires special handling.
163 -- Consider the following scheme:
165 -- type Global is ... -- outside of generic unit.
166 -- generic ...
167 -- package Outer is
168 -- ...
169 -- type Semi_Global is ... -- global to inner.
171 -- generic ... -- 1
172 -- procedure inner (X1 : Global; X2 : Semi_Global);
174 -- procedure in2 is new inner (...); -- 4
175 -- end Outer;
177 -- package New_Outer is new Outer (...); -- 2
178 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
180 -- The semantic analysis of Outer captures all occurrences of Global.
181 -- The semantic analysis of Inner (at 1) captures both occurrences of
182 -- Global and Semi_Global.
184 -- At point 2 (instantiation of Outer), we also produce a generic copy
185 -- of Inner, even though Inner is, at that point, not being instantiated.
186 -- (This is just part of the semantic analysis of New_Outer).
188 -- Critically, references to Global within Inner must be preserved, while
189 -- references to Semi_Global should not preserved, because they must now
190 -- resolve to an entity within New_Outer. To distinguish between these, we
191 -- use a global variable, Current_Instantiated_Parent, which is set when
192 -- performing a generic copy during instantiation (at 2). This variable is
193 -- used when performing a generic copy that is not an instantiation, but
194 -- that is nested within one, as the occurrence of 1 within 2. The analysis
195 -- of a nested generic only preserves references that are global to the
196 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
197 -- determine whether a reference is external to the given parent.
199 -- The instantiation at point 3 requires no special treatment. The method
200 -- works as well for further nestings of generic units, but of course the
201 -- variable Current_Instantiated_Parent must be stacked because nested
202 -- instantiations can occur, e.g. the occurrence of 4 within 2.
204 -- The instantiation of package and subprogram bodies is handled in a
205 -- similar manner, except that it is delayed until after semantic
206 -- analysis is complete. In this fashion complex cross-dependencies
207 -- between several package declarations and bodies containing generics
208 -- can be compiled which otherwise would diagnose spurious circularities.
210 -- For example, it is possible to compile two packages A and B that
211 -- have the following structure:
213 -- package A is package B is
214 -- generic ... generic ...
215 -- package G_A is package G_B is
217 -- with B; with A;
218 -- package body A is package body B is
219 -- package N_B is new G_B (..) package N_A is new G_A (..)
221 -- The table Pending_Instantiations in package Inline is used to keep
222 -- track of body instantiations that are delayed in this manner. Inline
223 -- handles the actual calls to do the body instantiations. This activity
224 -- is part of Inline, since the processing occurs at the same point, and
225 -- for essentially the same reason, as the handling of inlined routines.
227 ----------------------------------------------
228 -- Detection of Instantiation Circularities --
229 ----------------------------------------------
231 -- If we have a chain of instantiations that is circular, this is static
232 -- error which must be detected at compile time. The detection of these
233 -- circularities is carried out at the point that we insert a generic
234 -- instance spec or body. If there is a circularity, then the analysis of
235 -- the offending spec or body will eventually result in trying to load the
236 -- same unit again, and we detect this problem as we analyze the package
237 -- instantiation for the second time.
239 -- At least in some cases after we have detected the circularity, we get
240 -- into trouble if we try to keep going. The following flag is set if a
241 -- circularity is detected, and used to abandon compilation after the
242 -- messages have been posted.
244 -----------------------------------------
245 -- Implementation of Generic Contracts --
246 -----------------------------------------
248 -- A "contract" is a collection of aspects and pragmas that either verify a
249 -- property of a construct at runtime or classify the data flow to and from
250 -- the construct in some fashion.
252 -- Generic packages, subprograms and their respective bodies may be subject
253 -- to the following contract-related aspects or pragmas collectively known
254 -- as annotations:
256 -- package subprogram [body]
257 -- Abstract_State Contract_Cases
258 -- Initial_Condition Depends
259 -- Initializes Extensions_Visible
260 -- Global
261 -- package body Post
262 -- Refined_State Post_Class
263 -- Postcondition
264 -- Pre
265 -- Pre_Class
266 -- Precondition
267 -- Refined_Depends
268 -- Refined_Global
269 -- Refined_Post
270 -- Test_Case
272 -- Most package contract annotations utilize forward references to classify
273 -- data declared within the package [body]. Subprogram annotations then use
274 -- the classifications to further refine them. These inter dependencies are
275 -- problematic with respect to the implementation of generics because their
276 -- analysis, capture of global references and instantiation does not mesh
277 -- well with the existing mechanism.
279 -- 1) Analysis of generic contracts is carried out the same way non-generic
280 -- contracts are analyzed:
282 -- 1.1) General rule - a contract is analyzed after all related aspects
283 -- and pragmas are analyzed. This is done by routines
285 -- Analyze_Package_Body_Contract
286 -- Analyze_Package_Contract
287 -- Analyze_Subprogram_Body_Contract
288 -- Analyze_Subprogram_Contract
290 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
291 -- are processed.
293 -- 1.3) Compilation unit body - the contract is analyzed at the end of
294 -- the body declaration list.
296 -- 1.4) Package - the contract is analyzed at the end of the private or
297 -- visible declarations, prior to analyzing the contracts of any nested
298 -- packages or subprograms.
300 -- 1.5) Package body - the contract is analyzed at the end of the body
301 -- declaration list, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
304 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
305 -- package or a subprogram, then its contract is analyzed at the end of
306 -- the enclosing declarations, otherwise the subprogram is a compilation
307 -- unit 1.2).
309 -- 1.7) Subprogram body - if the subprogram body is declared inside a
310 -- block, a package body or a subprogram body, then its contract is
311 -- analyzed at the end of the enclosing declarations, otherwise the
312 -- subprogram is a compilation unit 1.3).
314 -- 2) Capture of global references within contracts is done after capturing
315 -- global references within the generic template. There are two reasons for
316 -- this delay - pragma annotations are not part of the generic template in
317 -- the case of a generic subprogram declaration, and analysis of contracts
318 -- is delayed.
320 -- Contract-related source pragmas within generic templates are prepared
321 -- for delayed capture of global references by routine
323 -- Create_Generic_Contract
325 -- The routine associates these pragmas with the contract of the template.
326 -- In the case of a generic subprogram declaration, the routine creates
327 -- generic templates for the pragmas declared after the subprogram because
328 -- they are not part of the template.
330 -- generic -- template starts
331 -- procedure Gen_Proc (Input : Integer); -- template ends
332 -- pragma Precondition (Input > 0); -- requires own template
334 -- 2.1) The capture of global references with aspect specifications and
335 -- source pragmas that apply to a generic unit must be suppressed when
336 -- the generic template is being processed because the contracts have not
337 -- been analyzed yet. Any attempts to capture global references at that
338 -- point will destroy the Associated_Node linkages and leave the template
339 -- undecorated. This delay is controlled by routine
341 -- Requires_Delayed_Save
343 -- 2.2) The real capture of global references within a contract is done
344 -- after the contract has been analyzed, by routine
346 -- Save_Global_References_In_Contract
348 -- 3) The instantiation of a generic contract occurs as part of the
349 -- instantiation of the contract owner. Generic subprogram declarations
350 -- require additional processing when the contract is specified by pragmas
351 -- because the pragmas are not part of the generic template. This is done
352 -- by routine
354 -- Instantiate_Subprogram_Contract
356 Circularity_Detected : Boolean := False;
357 -- This should really be reset on encountering a new main unit, but in
358 -- practice we are not using multiple main units so it is not critical.
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formal into this local package. The result is a
385 -- a package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back-end, so it can freely violate the placement
388 -- rules for generic formal declarations.
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formal. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted (i.e.
401 -- which still appear as generic formals.
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
407 -----------------------
408 -- Local subprograms --
409 -----------------------
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
418 Def : Node_Id);
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
424 -- The following procedures treat other kinds of formal parameters
426 procedure Analyze_Formal_Derived_Interface_Type
427 (N : Node_Id;
428 T : Entity_Id;
429 Def : Node_Id);
431 procedure Analyze_Formal_Derived_Type
432 (N : Node_Id;
433 T : Entity_Id;
434 Def : Node_Id);
436 procedure Analyze_Formal_Interface_Type
437 (N : Node_Id;
438 T : Entity_Id;
439 Def : Node_Id);
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
445 -- generic.
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Private_Type
457 (N : Node_Id;
458 T : Entity_Id;
459 Def : Node_Id);
460 -- Creates a new private type, which does not require completion
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
471 function Analyze_Associations
472 (I_Node : Node_Id;
473 Formals : List_Id;
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
481 procedure Analyze_Subprogram_Instantiation
482 (N : Node_Id;
483 K : Entity_Kind);
485 procedure Build_Instance_Compilation_Unit_Nodes
486 (N : Node_Id;
487 Act_Body : Node_Id;
488 Act_Decl : Node_Id);
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
499 procedure Check_Access_Definition (N : Node_Id);
500 -- Subsidiary routine to null exclusion processing. Perform an assertion
501 -- check on Ada version and the presence of an access definition in N.
503 procedure Check_Formal_Packages (P_Id : Entity_Id);
504 -- Apply the following to all formal packages in generic associations
506 procedure Check_Formal_Package_Instance
507 (Formal_Pack : Entity_Id;
508 Actual_Pack : Entity_Id);
509 -- Verify that the actuals of the actual instance match the actuals of
510 -- the template for a formal package that is not declared with a box.
512 procedure Check_Forward_Instantiation (Decl : Node_Id);
513 -- If the generic is a local entity and the corresponding body has not
514 -- been seen yet, flag enclosing packages to indicate that it will be
515 -- elaborated after the generic body. Subprograms declared in the same
516 -- package cannot be inlined by the front end because front-end inlining
517 -- requires a strict linear order of elaboration.
519 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
520 -- Check if some association between formals and actuals requires to make
521 -- visible primitives of a tagged type, and make those primitives visible.
522 -- Return the list of primitives whose visibility is modified (to restore
523 -- their visibility later through Restore_Hidden_Primitives). If no
524 -- candidate is found then return No_Elist.
526 procedure Check_Hidden_Child_Unit
527 (N : Node_Id;
528 Gen_Unit : Entity_Id;
529 Act_Decl_Id : Entity_Id);
530 -- If the generic unit is an implicit child instance within a parent
531 -- instance, we need to make an explicit test that it is not hidden by
532 -- a child instance of the same name and parent.
534 procedure Check_Generic_Actuals
535 (Instance : Entity_Id;
536 Is_Formal_Box : Boolean);
537 -- Similar to previous one. Check the actuals in the instantiation,
538 -- whose views can change between the point of instantiation and the point
539 -- of instantiation of the body. In addition, mark the generic renamings
540 -- as generic actuals, so that they are not compatible with other actuals.
541 -- Recurse on an actual that is a formal package whose declaration has
542 -- a box.
544 function Contains_Instance_Of
545 (Inner : Entity_Id;
546 Outer : Entity_Id;
547 N : Node_Id) return Boolean;
548 -- Inner is instantiated within the generic Outer. Check whether Inner
549 -- directly or indirectly contains an instance of Outer or of one of its
550 -- parents, in the case of a subunit. Each generic unit holds a list of
551 -- the entities instantiated within (at any depth). This procedure
552 -- determines whether the set of such lists contains a cycle, i.e. an
553 -- illegal circular instantiation.
555 function Denotes_Formal_Package
556 (Pack : Entity_Id;
557 On_Exit : Boolean := False;
558 Instance : Entity_Id := Empty) return Boolean;
559 -- Returns True if E is a formal package of an enclosing generic, or
560 -- the actual for such a formal in an enclosing instantiation. If such
561 -- a package is used as a formal in an nested generic, or as an actual
562 -- in a nested instantiation, the visibility of ITS formals should not
563 -- be modified. When called from within Restore_Private_Views, the flag
564 -- On_Exit is true, to indicate that the search for a possible enclosing
565 -- instance should ignore the current one. In that case Instance denotes
566 -- the declaration for which this is an actual. This declaration may be
567 -- an instantiation in the source, or the internal instantiation that
568 -- corresponds to the actual for a formal package.
570 function Earlier (N1, N2 : Node_Id) return Boolean;
571 -- Yields True if N1 and N2 appear in the same compilation unit,
572 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
573 -- traversal of the tree for the unit. Used to determine the placement
574 -- of freeze nodes for instance bodies that may depend on other instances.
576 function Find_Actual_Type
577 (Typ : Entity_Id;
578 Gen_Type : Entity_Id) return Entity_Id;
579 -- When validating the actual types of a child instance, check whether
580 -- the formal is a formal type of the parent unit, and retrieve the current
581 -- actual for it. Typ is the entity in the analyzed formal type declaration
582 -- (component or index type of an array type, or designated type of an
583 -- access formal) and Gen_Type is the enclosing analyzed formal array
584 -- or access type. The desired actual may be a formal of a parent, or may
585 -- be declared in a formal package of a parent. In both cases it is a
586 -- generic actual type because it appears within a visible instance.
587 -- Finally, it may be declared in a parent unit without being a formal
588 -- of that unit, in which case it must be retrieved by visibility.
589 -- Ambiguities may still arise if two homonyms are declared in two formal
590 -- packages, and the prefix of the formal type may be needed to resolve
591 -- the ambiguity in the instance ???
593 procedure Freeze_Subprogram_Body
594 (Inst_Node : Node_Id;
595 Gen_Body : Node_Id;
596 Pack_Id : Entity_Id);
597 -- The generic body may appear textually after the instance, including
598 -- in the proper body of a stub, or within a different package instance.
599 -- Given that the instance can only be elaborated after the generic, we
600 -- place freeze_nodes for the instance and/or for packages that may enclose
601 -- the instance and the generic, so that the back-end can establish the
602 -- proper order of elaboration.
604 function Get_Associated_Node (N : Node_Id) return Node_Id;
605 -- In order to propagate semantic information back from the analyzed copy
606 -- to the original generic, we maintain links between selected nodes in the
607 -- generic and their corresponding copies. At the end of generic analysis,
608 -- the routine Save_Global_References traverses the generic tree, examines
609 -- the semantic information, and preserves the links to those nodes that
610 -- contain global information. At instantiation, the information from the
611 -- associated node is placed on the new copy, so that name resolution is
612 -- not repeated.
614 -- Three kinds of source nodes have associated nodes:
616 -- a) those that can reference (denote) entities, that is identifiers,
617 -- character literals, expanded_names, operator symbols, operators,
618 -- and attribute reference nodes. These nodes have an Entity field
619 -- and are the set of nodes that are in N_Has_Entity.
621 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
623 -- c) selected components (N_Selected_Component)
625 -- For the first class, the associated node preserves the entity if it is
626 -- global. If the generic contains nested instantiations, the associated
627 -- node itself has been recopied, and a chain of them must be followed.
629 -- For aggregates, the associated node allows retrieval of the type, which
630 -- may otherwise not appear in the generic. The view of this type may be
631 -- different between generic and instantiation, and the full view can be
632 -- installed before the instantiation is analyzed. For aggregates of type
633 -- extensions, the same view exchange may have to be performed for some of
634 -- the ancestor types, if their view is private at the point of
635 -- instantiation.
637 -- Nodes that are selected components in the parse tree may be rewritten
638 -- as expanded names after resolution, and must be treated as potential
639 -- entity holders, which is why they also have an Associated_Node.
641 -- Nodes that do not come from source, such as freeze nodes, do not appear
642 -- in the generic tree, and need not have an associated node.
644 -- The associated node is stored in the Associated_Node field. Note that
645 -- this field overlaps Entity, which is fine, because the whole point is
646 -- that we don't need or want the normal Entity field in this situation.
648 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
649 -- Traverse the Exchanged_Views list to see if a type was private
650 -- and has already been flipped during this phase of instantiation.
652 procedure Hide_Current_Scope;
653 -- When instantiating a generic child unit, the parent context must be
654 -- present, but the instance and all entities that may be generated
655 -- must be inserted in the current scope. We leave the current scope
656 -- on the stack, but make its entities invisible to avoid visibility
657 -- problems. This is reversed at the end of the instantiation. This is
658 -- not done for the instantiation of the bodies, which only require the
659 -- instances of the generic parents to be in scope.
661 function In_Same_Declarative_Part
662 (F_Node : Node_Id;
663 Inst : Node_Id) return Boolean;
664 -- True if the instantiation Inst and the given freeze_node F_Node appear
665 -- within the same declarative part, ignoring subunits, but with no inter-
666 -- vening subprograms or concurrent units. Used to find the proper plave
667 -- for the freeze node of an instance, when the generic is declared in a
668 -- previous instance. If predicate is true, the freeze node of the instance
669 -- can be placed after the freeze node of the previous instance, Otherwise
670 -- it has to be placed at the end of the current declarative part.
672 function In_Main_Context (E : Entity_Id) return Boolean;
673 -- Check whether an instantiation is in the context of the main unit.
674 -- Used to determine whether its body should be elaborated to allow
675 -- front-end inlining.
677 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
678 -- Add the context clause of the unit containing a generic unit to a
679 -- compilation unit that is, or contains, an instantiation.
681 procedure Init_Env;
682 -- Establish environment for subsequent instantiation. Separated from
683 -- Save_Env because data-structures for visibility handling must be
684 -- initialized before call to Check_Generic_Child_Unit.
686 procedure Inline_Instance_Body
687 (N : Node_Id;
688 Gen_Unit : Entity_Id;
689 Act_Decl : Node_Id);
690 -- If front-end inlining is requested, instantiate the package body,
691 -- and preserve the visibility of its compilation unit, to insure
692 -- that successive instantiations succeed.
694 procedure Insert_Freeze_Node_For_Instance
695 (N : Node_Id;
696 F_Node : Node_Id);
697 -- N denotes a package or a subprogram instantiation and F_Node is the
698 -- associated freeze node. Insert the freeze node before the first source
699 -- body which follows immediately after N. If no such body is found, the
700 -- freeze node is inserted at the end of the declarative region which
701 -- contains N.
703 procedure Install_Body
704 (Act_Body : Node_Id;
705 N : Node_Id;
706 Gen_Body : Node_Id;
707 Gen_Decl : Node_Id);
708 -- If the instantiation happens textually before the body of the generic,
709 -- the instantiation of the body must be analyzed after the generic body,
710 -- and not at the point of instantiation. Such early instantiations can
711 -- happen if the generic and the instance appear in a package declaration
712 -- because the generic body can only appear in the corresponding package
713 -- body. Early instantiations can also appear if generic, instance and
714 -- body are all in the declarative part of a subprogram or entry. Entities
715 -- of packages that are early instantiations are delayed, and their freeze
716 -- node appears after the generic body. This rather complex machinery is
717 -- needed when nested instantiations are present, because the source does
718 -- not carry any indication of where the corresponding instance bodies must
719 -- be installed and frozen.
721 procedure Install_Formal_Packages (Par : Entity_Id);
722 -- Install the visible part of any formal of the parent that is a formal
723 -- package. Note that for the case of a formal package with a box, this
724 -- includes the formal part of the formal package (12.7(10/2)).
726 procedure Install_Hidden_Primitives
727 (Prims_List : in out Elist_Id;
728 Gen_T : Entity_Id;
729 Act_T : Entity_Id);
730 -- Remove suffix 'P' from hidden primitives of Act_T to match the
731 -- visibility of primitives of Gen_T. The list of primitives to which
732 -- the suffix is removed is added to Prims_List to restore them later.
734 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
735 -- When compiling an instance of a child unit the parent (which is
736 -- itself an instance) is an enclosing scope that must be made
737 -- immediately visible. This procedure is also used to install the non-
738 -- generic parent of a generic child unit when compiling its body, so
739 -- that full views of types in the parent are made visible.
741 -- The functions Instantiate_XXX perform various legality checks and build
742 -- the declarations for instantiated generic parameters. In all of these
743 -- Formal is the entity in the generic unit, Actual is the entity of
744 -- expression in the generic associations, and Analyzed_Formal is the
745 -- formal in the generic copy, which contains the semantic information to
746 -- be used to validate the actual.
748 function Instantiate_Object
749 (Formal : Node_Id;
750 Actual : Node_Id;
751 Analyzed_Formal : Node_Id) return List_Id;
753 function Instantiate_Type
754 (Formal : Node_Id;
755 Actual : Node_Id;
756 Analyzed_Formal : Node_Id;
757 Actual_Decls : List_Id) return List_Id;
759 function Instantiate_Formal_Subprogram
760 (Formal : Node_Id;
761 Actual : Node_Id;
762 Analyzed_Formal : Node_Id) return Node_Id;
764 function Instantiate_Formal_Package
765 (Formal : Node_Id;
766 Actual : Node_Id;
767 Analyzed_Formal : Node_Id) return List_Id;
768 -- If the formal package is declared with a box, special visibility rules
769 -- apply to its formals: they are in the visible part of the package. This
770 -- is true in the declarative region of the formal package, that is to say
771 -- in the enclosing generic or instantiation. For an instantiation, the
772 -- parameters of the formal package are made visible in an explicit step.
773 -- Furthermore, if the actual has a visible USE clause, these formals must
774 -- be made potentially use-visible as well. On exit from the enclosing
775 -- instantiation, the reverse must be done.
777 -- For a formal package declared without a box, there are conformance rules
778 -- that apply to the actuals in the generic declaration and the actuals of
779 -- the actual package in the enclosing instantiation. The simplest way to
780 -- apply these rules is to repeat the instantiation of the formal package
781 -- in the context of the enclosing instance, and compare the generic
782 -- associations of this instantiation with those of the actual package.
783 -- This internal instantiation only needs to contain the renamings of the
784 -- formals: the visible and private declarations themselves need not be
785 -- created.
787 -- In Ada 2005, the formal package may be only partially parameterized.
788 -- In that case the visibility step must make visible those actuals whose
789 -- corresponding formals were given with a box. A final complication
790 -- involves inherited operations from formal derived types, which must
791 -- be visible if the type is.
793 function Is_In_Main_Unit (N : Node_Id) return Boolean;
794 -- Test if given node is in the main unit
796 procedure Load_Parent_Of_Generic
797 (N : Node_Id;
798 Spec : Node_Id;
799 Body_Optional : Boolean := False);
800 -- If the generic appears in a separate non-generic library unit, load the
801 -- corresponding body to retrieve the body of the generic. N is the node
802 -- for the generic instantiation, Spec is the generic package declaration.
804 -- Body_Optional is a flag that indicates that the body is being loaded to
805 -- ensure that temporaries are generated consistently when there are other
806 -- instances in the current declarative part that precede the one being
807 -- loaded. In that case a missing body is acceptable.
809 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
810 -- Within the generic part, entities in the formal package are
811 -- visible. To validate subsequent type declarations, indicate
812 -- the correspondence between the entities in the analyzed formal,
813 -- and the entities in the actual package. There are three packages
814 -- involved in the instantiation of a formal package: the parent
815 -- generic P1 which appears in the generic declaration, the fake
816 -- instantiation P2 which appears in the analyzed generic, and whose
817 -- visible entities may be used in subsequent formals, and the actual
818 -- P3 in the instance. To validate subsequent formals, me indicate
819 -- that the entities in P2 are mapped into those of P3. The mapping of
820 -- entities has to be done recursively for nested packages.
822 procedure Move_Freeze_Nodes
823 (Out_Of : Entity_Id;
824 After : Node_Id;
825 L : List_Id);
826 -- Freeze nodes can be generated in the analysis of a generic unit, but
827 -- will not be seen by the back-end. It is necessary to move those nodes
828 -- to the enclosing scope if they freeze an outer entity. We place them
829 -- at the end of the enclosing generic package, which is semantically
830 -- neutral.
832 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
833 -- Analyze actuals to perform name resolution. Full resolution is done
834 -- later, when the expected types are known, but names have to be captured
835 -- before installing parents of generics, that are not visible for the
836 -- actuals themselves.
838 -- If Inst is present, it is the entity of the package instance. This
839 -- entity is marked as having a limited_view actual when some actual is
840 -- a limited view. This is used to place the instance body properly.
842 procedure Remove_Parent (In_Body : Boolean := False);
843 -- Reverse effect after instantiation of child is complete
845 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
846 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
847 -- set to No_Elist.
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
865 -- parent of node.
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
880 type Assoc_Ptr is new Int;
882 Assoc_Null : constant Assoc_Ptr := -1;
884 type Assoc is record
885 Gen_Id : Entity_Id;
886 Act_Id : Entity_Id;
887 Next_In_HTable : Assoc_Ptr;
888 end record;
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
894 Table_Initial => 10,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
903 -- Hash table for associations
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
915 Element => Assoc,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
919 Next => Next_Assoc,
920 Key => Entity_Id,
921 Get_Key => Get_Gen_Id,
922 Hash => Hash,
923 Equal => "=");
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
964 end record;
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
970 Table_Initial => 32,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
980 -- renamings.
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1027 procedure Abandon_Instantiation (N : Node_Id) is
1028 begin
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1033 --------------------------------
1034 -- Add_Pending_Instantiation --
1035 --------------------------------
1037 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1038 begin
1040 -- Add to the instantiation node and the corresponding unit declaration
1041 -- the current values of global flags to be used when analyzing the
1042 -- instance body.
1044 Pending_Instantiations.Append
1045 ((Inst_Node => Inst,
1046 Act_Decl => Act_Decl,
1047 Expander_Status => Expander_Active,
1048 Current_Sem_Unit => Current_Sem_Unit,
1049 Scope_Suppress => Scope_Suppress,
1050 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1051 Version => Ada_Version,
1052 Version_Pragma => Ada_Version_Pragma,
1053 Warnings => Save_Warnings,
1054 SPARK_Mode => SPARK_Mode,
1055 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1056 end Add_Pending_Instantiation;
1058 ----------------------------------
1059 -- Adjust_Inherited_Pragma_Sloc --
1060 ----------------------------------
1062 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1063 begin
1064 Adjust_Instantiation_Sloc (N, S_Adjustment);
1065 end Adjust_Inherited_Pragma_Sloc;
1067 --------------------------
1068 -- Analyze_Associations --
1069 --------------------------
1071 function Analyze_Associations
1072 (I_Node : Node_Id;
1073 Formals : List_Id;
1074 F_Copy : List_Id) return List_Id
1076 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1077 Assoc : constant List_Id := New_List;
1078 Default_Actuals : constant List_Id := New_List;
1079 Gen_Unit : constant Entity_Id :=
1080 Defining_Entity (Parent (F_Copy));
1082 Actuals : List_Id;
1083 Actual : Node_Id;
1084 Analyzed_Formal : Node_Id;
1085 First_Named : Node_Id := Empty;
1086 Formal : Node_Id;
1087 Match : Node_Id;
1088 Named : Node_Id;
1089 Saved_Formal : Node_Id;
1091 Default_Formals : constant List_Id := New_List;
1092 -- If an Others_Choice is present, some of the formals may be defaulted.
1093 -- To simplify the treatment of visibility in an instance, we introduce
1094 -- individual defaults for each such formal. These defaults are
1095 -- appended to the list of associations and replace the Others_Choice.
1097 Found_Assoc : Node_Id;
1098 -- Association for the current formal being match. Empty if there are
1099 -- no remaining actuals, or if there is no named association with the
1100 -- name of the formal.
1102 Is_Named_Assoc : Boolean;
1103 Num_Matched : Nat := 0;
1104 Num_Actuals : Nat := 0;
1106 Others_Present : Boolean := False;
1107 Others_Choice : Node_Id := Empty;
1108 -- In Ada 2005, indicates partial parameterization of a formal
1109 -- package. As usual an other association must be last in the list.
1111 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1112 -- Warn if an actual fixed-point type has user-defined arithmetic
1113 -- operations, but there is no corresponding formal in the generic,
1114 -- in which case the predefined operations will be used. This merits
1115 -- a warning because of the special semantics of fixed point ops.
1117 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1118 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1119 -- cannot have a named association for it. AI05-0025 extends this rule
1120 -- to formals of formal packages by AI05-0025, and it also applies to
1121 -- box-initialized formals.
1123 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1124 -- Determine whether the parameter types and the return type of Subp
1125 -- are fully defined at the point of instantiation.
1127 function Matching_Actual
1128 (F : Entity_Id;
1129 A_F : Entity_Id) return Node_Id;
1130 -- Find actual that corresponds to a given a formal parameter. If the
1131 -- actuals are positional, return the next one, if any. If the actuals
1132 -- are named, scan the parameter associations to find the right one.
1133 -- A_F is the corresponding entity in the analyzed generic, which is
1134 -- placed on the selector name for ASIS use.
1136 -- In Ada 2005, a named association may be given with a box, in which
1137 -- case Matching_Actual sets Found_Assoc to the generic association,
1138 -- but return Empty for the actual itself. In this case the code below
1139 -- creates a corresponding declaration for the formal.
1141 function Partial_Parameterization return Boolean;
1142 -- Ada 2005: if no match is found for a given formal, check if the
1143 -- association for it includes a box, or whether the associations
1144 -- include an Others clause.
1146 procedure Process_Default (F : Entity_Id);
1147 -- Add a copy of the declaration of generic formal F to the list of
1148 -- associations, and add an explicit box association for F if there
1149 -- is none yet, and the default comes from an Others_Choice.
1151 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1152 -- Determine whether Subp renames one of the subprograms defined in the
1153 -- generated package Standard.
1155 procedure Set_Analyzed_Formal;
1156 -- Find the node in the generic copy that corresponds to a given formal.
1157 -- The semantic information on this node is used to perform legality
1158 -- checks on the actuals. Because semantic analysis can introduce some
1159 -- anonymous entities or modify the declaration node itself, the
1160 -- correspondence between the two lists is not one-one. In addition to
1161 -- anonymous types, the presence a formal equality will introduce an
1162 -- implicit declaration for the corresponding inequality.
1164 ----------------------------------------
1165 -- Check_Overloaded_Formal_Subprogram --
1166 ----------------------------------------
1168 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1169 Temp_Formal : Entity_Id;
1171 begin
1172 Temp_Formal := First (Formals);
1173 while Present (Temp_Formal) loop
1174 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1175 and then Temp_Formal /= Formal
1176 and then
1177 Chars (Defining_Unit_Name (Specification (Formal))) =
1178 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1179 then
1180 if Present (Found_Assoc) then
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Found_Assoc);
1185 else
1186 Error_Msg_N
1187 ("named association not allowed for overloaded formal",
1188 Others_Choice);
1189 end if;
1191 Abandon_Instantiation (Instantiation_Node);
1192 end if;
1194 Next (Temp_Formal);
1195 end loop;
1196 end Check_Overloaded_Formal_Subprogram;
1198 -------------------------------
1199 -- Check_Fixed_Point_Actual --
1200 -------------------------------
1202 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1203 Typ : constant Entity_Id := Entity (Actual);
1204 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1205 Elem : Elmt_Id;
1206 Formal : Node_Id;
1208 begin
1209 -- Locate primitive operations of the type that are arithmetic
1210 -- operations.
1212 Elem := First_Elmt (Prims);
1213 while Present (Elem) loop
1214 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1216 -- Check whether the generic unit has a formal subprogram of
1217 -- the same name. This does not check types but is good enough
1218 -- to justify a warning.
1220 Formal := First_Non_Pragma (Formals);
1221 while Present (Formal) loop
1222 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1223 and then Chars (Defining_Entity (Formal)) =
1224 Chars (Node (Elem))
1225 then
1226 exit;
1227 end if;
1229 Next (Formal);
1230 end loop;
1232 if No (Formal) then
1233 Error_Msg_Sloc := Sloc (Node (Elem));
1234 Error_Msg_NE
1235 ("?instance does not use primitive operation&#",
1236 Actual, Node (Elem));
1237 end if;
1238 end if;
1240 Next_Elmt (Elem);
1241 end loop;
1242 end Check_Fixed_Point_Actual;
1244 -------------------------------
1245 -- Has_Fully_Defined_Profile --
1246 -------------------------------
1248 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1249 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1250 -- Determine whethet type Typ is fully defined
1252 ---------------------------
1253 -- Is_Fully_Defined_Type --
1254 ---------------------------
1256 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1257 begin
1258 -- A private type without a full view is not fully defined
1260 if Is_Private_Type (Typ)
1261 and then No (Full_View (Typ))
1262 then
1263 return False;
1265 -- An incomplete type is never fully defined
1267 elsif Is_Incomplete_Type (Typ) then
1268 return False;
1270 -- All other types are fully defined
1272 else
1273 return True;
1274 end if;
1275 end Is_Fully_Defined_Type;
1277 -- Local declarations
1279 Param : Entity_Id;
1281 -- Start of processing for Has_Fully_Defined_Profile
1283 begin
1284 -- Check the parameters
1286 Param := First_Formal (Subp);
1287 while Present (Param) loop
1288 if not Is_Fully_Defined_Type (Etype (Param)) then
1289 return False;
1290 end if;
1292 Next_Formal (Param);
1293 end loop;
1295 -- Check the return type
1297 return Is_Fully_Defined_Type (Etype (Subp));
1298 end Has_Fully_Defined_Profile;
1300 ---------------------
1301 -- Matching_Actual --
1302 ---------------------
1304 function Matching_Actual
1305 (F : Entity_Id;
1306 A_F : Entity_Id) return Node_Id
1308 Prev : Node_Id;
1309 Act : Node_Id;
1311 begin
1312 Is_Named_Assoc := False;
1314 -- End of list of purely positional parameters
1316 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1317 Found_Assoc := Empty;
1318 Act := Empty;
1320 -- Case of positional parameter corresponding to current formal
1322 elsif No (Selector_Name (Actual)) then
1323 Found_Assoc := Actual;
1324 Act := Explicit_Generic_Actual_Parameter (Actual);
1325 Num_Matched := Num_Matched + 1;
1326 Next (Actual);
1328 -- Otherwise scan list of named actuals to find the one with the
1329 -- desired name. All remaining actuals have explicit names.
1331 else
1332 Is_Named_Assoc := True;
1333 Found_Assoc := Empty;
1334 Act := Empty;
1335 Prev := Empty;
1337 while Present (Actual) loop
1338 if Nkind (Actual) = N_Others_Choice then
1339 Found_Assoc := Empty;
1340 Act := Empty;
1342 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1343 Set_Entity (Selector_Name (Actual), A_F);
1344 Set_Etype (Selector_Name (Actual), Etype (A_F));
1345 Generate_Reference (A_F, Selector_Name (Actual));
1347 Found_Assoc := Actual;
1348 Act := Explicit_Generic_Actual_Parameter (Actual);
1349 Num_Matched := Num_Matched + 1;
1350 exit;
1351 end if;
1353 Prev := Actual;
1354 Next (Actual);
1355 end loop;
1357 -- Reset for subsequent searches. In most cases the named
1358 -- associations are in order. If they are not, we reorder them
1359 -- to avoid scanning twice the same actual. This is not just a
1360 -- question of efficiency: there may be multiple defaults with
1361 -- boxes that have the same name. In a nested instantiation we
1362 -- insert actuals for those defaults, and cannot rely on their
1363 -- names to disambiguate them.
1365 if Actual = First_Named then
1366 Next (First_Named);
1368 elsif Present (Actual) then
1369 Insert_Before (First_Named, Remove_Next (Prev));
1370 end if;
1372 Actual := First_Named;
1373 end if;
1375 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1376 Set_Used_As_Generic_Actual (Entity (Act));
1377 end if;
1379 return Act;
1380 end Matching_Actual;
1382 ------------------------------
1383 -- Partial_Parameterization --
1384 ------------------------------
1386 function Partial_Parameterization return Boolean is
1387 begin
1388 return Others_Present
1389 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1390 end Partial_Parameterization;
1392 ---------------------
1393 -- Process_Default --
1394 ---------------------
1396 procedure Process_Default (F : Entity_Id) is
1397 Loc : constant Source_Ptr := Sloc (I_Node);
1398 F_Id : constant Entity_Id := Defining_Entity (F);
1399 Decl : Node_Id;
1400 Default : Node_Id;
1401 Id : Entity_Id;
1403 begin
1404 -- Append copy of formal declaration to associations, and create new
1405 -- defining identifier for it.
1407 Decl := New_Copy_Tree (F);
1408 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1410 if Nkind (F) in N_Formal_Subprogram_Declaration then
1411 Set_Defining_Unit_Name (Specification (Decl), Id);
1413 else
1414 Set_Defining_Identifier (Decl, Id);
1415 end if;
1417 Append (Decl, Assoc);
1419 if No (Found_Assoc) then
1420 Default :=
1421 Make_Generic_Association (Loc,
1422 Selector_Name =>
1423 New_Occurrence_Of (Id, Loc),
1424 Explicit_Generic_Actual_Parameter => Empty);
1425 Set_Box_Present (Default);
1426 Append (Default, Default_Formals);
1427 end if;
1428 end Process_Default;
1430 ---------------------------------
1431 -- Renames_Standard_Subprogram --
1432 ---------------------------------
1434 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1435 Id : Entity_Id;
1437 begin
1438 Id := Alias (Subp);
1439 while Present (Id) loop
1440 if Scope (Id) = Standard_Standard then
1441 return True;
1442 end if;
1444 Id := Alias (Id);
1445 end loop;
1447 return False;
1448 end Renames_Standard_Subprogram;
1450 -------------------------
1451 -- Set_Analyzed_Formal --
1452 -------------------------
1454 procedure Set_Analyzed_Formal is
1455 Kind : Node_Kind;
1457 begin
1458 while Present (Analyzed_Formal) loop
1459 Kind := Nkind (Analyzed_Formal);
1461 case Nkind (Formal) is
1462 when N_Formal_Subprogram_Declaration =>
1463 exit when Kind in N_Formal_Subprogram_Declaration
1464 and then
1465 Chars
1466 (Defining_Unit_Name (Specification (Formal))) =
1467 Chars
1468 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1470 when N_Formal_Package_Declaration =>
1471 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1472 N_Generic_Package_Declaration,
1473 N_Package_Declaration);
1475 when N_Use_Package_Clause
1476 | N_Use_Type_Clause
1478 exit;
1480 when others =>
1482 -- Skip freeze nodes, and nodes inserted to replace
1483 -- unrecognized pragmas.
1485 exit when
1486 Kind not in N_Formal_Subprogram_Declaration
1487 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1488 N_Freeze_Entity,
1489 N_Null_Statement,
1490 N_Itype_Reference)
1491 and then Chars (Defining_Identifier (Formal)) =
1492 Chars (Defining_Identifier (Analyzed_Formal));
1493 end case;
1495 Next (Analyzed_Formal);
1496 end loop;
1497 end Set_Analyzed_Formal;
1499 -- Start of processing for Analyze_Associations
1501 begin
1502 Actuals := Generic_Associations (I_Node);
1504 if Present (Actuals) then
1506 -- Check for an Others choice, indicating a partial parameterization
1507 -- for a formal package.
1509 Actual := First (Actuals);
1510 while Present (Actual) loop
1511 if Nkind (Actual) = N_Others_Choice then
1512 Others_Present := True;
1513 Others_Choice := Actual;
1515 if Present (Next (Actual)) then
1516 Error_Msg_N ("others must be last association", Actual);
1517 end if;
1519 -- This subprogram is used both for formal packages and for
1520 -- instantiations. For the latter, associations must all be
1521 -- explicit.
1523 if Nkind (I_Node) /= N_Formal_Package_Declaration
1524 and then Comes_From_Source (I_Node)
1525 then
1526 Error_Msg_N
1527 ("others association not allowed in an instance",
1528 Actual);
1529 end if;
1531 -- In any case, nothing to do after the others association
1533 exit;
1535 elsif Box_Present (Actual)
1536 and then Comes_From_Source (I_Node)
1537 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1538 then
1539 Error_Msg_N
1540 ("box association not allowed in an instance", Actual);
1541 end if;
1543 Next (Actual);
1544 end loop;
1546 -- If named associations are present, save first named association
1547 -- (it may of course be Empty) to facilitate subsequent name search.
1549 First_Named := First (Actuals);
1550 while Present (First_Named)
1551 and then Nkind (First_Named) /= N_Others_Choice
1552 and then No (Selector_Name (First_Named))
1553 loop
1554 Num_Actuals := Num_Actuals + 1;
1555 Next (First_Named);
1556 end loop;
1557 end if;
1559 Named := First_Named;
1560 while Present (Named) loop
1561 if Nkind (Named) /= N_Others_Choice
1562 and then No (Selector_Name (Named))
1563 then
1564 Error_Msg_N ("invalid positional actual after named one", Named);
1565 Abandon_Instantiation (Named);
1566 end if;
1568 -- A named association may lack an actual parameter, if it was
1569 -- introduced for a default subprogram that turns out to be local
1570 -- to the outer instantiation. If it has a box association it must
1571 -- correspond to some formal in the generic.
1573 if Nkind (Named) /= N_Others_Choice
1574 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1575 or else Box_Present (Named))
1576 then
1577 Num_Actuals := Num_Actuals + 1;
1578 end if;
1580 Next (Named);
1581 end loop;
1583 if Present (Formals) then
1584 Formal := First_Non_Pragma (Formals);
1585 Analyzed_Formal := First_Non_Pragma (F_Copy);
1587 if Present (Actuals) then
1588 Actual := First (Actuals);
1590 -- All formals should have default values
1592 else
1593 Actual := Empty;
1594 end if;
1596 while Present (Formal) loop
1597 Set_Analyzed_Formal;
1598 Saved_Formal := Next_Non_Pragma (Formal);
1600 case Nkind (Formal) is
1601 when N_Formal_Object_Declaration =>
1602 Match :=
1603 Matching_Actual
1604 (Defining_Identifier (Formal),
1605 Defining_Identifier (Analyzed_Formal));
1607 if No (Match) and then Partial_Parameterization then
1608 Process_Default (Formal);
1610 else
1611 Append_List
1612 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1613 Assoc);
1615 -- For a defaulted in_parameter, create an entry in the
1616 -- the list of defaulted actuals, for GNATProve use. Do
1617 -- not included these defaults for an instance nested
1618 -- within a generic, because the defaults are also used
1619 -- in the analysis of the enclosing generic, and only
1620 -- defaulted subprograms are relevant there.
1622 if No (Match) and then not Inside_A_Generic then
1623 Append_To (Default_Actuals,
1624 Make_Generic_Association (Sloc (I_Node),
1625 Selector_Name =>
1626 New_Occurrence_Of
1627 (Defining_Identifier (Formal), Sloc (I_Node)),
1628 Explicit_Generic_Actual_Parameter =>
1629 New_Copy_Tree (Default_Expression (Formal))));
1630 end if;
1631 end if;
1633 -- If the object is a call to an expression function, this
1634 -- is a freezing point for it.
1636 if Is_Entity_Name (Match)
1637 and then Present (Entity (Match))
1638 and then Nkind
1639 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1640 = N_Expression_Function
1641 then
1642 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1643 end if;
1645 when N_Formal_Type_Declaration =>
1646 Match :=
1647 Matching_Actual
1648 (Defining_Identifier (Formal),
1649 Defining_Identifier (Analyzed_Formal));
1651 if No (Match) then
1652 if Partial_Parameterization then
1653 Process_Default (Formal);
1655 else
1656 Error_Msg_Sloc := Sloc (Gen_Unit);
1657 Error_Msg_NE
1658 ("missing actual&",
1659 Instantiation_Node, Defining_Identifier (Formal));
1660 Error_Msg_NE
1661 ("\in instantiation of & declared#",
1662 Instantiation_Node, Gen_Unit);
1663 Abandon_Instantiation (Instantiation_Node);
1664 end if;
1666 else
1667 Analyze (Match);
1668 Append_List
1669 (Instantiate_Type
1670 (Formal, Match, Analyzed_Formal, Assoc),
1671 Assoc);
1673 if Is_Fixed_Point_Type (Entity (Match)) then
1674 Check_Fixed_Point_Actual (Match);
1675 end if;
1677 -- An instantiation is a freeze point for the actuals,
1678 -- unless this is a rewritten formal package, or the
1679 -- formal is an Ada 2012 formal incomplete type.
1681 if Nkind (I_Node) = N_Formal_Package_Declaration
1682 or else
1683 (Ada_Version >= Ada_2012
1684 and then
1685 Ekind (Defining_Identifier (Analyzed_Formal)) =
1686 E_Incomplete_Type)
1687 then
1688 null;
1690 else
1691 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1692 end if;
1693 end if;
1695 -- A remote access-to-class-wide type is not a legal actual
1696 -- for a generic formal of an access type (E.2.2(17/2)).
1697 -- In GNAT an exception to this rule is introduced when
1698 -- the formal is marked as remote using implementation
1699 -- defined aspect/pragma Remote_Access_Type. In that case
1700 -- the actual must be remote as well.
1702 -- If the current instantiation is the construction of a
1703 -- local copy for a formal package the actuals may be
1704 -- defaulted, and there is no matching actual to check.
1706 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1707 and then
1708 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1709 N_Access_To_Object_Definition
1710 and then Present (Match)
1711 then
1712 declare
1713 Formal_Ent : constant Entity_Id :=
1714 Defining_Identifier (Analyzed_Formal);
1715 begin
1716 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1717 = Is_Remote_Types (Formal_Ent)
1718 then
1719 -- Remoteness of formal and actual match
1721 null;
1723 elsif Is_Remote_Types (Formal_Ent) then
1725 -- Remote formal, non-remote actual
1727 Error_Msg_NE
1728 ("actual for& must be remote", Match, Formal_Ent);
1730 else
1731 -- Non-remote formal, remote actual
1733 Error_Msg_NE
1734 ("actual for& may not be remote",
1735 Match, Formal_Ent);
1736 end if;
1737 end;
1738 end if;
1740 when N_Formal_Subprogram_Declaration =>
1741 Match :=
1742 Matching_Actual
1743 (Defining_Unit_Name (Specification (Formal)),
1744 Defining_Unit_Name (Specification (Analyzed_Formal)));
1746 -- If the formal subprogram has the same name as another
1747 -- formal subprogram of the generic, then a named
1748 -- association is illegal (12.3(9)). Exclude named
1749 -- associations that are generated for a nested instance.
1751 if Present (Match)
1752 and then Is_Named_Assoc
1753 and then Comes_From_Source (Found_Assoc)
1754 then
1755 Check_Overloaded_Formal_Subprogram (Formal);
1756 end if;
1758 -- If there is no corresponding actual, this may be case
1759 -- of partial parameterization, or else the formal has a
1760 -- default or a box.
1762 if No (Match) and then Partial_Parameterization then
1763 Process_Default (Formal);
1765 if Nkind (I_Node) = N_Formal_Package_Declaration then
1766 Check_Overloaded_Formal_Subprogram (Formal);
1767 end if;
1769 else
1770 Append_To (Assoc,
1771 Instantiate_Formal_Subprogram
1772 (Formal, Match, Analyzed_Formal));
1774 -- An instantiation is a freeze point for the actuals,
1775 -- unless this is a rewritten formal package.
1777 if Nkind (I_Node) /= N_Formal_Package_Declaration
1778 and then Nkind (Match) = N_Identifier
1779 and then Is_Subprogram (Entity (Match))
1781 -- The actual subprogram may rename a routine defined
1782 -- in Standard. Avoid freezing such renamings because
1783 -- subprograms coming from Standard cannot be frozen.
1785 and then
1786 not Renames_Standard_Subprogram (Entity (Match))
1788 -- If the actual subprogram comes from a different
1789 -- unit, it is already frozen, either by a body in
1790 -- that unit or by the end of the declarative part
1791 -- of the unit. This check avoids the freezing of
1792 -- subprograms defined in Standard which are used
1793 -- as generic actuals.
1795 and then In_Same_Code_Unit (Entity (Match), I_Node)
1796 and then Has_Fully_Defined_Profile (Entity (Match))
1797 then
1798 -- Mark the subprogram as having a delayed freeze
1799 -- since this may be an out-of-order action.
1801 Set_Has_Delayed_Freeze (Entity (Match));
1802 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1803 end if;
1804 end if;
1806 -- If this is a nested generic, preserve default for later
1807 -- instantiations. We do this as well for GNATProve use,
1808 -- so that the list of generic associations is complete.
1810 if No (Match) and then Box_Present (Formal) then
1811 declare
1812 Subp : constant Entity_Id :=
1813 Defining_Unit_Name (Specification (Last (Assoc)));
1815 begin
1816 Append_To (Default_Actuals,
1817 Make_Generic_Association (Sloc (I_Node),
1818 Selector_Name =>
1819 New_Occurrence_Of (Subp, Sloc (I_Node)),
1820 Explicit_Generic_Actual_Parameter =>
1821 New_Occurrence_Of (Subp, Sloc (I_Node))));
1822 end;
1823 end if;
1825 when N_Formal_Package_Declaration =>
1826 Match :=
1827 Matching_Actual
1828 (Defining_Identifier (Formal),
1829 Defining_Identifier (Original_Node (Analyzed_Formal)));
1831 if No (Match) then
1832 if Partial_Parameterization then
1833 Process_Default (Formal);
1835 else
1836 Error_Msg_Sloc := Sloc (Gen_Unit);
1837 Error_Msg_NE
1838 ("missing actual&",
1839 Instantiation_Node, Defining_Identifier (Formal));
1840 Error_Msg_NE
1841 ("\in instantiation of & declared#",
1842 Instantiation_Node, Gen_Unit);
1844 Abandon_Instantiation (Instantiation_Node);
1845 end if;
1847 else
1848 Analyze (Match);
1849 Append_List
1850 (Instantiate_Formal_Package
1851 (Formal, Match, Analyzed_Formal),
1852 Assoc);
1853 end if;
1855 -- For use type and use package appearing in the generic part,
1856 -- we have already copied them, so we can just move them where
1857 -- they belong (we mustn't recopy them since this would mess up
1858 -- the Sloc values).
1860 when N_Use_Package_Clause
1861 | N_Use_Type_Clause
1863 if Nkind (Original_Node (I_Node)) =
1864 N_Formal_Package_Declaration
1865 then
1866 Append (New_Copy_Tree (Formal), Assoc);
1867 else
1868 Remove (Formal);
1869 Append (Formal, Assoc);
1870 end if;
1872 when others =>
1873 raise Program_Error;
1874 end case;
1876 Formal := Saved_Formal;
1877 Next_Non_Pragma (Analyzed_Formal);
1878 end loop;
1880 if Num_Actuals > Num_Matched then
1881 Error_Msg_Sloc := Sloc (Gen_Unit);
1883 if Present (Selector_Name (Actual)) then
1884 Error_Msg_NE
1885 ("unmatched actual &", Actual, Selector_Name (Actual));
1886 Error_Msg_NE
1887 ("\in instantiation of & declared#", Actual, Gen_Unit);
1888 else
1889 Error_Msg_NE
1890 ("unmatched actual in instantiation of & declared#",
1891 Actual, Gen_Unit);
1892 end if;
1893 end if;
1895 elsif Present (Actuals) then
1896 Error_Msg_N
1897 ("too many actuals in generic instantiation", Instantiation_Node);
1898 end if;
1900 -- An instantiation freezes all generic actuals. The only exceptions
1901 -- to this are incomplete types and subprograms which are not fully
1902 -- defined at the point of instantiation.
1904 declare
1905 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1906 begin
1907 while Present (Elmt) loop
1908 Freeze_Before (I_Node, Node (Elmt));
1909 Next_Elmt (Elmt);
1910 end loop;
1911 end;
1913 -- If there are default subprograms, normalize the tree by adding
1914 -- explicit associations for them. This is required if the instance
1915 -- appears within a generic.
1917 if not Is_Empty_List (Default_Actuals) then
1918 declare
1919 Default : Node_Id;
1921 begin
1922 Default := First (Default_Actuals);
1923 while Present (Default) loop
1924 Mark_Rewrite_Insertion (Default);
1925 Next (Default);
1926 end loop;
1928 if No (Actuals) then
1929 Set_Generic_Associations (I_Node, Default_Actuals);
1930 else
1931 Append_List_To (Actuals, Default_Actuals);
1932 end if;
1933 end;
1934 end if;
1936 -- If this is a formal package, normalize the parameter list by adding
1937 -- explicit box associations for the formals that are covered by an
1938 -- Others_Choice.
1940 if not Is_Empty_List (Default_Formals) then
1941 Append_List (Default_Formals, Formals);
1942 end if;
1944 return Assoc;
1945 end Analyze_Associations;
1947 -------------------------------
1948 -- Analyze_Formal_Array_Type --
1949 -------------------------------
1951 procedure Analyze_Formal_Array_Type
1952 (T : in out Entity_Id;
1953 Def : Node_Id)
1955 DSS : Node_Id;
1957 begin
1958 -- Treated like a non-generic array declaration, with additional
1959 -- semantic checks.
1961 Enter_Name (T);
1963 if Nkind (Def) = N_Constrained_Array_Definition then
1964 DSS := First (Discrete_Subtype_Definitions (Def));
1965 while Present (DSS) loop
1966 if Nkind_In (DSS, N_Subtype_Indication,
1967 N_Range,
1968 N_Attribute_Reference)
1969 then
1970 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1971 end if;
1973 Next (DSS);
1974 end loop;
1975 end if;
1977 Array_Type_Declaration (T, Def);
1978 Set_Is_Generic_Type (Base_Type (T));
1980 if Ekind (Component_Type (T)) = E_Incomplete_Type
1981 and then No (Full_View (Component_Type (T)))
1982 then
1983 Error_Msg_N ("premature usage of incomplete type", Def);
1985 -- Check that range constraint is not allowed on the component type
1986 -- of a generic formal array type (AARM 12.5.3(3))
1988 elsif Is_Internal (Component_Type (T))
1989 and then Present (Subtype_Indication (Component_Definition (Def)))
1990 and then Nkind (Original_Node
1991 (Subtype_Indication (Component_Definition (Def)))) =
1992 N_Subtype_Indication
1993 then
1994 Error_Msg_N
1995 ("in a formal, a subtype indication can only be "
1996 & "a subtype mark (RM 12.5.3(3))",
1997 Subtype_Indication (Component_Definition (Def)));
1998 end if;
2000 end Analyze_Formal_Array_Type;
2002 ---------------------------------------------
2003 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2004 ---------------------------------------------
2006 -- As for other generic types, we create a valid type representation with
2007 -- legal but arbitrary attributes, whose values are never considered
2008 -- static. For all scalar types we introduce an anonymous base type, with
2009 -- the same attributes. We choose the corresponding integer type to be
2010 -- Standard_Integer.
2011 -- Here and in other similar routines, the Sloc of the generated internal
2012 -- type must be the same as the sloc of the defining identifier of the
2013 -- formal type declaration, to provide proper source navigation.
2015 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2016 (T : Entity_Id;
2017 Def : Node_Id)
2019 Loc : constant Source_Ptr := Sloc (Def);
2021 Base : constant Entity_Id :=
2022 New_Internal_Entity
2023 (E_Decimal_Fixed_Point_Type,
2024 Current_Scope,
2025 Sloc (Defining_Identifier (Parent (Def))), 'G');
2027 Int_Base : constant Entity_Id := Standard_Integer;
2028 Delta_Val : constant Ureal := Ureal_1;
2029 Digs_Val : constant Uint := Uint_6;
2031 function Make_Dummy_Bound return Node_Id;
2032 -- Return a properly typed universal real literal to use as a bound
2034 ----------------------
2035 -- Make_Dummy_Bound --
2036 ----------------------
2038 function Make_Dummy_Bound return Node_Id is
2039 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2040 begin
2041 Set_Etype (Bound, Universal_Real);
2042 return Bound;
2043 end Make_Dummy_Bound;
2045 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2047 begin
2048 Enter_Name (T);
2050 Set_Etype (Base, Base);
2051 Set_Size_Info (Base, Int_Base);
2052 Set_RM_Size (Base, RM_Size (Int_Base));
2053 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2054 Set_Digits_Value (Base, Digs_Val);
2055 Set_Delta_Value (Base, Delta_Val);
2056 Set_Small_Value (Base, Delta_Val);
2057 Set_Scalar_Range (Base,
2058 Make_Range (Loc,
2059 Low_Bound => Make_Dummy_Bound,
2060 High_Bound => Make_Dummy_Bound));
2062 Set_Is_Generic_Type (Base);
2063 Set_Parent (Base, Parent (Def));
2065 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2066 Set_Etype (T, Base);
2067 Set_Size_Info (T, Int_Base);
2068 Set_RM_Size (T, RM_Size (Int_Base));
2069 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2070 Set_Digits_Value (T, Digs_Val);
2071 Set_Delta_Value (T, Delta_Val);
2072 Set_Small_Value (T, Delta_Val);
2073 Set_Scalar_Range (T, Scalar_Range (Base));
2074 Set_Is_Constrained (T);
2076 Check_Restriction (No_Fixed_Point, Def);
2077 end Analyze_Formal_Decimal_Fixed_Point_Type;
2079 -------------------------------------------
2080 -- Analyze_Formal_Derived_Interface_Type --
2081 -------------------------------------------
2083 procedure Analyze_Formal_Derived_Interface_Type
2084 (N : Node_Id;
2085 T : Entity_Id;
2086 Def : Node_Id)
2088 Loc : constant Source_Ptr := Sloc (Def);
2090 begin
2091 -- Rewrite as a type declaration of a derived type. This ensures that
2092 -- the interface list and primitive operations are properly captured.
2094 Rewrite (N,
2095 Make_Full_Type_Declaration (Loc,
2096 Defining_Identifier => T,
2097 Type_Definition => Def));
2098 Analyze (N);
2099 Set_Is_Generic_Type (T);
2100 end Analyze_Formal_Derived_Interface_Type;
2102 ---------------------------------
2103 -- Analyze_Formal_Derived_Type --
2104 ---------------------------------
2106 procedure Analyze_Formal_Derived_Type
2107 (N : Node_Id;
2108 T : Entity_Id;
2109 Def : Node_Id)
2111 Loc : constant Source_Ptr := Sloc (Def);
2112 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2113 New_N : Node_Id;
2115 begin
2116 Set_Is_Generic_Type (T);
2118 if Private_Present (Def) then
2119 New_N :=
2120 Make_Private_Extension_Declaration (Loc,
2121 Defining_Identifier => T,
2122 Discriminant_Specifications => Discriminant_Specifications (N),
2123 Unknown_Discriminants_Present => Unk_Disc,
2124 Subtype_Indication => Subtype_Mark (Def),
2125 Interface_List => Interface_List (Def));
2127 Set_Abstract_Present (New_N, Abstract_Present (Def));
2128 Set_Limited_Present (New_N, Limited_Present (Def));
2129 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2131 else
2132 New_N :=
2133 Make_Full_Type_Declaration (Loc,
2134 Defining_Identifier => T,
2135 Discriminant_Specifications =>
2136 Discriminant_Specifications (Parent (T)),
2137 Type_Definition =>
2138 Make_Derived_Type_Definition (Loc,
2139 Subtype_Indication => Subtype_Mark (Def)));
2141 Set_Abstract_Present
2142 (Type_Definition (New_N), Abstract_Present (Def));
2143 Set_Limited_Present
2144 (Type_Definition (New_N), Limited_Present (Def));
2145 end if;
2147 Rewrite (N, New_N);
2148 Analyze (N);
2150 if Unk_Disc then
2151 if not Is_Composite_Type (T) then
2152 Error_Msg_N
2153 ("unknown discriminants not allowed for elementary types", N);
2154 else
2155 Set_Has_Unknown_Discriminants (T);
2156 Set_Is_Constrained (T, False);
2157 end if;
2158 end if;
2160 -- If the parent type has a known size, so does the formal, which makes
2161 -- legal representation clauses that involve the formal.
2163 Set_Size_Known_At_Compile_Time
2164 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2165 end Analyze_Formal_Derived_Type;
2167 ----------------------------------
2168 -- Analyze_Formal_Discrete_Type --
2169 ----------------------------------
2171 -- The operations defined for a discrete types are those of an enumeration
2172 -- type. The size is set to an arbitrary value, for use in analyzing the
2173 -- generic unit.
2175 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2176 Loc : constant Source_Ptr := Sloc (Def);
2177 Lo : Node_Id;
2178 Hi : Node_Id;
2180 Base : constant Entity_Id :=
2181 New_Internal_Entity
2182 (E_Floating_Point_Type, Current_Scope,
2183 Sloc (Defining_Identifier (Parent (Def))), 'G');
2185 begin
2186 Enter_Name (T);
2187 Set_Ekind (T, E_Enumeration_Subtype);
2188 Set_Etype (T, Base);
2189 Init_Size (T, 8);
2190 Init_Alignment (T);
2191 Set_Is_Generic_Type (T);
2192 Set_Is_Constrained (T);
2194 -- For semantic analysis, the bounds of the type must be set to some
2195 -- non-static value. The simplest is to create attribute nodes for those
2196 -- bounds, that refer to the type itself. These bounds are never
2197 -- analyzed but serve as place-holders.
2199 Lo :=
2200 Make_Attribute_Reference (Loc,
2201 Attribute_Name => Name_First,
2202 Prefix => New_Occurrence_Of (T, Loc));
2203 Set_Etype (Lo, T);
2205 Hi :=
2206 Make_Attribute_Reference (Loc,
2207 Attribute_Name => Name_Last,
2208 Prefix => New_Occurrence_Of (T, Loc));
2209 Set_Etype (Hi, T);
2211 Set_Scalar_Range (T,
2212 Make_Range (Loc,
2213 Low_Bound => Lo,
2214 High_Bound => Hi));
2216 Set_Ekind (Base, E_Enumeration_Type);
2217 Set_Etype (Base, Base);
2218 Init_Size (Base, 8);
2219 Init_Alignment (Base);
2220 Set_Is_Generic_Type (Base);
2221 Set_Scalar_Range (Base, Scalar_Range (T));
2222 Set_Parent (Base, Parent (Def));
2223 end Analyze_Formal_Discrete_Type;
2225 ----------------------------------
2226 -- Analyze_Formal_Floating_Type --
2227 ---------------------------------
2229 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2230 Base : constant Entity_Id :=
2231 New_Internal_Entity
2232 (E_Floating_Point_Type, Current_Scope,
2233 Sloc (Defining_Identifier (Parent (Def))), 'G');
2235 begin
2236 -- The various semantic attributes are taken from the predefined type
2237 -- Float, just so that all of them are initialized. Their values are
2238 -- never used because no constant folding or expansion takes place in
2239 -- the generic itself.
2241 Enter_Name (T);
2242 Set_Ekind (T, E_Floating_Point_Subtype);
2243 Set_Etype (T, Base);
2244 Set_Size_Info (T, (Standard_Float));
2245 Set_RM_Size (T, RM_Size (Standard_Float));
2246 Set_Digits_Value (T, Digits_Value (Standard_Float));
2247 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2248 Set_Is_Constrained (T);
2250 Set_Is_Generic_Type (Base);
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, (Standard_Float));
2253 Set_RM_Size (Base, RM_Size (Standard_Float));
2254 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2255 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2256 Set_Parent (Base, Parent (Def));
2258 Check_Restriction (No_Floating_Point, Def);
2259 end Analyze_Formal_Floating_Type;
2261 -----------------------------------
2262 -- Analyze_Formal_Interface_Type;--
2263 -----------------------------------
2265 procedure Analyze_Formal_Interface_Type
2266 (N : Node_Id;
2267 T : Entity_Id;
2268 Def : Node_Id)
2270 Loc : constant Source_Ptr := Sloc (N);
2271 New_N : Node_Id;
2273 begin
2274 New_N :=
2275 Make_Full_Type_Declaration (Loc,
2276 Defining_Identifier => T,
2277 Type_Definition => Def);
2279 Rewrite (N, New_N);
2280 Analyze (N);
2281 Set_Is_Generic_Type (T);
2282 end Analyze_Formal_Interface_Type;
2284 ---------------------------------
2285 -- Analyze_Formal_Modular_Type --
2286 ---------------------------------
2288 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2289 begin
2290 -- Apart from their entity kind, generic modular types are treated like
2291 -- signed integer types, and have the same attributes.
2293 Analyze_Formal_Signed_Integer_Type (T, Def);
2294 Set_Ekind (T, E_Modular_Integer_Subtype);
2295 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2297 end Analyze_Formal_Modular_Type;
2299 ---------------------------------------
2300 -- Analyze_Formal_Object_Declaration --
2301 ---------------------------------------
2303 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2304 E : constant Node_Id := Default_Expression (N);
2305 Id : constant Node_Id := Defining_Identifier (N);
2306 K : Entity_Kind;
2307 T : Node_Id;
2309 begin
2310 Enter_Name (Id);
2312 -- Determine the mode of the formal object
2314 if Out_Present (N) then
2315 K := E_Generic_In_Out_Parameter;
2317 if not In_Present (N) then
2318 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2319 end if;
2321 else
2322 K := E_Generic_In_Parameter;
2323 end if;
2325 if Present (Subtype_Mark (N)) then
2326 Find_Type (Subtype_Mark (N));
2327 T := Entity (Subtype_Mark (N));
2329 -- Verify that there is no redundant null exclusion
2331 if Null_Exclusion_Present (N) then
2332 if not Is_Access_Type (T) then
2333 Error_Msg_N
2334 ("null exclusion can only apply to an access type", N);
2336 elsif Can_Never_Be_Null (T) then
2337 Error_Msg_NE
2338 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2339 end if;
2340 end if;
2342 -- Ada 2005 (AI-423): Formal object with an access definition
2344 else
2345 Check_Access_Definition (N);
2346 T := Access_Definition
2347 (Related_Nod => N,
2348 N => Access_Definition (N));
2349 end if;
2351 if Ekind (T) = E_Incomplete_Type then
2352 declare
2353 Error_Node : Node_Id;
2355 begin
2356 if Present (Subtype_Mark (N)) then
2357 Error_Node := Subtype_Mark (N);
2358 else
2359 Check_Access_Definition (N);
2360 Error_Node := Access_Definition (N);
2361 end if;
2363 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2364 end;
2365 end if;
2367 if K = E_Generic_In_Parameter then
2369 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2371 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2372 Error_Msg_N
2373 ("generic formal of mode IN must not be of limited type", N);
2374 Explain_Limited_Type (T, N);
2375 end if;
2377 if Is_Abstract_Type (T) then
2378 Error_Msg_N
2379 ("generic formal of mode IN must not be of abstract type", N);
2380 end if;
2382 if Present (E) then
2383 Preanalyze_Spec_Expression (E, T);
2385 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2386 Error_Msg_N
2387 ("initialization not allowed for limited types", E);
2388 Explain_Limited_Type (T, E);
2389 end if;
2390 end if;
2392 Set_Ekind (Id, K);
2393 Set_Etype (Id, T);
2395 -- Case of generic IN OUT parameter
2397 else
2398 -- If the formal has an unconstrained type, construct its actual
2399 -- subtype, as is done for subprogram formals. In this fashion, all
2400 -- its uses can refer to specific bounds.
2402 Set_Ekind (Id, K);
2403 Set_Etype (Id, T);
2405 if (Is_Array_Type (T) and then not Is_Constrained (T))
2406 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2407 then
2408 declare
2409 Non_Freezing_Ref : constant Node_Id :=
2410 New_Occurrence_Of (Id, Sloc (Id));
2411 Decl : Node_Id;
2413 begin
2414 -- Make sure the actual subtype doesn't generate bogus freezing
2416 Set_Must_Not_Freeze (Non_Freezing_Ref);
2417 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2418 Insert_Before_And_Analyze (N, Decl);
2419 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2420 end;
2421 else
2422 Set_Actual_Subtype (Id, T);
2423 end if;
2425 if Present (E) then
2426 Error_Msg_N
2427 ("initialization not allowed for `IN OUT` formals", N);
2428 end if;
2429 end if;
2431 if Has_Aspects (N) then
2432 Analyze_Aspect_Specifications (N, Id);
2433 end if;
2434 end Analyze_Formal_Object_Declaration;
2436 ----------------------------------------------
2437 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2438 ----------------------------------------------
2440 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2441 (T : Entity_Id;
2442 Def : Node_Id)
2444 Loc : constant Source_Ptr := Sloc (Def);
2445 Base : constant Entity_Id :=
2446 New_Internal_Entity
2447 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2448 Sloc (Defining_Identifier (Parent (Def))), 'G');
2450 begin
2451 -- The semantic attributes are set for completeness only, their values
2452 -- will never be used, since all properties of the type are non-static.
2454 Enter_Name (T);
2455 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2456 Set_Etype (T, Base);
2457 Set_Size_Info (T, Standard_Integer);
2458 Set_RM_Size (T, RM_Size (Standard_Integer));
2459 Set_Small_Value (T, Ureal_1);
2460 Set_Delta_Value (T, Ureal_1);
2461 Set_Scalar_Range (T,
2462 Make_Range (Loc,
2463 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2464 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2465 Set_Is_Constrained (T);
2467 Set_Is_Generic_Type (Base);
2468 Set_Etype (Base, Base);
2469 Set_Size_Info (Base, Standard_Integer);
2470 Set_RM_Size (Base, RM_Size (Standard_Integer));
2471 Set_Small_Value (Base, Ureal_1);
2472 Set_Delta_Value (Base, Ureal_1);
2473 Set_Scalar_Range (Base, Scalar_Range (T));
2474 Set_Parent (Base, Parent (Def));
2476 Check_Restriction (No_Fixed_Point, Def);
2477 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2479 ----------------------------------------
2480 -- Analyze_Formal_Package_Declaration --
2481 ----------------------------------------
2483 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2484 Gen_Id : constant Node_Id := Name (N);
2485 Loc : constant Source_Ptr := Sloc (N);
2486 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2487 Formal : Entity_Id;
2488 Gen_Decl : Node_Id;
2489 Gen_Unit : Entity_Id;
2490 Renaming : Node_Id;
2492 Vis_Prims_List : Elist_Id := No_Elist;
2493 -- List of primitives made temporarily visible in the instantiation
2494 -- to match the visibility of the formal type.
2496 function Build_Local_Package return Node_Id;
2497 -- The formal package is rewritten so that its parameters are replaced
2498 -- with corresponding declarations. For parameters with bona fide
2499 -- associations these declarations are created by Analyze_Associations
2500 -- as for a regular instantiation. For boxed parameters, we preserve
2501 -- the formal declarations and analyze them, in order to introduce
2502 -- entities of the right kind in the environment of the formal.
2504 -------------------------
2505 -- Build_Local_Package --
2506 -------------------------
2508 function Build_Local_Package return Node_Id is
2509 Decls : List_Id;
2510 Pack_Decl : Node_Id;
2512 begin
2513 -- Within the formal, the name of the generic package is a renaming
2514 -- of the formal (as for a regular instantiation).
2516 Pack_Decl :=
2517 Make_Package_Declaration (Loc,
2518 Specification =>
2519 Copy_Generic_Node
2520 (Specification (Original_Node (Gen_Decl)),
2521 Empty, Instantiating => True));
2523 Renaming :=
2524 Make_Package_Renaming_Declaration (Loc,
2525 Defining_Unit_Name =>
2526 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2527 Name => New_Occurrence_Of (Formal, Loc));
2529 if Nkind (Gen_Id) = N_Identifier
2530 and then Chars (Gen_Id) = Chars (Pack_Id)
2531 then
2532 Error_Msg_NE
2533 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2534 end if;
2536 -- If the formal is declared with a box, or with an others choice,
2537 -- create corresponding declarations for all entities in the formal
2538 -- part, so that names with the proper types are available in the
2539 -- specification of the formal package.
2541 -- On the other hand, if there are no associations, then all the
2542 -- formals must have defaults, and this will be checked by the
2543 -- call to Analyze_Associations.
2545 if Box_Present (N)
2546 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2547 then
2548 declare
2549 Formal_Decl : Node_Id;
2551 begin
2552 -- TBA : for a formal package, need to recurse ???
2554 Decls := New_List;
2555 Formal_Decl :=
2556 First
2557 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2558 while Present (Formal_Decl) loop
2559 Append_To
2560 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2561 Next (Formal_Decl);
2562 end loop;
2563 end;
2565 -- If generic associations are present, use Analyze_Associations to
2566 -- create the proper renaming declarations.
2568 else
2569 declare
2570 Act_Tree : constant Node_Id :=
2571 Copy_Generic_Node
2572 (Original_Node (Gen_Decl), Empty,
2573 Instantiating => True);
2575 begin
2576 Generic_Renamings.Set_Last (0);
2577 Generic_Renamings_HTable.Reset;
2578 Instantiation_Node := N;
2580 Decls :=
2581 Analyze_Associations
2582 (I_Node => Original_Node (N),
2583 Formals => Generic_Formal_Declarations (Act_Tree),
2584 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2586 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2587 end;
2588 end if;
2590 Append (Renaming, To => Decls);
2592 -- Add generated declarations ahead of local declarations in
2593 -- the package.
2595 if No (Visible_Declarations (Specification (Pack_Decl))) then
2596 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2597 else
2598 Insert_List_Before
2599 (First (Visible_Declarations (Specification (Pack_Decl))),
2600 Decls);
2601 end if;
2603 return Pack_Decl;
2604 end Build_Local_Package;
2606 -- Local variables
2608 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2609 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2611 Associations : Boolean := True;
2612 New_N : Node_Id;
2613 Parent_Installed : Boolean := False;
2614 Parent_Instance : Entity_Id;
2615 Renaming_In_Par : Entity_Id;
2617 -- Start of processing for Analyze_Formal_Package_Declaration
2619 begin
2620 Check_Text_IO_Special_Unit (Gen_Id);
2622 Init_Env;
2623 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2624 Gen_Unit := Entity (Gen_Id);
2626 -- Check for a formal package that is a package renaming
2628 if Present (Renamed_Object (Gen_Unit)) then
2630 -- Indicate that unit is used, before replacing it with renamed
2631 -- entity for use below.
2633 if In_Extended_Main_Source_Unit (N) then
2634 Set_Is_Instantiated (Gen_Unit);
2635 Generate_Reference (Gen_Unit, N);
2636 end if;
2638 Gen_Unit := Renamed_Object (Gen_Unit);
2639 end if;
2641 if Ekind (Gen_Unit) /= E_Generic_Package then
2642 Error_Msg_N ("expect generic package name", Gen_Id);
2643 Restore_Env;
2644 goto Leave;
2646 elsif Gen_Unit = Current_Scope then
2647 Error_Msg_N
2648 ("generic package cannot be used as a formal package of itself",
2649 Gen_Id);
2650 Restore_Env;
2651 goto Leave;
2653 elsif In_Open_Scopes (Gen_Unit) then
2654 if Is_Compilation_Unit (Gen_Unit)
2655 and then Is_Child_Unit (Current_Scope)
2656 then
2657 -- Special-case the error when the formal is a parent, and
2658 -- continue analysis to minimize cascaded errors.
2660 Error_Msg_N
2661 ("generic parent cannot be used as formal package of a child "
2662 & "unit", Gen_Id);
2664 else
2665 Error_Msg_N
2666 ("generic package cannot be used as a formal package within "
2667 & "itself", Gen_Id);
2668 Restore_Env;
2669 goto Leave;
2670 end if;
2671 end if;
2673 -- Check that name of formal package does not hide name of generic,
2674 -- or its leading prefix. This check must be done separately because
2675 -- the name of the generic has already been analyzed.
2677 declare
2678 Gen_Name : Entity_Id;
2680 begin
2681 Gen_Name := Gen_Id;
2682 while Nkind (Gen_Name) = N_Expanded_Name loop
2683 Gen_Name := Prefix (Gen_Name);
2684 end loop;
2686 if Chars (Gen_Name) = Chars (Pack_Id) then
2687 Error_Msg_NE
2688 ("& is hidden within declaration of formal package",
2689 Gen_Id, Gen_Name);
2690 end if;
2691 end;
2693 if Box_Present (N)
2694 or else No (Generic_Associations (N))
2695 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2696 then
2697 Associations := False;
2698 end if;
2700 -- If there are no generic associations, the generic parameters appear
2701 -- as local entities and are instantiated like them. We copy the generic
2702 -- package declaration as if it were an instantiation, and analyze it
2703 -- like a regular package, except that we treat the formals as
2704 -- additional visible components.
2706 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2708 if In_Extended_Main_Source_Unit (N) then
2709 Set_Is_Instantiated (Gen_Unit);
2710 Generate_Reference (Gen_Unit, N);
2711 end if;
2713 Formal := New_Copy (Pack_Id);
2714 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2716 -- Make local generic without formals. The formals will be replaced with
2717 -- internal declarations.
2719 begin
2720 New_N := Build_Local_Package;
2722 -- If there are errors in the parameter list, Analyze_Associations
2723 -- raises Instantiation_Error. Patch the declaration to prevent further
2724 -- exception propagation.
2726 exception
2727 when Instantiation_Error =>
2728 Enter_Name (Formal);
2729 Set_Ekind (Formal, E_Variable);
2730 Set_Etype (Formal, Any_Type);
2731 Restore_Hidden_Primitives (Vis_Prims_List);
2733 if Parent_Installed then
2734 Remove_Parent;
2735 end if;
2737 goto Leave;
2738 end;
2740 Rewrite (N, New_N);
2741 Set_Defining_Unit_Name (Specification (New_N), Formal);
2742 Set_Generic_Parent (Specification (N), Gen_Unit);
2743 Set_Instance_Env (Gen_Unit, Formal);
2744 Set_Is_Generic_Instance (Formal);
2746 Enter_Name (Formal);
2747 Set_Ekind (Formal, E_Package);
2748 Set_Etype (Formal, Standard_Void_Type);
2749 Set_Inner_Instances (Formal, New_Elmt_List);
2750 Push_Scope (Formal);
2752 -- Manually set the SPARK_Mode from the context because the package
2753 -- declaration is never analyzed.
2755 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2756 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2757 Set_SPARK_Pragma_Inherited (Formal);
2758 Set_SPARK_Aux_Pragma_Inherited (Formal);
2760 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2762 -- Similarly, we have to make the name of the formal visible in the
2763 -- parent instance, to resolve properly fully qualified names that
2764 -- may appear in the generic unit. The parent instance has been
2765 -- placed on the scope stack ahead of the current scope.
2767 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2769 Renaming_In_Par :=
2770 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2771 Set_Ekind (Renaming_In_Par, E_Package);
2772 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2773 Set_Scope (Renaming_In_Par, Parent_Instance);
2774 Set_Parent (Renaming_In_Par, Parent (Formal));
2775 Set_Renamed_Object (Renaming_In_Par, Formal);
2776 Append_Entity (Renaming_In_Par, Parent_Instance);
2777 end if;
2779 -- A formal package declaration behaves as a package instantiation with
2780 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2781 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2782 -- all SPARK_Mode pragmas within the generic_package_name.
2784 if SPARK_Mode /= On then
2785 Ignore_Pragma_SPARK_Mode := True;
2786 end if;
2788 Analyze (Specification (N));
2790 -- The formals for which associations are provided are not visible
2791 -- outside of the formal package. The others are still declared by a
2792 -- formal parameter declaration.
2794 -- If there are no associations, the only local entity to hide is the
2795 -- generated package renaming itself.
2797 declare
2798 E : Entity_Id;
2800 begin
2801 E := First_Entity (Formal);
2802 while Present (E) loop
2803 if Associations and then not Is_Generic_Formal (E) then
2804 Set_Is_Hidden (E);
2805 end if;
2807 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2808 Set_Is_Hidden (E);
2809 exit;
2810 end if;
2812 Next_Entity (E);
2813 end loop;
2814 end;
2816 End_Package_Scope (Formal);
2817 Restore_Hidden_Primitives (Vis_Prims_List);
2819 if Parent_Installed then
2820 Remove_Parent;
2821 end if;
2823 Restore_Env;
2825 -- Inside the generic unit, the formal package is a regular package, but
2826 -- no body is needed for it. Note that after instantiation, the defining
2827 -- unit name we need is in the new tree and not in the original (see
2828 -- Package_Instantiation). A generic formal package is an instance, and
2829 -- can be used as an actual for an inner instance.
2831 Set_Has_Completion (Formal, True);
2833 -- Add semantic information to the original defining identifier for ASIS
2834 -- use.
2836 Set_Ekind (Pack_Id, E_Package);
2837 Set_Etype (Pack_Id, Standard_Void_Type);
2838 Set_Scope (Pack_Id, Scope (Formal));
2839 Set_Has_Completion (Pack_Id, True);
2841 <<Leave>>
2842 if Has_Aspects (N) then
2843 Analyze_Aspect_Specifications (N, Pack_Id);
2844 end if;
2846 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2847 end Analyze_Formal_Package_Declaration;
2849 ---------------------------------
2850 -- Analyze_Formal_Private_Type --
2851 ---------------------------------
2853 procedure Analyze_Formal_Private_Type
2854 (N : Node_Id;
2855 T : Entity_Id;
2856 Def : Node_Id)
2858 begin
2859 New_Private_Type (N, T, Def);
2861 -- Set the size to an arbitrary but legal value
2863 Set_Size_Info (T, Standard_Integer);
2864 Set_RM_Size (T, RM_Size (Standard_Integer));
2865 end Analyze_Formal_Private_Type;
2867 ------------------------------------
2868 -- Analyze_Formal_Incomplete_Type --
2869 ------------------------------------
2871 procedure Analyze_Formal_Incomplete_Type
2872 (T : Entity_Id;
2873 Def : Node_Id)
2875 begin
2876 Enter_Name (T);
2877 Set_Ekind (T, E_Incomplete_Type);
2878 Set_Etype (T, T);
2879 Set_Private_Dependents (T, New_Elmt_List);
2881 if Tagged_Present (Def) then
2882 Set_Is_Tagged_Type (T);
2883 Make_Class_Wide_Type (T);
2884 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2885 end if;
2886 end Analyze_Formal_Incomplete_Type;
2888 ----------------------------------------
2889 -- Analyze_Formal_Signed_Integer_Type --
2890 ----------------------------------------
2892 procedure Analyze_Formal_Signed_Integer_Type
2893 (T : Entity_Id;
2894 Def : Node_Id)
2896 Base : constant Entity_Id :=
2897 New_Internal_Entity
2898 (E_Signed_Integer_Type,
2899 Current_Scope,
2900 Sloc (Defining_Identifier (Parent (Def))), 'G');
2902 begin
2903 Enter_Name (T);
2905 Set_Ekind (T, E_Signed_Integer_Subtype);
2906 Set_Etype (T, Base);
2907 Set_Size_Info (T, Standard_Integer);
2908 Set_RM_Size (T, RM_Size (Standard_Integer));
2909 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2910 Set_Is_Constrained (T);
2912 Set_Is_Generic_Type (Base);
2913 Set_Size_Info (Base, Standard_Integer);
2914 Set_RM_Size (Base, RM_Size (Standard_Integer));
2915 Set_Etype (Base, Base);
2916 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2917 Set_Parent (Base, Parent (Def));
2918 end Analyze_Formal_Signed_Integer_Type;
2920 -------------------------------------------
2921 -- Analyze_Formal_Subprogram_Declaration --
2922 -------------------------------------------
2924 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2925 Spec : constant Node_Id := Specification (N);
2926 Def : constant Node_Id := Default_Name (N);
2927 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2928 Subp : Entity_Id;
2930 begin
2931 if Nam = Error then
2932 return;
2933 end if;
2935 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2936 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2937 goto Leave;
2938 end if;
2940 Analyze_Subprogram_Declaration (N);
2941 Set_Is_Formal_Subprogram (Nam);
2942 Set_Has_Completion (Nam);
2944 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2945 Set_Is_Abstract_Subprogram (Nam);
2947 Set_Is_Dispatching_Operation (Nam);
2949 -- A formal abstract procedure cannot have a null default
2950 -- (RM 12.6(4.1/2)).
2952 if Nkind (Spec) = N_Procedure_Specification
2953 and then Null_Present (Spec)
2954 then
2955 Error_Msg_N
2956 ("a formal abstract subprogram cannot default to null", Spec);
2957 end if;
2959 declare
2960 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2961 begin
2962 if No (Ctrl_Type) then
2963 Error_Msg_N
2964 ("abstract formal subprogram must have a controlling type",
2967 elsif Ada_Version >= Ada_2012
2968 and then Is_Incomplete_Type (Ctrl_Type)
2969 then
2970 Error_Msg_NE
2971 ("controlling type of abstract formal subprogram cannot "
2972 & "be incomplete type", N, Ctrl_Type);
2974 else
2975 Check_Controlling_Formals (Ctrl_Type, Nam);
2976 end if;
2977 end;
2978 end if;
2980 -- Default name is resolved at the point of instantiation
2982 if Box_Present (N) then
2983 null;
2985 -- Else default is bound at the point of generic declaration
2987 elsif Present (Def) then
2988 if Nkind (Def) = N_Operator_Symbol then
2989 Find_Direct_Name (Def);
2991 elsif Nkind (Def) /= N_Attribute_Reference then
2992 Analyze (Def);
2994 else
2995 -- For an attribute reference, analyze the prefix and verify
2996 -- that it has the proper profile for the subprogram.
2998 Analyze (Prefix (Def));
2999 Valid_Default_Attribute (Nam, Def);
3000 goto Leave;
3001 end if;
3003 -- Default name may be overloaded, in which case the interpretation
3004 -- with the correct profile must be selected, as for a renaming.
3005 -- If the definition is an indexed component, it must denote a
3006 -- member of an entry family. If it is a selected component, it
3007 -- can be a protected operation.
3009 if Etype (Def) = Any_Type then
3010 goto Leave;
3012 elsif Nkind (Def) = N_Selected_Component then
3013 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3014 Error_Msg_N ("expect valid subprogram name as default", Def);
3015 end if;
3017 elsif Nkind (Def) = N_Indexed_Component then
3018 if Is_Entity_Name (Prefix (Def)) then
3019 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3020 Error_Msg_N ("expect valid subprogram name as default", Def);
3021 end if;
3023 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3024 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3025 E_Entry_Family
3026 then
3027 Error_Msg_N ("expect valid subprogram name as default", Def);
3028 end if;
3030 else
3031 Error_Msg_N ("expect valid subprogram name as default", Def);
3032 goto Leave;
3033 end if;
3035 elsif Nkind (Def) = N_Character_Literal then
3037 -- Needs some type checks: subprogram should be parameterless???
3039 Resolve (Def, (Etype (Nam)));
3041 elsif not Is_Entity_Name (Def)
3042 or else not Is_Overloadable (Entity (Def))
3043 then
3044 Error_Msg_N ("expect valid subprogram name as default", Def);
3045 goto Leave;
3047 elsif not Is_Overloaded (Def) then
3048 Subp := Entity (Def);
3050 if Subp = Nam then
3051 Error_Msg_N ("premature usage of formal subprogram", Def);
3053 elsif not Entity_Matches_Spec (Subp, Nam) then
3054 Error_Msg_N ("no visible entity matches specification", Def);
3055 end if;
3057 -- More than one interpretation, so disambiguate as for a renaming
3059 else
3060 declare
3061 I : Interp_Index;
3062 I1 : Interp_Index := 0;
3063 It : Interp;
3064 It1 : Interp;
3066 begin
3067 Subp := Any_Id;
3068 Get_First_Interp (Def, I, It);
3069 while Present (It.Nam) loop
3070 if Entity_Matches_Spec (It.Nam, Nam) then
3071 if Subp /= Any_Id then
3072 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3074 if It1 = No_Interp then
3075 Error_Msg_N ("ambiguous default subprogram", Def);
3076 else
3077 Subp := It1.Nam;
3078 end if;
3080 exit;
3082 else
3083 I1 := I;
3084 Subp := It.Nam;
3085 end if;
3086 end if;
3088 Get_Next_Interp (I, It);
3089 end loop;
3090 end;
3092 if Subp /= Any_Id then
3094 -- Subprogram found, generate reference to it
3096 Set_Entity (Def, Subp);
3097 Generate_Reference (Subp, Def);
3099 if Subp = Nam then
3100 Error_Msg_N ("premature usage of formal subprogram", Def);
3102 elsif Ekind (Subp) /= E_Operator then
3103 Check_Mode_Conformant (Subp, Nam);
3104 end if;
3106 else
3107 Error_Msg_N ("no visible subprogram matches specification", N);
3108 end if;
3109 end if;
3110 end if;
3112 <<Leave>>
3113 if Has_Aspects (N) then
3114 Analyze_Aspect_Specifications (N, Nam);
3115 end if;
3117 end Analyze_Formal_Subprogram_Declaration;
3119 -------------------------------------
3120 -- Analyze_Formal_Type_Declaration --
3121 -------------------------------------
3123 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3124 Def : constant Node_Id := Formal_Type_Definition (N);
3125 T : Entity_Id;
3127 begin
3128 T := Defining_Identifier (N);
3130 if Present (Discriminant_Specifications (N))
3131 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3132 then
3133 Error_Msg_N
3134 ("discriminants not allowed for this formal type", T);
3135 end if;
3137 -- Enter the new name, and branch to specific routine
3139 case Nkind (Def) is
3140 when N_Formal_Private_Type_Definition =>
3141 Analyze_Formal_Private_Type (N, T, Def);
3143 when N_Formal_Derived_Type_Definition =>
3144 Analyze_Formal_Derived_Type (N, T, Def);
3146 when N_Formal_Incomplete_Type_Definition =>
3147 Analyze_Formal_Incomplete_Type (T, Def);
3149 when N_Formal_Discrete_Type_Definition =>
3150 Analyze_Formal_Discrete_Type (T, Def);
3152 when N_Formal_Signed_Integer_Type_Definition =>
3153 Analyze_Formal_Signed_Integer_Type (T, Def);
3155 when N_Formal_Modular_Type_Definition =>
3156 Analyze_Formal_Modular_Type (T, Def);
3158 when N_Formal_Floating_Point_Definition =>
3159 Analyze_Formal_Floating_Type (T, Def);
3161 when N_Formal_Ordinary_Fixed_Point_Definition =>
3162 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3164 when N_Formal_Decimal_Fixed_Point_Definition =>
3165 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3167 when N_Array_Type_Definition =>
3168 Analyze_Formal_Array_Type (T, Def);
3170 when N_Access_Function_Definition
3171 | N_Access_Procedure_Definition
3172 | N_Access_To_Object_Definition
3174 Analyze_Generic_Access_Type (T, Def);
3176 -- Ada 2005: a interface declaration is encoded as an abstract
3177 -- record declaration or a abstract type derivation.
3179 when N_Record_Definition =>
3180 Analyze_Formal_Interface_Type (N, T, Def);
3182 when N_Derived_Type_Definition =>
3183 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3185 when N_Error =>
3186 null;
3188 when others =>
3189 raise Program_Error;
3190 end case;
3192 Set_Is_Generic_Type (T);
3194 if Has_Aspects (N) then
3195 Analyze_Aspect_Specifications (N, T);
3196 end if;
3197 end Analyze_Formal_Type_Declaration;
3199 ------------------------------------
3200 -- Analyze_Function_Instantiation --
3201 ------------------------------------
3203 procedure Analyze_Function_Instantiation (N : Node_Id) is
3204 begin
3205 Analyze_Subprogram_Instantiation (N, E_Function);
3206 end Analyze_Function_Instantiation;
3208 ---------------------------------
3209 -- Analyze_Generic_Access_Type --
3210 ---------------------------------
3212 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3213 begin
3214 Enter_Name (T);
3216 if Nkind (Def) = N_Access_To_Object_Definition then
3217 Access_Type_Declaration (T, Def);
3219 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3220 and then No (Full_View (Designated_Type (T)))
3221 and then not Is_Generic_Type (Designated_Type (T))
3222 then
3223 Error_Msg_N ("premature usage of incomplete type", Def);
3225 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3226 Error_Msg_N
3227 ("only a subtype mark is allowed in a formal", Def);
3228 end if;
3230 else
3231 Access_Subprogram_Declaration (T, Def);
3232 end if;
3233 end Analyze_Generic_Access_Type;
3235 ---------------------------------
3236 -- Analyze_Generic_Formal_Part --
3237 ---------------------------------
3239 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3240 Gen_Parm_Decl : Node_Id;
3242 begin
3243 -- The generic formals are processed in the scope of the generic unit,
3244 -- where they are immediately visible. The scope is installed by the
3245 -- caller.
3247 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3248 while Present (Gen_Parm_Decl) loop
3249 Analyze (Gen_Parm_Decl);
3250 Next (Gen_Parm_Decl);
3251 end loop;
3253 Generate_Reference_To_Generic_Formals (Current_Scope);
3254 end Analyze_Generic_Formal_Part;
3256 ------------------------------------------
3257 -- Analyze_Generic_Package_Declaration --
3258 ------------------------------------------
3260 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3261 Loc : constant Source_Ptr := Sloc (N);
3262 Decls : constant List_Id :=
3263 Visible_Declarations (Specification (N));
3264 Decl : Node_Id;
3265 Id : Entity_Id;
3266 New_N : Node_Id;
3267 Renaming : Node_Id;
3268 Save_Parent : Node_Id;
3270 begin
3271 Check_SPARK_05_Restriction ("generic is not allowed", N);
3273 -- We introduce a renaming of the enclosing package, to have a usable
3274 -- entity as the prefix of an expanded name for a local entity of the
3275 -- form Par.P.Q, where P is the generic package. This is because a local
3276 -- entity named P may hide it, so that the usual visibility rules in
3277 -- the instance will not resolve properly.
3279 Renaming :=
3280 Make_Package_Renaming_Declaration (Loc,
3281 Defining_Unit_Name =>
3282 Make_Defining_Identifier (Loc,
3283 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3284 Name =>
3285 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3287 if Present (Decls) then
3288 Decl := First (Decls);
3289 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3290 Next (Decl);
3291 end loop;
3293 if Present (Decl) then
3294 Insert_Before (Decl, Renaming);
3295 else
3296 Append (Renaming, Visible_Declarations (Specification (N)));
3297 end if;
3299 else
3300 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3301 end if;
3303 -- Create copy of generic unit, and save for instantiation. If the unit
3304 -- is a child unit, do not copy the specifications for the parent, which
3305 -- are not part of the generic tree.
3307 Save_Parent := Parent_Spec (N);
3308 Set_Parent_Spec (N, Empty);
3310 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3311 Set_Parent_Spec (New_N, Save_Parent);
3312 Rewrite (N, New_N);
3314 -- Once the contents of the generic copy and the template are swapped,
3315 -- do the same for their respective aspect specifications.
3317 Exchange_Aspects (N, New_N);
3319 -- Collect all contract-related source pragmas found within the template
3320 -- and attach them to the contract of the package spec. This contract is
3321 -- used in the capture of global references within annotations.
3323 Create_Generic_Contract (N);
3325 Id := Defining_Entity (N);
3326 Generate_Definition (Id);
3328 -- Expansion is not applied to generic units
3330 Start_Generic;
3332 Enter_Name (Id);
3333 Set_Ekind (Id, E_Generic_Package);
3334 Set_Etype (Id, Standard_Void_Type);
3336 -- Analyze aspects now, so that generated pragmas appear in the
3337 -- declarations before building and analyzing the generic copy.
3339 if Has_Aspects (N) then
3340 Analyze_Aspect_Specifications (N, Id);
3341 end if;
3343 Push_Scope (Id);
3344 Enter_Generic_Scope (Id);
3345 Set_Inner_Instances (Id, New_Elmt_List);
3347 Set_Categorization_From_Pragmas (N);
3348 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3350 -- Link the declaration of the generic homonym in the generic copy to
3351 -- the package it renames, so that it is always resolved properly.
3353 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3354 Set_Entity (Associated_Node (Name (Renaming)), Id);
3356 -- For a library unit, we have reconstructed the entity for the unit,
3357 -- and must reset it in the library tables.
3359 if Nkind (Parent (N)) = N_Compilation_Unit then
3360 Set_Cunit_Entity (Current_Sem_Unit, Id);
3361 end if;
3363 Analyze_Generic_Formal_Part (N);
3365 -- After processing the generic formals, analysis proceeds as for a
3366 -- non-generic package.
3368 Analyze (Specification (N));
3370 Validate_Categorization_Dependency (N, Id);
3372 End_Generic;
3374 End_Package_Scope (Id);
3375 Exit_Generic_Scope (Id);
3377 if Nkind (Parent (N)) /= N_Compilation_Unit then
3378 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3379 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3380 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3382 else
3383 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3384 Validate_RT_RAT_Component (N);
3386 -- If this is a spec without a body, check that generic parameters
3387 -- are referenced.
3389 if not Body_Required (Parent (N)) then
3390 Check_References (Id);
3391 end if;
3392 end if;
3394 -- If there is a specified storage pool in the context, create an
3395 -- aspect on the package declaration, so that it is used in any
3396 -- instance that does not override it.
3398 if Present (Default_Pool) then
3399 declare
3400 ASN : Node_Id;
3402 begin
3403 ASN :=
3404 Make_Aspect_Specification (Loc,
3405 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3406 Expression => New_Copy (Default_Pool));
3408 if No (Aspect_Specifications (Specification (N))) then
3409 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3410 else
3411 Append (ASN, Aspect_Specifications (Specification (N)));
3412 end if;
3413 end;
3414 end if;
3415 end Analyze_Generic_Package_Declaration;
3417 --------------------------------------------
3418 -- Analyze_Generic_Subprogram_Declaration --
3419 --------------------------------------------
3421 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3422 Formals : List_Id;
3423 Id : Entity_Id;
3424 New_N : Node_Id;
3425 Result_Type : Entity_Id;
3426 Save_Parent : Node_Id;
3427 Spec : Node_Id;
3428 Typ : Entity_Id;
3430 begin
3431 Check_SPARK_05_Restriction ("generic is not allowed", N);
3433 -- Create copy of generic unit, and save for instantiation. If the unit
3434 -- is a child unit, do not copy the specifications for the parent, which
3435 -- are not part of the generic tree.
3437 Save_Parent := Parent_Spec (N);
3438 Set_Parent_Spec (N, Empty);
3440 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3441 Set_Parent_Spec (New_N, Save_Parent);
3442 Rewrite (N, New_N);
3444 -- Once the contents of the generic copy and the template are swapped,
3445 -- do the same for their respective aspect specifications.
3447 Exchange_Aspects (N, New_N);
3449 -- Collect all contract-related source pragmas found within the template
3450 -- and attach them to the contract of the subprogram spec. This contract
3451 -- is used in the capture of global references within annotations.
3453 Create_Generic_Contract (N);
3455 Spec := Specification (N);
3456 Id := Defining_Entity (Spec);
3457 Generate_Definition (Id);
3459 if Nkind (Id) = N_Defining_Operator_Symbol then
3460 Error_Msg_N
3461 ("operator symbol not allowed for generic subprogram", Id);
3462 end if;
3464 Start_Generic;
3466 Enter_Name (Id);
3467 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3469 -- Analyze the aspects of the generic copy to ensure that all generated
3470 -- pragmas (if any) perform their semantic effects.
3472 if Has_Aspects (N) then
3473 Analyze_Aspect_Specifications (N, Id);
3474 end if;
3476 Push_Scope (Id);
3477 Enter_Generic_Scope (Id);
3478 Set_Inner_Instances (Id, New_Elmt_List);
3479 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3481 Analyze_Generic_Formal_Part (N);
3483 Formals := Parameter_Specifications (Spec);
3485 if Nkind (Spec) = N_Function_Specification then
3486 Set_Ekind (Id, E_Generic_Function);
3487 else
3488 Set_Ekind (Id, E_Generic_Procedure);
3489 end if;
3491 if Present (Formals) then
3492 Process_Formals (Formals, Spec);
3493 end if;
3495 if Nkind (Spec) = N_Function_Specification then
3496 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3497 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3498 Set_Etype (Id, Result_Type);
3500 -- Check restriction imposed by AI05-073: a generic function
3501 -- cannot return an abstract type or an access to such.
3503 -- This is a binding interpretation should it apply to earlier
3504 -- versions of Ada as well as Ada 2012???
3506 if Is_Abstract_Type (Designated_Type (Result_Type))
3507 and then Ada_Version >= Ada_2012
3508 then
3509 Error_Msg_N
3510 ("generic function cannot have an access result "
3511 & "that designates an abstract type", Spec);
3512 end if;
3514 else
3515 Find_Type (Result_Definition (Spec));
3516 Typ := Entity (Result_Definition (Spec));
3518 if Is_Abstract_Type (Typ)
3519 and then Ada_Version >= Ada_2012
3520 then
3521 Error_Msg_N
3522 ("generic function cannot have abstract result type", Spec);
3523 end if;
3525 -- If a null exclusion is imposed on the result type, then create
3526 -- a null-excluding itype (an access subtype) and use it as the
3527 -- function's Etype.
3529 if Is_Access_Type (Typ)
3530 and then Null_Exclusion_Present (Spec)
3531 then
3532 Set_Etype (Id,
3533 Create_Null_Excluding_Itype
3534 (T => Typ,
3535 Related_Nod => Spec,
3536 Scope_Id => Defining_Unit_Name (Spec)));
3537 else
3538 Set_Etype (Id, Typ);
3539 end if;
3540 end if;
3542 else
3543 Set_Etype (Id, Standard_Void_Type);
3544 end if;
3546 -- For a library unit, we have reconstructed the entity for the unit,
3547 -- and must reset it in the library tables. We also make sure that
3548 -- Body_Required is set properly in the original compilation unit node.
3550 if Nkind (Parent (N)) = N_Compilation_Unit then
3551 Set_Cunit_Entity (Current_Sem_Unit, Id);
3552 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3553 end if;
3555 Set_Categorization_From_Pragmas (N);
3556 Validate_Categorization_Dependency (N, Id);
3558 -- Capture all global references that occur within the profile of the
3559 -- generic subprogram. Aspects are not part of this processing because
3560 -- they must be delayed. If processed now, Save_Global_References will
3561 -- destroy the Associated_Node links and prevent the capture of global
3562 -- references when the contract of the generic subprogram is analyzed.
3564 Save_Global_References (Original_Node (N));
3566 End_Generic;
3567 End_Scope;
3568 Exit_Generic_Scope (Id);
3569 Generate_Reference_To_Formals (Id);
3571 List_Inherited_Pre_Post_Aspects (Id);
3572 end Analyze_Generic_Subprogram_Declaration;
3574 -----------------------------------
3575 -- Analyze_Package_Instantiation --
3576 -----------------------------------
3578 -- WARNING: This routine manages Ghost regions. Return statements must be
3579 -- replaced by gotos which jump to the end of the routine and restore the
3580 -- Ghost mode.
3582 procedure Analyze_Package_Instantiation (N : Node_Id) is
3583 Loc : constant Source_Ptr := Sloc (N);
3584 Gen_Id : constant Node_Id := Name (N);
3586 Act_Decl : Node_Id;
3587 Act_Decl_Name : Node_Id;
3588 Act_Decl_Id : Entity_Id;
3589 Act_Spec : Node_Id;
3590 Act_Tree : Node_Id;
3592 Gen_Decl : Node_Id;
3593 Gen_Spec : Node_Id;
3594 Gen_Unit : Entity_Id;
3596 Is_Actual_Pack : constant Boolean :=
3597 Is_Internal (Defining_Entity (N));
3599 Env_Installed : Boolean := False;
3600 Parent_Installed : Boolean := False;
3601 Renaming_List : List_Id;
3602 Unit_Renaming : Node_Id;
3603 Needs_Body : Boolean;
3604 Inline_Now : Boolean := False;
3605 Has_Inline_Always : Boolean := False;
3607 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3608 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3610 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3611 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3612 -- Save the SPARK_Mode-related data for restore on exit
3614 Save_Style_Check : constant Boolean := Style_Check;
3615 -- Save style check mode for restore on exit
3617 procedure Delay_Descriptors (E : Entity_Id);
3618 -- Delay generation of subprogram descriptors for given entity
3620 function Might_Inline_Subp return Boolean;
3621 -- If inlining is active and the generic contains inlined subprograms,
3622 -- we instantiate the body. This may cause superfluous instantiations,
3623 -- but it is simpler than detecting the need for the body at the point
3624 -- of inlining, when the context of the instance is not available.
3626 -----------------------
3627 -- Delay_Descriptors --
3628 -----------------------
3630 procedure Delay_Descriptors (E : Entity_Id) is
3631 begin
3632 if not Delay_Subprogram_Descriptors (E) then
3633 Set_Delay_Subprogram_Descriptors (E);
3634 Pending_Descriptor.Append (E);
3635 end if;
3636 end Delay_Descriptors;
3638 -----------------------
3639 -- Might_Inline_Subp --
3640 -----------------------
3642 function Might_Inline_Subp return Boolean is
3643 E : Entity_Id;
3645 begin
3646 if not Inline_Processing_Required then
3647 return False;
3649 else
3650 E := First_Entity (Gen_Unit);
3651 while Present (E) loop
3652 if Is_Subprogram (E) and then Is_Inlined (E) then
3653 -- Remember if there are any subprograms with Inline_Always
3655 if Has_Pragma_Inline_Always (E) then
3656 Has_Inline_Always := True;
3657 end if;
3659 return True;
3660 end if;
3662 Next_Entity (E);
3663 end loop;
3664 end if;
3666 return False;
3667 end Might_Inline_Subp;
3669 -- Local declarations
3671 Mode : Ghost_Mode_Type;
3672 Mode_Set : Boolean := False;
3674 Vis_Prims_List : Elist_Id := No_Elist;
3675 -- List of primitives made temporarily visible in the instantiation
3676 -- to match the visibility of the formal type
3678 -- Start of processing for Analyze_Package_Instantiation
3680 begin
3681 Check_SPARK_05_Restriction ("generic is not allowed", N);
3683 -- Very first thing: check for Text_IO special unit in case we are
3684 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3686 Check_Text_IO_Special_Unit (Name (N));
3688 -- Make node global for error reporting
3690 Instantiation_Node := N;
3692 -- Case of instantiation of a generic package
3694 if Nkind (N) = N_Package_Instantiation then
3695 Act_Decl_Id := New_Copy (Defining_Entity (N));
3696 Set_Comes_From_Source (Act_Decl_Id, True);
3698 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3699 Act_Decl_Name :=
3700 Make_Defining_Program_Unit_Name (Loc,
3701 Name =>
3702 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3703 Defining_Identifier => Act_Decl_Id);
3704 else
3705 Act_Decl_Name := Act_Decl_Id;
3706 end if;
3708 -- Case of instantiation of a formal package
3710 else
3711 Act_Decl_Id := Defining_Identifier (N);
3712 Act_Decl_Name := Act_Decl_Id;
3713 end if;
3715 Generate_Definition (Act_Decl_Id);
3716 Set_Ekind (Act_Decl_Id, E_Package);
3718 -- Initialize list of incomplete actuals before analysis
3720 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3722 Preanalyze_Actuals (N, Act_Decl_Id);
3724 -- Turn off style checking in instances. If the check is enabled on the
3725 -- generic unit, a warning in an instance would just be noise. If not
3726 -- enabled on the generic, then a warning in an instance is just wrong.
3728 Style_Check := False;
3730 Init_Env;
3731 Env_Installed := True;
3733 -- Reset renaming map for formal types. The mapping is established
3734 -- when analyzing the generic associations, but some mappings are
3735 -- inherited from formal packages of parent units, and these are
3736 -- constructed when the parents are installed.
3738 Generic_Renamings.Set_Last (0);
3739 Generic_Renamings_HTable.Reset;
3741 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3742 Gen_Unit := Entity (Gen_Id);
3744 -- A package instantiation is Ghost when it is subject to pragma Ghost
3745 -- or the generic template is Ghost. Set the mode now to ensure that
3746 -- any nodes generated during analysis and expansion are marked as
3747 -- Ghost.
3749 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
3750 Mode_Set := True;
3752 -- Verify that it is the name of a generic package
3754 -- A visibility glitch: if the instance is a child unit and the generic
3755 -- is the generic unit of a parent instance (i.e. both the parent and
3756 -- the child units are instances of the same package) the name now
3757 -- denotes the renaming within the parent, not the intended generic
3758 -- unit. See if there is a homonym that is the desired generic. The
3759 -- renaming declaration must be visible inside the instance of the
3760 -- child, but not when analyzing the name in the instantiation itself.
3762 if Ekind (Gen_Unit) = E_Package
3763 and then Present (Renamed_Entity (Gen_Unit))
3764 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3765 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3766 and then Present (Homonym (Gen_Unit))
3767 then
3768 Gen_Unit := Homonym (Gen_Unit);
3769 end if;
3771 if Etype (Gen_Unit) = Any_Type then
3772 Restore_Env;
3773 goto Leave;
3775 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3777 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3779 if From_Limited_With (Gen_Unit) then
3780 Error_Msg_N
3781 ("cannot instantiate a limited withed package", Gen_Id);
3782 else
3783 Error_Msg_NE
3784 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3785 end if;
3787 Restore_Env;
3788 goto Leave;
3789 end if;
3791 if In_Extended_Main_Source_Unit (N) then
3792 Set_Is_Instantiated (Gen_Unit);
3793 Generate_Reference (Gen_Unit, N);
3795 if Present (Renamed_Object (Gen_Unit)) then
3796 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3797 Generate_Reference (Renamed_Object (Gen_Unit), N);
3798 end if;
3799 end if;
3801 if Nkind (Gen_Id) = N_Identifier
3802 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3803 then
3804 Error_Msg_NE
3805 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3807 elsif Nkind (Gen_Id) = N_Expanded_Name
3808 and then Is_Child_Unit (Gen_Unit)
3809 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3810 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3811 then
3812 Error_Msg_N
3813 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3814 end if;
3816 Set_Entity (Gen_Id, Gen_Unit);
3818 -- If generic is a renaming, get original generic unit
3820 if Present (Renamed_Object (Gen_Unit))
3821 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3822 then
3823 Gen_Unit := Renamed_Object (Gen_Unit);
3824 end if;
3826 -- Verify that there are no circular instantiations
3828 if In_Open_Scopes (Gen_Unit) then
3829 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3830 Restore_Env;
3831 goto Leave;
3833 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3834 Error_Msg_Node_2 := Current_Scope;
3835 Error_Msg_NE
3836 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3837 Circularity_Detected := True;
3838 Restore_Env;
3839 goto Leave;
3841 else
3842 -- If the context of the instance is subject to SPARK_Mode "off" or
3843 -- the annotation is altogether missing, set the global flag which
3844 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3845 -- the instance.
3847 if SPARK_Mode /= On then
3848 Ignore_Pragma_SPARK_Mode := True;
3849 end if;
3851 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3852 Gen_Spec := Specification (Gen_Decl);
3854 -- Initialize renamings map, for error checking, and the list that
3855 -- holds private entities whose views have changed between generic
3856 -- definition and instantiation. If this is the instance created to
3857 -- validate an actual package, the instantiation environment is that
3858 -- of the enclosing instance.
3860 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3862 -- Copy original generic tree, to produce text for instantiation
3864 Act_Tree :=
3865 Copy_Generic_Node
3866 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3868 Act_Spec := Specification (Act_Tree);
3870 -- If this is the instance created to validate an actual package,
3871 -- only the formals matter, do not examine the package spec itself.
3873 if Is_Actual_Pack then
3874 Set_Visible_Declarations (Act_Spec, New_List);
3875 Set_Private_Declarations (Act_Spec, New_List);
3876 end if;
3878 Renaming_List :=
3879 Analyze_Associations
3880 (I_Node => N,
3881 Formals => Generic_Formal_Declarations (Act_Tree),
3882 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3884 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3886 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3887 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3888 Set_Is_Generic_Instance (Act_Decl_Id);
3889 Set_Generic_Parent (Act_Spec, Gen_Unit);
3891 -- References to the generic in its own declaration or its body are
3892 -- references to the instance. Add a renaming declaration for the
3893 -- generic unit itself. This declaration, as well as the renaming
3894 -- declarations for the generic formals, must remain private to the
3895 -- unit: the formals, because this is the language semantics, and
3896 -- the unit because its use is an artifact of the implementation.
3898 Unit_Renaming :=
3899 Make_Package_Renaming_Declaration (Loc,
3900 Defining_Unit_Name =>
3901 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3902 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3904 Append (Unit_Renaming, Renaming_List);
3906 -- The renaming declarations are the first local declarations of the
3907 -- new unit.
3909 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3910 Insert_List_Before
3911 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3912 else
3913 Set_Visible_Declarations (Act_Spec, Renaming_List);
3914 end if;
3916 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3918 -- Propagate the aspect specifications from the package declaration
3919 -- template to the instantiated version of the package declaration.
3921 if Has_Aspects (Act_Tree) then
3922 Set_Aspect_Specifications (Act_Decl,
3923 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3924 end if;
3926 -- The generic may have a generated Default_Storage_Pool aspect,
3927 -- set at the point of generic declaration. If the instance has
3928 -- that aspect, it overrides the one inherited from the generic.
3930 if Has_Aspects (Gen_Spec) then
3931 if No (Aspect_Specifications (N)) then
3932 Set_Aspect_Specifications (N,
3933 (New_Copy_List_Tree
3934 (Aspect_Specifications (Gen_Spec))));
3936 else
3937 declare
3938 ASN1, ASN2 : Node_Id;
3940 begin
3941 ASN1 := First (Aspect_Specifications (N));
3942 while Present (ASN1) loop
3943 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3944 then
3945 -- If generic carries a default storage pool, remove
3946 -- it in favor of the instance one.
3948 ASN2 := First (Aspect_Specifications (Gen_Spec));
3949 while Present (ASN2) loop
3950 if Chars (Identifier (ASN2)) =
3951 Name_Default_Storage_Pool
3952 then
3953 Remove (ASN2);
3954 exit;
3955 end if;
3957 Next (ASN2);
3958 end loop;
3959 end if;
3961 Next (ASN1);
3962 end loop;
3964 Prepend_List_To (Aspect_Specifications (N),
3965 (New_Copy_List_Tree
3966 (Aspect_Specifications (Gen_Spec))));
3967 end;
3968 end if;
3969 end if;
3971 -- Save the instantiation node, for subsequent instantiation of the
3972 -- body, if there is one and we are generating code for the current
3973 -- unit. Mark unit as having a body (avoids premature error message).
3975 -- We instantiate the body if we are generating code, if we are
3976 -- generating cross-reference information, or if we are building
3977 -- trees for ASIS use or GNATprove use.
3979 declare
3980 Enclosing_Body_Present : Boolean := False;
3981 -- If the generic unit is not a compilation unit, then a body may
3982 -- be present in its parent even if none is required. We create a
3983 -- tentative pending instantiation for the body, which will be
3984 -- discarded if none is actually present.
3986 Scop : Entity_Id;
3988 begin
3989 if Scope (Gen_Unit) /= Standard_Standard
3990 and then not Is_Child_Unit (Gen_Unit)
3991 then
3992 Scop := Scope (Gen_Unit);
3993 while Present (Scop) and then Scop /= Standard_Standard loop
3994 if Unit_Requires_Body (Scop) then
3995 Enclosing_Body_Present := True;
3996 exit;
3998 elsif In_Open_Scopes (Scop)
3999 and then In_Package_Body (Scop)
4000 then
4001 Enclosing_Body_Present := True;
4002 exit;
4003 end if;
4005 exit when Is_Compilation_Unit (Scop);
4006 Scop := Scope (Scop);
4007 end loop;
4008 end if;
4010 -- If front-end inlining is enabled or there are any subprograms
4011 -- marked with Inline_Always, and this is a unit for which code
4012 -- will be generated, we instantiate the body at once.
4014 -- This is done if the instance is not the main unit, and if the
4015 -- generic is not a child unit of another generic, to avoid scope
4016 -- problems and the reinstallation of parent instances.
4018 if Expander_Active
4019 and then (not Is_Child_Unit (Gen_Unit)
4020 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4021 and then Might_Inline_Subp
4022 and then not Is_Actual_Pack
4023 then
4024 if not Back_End_Inlining
4025 and then (Front_End_Inlining or else Has_Inline_Always)
4026 and then (Is_In_Main_Unit (N)
4027 or else In_Main_Context (Current_Scope))
4028 and then Nkind (Parent (N)) /= N_Compilation_Unit
4029 then
4030 Inline_Now := True;
4032 -- In configurable_run_time mode we force the inlining of
4033 -- predefined subprograms marked Inline_Always, to minimize
4034 -- the use of the run-time library.
4036 elsif Is_Predefined_File_Name
4037 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4038 and then Configurable_Run_Time_Mode
4039 and then Nkind (Parent (N)) /= N_Compilation_Unit
4040 then
4041 Inline_Now := True;
4042 end if;
4044 -- If the current scope is itself an instance within a child
4045 -- unit, there will be duplications in the scope stack, and the
4046 -- unstacking mechanism in Inline_Instance_Body will fail.
4047 -- This loses some rare cases of optimization, and might be
4048 -- improved some day, if we can find a proper abstraction for
4049 -- "the complete compilation context" that can be saved and
4050 -- restored. ???
4052 if Is_Generic_Instance (Current_Scope) then
4053 declare
4054 Curr_Unit : constant Entity_Id :=
4055 Cunit_Entity (Current_Sem_Unit);
4056 begin
4057 if Curr_Unit /= Current_Scope
4058 and then Is_Child_Unit (Curr_Unit)
4059 then
4060 Inline_Now := False;
4061 end if;
4062 end;
4063 end if;
4064 end if;
4066 Needs_Body :=
4067 (Unit_Requires_Body (Gen_Unit)
4068 or else Enclosing_Body_Present
4069 or else Present (Corresponding_Body (Gen_Decl)))
4070 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4071 and then not Is_Actual_Pack
4072 and then not Inline_Now
4073 and then (Operating_Mode = Generate_Code
4075 -- Need comment for this check ???
4077 or else (Operating_Mode = Check_Semantics
4078 and then (ASIS_Mode or GNATprove_Mode)));
4080 -- If front-end inlining is enabled or there are any subprograms
4081 -- marked with Inline_Always, do not instantiate body when within
4082 -- a generic context.
4084 if ((Front_End_Inlining or else Has_Inline_Always)
4085 and then not Expander_Active)
4086 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4087 then
4088 Needs_Body := False;
4089 end if;
4091 -- If the current context is generic, and the package being
4092 -- instantiated is declared within a formal package, there is no
4093 -- body to instantiate until the enclosing generic is instantiated
4094 -- and there is an actual for the formal package. If the formal
4095 -- package has parameters, we build a regular package instance for
4096 -- it, that precedes the original formal package declaration.
4098 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4099 declare
4100 Decl : constant Node_Id :=
4101 Original_Node
4102 (Unit_Declaration_Node (Scope (Gen_Unit)));
4103 begin
4104 if Nkind (Decl) = N_Formal_Package_Declaration
4105 or else (Nkind (Decl) = N_Package_Declaration
4106 and then Is_List_Member (Decl)
4107 and then Present (Next (Decl))
4108 and then
4109 Nkind (Next (Decl)) =
4110 N_Formal_Package_Declaration)
4111 then
4112 Needs_Body := False;
4113 end if;
4114 end;
4115 end if;
4116 end;
4118 -- For RCI unit calling stubs, we omit the instance body if the
4119 -- instance is the RCI library unit itself.
4121 -- However there is a special case for nested instances: in this case
4122 -- we do generate the instance body, as it might be required, e.g.
4123 -- because it provides stream attributes for some type used in the
4124 -- profile of a remote subprogram. This is consistent with 12.3(12),
4125 -- which indicates that the instance body occurs at the place of the
4126 -- instantiation, and thus is part of the RCI declaration, which is
4127 -- present on all client partitions (this is E.2.3(18)).
4129 -- Note that AI12-0002 may make it illegal at some point to have
4130 -- stream attributes defined in an RCI unit, in which case this
4131 -- special case will become unnecessary. In the meantime, there
4132 -- is known application code in production that depends on this
4133 -- being possible, so we definitely cannot eliminate the body in
4134 -- the case of nested instances for the time being.
4136 -- When we generate a nested instance body, calling stubs for any
4137 -- relevant subprogram will be be inserted immediately after the
4138 -- subprogram declarations, and will take precedence over the
4139 -- subsequent (original) body. (The stub and original body will be
4140 -- complete homographs, but this is permitted in an instance).
4141 -- (Could we do better and remove the original body???)
4143 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4144 and then Comes_From_Source (N)
4145 and then Nkind (Parent (N)) = N_Compilation_Unit
4146 then
4147 Needs_Body := False;
4148 end if;
4150 if Needs_Body then
4152 -- Here is a defence against a ludicrous number of instantiations
4153 -- caused by a circular set of instantiation attempts.
4155 if Pending_Instantiations.Last > Maximum_Instantiations then
4156 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4157 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4158 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4159 raise Unrecoverable_Error;
4160 end if;
4162 -- Indicate that the enclosing scopes contain an instantiation,
4163 -- and that cleanup actions should be delayed until after the
4164 -- instance body is expanded.
4166 Check_Forward_Instantiation (Gen_Decl);
4167 if Nkind (N) = N_Package_Instantiation then
4168 declare
4169 Enclosing_Master : Entity_Id;
4171 begin
4172 -- Loop to search enclosing masters
4174 Enclosing_Master := Current_Scope;
4175 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4176 if Ekind (Enclosing_Master) = E_Package then
4177 if Is_Compilation_Unit (Enclosing_Master) then
4178 if In_Package_Body (Enclosing_Master) then
4179 Delay_Descriptors
4180 (Body_Entity (Enclosing_Master));
4181 else
4182 Delay_Descriptors
4183 (Enclosing_Master);
4184 end if;
4186 exit Scope_Loop;
4188 else
4189 Enclosing_Master := Scope (Enclosing_Master);
4190 end if;
4192 elsif Is_Generic_Unit (Enclosing_Master)
4193 or else Ekind (Enclosing_Master) = E_Void
4194 then
4195 -- Cleanup actions will eventually be performed on the
4196 -- enclosing subprogram or package instance, if any.
4197 -- Enclosing scope is void in the formal part of a
4198 -- generic subprogram.
4200 exit Scope_Loop;
4202 else
4203 if Ekind (Enclosing_Master) = E_Entry
4204 and then
4205 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4206 then
4207 if not Expander_Active then
4208 exit Scope_Loop;
4209 else
4210 Enclosing_Master :=
4211 Protected_Body_Subprogram (Enclosing_Master);
4212 end if;
4213 end if;
4215 Set_Delay_Cleanups (Enclosing_Master);
4217 while Ekind (Enclosing_Master) = E_Block loop
4218 Enclosing_Master := Scope (Enclosing_Master);
4219 end loop;
4221 if Is_Subprogram (Enclosing_Master) then
4222 Delay_Descriptors (Enclosing_Master);
4224 elsif Is_Task_Type (Enclosing_Master) then
4225 declare
4226 TBP : constant Node_Id :=
4227 Get_Task_Body_Procedure
4228 (Enclosing_Master);
4229 begin
4230 if Present (TBP) then
4231 Delay_Descriptors (TBP);
4232 Set_Delay_Cleanups (TBP);
4233 end if;
4234 end;
4235 end if;
4237 exit Scope_Loop;
4238 end if;
4239 end loop Scope_Loop;
4240 end;
4242 -- Make entry in table
4244 Add_Pending_Instantiation (N, Act_Decl);
4245 end if;
4246 end if;
4248 Set_Categorization_From_Pragmas (Act_Decl);
4250 if Parent_Installed then
4251 Hide_Current_Scope;
4252 end if;
4254 Set_Instance_Spec (N, Act_Decl);
4256 -- If not a compilation unit, insert the package declaration before
4257 -- the original instantiation node.
4259 if Nkind (Parent (N)) /= N_Compilation_Unit then
4260 Mark_Rewrite_Insertion (Act_Decl);
4261 Insert_Before (N, Act_Decl);
4263 if Has_Aspects (N) then
4264 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4266 -- The pragma created for a Default_Storage_Pool aspect must
4267 -- appear ahead of the declarations in the instance spec.
4268 -- Analysis has placed it after the instance node, so remove
4269 -- it and reinsert it properly now.
4271 declare
4272 ASN : constant Node_Id := First (Aspect_Specifications (N));
4273 A_Name : constant Name_Id := Chars (Identifier (ASN));
4274 Decl : Node_Id;
4276 begin
4277 if A_Name = Name_Default_Storage_Pool then
4278 if No (Visible_Declarations (Act_Spec)) then
4279 Set_Visible_Declarations (Act_Spec, New_List);
4280 end if;
4282 Decl := Next (N);
4283 while Present (Decl) loop
4284 if Nkind (Decl) = N_Pragma then
4285 Remove (Decl);
4286 Prepend (Decl, Visible_Declarations (Act_Spec));
4287 exit;
4288 end if;
4290 Next (Decl);
4291 end loop;
4292 end if;
4293 end;
4294 end if;
4296 Analyze (Act_Decl);
4298 -- For an instantiation that is a compilation unit, place
4299 -- declaration on current node so context is complete for analysis
4300 -- (including nested instantiations). If this is the main unit,
4301 -- the declaration eventually replaces the instantiation node.
4302 -- If the instance body is created later, it replaces the
4303 -- instance node, and the declaration is attached to it
4304 -- (see Build_Instance_Compilation_Unit_Nodes).
4306 else
4307 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4309 -- The entity for the current unit is the newly created one,
4310 -- and all semantic information is attached to it.
4312 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4314 -- If this is the main unit, replace the main entity as well
4316 if Current_Sem_Unit = Main_Unit then
4317 Main_Unit_Entity := Act_Decl_Id;
4318 end if;
4319 end if;
4321 Set_Unit (Parent (N), Act_Decl);
4322 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4323 Set_Package_Instantiation (Act_Decl_Id, N);
4325 -- Process aspect specifications of the instance node, if any, to
4326 -- take into account categorization pragmas before analyzing the
4327 -- instance.
4329 if Has_Aspects (N) then
4330 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4331 end if;
4333 Analyze (Act_Decl);
4334 Set_Unit (Parent (N), N);
4335 Set_Body_Required (Parent (N), False);
4337 -- We never need elaboration checks on instantiations, since by
4338 -- definition, the body instantiation is elaborated at the same
4339 -- time as the spec instantiation.
4341 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4342 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4343 end if;
4345 Check_Elab_Instantiation (N);
4347 if ABE_Is_Certain (N) and then Needs_Body then
4348 Pending_Instantiations.Decrement_Last;
4349 end if;
4351 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4353 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4354 First_Private_Entity (Act_Decl_Id));
4356 -- If the instantiation will receive a body, the unit will be
4357 -- transformed into a package body, and receive its own elaboration
4358 -- entity. Otherwise, the nature of the unit is now a package
4359 -- declaration.
4361 if Nkind (Parent (N)) = N_Compilation_Unit
4362 and then not Needs_Body
4363 then
4364 Rewrite (N, Act_Decl);
4365 end if;
4367 if Present (Corresponding_Body (Gen_Decl))
4368 or else Unit_Requires_Body (Gen_Unit)
4369 then
4370 Set_Has_Completion (Act_Decl_Id);
4371 end if;
4373 Check_Formal_Packages (Act_Decl_Id);
4375 Restore_Hidden_Primitives (Vis_Prims_List);
4376 Restore_Private_Views (Act_Decl_Id);
4378 Inherit_Context (Gen_Decl, N);
4380 if Parent_Installed then
4381 Remove_Parent;
4382 end if;
4384 Restore_Env;
4385 Env_Installed := False;
4386 end if;
4388 Validate_Categorization_Dependency (N, Act_Decl_Id);
4390 -- There used to be a check here to prevent instantiations in local
4391 -- contexts if the No_Local_Allocators restriction was active. This
4392 -- check was removed by a binding interpretation in AI-95-00130/07,
4393 -- but we retain the code for documentation purposes.
4395 -- if Ekind (Act_Decl_Id) /= E_Void
4396 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4397 -- then
4398 -- Check_Restriction (No_Local_Allocators, N);
4399 -- end if;
4401 if Inline_Now then
4402 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4403 end if;
4405 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4406 -- be used as defining identifiers for a formal package and for the
4407 -- corresponding expanded package.
4409 if Nkind (N) = N_Formal_Package_Declaration then
4410 Act_Decl_Id := New_Copy (Defining_Entity (N));
4411 Set_Comes_From_Source (Act_Decl_Id, True);
4412 Set_Is_Generic_Instance (Act_Decl_Id, False);
4413 Set_Defining_Identifier (N, Act_Decl_Id);
4414 end if;
4416 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4417 SPARK_Mode := Save_SM;
4418 SPARK_Mode_Pragma := Save_SMP;
4419 Style_Check := Save_Style_Check;
4421 -- Check that if N is an instantiation of System.Dim_Float_IO or
4422 -- System.Dim_Integer_IO, the formal type has a dimension system.
4424 if Nkind (N) = N_Package_Instantiation
4425 and then Is_Dim_IO_Package_Instantiation (N)
4426 then
4427 declare
4428 Assoc : constant Node_Id := First (Generic_Associations (N));
4429 begin
4430 if not Has_Dimension_System
4431 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4432 then
4433 Error_Msg_N ("type with a dimension system expected", Assoc);
4434 end if;
4435 end;
4436 end if;
4438 <<Leave>>
4439 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4440 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4441 end if;
4443 if Mode_Set then
4444 Restore_Ghost_Mode (Mode);
4445 end if;
4447 exception
4448 when Instantiation_Error =>
4449 if Parent_Installed then
4450 Remove_Parent;
4451 end if;
4453 if Env_Installed then
4454 Restore_Env;
4455 end if;
4457 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4458 SPARK_Mode := Save_SM;
4459 SPARK_Mode_Pragma := Save_SMP;
4460 Style_Check := Save_Style_Check;
4462 if Mode_Set then
4463 Restore_Ghost_Mode (Mode);
4464 end if;
4465 end Analyze_Package_Instantiation;
4467 --------------------------
4468 -- Inline_Instance_Body --
4469 --------------------------
4471 procedure Inline_Instance_Body
4472 (N : Node_Id;
4473 Gen_Unit : Entity_Id;
4474 Act_Decl : Node_Id)
4476 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4477 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4478 Gen_Comp : constant Entity_Id :=
4479 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4481 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4482 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4483 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4484 -- to provide a clean environment for analysis of the inlined body will
4485 -- eliminate any previously set SPARK_Mode.
4487 Scope_Stack_Depth : constant Pos :=
4488 Scope_Stack.Last - Scope_Stack.First + 1;
4490 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4491 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4492 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4493 Curr_Scope : Entity_Id := Empty;
4494 List : Elist_Id;
4495 Num_Inner : Nat := 0;
4496 Num_Scopes : Nat := 0;
4497 N_Instances : Nat := 0;
4498 Removed : Boolean := False;
4499 S : Entity_Id;
4500 Vis : Boolean;
4502 begin
4503 -- Case of generic unit defined in another unit. We must remove the
4504 -- complete context of the current unit to install that of the generic.
4506 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4508 -- Add some comments for the following two loops ???
4510 S := Current_Scope;
4511 while Present (S) and then S /= Standard_Standard loop
4512 loop
4513 Num_Scopes := Num_Scopes + 1;
4515 Use_Clauses (Num_Scopes) :=
4516 (Scope_Stack.Table
4517 (Scope_Stack.Last - Num_Scopes + 1).
4518 First_Use_Clause);
4519 End_Use_Clauses (Use_Clauses (Num_Scopes));
4521 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4522 or else Scope_Stack.Table
4523 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4524 end loop;
4526 exit when Is_Generic_Instance (S)
4527 and then (In_Package_Body (S)
4528 or else Ekind (S) = E_Procedure
4529 or else Ekind (S) = E_Function);
4530 S := Scope (S);
4531 end loop;
4533 Vis := Is_Immediately_Visible (Gen_Comp);
4535 -- Find and save all enclosing instances
4537 S := Current_Scope;
4539 while Present (S)
4540 and then S /= Standard_Standard
4541 loop
4542 if Is_Generic_Instance (S) then
4543 N_Instances := N_Instances + 1;
4544 Instances (N_Instances) := S;
4546 exit when In_Package_Body (S);
4547 end if;
4549 S := Scope (S);
4550 end loop;
4552 -- Remove context of current compilation unit, unless we are within a
4553 -- nested package instantiation, in which case the context has been
4554 -- removed previously.
4556 -- If current scope is the body of a child unit, remove context of
4557 -- spec as well. If an enclosing scope is an instance body, the
4558 -- context has already been removed, but the entities in the body
4559 -- must be made invisible as well.
4561 S := Current_Scope;
4562 while Present (S) and then S /= Standard_Standard loop
4563 if Is_Generic_Instance (S)
4564 and then (In_Package_Body (S)
4565 or else Ekind_In (S, E_Procedure, E_Function))
4566 then
4567 -- We still have to remove the entities of the enclosing
4568 -- instance from direct visibility.
4570 declare
4571 E : Entity_Id;
4572 begin
4573 E := First_Entity (S);
4574 while Present (E) loop
4575 Set_Is_Immediately_Visible (E, False);
4576 Next_Entity (E);
4577 end loop;
4578 end;
4580 exit;
4581 end if;
4583 if S = Curr_Unit
4584 or else (Ekind (Curr_Unit) = E_Package_Body
4585 and then S = Spec_Entity (Curr_Unit))
4586 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4587 and then S = Corresponding_Spec
4588 (Unit_Declaration_Node (Curr_Unit)))
4589 then
4590 Removed := True;
4592 -- Remove entities in current scopes from visibility, so that
4593 -- instance body is compiled in a clean environment.
4595 List := Save_Scope_Stack (Handle_Use => False);
4597 if Is_Child_Unit (S) then
4599 -- Remove child unit from stack, as well as inner scopes.
4600 -- Removing the context of a child unit removes parent units
4601 -- as well.
4603 while Current_Scope /= S loop
4604 Num_Inner := Num_Inner + 1;
4605 Inner_Scopes (Num_Inner) := Current_Scope;
4606 Pop_Scope;
4607 end loop;
4609 Pop_Scope;
4610 Remove_Context (Curr_Comp);
4611 Curr_Scope := S;
4613 else
4614 Remove_Context (Curr_Comp);
4615 end if;
4617 if Ekind (Curr_Unit) = E_Package_Body then
4618 Remove_Context (Library_Unit (Curr_Comp));
4619 end if;
4620 end if;
4622 S := Scope (S);
4623 end loop;
4625 pragma Assert (Num_Inner < Num_Scopes);
4627 -- The inlined package body must be analyzed with the SPARK_Mode of
4628 -- the enclosing context, otherwise the body may cause bogus errors
4629 -- if a configuration SPARK_Mode pragma in in effect.
4631 Push_Scope (Standard_Standard);
4632 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4633 Instantiate_Package_Body
4634 (Body_Info =>
4635 ((Inst_Node => N,
4636 Act_Decl => Act_Decl,
4637 Expander_Status => Expander_Active,
4638 Current_Sem_Unit => Current_Sem_Unit,
4639 Scope_Suppress => Scope_Suppress,
4640 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4641 Version => Ada_Version,
4642 Version_Pragma => Ada_Version_Pragma,
4643 Warnings => Save_Warnings,
4644 SPARK_Mode => Save_SM,
4645 SPARK_Mode_Pragma => Save_SMP)),
4646 Inlined_Body => True);
4648 Pop_Scope;
4650 -- Restore context
4652 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4654 -- Reset Generic_Instance flag so that use clauses can be installed
4655 -- in the proper order. (See Use_One_Package for effect of enclosing
4656 -- instances on processing of use clauses).
4658 for J in 1 .. N_Instances loop
4659 Set_Is_Generic_Instance (Instances (J), False);
4660 end loop;
4662 if Removed then
4663 Install_Context (Curr_Comp);
4665 if Present (Curr_Scope)
4666 and then Is_Child_Unit (Curr_Scope)
4667 then
4668 Push_Scope (Curr_Scope);
4669 Set_Is_Immediately_Visible (Curr_Scope);
4671 -- Finally, restore inner scopes as well
4673 for J in reverse 1 .. Num_Inner loop
4674 Push_Scope (Inner_Scopes (J));
4675 end loop;
4676 end if;
4678 Restore_Scope_Stack (List, Handle_Use => False);
4680 if Present (Curr_Scope)
4681 and then
4682 (In_Private_Part (Curr_Scope)
4683 or else In_Package_Body (Curr_Scope))
4684 then
4685 -- Install private declaration of ancestor units, which are
4686 -- currently available. Restore_Scope_Stack and Install_Context
4687 -- only install the visible part of parents.
4689 declare
4690 Par : Entity_Id;
4691 begin
4692 Par := Scope (Curr_Scope);
4693 while (Present (Par)) and then Par /= Standard_Standard loop
4694 Install_Private_Declarations (Par);
4695 Par := Scope (Par);
4696 end loop;
4697 end;
4698 end if;
4699 end if;
4701 -- Restore use clauses. For a child unit, use clauses in the parents
4702 -- are restored when installing the context, so only those in inner
4703 -- scopes (and those local to the child unit itself) need to be
4704 -- installed explicitly.
4706 if Is_Child_Unit (Curr_Unit) and then Removed then
4707 for J in reverse 1 .. Num_Inner + 1 loop
4708 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4709 Use_Clauses (J);
4710 Install_Use_Clauses (Use_Clauses (J));
4711 end loop;
4713 else
4714 for J in reverse 1 .. Num_Scopes loop
4715 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4716 Use_Clauses (J);
4717 Install_Use_Clauses (Use_Clauses (J));
4718 end loop;
4719 end if;
4721 -- Restore status of instances. If one of them is a body, make its
4722 -- local entities visible again.
4724 declare
4725 E : Entity_Id;
4726 Inst : Entity_Id;
4728 begin
4729 for J in 1 .. N_Instances loop
4730 Inst := Instances (J);
4731 Set_Is_Generic_Instance (Inst, True);
4733 if In_Package_Body (Inst)
4734 or else Ekind_In (S, E_Procedure, E_Function)
4735 then
4736 E := First_Entity (Instances (J));
4737 while Present (E) loop
4738 Set_Is_Immediately_Visible (E);
4739 Next_Entity (E);
4740 end loop;
4741 end if;
4742 end loop;
4743 end;
4745 -- If generic unit is in current unit, current context is correct. Note
4746 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4747 -- enclosing scopes were removed.
4749 else
4750 Instantiate_Package_Body
4751 (Body_Info =>
4752 ((Inst_Node => N,
4753 Act_Decl => Act_Decl,
4754 Expander_Status => Expander_Active,
4755 Current_Sem_Unit => Current_Sem_Unit,
4756 Scope_Suppress => Scope_Suppress,
4757 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4758 Version => Ada_Version,
4759 Version_Pragma => Ada_Version_Pragma,
4760 Warnings => Save_Warnings,
4761 SPARK_Mode => SPARK_Mode,
4762 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4763 Inlined_Body => True);
4764 end if;
4765 end Inline_Instance_Body;
4767 -------------------------------------
4768 -- Analyze_Procedure_Instantiation --
4769 -------------------------------------
4771 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4772 begin
4773 Analyze_Subprogram_Instantiation (N, E_Procedure);
4774 end Analyze_Procedure_Instantiation;
4776 -----------------------------------
4777 -- Need_Subprogram_Instance_Body --
4778 -----------------------------------
4780 function Need_Subprogram_Instance_Body
4781 (N : Node_Id;
4782 Subp : Entity_Id) return Boolean
4785 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4786 -- Return True if E is an inlined subprogram, an inlined renaming or a
4787 -- subprogram nested in an inlined subprogram. The inlining machinery
4788 -- totally disregards nested subprograms since it considers that they
4789 -- will always be compiled if the parent is (see Inline.Is_Nested).
4791 ------------------------------------
4792 -- Is_Inlined_Or_Child_Of_Inlined --
4793 ------------------------------------
4795 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4796 Scop : Entity_Id;
4798 begin
4799 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4800 return True;
4801 end if;
4803 Scop := Scope (E);
4804 while Scop /= Standard_Standard loop
4805 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4806 return True;
4807 end if;
4809 Scop := Scope (Scop);
4810 end loop;
4812 return False;
4813 end Is_Inlined_Or_Child_Of_Inlined;
4815 begin
4816 -- Must be in the main unit or inlined (or child of inlined)
4818 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4820 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4822 and then (Operating_Mode = Generate_Code
4823 or else (Operating_Mode = Check_Semantics
4824 and then (ASIS_Mode or GNATprove_Mode)))
4826 -- The body is needed when generating code (full expansion), in ASIS
4827 -- mode for other tools, and in GNATprove mode (special expansion) for
4828 -- formal verification of the body itself.
4830 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4832 -- No point in inlining if ABE is inevitable
4834 and then not ABE_Is_Certain (N)
4836 -- Or if subprogram is eliminated
4838 and then not Is_Eliminated (Subp)
4839 then
4840 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4841 return True;
4843 -- Here if not inlined, or we ignore the inlining
4845 else
4846 return False;
4847 end if;
4848 end Need_Subprogram_Instance_Body;
4850 --------------------------------------
4851 -- Analyze_Subprogram_Instantiation --
4852 --------------------------------------
4854 -- WARNING: This routine manages Ghost regions. Return statements must be
4855 -- replaced by gotos which jump to the end of the routine and restore the
4856 -- Ghost mode.
4858 procedure Analyze_Subprogram_Instantiation
4859 (N : Node_Id;
4860 K : Entity_Kind)
4862 Loc : constant Source_Ptr := Sloc (N);
4863 Gen_Id : constant Node_Id := Name (N);
4865 Anon_Id : constant Entity_Id :=
4866 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4867 Chars => New_External_Name
4868 (Chars (Defining_Entity (N)), 'R'));
4870 Act_Decl_Id : Entity_Id;
4871 Act_Decl : Node_Id;
4872 Act_Spec : Node_Id;
4873 Act_Tree : Node_Id;
4875 Env_Installed : Boolean := False;
4876 Gen_Unit : Entity_Id;
4877 Gen_Decl : Node_Id;
4878 Pack_Id : Entity_Id;
4879 Parent_Installed : Boolean := False;
4881 Renaming_List : List_Id;
4882 -- The list of declarations that link formals and actuals of the
4883 -- instance. These are subtype declarations for formal types, and
4884 -- renaming declarations for other formals. The subprogram declaration
4885 -- for the instance is then appended to the list, and the last item on
4886 -- the list is the renaming declaration for the instance.
4888 procedure Analyze_Instance_And_Renamings;
4889 -- The instance must be analyzed in a context that includes the mappings
4890 -- of generic parameters into actuals. We create a package declaration
4891 -- for this purpose, and a subprogram with an internal name within the
4892 -- package. The subprogram instance is simply an alias for the internal
4893 -- subprogram, declared in the current scope.
4895 procedure Build_Subprogram_Renaming;
4896 -- If the subprogram is recursive, there are occurrences of the name of
4897 -- the generic within the body, which must resolve to the current
4898 -- instance. We add a renaming declaration after the declaration, which
4899 -- is available in the instance body, as well as in the analysis of
4900 -- aspects that appear in the generic. This renaming declaration is
4901 -- inserted after the instance declaration which it renames.
4903 ------------------------------------
4904 -- Analyze_Instance_And_Renamings --
4905 ------------------------------------
4907 procedure Analyze_Instance_And_Renamings is
4908 Def_Ent : constant Entity_Id := Defining_Entity (N);
4909 Pack_Decl : Node_Id;
4911 begin
4912 if Nkind (Parent (N)) = N_Compilation_Unit then
4914 -- For the case of a compilation unit, the container package has
4915 -- the same name as the instantiation, to insure that the binder
4916 -- calls the elaboration procedure with the right name. Copy the
4917 -- entity of the instance, which may have compilation level flags
4918 -- (e.g. Is_Child_Unit) set.
4920 Pack_Id := New_Copy (Def_Ent);
4922 else
4923 -- Otherwise we use the name of the instantiation concatenated
4924 -- with its source position to ensure uniqueness if there are
4925 -- several instantiations with the same name.
4927 Pack_Id :=
4928 Make_Defining_Identifier (Loc,
4929 Chars => New_External_Name
4930 (Related_Id => Chars (Def_Ent),
4931 Suffix => "GP",
4932 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4933 end if;
4935 Pack_Decl :=
4936 Make_Package_Declaration (Loc,
4937 Specification => Make_Package_Specification (Loc,
4938 Defining_Unit_Name => Pack_Id,
4939 Visible_Declarations => Renaming_List,
4940 End_Label => Empty));
4942 Set_Instance_Spec (N, Pack_Decl);
4943 Set_Is_Generic_Instance (Pack_Id);
4944 Set_Debug_Info_Needed (Pack_Id);
4946 -- Case of not a compilation unit
4948 if Nkind (Parent (N)) /= N_Compilation_Unit then
4949 Mark_Rewrite_Insertion (Pack_Decl);
4950 Insert_Before (N, Pack_Decl);
4951 Set_Has_Completion (Pack_Id);
4953 -- Case of an instantiation that is a compilation unit
4955 -- Place declaration on current node so context is complete for
4956 -- analysis (including nested instantiations), and for use in a
4957 -- context_clause (see Analyze_With_Clause).
4959 else
4960 Set_Unit (Parent (N), Pack_Decl);
4961 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4962 end if;
4964 Analyze (Pack_Decl);
4965 Check_Formal_Packages (Pack_Id);
4966 Set_Is_Generic_Instance (Pack_Id, False);
4968 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4969 -- above???
4971 -- Body of the enclosing package is supplied when instantiating the
4972 -- subprogram body, after semantic analysis is completed.
4974 if Nkind (Parent (N)) = N_Compilation_Unit then
4976 -- Remove package itself from visibility, so it does not
4977 -- conflict with subprogram.
4979 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4981 -- Set name and scope of internal subprogram so that the proper
4982 -- external name will be generated. The proper scope is the scope
4983 -- of the wrapper package. We need to generate debugging info for
4984 -- the internal subprogram, so set flag accordingly.
4986 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4987 Set_Scope (Anon_Id, Scope (Pack_Id));
4989 -- Mark wrapper package as referenced, to avoid spurious warnings
4990 -- if the instantiation appears in various with_ clauses of
4991 -- subunits of the main unit.
4993 Set_Referenced (Pack_Id);
4994 end if;
4996 Set_Is_Generic_Instance (Anon_Id);
4997 Set_Debug_Info_Needed (Anon_Id);
4998 Act_Decl_Id := New_Copy (Anon_Id);
5000 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5001 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5002 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5004 -- Subprogram instance comes from source only if generic does
5006 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5008 -- If the instance is a child unit, mark the Id accordingly. Mark
5009 -- the anonymous entity as well, which is the real subprogram and
5010 -- which is used when the instance appears in a context clause.
5011 -- Similarly, propagate the Is_Eliminated flag to handle properly
5012 -- nested eliminated subprograms.
5014 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5015 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5016 New_Overloaded_Entity (Act_Decl_Id);
5017 Check_Eliminated (Act_Decl_Id);
5018 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5020 -- In compilation unit case, kill elaboration checks on the
5021 -- instantiation, since they are never needed -- the body is
5022 -- instantiated at the same point as the spec.
5024 if Nkind (Parent (N)) = N_Compilation_Unit then
5025 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5026 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5027 Set_Is_Compilation_Unit (Anon_Id);
5029 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5030 end if;
5032 -- The instance is not a freezing point for the new subprogram.
5033 -- The anonymous subprogram may have a freeze node, created for
5034 -- some delayed aspects. This freeze node must not be inherited
5035 -- by the visible subprogram entity.
5037 Set_Is_Frozen (Act_Decl_Id, False);
5038 Set_Freeze_Node (Act_Decl_Id, Empty);
5040 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5041 Valid_Operator_Definition (Act_Decl_Id);
5042 end if;
5044 Set_Alias (Act_Decl_Id, Anon_Id);
5045 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5046 Set_Has_Completion (Act_Decl_Id);
5047 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5049 if Nkind (Parent (N)) = N_Compilation_Unit then
5050 Set_Body_Required (Parent (N), False);
5051 end if;
5052 end Analyze_Instance_And_Renamings;
5054 -------------------------------
5055 -- Build_Subprogram_Renaming --
5056 -------------------------------
5058 procedure Build_Subprogram_Renaming is
5059 Renaming_Decl : Node_Id;
5060 Unit_Renaming : Node_Id;
5062 begin
5063 Unit_Renaming :=
5064 Make_Subprogram_Renaming_Declaration (Loc,
5065 Specification =>
5066 Copy_Generic_Node
5067 (Specification (Original_Node (Gen_Decl)),
5068 Empty,
5069 Instantiating => True),
5070 Name => New_Occurrence_Of (Anon_Id, Loc));
5072 -- The generic may be a a child unit. The renaming needs an
5073 -- identifier with the proper name.
5075 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5076 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5078 -- If there is a formal subprogram with the same name as the unit
5079 -- itself, do not add this renaming declaration, to prevent
5080 -- ambiguities when there is a call with that name in the body.
5081 -- This is a partial and ugly fix for one ACATS test. ???
5083 Renaming_Decl := First (Renaming_List);
5084 while Present (Renaming_Decl) loop
5085 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5086 and then
5087 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5088 then
5089 exit;
5090 end if;
5092 Next (Renaming_Decl);
5093 end loop;
5095 if No (Renaming_Decl) then
5096 Append (Unit_Renaming, Renaming_List);
5097 end if;
5098 end Build_Subprogram_Renaming;
5100 -- Local variables
5102 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5103 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5105 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5106 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5107 -- Save the SPARK_Mode-related data for restore on exit
5109 Mode : Ghost_Mode_Type;
5110 Mode_Set : Boolean := False;
5112 Vis_Prims_List : Elist_Id := No_Elist;
5113 -- List of primitives made temporarily visible in the instantiation
5114 -- to match the visibility of the formal type
5116 -- Start of processing for Analyze_Subprogram_Instantiation
5118 begin
5119 Check_SPARK_05_Restriction ("generic is not allowed", N);
5121 -- Very first thing: check for special Text_IO unit in case we are
5122 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5123 -- such an instantiation is bogus (these are packages, not subprograms),
5124 -- but we get a better error message if we do this.
5126 Check_Text_IO_Special_Unit (Gen_Id);
5128 -- Make node global for error reporting
5130 Instantiation_Node := N;
5132 -- For package instantiations we turn off style checks, because they
5133 -- will have been emitted in the generic. For subprogram instantiations
5134 -- we want to apply at least the check on overriding indicators so we
5135 -- do not modify the style check status.
5137 -- The renaming declarations for the actuals do not come from source and
5138 -- will not generate spurious warnings.
5140 Preanalyze_Actuals (N);
5142 Init_Env;
5143 Env_Installed := True;
5144 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5145 Gen_Unit := Entity (Gen_Id);
5147 -- A subprogram instantiation is Ghost when it is subject to pragma
5148 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5149 -- that any nodes generated during analysis and expansion are marked as
5150 -- Ghost.
5152 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
5153 Mode_Set := True;
5155 Generate_Reference (Gen_Unit, Gen_Id);
5157 if Nkind (Gen_Id) = N_Identifier
5158 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5159 then
5160 Error_Msg_NE
5161 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5162 end if;
5164 if Etype (Gen_Unit) = Any_Type then
5165 Restore_Env;
5166 goto Leave;
5167 end if;
5169 -- Verify that it is a generic subprogram of the right kind, and that
5170 -- it does not lead to a circular instantiation.
5172 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5173 Error_Msg_NE
5174 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5176 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5177 Error_Msg_NE
5178 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5180 elsif In_Open_Scopes (Gen_Unit) then
5181 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5183 else
5184 -- If the context of the instance is subject to SPARK_Mode "off" or
5185 -- the annotation is altogether missing, set the global flag which
5186 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5187 -- the instance.
5189 if SPARK_Mode /= On then
5190 Ignore_Pragma_SPARK_Mode := True;
5191 end if;
5193 Set_Entity (Gen_Id, Gen_Unit);
5194 Set_Is_Instantiated (Gen_Unit);
5196 if In_Extended_Main_Source_Unit (N) then
5197 Generate_Reference (Gen_Unit, N);
5198 end if;
5200 -- If renaming, get original unit
5202 if Present (Renamed_Object (Gen_Unit))
5203 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5204 E_Generic_Function)
5205 then
5206 Gen_Unit := Renamed_Object (Gen_Unit);
5207 Set_Is_Instantiated (Gen_Unit);
5208 Generate_Reference (Gen_Unit, N);
5209 end if;
5211 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5212 Error_Msg_Node_2 := Current_Scope;
5213 Error_Msg_NE
5214 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5215 Circularity_Detected := True;
5216 Restore_Hidden_Primitives (Vis_Prims_List);
5217 goto Leave;
5218 end if;
5220 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5222 -- Initialize renamings map, for error checking
5224 Generic_Renamings.Set_Last (0);
5225 Generic_Renamings_HTable.Reset;
5227 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5229 -- Copy original generic tree, to produce text for instantiation
5231 Act_Tree :=
5232 Copy_Generic_Node
5233 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5235 -- Inherit overriding indicator from instance node
5237 Act_Spec := Specification (Act_Tree);
5238 Set_Must_Override (Act_Spec, Must_Override (N));
5239 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5241 Renaming_List :=
5242 Analyze_Associations
5243 (I_Node => N,
5244 Formals => Generic_Formal_Declarations (Act_Tree),
5245 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5247 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5249 -- The subprogram itself cannot contain a nested instance, so the
5250 -- current parent is left empty.
5252 Set_Instance_Env (Gen_Unit, Empty);
5254 -- Build the subprogram declaration, which does not appear in the
5255 -- generic template, and give it a sloc consistent with that of the
5256 -- template.
5258 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5259 Set_Generic_Parent (Act_Spec, Gen_Unit);
5260 Act_Decl :=
5261 Make_Subprogram_Declaration (Sloc (Act_Spec),
5262 Specification => Act_Spec);
5264 -- The aspects have been copied previously, but they have to be
5265 -- linked explicitly to the new subprogram declaration. Explicit
5266 -- pre/postconditions on the instance are analyzed below, in a
5267 -- separate step.
5269 Move_Aspects (Act_Tree, To => Act_Decl);
5270 Set_Categorization_From_Pragmas (Act_Decl);
5272 if Parent_Installed then
5273 Hide_Current_Scope;
5274 end if;
5276 Append (Act_Decl, Renaming_List);
5278 -- Contract-related source pragmas that follow a generic subprogram
5279 -- must be instantiated explicitly because they are not part of the
5280 -- subprogram template.
5282 Instantiate_Subprogram_Contract
5283 (Original_Node (Gen_Decl), Renaming_List);
5285 Build_Subprogram_Renaming;
5286 Analyze_Instance_And_Renamings;
5288 -- If the generic is marked Import (Intrinsic), then so is the
5289 -- instance. This indicates that there is no body to instantiate. If
5290 -- generic is marked inline, so it the instance, and the anonymous
5291 -- subprogram it renames. If inlined, or else if inlining is enabled
5292 -- for the compilation, we generate the instance body even if it is
5293 -- not within the main unit.
5295 if Is_Intrinsic_Subprogram (Gen_Unit) then
5296 Set_Is_Intrinsic_Subprogram (Anon_Id);
5297 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5299 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5300 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5301 end if;
5302 end if;
5304 -- Inherit convention from generic unit. Intrinsic convention, as for
5305 -- an instance of unchecked conversion, is not inherited because an
5306 -- explicit Ada instance has been created.
5308 if Has_Convention_Pragma (Gen_Unit)
5309 and then Convention (Gen_Unit) /= Convention_Intrinsic
5310 then
5311 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5312 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5313 end if;
5315 Generate_Definition (Act_Decl_Id);
5317 -- Inherit all inlining-related flags which apply to the generic in
5318 -- the subprogram and its declaration.
5320 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5321 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5323 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5324 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5326 Set_Has_Pragma_Inline_Always
5327 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5328 Set_Has_Pragma_Inline_Always
5329 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5331 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5332 Check_Elab_Instantiation (N);
5333 end if;
5335 if Is_Dispatching_Operation (Act_Decl_Id)
5336 and then Ada_Version >= Ada_2005
5337 then
5338 declare
5339 Formal : Entity_Id;
5341 begin
5342 Formal := First_Formal (Act_Decl_Id);
5343 while Present (Formal) loop
5344 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5345 and then Is_Controlling_Formal (Formal)
5346 and then not Can_Never_Be_Null (Formal)
5347 then
5348 Error_Msg_NE
5349 ("access parameter& is controlling,", N, Formal);
5350 Error_Msg_NE
5351 ("\corresponding parameter of & must be explicitly "
5352 & "null-excluding", N, Gen_Id);
5353 end if;
5355 Next_Formal (Formal);
5356 end loop;
5357 end;
5358 end if;
5360 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5362 Validate_Categorization_Dependency (N, Act_Decl_Id);
5364 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5365 Inherit_Context (Gen_Decl, N);
5367 Restore_Private_Views (Pack_Id, False);
5369 -- If the context requires a full instantiation, mark node for
5370 -- subsequent construction of the body.
5372 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5373 Check_Forward_Instantiation (Gen_Decl);
5375 -- The wrapper package is always delayed, because it does not
5376 -- constitute a freeze point, but to insure that the freeze node
5377 -- is placed properly, it is created directly when instantiating
5378 -- the body (otherwise the freeze node might appear to early for
5379 -- nested instantiations). For ASIS purposes, indicate that the
5380 -- wrapper package has replaced the instantiation node.
5382 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5383 Rewrite (N, Unit (Parent (N)));
5384 Set_Unit (Parent (N), N);
5385 end if;
5387 -- Replace instance node for library-level instantiations of
5388 -- intrinsic subprograms, for ASIS use.
5390 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5391 Rewrite (N, Unit (Parent (N)));
5392 Set_Unit (Parent (N), N);
5393 end if;
5395 if Parent_Installed then
5396 Remove_Parent;
5397 end if;
5399 Restore_Hidden_Primitives (Vis_Prims_List);
5400 Restore_Env;
5401 Env_Installed := False;
5402 Generic_Renamings.Set_Last (0);
5403 Generic_Renamings_HTable.Reset;
5405 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5406 SPARK_Mode := Save_SM;
5407 SPARK_Mode_Pragma := Save_SMP;
5408 end if;
5410 <<Leave>>
5411 if Has_Aspects (N) then
5412 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5413 end if;
5415 if Mode_Set then
5416 Restore_Ghost_Mode (Mode);
5417 end if;
5419 exception
5420 when Instantiation_Error =>
5421 if Parent_Installed then
5422 Remove_Parent;
5423 end if;
5425 if Env_Installed then
5426 Restore_Env;
5427 end if;
5429 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5430 SPARK_Mode := Save_SM;
5431 SPARK_Mode_Pragma := Save_SMP;
5433 if Mode_Set then
5434 Restore_Ghost_Mode (Mode);
5435 end if;
5436 end Analyze_Subprogram_Instantiation;
5438 -------------------------
5439 -- Get_Associated_Node --
5440 -------------------------
5442 function Get_Associated_Node (N : Node_Id) return Node_Id is
5443 Assoc : Node_Id;
5445 begin
5446 Assoc := Associated_Node (N);
5448 if Nkind (Assoc) /= Nkind (N) then
5449 return Assoc;
5451 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5452 return Assoc;
5454 else
5455 -- If the node is part of an inner generic, it may itself have been
5456 -- remapped into a further generic copy. Associated_Node is otherwise
5457 -- used for the entity of the node, and will be of a different node
5458 -- kind, or else N has been rewritten as a literal or function call.
5460 while Present (Associated_Node (Assoc))
5461 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5462 loop
5463 Assoc := Associated_Node (Assoc);
5464 end loop;
5466 -- Follow and additional link in case the final node was rewritten.
5467 -- This can only happen with nested generic units.
5469 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5470 and then Present (Associated_Node (Assoc))
5471 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5472 N_Explicit_Dereference,
5473 N_Integer_Literal,
5474 N_Real_Literal,
5475 N_String_Literal))
5476 then
5477 Assoc := Associated_Node (Assoc);
5478 end if;
5480 -- An additional special case: an unconstrained type in an object
5481 -- declaration may have been rewritten as a local subtype constrained
5482 -- by the expression in the declaration. We need to recover the
5483 -- original entity which may be global.
5485 if Present (Original_Node (Assoc))
5486 and then Nkind (Parent (N)) = N_Object_Declaration
5487 then
5488 Assoc := Original_Node (Assoc);
5489 end if;
5491 return Assoc;
5492 end if;
5493 end Get_Associated_Node;
5495 ----------------------------
5496 -- Build_Function_Wrapper --
5497 ----------------------------
5499 function Build_Function_Wrapper
5500 (Formal_Subp : Entity_Id;
5501 Actual_Subp : Entity_Id) return Node_Id
5503 Loc : constant Source_Ptr := Sloc (Current_Scope);
5504 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5505 Actuals : List_Id;
5506 Decl : Node_Id;
5507 Func_Name : Node_Id;
5508 Func : Entity_Id;
5509 Parm_Type : Node_Id;
5510 Profile : List_Id := New_List;
5511 Spec : Node_Id;
5512 Act_F : Entity_Id;
5513 Form_F : Entity_Id;
5514 New_F : Entity_Id;
5516 begin
5517 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5519 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5520 Set_Ekind (Func, E_Function);
5521 Set_Is_Generic_Actual_Subprogram (Func);
5523 Actuals := New_List;
5524 Profile := New_List;
5526 Act_F := First_Formal (Actual_Subp);
5527 Form_F := First_Formal (Formal_Subp);
5528 while Present (Form_F) loop
5530 -- Create new formal for profile of wrapper, and add a reference
5531 -- to it in the list of actuals for the enclosing call. The name
5532 -- must be that of the formal in the formal subprogram, because
5533 -- calls to it in the generic body may use named associations.
5535 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5537 Parm_Type :=
5538 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5540 Append_To (Profile,
5541 Make_Parameter_Specification (Loc,
5542 Defining_Identifier => New_F,
5543 Parameter_Type => Parm_Type));
5545 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5546 Next_Formal (Form_F);
5548 if Present (Act_F) then
5549 Next_Formal (Act_F);
5550 end if;
5551 end loop;
5553 Spec :=
5554 Make_Function_Specification (Loc,
5555 Defining_Unit_Name => Func,
5556 Parameter_Specifications => Profile,
5557 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5559 Decl :=
5560 Make_Expression_Function (Loc,
5561 Specification => Spec,
5562 Expression =>
5563 Make_Function_Call (Loc,
5564 Name => Func_Name,
5565 Parameter_Associations => Actuals));
5567 return Decl;
5568 end Build_Function_Wrapper;
5570 ----------------------------
5571 -- Build_Operator_Wrapper --
5572 ----------------------------
5574 function Build_Operator_Wrapper
5575 (Formal_Subp : Entity_Id;
5576 Actual_Subp : Entity_Id) return Node_Id
5578 Loc : constant Source_Ptr := Sloc (Current_Scope);
5579 Ret_Type : constant Entity_Id :=
5580 Get_Instance_Of (Etype (Formal_Subp));
5581 Op_Type : constant Entity_Id :=
5582 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5583 Is_Binary : constant Boolean :=
5584 Present (Next_Formal (First_Formal (Formal_Subp)));
5586 Decl : Node_Id;
5587 Expr : Node_Id;
5588 F1, F2 : Entity_Id;
5589 Func : Entity_Id;
5590 Op_Name : Name_Id;
5591 Spec : Node_Id;
5592 L, R : Node_Id;
5594 begin
5595 Op_Name := Chars (Actual_Subp);
5597 -- Create entities for wrapper function and its formals
5599 F1 := Make_Temporary (Loc, 'A');
5600 F2 := Make_Temporary (Loc, 'B');
5601 L := New_Occurrence_Of (F1, Loc);
5602 R := New_Occurrence_Of (F2, Loc);
5604 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5605 Set_Ekind (Func, E_Function);
5606 Set_Is_Generic_Actual_Subprogram (Func);
5608 Spec :=
5609 Make_Function_Specification (Loc,
5610 Defining_Unit_Name => Func,
5611 Parameter_Specifications => New_List (
5612 Make_Parameter_Specification (Loc,
5613 Defining_Identifier => F1,
5614 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5615 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5617 if Is_Binary then
5618 Append_To (Parameter_Specifications (Spec),
5619 Make_Parameter_Specification (Loc,
5620 Defining_Identifier => F2,
5621 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5622 end if;
5624 -- Build expression as a function call, or as an operator node
5625 -- that corresponds to the name of the actual, starting with
5626 -- binary operators.
5628 if Op_Name not in Any_Operator_Name then
5629 Expr :=
5630 Make_Function_Call (Loc,
5631 Name =>
5632 New_Occurrence_Of (Actual_Subp, Loc),
5633 Parameter_Associations => New_List (L));
5635 if Is_Binary then
5636 Append_To (Parameter_Associations (Expr), R);
5637 end if;
5639 -- Binary operators
5641 elsif Is_Binary then
5642 if Op_Name = Name_Op_And then
5643 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5644 elsif Op_Name = Name_Op_Or then
5645 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5646 elsif Op_Name = Name_Op_Xor then
5647 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5648 elsif Op_Name = Name_Op_Eq then
5649 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5650 elsif Op_Name = Name_Op_Ne then
5651 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5652 elsif Op_Name = Name_Op_Le then
5653 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5654 elsif Op_Name = Name_Op_Gt then
5655 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5656 elsif Op_Name = Name_Op_Ge then
5657 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5658 elsif Op_Name = Name_Op_Lt then
5659 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5660 elsif Op_Name = Name_Op_Add then
5661 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5662 elsif Op_Name = Name_Op_Subtract then
5663 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5664 elsif Op_Name = Name_Op_Concat then
5665 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5666 elsif Op_Name = Name_Op_Multiply then
5667 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5668 elsif Op_Name = Name_Op_Divide then
5669 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5670 elsif Op_Name = Name_Op_Mod then
5671 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5672 elsif Op_Name = Name_Op_Rem then
5673 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5674 elsif Op_Name = Name_Op_Expon then
5675 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5676 end if;
5678 -- Unary operators
5680 else
5681 if Op_Name = Name_Op_Add then
5682 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5683 elsif Op_Name = Name_Op_Subtract then
5684 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5685 elsif Op_Name = Name_Op_Abs then
5686 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5687 elsif Op_Name = Name_Op_Not then
5688 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5689 end if;
5690 end if;
5692 Decl :=
5693 Make_Expression_Function (Loc,
5694 Specification => Spec,
5695 Expression => Expr);
5697 return Decl;
5698 end Build_Operator_Wrapper;
5700 -------------------------------------------
5701 -- Build_Instance_Compilation_Unit_Nodes --
5702 -------------------------------------------
5704 procedure Build_Instance_Compilation_Unit_Nodes
5705 (N : Node_Id;
5706 Act_Body : Node_Id;
5707 Act_Decl : Node_Id)
5709 Decl_Cunit : Node_Id;
5710 Body_Cunit : Node_Id;
5711 Citem : Node_Id;
5712 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5713 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5715 begin
5716 -- A new compilation unit node is built for the instance declaration
5718 Decl_Cunit :=
5719 Make_Compilation_Unit (Sloc (N),
5720 Context_Items => Empty_List,
5721 Unit => Act_Decl,
5722 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5724 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5726 -- The new compilation unit is linked to its body, but both share the
5727 -- same file, so we do not set Body_Required on the new unit so as not
5728 -- to create a spurious dependency on a non-existent body in the ali.
5729 -- This simplifies CodePeer unit traversal.
5731 -- We use the original instantiation compilation unit as the resulting
5732 -- compilation unit of the instance, since this is the main unit.
5734 Rewrite (N, Act_Body);
5736 -- Propagate the aspect specifications from the package body template to
5737 -- the instantiated version of the package body.
5739 if Has_Aspects (Act_Body) then
5740 Set_Aspect_Specifications
5741 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5742 end if;
5744 Body_Cunit := Parent (N);
5746 -- The two compilation unit nodes are linked by the Library_Unit field
5748 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5749 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5751 -- Preserve the private nature of the package if needed
5753 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5755 -- If the instance is not the main unit, its context, categorization
5756 -- and elaboration entity are not relevant to the compilation.
5758 if Body_Cunit /= Cunit (Main_Unit) then
5759 Make_Instance_Unit (Body_Cunit, In_Main => False);
5760 return;
5761 end if;
5763 -- The context clause items on the instantiation, which are now attached
5764 -- to the body compilation unit (since the body overwrote the original
5765 -- instantiation node), semantically belong on the spec, so copy them
5766 -- there. It's harmless to leave them on the body as well. In fact one
5767 -- could argue that they belong in both places.
5769 Citem := First (Context_Items (Body_Cunit));
5770 while Present (Citem) loop
5771 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5772 Next (Citem);
5773 end loop;
5775 -- Propagate categorization flags on packages, so that they appear in
5776 -- the ali file for the spec of the unit.
5778 if Ekind (New_Main) = E_Package then
5779 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5780 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5781 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5782 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5783 Set_Is_Remote_Call_Interface
5784 (Old_Main, Is_Remote_Call_Interface (New_Main));
5785 end if;
5787 -- Make entry in Units table, so that binder can generate call to
5788 -- elaboration procedure for body, if any.
5790 Make_Instance_Unit (Body_Cunit, In_Main => True);
5791 Main_Unit_Entity := New_Main;
5792 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5794 -- Build elaboration entity, since the instance may certainly generate
5795 -- elaboration code requiring a flag for protection.
5797 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5798 end Build_Instance_Compilation_Unit_Nodes;
5800 -----------------------------
5801 -- Check_Access_Definition --
5802 -----------------------------
5804 procedure Check_Access_Definition (N : Node_Id) is
5805 begin
5806 pragma Assert
5807 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5808 null;
5809 end Check_Access_Definition;
5811 -----------------------------------
5812 -- Check_Formal_Package_Instance --
5813 -----------------------------------
5815 -- If the formal has specific parameters, they must match those of the
5816 -- actual. Both of them are instances, and the renaming declarations for
5817 -- their formal parameters appear in the same order in both. The analyzed
5818 -- formal has been analyzed in the context of the current instance.
5820 procedure Check_Formal_Package_Instance
5821 (Formal_Pack : Entity_Id;
5822 Actual_Pack : Entity_Id)
5824 E1 : Entity_Id := First_Entity (Actual_Pack);
5825 E2 : Entity_Id := First_Entity (Formal_Pack);
5826 Prev_E1 : Entity_Id;
5828 Expr1 : Node_Id;
5829 Expr2 : Node_Id;
5831 procedure Check_Mismatch (B : Boolean);
5832 -- Common error routine for mismatch between the parameters of the
5833 -- actual instance and those of the formal package.
5835 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5836 -- The formal may come from a nested formal package, and the actual may
5837 -- have been constant-folded. To determine whether the two denote the
5838 -- same entity we may have to traverse several definitions to recover
5839 -- the ultimate entity that they refer to.
5841 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5842 -- The formal and the actual must be identical, but if both are
5843 -- given by attributes they end up renaming different generated bodies,
5844 -- and we must verify that the attributes themselves match.
5846 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5847 -- Similarly, if the formal comes from a nested formal package, the
5848 -- actual may designate the formal through multiple renamings, which
5849 -- have to be followed to determine the original variable in question.
5851 --------------------
5852 -- Check_Mismatch --
5853 --------------------
5855 procedure Check_Mismatch (B : Boolean) is
5856 -- A Formal_Type_Declaration for a derived private type is rewritten
5857 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5858 -- which is why we examine the original node.
5860 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5862 begin
5863 if Kind = N_Formal_Type_Declaration then
5864 return;
5866 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5867 N_Formal_Package_Declaration)
5868 or else Kind in N_Formal_Subprogram_Declaration
5869 then
5870 null;
5872 -- Ada 2012: If both formal and actual are incomplete types they
5873 -- are conformant.
5875 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5876 null;
5878 elsif B then
5879 Error_Msg_NE
5880 ("actual for & in actual instance does not match formal",
5881 Parent (Actual_Pack), E1);
5882 end if;
5883 end Check_Mismatch;
5885 --------------------------------
5886 -- Same_Instantiated_Constant --
5887 --------------------------------
5889 function Same_Instantiated_Constant
5890 (E1, E2 : Entity_Id) return Boolean
5892 Ent : Entity_Id;
5894 begin
5895 Ent := E2;
5896 while Present (Ent) loop
5897 if E1 = Ent then
5898 return True;
5900 elsif Ekind (Ent) /= E_Constant then
5901 return False;
5903 elsif Is_Entity_Name (Constant_Value (Ent)) then
5904 if Entity (Constant_Value (Ent)) = E1 then
5905 return True;
5906 else
5907 Ent := Entity (Constant_Value (Ent));
5908 end if;
5910 -- The actual may be a constant that has been folded. Recover
5911 -- original name.
5913 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5914 Ent := Entity (Original_Node (Constant_Value (Ent)));
5916 else
5917 return False;
5918 end if;
5919 end loop;
5921 return False;
5922 end Same_Instantiated_Constant;
5924 --------------------------------
5925 -- Same_Instantiated_Function --
5926 --------------------------------
5928 function Same_Instantiated_Function
5929 (E1, E2 : Entity_Id) return Boolean
5931 U1, U2 : Node_Id;
5932 begin
5933 if Alias (E1) = Alias (E2) then
5934 return True;
5936 elsif Present (Alias (E2)) then
5937 U1 := Original_Node (Unit_Declaration_Node (E1));
5938 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5940 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5941 and then Nkind (Name (U1)) = N_Attribute_Reference
5943 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5944 and then Nkind (Name (U2)) = N_Attribute_Reference
5946 and then
5947 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5948 else
5949 return False;
5950 end if;
5951 end Same_Instantiated_Function;
5953 --------------------------------
5954 -- Same_Instantiated_Variable --
5955 --------------------------------
5957 function Same_Instantiated_Variable
5958 (E1, E2 : Entity_Id) return Boolean
5960 function Original_Entity (E : Entity_Id) return Entity_Id;
5961 -- Follow chain of renamings to the ultimate ancestor
5963 ---------------------
5964 -- Original_Entity --
5965 ---------------------
5967 function Original_Entity (E : Entity_Id) return Entity_Id is
5968 Orig : Entity_Id;
5970 begin
5971 Orig := E;
5972 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5973 and then Present (Renamed_Object (Orig))
5974 and then Is_Entity_Name (Renamed_Object (Orig))
5975 loop
5976 Orig := Entity (Renamed_Object (Orig));
5977 end loop;
5979 return Orig;
5980 end Original_Entity;
5982 -- Start of processing for Same_Instantiated_Variable
5984 begin
5985 return Ekind (E1) = Ekind (E2)
5986 and then Original_Entity (E1) = Original_Entity (E2);
5987 end Same_Instantiated_Variable;
5989 -- Start of processing for Check_Formal_Package_Instance
5991 begin
5992 Prev_E1 := E1;
5993 while Present (E1) and then Present (E2) loop
5994 exit when Ekind (E1) = E_Package
5995 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5997 -- If the formal is the renaming of the formal package, this
5998 -- is the end of its formal part, which may occur before the
5999 -- end of the formal part in the actual in the presence of
6000 -- defaulted parameters in the formal package.
6002 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6003 and then Renamed_Entity (E2) = Scope (E2);
6005 -- The analysis of the actual may generate additional internal
6006 -- entities. If the formal is defaulted, there is no corresponding
6007 -- analysis and the internal entities must be skipped, until we
6008 -- find corresponding entities again.
6010 if Comes_From_Source (E2)
6011 and then not Comes_From_Source (E1)
6012 and then Chars (E1) /= Chars (E2)
6013 then
6014 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6015 Next_Entity (E1);
6016 end loop;
6017 end if;
6019 if No (E1) then
6020 return;
6022 -- Entities may be declared without full declaration, such as
6023 -- itypes and predefined operators (concatenation for arrays, eg).
6024 -- Skip it and keep the formal entity to find a later match for it.
6026 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6027 E1 := Prev_E1;
6028 goto Next_E;
6030 -- If the formal entity comes from a formal declaration, it was
6031 -- defaulted in the formal package, and no check is needed on it.
6033 elsif Nkind_In (Original_Node (Parent (E2)),
6034 N_Formal_Object_Declaration,
6035 N_Formal_Type_Declaration)
6036 then
6037 -- If the formal is a tagged type the corresponding class-wide
6038 -- type has been generated as well, and it must be skipped.
6040 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6041 Next_Entity (E2);
6042 end if;
6044 goto Next_E;
6046 -- Ditto for defaulted formal subprograms.
6048 elsif Is_Overloadable (E1)
6049 and then Nkind (Unit_Declaration_Node (E2)) in
6050 N_Formal_Subprogram_Declaration
6051 then
6052 goto Next_E;
6054 elsif Is_Type (E1) then
6056 -- Subtypes must statically match. E1, E2 are the local entities
6057 -- that are subtypes of the actuals. Itypes generated for other
6058 -- parameters need not be checked, the check will be performed
6059 -- on the parameters themselves.
6061 -- If E2 is a formal type declaration, it is a defaulted parameter
6062 -- and needs no checking.
6064 if not Is_Itype (E1) and then not Is_Itype (E2) then
6065 Check_Mismatch
6066 (not Is_Type (E2)
6067 or else Etype (E1) /= Etype (E2)
6068 or else not Subtypes_Statically_Match (E1, E2));
6069 end if;
6071 elsif Ekind (E1) = E_Constant then
6073 -- IN parameters must denote the same static value, or the same
6074 -- constant, or the literal null.
6076 Expr1 := Expression (Parent (E1));
6078 if Ekind (E2) /= E_Constant then
6079 Check_Mismatch (True);
6080 goto Next_E;
6081 else
6082 Expr2 := Expression (Parent (E2));
6083 end if;
6085 if Is_OK_Static_Expression (Expr1) then
6086 if not Is_OK_Static_Expression (Expr2) then
6087 Check_Mismatch (True);
6089 elsif Is_Discrete_Type (Etype (E1)) then
6090 declare
6091 V1 : constant Uint := Expr_Value (Expr1);
6092 V2 : constant Uint := Expr_Value (Expr2);
6093 begin
6094 Check_Mismatch (V1 /= V2);
6095 end;
6097 elsif Is_Real_Type (Etype (E1)) then
6098 declare
6099 V1 : constant Ureal := Expr_Value_R (Expr1);
6100 V2 : constant Ureal := Expr_Value_R (Expr2);
6101 begin
6102 Check_Mismatch (V1 /= V2);
6103 end;
6105 elsif Is_String_Type (Etype (E1))
6106 and then Nkind (Expr1) = N_String_Literal
6107 then
6108 if Nkind (Expr2) /= N_String_Literal then
6109 Check_Mismatch (True);
6110 else
6111 Check_Mismatch
6112 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6113 end if;
6114 end if;
6116 elsif Is_Entity_Name (Expr1) then
6117 if Is_Entity_Name (Expr2) then
6118 if Entity (Expr1) = Entity (Expr2) then
6119 null;
6120 else
6121 Check_Mismatch
6122 (not Same_Instantiated_Constant
6123 (Entity (Expr1), Entity (Expr2)));
6124 end if;
6126 else
6127 Check_Mismatch (True);
6128 end if;
6130 elsif Is_Entity_Name (Original_Node (Expr1))
6131 and then Is_Entity_Name (Expr2)
6132 and then Same_Instantiated_Constant
6133 (Entity (Original_Node (Expr1)), Entity (Expr2))
6134 then
6135 null;
6137 elsif Nkind (Expr1) = N_Null then
6138 Check_Mismatch (Nkind (Expr1) /= N_Null);
6140 else
6141 Check_Mismatch (True);
6142 end if;
6144 elsif Ekind (E1) = E_Variable then
6145 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6147 elsif Ekind (E1) = E_Package then
6148 Check_Mismatch
6149 (Ekind (E1) /= Ekind (E2)
6150 or else Renamed_Object (E1) /= Renamed_Object (E2));
6152 elsif Is_Overloadable (E1) then
6154 -- Verify that the actual subprograms match. Note that actuals
6155 -- that are attributes are rewritten as subprograms. If the
6156 -- subprogram in the formal package is defaulted, no check is
6157 -- needed. Note that this can only happen in Ada 2005 when the
6158 -- formal package can be partially parameterized.
6160 if Nkind (Unit_Declaration_Node (E1)) =
6161 N_Subprogram_Renaming_Declaration
6162 and then From_Default (Unit_Declaration_Node (E1))
6163 then
6164 null;
6166 -- If the formal package has an "others" box association that
6167 -- covers this formal, there is no need for a check either.
6169 elsif Nkind (Unit_Declaration_Node (E2)) in
6170 N_Formal_Subprogram_Declaration
6171 and then Box_Present (Unit_Declaration_Node (E2))
6172 then
6173 null;
6175 -- No check needed if subprogram is a defaulted null procedure
6177 elsif No (Alias (E2))
6178 and then Ekind (E2) = E_Procedure
6179 and then
6180 Null_Present (Specification (Unit_Declaration_Node (E2)))
6181 then
6182 null;
6184 -- Otherwise the actual in the formal and the actual in the
6185 -- instantiation of the formal must match, up to renamings.
6187 else
6188 Check_Mismatch
6189 (Ekind (E2) /= Ekind (E1)
6190 or else not Same_Instantiated_Function (E1, E2));
6191 end if;
6193 else
6194 raise Program_Error;
6195 end if;
6197 <<Next_E>>
6198 Prev_E1 := E1;
6199 Next_Entity (E1);
6200 Next_Entity (E2);
6201 end loop;
6202 end Check_Formal_Package_Instance;
6204 ---------------------------
6205 -- Check_Formal_Packages --
6206 ---------------------------
6208 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6209 E : Entity_Id;
6210 Formal_P : Entity_Id;
6211 Formal_Decl : Node_Id;
6213 begin
6214 -- Iterate through the declarations in the instance, looking for package
6215 -- renaming declarations that denote instances of formal packages. Stop
6216 -- when we find the renaming of the current package itself. The
6217 -- declaration for a formal package without a box is followed by an
6218 -- internal entity that repeats the instantiation.
6220 E := First_Entity (P_Id);
6221 while Present (E) loop
6222 if Ekind (E) = E_Package then
6223 if Renamed_Object (E) = P_Id then
6224 exit;
6226 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6227 null;
6229 else
6230 Formal_Decl := Parent (Associated_Formal_Package (E));
6232 -- Nothing to check if the formal has a box or an others_clause
6233 -- (necessarily with a box).
6235 if Box_Present (Formal_Decl) then
6236 null;
6238 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6239 N_Others_Choice
6240 then
6241 -- The internal validating package was generated but formal
6242 -- and instance are known to be compatible.
6244 Formal_P := Next_Entity (E);
6245 Remove (Unit_Declaration_Node (Formal_P));
6247 else
6248 Formal_P := Next_Entity (E);
6249 Check_Formal_Package_Instance (Formal_P, E);
6251 -- After checking, remove the internal validating package.
6252 -- It is only needed for semantic checks, and as it may
6253 -- contain generic formal declarations it should not reach
6254 -- gigi.
6256 Remove (Unit_Declaration_Node (Formal_P));
6257 end if;
6258 end if;
6259 end if;
6261 Next_Entity (E);
6262 end loop;
6263 end Check_Formal_Packages;
6265 ---------------------------------
6266 -- Check_Forward_Instantiation --
6267 ---------------------------------
6269 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6270 S : Entity_Id;
6271 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6273 begin
6274 -- The instantiation appears before the generic body if we are in the
6275 -- scope of the unit containing the generic, either in its spec or in
6276 -- the package body, and before the generic body.
6278 if Ekind (Gen_Comp) = E_Package_Body then
6279 Gen_Comp := Spec_Entity (Gen_Comp);
6280 end if;
6282 if In_Open_Scopes (Gen_Comp)
6283 and then No (Corresponding_Body (Decl))
6284 then
6285 S := Current_Scope;
6287 while Present (S)
6288 and then not Is_Compilation_Unit (S)
6289 and then not Is_Child_Unit (S)
6290 loop
6291 if Ekind (S) = E_Package then
6292 Set_Has_Forward_Instantiation (S);
6293 end if;
6295 S := Scope (S);
6296 end loop;
6297 end if;
6298 end Check_Forward_Instantiation;
6300 ---------------------------
6301 -- Check_Generic_Actuals --
6302 ---------------------------
6304 -- The visibility of the actuals may be different between the point of
6305 -- generic instantiation and the instantiation of the body.
6307 procedure Check_Generic_Actuals
6308 (Instance : Entity_Id;
6309 Is_Formal_Box : Boolean)
6311 E : Entity_Id;
6312 Astype : Entity_Id;
6314 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6315 -- For a formal that is an array type, the component type is often a
6316 -- previous formal in the same unit. The privacy status of the component
6317 -- type will have been examined earlier in the traversal of the
6318 -- corresponding actuals, and this status should not be modified for
6319 -- the array (sub)type itself. However, if the base type of the array
6320 -- (sub)type is private, its full view must be restored in the body to
6321 -- be consistent with subsequent index subtypes, etc.
6323 -- To detect this case we have to rescan the list of formals, which is
6324 -- usually short enough to ignore the resulting inefficiency.
6326 -----------------------------
6327 -- Denotes_Previous_Actual --
6328 -----------------------------
6330 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6331 Prev : Entity_Id;
6333 begin
6334 Prev := First_Entity (Instance);
6335 while Present (Prev) loop
6336 if Is_Type (Prev)
6337 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6338 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6339 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6340 then
6341 return True;
6343 elsif Prev = E then
6344 return False;
6346 else
6347 Next_Entity (Prev);
6348 end if;
6349 end loop;
6351 return False;
6352 end Denotes_Previous_Actual;
6354 -- Start of processing for Check_Generic_Actuals
6356 begin
6357 E := First_Entity (Instance);
6358 while Present (E) loop
6359 if Is_Type (E)
6360 and then Nkind (Parent (E)) = N_Subtype_Declaration
6361 and then Scope (Etype (E)) /= Instance
6362 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6363 then
6364 if Is_Array_Type (E)
6365 and then not Is_Private_Type (Etype (E))
6366 and then Denotes_Previous_Actual (Component_Type (E))
6367 then
6368 null;
6369 else
6370 Check_Private_View (Subtype_Indication (Parent (E)));
6371 end if;
6373 Set_Is_Generic_Actual_Type (E, True);
6374 Set_Is_Hidden (E, False);
6375 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6377 -- We constructed the generic actual type as a subtype of the
6378 -- supplied type. This means that it normally would not inherit
6379 -- subtype specific attributes of the actual, which is wrong for
6380 -- the generic case.
6382 Astype := Ancestor_Subtype (E);
6384 if No (Astype) then
6386 -- This can happen when E is an itype that is the full view of
6387 -- a private type completed, e.g. with a constrained array. In
6388 -- that case, use the first subtype, which will carry size
6389 -- information. The base type itself is unconstrained and will
6390 -- not carry it.
6392 Astype := First_Subtype (E);
6393 end if;
6395 Set_Size_Info (E, (Astype));
6396 Set_RM_Size (E, RM_Size (Astype));
6397 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6399 if Is_Discrete_Or_Fixed_Point_Type (E) then
6400 Set_RM_Size (E, RM_Size (Astype));
6402 -- In nested instances, the base type of an access actual may
6403 -- itself be private, and need to be exchanged.
6405 elsif Is_Access_Type (E)
6406 and then Is_Private_Type (Etype (E))
6407 then
6408 Check_Private_View
6409 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6410 end if;
6412 elsif Ekind (E) = E_Package then
6414 -- If this is the renaming for the current instance, we're done.
6415 -- Otherwise it is a formal package. If the corresponding formal
6416 -- was declared with a box, the (instantiations of the) generic
6417 -- formal part are also visible. Otherwise, ignore the entity
6418 -- created to validate the actuals.
6420 if Renamed_Object (E) = Instance then
6421 exit;
6423 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6424 null;
6426 -- The visibility of a formal of an enclosing generic is already
6427 -- correct.
6429 elsif Denotes_Formal_Package (E) then
6430 null;
6432 elsif Present (Associated_Formal_Package (E))
6433 and then not Is_Generic_Formal (E)
6434 then
6435 if Box_Present (Parent (Associated_Formal_Package (E))) then
6436 Check_Generic_Actuals (Renamed_Object (E), True);
6438 else
6439 Check_Generic_Actuals (Renamed_Object (E), False);
6440 end if;
6442 Set_Is_Hidden (E, False);
6443 end if;
6445 -- If this is a subprogram instance (in a wrapper package) the
6446 -- actual is fully visible.
6448 elsif Is_Wrapper_Package (Instance) then
6449 Set_Is_Hidden (E, False);
6451 -- If the formal package is declared with a box, or if the formal
6452 -- parameter is defaulted, it is visible in the body.
6454 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6455 Set_Is_Hidden (E, False);
6456 end if;
6458 if Ekind (E) = E_Constant then
6460 -- If the type of the actual is a private type declared in the
6461 -- enclosing scope of the generic unit, the body of the generic
6462 -- sees the full view of the type (because it has to appear in
6463 -- the corresponding package body). If the type is private now,
6464 -- exchange views to restore the proper visiblity in the instance.
6466 declare
6467 Typ : constant Entity_Id := Base_Type (Etype (E));
6468 -- The type of the actual
6470 Gen_Id : Entity_Id;
6471 -- The generic unit
6473 Parent_Scope : Entity_Id;
6474 -- The enclosing scope of the generic unit
6476 begin
6477 if Is_Wrapper_Package (Instance) then
6478 Gen_Id :=
6479 Generic_Parent
6480 (Specification
6481 (Unit_Declaration_Node
6482 (Related_Instance (Instance))));
6483 else
6484 Gen_Id :=
6485 Generic_Parent (Package_Specification (Instance));
6486 end if;
6488 Parent_Scope := Scope (Gen_Id);
6490 -- The exchange is only needed if the generic is defined
6491 -- within a package which is not a common ancestor of the
6492 -- scope of the instance, and is not already in scope.
6494 if Is_Private_Type (Typ)
6495 and then Scope (Typ) = Parent_Scope
6496 and then Scope (Instance) /= Parent_Scope
6497 and then Ekind (Parent_Scope) = E_Package
6498 and then not Is_Child_Unit (Gen_Id)
6499 then
6500 Switch_View (Typ);
6502 -- If the type of the entity is a subtype, it may also have
6503 -- to be made visible, together with the base type of its
6504 -- full view, after exchange.
6506 if Is_Private_Type (Etype (E)) then
6507 Switch_View (Etype (E));
6508 Switch_View (Base_Type (Etype (E)));
6509 end if;
6510 end if;
6511 end;
6512 end if;
6514 Next_Entity (E);
6515 end loop;
6516 end Check_Generic_Actuals;
6518 ------------------------------
6519 -- Check_Generic_Child_Unit --
6520 ------------------------------
6522 procedure Check_Generic_Child_Unit
6523 (Gen_Id : Node_Id;
6524 Parent_Installed : in out Boolean)
6526 Loc : constant Source_Ptr := Sloc (Gen_Id);
6527 Gen_Par : Entity_Id := Empty;
6528 E : Entity_Id;
6529 Inst_Par : Entity_Id;
6530 S : Node_Id;
6532 function Find_Generic_Child
6533 (Scop : Entity_Id;
6534 Id : Node_Id) return Entity_Id;
6535 -- Search generic parent for possible child unit with the given name
6537 function In_Enclosing_Instance return Boolean;
6538 -- Within an instance of the parent, the child unit may be denoted by
6539 -- a simple name, or an abbreviated expanded name. Examine enclosing
6540 -- scopes to locate a possible parent instantiation.
6542 ------------------------
6543 -- Find_Generic_Child --
6544 ------------------------
6546 function Find_Generic_Child
6547 (Scop : Entity_Id;
6548 Id : Node_Id) return Entity_Id
6550 E : Entity_Id;
6552 begin
6553 -- If entity of name is already set, instance has already been
6554 -- resolved, e.g. in an enclosing instantiation.
6556 if Present (Entity (Id)) then
6557 if Scope (Entity (Id)) = Scop then
6558 return Entity (Id);
6559 else
6560 return Empty;
6561 end if;
6563 else
6564 E := First_Entity (Scop);
6565 while Present (E) loop
6566 if Chars (E) = Chars (Id)
6567 and then Is_Child_Unit (E)
6568 then
6569 if Is_Child_Unit (E)
6570 and then not Is_Visible_Lib_Unit (E)
6571 then
6572 Error_Msg_NE
6573 ("generic child unit& is not visible", Gen_Id, E);
6574 end if;
6576 Set_Entity (Id, E);
6577 return E;
6578 end if;
6580 Next_Entity (E);
6581 end loop;
6583 return Empty;
6584 end if;
6585 end Find_Generic_Child;
6587 ---------------------------
6588 -- In_Enclosing_Instance --
6589 ---------------------------
6591 function In_Enclosing_Instance return Boolean is
6592 Enclosing_Instance : Node_Id;
6593 Instance_Decl : Node_Id;
6595 begin
6596 -- We do not inline any call that contains instantiations, except
6597 -- for instantiations of Unchecked_Conversion, so if we are within
6598 -- an inlined body the current instance does not require parents.
6600 if In_Inlined_Body then
6601 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6602 return False;
6603 end if;
6605 -- Loop to check enclosing scopes
6607 Enclosing_Instance := Current_Scope;
6608 while Present (Enclosing_Instance) loop
6609 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6611 if Ekind (Enclosing_Instance) = E_Package
6612 and then Is_Generic_Instance (Enclosing_Instance)
6613 and then Present
6614 (Generic_Parent (Specification (Instance_Decl)))
6615 then
6616 -- Check whether the generic we are looking for is a child of
6617 -- this instance.
6619 E := Find_Generic_Child
6620 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6621 exit when Present (E);
6623 else
6624 E := Empty;
6625 end if;
6627 Enclosing_Instance := Scope (Enclosing_Instance);
6628 end loop;
6630 if No (E) then
6632 -- Not a child unit
6634 Analyze (Gen_Id);
6635 return False;
6637 else
6638 Rewrite (Gen_Id,
6639 Make_Expanded_Name (Loc,
6640 Chars => Chars (E),
6641 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6642 Selector_Name => New_Occurrence_Of (E, Loc)));
6644 Set_Entity (Gen_Id, E);
6645 Set_Etype (Gen_Id, Etype (E));
6646 Parent_Installed := False; -- Already in scope.
6647 return True;
6648 end if;
6649 end In_Enclosing_Instance;
6651 -- Start of processing for Check_Generic_Child_Unit
6653 begin
6654 -- If the name of the generic is given by a selected component, it may
6655 -- be the name of a generic child unit, and the prefix is the name of an
6656 -- instance of the parent, in which case the child unit must be visible.
6657 -- If this instance is not in scope, it must be placed there and removed
6658 -- after instantiation, because what is being instantiated is not the
6659 -- original child, but the corresponding child present in the instance
6660 -- of the parent.
6662 -- If the child is instantiated within the parent, it can be given by
6663 -- a simple name. In this case the instance is already in scope, but
6664 -- the child generic must be recovered from the generic parent as well.
6666 if Nkind (Gen_Id) = N_Selected_Component then
6667 S := Selector_Name (Gen_Id);
6668 Analyze (Prefix (Gen_Id));
6669 Inst_Par := Entity (Prefix (Gen_Id));
6671 if Ekind (Inst_Par) = E_Package
6672 and then Present (Renamed_Object (Inst_Par))
6673 then
6674 Inst_Par := Renamed_Object (Inst_Par);
6675 end if;
6677 if Ekind (Inst_Par) = E_Package then
6678 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6679 Gen_Par := Generic_Parent (Parent (Inst_Par));
6681 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6682 and then
6683 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6684 then
6685 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6686 end if;
6688 elsif Ekind (Inst_Par) = E_Generic_Package
6689 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6690 then
6691 -- A formal package may be a real child package, and not the
6692 -- implicit instance within a parent. In this case the child is
6693 -- not visible and has to be retrieved explicitly as well.
6695 Gen_Par := Inst_Par;
6696 end if;
6698 if Present (Gen_Par) then
6700 -- The prefix denotes an instantiation. The entity itself may be a
6701 -- nested generic, or a child unit.
6703 E := Find_Generic_Child (Gen_Par, S);
6705 if Present (E) then
6706 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6707 Set_Entity (Gen_Id, E);
6708 Set_Etype (Gen_Id, Etype (E));
6709 Set_Entity (S, E);
6710 Set_Etype (S, Etype (E));
6712 -- Indicate that this is a reference to the parent
6714 if In_Extended_Main_Source_Unit (Gen_Id) then
6715 Set_Is_Instantiated (Inst_Par);
6716 end if;
6718 -- A common mistake is to replicate the naming scheme of a
6719 -- hierarchy by instantiating a generic child directly, rather
6720 -- than the implicit child in a parent instance:
6722 -- generic .. package Gpar is ..
6723 -- generic .. package Gpar.Child is ..
6724 -- package Par is new Gpar ();
6726 -- with Gpar.Child;
6727 -- package Par.Child is new Gpar.Child ();
6728 -- rather than Par.Child
6730 -- In this case the instantiation is within Par, which is an
6731 -- instance, but Gpar does not denote Par because we are not IN
6732 -- the instance of Gpar, so this is illegal. The test below
6733 -- recognizes this particular case.
6735 if Is_Child_Unit (E)
6736 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6737 and then (not In_Instance
6738 or else Nkind (Parent (Parent (Gen_Id))) =
6739 N_Compilation_Unit)
6740 then
6741 Error_Msg_N
6742 ("prefix of generic child unit must be instance of parent",
6743 Gen_Id);
6744 end if;
6746 if not In_Open_Scopes (Inst_Par)
6747 and then Nkind (Parent (Gen_Id)) not in
6748 N_Generic_Renaming_Declaration
6749 then
6750 Install_Parent (Inst_Par);
6751 Parent_Installed := True;
6753 elsif In_Open_Scopes (Inst_Par) then
6755 -- If the parent is already installed, install the actuals
6756 -- for its formal packages. This is necessary when the child
6757 -- instance is a child of the parent instance: in this case,
6758 -- the parent is placed on the scope stack but the formal
6759 -- packages are not made visible.
6761 Install_Formal_Packages (Inst_Par);
6762 end if;
6764 else
6765 -- If the generic parent does not contain an entity that
6766 -- corresponds to the selector, the instance doesn't either.
6767 -- Analyzing the node will yield the appropriate error message.
6768 -- If the entity is not a child unit, then it is an inner
6769 -- generic in the parent.
6771 Analyze (Gen_Id);
6772 end if;
6774 else
6775 Analyze (Gen_Id);
6777 if Is_Child_Unit (Entity (Gen_Id))
6778 and then
6779 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6780 and then not In_Open_Scopes (Inst_Par)
6781 then
6782 Install_Parent (Inst_Par);
6783 Parent_Installed := True;
6785 -- The generic unit may be the renaming of the implicit child
6786 -- present in an instance. In that case the parent instance is
6787 -- obtained from the name of the renamed entity.
6789 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6790 and then Present (Renamed_Entity (Entity (Gen_Id)))
6791 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6792 then
6793 declare
6794 Renamed_Package : constant Node_Id :=
6795 Name (Parent (Entity (Gen_Id)));
6796 begin
6797 if Nkind (Renamed_Package) = N_Expanded_Name then
6798 Inst_Par := Entity (Prefix (Renamed_Package));
6799 Install_Parent (Inst_Par);
6800 Parent_Installed := True;
6801 end if;
6802 end;
6803 end if;
6804 end if;
6806 elsif Nkind (Gen_Id) = N_Expanded_Name then
6808 -- Entity already present, analyze prefix, whose meaning may be an
6809 -- instance in the current context. If it is an instance of a
6810 -- relative within another, the proper parent may still have to be
6811 -- installed, if they are not of the same generation.
6813 Analyze (Prefix (Gen_Id));
6815 -- Prevent cascaded errors
6817 if Etype (Prefix (Gen_Id)) = Any_Type then
6818 return;
6819 end if;
6821 -- In the unlikely case that a local declaration hides the name of
6822 -- the parent package, locate it on the homonym chain. If the context
6823 -- is an instance of the parent, the renaming entity is flagged as
6824 -- such.
6826 Inst_Par := Entity (Prefix (Gen_Id));
6827 while Present (Inst_Par)
6828 and then not Is_Package_Or_Generic_Package (Inst_Par)
6829 loop
6830 Inst_Par := Homonym (Inst_Par);
6831 end loop;
6833 pragma Assert (Present (Inst_Par));
6834 Set_Entity (Prefix (Gen_Id), Inst_Par);
6836 if In_Enclosing_Instance then
6837 null;
6839 elsif Present (Entity (Gen_Id))
6840 and then Is_Child_Unit (Entity (Gen_Id))
6841 and then not In_Open_Scopes (Inst_Par)
6842 then
6843 Install_Parent (Inst_Par);
6844 Parent_Installed := True;
6845 end if;
6847 elsif In_Enclosing_Instance then
6849 -- The child unit is found in some enclosing scope
6851 null;
6853 else
6854 Analyze (Gen_Id);
6856 -- If this is the renaming of the implicit child in a parent
6857 -- instance, recover the parent name and install it.
6859 if Is_Entity_Name (Gen_Id) then
6860 E := Entity (Gen_Id);
6862 if Is_Generic_Unit (E)
6863 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6864 and then Is_Child_Unit (Renamed_Object (E))
6865 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6866 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6867 then
6868 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6869 Inst_Par := Entity (Prefix (Gen_Id));
6871 if not In_Open_Scopes (Inst_Par) then
6872 Install_Parent (Inst_Par);
6873 Parent_Installed := True;
6874 end if;
6876 -- If it is a child unit of a non-generic parent, it may be
6877 -- use-visible and given by a direct name. Install parent as
6878 -- for other cases.
6880 elsif Is_Generic_Unit (E)
6881 and then Is_Child_Unit (E)
6882 and then
6883 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6884 and then not Is_Generic_Unit (Scope (E))
6885 then
6886 if not In_Open_Scopes (Scope (E)) then
6887 Install_Parent (Scope (E));
6888 Parent_Installed := True;
6889 end if;
6890 end if;
6891 end if;
6892 end if;
6893 end Check_Generic_Child_Unit;
6895 -----------------------------
6896 -- Check_Hidden_Child_Unit --
6897 -----------------------------
6899 procedure Check_Hidden_Child_Unit
6900 (N : Node_Id;
6901 Gen_Unit : Entity_Id;
6902 Act_Decl_Id : Entity_Id)
6904 Gen_Id : constant Node_Id := Name (N);
6906 begin
6907 if Is_Child_Unit (Gen_Unit)
6908 and then Is_Child_Unit (Act_Decl_Id)
6909 and then Nkind (Gen_Id) = N_Expanded_Name
6910 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6911 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6912 then
6913 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6914 Error_Msg_NE
6915 ("generic unit & is implicitly declared in &",
6916 Defining_Unit_Name (N), Gen_Unit);
6917 Error_Msg_N ("\instance must have different name",
6918 Defining_Unit_Name (N));
6919 end if;
6920 end Check_Hidden_Child_Unit;
6922 ------------------------
6923 -- Check_Private_View --
6924 ------------------------
6926 procedure Check_Private_View (N : Node_Id) is
6927 T : constant Entity_Id := Etype (N);
6928 BT : Entity_Id;
6930 begin
6931 -- Exchange views if the type was not private in the generic but is
6932 -- private at the point of instantiation. Do not exchange views if
6933 -- the scope of the type is in scope. This can happen if both generic
6934 -- and instance are sibling units, or if type is defined in a parent.
6935 -- In this case the visibility of the type will be correct for all
6936 -- semantic checks.
6938 if Present (T) then
6939 BT := Base_Type (T);
6941 if Is_Private_Type (T)
6942 and then not Has_Private_View (N)
6943 and then Present (Full_View (T))
6944 and then not In_Open_Scopes (Scope (T))
6945 then
6946 -- In the generic, the full type was visible. Save the private
6947 -- entity, for subsequent exchange.
6949 Switch_View (T);
6951 elsif Has_Private_View (N)
6952 and then not Is_Private_Type (T)
6953 and then not Has_Been_Exchanged (T)
6954 and then Etype (Get_Associated_Node (N)) /= T
6955 then
6956 -- Only the private declaration was visible in the generic. If
6957 -- the type appears in a subtype declaration, the subtype in the
6958 -- instance must have a view compatible with that of its parent,
6959 -- which must be exchanged (see corresponding code in Restore_
6960 -- Private_Views). Otherwise, if the type is defined in a parent
6961 -- unit, leave full visibility within instance, which is safe.
6963 if In_Open_Scopes (Scope (Base_Type (T)))
6964 and then not Is_Private_Type (Base_Type (T))
6965 and then Comes_From_Source (Base_Type (T))
6966 then
6967 null;
6969 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6970 or else not In_Private_Part (Scope (Base_Type (T)))
6971 then
6972 Prepend_Elmt (T, Exchanged_Views);
6973 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6974 end if;
6976 -- For composite types with inconsistent representation exchange
6977 -- component types accordingly.
6979 elsif Is_Access_Type (T)
6980 and then Is_Private_Type (Designated_Type (T))
6981 and then not Has_Private_View (N)
6982 and then Present (Full_View (Designated_Type (T)))
6983 then
6984 Switch_View (Designated_Type (T));
6986 elsif Is_Array_Type (T) then
6987 if Is_Private_Type (Component_Type (T))
6988 and then not Has_Private_View (N)
6989 and then Present (Full_View (Component_Type (T)))
6990 then
6991 Switch_View (Component_Type (T));
6992 end if;
6994 -- The normal exchange mechanism relies on the setting of a
6995 -- flag on the reference in the generic. However, an additional
6996 -- mechanism is needed for types that are not explicitly
6997 -- mentioned in the generic, but may be needed in expanded code
6998 -- in the instance. This includes component types of arrays and
6999 -- designated types of access types. This processing must also
7000 -- include the index types of arrays which we take care of here.
7002 declare
7003 Indx : Node_Id;
7004 Typ : Entity_Id;
7006 begin
7007 Indx := First_Index (T);
7008 while Present (Indx) loop
7009 Typ := Base_Type (Etype (Indx));
7011 if Is_Private_Type (Typ)
7012 and then Present (Full_View (Typ))
7013 then
7014 Switch_View (Typ);
7015 end if;
7017 Next_Index (Indx);
7018 end loop;
7019 end;
7021 elsif Is_Private_Type (T)
7022 and then Present (Full_View (T))
7023 and then Is_Array_Type (Full_View (T))
7024 and then Is_Private_Type (Component_Type (Full_View (T)))
7025 then
7026 Switch_View (T);
7028 -- Finally, a non-private subtype may have a private base type, which
7029 -- must be exchanged for consistency. This can happen when a package
7030 -- body is instantiated, when the scope stack is empty but in fact
7031 -- the subtype and the base type are declared in an enclosing scope.
7033 -- Note that in this case we introduce an inconsistency in the view
7034 -- set, because we switch the base type BT, but there could be some
7035 -- private dependent subtypes of BT which remain unswitched. Such
7036 -- subtypes might need to be switched at a later point (see specific
7037 -- provision for that case in Switch_View).
7039 elsif not Is_Private_Type (T)
7040 and then not Has_Private_View (N)
7041 and then Is_Private_Type (BT)
7042 and then Present (Full_View (BT))
7043 and then not Is_Generic_Type (BT)
7044 and then not In_Open_Scopes (BT)
7045 then
7046 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7047 Exchange_Declarations (BT);
7048 end if;
7049 end if;
7050 end Check_Private_View;
7052 -----------------------------
7053 -- Check_Hidden_Primitives --
7054 -----------------------------
7056 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7057 Actual : Node_Id;
7058 Gen_T : Entity_Id;
7059 Result : Elist_Id := No_Elist;
7061 begin
7062 if No (Assoc_List) then
7063 return No_Elist;
7064 end if;
7066 -- Traverse the list of associations between formals and actuals
7067 -- searching for renamings of tagged types
7069 Actual := First (Assoc_List);
7070 while Present (Actual) loop
7071 if Nkind (Actual) = N_Subtype_Declaration then
7072 Gen_T := Generic_Parent_Type (Actual);
7074 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7076 -- Traverse the list of primitives of the actual types
7077 -- searching for hidden primitives that are visible in the
7078 -- corresponding generic formal; leave them visible and
7079 -- append them to Result to restore their decoration later.
7081 Install_Hidden_Primitives
7082 (Prims_List => Result,
7083 Gen_T => Gen_T,
7084 Act_T => Entity (Subtype_Indication (Actual)));
7085 end if;
7086 end if;
7088 Next (Actual);
7089 end loop;
7091 return Result;
7092 end Check_Hidden_Primitives;
7094 --------------------------
7095 -- Contains_Instance_Of --
7096 --------------------------
7098 function Contains_Instance_Of
7099 (Inner : Entity_Id;
7100 Outer : Entity_Id;
7101 N : Node_Id) return Boolean
7103 Elmt : Elmt_Id;
7104 Scop : Entity_Id;
7106 begin
7107 Scop := Outer;
7109 -- Verify that there are no circular instantiations. We check whether
7110 -- the unit contains an instance of the current scope or some enclosing
7111 -- scope (in case one of the instances appears in a subunit). Longer
7112 -- circularities involving subunits might seem too pathological to
7113 -- consider, but they were not too pathological for the authors of
7114 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7115 -- enclosing generic scopes as containing an instance.
7117 loop
7118 -- Within a generic subprogram body, the scope is not generic, to
7119 -- allow for recursive subprograms. Use the declaration to determine
7120 -- whether this is a generic unit.
7122 if Ekind (Scop) = E_Generic_Package
7123 or else (Is_Subprogram (Scop)
7124 and then Nkind (Unit_Declaration_Node (Scop)) =
7125 N_Generic_Subprogram_Declaration)
7126 then
7127 Elmt := First_Elmt (Inner_Instances (Inner));
7129 while Present (Elmt) loop
7130 if Node (Elmt) = Scop then
7131 Error_Msg_Node_2 := Inner;
7132 Error_Msg_NE
7133 ("circular Instantiation: & instantiated within &!",
7134 N, Scop);
7135 return True;
7137 elsif Node (Elmt) = Inner then
7138 return True;
7140 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7141 Error_Msg_Node_2 := Inner;
7142 Error_Msg_NE
7143 ("circular Instantiation: & instantiated within &!",
7144 N, Node (Elmt));
7145 return True;
7146 end if;
7148 Next_Elmt (Elmt);
7149 end loop;
7151 -- Indicate that Inner is being instantiated within Scop
7153 Append_Elmt (Inner, Inner_Instances (Scop));
7154 end if;
7156 if Scop = Standard_Standard then
7157 exit;
7158 else
7159 Scop := Scope (Scop);
7160 end if;
7161 end loop;
7163 return False;
7164 end Contains_Instance_Of;
7166 -----------------------
7167 -- Copy_Generic_Node --
7168 -----------------------
7170 function Copy_Generic_Node
7171 (N : Node_Id;
7172 Parent_Id : Node_Id;
7173 Instantiating : Boolean) return Node_Id
7175 Ent : Entity_Id;
7176 New_N : Node_Id;
7178 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7179 -- Check the given value of one of the Fields referenced by the current
7180 -- node to determine whether to copy it recursively. The field may hold
7181 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7182 -- Char) in which case it need not be copied.
7184 procedure Copy_Descendants;
7185 -- Common utility for various nodes
7187 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7188 -- Make copy of element list
7190 function Copy_Generic_List
7191 (L : List_Id;
7192 Parent_Id : Node_Id) return List_Id;
7193 -- Apply Copy_Node recursively to the members of a node list
7195 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7196 -- True if an identifier is part of the defining program unit name of
7197 -- a child unit. The entity of such an identifier must be kept (for
7198 -- ASIS use) even though as the name of an enclosing generic it would
7199 -- otherwise not be preserved in the generic tree.
7201 ----------------------
7202 -- Copy_Descendants --
7203 ----------------------
7205 procedure Copy_Descendants is
7206 use Atree.Unchecked_Access;
7207 -- This code section is part of the implementation of an untyped
7208 -- tree traversal, so it needs direct access to node fields.
7210 begin
7211 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7212 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7213 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7214 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7215 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7216 end Copy_Descendants;
7218 -----------------------------
7219 -- Copy_Generic_Descendant --
7220 -----------------------------
7222 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7223 begin
7224 if D = Union_Id (Empty) then
7225 return D;
7227 elsif D in Node_Range then
7228 return Union_Id
7229 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7231 elsif D in List_Range then
7232 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7234 elsif D in Elist_Range then
7235 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7237 -- Nothing else is copyable (e.g. Uint values), return as is
7239 else
7240 return D;
7241 end if;
7242 end Copy_Generic_Descendant;
7244 ------------------------
7245 -- Copy_Generic_Elist --
7246 ------------------------
7248 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7249 M : Elmt_Id;
7250 L : Elist_Id;
7252 begin
7253 if Present (E) then
7254 L := New_Elmt_List;
7255 M := First_Elmt (E);
7256 while Present (M) loop
7257 Append_Elmt
7258 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7259 Next_Elmt (M);
7260 end loop;
7262 return L;
7264 else
7265 return No_Elist;
7266 end if;
7267 end Copy_Generic_Elist;
7269 -----------------------
7270 -- Copy_Generic_List --
7271 -----------------------
7273 function Copy_Generic_List
7274 (L : List_Id;
7275 Parent_Id : Node_Id) return List_Id
7277 N : Node_Id;
7278 New_L : List_Id;
7280 begin
7281 if Present (L) then
7282 New_L := New_List;
7283 Set_Parent (New_L, Parent_Id);
7285 N := First (L);
7286 while Present (N) loop
7287 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7288 Next (N);
7289 end loop;
7291 return New_L;
7293 else
7294 return No_List;
7295 end if;
7296 end Copy_Generic_List;
7298 ---------------------------
7299 -- In_Defining_Unit_Name --
7300 ---------------------------
7302 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7303 begin
7304 return
7305 Present (Parent (Nam))
7306 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7307 or else
7308 (Nkind (Parent (Nam)) = N_Expanded_Name
7309 and then In_Defining_Unit_Name (Parent (Nam))));
7310 end In_Defining_Unit_Name;
7312 -- Start of processing for Copy_Generic_Node
7314 begin
7315 if N = Empty then
7316 return N;
7317 end if;
7319 New_N := New_Copy (N);
7321 -- Copy aspects if present
7323 if Has_Aspects (N) then
7324 Set_Has_Aspects (New_N, False);
7325 Set_Aspect_Specifications
7326 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7327 end if;
7329 if Instantiating then
7330 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7331 end if;
7333 if not Is_List_Member (N) then
7334 Set_Parent (New_N, Parent_Id);
7335 end if;
7337 -- Special casing for identifiers and other entity names and operators
7339 if Nkind_In (New_N, N_Character_Literal,
7340 N_Expanded_Name,
7341 N_Identifier,
7342 N_Operator_Symbol)
7343 or else Nkind (New_N) in N_Op
7344 then
7345 if not Instantiating then
7347 -- Link both nodes in order to assign subsequently the entity of
7348 -- the copy to the original node, in case this is a global
7349 -- reference.
7351 Set_Associated_Node (N, New_N);
7353 -- If we are within an instantiation, this is a nested generic
7354 -- that has already been analyzed at the point of definition.
7355 -- We must preserve references that were global to the enclosing
7356 -- parent at that point. Other occurrences, whether global or
7357 -- local to the current generic, must be resolved anew, so we
7358 -- reset the entity in the generic copy. A global reference has a
7359 -- smaller depth than the parent, or else the same depth in case
7360 -- both are distinct compilation units.
7362 -- A child unit is implicitly declared within the enclosing parent
7363 -- but is in fact global to it, and must be preserved.
7365 -- It is also possible for Current_Instantiated_Parent to be
7366 -- defined, and for this not to be a nested generic, namely if
7367 -- the unit is loaded through Rtsfind. In that case, the entity of
7368 -- New_N is only a link to the associated node, and not a defining
7369 -- occurrence.
7371 -- The entities for parent units in the defining_program_unit of a
7372 -- generic child unit are established when the context of the unit
7373 -- is first analyzed, before the generic copy is made. They are
7374 -- preserved in the copy for use in ASIS queries.
7376 Ent := Entity (New_N);
7378 if No (Current_Instantiated_Parent.Gen_Id) then
7379 if No (Ent)
7380 or else Nkind (Ent) /= N_Defining_Identifier
7381 or else not In_Defining_Unit_Name (N)
7382 then
7383 Set_Associated_Node (New_N, Empty);
7384 end if;
7386 elsif No (Ent)
7387 or else
7388 not Nkind_In (Ent, N_Defining_Identifier,
7389 N_Defining_Character_Literal,
7390 N_Defining_Operator_Symbol)
7391 or else No (Scope (Ent))
7392 or else
7393 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7394 and then not Is_Child_Unit (Ent))
7395 or else
7396 (Scope_Depth (Scope (Ent)) >
7397 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7398 and then
7399 Get_Source_Unit (Ent) =
7400 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7401 then
7402 Set_Associated_Node (New_N, Empty);
7403 end if;
7405 -- Case of instantiating identifier or some other name or operator
7407 else
7408 -- If the associated node is still defined, the entity in it
7409 -- is global, and must be copied to the instance. If this copy
7410 -- is being made for a body to inline, it is applied to an
7411 -- instantiated tree, and the entity is already present and
7412 -- must be also preserved.
7414 declare
7415 Assoc : constant Node_Id := Get_Associated_Node (N);
7417 begin
7418 if Present (Assoc) then
7419 if Nkind (Assoc) = Nkind (N) then
7420 Set_Entity (New_N, Entity (Assoc));
7421 Check_Private_View (N);
7423 -- The node is a reference to a global type and acts as the
7424 -- subtype mark of a qualified expression created in order
7425 -- to aid resolution of accidental overloading in instances.
7426 -- Since N is a reference to a type, the Associated_Node of
7427 -- N denotes an entity rather than another identifier. See
7428 -- Qualify_Universal_Operands for details.
7430 elsif Nkind (N) = N_Identifier
7431 and then Nkind (Parent (N)) = N_Qualified_Expression
7432 and then Subtype_Mark (Parent (N)) = N
7433 and then Is_Qualified_Universal_Literal (Parent (N))
7434 then
7435 Set_Entity (New_N, Assoc);
7437 -- The name in the call may be a selected component if the
7438 -- call has not been analyzed yet, as may be the case for
7439 -- pre/post conditions in a generic unit.
7441 elsif Nkind (Assoc) = N_Function_Call
7442 and then Is_Entity_Name (Name (Assoc))
7443 then
7444 Set_Entity (New_N, Entity (Name (Assoc)));
7446 elsif Nkind_In (Assoc, N_Defining_Identifier,
7447 N_Defining_Character_Literal,
7448 N_Defining_Operator_Symbol)
7449 and then Expander_Active
7450 then
7451 -- Inlining case: we are copying a tree that contains
7452 -- global entities, which are preserved in the copy to be
7453 -- used for subsequent inlining.
7455 null;
7457 else
7458 Set_Entity (New_N, Empty);
7459 end if;
7460 end if;
7461 end;
7462 end if;
7464 -- For expanded name, we must copy the Prefix and Selector_Name
7466 if Nkind (N) = N_Expanded_Name then
7467 Set_Prefix
7468 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7470 Set_Selector_Name (New_N,
7471 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7473 -- For operators, we must copy the right operand
7475 elsif Nkind (N) in N_Op then
7476 Set_Right_Opnd (New_N,
7477 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7479 -- And for binary operators, the left operand as well
7481 if Nkind (N) in N_Binary_Op then
7482 Set_Left_Opnd (New_N,
7483 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7484 end if;
7485 end if;
7487 -- Establish a link between an entity from the generic template and the
7488 -- corresponding entity in the generic copy to be analyzed.
7490 elsif Nkind (N) in N_Entity then
7491 if not Instantiating then
7492 Set_Associated_Entity (N, New_N);
7493 end if;
7495 -- Clear any existing link the copy may inherit from the replicated
7496 -- generic template entity.
7498 Set_Associated_Entity (New_N, Empty);
7500 -- Special casing for stubs
7502 elsif Nkind (N) in N_Body_Stub then
7504 -- In any case, we must copy the specification or defining
7505 -- identifier as appropriate.
7507 if Nkind (N) = N_Subprogram_Body_Stub then
7508 Set_Specification (New_N,
7509 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7511 else
7512 Set_Defining_Identifier (New_N,
7513 Copy_Generic_Node
7514 (Defining_Identifier (N), New_N, Instantiating));
7515 end if;
7517 -- If we are not instantiating, then this is where we load and
7518 -- analyze subunits, i.e. at the point where the stub occurs. A
7519 -- more permissive system might defer this analysis to the point
7520 -- of instantiation, but this seems too complicated for now.
7522 if not Instantiating then
7523 declare
7524 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7525 Subunit : Node_Id;
7526 Unum : Unit_Number_Type;
7527 New_Body : Node_Id;
7529 begin
7530 -- Make sure that, if it is a subunit of the main unit that is
7531 -- preprocessed and if -gnateG is specified, the preprocessed
7532 -- file will be written.
7534 Lib.Analysing_Subunit_Of_Main :=
7535 Lib.In_Extended_Main_Source_Unit (N);
7536 Unum :=
7537 Load_Unit
7538 (Load_Name => Subunit_Name,
7539 Required => False,
7540 Subunit => True,
7541 Error_Node => N);
7542 Lib.Analysing_Subunit_Of_Main := False;
7544 -- If the proper body is not found, a warning message will be
7545 -- emitted when analyzing the stub, or later at the point of
7546 -- instantiation. Here we just leave the stub as is.
7548 if Unum = No_Unit then
7549 Subunits_Missing := True;
7550 goto Subunit_Not_Found;
7551 end if;
7553 Subunit := Cunit (Unum);
7555 if Nkind (Unit (Subunit)) /= N_Subunit then
7556 Error_Msg_N
7557 ("found child unit instead of expected SEPARATE subunit",
7558 Subunit);
7559 Error_Msg_Sloc := Sloc (N);
7560 Error_Msg_N ("\to complete stub #", Subunit);
7561 goto Subunit_Not_Found;
7562 end if;
7564 -- We must create a generic copy of the subunit, in order to
7565 -- perform semantic analysis on it, and we must replace the
7566 -- stub in the original generic unit with the subunit, in order
7567 -- to preserve non-local references within.
7569 -- Only the proper body needs to be copied. Library_Unit and
7570 -- context clause are simply inherited by the generic copy.
7571 -- Note that the copy (which may be recursive if there are
7572 -- nested subunits) must be done first, before attaching it to
7573 -- the enclosing generic.
7575 New_Body :=
7576 Copy_Generic_Node
7577 (Proper_Body (Unit (Subunit)),
7578 Empty, Instantiating => False);
7580 -- Now place the original proper body in the original generic
7581 -- unit. This is a body, not a compilation unit.
7583 Rewrite (N, Proper_Body (Unit (Subunit)));
7584 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7585 Set_Was_Originally_Stub (N);
7587 -- Finally replace the body of the subunit with its copy, and
7588 -- make this new subunit into the library unit of the generic
7589 -- copy, which does not have stubs any longer.
7591 Set_Proper_Body (Unit (Subunit), New_Body);
7592 Set_Library_Unit (New_N, Subunit);
7593 Inherit_Context (Unit (Subunit), N);
7594 end;
7596 -- If we are instantiating, this must be an error case, since
7597 -- otherwise we would have replaced the stub node by the proper body
7598 -- that corresponds. So just ignore it in the copy (i.e. we have
7599 -- copied it, and that is good enough).
7601 else
7602 null;
7603 end if;
7605 <<Subunit_Not_Found>> null;
7607 -- If the node is a compilation unit, it is the subunit of a stub, which
7608 -- has been loaded already (see code below). In this case, the library
7609 -- unit field of N points to the parent unit (which is a compilation
7610 -- unit) and need not (and cannot) be copied.
7612 -- When the proper body of the stub is analyzed, the library_unit link
7613 -- is used to establish the proper context (see sem_ch10).
7615 -- The other fields of a compilation unit are copied as usual
7617 elsif Nkind (N) = N_Compilation_Unit then
7619 -- This code can only be executed when not instantiating, because in
7620 -- the copy made for an instantiation, the compilation unit node has
7621 -- disappeared at the point that a stub is replaced by its proper
7622 -- body.
7624 pragma Assert (not Instantiating);
7626 Set_Context_Items (New_N,
7627 Copy_Generic_List (Context_Items (N), New_N));
7629 Set_Unit (New_N,
7630 Copy_Generic_Node (Unit (N), New_N, False));
7632 Set_First_Inlined_Subprogram (New_N,
7633 Copy_Generic_Node
7634 (First_Inlined_Subprogram (N), New_N, False));
7636 Set_Aux_Decls_Node (New_N,
7637 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7639 -- For an assignment node, the assignment is known to be semantically
7640 -- legal if we are instantiating the template. This avoids incorrect
7641 -- diagnostics in generated code.
7643 elsif Nkind (N) = N_Assignment_Statement then
7645 -- Copy name and expression fields in usual manner
7647 Set_Name (New_N,
7648 Copy_Generic_Node (Name (N), New_N, Instantiating));
7650 Set_Expression (New_N,
7651 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7653 if Instantiating then
7654 Set_Assignment_OK (Name (New_N), True);
7655 end if;
7657 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7658 if not Instantiating then
7659 Set_Associated_Node (N, New_N);
7661 else
7662 if Present (Get_Associated_Node (N))
7663 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7664 then
7665 -- In the generic the aggregate has some composite type. If at
7666 -- the point of instantiation the type has a private view,
7667 -- install the full view (and that of its ancestors, if any).
7669 declare
7670 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7671 Rt : Entity_Id;
7673 begin
7674 if Present (T) and then Is_Private_Type (T) then
7675 Switch_View (T);
7676 end if;
7678 if Present (T)
7679 and then Is_Tagged_Type (T)
7680 and then Is_Derived_Type (T)
7681 then
7682 Rt := Root_Type (T);
7684 loop
7685 T := Etype (T);
7687 if Is_Private_Type (T) then
7688 Switch_View (T);
7689 end if;
7691 exit when T = Rt;
7692 end loop;
7693 end if;
7694 end;
7695 end if;
7696 end if;
7698 -- Do not copy the associated node, which points to the generic copy
7699 -- of the aggregate.
7701 declare
7702 use Atree.Unchecked_Access;
7703 -- This code section is part of the implementation of an untyped
7704 -- tree traversal, so it needs direct access to node fields.
7706 begin
7707 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7708 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7709 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7710 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7711 end;
7713 -- Allocators do not have an identifier denoting the access type, so we
7714 -- must locate it through the expression to check whether the views are
7715 -- consistent.
7717 elsif Nkind (N) = N_Allocator
7718 and then Nkind (Expression (N)) = N_Qualified_Expression
7719 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7720 and then Instantiating
7721 then
7722 declare
7723 T : constant Node_Id :=
7724 Get_Associated_Node (Subtype_Mark (Expression (N)));
7725 Acc_T : Entity_Id;
7727 begin
7728 if Present (T) then
7730 -- Retrieve the allocator node in the generic copy
7732 Acc_T := Etype (Parent (Parent (T)));
7734 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7735 Switch_View (Acc_T);
7736 end if;
7737 end if;
7739 Copy_Descendants;
7740 end;
7742 -- For a proper body, we must catch the case of a proper body that
7743 -- replaces a stub. This represents the point at which a separate
7744 -- compilation unit, and hence template file, may be referenced, so we
7745 -- must make a new source instantiation entry for the template of the
7746 -- subunit, and ensure that all nodes in the subunit are adjusted using
7747 -- this new source instantiation entry.
7749 elsif Nkind (N) in N_Proper_Body then
7750 declare
7751 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7753 begin
7754 if Instantiating and then Was_Originally_Stub (N) then
7755 Create_Instantiation_Source
7756 (Instantiation_Node,
7757 Defining_Entity (N),
7758 S_Adjustment);
7759 end if;
7761 -- Now copy the fields of the proper body, using the new
7762 -- adjustment factor if one was needed as per test above.
7764 Copy_Descendants;
7766 -- Restore the original adjustment factor in case changed
7768 S_Adjustment := Save_Adjustment;
7769 end;
7771 elsif Nkind (N) = N_Pragma and then Instantiating then
7773 -- Do not copy Comment or Ident pragmas their content is relevant to
7774 -- the generic unit, not to the instantiating unit.
7776 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
7777 New_N := Make_Null_Statement (Sloc (N));
7779 -- Do not copy pragmas generated from aspects because the pragmas do
7780 -- not carry any semantic information, plus they will be regenerated
7781 -- in the instance.
7783 -- However, generating C we need to copy them since postconditions
7784 -- are inlined by the front end, and the front-end inlining machinery
7785 -- relies on this routine to perform inlining.
7787 elsif From_Aspect_Specification (N)
7788 and then not Modify_Tree_For_C
7789 then
7790 New_N := Make_Null_Statement (Sloc (N));
7792 else
7793 Copy_Descendants;
7794 end if;
7796 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7798 -- No descendant fields need traversing
7800 null;
7802 elsif Nkind (N) = N_String_Literal
7803 and then Present (Etype (N))
7804 and then Instantiating
7805 then
7806 -- If the string is declared in an outer scope, the string_literal
7807 -- subtype created for it may have the wrong scope. Force reanalysis
7808 -- of the constant to generate a new itype in the proper context.
7810 Set_Etype (New_N, Empty);
7811 Set_Analyzed (New_N, False);
7813 -- For the remaining nodes, copy their descendants recursively
7815 else
7816 Copy_Descendants;
7818 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7819 Set_Generic_Parent (Specification (New_N), N);
7821 -- Should preserve Corresponding_Spec??? (12.3(14))
7822 end if;
7823 end if;
7825 -- Propagate dimensions if present, so that they are reflected in the
7826 -- instance.
7828 if Nkind (N) in N_Has_Etype
7829 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7830 and then Present (Etype (N))
7831 and then Is_Floating_Point_Type (Etype (N))
7832 and then Has_Dimension_System (Etype (N))
7833 then
7834 Copy_Dimensions (N, New_N);
7835 end if;
7837 return New_N;
7838 end Copy_Generic_Node;
7840 ----------------------------
7841 -- Denotes_Formal_Package --
7842 ----------------------------
7844 function Denotes_Formal_Package
7845 (Pack : Entity_Id;
7846 On_Exit : Boolean := False;
7847 Instance : Entity_Id := Empty) return Boolean
7849 Par : Entity_Id;
7850 Scop : constant Entity_Id := Scope (Pack);
7851 E : Entity_Id;
7853 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7854 -- The package in question may be an actual for a previous formal
7855 -- package P of the current instance, so examine its actuals as well.
7856 -- This must be recursive over other formal packages.
7858 ----------------------------------
7859 -- Is_Actual_Of_Previous_Formal --
7860 ----------------------------------
7862 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7863 E1 : Entity_Id;
7865 begin
7866 E1 := First_Entity (P);
7867 while Present (E1) and then E1 /= Instance loop
7868 if Ekind (E1) = E_Package
7869 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7870 then
7871 if Renamed_Object (E1) = Pack then
7872 return True;
7874 elsif E1 = P or else Renamed_Object (E1) = P then
7875 return False;
7877 elsif Is_Actual_Of_Previous_Formal (E1) then
7878 return True;
7879 end if;
7880 end if;
7882 Next_Entity (E1);
7883 end loop;
7885 return False;
7886 end Is_Actual_Of_Previous_Formal;
7888 -- Start of processing for Denotes_Formal_Package
7890 begin
7891 if On_Exit then
7892 Par :=
7893 Instance_Envs.Table
7894 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7895 else
7896 Par := Current_Instantiated_Parent.Act_Id;
7897 end if;
7899 if Ekind (Scop) = E_Generic_Package
7900 or else Nkind (Unit_Declaration_Node (Scop)) =
7901 N_Generic_Subprogram_Declaration
7902 then
7903 return True;
7905 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7906 N_Formal_Package_Declaration
7907 then
7908 return True;
7910 elsif No (Par) then
7911 return False;
7913 else
7914 -- Check whether this package is associated with a formal package of
7915 -- the enclosing instantiation. Iterate over the list of renamings.
7917 E := First_Entity (Par);
7918 while Present (E) loop
7919 if Ekind (E) /= E_Package
7920 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7921 then
7922 null;
7924 elsif Renamed_Object (E) = Par then
7925 return False;
7927 elsif Renamed_Object (E) = Pack then
7928 return True;
7930 elsif Is_Actual_Of_Previous_Formal (E) then
7931 return True;
7933 end if;
7935 Next_Entity (E);
7936 end loop;
7938 return False;
7939 end if;
7940 end Denotes_Formal_Package;
7942 -----------------
7943 -- End_Generic --
7944 -----------------
7946 procedure End_Generic is
7947 begin
7948 -- ??? More things could be factored out in this routine. Should
7949 -- probably be done at a later stage.
7951 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7952 Generic_Flags.Decrement_Last;
7954 Expander_Mode_Restore;
7955 end End_Generic;
7957 -------------
7958 -- Earlier --
7959 -------------
7961 function Earlier (N1, N2 : Node_Id) return Boolean is
7962 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7963 -- Find distance from given node to enclosing compilation unit
7965 ----------------
7966 -- Find_Depth --
7967 ----------------
7969 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7970 begin
7971 while Present (P)
7972 and then Nkind (P) /= N_Compilation_Unit
7973 loop
7974 P := True_Parent (P);
7975 D := D + 1;
7976 end loop;
7977 end Find_Depth;
7979 -- Local declarations
7981 D1 : Integer := 0;
7982 D2 : Integer := 0;
7983 P1 : Node_Id := N1;
7984 P2 : Node_Id := N2;
7985 T1 : Source_Ptr;
7986 T2 : Source_Ptr;
7988 -- Start of processing for Earlier
7990 begin
7991 Find_Depth (P1, D1);
7992 Find_Depth (P2, D2);
7994 if P1 /= P2 then
7995 return False;
7996 else
7997 P1 := N1;
7998 P2 := N2;
7999 end if;
8001 while D1 > D2 loop
8002 P1 := True_Parent (P1);
8003 D1 := D1 - 1;
8004 end loop;
8006 while D2 > D1 loop
8007 P2 := True_Parent (P2);
8008 D2 := D2 - 1;
8009 end loop;
8011 -- At this point P1 and P2 are at the same distance from the root.
8012 -- We examine their parents until we find a common declarative list.
8013 -- If we reach the root, N1 and N2 do not descend from the same
8014 -- declarative list (e.g. one is nested in the declarative part and
8015 -- the other is in a block in the statement part) and the earlier
8016 -- one is already frozen.
8018 while not Is_List_Member (P1)
8019 or else not Is_List_Member (P2)
8020 or else List_Containing (P1) /= List_Containing (P2)
8021 loop
8022 P1 := True_Parent (P1);
8023 P2 := True_Parent (P2);
8025 if Nkind (Parent (P1)) = N_Subunit then
8026 P1 := Corresponding_Stub (Parent (P1));
8027 end if;
8029 if Nkind (Parent (P2)) = N_Subunit then
8030 P2 := Corresponding_Stub (Parent (P2));
8031 end if;
8033 if P1 = P2 then
8034 return False;
8035 end if;
8036 end loop;
8038 -- Expanded code usually shares the source location of the original
8039 -- construct it was generated for. This however may not necessarily
8040 -- reflect the true location of the code within the tree.
8042 -- Before comparing the slocs of the two nodes, make sure that we are
8043 -- working with correct source locations. Assume that P1 is to the left
8044 -- of P2. If either one does not come from source, traverse the common
8045 -- list heading towards the other node and locate the first source
8046 -- statement.
8048 -- P1 P2
8049 -- ----+===+===+--------------+===+===+----
8050 -- expanded code expanded code
8052 if not Comes_From_Source (P1) then
8053 while Present (P1) loop
8055 -- Neither P2 nor a source statement were located during the
8056 -- search. If we reach the end of the list, then P1 does not
8057 -- occur earlier than P2.
8059 -- ---->
8060 -- start --- P2 ----- P1 --- end
8062 if No (Next (P1)) then
8063 return False;
8065 -- We encounter P2 while going to the right of the list. This
8066 -- means that P1 does indeed appear earlier.
8068 -- ---->
8069 -- start --- P1 ===== P2 --- end
8070 -- expanded code in between
8072 elsif P1 = P2 then
8073 return True;
8075 -- No need to look any further since we have located a source
8076 -- statement.
8078 elsif Comes_From_Source (P1) then
8079 exit;
8080 end if;
8082 -- Keep going right
8084 Next (P1);
8085 end loop;
8086 end if;
8088 if not Comes_From_Source (P2) then
8089 while Present (P2) loop
8091 -- Neither P1 nor a source statement were located during the
8092 -- search. If we reach the start of the list, then P1 does not
8093 -- occur earlier than P2.
8095 -- <----
8096 -- start --- P2 --- P1 --- end
8098 if No (Prev (P2)) then
8099 return False;
8101 -- We encounter P1 while going to the left of the list. This
8102 -- means that P1 does indeed appear earlier.
8104 -- <----
8105 -- start --- P1 ===== P2 --- end
8106 -- expanded code in between
8108 elsif P2 = P1 then
8109 return True;
8111 -- No need to look any further since we have located a source
8112 -- statement.
8114 elsif Comes_From_Source (P2) then
8115 exit;
8116 end if;
8118 -- Keep going left
8120 Prev (P2);
8121 end loop;
8122 end if;
8124 -- At this point either both nodes came from source or we approximated
8125 -- their source locations through neighboring source statements.
8127 T1 := Top_Level_Location (Sloc (P1));
8128 T2 := Top_Level_Location (Sloc (P2));
8130 -- When two nodes come from the same instance, they have identical top
8131 -- level locations. To determine proper relation within the tree, check
8132 -- their locations within the template.
8134 if T1 = T2 then
8135 return Sloc (P1) < Sloc (P2);
8137 -- The two nodes either come from unrelated instances or do not come
8138 -- from instantiated code at all.
8140 else
8141 return T1 < T2;
8142 end if;
8143 end Earlier;
8145 ----------------------
8146 -- Find_Actual_Type --
8147 ----------------------
8149 function Find_Actual_Type
8150 (Typ : Entity_Id;
8151 Gen_Type : Entity_Id) return Entity_Id
8153 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8154 T : Entity_Id;
8156 begin
8157 -- Special processing only applies to child units
8159 if not Is_Child_Unit (Gen_Scope) then
8160 return Get_Instance_Of (Typ);
8162 -- If designated or component type is itself a formal of the child unit,
8163 -- its instance is available.
8165 elsif Scope (Typ) = Gen_Scope then
8166 return Get_Instance_Of (Typ);
8168 -- If the array or access type is not declared in the parent unit,
8169 -- no special processing needed.
8171 elsif not Is_Generic_Type (Typ)
8172 and then Scope (Gen_Scope) /= Scope (Typ)
8173 then
8174 return Get_Instance_Of (Typ);
8176 -- Otherwise, retrieve designated or component type by visibility
8178 else
8179 T := Current_Entity (Typ);
8180 while Present (T) loop
8181 if In_Open_Scopes (Scope (T)) then
8182 return T;
8183 elsif Is_Generic_Actual_Type (T) then
8184 return T;
8185 end if;
8187 T := Homonym (T);
8188 end loop;
8190 return Typ;
8191 end if;
8192 end Find_Actual_Type;
8194 ----------------------------
8195 -- Freeze_Subprogram_Body --
8196 ----------------------------
8198 procedure Freeze_Subprogram_Body
8199 (Inst_Node : Node_Id;
8200 Gen_Body : Node_Id;
8201 Pack_Id : Entity_Id)
8203 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8204 Par : constant Entity_Id := Scope (Gen_Unit);
8205 E_G_Id : Entity_Id;
8206 Enc_G : Entity_Id;
8207 Enc_I : Node_Id;
8208 F_Node : Node_Id;
8210 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8211 -- Find innermost package body that encloses the given node, and which
8212 -- is not a compilation unit. Freeze nodes for the instance, or for its
8213 -- enclosing body, may be inserted after the enclosing_body of the
8214 -- generic unit. Used to determine proper placement of freeze node for
8215 -- both package and subprogram instances.
8217 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8218 -- Find entity for given package body, and locate or create a freeze
8219 -- node for it.
8221 ----------------------------
8222 -- Enclosing_Package_Body --
8223 ----------------------------
8225 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8226 P : Node_Id;
8228 begin
8229 P := Parent (N);
8230 while Present (P)
8231 and then Nkind (Parent (P)) /= N_Compilation_Unit
8232 loop
8233 if Nkind (P) = N_Package_Body then
8234 if Nkind (Parent (P)) = N_Subunit then
8235 return Corresponding_Stub (Parent (P));
8236 else
8237 return P;
8238 end if;
8239 end if;
8241 P := True_Parent (P);
8242 end loop;
8244 return Empty;
8245 end Enclosing_Package_Body;
8247 -------------------------
8248 -- Package_Freeze_Node --
8249 -------------------------
8251 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8252 Id : Entity_Id;
8254 begin
8255 if Nkind (B) = N_Package_Body then
8256 Id := Corresponding_Spec (B);
8257 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8258 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8259 end if;
8261 Ensure_Freeze_Node (Id);
8262 return Freeze_Node (Id);
8263 end Package_Freeze_Node;
8265 -- Start of processing for Freeze_Subprogram_Body
8267 begin
8268 -- If the instance and the generic body appear within the same unit, and
8269 -- the instance precedes the generic, the freeze node for the instance
8270 -- must appear after that of the generic. If the generic is nested
8271 -- within another instance I2, then current instance must be frozen
8272 -- after I2. In both cases, the freeze nodes are those of enclosing
8273 -- packages. Otherwise, the freeze node is placed at the end of the
8274 -- current declarative part.
8276 Enc_G := Enclosing_Package_Body (Gen_Body);
8277 Enc_I := Enclosing_Package_Body (Inst_Node);
8278 Ensure_Freeze_Node (Pack_Id);
8279 F_Node := Freeze_Node (Pack_Id);
8281 if Is_Generic_Instance (Par)
8282 and then Present (Freeze_Node (Par))
8283 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8284 then
8285 -- The parent was a premature instantiation. Insert freeze node at
8286 -- the end the current declarative part.
8288 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8289 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8291 -- Handle the following case:
8293 -- package Parent_Inst is new ...
8294 -- Parent_Inst []
8296 -- procedure P ... -- this body freezes Parent_Inst
8298 -- package Inst is new ...
8300 -- In this particular scenario, the freeze node for Inst must be
8301 -- inserted in the same manner as that of Parent_Inst - before the
8302 -- next source body or at the end of the declarative list (body not
8303 -- available). If body P did not exist and Parent_Inst was frozen
8304 -- after Inst, either by a body following Inst or at the end of the
8305 -- declarative region, the freeze node for Inst must be inserted
8306 -- after that of Parent_Inst. This relation is established by
8307 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8309 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8310 List_Containing (Inst_Node)
8311 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8312 then
8313 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8315 else
8316 Insert_After (Freeze_Node (Par), F_Node);
8317 end if;
8319 -- The body enclosing the instance should be frozen after the body that
8320 -- includes the generic, because the body of the instance may make
8321 -- references to entities therein. If the two are not in the same
8322 -- declarative part, or if the one enclosing the instance is frozen
8323 -- already, freeze the instance at the end of the current declarative
8324 -- part.
8326 elsif Is_Generic_Instance (Par)
8327 and then Present (Freeze_Node (Par))
8328 and then Present (Enc_I)
8329 then
8330 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8331 or else
8332 (Nkind (Enc_I) = N_Package_Body
8333 and then
8334 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8335 then
8336 -- The enclosing package may contain several instances. Rather
8337 -- than computing the earliest point at which to insert its freeze
8338 -- node, we place it at the end of the declarative part of the
8339 -- parent of the generic.
8341 Insert_Freeze_Node_For_Instance
8342 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8343 end if;
8345 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8347 elsif Present (Enc_G)
8348 and then Present (Enc_I)
8349 and then Enc_G /= Enc_I
8350 and then Earlier (Inst_Node, Gen_Body)
8351 then
8352 if Nkind (Enc_G) = N_Package_Body then
8353 E_G_Id :=
8354 Corresponding_Spec (Enc_G);
8355 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8356 E_G_Id :=
8357 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8358 end if;
8360 -- Freeze package that encloses instance, and place node after the
8361 -- package that encloses generic. If enclosing package is already
8362 -- frozen we have to assume it is at the proper place. This may be a
8363 -- potential ABE that requires dynamic checking. Do not add a freeze
8364 -- node if the package that encloses the generic is inside the body
8365 -- that encloses the instance, because the freeze node would be in
8366 -- the wrong scope. Additional contortions needed if the bodies are
8367 -- within a subunit.
8369 declare
8370 Enclosing_Body : Node_Id;
8372 begin
8373 if Nkind (Enc_I) = N_Package_Body_Stub then
8374 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8375 else
8376 Enclosing_Body := Enc_I;
8377 end if;
8379 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8380 Insert_Freeze_Node_For_Instance
8381 (Enc_G, Package_Freeze_Node (Enc_I));
8382 end if;
8383 end;
8385 -- Freeze enclosing subunit before instance
8387 Ensure_Freeze_Node (E_G_Id);
8389 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8390 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8391 end if;
8393 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8395 else
8396 -- If none of the above, insert freeze node at the end of the current
8397 -- declarative part.
8399 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8400 end if;
8401 end Freeze_Subprogram_Body;
8403 ----------------
8404 -- Get_Gen_Id --
8405 ----------------
8407 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8408 begin
8409 return Generic_Renamings.Table (E).Gen_Id;
8410 end Get_Gen_Id;
8412 ---------------------
8413 -- Get_Instance_Of --
8414 ---------------------
8416 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8417 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8419 begin
8420 if Res /= Assoc_Null then
8421 return Generic_Renamings.Table (Res).Act_Id;
8423 else
8424 -- On exit, entity is not instantiated: not a generic parameter, or
8425 -- else parameter of an inner generic unit.
8427 return A;
8428 end if;
8429 end Get_Instance_Of;
8431 ------------------------------------
8432 -- Get_Package_Instantiation_Node --
8433 ------------------------------------
8435 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8436 Decl : Node_Id := Unit_Declaration_Node (A);
8437 Inst : Node_Id;
8439 begin
8440 -- If the Package_Instantiation attribute has been set on the package
8441 -- entity, then use it directly when it (or its Original_Node) refers
8442 -- to an N_Package_Instantiation node. In principle it should be
8443 -- possible to have this field set in all cases, which should be
8444 -- investigated, and would allow this function to be significantly
8445 -- simplified. ???
8447 Inst := Package_Instantiation (A);
8449 if Present (Inst) then
8450 if Nkind (Inst) = N_Package_Instantiation then
8451 return Inst;
8453 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8454 return Original_Node (Inst);
8455 end if;
8456 end if;
8458 -- If the instantiation is a compilation unit that does not need body
8459 -- then the instantiation node has been rewritten as a package
8460 -- declaration for the instance, and we return the original node.
8462 -- If it is a compilation unit and the instance node has not been
8463 -- rewritten, then it is still the unit of the compilation. Finally, if
8464 -- a body is present, this is a parent of the main unit whose body has
8465 -- been compiled for inlining purposes, and the instantiation node has
8466 -- been rewritten with the instance body.
8468 -- Otherwise the instantiation node appears after the declaration. If
8469 -- the entity is a formal package, the declaration may have been
8470 -- rewritten as a generic declaration (in the case of a formal with box)
8471 -- or left as a formal package declaration if it has actuals, and is
8472 -- found with a forward search.
8474 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8475 if Nkind (Decl) = N_Package_Declaration
8476 and then Present (Corresponding_Body (Decl))
8477 then
8478 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8479 end if;
8481 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8482 return Original_Node (Decl);
8483 else
8484 return Unit (Parent (Decl));
8485 end if;
8487 elsif Nkind (Decl) = N_Package_Declaration
8488 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8489 then
8490 return Original_Node (Decl);
8492 else
8493 Inst := Next (Decl);
8494 while not Nkind_In (Inst, N_Package_Instantiation,
8495 N_Formal_Package_Declaration)
8496 loop
8497 Next (Inst);
8498 end loop;
8500 return Inst;
8501 end if;
8502 end Get_Package_Instantiation_Node;
8504 ------------------------
8505 -- Has_Been_Exchanged --
8506 ------------------------
8508 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8509 Next : Elmt_Id;
8511 begin
8512 Next := First_Elmt (Exchanged_Views);
8513 while Present (Next) loop
8514 if Full_View (Node (Next)) = E then
8515 return True;
8516 end if;
8518 Next_Elmt (Next);
8519 end loop;
8521 return False;
8522 end Has_Been_Exchanged;
8524 ----------
8525 -- Hash --
8526 ----------
8528 function Hash (F : Entity_Id) return HTable_Range is
8529 begin
8530 return HTable_Range (F mod HTable_Size);
8531 end Hash;
8533 ------------------------
8534 -- Hide_Current_Scope --
8535 ------------------------
8537 procedure Hide_Current_Scope is
8538 C : constant Entity_Id := Current_Scope;
8539 E : Entity_Id;
8541 begin
8542 Set_Is_Hidden_Open_Scope (C);
8544 E := First_Entity (C);
8545 while Present (E) loop
8546 if Is_Immediately_Visible (E) then
8547 Set_Is_Immediately_Visible (E, False);
8548 Append_Elmt (E, Hidden_Entities);
8549 end if;
8551 Next_Entity (E);
8552 end loop;
8554 -- Make the scope name invisible as well. This is necessary, but might
8555 -- conflict with calls to Rtsfind later on, in case the scope is a
8556 -- predefined one. There is no clean solution to this problem, so for
8557 -- now we depend on the user not redefining Standard itself in one of
8558 -- the parent units.
8560 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8561 Set_Is_Immediately_Visible (C, False);
8562 Append_Elmt (C, Hidden_Entities);
8563 end if;
8565 end Hide_Current_Scope;
8567 --------------
8568 -- Init_Env --
8569 --------------
8571 procedure Init_Env is
8572 Saved : Instance_Env;
8574 begin
8575 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8576 Saved.Exchanged_Views := Exchanged_Views;
8577 Saved.Hidden_Entities := Hidden_Entities;
8578 Saved.Current_Sem_Unit := Current_Sem_Unit;
8579 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8580 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8582 -- Save configuration switches. These may be reset if the unit is a
8583 -- predefined unit, and the current mode is not Ada 2005.
8585 Save_Opt_Config_Switches (Saved.Switches);
8587 Instance_Envs.Append (Saved);
8589 Exchanged_Views := New_Elmt_List;
8590 Hidden_Entities := New_Elmt_List;
8592 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8593 -- this is set properly in Set_Instance_Env.
8595 Current_Instantiated_Parent :=
8596 (Current_Scope, Current_Scope, Assoc_Null);
8597 end Init_Env;
8599 ------------------------------
8600 -- In_Same_Declarative_Part --
8601 ------------------------------
8603 function In_Same_Declarative_Part
8604 (F_Node : Node_Id;
8605 Inst : Node_Id) return Boolean
8607 Decls : constant Node_Id := Parent (F_Node);
8608 Nod : Node_Id;
8610 begin
8611 Nod := Parent (Inst);
8612 while Present (Nod) loop
8613 if Nod = Decls then
8614 return True;
8616 elsif Nkind_In (Nod, N_Subprogram_Body,
8617 N_Package_Body,
8618 N_Package_Declaration,
8619 N_Task_Body,
8620 N_Protected_Body,
8621 N_Block_Statement)
8622 then
8623 return False;
8625 elsif Nkind (Nod) = N_Subunit then
8626 Nod := Corresponding_Stub (Nod);
8628 elsif Nkind (Nod) = N_Compilation_Unit then
8629 return False;
8631 else
8632 Nod := Parent (Nod);
8633 end if;
8634 end loop;
8636 return False;
8637 end In_Same_Declarative_Part;
8639 ---------------------
8640 -- In_Main_Context --
8641 ---------------------
8643 function In_Main_Context (E : Entity_Id) return Boolean is
8644 Context : List_Id;
8645 Clause : Node_Id;
8646 Nam : Node_Id;
8648 begin
8649 if not Is_Compilation_Unit (E)
8650 or else Ekind (E) /= E_Package
8651 or else In_Private_Part (E)
8652 then
8653 return False;
8654 end if;
8656 Context := Context_Items (Cunit (Main_Unit));
8658 Clause := First (Context);
8659 while Present (Clause) loop
8660 if Nkind (Clause) = N_With_Clause then
8661 Nam := Name (Clause);
8663 -- If the current scope is part of the context of the main unit,
8664 -- analysis of the corresponding with_clause is not complete, and
8665 -- the entity is not set. We use the Chars field directly, which
8666 -- might produce false positives in rare cases, but guarantees
8667 -- that we produce all the instance bodies we will need.
8669 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8670 or else (Nkind (Nam) = N_Selected_Component
8671 and then Chars (Selector_Name (Nam)) = Chars (E))
8672 then
8673 return True;
8674 end if;
8675 end if;
8677 Next (Clause);
8678 end loop;
8680 return False;
8681 end In_Main_Context;
8683 ---------------------
8684 -- Inherit_Context --
8685 ---------------------
8687 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8688 Current_Context : List_Id;
8689 Current_Unit : Node_Id;
8690 Item : Node_Id;
8691 New_I : Node_Id;
8693 Clause : Node_Id;
8694 OK : Boolean;
8695 Lib_Unit : Node_Id;
8697 begin
8698 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8700 -- The inherited context is attached to the enclosing compilation
8701 -- unit. This is either the main unit, or the declaration for the
8702 -- main unit (in case the instantiation appears within the package
8703 -- declaration and the main unit is its body).
8705 Current_Unit := Parent (Inst);
8706 while Present (Current_Unit)
8707 and then Nkind (Current_Unit) /= N_Compilation_Unit
8708 loop
8709 Current_Unit := Parent (Current_Unit);
8710 end loop;
8712 Current_Context := Context_Items (Current_Unit);
8714 Item := First (Context_Items (Parent (Gen_Decl)));
8715 while Present (Item) loop
8716 if Nkind (Item) = N_With_Clause then
8717 Lib_Unit := Library_Unit (Item);
8719 -- Take care to prevent direct cyclic with's
8721 if Lib_Unit /= Current_Unit then
8723 -- Do not add a unit if it is already in the context
8725 Clause := First (Current_Context);
8726 OK := True;
8727 while Present (Clause) loop
8728 if Nkind (Clause) = N_With_Clause and then
8729 Library_Unit (Clause) = Lib_Unit
8730 then
8731 OK := False;
8732 exit;
8733 end if;
8735 Next (Clause);
8736 end loop;
8738 if OK then
8739 New_I := New_Copy (Item);
8740 Set_Implicit_With (New_I, True);
8741 Set_Implicit_With_From_Instantiation (New_I, True);
8742 Append (New_I, Current_Context);
8743 end if;
8744 end if;
8745 end if;
8747 Next (Item);
8748 end loop;
8749 end if;
8750 end Inherit_Context;
8752 ----------------
8753 -- Initialize --
8754 ----------------
8756 procedure Initialize is
8757 begin
8758 Generic_Renamings.Init;
8759 Instance_Envs.Init;
8760 Generic_Flags.Init;
8761 Generic_Renamings_HTable.Reset;
8762 Circularity_Detected := False;
8763 Exchanged_Views := No_Elist;
8764 Hidden_Entities := No_Elist;
8765 end Initialize;
8767 -------------------------------------
8768 -- Insert_Freeze_Node_For_Instance --
8769 -------------------------------------
8771 procedure Insert_Freeze_Node_For_Instance
8772 (N : Node_Id;
8773 F_Node : Node_Id)
8775 Decl : Node_Id;
8776 Decls : List_Id;
8777 Inst : Entity_Id;
8778 Par_N : Node_Id;
8780 function Enclosing_Body (N : Node_Id) return Node_Id;
8781 -- Find enclosing package or subprogram body, if any. Freeze node may
8782 -- be placed at end of current declarative list if previous instance
8783 -- and current one have different enclosing bodies.
8785 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8786 -- Find the local instance, if any, that declares the generic that is
8787 -- being instantiated. If present, the freeze node for this instance
8788 -- must follow the freeze node for the previous instance.
8790 --------------------
8791 -- Enclosing_Body --
8792 --------------------
8794 function Enclosing_Body (N : Node_Id) return Node_Id is
8795 P : Node_Id;
8797 begin
8798 P := Parent (N);
8799 while Present (P)
8800 and then Nkind (Parent (P)) /= N_Compilation_Unit
8801 loop
8802 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8803 if Nkind (Parent (P)) = N_Subunit then
8804 return Corresponding_Stub (Parent (P));
8805 else
8806 return P;
8807 end if;
8808 end if;
8810 P := True_Parent (P);
8811 end loop;
8813 return Empty;
8814 end Enclosing_Body;
8816 -----------------------
8817 -- Previous_Instance --
8818 -----------------------
8820 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8821 S : Entity_Id;
8823 begin
8824 S := Scope (Gen);
8825 while Present (S) and then S /= Standard_Standard loop
8826 if Is_Generic_Instance (S)
8827 and then In_Same_Source_Unit (S, N)
8828 then
8829 return S;
8830 end if;
8832 S := Scope (S);
8833 end loop;
8835 return Empty;
8836 end Previous_Instance;
8838 -- Start of processing for Insert_Freeze_Node_For_Instance
8840 begin
8841 if not Is_List_Member (F_Node) then
8842 Decl := N;
8843 Decls := List_Containing (N);
8844 Inst := Entity (F_Node);
8845 Par_N := Parent (Decls);
8847 -- When processing a subprogram instantiation, utilize the actual
8848 -- subprogram instantiation rather than its package wrapper as it
8849 -- carries all the context information.
8851 if Is_Wrapper_Package (Inst) then
8852 Inst := Related_Instance (Inst);
8853 end if;
8855 -- If this is a package instance, check whether the generic is
8856 -- declared in a previous instance and the current instance is
8857 -- not within the previous one.
8859 if Present (Generic_Parent (Parent (Inst)))
8860 and then Is_In_Main_Unit (N)
8861 then
8862 declare
8863 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8864 Par_I : constant Entity_Id :=
8865 Previous_Instance
8866 (Generic_Parent (Parent (Inst)));
8867 Scop : Entity_Id;
8869 begin
8870 if Present (Par_I)
8871 and then Earlier (N, Freeze_Node (Par_I))
8872 then
8873 Scop := Scope (Inst);
8875 -- If the current instance is within the one that contains
8876 -- the generic, the freeze node for the current one must
8877 -- appear in the current declarative part. Ditto, if the
8878 -- current instance is within another package instance or
8879 -- within a body that does not enclose the current instance.
8880 -- In these three cases the freeze node of the previous
8881 -- instance is not relevant.
8883 while Present (Scop) and then Scop /= Standard_Standard loop
8884 exit when Scop = Par_I
8885 or else
8886 (Is_Generic_Instance (Scop)
8887 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8888 Scop := Scope (Scop);
8889 end loop;
8891 -- Previous instance encloses current instance
8893 if Scop = Par_I then
8894 null;
8896 -- If the next node is a source body we must freeze in
8897 -- the current scope as well.
8899 elsif Present (Next (N))
8900 and then Nkind_In (Next (N), N_Subprogram_Body,
8901 N_Package_Body)
8902 and then Comes_From_Source (Next (N))
8903 then
8904 null;
8906 -- Current instance is within an unrelated instance
8908 elsif Is_Generic_Instance (Scop) then
8909 null;
8911 -- Current instance is within an unrelated body
8913 elsif Present (Enclosing_N)
8914 and then Enclosing_N /= Enclosing_Body (Par_I)
8915 then
8916 null;
8918 else
8919 Insert_After (Freeze_Node (Par_I), F_Node);
8920 return;
8921 end if;
8922 end if;
8923 end;
8924 end if;
8926 -- When the instantiation occurs in a package declaration, append the
8927 -- freeze node to the private declarations (if any).
8929 if Nkind (Par_N) = N_Package_Specification
8930 and then Decls = Visible_Declarations (Par_N)
8931 and then Present (Private_Declarations (Par_N))
8932 and then not Is_Empty_List (Private_Declarations (Par_N))
8933 then
8934 Decls := Private_Declarations (Par_N);
8935 Decl := First (Decls);
8936 end if;
8938 -- Determine the proper freeze point of a package instantiation. We
8939 -- adhere to the general rule of a package or subprogram body causing
8940 -- freezing of anything before it in the same declarative region. In
8941 -- this case, the proper freeze point of a package instantiation is
8942 -- before the first source body which follows, or before a stub. This
8943 -- ensures that entities coming from the instance are already frozen
8944 -- and usable in source bodies.
8946 if Nkind (Par_N) /= N_Package_Declaration
8947 and then Ekind (Inst) = E_Package
8948 and then Is_Generic_Instance (Inst)
8949 and then
8950 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8951 then
8952 while Present (Decl) loop
8953 if (Nkind (Decl) in N_Unit_Body
8954 or else
8955 Nkind (Decl) in N_Body_Stub)
8956 and then Comes_From_Source (Decl)
8957 then
8958 Insert_Before (Decl, F_Node);
8959 return;
8960 end if;
8962 Next (Decl);
8963 end loop;
8964 end if;
8966 -- In a package declaration, or if no previous body, insert at end
8967 -- of list.
8969 Set_Sloc (F_Node, Sloc (Last (Decls)));
8970 Insert_After (Last (Decls), F_Node);
8971 end if;
8972 end Insert_Freeze_Node_For_Instance;
8974 ------------------
8975 -- Install_Body --
8976 ------------------
8978 procedure Install_Body
8979 (Act_Body : Node_Id;
8980 N : Node_Id;
8981 Gen_Body : Node_Id;
8982 Gen_Decl : Node_Id)
8984 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
8985 -- Check if the generic definition and the instantiation come from
8986 -- a common scope, in which case the instance must be frozen after
8987 -- the generic body.
8989 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
8990 -- If the instance is nested inside a generic unit, the Sloc of the
8991 -- instance indicates the place of the original definition, not the
8992 -- point of the current enclosing instance. Pending a better usage of
8993 -- Slocs to indicate instantiation places, we determine the place of
8994 -- origin of a node by finding the maximum sloc of any ancestor node.
8995 -- Why is this not equivalent to Top_Level_Location ???
8997 -------------------
8998 -- In_Same_Scope --
8999 -------------------
9001 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9002 Act_Scop : Entity_Id := Scope (Act_Id);
9003 Gen_Scop : Entity_Id := Scope (Gen_Id);
9005 begin
9006 while Act_Scop /= Standard_Standard
9007 and then Gen_Scop /= Standard_Standard
9008 loop
9009 if Act_Scop = Gen_Scop then
9010 return True;
9011 end if;
9013 Act_Scop := Scope (Act_Scop);
9014 Gen_Scop := Scope (Gen_Scop);
9015 end loop;
9017 return False;
9018 end In_Same_Scope;
9020 ---------------
9021 -- True_Sloc --
9022 ---------------
9024 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9025 N1 : Node_Id;
9026 Res : Source_Ptr;
9028 begin
9029 Res := Sloc (N);
9030 N1 := N;
9031 while Present (N1) and then N1 /= Act_Unit loop
9032 if Sloc (N1) > Res then
9033 Res := Sloc (N1);
9034 end if;
9036 N1 := Parent (N1);
9037 end loop;
9039 return Res;
9040 end True_Sloc;
9042 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9043 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9044 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9045 Par : constant Entity_Id := Scope (Gen_Id);
9046 Gen_Unit : constant Node_Id :=
9047 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9049 Body_Unit : Node_Id;
9050 F_Node : Node_Id;
9051 Must_Delay : Boolean;
9052 Orig_Body : Node_Id := Gen_Body;
9054 -- Start of processing for Install_Body
9056 begin
9057 -- Handle first the case of an instance with incomplete actual types.
9058 -- The instance body cannot be placed after the declaration because
9059 -- full views have not been seen yet. Any use of the non-limited views
9060 -- in the instance body requires the presence of a regular with_clause
9061 -- in the enclosing unit, and will fail if this with_clause is missing.
9062 -- We place the instance body at the beginning of the enclosing body,
9063 -- which is the unit being compiled. The freeze node for the instance
9064 -- is then placed after the instance body.
9066 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9067 and then Expander_Active
9068 and then Ekind (Scope (Act_Id)) = E_Package
9069 then
9070 declare
9071 Scop : constant Entity_Id := Scope (Act_Id);
9072 Body_Id : constant Node_Id :=
9073 Corresponding_Body (Unit_Declaration_Node (Scop));
9075 begin
9076 Ensure_Freeze_Node (Act_Id);
9077 F_Node := Freeze_Node (Act_Id);
9078 if Present (Body_Id) then
9079 Set_Is_Frozen (Act_Id, False);
9080 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9081 if Is_List_Member (F_Node) then
9082 Remove (F_Node);
9083 end if;
9085 Insert_After (Act_Body, F_Node);
9086 end if;
9087 end;
9088 return;
9089 end if;
9091 -- If the body is a subunit, the freeze point is the corresponding stub
9092 -- in the current compilation, not the subunit itself.
9094 if Nkind (Parent (Gen_Body)) = N_Subunit then
9095 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9096 else
9097 Orig_Body := Gen_Body;
9098 end if;
9100 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9102 -- If the instantiation and the generic definition appear in the same
9103 -- package declaration, this is an early instantiation. If they appear
9104 -- in the same declarative part, it is an early instantiation only if
9105 -- the generic body appears textually later, and the generic body is
9106 -- also in the main unit.
9108 -- If instance is nested within a subprogram, and the generic body
9109 -- is not, the instance is delayed because the enclosing body is. If
9110 -- instance and body are within the same scope, or the same subprogram
9111 -- body, indicate explicitly that the instance is delayed.
9113 Must_Delay :=
9114 (Gen_Unit = Act_Unit
9115 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9116 N_Package_Declaration)
9117 or else (Gen_Unit = Body_Unit
9118 and then True_Sloc (N, Act_Unit)
9119 < Sloc (Orig_Body)))
9120 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9121 and then In_Same_Scope (Gen_Id, Act_Id));
9123 -- If this is an early instantiation, the freeze node is placed after
9124 -- the generic body. Otherwise, if the generic appears in an instance,
9125 -- we cannot freeze the current instance until the outer one is frozen.
9126 -- This is only relevant if the current instance is nested within some
9127 -- inner scope not itself within the outer instance. If this scope is
9128 -- a package body in the same declarative part as the outer instance,
9129 -- then that body needs to be frozen after the outer instance. Finally,
9130 -- if no delay is needed, we place the freeze node at the end of the
9131 -- current declarative part.
9133 if Expander_Active then
9134 Ensure_Freeze_Node (Act_Id);
9135 F_Node := Freeze_Node (Act_Id);
9137 if Must_Delay then
9138 Insert_After (Orig_Body, F_Node);
9140 elsif Is_Generic_Instance (Par)
9141 and then Present (Freeze_Node (Par))
9142 and then Scope (Act_Id) /= Par
9143 then
9144 -- Freeze instance of inner generic after instance of enclosing
9145 -- generic.
9147 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9149 -- Handle the following case:
9151 -- package Parent_Inst is new ...
9152 -- Parent_Inst []
9154 -- procedure P ... -- this body freezes Parent_Inst
9156 -- package Inst is new ...
9158 -- In this particular scenario, the freeze node for Inst must
9159 -- be inserted in the same manner as that of Parent_Inst,
9160 -- before the next source body or at the end of the declarative
9161 -- list (body not available). If body P did not exist and
9162 -- Parent_Inst was frozen after Inst, either by a body
9163 -- following Inst or at the end of the declarative region,
9164 -- the freeze node for Inst must be inserted after that of
9165 -- Parent_Inst. This relation is established by comparing
9166 -- the Slocs of Parent_Inst freeze node and Inst.
9168 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9169 List_Containing (N)
9170 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9171 then
9172 Insert_Freeze_Node_For_Instance (N, F_Node);
9173 else
9174 Insert_After (Freeze_Node (Par), F_Node);
9175 end if;
9177 -- Freeze package enclosing instance of inner generic after
9178 -- instance of enclosing generic.
9180 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9181 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9182 then
9183 declare
9184 Enclosing : Entity_Id;
9186 begin
9187 Enclosing := Corresponding_Spec (Parent (N));
9189 if No (Enclosing) then
9190 Enclosing := Defining_Entity (Parent (N));
9191 end if;
9193 Insert_Freeze_Node_For_Instance (N, F_Node);
9194 Ensure_Freeze_Node (Enclosing);
9196 if not Is_List_Member (Freeze_Node (Enclosing)) then
9198 -- The enclosing context is a subunit, insert the freeze
9199 -- node after the stub.
9201 if Nkind (Parent (Parent (N))) = N_Subunit then
9202 Insert_Freeze_Node_For_Instance
9203 (Corresponding_Stub (Parent (Parent (N))),
9204 Freeze_Node (Enclosing));
9206 -- The enclosing context is a package with a stub body
9207 -- which has already been replaced by the real body.
9208 -- Insert the freeze node after the actual body.
9210 elsif Ekind (Enclosing) = E_Package
9211 and then Present (Body_Entity (Enclosing))
9212 and then Was_Originally_Stub
9213 (Parent (Body_Entity (Enclosing)))
9214 then
9215 Insert_Freeze_Node_For_Instance
9216 (Parent (Body_Entity (Enclosing)),
9217 Freeze_Node (Enclosing));
9219 -- The parent instance has been frozen before the body of
9220 -- the enclosing package, insert the freeze node after
9221 -- the body.
9223 elsif List_Containing (Freeze_Node (Par)) =
9224 List_Containing (Parent (N))
9225 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9226 then
9227 Insert_Freeze_Node_For_Instance
9228 (Parent (N), Freeze_Node (Enclosing));
9230 else
9231 Insert_After
9232 (Freeze_Node (Par), Freeze_Node (Enclosing));
9233 end if;
9234 end if;
9235 end;
9237 else
9238 Insert_Freeze_Node_For_Instance (N, F_Node);
9239 end if;
9241 else
9242 Insert_Freeze_Node_For_Instance (N, F_Node);
9243 end if;
9244 end if;
9246 Set_Is_Frozen (Act_Id);
9247 Insert_Before (N, Act_Body);
9248 Mark_Rewrite_Insertion (Act_Body);
9249 end Install_Body;
9251 -----------------------------
9252 -- Install_Formal_Packages --
9253 -----------------------------
9255 procedure Install_Formal_Packages (Par : Entity_Id) is
9256 E : Entity_Id;
9257 Gen : Entity_Id;
9258 Gen_E : Entity_Id := Empty;
9260 begin
9261 E := First_Entity (Par);
9263 -- If we are installing an instance parent, locate the formal packages
9264 -- of its generic parent.
9266 if Is_Generic_Instance (Par) then
9267 Gen := Generic_Parent (Package_Specification (Par));
9268 Gen_E := First_Entity (Gen);
9269 end if;
9271 while Present (E) loop
9272 if Ekind (E) = E_Package
9273 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9274 then
9275 -- If this is the renaming for the parent instance, done
9277 if Renamed_Object (E) = Par then
9278 exit;
9280 -- The visibility of a formal of an enclosing generic is already
9281 -- correct.
9283 elsif Denotes_Formal_Package (E) then
9284 null;
9286 elsif Present (Associated_Formal_Package (E)) then
9287 Check_Generic_Actuals (Renamed_Object (E), True);
9288 Set_Is_Hidden (E, False);
9290 -- Find formal package in generic unit that corresponds to
9291 -- (instance of) formal package in instance.
9293 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9294 Next_Entity (Gen_E);
9295 end loop;
9297 if Present (Gen_E) then
9298 Map_Formal_Package_Entities (Gen_E, E);
9299 end if;
9300 end if;
9301 end if;
9303 Next_Entity (E);
9305 if Present (Gen_E) then
9306 Next_Entity (Gen_E);
9307 end if;
9308 end loop;
9309 end Install_Formal_Packages;
9311 --------------------
9312 -- Install_Parent --
9313 --------------------
9315 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9316 Ancestors : constant Elist_Id := New_Elmt_List;
9317 S : constant Entity_Id := Current_Scope;
9318 Inst_Par : Entity_Id;
9319 First_Par : Entity_Id;
9320 Inst_Node : Node_Id;
9321 Gen_Par : Entity_Id;
9322 First_Gen : Entity_Id;
9323 Elmt : Elmt_Id;
9325 procedure Install_Noninstance_Specs (Par : Entity_Id);
9326 -- Install the scopes of noninstance parent units ending with Par
9328 procedure Install_Spec (Par : Entity_Id);
9329 -- The child unit is within the declarative part of the parent, so the
9330 -- declarations within the parent are immediately visible.
9332 -------------------------------
9333 -- Install_Noninstance_Specs --
9334 -------------------------------
9336 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9337 begin
9338 if Present (Par)
9339 and then Par /= Standard_Standard
9340 and then not In_Open_Scopes (Par)
9341 then
9342 Install_Noninstance_Specs (Scope (Par));
9343 Install_Spec (Par);
9344 end if;
9345 end Install_Noninstance_Specs;
9347 ------------------
9348 -- Install_Spec --
9349 ------------------
9351 procedure Install_Spec (Par : Entity_Id) is
9352 Spec : constant Node_Id := Package_Specification (Par);
9354 begin
9355 -- If this parent of the child instance is a top-level unit,
9356 -- then record the unit and its visibility for later resetting in
9357 -- Remove_Parent. We exclude units that are generic instances, as we
9358 -- only want to record this information for the ultimate top-level
9359 -- noninstance parent (is that always correct???).
9361 if Scope (Par) = Standard_Standard
9362 and then not Is_Generic_Instance (Par)
9363 then
9364 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9365 Instance_Parent_Unit := Par;
9366 end if;
9368 -- Open the parent scope and make it and its declarations visible.
9369 -- If this point is not within a body, then only the visible
9370 -- declarations should be made visible, and installation of the
9371 -- private declarations is deferred until the appropriate point
9372 -- within analysis of the spec being instantiated (see the handling
9373 -- of parent visibility in Analyze_Package_Specification). This is
9374 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9375 -- private view problems that occur when compiling instantiations of
9376 -- a generic child of that package (Generic_Dispatching_Constructor).
9377 -- If the instance freezes a tagged type, inlinings of operations
9378 -- from Ada.Tags may need the full view of type Tag. If inlining took
9379 -- proper account of establishing visibility of inlined subprograms'
9380 -- parents then it should be possible to remove this
9381 -- special check. ???
9383 Push_Scope (Par);
9384 Set_Is_Immediately_Visible (Par);
9385 Install_Visible_Declarations (Par);
9386 Set_Use (Visible_Declarations (Spec));
9388 if In_Body or else Is_RTU (Par, Ada_Tags) then
9389 Install_Private_Declarations (Par);
9390 Set_Use (Private_Declarations (Spec));
9391 end if;
9392 end Install_Spec;
9394 -- Start of processing for Install_Parent
9396 begin
9397 -- We need to install the parent instance to compile the instantiation
9398 -- of the child, but the child instance must appear in the current
9399 -- scope. Given that we cannot place the parent above the current scope
9400 -- in the scope stack, we duplicate the current scope and unstack both
9401 -- after the instantiation is complete.
9403 -- If the parent is itself the instantiation of a child unit, we must
9404 -- also stack the instantiation of its parent, and so on. Each such
9405 -- ancestor is the prefix of the name in a prior instantiation.
9407 -- If this is a nested instance, the parent unit itself resolves to
9408 -- a renaming of the parent instance, whose declaration we need.
9410 -- Finally, the parent may be a generic (not an instance) when the
9411 -- child unit appears as a formal package.
9413 Inst_Par := P;
9415 if Present (Renamed_Entity (Inst_Par)) then
9416 Inst_Par := Renamed_Entity (Inst_Par);
9417 end if;
9419 First_Par := Inst_Par;
9421 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9423 First_Gen := Gen_Par;
9425 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9427 -- Load grandparent instance as well
9429 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9431 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9432 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9434 if Present (Renamed_Entity (Inst_Par)) then
9435 Inst_Par := Renamed_Entity (Inst_Par);
9436 end if;
9438 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9440 if Present (Gen_Par) then
9441 Prepend_Elmt (Inst_Par, Ancestors);
9443 else
9444 -- Parent is not the name of an instantiation
9446 Install_Noninstance_Specs (Inst_Par);
9447 exit;
9448 end if;
9450 else
9451 -- Previous error
9453 exit;
9454 end if;
9455 end loop;
9457 if Present (First_Gen) then
9458 Append_Elmt (First_Par, Ancestors);
9459 else
9460 Install_Noninstance_Specs (First_Par);
9461 end if;
9463 if not Is_Empty_Elmt_List (Ancestors) then
9464 Elmt := First_Elmt (Ancestors);
9465 while Present (Elmt) loop
9466 Install_Spec (Node (Elmt));
9467 Install_Formal_Packages (Node (Elmt));
9468 Next_Elmt (Elmt);
9469 end loop;
9470 end if;
9472 if not In_Body then
9473 Push_Scope (S);
9474 end if;
9475 end Install_Parent;
9477 -------------------------------
9478 -- Install_Hidden_Primitives --
9479 -------------------------------
9481 procedure Install_Hidden_Primitives
9482 (Prims_List : in out Elist_Id;
9483 Gen_T : Entity_Id;
9484 Act_T : Entity_Id)
9486 Elmt : Elmt_Id;
9487 List : Elist_Id := No_Elist;
9488 Prim_G_Elmt : Elmt_Id;
9489 Prim_A_Elmt : Elmt_Id;
9490 Prim_G : Node_Id;
9491 Prim_A : Node_Id;
9493 begin
9494 -- No action needed in case of serious errors because we cannot trust
9495 -- in the order of primitives
9497 if Serious_Errors_Detected > 0 then
9498 return;
9500 -- No action possible if we don't have available the list of primitive
9501 -- operations
9503 elsif No (Gen_T)
9504 or else not Is_Record_Type (Gen_T)
9505 or else not Is_Tagged_Type (Gen_T)
9506 or else not Is_Record_Type (Act_T)
9507 or else not Is_Tagged_Type (Act_T)
9508 then
9509 return;
9511 -- There is no need to handle interface types since their primitives
9512 -- cannot be hidden
9514 elsif Is_Interface (Gen_T) then
9515 return;
9516 end if;
9518 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9520 if not Is_Class_Wide_Type (Act_T) then
9521 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9522 else
9523 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9524 end if;
9526 loop
9527 -- Skip predefined primitives in the generic formal
9529 while Present (Prim_G_Elmt)
9530 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9531 loop
9532 Next_Elmt (Prim_G_Elmt);
9533 end loop;
9535 -- Skip predefined primitives in the generic actual
9537 while Present (Prim_A_Elmt)
9538 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9539 loop
9540 Next_Elmt (Prim_A_Elmt);
9541 end loop;
9543 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9545 Prim_G := Node (Prim_G_Elmt);
9546 Prim_A := Node (Prim_A_Elmt);
9548 -- There is no need to handle interface primitives because their
9549 -- primitives are not hidden
9551 exit when Present (Interface_Alias (Prim_G));
9553 -- Here we install one hidden primitive
9555 if Chars (Prim_G) /= Chars (Prim_A)
9556 and then Has_Suffix (Prim_A, 'P')
9557 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9558 then
9559 Set_Chars (Prim_A, Chars (Prim_G));
9560 Append_New_Elmt (Prim_A, To => List);
9561 end if;
9563 Next_Elmt (Prim_A_Elmt);
9564 Next_Elmt (Prim_G_Elmt);
9565 end loop;
9567 -- Append the elements to the list of temporarily visible primitives
9568 -- avoiding duplicates.
9570 if Present (List) then
9571 if No (Prims_List) then
9572 Prims_List := New_Elmt_List;
9573 end if;
9575 Elmt := First_Elmt (List);
9576 while Present (Elmt) loop
9577 Append_Unique_Elmt (Node (Elmt), Prims_List);
9578 Next_Elmt (Elmt);
9579 end loop;
9580 end if;
9581 end Install_Hidden_Primitives;
9583 -------------------------------
9584 -- Restore_Hidden_Primitives --
9585 -------------------------------
9587 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9588 Prim_Elmt : Elmt_Id;
9589 Prim : Node_Id;
9591 begin
9592 if Prims_List /= No_Elist then
9593 Prim_Elmt := First_Elmt (Prims_List);
9594 while Present (Prim_Elmt) loop
9595 Prim := Node (Prim_Elmt);
9596 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9597 Next_Elmt (Prim_Elmt);
9598 end loop;
9600 Prims_List := No_Elist;
9601 end if;
9602 end Restore_Hidden_Primitives;
9604 --------------------------------
9605 -- Instantiate_Formal_Package --
9606 --------------------------------
9608 function Instantiate_Formal_Package
9609 (Formal : Node_Id;
9610 Actual : Node_Id;
9611 Analyzed_Formal : Node_Id) return List_Id
9613 Loc : constant Source_Ptr := Sloc (Actual);
9614 Actual_Pack : Entity_Id;
9615 Formal_Pack : Entity_Id;
9616 Gen_Parent : Entity_Id;
9617 Decls : List_Id;
9618 Nod : Node_Id;
9619 Parent_Spec : Node_Id;
9621 procedure Find_Matching_Actual
9622 (F : Node_Id;
9623 Act : in out Entity_Id);
9624 -- We need to associate each formal entity in the formal package with
9625 -- the corresponding entity in the actual package. The actual package
9626 -- has been analyzed and possibly expanded, and as a result there is
9627 -- no one-to-one correspondence between the two lists (for example,
9628 -- the actual may include subtypes, itypes, and inherited primitive
9629 -- operations, interspersed among the renaming declarations for the
9630 -- actuals). We retrieve the corresponding actual by name because each
9631 -- actual has the same name as the formal, and they do appear in the
9632 -- same order.
9634 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9635 -- Retrieve entity of defining entity of generic formal parameter.
9636 -- Only the declarations of formals need to be considered when
9637 -- linking them to actuals, but the declarative list may include
9638 -- internal entities generated during analysis, and those are ignored.
9640 procedure Match_Formal_Entity
9641 (Formal_Node : Node_Id;
9642 Formal_Ent : Entity_Id;
9643 Actual_Ent : Entity_Id);
9644 -- Associates the formal entity with the actual. In the case where
9645 -- Formal_Ent is a formal package, this procedure iterates through all
9646 -- of its formals and enters associations between the actuals occurring
9647 -- in the formal package's corresponding actual package (given by
9648 -- Actual_Ent) and the formal package's formal parameters. This
9649 -- procedure recurses if any of the parameters is itself a package.
9651 function Is_Instance_Of
9652 (Act_Spec : Entity_Id;
9653 Gen_Anc : Entity_Id) return Boolean;
9654 -- The actual can be an instantiation of a generic within another
9655 -- instance, in which case there is no direct link from it to the
9656 -- original generic ancestor. In that case, we recognize that the
9657 -- ultimate ancestor is the same by examining names and scopes.
9659 procedure Process_Nested_Formal (Formal : Entity_Id);
9660 -- If the current formal is declared with a box, its own formals are
9661 -- visible in the instance, as they were in the generic, and their
9662 -- Hidden flag must be reset. If some of these formals are themselves
9663 -- packages declared with a box, the processing must be recursive.
9665 --------------------------
9666 -- Find_Matching_Actual --
9667 --------------------------
9669 procedure Find_Matching_Actual
9670 (F : Node_Id;
9671 Act : in out Entity_Id)
9673 Formal_Ent : Entity_Id;
9675 begin
9676 case Nkind (Original_Node (F)) is
9677 when N_Formal_Object_Declaration
9678 | N_Formal_Type_Declaration
9680 Formal_Ent := Defining_Identifier (F);
9682 while Chars (Act) /= Chars (Formal_Ent) loop
9683 Next_Entity (Act);
9684 end loop;
9686 when N_Formal_Package_Declaration
9687 | N_Formal_Subprogram_Declaration
9688 | N_Generic_Package_Declaration
9689 | N_Package_Declaration
9691 Formal_Ent := Defining_Entity (F);
9693 while Chars (Act) /= Chars (Formal_Ent) loop
9694 Next_Entity (Act);
9695 end loop;
9697 when others =>
9698 raise Program_Error;
9699 end case;
9700 end Find_Matching_Actual;
9702 -------------------------
9703 -- Match_Formal_Entity --
9704 -------------------------
9706 procedure Match_Formal_Entity
9707 (Formal_Node : Node_Id;
9708 Formal_Ent : Entity_Id;
9709 Actual_Ent : Entity_Id)
9711 Act_Pkg : Entity_Id;
9713 begin
9714 Set_Instance_Of (Formal_Ent, Actual_Ent);
9716 if Ekind (Actual_Ent) = E_Package then
9718 -- Record associations for each parameter
9720 Act_Pkg := Actual_Ent;
9722 declare
9723 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9724 F_Ent : Entity_Id;
9725 F_Node : Node_Id;
9727 Gen_Decl : Node_Id;
9728 Formals : List_Id;
9729 Actual : Entity_Id;
9731 begin
9732 -- Retrieve the actual given in the formal package declaration
9734 Actual := Entity (Name (Original_Node (Formal_Node)));
9736 -- The actual in the formal package declaration may be a
9737 -- renamed generic package, in which case we want to retrieve
9738 -- the original generic in order to traverse its formal part.
9740 if Present (Renamed_Entity (Actual)) then
9741 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9742 else
9743 Gen_Decl := Unit_Declaration_Node (Actual);
9744 end if;
9746 Formals := Generic_Formal_Declarations (Gen_Decl);
9748 if Present (Formals) then
9749 F_Node := First_Non_Pragma (Formals);
9750 else
9751 F_Node := Empty;
9752 end if;
9754 while Present (A_Ent)
9755 and then Present (F_Node)
9756 and then A_Ent /= First_Private_Entity (Act_Pkg)
9757 loop
9758 F_Ent := Get_Formal_Entity (F_Node);
9760 if Present (F_Ent) then
9762 -- This is a formal of the original package. Record
9763 -- association and recurse.
9765 Find_Matching_Actual (F_Node, A_Ent);
9766 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9767 Next_Entity (A_Ent);
9768 end if;
9770 Next_Non_Pragma (F_Node);
9771 end loop;
9772 end;
9773 end if;
9774 end Match_Formal_Entity;
9776 -----------------------
9777 -- Get_Formal_Entity --
9778 -----------------------
9780 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9781 Kind : constant Node_Kind := Nkind (Original_Node (N));
9782 begin
9783 case Kind is
9784 when N_Formal_Object_Declaration =>
9785 return Defining_Identifier (N);
9787 when N_Formal_Type_Declaration =>
9788 return Defining_Identifier (N);
9790 when N_Formal_Subprogram_Declaration =>
9791 return Defining_Unit_Name (Specification (N));
9793 when N_Formal_Package_Declaration =>
9794 return Defining_Identifier (Original_Node (N));
9796 when N_Generic_Package_Declaration =>
9797 return Defining_Identifier (Original_Node (N));
9799 -- All other declarations are introduced by semantic analysis and
9800 -- have no match in the actual.
9802 when others =>
9803 return Empty;
9804 end case;
9805 end Get_Formal_Entity;
9807 --------------------
9808 -- Is_Instance_Of --
9809 --------------------
9811 function Is_Instance_Of
9812 (Act_Spec : Entity_Id;
9813 Gen_Anc : Entity_Id) return Boolean
9815 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9817 begin
9818 if No (Gen_Par) then
9819 return False;
9821 -- Simplest case: the generic parent of the actual is the formal
9823 elsif Gen_Par = Gen_Anc then
9824 return True;
9826 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9827 return False;
9829 -- The actual may be obtained through several instantiations. Its
9830 -- scope must itself be an instance of a generic declared in the
9831 -- same scope as the formal. Any other case is detected above.
9833 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9834 return False;
9836 else
9837 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9838 end if;
9839 end Is_Instance_Of;
9841 ---------------------------
9842 -- Process_Nested_Formal --
9843 ---------------------------
9845 procedure Process_Nested_Formal (Formal : Entity_Id) is
9846 Ent : Entity_Id;
9848 begin
9849 if Present (Associated_Formal_Package (Formal))
9850 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9851 then
9852 Ent := First_Entity (Formal);
9853 while Present (Ent) loop
9854 Set_Is_Hidden (Ent, False);
9855 Set_Is_Visible_Formal (Ent);
9856 Set_Is_Potentially_Use_Visible
9857 (Ent, Is_Potentially_Use_Visible (Formal));
9859 if Ekind (Ent) = E_Package then
9860 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9861 Process_Nested_Formal (Ent);
9862 end if;
9864 Next_Entity (Ent);
9865 end loop;
9866 end if;
9867 end Process_Nested_Formal;
9869 -- Start of processing for Instantiate_Formal_Package
9871 begin
9872 Analyze (Actual);
9874 if not Is_Entity_Name (Actual)
9875 or else Ekind (Entity (Actual)) /= E_Package
9876 then
9877 Error_Msg_N
9878 ("expect package instance to instantiate formal", Actual);
9879 Abandon_Instantiation (Actual);
9880 raise Program_Error;
9882 else
9883 Actual_Pack := Entity (Actual);
9884 Set_Is_Instantiated (Actual_Pack);
9886 -- The actual may be a renamed package, or an outer generic formal
9887 -- package whose instantiation is converted into a renaming.
9889 if Present (Renamed_Object (Actual_Pack)) then
9890 Actual_Pack := Renamed_Object (Actual_Pack);
9891 end if;
9893 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9894 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9895 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9896 else
9897 Gen_Parent :=
9898 Generic_Parent (Specification (Analyzed_Formal));
9899 Formal_Pack :=
9900 Defining_Unit_Name (Specification (Analyzed_Formal));
9901 end if;
9903 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9904 Parent_Spec := Package_Specification (Actual_Pack);
9905 else
9906 Parent_Spec := Parent (Actual_Pack);
9907 end if;
9909 if Gen_Parent = Any_Id then
9910 Error_Msg_N
9911 ("previous error in declaration of formal package", Actual);
9912 Abandon_Instantiation (Actual);
9914 elsif
9915 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9916 then
9917 null;
9919 else
9920 Error_Msg_NE
9921 ("actual parameter must be instance of&", Actual, Gen_Parent);
9922 Abandon_Instantiation (Actual);
9923 end if;
9925 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9926 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9928 Nod :=
9929 Make_Package_Renaming_Declaration (Loc,
9930 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9931 Name => New_Occurrence_Of (Actual_Pack, Loc));
9933 Set_Associated_Formal_Package
9934 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9935 Decls := New_List (Nod);
9937 -- If the formal F has a box, then the generic declarations are
9938 -- visible in the generic G. In an instance of G, the corresponding
9939 -- entities in the actual for F (which are the actuals for the
9940 -- instantiation of the generic that F denotes) must also be made
9941 -- visible for analysis of the current instance. On exit from the
9942 -- current instance, those entities are made private again. If the
9943 -- actual is currently in use, these entities are also use-visible.
9945 -- The loop through the actual entities also steps through the formal
9946 -- entities and enters associations from formals to actuals into the
9947 -- renaming map. This is necessary to properly handle checking of
9948 -- actual parameter associations for later formals that depend on
9949 -- actuals declared in the formal package.
9951 -- In Ada 2005, partial parameterization requires that we make
9952 -- visible the actuals corresponding to formals that were defaulted
9953 -- in the formal package. There formals are identified because they
9954 -- remain formal generics within the formal package, rather than
9955 -- being renamings of the actuals supplied.
9957 declare
9958 Gen_Decl : constant Node_Id :=
9959 Unit_Declaration_Node (Gen_Parent);
9960 Formals : constant List_Id :=
9961 Generic_Formal_Declarations (Gen_Decl);
9963 Actual_Ent : Entity_Id;
9964 Actual_Of_Formal : Node_Id;
9965 Formal_Node : Node_Id;
9966 Formal_Ent : Entity_Id;
9968 begin
9969 if Present (Formals) then
9970 Formal_Node := First_Non_Pragma (Formals);
9971 else
9972 Formal_Node := Empty;
9973 end if;
9975 Actual_Ent := First_Entity (Actual_Pack);
9976 Actual_Of_Formal :=
9977 First (Visible_Declarations (Specification (Analyzed_Formal)));
9978 while Present (Actual_Ent)
9979 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9980 loop
9981 if Present (Formal_Node) then
9982 Formal_Ent := Get_Formal_Entity (Formal_Node);
9984 if Present (Formal_Ent) then
9985 Find_Matching_Actual (Formal_Node, Actual_Ent);
9986 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9988 -- We iterate at the same time over the actuals of the
9989 -- local package created for the formal, to determine
9990 -- which one of the formals of the original generic were
9991 -- defaulted in the formal. The corresponding actual
9992 -- entities are visible in the enclosing instance.
9994 if Box_Present (Formal)
9995 or else
9996 (Present (Actual_Of_Formal)
9997 and then
9998 Is_Generic_Formal
9999 (Get_Formal_Entity (Actual_Of_Formal)))
10000 then
10001 Set_Is_Hidden (Actual_Ent, False);
10002 Set_Is_Visible_Formal (Actual_Ent);
10003 Set_Is_Potentially_Use_Visible
10004 (Actual_Ent, In_Use (Actual_Pack));
10006 if Ekind (Actual_Ent) = E_Package then
10007 Process_Nested_Formal (Actual_Ent);
10008 end if;
10010 else
10011 Set_Is_Hidden (Actual_Ent);
10012 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10013 end if;
10014 end if;
10016 Next_Non_Pragma (Formal_Node);
10017 Next (Actual_Of_Formal);
10019 else
10020 -- No further formals to match, but the generic part may
10021 -- contain inherited operation that are not hidden in the
10022 -- enclosing instance.
10024 Next_Entity (Actual_Ent);
10025 end if;
10026 end loop;
10028 -- Inherited subprograms generated by formal derived types are
10029 -- also visible if the types are.
10031 Actual_Ent := First_Entity (Actual_Pack);
10032 while Present (Actual_Ent)
10033 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10034 loop
10035 if Is_Overloadable (Actual_Ent)
10036 and then
10037 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10038 and then
10039 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10040 then
10041 Set_Is_Hidden (Actual_Ent, False);
10042 Set_Is_Potentially_Use_Visible
10043 (Actual_Ent, In_Use (Actual_Pack));
10044 end if;
10046 Next_Entity (Actual_Ent);
10047 end loop;
10048 end;
10050 -- If the formal is not declared with a box, reanalyze it as an
10051 -- abbreviated instantiation, to verify the matching rules of 12.7.
10052 -- The actual checks are performed after the generic associations
10053 -- have been analyzed, to guarantee the same visibility for this
10054 -- instantiation and for the actuals.
10056 -- In Ada 2005, the generic associations for the formal can include
10057 -- defaulted parameters. These are ignored during check. This
10058 -- internal instantiation is removed from the tree after conformance
10059 -- checking, because it contains formal declarations for those
10060 -- defaulted parameters, and those should not reach the back-end.
10062 if not Box_Present (Formal) then
10063 declare
10064 I_Pack : constant Entity_Id :=
10065 Make_Temporary (Sloc (Actual), 'P');
10067 begin
10068 Set_Is_Internal (I_Pack);
10070 Append_To (Decls,
10071 Make_Package_Instantiation (Sloc (Actual),
10072 Defining_Unit_Name => I_Pack,
10073 Name =>
10074 New_Occurrence_Of
10075 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10076 Generic_Associations => Generic_Associations (Formal)));
10077 end;
10078 end if;
10080 return Decls;
10081 end if;
10082 end Instantiate_Formal_Package;
10084 -----------------------------------
10085 -- Instantiate_Formal_Subprogram --
10086 -----------------------------------
10088 function Instantiate_Formal_Subprogram
10089 (Formal : Node_Id;
10090 Actual : Node_Id;
10091 Analyzed_Formal : Node_Id) return Node_Id
10093 Analyzed_S : constant Entity_Id :=
10094 Defining_Unit_Name (Specification (Analyzed_Formal));
10095 Formal_Sub : constant Entity_Id :=
10096 Defining_Unit_Name (Specification (Formal));
10098 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10099 -- If the generic is a child unit, the parent has been installed on the
10100 -- scope stack, but a default subprogram cannot resolve to something
10101 -- on the parent because that parent is not really part of the visible
10102 -- context (it is there to resolve explicit local entities). If the
10103 -- default has resolved in this way, we remove the entity from immediate
10104 -- visibility and analyze the node again to emit an error message or
10105 -- find another visible candidate.
10107 procedure Valid_Actual_Subprogram (Act : Node_Id);
10108 -- Perform legality check and raise exception on failure
10110 -----------------------
10111 -- From_Parent_Scope --
10112 -----------------------
10114 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10115 Gen_Scope : Node_Id;
10117 begin
10118 Gen_Scope := Scope (Analyzed_S);
10119 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10120 if Scope (Subp) = Scope (Gen_Scope) then
10121 return True;
10122 end if;
10124 Gen_Scope := Scope (Gen_Scope);
10125 end loop;
10127 return False;
10128 end From_Parent_Scope;
10130 -----------------------------
10131 -- Valid_Actual_Subprogram --
10132 -----------------------------
10134 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10135 Act_E : Entity_Id;
10137 begin
10138 if Is_Entity_Name (Act) then
10139 Act_E := Entity (Act);
10141 elsif Nkind (Act) = N_Selected_Component
10142 and then Is_Entity_Name (Selector_Name (Act))
10143 then
10144 Act_E := Entity (Selector_Name (Act));
10146 else
10147 Act_E := Empty;
10148 end if;
10150 if (Present (Act_E) and then Is_Overloadable (Act_E))
10151 or else Nkind_In (Act, N_Attribute_Reference,
10152 N_Indexed_Component,
10153 N_Character_Literal,
10154 N_Explicit_Dereference)
10155 then
10156 return;
10157 end if;
10159 Error_Msg_NE
10160 ("expect subprogram or entry name in instantiation of &",
10161 Instantiation_Node, Formal_Sub);
10162 Abandon_Instantiation (Instantiation_Node);
10163 end Valid_Actual_Subprogram;
10165 -- Local variables
10167 Decl_Node : Node_Id;
10168 Loc : Source_Ptr;
10169 Nam : Node_Id;
10170 New_Spec : Node_Id;
10171 New_Subp : Entity_Id;
10173 -- Start of processing for Instantiate_Formal_Subprogram
10175 begin
10176 New_Spec := New_Copy_Tree (Specification (Formal));
10178 -- The tree copy has created the proper instantiation sloc for the
10179 -- new specification. Use this location for all other constructed
10180 -- declarations.
10182 Loc := Sloc (Defining_Unit_Name (New_Spec));
10184 -- Create new entity for the actual (New_Copy_Tree does not), and
10185 -- indicate that it is an actual.
10187 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10188 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10189 Set_Is_Generic_Actual_Subprogram (New_Subp);
10190 Set_Defining_Unit_Name (New_Spec, New_Subp);
10192 -- Create new entities for the each of the formals in the specification
10193 -- of the renaming declaration built for the actual.
10195 if Present (Parameter_Specifications (New_Spec)) then
10196 declare
10197 F : Node_Id;
10198 F_Id : Entity_Id;
10200 begin
10201 F := First (Parameter_Specifications (New_Spec));
10202 while Present (F) loop
10203 F_Id := Defining_Identifier (F);
10205 Set_Defining_Identifier (F,
10206 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10207 Next (F);
10208 end loop;
10209 end;
10210 end if;
10212 -- Find entity of actual. If the actual is an attribute reference, it
10213 -- cannot be resolved here (its formal is missing) but is handled
10214 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10215 -- fully resolved subsequently, when the renaming declaration for the
10216 -- formal is analyzed. If it is an explicit dereference, resolve the
10217 -- prefix but not the actual itself, to prevent interpretation as call.
10219 if Present (Actual) then
10220 Loc := Sloc (Actual);
10221 Set_Sloc (New_Spec, Loc);
10223 if Nkind (Actual) = N_Operator_Symbol then
10224 Find_Direct_Name (Actual);
10226 elsif Nkind (Actual) = N_Explicit_Dereference then
10227 Analyze (Prefix (Actual));
10229 elsif Nkind (Actual) /= N_Attribute_Reference then
10230 Analyze (Actual);
10231 end if;
10233 Valid_Actual_Subprogram (Actual);
10234 Nam := Actual;
10236 elsif Present (Default_Name (Formal)) then
10237 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10238 N_Selected_Component,
10239 N_Indexed_Component,
10240 N_Character_Literal)
10241 and then Present (Entity (Default_Name (Formal)))
10242 then
10243 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10244 else
10245 Nam := New_Copy (Default_Name (Formal));
10246 Set_Sloc (Nam, Loc);
10247 end if;
10249 elsif Box_Present (Formal) then
10251 -- Actual is resolved at the point of instantiation. Create an
10252 -- identifier or operator with the same name as the formal.
10254 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10255 Nam :=
10256 Make_Operator_Symbol (Loc,
10257 Chars => Chars (Formal_Sub),
10258 Strval => No_String);
10259 else
10260 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10261 end if;
10263 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10264 and then Null_Present (Specification (Formal))
10265 then
10266 -- Generate null body for procedure, for use in the instance
10268 Decl_Node :=
10269 Make_Subprogram_Body (Loc,
10270 Specification => New_Spec,
10271 Declarations => New_List,
10272 Handled_Statement_Sequence =>
10273 Make_Handled_Sequence_Of_Statements (Loc,
10274 Statements => New_List (Make_Null_Statement (Loc))));
10276 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10277 return Decl_Node;
10279 else
10280 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10281 Error_Msg_NE
10282 ("missing actual&", Instantiation_Node, Formal_Sub);
10283 Error_Msg_NE
10284 ("\in instantiation of & declared#",
10285 Instantiation_Node, Scope (Analyzed_S));
10286 Abandon_Instantiation (Instantiation_Node);
10287 end if;
10289 Decl_Node :=
10290 Make_Subprogram_Renaming_Declaration (Loc,
10291 Specification => New_Spec,
10292 Name => Nam);
10294 -- If we do not have an actual and the formal specified <> then set to
10295 -- get proper default.
10297 if No (Actual) and then Box_Present (Formal) then
10298 Set_From_Default (Decl_Node);
10299 end if;
10301 -- Gather possible interpretations for the actual before analyzing the
10302 -- instance. If overloaded, it will be resolved when analyzing the
10303 -- renaming declaration.
10305 if Box_Present (Formal) and then No (Actual) then
10306 Analyze (Nam);
10308 if Is_Child_Unit (Scope (Analyzed_S))
10309 and then Present (Entity (Nam))
10310 then
10311 if not Is_Overloaded (Nam) then
10312 if From_Parent_Scope (Entity (Nam)) then
10313 Set_Is_Immediately_Visible (Entity (Nam), False);
10314 Set_Entity (Nam, Empty);
10315 Set_Etype (Nam, Empty);
10317 Analyze (Nam);
10318 Set_Is_Immediately_Visible (Entity (Nam));
10319 end if;
10321 else
10322 declare
10323 I : Interp_Index;
10324 It : Interp;
10326 begin
10327 Get_First_Interp (Nam, I, It);
10328 while Present (It.Nam) loop
10329 if From_Parent_Scope (It.Nam) then
10330 Remove_Interp (I);
10331 end if;
10333 Get_Next_Interp (I, It);
10334 end loop;
10335 end;
10336 end if;
10337 end if;
10338 end if;
10340 -- The generic instantiation freezes the actual. This can only be done
10341 -- once the actual is resolved, in the analysis of the renaming
10342 -- declaration. To make the formal subprogram entity available, we set
10343 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10344 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10345 -- of formal abstract subprograms.
10347 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10349 -- We cannot analyze the renaming declaration, and thus find the actual,
10350 -- until all the actuals are assembled in the instance. For subsequent
10351 -- checks of other actuals, indicate the node that will hold the
10352 -- instance of this formal.
10354 Set_Instance_Of (Analyzed_S, Nam);
10356 if Nkind (Actual) = N_Selected_Component
10357 and then Is_Task_Type (Etype (Prefix (Actual)))
10358 and then not Is_Frozen (Etype (Prefix (Actual)))
10359 then
10360 -- The renaming declaration will create a body, which must appear
10361 -- outside of the instantiation, We move the renaming declaration
10362 -- out of the instance, and create an additional renaming inside,
10363 -- to prevent freezing anomalies.
10365 declare
10366 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10368 begin
10369 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10370 Insert_Before (Instantiation_Node, Decl_Node);
10371 Analyze (Decl_Node);
10373 -- Now create renaming within the instance
10375 Decl_Node :=
10376 Make_Subprogram_Renaming_Declaration (Loc,
10377 Specification => New_Copy_Tree (New_Spec),
10378 Name => New_Occurrence_Of (Anon_Id, Loc));
10380 Set_Defining_Unit_Name (Specification (Decl_Node),
10381 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10382 end;
10383 end if;
10385 return Decl_Node;
10386 end Instantiate_Formal_Subprogram;
10388 ------------------------
10389 -- Instantiate_Object --
10390 ------------------------
10392 function Instantiate_Object
10393 (Formal : Node_Id;
10394 Actual : Node_Id;
10395 Analyzed_Formal : Node_Id) return List_Id
10397 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10398 A_Gen_Obj : constant Entity_Id :=
10399 Defining_Identifier (Analyzed_Formal);
10400 Acc_Def : Node_Id := Empty;
10401 Act_Assoc : constant Node_Id := Parent (Actual);
10402 Actual_Decl : Node_Id := Empty;
10403 Decl_Node : Node_Id;
10404 Def : Node_Id;
10405 Ftyp : Entity_Id;
10406 List : constant List_Id := New_List;
10407 Loc : constant Source_Ptr := Sloc (Actual);
10408 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10409 Subt_Decl : Node_Id := Empty;
10410 Subt_Mark : Node_Id := Empty;
10412 function Copy_Access_Def return Node_Id;
10413 -- If formal is an anonymous access, copy access definition of formal
10414 -- for generated object declaration.
10416 ---------------------
10417 -- Copy_Access_Def --
10418 ---------------------
10420 function Copy_Access_Def return Node_Id is
10421 begin
10422 Def := New_Copy_Tree (Acc_Def);
10424 -- In addition, if formal is an access to subprogram we need to
10425 -- generate new formals for the signature of the default, so that
10426 -- the tree is properly formatted for ASIS use.
10428 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10429 declare
10430 Par_Spec : Node_Id;
10431 begin
10432 Par_Spec :=
10433 First (Parameter_Specifications
10434 (Access_To_Subprogram_Definition (Def)));
10435 while Present (Par_Spec) loop
10436 Set_Defining_Identifier (Par_Spec,
10437 Make_Defining_Identifier (Sloc (Acc_Def),
10438 Chars => Chars (Defining_Identifier (Par_Spec))));
10439 Next (Par_Spec);
10440 end loop;
10441 end;
10442 end if;
10444 return Def;
10445 end Copy_Access_Def;
10447 -- Start of processing for Instantiate_Object
10449 begin
10450 -- Formal may be an anonymous access
10452 if Present (Subtype_Mark (Formal)) then
10453 Subt_Mark := Subtype_Mark (Formal);
10454 else
10455 Check_Access_Definition (Formal);
10456 Acc_Def := Access_Definition (Formal);
10457 end if;
10459 -- Sloc for error message on missing actual
10461 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10463 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10464 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10465 end if;
10467 Set_Parent (List, Parent (Actual));
10469 -- OUT present
10471 if Out_Present (Formal) then
10473 -- An IN OUT generic actual must be a name. The instantiation is a
10474 -- renaming declaration. The actual is the name being renamed. We
10475 -- use the actual directly, rather than a copy, because it is not
10476 -- used further in the list of actuals, and because a copy or a use
10477 -- of relocate_node is incorrect if the instance is nested within a
10478 -- generic. In order to simplify ASIS searches, the Generic_Parent
10479 -- field links the declaration to the generic association.
10481 if No (Actual) then
10482 Error_Msg_NE
10483 ("missing actual &",
10484 Instantiation_Node, Gen_Obj);
10485 Error_Msg_NE
10486 ("\in instantiation of & declared#",
10487 Instantiation_Node, Scope (A_Gen_Obj));
10488 Abandon_Instantiation (Instantiation_Node);
10489 end if;
10491 if Present (Subt_Mark) then
10492 Decl_Node :=
10493 Make_Object_Renaming_Declaration (Loc,
10494 Defining_Identifier => New_Copy (Gen_Obj),
10495 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10496 Name => Actual);
10498 else pragma Assert (Present (Acc_Def));
10499 Decl_Node :=
10500 Make_Object_Renaming_Declaration (Loc,
10501 Defining_Identifier => New_Copy (Gen_Obj),
10502 Access_Definition => New_Copy_Tree (Acc_Def),
10503 Name => Actual);
10504 end if;
10506 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10508 -- The analysis of the actual may produce Insert_Action nodes, so
10509 -- the declaration must have a context in which to attach them.
10511 Append (Decl_Node, List);
10512 Analyze (Actual);
10514 -- Return if the analysis of the actual reported some error
10516 if Etype (Actual) = Any_Type then
10517 return List;
10518 end if;
10520 -- This check is performed here because Analyze_Object_Renaming will
10521 -- not check it when Comes_From_Source is False. Note though that the
10522 -- check for the actual being the name of an object will be performed
10523 -- in Analyze_Object_Renaming.
10525 if Is_Object_Reference (Actual)
10526 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10527 then
10528 Error_Msg_N
10529 ("illegal discriminant-dependent component for in out parameter",
10530 Actual);
10531 end if;
10533 -- The actual has to be resolved in order to check that it is a
10534 -- variable (due to cases such as F (1), where F returns access to
10535 -- an array, and for overloaded prefixes).
10537 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10539 -- If the type of the formal is not itself a formal, and the current
10540 -- unit is a child unit, the formal type must be declared in a
10541 -- parent, and must be retrieved by visibility.
10543 if Ftyp = Orig_Ftyp
10544 and then Is_Generic_Unit (Scope (Ftyp))
10545 and then Is_Child_Unit (Scope (A_Gen_Obj))
10546 then
10547 declare
10548 Temp : constant Node_Id :=
10549 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10550 begin
10551 Set_Entity (Temp, Empty);
10552 Find_Type (Temp);
10553 Ftyp := Entity (Temp);
10554 end;
10555 end if;
10557 if Is_Private_Type (Ftyp)
10558 and then not Is_Private_Type (Etype (Actual))
10559 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10560 or else Base_Type (Etype (Actual)) = Ftyp)
10561 then
10562 -- If the actual has the type of the full view of the formal, or
10563 -- else a non-private subtype of the formal, then the visibility
10564 -- of the formal type has changed. Add to the actuals a subtype
10565 -- declaration that will force the exchange of views in the body
10566 -- of the instance as well.
10568 Subt_Decl :=
10569 Make_Subtype_Declaration (Loc,
10570 Defining_Identifier => Make_Temporary (Loc, 'P'),
10571 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10573 Prepend (Subt_Decl, List);
10575 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10576 Exchange_Declarations (Ftyp);
10577 end if;
10579 Resolve (Actual, Ftyp);
10581 if not Denotes_Variable (Actual) then
10582 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10584 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10586 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10587 -- the type of the actual shall resolve to a specific anonymous
10588 -- access type.
10590 if Ada_Version < Ada_2005
10591 or else Ekind (Base_Type (Ftyp)) /=
10592 E_Anonymous_Access_Type
10593 or else Ekind (Base_Type (Etype (Actual))) /=
10594 E_Anonymous_Access_Type
10595 then
10596 Error_Msg_NE
10597 ("type of actual does not match type of&", Actual, Gen_Obj);
10598 end if;
10599 end if;
10601 Note_Possible_Modification (Actual, Sure => True);
10603 -- Check for instantiation of atomic/volatile actual for
10604 -- non-atomic/volatile formal (RM C.6 (12)).
10606 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10607 Error_Msg_N
10608 ("cannot instantiate non-atomic formal object "
10609 & "with atomic actual", Actual);
10611 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10612 then
10613 Error_Msg_N
10614 ("cannot instantiate non-volatile formal object "
10615 & "with volatile actual", Actual);
10616 end if;
10618 -- Formal in-parameter
10620 else
10621 -- The instantiation of a generic formal in-parameter is constant
10622 -- declaration. The actual is the expression for that declaration.
10623 -- Its type is a full copy of the type of the formal. This may be
10624 -- an access to subprogram, for which we need to generate entities
10625 -- for the formals in the new signature.
10627 if Present (Actual) then
10628 if Present (Subt_Mark) then
10629 Def := New_Copy_Tree (Subt_Mark);
10630 else pragma Assert (Present (Acc_Def));
10631 Def := Copy_Access_Def;
10632 end if;
10634 Decl_Node :=
10635 Make_Object_Declaration (Loc,
10636 Defining_Identifier => New_Copy (Gen_Obj),
10637 Constant_Present => True,
10638 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10639 Object_Definition => Def,
10640 Expression => Actual);
10642 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10644 -- A generic formal object of a tagged type is defined to be
10645 -- aliased so the new constant must also be treated as aliased.
10647 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10648 Set_Aliased_Present (Decl_Node);
10649 end if;
10651 Append (Decl_Node, List);
10653 -- No need to repeat (pre-)analysis of some expression nodes
10654 -- already handled in Preanalyze_Actuals.
10656 if Nkind (Actual) /= N_Allocator then
10657 Analyze (Actual);
10659 -- Return if the analysis of the actual reported some error
10661 if Etype (Actual) = Any_Type then
10662 return List;
10663 end if;
10664 end if;
10666 declare
10667 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10668 Typ : Entity_Id;
10670 begin
10671 Typ := Get_Instance_Of (Formal_Type);
10673 -- If the actual appears in the current or an enclosing scope,
10674 -- use its type directly. This is relevant if it has an actual
10675 -- subtype that is distinct from its nominal one. This cannot
10676 -- be done in general because the type of the actual may
10677 -- depend on other actuals, and only be fully determined when
10678 -- the enclosing instance is analyzed.
10680 if Present (Etype (Actual))
10681 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10682 then
10683 Freeze_Before (Instantiation_Node, Etype (Actual));
10684 else
10685 Freeze_Before (Instantiation_Node, Typ);
10686 end if;
10688 -- If the actual is an aggregate, perform name resolution on
10689 -- its components (the analysis of an aggregate does not do it)
10690 -- to capture local names that may be hidden if the generic is
10691 -- a child unit.
10693 if Nkind (Actual) = N_Aggregate then
10694 Preanalyze_And_Resolve (Actual, Typ);
10695 end if;
10697 if Is_Limited_Type (Typ)
10698 and then not OK_For_Limited_Init (Typ, Actual)
10699 then
10700 Error_Msg_N
10701 ("initialization not allowed for limited types", Actual);
10702 Explain_Limited_Type (Typ, Actual);
10703 end if;
10704 end;
10706 elsif Present (Default_Expression (Formal)) then
10708 -- Use default to construct declaration
10710 if Present (Subt_Mark) then
10711 Def := New_Copy (Subt_Mark);
10712 else pragma Assert (Present (Acc_Def));
10713 Def := Copy_Access_Def;
10714 end if;
10716 Decl_Node :=
10717 Make_Object_Declaration (Sloc (Formal),
10718 Defining_Identifier => New_Copy (Gen_Obj),
10719 Constant_Present => True,
10720 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10721 Object_Definition => Def,
10722 Expression => New_Copy_Tree
10723 (Default_Expression (Formal)));
10725 Append (Decl_Node, List);
10726 Set_Analyzed (Expression (Decl_Node), False);
10728 else
10729 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10730 Error_Msg_NE ("\in instantiation of & declared#",
10731 Instantiation_Node, Scope (A_Gen_Obj));
10733 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10735 -- Create dummy constant declaration so that instance can be
10736 -- analyzed, to minimize cascaded visibility errors.
10738 if Present (Subt_Mark) then
10739 Def := Subt_Mark;
10740 else pragma Assert (Present (Acc_Def));
10741 Def := Acc_Def;
10742 end if;
10744 Decl_Node :=
10745 Make_Object_Declaration (Loc,
10746 Defining_Identifier => New_Copy (Gen_Obj),
10747 Constant_Present => True,
10748 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10749 Object_Definition => New_Copy (Def),
10750 Expression =>
10751 Make_Attribute_Reference (Sloc (Gen_Obj),
10752 Attribute_Name => Name_First,
10753 Prefix => New_Copy (Def)));
10755 Append (Decl_Node, List);
10757 else
10758 Abandon_Instantiation (Instantiation_Node);
10759 end if;
10760 end if;
10761 end if;
10763 if Nkind (Actual) in N_Has_Entity then
10764 Actual_Decl := Parent (Entity (Actual));
10765 end if;
10767 -- Ada 2005 (AI-423): For a formal object declaration with a null
10768 -- exclusion or an access definition that has a null exclusion: If the
10769 -- actual matching the formal object declaration denotes a generic
10770 -- formal object of another generic unit G, and the instantiation
10771 -- containing the actual occurs within the body of G or within the body
10772 -- of a generic unit declared within the declarative region of G, then
10773 -- the declaration of the formal object of G must have a null exclusion.
10774 -- Otherwise, the subtype of the actual matching the formal object
10775 -- declaration shall exclude null.
10777 if Ada_Version >= Ada_2005
10778 and then Present (Actual_Decl)
10779 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10780 N_Object_Declaration)
10781 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10782 and then not Has_Null_Exclusion (Actual_Decl)
10783 and then Has_Null_Exclusion (Analyzed_Formal)
10784 then
10785 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10786 Error_Msg_N
10787 ("actual must exclude null to match generic formal#", Actual);
10788 end if;
10790 -- An effectively volatile object cannot be used as an actual in a
10791 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10792 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10793 -- legality rule, and also verifies that the actual is an object.
10795 if SPARK_Mode = On
10796 and then Present (Actual)
10797 and then Is_Object_Reference (Actual)
10798 and then Is_Effectively_Volatile_Object (Actual)
10799 then
10800 Error_Msg_N
10801 ("volatile object cannot act as actual in generic instantiation",
10802 Actual);
10803 end if;
10805 return List;
10806 end Instantiate_Object;
10808 ------------------------------
10809 -- Instantiate_Package_Body --
10810 ------------------------------
10812 -- WARNING: This routine manages Ghost regions. Return statements must be
10813 -- replaced by gotos which jump to the end of the routine and restore the
10814 -- Ghost mode.
10816 procedure Instantiate_Package_Body
10817 (Body_Info : Pending_Body_Info;
10818 Inlined_Body : Boolean := False;
10819 Body_Optional : Boolean := False)
10821 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10822 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
10823 Act_Spec : constant Node_Id := Specification (Act_Decl);
10824 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10825 Gen_Id : constant Node_Id := Name (Inst_Node);
10826 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10827 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10828 Loc : constant Source_Ptr := Sloc (Inst_Node);
10830 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10831 Save_Style_Check : constant Boolean := Style_Check;
10833 procedure Check_Initialized_Types;
10834 -- In a generic package body, an entity of a generic private type may
10835 -- appear uninitialized. This is suspicious, unless the actual is a
10836 -- fully initialized type.
10838 -----------------------------
10839 -- Check_Initialized_Types --
10840 -----------------------------
10842 procedure Check_Initialized_Types is
10843 Decl : Node_Id;
10844 Formal : Entity_Id;
10845 Actual : Entity_Id;
10846 Uninit_Var : Entity_Id;
10848 begin
10849 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10850 while Present (Decl) loop
10851 Uninit_Var := Empty;
10853 if Nkind (Decl) = N_Private_Extension_Declaration then
10854 Uninit_Var := Uninitialized_Variable (Decl);
10856 elsif Nkind (Decl) = N_Formal_Type_Declaration
10857 and then Nkind (Formal_Type_Definition (Decl)) =
10858 N_Formal_Private_Type_Definition
10859 then
10860 Uninit_Var :=
10861 Uninitialized_Variable (Formal_Type_Definition (Decl));
10862 end if;
10864 if Present (Uninit_Var) then
10865 Formal := Defining_Identifier (Decl);
10866 Actual := First_Entity (Act_Decl_Id);
10868 -- For each formal there is a subtype declaration that renames
10869 -- the actual and has the same name as the formal. Locate the
10870 -- formal for warning message about uninitialized variables
10871 -- in the generic, for which the actual type should be a fully
10872 -- initialized type.
10874 while Present (Actual) loop
10875 exit when Ekind (Actual) = E_Package
10876 and then Present (Renamed_Object (Actual));
10878 if Chars (Actual) = Chars (Formal)
10879 and then not Is_Scalar_Type (Actual)
10880 and then not Is_Fully_Initialized_Type (Actual)
10881 and then Warn_On_No_Value_Assigned
10882 then
10883 Error_Msg_Node_2 := Formal;
10884 Error_Msg_NE
10885 ("generic unit has uninitialized variable& of "
10886 & "formal private type &?v?", Actual, Uninit_Var);
10887 Error_Msg_NE
10888 ("actual type for& should be fully initialized type?v?",
10889 Actual, Formal);
10890 exit;
10891 end if;
10893 Next_Entity (Actual);
10894 end loop;
10895 end if;
10897 Next (Decl);
10898 end loop;
10899 end Check_Initialized_Types;
10901 -- Local variables
10903 Act_Body : Node_Id;
10904 Act_Body_Id : Entity_Id;
10905 Act_Body_Name : Node_Id;
10906 Gen_Body : Node_Id;
10907 Gen_Body_Id : Node_Id;
10908 Mode : Ghost_Mode_Type;
10909 Par_Ent : Entity_Id := Empty;
10910 Par_Vis : Boolean := False;
10912 Parent_Installed : Boolean := False;
10914 Vis_Prims_List : Elist_Id := No_Elist;
10915 -- List of primitives made temporarily visible in the instantiation
10916 -- to match the visibility of the formal type.
10918 -- Start of processing for Instantiate_Package_Body
10920 begin
10921 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10923 -- The instance body may already have been processed, as the parent of
10924 -- another instance that is inlined (Load_Parent_Of_Generic).
10926 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10927 return;
10928 end if;
10930 -- The package being instantiated may be subject to pragma Ghost. Set
10931 -- the mode now to ensure that any nodes generated during instantiation
10932 -- are properly marked as Ghost.
10934 Set_Ghost_Mode (Act_Decl_Id, Mode);
10936 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10938 -- Re-establish the state of information on which checks are suppressed.
10939 -- This information was set in Body_Info at the point of instantiation,
10940 -- and now we restore it so that the instance is compiled using the
10941 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10943 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10944 Scope_Suppress := Body_Info.Scope_Suppress;
10945 Opt.Ada_Version := Body_Info.Version;
10946 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10947 Restore_Warnings (Body_Info.Warnings);
10948 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10949 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10951 if No (Gen_Body_Id) then
10953 -- Do not look for parent of generic body if none is required.
10954 -- This may happen when the routine is called as part of the
10955 -- Pending_Instantiations processing, when nested instances
10956 -- may precede the one generated from the main unit.
10958 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10959 and then Body_Optional
10960 then
10961 goto Leave;
10962 else
10963 Load_Parent_Of_Generic
10964 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10965 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10966 end if;
10967 end if;
10969 -- Establish global variable for sloc adjustment and for error recovery
10970 -- In the case of an instance body for an instantiation with actuals
10971 -- from a limited view, the instance body is placed at the beginning
10972 -- of the enclosing package body: use the body entity as the source
10973 -- location for nodes of the instance body.
10975 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10976 declare
10977 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10978 Body_Id : constant Node_Id :=
10979 Corresponding_Body (Unit_Declaration_Node (Scop));
10981 begin
10982 Instantiation_Node := Body_Id;
10983 end;
10984 else
10985 Instantiation_Node := Inst_Node;
10986 end if;
10988 if Present (Gen_Body_Id) then
10989 Save_Env (Gen_Unit, Act_Decl_Id);
10990 Style_Check := False;
10992 -- If the context of the instance is subject to SPARK_Mode "off" or
10993 -- the annotation is altogether missing, set the global flag which
10994 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10995 -- the instance.
10997 if SPARK_Mode /= On then
10998 Ignore_Pragma_SPARK_Mode := True;
10999 end if;
11001 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11002 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11004 Create_Instantiation_Source
11005 (Inst_Node, Gen_Body_Id, S_Adjustment);
11007 Act_Body :=
11008 Copy_Generic_Node
11009 (Original_Node (Gen_Body), Empty, Instantiating => True);
11011 -- Create proper (possibly qualified) defining name for the body, to
11012 -- correspond to the one in the spec.
11014 Act_Body_Id :=
11015 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11016 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11018 -- Some attributes of spec entity are not inherited by body entity
11020 Set_Handler_Records (Act_Body_Id, No_List);
11022 if Nkind (Defining_Unit_Name (Act_Spec)) =
11023 N_Defining_Program_Unit_Name
11024 then
11025 Act_Body_Name :=
11026 Make_Defining_Program_Unit_Name (Loc,
11027 Name =>
11028 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11029 Defining_Identifier => Act_Body_Id);
11030 else
11031 Act_Body_Name := Act_Body_Id;
11032 end if;
11034 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11036 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11037 Check_Generic_Actuals (Act_Decl_Id, False);
11038 Check_Initialized_Types;
11040 -- Install primitives hidden at the point of the instantiation but
11041 -- visible when processing the generic formals
11043 declare
11044 E : Entity_Id;
11046 begin
11047 E := First_Entity (Act_Decl_Id);
11048 while Present (E) loop
11049 if Is_Type (E)
11050 and then not Is_Itype (E)
11051 and then Is_Generic_Actual_Type (E)
11052 and then Is_Tagged_Type (E)
11053 then
11054 Install_Hidden_Primitives
11055 (Prims_List => Vis_Prims_List,
11056 Gen_T => Generic_Parent_Type (Parent (E)),
11057 Act_T => E);
11058 end if;
11060 Next_Entity (E);
11061 end loop;
11062 end;
11064 -- If it is a child unit, make the parent instance (which is an
11065 -- instance of the parent of the generic) visible. The parent
11066 -- instance is the prefix of the name of the generic unit.
11068 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11069 and then Nkind (Gen_Id) = N_Expanded_Name
11070 then
11071 Par_Ent := Entity (Prefix (Gen_Id));
11072 Par_Vis := Is_Immediately_Visible (Par_Ent);
11073 Install_Parent (Par_Ent, In_Body => True);
11074 Parent_Installed := True;
11076 elsif Is_Child_Unit (Gen_Unit) then
11077 Par_Ent := Scope (Gen_Unit);
11078 Par_Vis := Is_Immediately_Visible (Par_Ent);
11079 Install_Parent (Par_Ent, In_Body => True);
11080 Parent_Installed := True;
11081 end if;
11083 -- If the instantiation is a library unit, and this is the main unit,
11084 -- then build the resulting compilation unit nodes for the instance.
11085 -- If this is a compilation unit but it is not the main unit, then it
11086 -- is the body of a unit in the context, that is being compiled
11087 -- because it is encloses some inlined unit or another generic unit
11088 -- being instantiated. In that case, this body is not part of the
11089 -- current compilation, and is not attached to the tree, but its
11090 -- parent must be set for analysis.
11092 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11094 -- Replace instance node with body of instance, and create new
11095 -- node for corresponding instance declaration.
11097 Build_Instance_Compilation_Unit_Nodes
11098 (Inst_Node, Act_Body, Act_Decl);
11099 Analyze (Inst_Node);
11101 if Parent (Inst_Node) = Cunit (Main_Unit) then
11103 -- If the instance is a child unit itself, then set the scope
11104 -- of the expanded body to be the parent of the instantiation
11105 -- (ensuring that the fully qualified name will be generated
11106 -- for the elaboration subprogram).
11108 if Nkind (Defining_Unit_Name (Act_Spec)) =
11109 N_Defining_Program_Unit_Name
11110 then
11111 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11112 end if;
11113 end if;
11115 -- Case where instantiation is not a library unit
11117 else
11118 -- If this is an early instantiation, i.e. appears textually
11119 -- before the corresponding body and must be elaborated first,
11120 -- indicate that the body instance is to be delayed.
11122 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11124 -- Now analyze the body. We turn off all checks if this is an
11125 -- internal unit, since there is no reason to have checks on for
11126 -- any predefined run-time library code. All such code is designed
11127 -- to be compiled with checks off.
11129 -- Note that we do NOT apply this criterion to children of GNAT
11130 -- The latter units must suppress checks explicitly if needed.
11132 -- We also do not suppress checks in CodePeer mode where we are
11133 -- interested in finding possible runtime errors.
11135 if not CodePeer_Mode
11136 and then Is_Predefined_File_Name
11137 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11138 then
11139 Analyze (Act_Body, Suppress => All_Checks);
11140 else
11141 Analyze (Act_Body);
11142 end if;
11143 end if;
11145 Inherit_Context (Gen_Body, Inst_Node);
11147 -- Remove the parent instances if they have been placed on the scope
11148 -- stack to compile the body.
11150 if Parent_Installed then
11151 Remove_Parent (In_Body => True);
11153 -- Restore the previous visibility of the parent
11155 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11156 end if;
11158 Restore_Hidden_Primitives (Vis_Prims_List);
11159 Restore_Private_Views (Act_Decl_Id);
11161 -- Remove the current unit from visibility if this is an instance
11162 -- that is not elaborated on the fly for inlining purposes.
11164 if not Inlined_Body then
11165 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11166 end if;
11168 Restore_Env;
11169 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11170 Style_Check := Save_Style_Check;
11172 -- If we have no body, and the unit requires a body, then complain. This
11173 -- complaint is suppressed if we have detected other errors (since a
11174 -- common reason for missing the body is that it had errors).
11175 -- In CodePeer mode, a warning has been emitted already, no need for
11176 -- further messages.
11178 elsif Unit_Requires_Body (Gen_Unit)
11179 and then not Body_Optional
11180 then
11181 if CodePeer_Mode then
11182 null;
11184 elsif Serious_Errors_Detected = 0 then
11185 Error_Msg_NE
11186 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11188 -- Don't attempt to perform any cleanup actions if some other error
11189 -- was already detected, since this can cause blowups.
11191 else
11192 return;
11193 end if;
11195 -- Case of package that does not need a body
11197 else
11198 -- If the instantiation of the declaration is a library unit, rewrite
11199 -- the original package instantiation as a package declaration in the
11200 -- compilation unit node.
11202 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11203 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11204 Rewrite (Inst_Node, Act_Decl);
11206 -- Generate elaboration entity, in case spec has elaboration code.
11207 -- This cannot be done when the instance is analyzed, because it
11208 -- is not known yet whether the body exists.
11210 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11211 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11213 -- If the instantiation is not a library unit, then append the
11214 -- declaration to the list of implicitly generated entities, unless
11215 -- it is already a list member which means that it was already
11216 -- processed
11218 elsif not Is_List_Member (Act_Decl) then
11219 Mark_Rewrite_Insertion (Act_Decl);
11220 Insert_Before (Inst_Node, Act_Decl);
11221 end if;
11222 end if;
11224 Expander_Mode_Restore;
11226 <<Leave>>
11227 Restore_Ghost_Mode (Mode);
11228 end Instantiate_Package_Body;
11230 ---------------------------------
11231 -- Instantiate_Subprogram_Body --
11232 ---------------------------------
11234 -- WARNING: This routine manages Ghost regions. Return statements must be
11235 -- replaced by gotos which jump to the end of the routine and restore the
11236 -- Ghost mode.
11238 procedure Instantiate_Subprogram_Body
11239 (Body_Info : Pending_Body_Info;
11240 Body_Optional : Boolean := False)
11242 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11243 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11244 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11245 Gen_Id : constant Node_Id := Name (Inst_Node);
11246 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11247 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11248 Loc : constant Source_Ptr := Sloc (Inst_Node);
11249 Pack_Id : constant Entity_Id :=
11250 Defining_Unit_Name (Parent (Act_Decl));
11252 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11253 Saved_Style_Check : constant Boolean := Style_Check;
11254 Saved_Warnings : constant Warning_Record := Save_Warnings;
11256 Act_Body : Node_Id;
11257 Act_Body_Id : Entity_Id;
11258 Gen_Body : Node_Id;
11259 Gen_Body_Id : Node_Id;
11260 Mode : Ghost_Mode_Type;
11261 Pack_Body : Node_Id;
11262 Par_Ent : Entity_Id := Empty;
11263 Par_Vis : Boolean := False;
11264 Ret_Expr : Node_Id;
11266 Parent_Installed : Boolean := False;
11268 begin
11269 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11271 -- Subprogram body may have been created already because of an inline
11272 -- pragma, or because of multiple elaborations of the enclosing package
11273 -- when several instances of the subprogram appear in the main unit.
11275 if Present (Corresponding_Body (Act_Decl)) then
11276 return;
11277 end if;
11279 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11280 -- the mode now to ensure that any nodes generated during instantiation
11281 -- are properly marked as Ghost.
11283 Set_Ghost_Mode (Act_Decl_Id, Mode);
11285 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11287 -- Re-establish the state of information on which checks are suppressed.
11288 -- This information was set in Body_Info at the point of instantiation,
11289 -- and now we restore it so that the instance is compiled using the
11290 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11292 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11293 Scope_Suppress := Body_Info.Scope_Suppress;
11294 Opt.Ada_Version := Body_Info.Version;
11295 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11296 Restore_Warnings (Body_Info.Warnings);
11297 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11298 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11300 if No (Gen_Body_Id) then
11302 -- For imported generic subprogram, no body to compile, complete
11303 -- the spec entity appropriately.
11305 if Is_Imported (Gen_Unit) then
11306 Set_Is_Imported (Act_Decl_Id);
11307 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11308 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11309 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11310 Set_Has_Completion (Act_Decl_Id);
11311 goto Leave;
11313 -- For other cases, compile the body
11315 else
11316 Load_Parent_Of_Generic
11317 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11318 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11319 end if;
11320 end if;
11322 Instantiation_Node := Inst_Node;
11324 if Present (Gen_Body_Id) then
11325 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11327 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11329 -- Either body is not present, or context is non-expanding, as
11330 -- when compiling a subunit. Mark the instance as completed, and
11331 -- diagnose a missing body when needed.
11333 if Expander_Active
11334 and then Operating_Mode = Generate_Code
11335 then
11336 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11337 end if;
11339 Set_Has_Completion (Act_Decl_Id);
11340 goto Leave;
11341 end if;
11343 Save_Env (Gen_Unit, Act_Decl_Id);
11344 Style_Check := False;
11346 -- If the context of the instance is subject to SPARK_Mode "off" or
11347 -- the annotation is altogether missing, set the global flag which
11348 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11349 -- the instance.
11351 if SPARK_Mode /= On then
11352 Ignore_Pragma_SPARK_Mode := True;
11353 end if;
11355 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11356 Create_Instantiation_Source
11357 (Inst_Node,
11358 Gen_Body_Id,
11359 S_Adjustment);
11361 Act_Body :=
11362 Copy_Generic_Node
11363 (Original_Node (Gen_Body), Empty, Instantiating => True);
11365 -- Create proper defining name for the body, to correspond to the one
11366 -- in the spec.
11368 Act_Body_Id :=
11369 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11371 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11372 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11374 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11375 Set_Has_Completion (Act_Decl_Id);
11376 Check_Generic_Actuals (Pack_Id, False);
11378 -- Generate a reference to link the visible subprogram instance to
11379 -- the generic body, which for navigation purposes is the only
11380 -- available source for the instance.
11382 Generate_Reference
11383 (Related_Instance (Pack_Id),
11384 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11386 -- If it is a child unit, make the parent instance (which is an
11387 -- instance of the parent of the generic) visible. The parent
11388 -- instance is the prefix of the name of the generic unit.
11390 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11391 and then Nkind (Gen_Id) = N_Expanded_Name
11392 then
11393 Par_Ent := Entity (Prefix (Gen_Id));
11394 Par_Vis := Is_Immediately_Visible (Par_Ent);
11395 Install_Parent (Par_Ent, In_Body => True);
11396 Parent_Installed := True;
11398 elsif Is_Child_Unit (Gen_Unit) then
11399 Par_Ent := Scope (Gen_Unit);
11400 Par_Vis := Is_Immediately_Visible (Par_Ent);
11401 Install_Parent (Par_Ent, In_Body => True);
11402 Parent_Installed := True;
11403 end if;
11405 -- Subprogram body is placed in the body of wrapper package,
11406 -- whose spec contains the subprogram declaration as well as
11407 -- the renaming declarations for the generic parameters.
11409 Pack_Body :=
11410 Make_Package_Body (Loc,
11411 Defining_Unit_Name => New_Copy (Pack_Id),
11412 Declarations => New_List (Act_Body));
11414 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11416 -- If the instantiation is a library unit, then build resulting
11417 -- compilation unit nodes for the instance. The declaration of
11418 -- the enclosing package is the grandparent of the subprogram
11419 -- declaration. First replace the instantiation node as the unit
11420 -- of the corresponding compilation.
11422 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11423 if Parent (Inst_Node) = Cunit (Main_Unit) then
11424 Set_Unit (Parent (Inst_Node), Inst_Node);
11425 Build_Instance_Compilation_Unit_Nodes
11426 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11427 Analyze (Inst_Node);
11428 else
11429 Set_Parent (Pack_Body, Parent (Inst_Node));
11430 Analyze (Pack_Body);
11431 end if;
11433 else
11434 Insert_Before (Inst_Node, Pack_Body);
11435 Mark_Rewrite_Insertion (Pack_Body);
11436 Analyze (Pack_Body);
11438 if Expander_Active then
11439 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11440 end if;
11441 end if;
11443 Inherit_Context (Gen_Body, Inst_Node);
11445 Restore_Private_Views (Pack_Id, False);
11447 if Parent_Installed then
11448 Remove_Parent (In_Body => True);
11450 -- Restore the previous visibility of the parent
11452 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11453 end if;
11455 Restore_Env;
11456 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11457 Style_Check := Saved_Style_Check;
11458 Restore_Warnings (Saved_Warnings);
11460 -- Body not found. Error was emitted already. If there were no previous
11461 -- errors, this may be an instance whose scope is a premature instance.
11462 -- In that case we must insure that the (legal) program does raise
11463 -- program error if executed. We generate a subprogram body for this
11464 -- purpose. See DEC ac30vso.
11466 -- Should not reference proprietary DEC tests in comments ???
11468 elsif Serious_Errors_Detected = 0
11469 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11470 then
11471 if Body_Optional then
11472 goto Leave;
11474 elsif Ekind (Act_Decl_Id) = E_Procedure then
11475 Act_Body :=
11476 Make_Subprogram_Body (Loc,
11477 Specification =>
11478 Make_Procedure_Specification (Loc,
11479 Defining_Unit_Name =>
11480 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11481 Parameter_Specifications =>
11482 New_Copy_List
11483 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11485 Declarations => Empty_List,
11486 Handled_Statement_Sequence =>
11487 Make_Handled_Sequence_Of_Statements (Loc,
11488 Statements => New_List (
11489 Make_Raise_Program_Error (Loc,
11490 Reason => PE_Access_Before_Elaboration))));
11492 else
11493 Ret_Expr :=
11494 Make_Raise_Program_Error (Loc,
11495 Reason => PE_Access_Before_Elaboration);
11497 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11498 Set_Analyzed (Ret_Expr);
11500 Act_Body :=
11501 Make_Subprogram_Body (Loc,
11502 Specification =>
11503 Make_Function_Specification (Loc,
11504 Defining_Unit_Name =>
11505 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11506 Parameter_Specifications =>
11507 New_Copy_List
11508 (Parameter_Specifications (Parent (Act_Decl_Id))),
11509 Result_Definition =>
11510 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11512 Declarations => Empty_List,
11513 Handled_Statement_Sequence =>
11514 Make_Handled_Sequence_Of_Statements (Loc,
11515 Statements => New_List (
11516 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11517 end if;
11519 Pack_Body :=
11520 Make_Package_Body (Loc,
11521 Defining_Unit_Name => New_Copy (Pack_Id),
11522 Declarations => New_List (Act_Body));
11524 Insert_After (Inst_Node, Pack_Body);
11525 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11526 Analyze (Pack_Body);
11527 end if;
11529 Expander_Mode_Restore;
11531 <<Leave>>
11532 Restore_Ghost_Mode (Mode);
11533 end Instantiate_Subprogram_Body;
11535 ----------------------
11536 -- Instantiate_Type --
11537 ----------------------
11539 function Instantiate_Type
11540 (Formal : Node_Id;
11541 Actual : Node_Id;
11542 Analyzed_Formal : Node_Id;
11543 Actual_Decls : List_Id) return List_Id
11545 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11546 A_Gen_T : constant Entity_Id :=
11547 Defining_Identifier (Analyzed_Formal);
11548 Ancestor : Entity_Id := Empty;
11549 Def : constant Node_Id := Formal_Type_Definition (Formal);
11550 Act_T : Entity_Id;
11551 Decl_Node : Node_Id;
11552 Decl_Nodes : List_Id;
11553 Loc : Source_Ptr;
11554 Subt : Entity_Id;
11556 procedure Diagnose_Predicated_Actual;
11557 -- There are a number of constructs in which a discrete type with
11558 -- predicates is illegal, e.g. as an index in an array type declaration.
11559 -- If a generic type is used is such a construct in a generic package
11560 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11561 -- of the generic contract that the actual cannot have predicates.
11563 procedure Validate_Array_Type_Instance;
11564 procedure Validate_Access_Subprogram_Instance;
11565 procedure Validate_Access_Type_Instance;
11566 procedure Validate_Derived_Type_Instance;
11567 procedure Validate_Derived_Interface_Type_Instance;
11568 procedure Validate_Discriminated_Formal_Type;
11569 procedure Validate_Interface_Type_Instance;
11570 procedure Validate_Private_Type_Instance;
11571 procedure Validate_Incomplete_Type_Instance;
11572 -- These procedures perform validation tests for the named case.
11573 -- Validate_Discriminated_Formal_Type is shared by formal private
11574 -- types and Ada 2012 formal incomplete types.
11576 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11577 -- Check that base types are the same and that the subtypes match
11578 -- statically. Used in several of the above.
11580 ---------------------------------
11581 -- Diagnose_Predicated_Actual --
11582 ---------------------------------
11584 procedure Diagnose_Predicated_Actual is
11585 begin
11586 if No_Predicate_On_Actual (A_Gen_T)
11587 and then Has_Predicates (Act_T)
11588 then
11589 Error_Msg_NE
11590 ("actual for& cannot be a type with predicate",
11591 Instantiation_Node, A_Gen_T);
11593 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11594 and then Has_Predicates (Act_T)
11595 and then not Has_Static_Predicate_Aspect (Act_T)
11596 then
11597 Error_Msg_NE
11598 ("actual for& cannot be a type with a dynamic predicate",
11599 Instantiation_Node, A_Gen_T);
11600 end if;
11601 end Diagnose_Predicated_Actual;
11603 --------------------
11604 -- Subtypes_Match --
11605 --------------------
11607 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11608 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11610 begin
11611 -- Some detailed comments would be useful here ???
11613 return ((Base_Type (T) = Act_T
11614 or else Base_Type (T) = Base_Type (Act_T))
11615 and then Subtypes_Statically_Match (T, Act_T))
11617 or else (Is_Class_Wide_Type (Gen_T)
11618 and then Is_Class_Wide_Type (Act_T)
11619 and then Subtypes_Match
11620 (Get_Instance_Of (Root_Type (Gen_T)),
11621 Root_Type (Act_T)))
11623 or else
11624 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11625 E_Anonymous_Access_Type)
11626 and then Ekind (Act_T) = Ekind (Gen_T)
11627 and then Subtypes_Statically_Match
11628 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11629 end Subtypes_Match;
11631 -----------------------------------------
11632 -- Validate_Access_Subprogram_Instance --
11633 -----------------------------------------
11635 procedure Validate_Access_Subprogram_Instance is
11636 begin
11637 if not Is_Access_Type (Act_T)
11638 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11639 then
11640 Error_Msg_NE
11641 ("expect access type in instantiation of &", Actual, Gen_T);
11642 Abandon_Instantiation (Actual);
11643 end if;
11645 -- According to AI05-288, actuals for access_to_subprograms must be
11646 -- subtype conformant with the generic formal. Previous to AI05-288
11647 -- only mode conformance was required.
11649 -- This is a binding interpretation that applies to previous versions
11650 -- of the language, no need to maintain previous weaker checks.
11652 Check_Subtype_Conformant
11653 (Designated_Type (Act_T),
11654 Designated_Type (A_Gen_T),
11655 Actual,
11656 Get_Inst => True);
11658 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11659 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11660 Error_Msg_NE
11661 ("protected access type not allowed for formal &",
11662 Actual, Gen_T);
11663 end if;
11665 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11666 Error_Msg_NE
11667 ("expect protected access type for formal &",
11668 Actual, Gen_T);
11669 end if;
11671 -- If the formal has a specified convention (which in most cases
11672 -- will be StdCall) verify that the actual has the same convention.
11674 if Has_Convention_Pragma (A_Gen_T)
11675 and then Convention (A_Gen_T) /= Convention (Act_T)
11676 then
11677 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11678 Error_Msg_NE
11679 ("actual for formal & must have convention %", Actual, Gen_T);
11680 end if;
11681 end Validate_Access_Subprogram_Instance;
11683 -----------------------------------
11684 -- Validate_Access_Type_Instance --
11685 -----------------------------------
11687 procedure Validate_Access_Type_Instance is
11688 Desig_Type : constant Entity_Id :=
11689 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11690 Desig_Act : Entity_Id;
11692 begin
11693 if not Is_Access_Type (Act_T) then
11694 Error_Msg_NE
11695 ("expect access type in instantiation of &", Actual, Gen_T);
11696 Abandon_Instantiation (Actual);
11697 end if;
11699 if Is_Access_Constant (A_Gen_T) then
11700 if not Is_Access_Constant (Act_T) then
11701 Error_Msg_N
11702 ("actual type must be access-to-constant type", Actual);
11703 Abandon_Instantiation (Actual);
11704 end if;
11705 else
11706 if Is_Access_Constant (Act_T) then
11707 Error_Msg_N
11708 ("actual type must be access-to-variable type", Actual);
11709 Abandon_Instantiation (Actual);
11711 elsif Ekind (A_Gen_T) = E_General_Access_Type
11712 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11713 then
11714 Error_Msg_N -- CODEFIX
11715 ("actual must be general access type!", Actual);
11716 Error_Msg_NE -- CODEFIX
11717 ("add ALL to }!", Actual, Act_T);
11718 Abandon_Instantiation (Actual);
11719 end if;
11720 end if;
11722 -- The designated subtypes, that is to say the subtypes introduced
11723 -- by an access type declaration (and not by a subtype declaration)
11724 -- must match.
11726 Desig_Act := Designated_Type (Base_Type (Act_T));
11728 -- The designated type may have been introduced through a limited_
11729 -- with clause, in which case retrieve the non-limited view. This
11730 -- applies to incomplete types as well as to class-wide types.
11732 if From_Limited_With (Desig_Act) then
11733 Desig_Act := Available_View (Desig_Act);
11734 end if;
11736 if not Subtypes_Match (Desig_Type, Desig_Act) then
11737 Error_Msg_NE
11738 ("designated type of actual does not match that of formal &",
11739 Actual, Gen_T);
11741 if not Predicates_Match (Desig_Type, Desig_Act) then
11742 Error_Msg_N ("\predicates do not match", Actual);
11743 end if;
11745 Abandon_Instantiation (Actual);
11747 elsif Is_Access_Type (Designated_Type (Act_T))
11748 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11750 Is_Constrained (Designated_Type (Desig_Type))
11751 then
11752 Error_Msg_NE
11753 ("designated type of actual does not match that of formal &",
11754 Actual, Gen_T);
11756 if not Predicates_Match (Desig_Type, Desig_Act) then
11757 Error_Msg_N ("\predicates do not match", Actual);
11758 end if;
11760 Abandon_Instantiation (Actual);
11761 end if;
11763 -- Ada 2005: null-exclusion indicators of the two types must agree
11765 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11766 Error_Msg_NE
11767 ("non null exclusion of actual and formal & do not match",
11768 Actual, Gen_T);
11769 end if;
11770 end Validate_Access_Type_Instance;
11772 ----------------------------------
11773 -- Validate_Array_Type_Instance --
11774 ----------------------------------
11776 procedure Validate_Array_Type_Instance is
11777 I1 : Node_Id;
11778 I2 : Node_Id;
11779 T2 : Entity_Id;
11781 function Formal_Dimensions return Nat;
11782 -- Count number of dimensions in array type formal
11784 -----------------------
11785 -- Formal_Dimensions --
11786 -----------------------
11788 function Formal_Dimensions return Nat is
11789 Num : Nat := 0;
11790 Index : Node_Id;
11792 begin
11793 if Nkind (Def) = N_Constrained_Array_Definition then
11794 Index := First (Discrete_Subtype_Definitions (Def));
11795 else
11796 Index := First (Subtype_Marks (Def));
11797 end if;
11799 while Present (Index) loop
11800 Num := Num + 1;
11801 Next_Index (Index);
11802 end loop;
11804 return Num;
11805 end Formal_Dimensions;
11807 -- Start of processing for Validate_Array_Type_Instance
11809 begin
11810 if not Is_Array_Type (Act_T) then
11811 Error_Msg_NE
11812 ("expect array type in instantiation of &", Actual, Gen_T);
11813 Abandon_Instantiation (Actual);
11815 elsif Nkind (Def) = N_Constrained_Array_Definition then
11816 if not (Is_Constrained (Act_T)) then
11817 Error_Msg_NE
11818 ("expect constrained array in instantiation of &",
11819 Actual, Gen_T);
11820 Abandon_Instantiation (Actual);
11821 end if;
11823 else
11824 if Is_Constrained (Act_T) then
11825 Error_Msg_NE
11826 ("expect unconstrained array in instantiation of &",
11827 Actual, Gen_T);
11828 Abandon_Instantiation (Actual);
11829 end if;
11830 end if;
11832 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11833 Error_Msg_NE
11834 ("dimensions of actual do not match formal &", Actual, Gen_T);
11835 Abandon_Instantiation (Actual);
11836 end if;
11838 I1 := First_Index (A_Gen_T);
11839 I2 := First_Index (Act_T);
11840 for J in 1 .. Formal_Dimensions loop
11842 -- If the indexes of the actual were given by a subtype_mark,
11843 -- the index was transformed into a range attribute. Retrieve
11844 -- the original type mark for checking.
11846 if Is_Entity_Name (Original_Node (I2)) then
11847 T2 := Entity (Original_Node (I2));
11848 else
11849 T2 := Etype (I2);
11850 end if;
11852 if not Subtypes_Match
11853 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11854 then
11855 Error_Msg_NE
11856 ("index types of actual do not match those of formal &",
11857 Actual, Gen_T);
11858 Abandon_Instantiation (Actual);
11859 end if;
11861 Next_Index (I1);
11862 Next_Index (I2);
11863 end loop;
11865 -- Check matching subtypes. Note that there are complex visibility
11866 -- issues when the generic is a child unit and some aspect of the
11867 -- generic type is declared in a parent unit of the generic. We do
11868 -- the test to handle this special case only after a direct check
11869 -- for static matching has failed. The case where both the component
11870 -- type and the array type are separate formals, and the component
11871 -- type is a private view may also require special checking in
11872 -- Subtypes_Match.
11874 if Subtypes_Match
11875 (Component_Type (A_Gen_T), Component_Type (Act_T))
11876 or else
11877 Subtypes_Match
11878 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11879 Component_Type (Act_T))
11880 then
11881 null;
11882 else
11883 Error_Msg_NE
11884 ("component subtype of actual does not match that of formal &",
11885 Actual, Gen_T);
11886 Abandon_Instantiation (Actual);
11887 end if;
11889 if Has_Aliased_Components (A_Gen_T)
11890 and then not Has_Aliased_Components (Act_T)
11891 then
11892 Error_Msg_NE
11893 ("actual must have aliased components to match formal type &",
11894 Actual, Gen_T);
11895 end if;
11896 end Validate_Array_Type_Instance;
11898 -----------------------------------------------
11899 -- Validate_Derived_Interface_Type_Instance --
11900 -----------------------------------------------
11902 procedure Validate_Derived_Interface_Type_Instance is
11903 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11904 Elmt : Elmt_Id;
11906 begin
11907 -- First apply interface instance checks
11909 Validate_Interface_Type_Instance;
11911 -- Verify that immediate parent interface is an ancestor of
11912 -- the actual.
11914 if Present (Par)
11915 and then not Interface_Present_In_Ancestor (Act_T, Par)
11916 then
11917 Error_Msg_NE
11918 ("interface actual must include progenitor&", Actual, Par);
11919 end if;
11921 -- Now verify that the actual includes all other ancestors of
11922 -- the formal.
11924 Elmt := First_Elmt (Interfaces (A_Gen_T));
11925 while Present (Elmt) loop
11926 if not Interface_Present_In_Ancestor
11927 (Act_T, Get_Instance_Of (Node (Elmt)))
11928 then
11929 Error_Msg_NE
11930 ("interface actual must include progenitor&",
11931 Actual, Node (Elmt));
11932 end if;
11934 Next_Elmt (Elmt);
11935 end loop;
11936 end Validate_Derived_Interface_Type_Instance;
11938 ------------------------------------
11939 -- Validate_Derived_Type_Instance --
11940 ------------------------------------
11942 procedure Validate_Derived_Type_Instance is
11943 Actual_Discr : Entity_Id;
11944 Ancestor_Discr : Entity_Id;
11946 begin
11947 -- If the parent type in the generic declaration is itself a previous
11948 -- formal type, then it is local to the generic and absent from the
11949 -- analyzed generic definition. In that case the ancestor is the
11950 -- instance of the formal (which must have been instantiated
11951 -- previously), unless the ancestor is itself a formal derived type.
11952 -- In this latter case (which is the subject of Corrigendum 8652/0038
11953 -- (AI-202) the ancestor of the formals is the ancestor of its
11954 -- parent. Otherwise, the analyzed generic carries the parent type.
11955 -- If the parent type is defined in a previous formal package, then
11956 -- the scope of that formal package is that of the generic type
11957 -- itself, and it has already been mapped into the corresponding type
11958 -- in the actual package.
11960 -- Common case: parent type defined outside of the generic
11962 if Is_Entity_Name (Subtype_Mark (Def))
11963 and then Present (Entity (Subtype_Mark (Def)))
11964 then
11965 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11967 -- Check whether parent is defined in a previous formal package
11969 elsif
11970 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11971 then
11972 Ancestor :=
11973 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11975 -- The type may be a local derivation, or a type extension of a
11976 -- previous formal, or of a formal of a parent package.
11978 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11979 or else
11980 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11981 then
11982 -- Check whether the parent is another derived formal type in the
11983 -- same generic unit.
11985 if Etype (A_Gen_T) /= A_Gen_T
11986 and then Is_Generic_Type (Etype (A_Gen_T))
11987 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11988 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11989 then
11990 -- Locate ancestor of parent from the subtype declaration
11991 -- created for the actual.
11993 declare
11994 Decl : Node_Id;
11996 begin
11997 Decl := First (Actual_Decls);
11998 while Present (Decl) loop
11999 if Nkind (Decl) = N_Subtype_Declaration
12000 and then Chars (Defining_Identifier (Decl)) =
12001 Chars (Etype (A_Gen_T))
12002 then
12003 Ancestor := Generic_Parent_Type (Decl);
12004 exit;
12005 else
12006 Next (Decl);
12007 end if;
12008 end loop;
12009 end;
12011 pragma Assert (Present (Ancestor));
12013 -- The ancestor itself may be a previous formal that has been
12014 -- instantiated.
12016 Ancestor := Get_Instance_Of (Ancestor);
12018 else
12019 Ancestor :=
12020 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12021 end if;
12023 -- Check whether parent is a previous formal of the current generic
12025 elsif Is_Derived_Type (A_Gen_T)
12026 and then Is_Generic_Type (Etype (A_Gen_T))
12027 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12028 then
12029 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12031 -- An unusual case: the actual is a type declared in a parent unit,
12032 -- but is not a formal type so there is no instance_of for it.
12033 -- Retrieve it by analyzing the record extension.
12035 elsif Is_Child_Unit (Scope (A_Gen_T))
12036 and then In_Open_Scopes (Scope (Act_T))
12037 and then Is_Generic_Instance (Scope (Act_T))
12038 then
12039 Analyze (Subtype_Mark (Def));
12040 Ancestor := Entity (Subtype_Mark (Def));
12042 else
12043 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12044 end if;
12046 -- If the formal derived type has pragma Preelaborable_Initialization
12047 -- then the actual type must have preelaborable initialization.
12049 if Known_To_Have_Preelab_Init (A_Gen_T)
12050 and then not Has_Preelaborable_Initialization (Act_T)
12051 then
12052 Error_Msg_NE
12053 ("actual for & must have preelaborable initialization",
12054 Actual, Gen_T);
12055 end if;
12057 -- Ada 2005 (AI-251)
12059 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12060 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12061 Error_Msg_NE
12062 ("(Ada 2005) expected type implementing & in instantiation",
12063 Actual, Ancestor);
12064 end if;
12066 -- Finally verify that the (instance of) the ancestor is an ancestor
12067 -- of the actual.
12069 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12070 Error_Msg_NE
12071 ("expect type derived from & in instantiation",
12072 Actual, First_Subtype (Ancestor));
12073 Abandon_Instantiation (Actual);
12074 end if;
12076 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12077 -- that the formal type declaration has been rewritten as a private
12078 -- extension.
12080 if Ada_Version >= Ada_2005
12081 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12082 and then Synchronized_Present (Parent (A_Gen_T))
12083 then
12084 -- The actual must be a synchronized tagged type
12086 if not Is_Tagged_Type (Act_T) then
12087 Error_Msg_N
12088 ("actual of synchronized type must be tagged", Actual);
12089 Abandon_Instantiation (Actual);
12091 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12092 and then Nkind (Type_Definition (Parent (Act_T))) =
12093 N_Derived_Type_Definition
12094 and then not Synchronized_Present
12095 (Type_Definition (Parent (Act_T)))
12096 then
12097 Error_Msg_N
12098 ("actual of synchronized type must be synchronized", Actual);
12099 Abandon_Instantiation (Actual);
12100 end if;
12101 end if;
12103 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12104 -- removes the second instance of the phrase "or allow pass by copy".
12106 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12107 Error_Msg_N
12108 ("cannot have atomic actual type for non-atomic formal type",
12109 Actual);
12111 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12112 Error_Msg_N
12113 ("cannot have volatile actual type for non-volatile formal type",
12114 Actual);
12115 end if;
12117 -- It should not be necessary to check for unknown discriminants on
12118 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12119 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12120 -- needs fixing. ???
12122 if Is_Definite_Subtype (A_Gen_T)
12123 and then not Unknown_Discriminants_Present (Formal)
12124 and then not Is_Definite_Subtype (Act_T)
12125 then
12126 Error_Msg_N ("actual subtype must be constrained", Actual);
12127 Abandon_Instantiation (Actual);
12128 end if;
12130 if not Unknown_Discriminants_Present (Formal) then
12131 if Is_Constrained (Ancestor) then
12132 if not Is_Constrained (Act_T) then
12133 Error_Msg_N ("actual subtype must be constrained", Actual);
12134 Abandon_Instantiation (Actual);
12135 end if;
12137 -- Ancestor is unconstrained, Check if generic formal and actual
12138 -- agree on constrainedness. The check only applies to array types
12139 -- and discriminated types.
12141 elsif Is_Constrained (Act_T) then
12142 if Ekind (Ancestor) = E_Access_Type
12143 or else (not Is_Constrained (A_Gen_T)
12144 and then Is_Composite_Type (A_Gen_T))
12145 then
12146 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12147 Abandon_Instantiation (Actual);
12148 end if;
12150 -- A class-wide type is only allowed if the formal has unknown
12151 -- discriminants.
12153 elsif Is_Class_Wide_Type (Act_T)
12154 and then not Has_Unknown_Discriminants (Ancestor)
12155 then
12156 Error_Msg_NE
12157 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12158 Abandon_Instantiation (Actual);
12160 -- Otherwise, the formal and actual must have the same number
12161 -- of discriminants and each discriminant of the actual must
12162 -- correspond to a discriminant of the formal.
12164 elsif Has_Discriminants (Act_T)
12165 and then not Has_Unknown_Discriminants (Act_T)
12166 and then Has_Discriminants (Ancestor)
12167 then
12168 Actual_Discr := First_Discriminant (Act_T);
12169 Ancestor_Discr := First_Discriminant (Ancestor);
12170 while Present (Actual_Discr)
12171 and then Present (Ancestor_Discr)
12172 loop
12173 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12174 No (Corresponding_Discriminant (Actual_Discr))
12175 then
12176 Error_Msg_NE
12177 ("discriminant & does not correspond "
12178 & "to ancestor discriminant", Actual, Actual_Discr);
12179 Abandon_Instantiation (Actual);
12180 end if;
12182 Next_Discriminant (Actual_Discr);
12183 Next_Discriminant (Ancestor_Discr);
12184 end loop;
12186 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12187 Error_Msg_NE
12188 ("actual for & must have same number of discriminants",
12189 Actual, Gen_T);
12190 Abandon_Instantiation (Actual);
12191 end if;
12193 -- This case should be caught by the earlier check for
12194 -- constrainedness, but the check here is added for completeness.
12196 elsif Has_Discriminants (Act_T)
12197 and then not Has_Unknown_Discriminants (Act_T)
12198 then
12199 Error_Msg_NE
12200 ("actual for & must not have discriminants", Actual, Gen_T);
12201 Abandon_Instantiation (Actual);
12203 elsif Has_Discriminants (Ancestor) then
12204 Error_Msg_NE
12205 ("actual for & must have known discriminants", Actual, Gen_T);
12206 Abandon_Instantiation (Actual);
12207 end if;
12209 if not Subtypes_Statically_Compatible
12210 (Act_T, Ancestor, Formal_Derived_Matching => True)
12211 then
12212 Error_Msg_N
12213 ("constraint on actual is incompatible with formal", Actual);
12214 Abandon_Instantiation (Actual);
12215 end if;
12216 end if;
12218 -- If the formal and actual types are abstract, check that there
12219 -- are no abstract primitives of the actual type that correspond to
12220 -- nonabstract primitives of the formal type (second sentence of
12221 -- RM95 3.9.3(9)).
12223 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12224 Check_Abstract_Primitives : declare
12225 Gen_Prims : constant Elist_Id :=
12226 Primitive_Operations (A_Gen_T);
12227 Gen_Elmt : Elmt_Id;
12228 Gen_Subp : Entity_Id;
12229 Anc_Subp : Entity_Id;
12230 Anc_Formal : Entity_Id;
12231 Anc_F_Type : Entity_Id;
12233 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12234 Act_Elmt : Elmt_Id;
12235 Act_Subp : Entity_Id;
12236 Act_Formal : Entity_Id;
12237 Act_F_Type : Entity_Id;
12239 Subprograms_Correspond : Boolean;
12241 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12242 -- Returns true if T2 is derived directly or indirectly from
12243 -- T1, including derivations from interfaces. T1 and T2 are
12244 -- required to be specific tagged base types.
12246 ------------------------
12247 -- Is_Tagged_Ancestor --
12248 ------------------------
12250 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12252 Intfc_Elmt : Elmt_Id;
12254 begin
12255 -- The predicate is satisfied if the types are the same
12257 if T1 = T2 then
12258 return True;
12260 -- If we've reached the top of the derivation chain then
12261 -- we know that T1 is not an ancestor of T2.
12263 elsif Etype (T2) = T2 then
12264 return False;
12266 -- Proceed to check T2's immediate parent
12268 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12269 return True;
12271 -- Finally, check to see if T1 is an ancestor of any of T2's
12272 -- progenitors.
12274 else
12275 Intfc_Elmt := First_Elmt (Interfaces (T2));
12276 while Present (Intfc_Elmt) loop
12277 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12278 return True;
12279 end if;
12281 Next_Elmt (Intfc_Elmt);
12282 end loop;
12283 end if;
12285 return False;
12286 end Is_Tagged_Ancestor;
12288 -- Start of processing for Check_Abstract_Primitives
12290 begin
12291 -- Loop over all of the formal derived type's primitives
12293 Gen_Elmt := First_Elmt (Gen_Prims);
12294 while Present (Gen_Elmt) loop
12295 Gen_Subp := Node (Gen_Elmt);
12297 -- If the primitive of the formal is not abstract, then
12298 -- determine whether there is a corresponding primitive of
12299 -- the actual type that's abstract.
12301 if not Is_Abstract_Subprogram (Gen_Subp) then
12302 Act_Elmt := First_Elmt (Act_Prims);
12303 while Present (Act_Elmt) loop
12304 Act_Subp := Node (Act_Elmt);
12306 -- If we find an abstract primitive of the actual,
12307 -- then we need to test whether it corresponds to the
12308 -- subprogram from which the generic formal primitive
12309 -- is inherited.
12311 if Is_Abstract_Subprogram (Act_Subp) then
12312 Anc_Subp := Alias (Gen_Subp);
12314 -- Test whether we have a corresponding primitive
12315 -- by comparing names, kinds, formal types, and
12316 -- result types.
12318 if Chars (Anc_Subp) = Chars (Act_Subp)
12319 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12320 then
12321 Anc_Formal := First_Formal (Anc_Subp);
12322 Act_Formal := First_Formal (Act_Subp);
12323 while Present (Anc_Formal)
12324 and then Present (Act_Formal)
12325 loop
12326 Anc_F_Type := Etype (Anc_Formal);
12327 Act_F_Type := Etype (Act_Formal);
12329 if Ekind (Anc_F_Type) =
12330 E_Anonymous_Access_Type
12331 then
12332 Anc_F_Type := Designated_Type (Anc_F_Type);
12334 if Ekind (Act_F_Type) =
12335 E_Anonymous_Access_Type
12336 then
12337 Act_F_Type :=
12338 Designated_Type (Act_F_Type);
12339 else
12340 exit;
12341 end if;
12343 elsif
12344 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12345 then
12346 exit;
12347 end if;
12349 Anc_F_Type := Base_Type (Anc_F_Type);
12350 Act_F_Type := Base_Type (Act_F_Type);
12352 -- If the formal is controlling, then the
12353 -- the type of the actual primitive's formal
12354 -- must be derived directly or indirectly
12355 -- from the type of the ancestor primitive's
12356 -- formal.
12358 if Is_Controlling_Formal (Anc_Formal) then
12359 if not Is_Tagged_Ancestor
12360 (Anc_F_Type, Act_F_Type)
12361 then
12362 exit;
12363 end if;
12365 -- Otherwise the types of the formals must
12366 -- be the same.
12368 elsif Anc_F_Type /= Act_F_Type then
12369 exit;
12370 end if;
12372 Next_Entity (Anc_Formal);
12373 Next_Entity (Act_Formal);
12374 end loop;
12376 -- If we traversed through all of the formals
12377 -- then so far the subprograms correspond, so
12378 -- now check that any result types correspond.
12380 if No (Anc_Formal) and then No (Act_Formal) then
12381 Subprograms_Correspond := True;
12383 if Ekind (Act_Subp) = E_Function then
12384 Anc_F_Type := Etype (Anc_Subp);
12385 Act_F_Type := Etype (Act_Subp);
12387 if Ekind (Anc_F_Type) =
12388 E_Anonymous_Access_Type
12389 then
12390 Anc_F_Type :=
12391 Designated_Type (Anc_F_Type);
12393 if Ekind (Act_F_Type) =
12394 E_Anonymous_Access_Type
12395 then
12396 Act_F_Type :=
12397 Designated_Type (Act_F_Type);
12398 else
12399 Subprograms_Correspond := False;
12400 end if;
12402 elsif
12403 Ekind (Act_F_Type)
12404 = E_Anonymous_Access_Type
12405 then
12406 Subprograms_Correspond := False;
12407 end if;
12409 Anc_F_Type := Base_Type (Anc_F_Type);
12410 Act_F_Type := Base_Type (Act_F_Type);
12412 -- Now either the result types must be
12413 -- the same or, if the result type is
12414 -- controlling, the result type of the
12415 -- actual primitive must descend from the
12416 -- result type of the ancestor primitive.
12418 if Subprograms_Correspond
12419 and then Anc_F_Type /= Act_F_Type
12420 and then
12421 Has_Controlling_Result (Anc_Subp)
12422 and then not Is_Tagged_Ancestor
12423 (Anc_F_Type, Act_F_Type)
12424 then
12425 Subprograms_Correspond := False;
12426 end if;
12427 end if;
12429 -- Found a matching subprogram belonging to
12430 -- formal ancestor type, so actual subprogram
12431 -- corresponds and this violates 3.9.3(9).
12433 if Subprograms_Correspond then
12434 Error_Msg_NE
12435 ("abstract subprogram & overrides "
12436 & "nonabstract subprogram of ancestor",
12437 Actual, Act_Subp);
12438 end if;
12439 end if;
12440 end if;
12441 end if;
12443 Next_Elmt (Act_Elmt);
12444 end loop;
12445 end if;
12447 Next_Elmt (Gen_Elmt);
12448 end loop;
12449 end Check_Abstract_Primitives;
12450 end if;
12452 -- Verify that limitedness matches. If parent is a limited
12453 -- interface then the generic formal is not unless declared
12454 -- explicitly so. If not declared limited, the actual cannot be
12455 -- limited (see AI05-0087).
12457 -- Even though this AI is a binding interpretation, we enable the
12458 -- check only in Ada 2012 mode, because this improper construct
12459 -- shows up in user code and in existing B-tests.
12461 if Is_Limited_Type (Act_T)
12462 and then not Is_Limited_Type (A_Gen_T)
12463 and then Ada_Version >= Ada_2012
12464 then
12465 if In_Instance then
12466 null;
12467 else
12468 Error_Msg_NE
12469 ("actual for non-limited & cannot be a limited type",
12470 Actual, Gen_T);
12471 Explain_Limited_Type (Act_T, Actual);
12472 Abandon_Instantiation (Actual);
12473 end if;
12474 end if;
12475 end Validate_Derived_Type_Instance;
12477 ----------------------------------------
12478 -- Validate_Discriminated_Formal_Type --
12479 ----------------------------------------
12481 procedure Validate_Discriminated_Formal_Type is
12482 Formal_Discr : Entity_Id;
12483 Actual_Discr : Entity_Id;
12484 Formal_Subt : Entity_Id;
12486 begin
12487 if Has_Discriminants (A_Gen_T) then
12488 if not Has_Discriminants (Act_T) then
12489 Error_Msg_NE
12490 ("actual for & must have discriminants", Actual, Gen_T);
12491 Abandon_Instantiation (Actual);
12493 elsif Is_Constrained (Act_T) then
12494 Error_Msg_NE
12495 ("actual for & must be unconstrained", Actual, Gen_T);
12496 Abandon_Instantiation (Actual);
12498 else
12499 Formal_Discr := First_Discriminant (A_Gen_T);
12500 Actual_Discr := First_Discriminant (Act_T);
12501 while Formal_Discr /= Empty loop
12502 if Actual_Discr = Empty then
12503 Error_Msg_NE
12504 ("discriminants on actual do not match formal",
12505 Actual, Gen_T);
12506 Abandon_Instantiation (Actual);
12507 end if;
12509 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12511 -- Access discriminants match if designated types do
12513 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12514 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12515 E_Anonymous_Access_Type
12516 and then
12517 Get_Instance_Of
12518 (Designated_Type (Base_Type (Formal_Subt))) =
12519 Designated_Type (Base_Type (Etype (Actual_Discr)))
12520 then
12521 null;
12523 elsif Base_Type (Formal_Subt) /=
12524 Base_Type (Etype (Actual_Discr))
12525 then
12526 Error_Msg_NE
12527 ("types of actual discriminants must match formal",
12528 Actual, Gen_T);
12529 Abandon_Instantiation (Actual);
12531 elsif not Subtypes_Statically_Match
12532 (Formal_Subt, Etype (Actual_Discr))
12533 and then Ada_Version >= Ada_95
12534 then
12535 Error_Msg_NE
12536 ("subtypes of actual discriminants must match formal",
12537 Actual, Gen_T);
12538 Abandon_Instantiation (Actual);
12539 end if;
12541 Next_Discriminant (Formal_Discr);
12542 Next_Discriminant (Actual_Discr);
12543 end loop;
12545 if Actual_Discr /= Empty then
12546 Error_Msg_NE
12547 ("discriminants on actual do not match formal",
12548 Actual, Gen_T);
12549 Abandon_Instantiation (Actual);
12550 end if;
12551 end if;
12552 end if;
12553 end Validate_Discriminated_Formal_Type;
12555 ---------------------------------------
12556 -- Validate_Incomplete_Type_Instance --
12557 ---------------------------------------
12559 procedure Validate_Incomplete_Type_Instance is
12560 begin
12561 if not Is_Tagged_Type (Act_T)
12562 and then Is_Tagged_Type (A_Gen_T)
12563 then
12564 Error_Msg_NE
12565 ("actual for & must be a tagged type", Actual, Gen_T);
12566 end if;
12568 Validate_Discriminated_Formal_Type;
12569 end Validate_Incomplete_Type_Instance;
12571 --------------------------------------
12572 -- Validate_Interface_Type_Instance --
12573 --------------------------------------
12575 procedure Validate_Interface_Type_Instance is
12576 begin
12577 if not Is_Interface (Act_T) then
12578 Error_Msg_NE
12579 ("actual for formal interface type must be an interface",
12580 Actual, Gen_T);
12582 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12583 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12584 or else Is_Protected_Interface (A_Gen_T) /=
12585 Is_Protected_Interface (Act_T)
12586 or else Is_Synchronized_Interface (A_Gen_T) /=
12587 Is_Synchronized_Interface (Act_T)
12588 then
12589 Error_Msg_NE
12590 ("actual for interface& does not match (RM 12.5.5(4))",
12591 Actual, Gen_T);
12592 end if;
12593 end Validate_Interface_Type_Instance;
12595 ------------------------------------
12596 -- Validate_Private_Type_Instance --
12597 ------------------------------------
12599 procedure Validate_Private_Type_Instance is
12600 begin
12601 if Is_Limited_Type (Act_T)
12602 and then not Is_Limited_Type (A_Gen_T)
12603 then
12604 if In_Instance then
12605 null;
12606 else
12607 Error_Msg_NE
12608 ("actual for non-limited & cannot be a limited type", Actual,
12609 Gen_T);
12610 Explain_Limited_Type (Act_T, Actual);
12611 Abandon_Instantiation (Actual);
12612 end if;
12614 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12615 and then not Has_Preelaborable_Initialization (Act_T)
12616 then
12617 Error_Msg_NE
12618 ("actual for & must have preelaborable initialization", Actual,
12619 Gen_T);
12621 elsif not Is_Definite_Subtype (Act_T)
12622 and then Is_Definite_Subtype (A_Gen_T)
12623 and then Ada_Version >= Ada_95
12624 then
12625 Error_Msg_NE
12626 ("actual for & must be a definite subtype", Actual, Gen_T);
12628 elsif not Is_Tagged_Type (Act_T)
12629 and then Is_Tagged_Type (A_Gen_T)
12630 then
12631 Error_Msg_NE
12632 ("actual for & must be a tagged type", Actual, Gen_T);
12633 end if;
12635 Validate_Discriminated_Formal_Type;
12636 Ancestor := Gen_T;
12637 end Validate_Private_Type_Instance;
12639 -- Start of processing for Instantiate_Type
12641 begin
12642 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12643 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12644 return New_List (Error);
12646 elsif not Is_Entity_Name (Actual)
12647 or else not Is_Type (Entity (Actual))
12648 then
12649 Error_Msg_NE
12650 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12651 Abandon_Instantiation (Actual);
12653 else
12654 Act_T := Entity (Actual);
12656 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12657 -- as a generic actual parameter if the corresponding formal type
12658 -- does not have a known_discriminant_part, or is a formal derived
12659 -- type that is an Unchecked_Union type.
12661 if Is_Unchecked_Union (Base_Type (Act_T)) then
12662 if not Has_Discriminants (A_Gen_T)
12663 or else (Is_Derived_Type (A_Gen_T)
12664 and then Is_Unchecked_Union (A_Gen_T))
12665 then
12666 null;
12667 else
12668 Error_Msg_N ("unchecked union cannot be the actual for a "
12669 & "discriminated formal type", Act_T);
12671 end if;
12672 end if;
12674 -- Deal with fixed/floating restrictions
12676 if Is_Floating_Point_Type (Act_T) then
12677 Check_Restriction (No_Floating_Point, Actual);
12678 elsif Is_Fixed_Point_Type (Act_T) then
12679 Check_Restriction (No_Fixed_Point, Actual);
12680 end if;
12682 -- Deal with error of using incomplete type as generic actual.
12683 -- This includes limited views of a type, even if the non-limited
12684 -- view may be available.
12686 if Ekind (Act_T) = E_Incomplete_Type
12687 or else (Is_Class_Wide_Type (Act_T)
12688 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12689 then
12690 -- If the formal is an incomplete type, the actual can be
12691 -- incomplete as well.
12693 if Ekind (A_Gen_T) = E_Incomplete_Type then
12694 null;
12696 elsif Is_Class_Wide_Type (Act_T)
12697 or else No (Full_View (Act_T))
12698 then
12699 Error_Msg_N ("premature use of incomplete type", Actual);
12700 Abandon_Instantiation (Actual);
12701 else
12702 Act_T := Full_View (Act_T);
12703 Set_Entity (Actual, Act_T);
12705 if Has_Private_Component (Act_T) then
12706 Error_Msg_N
12707 ("premature use of type with private component", Actual);
12708 end if;
12709 end if;
12711 -- Deal with error of premature use of private type as generic actual
12713 elsif Is_Private_Type (Act_T)
12714 and then Is_Private_Type (Base_Type (Act_T))
12715 and then not Is_Generic_Type (Act_T)
12716 and then not Is_Derived_Type (Act_T)
12717 and then No (Full_View (Root_Type (Act_T)))
12718 then
12719 -- If the formal is an incomplete type, the actual can be
12720 -- private or incomplete as well.
12722 if Ekind (A_Gen_T) = E_Incomplete_Type then
12723 null;
12724 else
12725 Error_Msg_N ("premature use of private type", Actual);
12726 end if;
12728 elsif Has_Private_Component (Act_T) then
12729 Error_Msg_N
12730 ("premature use of type with private component", Actual);
12731 end if;
12733 Set_Instance_Of (A_Gen_T, Act_T);
12735 -- If the type is generic, the class-wide type may also be used
12737 if Is_Tagged_Type (A_Gen_T)
12738 and then Is_Tagged_Type (Act_T)
12739 and then not Is_Class_Wide_Type (A_Gen_T)
12740 then
12741 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12742 Class_Wide_Type (Act_T));
12743 end if;
12745 if not Is_Abstract_Type (A_Gen_T)
12746 and then Is_Abstract_Type (Act_T)
12747 then
12748 Error_Msg_N
12749 ("actual of non-abstract formal cannot be abstract", Actual);
12750 end if;
12752 -- A generic scalar type is a first subtype for which we generate
12753 -- an anonymous base type. Indicate that the instance of this base
12754 -- is the base type of the actual.
12756 if Is_Scalar_Type (A_Gen_T) then
12757 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12758 end if;
12759 end if;
12761 if Error_Posted (Act_T) then
12762 null;
12763 else
12764 case Nkind (Def) is
12765 when N_Formal_Private_Type_Definition =>
12766 Validate_Private_Type_Instance;
12768 when N_Formal_Incomplete_Type_Definition =>
12769 Validate_Incomplete_Type_Instance;
12771 when N_Formal_Derived_Type_Definition =>
12772 Validate_Derived_Type_Instance;
12774 when N_Formal_Discrete_Type_Definition =>
12775 if not Is_Discrete_Type (Act_T) then
12776 Error_Msg_NE
12777 ("expect discrete type in instantiation of&",
12778 Actual, Gen_T);
12779 Abandon_Instantiation (Actual);
12780 end if;
12782 Diagnose_Predicated_Actual;
12784 when N_Formal_Signed_Integer_Type_Definition =>
12785 if not Is_Signed_Integer_Type (Act_T) then
12786 Error_Msg_NE
12787 ("expect signed integer type in instantiation of&",
12788 Actual, Gen_T);
12789 Abandon_Instantiation (Actual);
12790 end if;
12792 Diagnose_Predicated_Actual;
12794 when N_Formal_Modular_Type_Definition =>
12795 if not Is_Modular_Integer_Type (Act_T) then
12796 Error_Msg_NE
12797 ("expect modular type in instantiation of &",
12798 Actual, Gen_T);
12799 Abandon_Instantiation (Actual);
12800 end if;
12802 Diagnose_Predicated_Actual;
12804 when N_Formal_Floating_Point_Definition =>
12805 if not Is_Floating_Point_Type (Act_T) then
12806 Error_Msg_NE
12807 ("expect float type in instantiation of &", Actual, Gen_T);
12808 Abandon_Instantiation (Actual);
12809 end if;
12811 when N_Formal_Ordinary_Fixed_Point_Definition =>
12812 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12813 Error_Msg_NE
12814 ("expect ordinary fixed point type in instantiation of &",
12815 Actual, Gen_T);
12816 Abandon_Instantiation (Actual);
12817 end if;
12819 when N_Formal_Decimal_Fixed_Point_Definition =>
12820 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12821 Error_Msg_NE
12822 ("expect decimal type in instantiation of &",
12823 Actual, Gen_T);
12824 Abandon_Instantiation (Actual);
12825 end if;
12827 when N_Array_Type_Definition =>
12828 Validate_Array_Type_Instance;
12830 when N_Access_To_Object_Definition =>
12831 Validate_Access_Type_Instance;
12833 when N_Access_Function_Definition
12834 | N_Access_Procedure_Definition
12836 Validate_Access_Subprogram_Instance;
12838 when N_Record_Definition =>
12839 Validate_Interface_Type_Instance;
12841 when N_Derived_Type_Definition =>
12842 Validate_Derived_Interface_Type_Instance;
12844 when others =>
12845 raise Program_Error;
12846 end case;
12847 end if;
12849 Subt := New_Copy (Gen_T);
12851 -- Use adjusted sloc of subtype name as the location for other nodes in
12852 -- the subtype declaration.
12854 Loc := Sloc (Subt);
12856 Decl_Node :=
12857 Make_Subtype_Declaration (Loc,
12858 Defining_Identifier => Subt,
12859 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12861 if Is_Private_Type (Act_T) then
12862 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12864 elsif Is_Access_Type (Act_T)
12865 and then Is_Private_Type (Designated_Type (Act_T))
12866 then
12867 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12868 end if;
12870 -- In Ada 2012 the actual may be a limited view. Indicate that
12871 -- the local subtype must be treated as such.
12873 if From_Limited_With (Act_T) then
12874 Set_Ekind (Subt, E_Incomplete_Subtype);
12875 Set_From_Limited_With (Subt);
12876 end if;
12878 Decl_Nodes := New_List (Decl_Node);
12880 -- Flag actual derived types so their elaboration produces the
12881 -- appropriate renamings for the primitive operations of the ancestor.
12882 -- Flag actual for formal private types as well, to determine whether
12883 -- operations in the private part may override inherited operations.
12884 -- If the formal has an interface list, the ancestor is not the
12885 -- parent, but the analyzed formal that includes the interface
12886 -- operations of all its progenitors.
12888 -- Same treatment for formal private types, so we can check whether the
12889 -- type is tagged limited when validating derivations in the private
12890 -- part. (See AI05-096).
12892 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12893 if Present (Interface_List (Def)) then
12894 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12895 else
12896 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12897 end if;
12899 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12900 N_Formal_Incomplete_Type_Definition)
12901 then
12902 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12903 end if;
12905 -- If the actual is a synchronized type that implements an interface,
12906 -- the primitive operations are attached to the corresponding record,
12907 -- and we have to treat it as an additional generic actual, so that its
12908 -- primitive operations become visible in the instance. The task or
12909 -- protected type itself does not carry primitive operations.
12911 if Is_Concurrent_Type (Act_T)
12912 and then Is_Tagged_Type (Act_T)
12913 and then Present (Corresponding_Record_Type (Act_T))
12914 and then Present (Ancestor)
12915 and then Is_Interface (Ancestor)
12916 then
12917 declare
12918 Corr_Rec : constant Entity_Id :=
12919 Corresponding_Record_Type (Act_T);
12920 New_Corr : Entity_Id;
12921 Corr_Decl : Node_Id;
12923 begin
12924 New_Corr := Make_Temporary (Loc, 'S');
12925 Corr_Decl :=
12926 Make_Subtype_Declaration (Loc,
12927 Defining_Identifier => New_Corr,
12928 Subtype_Indication =>
12929 New_Occurrence_Of (Corr_Rec, Loc));
12930 Append_To (Decl_Nodes, Corr_Decl);
12932 if Ekind (Act_T) = E_Task_Type then
12933 Set_Ekind (Subt, E_Task_Subtype);
12934 else
12935 Set_Ekind (Subt, E_Protected_Subtype);
12936 end if;
12938 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12939 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12940 Set_Generic_Parent_Type (Decl_Node, Empty);
12941 end;
12942 end if;
12944 -- For a floating-point type, capture dimension info if any, because
12945 -- the generated subtype declaration does not come from source and
12946 -- will not process dimensions.
12948 if Is_Floating_Point_Type (Act_T) then
12949 Copy_Dimensions (Act_T, Subt);
12950 end if;
12952 return Decl_Nodes;
12953 end Instantiate_Type;
12955 ---------------------
12956 -- Is_In_Main_Unit --
12957 ---------------------
12959 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12960 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12961 Current_Unit : Node_Id;
12963 begin
12964 if Unum = Main_Unit then
12965 return True;
12967 -- If the current unit is a subunit then it is either the main unit or
12968 -- is being compiled as part of the main unit.
12970 elsif Nkind (N) = N_Compilation_Unit then
12971 return Nkind (Unit (N)) = N_Subunit;
12972 end if;
12974 Current_Unit := Parent (N);
12975 while Present (Current_Unit)
12976 and then Nkind (Current_Unit) /= N_Compilation_Unit
12977 loop
12978 Current_Unit := Parent (Current_Unit);
12979 end loop;
12981 -- The instantiation node is in the main unit, or else the current node
12982 -- (perhaps as the result of nested instantiations) is in the main unit,
12983 -- or in the declaration of the main unit, which in this last case must
12984 -- be a body.
12986 return
12987 Current_Unit = Cunit (Main_Unit)
12988 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12989 or else (Present (Current_Unit)
12990 and then Present (Library_Unit (Current_Unit))
12991 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12992 end Is_In_Main_Unit;
12994 ----------------------------
12995 -- Load_Parent_Of_Generic --
12996 ----------------------------
12998 procedure Load_Parent_Of_Generic
12999 (N : Node_Id;
13000 Spec : Node_Id;
13001 Body_Optional : Boolean := False)
13003 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13004 Saved_Style_Check : constant Boolean := Style_Check;
13005 Saved_Warnings : constant Warning_Record := Save_Warnings;
13006 True_Parent : Node_Id;
13007 Inst_Node : Node_Id;
13008 OK : Boolean;
13009 Previous_Instances : constant Elist_Id := New_Elmt_List;
13011 procedure Collect_Previous_Instances (Decls : List_Id);
13012 -- Collect all instantiations in the given list of declarations, that
13013 -- precede the generic that we need to load. If the bodies of these
13014 -- instantiations are available, we must analyze them, to ensure that
13015 -- the public symbols generated are the same when the unit is compiled
13016 -- to generate code, and when it is compiled in the context of a unit
13017 -- that needs a particular nested instance. This process is applied to
13018 -- both package and subprogram instances.
13020 --------------------------------
13021 -- Collect_Previous_Instances --
13022 --------------------------------
13024 procedure Collect_Previous_Instances (Decls : List_Id) is
13025 Decl : Node_Id;
13027 begin
13028 Decl := First (Decls);
13029 while Present (Decl) loop
13030 if Sloc (Decl) >= Sloc (Inst_Node) then
13031 return;
13033 -- If Decl is an instantiation, then record it as requiring
13034 -- instantiation of the corresponding body, except if it is an
13035 -- abbreviated instantiation generated internally for conformance
13036 -- checking purposes only for the case of a formal package
13037 -- declared without a box (see Instantiate_Formal_Package). Such
13038 -- an instantiation does not generate any code (the actual code
13039 -- comes from actual) and thus does not need to be analyzed here.
13040 -- If the instantiation appears with a generic package body it is
13041 -- not analyzed here either.
13043 elsif Nkind (Decl) = N_Package_Instantiation
13044 and then not Is_Internal (Defining_Entity (Decl))
13045 then
13046 Append_Elmt (Decl, Previous_Instances);
13048 -- For a subprogram instantiation, omit instantiations intrinsic
13049 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13051 elsif Nkind_In (Decl, N_Function_Instantiation,
13052 N_Procedure_Instantiation)
13053 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13054 then
13055 Append_Elmt (Decl, Previous_Instances);
13057 elsif Nkind (Decl) = N_Package_Declaration then
13058 Collect_Previous_Instances
13059 (Visible_Declarations (Specification (Decl)));
13060 Collect_Previous_Instances
13061 (Private_Declarations (Specification (Decl)));
13063 -- Previous non-generic bodies may contain instances as well
13065 elsif Nkind (Decl) = N_Package_Body
13066 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13067 then
13068 Collect_Previous_Instances (Declarations (Decl));
13070 elsif Nkind (Decl) = N_Subprogram_Body
13071 and then not Acts_As_Spec (Decl)
13072 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13073 then
13074 Collect_Previous_Instances (Declarations (Decl));
13075 end if;
13077 Next (Decl);
13078 end loop;
13079 end Collect_Previous_Instances;
13081 -- Start of processing for Load_Parent_Of_Generic
13083 begin
13084 if not In_Same_Source_Unit (N, Spec)
13085 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13086 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13087 and then not Is_In_Main_Unit (Spec))
13088 then
13089 -- Find body of parent of spec, and analyze it. A special case arises
13090 -- when the parent is an instantiation, that is to say when we are
13091 -- currently instantiating a nested generic. In that case, there is
13092 -- no separate file for the body of the enclosing instance. Instead,
13093 -- the enclosing body must be instantiated as if it were a pending
13094 -- instantiation, in order to produce the body for the nested generic
13095 -- we require now. Note that in that case the generic may be defined
13096 -- in a package body, the instance defined in the same package body,
13097 -- and the original enclosing body may not be in the main unit.
13099 Inst_Node := Empty;
13101 True_Parent := Parent (Spec);
13102 while Present (True_Parent)
13103 and then Nkind (True_Parent) /= N_Compilation_Unit
13104 loop
13105 if Nkind (True_Parent) = N_Package_Declaration
13106 and then
13107 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13108 then
13109 -- Parent is a compilation unit that is an instantiation.
13110 -- Instantiation node has been replaced with package decl.
13112 Inst_Node := Original_Node (True_Parent);
13113 exit;
13115 elsif Nkind (True_Parent) = N_Package_Declaration
13116 and then Present (Generic_Parent (Specification (True_Parent)))
13117 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13118 then
13119 -- Parent is an instantiation within another specification.
13120 -- Declaration for instance has been inserted before original
13121 -- instantiation node. A direct link would be preferable?
13123 Inst_Node := Next (True_Parent);
13124 while Present (Inst_Node)
13125 and then Nkind (Inst_Node) /= N_Package_Instantiation
13126 loop
13127 Next (Inst_Node);
13128 end loop;
13130 -- If the instance appears within a generic, and the generic
13131 -- unit is defined within a formal package of the enclosing
13132 -- generic, there is no generic body available, and none
13133 -- needed. A more precise test should be used ???
13135 if No (Inst_Node) then
13136 return;
13137 end if;
13139 exit;
13141 else
13142 True_Parent := Parent (True_Parent);
13143 end if;
13144 end loop;
13146 -- Case where we are currently instantiating a nested generic
13148 if Present (Inst_Node) then
13149 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13151 -- Instantiation node and declaration of instantiated package
13152 -- were exchanged when only the declaration was needed.
13153 -- Restore instantiation node before proceeding with body.
13155 Set_Unit (Parent (True_Parent), Inst_Node);
13156 end if;
13158 -- Now complete instantiation of enclosing body, if it appears in
13159 -- some other unit. If it appears in the current unit, the body
13160 -- will have been instantiated already.
13162 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13164 -- We need to determine the expander mode to instantiate the
13165 -- enclosing body. Because the generic body we need may use
13166 -- global entities declared in the enclosing package (including
13167 -- aggregates) it is in general necessary to compile this body
13168 -- with expansion enabled, except if we are within a generic
13169 -- package, in which case the usual generic rule applies.
13171 declare
13172 Exp_Status : Boolean := True;
13173 Scop : Entity_Id;
13175 begin
13176 -- Loop through scopes looking for generic package
13178 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13179 while Present (Scop)
13180 and then Scop /= Standard_Standard
13181 loop
13182 if Ekind (Scop) = E_Generic_Package then
13183 Exp_Status := False;
13184 exit;
13185 end if;
13187 Scop := Scope (Scop);
13188 end loop;
13190 -- Collect previous instantiations in the unit that contains
13191 -- the desired generic.
13193 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13194 and then not Body_Optional
13195 then
13196 declare
13197 Decl : Elmt_Id;
13198 Info : Pending_Body_Info;
13199 Par : Node_Id;
13201 begin
13202 Par := Parent (Inst_Node);
13203 while Present (Par) loop
13204 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13205 Par := Parent (Par);
13206 end loop;
13208 pragma Assert (Present (Par));
13210 if Nkind (Par) = N_Package_Body then
13211 Collect_Previous_Instances (Declarations (Par));
13213 elsif Nkind (Par) = N_Package_Declaration then
13214 Collect_Previous_Instances
13215 (Visible_Declarations (Specification (Par)));
13216 Collect_Previous_Instances
13217 (Private_Declarations (Specification (Par)));
13219 else
13220 -- Enclosing unit is a subprogram body. In this
13221 -- case all instance bodies are processed in order
13222 -- and there is no need to collect them separately.
13224 null;
13225 end if;
13227 Decl := First_Elmt (Previous_Instances);
13228 while Present (Decl) loop
13229 Info :=
13230 (Inst_Node => Node (Decl),
13231 Act_Decl =>
13232 Instance_Spec (Node (Decl)),
13233 Expander_Status => Exp_Status,
13234 Current_Sem_Unit =>
13235 Get_Code_Unit (Sloc (Node (Decl))),
13236 Scope_Suppress => Scope_Suppress,
13237 Local_Suppress_Stack_Top =>
13238 Local_Suppress_Stack_Top,
13239 Version => Ada_Version,
13240 Version_Pragma => Ada_Version_Pragma,
13241 Warnings => Save_Warnings,
13242 SPARK_Mode => SPARK_Mode,
13243 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13245 -- Package instance
13248 Nkind (Node (Decl)) = N_Package_Instantiation
13249 then
13250 Instantiate_Package_Body
13251 (Info, Body_Optional => True);
13253 -- Subprogram instance
13255 else
13256 -- The instance_spec is in the wrapper package,
13257 -- usually followed by its local renaming
13258 -- declaration. See Build_Subprogram_Renaming
13259 -- for details. If the instance carries aspects,
13260 -- these result in the corresponding pragmas,
13261 -- inserted after the subprogram declaration.
13262 -- They must be skipped as well when retrieving
13263 -- the desired spec. A direct link would be
13264 -- more robust ???
13266 declare
13267 Decl : Node_Id :=
13268 (Last (Visible_Declarations
13269 (Specification (Info.Act_Decl))));
13270 begin
13271 while Nkind_In (Decl,
13272 N_Subprogram_Renaming_Declaration, N_Pragma)
13273 loop
13274 Decl := Prev (Decl);
13275 end loop;
13277 Info.Act_Decl := Decl;
13278 end;
13280 Instantiate_Subprogram_Body
13281 (Info, Body_Optional => True);
13282 end if;
13284 Next_Elmt (Decl);
13285 end loop;
13286 end;
13287 end if;
13289 Instantiate_Package_Body
13290 (Body_Info =>
13291 ((Inst_Node => Inst_Node,
13292 Act_Decl => True_Parent,
13293 Expander_Status => Exp_Status,
13294 Current_Sem_Unit => Get_Code_Unit
13295 (Sloc (Inst_Node)),
13296 Scope_Suppress => Scope_Suppress,
13297 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13298 Version => Ada_Version,
13299 Version_Pragma => Ada_Version_Pragma,
13300 Warnings => Save_Warnings,
13301 SPARK_Mode => SPARK_Mode,
13302 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13303 Body_Optional => Body_Optional);
13304 end;
13305 end if;
13307 -- Case where we are not instantiating a nested generic
13309 else
13310 Opt.Style_Check := False;
13311 Expander_Mode_Save_And_Set (True);
13312 Load_Needed_Body (Comp_Unit, OK);
13313 Opt.Style_Check := Saved_Style_Check;
13314 Restore_Warnings (Saved_Warnings);
13315 Expander_Mode_Restore;
13317 if not OK
13318 and then Unit_Requires_Body (Defining_Entity (Spec))
13319 and then not Body_Optional
13320 then
13321 declare
13322 Bname : constant Unit_Name_Type :=
13323 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13325 begin
13326 -- In CodePeer mode, the missing body may make the analysis
13327 -- incomplete, but we do not treat it as fatal.
13329 if CodePeer_Mode then
13330 return;
13332 else
13333 Error_Msg_Unit_1 := Bname;
13334 Error_Msg_N ("this instantiation requires$!", N);
13335 Error_Msg_File_1 :=
13336 Get_File_Name (Bname, Subunit => False);
13337 Error_Msg_N ("\but file{ was not found!", N);
13338 raise Unrecoverable_Error;
13339 end if;
13340 end;
13341 end if;
13342 end if;
13343 end if;
13345 -- If loading parent of the generic caused an instantiation circularity,
13346 -- we abandon compilation at this point, because otherwise in some cases
13347 -- we get into trouble with infinite recursions after this point.
13349 if Circularity_Detected then
13350 raise Unrecoverable_Error;
13351 end if;
13352 end Load_Parent_Of_Generic;
13354 ---------------------------------
13355 -- Map_Formal_Package_Entities --
13356 ---------------------------------
13358 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13359 E1 : Entity_Id;
13360 E2 : Entity_Id;
13362 begin
13363 Set_Instance_Of (Form, Act);
13365 -- Traverse formal and actual package to map the corresponding entities.
13366 -- We skip over internal entities that may be generated during semantic
13367 -- analysis, and find the matching entities by name, given that they
13368 -- must appear in the same order.
13370 E1 := First_Entity (Form);
13371 E2 := First_Entity (Act);
13372 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13373 -- Could this test be a single condition??? Seems like it could, and
13374 -- isn't FPE (Form) a constant anyway???
13376 if not Is_Internal (E1)
13377 and then Present (Parent (E1))
13378 and then not Is_Class_Wide_Type (E1)
13379 and then not Is_Internal_Name (Chars (E1))
13380 then
13381 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13382 Next_Entity (E2);
13383 end loop;
13385 if No (E2) then
13386 exit;
13387 else
13388 Set_Instance_Of (E1, E2);
13390 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13391 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13392 end if;
13394 if Is_Constrained (E1) then
13395 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13396 end if;
13398 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13399 Map_Formal_Package_Entities (E1, E2);
13400 end if;
13401 end if;
13402 end if;
13404 Next_Entity (E1);
13405 end loop;
13406 end Map_Formal_Package_Entities;
13408 -----------------------
13409 -- Move_Freeze_Nodes --
13410 -----------------------
13412 procedure Move_Freeze_Nodes
13413 (Out_Of : Entity_Id;
13414 After : Node_Id;
13415 L : List_Id)
13417 Decl : Node_Id;
13418 Next_Decl : Node_Id;
13419 Next_Node : Node_Id := After;
13420 Spec : Node_Id;
13422 function Is_Outer_Type (T : Entity_Id) return Boolean;
13423 -- Check whether entity is declared in a scope external to that of the
13424 -- generic unit.
13426 -------------------
13427 -- Is_Outer_Type --
13428 -------------------
13430 function Is_Outer_Type (T : Entity_Id) return Boolean is
13431 Scop : Entity_Id := Scope (T);
13433 begin
13434 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13435 return True;
13437 else
13438 while Scop /= Standard_Standard loop
13439 if Scop = Out_Of then
13440 return False;
13441 else
13442 Scop := Scope (Scop);
13443 end if;
13444 end loop;
13446 return True;
13447 end if;
13448 end Is_Outer_Type;
13450 -- Start of processing for Move_Freeze_Nodes
13452 begin
13453 if No (L) then
13454 return;
13455 end if;
13457 -- First remove the freeze nodes that may appear before all other
13458 -- declarations.
13460 Decl := First (L);
13461 while Present (Decl)
13462 and then Nkind (Decl) = N_Freeze_Entity
13463 and then Is_Outer_Type (Entity (Decl))
13464 loop
13465 Decl := Remove_Head (L);
13466 Insert_After (Next_Node, Decl);
13467 Set_Analyzed (Decl, False);
13468 Next_Node := Decl;
13469 Decl := First (L);
13470 end loop;
13472 -- Next scan the list of declarations and remove each freeze node that
13473 -- appears ahead of the current node.
13475 while Present (Decl) loop
13476 while Present (Next (Decl))
13477 and then Nkind (Next (Decl)) = N_Freeze_Entity
13478 and then Is_Outer_Type (Entity (Next (Decl)))
13479 loop
13480 Next_Decl := Remove_Next (Decl);
13481 Insert_After (Next_Node, Next_Decl);
13482 Set_Analyzed (Next_Decl, False);
13483 Next_Node := Next_Decl;
13484 end loop;
13486 -- If the declaration is a nested package or concurrent type, then
13487 -- recurse. Nested generic packages will have been processed from the
13488 -- inside out.
13490 case Nkind (Decl) is
13491 when N_Package_Declaration =>
13492 Spec := Specification (Decl);
13494 when N_Task_Type_Declaration =>
13495 Spec := Task_Definition (Decl);
13497 when N_Protected_Type_Declaration =>
13498 Spec := Protected_Definition (Decl);
13500 when others =>
13501 Spec := Empty;
13502 end case;
13504 if Present (Spec) then
13505 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13506 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13507 end if;
13509 Next (Decl);
13510 end loop;
13511 end Move_Freeze_Nodes;
13513 ----------------
13514 -- Next_Assoc --
13515 ----------------
13517 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13518 begin
13519 return Generic_Renamings.Table (E).Next_In_HTable;
13520 end Next_Assoc;
13522 ------------------------
13523 -- Preanalyze_Actuals --
13524 ------------------------
13526 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13527 Assoc : Node_Id;
13528 Act : Node_Id;
13529 Errs : constant Nat := Serious_Errors_Detected;
13531 Cur : Entity_Id := Empty;
13532 -- Current homograph of the instance name
13534 Vis : Boolean;
13535 -- Saved visibility status of the current homograph
13537 begin
13538 Assoc := First (Generic_Associations (N));
13540 -- If the instance is a child unit, its name may hide an outer homonym,
13541 -- so make it invisible to perform name resolution on the actuals.
13543 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13544 and then Present
13545 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13546 then
13547 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13549 if Is_Compilation_Unit (Cur) then
13550 Vis := Is_Immediately_Visible (Cur);
13551 Set_Is_Immediately_Visible (Cur, False);
13552 else
13553 Cur := Empty;
13554 end if;
13555 end if;
13557 while Present (Assoc) loop
13558 if Nkind (Assoc) /= N_Others_Choice then
13559 Act := Explicit_Generic_Actual_Parameter (Assoc);
13561 -- Within a nested instantiation, a defaulted actual is an empty
13562 -- association, so nothing to analyze. If the subprogram actual
13563 -- is an attribute, analyze prefix only, because actual is not a
13564 -- complete attribute reference.
13566 -- If actual is an allocator, analyze expression only. The full
13567 -- analysis can generate code, and if instance is a compilation
13568 -- unit we have to wait until the package instance is installed
13569 -- to have a proper place to insert this code.
13571 -- String literals may be operators, but at this point we do not
13572 -- know whether the actual is a formal subprogram or a string.
13574 if No (Act) then
13575 null;
13577 elsif Nkind (Act) = N_Attribute_Reference then
13578 Analyze (Prefix (Act));
13580 elsif Nkind (Act) = N_Explicit_Dereference then
13581 Analyze (Prefix (Act));
13583 elsif Nkind (Act) = N_Allocator then
13584 declare
13585 Expr : constant Node_Id := Expression (Act);
13587 begin
13588 if Nkind (Expr) = N_Subtype_Indication then
13589 Analyze (Subtype_Mark (Expr));
13591 -- Analyze separately each discriminant constraint, when
13592 -- given with a named association.
13594 declare
13595 Constr : Node_Id;
13597 begin
13598 Constr := First (Constraints (Constraint (Expr)));
13599 while Present (Constr) loop
13600 if Nkind (Constr) = N_Discriminant_Association then
13601 Analyze (Expression (Constr));
13602 else
13603 Analyze (Constr);
13604 end if;
13606 Next (Constr);
13607 end loop;
13608 end;
13610 else
13611 Analyze (Expr);
13612 end if;
13613 end;
13615 elsif Nkind (Act) /= N_Operator_Symbol then
13616 Analyze (Act);
13618 -- Within a package instance, mark actuals that are limited
13619 -- views, so their use can be moved to the body of the
13620 -- enclosing unit.
13622 if Is_Entity_Name (Act)
13623 and then Is_Type (Entity (Act))
13624 and then From_Limited_With (Entity (Act))
13625 and then Present (Inst)
13626 then
13627 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13628 end if;
13629 end if;
13631 if Errs /= Serious_Errors_Detected then
13633 -- Do a minimal analysis of the generic, to prevent spurious
13634 -- warnings complaining about the generic being unreferenced,
13635 -- before abandoning the instantiation.
13637 Analyze (Name (N));
13639 if Is_Entity_Name (Name (N))
13640 and then Etype (Name (N)) /= Any_Type
13641 then
13642 Generate_Reference (Entity (Name (N)), Name (N));
13643 Set_Is_Instantiated (Entity (Name (N)));
13644 end if;
13646 if Present (Cur) then
13648 -- For the case of a child instance hiding an outer homonym,
13649 -- provide additional warning which might explain the error.
13651 Set_Is_Immediately_Visible (Cur, Vis);
13652 Error_Msg_NE
13653 ("& hides outer unit with the same name??",
13654 N, Defining_Unit_Name (N));
13655 end if;
13657 Abandon_Instantiation (Act);
13658 end if;
13659 end if;
13661 Next (Assoc);
13662 end loop;
13664 if Present (Cur) then
13665 Set_Is_Immediately_Visible (Cur, Vis);
13666 end if;
13667 end Preanalyze_Actuals;
13669 -------------------
13670 -- Remove_Parent --
13671 -------------------
13673 procedure Remove_Parent (In_Body : Boolean := False) is
13674 S : Entity_Id := Current_Scope;
13675 -- S is the scope containing the instantiation just completed. The scope
13676 -- stack contains the parent instances of the instantiation, followed by
13677 -- the original S.
13679 Cur_P : Entity_Id;
13680 E : Entity_Id;
13681 P : Entity_Id;
13682 Hidden : Elmt_Id;
13684 begin
13685 -- After child instantiation is complete, remove from scope stack the
13686 -- extra copy of the current scope, and then remove parent instances.
13688 if not In_Body then
13689 Pop_Scope;
13691 while Current_Scope /= S loop
13692 P := Current_Scope;
13693 End_Package_Scope (Current_Scope);
13695 if In_Open_Scopes (P) then
13696 E := First_Entity (P);
13697 while Present (E) loop
13698 Set_Is_Immediately_Visible (E, True);
13699 Next_Entity (E);
13700 end loop;
13702 -- If instantiation is declared in a block, it is the enclosing
13703 -- scope that might be a parent instance. Note that only one
13704 -- block can be involved, because the parent instances have
13705 -- been installed within it.
13707 if Ekind (P) = E_Block then
13708 Cur_P := Scope (P);
13709 else
13710 Cur_P := P;
13711 end if;
13713 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13714 -- We are within an instance of some sibling. Retain
13715 -- visibility of parent, for proper subsequent cleanup, and
13716 -- reinstall private declarations as well.
13718 Set_In_Private_Part (P);
13719 Install_Private_Declarations (P);
13720 end if;
13722 -- If the ultimate parent is a top-level unit recorded in
13723 -- Instance_Parent_Unit, then reset its visibility to what it was
13724 -- before instantiation. (It's not clear what the purpose is of
13725 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13726 -- present before the ultimate parent test was added.???)
13728 elsif not In_Open_Scopes (Scope (P))
13729 or else (P = Instance_Parent_Unit
13730 and then not Parent_Unit_Visible)
13731 then
13732 Set_Is_Immediately_Visible (P, False);
13734 -- If the current scope is itself an instantiation of a generic
13735 -- nested within P, and we are in the private part of body of this
13736 -- instantiation, restore the full views of P, that were removed
13737 -- in End_Package_Scope above. This obscure case can occur when a
13738 -- subunit of a generic contains an instance of a child unit of
13739 -- its generic parent unit.
13741 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13742 declare
13743 Par : constant Entity_Id :=
13744 Generic_Parent (Package_Specification (S));
13745 begin
13746 if Present (Par)
13747 and then P = Scope (Par)
13748 and then (In_Package_Body (S) or else In_Private_Part (S))
13749 then
13750 Set_In_Private_Part (P);
13751 Install_Private_Declarations (P);
13752 end if;
13753 end;
13754 end if;
13755 end loop;
13757 -- Reset visibility of entities in the enclosing scope
13759 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13761 Hidden := First_Elmt (Hidden_Entities);
13762 while Present (Hidden) loop
13763 Set_Is_Immediately_Visible (Node (Hidden), True);
13764 Next_Elmt (Hidden);
13765 end loop;
13767 else
13768 -- Each body is analyzed separately, and there is no context that
13769 -- needs preserving from one body instance to the next, so remove all
13770 -- parent scopes that have been installed.
13772 while Present (S) loop
13773 End_Package_Scope (S);
13774 Set_Is_Immediately_Visible (S, False);
13775 S := Current_Scope;
13776 exit when S = Standard_Standard;
13777 end loop;
13778 end if;
13779 end Remove_Parent;
13781 -----------------
13782 -- Restore_Env --
13783 -----------------
13785 procedure Restore_Env is
13786 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13788 begin
13789 if No (Current_Instantiated_Parent.Act_Id) then
13790 -- Restore environment after subprogram inlining
13792 Restore_Private_Views (Empty);
13793 end if;
13795 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13796 Exchanged_Views := Saved.Exchanged_Views;
13797 Hidden_Entities := Saved.Hidden_Entities;
13798 Current_Sem_Unit := Saved.Current_Sem_Unit;
13799 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13800 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13802 Restore_Opt_Config_Switches (Saved.Switches);
13804 Instance_Envs.Decrement_Last;
13805 end Restore_Env;
13807 ---------------------------
13808 -- Restore_Private_Views --
13809 ---------------------------
13811 procedure Restore_Private_Views
13812 (Pack_Id : Entity_Id;
13813 Is_Package : Boolean := True)
13815 M : Elmt_Id;
13816 E : Entity_Id;
13817 Typ : Entity_Id;
13818 Dep_Elmt : Elmt_Id;
13819 Dep_Typ : Node_Id;
13821 procedure Restore_Nested_Formal (Formal : Entity_Id);
13822 -- Hide the generic formals of formal packages declared with box which
13823 -- were reachable in the current instantiation.
13825 ---------------------------
13826 -- Restore_Nested_Formal --
13827 ---------------------------
13829 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13830 Ent : Entity_Id;
13832 begin
13833 if Present (Renamed_Object (Formal))
13834 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13835 then
13836 return;
13838 elsif Present (Associated_Formal_Package (Formal)) then
13839 Ent := First_Entity (Formal);
13840 while Present (Ent) loop
13841 exit when Ekind (Ent) = E_Package
13842 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13844 Set_Is_Hidden (Ent);
13845 Set_Is_Potentially_Use_Visible (Ent, False);
13847 -- If package, then recurse
13849 if Ekind (Ent) = E_Package then
13850 Restore_Nested_Formal (Ent);
13851 end if;
13853 Next_Entity (Ent);
13854 end loop;
13855 end if;
13856 end Restore_Nested_Formal;
13858 -- Start of processing for Restore_Private_Views
13860 begin
13861 M := First_Elmt (Exchanged_Views);
13862 while Present (M) loop
13863 Typ := Node (M);
13865 -- Subtypes of types whose views have been exchanged, and that are
13866 -- defined within the instance, were not on the Private_Dependents
13867 -- list on entry to the instance, so they have to be exchanged
13868 -- explicitly now, in order to remain consistent with the view of the
13869 -- parent type.
13871 if Ekind_In (Typ, E_Private_Type,
13872 E_Limited_Private_Type,
13873 E_Record_Type_With_Private)
13874 then
13875 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13876 while Present (Dep_Elmt) loop
13877 Dep_Typ := Node (Dep_Elmt);
13879 if Scope (Dep_Typ) = Pack_Id
13880 and then Present (Full_View (Dep_Typ))
13881 then
13882 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13883 Exchange_Declarations (Dep_Typ);
13884 end if;
13886 Next_Elmt (Dep_Elmt);
13887 end loop;
13888 end if;
13890 Exchange_Declarations (Node (M));
13891 Next_Elmt (M);
13892 end loop;
13894 if No (Pack_Id) then
13895 return;
13896 end if;
13898 -- Make the generic formal parameters private, and make the formal types
13899 -- into subtypes of the actuals again.
13901 E := First_Entity (Pack_Id);
13902 while Present (E) loop
13903 Set_Is_Hidden (E, True);
13905 if Is_Type (E)
13906 and then Nkind (Parent (E)) = N_Subtype_Declaration
13907 then
13908 -- If the actual for E is itself a generic actual type from
13909 -- an enclosing instance, E is still a generic actual type
13910 -- outside of the current instance. This matter when resolving
13911 -- an overloaded call that may be ambiguous in the enclosing
13912 -- instance, when two of its actuals coincide.
13914 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13915 and then Is_Generic_Actual_Type
13916 (Entity (Subtype_Indication (Parent (E))))
13917 then
13918 null;
13919 else
13920 Set_Is_Generic_Actual_Type (E, False);
13921 end if;
13923 -- An unusual case of aliasing: the actual may also be directly
13924 -- visible in the generic, and be private there, while it is fully
13925 -- visible in the context of the instance. The internal subtype
13926 -- is private in the instance but has full visibility like its
13927 -- parent in the enclosing scope. This enforces the invariant that
13928 -- the privacy status of all private dependents of a type coincide
13929 -- with that of the parent type. This can only happen when a
13930 -- generic child unit is instantiated within a sibling.
13932 if Is_Private_Type (E)
13933 and then not Is_Private_Type (Etype (E))
13934 then
13935 Exchange_Declarations (E);
13936 end if;
13938 elsif Ekind (E) = E_Package then
13940 -- The end of the renaming list is the renaming of the generic
13941 -- package itself. If the instance is a subprogram, all entities
13942 -- in the corresponding package are renamings. If this entity is
13943 -- a formal package, make its own formals private as well. The
13944 -- actual in this case is itself the renaming of an instantiation.
13945 -- If the entity is not a package renaming, it is the entity
13946 -- created to validate formal package actuals: ignore it.
13948 -- If the actual is itself a formal package for the enclosing
13949 -- generic, or the actual for such a formal package, it remains
13950 -- visible on exit from the instance, and therefore nothing needs
13951 -- to be done either, except to keep it accessible.
13953 if Is_Package and then Renamed_Object (E) = Pack_Id then
13954 exit;
13956 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13957 null;
13959 elsif
13960 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13961 then
13962 Set_Is_Hidden (E, False);
13964 else
13965 declare
13966 Act_P : constant Entity_Id := Renamed_Object (E);
13967 Id : Entity_Id;
13969 begin
13970 Id := First_Entity (Act_P);
13971 while Present (Id)
13972 and then Id /= First_Private_Entity (Act_P)
13973 loop
13974 exit when Ekind (Id) = E_Package
13975 and then Renamed_Object (Id) = Act_P;
13977 Set_Is_Hidden (Id, True);
13978 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13980 if Ekind (Id) = E_Package then
13981 Restore_Nested_Formal (Id);
13982 end if;
13984 Next_Entity (Id);
13985 end loop;
13986 end;
13987 end if;
13988 end if;
13990 Next_Entity (E);
13991 end loop;
13992 end Restore_Private_Views;
13994 --------------
13995 -- Save_Env --
13996 --------------
13998 procedure Save_Env
13999 (Gen_Unit : Entity_Id;
14000 Act_Unit : Entity_Id)
14002 begin
14003 Init_Env;
14004 Set_Instance_Env (Gen_Unit, Act_Unit);
14005 end Save_Env;
14007 ----------------------------
14008 -- Save_Global_References --
14009 ----------------------------
14011 procedure Save_Global_References (Templ : Node_Id) is
14013 -- ??? it is horrible to use global variables in highly recursive code
14015 E : Entity_Id;
14016 -- The entity of the current associated node
14018 Gen_Scope : Entity_Id;
14019 -- The scope of the generic for which references are being saved
14021 N2 : Node_Id;
14022 -- The current associated node
14024 function Is_Global (E : Entity_Id) return Boolean;
14025 -- Check whether entity is defined outside of generic unit. Examine the
14026 -- scope of an entity, and the scope of the scope, etc, until we find
14027 -- either Standard, in which case the entity is global, or the generic
14028 -- unit itself, which indicates that the entity is local. If the entity
14029 -- is the generic unit itself, as in the case of a recursive call, or
14030 -- the enclosing generic unit, if different from the current scope, then
14031 -- it is local as well, because it will be replaced at the point of
14032 -- instantiation. On the other hand, if it is a reference to a child
14033 -- unit of a common ancestor, which appears in an instantiation, it is
14034 -- global because it is used to denote a specific compilation unit at
14035 -- the time the instantiations will be analyzed.
14037 procedure Qualify_Universal_Operands
14038 (Op : Node_Id;
14039 Func_Call : Node_Id);
14040 -- Op denotes a binary or unary operator in generic template Templ. Node
14041 -- Func_Call is the function call alternative of the operator within the
14042 -- the analyzed copy of the template. Change each operand which yields a
14043 -- universal type by wrapping it into a qualified expression
14045 -- Actual_Typ'(Operand)
14047 -- where Actual_Typ is the type of corresponding actual parameter of
14048 -- Operand in Func_Call.
14050 procedure Reset_Entity (N : Node_Id);
14051 -- Save semantic information on global entity so that it is not resolved
14052 -- again at instantiation time.
14054 procedure Save_Entity_Descendants (N : Node_Id);
14055 -- Apply Save_Global_References to the two syntactic descendants of
14056 -- non-terminal nodes that carry an Associated_Node and are processed
14057 -- through Reset_Entity. Once the global entity (if any) has been
14058 -- captured together with its type, only two syntactic descendants need
14059 -- to be traversed to complete the processing of the tree rooted at N.
14060 -- This applies to Selected_Components, Expanded_Names, and to Operator
14061 -- nodes. N can also be a character literal, identifier, or operator
14062 -- symbol node, but the call has no effect in these cases.
14064 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14065 -- Default actuals in nested instances must be handled specially
14066 -- because there is no link to them from the original tree. When an
14067 -- actual subprogram is given by a default, we add an explicit generic
14068 -- association for it in the instantiation node. When we save the
14069 -- global references on the name of the instance, we recover the list
14070 -- of generic associations, and add an explicit one to the original
14071 -- generic tree, through which a global actual can be preserved.
14072 -- Similarly, if a child unit is instantiated within a sibling, in the
14073 -- context of the parent, we must preserve the identifier of the parent
14074 -- so that it can be properly resolved in a subsequent instantiation.
14076 procedure Save_Global_Descendant (D : Union_Id);
14077 -- Apply Save_References recursively to the descendants of node D
14079 procedure Save_References (N : Node_Id);
14080 -- This is the recursive procedure that does the work, once the
14081 -- enclosing generic scope has been established.
14083 ---------------
14084 -- Is_Global --
14085 ---------------
14087 function Is_Global (E : Entity_Id) return Boolean is
14088 Se : Entity_Id;
14090 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14091 -- Determine whether the parent node of a reference to a child unit
14092 -- denotes an instantiation or a formal package, in which case the
14093 -- reference to the child unit is global, even if it appears within
14094 -- the current scope (e.g. when the instance appears within the body
14095 -- of an ancestor).
14097 ----------------------
14098 -- Is_Instance_Node --
14099 ----------------------
14101 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14102 begin
14103 return Nkind (Decl) in N_Generic_Instantiation
14104 or else
14105 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14106 end Is_Instance_Node;
14108 -- Start of processing for Is_Global
14110 begin
14111 if E = Gen_Scope then
14112 return False;
14114 elsif E = Standard_Standard then
14115 return True;
14117 elsif Is_Child_Unit (E)
14118 and then (Is_Instance_Node (Parent (N2))
14119 or else (Nkind (Parent (N2)) = N_Expanded_Name
14120 and then N2 = Selector_Name (Parent (N2))
14121 and then
14122 Is_Instance_Node (Parent (Parent (N2)))))
14123 then
14124 return True;
14126 else
14127 Se := Scope (E);
14128 while Se /= Gen_Scope loop
14129 if Se = Standard_Standard then
14130 return True;
14131 else
14132 Se := Scope (Se);
14133 end if;
14134 end loop;
14136 return False;
14137 end if;
14138 end Is_Global;
14140 --------------------------------
14141 -- Qualify_Universal_Operands --
14142 --------------------------------
14144 procedure Qualify_Universal_Operands
14145 (Op : Node_Id;
14146 Func_Call : Node_Id)
14148 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14149 -- Rewrite operand Opnd as a qualified expression of the form
14151 -- Actual_Typ'(Opnd)
14153 -- where Actual is the corresponding actual parameter of Opnd in
14154 -- function call Func_Call.
14156 function Qualify_Type
14157 (Loc : Source_Ptr;
14158 Typ : Entity_Id) return Node_Id;
14159 -- Qualify type Typ by creating a selected component of the form
14161 -- Scope_Of_Typ.Typ
14163 ---------------------
14164 -- Qualify_Operand --
14165 ---------------------
14167 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14168 Loc : constant Source_Ptr := Sloc (Opnd);
14169 Typ : constant Entity_Id := Etype (Actual);
14170 Mark : Node_Id;
14171 Qual : Node_Id;
14173 begin
14174 -- Qualify the operand when it is of a universal type. Note that
14175 -- the template is unanalyzed and it is not possible to directly
14176 -- query the type. This transformation is not done when the type
14177 -- of the actual is internally generated because the type will be
14178 -- regenerated in the instance.
14180 if Yields_Universal_Type (Opnd)
14181 and then Comes_From_Source (Typ)
14182 and then not Is_Hidden (Typ)
14183 then
14184 -- The type of the actual may be a global reference. Save this
14185 -- information by creating a reference to it.
14187 if Is_Global (Typ) then
14188 Mark := New_Occurrence_Of (Typ, Loc);
14190 -- Otherwise rely on resolution to find the proper type within
14191 -- the instance.
14193 else
14194 Mark := Qualify_Type (Loc, Typ);
14195 end if;
14197 Qual :=
14198 Make_Qualified_Expression (Loc,
14199 Subtype_Mark => Mark,
14200 Expression => Relocate_Node (Opnd));
14202 -- Mark the qualification to distinguish it from other source
14203 -- constructs and signal the instantiation mechanism that this
14204 -- node requires special processing. See Copy_Generic_Node for
14205 -- details.
14207 Set_Is_Qualified_Universal_Literal (Qual);
14209 Rewrite (Opnd, Qual);
14210 end if;
14211 end Qualify_Operand;
14213 ------------------
14214 -- Qualify_Type --
14215 ------------------
14217 function Qualify_Type
14218 (Loc : Source_Ptr;
14219 Typ : Entity_Id) return Node_Id
14221 Scop : constant Entity_Id := Scope (Typ);
14222 Result : Node_Id;
14224 begin
14225 Result := Make_Identifier (Loc, Chars (Typ));
14227 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14228 Result :=
14229 Make_Selected_Component (Loc,
14230 Prefix => Make_Identifier (Loc, Chars (Scop)),
14231 Selector_Name => Result);
14232 end if;
14234 return Result;
14235 end Qualify_Type;
14237 -- Local variables
14239 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14241 -- Start of processing for Qualify_Universal_Operands
14243 begin
14244 if Nkind (Op) in N_Binary_Op then
14245 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14246 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14248 elsif Nkind (Op) in N_Unary_Op then
14249 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14250 end if;
14251 end Qualify_Universal_Operands;
14253 ------------------
14254 -- Reset_Entity --
14255 ------------------
14257 procedure Reset_Entity (N : Node_Id) is
14258 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14259 -- If the type of N2 is global to the generic unit, save the type in
14260 -- the generic node. Just as we perform name capture for explicit
14261 -- references within the generic, we must capture the global types
14262 -- of local entities because they may participate in resolution in
14263 -- the instance.
14265 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14266 -- Find the ultimate ancestor of the current unit. If it is not a
14267 -- generic unit, then the name of the current unit in the prefix of
14268 -- an expanded name must be replaced with its generic homonym to
14269 -- ensure that it will be properly resolved in an instance.
14271 ---------------------
14272 -- Set_Global_Type --
14273 ---------------------
14275 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14276 Typ : constant Entity_Id := Etype (N2);
14278 begin
14279 Set_Etype (N, Typ);
14281 -- If the entity of N is not the associated node, this is a
14282 -- nested generic and it has an associated node as well, whose
14283 -- type is already the full view (see below). Indicate that the
14284 -- original node has a private view.
14286 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14287 Set_Has_Private_View (N);
14288 end if;
14290 -- If not a private type, nothing else to do
14292 if not Is_Private_Type (Typ) then
14293 if Is_Array_Type (Typ)
14294 and then Is_Private_Type (Component_Type (Typ))
14295 then
14296 Set_Has_Private_View (N);
14297 end if;
14299 -- If it is a derivation of a private type in a context where no
14300 -- full view is needed, nothing to do either.
14302 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14303 null;
14305 -- Otherwise mark the type for flipping and use the full view when
14306 -- available.
14308 else
14309 Set_Has_Private_View (N);
14311 if Present (Full_View (Typ)) then
14312 Set_Etype (N2, Full_View (Typ));
14313 end if;
14314 end if;
14316 if Is_Floating_Point_Type (Typ)
14317 and then Has_Dimension_System (Typ)
14318 then
14319 Copy_Dimensions (N2, N);
14320 end if;
14321 end Set_Global_Type;
14323 ------------------
14324 -- Top_Ancestor --
14325 ------------------
14327 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14328 Par : Entity_Id;
14330 begin
14331 Par := E;
14332 while Is_Child_Unit (Par) loop
14333 Par := Scope (Par);
14334 end loop;
14336 return Par;
14337 end Top_Ancestor;
14339 -- Start of processing for Reset_Entity
14341 begin
14342 N2 := Get_Associated_Node (N);
14343 E := Entity (N2);
14345 if Present (E) then
14347 -- If the node is an entry call to an entry in an enclosing task,
14348 -- it is rewritten as a selected component. No global entity to
14349 -- preserve in this case, since the expansion will be redone in
14350 -- the instance.
14352 if not Nkind_In (E, N_Defining_Character_Literal,
14353 N_Defining_Identifier,
14354 N_Defining_Operator_Symbol)
14355 then
14356 Set_Associated_Node (N, Empty);
14357 Set_Etype (N, Empty);
14358 return;
14359 end if;
14361 -- If the entity is an itype created as a subtype of an access
14362 -- type with a null exclusion restore source entity for proper
14363 -- visibility. The itype will be created anew in the instance.
14365 if Is_Itype (E)
14366 and then Ekind (E) = E_Access_Subtype
14367 and then Is_Entity_Name (N)
14368 and then Chars (Etype (E)) = Chars (N)
14369 then
14370 E := Etype (E);
14371 Set_Entity (N2, E);
14372 Set_Etype (N2, E);
14373 end if;
14375 if Is_Global (E) then
14377 -- If the entity is a package renaming that is the prefix of
14378 -- an expanded name, it has been rewritten as the renamed
14379 -- package, which is necessary semantically but complicates
14380 -- ASIS tree traversal, so we recover the original entity to
14381 -- expose the renaming. Take into account that the context may
14382 -- be a nested generic, that the original node may itself have
14383 -- an associated node that had better be an entity, and that
14384 -- the current node is still a selected component.
14386 if Ekind (E) = E_Package
14387 and then Nkind (N) = N_Selected_Component
14388 and then Nkind (Parent (N)) = N_Expanded_Name
14389 and then Present (Original_Node (N2))
14390 and then Is_Entity_Name (Original_Node (N2))
14391 and then Present (Entity (Original_Node (N2)))
14392 then
14393 if Is_Global (Entity (Original_Node (N2))) then
14394 N2 := Original_Node (N2);
14395 Set_Associated_Node (N, N2);
14396 Set_Global_Type (N, N2);
14398 -- Renaming is local, and will be resolved in instance
14400 else
14401 Set_Associated_Node (N, Empty);
14402 Set_Etype (N, Empty);
14403 end if;
14405 else
14406 Set_Global_Type (N, N2);
14407 end if;
14409 elsif Nkind (N) = N_Op_Concat
14410 and then Is_Generic_Type (Etype (N2))
14411 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14412 or else
14413 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14414 and then Is_Intrinsic_Subprogram (E)
14415 then
14416 null;
14418 -- Entity is local. Mark generic node as unresolved. Note that now
14419 -- it does not have an entity.
14421 else
14422 Set_Associated_Node (N, Empty);
14423 Set_Etype (N, Empty);
14424 end if;
14426 if Nkind (Parent (N)) in N_Generic_Instantiation
14427 and then N = Name (Parent (N))
14428 then
14429 Save_Global_Defaults (Parent (N), Parent (N2));
14430 end if;
14432 elsif Nkind (Parent (N)) = N_Selected_Component
14433 and then Nkind (Parent (N2)) = N_Expanded_Name
14434 then
14435 if Is_Global (Entity (Parent (N2))) then
14436 Change_Selected_Component_To_Expanded_Name (Parent (N));
14437 Set_Associated_Node (Parent (N), Parent (N2));
14438 Set_Global_Type (Parent (N), Parent (N2));
14439 Save_Entity_Descendants (N);
14441 -- If this is a reference to the current generic entity, replace
14442 -- by the name of the generic homonym of the current package. This
14443 -- is because in an instantiation Par.P.Q will not resolve to the
14444 -- name of the instance, whose enclosing scope is not necessarily
14445 -- Par. We use the generic homonym rather that the name of the
14446 -- generic itself because it may be hidden by a local declaration.
14448 elsif In_Open_Scopes (Entity (Parent (N2)))
14449 and then not
14450 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14451 then
14452 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14453 Rewrite (Parent (N),
14454 Make_Identifier (Sloc (N),
14455 Chars =>
14456 Chars (Generic_Homonym (Entity (Parent (N2))))));
14457 else
14458 Rewrite (Parent (N),
14459 Make_Identifier (Sloc (N),
14460 Chars => Chars (Selector_Name (Parent (N2)))));
14461 end if;
14462 end if;
14464 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14465 and then Parent (N) = Name (Parent (Parent (N)))
14466 then
14467 Save_Global_Defaults
14468 (Parent (Parent (N)), Parent (Parent (N2)));
14469 end if;
14471 -- A selected component may denote a static constant that has been
14472 -- folded. If the static constant is global to the generic, capture
14473 -- its value. Otherwise the folding will happen in any instantiation.
14475 elsif Nkind (Parent (N)) = N_Selected_Component
14476 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14477 then
14478 if Present (Entity (Original_Node (Parent (N2))))
14479 and then Is_Global (Entity (Original_Node (Parent (N2))))
14480 then
14481 Rewrite (Parent (N), New_Copy (Parent (N2)));
14482 Set_Analyzed (Parent (N), False);
14483 end if;
14485 -- A selected component may be transformed into a parameterless
14486 -- function call. If the called entity is global, rewrite the node
14487 -- appropriately, i.e. as an extended name for the global entity.
14489 elsif Nkind (Parent (N)) = N_Selected_Component
14490 and then Nkind (Parent (N2)) = N_Function_Call
14491 and then N = Selector_Name (Parent (N))
14492 then
14493 if No (Parameter_Associations (Parent (N2))) then
14494 if Is_Global (Entity (Name (Parent (N2)))) then
14495 Change_Selected_Component_To_Expanded_Name (Parent (N));
14496 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14497 Set_Global_Type (Parent (N), Name (Parent (N2)));
14498 Save_Entity_Descendants (N);
14500 else
14501 Set_Is_Prefixed_Call (Parent (N));
14502 Set_Associated_Node (N, Empty);
14503 Set_Etype (N, Empty);
14504 end if;
14506 -- In Ada 2005, X.F may be a call to a primitive operation,
14507 -- rewritten as F (X). This rewriting will be done again in an
14508 -- instance, so keep the original node. Global entities will be
14509 -- captured as for other constructs. Indicate that this must
14510 -- resolve as a call, to prevent accidental overloading in the
14511 -- instance, if both a component and a primitive operation appear
14512 -- as candidates.
14514 else
14515 Set_Is_Prefixed_Call (Parent (N));
14516 end if;
14518 -- Entity is local. Reset in generic unit, so that node is resolved
14519 -- anew at the point of instantiation.
14521 else
14522 Set_Associated_Node (N, Empty);
14523 Set_Etype (N, Empty);
14524 end if;
14525 end Reset_Entity;
14527 -----------------------------
14528 -- Save_Entity_Descendants --
14529 -----------------------------
14531 procedure Save_Entity_Descendants (N : Node_Id) is
14532 begin
14533 case Nkind (N) is
14534 when N_Binary_Op =>
14535 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14536 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14538 when N_Unary_Op =>
14539 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14541 when N_Expanded_Name
14542 | N_Selected_Component
14544 Save_Global_Descendant (Union_Id (Prefix (N)));
14545 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14547 when N_Character_Literal
14548 | N_Identifier
14549 | N_Operator_Symbol
14551 null;
14553 when others =>
14554 raise Program_Error;
14555 end case;
14556 end Save_Entity_Descendants;
14558 --------------------------
14559 -- Save_Global_Defaults --
14560 --------------------------
14562 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14563 Loc : constant Source_Ptr := Sloc (N1);
14564 Assoc2 : constant List_Id := Generic_Associations (N2);
14565 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14566 Assoc1 : List_Id;
14567 Act1 : Node_Id;
14568 Act2 : Node_Id;
14569 Def : Node_Id;
14570 Ndec : Node_Id;
14571 Subp : Entity_Id;
14572 Actual : Entity_Id;
14574 begin
14575 Assoc1 := Generic_Associations (N1);
14577 if Present (Assoc1) then
14578 Act1 := First (Assoc1);
14579 else
14580 Act1 := Empty;
14581 Set_Generic_Associations (N1, New_List);
14582 Assoc1 := Generic_Associations (N1);
14583 end if;
14585 if Present (Assoc2) then
14586 Act2 := First (Assoc2);
14587 else
14588 return;
14589 end if;
14591 while Present (Act1) and then Present (Act2) loop
14592 Next (Act1);
14593 Next (Act2);
14594 end loop;
14596 -- Find the associations added for default subprograms
14598 if Present (Act2) then
14599 while Nkind (Act2) /= N_Generic_Association
14600 or else No (Entity (Selector_Name (Act2)))
14601 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14602 loop
14603 Next (Act2);
14604 end loop;
14606 -- Add a similar association if the default is global. The
14607 -- renaming declaration for the actual has been analyzed, and
14608 -- its alias is the program it renames. Link the actual in the
14609 -- original generic tree with the node in the analyzed tree.
14611 while Present (Act2) loop
14612 Subp := Entity (Selector_Name (Act2));
14613 Def := Explicit_Generic_Actual_Parameter (Act2);
14615 -- Following test is defence against rubbish errors
14617 if No (Alias (Subp)) then
14618 return;
14619 end if;
14621 -- Retrieve the resolved actual from the renaming declaration
14622 -- created for the instantiated formal.
14624 Actual := Entity (Name (Parent (Parent (Subp))));
14625 Set_Entity (Def, Actual);
14626 Set_Etype (Def, Etype (Actual));
14628 if Is_Global (Actual) then
14629 Ndec :=
14630 Make_Generic_Association (Loc,
14631 Selector_Name =>
14632 New_Occurrence_Of (Subp, Loc),
14633 Explicit_Generic_Actual_Parameter =>
14634 New_Occurrence_Of (Actual, Loc));
14636 Set_Associated_Node
14637 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14639 Append (Ndec, Assoc1);
14641 -- If there are other defaults, add a dummy association in case
14642 -- there are other defaulted formals with the same name.
14644 elsif Present (Next (Act2)) then
14645 Ndec :=
14646 Make_Generic_Association (Loc,
14647 Selector_Name =>
14648 New_Occurrence_Of (Subp, Loc),
14649 Explicit_Generic_Actual_Parameter => Empty);
14651 Append (Ndec, Assoc1);
14652 end if;
14654 Next (Act2);
14655 end loop;
14656 end if;
14658 if Nkind (Name (N1)) = N_Identifier
14659 and then Is_Child_Unit (Gen_Id)
14660 and then Is_Global (Gen_Id)
14661 and then Is_Generic_Unit (Scope (Gen_Id))
14662 and then In_Open_Scopes (Scope (Gen_Id))
14663 then
14664 -- This is an instantiation of a child unit within a sibling, so
14665 -- that the generic parent is in scope. An eventual instance must
14666 -- occur within the scope of an instance of the parent. Make name
14667 -- in instance into an expanded name, to preserve the identifier
14668 -- of the parent, so it can be resolved subsequently.
14670 Rewrite (Name (N2),
14671 Make_Expanded_Name (Loc,
14672 Chars => Chars (Gen_Id),
14673 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14674 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14675 Set_Entity (Name (N2), Gen_Id);
14677 Rewrite (Name (N1),
14678 Make_Expanded_Name (Loc,
14679 Chars => Chars (Gen_Id),
14680 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14681 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14683 Set_Associated_Node (Name (N1), Name (N2));
14684 Set_Associated_Node (Prefix (Name (N1)), Empty);
14685 Set_Associated_Node
14686 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14687 Set_Etype (Name (N1), Etype (Gen_Id));
14688 end if;
14689 end Save_Global_Defaults;
14691 ----------------------------
14692 -- Save_Global_Descendant --
14693 ----------------------------
14695 procedure Save_Global_Descendant (D : Union_Id) is
14696 N1 : Node_Id;
14698 begin
14699 if D in Node_Range then
14700 if D = Union_Id (Empty) then
14701 null;
14703 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14704 Save_References (Node_Id (D));
14705 end if;
14707 elsif D in List_Range then
14708 pragma Assert (D /= Union_Id (No_List));
14709 -- Because No_List = Empty, which is in Node_Range above
14711 if Is_Empty_List (List_Id (D)) then
14712 null;
14714 else
14715 N1 := First (List_Id (D));
14716 while Present (N1) loop
14717 Save_References (N1);
14718 Next (N1);
14719 end loop;
14720 end if;
14722 -- Element list or other non-node field, nothing to do
14724 else
14725 null;
14726 end if;
14727 end Save_Global_Descendant;
14729 ---------------------
14730 -- Save_References --
14731 ---------------------
14733 -- This is the recursive procedure that does the work once the enclosing
14734 -- generic scope has been established. We have to treat specially a
14735 -- number of node rewritings that are required by semantic processing
14736 -- and which change the kind of nodes in the generic copy: typically
14737 -- constant-folding, replacing an operator node by a string literal, or
14738 -- a selected component by an expanded name. In each of those cases, the
14739 -- transformation is propagated to the generic unit.
14741 procedure Save_References (N : Node_Id) is
14742 Loc : constant Source_Ptr := Sloc (N);
14744 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14745 -- Determine whether arbitrary node Nod requires delayed capture of
14746 -- global references within its aspect specifications.
14748 procedure Save_References_In_Aggregate (N : Node_Id);
14749 -- Save all global references in [extension] aggregate node N
14751 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14752 -- Save all global references in a character literal or operator
14753 -- symbol denoted by N.
14755 procedure Save_References_In_Descendants (N : Node_Id);
14756 -- Save all global references in all descendants of node N
14758 procedure Save_References_In_Identifier (N : Node_Id);
14759 -- Save all global references in identifier node N
14761 procedure Save_References_In_Operator (N : Node_Id);
14762 -- Save all global references in operator node N
14764 procedure Save_References_In_Pragma (Prag : Node_Id);
14765 -- Save all global references found within the expression of pragma
14766 -- Prag.
14768 ---------------------------
14769 -- Requires_Delayed_Save --
14770 ---------------------------
14772 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14773 begin
14774 -- Generic packages and subprograms require delayed capture of
14775 -- global references within their aspects due to the timing of
14776 -- annotation analysis.
14778 if Nkind_In (Nod, N_Generic_Package_Declaration,
14779 N_Generic_Subprogram_Declaration,
14780 N_Package_Body,
14781 N_Package_Body_Stub,
14782 N_Subprogram_Body,
14783 N_Subprogram_Body_Stub)
14784 then
14785 -- Since the capture of global references is done on the
14786 -- unanalyzed generic template, there is no information around
14787 -- to infer the context. Use the Associated_Entity linkages to
14788 -- peek into the analyzed generic copy and determine what the
14789 -- template corresponds to.
14791 if Nod = Templ then
14792 return
14793 Is_Generic_Declaration_Or_Body
14794 (Unit_Declaration_Node
14795 (Associated_Entity (Defining_Entity (Nod))));
14797 -- Otherwise the generic unit being processed is not the top
14798 -- level template. It is safe to capture of global references
14799 -- within the generic unit because at this point the top level
14800 -- copy is fully analyzed.
14802 else
14803 return False;
14804 end if;
14806 -- Otherwise capture the global references without interference
14808 else
14809 return False;
14810 end if;
14811 end Requires_Delayed_Save;
14813 ----------------------------------
14814 -- Save_References_In_Aggregate --
14815 ----------------------------------
14817 procedure Save_References_In_Aggregate (N : Node_Id) is
14818 Nam : Node_Id;
14819 Qual : Node_Id := Empty;
14820 Typ : Entity_Id := Empty;
14822 use Atree.Unchecked_Access;
14823 -- This code section is part of implementing an untyped tree
14824 -- traversal, so it needs direct access to node fields.
14826 begin
14827 N2 := Get_Associated_Node (N);
14829 if Present (N2) then
14830 Typ := Etype (N2);
14832 -- In an instance within a generic, use the name of the actual
14833 -- and not the original generic parameter. If the actual is
14834 -- global in the current generic it must be preserved for its
14835 -- instantiation.
14837 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14838 and then Present (Generic_Parent_Type (Parent (Typ)))
14839 then
14840 Typ := Base_Type (Typ);
14841 Set_Etype (N2, Typ);
14842 end if;
14843 end if;
14845 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14846 Set_Associated_Node (N, Empty);
14848 -- If the aggregate is an actual in a call, it has been
14849 -- resolved in the current context, to some local type. The
14850 -- enclosing call may have been disambiguated by the aggregate,
14851 -- and this disambiguation might fail at instantiation time
14852 -- because the type to which the aggregate did resolve is not
14853 -- preserved. In order to preserve some of this information,
14854 -- wrap the aggregate in a qualified expression, using the id
14855 -- of its type. For further disambiguation we qualify the type
14856 -- name with its scope (if visible) because both id's will have
14857 -- corresponding entities in an instance. This resolves most of
14858 -- the problems with missing type information on aggregates in
14859 -- instances.
14861 if Present (N2)
14862 and then Nkind (N2) = Nkind (N)
14863 and then Nkind (Parent (N2)) in N_Subprogram_Call
14864 and then Present (Typ)
14865 and then Comes_From_Source (Typ)
14866 then
14867 Nam := Make_Identifier (Loc, Chars (Typ));
14869 if Is_Immediately_Visible (Scope (Typ)) then
14870 Nam :=
14871 Make_Selected_Component (Loc,
14872 Prefix =>
14873 Make_Identifier (Loc, Chars (Scope (Typ))),
14874 Selector_Name => Nam);
14875 end if;
14877 Qual :=
14878 Make_Qualified_Expression (Loc,
14879 Subtype_Mark => Nam,
14880 Expression => Relocate_Node (N));
14881 end if;
14882 end if;
14884 Save_Global_Descendant (Field1 (N));
14885 Save_Global_Descendant (Field2 (N));
14886 Save_Global_Descendant (Field3 (N));
14887 Save_Global_Descendant (Field5 (N));
14889 if Present (Qual) then
14890 Rewrite (N, Qual);
14891 end if;
14892 end Save_References_In_Aggregate;
14894 ----------------------------------------------
14895 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14896 ----------------------------------------------
14898 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14899 begin
14900 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14901 Reset_Entity (N);
14903 elsif Nkind (N) = N_Operator_Symbol
14904 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14905 then
14906 Change_Operator_Symbol_To_String_Literal (N);
14907 end if;
14908 end Save_References_In_Char_Lit_Or_Op_Symbol;
14910 ------------------------------------
14911 -- Save_References_In_Descendants --
14912 ------------------------------------
14914 procedure Save_References_In_Descendants (N : Node_Id) is
14915 use Atree.Unchecked_Access;
14916 -- This code section is part of implementing an untyped tree
14917 -- traversal, so it needs direct access to node fields.
14919 begin
14920 Save_Global_Descendant (Field1 (N));
14921 Save_Global_Descendant (Field2 (N));
14922 Save_Global_Descendant (Field3 (N));
14923 Save_Global_Descendant (Field4 (N));
14924 Save_Global_Descendant (Field5 (N));
14925 end Save_References_In_Descendants;
14927 -----------------------------------
14928 -- Save_References_In_Identifier --
14929 -----------------------------------
14931 procedure Save_References_In_Identifier (N : Node_Id) is
14932 begin
14933 -- The node did not undergo a transformation
14935 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14936 declare
14937 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
14938 Orig_N2_Parent : constant Node_Id :=
14939 Original_Node (Parent (Aux_N2));
14940 begin
14941 -- The parent of this identifier is a selected component
14942 -- which denotes a named number that was constant folded.
14943 -- Preserve the original name for ASIS and link the parent
14944 -- with its expanded name. The constant folding will be
14945 -- repeated in the instance.
14947 if Nkind (Parent (N)) = N_Selected_Component
14948 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
14949 N_Real_Literal)
14950 and then Is_Entity_Name (Orig_N2_Parent)
14951 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
14952 and then Is_Global (Entity (Orig_N2_Parent))
14953 then
14954 N2 := Aux_N2;
14955 Set_Associated_Node
14956 (Parent (N), Original_Node (Parent (N2)));
14958 -- Common case
14960 else
14961 -- If this is a discriminant reference, always save it.
14962 -- It is used in the instance to find the corresponding
14963 -- discriminant positionally rather than by name.
14965 Set_Original_Discriminant
14966 (N, Original_Discriminant (Get_Associated_Node (N)));
14967 end if;
14969 Reset_Entity (N);
14970 end;
14972 -- The analysis of the generic copy transformed the identifier
14973 -- into another construct. Propagate the changes to the template.
14975 else
14976 N2 := Get_Associated_Node (N);
14978 -- The identifier denotes a call to a parameterless function.
14979 -- Mark the node as resolved when the function is external.
14981 if Nkind (N2) = N_Function_Call then
14982 E := Entity (Name (N2));
14984 if Present (E) and then Is_Global (E) then
14985 Set_Etype (N, Etype (N2));
14986 else
14987 Set_Associated_Node (N, Empty);
14988 Set_Etype (N, Empty);
14989 end if;
14991 -- The identifier denotes a named number that was constant
14992 -- folded. Preserve the original name for ASIS and undo the
14993 -- constant folding which will be repeated in the instance.
14995 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14996 and then Is_Entity_Name (Original_Node (N2))
14997 then
14998 Set_Associated_Node (N, Original_Node (N2));
14999 Reset_Entity (N);
15001 -- The identifier resolved to a string literal. Propagate this
15002 -- information to the generic template.
15004 elsif Nkind (N2) = N_String_Literal then
15005 Rewrite (N, New_Copy (N2));
15007 -- The identifier is rewritten as a dereference if it is the
15008 -- prefix of an implicit dereference. Preserve the original
15009 -- tree as the analysis of the instance will expand the node
15010 -- again, but preserve the resolved entity if it is global.
15012 elsif Nkind (N2) = N_Explicit_Dereference then
15013 if Is_Entity_Name (Prefix (N2))
15014 and then Present (Entity (Prefix (N2)))
15015 and then Is_Global (Entity (Prefix (N2)))
15016 then
15017 Set_Associated_Node (N, Prefix (N2));
15019 elsif Nkind (Prefix (N2)) = N_Function_Call
15020 and then Present (Entity (Name (Prefix (N2))))
15021 and then Is_Global (Entity (Name (Prefix (N2))))
15022 then
15023 Rewrite (N,
15024 Make_Explicit_Dereference (Loc,
15025 Prefix =>
15026 Make_Function_Call (Loc,
15027 Name =>
15028 New_Occurrence_Of
15029 (Entity (Name (Prefix (N2))), Loc))));
15031 else
15032 Set_Associated_Node (N, Empty);
15033 Set_Etype (N, Empty);
15034 end if;
15036 -- The subtype mark of a nominally unconstrained object is
15037 -- rewritten as a subtype indication using the bounds of the
15038 -- expression. Recover the original subtype mark.
15040 elsif Nkind (N2) = N_Subtype_Indication
15041 and then Is_Entity_Name (Original_Node (N2))
15042 then
15043 Set_Associated_Node (N, Original_Node (N2));
15044 Reset_Entity (N);
15045 end if;
15046 end if;
15047 end Save_References_In_Identifier;
15049 ---------------------------------
15050 -- Save_References_In_Operator --
15051 ---------------------------------
15053 procedure Save_References_In_Operator (N : Node_Id) is
15054 begin
15055 -- The node did not undergo a transformation
15057 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15058 if Nkind (N) = N_Op_Concat then
15059 Set_Is_Component_Left_Opnd (N,
15060 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15062 Set_Is_Component_Right_Opnd (N,
15063 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15064 end if;
15066 Reset_Entity (N);
15068 -- The analysis of the generic copy transformed the operator into
15069 -- some other construct. Propagate the changes to the template if
15070 -- applicable.
15072 else
15073 N2 := Get_Associated_Node (N);
15075 -- The operator resoved to a function call
15077 if Nkind (N2) = N_Function_Call then
15079 -- Add explicit qualifications in the generic template for
15080 -- all operands of universal type. This aids resolution by
15081 -- preserving the actual type of a literal or an attribute
15082 -- that yields a universal result.
15084 Qualify_Universal_Operands (N, N2);
15086 E := Entity (Name (N2));
15088 if Present (E) and then Is_Global (E) then
15089 Set_Etype (N, Etype (N2));
15090 else
15091 Set_Associated_Node (N, Empty);
15092 Set_Etype (N, Empty);
15093 end if;
15095 -- The operator was folded into a literal
15097 elsif Nkind_In (N2, N_Integer_Literal,
15098 N_Real_Literal,
15099 N_String_Literal)
15100 then
15101 if Present (Original_Node (N2))
15102 and then Nkind (Original_Node (N2)) = Nkind (N)
15103 then
15104 -- Operation was constant-folded. Whenever possible,
15105 -- recover semantic information from unfolded node,
15106 -- for ASIS use.
15108 Set_Associated_Node (N, Original_Node (N2));
15110 if Nkind (N) = N_Op_Concat then
15111 Set_Is_Component_Left_Opnd (N,
15112 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15113 Set_Is_Component_Right_Opnd (N,
15114 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15115 end if;
15117 Reset_Entity (N);
15119 -- Propagate the constant folding back to the template
15121 else
15122 Rewrite (N, New_Copy (N2));
15123 Set_Analyzed (N, False);
15124 end if;
15126 -- The operator was folded into an enumeration literal. Retain
15127 -- the entity to avoid spurious ambiguities if it is overloaded
15128 -- at the point of instantiation or inlining.
15130 elsif Nkind (N2) = N_Identifier
15131 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15132 then
15133 Rewrite (N, New_Copy (N2));
15134 Set_Analyzed (N, False);
15135 end if;
15136 end if;
15138 -- Complete the operands check if node has not been constant
15139 -- folded.
15141 if Nkind (N) in N_Op then
15142 Save_Entity_Descendants (N);
15143 end if;
15144 end Save_References_In_Operator;
15146 -------------------------------
15147 -- Save_References_In_Pragma --
15148 -------------------------------
15150 procedure Save_References_In_Pragma (Prag : Node_Id) is
15151 Context : Node_Id;
15152 Do_Save : Boolean := True;
15154 use Atree.Unchecked_Access;
15155 -- This code section is part of implementing an untyped tree
15156 -- traversal, so it needs direct access to node fields.
15158 begin
15159 -- Do not save global references in pragmas generated from aspects
15160 -- because the pragmas will be regenerated at instantiation time.
15162 if From_Aspect_Specification (Prag) then
15163 Do_Save := False;
15165 -- The capture of global references within contract-related source
15166 -- pragmas associated with generic packages, subprograms or their
15167 -- respective bodies must be delayed due to timing of annotation
15168 -- analysis. Global references are still captured in routine
15169 -- Save_Global_References_In_Contract.
15171 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15172 if Is_Package_Contract_Annotation (Prag) then
15173 Context := Find_Related_Package_Or_Body (Prag);
15174 else
15175 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15176 Context := Find_Related_Declaration_Or_Body (Prag);
15177 end if;
15179 -- The use of Original_Node accounts for the case when the
15180 -- related context is generic template.
15182 if Requires_Delayed_Save (Original_Node (Context)) then
15183 Do_Save := False;
15184 end if;
15185 end if;
15187 -- For all other cases, save all global references within the
15188 -- descendants, but skip the following semantic fields:
15190 -- Field1 - Next_Pragma
15191 -- Field3 - Corresponding_Aspect
15192 -- Field5 - Next_Rep_Item
15194 if Do_Save then
15195 Save_Global_Descendant (Field2 (Prag));
15196 Save_Global_Descendant (Field4 (Prag));
15197 end if;
15198 end Save_References_In_Pragma;
15200 -- Start of processing for Save_References
15202 begin
15203 if N = Empty then
15204 null;
15206 -- Aggregates
15208 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15209 Save_References_In_Aggregate (N);
15211 -- Character literals, operator symbols
15213 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15214 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15216 -- Defining identifiers
15218 elsif Nkind (N) in N_Entity then
15219 null;
15221 -- Identifiers
15223 elsif Nkind (N) = N_Identifier then
15224 Save_References_In_Identifier (N);
15226 -- Operators
15228 elsif Nkind (N) in N_Op then
15229 Save_References_In_Operator (N);
15231 -- Pragmas
15233 elsif Nkind (N) = N_Pragma then
15234 Save_References_In_Pragma (N);
15236 else
15237 Save_References_In_Descendants (N);
15238 end if;
15240 -- Save all global references found within the aspect specifications
15241 -- of the related node.
15243 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15245 -- The capture of global references within aspects associated with
15246 -- generic packages, subprograms or their bodies must be delayed
15247 -- due to timing of annotation analysis. Global references are
15248 -- still captured in routine Save_Global_References_In_Contract.
15250 if Requires_Delayed_Save (N) then
15251 null;
15253 -- Otherwise save all global references within the aspects
15255 else
15256 Save_Global_References_In_Aspects (N);
15257 end if;
15258 end if;
15259 end Save_References;
15261 -- Start of processing for Save_Global_References
15263 begin
15264 Gen_Scope := Current_Scope;
15266 -- If the generic unit is a child unit, references to entities in the
15267 -- parent are treated as local, because they will be resolved anew in
15268 -- the context of the instance of the parent.
15270 while Is_Child_Unit (Gen_Scope)
15271 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15272 loop
15273 Gen_Scope := Scope (Gen_Scope);
15274 end loop;
15276 Save_References (Templ);
15277 end Save_Global_References;
15279 ---------------------------------------
15280 -- Save_Global_References_In_Aspects --
15281 ---------------------------------------
15283 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15284 Asp : Node_Id;
15285 Expr : Node_Id;
15287 begin
15288 Asp := First (Aspect_Specifications (N));
15289 while Present (Asp) loop
15290 Expr := Expression (Asp);
15292 if Present (Expr) then
15293 Save_Global_References (Expr);
15294 end if;
15296 Next (Asp);
15297 end loop;
15298 end Save_Global_References_In_Aspects;
15300 ------------------------------------------
15301 -- Set_Copied_Sloc_For_Inherited_Pragma --
15302 ------------------------------------------
15304 procedure Set_Copied_Sloc_For_Inherited_Pragma
15305 (N : Node_Id;
15306 E : Entity_Id)
15308 begin
15309 Create_Instantiation_Source (N, E,
15310 Inlined_Body => False,
15311 Inherited_Pragma => True,
15312 Factor => S_Adjustment);
15313 end Set_Copied_Sloc_For_Inherited_Pragma;
15315 --------------------------------------
15316 -- Set_Copied_Sloc_For_Inlined_Body --
15317 --------------------------------------
15319 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15320 begin
15321 Create_Instantiation_Source (N, E,
15322 Inlined_Body => True,
15323 Inherited_Pragma => False,
15324 Factor => S_Adjustment);
15325 end Set_Copied_Sloc_For_Inlined_Body;
15327 ---------------------
15328 -- Set_Instance_Of --
15329 ---------------------
15331 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15332 begin
15333 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15334 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15335 Generic_Renamings.Increment_Last;
15336 end Set_Instance_Of;
15338 --------------------
15339 -- Set_Next_Assoc --
15340 --------------------
15342 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15343 begin
15344 Generic_Renamings.Table (E).Next_In_HTable := Next;
15345 end Set_Next_Assoc;
15347 -------------------
15348 -- Start_Generic --
15349 -------------------
15351 procedure Start_Generic is
15352 begin
15353 -- ??? More things could be factored out in this routine.
15354 -- Should probably be done at a later stage.
15356 Generic_Flags.Append (Inside_A_Generic);
15357 Inside_A_Generic := True;
15359 Expander_Mode_Save_And_Set (False);
15360 end Start_Generic;
15362 ----------------------
15363 -- Set_Instance_Env --
15364 ----------------------
15366 procedure Set_Instance_Env
15367 (Gen_Unit : Entity_Id;
15368 Act_Unit : Entity_Id)
15370 Assertion_Status : constant Boolean := Assertions_Enabled;
15371 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15372 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15374 begin
15375 -- Regardless of the current mode, predefined units are analyzed in the
15376 -- most current Ada mode, and earlier version Ada checks do not apply
15377 -- to predefined units. Nothing needs to be done for non-internal units.
15378 -- These are always analyzed in the current mode.
15380 if Is_Internal_File_Name
15381 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15382 Renamings_Included => True)
15383 then
15384 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15386 -- In Ada2012 we may want to enable assertions in an instance of a
15387 -- predefined unit, in which case we need to preserve the current
15388 -- setting for the Assertions_Enabled flag. This will become more
15389 -- critical when pre/postconditions are added to predefined units,
15390 -- as is already the case for some numeric libraries.
15392 if Ada_Version >= Ada_2012 then
15393 Assertions_Enabled := Assertion_Status;
15394 end if;
15396 -- SPARK_Mode for an instance is the one applicable at the point of
15397 -- instantiation.
15399 SPARK_Mode := Save_SPARK_Mode;
15400 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15401 end if;
15403 Current_Instantiated_Parent :=
15404 (Gen_Id => Gen_Unit,
15405 Act_Id => Act_Unit,
15406 Next_In_HTable => Assoc_Null);
15407 end Set_Instance_Env;
15409 -----------------
15410 -- Switch_View --
15411 -----------------
15413 procedure Switch_View (T : Entity_Id) is
15414 BT : constant Entity_Id := Base_Type (T);
15415 Priv_Elmt : Elmt_Id := No_Elmt;
15416 Priv_Sub : Entity_Id;
15418 begin
15419 -- T may be private but its base type may have been exchanged through
15420 -- some other occurrence, in which case there is nothing to switch
15421 -- besides T itself. Note that a private dependent subtype of a private
15422 -- type might not have been switched even if the base type has been,
15423 -- because of the last branch of Check_Private_View (see comment there).
15425 if not Is_Private_Type (BT) then
15426 Prepend_Elmt (Full_View (T), Exchanged_Views);
15427 Exchange_Declarations (T);
15428 return;
15429 end if;
15431 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15433 if Present (Full_View (BT)) then
15434 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15435 Exchange_Declarations (BT);
15436 end if;
15438 while Present (Priv_Elmt) loop
15439 Priv_Sub := (Node (Priv_Elmt));
15441 -- We avoid flipping the subtype if the Etype of its full view is
15442 -- private because this would result in a malformed subtype. This
15443 -- occurs when the Etype of the subtype full view is the full view of
15444 -- the base type (and since the base types were just switched, the
15445 -- subtype is pointing to the wrong view). This is currently the case
15446 -- for tagged record types, access types (maybe more?) and needs to
15447 -- be resolved. ???
15449 if Present (Full_View (Priv_Sub))
15450 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15451 then
15452 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15453 Exchange_Declarations (Priv_Sub);
15454 end if;
15456 Next_Elmt (Priv_Elmt);
15457 end loop;
15458 end Switch_View;
15460 -----------------
15461 -- True_Parent --
15462 -----------------
15464 function True_Parent (N : Node_Id) return Node_Id is
15465 begin
15466 if Nkind (Parent (N)) = N_Subunit then
15467 return Parent (Corresponding_Stub (Parent (N)));
15468 else
15469 return Parent (N);
15470 end if;
15471 end True_Parent;
15473 -----------------------------
15474 -- Valid_Default_Attribute --
15475 -----------------------------
15477 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15478 Attr_Id : constant Attribute_Id :=
15479 Get_Attribute_Id (Attribute_Name (Def));
15480 T : constant Entity_Id := Entity (Prefix (Def));
15481 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15482 F : Entity_Id;
15483 Num_F : Nat;
15484 OK : Boolean;
15486 begin
15487 if No (T) or else T = Any_Id then
15488 return;
15489 end if;
15491 Num_F := 0;
15492 F := First_Formal (Nam);
15493 while Present (F) loop
15494 Num_F := Num_F + 1;
15495 Next_Formal (F);
15496 end loop;
15498 case Attr_Id is
15499 when Attribute_Adjacent
15500 | Attribute_Ceiling
15501 | Attribute_Copy_Sign
15502 | Attribute_Floor
15503 | Attribute_Fraction
15504 | Attribute_Machine
15505 | Attribute_Model
15506 | Attribute_Remainder
15507 | Attribute_Rounding
15508 | Attribute_Unbiased_Rounding
15510 OK := Is_Fun
15511 and then Num_F = 1
15512 and then Is_Floating_Point_Type (T);
15514 when Attribute_Image
15515 | Attribute_Pred
15516 | Attribute_Succ
15517 | Attribute_Value
15518 | Attribute_Wide_Image
15519 | Attribute_Wide_Value
15521 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
15523 when Attribute_Max
15524 | Attribute_Min
15526 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
15528 when Attribute_Input =>
15529 OK := (Is_Fun and then Num_F = 1);
15531 when Attribute_Output
15532 | Attribute_Read
15533 | Attribute_Write
15535 OK := not Is_Fun and then Num_F = 2;
15537 when others =>
15538 OK := False;
15539 end case;
15541 if not OK then
15542 Error_Msg_N
15543 ("attribute reference has wrong profile for subprogram", Def);
15544 end if;
15545 end Valid_Default_Attribute;
15547 end Sem_Ch12;