PR middle-end/77674
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob3d940a63e8e87725e509b49106dc62ed7f88ab34
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
1463 when N_Formal_Subprogram_Declaration =>
1464 exit when Kind in N_Formal_Subprogram_Declaration
1465 and then
1466 Chars
1467 (Defining_Unit_Name (Specification (Formal))) =
1468 Chars
1469 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1471 when N_Formal_Package_Declaration =>
1472 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1473 N_Generic_Package_Declaration,
1474 N_Package_Declaration);
1476 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1478 when others =>
1480 -- Skip freeze nodes, and nodes inserted to replace
1481 -- unrecognized pragmas.
1483 exit when
1484 Kind not in N_Formal_Subprogram_Declaration
1485 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1486 N_Freeze_Entity,
1487 N_Null_Statement,
1488 N_Itype_Reference)
1489 and then Chars (Defining_Identifier (Formal)) =
1490 Chars (Defining_Identifier (Analyzed_Formal));
1491 end case;
1493 Next (Analyzed_Formal);
1494 end loop;
1495 end Set_Analyzed_Formal;
1497 -- Start of processing for Analyze_Associations
1499 begin
1500 Actuals := Generic_Associations (I_Node);
1502 if Present (Actuals) then
1504 -- Check for an Others choice, indicating a partial parameterization
1505 -- for a formal package.
1507 Actual := First (Actuals);
1508 while Present (Actual) loop
1509 if Nkind (Actual) = N_Others_Choice then
1510 Others_Present := True;
1511 Others_Choice := Actual;
1513 if Present (Next (Actual)) then
1514 Error_Msg_N ("others must be last association", Actual);
1515 end if;
1517 -- This subprogram is used both for formal packages and for
1518 -- instantiations. For the latter, associations must all be
1519 -- explicit.
1521 if Nkind (I_Node) /= N_Formal_Package_Declaration
1522 and then Comes_From_Source (I_Node)
1523 then
1524 Error_Msg_N
1525 ("others association not allowed in an instance",
1526 Actual);
1527 end if;
1529 -- In any case, nothing to do after the others association
1531 exit;
1533 elsif Box_Present (Actual)
1534 and then Comes_From_Source (I_Node)
1535 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1536 then
1537 Error_Msg_N
1538 ("box association not allowed in an instance", Actual);
1539 end if;
1541 Next (Actual);
1542 end loop;
1544 -- If named associations are present, save first named association
1545 -- (it may of course be Empty) to facilitate subsequent name search.
1547 First_Named := First (Actuals);
1548 while Present (First_Named)
1549 and then Nkind (First_Named) /= N_Others_Choice
1550 and then No (Selector_Name (First_Named))
1551 loop
1552 Num_Actuals := Num_Actuals + 1;
1553 Next (First_Named);
1554 end loop;
1555 end if;
1557 Named := First_Named;
1558 while Present (Named) loop
1559 if Nkind (Named) /= N_Others_Choice
1560 and then No (Selector_Name (Named))
1561 then
1562 Error_Msg_N ("invalid positional actual after named one", Named);
1563 Abandon_Instantiation (Named);
1564 end if;
1566 -- A named association may lack an actual parameter, if it was
1567 -- introduced for a default subprogram that turns out to be local
1568 -- to the outer instantiation. If it has a box association it must
1569 -- correspond to some formal in the generic.
1571 if Nkind (Named) /= N_Others_Choice
1572 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1573 or else Box_Present (Named))
1574 then
1575 Num_Actuals := Num_Actuals + 1;
1576 end if;
1578 Next (Named);
1579 end loop;
1581 if Present (Formals) then
1582 Formal := First_Non_Pragma (Formals);
1583 Analyzed_Formal := First_Non_Pragma (F_Copy);
1585 if Present (Actuals) then
1586 Actual := First (Actuals);
1588 -- All formals should have default values
1590 else
1591 Actual := Empty;
1592 end if;
1594 while Present (Formal) loop
1595 Set_Analyzed_Formal;
1596 Saved_Formal := Next_Non_Pragma (Formal);
1598 case Nkind (Formal) is
1599 when N_Formal_Object_Declaration =>
1600 Match :=
1601 Matching_Actual
1602 (Defining_Identifier (Formal),
1603 Defining_Identifier (Analyzed_Formal));
1605 if No (Match) and then Partial_Parameterization then
1606 Process_Default (Formal);
1608 else
1609 Append_List
1610 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1611 Assoc);
1613 -- For a defaulted in_parameter, create an entry in the
1614 -- the list of defaulted actuals, for GNATProve use. Do
1615 -- not included these defaults for an instance nested
1616 -- within a generic, because the defaults are also used
1617 -- in the analysis of the enclosing generic, and only
1618 -- defaulted subprograms are relevant there.
1620 if No (Match) and then not Inside_A_Generic then
1621 Append_To (Default_Actuals,
1622 Make_Generic_Association (Sloc (I_Node),
1623 Selector_Name =>
1624 New_Occurrence_Of
1625 (Defining_Identifier (Formal), Sloc (I_Node)),
1626 Explicit_Generic_Actual_Parameter =>
1627 New_Copy_Tree (Default_Expression (Formal))));
1628 end if;
1629 end if;
1631 -- If the object is a call to an expression function, this
1632 -- is a freezing point for it.
1634 if Is_Entity_Name (Match)
1635 and then Present (Entity (Match))
1636 and then Nkind
1637 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1638 = N_Expression_Function
1639 then
1640 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1641 end if;
1643 when N_Formal_Type_Declaration =>
1644 Match :=
1645 Matching_Actual
1646 (Defining_Identifier (Formal),
1647 Defining_Identifier (Analyzed_Formal));
1649 if No (Match) then
1650 if Partial_Parameterization then
1651 Process_Default (Formal);
1653 else
1654 Error_Msg_Sloc := Sloc (Gen_Unit);
1655 Error_Msg_NE
1656 ("missing actual&",
1657 Instantiation_Node, Defining_Identifier (Formal));
1658 Error_Msg_NE
1659 ("\in instantiation of & declared#",
1660 Instantiation_Node, Gen_Unit);
1661 Abandon_Instantiation (Instantiation_Node);
1662 end if;
1664 else
1665 Analyze (Match);
1666 Append_List
1667 (Instantiate_Type
1668 (Formal, Match, Analyzed_Formal, Assoc),
1669 Assoc);
1671 if Is_Fixed_Point_Type (Entity (Match)) then
1672 Check_Fixed_Point_Actual (Match);
1673 end if;
1675 -- An instantiation is a freeze point for the actuals,
1676 -- unless this is a rewritten formal package, or the
1677 -- formal is an Ada 2012 formal incomplete type.
1679 if Nkind (I_Node) = N_Formal_Package_Declaration
1680 or else
1681 (Ada_Version >= Ada_2012
1682 and then
1683 Ekind (Defining_Identifier (Analyzed_Formal)) =
1684 E_Incomplete_Type)
1685 then
1686 null;
1688 else
1689 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1690 end if;
1691 end if;
1693 -- A remote access-to-class-wide type is not a legal actual
1694 -- for a generic formal of an access type (E.2.2(17/2)).
1695 -- In GNAT an exception to this rule is introduced when
1696 -- the formal is marked as remote using implementation
1697 -- defined aspect/pragma Remote_Access_Type. In that case
1698 -- the actual must be remote as well.
1700 -- If the current instantiation is the construction of a
1701 -- local copy for a formal package the actuals may be
1702 -- defaulted, and there is no matching actual to check.
1704 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1705 and then
1706 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1707 N_Access_To_Object_Definition
1708 and then Present (Match)
1709 then
1710 declare
1711 Formal_Ent : constant Entity_Id :=
1712 Defining_Identifier (Analyzed_Formal);
1713 begin
1714 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1715 = Is_Remote_Types (Formal_Ent)
1716 then
1717 -- Remoteness of formal and actual match
1719 null;
1721 elsif Is_Remote_Types (Formal_Ent) then
1723 -- Remote formal, non-remote actual
1725 Error_Msg_NE
1726 ("actual for& must be remote", Match, Formal_Ent);
1728 else
1729 -- Non-remote formal, remote actual
1731 Error_Msg_NE
1732 ("actual for& may not be remote",
1733 Match, Formal_Ent);
1734 end if;
1735 end;
1736 end if;
1738 when N_Formal_Subprogram_Declaration =>
1739 Match :=
1740 Matching_Actual
1741 (Defining_Unit_Name (Specification (Formal)),
1742 Defining_Unit_Name (Specification (Analyzed_Formal)));
1744 -- If the formal subprogram has the same name as another
1745 -- formal subprogram of the generic, then a named
1746 -- association is illegal (12.3(9)). Exclude named
1747 -- associations that are generated for a nested instance.
1749 if Present (Match)
1750 and then Is_Named_Assoc
1751 and then Comes_From_Source (Found_Assoc)
1752 then
1753 Check_Overloaded_Formal_Subprogram (Formal);
1754 end if;
1756 -- If there is no corresponding actual, this may be case
1757 -- of partial parameterization, or else the formal has a
1758 -- default or a box.
1760 if No (Match) and then Partial_Parameterization then
1761 Process_Default (Formal);
1763 if Nkind (I_Node) = N_Formal_Package_Declaration then
1764 Check_Overloaded_Formal_Subprogram (Formal);
1765 end if;
1767 else
1768 Append_To (Assoc,
1769 Instantiate_Formal_Subprogram
1770 (Formal, Match, Analyzed_Formal));
1772 -- An instantiation is a freeze point for the actuals,
1773 -- unless this is a rewritten formal package.
1775 if Nkind (I_Node) /= N_Formal_Package_Declaration
1776 and then Nkind (Match) = N_Identifier
1777 and then Is_Subprogram (Entity (Match))
1779 -- The actual subprogram may rename a routine defined
1780 -- in Standard. Avoid freezing such renamings because
1781 -- subprograms coming from Standard cannot be frozen.
1783 and then
1784 not Renames_Standard_Subprogram (Entity (Match))
1786 -- If the actual subprogram comes from a different
1787 -- unit, it is already frozen, either by a body in
1788 -- that unit or by the end of the declarative part
1789 -- of the unit. This check avoids the freezing of
1790 -- subprograms defined in Standard which are used
1791 -- as generic actuals.
1793 and then In_Same_Code_Unit (Entity (Match), I_Node)
1794 and then Has_Fully_Defined_Profile (Entity (Match))
1795 then
1796 -- Mark the subprogram as having a delayed freeze
1797 -- since this may be an out-of-order action.
1799 Set_Has_Delayed_Freeze (Entity (Match));
1800 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1801 end if;
1802 end if;
1804 -- If this is a nested generic, preserve default for later
1805 -- instantiations. We do this as well for GNATProve use,
1806 -- so that the list of generic associations is complete.
1808 if No (Match) and then Box_Present (Formal) then
1809 declare
1810 Subp : constant Entity_Id :=
1811 Defining_Unit_Name (Specification (Last (Assoc)));
1813 begin
1814 Append_To (Default_Actuals,
1815 Make_Generic_Association (Sloc (I_Node),
1816 Selector_Name =>
1817 New_Occurrence_Of (Subp, Sloc (I_Node)),
1818 Explicit_Generic_Actual_Parameter =>
1819 New_Occurrence_Of (Subp, Sloc (I_Node))));
1820 end;
1821 end if;
1823 when N_Formal_Package_Declaration =>
1824 Match :=
1825 Matching_Actual
1826 (Defining_Identifier (Formal),
1827 Defining_Identifier (Original_Node (Analyzed_Formal)));
1829 if No (Match) then
1830 if Partial_Parameterization then
1831 Process_Default (Formal);
1833 else
1834 Error_Msg_Sloc := Sloc (Gen_Unit);
1835 Error_Msg_NE
1836 ("missing actual&",
1837 Instantiation_Node, Defining_Identifier (Formal));
1838 Error_Msg_NE
1839 ("\in instantiation of & declared#",
1840 Instantiation_Node, Gen_Unit);
1842 Abandon_Instantiation (Instantiation_Node);
1843 end if;
1845 else
1846 Analyze (Match);
1847 Append_List
1848 (Instantiate_Formal_Package
1849 (Formal, Match, Analyzed_Formal),
1850 Assoc);
1851 end if;
1853 -- For use type and use package appearing in the generic part,
1854 -- we have already copied them, so we can just move them where
1855 -- they belong (we mustn't recopy them since this would mess up
1856 -- the Sloc values).
1858 when N_Use_Package_Clause |
1859 N_Use_Type_Clause =>
1860 if Nkind (Original_Node (I_Node)) =
1861 N_Formal_Package_Declaration
1862 then
1863 Append (New_Copy_Tree (Formal), Assoc);
1864 else
1865 Remove (Formal);
1866 Append (Formal, Assoc);
1867 end if;
1869 when others =>
1870 raise Program_Error;
1872 end case;
1874 Formal := Saved_Formal;
1875 Next_Non_Pragma (Analyzed_Formal);
1876 end loop;
1878 if Num_Actuals > Num_Matched then
1879 Error_Msg_Sloc := Sloc (Gen_Unit);
1881 if Present (Selector_Name (Actual)) then
1882 Error_Msg_NE
1883 ("unmatched actual &", Actual, Selector_Name (Actual));
1884 Error_Msg_NE
1885 ("\in instantiation of & declared#", Actual, Gen_Unit);
1886 else
1887 Error_Msg_NE
1888 ("unmatched actual in instantiation of & declared#",
1889 Actual, Gen_Unit);
1890 end if;
1891 end if;
1893 elsif Present (Actuals) then
1894 Error_Msg_N
1895 ("too many actuals in generic instantiation", Instantiation_Node);
1896 end if;
1898 -- An instantiation freezes all generic actuals. The only exceptions
1899 -- to this are incomplete types and subprograms which are not fully
1900 -- defined at the point of instantiation.
1902 declare
1903 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1904 begin
1905 while Present (Elmt) loop
1906 Freeze_Before (I_Node, Node (Elmt));
1907 Next_Elmt (Elmt);
1908 end loop;
1909 end;
1911 -- If there are default subprograms, normalize the tree by adding
1912 -- explicit associations for them. This is required if the instance
1913 -- appears within a generic.
1915 if not Is_Empty_List (Default_Actuals) then
1916 declare
1917 Default : Node_Id;
1919 begin
1920 Default := First (Default_Actuals);
1921 while Present (Default) loop
1922 Mark_Rewrite_Insertion (Default);
1923 Next (Default);
1924 end loop;
1926 if No (Actuals) then
1927 Set_Generic_Associations (I_Node, Default_Actuals);
1928 else
1929 Append_List_To (Actuals, Default_Actuals);
1930 end if;
1931 end;
1932 end if;
1934 -- If this is a formal package, normalize the parameter list by adding
1935 -- explicit box associations for the formals that are covered by an
1936 -- Others_Choice.
1938 if not Is_Empty_List (Default_Formals) then
1939 Append_List (Default_Formals, Formals);
1940 end if;
1942 return Assoc;
1943 end Analyze_Associations;
1945 -------------------------------
1946 -- Analyze_Formal_Array_Type --
1947 -------------------------------
1949 procedure Analyze_Formal_Array_Type
1950 (T : in out Entity_Id;
1951 Def : Node_Id)
1953 DSS : Node_Id;
1955 begin
1956 -- Treated like a non-generic array declaration, with additional
1957 -- semantic checks.
1959 Enter_Name (T);
1961 if Nkind (Def) = N_Constrained_Array_Definition then
1962 DSS := First (Discrete_Subtype_Definitions (Def));
1963 while Present (DSS) loop
1964 if Nkind_In (DSS, N_Subtype_Indication,
1965 N_Range,
1966 N_Attribute_Reference)
1967 then
1968 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1969 end if;
1971 Next (DSS);
1972 end loop;
1973 end if;
1975 Array_Type_Declaration (T, Def);
1976 Set_Is_Generic_Type (Base_Type (T));
1978 if Ekind (Component_Type (T)) = E_Incomplete_Type
1979 and then No (Full_View (Component_Type (T)))
1980 then
1981 Error_Msg_N ("premature usage of incomplete type", Def);
1983 -- Check that range constraint is not allowed on the component type
1984 -- of a generic formal array type (AARM 12.5.3(3))
1986 elsif Is_Internal (Component_Type (T))
1987 and then Present (Subtype_Indication (Component_Definition (Def)))
1988 and then Nkind (Original_Node
1989 (Subtype_Indication (Component_Definition (Def)))) =
1990 N_Subtype_Indication
1991 then
1992 Error_Msg_N
1993 ("in a formal, a subtype indication can only be "
1994 & "a subtype mark (RM 12.5.3(3))",
1995 Subtype_Indication (Component_Definition (Def)));
1996 end if;
1998 end Analyze_Formal_Array_Type;
2000 ---------------------------------------------
2001 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2002 ---------------------------------------------
2004 -- As for other generic types, we create a valid type representation with
2005 -- legal but arbitrary attributes, whose values are never considered
2006 -- static. For all scalar types we introduce an anonymous base type, with
2007 -- the same attributes. We choose the corresponding integer type to be
2008 -- Standard_Integer.
2009 -- Here and in other similar routines, the Sloc of the generated internal
2010 -- type must be the same as the sloc of the defining identifier of the
2011 -- formal type declaration, to provide proper source navigation.
2013 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2014 (T : Entity_Id;
2015 Def : Node_Id)
2017 Loc : constant Source_Ptr := Sloc (Def);
2019 Base : constant Entity_Id :=
2020 New_Internal_Entity
2021 (E_Decimal_Fixed_Point_Type,
2022 Current_Scope,
2023 Sloc (Defining_Identifier (Parent (Def))), 'G');
2025 Int_Base : constant Entity_Id := Standard_Integer;
2026 Delta_Val : constant Ureal := Ureal_1;
2027 Digs_Val : constant Uint := Uint_6;
2029 function Make_Dummy_Bound return Node_Id;
2030 -- Return a properly typed universal real literal to use as a bound
2032 ----------------------
2033 -- Make_Dummy_Bound --
2034 ----------------------
2036 function Make_Dummy_Bound return Node_Id is
2037 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2038 begin
2039 Set_Etype (Bound, Universal_Real);
2040 return Bound;
2041 end Make_Dummy_Bound;
2043 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2045 begin
2046 Enter_Name (T);
2048 Set_Etype (Base, Base);
2049 Set_Size_Info (Base, Int_Base);
2050 Set_RM_Size (Base, RM_Size (Int_Base));
2051 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2052 Set_Digits_Value (Base, Digs_Val);
2053 Set_Delta_Value (Base, Delta_Val);
2054 Set_Small_Value (Base, Delta_Val);
2055 Set_Scalar_Range (Base,
2056 Make_Range (Loc,
2057 Low_Bound => Make_Dummy_Bound,
2058 High_Bound => Make_Dummy_Bound));
2060 Set_Is_Generic_Type (Base);
2061 Set_Parent (Base, Parent (Def));
2063 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2064 Set_Etype (T, Base);
2065 Set_Size_Info (T, Int_Base);
2066 Set_RM_Size (T, RM_Size (Int_Base));
2067 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2068 Set_Digits_Value (T, Digs_Val);
2069 Set_Delta_Value (T, Delta_Val);
2070 Set_Small_Value (T, Delta_Val);
2071 Set_Scalar_Range (T, Scalar_Range (Base));
2072 Set_Is_Constrained (T);
2074 Check_Restriction (No_Fixed_Point, Def);
2075 end Analyze_Formal_Decimal_Fixed_Point_Type;
2077 -------------------------------------------
2078 -- Analyze_Formal_Derived_Interface_Type --
2079 -------------------------------------------
2081 procedure Analyze_Formal_Derived_Interface_Type
2082 (N : Node_Id;
2083 T : Entity_Id;
2084 Def : Node_Id)
2086 Loc : constant Source_Ptr := Sloc (Def);
2088 begin
2089 -- Rewrite as a type declaration of a derived type. This ensures that
2090 -- the interface list and primitive operations are properly captured.
2092 Rewrite (N,
2093 Make_Full_Type_Declaration (Loc,
2094 Defining_Identifier => T,
2095 Type_Definition => Def));
2096 Analyze (N);
2097 Set_Is_Generic_Type (T);
2098 end Analyze_Formal_Derived_Interface_Type;
2100 ---------------------------------
2101 -- Analyze_Formal_Derived_Type --
2102 ---------------------------------
2104 procedure Analyze_Formal_Derived_Type
2105 (N : Node_Id;
2106 T : Entity_Id;
2107 Def : Node_Id)
2109 Loc : constant Source_Ptr := Sloc (Def);
2110 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2111 New_N : Node_Id;
2113 begin
2114 Set_Is_Generic_Type (T);
2116 if Private_Present (Def) then
2117 New_N :=
2118 Make_Private_Extension_Declaration (Loc,
2119 Defining_Identifier => T,
2120 Discriminant_Specifications => Discriminant_Specifications (N),
2121 Unknown_Discriminants_Present => Unk_Disc,
2122 Subtype_Indication => Subtype_Mark (Def),
2123 Interface_List => Interface_List (Def));
2125 Set_Abstract_Present (New_N, Abstract_Present (Def));
2126 Set_Limited_Present (New_N, Limited_Present (Def));
2127 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2129 else
2130 New_N :=
2131 Make_Full_Type_Declaration (Loc,
2132 Defining_Identifier => T,
2133 Discriminant_Specifications =>
2134 Discriminant_Specifications (Parent (T)),
2135 Type_Definition =>
2136 Make_Derived_Type_Definition (Loc,
2137 Subtype_Indication => Subtype_Mark (Def)));
2139 Set_Abstract_Present
2140 (Type_Definition (New_N), Abstract_Present (Def));
2141 Set_Limited_Present
2142 (Type_Definition (New_N), Limited_Present (Def));
2143 end if;
2145 Rewrite (N, New_N);
2146 Analyze (N);
2148 if Unk_Disc then
2149 if not Is_Composite_Type (T) then
2150 Error_Msg_N
2151 ("unknown discriminants not allowed for elementary types", N);
2152 else
2153 Set_Has_Unknown_Discriminants (T);
2154 Set_Is_Constrained (T, False);
2155 end if;
2156 end if;
2158 -- If the parent type has a known size, so does the formal, which makes
2159 -- legal representation clauses that involve the formal.
2161 Set_Size_Known_At_Compile_Time
2162 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2163 end Analyze_Formal_Derived_Type;
2165 ----------------------------------
2166 -- Analyze_Formal_Discrete_Type --
2167 ----------------------------------
2169 -- The operations defined for a discrete types are those of an enumeration
2170 -- type. The size is set to an arbitrary value, for use in analyzing the
2171 -- generic unit.
2173 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2174 Loc : constant Source_Ptr := Sloc (Def);
2175 Lo : Node_Id;
2176 Hi : Node_Id;
2178 Base : constant Entity_Id :=
2179 New_Internal_Entity
2180 (E_Floating_Point_Type, Current_Scope,
2181 Sloc (Defining_Identifier (Parent (Def))), 'G');
2183 begin
2184 Enter_Name (T);
2185 Set_Ekind (T, E_Enumeration_Subtype);
2186 Set_Etype (T, Base);
2187 Init_Size (T, 8);
2188 Init_Alignment (T);
2189 Set_Is_Generic_Type (T);
2190 Set_Is_Constrained (T);
2192 -- For semantic analysis, the bounds of the type must be set to some
2193 -- non-static value. The simplest is to create attribute nodes for those
2194 -- bounds, that refer to the type itself. These bounds are never
2195 -- analyzed but serve as place-holders.
2197 Lo :=
2198 Make_Attribute_Reference (Loc,
2199 Attribute_Name => Name_First,
2200 Prefix => New_Occurrence_Of (T, Loc));
2201 Set_Etype (Lo, T);
2203 Hi :=
2204 Make_Attribute_Reference (Loc,
2205 Attribute_Name => Name_Last,
2206 Prefix => New_Occurrence_Of (T, Loc));
2207 Set_Etype (Hi, T);
2209 Set_Scalar_Range (T,
2210 Make_Range (Loc,
2211 Low_Bound => Lo,
2212 High_Bound => Hi));
2214 Set_Ekind (Base, E_Enumeration_Type);
2215 Set_Etype (Base, Base);
2216 Init_Size (Base, 8);
2217 Init_Alignment (Base);
2218 Set_Is_Generic_Type (Base);
2219 Set_Scalar_Range (Base, Scalar_Range (T));
2220 Set_Parent (Base, Parent (Def));
2221 end Analyze_Formal_Discrete_Type;
2223 ----------------------------------
2224 -- Analyze_Formal_Floating_Type --
2225 ---------------------------------
2227 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2228 Base : constant Entity_Id :=
2229 New_Internal_Entity
2230 (E_Floating_Point_Type, Current_Scope,
2231 Sloc (Defining_Identifier (Parent (Def))), 'G');
2233 begin
2234 -- The various semantic attributes are taken from the predefined type
2235 -- Float, just so that all of them are initialized. Their values are
2236 -- never used because no constant folding or expansion takes place in
2237 -- the generic itself.
2239 Enter_Name (T);
2240 Set_Ekind (T, E_Floating_Point_Subtype);
2241 Set_Etype (T, Base);
2242 Set_Size_Info (T, (Standard_Float));
2243 Set_RM_Size (T, RM_Size (Standard_Float));
2244 Set_Digits_Value (T, Digits_Value (Standard_Float));
2245 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2246 Set_Is_Constrained (T);
2248 Set_Is_Generic_Type (Base);
2249 Set_Etype (Base, Base);
2250 Set_Size_Info (Base, (Standard_Float));
2251 Set_RM_Size (Base, RM_Size (Standard_Float));
2252 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2253 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2254 Set_Parent (Base, Parent (Def));
2256 Check_Restriction (No_Floating_Point, Def);
2257 end Analyze_Formal_Floating_Type;
2259 -----------------------------------
2260 -- Analyze_Formal_Interface_Type;--
2261 -----------------------------------
2263 procedure Analyze_Formal_Interface_Type
2264 (N : Node_Id;
2265 T : Entity_Id;
2266 Def : Node_Id)
2268 Loc : constant Source_Ptr := Sloc (N);
2269 New_N : Node_Id;
2271 begin
2272 New_N :=
2273 Make_Full_Type_Declaration (Loc,
2274 Defining_Identifier => T,
2275 Type_Definition => Def);
2277 Rewrite (N, New_N);
2278 Analyze (N);
2279 Set_Is_Generic_Type (T);
2280 end Analyze_Formal_Interface_Type;
2282 ---------------------------------
2283 -- Analyze_Formal_Modular_Type --
2284 ---------------------------------
2286 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2287 begin
2288 -- Apart from their entity kind, generic modular types are treated like
2289 -- signed integer types, and have the same attributes.
2291 Analyze_Formal_Signed_Integer_Type (T, Def);
2292 Set_Ekind (T, E_Modular_Integer_Subtype);
2293 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2295 end Analyze_Formal_Modular_Type;
2297 ---------------------------------------
2298 -- Analyze_Formal_Object_Declaration --
2299 ---------------------------------------
2301 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2302 E : constant Node_Id := Default_Expression (N);
2303 Id : constant Node_Id := Defining_Identifier (N);
2304 K : Entity_Kind;
2305 T : Node_Id;
2307 begin
2308 Enter_Name (Id);
2310 -- Determine the mode of the formal object
2312 if Out_Present (N) then
2313 K := E_Generic_In_Out_Parameter;
2315 if not In_Present (N) then
2316 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2317 end if;
2319 else
2320 K := E_Generic_In_Parameter;
2321 end if;
2323 if Present (Subtype_Mark (N)) then
2324 Find_Type (Subtype_Mark (N));
2325 T := Entity (Subtype_Mark (N));
2327 -- Verify that there is no redundant null exclusion
2329 if Null_Exclusion_Present (N) then
2330 if not Is_Access_Type (T) then
2331 Error_Msg_N
2332 ("null exclusion can only apply to an access type", N);
2334 elsif Can_Never_Be_Null (T) then
2335 Error_Msg_NE
2336 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2337 end if;
2338 end if;
2340 -- Ada 2005 (AI-423): Formal object with an access definition
2342 else
2343 Check_Access_Definition (N);
2344 T := Access_Definition
2345 (Related_Nod => N,
2346 N => Access_Definition (N));
2347 end if;
2349 if Ekind (T) = E_Incomplete_Type then
2350 declare
2351 Error_Node : Node_Id;
2353 begin
2354 if Present (Subtype_Mark (N)) then
2355 Error_Node := Subtype_Mark (N);
2356 else
2357 Check_Access_Definition (N);
2358 Error_Node := Access_Definition (N);
2359 end if;
2361 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2362 end;
2363 end if;
2365 if K = E_Generic_In_Parameter then
2367 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2369 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2370 Error_Msg_N
2371 ("generic formal of mode IN must not be of limited type", N);
2372 Explain_Limited_Type (T, N);
2373 end if;
2375 if Is_Abstract_Type (T) then
2376 Error_Msg_N
2377 ("generic formal of mode IN must not be of abstract type", N);
2378 end if;
2380 if Present (E) then
2381 Preanalyze_Spec_Expression (E, T);
2383 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2384 Error_Msg_N
2385 ("initialization not allowed for limited types", E);
2386 Explain_Limited_Type (T, E);
2387 end if;
2388 end if;
2390 Set_Ekind (Id, K);
2391 Set_Etype (Id, T);
2393 -- Case of generic IN OUT parameter
2395 else
2396 -- If the formal has an unconstrained type, construct its actual
2397 -- subtype, as is done for subprogram formals. In this fashion, all
2398 -- its uses can refer to specific bounds.
2400 Set_Ekind (Id, K);
2401 Set_Etype (Id, T);
2403 if (Is_Array_Type (T) and then not Is_Constrained (T))
2404 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2405 then
2406 declare
2407 Non_Freezing_Ref : constant Node_Id :=
2408 New_Occurrence_Of (Id, Sloc (Id));
2409 Decl : Node_Id;
2411 begin
2412 -- Make sure the actual subtype doesn't generate bogus freezing
2414 Set_Must_Not_Freeze (Non_Freezing_Ref);
2415 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2416 Insert_Before_And_Analyze (N, Decl);
2417 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2418 end;
2419 else
2420 Set_Actual_Subtype (Id, T);
2421 end if;
2423 if Present (E) then
2424 Error_Msg_N
2425 ("initialization not allowed for `IN OUT` formals", N);
2426 end if;
2427 end if;
2429 if Has_Aspects (N) then
2430 Analyze_Aspect_Specifications (N, Id);
2431 end if;
2432 end Analyze_Formal_Object_Declaration;
2434 ----------------------------------------------
2435 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2436 ----------------------------------------------
2438 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2439 (T : Entity_Id;
2440 Def : Node_Id)
2442 Loc : constant Source_Ptr := Sloc (Def);
2443 Base : constant Entity_Id :=
2444 New_Internal_Entity
2445 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2446 Sloc (Defining_Identifier (Parent (Def))), 'G');
2448 begin
2449 -- The semantic attributes are set for completeness only, their values
2450 -- will never be used, since all properties of the type are non-static.
2452 Enter_Name (T);
2453 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2454 Set_Etype (T, Base);
2455 Set_Size_Info (T, Standard_Integer);
2456 Set_RM_Size (T, RM_Size (Standard_Integer));
2457 Set_Small_Value (T, Ureal_1);
2458 Set_Delta_Value (T, Ureal_1);
2459 Set_Scalar_Range (T,
2460 Make_Range (Loc,
2461 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2462 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2463 Set_Is_Constrained (T);
2465 Set_Is_Generic_Type (Base);
2466 Set_Etype (Base, Base);
2467 Set_Size_Info (Base, Standard_Integer);
2468 Set_RM_Size (Base, RM_Size (Standard_Integer));
2469 Set_Small_Value (Base, Ureal_1);
2470 Set_Delta_Value (Base, Ureal_1);
2471 Set_Scalar_Range (Base, Scalar_Range (T));
2472 Set_Parent (Base, Parent (Def));
2474 Check_Restriction (No_Fixed_Point, Def);
2475 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2477 ----------------------------------------
2478 -- Analyze_Formal_Package_Declaration --
2479 ----------------------------------------
2481 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2482 Gen_Id : constant Node_Id := Name (N);
2483 Loc : constant Source_Ptr := Sloc (N);
2484 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2485 Formal : Entity_Id;
2486 Gen_Decl : Node_Id;
2487 Gen_Unit : Entity_Id;
2488 Renaming : Node_Id;
2490 Vis_Prims_List : Elist_Id := No_Elist;
2491 -- List of primitives made temporarily visible in the instantiation
2492 -- to match the visibility of the formal type.
2494 function Build_Local_Package return Node_Id;
2495 -- The formal package is rewritten so that its parameters are replaced
2496 -- with corresponding declarations. For parameters with bona fide
2497 -- associations these declarations are created by Analyze_Associations
2498 -- as for a regular instantiation. For boxed parameters, we preserve
2499 -- the formal declarations and analyze them, in order to introduce
2500 -- entities of the right kind in the environment of the formal.
2502 -------------------------
2503 -- Build_Local_Package --
2504 -------------------------
2506 function Build_Local_Package return Node_Id is
2507 Decls : List_Id;
2508 Pack_Decl : Node_Id;
2510 begin
2511 -- Within the formal, the name of the generic package is a renaming
2512 -- of the formal (as for a regular instantiation).
2514 Pack_Decl :=
2515 Make_Package_Declaration (Loc,
2516 Specification =>
2517 Copy_Generic_Node
2518 (Specification (Original_Node (Gen_Decl)),
2519 Empty, Instantiating => True));
2521 Renaming :=
2522 Make_Package_Renaming_Declaration (Loc,
2523 Defining_Unit_Name =>
2524 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2525 Name => New_Occurrence_Of (Formal, Loc));
2527 if Nkind (Gen_Id) = N_Identifier
2528 and then Chars (Gen_Id) = Chars (Pack_Id)
2529 then
2530 Error_Msg_NE
2531 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2532 end if;
2534 -- If the formal is declared with a box, or with an others choice,
2535 -- create corresponding declarations for all entities in the formal
2536 -- part, so that names with the proper types are available in the
2537 -- specification of the formal package.
2539 -- On the other hand, if there are no associations, then all the
2540 -- formals must have defaults, and this will be checked by the
2541 -- call to Analyze_Associations.
2543 if Box_Present (N)
2544 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2545 then
2546 declare
2547 Formal_Decl : Node_Id;
2549 begin
2550 -- TBA : for a formal package, need to recurse ???
2552 Decls := New_List;
2553 Formal_Decl :=
2554 First
2555 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2556 while Present (Formal_Decl) loop
2557 Append_To
2558 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2559 Next (Formal_Decl);
2560 end loop;
2561 end;
2563 -- If generic associations are present, use Analyze_Associations to
2564 -- create the proper renaming declarations.
2566 else
2567 declare
2568 Act_Tree : constant Node_Id :=
2569 Copy_Generic_Node
2570 (Original_Node (Gen_Decl), Empty,
2571 Instantiating => True);
2573 begin
2574 Generic_Renamings.Set_Last (0);
2575 Generic_Renamings_HTable.Reset;
2576 Instantiation_Node := N;
2578 Decls :=
2579 Analyze_Associations
2580 (I_Node => Original_Node (N),
2581 Formals => Generic_Formal_Declarations (Act_Tree),
2582 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2584 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2585 end;
2586 end if;
2588 Append (Renaming, To => Decls);
2590 -- Add generated declarations ahead of local declarations in
2591 -- the package.
2593 if No (Visible_Declarations (Specification (Pack_Decl))) then
2594 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2595 else
2596 Insert_List_Before
2597 (First (Visible_Declarations (Specification (Pack_Decl))),
2598 Decls);
2599 end if;
2601 return Pack_Decl;
2602 end Build_Local_Package;
2604 -- Local variables
2606 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2607 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2609 Associations : Boolean := True;
2610 New_N : Node_Id;
2611 Parent_Installed : Boolean := False;
2612 Parent_Instance : Entity_Id;
2613 Renaming_In_Par : Entity_Id;
2615 -- Start of processing for Analyze_Formal_Package_Declaration
2617 begin
2618 Check_Text_IO_Special_Unit (Gen_Id);
2620 Init_Env;
2621 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2622 Gen_Unit := Entity (Gen_Id);
2624 -- Check for a formal package that is a package renaming
2626 if Present (Renamed_Object (Gen_Unit)) then
2628 -- Indicate that unit is used, before replacing it with renamed
2629 -- entity for use below.
2631 if In_Extended_Main_Source_Unit (N) then
2632 Set_Is_Instantiated (Gen_Unit);
2633 Generate_Reference (Gen_Unit, N);
2634 end if;
2636 Gen_Unit := Renamed_Object (Gen_Unit);
2637 end if;
2639 if Ekind (Gen_Unit) /= E_Generic_Package then
2640 Error_Msg_N ("expect generic package name", Gen_Id);
2641 Restore_Env;
2642 goto Leave;
2644 elsif Gen_Unit = Current_Scope then
2645 Error_Msg_N
2646 ("generic package cannot be used as a formal package of itself",
2647 Gen_Id);
2648 Restore_Env;
2649 goto Leave;
2651 elsif In_Open_Scopes (Gen_Unit) then
2652 if Is_Compilation_Unit (Gen_Unit)
2653 and then Is_Child_Unit (Current_Scope)
2654 then
2655 -- Special-case the error when the formal is a parent, and
2656 -- continue analysis to minimize cascaded errors.
2658 Error_Msg_N
2659 ("generic parent cannot be used as formal package "
2660 & "of a child unit", Gen_Id);
2662 else
2663 Error_Msg_N
2664 ("generic package cannot be used as a formal package "
2665 & "within itself", Gen_Id);
2666 Restore_Env;
2667 goto Leave;
2668 end if;
2669 end if;
2671 -- Check that name of formal package does not hide name of generic,
2672 -- or its leading prefix. This check must be done separately because
2673 -- the name of the generic has already been analyzed.
2675 declare
2676 Gen_Name : Entity_Id;
2678 begin
2679 Gen_Name := Gen_Id;
2680 while Nkind (Gen_Name) = N_Expanded_Name loop
2681 Gen_Name := Prefix (Gen_Name);
2682 end loop;
2684 if Chars (Gen_Name) = Chars (Pack_Id) then
2685 Error_Msg_NE
2686 ("& is hidden within declaration of formal package",
2687 Gen_Id, Gen_Name);
2688 end if;
2689 end;
2691 if Box_Present (N)
2692 or else No (Generic_Associations (N))
2693 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2694 then
2695 Associations := False;
2696 end if;
2698 -- If there are no generic associations, the generic parameters appear
2699 -- as local entities and are instantiated like them. We copy the generic
2700 -- package declaration as if it were an instantiation, and analyze it
2701 -- like a regular package, except that we treat the formals as
2702 -- additional visible components.
2704 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2706 if In_Extended_Main_Source_Unit (N) then
2707 Set_Is_Instantiated (Gen_Unit);
2708 Generate_Reference (Gen_Unit, N);
2709 end if;
2711 Formal := New_Copy (Pack_Id);
2712 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2714 -- Make local generic without formals. The formals will be replaced with
2715 -- internal declarations.
2717 begin
2718 New_N := Build_Local_Package;
2720 -- If there are errors in the parameter list, Analyze_Associations
2721 -- raises Instantiation_Error. Patch the declaration to prevent further
2722 -- exception propagation.
2724 exception
2725 when Instantiation_Error =>
2726 Enter_Name (Formal);
2727 Set_Ekind (Formal, E_Variable);
2728 Set_Etype (Formal, Any_Type);
2729 Restore_Hidden_Primitives (Vis_Prims_List);
2731 if Parent_Installed then
2732 Remove_Parent;
2733 end if;
2735 goto Leave;
2736 end;
2738 Rewrite (N, New_N);
2739 Set_Defining_Unit_Name (Specification (New_N), Formal);
2740 Set_Generic_Parent (Specification (N), Gen_Unit);
2741 Set_Instance_Env (Gen_Unit, Formal);
2742 Set_Is_Generic_Instance (Formal);
2744 Enter_Name (Formal);
2745 Set_Ekind (Formal, E_Package);
2746 Set_Etype (Formal, Standard_Void_Type);
2747 Set_Inner_Instances (Formal, New_Elmt_List);
2748 Push_Scope (Formal);
2750 -- Manually set the SPARK_Mode from the context because the package
2751 -- declaration is never analyzed.
2753 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2754 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2755 Set_SPARK_Pragma_Inherited (Formal);
2756 Set_SPARK_Aux_Pragma_Inherited (Formal);
2758 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2760 -- Similarly, we have to make the name of the formal visible in the
2761 -- parent instance, to resolve properly fully qualified names that
2762 -- may appear in the generic unit. The parent instance has been
2763 -- placed on the scope stack ahead of the current scope.
2765 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2767 Renaming_In_Par :=
2768 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2769 Set_Ekind (Renaming_In_Par, E_Package);
2770 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2771 Set_Scope (Renaming_In_Par, Parent_Instance);
2772 Set_Parent (Renaming_In_Par, Parent (Formal));
2773 Set_Renamed_Object (Renaming_In_Par, Formal);
2774 Append_Entity (Renaming_In_Par, Parent_Instance);
2775 end if;
2777 -- A formal package declaration behaves as a package instantiation with
2778 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2779 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2780 -- all SPARK_Mode pragmas within the generic_package_name.
2782 if SPARK_Mode /= On then
2783 Ignore_Pragma_SPARK_Mode := True;
2784 end if;
2786 Analyze (Specification (N));
2788 -- The formals for which associations are provided are not visible
2789 -- outside of the formal package. The others are still declared by a
2790 -- formal parameter declaration.
2792 -- If there are no associations, the only local entity to hide is the
2793 -- generated package renaming itself.
2795 declare
2796 E : Entity_Id;
2798 begin
2799 E := First_Entity (Formal);
2800 while Present (E) loop
2801 if Associations and then not Is_Generic_Formal (E) then
2802 Set_Is_Hidden (E);
2803 end if;
2805 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2806 Set_Is_Hidden (E);
2807 exit;
2808 end if;
2810 Next_Entity (E);
2811 end loop;
2812 end;
2814 End_Package_Scope (Formal);
2815 Restore_Hidden_Primitives (Vis_Prims_List);
2817 if Parent_Installed then
2818 Remove_Parent;
2819 end if;
2821 Restore_Env;
2823 -- Inside the generic unit, the formal package is a regular package, but
2824 -- no body is needed for it. Note that after instantiation, the defining
2825 -- unit name we need is in the new tree and not in the original (see
2826 -- Package_Instantiation). A generic formal package is an instance, and
2827 -- can be used as an actual for an inner instance.
2829 Set_Has_Completion (Formal, True);
2831 -- Add semantic information to the original defining identifier for ASIS
2832 -- use.
2834 Set_Ekind (Pack_Id, E_Package);
2835 Set_Etype (Pack_Id, Standard_Void_Type);
2836 Set_Scope (Pack_Id, Scope (Formal));
2837 Set_Has_Completion (Pack_Id, True);
2839 <<Leave>>
2840 if Has_Aspects (N) then
2841 Analyze_Aspect_Specifications (N, Pack_Id);
2842 end if;
2844 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2845 end Analyze_Formal_Package_Declaration;
2847 ---------------------------------
2848 -- Analyze_Formal_Private_Type --
2849 ---------------------------------
2851 procedure Analyze_Formal_Private_Type
2852 (N : Node_Id;
2853 T : Entity_Id;
2854 Def : Node_Id)
2856 begin
2857 New_Private_Type (N, T, Def);
2859 -- Set the size to an arbitrary but legal value
2861 Set_Size_Info (T, Standard_Integer);
2862 Set_RM_Size (T, RM_Size (Standard_Integer));
2863 end Analyze_Formal_Private_Type;
2865 ------------------------------------
2866 -- Analyze_Formal_Incomplete_Type --
2867 ------------------------------------
2869 procedure Analyze_Formal_Incomplete_Type
2870 (T : Entity_Id;
2871 Def : Node_Id)
2873 begin
2874 Enter_Name (T);
2875 Set_Ekind (T, E_Incomplete_Type);
2876 Set_Etype (T, T);
2877 Set_Private_Dependents (T, New_Elmt_List);
2879 if Tagged_Present (Def) then
2880 Set_Is_Tagged_Type (T);
2881 Make_Class_Wide_Type (T);
2882 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2883 end if;
2884 end Analyze_Formal_Incomplete_Type;
2886 ----------------------------------------
2887 -- Analyze_Formal_Signed_Integer_Type --
2888 ----------------------------------------
2890 procedure Analyze_Formal_Signed_Integer_Type
2891 (T : Entity_Id;
2892 Def : Node_Id)
2894 Base : constant Entity_Id :=
2895 New_Internal_Entity
2896 (E_Signed_Integer_Type,
2897 Current_Scope,
2898 Sloc (Defining_Identifier (Parent (Def))), 'G');
2900 begin
2901 Enter_Name (T);
2903 Set_Ekind (T, E_Signed_Integer_Subtype);
2904 Set_Etype (T, Base);
2905 Set_Size_Info (T, Standard_Integer);
2906 Set_RM_Size (T, RM_Size (Standard_Integer));
2907 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2908 Set_Is_Constrained (T);
2910 Set_Is_Generic_Type (Base);
2911 Set_Size_Info (Base, Standard_Integer);
2912 Set_RM_Size (Base, RM_Size (Standard_Integer));
2913 Set_Etype (Base, Base);
2914 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2915 Set_Parent (Base, Parent (Def));
2916 end Analyze_Formal_Signed_Integer_Type;
2918 -------------------------------------------
2919 -- Analyze_Formal_Subprogram_Declaration --
2920 -------------------------------------------
2922 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2923 Spec : constant Node_Id := Specification (N);
2924 Def : constant Node_Id := Default_Name (N);
2925 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2926 Subp : Entity_Id;
2928 begin
2929 if Nam = Error then
2930 return;
2931 end if;
2933 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2934 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2935 goto Leave;
2936 end if;
2938 Analyze_Subprogram_Declaration (N);
2939 Set_Is_Formal_Subprogram (Nam);
2940 Set_Has_Completion (Nam);
2942 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2943 Set_Is_Abstract_Subprogram (Nam);
2945 Set_Is_Dispatching_Operation (Nam);
2947 -- A formal abstract procedure cannot have a null default
2948 -- (RM 12.6(4.1/2)).
2950 if Nkind (Spec) = N_Procedure_Specification
2951 and then Null_Present (Spec)
2952 then
2953 Error_Msg_N
2954 ("a formal abstract subprogram cannot default to null", Spec);
2955 end if;
2957 declare
2958 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2959 begin
2960 if No (Ctrl_Type) then
2961 Error_Msg_N
2962 ("abstract formal subprogram must have a controlling type",
2965 elsif Ada_Version >= Ada_2012
2966 and then Is_Incomplete_Type (Ctrl_Type)
2967 then
2968 Error_Msg_NE
2969 ("controlling type of abstract formal subprogram cannot "
2970 & "be incomplete type", N, Ctrl_Type);
2972 else
2973 Check_Controlling_Formals (Ctrl_Type, Nam);
2974 end if;
2975 end;
2976 end if;
2978 -- Default name is resolved at the point of instantiation
2980 if Box_Present (N) then
2981 null;
2983 -- Else default is bound at the point of generic declaration
2985 elsif Present (Def) then
2986 if Nkind (Def) = N_Operator_Symbol then
2987 Find_Direct_Name (Def);
2989 elsif Nkind (Def) /= N_Attribute_Reference then
2990 Analyze (Def);
2992 else
2993 -- For an attribute reference, analyze the prefix and verify
2994 -- that it has the proper profile for the subprogram.
2996 Analyze (Prefix (Def));
2997 Valid_Default_Attribute (Nam, Def);
2998 goto Leave;
2999 end if;
3001 -- Default name may be overloaded, in which case the interpretation
3002 -- with the correct profile must be selected, as for a renaming.
3003 -- If the definition is an indexed component, it must denote a
3004 -- member of an entry family. If it is a selected component, it
3005 -- can be a protected operation.
3007 if Etype (Def) = Any_Type then
3008 goto Leave;
3010 elsif Nkind (Def) = N_Selected_Component then
3011 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3012 Error_Msg_N ("expect valid subprogram name as default", Def);
3013 end if;
3015 elsif Nkind (Def) = N_Indexed_Component then
3016 if Is_Entity_Name (Prefix (Def)) then
3017 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3018 Error_Msg_N ("expect valid subprogram name as default", Def);
3019 end if;
3021 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3022 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3023 E_Entry_Family
3024 then
3025 Error_Msg_N ("expect valid subprogram name as default", Def);
3026 end if;
3028 else
3029 Error_Msg_N ("expect valid subprogram name as default", Def);
3030 goto Leave;
3031 end if;
3033 elsif Nkind (Def) = N_Character_Literal then
3035 -- Needs some type checks: subprogram should be parameterless???
3037 Resolve (Def, (Etype (Nam)));
3039 elsif not Is_Entity_Name (Def)
3040 or else not Is_Overloadable (Entity (Def))
3041 then
3042 Error_Msg_N ("expect valid subprogram name as default", Def);
3043 goto Leave;
3045 elsif not Is_Overloaded (Def) then
3046 Subp := Entity (Def);
3048 if Subp = Nam then
3049 Error_Msg_N ("premature usage of formal subprogram", Def);
3051 elsif not Entity_Matches_Spec (Subp, Nam) then
3052 Error_Msg_N ("no visible entity matches specification", Def);
3053 end if;
3055 -- More than one interpretation, so disambiguate as for a renaming
3057 else
3058 declare
3059 I : Interp_Index;
3060 I1 : Interp_Index := 0;
3061 It : Interp;
3062 It1 : Interp;
3064 begin
3065 Subp := Any_Id;
3066 Get_First_Interp (Def, I, It);
3067 while Present (It.Nam) loop
3068 if Entity_Matches_Spec (It.Nam, Nam) then
3069 if Subp /= Any_Id then
3070 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3072 if It1 = No_Interp then
3073 Error_Msg_N ("ambiguous default subprogram", Def);
3074 else
3075 Subp := It1.Nam;
3076 end if;
3078 exit;
3080 else
3081 I1 := I;
3082 Subp := It.Nam;
3083 end if;
3084 end if;
3086 Get_Next_Interp (I, It);
3087 end loop;
3088 end;
3090 if Subp /= Any_Id then
3092 -- Subprogram found, generate reference to it
3094 Set_Entity (Def, Subp);
3095 Generate_Reference (Subp, Def);
3097 if Subp = Nam then
3098 Error_Msg_N ("premature usage of formal subprogram", Def);
3100 elsif Ekind (Subp) /= E_Operator then
3101 Check_Mode_Conformant (Subp, Nam);
3102 end if;
3104 else
3105 Error_Msg_N ("no visible subprogram matches specification", N);
3106 end if;
3107 end if;
3108 end if;
3110 <<Leave>>
3111 if Has_Aspects (N) then
3112 Analyze_Aspect_Specifications (N, Nam);
3113 end if;
3115 end Analyze_Formal_Subprogram_Declaration;
3117 -------------------------------------
3118 -- Analyze_Formal_Type_Declaration --
3119 -------------------------------------
3121 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3122 Def : constant Node_Id := Formal_Type_Definition (N);
3123 T : Entity_Id;
3125 begin
3126 T := Defining_Identifier (N);
3128 if Present (Discriminant_Specifications (N))
3129 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3130 then
3131 Error_Msg_N
3132 ("discriminants not allowed for this formal type", T);
3133 end if;
3135 -- Enter the new name, and branch to specific routine
3137 case Nkind (Def) is
3138 when N_Formal_Private_Type_Definition =>
3139 Analyze_Formal_Private_Type (N, T, Def);
3141 when N_Formal_Derived_Type_Definition =>
3142 Analyze_Formal_Derived_Type (N, T, Def);
3144 when N_Formal_Incomplete_Type_Definition =>
3145 Analyze_Formal_Incomplete_Type (T, Def);
3147 when N_Formal_Discrete_Type_Definition =>
3148 Analyze_Formal_Discrete_Type (T, Def);
3150 when N_Formal_Signed_Integer_Type_Definition =>
3151 Analyze_Formal_Signed_Integer_Type (T, Def);
3153 when N_Formal_Modular_Type_Definition =>
3154 Analyze_Formal_Modular_Type (T, Def);
3156 when N_Formal_Floating_Point_Definition =>
3157 Analyze_Formal_Floating_Type (T, Def);
3159 when N_Formal_Ordinary_Fixed_Point_Definition =>
3160 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3162 when N_Formal_Decimal_Fixed_Point_Definition =>
3163 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3165 when N_Array_Type_Definition =>
3166 Analyze_Formal_Array_Type (T, Def);
3168 when N_Access_To_Object_Definition |
3169 N_Access_Function_Definition |
3170 N_Access_Procedure_Definition =>
3171 Analyze_Generic_Access_Type (T, Def);
3173 -- Ada 2005: a interface declaration is encoded as an abstract
3174 -- record declaration or a abstract type derivation.
3176 when N_Record_Definition =>
3177 Analyze_Formal_Interface_Type (N, T, Def);
3179 when N_Derived_Type_Definition =>
3180 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3182 when N_Error =>
3183 null;
3185 when others =>
3186 raise Program_Error;
3188 end case;
3190 Set_Is_Generic_Type (T);
3192 if Has_Aspects (N) then
3193 Analyze_Aspect_Specifications (N, T);
3194 end if;
3195 end Analyze_Formal_Type_Declaration;
3197 ------------------------------------
3198 -- Analyze_Function_Instantiation --
3199 ------------------------------------
3201 procedure Analyze_Function_Instantiation (N : Node_Id) is
3202 begin
3203 Analyze_Subprogram_Instantiation (N, E_Function);
3204 end Analyze_Function_Instantiation;
3206 ---------------------------------
3207 -- Analyze_Generic_Access_Type --
3208 ---------------------------------
3210 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3211 begin
3212 Enter_Name (T);
3214 if Nkind (Def) = N_Access_To_Object_Definition then
3215 Access_Type_Declaration (T, Def);
3217 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3218 and then No (Full_View (Designated_Type (T)))
3219 and then not Is_Generic_Type (Designated_Type (T))
3220 then
3221 Error_Msg_N ("premature usage of incomplete type", Def);
3223 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3224 Error_Msg_N
3225 ("only a subtype mark is allowed in a formal", Def);
3226 end if;
3228 else
3229 Access_Subprogram_Declaration (T, Def);
3230 end if;
3231 end Analyze_Generic_Access_Type;
3233 ---------------------------------
3234 -- Analyze_Generic_Formal_Part --
3235 ---------------------------------
3237 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3238 Gen_Parm_Decl : Node_Id;
3240 begin
3241 -- The generic formals are processed in the scope of the generic unit,
3242 -- where they are immediately visible. The scope is installed by the
3243 -- caller.
3245 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3246 while Present (Gen_Parm_Decl) loop
3247 Analyze (Gen_Parm_Decl);
3248 Next (Gen_Parm_Decl);
3249 end loop;
3251 Generate_Reference_To_Generic_Formals (Current_Scope);
3252 end Analyze_Generic_Formal_Part;
3254 ------------------------------------------
3255 -- Analyze_Generic_Package_Declaration --
3256 ------------------------------------------
3258 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3259 Loc : constant Source_Ptr := Sloc (N);
3260 Decls : constant List_Id :=
3261 Visible_Declarations (Specification (N));
3262 Decl : Node_Id;
3263 Id : Entity_Id;
3264 New_N : Node_Id;
3265 Renaming : Node_Id;
3266 Save_Parent : Node_Id;
3268 begin
3269 Check_SPARK_05_Restriction ("generic is not allowed", N);
3271 -- We introduce a renaming of the enclosing package, to have a usable
3272 -- entity as the prefix of an expanded name for a local entity of the
3273 -- form Par.P.Q, where P is the generic package. This is because a local
3274 -- entity named P may hide it, so that the usual visibility rules in
3275 -- the instance will not resolve properly.
3277 Renaming :=
3278 Make_Package_Renaming_Declaration (Loc,
3279 Defining_Unit_Name =>
3280 Make_Defining_Identifier (Loc,
3281 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3282 Name =>
3283 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3285 if Present (Decls) then
3286 Decl := First (Decls);
3287 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3288 Next (Decl);
3289 end loop;
3291 if Present (Decl) then
3292 Insert_Before (Decl, Renaming);
3293 else
3294 Append (Renaming, Visible_Declarations (Specification (N)));
3295 end if;
3297 else
3298 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3299 end if;
3301 -- Create copy of generic unit, and save for instantiation. If the unit
3302 -- is a child unit, do not copy the specifications for the parent, which
3303 -- are not part of the generic tree.
3305 Save_Parent := Parent_Spec (N);
3306 Set_Parent_Spec (N, Empty);
3308 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3309 Set_Parent_Spec (New_N, Save_Parent);
3310 Rewrite (N, New_N);
3312 -- Once the contents of the generic copy and the template are swapped,
3313 -- do the same for their respective aspect specifications.
3315 Exchange_Aspects (N, New_N);
3317 -- Collect all contract-related source pragmas found within the template
3318 -- and attach them to the contract of the package spec. This contract is
3319 -- used in the capture of global references within annotations.
3321 Create_Generic_Contract (N);
3323 Id := Defining_Entity (N);
3324 Generate_Definition (Id);
3326 -- Expansion is not applied to generic units
3328 Start_Generic;
3330 Enter_Name (Id);
3331 Set_Ekind (Id, E_Generic_Package);
3332 Set_Etype (Id, Standard_Void_Type);
3334 -- A generic package declared within a Ghost region is rendered Ghost
3335 -- (SPARK RM 6.9(2)).
3337 if Ghost_Mode > None then
3338 Set_Is_Ghost_Entity (Id);
3339 end if;
3341 -- Analyze aspects now, so that generated pragmas appear in the
3342 -- declarations before building and analyzing the generic copy.
3344 if Has_Aspects (N) then
3345 Analyze_Aspect_Specifications (N, Id);
3346 end if;
3348 Push_Scope (Id);
3349 Enter_Generic_Scope (Id);
3350 Set_Inner_Instances (Id, New_Elmt_List);
3352 Set_Categorization_From_Pragmas (N);
3353 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3355 -- Link the declaration of the generic homonym in the generic copy to
3356 -- the package it renames, so that it is always resolved properly.
3358 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3359 Set_Entity (Associated_Node (Name (Renaming)), Id);
3361 -- For a library unit, we have reconstructed the entity for the unit,
3362 -- and must reset it in the library tables.
3364 if Nkind (Parent (N)) = N_Compilation_Unit then
3365 Set_Cunit_Entity (Current_Sem_Unit, Id);
3366 end if;
3368 Analyze_Generic_Formal_Part (N);
3370 -- After processing the generic formals, analysis proceeds as for a
3371 -- non-generic package.
3373 Analyze (Specification (N));
3375 Validate_Categorization_Dependency (N, Id);
3377 End_Generic;
3379 End_Package_Scope (Id);
3380 Exit_Generic_Scope (Id);
3382 if Nkind (Parent (N)) /= N_Compilation_Unit then
3383 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3384 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3385 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3387 else
3388 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3389 Validate_RT_RAT_Component (N);
3391 -- If this is a spec without a body, check that generic parameters
3392 -- are referenced.
3394 if not Body_Required (Parent (N)) then
3395 Check_References (Id);
3396 end if;
3397 end if;
3399 -- If there is a specified storage pool in the context, create an
3400 -- aspect on the package declaration, so that it is used in any
3401 -- instance that does not override it.
3403 if Present (Default_Pool) then
3404 declare
3405 ASN : Node_Id;
3407 begin
3408 ASN :=
3409 Make_Aspect_Specification (Loc,
3410 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3411 Expression => New_Copy (Default_Pool));
3413 if No (Aspect_Specifications (Specification (N))) then
3414 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3415 else
3416 Append (ASN, Aspect_Specifications (Specification (N)));
3417 end if;
3418 end;
3419 end if;
3420 end Analyze_Generic_Package_Declaration;
3422 --------------------------------------------
3423 -- Analyze_Generic_Subprogram_Declaration --
3424 --------------------------------------------
3426 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3427 Formals : List_Id;
3428 Id : Entity_Id;
3429 New_N : Node_Id;
3430 Result_Type : Entity_Id;
3431 Save_Parent : Node_Id;
3432 Spec : Node_Id;
3433 Typ : Entity_Id;
3435 begin
3436 Check_SPARK_05_Restriction ("generic is not allowed", N);
3438 -- Create copy of generic unit, and save for instantiation. If the unit
3439 -- is a child unit, do not copy the specifications for the parent, which
3440 -- are not part of the generic tree.
3442 Save_Parent := Parent_Spec (N);
3443 Set_Parent_Spec (N, Empty);
3445 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3446 Set_Parent_Spec (New_N, Save_Parent);
3447 Rewrite (N, New_N);
3449 -- Once the contents of the generic copy and the template are swapped,
3450 -- do the same for their respective aspect specifications.
3452 Exchange_Aspects (N, New_N);
3454 -- Collect all contract-related source pragmas found within the template
3455 -- and attach them to the contract of the subprogram spec. This contract
3456 -- is used in the capture of global references within annotations.
3458 Create_Generic_Contract (N);
3460 Spec := Specification (N);
3461 Id := Defining_Entity (Spec);
3462 Generate_Definition (Id);
3464 if Nkind (Id) = N_Defining_Operator_Symbol then
3465 Error_Msg_N
3466 ("operator symbol not allowed for generic subprogram", Id);
3467 end if;
3469 Start_Generic;
3471 Enter_Name (Id);
3472 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3474 -- Analyze the aspects of the generic copy to ensure that all generated
3475 -- pragmas (if any) perform their semantic effects.
3477 if Has_Aspects (N) then
3478 Analyze_Aspect_Specifications (N, Id);
3479 end if;
3481 Push_Scope (Id);
3482 Enter_Generic_Scope (Id);
3483 Set_Inner_Instances (Id, New_Elmt_List);
3484 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3486 Analyze_Generic_Formal_Part (N);
3488 Formals := Parameter_Specifications (Spec);
3490 if Nkind (Spec) = N_Function_Specification then
3491 Set_Ekind (Id, E_Generic_Function);
3492 else
3493 Set_Ekind (Id, E_Generic_Procedure);
3494 end if;
3496 if Present (Formals) then
3497 Process_Formals (Formals, Spec);
3498 end if;
3500 if Nkind (Spec) = N_Function_Specification then
3501 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3502 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3503 Set_Etype (Id, Result_Type);
3505 -- Check restriction imposed by AI05-073: a generic function
3506 -- cannot return an abstract type or an access to such.
3508 -- This is a binding interpretation should it apply to earlier
3509 -- versions of Ada as well as Ada 2012???
3511 if Is_Abstract_Type (Designated_Type (Result_Type))
3512 and then Ada_Version >= Ada_2012
3513 then
3514 Error_Msg_N
3515 ("generic function cannot have an access result "
3516 & "that designates an abstract type", Spec);
3517 end if;
3519 else
3520 Find_Type (Result_Definition (Spec));
3521 Typ := Entity (Result_Definition (Spec));
3523 if Is_Abstract_Type (Typ)
3524 and then Ada_Version >= Ada_2012
3525 then
3526 Error_Msg_N
3527 ("generic function cannot have abstract result type", Spec);
3528 end if;
3530 -- If a null exclusion is imposed on the result type, then create
3531 -- a null-excluding itype (an access subtype) and use it as the
3532 -- function's Etype.
3534 if Is_Access_Type (Typ)
3535 and then Null_Exclusion_Present (Spec)
3536 then
3537 Set_Etype (Id,
3538 Create_Null_Excluding_Itype
3539 (T => Typ,
3540 Related_Nod => Spec,
3541 Scope_Id => Defining_Unit_Name (Spec)));
3542 else
3543 Set_Etype (Id, Typ);
3544 end if;
3545 end if;
3547 else
3548 Set_Etype (Id, Standard_Void_Type);
3549 end if;
3551 -- A generic subprogram declared within a Ghost region is rendered Ghost
3552 -- (SPARK RM 6.9(2)).
3554 if Ghost_Mode > None then
3555 Set_Is_Ghost_Entity (Id);
3556 end if;
3558 -- For a library unit, we have reconstructed the entity for the unit,
3559 -- and must reset it in the library tables. We also make sure that
3560 -- Body_Required is set properly in the original compilation unit node.
3562 if Nkind (Parent (N)) = N_Compilation_Unit then
3563 Set_Cunit_Entity (Current_Sem_Unit, Id);
3564 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3565 end if;
3567 Set_Categorization_From_Pragmas (N);
3568 Validate_Categorization_Dependency (N, Id);
3570 -- Capture all global references that occur within the profile of the
3571 -- generic subprogram. Aspects are not part of this processing because
3572 -- they must be delayed. If processed now, Save_Global_References will
3573 -- destroy the Associated_Node links and prevent the capture of global
3574 -- references when the contract of the generic subprogram is analyzed.
3576 Save_Global_References (Original_Node (N));
3578 End_Generic;
3579 End_Scope;
3580 Exit_Generic_Scope (Id);
3581 Generate_Reference_To_Formals (Id);
3583 List_Inherited_Pre_Post_Aspects (Id);
3584 end Analyze_Generic_Subprogram_Declaration;
3586 -----------------------------------
3587 -- Analyze_Package_Instantiation --
3588 -----------------------------------
3590 procedure Analyze_Package_Instantiation (N : Node_Id) is
3591 Loc : constant Source_Ptr := Sloc (N);
3592 Gen_Id : constant Node_Id := Name (N);
3594 Act_Decl : Node_Id;
3595 Act_Decl_Name : Node_Id;
3596 Act_Decl_Id : Entity_Id;
3597 Act_Spec : Node_Id;
3598 Act_Tree : Node_Id;
3600 Gen_Decl : Node_Id;
3601 Gen_Spec : Node_Id;
3602 Gen_Unit : Entity_Id;
3604 Is_Actual_Pack : constant Boolean :=
3605 Is_Internal (Defining_Entity (N));
3607 Env_Installed : Boolean := False;
3608 Parent_Installed : Boolean := False;
3609 Renaming_List : List_Id;
3610 Unit_Renaming : Node_Id;
3611 Needs_Body : Boolean;
3612 Inline_Now : Boolean := False;
3613 Has_Inline_Always : Boolean := False;
3615 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3616 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3618 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3619 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3620 -- Save the SPARK_Mode-related data for restore on exit
3622 Save_Style_Check : constant Boolean := Style_Check;
3623 -- Save style check mode for restore on exit
3625 procedure Delay_Descriptors (E : Entity_Id);
3626 -- Delay generation of subprogram descriptors for given entity
3628 function Might_Inline_Subp return Boolean;
3629 -- If inlining is active and the generic contains inlined subprograms,
3630 -- we instantiate the body. This may cause superfluous instantiations,
3631 -- but it is simpler than detecting the need for the body at the point
3632 -- of inlining, when the context of the instance is not available.
3634 -----------------------
3635 -- Delay_Descriptors --
3636 -----------------------
3638 procedure Delay_Descriptors (E : Entity_Id) is
3639 begin
3640 if not Delay_Subprogram_Descriptors (E) then
3641 Set_Delay_Subprogram_Descriptors (E);
3642 Pending_Descriptor.Append (E);
3643 end if;
3644 end Delay_Descriptors;
3646 -----------------------
3647 -- Might_Inline_Subp --
3648 -----------------------
3650 function Might_Inline_Subp return Boolean is
3651 E : Entity_Id;
3653 begin
3654 if not Inline_Processing_Required then
3655 return False;
3657 else
3658 E := First_Entity (Gen_Unit);
3659 while Present (E) loop
3660 if Is_Subprogram (E) and then Is_Inlined (E) then
3661 -- Remember if there are any subprograms with Inline_Always
3663 if Has_Pragma_Inline_Always (E) then
3664 Has_Inline_Always := True;
3665 end if;
3667 return True;
3668 end if;
3670 Next_Entity (E);
3671 end loop;
3672 end if;
3674 return False;
3675 end Might_Inline_Subp;
3677 -- Local declarations
3679 Vis_Prims_List : Elist_Id := No_Elist;
3680 -- List of primitives made temporarily visible in the instantiation
3681 -- to match the visibility of the formal type
3683 -- Start of processing for Analyze_Package_Instantiation
3685 begin
3686 Check_SPARK_05_Restriction ("generic is not allowed", N);
3688 -- Very first thing: check for Text_IO special unit in case we are
3689 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3691 Check_Text_IO_Special_Unit (Name (N));
3693 -- Make node global for error reporting
3695 Instantiation_Node := N;
3697 -- Turn off style checking in instances. If the check is enabled on the
3698 -- generic unit, a warning in an instance would just be noise. If not
3699 -- enabled on the generic, then a warning in an instance is just wrong.
3701 Style_Check := False;
3703 -- Case of instantiation of a generic package
3705 if Nkind (N) = N_Package_Instantiation then
3706 Act_Decl_Id := New_Copy (Defining_Entity (N));
3707 Set_Comes_From_Source (Act_Decl_Id, True);
3709 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3710 Act_Decl_Name :=
3711 Make_Defining_Program_Unit_Name (Loc,
3712 Name =>
3713 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3714 Defining_Identifier => Act_Decl_Id);
3715 else
3716 Act_Decl_Name := Act_Decl_Id;
3717 end if;
3719 -- Case of instantiation of a formal package
3721 else
3722 Act_Decl_Id := Defining_Identifier (N);
3723 Act_Decl_Name := Act_Decl_Id;
3724 end if;
3726 Generate_Definition (Act_Decl_Id);
3727 Set_Ekind (Act_Decl_Id, E_Package);
3729 -- Initialize list of incomplete actuals before analysis
3731 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3733 Preanalyze_Actuals (N, Act_Decl_Id);
3735 Init_Env;
3736 Env_Installed := True;
3738 -- Reset renaming map for formal types. The mapping is established
3739 -- when analyzing the generic associations, but some mappings are
3740 -- inherited from formal packages of parent units, and these are
3741 -- constructed when the parents are installed.
3743 Generic_Renamings.Set_Last (0);
3744 Generic_Renamings_HTable.Reset;
3746 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3747 Gen_Unit := Entity (Gen_Id);
3749 -- Verify that it is the name of a generic package
3751 -- A visibility glitch: if the instance is a child unit and the generic
3752 -- is the generic unit of a parent instance (i.e. both the parent and
3753 -- the child units are instances of the same package) the name now
3754 -- denotes the renaming within the parent, not the intended generic
3755 -- unit. See if there is a homonym that is the desired generic. The
3756 -- renaming declaration must be visible inside the instance of the
3757 -- child, but not when analyzing the name in the instantiation itself.
3759 if Ekind (Gen_Unit) = E_Package
3760 and then Present (Renamed_Entity (Gen_Unit))
3761 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3762 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3763 and then Present (Homonym (Gen_Unit))
3764 then
3765 Gen_Unit := Homonym (Gen_Unit);
3766 end if;
3768 if Etype (Gen_Unit) = Any_Type then
3769 Restore_Env;
3770 goto Leave;
3772 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3774 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3776 if From_Limited_With (Gen_Unit) then
3777 Error_Msg_N
3778 ("cannot instantiate a limited withed package", Gen_Id);
3779 else
3780 Error_Msg_NE
3781 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3782 end if;
3784 Restore_Env;
3785 goto Leave;
3786 end if;
3788 if In_Extended_Main_Source_Unit (N) then
3789 Set_Is_Instantiated (Gen_Unit);
3790 Generate_Reference (Gen_Unit, N);
3792 if Present (Renamed_Object (Gen_Unit)) then
3793 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3794 Generate_Reference (Renamed_Object (Gen_Unit), N);
3795 end if;
3796 end if;
3798 if Nkind (Gen_Id) = N_Identifier
3799 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3800 then
3801 Error_Msg_NE
3802 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3804 elsif Nkind (Gen_Id) = N_Expanded_Name
3805 and then Is_Child_Unit (Gen_Unit)
3806 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3807 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3808 then
3809 Error_Msg_N
3810 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3811 end if;
3813 Set_Entity (Gen_Id, Gen_Unit);
3815 -- If generic is a renaming, get original generic unit
3817 if Present (Renamed_Object (Gen_Unit))
3818 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3819 then
3820 Gen_Unit := Renamed_Object (Gen_Unit);
3821 end if;
3823 -- Verify that there are no circular instantiations
3825 if In_Open_Scopes (Gen_Unit) then
3826 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3827 Restore_Env;
3828 goto Leave;
3830 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3831 Error_Msg_Node_2 := Current_Scope;
3832 Error_Msg_NE
3833 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3834 Circularity_Detected := True;
3835 Restore_Env;
3836 goto Leave;
3838 else
3839 -- If the context of the instance is subject to SPARK_Mode "off" or
3840 -- the annotation is altogether missing, set the global flag which
3841 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3842 -- the instance.
3844 if SPARK_Mode /= On then
3845 Ignore_Pragma_SPARK_Mode := True;
3846 end if;
3848 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3849 Gen_Spec := Specification (Gen_Decl);
3851 -- Initialize renamings map, for error checking, and the list that
3852 -- holds private entities whose views have changed between generic
3853 -- definition and instantiation. If this is the instance created to
3854 -- validate an actual package, the instantiation environment is that
3855 -- of the enclosing instance.
3857 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3859 -- Copy original generic tree, to produce text for instantiation
3861 Act_Tree :=
3862 Copy_Generic_Node
3863 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3865 Act_Spec := Specification (Act_Tree);
3867 -- If this is the instance created to validate an actual package,
3868 -- only the formals matter, do not examine the package spec itself.
3870 if Is_Actual_Pack then
3871 Set_Visible_Declarations (Act_Spec, New_List);
3872 Set_Private_Declarations (Act_Spec, New_List);
3873 end if;
3875 Renaming_List :=
3876 Analyze_Associations
3877 (I_Node => N,
3878 Formals => Generic_Formal_Declarations (Act_Tree),
3879 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3881 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3883 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3884 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3885 Set_Is_Generic_Instance (Act_Decl_Id);
3886 Set_Generic_Parent (Act_Spec, Gen_Unit);
3888 -- References to the generic in its own declaration or its body are
3889 -- references to the instance. Add a renaming declaration for the
3890 -- generic unit itself. This declaration, as well as the renaming
3891 -- declarations for the generic formals, must remain private to the
3892 -- unit: the formals, because this is the language semantics, and
3893 -- the unit because its use is an artifact of the implementation.
3895 Unit_Renaming :=
3896 Make_Package_Renaming_Declaration (Loc,
3897 Defining_Unit_Name =>
3898 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3899 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3901 Append (Unit_Renaming, Renaming_List);
3903 -- The renaming declarations are the first local declarations of the
3904 -- new unit.
3906 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3907 Insert_List_Before
3908 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3909 else
3910 Set_Visible_Declarations (Act_Spec, Renaming_List);
3911 end if;
3913 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3915 -- Propagate the aspect specifications from the package declaration
3916 -- template to the instantiated version of the package declaration.
3918 if Has_Aspects (Act_Tree) then
3919 Set_Aspect_Specifications (Act_Decl,
3920 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3921 end if;
3923 -- The generic may have a generated Default_Storage_Pool aspect,
3924 -- set at the point of generic declaration. If the instance has
3925 -- that aspect, it overrides the one inherited from the generic.
3927 if Has_Aspects (Gen_Spec) then
3928 if No (Aspect_Specifications (N)) then
3929 Set_Aspect_Specifications (N,
3930 (New_Copy_List_Tree
3931 (Aspect_Specifications (Gen_Spec))));
3933 else
3934 declare
3935 ASN1, ASN2 : Node_Id;
3937 begin
3938 ASN1 := First (Aspect_Specifications (N));
3939 while Present (ASN1) loop
3940 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3941 then
3942 -- If generic carries a default storage pool, remove
3943 -- it in favor of the instance one.
3945 ASN2 := First (Aspect_Specifications (Gen_Spec));
3946 while Present (ASN2) loop
3947 if Chars (Identifier (ASN2)) =
3948 Name_Default_Storage_Pool
3949 then
3950 Remove (ASN2);
3951 exit;
3952 end if;
3954 Next (ASN2);
3955 end loop;
3956 end if;
3958 Next (ASN1);
3959 end loop;
3961 Prepend_List_To (Aspect_Specifications (N),
3962 (New_Copy_List_Tree
3963 (Aspect_Specifications (Gen_Spec))));
3964 end;
3965 end if;
3966 end if;
3968 -- Save the instantiation node, for subsequent instantiation of the
3969 -- body, if there is one and we are generating code for the current
3970 -- unit. Mark unit as having a body (avoids premature error message).
3972 -- We instantiate the body if we are generating code, if we are
3973 -- generating cross-reference information, or if we are building
3974 -- trees for ASIS use or GNATprove use.
3976 declare
3977 Enclosing_Body_Present : Boolean := False;
3978 -- If the generic unit is not a compilation unit, then a body may
3979 -- be present in its parent even if none is required. We create a
3980 -- tentative pending instantiation for the body, which will be
3981 -- discarded if none is actually present.
3983 Scop : Entity_Id;
3985 begin
3986 if Scope (Gen_Unit) /= Standard_Standard
3987 and then not Is_Child_Unit (Gen_Unit)
3988 then
3989 Scop := Scope (Gen_Unit);
3990 while Present (Scop) and then Scop /= Standard_Standard loop
3991 if Unit_Requires_Body (Scop) then
3992 Enclosing_Body_Present := True;
3993 exit;
3995 elsif In_Open_Scopes (Scop)
3996 and then In_Package_Body (Scop)
3997 then
3998 Enclosing_Body_Present := True;
3999 exit;
4000 end if;
4002 exit when Is_Compilation_Unit (Scop);
4003 Scop := Scope (Scop);
4004 end loop;
4005 end if;
4007 -- If front-end inlining is enabled or there are any subprograms
4008 -- marked with Inline_Always, and this is a unit for which code
4009 -- will be generated, we instantiate the body at once.
4011 -- This is done if the instance is not the main unit, and if the
4012 -- generic is not a child unit of another generic, to avoid scope
4013 -- problems and the reinstallation of parent instances.
4015 if Expander_Active
4016 and then (not Is_Child_Unit (Gen_Unit)
4017 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4018 and then Might_Inline_Subp
4019 and then not Is_Actual_Pack
4020 then
4021 if not Back_End_Inlining
4022 and then (Front_End_Inlining or else Has_Inline_Always)
4023 and then (Is_In_Main_Unit (N)
4024 or else In_Main_Context (Current_Scope))
4025 and then Nkind (Parent (N)) /= N_Compilation_Unit
4026 then
4027 Inline_Now := True;
4029 -- In configurable_run_time mode we force the inlining of
4030 -- predefined subprograms marked Inline_Always, to minimize
4031 -- the use of the run-time library.
4033 elsif Is_Predefined_File_Name
4034 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4035 and then Configurable_Run_Time_Mode
4036 and then Nkind (Parent (N)) /= N_Compilation_Unit
4037 then
4038 Inline_Now := True;
4039 end if;
4041 -- If the current scope is itself an instance within a child
4042 -- unit, there will be duplications in the scope stack, and the
4043 -- unstacking mechanism in Inline_Instance_Body will fail.
4044 -- This loses some rare cases of optimization, and might be
4045 -- improved some day, if we can find a proper abstraction for
4046 -- "the complete compilation context" that can be saved and
4047 -- restored. ???
4049 if Is_Generic_Instance (Current_Scope) then
4050 declare
4051 Curr_Unit : constant Entity_Id :=
4052 Cunit_Entity (Current_Sem_Unit);
4053 begin
4054 if Curr_Unit /= Current_Scope
4055 and then Is_Child_Unit (Curr_Unit)
4056 then
4057 Inline_Now := False;
4058 end if;
4059 end;
4060 end if;
4061 end if;
4063 Needs_Body :=
4064 (Unit_Requires_Body (Gen_Unit)
4065 or else Enclosing_Body_Present
4066 or else Present (Corresponding_Body (Gen_Decl)))
4067 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4068 and then not Is_Actual_Pack
4069 and then not Inline_Now
4070 and then (Operating_Mode = Generate_Code
4072 -- Need comment for this check ???
4074 or else (Operating_Mode = Check_Semantics
4075 and then (ASIS_Mode or GNATprove_Mode)));
4077 -- If front-end inlining is enabled or there are any subprograms
4078 -- marked with Inline_Always, do not instantiate body when within
4079 -- a generic context.
4081 if ((Front_End_Inlining or else Has_Inline_Always)
4082 and then not Expander_Active)
4083 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4084 then
4085 Needs_Body := False;
4086 end if;
4088 -- If the current context is generic, and the package being
4089 -- instantiated is declared within a formal package, there is no
4090 -- body to instantiate until the enclosing generic is instantiated
4091 -- and there is an actual for the formal package. If the formal
4092 -- package has parameters, we build a regular package instance for
4093 -- it, that precedes the original formal package declaration.
4095 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4096 declare
4097 Decl : constant Node_Id :=
4098 Original_Node
4099 (Unit_Declaration_Node (Scope (Gen_Unit)));
4100 begin
4101 if Nkind (Decl) = N_Formal_Package_Declaration
4102 or else (Nkind (Decl) = N_Package_Declaration
4103 and then Is_List_Member (Decl)
4104 and then Present (Next (Decl))
4105 and then
4106 Nkind (Next (Decl)) =
4107 N_Formal_Package_Declaration)
4108 then
4109 Needs_Body := False;
4110 end if;
4111 end;
4112 end if;
4113 end;
4115 -- For RCI unit calling stubs, we omit the instance body if the
4116 -- instance is the RCI library unit itself.
4118 -- However there is a special case for nested instances: in this case
4119 -- we do generate the instance body, as it might be required, e.g.
4120 -- because it provides stream attributes for some type used in the
4121 -- profile of a remote subprogram. This is consistent with 12.3(12),
4122 -- which indicates that the instance body occurs at the place of the
4123 -- instantiation, and thus is part of the RCI declaration, which is
4124 -- present on all client partitions (this is E.2.3(18)).
4126 -- Note that AI12-0002 may make it illegal at some point to have
4127 -- stream attributes defined in an RCI unit, in which case this
4128 -- special case will become unnecessary. In the meantime, there
4129 -- is known application code in production that depends on this
4130 -- being possible, so we definitely cannot eliminate the body in
4131 -- the case of nested instances for the time being.
4133 -- When we generate a nested instance body, calling stubs for any
4134 -- relevant subprogram will be be inserted immediately after the
4135 -- subprogram declarations, and will take precedence over the
4136 -- subsequent (original) body. (The stub and original body will be
4137 -- complete homographs, but this is permitted in an instance).
4138 -- (Could we do better and remove the original body???)
4140 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4141 and then Comes_From_Source (N)
4142 and then Nkind (Parent (N)) = N_Compilation_Unit
4143 then
4144 Needs_Body := False;
4145 end if;
4147 if Needs_Body then
4149 -- Here is a defence against a ludicrous number of instantiations
4150 -- caused by a circular set of instantiation attempts.
4152 if Pending_Instantiations.Last > Maximum_Instantiations then
4153 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4154 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4155 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4156 raise Unrecoverable_Error;
4157 end if;
4159 -- Indicate that the enclosing scopes contain an instantiation,
4160 -- and that cleanup actions should be delayed until after the
4161 -- instance body is expanded.
4163 Check_Forward_Instantiation (Gen_Decl);
4164 if Nkind (N) = N_Package_Instantiation then
4165 declare
4166 Enclosing_Master : Entity_Id;
4168 begin
4169 -- Loop to search enclosing masters
4171 Enclosing_Master := Current_Scope;
4172 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4173 if Ekind (Enclosing_Master) = E_Package then
4174 if Is_Compilation_Unit (Enclosing_Master) then
4175 if In_Package_Body (Enclosing_Master) then
4176 Delay_Descriptors
4177 (Body_Entity (Enclosing_Master));
4178 else
4179 Delay_Descriptors
4180 (Enclosing_Master);
4181 end if;
4183 exit Scope_Loop;
4185 else
4186 Enclosing_Master := Scope (Enclosing_Master);
4187 end if;
4189 elsif Is_Generic_Unit (Enclosing_Master)
4190 or else Ekind (Enclosing_Master) = E_Void
4191 then
4192 -- Cleanup actions will eventually be performed on the
4193 -- enclosing subprogram or package instance, if any.
4194 -- Enclosing scope is void in the formal part of a
4195 -- generic subprogram.
4197 exit Scope_Loop;
4199 else
4200 if Ekind (Enclosing_Master) = E_Entry
4201 and then
4202 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4203 then
4204 if not Expander_Active then
4205 exit Scope_Loop;
4206 else
4207 Enclosing_Master :=
4208 Protected_Body_Subprogram (Enclosing_Master);
4209 end if;
4210 end if;
4212 Set_Delay_Cleanups (Enclosing_Master);
4214 while Ekind (Enclosing_Master) = E_Block loop
4215 Enclosing_Master := Scope (Enclosing_Master);
4216 end loop;
4218 if Is_Subprogram (Enclosing_Master) then
4219 Delay_Descriptors (Enclosing_Master);
4221 elsif Is_Task_Type (Enclosing_Master) then
4222 declare
4223 TBP : constant Node_Id :=
4224 Get_Task_Body_Procedure
4225 (Enclosing_Master);
4226 begin
4227 if Present (TBP) then
4228 Delay_Descriptors (TBP);
4229 Set_Delay_Cleanups (TBP);
4230 end if;
4231 end;
4232 end if;
4234 exit Scope_Loop;
4235 end if;
4236 end loop Scope_Loop;
4237 end;
4239 -- Make entry in table
4241 Add_Pending_Instantiation (N, Act_Decl);
4242 end if;
4243 end if;
4245 Set_Categorization_From_Pragmas (Act_Decl);
4247 if Parent_Installed then
4248 Hide_Current_Scope;
4249 end if;
4251 Set_Instance_Spec (N, Act_Decl);
4253 -- If not a compilation unit, insert the package declaration before
4254 -- the original instantiation node.
4256 if Nkind (Parent (N)) /= N_Compilation_Unit then
4257 Mark_Rewrite_Insertion (Act_Decl);
4258 Insert_Before (N, Act_Decl);
4260 if Has_Aspects (N) then
4261 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4263 -- The pragma created for a Default_Storage_Pool aspect must
4264 -- appear ahead of the declarations in the instance spec.
4265 -- Analysis has placed it after the instance node, so remove
4266 -- it and reinsert it properly now.
4268 declare
4269 ASN : constant Node_Id := First (Aspect_Specifications (N));
4270 A_Name : constant Name_Id := Chars (Identifier (ASN));
4271 Decl : Node_Id;
4273 begin
4274 if A_Name = Name_Default_Storage_Pool then
4275 if No (Visible_Declarations (Act_Spec)) then
4276 Set_Visible_Declarations (Act_Spec, New_List);
4277 end if;
4279 Decl := Next (N);
4280 while Present (Decl) loop
4281 if Nkind (Decl) = N_Pragma then
4282 Remove (Decl);
4283 Prepend (Decl, Visible_Declarations (Act_Spec));
4284 exit;
4285 end if;
4287 Next (Decl);
4288 end loop;
4289 end if;
4290 end;
4291 end if;
4293 Analyze (Act_Decl);
4295 -- For an instantiation that is a compilation unit, place
4296 -- declaration on current node so context is complete for analysis
4297 -- (including nested instantiations). If this is the main unit,
4298 -- the declaration eventually replaces the instantiation node.
4299 -- If the instance body is created later, it replaces the
4300 -- instance node, and the declaration is attached to it
4301 -- (see Build_Instance_Compilation_Unit_Nodes).
4303 else
4304 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4306 -- The entity for the current unit is the newly created one,
4307 -- and all semantic information is attached to it.
4309 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4311 -- If this is the main unit, replace the main entity as well
4313 if Current_Sem_Unit = Main_Unit then
4314 Main_Unit_Entity := Act_Decl_Id;
4315 end if;
4316 end if;
4318 Set_Unit (Parent (N), Act_Decl);
4319 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4320 Set_Package_Instantiation (Act_Decl_Id, N);
4322 -- Process aspect specifications of the instance node, if any, to
4323 -- take into account categorization pragmas before analyzing the
4324 -- instance.
4326 if Has_Aspects (N) then
4327 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4328 end if;
4330 Analyze (Act_Decl);
4331 Set_Unit (Parent (N), N);
4332 Set_Body_Required (Parent (N), False);
4334 -- We never need elaboration checks on instantiations, since by
4335 -- definition, the body instantiation is elaborated at the same
4336 -- time as the spec instantiation.
4338 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4339 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4340 end if;
4342 Check_Elab_Instantiation (N);
4344 if ABE_Is_Certain (N) and then Needs_Body then
4345 Pending_Instantiations.Decrement_Last;
4346 end if;
4348 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4350 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4351 First_Private_Entity (Act_Decl_Id));
4353 -- If the instantiation will receive a body, the unit will be
4354 -- transformed into a package body, and receive its own elaboration
4355 -- entity. Otherwise, the nature of the unit is now a package
4356 -- declaration.
4358 if Nkind (Parent (N)) = N_Compilation_Unit
4359 and then not Needs_Body
4360 then
4361 Rewrite (N, Act_Decl);
4362 end if;
4364 if Present (Corresponding_Body (Gen_Decl))
4365 or else Unit_Requires_Body (Gen_Unit)
4366 then
4367 Set_Has_Completion (Act_Decl_Id);
4368 end if;
4370 Check_Formal_Packages (Act_Decl_Id);
4372 Restore_Hidden_Primitives (Vis_Prims_List);
4373 Restore_Private_Views (Act_Decl_Id);
4375 Inherit_Context (Gen_Decl, N);
4377 if Parent_Installed then
4378 Remove_Parent;
4379 end if;
4381 Restore_Env;
4382 Env_Installed := False;
4383 end if;
4385 Validate_Categorization_Dependency (N, Act_Decl_Id);
4387 -- There used to be a check here to prevent instantiations in local
4388 -- contexts if the No_Local_Allocators restriction was active. This
4389 -- check was removed by a binding interpretation in AI-95-00130/07,
4390 -- but we retain the code for documentation purposes.
4392 -- if Ekind (Act_Decl_Id) /= E_Void
4393 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4394 -- then
4395 -- Check_Restriction (No_Local_Allocators, N);
4396 -- end if;
4398 if Inline_Now then
4399 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4400 end if;
4402 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4403 -- be used as defining identifiers for a formal package and for the
4404 -- corresponding expanded package.
4406 if Nkind (N) = N_Formal_Package_Declaration then
4407 Act_Decl_Id := New_Copy (Defining_Entity (N));
4408 Set_Comes_From_Source (Act_Decl_Id, True);
4409 Set_Is_Generic_Instance (Act_Decl_Id, False);
4410 Set_Defining_Identifier (N, Act_Decl_Id);
4411 end if;
4413 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4414 SPARK_Mode := Save_SM;
4415 SPARK_Mode_Pragma := Save_SMP;
4416 Style_Check := Save_Style_Check;
4418 -- Check that if N is an instantiation of System.Dim_Float_IO or
4419 -- System.Dim_Integer_IO, the formal type has a dimension system.
4421 if Nkind (N) = N_Package_Instantiation
4422 and then Is_Dim_IO_Package_Instantiation (N)
4423 then
4424 declare
4425 Assoc : constant Node_Id := First (Generic_Associations (N));
4426 begin
4427 if not Has_Dimension_System
4428 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4429 then
4430 Error_Msg_N ("type with a dimension system expected", Assoc);
4431 end if;
4432 end;
4433 end if;
4435 <<Leave>>
4436 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4437 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4438 end if;
4440 exception
4441 when Instantiation_Error =>
4442 if Parent_Installed then
4443 Remove_Parent;
4444 end if;
4446 if Env_Installed then
4447 Restore_Env;
4448 end if;
4450 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4451 SPARK_Mode := Save_SM;
4452 SPARK_Mode_Pragma := Save_SMP;
4453 Style_Check := Save_Style_Check;
4454 end Analyze_Package_Instantiation;
4456 --------------------------
4457 -- Inline_Instance_Body --
4458 --------------------------
4460 procedure Inline_Instance_Body
4461 (N : Node_Id;
4462 Gen_Unit : Entity_Id;
4463 Act_Decl : Node_Id)
4465 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4466 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4467 Gen_Comp : constant Entity_Id :=
4468 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4470 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4471 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4472 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4473 -- to provide a clean environment for analysis of the inlined body will
4474 -- eliminate any previously set SPARK_Mode.
4476 Scope_Stack_Depth : constant Pos :=
4477 Scope_Stack.Last - Scope_Stack.First + 1;
4479 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4480 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4481 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4482 Curr_Scope : Entity_Id := Empty;
4483 List : Elist_Id;
4484 Num_Inner : Nat := 0;
4485 Num_Scopes : Nat := 0;
4486 N_Instances : Nat := 0;
4487 Removed : Boolean := False;
4488 S : Entity_Id;
4489 Vis : Boolean;
4491 begin
4492 -- Case of generic unit defined in another unit. We must remove the
4493 -- complete context of the current unit to install that of the generic.
4495 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4497 -- Add some comments for the following two loops ???
4499 S := Current_Scope;
4500 while Present (S) and then S /= Standard_Standard loop
4501 loop
4502 Num_Scopes := Num_Scopes + 1;
4504 Use_Clauses (Num_Scopes) :=
4505 (Scope_Stack.Table
4506 (Scope_Stack.Last - Num_Scopes + 1).
4507 First_Use_Clause);
4508 End_Use_Clauses (Use_Clauses (Num_Scopes));
4510 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4511 or else Scope_Stack.Table
4512 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4513 end loop;
4515 exit when Is_Generic_Instance (S)
4516 and then (In_Package_Body (S)
4517 or else Ekind (S) = E_Procedure
4518 or else Ekind (S) = E_Function);
4519 S := Scope (S);
4520 end loop;
4522 Vis := Is_Immediately_Visible (Gen_Comp);
4524 -- Find and save all enclosing instances
4526 S := Current_Scope;
4528 while Present (S)
4529 and then S /= Standard_Standard
4530 loop
4531 if Is_Generic_Instance (S) then
4532 N_Instances := N_Instances + 1;
4533 Instances (N_Instances) := S;
4535 exit when In_Package_Body (S);
4536 end if;
4538 S := Scope (S);
4539 end loop;
4541 -- Remove context of current compilation unit, unless we are within a
4542 -- nested package instantiation, in which case the context has been
4543 -- removed previously.
4545 -- If current scope is the body of a child unit, remove context of
4546 -- spec as well. If an enclosing scope is an instance body, the
4547 -- context has already been removed, but the entities in the body
4548 -- must be made invisible as well.
4550 S := Current_Scope;
4551 while Present (S) and then S /= Standard_Standard loop
4552 if Is_Generic_Instance (S)
4553 and then (In_Package_Body (S)
4554 or else Ekind_In (S, E_Procedure, E_Function))
4555 then
4556 -- We still have to remove the entities of the enclosing
4557 -- instance from direct visibility.
4559 declare
4560 E : Entity_Id;
4561 begin
4562 E := First_Entity (S);
4563 while Present (E) loop
4564 Set_Is_Immediately_Visible (E, False);
4565 Next_Entity (E);
4566 end loop;
4567 end;
4569 exit;
4570 end if;
4572 if S = Curr_Unit
4573 or else (Ekind (Curr_Unit) = E_Package_Body
4574 and then S = Spec_Entity (Curr_Unit))
4575 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4576 and then S = Corresponding_Spec
4577 (Unit_Declaration_Node (Curr_Unit)))
4578 then
4579 Removed := True;
4581 -- Remove entities in current scopes from visibility, so that
4582 -- instance body is compiled in a clean environment.
4584 List := Save_Scope_Stack (Handle_Use => False);
4586 if Is_Child_Unit (S) then
4588 -- Remove child unit from stack, as well as inner scopes.
4589 -- Removing the context of a child unit removes parent units
4590 -- as well.
4592 while Current_Scope /= S loop
4593 Num_Inner := Num_Inner + 1;
4594 Inner_Scopes (Num_Inner) := Current_Scope;
4595 Pop_Scope;
4596 end loop;
4598 Pop_Scope;
4599 Remove_Context (Curr_Comp);
4600 Curr_Scope := S;
4602 else
4603 Remove_Context (Curr_Comp);
4604 end if;
4606 if Ekind (Curr_Unit) = E_Package_Body then
4607 Remove_Context (Library_Unit (Curr_Comp));
4608 end if;
4609 end if;
4611 S := Scope (S);
4612 end loop;
4614 pragma Assert (Num_Inner < Num_Scopes);
4616 -- The inlined package body must be analyzed with the SPARK_Mode of
4617 -- the enclosing context, otherwise the body may cause bogus errors
4618 -- if a configuration SPARK_Mode pragma in in effect.
4620 Push_Scope (Standard_Standard);
4621 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4622 Instantiate_Package_Body
4623 (Body_Info =>
4624 ((Inst_Node => N,
4625 Act_Decl => Act_Decl,
4626 Expander_Status => Expander_Active,
4627 Current_Sem_Unit => Current_Sem_Unit,
4628 Scope_Suppress => Scope_Suppress,
4629 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4630 Version => Ada_Version,
4631 Version_Pragma => Ada_Version_Pragma,
4632 Warnings => Save_Warnings,
4633 SPARK_Mode => Save_SM,
4634 SPARK_Mode_Pragma => Save_SMP)),
4635 Inlined_Body => True);
4637 Pop_Scope;
4639 -- Restore context
4641 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4643 -- Reset Generic_Instance flag so that use clauses can be installed
4644 -- in the proper order. (See Use_One_Package for effect of enclosing
4645 -- instances on processing of use clauses).
4647 for J in 1 .. N_Instances loop
4648 Set_Is_Generic_Instance (Instances (J), False);
4649 end loop;
4651 if Removed then
4652 Install_Context (Curr_Comp);
4654 if Present (Curr_Scope)
4655 and then Is_Child_Unit (Curr_Scope)
4656 then
4657 Push_Scope (Curr_Scope);
4658 Set_Is_Immediately_Visible (Curr_Scope);
4660 -- Finally, restore inner scopes as well
4662 for J in reverse 1 .. Num_Inner loop
4663 Push_Scope (Inner_Scopes (J));
4664 end loop;
4665 end if;
4667 Restore_Scope_Stack (List, Handle_Use => False);
4669 if Present (Curr_Scope)
4670 and then
4671 (In_Private_Part (Curr_Scope)
4672 or else In_Package_Body (Curr_Scope))
4673 then
4674 -- Install private declaration of ancestor units, which are
4675 -- currently available. Restore_Scope_Stack and Install_Context
4676 -- only install the visible part of parents.
4678 declare
4679 Par : Entity_Id;
4680 begin
4681 Par := Scope (Curr_Scope);
4682 while (Present (Par)) and then Par /= Standard_Standard loop
4683 Install_Private_Declarations (Par);
4684 Par := Scope (Par);
4685 end loop;
4686 end;
4687 end if;
4688 end if;
4690 -- Restore use clauses. For a child unit, use clauses in the parents
4691 -- are restored when installing the context, so only those in inner
4692 -- scopes (and those local to the child unit itself) need to be
4693 -- installed explicitly.
4695 if Is_Child_Unit (Curr_Unit) and then Removed then
4696 for J in reverse 1 .. Num_Inner + 1 loop
4697 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4698 Use_Clauses (J);
4699 Install_Use_Clauses (Use_Clauses (J));
4700 end loop;
4702 else
4703 for J in reverse 1 .. Num_Scopes loop
4704 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4705 Use_Clauses (J);
4706 Install_Use_Clauses (Use_Clauses (J));
4707 end loop;
4708 end if;
4710 -- Restore status of instances. If one of them is a body, make its
4711 -- local entities visible again.
4713 declare
4714 E : Entity_Id;
4715 Inst : Entity_Id;
4717 begin
4718 for J in 1 .. N_Instances loop
4719 Inst := Instances (J);
4720 Set_Is_Generic_Instance (Inst, True);
4722 if In_Package_Body (Inst)
4723 or else Ekind_In (S, E_Procedure, E_Function)
4724 then
4725 E := First_Entity (Instances (J));
4726 while Present (E) loop
4727 Set_Is_Immediately_Visible (E);
4728 Next_Entity (E);
4729 end loop;
4730 end if;
4731 end loop;
4732 end;
4734 -- If generic unit is in current unit, current context is correct. Note
4735 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4736 -- enclosing scopes were removed.
4738 else
4739 Instantiate_Package_Body
4740 (Body_Info =>
4741 ((Inst_Node => N,
4742 Act_Decl => Act_Decl,
4743 Expander_Status => Expander_Active,
4744 Current_Sem_Unit => Current_Sem_Unit,
4745 Scope_Suppress => Scope_Suppress,
4746 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4747 Version => Ada_Version,
4748 Version_Pragma => Ada_Version_Pragma,
4749 Warnings => Save_Warnings,
4750 SPARK_Mode => SPARK_Mode,
4751 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4752 Inlined_Body => True);
4753 end if;
4754 end Inline_Instance_Body;
4756 -------------------------------------
4757 -- Analyze_Procedure_Instantiation --
4758 -------------------------------------
4760 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4761 begin
4762 Analyze_Subprogram_Instantiation (N, E_Procedure);
4763 end Analyze_Procedure_Instantiation;
4765 -----------------------------------
4766 -- Need_Subprogram_Instance_Body --
4767 -----------------------------------
4769 function Need_Subprogram_Instance_Body
4770 (N : Node_Id;
4771 Subp : Entity_Id) return Boolean
4774 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4775 -- Return True if E is an inlined subprogram, an inlined renaming or a
4776 -- subprogram nested in an inlined subprogram. The inlining machinery
4777 -- totally disregards nested subprograms since it considers that they
4778 -- will always be compiled if the parent is (see Inline.Is_Nested).
4780 ------------------------------------
4781 -- Is_Inlined_Or_Child_Of_Inlined --
4782 ------------------------------------
4784 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4785 Scop : Entity_Id;
4787 begin
4788 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4789 return True;
4790 end if;
4792 Scop := Scope (E);
4793 while Scop /= Standard_Standard loop
4794 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4795 return True;
4796 end if;
4798 Scop := Scope (Scop);
4799 end loop;
4801 return False;
4802 end Is_Inlined_Or_Child_Of_Inlined;
4804 begin
4805 -- Must be in the main unit or inlined (or child of inlined)
4807 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4809 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4811 and then (Operating_Mode = Generate_Code
4812 or else (Operating_Mode = Check_Semantics
4813 and then (ASIS_Mode or GNATprove_Mode)))
4815 -- The body is needed when generating code (full expansion), in ASIS
4816 -- mode for other tools, and in GNATprove mode (special expansion) for
4817 -- formal verification of the body itself.
4819 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4821 -- No point in inlining if ABE is inevitable
4823 and then not ABE_Is_Certain (N)
4825 -- Or if subprogram is eliminated
4827 and then not Is_Eliminated (Subp)
4828 then
4829 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4830 return True;
4832 -- Here if not inlined, or we ignore the inlining
4834 else
4835 return False;
4836 end if;
4837 end Need_Subprogram_Instance_Body;
4839 --------------------------------------
4840 -- Analyze_Subprogram_Instantiation --
4841 --------------------------------------
4843 procedure Analyze_Subprogram_Instantiation
4844 (N : Node_Id;
4845 K : Entity_Kind)
4847 Loc : constant Source_Ptr := Sloc (N);
4848 Gen_Id : constant Node_Id := Name (N);
4850 Anon_Id : constant Entity_Id :=
4851 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4852 Chars => New_External_Name
4853 (Chars (Defining_Entity (N)), 'R'));
4855 Act_Decl_Id : Entity_Id;
4856 Act_Decl : Node_Id;
4857 Act_Spec : Node_Id;
4858 Act_Tree : Node_Id;
4860 Env_Installed : Boolean := False;
4861 Gen_Unit : Entity_Id;
4862 Gen_Decl : Node_Id;
4863 Pack_Id : Entity_Id;
4864 Parent_Installed : Boolean := False;
4866 Renaming_List : List_Id;
4867 -- The list of declarations that link formals and actuals of the
4868 -- instance. These are subtype declarations for formal types, and
4869 -- renaming declarations for other formals. The subprogram declaration
4870 -- for the instance is then appended to the list, and the last item on
4871 -- the list is the renaming declaration for the instance.
4873 procedure Analyze_Instance_And_Renamings;
4874 -- The instance must be analyzed in a context that includes the mappings
4875 -- of generic parameters into actuals. We create a package declaration
4876 -- for this purpose, and a subprogram with an internal name within the
4877 -- package. The subprogram instance is simply an alias for the internal
4878 -- subprogram, declared in the current scope.
4880 procedure Build_Subprogram_Renaming;
4881 -- If the subprogram is recursive, there are occurrences of the name of
4882 -- the generic within the body, which must resolve to the current
4883 -- instance. We add a renaming declaration after the declaration, which
4884 -- is available in the instance body, as well as in the analysis of
4885 -- aspects that appear in the generic. This renaming declaration is
4886 -- inserted after the instance declaration which it renames.
4888 ------------------------------------
4889 -- Analyze_Instance_And_Renamings --
4890 ------------------------------------
4892 procedure Analyze_Instance_And_Renamings is
4893 Def_Ent : constant Entity_Id := Defining_Entity (N);
4894 Pack_Decl : Node_Id;
4896 begin
4897 if Nkind (Parent (N)) = N_Compilation_Unit then
4899 -- For the case of a compilation unit, the container package has
4900 -- the same name as the instantiation, to insure that the binder
4901 -- calls the elaboration procedure with the right name. Copy the
4902 -- entity of the instance, which may have compilation level flags
4903 -- (e.g. Is_Child_Unit) set.
4905 Pack_Id := New_Copy (Def_Ent);
4907 else
4908 -- Otherwise we use the name of the instantiation concatenated
4909 -- with its source position to ensure uniqueness if there are
4910 -- several instantiations with the same name.
4912 Pack_Id :=
4913 Make_Defining_Identifier (Loc,
4914 Chars => New_External_Name
4915 (Related_Id => Chars (Def_Ent),
4916 Suffix => "GP",
4917 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4918 end if;
4920 Pack_Decl :=
4921 Make_Package_Declaration (Loc,
4922 Specification => Make_Package_Specification (Loc,
4923 Defining_Unit_Name => Pack_Id,
4924 Visible_Declarations => Renaming_List,
4925 End_Label => Empty));
4927 Set_Instance_Spec (N, Pack_Decl);
4928 Set_Is_Generic_Instance (Pack_Id);
4929 Set_Debug_Info_Needed (Pack_Id);
4931 -- Case of not a compilation unit
4933 if Nkind (Parent (N)) /= N_Compilation_Unit then
4934 Mark_Rewrite_Insertion (Pack_Decl);
4935 Insert_Before (N, Pack_Decl);
4936 Set_Has_Completion (Pack_Id);
4938 -- Case of an instantiation that is a compilation unit
4940 -- Place declaration on current node so context is complete for
4941 -- analysis (including nested instantiations), and for use in a
4942 -- context_clause (see Analyze_With_Clause).
4944 else
4945 Set_Unit (Parent (N), Pack_Decl);
4946 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4947 end if;
4949 Analyze (Pack_Decl);
4950 Check_Formal_Packages (Pack_Id);
4951 Set_Is_Generic_Instance (Pack_Id, False);
4953 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4954 -- above???
4956 -- Body of the enclosing package is supplied when instantiating the
4957 -- subprogram body, after semantic analysis is completed.
4959 if Nkind (Parent (N)) = N_Compilation_Unit then
4961 -- Remove package itself from visibility, so it does not
4962 -- conflict with subprogram.
4964 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4966 -- Set name and scope of internal subprogram so that the proper
4967 -- external name will be generated. The proper scope is the scope
4968 -- of the wrapper package. We need to generate debugging info for
4969 -- the internal subprogram, so set flag accordingly.
4971 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4972 Set_Scope (Anon_Id, Scope (Pack_Id));
4974 -- Mark wrapper package as referenced, to avoid spurious warnings
4975 -- if the instantiation appears in various with_ clauses of
4976 -- subunits of the main unit.
4978 Set_Referenced (Pack_Id);
4979 end if;
4981 Set_Is_Generic_Instance (Anon_Id);
4982 Set_Debug_Info_Needed (Anon_Id);
4983 Act_Decl_Id := New_Copy (Anon_Id);
4985 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4986 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4987 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4989 -- Subprogram instance comes from source only if generic does
4991 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4993 -- If the instance is a child unit, mark the Id accordingly. Mark
4994 -- the anonymous entity as well, which is the real subprogram and
4995 -- which is used when the instance appears in a context clause.
4996 -- Similarly, propagate the Is_Eliminated flag to handle properly
4997 -- nested eliminated subprograms.
4999 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5000 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5001 New_Overloaded_Entity (Act_Decl_Id);
5002 Check_Eliminated (Act_Decl_Id);
5003 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5005 -- In compilation unit case, kill elaboration checks on the
5006 -- instantiation, since they are never needed -- the body is
5007 -- instantiated at the same point as the spec.
5009 if Nkind (Parent (N)) = N_Compilation_Unit then
5010 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5011 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5012 Set_Is_Compilation_Unit (Anon_Id);
5014 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5015 end if;
5017 -- The instance is not a freezing point for the new subprogram.
5018 -- The anonymous subprogram may have a freeze node, created for
5019 -- some delayed aspects. This freeze node must not be inherited
5020 -- by the visible subprogram entity.
5022 Set_Is_Frozen (Act_Decl_Id, False);
5023 Set_Freeze_Node (Act_Decl_Id, Empty);
5025 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5026 Valid_Operator_Definition (Act_Decl_Id);
5027 end if;
5029 Set_Alias (Act_Decl_Id, Anon_Id);
5030 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5031 Set_Has_Completion (Act_Decl_Id);
5032 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5034 if Nkind (Parent (N)) = N_Compilation_Unit then
5035 Set_Body_Required (Parent (N), False);
5036 end if;
5037 end Analyze_Instance_And_Renamings;
5039 -------------------------------
5040 -- Build_Subprogram_Renaming --
5041 -------------------------------
5043 procedure Build_Subprogram_Renaming is
5044 Renaming_Decl : Node_Id;
5045 Unit_Renaming : Node_Id;
5047 begin
5048 Unit_Renaming :=
5049 Make_Subprogram_Renaming_Declaration (Loc,
5050 Specification =>
5051 Copy_Generic_Node
5052 (Specification (Original_Node (Gen_Decl)),
5053 Empty,
5054 Instantiating => True),
5055 Name => New_Occurrence_Of (Anon_Id, Loc));
5057 -- The generic may be a a child unit. The renaming needs an
5058 -- identifier with the proper name.
5060 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5061 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5063 -- If there is a formal subprogram with the same name as the unit
5064 -- itself, do not add this renaming declaration, to prevent
5065 -- ambiguities when there is a call with that name in the body.
5066 -- This is a partial and ugly fix for one ACATS test. ???
5068 Renaming_Decl := First (Renaming_List);
5069 while Present (Renaming_Decl) loop
5070 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5071 and then
5072 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5073 then
5074 exit;
5075 end if;
5077 Next (Renaming_Decl);
5078 end loop;
5080 if No (Renaming_Decl) then
5081 Append (Unit_Renaming, Renaming_List);
5082 end if;
5083 end Build_Subprogram_Renaming;
5085 -- Local variables
5087 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5088 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5090 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5091 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5092 -- Save the SPARK_Mode-related data for restore on exit
5094 Vis_Prims_List : Elist_Id := No_Elist;
5095 -- List of primitives made temporarily visible in the instantiation
5096 -- to match the visibility of the formal type
5098 -- Start of processing for Analyze_Subprogram_Instantiation
5100 begin
5101 Check_SPARK_05_Restriction ("generic is not allowed", N);
5103 -- Very first thing: check for special Text_IO unit in case we are
5104 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5105 -- such an instantiation is bogus (these are packages, not subprograms),
5106 -- but we get a better error message if we do this.
5108 Check_Text_IO_Special_Unit (Gen_Id);
5110 -- Make node global for error reporting
5112 Instantiation_Node := N;
5114 -- For package instantiations we turn off style checks, because they
5115 -- will have been emitted in the generic. For subprogram instantiations
5116 -- we want to apply at least the check on overriding indicators so we
5117 -- do not modify the style check status.
5119 -- The renaming declarations for the actuals do not come from source and
5120 -- will not generate spurious warnings.
5122 Preanalyze_Actuals (N);
5124 Init_Env;
5125 Env_Installed := True;
5126 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5127 Gen_Unit := Entity (Gen_Id);
5129 Generate_Reference (Gen_Unit, Gen_Id);
5131 if Nkind (Gen_Id) = N_Identifier
5132 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5133 then
5134 Error_Msg_NE
5135 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5136 end if;
5138 if Etype (Gen_Unit) = Any_Type then
5139 Restore_Env;
5140 return;
5141 end if;
5143 -- Verify that it is a generic subprogram of the right kind, and that
5144 -- it does not lead to a circular instantiation.
5146 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5147 Error_Msg_NE
5148 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5150 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5151 Error_Msg_NE
5152 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5154 elsif In_Open_Scopes (Gen_Unit) then
5155 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5157 else
5158 -- If the context of the instance is subject to SPARK_Mode "off" or
5159 -- the annotation is altogether missing, set the global flag which
5160 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5161 -- the instance.
5163 if SPARK_Mode /= On then
5164 Ignore_Pragma_SPARK_Mode := True;
5165 end if;
5167 Set_Entity (Gen_Id, Gen_Unit);
5168 Set_Is_Instantiated (Gen_Unit);
5170 if In_Extended_Main_Source_Unit (N) then
5171 Generate_Reference (Gen_Unit, N);
5172 end if;
5174 -- If renaming, get original unit
5176 if Present (Renamed_Object (Gen_Unit))
5177 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5178 E_Generic_Function)
5179 then
5180 Gen_Unit := Renamed_Object (Gen_Unit);
5181 Set_Is_Instantiated (Gen_Unit);
5182 Generate_Reference (Gen_Unit, N);
5183 end if;
5185 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5186 Error_Msg_Node_2 := Current_Scope;
5187 Error_Msg_NE
5188 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5189 Circularity_Detected := True;
5190 Restore_Hidden_Primitives (Vis_Prims_List);
5191 goto Leave;
5192 end if;
5194 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5196 -- Initialize renamings map, for error checking
5198 Generic_Renamings.Set_Last (0);
5199 Generic_Renamings_HTable.Reset;
5201 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5203 -- Copy original generic tree, to produce text for instantiation
5205 Act_Tree :=
5206 Copy_Generic_Node
5207 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5209 -- Inherit overriding indicator from instance node
5211 Act_Spec := Specification (Act_Tree);
5212 Set_Must_Override (Act_Spec, Must_Override (N));
5213 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5215 Renaming_List :=
5216 Analyze_Associations
5217 (I_Node => N,
5218 Formals => Generic_Formal_Declarations (Act_Tree),
5219 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5221 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5223 -- The subprogram itself cannot contain a nested instance, so the
5224 -- current parent is left empty.
5226 Set_Instance_Env (Gen_Unit, Empty);
5228 -- Build the subprogram declaration, which does not appear in the
5229 -- generic template, and give it a sloc consistent with that of the
5230 -- template.
5232 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5233 Set_Generic_Parent (Act_Spec, Gen_Unit);
5234 Act_Decl :=
5235 Make_Subprogram_Declaration (Sloc (Act_Spec),
5236 Specification => Act_Spec);
5238 -- The aspects have been copied previously, but they have to be
5239 -- linked explicitly to the new subprogram declaration. Explicit
5240 -- pre/postconditions on the instance are analyzed below, in a
5241 -- separate step.
5243 Move_Aspects (Act_Tree, To => Act_Decl);
5244 Set_Categorization_From_Pragmas (Act_Decl);
5246 if Parent_Installed then
5247 Hide_Current_Scope;
5248 end if;
5250 Append (Act_Decl, Renaming_List);
5252 -- Contract-related source pragmas that follow a generic subprogram
5253 -- must be instantiated explicitly because they are not part of the
5254 -- subprogram template.
5256 Instantiate_Subprogram_Contract
5257 (Original_Node (Gen_Decl), Renaming_List);
5259 Build_Subprogram_Renaming;
5260 Analyze_Instance_And_Renamings;
5262 -- If the generic is marked Import (Intrinsic), then so is the
5263 -- instance. This indicates that there is no body to instantiate. If
5264 -- generic is marked inline, so it the instance, and the anonymous
5265 -- subprogram it renames. If inlined, or else if inlining is enabled
5266 -- for the compilation, we generate the instance body even if it is
5267 -- not within the main unit.
5269 if Is_Intrinsic_Subprogram (Gen_Unit) then
5270 Set_Is_Intrinsic_Subprogram (Anon_Id);
5271 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5273 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5274 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5275 end if;
5276 end if;
5278 -- Inherit convention from generic unit. Intrinsic convention, as for
5279 -- an instance of unchecked conversion, is not inherited because an
5280 -- explicit Ada instance has been created.
5282 if Has_Convention_Pragma (Gen_Unit)
5283 and then Convention (Gen_Unit) /= Convention_Intrinsic
5284 then
5285 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5286 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5287 end if;
5289 Generate_Definition (Act_Decl_Id);
5291 -- Inherit all inlining-related flags which apply to the generic in
5292 -- the subprogram and its declaration.
5294 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5295 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5297 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5298 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5300 Set_Has_Pragma_Inline_Always
5301 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5302 Set_Has_Pragma_Inline_Always
5303 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5305 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5306 Check_Elab_Instantiation (N);
5307 end if;
5309 if Is_Dispatching_Operation (Act_Decl_Id)
5310 and then Ada_Version >= Ada_2005
5311 then
5312 declare
5313 Formal : Entity_Id;
5315 begin
5316 Formal := First_Formal (Act_Decl_Id);
5317 while Present (Formal) loop
5318 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5319 and then Is_Controlling_Formal (Formal)
5320 and then not Can_Never_Be_Null (Formal)
5321 then
5322 Error_Msg_NE
5323 ("access parameter& is controlling,", N, Formal);
5324 Error_Msg_NE
5325 ("\corresponding parameter of & must be "
5326 & "explicitly null-excluding", N, Gen_Id);
5327 end if;
5329 Next_Formal (Formal);
5330 end loop;
5331 end;
5332 end if;
5334 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5336 Validate_Categorization_Dependency (N, Act_Decl_Id);
5338 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5339 Inherit_Context (Gen_Decl, N);
5341 Restore_Private_Views (Pack_Id, False);
5343 -- If the context requires a full instantiation, mark node for
5344 -- subsequent construction of the body.
5346 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5347 Check_Forward_Instantiation (Gen_Decl);
5349 -- The wrapper package is always delayed, because it does not
5350 -- constitute a freeze point, but to insure that the freeze node
5351 -- is placed properly, it is created directly when instantiating
5352 -- the body (otherwise the freeze node might appear to early for
5353 -- nested instantiations). For ASIS purposes, indicate that the
5354 -- wrapper package has replaced the instantiation node.
5356 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5357 Rewrite (N, Unit (Parent (N)));
5358 Set_Unit (Parent (N), N);
5359 end if;
5361 -- Replace instance node for library-level instantiations of
5362 -- intrinsic subprograms, for ASIS use.
5364 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5365 Rewrite (N, Unit (Parent (N)));
5366 Set_Unit (Parent (N), N);
5367 end if;
5369 if Parent_Installed then
5370 Remove_Parent;
5371 end if;
5373 Restore_Hidden_Primitives (Vis_Prims_List);
5374 Restore_Env;
5375 Env_Installed := False;
5376 Generic_Renamings.Set_Last (0);
5377 Generic_Renamings_HTable.Reset;
5379 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5380 SPARK_Mode := Save_SM;
5381 SPARK_Mode_Pragma := Save_SMP;
5382 end if;
5384 <<Leave>>
5385 if Has_Aspects (N) then
5386 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5387 end if;
5389 exception
5390 when Instantiation_Error =>
5391 if Parent_Installed then
5392 Remove_Parent;
5393 end if;
5395 if Env_Installed then
5396 Restore_Env;
5397 end if;
5399 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5400 SPARK_Mode := Save_SM;
5401 SPARK_Mode_Pragma := Save_SMP;
5402 end Analyze_Subprogram_Instantiation;
5404 -------------------------
5405 -- Get_Associated_Node --
5406 -------------------------
5408 function Get_Associated_Node (N : Node_Id) return Node_Id is
5409 Assoc : Node_Id;
5411 begin
5412 Assoc := Associated_Node (N);
5414 if Nkind (Assoc) /= Nkind (N) then
5415 return Assoc;
5417 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5418 return Assoc;
5420 else
5421 -- If the node is part of an inner generic, it may itself have been
5422 -- remapped into a further generic copy. Associated_Node is otherwise
5423 -- used for the entity of the node, and will be of a different node
5424 -- kind, or else N has been rewritten as a literal or function call.
5426 while Present (Associated_Node (Assoc))
5427 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5428 loop
5429 Assoc := Associated_Node (Assoc);
5430 end loop;
5432 -- Follow and additional link in case the final node was rewritten.
5433 -- This can only happen with nested generic units.
5435 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5436 and then Present (Associated_Node (Assoc))
5437 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5438 N_Explicit_Dereference,
5439 N_Integer_Literal,
5440 N_Real_Literal,
5441 N_String_Literal))
5442 then
5443 Assoc := Associated_Node (Assoc);
5444 end if;
5446 -- An additional special case: an unconstrained type in an object
5447 -- declaration may have been rewritten as a local subtype constrained
5448 -- by the expression in the declaration. We need to recover the
5449 -- original entity which may be global.
5451 if Present (Original_Node (Assoc))
5452 and then Nkind (Parent (N)) = N_Object_Declaration
5453 then
5454 Assoc := Original_Node (Assoc);
5455 end if;
5457 return Assoc;
5458 end if;
5459 end Get_Associated_Node;
5461 ----------------------------
5462 -- Build_Function_Wrapper --
5463 ----------------------------
5465 function Build_Function_Wrapper
5466 (Formal_Subp : Entity_Id;
5467 Actual_Subp : Entity_Id) return Node_Id
5469 Loc : constant Source_Ptr := Sloc (Current_Scope);
5470 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5471 Actuals : List_Id;
5472 Decl : Node_Id;
5473 Func_Name : Node_Id;
5474 Func : Entity_Id;
5475 Parm_Type : Node_Id;
5476 Profile : List_Id := New_List;
5477 Spec : Node_Id;
5478 Act_F : Entity_Id;
5479 Form_F : Entity_Id;
5480 New_F : Entity_Id;
5482 begin
5483 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5485 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5486 Set_Ekind (Func, E_Function);
5487 Set_Is_Generic_Actual_Subprogram (Func);
5489 Actuals := New_List;
5490 Profile := New_List;
5492 Act_F := First_Formal (Actual_Subp);
5493 Form_F := First_Formal (Formal_Subp);
5494 while Present (Form_F) loop
5496 -- Create new formal for profile of wrapper, and add a reference
5497 -- to it in the list of actuals for the enclosing call. The name
5498 -- must be that of the formal in the formal subprogram, because
5499 -- calls to it in the generic body may use named associations.
5501 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5503 Parm_Type :=
5504 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5506 Append_To (Profile,
5507 Make_Parameter_Specification (Loc,
5508 Defining_Identifier => New_F,
5509 Parameter_Type => Parm_Type));
5511 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5512 Next_Formal (Form_F);
5514 if Present (Act_F) then
5515 Next_Formal (Act_F);
5516 end if;
5517 end loop;
5519 Spec :=
5520 Make_Function_Specification (Loc,
5521 Defining_Unit_Name => Func,
5522 Parameter_Specifications => Profile,
5523 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5525 Decl :=
5526 Make_Expression_Function (Loc,
5527 Specification => Spec,
5528 Expression =>
5529 Make_Function_Call (Loc,
5530 Name => Func_Name,
5531 Parameter_Associations => Actuals));
5533 return Decl;
5534 end Build_Function_Wrapper;
5536 ----------------------------
5537 -- Build_Operator_Wrapper --
5538 ----------------------------
5540 function Build_Operator_Wrapper
5541 (Formal_Subp : Entity_Id;
5542 Actual_Subp : Entity_Id) return Node_Id
5544 Loc : constant Source_Ptr := Sloc (Current_Scope);
5545 Ret_Type : constant Entity_Id :=
5546 Get_Instance_Of (Etype (Formal_Subp));
5547 Op_Type : constant Entity_Id :=
5548 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5549 Is_Binary : constant Boolean :=
5550 Present (Next_Formal (First_Formal (Formal_Subp)));
5552 Decl : Node_Id;
5553 Expr : Node_Id;
5554 F1, F2 : Entity_Id;
5555 Func : Entity_Id;
5556 Op_Name : Name_Id;
5557 Spec : Node_Id;
5558 L, R : Node_Id;
5560 begin
5561 Op_Name := Chars (Actual_Subp);
5563 -- Create entities for wrapper function and its formals
5565 F1 := Make_Temporary (Loc, 'A');
5566 F2 := Make_Temporary (Loc, 'B');
5567 L := New_Occurrence_Of (F1, Loc);
5568 R := New_Occurrence_Of (F2, Loc);
5570 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5571 Set_Ekind (Func, E_Function);
5572 Set_Is_Generic_Actual_Subprogram (Func);
5574 Spec :=
5575 Make_Function_Specification (Loc,
5576 Defining_Unit_Name => Func,
5577 Parameter_Specifications => New_List (
5578 Make_Parameter_Specification (Loc,
5579 Defining_Identifier => F1,
5580 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5581 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5583 if Is_Binary then
5584 Append_To (Parameter_Specifications (Spec),
5585 Make_Parameter_Specification (Loc,
5586 Defining_Identifier => F2,
5587 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5588 end if;
5590 -- Build expression as a function call, or as an operator node
5591 -- that corresponds to the name of the actual, starting with
5592 -- binary operators.
5594 if Op_Name not in Any_Operator_Name then
5595 Expr :=
5596 Make_Function_Call (Loc,
5597 Name =>
5598 New_Occurrence_Of (Actual_Subp, Loc),
5599 Parameter_Associations => New_List (L));
5601 if Is_Binary then
5602 Append_To (Parameter_Associations (Expr), R);
5603 end if;
5605 -- Binary operators
5607 elsif Is_Binary then
5608 if Op_Name = Name_Op_And then
5609 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5610 elsif Op_Name = Name_Op_Or then
5611 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5612 elsif Op_Name = Name_Op_Xor then
5613 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5614 elsif Op_Name = Name_Op_Eq then
5615 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5616 elsif Op_Name = Name_Op_Ne then
5617 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5618 elsif Op_Name = Name_Op_Le then
5619 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5620 elsif Op_Name = Name_Op_Gt then
5621 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5622 elsif Op_Name = Name_Op_Ge then
5623 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5624 elsif Op_Name = Name_Op_Lt then
5625 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5626 elsif Op_Name = Name_Op_Add then
5627 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5628 elsif Op_Name = Name_Op_Subtract then
5629 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5630 elsif Op_Name = Name_Op_Concat then
5631 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5632 elsif Op_Name = Name_Op_Multiply then
5633 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5634 elsif Op_Name = Name_Op_Divide then
5635 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5636 elsif Op_Name = Name_Op_Mod then
5637 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5638 elsif Op_Name = Name_Op_Rem then
5639 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5640 elsif Op_Name = Name_Op_Expon then
5641 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5642 end if;
5644 -- Unary operators
5646 else
5647 if Op_Name = Name_Op_Add then
5648 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5649 elsif Op_Name = Name_Op_Subtract then
5650 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5651 elsif Op_Name = Name_Op_Abs then
5652 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5653 elsif Op_Name = Name_Op_Not then
5654 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5655 end if;
5656 end if;
5658 Decl :=
5659 Make_Expression_Function (Loc,
5660 Specification => Spec,
5661 Expression => Expr);
5663 return Decl;
5664 end Build_Operator_Wrapper;
5666 -------------------------------------------
5667 -- Build_Instance_Compilation_Unit_Nodes --
5668 -------------------------------------------
5670 procedure Build_Instance_Compilation_Unit_Nodes
5671 (N : Node_Id;
5672 Act_Body : Node_Id;
5673 Act_Decl : Node_Id)
5675 Decl_Cunit : Node_Id;
5676 Body_Cunit : Node_Id;
5677 Citem : Node_Id;
5678 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5679 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5681 begin
5682 -- A new compilation unit node is built for the instance declaration
5684 Decl_Cunit :=
5685 Make_Compilation_Unit (Sloc (N),
5686 Context_Items => Empty_List,
5687 Unit => Act_Decl,
5688 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5690 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5692 -- The new compilation unit is linked to its body, but both share the
5693 -- same file, so we do not set Body_Required on the new unit so as not
5694 -- to create a spurious dependency on a non-existent body in the ali.
5695 -- This simplifies CodePeer unit traversal.
5697 -- We use the original instantiation compilation unit as the resulting
5698 -- compilation unit of the instance, since this is the main unit.
5700 Rewrite (N, Act_Body);
5702 -- Propagate the aspect specifications from the package body template to
5703 -- the instantiated version of the package body.
5705 if Has_Aspects (Act_Body) then
5706 Set_Aspect_Specifications
5707 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5708 end if;
5710 Body_Cunit := Parent (N);
5712 -- The two compilation unit nodes are linked by the Library_Unit field
5714 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5715 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5717 -- Preserve the private nature of the package if needed
5719 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5721 -- If the instance is not the main unit, its context, categorization
5722 -- and elaboration entity are not relevant to the compilation.
5724 if Body_Cunit /= Cunit (Main_Unit) then
5725 Make_Instance_Unit (Body_Cunit, In_Main => False);
5726 return;
5727 end if;
5729 -- The context clause items on the instantiation, which are now attached
5730 -- to the body compilation unit (since the body overwrote the original
5731 -- instantiation node), semantically belong on the spec, so copy them
5732 -- there. It's harmless to leave them on the body as well. In fact one
5733 -- could argue that they belong in both places.
5735 Citem := First (Context_Items (Body_Cunit));
5736 while Present (Citem) loop
5737 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5738 Next (Citem);
5739 end loop;
5741 -- Propagate categorization flags on packages, so that they appear in
5742 -- the ali file for the spec of the unit.
5744 if Ekind (New_Main) = E_Package then
5745 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5746 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5747 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5748 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5749 Set_Is_Remote_Call_Interface
5750 (Old_Main, Is_Remote_Call_Interface (New_Main));
5751 end if;
5753 -- Make entry in Units table, so that binder can generate call to
5754 -- elaboration procedure for body, if any.
5756 Make_Instance_Unit (Body_Cunit, In_Main => True);
5757 Main_Unit_Entity := New_Main;
5758 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5760 -- Build elaboration entity, since the instance may certainly generate
5761 -- elaboration code requiring a flag for protection.
5763 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5764 end Build_Instance_Compilation_Unit_Nodes;
5766 -----------------------------
5767 -- Check_Access_Definition --
5768 -----------------------------
5770 procedure Check_Access_Definition (N : Node_Id) is
5771 begin
5772 pragma Assert
5773 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5774 null;
5775 end Check_Access_Definition;
5777 -----------------------------------
5778 -- Check_Formal_Package_Instance --
5779 -----------------------------------
5781 -- If the formal has specific parameters, they must match those of the
5782 -- actual. Both of them are instances, and the renaming declarations for
5783 -- their formal parameters appear in the same order in both. The analyzed
5784 -- formal has been analyzed in the context of the current instance.
5786 procedure Check_Formal_Package_Instance
5787 (Formal_Pack : Entity_Id;
5788 Actual_Pack : Entity_Id)
5790 E1 : Entity_Id := First_Entity (Actual_Pack);
5791 E2 : Entity_Id := First_Entity (Formal_Pack);
5792 Prev_E1 : Entity_Id;
5794 Expr1 : Node_Id;
5795 Expr2 : Node_Id;
5797 procedure Check_Mismatch (B : Boolean);
5798 -- Common error routine for mismatch between the parameters of the
5799 -- actual instance and those of the formal package.
5801 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5802 -- The formal may come from a nested formal package, and the actual may
5803 -- have been constant-folded. To determine whether the two denote the
5804 -- same entity we may have to traverse several definitions to recover
5805 -- the ultimate entity that they refer to.
5807 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5808 -- The formal and the actual must be identical, but if both are
5809 -- given by attributes they end up renaming different generated bodies,
5810 -- and we must verify that the attributes themselves match.
5812 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5813 -- Similarly, if the formal comes from a nested formal package, the
5814 -- actual may designate the formal through multiple renamings, which
5815 -- have to be followed to determine the original variable in question.
5817 --------------------
5818 -- Check_Mismatch --
5819 --------------------
5821 procedure Check_Mismatch (B : Boolean) is
5822 -- A Formal_Type_Declaration for a derived private type is rewritten
5823 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5824 -- which is why we examine the original node.
5826 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5828 begin
5829 if Kind = N_Formal_Type_Declaration then
5830 return;
5832 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5833 N_Formal_Package_Declaration)
5834 or else Kind in N_Formal_Subprogram_Declaration
5835 then
5836 null;
5838 -- Ada 2012: If both formal and actual are incomplete types they
5839 -- are conformant.
5841 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5842 null;
5844 elsif B then
5845 Error_Msg_NE
5846 ("actual for & in actual instance does not match formal",
5847 Parent (Actual_Pack), E1);
5848 end if;
5849 end Check_Mismatch;
5851 --------------------------------
5852 -- Same_Instantiated_Constant --
5853 --------------------------------
5855 function Same_Instantiated_Constant
5856 (E1, E2 : Entity_Id) return Boolean
5858 Ent : Entity_Id;
5860 begin
5861 Ent := E2;
5862 while Present (Ent) loop
5863 if E1 = Ent then
5864 return True;
5866 elsif Ekind (Ent) /= E_Constant then
5867 return False;
5869 elsif Is_Entity_Name (Constant_Value (Ent)) then
5870 if Entity (Constant_Value (Ent)) = E1 then
5871 return True;
5872 else
5873 Ent := Entity (Constant_Value (Ent));
5874 end if;
5876 -- The actual may be a constant that has been folded. Recover
5877 -- original name.
5879 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5880 Ent := Entity (Original_Node (Constant_Value (Ent)));
5882 else
5883 return False;
5884 end if;
5885 end loop;
5887 return False;
5888 end Same_Instantiated_Constant;
5890 --------------------------------
5891 -- Same_Instantiated_Function --
5892 --------------------------------
5894 function Same_Instantiated_Function
5895 (E1, E2 : Entity_Id) return Boolean
5897 U1, U2 : Node_Id;
5898 begin
5899 if Alias (E1) = Alias (E2) then
5900 return True;
5902 elsif Present (Alias (E2)) then
5903 U1 := Original_Node (Unit_Declaration_Node (E1));
5904 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5906 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5907 and then Nkind (Name (U1)) = N_Attribute_Reference
5909 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5910 and then Nkind (Name (U2)) = N_Attribute_Reference
5912 and then
5913 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5914 else
5915 return False;
5916 end if;
5917 end Same_Instantiated_Function;
5919 --------------------------------
5920 -- Same_Instantiated_Variable --
5921 --------------------------------
5923 function Same_Instantiated_Variable
5924 (E1, E2 : Entity_Id) return Boolean
5926 function Original_Entity (E : Entity_Id) return Entity_Id;
5927 -- Follow chain of renamings to the ultimate ancestor
5929 ---------------------
5930 -- Original_Entity --
5931 ---------------------
5933 function Original_Entity (E : Entity_Id) return Entity_Id is
5934 Orig : Entity_Id;
5936 begin
5937 Orig := E;
5938 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5939 and then Present (Renamed_Object (Orig))
5940 and then Is_Entity_Name (Renamed_Object (Orig))
5941 loop
5942 Orig := Entity (Renamed_Object (Orig));
5943 end loop;
5945 return Orig;
5946 end Original_Entity;
5948 -- Start of processing for Same_Instantiated_Variable
5950 begin
5951 return Ekind (E1) = Ekind (E2)
5952 and then Original_Entity (E1) = Original_Entity (E2);
5953 end Same_Instantiated_Variable;
5955 -- Start of processing for Check_Formal_Package_Instance
5957 begin
5958 Prev_E1 := E1;
5959 while Present (E1) and then Present (E2) loop
5960 exit when Ekind (E1) = E_Package
5961 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5963 -- If the formal is the renaming of the formal package, this
5964 -- is the end of its formal part, which may occur before the
5965 -- end of the formal part in the actual in the presence of
5966 -- defaulted parameters in the formal package.
5968 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5969 and then Renamed_Entity (E2) = Scope (E2);
5971 -- The analysis of the actual may generate additional internal
5972 -- entities. If the formal is defaulted, there is no corresponding
5973 -- analysis and the internal entities must be skipped, until we
5974 -- find corresponding entities again.
5976 if Comes_From_Source (E2)
5977 and then not Comes_From_Source (E1)
5978 and then Chars (E1) /= Chars (E2)
5979 then
5980 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5981 Next_Entity (E1);
5982 end loop;
5983 end if;
5985 if No (E1) then
5986 return;
5988 -- Entities may be declared without full declaration, such as
5989 -- itypes and predefined operators (concatenation for arrays, eg).
5990 -- Skip it and keep the formal entity to find a later match for it.
5992 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
5993 E1 := Prev_E1;
5994 goto Next_E;
5996 -- If the formal entity comes from a formal declaration, it was
5997 -- defaulted in the formal package, and no check is needed on it.
5999 elsif Nkind_In (Original_Node (Parent (E2)),
6000 N_Formal_Object_Declaration,
6001 N_Formal_Type_Declaration)
6002 then
6003 -- If the formal is a tagged type the corresponding class-wide
6004 -- type has been generated as well, and it must be skipped.
6006 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6007 Next_Entity (E2);
6008 end if;
6010 goto Next_E;
6012 -- Ditto for defaulted formal subprograms.
6014 elsif Is_Overloadable (E1)
6015 and then Nkind (Unit_Declaration_Node (E2)) in
6016 N_Formal_Subprogram_Declaration
6017 then
6018 goto Next_E;
6020 elsif Is_Type (E1) then
6022 -- Subtypes must statically match. E1, E2 are the local entities
6023 -- that are subtypes of the actuals. Itypes generated for other
6024 -- parameters need not be checked, the check will be performed
6025 -- on the parameters themselves.
6027 -- If E2 is a formal type declaration, it is a defaulted parameter
6028 -- and needs no checking.
6030 if not Is_Itype (E1) and then not Is_Itype (E2) then
6031 Check_Mismatch
6032 (not Is_Type (E2)
6033 or else Etype (E1) /= Etype (E2)
6034 or else not Subtypes_Statically_Match (E1, E2));
6035 end if;
6037 elsif Ekind (E1) = E_Constant then
6039 -- IN parameters must denote the same static value, or the same
6040 -- constant, or the literal null.
6042 Expr1 := Expression (Parent (E1));
6044 if Ekind (E2) /= E_Constant then
6045 Check_Mismatch (True);
6046 goto Next_E;
6047 else
6048 Expr2 := Expression (Parent (E2));
6049 end if;
6051 if Is_OK_Static_Expression (Expr1) then
6052 if not Is_OK_Static_Expression (Expr2) then
6053 Check_Mismatch (True);
6055 elsif Is_Discrete_Type (Etype (E1)) then
6056 declare
6057 V1 : constant Uint := Expr_Value (Expr1);
6058 V2 : constant Uint := Expr_Value (Expr2);
6059 begin
6060 Check_Mismatch (V1 /= V2);
6061 end;
6063 elsif Is_Real_Type (Etype (E1)) then
6064 declare
6065 V1 : constant Ureal := Expr_Value_R (Expr1);
6066 V2 : constant Ureal := Expr_Value_R (Expr2);
6067 begin
6068 Check_Mismatch (V1 /= V2);
6069 end;
6071 elsif Is_String_Type (Etype (E1))
6072 and then Nkind (Expr1) = N_String_Literal
6073 then
6074 if Nkind (Expr2) /= N_String_Literal then
6075 Check_Mismatch (True);
6076 else
6077 Check_Mismatch
6078 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6079 end if;
6080 end if;
6082 elsif Is_Entity_Name (Expr1) then
6083 if Is_Entity_Name (Expr2) then
6084 if Entity (Expr1) = Entity (Expr2) then
6085 null;
6086 else
6087 Check_Mismatch
6088 (not Same_Instantiated_Constant
6089 (Entity (Expr1), Entity (Expr2)));
6090 end if;
6092 else
6093 Check_Mismatch (True);
6094 end if;
6096 elsif Is_Entity_Name (Original_Node (Expr1))
6097 and then Is_Entity_Name (Expr2)
6098 and then Same_Instantiated_Constant
6099 (Entity (Original_Node (Expr1)), Entity (Expr2))
6100 then
6101 null;
6103 elsif Nkind (Expr1) = N_Null then
6104 Check_Mismatch (Nkind (Expr1) /= N_Null);
6106 else
6107 Check_Mismatch (True);
6108 end if;
6110 elsif Ekind (E1) = E_Variable then
6111 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6113 elsif Ekind (E1) = E_Package then
6114 Check_Mismatch
6115 (Ekind (E1) /= Ekind (E2)
6116 or else Renamed_Object (E1) /= Renamed_Object (E2));
6118 elsif Is_Overloadable (E1) then
6120 -- Verify that the actual subprograms match. Note that actuals
6121 -- that are attributes are rewritten as subprograms. If the
6122 -- subprogram in the formal package is defaulted, no check is
6123 -- needed. Note that this can only happen in Ada 2005 when the
6124 -- formal package can be partially parameterized.
6126 if Nkind (Unit_Declaration_Node (E1)) =
6127 N_Subprogram_Renaming_Declaration
6128 and then From_Default (Unit_Declaration_Node (E1))
6129 then
6130 null;
6132 -- If the formal package has an "others" box association that
6133 -- covers this formal, there is no need for a check either.
6135 elsif Nkind (Unit_Declaration_Node (E2)) in
6136 N_Formal_Subprogram_Declaration
6137 and then Box_Present (Unit_Declaration_Node (E2))
6138 then
6139 null;
6141 -- No check needed if subprogram is a defaulted null procedure
6143 elsif No (Alias (E2))
6144 and then Ekind (E2) = E_Procedure
6145 and then
6146 Null_Present (Specification (Unit_Declaration_Node (E2)))
6147 then
6148 null;
6150 -- Otherwise the actual in the formal and the actual in the
6151 -- instantiation of the formal must match, up to renamings.
6153 else
6154 Check_Mismatch
6155 (Ekind (E2) /= Ekind (E1)
6156 or else not Same_Instantiated_Function (E1, E2));
6157 end if;
6159 else
6160 raise Program_Error;
6161 end if;
6163 <<Next_E>>
6164 Prev_E1 := E1;
6165 Next_Entity (E1);
6166 Next_Entity (E2);
6167 end loop;
6168 end Check_Formal_Package_Instance;
6170 ---------------------------
6171 -- Check_Formal_Packages --
6172 ---------------------------
6174 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6175 E : Entity_Id;
6176 Formal_P : Entity_Id;
6177 Formal_Decl : Node_Id;
6179 begin
6180 -- Iterate through the declarations in the instance, looking for package
6181 -- renaming declarations that denote instances of formal packages. Stop
6182 -- when we find the renaming of the current package itself. The
6183 -- declaration for a formal package without a box is followed by an
6184 -- internal entity that repeats the instantiation.
6186 E := First_Entity (P_Id);
6187 while Present (E) loop
6188 if Ekind (E) = E_Package then
6189 if Renamed_Object (E) = P_Id then
6190 exit;
6192 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6193 null;
6195 else
6196 Formal_Decl := Parent (Associated_Formal_Package (E));
6198 -- Nothing to check if the formal has a box or an others_clause
6199 -- (necessarily with a box).
6201 if Box_Present (Formal_Decl) then
6202 null;
6204 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6205 N_Others_Choice
6206 then
6207 -- The internal validating package was generated but formal
6208 -- and instance are known to be compatible.
6210 Formal_P := Next_Entity (E);
6211 Remove (Unit_Declaration_Node (Formal_P));
6213 else
6214 Formal_P := Next_Entity (E);
6215 Check_Formal_Package_Instance (Formal_P, E);
6217 -- After checking, remove the internal validating package.
6218 -- It is only needed for semantic checks, and as it may
6219 -- contain generic formal declarations it should not reach
6220 -- gigi.
6222 Remove (Unit_Declaration_Node (Formal_P));
6223 end if;
6224 end if;
6225 end if;
6227 Next_Entity (E);
6228 end loop;
6229 end Check_Formal_Packages;
6231 ---------------------------------
6232 -- Check_Forward_Instantiation --
6233 ---------------------------------
6235 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6236 S : Entity_Id;
6237 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6239 begin
6240 -- The instantiation appears before the generic body if we are in the
6241 -- scope of the unit containing the generic, either in its spec or in
6242 -- the package body, and before the generic body.
6244 if Ekind (Gen_Comp) = E_Package_Body then
6245 Gen_Comp := Spec_Entity (Gen_Comp);
6246 end if;
6248 if In_Open_Scopes (Gen_Comp)
6249 and then No (Corresponding_Body (Decl))
6250 then
6251 S := Current_Scope;
6253 while Present (S)
6254 and then not Is_Compilation_Unit (S)
6255 and then not Is_Child_Unit (S)
6256 loop
6257 if Ekind (S) = E_Package then
6258 Set_Has_Forward_Instantiation (S);
6259 end if;
6261 S := Scope (S);
6262 end loop;
6263 end if;
6264 end Check_Forward_Instantiation;
6266 ---------------------------
6267 -- Check_Generic_Actuals --
6268 ---------------------------
6270 -- The visibility of the actuals may be different between the point of
6271 -- generic instantiation and the instantiation of the body.
6273 procedure Check_Generic_Actuals
6274 (Instance : Entity_Id;
6275 Is_Formal_Box : Boolean)
6277 E : Entity_Id;
6278 Astype : Entity_Id;
6280 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6281 -- For a formal that is an array type, the component type is often a
6282 -- previous formal in the same unit. The privacy status of the component
6283 -- type will have been examined earlier in the traversal of the
6284 -- corresponding actuals, and this status should not be modified for
6285 -- the array (sub)type itself. However, if the base type of the array
6286 -- (sub)type is private, its full view must be restored in the body to
6287 -- be consistent with subsequent index subtypes, etc.
6289 -- To detect this case we have to rescan the list of formals, which is
6290 -- usually short enough to ignore the resulting inefficiency.
6292 -----------------------------
6293 -- Denotes_Previous_Actual --
6294 -----------------------------
6296 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6297 Prev : Entity_Id;
6299 begin
6300 Prev := First_Entity (Instance);
6301 while Present (Prev) loop
6302 if Is_Type (Prev)
6303 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6304 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6305 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6306 then
6307 return True;
6309 elsif Prev = E then
6310 return False;
6312 else
6313 Next_Entity (Prev);
6314 end if;
6315 end loop;
6317 return False;
6318 end Denotes_Previous_Actual;
6320 -- Start of processing for Check_Generic_Actuals
6322 begin
6323 E := First_Entity (Instance);
6324 while Present (E) loop
6325 if Is_Type (E)
6326 and then Nkind (Parent (E)) = N_Subtype_Declaration
6327 and then Scope (Etype (E)) /= Instance
6328 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6329 then
6330 if Is_Array_Type (E)
6331 and then not Is_Private_Type (Etype (E))
6332 and then Denotes_Previous_Actual (Component_Type (E))
6333 then
6334 null;
6335 else
6336 Check_Private_View (Subtype_Indication (Parent (E)));
6337 end if;
6339 Set_Is_Generic_Actual_Type (E, True);
6340 Set_Is_Hidden (E, False);
6341 Set_Is_Potentially_Use_Visible (E,
6342 In_Use (Instance));
6344 -- We constructed the generic actual type as a subtype of the
6345 -- supplied type. This means that it normally would not inherit
6346 -- subtype specific attributes of the actual, which is wrong for
6347 -- the generic case.
6349 Astype := Ancestor_Subtype (E);
6351 if No (Astype) then
6353 -- This can happen when E is an itype that is the full view of
6354 -- a private type completed, e.g. with a constrained array. In
6355 -- that case, use the first subtype, which will carry size
6356 -- information. The base type itself is unconstrained and will
6357 -- not carry it.
6359 Astype := First_Subtype (E);
6360 end if;
6362 Set_Size_Info (E, (Astype));
6363 Set_RM_Size (E, RM_Size (Astype));
6364 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6366 if Is_Discrete_Or_Fixed_Point_Type (E) then
6367 Set_RM_Size (E, RM_Size (Astype));
6369 -- In nested instances, the base type of an access actual may
6370 -- itself be private, and need to be exchanged.
6372 elsif Is_Access_Type (E)
6373 and then Is_Private_Type (Etype (E))
6374 then
6375 Check_Private_View
6376 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6377 end if;
6379 elsif Ekind (E) = E_Package then
6381 -- If this is the renaming for the current instance, we're done.
6382 -- Otherwise it is a formal package. If the corresponding formal
6383 -- was declared with a box, the (instantiations of the) generic
6384 -- formal part are also visible. Otherwise, ignore the entity
6385 -- created to validate the actuals.
6387 if Renamed_Object (E) = Instance then
6388 exit;
6390 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6391 null;
6393 -- The visibility of a formal of an enclosing generic is already
6394 -- correct.
6396 elsif Denotes_Formal_Package (E) then
6397 null;
6399 elsif Present (Associated_Formal_Package (E))
6400 and then not Is_Generic_Formal (E)
6401 then
6402 if Box_Present (Parent (Associated_Formal_Package (E))) then
6403 Check_Generic_Actuals (Renamed_Object (E), True);
6405 else
6406 Check_Generic_Actuals (Renamed_Object (E), False);
6407 end if;
6409 Set_Is_Hidden (E, False);
6410 end if;
6412 -- If this is a subprogram instance (in a wrapper package) the
6413 -- actual is fully visible.
6415 elsif Is_Wrapper_Package (Instance) then
6416 Set_Is_Hidden (E, False);
6418 -- If the formal package is declared with a box, or if the formal
6419 -- parameter is defaulted, it is visible in the body.
6421 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6422 Set_Is_Hidden (E, False);
6423 end if;
6425 if Ekind (E) = E_Constant then
6427 -- If the type of the actual is a private type declared in the
6428 -- enclosing scope of the generic unit, the body of the generic
6429 -- sees the full view of the type (because it has to appear in
6430 -- the corresponding package body). If the type is private now,
6431 -- exchange views to restore the proper visiblity in the instance.
6433 declare
6434 Typ : constant Entity_Id := Base_Type (Etype (E));
6435 -- The type of the actual
6437 Gen_Id : Entity_Id;
6438 -- The generic unit
6440 Parent_Scope : Entity_Id;
6441 -- The enclosing scope of the generic unit
6443 begin
6444 if Is_Wrapper_Package (Instance) then
6445 Gen_Id :=
6446 Generic_Parent
6447 (Specification
6448 (Unit_Declaration_Node
6449 (Related_Instance (Instance))));
6450 else
6451 Gen_Id :=
6452 Generic_Parent (Package_Specification (Instance));
6453 end if;
6455 Parent_Scope := Scope (Gen_Id);
6457 -- The exchange is only needed if the generic is defined
6458 -- within a package which is not a common ancestor of the
6459 -- scope of the instance, and is not already in scope.
6461 if Is_Private_Type (Typ)
6462 and then Scope (Typ) = Parent_Scope
6463 and then Scope (Instance) /= Parent_Scope
6464 and then Ekind (Parent_Scope) = E_Package
6465 and then not Is_Child_Unit (Gen_Id)
6466 then
6467 Switch_View (Typ);
6469 -- If the type of the entity is a subtype, it may also have
6470 -- to be made visible, together with the base type of its
6471 -- full view, after exchange.
6473 if Is_Private_Type (Etype (E)) then
6474 Switch_View (Etype (E));
6475 Switch_View (Base_Type (Etype (E)));
6476 end if;
6477 end if;
6478 end;
6479 end if;
6481 Next_Entity (E);
6482 end loop;
6483 end Check_Generic_Actuals;
6485 ------------------------------
6486 -- Check_Generic_Child_Unit --
6487 ------------------------------
6489 procedure Check_Generic_Child_Unit
6490 (Gen_Id : Node_Id;
6491 Parent_Installed : in out Boolean)
6493 Loc : constant Source_Ptr := Sloc (Gen_Id);
6494 Gen_Par : Entity_Id := Empty;
6495 E : Entity_Id;
6496 Inst_Par : Entity_Id;
6497 S : Node_Id;
6499 function Find_Generic_Child
6500 (Scop : Entity_Id;
6501 Id : Node_Id) return Entity_Id;
6502 -- Search generic parent for possible child unit with the given name
6504 function In_Enclosing_Instance return Boolean;
6505 -- Within an instance of the parent, the child unit may be denoted by
6506 -- a simple name, or an abbreviated expanded name. Examine enclosing
6507 -- scopes to locate a possible parent instantiation.
6509 ------------------------
6510 -- Find_Generic_Child --
6511 ------------------------
6513 function Find_Generic_Child
6514 (Scop : Entity_Id;
6515 Id : Node_Id) return Entity_Id
6517 E : Entity_Id;
6519 begin
6520 -- If entity of name is already set, instance has already been
6521 -- resolved, e.g. in an enclosing instantiation.
6523 if Present (Entity (Id)) then
6524 if Scope (Entity (Id)) = Scop then
6525 return Entity (Id);
6526 else
6527 return Empty;
6528 end if;
6530 else
6531 E := First_Entity (Scop);
6532 while Present (E) loop
6533 if Chars (E) = Chars (Id)
6534 and then Is_Child_Unit (E)
6535 then
6536 if Is_Child_Unit (E)
6537 and then not Is_Visible_Lib_Unit (E)
6538 then
6539 Error_Msg_NE
6540 ("generic child unit& is not visible", Gen_Id, E);
6541 end if;
6543 Set_Entity (Id, E);
6544 return E;
6545 end if;
6547 Next_Entity (E);
6548 end loop;
6550 return Empty;
6551 end if;
6552 end Find_Generic_Child;
6554 ---------------------------
6555 -- In_Enclosing_Instance --
6556 ---------------------------
6558 function In_Enclosing_Instance return Boolean is
6559 Enclosing_Instance : Node_Id;
6560 Instance_Decl : Node_Id;
6562 begin
6563 -- We do not inline any call that contains instantiations, except
6564 -- for instantiations of Unchecked_Conversion, so if we are within
6565 -- an inlined body the current instance does not require parents.
6567 if In_Inlined_Body then
6568 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6569 return False;
6570 end if;
6572 -- Loop to check enclosing scopes
6574 Enclosing_Instance := Current_Scope;
6575 while Present (Enclosing_Instance) loop
6576 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6578 if Ekind (Enclosing_Instance) = E_Package
6579 and then Is_Generic_Instance (Enclosing_Instance)
6580 and then Present
6581 (Generic_Parent (Specification (Instance_Decl)))
6582 then
6583 -- Check whether the generic we are looking for is a child of
6584 -- this instance.
6586 E := Find_Generic_Child
6587 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6588 exit when Present (E);
6590 else
6591 E := Empty;
6592 end if;
6594 Enclosing_Instance := Scope (Enclosing_Instance);
6595 end loop;
6597 if No (E) then
6599 -- Not a child unit
6601 Analyze (Gen_Id);
6602 return False;
6604 else
6605 Rewrite (Gen_Id,
6606 Make_Expanded_Name (Loc,
6607 Chars => Chars (E),
6608 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6609 Selector_Name => New_Occurrence_Of (E, Loc)));
6611 Set_Entity (Gen_Id, E);
6612 Set_Etype (Gen_Id, Etype (E));
6613 Parent_Installed := False; -- Already in scope.
6614 return True;
6615 end if;
6616 end In_Enclosing_Instance;
6618 -- Start of processing for Check_Generic_Child_Unit
6620 begin
6621 -- If the name of the generic is given by a selected component, it may
6622 -- be the name of a generic child unit, and the prefix is the name of an
6623 -- instance of the parent, in which case the child unit must be visible.
6624 -- If this instance is not in scope, it must be placed there and removed
6625 -- after instantiation, because what is being instantiated is not the
6626 -- original child, but the corresponding child present in the instance
6627 -- of the parent.
6629 -- If the child is instantiated within the parent, it can be given by
6630 -- a simple name. In this case the instance is already in scope, but
6631 -- the child generic must be recovered from the generic parent as well.
6633 if Nkind (Gen_Id) = N_Selected_Component then
6634 S := Selector_Name (Gen_Id);
6635 Analyze (Prefix (Gen_Id));
6636 Inst_Par := Entity (Prefix (Gen_Id));
6638 if Ekind (Inst_Par) = E_Package
6639 and then Present (Renamed_Object (Inst_Par))
6640 then
6641 Inst_Par := Renamed_Object (Inst_Par);
6642 end if;
6644 if Ekind (Inst_Par) = E_Package then
6645 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6646 Gen_Par := Generic_Parent (Parent (Inst_Par));
6648 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6649 and then
6650 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6651 then
6652 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6653 end if;
6655 elsif Ekind (Inst_Par) = E_Generic_Package
6656 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6657 then
6658 -- A formal package may be a real child package, and not the
6659 -- implicit instance within a parent. In this case the child is
6660 -- not visible and has to be retrieved explicitly as well.
6662 Gen_Par := Inst_Par;
6663 end if;
6665 if Present (Gen_Par) then
6667 -- The prefix denotes an instantiation. The entity itself may be a
6668 -- nested generic, or a child unit.
6670 E := Find_Generic_Child (Gen_Par, S);
6672 if Present (E) then
6673 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6674 Set_Entity (Gen_Id, E);
6675 Set_Etype (Gen_Id, Etype (E));
6676 Set_Entity (S, E);
6677 Set_Etype (S, Etype (E));
6679 -- Indicate that this is a reference to the parent
6681 if In_Extended_Main_Source_Unit (Gen_Id) then
6682 Set_Is_Instantiated (Inst_Par);
6683 end if;
6685 -- A common mistake is to replicate the naming scheme of a
6686 -- hierarchy by instantiating a generic child directly, rather
6687 -- than the implicit child in a parent instance:
6689 -- generic .. package Gpar is ..
6690 -- generic .. package Gpar.Child is ..
6691 -- package Par is new Gpar ();
6693 -- with Gpar.Child;
6694 -- package Par.Child is new Gpar.Child ();
6695 -- rather than Par.Child
6697 -- In this case the instantiation is within Par, which is an
6698 -- instance, but Gpar does not denote Par because we are not IN
6699 -- the instance of Gpar, so this is illegal. The test below
6700 -- recognizes this particular case.
6702 if Is_Child_Unit (E)
6703 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6704 and then (not In_Instance
6705 or else Nkind (Parent (Parent (Gen_Id))) =
6706 N_Compilation_Unit)
6707 then
6708 Error_Msg_N
6709 ("prefix of generic child unit must be instance of parent",
6710 Gen_Id);
6711 end if;
6713 if not In_Open_Scopes (Inst_Par)
6714 and then Nkind (Parent (Gen_Id)) not in
6715 N_Generic_Renaming_Declaration
6716 then
6717 Install_Parent (Inst_Par);
6718 Parent_Installed := True;
6720 elsif In_Open_Scopes (Inst_Par) then
6722 -- If the parent is already installed, install the actuals
6723 -- for its formal packages. This is necessary when the child
6724 -- instance is a child of the parent instance: in this case,
6725 -- the parent is placed on the scope stack but the formal
6726 -- packages are not made visible.
6728 Install_Formal_Packages (Inst_Par);
6729 end if;
6731 else
6732 -- If the generic parent does not contain an entity that
6733 -- corresponds to the selector, the instance doesn't either.
6734 -- Analyzing the node will yield the appropriate error message.
6735 -- If the entity is not a child unit, then it is an inner
6736 -- generic in the parent.
6738 Analyze (Gen_Id);
6739 end if;
6741 else
6742 Analyze (Gen_Id);
6744 if Is_Child_Unit (Entity (Gen_Id))
6745 and then
6746 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6747 and then not In_Open_Scopes (Inst_Par)
6748 then
6749 Install_Parent (Inst_Par);
6750 Parent_Installed := True;
6752 -- The generic unit may be the renaming of the implicit child
6753 -- present in an instance. In that case the parent instance is
6754 -- obtained from the name of the renamed entity.
6756 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6757 and then Present (Renamed_Entity (Entity (Gen_Id)))
6758 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6759 then
6760 declare
6761 Renamed_Package : constant Node_Id :=
6762 Name (Parent (Entity (Gen_Id)));
6763 begin
6764 if Nkind (Renamed_Package) = N_Expanded_Name then
6765 Inst_Par := Entity (Prefix (Renamed_Package));
6766 Install_Parent (Inst_Par);
6767 Parent_Installed := True;
6768 end if;
6769 end;
6770 end if;
6771 end if;
6773 elsif Nkind (Gen_Id) = N_Expanded_Name then
6775 -- Entity already present, analyze prefix, whose meaning may be an
6776 -- instance in the current context. If it is an instance of a
6777 -- relative within another, the proper parent may still have to be
6778 -- installed, if they are not of the same generation.
6780 Analyze (Prefix (Gen_Id));
6782 -- Prevent cascaded errors
6784 if Etype (Prefix (Gen_Id)) = Any_Type then
6785 return;
6786 end if;
6788 -- In the unlikely case that a local declaration hides the name of
6789 -- the parent package, locate it on the homonym chain. If the context
6790 -- is an instance of the parent, the renaming entity is flagged as
6791 -- such.
6793 Inst_Par := Entity (Prefix (Gen_Id));
6794 while Present (Inst_Par)
6795 and then not Is_Package_Or_Generic_Package (Inst_Par)
6796 loop
6797 Inst_Par := Homonym (Inst_Par);
6798 end loop;
6800 pragma Assert (Present (Inst_Par));
6801 Set_Entity (Prefix (Gen_Id), Inst_Par);
6803 if In_Enclosing_Instance then
6804 null;
6806 elsif Present (Entity (Gen_Id))
6807 and then Is_Child_Unit (Entity (Gen_Id))
6808 and then not In_Open_Scopes (Inst_Par)
6809 then
6810 Install_Parent (Inst_Par);
6811 Parent_Installed := True;
6812 end if;
6814 elsif In_Enclosing_Instance then
6816 -- The child unit is found in some enclosing scope
6818 null;
6820 else
6821 Analyze (Gen_Id);
6823 -- If this is the renaming of the implicit child in a parent
6824 -- instance, recover the parent name and install it.
6826 if Is_Entity_Name (Gen_Id) then
6827 E := Entity (Gen_Id);
6829 if Is_Generic_Unit (E)
6830 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6831 and then Is_Child_Unit (Renamed_Object (E))
6832 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6833 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6834 then
6835 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6836 Inst_Par := Entity (Prefix (Gen_Id));
6838 if not In_Open_Scopes (Inst_Par) then
6839 Install_Parent (Inst_Par);
6840 Parent_Installed := True;
6841 end if;
6843 -- If it is a child unit of a non-generic parent, it may be
6844 -- use-visible and given by a direct name. Install parent as
6845 -- for other cases.
6847 elsif Is_Generic_Unit (E)
6848 and then Is_Child_Unit (E)
6849 and then
6850 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6851 and then not Is_Generic_Unit (Scope (E))
6852 then
6853 if not In_Open_Scopes (Scope (E)) then
6854 Install_Parent (Scope (E));
6855 Parent_Installed := True;
6856 end if;
6857 end if;
6858 end if;
6859 end if;
6860 end Check_Generic_Child_Unit;
6862 -----------------------------
6863 -- Check_Hidden_Child_Unit --
6864 -----------------------------
6866 procedure Check_Hidden_Child_Unit
6867 (N : Node_Id;
6868 Gen_Unit : Entity_Id;
6869 Act_Decl_Id : Entity_Id)
6871 Gen_Id : constant Node_Id := Name (N);
6873 begin
6874 if Is_Child_Unit (Gen_Unit)
6875 and then Is_Child_Unit (Act_Decl_Id)
6876 and then Nkind (Gen_Id) = N_Expanded_Name
6877 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6878 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6879 then
6880 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6881 Error_Msg_NE
6882 ("generic unit & is implicitly declared in &",
6883 Defining_Unit_Name (N), Gen_Unit);
6884 Error_Msg_N ("\instance must have different name",
6885 Defining_Unit_Name (N));
6886 end if;
6887 end Check_Hidden_Child_Unit;
6889 ------------------------
6890 -- Check_Private_View --
6891 ------------------------
6893 procedure Check_Private_View (N : Node_Id) is
6894 T : constant Entity_Id := Etype (N);
6895 BT : Entity_Id;
6897 begin
6898 -- Exchange views if the type was not private in the generic but is
6899 -- private at the point of instantiation. Do not exchange views if
6900 -- the scope of the type is in scope. This can happen if both generic
6901 -- and instance are sibling units, or if type is defined in a parent.
6902 -- In this case the visibility of the type will be correct for all
6903 -- semantic checks.
6905 if Present (T) then
6906 BT := Base_Type (T);
6908 if Is_Private_Type (T)
6909 and then not Has_Private_View (N)
6910 and then Present (Full_View (T))
6911 and then not In_Open_Scopes (Scope (T))
6912 then
6913 -- In the generic, the full type was visible. Save the private
6914 -- entity, for subsequent exchange.
6916 Switch_View (T);
6918 elsif Has_Private_View (N)
6919 and then not Is_Private_Type (T)
6920 and then not Has_Been_Exchanged (T)
6921 and then Etype (Get_Associated_Node (N)) /= T
6922 then
6923 -- Only the private declaration was visible in the generic. If
6924 -- the type appears in a subtype declaration, the subtype in the
6925 -- instance must have a view compatible with that of its parent,
6926 -- which must be exchanged (see corresponding code in Restore_
6927 -- Private_Views). Otherwise, if the type is defined in a parent
6928 -- unit, leave full visibility within instance, which is safe.
6930 if In_Open_Scopes (Scope (Base_Type (T)))
6931 and then not Is_Private_Type (Base_Type (T))
6932 and then Comes_From_Source (Base_Type (T))
6933 then
6934 null;
6936 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6937 or else not In_Private_Part (Scope (Base_Type (T)))
6938 then
6939 Prepend_Elmt (T, Exchanged_Views);
6940 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6941 end if;
6943 -- For composite types with inconsistent representation exchange
6944 -- component types accordingly.
6946 elsif Is_Access_Type (T)
6947 and then Is_Private_Type (Designated_Type (T))
6948 and then not Has_Private_View (N)
6949 and then Present (Full_View (Designated_Type (T)))
6950 then
6951 Switch_View (Designated_Type (T));
6953 elsif Is_Array_Type (T) then
6954 if Is_Private_Type (Component_Type (T))
6955 and then not Has_Private_View (N)
6956 and then Present (Full_View (Component_Type (T)))
6957 then
6958 Switch_View (Component_Type (T));
6959 end if;
6961 -- The normal exchange mechanism relies on the setting of a
6962 -- flag on the reference in the generic. However, an additional
6963 -- mechanism is needed for types that are not explicitly
6964 -- mentioned in the generic, but may be needed in expanded code
6965 -- in the instance. This includes component types of arrays and
6966 -- designated types of access types. This processing must also
6967 -- include the index types of arrays which we take care of here.
6969 declare
6970 Indx : Node_Id;
6971 Typ : Entity_Id;
6973 begin
6974 Indx := First_Index (T);
6975 while Present (Indx) loop
6976 Typ := Base_Type (Etype (Indx));
6978 if Is_Private_Type (Typ)
6979 and then Present (Full_View (Typ))
6980 then
6981 Switch_View (Typ);
6982 end if;
6984 Next_Index (Indx);
6985 end loop;
6986 end;
6988 elsif Is_Private_Type (T)
6989 and then Present (Full_View (T))
6990 and then Is_Array_Type (Full_View (T))
6991 and then Is_Private_Type (Component_Type (Full_View (T)))
6992 then
6993 Switch_View (T);
6995 -- Finally, a non-private subtype may have a private base type, which
6996 -- must be exchanged for consistency. This can happen when a package
6997 -- body is instantiated, when the scope stack is empty but in fact
6998 -- the subtype and the base type are declared in an enclosing scope.
7000 -- Note that in this case we introduce an inconsistency in the view
7001 -- set, because we switch the base type BT, but there could be some
7002 -- private dependent subtypes of BT which remain unswitched. Such
7003 -- subtypes might need to be switched at a later point (see specific
7004 -- provision for that case in Switch_View).
7006 elsif not Is_Private_Type (T)
7007 and then not Has_Private_View (N)
7008 and then Is_Private_Type (BT)
7009 and then Present (Full_View (BT))
7010 and then not Is_Generic_Type (BT)
7011 and then not In_Open_Scopes (BT)
7012 then
7013 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7014 Exchange_Declarations (BT);
7015 end if;
7016 end if;
7017 end Check_Private_View;
7019 -----------------------------
7020 -- Check_Hidden_Primitives --
7021 -----------------------------
7023 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7024 Actual : Node_Id;
7025 Gen_T : Entity_Id;
7026 Result : Elist_Id := No_Elist;
7028 begin
7029 if No (Assoc_List) then
7030 return No_Elist;
7031 end if;
7033 -- Traverse the list of associations between formals and actuals
7034 -- searching for renamings of tagged types
7036 Actual := First (Assoc_List);
7037 while Present (Actual) loop
7038 if Nkind (Actual) = N_Subtype_Declaration then
7039 Gen_T := Generic_Parent_Type (Actual);
7041 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7043 -- Traverse the list of primitives of the actual types
7044 -- searching for hidden primitives that are visible in the
7045 -- corresponding generic formal; leave them visible and
7046 -- append them to Result to restore their decoration later.
7048 Install_Hidden_Primitives
7049 (Prims_List => Result,
7050 Gen_T => Gen_T,
7051 Act_T => Entity (Subtype_Indication (Actual)));
7052 end if;
7053 end if;
7055 Next (Actual);
7056 end loop;
7058 return Result;
7059 end Check_Hidden_Primitives;
7061 --------------------------
7062 -- Contains_Instance_Of --
7063 --------------------------
7065 function Contains_Instance_Of
7066 (Inner : Entity_Id;
7067 Outer : Entity_Id;
7068 N : Node_Id) return Boolean
7070 Elmt : Elmt_Id;
7071 Scop : Entity_Id;
7073 begin
7074 Scop := Outer;
7076 -- Verify that there are no circular instantiations. We check whether
7077 -- the unit contains an instance of the current scope or some enclosing
7078 -- scope (in case one of the instances appears in a subunit). Longer
7079 -- circularities involving subunits might seem too pathological to
7080 -- consider, but they were not too pathological for the authors of
7081 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7082 -- enclosing generic scopes as containing an instance.
7084 loop
7085 -- Within a generic subprogram body, the scope is not generic, to
7086 -- allow for recursive subprograms. Use the declaration to determine
7087 -- whether this is a generic unit.
7089 if Ekind (Scop) = E_Generic_Package
7090 or else (Is_Subprogram (Scop)
7091 and then Nkind (Unit_Declaration_Node (Scop)) =
7092 N_Generic_Subprogram_Declaration)
7093 then
7094 Elmt := First_Elmt (Inner_Instances (Inner));
7096 while Present (Elmt) loop
7097 if Node (Elmt) = Scop then
7098 Error_Msg_Node_2 := Inner;
7099 Error_Msg_NE
7100 ("circular Instantiation: & instantiated within &!",
7101 N, Scop);
7102 return True;
7104 elsif Node (Elmt) = Inner then
7105 return True;
7107 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7108 Error_Msg_Node_2 := Inner;
7109 Error_Msg_NE
7110 ("circular Instantiation: & instantiated within &!",
7111 N, Node (Elmt));
7112 return True;
7113 end if;
7115 Next_Elmt (Elmt);
7116 end loop;
7118 -- Indicate that Inner is being instantiated within Scop
7120 Append_Elmt (Inner, Inner_Instances (Scop));
7121 end if;
7123 if Scop = Standard_Standard then
7124 exit;
7125 else
7126 Scop := Scope (Scop);
7127 end if;
7128 end loop;
7130 return False;
7131 end Contains_Instance_Of;
7133 -----------------------
7134 -- Copy_Generic_Node --
7135 -----------------------
7137 function Copy_Generic_Node
7138 (N : Node_Id;
7139 Parent_Id : Node_Id;
7140 Instantiating : Boolean) return Node_Id
7142 Ent : Entity_Id;
7143 New_N : Node_Id;
7145 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7146 -- Check the given value of one of the Fields referenced by the current
7147 -- node to determine whether to copy it recursively. The field may hold
7148 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7149 -- Char) in which case it need not be copied.
7151 procedure Copy_Descendants;
7152 -- Common utility for various nodes
7154 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7155 -- Make copy of element list
7157 function Copy_Generic_List
7158 (L : List_Id;
7159 Parent_Id : Node_Id) return List_Id;
7160 -- Apply Copy_Node recursively to the members of a node list
7162 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7163 -- True if an identifier is part of the defining program unit name of
7164 -- a child unit. The entity of such an identifier must be kept (for
7165 -- ASIS use) even though as the name of an enclosing generic it would
7166 -- otherwise not be preserved in the generic tree.
7168 ----------------------
7169 -- Copy_Descendants --
7170 ----------------------
7172 procedure Copy_Descendants is
7173 use Atree.Unchecked_Access;
7174 -- This code section is part of the implementation of an untyped
7175 -- tree traversal, so it needs direct access to node fields.
7177 begin
7178 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7179 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7180 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7181 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7182 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7183 end Copy_Descendants;
7185 -----------------------------
7186 -- Copy_Generic_Descendant --
7187 -----------------------------
7189 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7190 begin
7191 if D = Union_Id (Empty) then
7192 return D;
7194 elsif D in Node_Range then
7195 return Union_Id
7196 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7198 elsif D in List_Range then
7199 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7201 elsif D in Elist_Range then
7202 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7204 -- Nothing else is copyable (e.g. Uint values), return as is
7206 else
7207 return D;
7208 end if;
7209 end Copy_Generic_Descendant;
7211 ------------------------
7212 -- Copy_Generic_Elist --
7213 ------------------------
7215 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7216 M : Elmt_Id;
7217 L : Elist_Id;
7219 begin
7220 if Present (E) then
7221 L := New_Elmt_List;
7222 M := First_Elmt (E);
7223 while Present (M) loop
7224 Append_Elmt
7225 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7226 Next_Elmt (M);
7227 end loop;
7229 return L;
7231 else
7232 return No_Elist;
7233 end if;
7234 end Copy_Generic_Elist;
7236 -----------------------
7237 -- Copy_Generic_List --
7238 -----------------------
7240 function Copy_Generic_List
7241 (L : List_Id;
7242 Parent_Id : Node_Id) return List_Id
7244 N : Node_Id;
7245 New_L : List_Id;
7247 begin
7248 if Present (L) then
7249 New_L := New_List;
7250 Set_Parent (New_L, Parent_Id);
7252 N := First (L);
7253 while Present (N) loop
7254 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7255 Next (N);
7256 end loop;
7258 return New_L;
7260 else
7261 return No_List;
7262 end if;
7263 end Copy_Generic_List;
7265 ---------------------------
7266 -- In_Defining_Unit_Name --
7267 ---------------------------
7269 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7270 begin
7271 return
7272 Present (Parent (Nam))
7273 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7274 or else
7275 (Nkind (Parent (Nam)) = N_Expanded_Name
7276 and then In_Defining_Unit_Name (Parent (Nam))));
7277 end In_Defining_Unit_Name;
7279 -- Start of processing for Copy_Generic_Node
7281 begin
7282 if N = Empty then
7283 return N;
7284 end if;
7286 New_N := New_Copy (N);
7288 -- Copy aspects if present
7290 if Has_Aspects (N) then
7291 Set_Has_Aspects (New_N, False);
7292 Set_Aspect_Specifications
7293 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7294 end if;
7296 if Instantiating then
7297 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7298 end if;
7300 if not Is_List_Member (N) then
7301 Set_Parent (New_N, Parent_Id);
7302 end if;
7304 -- Special casing for identifiers and other entity names and operators
7306 if Nkind_In (New_N, N_Character_Literal,
7307 N_Expanded_Name,
7308 N_Identifier,
7309 N_Operator_Symbol)
7310 or else Nkind (New_N) in N_Op
7311 then
7312 if not Instantiating then
7314 -- Link both nodes in order to assign subsequently the entity of
7315 -- the copy to the original node, in case this is a global
7316 -- reference.
7318 Set_Associated_Node (N, New_N);
7320 -- If we are within an instantiation, this is a nested generic
7321 -- that has already been analyzed at the point of definition.
7322 -- We must preserve references that were global to the enclosing
7323 -- parent at that point. Other occurrences, whether global or
7324 -- local to the current generic, must be resolved anew, so we
7325 -- reset the entity in the generic copy. A global reference has a
7326 -- smaller depth than the parent, or else the same depth in case
7327 -- both are distinct compilation units.
7329 -- A child unit is implicitly declared within the enclosing parent
7330 -- but is in fact global to it, and must be preserved.
7332 -- It is also possible for Current_Instantiated_Parent to be
7333 -- defined, and for this not to be a nested generic, namely if
7334 -- the unit is loaded through Rtsfind. In that case, the entity of
7335 -- New_N is only a link to the associated node, and not a defining
7336 -- occurrence.
7338 -- The entities for parent units in the defining_program_unit of a
7339 -- generic child unit are established when the context of the unit
7340 -- is first analyzed, before the generic copy is made. They are
7341 -- preserved in the copy for use in ASIS queries.
7343 Ent := Entity (New_N);
7345 if No (Current_Instantiated_Parent.Gen_Id) then
7346 if No (Ent)
7347 or else Nkind (Ent) /= N_Defining_Identifier
7348 or else not In_Defining_Unit_Name (N)
7349 then
7350 Set_Associated_Node (New_N, Empty);
7351 end if;
7353 elsif No (Ent)
7354 or else
7355 not Nkind_In (Ent, N_Defining_Identifier,
7356 N_Defining_Character_Literal,
7357 N_Defining_Operator_Symbol)
7358 or else No (Scope (Ent))
7359 or else
7360 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7361 and then not Is_Child_Unit (Ent))
7362 or else
7363 (Scope_Depth (Scope (Ent)) >
7364 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7365 and then
7366 Get_Source_Unit (Ent) =
7367 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7368 then
7369 Set_Associated_Node (New_N, Empty);
7370 end if;
7372 -- Case of instantiating identifier or some other name or operator
7374 else
7375 -- If the associated node is still defined, the entity in it
7376 -- is global, and must be copied to the instance. If this copy
7377 -- is being made for a body to inline, it is applied to an
7378 -- instantiated tree, and the entity is already present and
7379 -- must be also preserved.
7381 declare
7382 Assoc : constant Node_Id := Get_Associated_Node (N);
7384 begin
7385 if Present (Assoc) then
7386 if Nkind (Assoc) = Nkind (N) then
7387 Set_Entity (New_N, Entity (Assoc));
7388 Check_Private_View (N);
7390 -- The node is a reference to a global type and acts as the
7391 -- subtype mark of a qualified expression created in order
7392 -- to aid resolution of accidental overloading in instances.
7393 -- Since N is a reference to a type, the Associated_Node of
7394 -- N denotes an entity rather than another identifier. See
7395 -- Qualify_Universal_Operands for details.
7397 elsif Nkind (N) = N_Identifier
7398 and then Nkind (Parent (N)) = N_Qualified_Expression
7399 and then Subtype_Mark (Parent (N)) = N
7400 and then Is_Qualified_Universal_Literal (Parent (N))
7401 then
7402 Set_Entity (New_N, Assoc);
7404 -- The name in the call may be a selected component if the
7405 -- call has not been analyzed yet, as may be the case for
7406 -- pre/post conditions in a generic unit.
7408 elsif Nkind (Assoc) = N_Function_Call
7409 and then Is_Entity_Name (Name (Assoc))
7410 then
7411 Set_Entity (New_N, Entity (Name (Assoc)));
7413 elsif Nkind_In (Assoc, N_Defining_Identifier,
7414 N_Defining_Character_Literal,
7415 N_Defining_Operator_Symbol)
7416 and then Expander_Active
7417 then
7418 -- Inlining case: we are copying a tree that contains
7419 -- global entities, which are preserved in the copy to be
7420 -- used for subsequent inlining.
7422 null;
7424 else
7425 Set_Entity (New_N, Empty);
7426 end if;
7427 end if;
7428 end;
7429 end if;
7431 -- For expanded name, we must copy the Prefix and Selector_Name
7433 if Nkind (N) = N_Expanded_Name then
7434 Set_Prefix
7435 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7437 Set_Selector_Name (New_N,
7438 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7440 -- For operators, we must copy the right operand
7442 elsif Nkind (N) in N_Op then
7443 Set_Right_Opnd (New_N,
7444 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7446 -- And for binary operators, the left operand as well
7448 if Nkind (N) in N_Binary_Op then
7449 Set_Left_Opnd (New_N,
7450 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7451 end if;
7452 end if;
7454 -- Establish a link between an entity from the generic template and the
7455 -- corresponding entity in the generic copy to be analyzed.
7457 elsif Nkind (N) in N_Entity then
7458 if not Instantiating then
7459 Set_Associated_Entity (N, New_N);
7460 end if;
7462 -- Clear any existing link the copy may inherit from the replicated
7463 -- generic template entity.
7465 Set_Associated_Entity (New_N, Empty);
7467 -- Special casing for stubs
7469 elsif Nkind (N) in N_Body_Stub then
7471 -- In any case, we must copy the specification or defining
7472 -- identifier as appropriate.
7474 if Nkind (N) = N_Subprogram_Body_Stub then
7475 Set_Specification (New_N,
7476 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7478 else
7479 Set_Defining_Identifier (New_N,
7480 Copy_Generic_Node
7481 (Defining_Identifier (N), New_N, Instantiating));
7482 end if;
7484 -- If we are not instantiating, then this is where we load and
7485 -- analyze subunits, i.e. at the point where the stub occurs. A
7486 -- more permissive system might defer this analysis to the point
7487 -- of instantiation, but this seems too complicated for now.
7489 if not Instantiating then
7490 declare
7491 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7492 Subunit : Node_Id;
7493 Unum : Unit_Number_Type;
7494 New_Body : Node_Id;
7496 begin
7497 -- Make sure that, if it is a subunit of the main unit that is
7498 -- preprocessed and if -gnateG is specified, the preprocessed
7499 -- file will be written.
7501 Lib.Analysing_Subunit_Of_Main :=
7502 Lib.In_Extended_Main_Source_Unit (N);
7503 Unum :=
7504 Load_Unit
7505 (Load_Name => Subunit_Name,
7506 Required => False,
7507 Subunit => True,
7508 Error_Node => N);
7509 Lib.Analysing_Subunit_Of_Main := False;
7511 -- If the proper body is not found, a warning message will be
7512 -- emitted when analyzing the stub, or later at the point of
7513 -- instantiation. Here we just leave the stub as is.
7515 if Unum = No_Unit then
7516 Subunits_Missing := True;
7517 goto Subunit_Not_Found;
7518 end if;
7520 Subunit := Cunit (Unum);
7522 if Nkind (Unit (Subunit)) /= N_Subunit then
7523 Error_Msg_N
7524 ("found child unit instead of expected SEPARATE subunit",
7525 Subunit);
7526 Error_Msg_Sloc := Sloc (N);
7527 Error_Msg_N ("\to complete stub #", Subunit);
7528 goto Subunit_Not_Found;
7529 end if;
7531 -- We must create a generic copy of the subunit, in order to
7532 -- perform semantic analysis on it, and we must replace the
7533 -- stub in the original generic unit with the subunit, in order
7534 -- to preserve non-local references within.
7536 -- Only the proper body needs to be copied. Library_Unit and
7537 -- context clause are simply inherited by the generic copy.
7538 -- Note that the copy (which may be recursive if there are
7539 -- nested subunits) must be done first, before attaching it to
7540 -- the enclosing generic.
7542 New_Body :=
7543 Copy_Generic_Node
7544 (Proper_Body (Unit (Subunit)),
7545 Empty, Instantiating => False);
7547 -- Now place the original proper body in the original generic
7548 -- unit. This is a body, not a compilation unit.
7550 Rewrite (N, Proper_Body (Unit (Subunit)));
7551 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7552 Set_Was_Originally_Stub (N);
7554 -- Finally replace the body of the subunit with its copy, and
7555 -- make this new subunit into the library unit of the generic
7556 -- copy, which does not have stubs any longer.
7558 Set_Proper_Body (Unit (Subunit), New_Body);
7559 Set_Library_Unit (New_N, Subunit);
7560 Inherit_Context (Unit (Subunit), N);
7561 end;
7563 -- If we are instantiating, this must be an error case, since
7564 -- otherwise we would have replaced the stub node by the proper body
7565 -- that corresponds. So just ignore it in the copy (i.e. we have
7566 -- copied it, and that is good enough).
7568 else
7569 null;
7570 end if;
7572 <<Subunit_Not_Found>> null;
7574 -- If the node is a compilation unit, it is the subunit of a stub, which
7575 -- has been loaded already (see code below). In this case, the library
7576 -- unit field of N points to the parent unit (which is a compilation
7577 -- unit) and need not (and cannot) be copied.
7579 -- When the proper body of the stub is analyzed, the library_unit link
7580 -- is used to establish the proper context (see sem_ch10).
7582 -- The other fields of a compilation unit are copied as usual
7584 elsif Nkind (N) = N_Compilation_Unit then
7586 -- This code can only be executed when not instantiating, because in
7587 -- the copy made for an instantiation, the compilation unit node has
7588 -- disappeared at the point that a stub is replaced by its proper
7589 -- body.
7591 pragma Assert (not Instantiating);
7593 Set_Context_Items (New_N,
7594 Copy_Generic_List (Context_Items (N), New_N));
7596 Set_Unit (New_N,
7597 Copy_Generic_Node (Unit (N), New_N, False));
7599 Set_First_Inlined_Subprogram (New_N,
7600 Copy_Generic_Node
7601 (First_Inlined_Subprogram (N), New_N, False));
7603 Set_Aux_Decls_Node (New_N,
7604 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7606 -- For an assignment node, the assignment is known to be semantically
7607 -- legal if we are instantiating the template. This avoids incorrect
7608 -- diagnostics in generated code.
7610 elsif Nkind (N) = N_Assignment_Statement then
7612 -- Copy name and expression fields in usual manner
7614 Set_Name (New_N,
7615 Copy_Generic_Node (Name (N), New_N, Instantiating));
7617 Set_Expression (New_N,
7618 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7620 if Instantiating then
7621 Set_Assignment_OK (Name (New_N), True);
7622 end if;
7624 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7625 if not Instantiating then
7626 Set_Associated_Node (N, New_N);
7628 else
7629 if Present (Get_Associated_Node (N))
7630 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7631 then
7632 -- In the generic the aggregate has some composite type. If at
7633 -- the point of instantiation the type has a private view,
7634 -- install the full view (and that of its ancestors, if any).
7636 declare
7637 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7638 Rt : Entity_Id;
7640 begin
7641 if Present (T) and then Is_Private_Type (T) then
7642 Switch_View (T);
7643 end if;
7645 if Present (T)
7646 and then Is_Tagged_Type (T)
7647 and then Is_Derived_Type (T)
7648 then
7649 Rt := Root_Type (T);
7651 loop
7652 T := Etype (T);
7654 if Is_Private_Type (T) then
7655 Switch_View (T);
7656 end if;
7658 exit when T = Rt;
7659 end loop;
7660 end if;
7661 end;
7662 end if;
7663 end if;
7665 -- Do not copy the associated node, which points to the generic copy
7666 -- of the aggregate.
7668 declare
7669 use Atree.Unchecked_Access;
7670 -- This code section is part of the implementation of an untyped
7671 -- tree traversal, so it needs direct access to node fields.
7673 begin
7674 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7675 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7676 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7677 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7678 end;
7680 -- Allocators do not have an identifier denoting the access type, so we
7681 -- must locate it through the expression to check whether the views are
7682 -- consistent.
7684 elsif Nkind (N) = N_Allocator
7685 and then Nkind (Expression (N)) = N_Qualified_Expression
7686 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7687 and then Instantiating
7688 then
7689 declare
7690 T : constant Node_Id :=
7691 Get_Associated_Node (Subtype_Mark (Expression (N)));
7692 Acc_T : Entity_Id;
7694 begin
7695 if Present (T) then
7697 -- Retrieve the allocator node in the generic copy
7699 Acc_T := Etype (Parent (Parent (T)));
7701 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7702 Switch_View (Acc_T);
7703 end if;
7704 end if;
7706 Copy_Descendants;
7707 end;
7709 -- For a proper body, we must catch the case of a proper body that
7710 -- replaces a stub. This represents the point at which a separate
7711 -- compilation unit, and hence template file, may be referenced, so we
7712 -- must make a new source instantiation entry for the template of the
7713 -- subunit, and ensure that all nodes in the subunit are adjusted using
7714 -- this new source instantiation entry.
7716 elsif Nkind (N) in N_Proper_Body then
7717 declare
7718 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7720 begin
7721 if Instantiating and then Was_Originally_Stub (N) then
7722 Create_Instantiation_Source
7723 (Instantiation_Node,
7724 Defining_Entity (N),
7725 S_Adjustment);
7726 end if;
7728 -- Now copy the fields of the proper body, using the new
7729 -- adjustment factor if one was needed as per test above.
7731 Copy_Descendants;
7733 -- Restore the original adjustment factor in case changed
7735 S_Adjustment := Save_Adjustment;
7736 end;
7738 elsif Nkind (N) = N_Pragma and then Instantiating then
7740 -- Do not copy Comment or Ident pragmas their content is relevant to
7741 -- the generic unit, not to the instantiating unit.
7743 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7744 New_N := Make_Null_Statement (Sloc (N));
7746 -- Do not copy pragmas generated from aspects because the pragmas do
7747 -- not carry any semantic information, plus they will be regenerated
7748 -- in the instance.
7750 -- However, generating C we need to copy them since postconditions
7751 -- are inlined by the front end, and the front-end inlining machinery
7752 -- relies on this routine to perform inlining.
7754 elsif From_Aspect_Specification (N)
7755 and then not Modify_Tree_For_C
7756 then
7757 New_N := Make_Null_Statement (Sloc (N));
7759 else
7760 Copy_Descendants;
7761 end if;
7763 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7765 -- No descendant fields need traversing
7767 null;
7769 elsif Nkind (N) = N_String_Literal
7770 and then Present (Etype (N))
7771 and then Instantiating
7772 then
7773 -- If the string is declared in an outer scope, the string_literal
7774 -- subtype created for it may have the wrong scope. Force reanalysis
7775 -- of the constant to generate a new itype in the proper context.
7777 Set_Etype (New_N, Empty);
7778 Set_Analyzed (New_N, False);
7780 -- For the remaining nodes, copy their descendants recursively
7782 else
7783 Copy_Descendants;
7785 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7786 Set_Generic_Parent (Specification (New_N), N);
7788 -- Should preserve Corresponding_Spec??? (12.3(14))
7789 end if;
7790 end if;
7792 -- Propagate dimensions if present, so that they are reflected in the
7793 -- instance.
7795 if Nkind (N) in N_Has_Etype
7796 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7797 and then Present (Etype (N))
7798 and then Is_Floating_Point_Type (Etype (N))
7799 and then Has_Dimension_System (Etype (N))
7800 then
7801 Copy_Dimensions (N, New_N);
7802 end if;
7804 return New_N;
7805 end Copy_Generic_Node;
7807 ----------------------------
7808 -- Denotes_Formal_Package --
7809 ----------------------------
7811 function Denotes_Formal_Package
7812 (Pack : Entity_Id;
7813 On_Exit : Boolean := False;
7814 Instance : Entity_Id := Empty) return Boolean
7816 Par : Entity_Id;
7817 Scop : constant Entity_Id := Scope (Pack);
7818 E : Entity_Id;
7820 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7821 -- The package in question may be an actual for a previous formal
7822 -- package P of the current instance, so examine its actuals as well.
7823 -- This must be recursive over other formal packages.
7825 ----------------------------------
7826 -- Is_Actual_Of_Previous_Formal --
7827 ----------------------------------
7829 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7830 E1 : Entity_Id;
7832 begin
7833 E1 := First_Entity (P);
7834 while Present (E1) and then E1 /= Instance loop
7835 if Ekind (E1) = E_Package
7836 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7837 then
7838 if Renamed_Object (E1) = Pack then
7839 return True;
7841 elsif E1 = P or else Renamed_Object (E1) = P then
7842 return False;
7844 elsif Is_Actual_Of_Previous_Formal (E1) then
7845 return True;
7846 end if;
7847 end if;
7849 Next_Entity (E1);
7850 end loop;
7852 return False;
7853 end Is_Actual_Of_Previous_Formal;
7855 -- Start of processing for Denotes_Formal_Package
7857 begin
7858 if On_Exit then
7859 Par :=
7860 Instance_Envs.Table
7861 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7862 else
7863 Par := Current_Instantiated_Parent.Act_Id;
7864 end if;
7866 if Ekind (Scop) = E_Generic_Package
7867 or else Nkind (Unit_Declaration_Node (Scop)) =
7868 N_Generic_Subprogram_Declaration
7869 then
7870 return True;
7872 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7873 N_Formal_Package_Declaration
7874 then
7875 return True;
7877 elsif No (Par) then
7878 return False;
7880 else
7881 -- Check whether this package is associated with a formal package of
7882 -- the enclosing instantiation. Iterate over the list of renamings.
7884 E := First_Entity (Par);
7885 while Present (E) loop
7886 if Ekind (E) /= E_Package
7887 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7888 then
7889 null;
7891 elsif Renamed_Object (E) = Par then
7892 return False;
7894 elsif Renamed_Object (E) = Pack then
7895 return True;
7897 elsif Is_Actual_Of_Previous_Formal (E) then
7898 return True;
7900 end if;
7902 Next_Entity (E);
7903 end loop;
7905 return False;
7906 end if;
7907 end Denotes_Formal_Package;
7909 -----------------
7910 -- End_Generic --
7911 -----------------
7913 procedure End_Generic is
7914 begin
7915 -- ??? More things could be factored out in this routine. Should
7916 -- probably be done at a later stage.
7918 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7919 Generic_Flags.Decrement_Last;
7921 Expander_Mode_Restore;
7922 end End_Generic;
7924 -------------
7925 -- Earlier --
7926 -------------
7928 function Earlier (N1, N2 : Node_Id) return Boolean is
7929 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7930 -- Find distance from given node to enclosing compilation unit
7932 ----------------
7933 -- Find_Depth --
7934 ----------------
7936 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7937 begin
7938 while Present (P)
7939 and then Nkind (P) /= N_Compilation_Unit
7940 loop
7941 P := True_Parent (P);
7942 D := D + 1;
7943 end loop;
7944 end Find_Depth;
7946 -- Local declarations
7948 D1 : Integer := 0;
7949 D2 : Integer := 0;
7950 P1 : Node_Id := N1;
7951 P2 : Node_Id := N2;
7952 T1 : Source_Ptr;
7953 T2 : Source_Ptr;
7955 -- Start of processing for Earlier
7957 begin
7958 Find_Depth (P1, D1);
7959 Find_Depth (P2, D2);
7961 if P1 /= P2 then
7962 return False;
7963 else
7964 P1 := N1;
7965 P2 := N2;
7966 end if;
7968 while D1 > D2 loop
7969 P1 := True_Parent (P1);
7970 D1 := D1 - 1;
7971 end loop;
7973 while D2 > D1 loop
7974 P2 := True_Parent (P2);
7975 D2 := D2 - 1;
7976 end loop;
7978 -- At this point P1 and P2 are at the same distance from the root.
7979 -- We examine their parents until we find a common declarative list.
7980 -- If we reach the root, N1 and N2 do not descend from the same
7981 -- declarative list (e.g. one is nested in the declarative part and
7982 -- the other is in a block in the statement part) and the earlier
7983 -- one is already frozen.
7985 while not Is_List_Member (P1)
7986 or else not Is_List_Member (P2)
7987 or else List_Containing (P1) /= List_Containing (P2)
7988 loop
7989 P1 := True_Parent (P1);
7990 P2 := True_Parent (P2);
7992 if Nkind (Parent (P1)) = N_Subunit then
7993 P1 := Corresponding_Stub (Parent (P1));
7994 end if;
7996 if Nkind (Parent (P2)) = N_Subunit then
7997 P2 := Corresponding_Stub (Parent (P2));
7998 end if;
8000 if P1 = P2 then
8001 return False;
8002 end if;
8003 end loop;
8005 -- Expanded code usually shares the source location of the original
8006 -- construct it was generated for. This however may not necessarily
8007 -- reflect the true location of the code within the tree.
8009 -- Before comparing the slocs of the two nodes, make sure that we are
8010 -- working with correct source locations. Assume that P1 is to the left
8011 -- of P2. If either one does not come from source, traverse the common
8012 -- list heading towards the other node and locate the first source
8013 -- statement.
8015 -- P1 P2
8016 -- ----+===+===+--------------+===+===+----
8017 -- expanded code expanded code
8019 if not Comes_From_Source (P1) then
8020 while Present (P1) loop
8022 -- Neither P2 nor a source statement were located during the
8023 -- search. If we reach the end of the list, then P1 does not
8024 -- occur earlier than P2.
8026 -- ---->
8027 -- start --- P2 ----- P1 --- end
8029 if No (Next (P1)) then
8030 return False;
8032 -- We encounter P2 while going to the right of the list. This
8033 -- means that P1 does indeed appear earlier.
8035 -- ---->
8036 -- start --- P1 ===== P2 --- end
8037 -- expanded code in between
8039 elsif P1 = P2 then
8040 return True;
8042 -- No need to look any further since we have located a source
8043 -- statement.
8045 elsif Comes_From_Source (P1) then
8046 exit;
8047 end if;
8049 -- Keep going right
8051 Next (P1);
8052 end loop;
8053 end if;
8055 if not Comes_From_Source (P2) then
8056 while Present (P2) loop
8058 -- Neither P1 nor a source statement were located during the
8059 -- search. If we reach the start of the list, then P1 does not
8060 -- occur earlier than P2.
8062 -- <----
8063 -- start --- P2 --- P1 --- end
8065 if No (Prev (P2)) then
8066 return False;
8068 -- We encounter P1 while going to the left of the list. This
8069 -- means that P1 does indeed appear earlier.
8071 -- <----
8072 -- start --- P1 ===== P2 --- end
8073 -- expanded code in between
8075 elsif P2 = P1 then
8076 return True;
8078 -- No need to look any further since we have located a source
8079 -- statement.
8081 elsif Comes_From_Source (P2) then
8082 exit;
8083 end if;
8085 -- Keep going left
8087 Prev (P2);
8088 end loop;
8089 end if;
8091 -- At this point either both nodes came from source or we approximated
8092 -- their source locations through neighboring source statements.
8094 T1 := Top_Level_Location (Sloc (P1));
8095 T2 := Top_Level_Location (Sloc (P2));
8097 -- When two nodes come from the same instance, they have identical top
8098 -- level locations. To determine proper relation within the tree, check
8099 -- their locations within the template.
8101 if T1 = T2 then
8102 return Sloc (P1) < Sloc (P2);
8104 -- The two nodes either come from unrelated instances or do not come
8105 -- from instantiated code at all.
8107 else
8108 return T1 < T2;
8109 end if;
8110 end Earlier;
8112 ----------------------
8113 -- Find_Actual_Type --
8114 ----------------------
8116 function Find_Actual_Type
8117 (Typ : Entity_Id;
8118 Gen_Type : Entity_Id) return Entity_Id
8120 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8121 T : Entity_Id;
8123 begin
8124 -- Special processing only applies to child units
8126 if not Is_Child_Unit (Gen_Scope) then
8127 return Get_Instance_Of (Typ);
8129 -- If designated or component type is itself a formal of the child unit,
8130 -- its instance is available.
8132 elsif Scope (Typ) = Gen_Scope then
8133 return Get_Instance_Of (Typ);
8135 -- If the array or access type is not declared in the parent unit,
8136 -- no special processing needed.
8138 elsif not Is_Generic_Type (Typ)
8139 and then Scope (Gen_Scope) /= Scope (Typ)
8140 then
8141 return Get_Instance_Of (Typ);
8143 -- Otherwise, retrieve designated or component type by visibility
8145 else
8146 T := Current_Entity (Typ);
8147 while Present (T) loop
8148 if In_Open_Scopes (Scope (T)) then
8149 return T;
8150 elsif Is_Generic_Actual_Type (T) then
8151 return T;
8152 end if;
8154 T := Homonym (T);
8155 end loop;
8157 return Typ;
8158 end if;
8159 end Find_Actual_Type;
8161 ----------------------------
8162 -- Freeze_Subprogram_Body --
8163 ----------------------------
8165 procedure Freeze_Subprogram_Body
8166 (Inst_Node : Node_Id;
8167 Gen_Body : Node_Id;
8168 Pack_Id : Entity_Id)
8170 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8171 Par : constant Entity_Id := Scope (Gen_Unit);
8172 E_G_Id : Entity_Id;
8173 Enc_G : Entity_Id;
8174 Enc_I : Node_Id;
8175 F_Node : Node_Id;
8177 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8178 -- Find innermost package body that encloses the given node, and which
8179 -- is not a compilation unit. Freeze nodes for the instance, or for its
8180 -- enclosing body, may be inserted after the enclosing_body of the
8181 -- generic unit. Used to determine proper placement of freeze node for
8182 -- both package and subprogram instances.
8184 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8185 -- Find entity for given package body, and locate or create a freeze
8186 -- node for it.
8188 ----------------------------
8189 -- Enclosing_Package_Body --
8190 ----------------------------
8192 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8193 P : Node_Id;
8195 begin
8196 P := Parent (N);
8197 while Present (P)
8198 and then Nkind (Parent (P)) /= N_Compilation_Unit
8199 loop
8200 if Nkind (P) = N_Package_Body then
8201 if Nkind (Parent (P)) = N_Subunit then
8202 return Corresponding_Stub (Parent (P));
8203 else
8204 return P;
8205 end if;
8206 end if;
8208 P := True_Parent (P);
8209 end loop;
8211 return Empty;
8212 end Enclosing_Package_Body;
8214 -------------------------
8215 -- Package_Freeze_Node --
8216 -------------------------
8218 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8219 Id : Entity_Id;
8221 begin
8222 if Nkind (B) = N_Package_Body then
8223 Id := Corresponding_Spec (B);
8224 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8225 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8226 end if;
8228 Ensure_Freeze_Node (Id);
8229 return Freeze_Node (Id);
8230 end Package_Freeze_Node;
8232 -- Start of processing for Freeze_Subprogram_Body
8234 begin
8235 -- If the instance and the generic body appear within the same unit, and
8236 -- the instance precedes the generic, the freeze node for the instance
8237 -- must appear after that of the generic. If the generic is nested
8238 -- within another instance I2, then current instance must be frozen
8239 -- after I2. In both cases, the freeze nodes are those of enclosing
8240 -- packages. Otherwise, the freeze node is placed at the end of the
8241 -- current declarative part.
8243 Enc_G := Enclosing_Package_Body (Gen_Body);
8244 Enc_I := Enclosing_Package_Body (Inst_Node);
8245 Ensure_Freeze_Node (Pack_Id);
8246 F_Node := Freeze_Node (Pack_Id);
8248 if Is_Generic_Instance (Par)
8249 and then Present (Freeze_Node (Par))
8250 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8251 then
8252 -- The parent was a premature instantiation. Insert freeze node at
8253 -- the end the current declarative part.
8255 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8256 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8258 -- Handle the following case:
8260 -- package Parent_Inst is new ...
8261 -- Parent_Inst []
8263 -- procedure P ... -- this body freezes Parent_Inst
8265 -- package Inst is new ...
8267 -- In this particular scenario, the freeze node for Inst must be
8268 -- inserted in the same manner as that of Parent_Inst - before the
8269 -- next source body or at the end of the declarative list (body not
8270 -- available). If body P did not exist and Parent_Inst was frozen
8271 -- after Inst, either by a body following Inst or at the end of the
8272 -- declarative region, the freeze node for Inst must be inserted
8273 -- after that of Parent_Inst. This relation is established by
8274 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8276 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8277 List_Containing (Inst_Node)
8278 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8279 then
8280 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8282 else
8283 Insert_After (Freeze_Node (Par), F_Node);
8284 end if;
8286 -- The body enclosing the instance should be frozen after the body that
8287 -- includes the generic, because the body of the instance may make
8288 -- references to entities therein. If the two are not in the same
8289 -- declarative part, or if the one enclosing the instance is frozen
8290 -- already, freeze the instance at the end of the current declarative
8291 -- part.
8293 elsif Is_Generic_Instance (Par)
8294 and then Present (Freeze_Node (Par))
8295 and then Present (Enc_I)
8296 then
8297 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8298 or else
8299 (Nkind (Enc_I) = N_Package_Body
8300 and then
8301 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8302 then
8303 -- The enclosing package may contain several instances. Rather
8304 -- than computing the earliest point at which to insert its freeze
8305 -- node, we place it at the end of the declarative part of the
8306 -- parent of the generic.
8308 Insert_Freeze_Node_For_Instance
8309 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8310 end if;
8312 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8314 elsif Present (Enc_G)
8315 and then Present (Enc_I)
8316 and then Enc_G /= Enc_I
8317 and then Earlier (Inst_Node, Gen_Body)
8318 then
8319 if Nkind (Enc_G) = N_Package_Body then
8320 E_G_Id :=
8321 Corresponding_Spec (Enc_G);
8322 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8323 E_G_Id :=
8324 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8325 end if;
8327 -- Freeze package that encloses instance, and place node after the
8328 -- package that encloses generic. If enclosing package is already
8329 -- frozen we have to assume it is at the proper place. This may be a
8330 -- potential ABE that requires dynamic checking. Do not add a freeze
8331 -- node if the package that encloses the generic is inside the body
8332 -- that encloses the instance, because the freeze node would be in
8333 -- the wrong scope. Additional contortions needed if the bodies are
8334 -- within a subunit.
8336 declare
8337 Enclosing_Body : Node_Id;
8339 begin
8340 if Nkind (Enc_I) = N_Package_Body_Stub then
8341 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8342 else
8343 Enclosing_Body := Enc_I;
8344 end if;
8346 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8347 Insert_Freeze_Node_For_Instance
8348 (Enc_G, Package_Freeze_Node (Enc_I));
8349 end if;
8350 end;
8352 -- Freeze enclosing subunit before instance
8354 Ensure_Freeze_Node (E_G_Id);
8356 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8357 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8358 end if;
8360 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8362 else
8363 -- If none of the above, insert freeze node at the end of the current
8364 -- declarative part.
8366 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8367 end if;
8368 end Freeze_Subprogram_Body;
8370 ----------------
8371 -- Get_Gen_Id --
8372 ----------------
8374 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8375 begin
8376 return Generic_Renamings.Table (E).Gen_Id;
8377 end Get_Gen_Id;
8379 ---------------------
8380 -- Get_Instance_Of --
8381 ---------------------
8383 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8384 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8386 begin
8387 if Res /= Assoc_Null then
8388 return Generic_Renamings.Table (Res).Act_Id;
8390 else
8391 -- On exit, entity is not instantiated: not a generic parameter, or
8392 -- else parameter of an inner generic unit.
8394 return A;
8395 end if;
8396 end Get_Instance_Of;
8398 ------------------------------------
8399 -- Get_Package_Instantiation_Node --
8400 ------------------------------------
8402 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8403 Decl : Node_Id := Unit_Declaration_Node (A);
8404 Inst : Node_Id;
8406 begin
8407 -- If the Package_Instantiation attribute has been set on the package
8408 -- entity, then use it directly when it (or its Original_Node) refers
8409 -- to an N_Package_Instantiation node. In principle it should be
8410 -- possible to have this field set in all cases, which should be
8411 -- investigated, and would allow this function to be significantly
8412 -- simplified. ???
8414 Inst := Package_Instantiation (A);
8416 if Present (Inst) then
8417 if Nkind (Inst) = N_Package_Instantiation then
8418 return Inst;
8420 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8421 return Original_Node (Inst);
8422 end if;
8423 end if;
8425 -- If the instantiation is a compilation unit that does not need body
8426 -- then the instantiation node has been rewritten as a package
8427 -- declaration for the instance, and we return the original node.
8429 -- If it is a compilation unit and the instance node has not been
8430 -- rewritten, then it is still the unit of the compilation. Finally, if
8431 -- a body is present, this is a parent of the main unit whose body has
8432 -- been compiled for inlining purposes, and the instantiation node has
8433 -- been rewritten with the instance body.
8435 -- Otherwise the instantiation node appears after the declaration. If
8436 -- the entity is a formal package, the declaration may have been
8437 -- rewritten as a generic declaration (in the case of a formal with box)
8438 -- or left as a formal package declaration if it has actuals, and is
8439 -- found with a forward search.
8441 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8442 if Nkind (Decl) = N_Package_Declaration
8443 and then Present (Corresponding_Body (Decl))
8444 then
8445 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8446 end if;
8448 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8449 return Original_Node (Decl);
8450 else
8451 return Unit (Parent (Decl));
8452 end if;
8454 elsif Nkind (Decl) = N_Package_Declaration
8455 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8456 then
8457 return Original_Node (Decl);
8459 else
8460 Inst := Next (Decl);
8461 while not Nkind_In (Inst, N_Package_Instantiation,
8462 N_Formal_Package_Declaration)
8463 loop
8464 Next (Inst);
8465 end loop;
8467 return Inst;
8468 end if;
8469 end Get_Package_Instantiation_Node;
8471 ------------------------
8472 -- Has_Been_Exchanged --
8473 ------------------------
8475 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8476 Next : Elmt_Id;
8478 begin
8479 Next := First_Elmt (Exchanged_Views);
8480 while Present (Next) loop
8481 if Full_View (Node (Next)) = E then
8482 return True;
8483 end if;
8485 Next_Elmt (Next);
8486 end loop;
8488 return False;
8489 end Has_Been_Exchanged;
8491 ----------
8492 -- Hash --
8493 ----------
8495 function Hash (F : Entity_Id) return HTable_Range is
8496 begin
8497 return HTable_Range (F mod HTable_Size);
8498 end Hash;
8500 ------------------------
8501 -- Hide_Current_Scope --
8502 ------------------------
8504 procedure Hide_Current_Scope is
8505 C : constant Entity_Id := Current_Scope;
8506 E : Entity_Id;
8508 begin
8509 Set_Is_Hidden_Open_Scope (C);
8511 E := First_Entity (C);
8512 while Present (E) loop
8513 if Is_Immediately_Visible (E) then
8514 Set_Is_Immediately_Visible (E, False);
8515 Append_Elmt (E, Hidden_Entities);
8516 end if;
8518 Next_Entity (E);
8519 end loop;
8521 -- Make the scope name invisible as well. This is necessary, but might
8522 -- conflict with calls to Rtsfind later on, in case the scope is a
8523 -- predefined one. There is no clean solution to this problem, so for
8524 -- now we depend on the user not redefining Standard itself in one of
8525 -- the parent units.
8527 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8528 Set_Is_Immediately_Visible (C, False);
8529 Append_Elmt (C, Hidden_Entities);
8530 end if;
8532 end Hide_Current_Scope;
8534 --------------
8535 -- Init_Env --
8536 --------------
8538 procedure Init_Env is
8539 Saved : Instance_Env;
8541 begin
8542 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8543 Saved.Exchanged_Views := Exchanged_Views;
8544 Saved.Hidden_Entities := Hidden_Entities;
8545 Saved.Current_Sem_Unit := Current_Sem_Unit;
8546 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8547 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8549 -- Save configuration switches. These may be reset if the unit is a
8550 -- predefined unit, and the current mode is not Ada 2005.
8552 Save_Opt_Config_Switches (Saved.Switches);
8554 Instance_Envs.Append (Saved);
8556 Exchanged_Views := New_Elmt_List;
8557 Hidden_Entities := New_Elmt_List;
8559 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8560 -- this is set properly in Set_Instance_Env.
8562 Current_Instantiated_Parent :=
8563 (Current_Scope, Current_Scope, Assoc_Null);
8564 end Init_Env;
8566 ------------------------------
8567 -- In_Same_Declarative_Part --
8568 ------------------------------
8570 function In_Same_Declarative_Part
8571 (F_Node : Node_Id;
8572 Inst : Node_Id) return Boolean
8574 Decls : constant Node_Id := Parent (F_Node);
8575 Nod : Node_Id;
8577 begin
8578 Nod := Parent (Inst);
8579 while Present (Nod) loop
8580 if Nod = Decls then
8581 return True;
8583 elsif Nkind_In (Nod, N_Subprogram_Body,
8584 N_Package_Body,
8585 N_Package_Declaration,
8586 N_Task_Body,
8587 N_Protected_Body,
8588 N_Block_Statement)
8589 then
8590 return False;
8592 elsif Nkind (Nod) = N_Subunit then
8593 Nod := Corresponding_Stub (Nod);
8595 elsif Nkind (Nod) = N_Compilation_Unit then
8596 return False;
8598 else
8599 Nod := Parent (Nod);
8600 end if;
8601 end loop;
8603 return False;
8604 end In_Same_Declarative_Part;
8606 ---------------------
8607 -- In_Main_Context --
8608 ---------------------
8610 function In_Main_Context (E : Entity_Id) return Boolean is
8611 Context : List_Id;
8612 Clause : Node_Id;
8613 Nam : Node_Id;
8615 begin
8616 if not Is_Compilation_Unit (E)
8617 or else Ekind (E) /= E_Package
8618 or else In_Private_Part (E)
8619 then
8620 return False;
8621 end if;
8623 Context := Context_Items (Cunit (Main_Unit));
8625 Clause := First (Context);
8626 while Present (Clause) loop
8627 if Nkind (Clause) = N_With_Clause then
8628 Nam := Name (Clause);
8630 -- If the current scope is part of the context of the main unit,
8631 -- analysis of the corresponding with_clause is not complete, and
8632 -- the entity is not set. We use the Chars field directly, which
8633 -- might produce false positives in rare cases, but guarantees
8634 -- that we produce all the instance bodies we will need.
8636 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8637 or else (Nkind (Nam) = N_Selected_Component
8638 and then Chars (Selector_Name (Nam)) = Chars (E))
8639 then
8640 return True;
8641 end if;
8642 end if;
8644 Next (Clause);
8645 end loop;
8647 return False;
8648 end In_Main_Context;
8650 ---------------------
8651 -- Inherit_Context --
8652 ---------------------
8654 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8655 Current_Context : List_Id;
8656 Current_Unit : Node_Id;
8657 Item : Node_Id;
8658 New_I : Node_Id;
8660 Clause : Node_Id;
8661 OK : Boolean;
8662 Lib_Unit : Node_Id;
8664 begin
8665 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8667 -- The inherited context is attached to the enclosing compilation
8668 -- unit. This is either the main unit, or the declaration for the
8669 -- main unit (in case the instantiation appears within the package
8670 -- declaration and the main unit is its body).
8672 Current_Unit := Parent (Inst);
8673 while Present (Current_Unit)
8674 and then Nkind (Current_Unit) /= N_Compilation_Unit
8675 loop
8676 Current_Unit := Parent (Current_Unit);
8677 end loop;
8679 Current_Context := Context_Items (Current_Unit);
8681 Item := First (Context_Items (Parent (Gen_Decl)));
8682 while Present (Item) loop
8683 if Nkind (Item) = N_With_Clause then
8684 Lib_Unit := Library_Unit (Item);
8686 -- Take care to prevent direct cyclic with's
8688 if Lib_Unit /= Current_Unit then
8690 -- Do not add a unit if it is already in the context
8692 Clause := First (Current_Context);
8693 OK := True;
8694 while Present (Clause) loop
8695 if Nkind (Clause) = N_With_Clause and then
8696 Library_Unit (Clause) = Lib_Unit
8697 then
8698 OK := False;
8699 exit;
8700 end if;
8702 Next (Clause);
8703 end loop;
8705 if OK then
8706 New_I := New_Copy (Item);
8707 Set_Implicit_With (New_I, True);
8708 Set_Implicit_With_From_Instantiation (New_I, True);
8709 Append (New_I, Current_Context);
8710 end if;
8711 end if;
8712 end if;
8714 Next (Item);
8715 end loop;
8716 end if;
8717 end Inherit_Context;
8719 ----------------
8720 -- Initialize --
8721 ----------------
8723 procedure Initialize is
8724 begin
8725 Generic_Renamings.Init;
8726 Instance_Envs.Init;
8727 Generic_Flags.Init;
8728 Generic_Renamings_HTable.Reset;
8729 Circularity_Detected := False;
8730 Exchanged_Views := No_Elist;
8731 Hidden_Entities := No_Elist;
8732 end Initialize;
8734 -------------------------------------
8735 -- Insert_Freeze_Node_For_Instance --
8736 -------------------------------------
8738 procedure Insert_Freeze_Node_For_Instance
8739 (N : Node_Id;
8740 F_Node : Node_Id)
8742 Decl : Node_Id;
8743 Decls : List_Id;
8744 Inst : Entity_Id;
8745 Par_N : Node_Id;
8747 function Enclosing_Body (N : Node_Id) return Node_Id;
8748 -- Find enclosing package or subprogram body, if any. Freeze node may
8749 -- be placed at end of current declarative list if previous instance
8750 -- and current one have different enclosing bodies.
8752 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8753 -- Find the local instance, if any, that declares the generic that is
8754 -- being instantiated. If present, the freeze node for this instance
8755 -- must follow the freeze node for the previous instance.
8757 --------------------
8758 -- Enclosing_Body --
8759 --------------------
8761 function Enclosing_Body (N : Node_Id) return Node_Id is
8762 P : Node_Id;
8764 begin
8765 P := Parent (N);
8766 while Present (P)
8767 and then Nkind (Parent (P)) /= N_Compilation_Unit
8768 loop
8769 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8770 if Nkind (Parent (P)) = N_Subunit then
8771 return Corresponding_Stub (Parent (P));
8772 else
8773 return P;
8774 end if;
8775 end if;
8777 P := True_Parent (P);
8778 end loop;
8780 return Empty;
8781 end Enclosing_Body;
8783 -----------------------
8784 -- Previous_Instance --
8785 -----------------------
8787 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8788 S : Entity_Id;
8790 begin
8791 S := Scope (Gen);
8792 while Present (S) and then S /= Standard_Standard loop
8793 if Is_Generic_Instance (S)
8794 and then In_Same_Source_Unit (S, N)
8795 then
8796 return S;
8797 end if;
8799 S := Scope (S);
8800 end loop;
8802 return Empty;
8803 end Previous_Instance;
8805 -- Start of processing for Insert_Freeze_Node_For_Instance
8807 begin
8808 if not Is_List_Member (F_Node) then
8809 Decl := N;
8810 Decls := List_Containing (N);
8811 Inst := Entity (F_Node);
8812 Par_N := Parent (Decls);
8814 -- When processing a subprogram instantiation, utilize the actual
8815 -- subprogram instantiation rather than its package wrapper as it
8816 -- carries all the context information.
8818 if Is_Wrapper_Package (Inst) then
8819 Inst := Related_Instance (Inst);
8820 end if;
8822 -- If this is a package instance, check whether the generic is
8823 -- declared in a previous instance and the current instance is
8824 -- not within the previous one.
8826 if Present (Generic_Parent (Parent (Inst)))
8827 and then Is_In_Main_Unit (N)
8828 then
8829 declare
8830 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8831 Par_I : constant Entity_Id :=
8832 Previous_Instance
8833 (Generic_Parent (Parent (Inst)));
8834 Scop : Entity_Id;
8836 begin
8837 if Present (Par_I)
8838 and then Earlier (N, Freeze_Node (Par_I))
8839 then
8840 Scop := Scope (Inst);
8842 -- If the current instance is within the one that contains
8843 -- the generic, the freeze node for the current one must
8844 -- appear in the current declarative part. Ditto, if the
8845 -- current instance is within another package instance or
8846 -- within a body that does not enclose the current instance.
8847 -- In these three cases the freeze node of the previous
8848 -- instance is not relevant.
8850 while Present (Scop) and then Scop /= Standard_Standard loop
8851 exit when Scop = Par_I
8852 or else
8853 (Is_Generic_Instance (Scop)
8854 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8855 Scop := Scope (Scop);
8856 end loop;
8858 -- Previous instance encloses current instance
8860 if Scop = Par_I then
8861 null;
8863 -- If the next node is a source body we must freeze in
8864 -- the current scope as well.
8866 elsif Present (Next (N))
8867 and then Nkind_In (Next (N), N_Subprogram_Body,
8868 N_Package_Body)
8869 and then Comes_From_Source (Next (N))
8870 then
8871 null;
8873 -- Current instance is within an unrelated instance
8875 elsif Is_Generic_Instance (Scop) then
8876 null;
8878 -- Current instance is within an unrelated body
8880 elsif Present (Enclosing_N)
8881 and then Enclosing_N /= Enclosing_Body (Par_I)
8882 then
8883 null;
8885 else
8886 Insert_After (Freeze_Node (Par_I), F_Node);
8887 return;
8888 end if;
8889 end if;
8890 end;
8891 end if;
8893 -- When the instantiation occurs in a package declaration, append the
8894 -- freeze node to the private declarations (if any).
8896 if Nkind (Par_N) = N_Package_Specification
8897 and then Decls = Visible_Declarations (Par_N)
8898 and then Present (Private_Declarations (Par_N))
8899 and then not Is_Empty_List (Private_Declarations (Par_N))
8900 then
8901 Decls := Private_Declarations (Par_N);
8902 Decl := First (Decls);
8903 end if;
8905 -- Determine the proper freeze point of a package instantiation. We
8906 -- adhere to the general rule of a package or subprogram body causing
8907 -- freezing of anything before it in the same declarative region. In
8908 -- this case, the proper freeze point of a package instantiation is
8909 -- before the first source body which follows, or before a stub. This
8910 -- ensures that entities coming from the instance are already frozen
8911 -- and usable in source bodies.
8913 if Nkind (Par_N) /= N_Package_Declaration
8914 and then Ekind (Inst) = E_Package
8915 and then Is_Generic_Instance (Inst)
8916 and then
8917 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8918 then
8919 while Present (Decl) loop
8920 if (Nkind (Decl) in N_Unit_Body
8921 or else
8922 Nkind (Decl) in N_Body_Stub)
8923 and then Comes_From_Source (Decl)
8924 then
8925 Insert_Before (Decl, F_Node);
8926 return;
8927 end if;
8929 Next (Decl);
8930 end loop;
8931 end if;
8933 -- In a package declaration, or if no previous body, insert at end
8934 -- of list.
8936 Set_Sloc (F_Node, Sloc (Last (Decls)));
8937 Insert_After (Last (Decls), F_Node);
8938 end if;
8939 end Insert_Freeze_Node_For_Instance;
8941 ------------------
8942 -- Install_Body --
8943 ------------------
8945 procedure Install_Body
8946 (Act_Body : Node_Id;
8947 N : Node_Id;
8948 Gen_Body : Node_Id;
8949 Gen_Decl : Node_Id)
8951 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
8952 -- Check if the generic definition and the instantiation come from
8953 -- a common scope, in which case the instance must be frozen after
8954 -- the generic body.
8956 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
8957 -- If the instance is nested inside a generic unit, the Sloc of the
8958 -- instance indicates the place of the original definition, not the
8959 -- point of the current enclosing instance. Pending a better usage of
8960 -- Slocs to indicate instantiation places, we determine the place of
8961 -- origin of a node by finding the maximum sloc of any ancestor node.
8962 -- Why is this not equivalent to Top_Level_Location ???
8964 -------------------
8965 -- In_Same_Scope --
8966 -------------------
8968 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
8969 Act_Scop : Entity_Id := Scope (Act_Id);
8970 Gen_Scop : Entity_Id := Scope (Gen_Id);
8972 begin
8973 while Act_Scop /= Standard_Standard
8974 and then Gen_Scop /= Standard_Standard
8975 loop
8976 if Act_Scop = Gen_Scop then
8977 return True;
8978 end if;
8980 Act_Scop := Scope (Act_Scop);
8981 Gen_Scop := Scope (Gen_Scop);
8982 end loop;
8984 return False;
8985 end In_Same_Scope;
8987 ---------------
8988 -- True_Sloc --
8989 ---------------
8991 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
8992 N1 : Node_Id;
8993 Res : Source_Ptr;
8995 begin
8996 Res := Sloc (N);
8997 N1 := N;
8998 while Present (N1) and then N1 /= Act_Unit loop
8999 if Sloc (N1) > Res then
9000 Res := Sloc (N1);
9001 end if;
9003 N1 := Parent (N1);
9004 end loop;
9006 return Res;
9007 end True_Sloc;
9009 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9010 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9011 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9012 Par : constant Entity_Id := Scope (Gen_Id);
9013 Gen_Unit : constant Node_Id :=
9014 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9016 Body_Unit : Node_Id;
9017 F_Node : Node_Id;
9018 Must_Delay : Boolean;
9019 Orig_Body : Node_Id := Gen_Body;
9021 -- Start of processing for Install_Body
9023 begin
9024 -- Handle first the case of an instance with incomplete actual types.
9025 -- The instance body cannot be placed after the declaration because
9026 -- full views have not been seen yet. Any use of the non-limited views
9027 -- in the instance body requires the presence of a regular with_clause
9028 -- in the enclosing unit, and will fail if this with_clause is missing.
9029 -- We place the instance body at the beginning of the enclosing body,
9030 -- which is the unit being compiled. The freeze node for the instance
9031 -- is then placed after the instance body.
9033 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9034 and then Expander_Active
9035 and then Ekind (Scope (Act_Id)) = E_Package
9036 then
9037 declare
9038 Scop : constant Entity_Id := Scope (Act_Id);
9039 Body_Id : constant Node_Id :=
9040 Corresponding_Body (Unit_Declaration_Node (Scop));
9042 begin
9043 Ensure_Freeze_Node (Act_Id);
9044 F_Node := Freeze_Node (Act_Id);
9045 if Present (Body_Id) then
9046 Set_Is_Frozen (Act_Id, False);
9047 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9048 if Is_List_Member (F_Node) then
9049 Remove (F_Node);
9050 end if;
9052 Insert_After (Act_Body, F_Node);
9053 end if;
9054 end;
9055 return;
9056 end if;
9058 -- If the body is a subunit, the freeze point is the corresponding stub
9059 -- in the current compilation, not the subunit itself.
9061 if Nkind (Parent (Gen_Body)) = N_Subunit then
9062 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9063 else
9064 Orig_Body := Gen_Body;
9065 end if;
9067 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9069 -- If the instantiation and the generic definition appear in the same
9070 -- package declaration, this is an early instantiation. If they appear
9071 -- in the same declarative part, it is an early instantiation only if
9072 -- the generic body appears textually later, and the generic body is
9073 -- also in the main unit.
9075 -- If instance is nested within a subprogram, and the generic body
9076 -- is not, the instance is delayed because the enclosing body is. If
9077 -- instance and body are within the same scope, or the same subprogram
9078 -- body, indicate explicitly that the instance is delayed.
9080 Must_Delay :=
9081 (Gen_Unit = Act_Unit
9082 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9083 N_Package_Declaration)
9084 or else (Gen_Unit = Body_Unit
9085 and then True_Sloc (N, Act_Unit)
9086 < Sloc (Orig_Body)))
9087 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9088 and then In_Same_Scope (Gen_Id, Act_Id));
9090 -- If this is an early instantiation, the freeze node is placed after
9091 -- the generic body. Otherwise, if the generic appears in an instance,
9092 -- we cannot freeze the current instance until the outer one is frozen.
9093 -- This is only relevant if the current instance is nested within some
9094 -- inner scope not itself within the outer instance. If this scope is
9095 -- a package body in the same declarative part as the outer instance,
9096 -- then that body needs to be frozen after the outer instance. Finally,
9097 -- if no delay is needed, we place the freeze node at the end of the
9098 -- current declarative part.
9100 if Expander_Active then
9101 Ensure_Freeze_Node (Act_Id);
9102 F_Node := Freeze_Node (Act_Id);
9104 if Must_Delay then
9105 Insert_After (Orig_Body, F_Node);
9107 elsif Is_Generic_Instance (Par)
9108 and then Present (Freeze_Node (Par))
9109 and then Scope (Act_Id) /= Par
9110 then
9111 -- Freeze instance of inner generic after instance of enclosing
9112 -- generic.
9114 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9116 -- Handle the following case:
9118 -- package Parent_Inst is new ...
9119 -- Parent_Inst []
9121 -- procedure P ... -- this body freezes Parent_Inst
9123 -- package Inst is new ...
9125 -- In this particular scenario, the freeze node for Inst must
9126 -- be inserted in the same manner as that of Parent_Inst,
9127 -- before the next source body or at the end of the declarative
9128 -- list (body not available). If body P did not exist and
9129 -- Parent_Inst was frozen after Inst, either by a body
9130 -- following Inst or at the end of the declarative region,
9131 -- the freeze node for Inst must be inserted after that of
9132 -- Parent_Inst. This relation is established by comparing
9133 -- the Slocs of Parent_Inst freeze node and Inst.
9135 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9136 List_Containing (N)
9137 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9138 then
9139 Insert_Freeze_Node_For_Instance (N, F_Node);
9140 else
9141 Insert_After (Freeze_Node (Par), F_Node);
9142 end if;
9144 -- Freeze package enclosing instance of inner generic after
9145 -- instance of enclosing generic.
9147 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9148 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9149 then
9150 declare
9151 Enclosing : Entity_Id;
9153 begin
9154 Enclosing := Corresponding_Spec (Parent (N));
9156 if No (Enclosing) then
9157 Enclosing := Defining_Entity (Parent (N));
9158 end if;
9160 Insert_Freeze_Node_For_Instance (N, F_Node);
9161 Ensure_Freeze_Node (Enclosing);
9163 if not Is_List_Member (Freeze_Node (Enclosing)) then
9165 -- The enclosing context is a subunit, insert the freeze
9166 -- node after the stub.
9168 if Nkind (Parent (Parent (N))) = N_Subunit then
9169 Insert_Freeze_Node_For_Instance
9170 (Corresponding_Stub (Parent (Parent (N))),
9171 Freeze_Node (Enclosing));
9173 -- The enclosing context is a package with a stub body
9174 -- which has already been replaced by the real body.
9175 -- Insert the freeze node after the actual body.
9177 elsif Ekind (Enclosing) = E_Package
9178 and then Present (Body_Entity (Enclosing))
9179 and then Was_Originally_Stub
9180 (Parent (Body_Entity (Enclosing)))
9181 then
9182 Insert_Freeze_Node_For_Instance
9183 (Parent (Body_Entity (Enclosing)),
9184 Freeze_Node (Enclosing));
9186 -- The parent instance has been frozen before the body of
9187 -- the enclosing package, insert the freeze node after
9188 -- the body.
9190 elsif List_Containing (Freeze_Node (Par)) =
9191 List_Containing (Parent (N))
9192 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9193 then
9194 Insert_Freeze_Node_For_Instance
9195 (Parent (N), Freeze_Node (Enclosing));
9197 else
9198 Insert_After
9199 (Freeze_Node (Par), Freeze_Node (Enclosing));
9200 end if;
9201 end if;
9202 end;
9204 else
9205 Insert_Freeze_Node_For_Instance (N, F_Node);
9206 end if;
9208 else
9209 Insert_Freeze_Node_For_Instance (N, F_Node);
9210 end if;
9211 end if;
9213 Set_Is_Frozen (Act_Id);
9214 Insert_Before (N, Act_Body);
9215 Mark_Rewrite_Insertion (Act_Body);
9216 end Install_Body;
9218 -----------------------------
9219 -- Install_Formal_Packages --
9220 -----------------------------
9222 procedure Install_Formal_Packages (Par : Entity_Id) is
9223 E : Entity_Id;
9224 Gen : Entity_Id;
9225 Gen_E : Entity_Id := Empty;
9227 begin
9228 E := First_Entity (Par);
9230 -- If we are installing an instance parent, locate the formal packages
9231 -- of its generic parent.
9233 if Is_Generic_Instance (Par) then
9234 Gen := Generic_Parent (Package_Specification (Par));
9235 Gen_E := First_Entity (Gen);
9236 end if;
9238 while Present (E) loop
9239 if Ekind (E) = E_Package
9240 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9241 then
9242 -- If this is the renaming for the parent instance, done
9244 if Renamed_Object (E) = Par then
9245 exit;
9247 -- The visibility of a formal of an enclosing generic is already
9248 -- correct.
9250 elsif Denotes_Formal_Package (E) then
9251 null;
9253 elsif Present (Associated_Formal_Package (E)) then
9254 Check_Generic_Actuals (Renamed_Object (E), True);
9255 Set_Is_Hidden (E, False);
9257 -- Find formal package in generic unit that corresponds to
9258 -- (instance of) formal package in instance.
9260 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9261 Next_Entity (Gen_E);
9262 end loop;
9264 if Present (Gen_E) then
9265 Map_Formal_Package_Entities (Gen_E, E);
9266 end if;
9267 end if;
9268 end if;
9270 Next_Entity (E);
9272 if Present (Gen_E) then
9273 Next_Entity (Gen_E);
9274 end if;
9275 end loop;
9276 end Install_Formal_Packages;
9278 --------------------
9279 -- Install_Parent --
9280 --------------------
9282 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9283 Ancestors : constant Elist_Id := New_Elmt_List;
9284 S : constant Entity_Id := Current_Scope;
9285 Inst_Par : Entity_Id;
9286 First_Par : Entity_Id;
9287 Inst_Node : Node_Id;
9288 Gen_Par : Entity_Id;
9289 First_Gen : Entity_Id;
9290 Elmt : Elmt_Id;
9292 procedure Install_Noninstance_Specs (Par : Entity_Id);
9293 -- Install the scopes of noninstance parent units ending with Par
9295 procedure Install_Spec (Par : Entity_Id);
9296 -- The child unit is within the declarative part of the parent, so the
9297 -- declarations within the parent are immediately visible.
9299 -------------------------------
9300 -- Install_Noninstance_Specs --
9301 -------------------------------
9303 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9304 begin
9305 if Present (Par)
9306 and then Par /= Standard_Standard
9307 and then not In_Open_Scopes (Par)
9308 then
9309 Install_Noninstance_Specs (Scope (Par));
9310 Install_Spec (Par);
9311 end if;
9312 end Install_Noninstance_Specs;
9314 ------------------
9315 -- Install_Spec --
9316 ------------------
9318 procedure Install_Spec (Par : Entity_Id) is
9319 Spec : constant Node_Id := Package_Specification (Par);
9321 begin
9322 -- If this parent of the child instance is a top-level unit,
9323 -- then record the unit and its visibility for later resetting in
9324 -- Remove_Parent. We exclude units that are generic instances, as we
9325 -- only want to record this information for the ultimate top-level
9326 -- noninstance parent (is that always correct???).
9328 if Scope (Par) = Standard_Standard
9329 and then not Is_Generic_Instance (Par)
9330 then
9331 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9332 Instance_Parent_Unit := Par;
9333 end if;
9335 -- Open the parent scope and make it and its declarations visible.
9336 -- If this point is not within a body, then only the visible
9337 -- declarations should be made visible, and installation of the
9338 -- private declarations is deferred until the appropriate point
9339 -- within analysis of the spec being instantiated (see the handling
9340 -- of parent visibility in Analyze_Package_Specification). This is
9341 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9342 -- private view problems that occur when compiling instantiations of
9343 -- a generic child of that package (Generic_Dispatching_Constructor).
9344 -- If the instance freezes a tagged type, inlinings of operations
9345 -- from Ada.Tags may need the full view of type Tag. If inlining took
9346 -- proper account of establishing visibility of inlined subprograms'
9347 -- parents then it should be possible to remove this
9348 -- special check. ???
9350 Push_Scope (Par);
9351 Set_Is_Immediately_Visible (Par);
9352 Install_Visible_Declarations (Par);
9353 Set_Use (Visible_Declarations (Spec));
9355 if In_Body or else Is_RTU (Par, Ada_Tags) then
9356 Install_Private_Declarations (Par);
9357 Set_Use (Private_Declarations (Spec));
9358 end if;
9359 end Install_Spec;
9361 -- Start of processing for Install_Parent
9363 begin
9364 -- We need to install the parent instance to compile the instantiation
9365 -- of the child, but the child instance must appear in the current
9366 -- scope. Given that we cannot place the parent above the current scope
9367 -- in the scope stack, we duplicate the current scope and unstack both
9368 -- after the instantiation is complete.
9370 -- If the parent is itself the instantiation of a child unit, we must
9371 -- also stack the instantiation of its parent, and so on. Each such
9372 -- ancestor is the prefix of the name in a prior instantiation.
9374 -- If this is a nested instance, the parent unit itself resolves to
9375 -- a renaming of the parent instance, whose declaration we need.
9377 -- Finally, the parent may be a generic (not an instance) when the
9378 -- child unit appears as a formal package.
9380 Inst_Par := P;
9382 if Present (Renamed_Entity (Inst_Par)) then
9383 Inst_Par := Renamed_Entity (Inst_Par);
9384 end if;
9386 First_Par := Inst_Par;
9388 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9390 First_Gen := Gen_Par;
9392 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9394 -- Load grandparent instance as well
9396 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9398 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9399 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9401 if Present (Renamed_Entity (Inst_Par)) then
9402 Inst_Par := Renamed_Entity (Inst_Par);
9403 end if;
9405 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9407 if Present (Gen_Par) then
9408 Prepend_Elmt (Inst_Par, Ancestors);
9410 else
9411 -- Parent is not the name of an instantiation
9413 Install_Noninstance_Specs (Inst_Par);
9414 exit;
9415 end if;
9417 else
9418 -- Previous error
9420 exit;
9421 end if;
9422 end loop;
9424 if Present (First_Gen) then
9425 Append_Elmt (First_Par, Ancestors);
9426 else
9427 Install_Noninstance_Specs (First_Par);
9428 end if;
9430 if not Is_Empty_Elmt_List (Ancestors) then
9431 Elmt := First_Elmt (Ancestors);
9432 while Present (Elmt) loop
9433 Install_Spec (Node (Elmt));
9434 Install_Formal_Packages (Node (Elmt));
9435 Next_Elmt (Elmt);
9436 end loop;
9437 end if;
9439 if not In_Body then
9440 Push_Scope (S);
9441 end if;
9442 end Install_Parent;
9444 -------------------------------
9445 -- Install_Hidden_Primitives --
9446 -------------------------------
9448 procedure Install_Hidden_Primitives
9449 (Prims_List : in out Elist_Id;
9450 Gen_T : Entity_Id;
9451 Act_T : Entity_Id)
9453 Elmt : Elmt_Id;
9454 List : Elist_Id := No_Elist;
9455 Prim_G_Elmt : Elmt_Id;
9456 Prim_A_Elmt : Elmt_Id;
9457 Prim_G : Node_Id;
9458 Prim_A : Node_Id;
9460 begin
9461 -- No action needed in case of serious errors because we cannot trust
9462 -- in the order of primitives
9464 if Serious_Errors_Detected > 0 then
9465 return;
9467 -- No action possible if we don't have available the list of primitive
9468 -- operations
9470 elsif No (Gen_T)
9471 or else not Is_Record_Type (Gen_T)
9472 or else not Is_Tagged_Type (Gen_T)
9473 or else not Is_Record_Type (Act_T)
9474 or else not Is_Tagged_Type (Act_T)
9475 then
9476 return;
9478 -- There is no need to handle interface types since their primitives
9479 -- cannot be hidden
9481 elsif Is_Interface (Gen_T) then
9482 return;
9483 end if;
9485 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9487 if not Is_Class_Wide_Type (Act_T) then
9488 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9489 else
9490 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9491 end if;
9493 loop
9494 -- Skip predefined primitives in the generic formal
9496 while Present (Prim_G_Elmt)
9497 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9498 loop
9499 Next_Elmt (Prim_G_Elmt);
9500 end loop;
9502 -- Skip predefined primitives in the generic actual
9504 while Present (Prim_A_Elmt)
9505 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9506 loop
9507 Next_Elmt (Prim_A_Elmt);
9508 end loop;
9510 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9512 Prim_G := Node (Prim_G_Elmt);
9513 Prim_A := Node (Prim_A_Elmt);
9515 -- There is no need to handle interface primitives because their
9516 -- primitives are not hidden
9518 exit when Present (Interface_Alias (Prim_G));
9520 -- Here we install one hidden primitive
9522 if Chars (Prim_G) /= Chars (Prim_A)
9523 and then Has_Suffix (Prim_A, 'P')
9524 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9525 then
9526 Set_Chars (Prim_A, Chars (Prim_G));
9527 Append_New_Elmt (Prim_A, To => List);
9528 end if;
9530 Next_Elmt (Prim_A_Elmt);
9531 Next_Elmt (Prim_G_Elmt);
9532 end loop;
9534 -- Append the elements to the list of temporarily visible primitives
9535 -- avoiding duplicates.
9537 if Present (List) then
9538 if No (Prims_List) then
9539 Prims_List := New_Elmt_List;
9540 end if;
9542 Elmt := First_Elmt (List);
9543 while Present (Elmt) loop
9544 Append_Unique_Elmt (Node (Elmt), Prims_List);
9545 Next_Elmt (Elmt);
9546 end loop;
9547 end if;
9548 end Install_Hidden_Primitives;
9550 -------------------------------
9551 -- Restore_Hidden_Primitives --
9552 -------------------------------
9554 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9555 Prim_Elmt : Elmt_Id;
9556 Prim : Node_Id;
9558 begin
9559 if Prims_List /= No_Elist then
9560 Prim_Elmt := First_Elmt (Prims_List);
9561 while Present (Prim_Elmt) loop
9562 Prim := Node (Prim_Elmt);
9563 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9564 Next_Elmt (Prim_Elmt);
9565 end loop;
9567 Prims_List := No_Elist;
9568 end if;
9569 end Restore_Hidden_Primitives;
9571 --------------------------------
9572 -- Instantiate_Formal_Package --
9573 --------------------------------
9575 function Instantiate_Formal_Package
9576 (Formal : Node_Id;
9577 Actual : Node_Id;
9578 Analyzed_Formal : Node_Id) return List_Id
9580 Loc : constant Source_Ptr := Sloc (Actual);
9581 Actual_Pack : Entity_Id;
9582 Formal_Pack : Entity_Id;
9583 Gen_Parent : Entity_Id;
9584 Decls : List_Id;
9585 Nod : Node_Id;
9586 Parent_Spec : Node_Id;
9588 procedure Find_Matching_Actual
9589 (F : Node_Id;
9590 Act : in out Entity_Id);
9591 -- We need to associate each formal entity in the formal package with
9592 -- the corresponding entity in the actual package. The actual package
9593 -- has been analyzed and possibly expanded, and as a result there is
9594 -- no one-to-one correspondence between the two lists (for example,
9595 -- the actual may include subtypes, itypes, and inherited primitive
9596 -- operations, interspersed among the renaming declarations for the
9597 -- actuals). We retrieve the corresponding actual by name because each
9598 -- actual has the same name as the formal, and they do appear in the
9599 -- same order.
9601 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9602 -- Retrieve entity of defining entity of generic formal parameter.
9603 -- Only the declarations of formals need to be considered when
9604 -- linking them to actuals, but the declarative list may include
9605 -- internal entities generated during analysis, and those are ignored.
9607 procedure Match_Formal_Entity
9608 (Formal_Node : Node_Id;
9609 Formal_Ent : Entity_Id;
9610 Actual_Ent : Entity_Id);
9611 -- Associates the formal entity with the actual. In the case where
9612 -- Formal_Ent is a formal package, this procedure iterates through all
9613 -- of its formals and enters associations between the actuals occurring
9614 -- in the formal package's corresponding actual package (given by
9615 -- Actual_Ent) and the formal package's formal parameters. This
9616 -- procedure recurses if any of the parameters is itself a package.
9618 function Is_Instance_Of
9619 (Act_Spec : Entity_Id;
9620 Gen_Anc : Entity_Id) return Boolean;
9621 -- The actual can be an instantiation of a generic within another
9622 -- instance, in which case there is no direct link from it to the
9623 -- original generic ancestor. In that case, we recognize that the
9624 -- ultimate ancestor is the same by examining names and scopes.
9626 procedure Process_Nested_Formal (Formal : Entity_Id);
9627 -- If the current formal is declared with a box, its own formals are
9628 -- visible in the instance, as they were in the generic, and their
9629 -- Hidden flag must be reset. If some of these formals are themselves
9630 -- packages declared with a box, the processing must be recursive.
9632 --------------------------
9633 -- Find_Matching_Actual --
9634 --------------------------
9636 procedure Find_Matching_Actual
9637 (F : Node_Id;
9638 Act : in out Entity_Id)
9640 Formal_Ent : Entity_Id;
9642 begin
9643 case Nkind (Original_Node (F)) is
9644 when N_Formal_Object_Declaration |
9645 N_Formal_Type_Declaration =>
9646 Formal_Ent := Defining_Identifier (F);
9648 while Chars (Act) /= Chars (Formal_Ent) loop
9649 Next_Entity (Act);
9650 end loop;
9652 when N_Formal_Subprogram_Declaration |
9653 N_Formal_Package_Declaration |
9654 N_Package_Declaration |
9655 N_Generic_Package_Declaration =>
9656 Formal_Ent := Defining_Entity (F);
9658 while Chars (Act) /= Chars (Formal_Ent) loop
9659 Next_Entity (Act);
9660 end loop;
9662 when others =>
9663 raise Program_Error;
9664 end case;
9665 end Find_Matching_Actual;
9667 -------------------------
9668 -- Match_Formal_Entity --
9669 -------------------------
9671 procedure Match_Formal_Entity
9672 (Formal_Node : Node_Id;
9673 Formal_Ent : Entity_Id;
9674 Actual_Ent : Entity_Id)
9676 Act_Pkg : Entity_Id;
9678 begin
9679 Set_Instance_Of (Formal_Ent, Actual_Ent);
9681 if Ekind (Actual_Ent) = E_Package then
9683 -- Record associations for each parameter
9685 Act_Pkg := Actual_Ent;
9687 declare
9688 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9689 F_Ent : Entity_Id;
9690 F_Node : Node_Id;
9692 Gen_Decl : Node_Id;
9693 Formals : List_Id;
9694 Actual : Entity_Id;
9696 begin
9697 -- Retrieve the actual given in the formal package declaration
9699 Actual := Entity (Name (Original_Node (Formal_Node)));
9701 -- The actual in the formal package declaration may be a
9702 -- renamed generic package, in which case we want to retrieve
9703 -- the original generic in order to traverse its formal part.
9705 if Present (Renamed_Entity (Actual)) then
9706 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9707 else
9708 Gen_Decl := Unit_Declaration_Node (Actual);
9709 end if;
9711 Formals := Generic_Formal_Declarations (Gen_Decl);
9713 if Present (Formals) then
9714 F_Node := First_Non_Pragma (Formals);
9715 else
9716 F_Node := Empty;
9717 end if;
9719 while Present (A_Ent)
9720 and then Present (F_Node)
9721 and then A_Ent /= First_Private_Entity (Act_Pkg)
9722 loop
9723 F_Ent := Get_Formal_Entity (F_Node);
9725 if Present (F_Ent) then
9727 -- This is a formal of the original package. Record
9728 -- association and recurse.
9730 Find_Matching_Actual (F_Node, A_Ent);
9731 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9732 Next_Entity (A_Ent);
9733 end if;
9735 Next_Non_Pragma (F_Node);
9736 end loop;
9737 end;
9738 end if;
9739 end Match_Formal_Entity;
9741 -----------------------
9742 -- Get_Formal_Entity --
9743 -----------------------
9745 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9746 Kind : constant Node_Kind := Nkind (Original_Node (N));
9747 begin
9748 case Kind is
9749 when N_Formal_Object_Declaration =>
9750 return Defining_Identifier (N);
9752 when N_Formal_Type_Declaration =>
9753 return Defining_Identifier (N);
9755 when N_Formal_Subprogram_Declaration =>
9756 return Defining_Unit_Name (Specification (N));
9758 when N_Formal_Package_Declaration =>
9759 return Defining_Identifier (Original_Node (N));
9761 when N_Generic_Package_Declaration =>
9762 return Defining_Identifier (Original_Node (N));
9764 -- All other declarations are introduced by semantic analysis and
9765 -- have no match in the actual.
9767 when others =>
9768 return Empty;
9769 end case;
9770 end Get_Formal_Entity;
9772 --------------------
9773 -- Is_Instance_Of --
9774 --------------------
9776 function Is_Instance_Of
9777 (Act_Spec : Entity_Id;
9778 Gen_Anc : Entity_Id) return Boolean
9780 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9782 begin
9783 if No (Gen_Par) then
9784 return False;
9786 -- Simplest case: the generic parent of the actual is the formal
9788 elsif Gen_Par = Gen_Anc then
9789 return True;
9791 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9792 return False;
9794 -- The actual may be obtained through several instantiations. Its
9795 -- scope must itself be an instance of a generic declared in the
9796 -- same scope as the formal. Any other case is detected above.
9798 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9799 return False;
9801 else
9802 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9803 end if;
9804 end Is_Instance_Of;
9806 ---------------------------
9807 -- Process_Nested_Formal --
9808 ---------------------------
9810 procedure Process_Nested_Formal (Formal : Entity_Id) is
9811 Ent : Entity_Id;
9813 begin
9814 if Present (Associated_Formal_Package (Formal))
9815 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9816 then
9817 Ent := First_Entity (Formal);
9818 while Present (Ent) loop
9819 Set_Is_Hidden (Ent, False);
9820 Set_Is_Visible_Formal (Ent);
9821 Set_Is_Potentially_Use_Visible
9822 (Ent, Is_Potentially_Use_Visible (Formal));
9824 if Ekind (Ent) = E_Package then
9825 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9826 Process_Nested_Formal (Ent);
9827 end if;
9829 Next_Entity (Ent);
9830 end loop;
9831 end if;
9832 end Process_Nested_Formal;
9834 -- Start of processing for Instantiate_Formal_Package
9836 begin
9837 Analyze (Actual);
9839 if not Is_Entity_Name (Actual)
9840 or else Ekind (Entity (Actual)) /= E_Package
9841 then
9842 Error_Msg_N
9843 ("expect package instance to instantiate formal", Actual);
9844 Abandon_Instantiation (Actual);
9845 raise Program_Error;
9847 else
9848 Actual_Pack := Entity (Actual);
9849 Set_Is_Instantiated (Actual_Pack);
9851 -- The actual may be a renamed package, or an outer generic formal
9852 -- package whose instantiation is converted into a renaming.
9854 if Present (Renamed_Object (Actual_Pack)) then
9855 Actual_Pack := Renamed_Object (Actual_Pack);
9856 end if;
9858 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9859 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9860 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9861 else
9862 Gen_Parent :=
9863 Generic_Parent (Specification (Analyzed_Formal));
9864 Formal_Pack :=
9865 Defining_Unit_Name (Specification (Analyzed_Formal));
9866 end if;
9868 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9869 Parent_Spec := Package_Specification (Actual_Pack);
9870 else
9871 Parent_Spec := Parent (Actual_Pack);
9872 end if;
9874 if Gen_Parent = Any_Id then
9875 Error_Msg_N
9876 ("previous error in declaration of formal package", Actual);
9877 Abandon_Instantiation (Actual);
9879 elsif
9880 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9881 then
9882 null;
9884 else
9885 Error_Msg_NE
9886 ("actual parameter must be instance of&", Actual, Gen_Parent);
9887 Abandon_Instantiation (Actual);
9888 end if;
9890 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9891 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9893 Nod :=
9894 Make_Package_Renaming_Declaration (Loc,
9895 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9896 Name => New_Occurrence_Of (Actual_Pack, Loc));
9898 Set_Associated_Formal_Package
9899 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9900 Decls := New_List (Nod);
9902 -- If the formal F has a box, then the generic declarations are
9903 -- visible in the generic G. In an instance of G, the corresponding
9904 -- entities in the actual for F (which are the actuals for the
9905 -- instantiation of the generic that F denotes) must also be made
9906 -- visible for analysis of the current instance. On exit from the
9907 -- current instance, those entities are made private again. If the
9908 -- actual is currently in use, these entities are also use-visible.
9910 -- The loop through the actual entities also steps through the formal
9911 -- entities and enters associations from formals to actuals into the
9912 -- renaming map. This is necessary to properly handle checking of
9913 -- actual parameter associations for later formals that depend on
9914 -- actuals declared in the formal package.
9916 -- In Ada 2005, partial parameterization requires that we make
9917 -- visible the actuals corresponding to formals that were defaulted
9918 -- in the formal package. There formals are identified because they
9919 -- remain formal generics within the formal package, rather than
9920 -- being renamings of the actuals supplied.
9922 declare
9923 Gen_Decl : constant Node_Id :=
9924 Unit_Declaration_Node (Gen_Parent);
9925 Formals : constant List_Id :=
9926 Generic_Formal_Declarations (Gen_Decl);
9928 Actual_Ent : Entity_Id;
9929 Actual_Of_Formal : Node_Id;
9930 Formal_Node : Node_Id;
9931 Formal_Ent : Entity_Id;
9933 begin
9934 if Present (Formals) then
9935 Formal_Node := First_Non_Pragma (Formals);
9936 else
9937 Formal_Node := Empty;
9938 end if;
9940 Actual_Ent := First_Entity (Actual_Pack);
9941 Actual_Of_Formal :=
9942 First (Visible_Declarations (Specification (Analyzed_Formal)));
9943 while Present (Actual_Ent)
9944 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9945 loop
9946 if Present (Formal_Node) then
9947 Formal_Ent := Get_Formal_Entity (Formal_Node);
9949 if Present (Formal_Ent) then
9950 Find_Matching_Actual (Formal_Node, Actual_Ent);
9951 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9953 -- We iterate at the same time over the actuals of the
9954 -- local package created for the formal, to determine
9955 -- which one of the formals of the original generic were
9956 -- defaulted in the formal. The corresponding actual
9957 -- entities are visible in the enclosing instance.
9959 if Box_Present (Formal)
9960 or else
9961 (Present (Actual_Of_Formal)
9962 and then
9963 Is_Generic_Formal
9964 (Get_Formal_Entity (Actual_Of_Formal)))
9965 then
9966 Set_Is_Hidden (Actual_Ent, False);
9967 Set_Is_Visible_Formal (Actual_Ent);
9968 Set_Is_Potentially_Use_Visible
9969 (Actual_Ent, In_Use (Actual_Pack));
9971 if Ekind (Actual_Ent) = E_Package then
9972 Process_Nested_Formal (Actual_Ent);
9973 end if;
9975 else
9976 Set_Is_Hidden (Actual_Ent);
9977 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9978 end if;
9979 end if;
9981 Next_Non_Pragma (Formal_Node);
9982 Next (Actual_Of_Formal);
9984 else
9985 -- No further formals to match, but the generic part may
9986 -- contain inherited operation that are not hidden in the
9987 -- enclosing instance.
9989 Next_Entity (Actual_Ent);
9990 end if;
9991 end loop;
9993 -- Inherited subprograms generated by formal derived types are
9994 -- also visible if the types are.
9996 Actual_Ent := First_Entity (Actual_Pack);
9997 while Present (Actual_Ent)
9998 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9999 loop
10000 if Is_Overloadable (Actual_Ent)
10001 and then
10002 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10003 and then
10004 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10005 then
10006 Set_Is_Hidden (Actual_Ent, False);
10007 Set_Is_Potentially_Use_Visible
10008 (Actual_Ent, In_Use (Actual_Pack));
10009 end if;
10011 Next_Entity (Actual_Ent);
10012 end loop;
10013 end;
10015 -- If the formal is not declared with a box, reanalyze it as an
10016 -- abbreviated instantiation, to verify the matching rules of 12.7.
10017 -- The actual checks are performed after the generic associations
10018 -- have been analyzed, to guarantee the same visibility for this
10019 -- instantiation and for the actuals.
10021 -- In Ada 2005, the generic associations for the formal can include
10022 -- defaulted parameters. These are ignored during check. This
10023 -- internal instantiation is removed from the tree after conformance
10024 -- checking, because it contains formal declarations for those
10025 -- defaulted parameters, and those should not reach the back-end.
10027 if not Box_Present (Formal) then
10028 declare
10029 I_Pack : constant Entity_Id :=
10030 Make_Temporary (Sloc (Actual), 'P');
10032 begin
10033 Set_Is_Internal (I_Pack);
10035 Append_To (Decls,
10036 Make_Package_Instantiation (Sloc (Actual),
10037 Defining_Unit_Name => I_Pack,
10038 Name =>
10039 New_Occurrence_Of
10040 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10041 Generic_Associations => Generic_Associations (Formal)));
10042 end;
10043 end if;
10045 return Decls;
10046 end if;
10047 end Instantiate_Formal_Package;
10049 -----------------------------------
10050 -- Instantiate_Formal_Subprogram --
10051 -----------------------------------
10053 function Instantiate_Formal_Subprogram
10054 (Formal : Node_Id;
10055 Actual : Node_Id;
10056 Analyzed_Formal : Node_Id) return Node_Id
10058 Analyzed_S : constant Entity_Id :=
10059 Defining_Unit_Name (Specification (Analyzed_Formal));
10060 Formal_Sub : constant Entity_Id :=
10061 Defining_Unit_Name (Specification (Formal));
10063 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10064 -- If the generic is a child unit, the parent has been installed on the
10065 -- scope stack, but a default subprogram cannot resolve to something
10066 -- on the parent because that parent is not really part of the visible
10067 -- context (it is there to resolve explicit local entities). If the
10068 -- default has resolved in this way, we remove the entity from immediate
10069 -- visibility and analyze the node again to emit an error message or
10070 -- find another visible candidate.
10072 procedure Valid_Actual_Subprogram (Act : Node_Id);
10073 -- Perform legality check and raise exception on failure
10075 -----------------------
10076 -- From_Parent_Scope --
10077 -----------------------
10079 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10080 Gen_Scope : Node_Id;
10082 begin
10083 Gen_Scope := Scope (Analyzed_S);
10084 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10085 if Scope (Subp) = Scope (Gen_Scope) then
10086 return True;
10087 end if;
10089 Gen_Scope := Scope (Gen_Scope);
10090 end loop;
10092 return False;
10093 end From_Parent_Scope;
10095 -----------------------------
10096 -- Valid_Actual_Subprogram --
10097 -----------------------------
10099 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10100 Act_E : Entity_Id;
10102 begin
10103 if Is_Entity_Name (Act) then
10104 Act_E := Entity (Act);
10106 elsif Nkind (Act) = N_Selected_Component
10107 and then Is_Entity_Name (Selector_Name (Act))
10108 then
10109 Act_E := Entity (Selector_Name (Act));
10111 else
10112 Act_E := Empty;
10113 end if;
10115 if (Present (Act_E) and then Is_Overloadable (Act_E))
10116 or else Nkind_In (Act, N_Attribute_Reference,
10117 N_Indexed_Component,
10118 N_Character_Literal,
10119 N_Explicit_Dereference)
10120 then
10121 return;
10122 end if;
10124 Error_Msg_NE
10125 ("expect subprogram or entry name in instantiation of &",
10126 Instantiation_Node, Formal_Sub);
10127 Abandon_Instantiation (Instantiation_Node);
10128 end Valid_Actual_Subprogram;
10130 -- Local variables
10132 Decl_Node : Node_Id;
10133 Loc : Source_Ptr;
10134 Nam : Node_Id;
10135 New_Spec : Node_Id;
10136 New_Subp : Entity_Id;
10138 -- Start of processing for Instantiate_Formal_Subprogram
10140 begin
10141 New_Spec := New_Copy_Tree (Specification (Formal));
10143 -- The tree copy has created the proper instantiation sloc for the
10144 -- new specification. Use this location for all other constructed
10145 -- declarations.
10147 Loc := Sloc (Defining_Unit_Name (New_Spec));
10149 -- Create new entity for the actual (New_Copy_Tree does not), and
10150 -- indicate that it is an actual.
10152 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10153 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10154 Set_Is_Generic_Actual_Subprogram (New_Subp);
10155 Set_Defining_Unit_Name (New_Spec, New_Subp);
10157 -- Create new entities for the each of the formals in the specification
10158 -- of the renaming declaration built for the actual.
10160 if Present (Parameter_Specifications (New_Spec)) then
10161 declare
10162 F : Node_Id;
10163 F_Id : Entity_Id;
10165 begin
10166 F := First (Parameter_Specifications (New_Spec));
10167 while Present (F) loop
10168 F_Id := Defining_Identifier (F);
10170 Set_Defining_Identifier (F,
10171 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10172 Next (F);
10173 end loop;
10174 end;
10175 end if;
10177 -- Find entity of actual. If the actual is an attribute reference, it
10178 -- cannot be resolved here (its formal is missing) but is handled
10179 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10180 -- fully resolved subsequently, when the renaming declaration for the
10181 -- formal is analyzed. If it is an explicit dereference, resolve the
10182 -- prefix but not the actual itself, to prevent interpretation as call.
10184 if Present (Actual) then
10185 Loc := Sloc (Actual);
10186 Set_Sloc (New_Spec, Loc);
10188 if Nkind (Actual) = N_Operator_Symbol then
10189 Find_Direct_Name (Actual);
10191 elsif Nkind (Actual) = N_Explicit_Dereference then
10192 Analyze (Prefix (Actual));
10194 elsif Nkind (Actual) /= N_Attribute_Reference then
10195 Analyze (Actual);
10196 end if;
10198 Valid_Actual_Subprogram (Actual);
10199 Nam := Actual;
10201 elsif Present (Default_Name (Formal)) then
10202 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10203 N_Selected_Component,
10204 N_Indexed_Component,
10205 N_Character_Literal)
10206 and then Present (Entity (Default_Name (Formal)))
10207 then
10208 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10209 else
10210 Nam := New_Copy (Default_Name (Formal));
10211 Set_Sloc (Nam, Loc);
10212 end if;
10214 elsif Box_Present (Formal) then
10216 -- Actual is resolved at the point of instantiation. Create an
10217 -- identifier or operator with the same name as the formal.
10219 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10220 Nam :=
10221 Make_Operator_Symbol (Loc,
10222 Chars => Chars (Formal_Sub),
10223 Strval => No_String);
10224 else
10225 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10226 end if;
10228 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10229 and then Null_Present (Specification (Formal))
10230 then
10231 -- Generate null body for procedure, for use in the instance
10233 Decl_Node :=
10234 Make_Subprogram_Body (Loc,
10235 Specification => New_Spec,
10236 Declarations => New_List,
10237 Handled_Statement_Sequence =>
10238 Make_Handled_Sequence_Of_Statements (Loc,
10239 Statements => New_List (Make_Null_Statement (Loc))));
10241 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10242 return Decl_Node;
10244 else
10245 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10246 Error_Msg_NE
10247 ("missing actual&", Instantiation_Node, Formal_Sub);
10248 Error_Msg_NE
10249 ("\in instantiation of & declared#",
10250 Instantiation_Node, Scope (Analyzed_S));
10251 Abandon_Instantiation (Instantiation_Node);
10252 end if;
10254 Decl_Node :=
10255 Make_Subprogram_Renaming_Declaration (Loc,
10256 Specification => New_Spec,
10257 Name => Nam);
10259 -- If we do not have an actual and the formal specified <> then set to
10260 -- get proper default.
10262 if No (Actual) and then Box_Present (Formal) then
10263 Set_From_Default (Decl_Node);
10264 end if;
10266 -- Gather possible interpretations for the actual before analyzing the
10267 -- instance. If overloaded, it will be resolved when analyzing the
10268 -- renaming declaration.
10270 if Box_Present (Formal) and then No (Actual) then
10271 Analyze (Nam);
10273 if Is_Child_Unit (Scope (Analyzed_S))
10274 and then Present (Entity (Nam))
10275 then
10276 if not Is_Overloaded (Nam) then
10277 if From_Parent_Scope (Entity (Nam)) then
10278 Set_Is_Immediately_Visible (Entity (Nam), False);
10279 Set_Entity (Nam, Empty);
10280 Set_Etype (Nam, Empty);
10282 Analyze (Nam);
10283 Set_Is_Immediately_Visible (Entity (Nam));
10284 end if;
10286 else
10287 declare
10288 I : Interp_Index;
10289 It : Interp;
10291 begin
10292 Get_First_Interp (Nam, I, It);
10293 while Present (It.Nam) loop
10294 if From_Parent_Scope (It.Nam) then
10295 Remove_Interp (I);
10296 end if;
10298 Get_Next_Interp (I, It);
10299 end loop;
10300 end;
10301 end if;
10302 end if;
10303 end if;
10305 -- The generic instantiation freezes the actual. This can only be done
10306 -- once the actual is resolved, in the analysis of the renaming
10307 -- declaration. To make the formal subprogram entity available, we set
10308 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10309 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10310 -- of formal abstract subprograms.
10312 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10314 -- We cannot analyze the renaming declaration, and thus find the actual,
10315 -- until all the actuals are assembled in the instance. For subsequent
10316 -- checks of other actuals, indicate the node that will hold the
10317 -- instance of this formal.
10319 Set_Instance_Of (Analyzed_S, Nam);
10321 if Nkind (Actual) = N_Selected_Component
10322 and then Is_Task_Type (Etype (Prefix (Actual)))
10323 and then not Is_Frozen (Etype (Prefix (Actual)))
10324 then
10325 -- The renaming declaration will create a body, which must appear
10326 -- outside of the instantiation, We move the renaming declaration
10327 -- out of the instance, and create an additional renaming inside,
10328 -- to prevent freezing anomalies.
10330 declare
10331 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10333 begin
10334 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10335 Insert_Before (Instantiation_Node, Decl_Node);
10336 Analyze (Decl_Node);
10338 -- Now create renaming within the instance
10340 Decl_Node :=
10341 Make_Subprogram_Renaming_Declaration (Loc,
10342 Specification => New_Copy_Tree (New_Spec),
10343 Name => New_Occurrence_Of (Anon_Id, Loc));
10345 Set_Defining_Unit_Name (Specification (Decl_Node),
10346 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10347 end;
10348 end if;
10350 return Decl_Node;
10351 end Instantiate_Formal_Subprogram;
10353 ------------------------
10354 -- Instantiate_Object --
10355 ------------------------
10357 function Instantiate_Object
10358 (Formal : Node_Id;
10359 Actual : Node_Id;
10360 Analyzed_Formal : Node_Id) return List_Id
10362 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10363 A_Gen_Obj : constant Entity_Id :=
10364 Defining_Identifier (Analyzed_Formal);
10365 Acc_Def : Node_Id := Empty;
10366 Act_Assoc : constant Node_Id := Parent (Actual);
10367 Actual_Decl : Node_Id := Empty;
10368 Decl_Node : Node_Id;
10369 Def : Node_Id;
10370 Ftyp : Entity_Id;
10371 List : constant List_Id := New_List;
10372 Loc : constant Source_Ptr := Sloc (Actual);
10373 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10374 Subt_Decl : Node_Id := Empty;
10375 Subt_Mark : Node_Id := Empty;
10377 function Copy_Access_Def return Node_Id;
10378 -- If formal is an anonymous access, copy access definition of formal
10379 -- for generated object declaration.
10381 ---------------------
10382 -- Copy_Access_Def --
10383 ---------------------
10385 function Copy_Access_Def return Node_Id is
10386 begin
10387 Def := New_Copy_Tree (Acc_Def);
10389 -- In addition, if formal is an access to subprogram we need to
10390 -- generate new formals for the signature of the default, so that
10391 -- the tree is properly formatted for ASIS use.
10393 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10394 declare
10395 Par_Spec : Node_Id;
10396 begin
10397 Par_Spec :=
10398 First (Parameter_Specifications
10399 (Access_To_Subprogram_Definition (Def)));
10400 while Present (Par_Spec) loop
10401 Set_Defining_Identifier (Par_Spec,
10402 Make_Defining_Identifier (Sloc (Acc_Def),
10403 Chars => Chars (Defining_Identifier (Par_Spec))));
10404 Next (Par_Spec);
10405 end loop;
10406 end;
10407 end if;
10409 return Def;
10410 end Copy_Access_Def;
10412 -- Start of processing for Instantiate_Object
10414 begin
10415 -- Formal may be an anonymous access
10417 if Present (Subtype_Mark (Formal)) then
10418 Subt_Mark := Subtype_Mark (Formal);
10419 else
10420 Check_Access_Definition (Formal);
10421 Acc_Def := Access_Definition (Formal);
10422 end if;
10424 -- Sloc for error message on missing actual
10426 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10428 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10429 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10430 end if;
10432 Set_Parent (List, Parent (Actual));
10434 -- OUT present
10436 if Out_Present (Formal) then
10438 -- An IN OUT generic actual must be a name. The instantiation is a
10439 -- renaming declaration. The actual is the name being renamed. We
10440 -- use the actual directly, rather than a copy, because it is not
10441 -- used further in the list of actuals, and because a copy or a use
10442 -- of relocate_node is incorrect if the instance is nested within a
10443 -- generic. In order to simplify ASIS searches, the Generic_Parent
10444 -- field links the declaration to the generic association.
10446 if No (Actual) then
10447 Error_Msg_NE
10448 ("missing actual &",
10449 Instantiation_Node, Gen_Obj);
10450 Error_Msg_NE
10451 ("\in instantiation of & declared#",
10452 Instantiation_Node, Scope (A_Gen_Obj));
10453 Abandon_Instantiation (Instantiation_Node);
10454 end if;
10456 if Present (Subt_Mark) then
10457 Decl_Node :=
10458 Make_Object_Renaming_Declaration (Loc,
10459 Defining_Identifier => New_Copy (Gen_Obj),
10460 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10461 Name => Actual);
10463 else pragma Assert (Present (Acc_Def));
10464 Decl_Node :=
10465 Make_Object_Renaming_Declaration (Loc,
10466 Defining_Identifier => New_Copy (Gen_Obj),
10467 Access_Definition => New_Copy_Tree (Acc_Def),
10468 Name => Actual);
10469 end if;
10471 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10473 -- The analysis of the actual may produce Insert_Action nodes, so
10474 -- the declaration must have a context in which to attach them.
10476 Append (Decl_Node, List);
10477 Analyze (Actual);
10479 -- Return if the analysis of the actual reported some error
10481 if Etype (Actual) = Any_Type then
10482 return List;
10483 end if;
10485 -- This check is performed here because Analyze_Object_Renaming will
10486 -- not check it when Comes_From_Source is False. Note though that the
10487 -- check for the actual being the name of an object will be performed
10488 -- in Analyze_Object_Renaming.
10490 if Is_Object_Reference (Actual)
10491 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10492 then
10493 Error_Msg_N
10494 ("illegal discriminant-dependent component for in out parameter",
10495 Actual);
10496 end if;
10498 -- The actual has to be resolved in order to check that it is a
10499 -- variable (due to cases such as F (1), where F returns access to
10500 -- an array, and for overloaded prefixes).
10502 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10504 -- If the type of the formal is not itself a formal, and the current
10505 -- unit is a child unit, the formal type must be declared in a
10506 -- parent, and must be retrieved by visibility.
10508 if Ftyp = Orig_Ftyp
10509 and then Is_Generic_Unit (Scope (Ftyp))
10510 and then Is_Child_Unit (Scope (A_Gen_Obj))
10511 then
10512 declare
10513 Temp : constant Node_Id :=
10514 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10515 begin
10516 Set_Entity (Temp, Empty);
10517 Find_Type (Temp);
10518 Ftyp := Entity (Temp);
10519 end;
10520 end if;
10522 if Is_Private_Type (Ftyp)
10523 and then not Is_Private_Type (Etype (Actual))
10524 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10525 or else Base_Type (Etype (Actual)) = Ftyp)
10526 then
10527 -- If the actual has the type of the full view of the formal, or
10528 -- else a non-private subtype of the formal, then the visibility
10529 -- of the formal type has changed. Add to the actuals a subtype
10530 -- declaration that will force the exchange of views in the body
10531 -- of the instance as well.
10533 Subt_Decl :=
10534 Make_Subtype_Declaration (Loc,
10535 Defining_Identifier => Make_Temporary (Loc, 'P'),
10536 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10538 Prepend (Subt_Decl, List);
10540 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10541 Exchange_Declarations (Ftyp);
10542 end if;
10544 Resolve (Actual, Ftyp);
10546 if not Denotes_Variable (Actual) then
10547 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10549 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10551 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10552 -- the type of the actual shall resolve to a specific anonymous
10553 -- access type.
10555 if Ada_Version < Ada_2005
10556 or else Ekind (Base_Type (Ftyp)) /=
10557 E_Anonymous_Access_Type
10558 or else Ekind (Base_Type (Etype (Actual))) /=
10559 E_Anonymous_Access_Type
10560 then
10561 Error_Msg_NE
10562 ("type of actual does not match type of&", Actual, Gen_Obj);
10563 end if;
10564 end if;
10566 Note_Possible_Modification (Actual, Sure => True);
10568 -- Check for instantiation of atomic/volatile actual for
10569 -- non-atomic/volatile formal (RM C.6 (12)).
10571 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10572 Error_Msg_N
10573 ("cannot instantiate non-atomic formal object "
10574 & "with atomic actual", Actual);
10576 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10577 then
10578 Error_Msg_N
10579 ("cannot instantiate non-volatile formal object "
10580 & "with volatile actual", Actual);
10581 end if;
10583 -- Formal in-parameter
10585 else
10586 -- The instantiation of a generic formal in-parameter is constant
10587 -- declaration. The actual is the expression for that declaration.
10588 -- Its type is a full copy of the type of the formal. This may be
10589 -- an access to subprogram, for which we need to generate entities
10590 -- for the formals in the new signature.
10592 if Present (Actual) then
10593 if Present (Subt_Mark) then
10594 Def := New_Copy_Tree (Subt_Mark);
10595 else pragma Assert (Present (Acc_Def));
10596 Def := Copy_Access_Def;
10597 end if;
10599 Decl_Node :=
10600 Make_Object_Declaration (Loc,
10601 Defining_Identifier => New_Copy (Gen_Obj),
10602 Constant_Present => True,
10603 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10604 Object_Definition => Def,
10605 Expression => Actual);
10607 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10609 -- A generic formal object of a tagged type is defined to be
10610 -- aliased so the new constant must also be treated as aliased.
10612 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10613 Set_Aliased_Present (Decl_Node);
10614 end if;
10616 Append (Decl_Node, List);
10618 -- No need to repeat (pre-)analysis of some expression nodes
10619 -- already handled in Preanalyze_Actuals.
10621 if Nkind (Actual) /= N_Allocator then
10622 Analyze (Actual);
10624 -- Return if the analysis of the actual reported some error
10626 if Etype (Actual) = Any_Type then
10627 return List;
10628 end if;
10629 end if;
10631 declare
10632 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10633 Typ : Entity_Id;
10635 begin
10636 Typ := Get_Instance_Of (Formal_Type);
10638 -- If the actual appears in the current or an enclosing scope,
10639 -- use its type directly. This is relevant if it has an actual
10640 -- subtype that is distinct from its nominal one. This cannot
10641 -- be done in general because the type of the actual may
10642 -- depend on other actuals, and only be fully determined when
10643 -- the enclosing instance is analyzed.
10645 if Present (Etype (Actual))
10646 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10647 then
10648 Freeze_Before (Instantiation_Node, Etype (Actual));
10649 else
10650 Freeze_Before (Instantiation_Node, Typ);
10651 end if;
10653 -- If the actual is an aggregate, perform name resolution on
10654 -- its components (the analysis of an aggregate does not do it)
10655 -- to capture local names that may be hidden if the generic is
10656 -- a child unit.
10658 if Nkind (Actual) = N_Aggregate then
10659 Preanalyze_And_Resolve (Actual, Typ);
10660 end if;
10662 if Is_Limited_Type (Typ)
10663 and then not OK_For_Limited_Init (Typ, Actual)
10664 then
10665 Error_Msg_N
10666 ("initialization not allowed for limited types", Actual);
10667 Explain_Limited_Type (Typ, Actual);
10668 end if;
10669 end;
10671 elsif Present (Default_Expression (Formal)) then
10673 -- Use default to construct declaration
10675 if Present (Subt_Mark) then
10676 Def := New_Copy (Subt_Mark);
10677 else pragma Assert (Present (Acc_Def));
10678 Def := Copy_Access_Def;
10679 end if;
10681 Decl_Node :=
10682 Make_Object_Declaration (Sloc (Formal),
10683 Defining_Identifier => New_Copy (Gen_Obj),
10684 Constant_Present => True,
10685 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10686 Object_Definition => Def,
10687 Expression => New_Copy_Tree
10688 (Default_Expression (Formal)));
10690 Append (Decl_Node, List);
10691 Set_Analyzed (Expression (Decl_Node), False);
10693 else
10694 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10695 Error_Msg_NE ("\in instantiation of & declared#",
10696 Instantiation_Node, Scope (A_Gen_Obj));
10698 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10700 -- Create dummy constant declaration so that instance can be
10701 -- analyzed, to minimize cascaded visibility errors.
10703 if Present (Subt_Mark) then
10704 Def := Subt_Mark;
10705 else pragma Assert (Present (Acc_Def));
10706 Def := Acc_Def;
10707 end if;
10709 Decl_Node :=
10710 Make_Object_Declaration (Loc,
10711 Defining_Identifier => New_Copy (Gen_Obj),
10712 Constant_Present => True,
10713 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10714 Object_Definition => New_Copy (Def),
10715 Expression =>
10716 Make_Attribute_Reference (Sloc (Gen_Obj),
10717 Attribute_Name => Name_First,
10718 Prefix => New_Copy (Def)));
10720 Append (Decl_Node, List);
10722 else
10723 Abandon_Instantiation (Instantiation_Node);
10724 end if;
10725 end if;
10726 end if;
10728 if Nkind (Actual) in N_Has_Entity then
10729 Actual_Decl := Parent (Entity (Actual));
10730 end if;
10732 -- Ada 2005 (AI-423): For a formal object declaration with a null
10733 -- exclusion or an access definition that has a null exclusion: If the
10734 -- actual matching the formal object declaration denotes a generic
10735 -- formal object of another generic unit G, and the instantiation
10736 -- containing the actual occurs within the body of G or within the body
10737 -- of a generic unit declared within the declarative region of G, then
10738 -- the declaration of the formal object of G must have a null exclusion.
10739 -- Otherwise, the subtype of the actual matching the formal object
10740 -- declaration shall exclude null.
10742 if Ada_Version >= Ada_2005
10743 and then Present (Actual_Decl)
10744 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10745 N_Object_Declaration)
10746 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10747 and then not Has_Null_Exclusion (Actual_Decl)
10748 and then Has_Null_Exclusion (Analyzed_Formal)
10749 then
10750 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10751 Error_Msg_N
10752 ("actual must exclude null to match generic formal#", Actual);
10753 end if;
10755 -- An effectively volatile object cannot be used as an actual in a
10756 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10757 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10758 -- legality rule, and also verifies that the actual is an object.
10760 if SPARK_Mode = On
10761 and then Present (Actual)
10762 and then Is_Object_Reference (Actual)
10763 and then Is_Effectively_Volatile_Object (Actual)
10764 then
10765 Error_Msg_N
10766 ("volatile object cannot act as actual in generic instantiation",
10767 Actual);
10768 end if;
10770 return List;
10771 end Instantiate_Object;
10773 ------------------------------
10774 -- Instantiate_Package_Body --
10775 ------------------------------
10777 procedure Instantiate_Package_Body
10778 (Body_Info : Pending_Body_Info;
10779 Inlined_Body : Boolean := False;
10780 Body_Optional : Boolean := False)
10782 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10783 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10784 Loc : constant Source_Ptr := Sloc (Inst_Node);
10786 Gen_Id : constant Node_Id := Name (Inst_Node);
10787 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10788 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10789 Act_Spec : constant Node_Id := Specification (Act_Decl);
10790 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10792 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10793 Save_Style_Check : constant Boolean := Style_Check;
10795 Act_Body : Node_Id;
10796 Act_Body_Id : Entity_Id;
10797 Act_Body_Name : Node_Id;
10798 Gen_Body : Node_Id;
10799 Gen_Body_Id : Node_Id;
10800 Par_Ent : Entity_Id := Empty;
10801 Par_Vis : Boolean := False;
10803 Parent_Installed : Boolean := False;
10805 Vis_Prims_List : Elist_Id := No_Elist;
10806 -- List of primitives made temporarily visible in the instantiation
10807 -- to match the visibility of the formal type
10809 procedure Check_Initialized_Types;
10810 -- In a generic package body, an entity of a generic private type may
10811 -- appear uninitialized. This is suspicious, unless the actual is a
10812 -- fully initialized type.
10814 -----------------------------
10815 -- Check_Initialized_Types --
10816 -----------------------------
10818 procedure Check_Initialized_Types is
10819 Decl : Node_Id;
10820 Formal : Entity_Id;
10821 Actual : Entity_Id;
10822 Uninit_Var : Entity_Id;
10824 begin
10825 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10826 while Present (Decl) loop
10827 Uninit_Var := Empty;
10829 if Nkind (Decl) = N_Private_Extension_Declaration then
10830 Uninit_Var := Uninitialized_Variable (Decl);
10832 elsif Nkind (Decl) = N_Formal_Type_Declaration
10833 and then Nkind (Formal_Type_Definition (Decl)) =
10834 N_Formal_Private_Type_Definition
10835 then
10836 Uninit_Var :=
10837 Uninitialized_Variable (Formal_Type_Definition (Decl));
10838 end if;
10840 if Present (Uninit_Var) then
10841 Formal := Defining_Identifier (Decl);
10842 Actual := First_Entity (Act_Decl_Id);
10844 -- For each formal there is a subtype declaration that renames
10845 -- the actual and has the same name as the formal. Locate the
10846 -- formal for warning message about uninitialized variables
10847 -- in the generic, for which the actual type should be a fully
10848 -- initialized type.
10850 while Present (Actual) loop
10851 exit when Ekind (Actual) = E_Package
10852 and then Present (Renamed_Object (Actual));
10854 if Chars (Actual) = Chars (Formal)
10855 and then not Is_Scalar_Type (Actual)
10856 and then not Is_Fully_Initialized_Type (Actual)
10857 and then Warn_On_No_Value_Assigned
10858 then
10859 Error_Msg_Node_2 := Formal;
10860 Error_Msg_NE
10861 ("generic unit has uninitialized variable& of "
10862 & "formal private type &?v?", Actual, Uninit_Var);
10863 Error_Msg_NE
10864 ("actual type for& should be fully initialized type?v?",
10865 Actual, Formal);
10866 exit;
10867 end if;
10869 Next_Entity (Actual);
10870 end loop;
10871 end if;
10873 Next (Decl);
10874 end loop;
10875 end Check_Initialized_Types;
10877 -- Start of processing for Instantiate_Package_Body
10879 begin
10880 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10882 -- The instance body may already have been processed, as the parent of
10883 -- another instance that is inlined (Load_Parent_Of_Generic).
10885 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10886 return;
10887 end if;
10889 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10891 -- Re-establish the state of information on which checks are suppressed.
10892 -- This information was set in Body_Info at the point of instantiation,
10893 -- and now we restore it so that the instance is compiled using the
10894 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10896 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10897 Scope_Suppress := Body_Info.Scope_Suppress;
10898 Opt.Ada_Version := Body_Info.Version;
10899 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10900 Restore_Warnings (Body_Info.Warnings);
10901 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10902 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10904 if No (Gen_Body_Id) then
10906 -- Do not look for parent of generic body if none is required.
10907 -- This may happen when the routine is called as part of the
10908 -- Pending_Instantiations processing, when nested instances
10909 -- may precede the one generated from the main unit.
10911 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10912 and then Body_Optional
10913 then
10914 return;
10915 else
10916 Load_Parent_Of_Generic
10917 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10918 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10919 end if;
10920 end if;
10922 -- Establish global variable for sloc adjustment and for error recovery
10923 -- In the case of an instance body for an instantiation with actuals
10924 -- from a limited view, the instance body is placed at the beginning
10925 -- of the enclosing package body: use the body entity as the source
10926 -- location for nodes of the instance body.
10928 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10929 declare
10930 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10931 Body_Id : constant Node_Id :=
10932 Corresponding_Body (Unit_Declaration_Node (Scop));
10934 begin
10935 Instantiation_Node := Body_Id;
10936 end;
10937 else
10938 Instantiation_Node := Inst_Node;
10939 end if;
10941 if Present (Gen_Body_Id) then
10942 Save_Env (Gen_Unit, Act_Decl_Id);
10943 Style_Check := False;
10945 -- If the context of the instance is subject to SPARK_Mode "off" or
10946 -- the annotation is altogether missing, set the global flag which
10947 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10948 -- the instance.
10950 if SPARK_Mode /= On then
10951 Ignore_Pragma_SPARK_Mode := True;
10952 end if;
10954 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10955 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10957 Create_Instantiation_Source
10958 (Inst_Node, Gen_Body_Id, S_Adjustment);
10960 Act_Body :=
10961 Copy_Generic_Node
10962 (Original_Node (Gen_Body), Empty, Instantiating => True);
10964 -- Create proper (possibly qualified) defining name for the body, to
10965 -- correspond to the one in the spec.
10967 Act_Body_Id :=
10968 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
10969 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
10971 -- Some attributes of spec entity are not inherited by body entity
10973 Set_Handler_Records (Act_Body_Id, No_List);
10975 if Nkind (Defining_Unit_Name (Act_Spec)) =
10976 N_Defining_Program_Unit_Name
10977 then
10978 Act_Body_Name :=
10979 Make_Defining_Program_Unit_Name (Loc,
10980 Name =>
10981 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10982 Defining_Identifier => Act_Body_Id);
10983 else
10984 Act_Body_Name := Act_Body_Id;
10985 end if;
10987 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10989 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10990 Check_Generic_Actuals (Act_Decl_Id, False);
10991 Check_Initialized_Types;
10993 -- Install primitives hidden at the point of the instantiation but
10994 -- visible when processing the generic formals
10996 declare
10997 E : Entity_Id;
10999 begin
11000 E := First_Entity (Act_Decl_Id);
11001 while Present (E) loop
11002 if Is_Type (E)
11003 and then not Is_Itype (E)
11004 and then Is_Generic_Actual_Type (E)
11005 and then Is_Tagged_Type (E)
11006 then
11007 Install_Hidden_Primitives
11008 (Prims_List => Vis_Prims_List,
11009 Gen_T => Generic_Parent_Type (Parent (E)),
11010 Act_T => E);
11011 end if;
11013 Next_Entity (E);
11014 end loop;
11015 end;
11017 -- If it is a child unit, make the parent instance (which is an
11018 -- instance of the parent of the generic) visible. The parent
11019 -- instance is the prefix of the name of the generic unit.
11021 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11022 and then Nkind (Gen_Id) = N_Expanded_Name
11023 then
11024 Par_Ent := Entity (Prefix (Gen_Id));
11025 Par_Vis := Is_Immediately_Visible (Par_Ent);
11026 Install_Parent (Par_Ent, In_Body => True);
11027 Parent_Installed := True;
11029 elsif Is_Child_Unit (Gen_Unit) then
11030 Par_Ent := Scope (Gen_Unit);
11031 Par_Vis := Is_Immediately_Visible (Par_Ent);
11032 Install_Parent (Par_Ent, In_Body => True);
11033 Parent_Installed := True;
11034 end if;
11036 -- If the instantiation is a library unit, and this is the main unit,
11037 -- then build the resulting compilation unit nodes for the instance.
11038 -- If this is a compilation unit but it is not the main unit, then it
11039 -- is the body of a unit in the context, that is being compiled
11040 -- because it is encloses some inlined unit or another generic unit
11041 -- being instantiated. In that case, this body is not part of the
11042 -- current compilation, and is not attached to the tree, but its
11043 -- parent must be set for analysis.
11045 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11047 -- Replace instance node with body of instance, and create new
11048 -- node for corresponding instance declaration.
11050 Build_Instance_Compilation_Unit_Nodes
11051 (Inst_Node, Act_Body, Act_Decl);
11052 Analyze (Inst_Node);
11054 if Parent (Inst_Node) = Cunit (Main_Unit) then
11056 -- If the instance is a child unit itself, then set the scope
11057 -- of the expanded body to be the parent of the instantiation
11058 -- (ensuring that the fully qualified name will be generated
11059 -- for the elaboration subprogram).
11061 if Nkind (Defining_Unit_Name (Act_Spec)) =
11062 N_Defining_Program_Unit_Name
11063 then
11064 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11065 end if;
11066 end if;
11068 -- Case where instantiation is not a library unit
11070 else
11071 -- If this is an early instantiation, i.e. appears textually
11072 -- before the corresponding body and must be elaborated first,
11073 -- indicate that the body instance is to be delayed.
11075 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11077 -- Now analyze the body. We turn off all checks if this is an
11078 -- internal unit, since there is no reason to have checks on for
11079 -- any predefined run-time library code. All such code is designed
11080 -- to be compiled with checks off.
11082 -- Note that we do NOT apply this criterion to children of GNAT
11083 -- The latter units must suppress checks explicitly if needed.
11085 -- We also do not suppress checks in CodePeer mode where we are
11086 -- interested in finding possible runtime errors.
11088 if not CodePeer_Mode
11089 and then Is_Predefined_File_Name
11090 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11091 then
11092 Analyze (Act_Body, Suppress => All_Checks);
11093 else
11094 Analyze (Act_Body);
11095 end if;
11096 end if;
11098 Inherit_Context (Gen_Body, Inst_Node);
11100 -- Remove the parent instances if they have been placed on the scope
11101 -- stack to compile the body.
11103 if Parent_Installed then
11104 Remove_Parent (In_Body => True);
11106 -- Restore the previous visibility of the parent
11108 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11109 end if;
11111 Restore_Hidden_Primitives (Vis_Prims_List);
11112 Restore_Private_Views (Act_Decl_Id);
11114 -- Remove the current unit from visibility if this is an instance
11115 -- that is not elaborated on the fly for inlining purposes.
11117 if not Inlined_Body then
11118 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11119 end if;
11121 Restore_Env;
11122 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11123 Style_Check := Save_Style_Check;
11125 -- If we have no body, and the unit requires a body, then complain. This
11126 -- complaint is suppressed if we have detected other errors (since a
11127 -- common reason for missing the body is that it had errors).
11128 -- In CodePeer mode, a warning has been emitted already, no need for
11129 -- further messages.
11131 elsif Unit_Requires_Body (Gen_Unit)
11132 and then not Body_Optional
11133 then
11134 if CodePeer_Mode then
11135 null;
11137 elsif Serious_Errors_Detected = 0 then
11138 Error_Msg_NE
11139 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11141 -- Don't attempt to perform any cleanup actions if some other error
11142 -- was already detected, since this can cause blowups.
11144 else
11145 return;
11146 end if;
11148 -- Case of package that does not need a body
11150 else
11151 -- If the instantiation of the declaration is a library unit, rewrite
11152 -- the original package instantiation as a package declaration in the
11153 -- compilation unit node.
11155 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11156 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11157 Rewrite (Inst_Node, Act_Decl);
11159 -- Generate elaboration entity, in case spec has elaboration code.
11160 -- This cannot be done when the instance is analyzed, because it
11161 -- is not known yet whether the body exists.
11163 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11164 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11166 -- If the instantiation is not a library unit, then append the
11167 -- declaration to the list of implicitly generated entities, unless
11168 -- it is already a list member which means that it was already
11169 -- processed
11171 elsif not Is_List_Member (Act_Decl) then
11172 Mark_Rewrite_Insertion (Act_Decl);
11173 Insert_Before (Inst_Node, Act_Decl);
11174 end if;
11175 end if;
11177 Expander_Mode_Restore;
11178 end Instantiate_Package_Body;
11180 ---------------------------------
11181 -- Instantiate_Subprogram_Body --
11182 ---------------------------------
11184 procedure Instantiate_Subprogram_Body
11185 (Body_Info : Pending_Body_Info;
11186 Body_Optional : Boolean := False)
11188 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11189 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11190 Loc : constant Source_Ptr := Sloc (Inst_Node);
11191 Gen_Id : constant Node_Id := Name (Inst_Node);
11192 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11193 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11194 Act_Decl_Id : constant Entity_Id :=
11195 Defining_Unit_Name (Specification (Act_Decl));
11196 Pack_Id : constant Entity_Id :=
11197 Defining_Unit_Name (Parent (Act_Decl));
11199 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11200 Saved_Style_Check : constant Boolean := Style_Check;
11201 Saved_Warnings : constant Warning_Record := Save_Warnings;
11203 Act_Body : Node_Id;
11204 Act_Body_Id : Entity_Id;
11205 Gen_Body : Node_Id;
11206 Gen_Body_Id : Node_Id;
11207 Pack_Body : Node_Id;
11208 Par_Ent : Entity_Id := Empty;
11209 Par_Vis : Boolean := False;
11210 Ret_Expr : Node_Id;
11212 Parent_Installed : Boolean := False;
11214 begin
11215 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11217 -- Subprogram body may have been created already because of an inline
11218 -- pragma, or because of multiple elaborations of the enclosing package
11219 -- when several instances of the subprogram appear in the main unit.
11221 if Present (Corresponding_Body (Act_Decl)) then
11222 return;
11223 end if;
11225 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11227 -- Re-establish the state of information on which checks are suppressed.
11228 -- This information was set in Body_Info at the point of instantiation,
11229 -- and now we restore it so that the instance is compiled using the
11230 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11232 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11233 Scope_Suppress := Body_Info.Scope_Suppress;
11234 Opt.Ada_Version := Body_Info.Version;
11235 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11236 Restore_Warnings (Body_Info.Warnings);
11237 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11238 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11240 if No (Gen_Body_Id) then
11242 -- For imported generic subprogram, no body to compile, complete
11243 -- the spec entity appropriately.
11245 if Is_Imported (Gen_Unit) then
11246 Set_Is_Imported (Act_Decl_Id);
11247 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11248 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11249 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11250 Set_Has_Completion (Act_Decl_Id);
11251 return;
11253 -- For other cases, compile the body
11255 else
11256 Load_Parent_Of_Generic
11257 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11258 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11259 end if;
11260 end if;
11262 Instantiation_Node := Inst_Node;
11264 if Present (Gen_Body_Id) then
11265 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11267 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11269 -- Either body is not present, or context is non-expanding, as
11270 -- when compiling a subunit. Mark the instance as completed, and
11271 -- diagnose a missing body when needed.
11273 if Expander_Active
11274 and then Operating_Mode = Generate_Code
11275 then
11276 Error_Msg_N
11277 ("missing proper body for instantiation", Gen_Body);
11278 end if;
11280 Set_Has_Completion (Act_Decl_Id);
11281 return;
11282 end if;
11284 Save_Env (Gen_Unit, Act_Decl_Id);
11285 Style_Check := False;
11287 -- If the context of the instance is subject to SPARK_Mode "off" or
11288 -- the annotation is altogether missing, set the global flag which
11289 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11290 -- the instance.
11292 if SPARK_Mode /= On then
11293 Ignore_Pragma_SPARK_Mode := True;
11294 end if;
11296 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11297 Create_Instantiation_Source
11298 (Inst_Node,
11299 Gen_Body_Id,
11300 S_Adjustment);
11302 Act_Body :=
11303 Copy_Generic_Node
11304 (Original_Node (Gen_Body), Empty, Instantiating => True);
11306 -- Create proper defining name for the body, to correspond to the one
11307 -- in the spec.
11309 Act_Body_Id :=
11310 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11312 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11313 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11315 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11316 Set_Has_Completion (Act_Decl_Id);
11317 Check_Generic_Actuals (Pack_Id, False);
11319 -- Generate a reference to link the visible subprogram instance to
11320 -- the generic body, which for navigation purposes is the only
11321 -- available source for the instance.
11323 Generate_Reference
11324 (Related_Instance (Pack_Id),
11325 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11327 -- If it is a child unit, make the parent instance (which is an
11328 -- instance of the parent of the generic) visible. The parent
11329 -- instance is the prefix of the name of the generic unit.
11331 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11332 and then Nkind (Gen_Id) = N_Expanded_Name
11333 then
11334 Par_Ent := Entity (Prefix (Gen_Id));
11335 Par_Vis := Is_Immediately_Visible (Par_Ent);
11336 Install_Parent (Par_Ent, In_Body => True);
11337 Parent_Installed := True;
11339 elsif Is_Child_Unit (Gen_Unit) then
11340 Par_Ent := Scope (Gen_Unit);
11341 Par_Vis := Is_Immediately_Visible (Par_Ent);
11342 Install_Parent (Par_Ent, In_Body => True);
11343 Parent_Installed := True;
11344 end if;
11346 -- Subprogram body is placed in the body of wrapper package,
11347 -- whose spec contains the subprogram declaration as well as
11348 -- the renaming declarations for the generic parameters.
11350 Pack_Body :=
11351 Make_Package_Body (Loc,
11352 Defining_Unit_Name => New_Copy (Pack_Id),
11353 Declarations => New_List (Act_Body));
11355 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11357 -- If the instantiation is a library unit, then build resulting
11358 -- compilation unit nodes for the instance. The declaration of
11359 -- the enclosing package is the grandparent of the subprogram
11360 -- declaration. First replace the instantiation node as the unit
11361 -- of the corresponding compilation.
11363 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11364 if Parent (Inst_Node) = Cunit (Main_Unit) then
11365 Set_Unit (Parent (Inst_Node), Inst_Node);
11366 Build_Instance_Compilation_Unit_Nodes
11367 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11368 Analyze (Inst_Node);
11369 else
11370 Set_Parent (Pack_Body, Parent (Inst_Node));
11371 Analyze (Pack_Body);
11372 end if;
11374 else
11375 Insert_Before (Inst_Node, Pack_Body);
11376 Mark_Rewrite_Insertion (Pack_Body);
11377 Analyze (Pack_Body);
11379 if Expander_Active then
11380 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11381 end if;
11382 end if;
11384 Inherit_Context (Gen_Body, Inst_Node);
11386 Restore_Private_Views (Pack_Id, False);
11388 if Parent_Installed then
11389 Remove_Parent (In_Body => True);
11391 -- Restore the previous visibility of the parent
11393 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11394 end if;
11396 Restore_Env;
11397 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11398 Style_Check := Saved_Style_Check;
11399 Restore_Warnings (Saved_Warnings);
11401 -- Body not found. Error was emitted already. If there were no previous
11402 -- errors, this may be an instance whose scope is a premature instance.
11403 -- In that case we must insure that the (legal) program does raise
11404 -- program error if executed. We generate a subprogram body for this
11405 -- purpose. See DEC ac30vso.
11407 -- Should not reference proprietary DEC tests in comments ???
11409 elsif Serious_Errors_Detected = 0
11410 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11411 then
11412 if Body_Optional then
11413 return;
11415 elsif Ekind (Act_Decl_Id) = E_Procedure then
11416 Act_Body :=
11417 Make_Subprogram_Body (Loc,
11418 Specification =>
11419 Make_Procedure_Specification (Loc,
11420 Defining_Unit_Name =>
11421 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11422 Parameter_Specifications =>
11423 New_Copy_List
11424 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11426 Declarations => Empty_List,
11427 Handled_Statement_Sequence =>
11428 Make_Handled_Sequence_Of_Statements (Loc,
11429 Statements =>
11430 New_List (
11431 Make_Raise_Program_Error (Loc,
11432 Reason =>
11433 PE_Access_Before_Elaboration))));
11435 else
11436 Ret_Expr :=
11437 Make_Raise_Program_Error (Loc,
11438 Reason => PE_Access_Before_Elaboration);
11440 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11441 Set_Analyzed (Ret_Expr);
11443 Act_Body :=
11444 Make_Subprogram_Body (Loc,
11445 Specification =>
11446 Make_Function_Specification (Loc,
11447 Defining_Unit_Name =>
11448 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11449 Parameter_Specifications =>
11450 New_Copy_List
11451 (Parameter_Specifications (Parent (Act_Decl_Id))),
11452 Result_Definition =>
11453 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11455 Declarations => Empty_List,
11456 Handled_Statement_Sequence =>
11457 Make_Handled_Sequence_Of_Statements (Loc,
11458 Statements =>
11459 New_List
11460 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11461 end if;
11463 Pack_Body :=
11464 Make_Package_Body (Loc,
11465 Defining_Unit_Name => New_Copy (Pack_Id),
11466 Declarations => New_List (Act_Body));
11468 Insert_After (Inst_Node, Pack_Body);
11469 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11470 Analyze (Pack_Body);
11471 end if;
11473 Expander_Mode_Restore;
11474 end Instantiate_Subprogram_Body;
11476 ----------------------
11477 -- Instantiate_Type --
11478 ----------------------
11480 function Instantiate_Type
11481 (Formal : Node_Id;
11482 Actual : Node_Id;
11483 Analyzed_Formal : Node_Id;
11484 Actual_Decls : List_Id) return List_Id
11486 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11487 A_Gen_T : constant Entity_Id :=
11488 Defining_Identifier (Analyzed_Formal);
11489 Ancestor : Entity_Id := Empty;
11490 Def : constant Node_Id := Formal_Type_Definition (Formal);
11491 Act_T : Entity_Id;
11492 Decl_Node : Node_Id;
11493 Decl_Nodes : List_Id;
11494 Loc : Source_Ptr;
11495 Subt : Entity_Id;
11497 procedure Diagnose_Predicated_Actual;
11498 -- There are a number of constructs in which a discrete type with
11499 -- predicates is illegal, e.g. as an index in an array type declaration.
11500 -- If a generic type is used is such a construct in a generic package
11501 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11502 -- of the generic contract that the actual cannot have predicates.
11504 procedure Validate_Array_Type_Instance;
11505 procedure Validate_Access_Subprogram_Instance;
11506 procedure Validate_Access_Type_Instance;
11507 procedure Validate_Derived_Type_Instance;
11508 procedure Validate_Derived_Interface_Type_Instance;
11509 procedure Validate_Discriminated_Formal_Type;
11510 procedure Validate_Interface_Type_Instance;
11511 procedure Validate_Private_Type_Instance;
11512 procedure Validate_Incomplete_Type_Instance;
11513 -- These procedures perform validation tests for the named case.
11514 -- Validate_Discriminated_Formal_Type is shared by formal private
11515 -- types and Ada 2012 formal incomplete types.
11517 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11518 -- Check that base types are the same and that the subtypes match
11519 -- statically. Used in several of the above.
11521 ---------------------------------
11522 -- Diagnose_Predicated_Actual --
11523 ---------------------------------
11525 procedure Diagnose_Predicated_Actual is
11526 begin
11527 if No_Predicate_On_Actual (A_Gen_T)
11528 and then Has_Predicates (Act_T)
11529 then
11530 Error_Msg_NE
11531 ("actual for& cannot be a type with predicate",
11532 Instantiation_Node, A_Gen_T);
11534 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11535 and then Has_Predicates (Act_T)
11536 and then not Has_Static_Predicate_Aspect (Act_T)
11537 then
11538 Error_Msg_NE
11539 ("actual for& cannot be a type with a dynamic predicate",
11540 Instantiation_Node, A_Gen_T);
11541 end if;
11542 end Diagnose_Predicated_Actual;
11544 --------------------
11545 -- Subtypes_Match --
11546 --------------------
11548 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11549 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11551 begin
11552 -- Some detailed comments would be useful here ???
11554 return ((Base_Type (T) = Act_T
11555 or else Base_Type (T) = Base_Type (Act_T))
11556 and then Subtypes_Statically_Match (T, Act_T))
11558 or else (Is_Class_Wide_Type (Gen_T)
11559 and then Is_Class_Wide_Type (Act_T)
11560 and then Subtypes_Match
11561 (Get_Instance_Of (Root_Type (Gen_T)),
11562 Root_Type (Act_T)))
11564 or else
11565 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11566 E_Anonymous_Access_Type)
11567 and then Ekind (Act_T) = Ekind (Gen_T)
11568 and then Subtypes_Statically_Match
11569 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11570 end Subtypes_Match;
11572 -----------------------------------------
11573 -- Validate_Access_Subprogram_Instance --
11574 -----------------------------------------
11576 procedure Validate_Access_Subprogram_Instance is
11577 begin
11578 if not Is_Access_Type (Act_T)
11579 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11580 then
11581 Error_Msg_NE
11582 ("expect access type in instantiation of &", Actual, Gen_T);
11583 Abandon_Instantiation (Actual);
11584 end if;
11586 -- According to AI05-288, actuals for access_to_subprograms must be
11587 -- subtype conformant with the generic formal. Previous to AI05-288
11588 -- only mode conformance was required.
11590 -- This is a binding interpretation that applies to previous versions
11591 -- of the language, no need to maintain previous weaker checks.
11593 Check_Subtype_Conformant
11594 (Designated_Type (Act_T),
11595 Designated_Type (A_Gen_T),
11596 Actual,
11597 Get_Inst => True);
11599 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11600 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11601 Error_Msg_NE
11602 ("protected access type not allowed for formal &",
11603 Actual, Gen_T);
11604 end if;
11606 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11607 Error_Msg_NE
11608 ("expect protected access type for formal &",
11609 Actual, Gen_T);
11610 end if;
11612 -- If the formal has a specified convention (which in most cases
11613 -- will be StdCall) verify that the actual has the same convention.
11615 if Has_Convention_Pragma (A_Gen_T)
11616 and then Convention (A_Gen_T) /= Convention (Act_T)
11617 then
11618 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11619 Error_Msg_NE
11620 ("actual for formal & must have convention %", Actual, Gen_T);
11621 end if;
11622 end Validate_Access_Subprogram_Instance;
11624 -----------------------------------
11625 -- Validate_Access_Type_Instance --
11626 -----------------------------------
11628 procedure Validate_Access_Type_Instance is
11629 Desig_Type : constant Entity_Id :=
11630 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11631 Desig_Act : Entity_Id;
11633 begin
11634 if not Is_Access_Type (Act_T) then
11635 Error_Msg_NE
11636 ("expect access type in instantiation of &", Actual, Gen_T);
11637 Abandon_Instantiation (Actual);
11638 end if;
11640 if Is_Access_Constant (A_Gen_T) then
11641 if not Is_Access_Constant (Act_T) then
11642 Error_Msg_N
11643 ("actual type must be access-to-constant type", Actual);
11644 Abandon_Instantiation (Actual);
11645 end if;
11646 else
11647 if Is_Access_Constant (Act_T) then
11648 Error_Msg_N
11649 ("actual type must be access-to-variable type", Actual);
11650 Abandon_Instantiation (Actual);
11652 elsif Ekind (A_Gen_T) = E_General_Access_Type
11653 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11654 then
11655 Error_Msg_N -- CODEFIX
11656 ("actual must be general access type!", Actual);
11657 Error_Msg_NE -- CODEFIX
11658 ("add ALL to }!", Actual, Act_T);
11659 Abandon_Instantiation (Actual);
11660 end if;
11661 end if;
11663 -- The designated subtypes, that is to say the subtypes introduced
11664 -- by an access type declaration (and not by a subtype declaration)
11665 -- must match.
11667 Desig_Act := Designated_Type (Base_Type (Act_T));
11669 -- The designated type may have been introduced through a limited_
11670 -- with clause, in which case retrieve the non-limited view. This
11671 -- applies to incomplete types as well as to class-wide types.
11673 if From_Limited_With (Desig_Act) then
11674 Desig_Act := Available_View (Desig_Act);
11675 end if;
11677 if not Subtypes_Match (Desig_Type, Desig_Act) then
11678 Error_Msg_NE
11679 ("designated type of actual does not match that of formal &",
11680 Actual, Gen_T);
11682 if not Predicates_Match (Desig_Type, Desig_Act) then
11683 Error_Msg_N ("\predicates do not match", Actual);
11684 end if;
11686 Abandon_Instantiation (Actual);
11688 elsif Is_Access_Type (Designated_Type (Act_T))
11689 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11691 Is_Constrained (Designated_Type (Desig_Type))
11692 then
11693 Error_Msg_NE
11694 ("designated type of actual does not match that of formal &",
11695 Actual, Gen_T);
11697 if not Predicates_Match (Desig_Type, Desig_Act) then
11698 Error_Msg_N ("\predicates do not match", Actual);
11699 end if;
11701 Abandon_Instantiation (Actual);
11702 end if;
11704 -- Ada 2005: null-exclusion indicators of the two types must agree
11706 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11707 Error_Msg_NE
11708 ("non null exclusion of actual and formal & do not match",
11709 Actual, Gen_T);
11710 end if;
11711 end Validate_Access_Type_Instance;
11713 ----------------------------------
11714 -- Validate_Array_Type_Instance --
11715 ----------------------------------
11717 procedure Validate_Array_Type_Instance is
11718 I1 : Node_Id;
11719 I2 : Node_Id;
11720 T2 : Entity_Id;
11722 function Formal_Dimensions return Nat;
11723 -- Count number of dimensions in array type formal
11725 -----------------------
11726 -- Formal_Dimensions --
11727 -----------------------
11729 function Formal_Dimensions return Nat is
11730 Num : Nat := 0;
11731 Index : Node_Id;
11733 begin
11734 if Nkind (Def) = N_Constrained_Array_Definition then
11735 Index := First (Discrete_Subtype_Definitions (Def));
11736 else
11737 Index := First (Subtype_Marks (Def));
11738 end if;
11740 while Present (Index) loop
11741 Num := Num + 1;
11742 Next_Index (Index);
11743 end loop;
11745 return Num;
11746 end Formal_Dimensions;
11748 -- Start of processing for Validate_Array_Type_Instance
11750 begin
11751 if not Is_Array_Type (Act_T) then
11752 Error_Msg_NE
11753 ("expect array type in instantiation of &", Actual, Gen_T);
11754 Abandon_Instantiation (Actual);
11756 elsif Nkind (Def) = N_Constrained_Array_Definition then
11757 if not (Is_Constrained (Act_T)) then
11758 Error_Msg_NE
11759 ("expect constrained array in instantiation of &",
11760 Actual, Gen_T);
11761 Abandon_Instantiation (Actual);
11762 end if;
11764 else
11765 if Is_Constrained (Act_T) then
11766 Error_Msg_NE
11767 ("expect unconstrained array in instantiation of &",
11768 Actual, Gen_T);
11769 Abandon_Instantiation (Actual);
11770 end if;
11771 end if;
11773 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11774 Error_Msg_NE
11775 ("dimensions of actual do not match formal &", Actual, Gen_T);
11776 Abandon_Instantiation (Actual);
11777 end if;
11779 I1 := First_Index (A_Gen_T);
11780 I2 := First_Index (Act_T);
11781 for J in 1 .. Formal_Dimensions loop
11783 -- If the indexes of the actual were given by a subtype_mark,
11784 -- the index was transformed into a range attribute. Retrieve
11785 -- the original type mark for checking.
11787 if Is_Entity_Name (Original_Node (I2)) then
11788 T2 := Entity (Original_Node (I2));
11789 else
11790 T2 := Etype (I2);
11791 end if;
11793 if not Subtypes_Match
11794 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11795 then
11796 Error_Msg_NE
11797 ("index types of actual do not match those of formal &",
11798 Actual, Gen_T);
11799 Abandon_Instantiation (Actual);
11800 end if;
11802 Next_Index (I1);
11803 Next_Index (I2);
11804 end loop;
11806 -- Check matching subtypes. Note that there are complex visibility
11807 -- issues when the generic is a child unit and some aspect of the
11808 -- generic type is declared in a parent unit of the generic. We do
11809 -- the test to handle this special case only after a direct check
11810 -- for static matching has failed. The case where both the component
11811 -- type and the array type are separate formals, and the component
11812 -- type is a private view may also require special checking in
11813 -- Subtypes_Match.
11815 if Subtypes_Match
11816 (Component_Type (A_Gen_T), Component_Type (Act_T))
11817 or else
11818 Subtypes_Match
11819 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11820 Component_Type (Act_T))
11821 then
11822 null;
11823 else
11824 Error_Msg_NE
11825 ("component subtype of actual does not match that of formal &",
11826 Actual, Gen_T);
11827 Abandon_Instantiation (Actual);
11828 end if;
11830 if Has_Aliased_Components (A_Gen_T)
11831 and then not Has_Aliased_Components (Act_T)
11832 then
11833 Error_Msg_NE
11834 ("actual must have aliased components to match formal type &",
11835 Actual, Gen_T);
11836 end if;
11837 end Validate_Array_Type_Instance;
11839 -----------------------------------------------
11840 -- Validate_Derived_Interface_Type_Instance --
11841 -----------------------------------------------
11843 procedure Validate_Derived_Interface_Type_Instance is
11844 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11845 Elmt : Elmt_Id;
11847 begin
11848 -- First apply interface instance checks
11850 Validate_Interface_Type_Instance;
11852 -- Verify that immediate parent interface is an ancestor of
11853 -- the actual.
11855 if Present (Par)
11856 and then not Interface_Present_In_Ancestor (Act_T, Par)
11857 then
11858 Error_Msg_NE
11859 ("interface actual must include progenitor&", Actual, Par);
11860 end if;
11862 -- Now verify that the actual includes all other ancestors of
11863 -- the formal.
11865 Elmt := First_Elmt (Interfaces (A_Gen_T));
11866 while Present (Elmt) loop
11867 if not Interface_Present_In_Ancestor
11868 (Act_T, Get_Instance_Of (Node (Elmt)))
11869 then
11870 Error_Msg_NE
11871 ("interface actual must include progenitor&",
11872 Actual, Node (Elmt));
11873 end if;
11875 Next_Elmt (Elmt);
11876 end loop;
11877 end Validate_Derived_Interface_Type_Instance;
11879 ------------------------------------
11880 -- Validate_Derived_Type_Instance --
11881 ------------------------------------
11883 procedure Validate_Derived_Type_Instance is
11884 Actual_Discr : Entity_Id;
11885 Ancestor_Discr : Entity_Id;
11887 begin
11888 -- If the parent type in the generic declaration is itself a previous
11889 -- formal type, then it is local to the generic and absent from the
11890 -- analyzed generic definition. In that case the ancestor is the
11891 -- instance of the formal (which must have been instantiated
11892 -- previously), unless the ancestor is itself a formal derived type.
11893 -- In this latter case (which is the subject of Corrigendum 8652/0038
11894 -- (AI-202) the ancestor of the formals is the ancestor of its
11895 -- parent. Otherwise, the analyzed generic carries the parent type.
11896 -- If the parent type is defined in a previous formal package, then
11897 -- the scope of that formal package is that of the generic type
11898 -- itself, and it has already been mapped into the corresponding type
11899 -- in the actual package.
11901 -- Common case: parent type defined outside of the generic
11903 if Is_Entity_Name (Subtype_Mark (Def))
11904 and then Present (Entity (Subtype_Mark (Def)))
11905 then
11906 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11908 -- Check whether parent is defined in a previous formal package
11910 elsif
11911 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11912 then
11913 Ancestor :=
11914 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11916 -- The type may be a local derivation, or a type extension of a
11917 -- previous formal, or of a formal of a parent package.
11919 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11920 or else
11921 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11922 then
11923 -- Check whether the parent is another derived formal type in the
11924 -- same generic unit.
11926 if Etype (A_Gen_T) /= A_Gen_T
11927 and then Is_Generic_Type (Etype (A_Gen_T))
11928 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11929 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11930 then
11931 -- Locate ancestor of parent from the subtype declaration
11932 -- created for the actual.
11934 declare
11935 Decl : Node_Id;
11937 begin
11938 Decl := First (Actual_Decls);
11939 while Present (Decl) loop
11940 if Nkind (Decl) = N_Subtype_Declaration
11941 and then Chars (Defining_Identifier (Decl)) =
11942 Chars (Etype (A_Gen_T))
11943 then
11944 Ancestor := Generic_Parent_Type (Decl);
11945 exit;
11946 else
11947 Next (Decl);
11948 end if;
11949 end loop;
11950 end;
11952 pragma Assert (Present (Ancestor));
11954 -- The ancestor itself may be a previous formal that has been
11955 -- instantiated.
11957 Ancestor := Get_Instance_Of (Ancestor);
11959 else
11960 Ancestor :=
11961 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11962 end if;
11964 -- Check whether parent is a previous formal of the current generic
11966 elsif Is_Derived_Type (A_Gen_T)
11967 and then Is_Generic_Type (Etype (A_Gen_T))
11968 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11969 then
11970 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11972 -- An unusual case: the actual is a type declared in a parent unit,
11973 -- but is not a formal type so there is no instance_of for it.
11974 -- Retrieve it by analyzing the record extension.
11976 elsif Is_Child_Unit (Scope (A_Gen_T))
11977 and then In_Open_Scopes (Scope (Act_T))
11978 and then Is_Generic_Instance (Scope (Act_T))
11979 then
11980 Analyze (Subtype_Mark (Def));
11981 Ancestor := Entity (Subtype_Mark (Def));
11983 else
11984 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11985 end if;
11987 -- If the formal derived type has pragma Preelaborable_Initialization
11988 -- then the actual type must have preelaborable initialization.
11990 if Known_To_Have_Preelab_Init (A_Gen_T)
11991 and then not Has_Preelaborable_Initialization (Act_T)
11992 then
11993 Error_Msg_NE
11994 ("actual for & must have preelaborable initialization",
11995 Actual, Gen_T);
11996 end if;
11998 -- Ada 2005 (AI-251)
12000 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12001 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12002 Error_Msg_NE
12003 ("(Ada 2005) expected type implementing & in instantiation",
12004 Actual, Ancestor);
12005 end if;
12007 -- Finally verify that the (instance of) the ancestor is an ancestor
12008 -- of the actual.
12010 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12011 Error_Msg_NE
12012 ("expect type derived from & in instantiation",
12013 Actual, First_Subtype (Ancestor));
12014 Abandon_Instantiation (Actual);
12015 end if;
12017 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12018 -- that the formal type declaration has been rewritten as a private
12019 -- extension.
12021 if Ada_Version >= Ada_2005
12022 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12023 and then Synchronized_Present (Parent (A_Gen_T))
12024 then
12025 -- The actual must be a synchronized tagged type
12027 if not Is_Tagged_Type (Act_T) then
12028 Error_Msg_N
12029 ("actual of synchronized type must be tagged", Actual);
12030 Abandon_Instantiation (Actual);
12032 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12033 and then Nkind (Type_Definition (Parent (Act_T))) =
12034 N_Derived_Type_Definition
12035 and then not Synchronized_Present
12036 (Type_Definition (Parent (Act_T)))
12037 then
12038 Error_Msg_N
12039 ("actual of synchronized type must be synchronized", Actual);
12040 Abandon_Instantiation (Actual);
12041 end if;
12042 end if;
12044 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12045 -- removes the second instance of the phrase "or allow pass by copy".
12047 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12048 Error_Msg_N
12049 ("cannot have atomic actual type for non-atomic formal type",
12050 Actual);
12052 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12053 Error_Msg_N
12054 ("cannot have volatile actual type for non-volatile formal type",
12055 Actual);
12056 end if;
12058 -- It should not be necessary to check for unknown discriminants on
12059 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12060 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12061 -- needs fixing. ???
12063 if Is_Definite_Subtype (A_Gen_T)
12064 and then not Unknown_Discriminants_Present (Formal)
12065 and then not Is_Definite_Subtype (Act_T)
12066 then
12067 Error_Msg_N ("actual subtype must be constrained", Actual);
12068 Abandon_Instantiation (Actual);
12069 end if;
12071 if not Unknown_Discriminants_Present (Formal) then
12072 if Is_Constrained (Ancestor) then
12073 if not Is_Constrained (Act_T) then
12074 Error_Msg_N ("actual subtype must be constrained", Actual);
12075 Abandon_Instantiation (Actual);
12076 end if;
12078 -- Ancestor is unconstrained, Check if generic formal and actual
12079 -- agree on constrainedness. The check only applies to array types
12080 -- and discriminated types.
12082 elsif Is_Constrained (Act_T) then
12083 if Ekind (Ancestor) = E_Access_Type
12084 or else (not Is_Constrained (A_Gen_T)
12085 and then Is_Composite_Type (A_Gen_T))
12086 then
12087 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12088 Abandon_Instantiation (Actual);
12089 end if;
12091 -- A class-wide type is only allowed if the formal has unknown
12092 -- discriminants.
12094 elsif Is_Class_Wide_Type (Act_T)
12095 and then not Has_Unknown_Discriminants (Ancestor)
12096 then
12097 Error_Msg_NE
12098 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12099 Abandon_Instantiation (Actual);
12101 -- Otherwise, the formal and actual must have the same number
12102 -- of discriminants and each discriminant of the actual must
12103 -- correspond to a discriminant of the formal.
12105 elsif Has_Discriminants (Act_T)
12106 and then not Has_Unknown_Discriminants (Act_T)
12107 and then Has_Discriminants (Ancestor)
12108 then
12109 Actual_Discr := First_Discriminant (Act_T);
12110 Ancestor_Discr := First_Discriminant (Ancestor);
12111 while Present (Actual_Discr)
12112 and then Present (Ancestor_Discr)
12113 loop
12114 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12115 No (Corresponding_Discriminant (Actual_Discr))
12116 then
12117 Error_Msg_NE
12118 ("discriminant & does not correspond "
12119 & "to ancestor discriminant", Actual, Actual_Discr);
12120 Abandon_Instantiation (Actual);
12121 end if;
12123 Next_Discriminant (Actual_Discr);
12124 Next_Discriminant (Ancestor_Discr);
12125 end loop;
12127 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12128 Error_Msg_NE
12129 ("actual for & must have same number of discriminants",
12130 Actual, Gen_T);
12131 Abandon_Instantiation (Actual);
12132 end if;
12134 -- This case should be caught by the earlier check for
12135 -- constrainedness, but the check here is added for completeness.
12137 elsif Has_Discriminants (Act_T)
12138 and then not Has_Unknown_Discriminants (Act_T)
12139 then
12140 Error_Msg_NE
12141 ("actual for & must not have discriminants", Actual, Gen_T);
12142 Abandon_Instantiation (Actual);
12144 elsif Has_Discriminants (Ancestor) then
12145 Error_Msg_NE
12146 ("actual for & must have known discriminants", Actual, Gen_T);
12147 Abandon_Instantiation (Actual);
12148 end if;
12150 if not Subtypes_Statically_Compatible
12151 (Act_T, Ancestor, Formal_Derived_Matching => True)
12152 then
12153 Error_Msg_N
12154 ("constraint on actual is incompatible with formal", Actual);
12155 Abandon_Instantiation (Actual);
12156 end if;
12157 end if;
12159 -- If the formal and actual types are abstract, check that there
12160 -- are no abstract primitives of the actual type that correspond to
12161 -- nonabstract primitives of the formal type (second sentence of
12162 -- RM95 3.9.3(9)).
12164 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12165 Check_Abstract_Primitives : declare
12166 Gen_Prims : constant Elist_Id :=
12167 Primitive_Operations (A_Gen_T);
12168 Gen_Elmt : Elmt_Id;
12169 Gen_Subp : Entity_Id;
12170 Anc_Subp : Entity_Id;
12171 Anc_Formal : Entity_Id;
12172 Anc_F_Type : Entity_Id;
12174 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12175 Act_Elmt : Elmt_Id;
12176 Act_Subp : Entity_Id;
12177 Act_Formal : Entity_Id;
12178 Act_F_Type : Entity_Id;
12180 Subprograms_Correspond : Boolean;
12182 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12183 -- Returns true if T2 is derived directly or indirectly from
12184 -- T1, including derivations from interfaces. T1 and T2 are
12185 -- required to be specific tagged base types.
12187 ------------------------
12188 -- Is_Tagged_Ancestor --
12189 ------------------------
12191 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12193 Intfc_Elmt : Elmt_Id;
12195 begin
12196 -- The predicate is satisfied if the types are the same
12198 if T1 = T2 then
12199 return True;
12201 -- If we've reached the top of the derivation chain then
12202 -- we know that T1 is not an ancestor of T2.
12204 elsif Etype (T2) = T2 then
12205 return False;
12207 -- Proceed to check T2's immediate parent
12209 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12210 return True;
12212 -- Finally, check to see if T1 is an ancestor of any of T2's
12213 -- progenitors.
12215 else
12216 Intfc_Elmt := First_Elmt (Interfaces (T2));
12217 while Present (Intfc_Elmt) loop
12218 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12219 return True;
12220 end if;
12222 Next_Elmt (Intfc_Elmt);
12223 end loop;
12224 end if;
12226 return False;
12227 end Is_Tagged_Ancestor;
12229 -- Start of processing for Check_Abstract_Primitives
12231 begin
12232 -- Loop over all of the formal derived type's primitives
12234 Gen_Elmt := First_Elmt (Gen_Prims);
12235 while Present (Gen_Elmt) loop
12236 Gen_Subp := Node (Gen_Elmt);
12238 -- If the primitive of the formal is not abstract, then
12239 -- determine whether there is a corresponding primitive of
12240 -- the actual type that's abstract.
12242 if not Is_Abstract_Subprogram (Gen_Subp) then
12243 Act_Elmt := First_Elmt (Act_Prims);
12244 while Present (Act_Elmt) loop
12245 Act_Subp := Node (Act_Elmt);
12247 -- If we find an abstract primitive of the actual,
12248 -- then we need to test whether it corresponds to the
12249 -- subprogram from which the generic formal primitive
12250 -- is inherited.
12252 if Is_Abstract_Subprogram (Act_Subp) then
12253 Anc_Subp := Alias (Gen_Subp);
12255 -- Test whether we have a corresponding primitive
12256 -- by comparing names, kinds, formal types, and
12257 -- result types.
12259 if Chars (Anc_Subp) = Chars (Act_Subp)
12260 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12261 then
12262 Anc_Formal := First_Formal (Anc_Subp);
12263 Act_Formal := First_Formal (Act_Subp);
12264 while Present (Anc_Formal)
12265 and then Present (Act_Formal)
12266 loop
12267 Anc_F_Type := Etype (Anc_Formal);
12268 Act_F_Type := Etype (Act_Formal);
12270 if Ekind (Anc_F_Type) =
12271 E_Anonymous_Access_Type
12272 then
12273 Anc_F_Type := Designated_Type (Anc_F_Type);
12275 if Ekind (Act_F_Type) =
12276 E_Anonymous_Access_Type
12277 then
12278 Act_F_Type :=
12279 Designated_Type (Act_F_Type);
12280 else
12281 exit;
12282 end if;
12284 elsif
12285 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12286 then
12287 exit;
12288 end if;
12290 Anc_F_Type := Base_Type (Anc_F_Type);
12291 Act_F_Type := Base_Type (Act_F_Type);
12293 -- If the formal is controlling, then the
12294 -- the type of the actual primitive's formal
12295 -- must be derived directly or indirectly
12296 -- from the type of the ancestor primitive's
12297 -- formal.
12299 if Is_Controlling_Formal (Anc_Formal) then
12300 if not Is_Tagged_Ancestor
12301 (Anc_F_Type, Act_F_Type)
12302 then
12303 exit;
12304 end if;
12306 -- Otherwise the types of the formals must
12307 -- be the same.
12309 elsif Anc_F_Type /= Act_F_Type then
12310 exit;
12311 end if;
12313 Next_Entity (Anc_Formal);
12314 Next_Entity (Act_Formal);
12315 end loop;
12317 -- If we traversed through all of the formals
12318 -- then so far the subprograms correspond, so
12319 -- now check that any result types correspond.
12321 if No (Anc_Formal) and then No (Act_Formal) then
12322 Subprograms_Correspond := True;
12324 if Ekind (Act_Subp) = E_Function then
12325 Anc_F_Type := Etype (Anc_Subp);
12326 Act_F_Type := Etype (Act_Subp);
12328 if Ekind (Anc_F_Type) =
12329 E_Anonymous_Access_Type
12330 then
12331 Anc_F_Type :=
12332 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 Subprograms_Correspond := False;
12341 end if;
12343 elsif
12344 Ekind (Act_F_Type)
12345 = E_Anonymous_Access_Type
12346 then
12347 Subprograms_Correspond := False;
12348 end if;
12350 Anc_F_Type := Base_Type (Anc_F_Type);
12351 Act_F_Type := Base_Type (Act_F_Type);
12353 -- Now either the result types must be
12354 -- the same or, if the result type is
12355 -- controlling, the result type of the
12356 -- actual primitive must descend from the
12357 -- result type of the ancestor primitive.
12359 if Subprograms_Correspond
12360 and then Anc_F_Type /= Act_F_Type
12361 and then
12362 Has_Controlling_Result (Anc_Subp)
12363 and then not Is_Tagged_Ancestor
12364 (Anc_F_Type, Act_F_Type)
12365 then
12366 Subprograms_Correspond := False;
12367 end if;
12368 end if;
12370 -- Found a matching subprogram belonging to
12371 -- formal ancestor type, so actual subprogram
12372 -- corresponds and this violates 3.9.3(9).
12374 if Subprograms_Correspond then
12375 Error_Msg_NE
12376 ("abstract subprogram & overrides "
12377 & "nonabstract subprogram of ancestor",
12378 Actual, Act_Subp);
12379 end if;
12380 end if;
12381 end if;
12382 end if;
12384 Next_Elmt (Act_Elmt);
12385 end loop;
12386 end if;
12388 Next_Elmt (Gen_Elmt);
12389 end loop;
12390 end Check_Abstract_Primitives;
12391 end if;
12393 -- Verify that limitedness matches. If parent is a limited
12394 -- interface then the generic formal is not unless declared
12395 -- explicitly so. If not declared limited, the actual cannot be
12396 -- limited (see AI05-0087).
12398 -- Even though this AI is a binding interpretation, we enable the
12399 -- check only in Ada 2012 mode, because this improper construct
12400 -- shows up in user code and in existing B-tests.
12402 if Is_Limited_Type (Act_T)
12403 and then not Is_Limited_Type (A_Gen_T)
12404 and then Ada_Version >= Ada_2012
12405 then
12406 if In_Instance then
12407 null;
12408 else
12409 Error_Msg_NE
12410 ("actual for non-limited & cannot be a limited type",
12411 Actual, Gen_T);
12412 Explain_Limited_Type (Act_T, Actual);
12413 Abandon_Instantiation (Actual);
12414 end if;
12415 end if;
12416 end Validate_Derived_Type_Instance;
12418 ----------------------------------------
12419 -- Validate_Discriminated_Formal_Type --
12420 ----------------------------------------
12422 procedure Validate_Discriminated_Formal_Type is
12423 Formal_Discr : Entity_Id;
12424 Actual_Discr : Entity_Id;
12425 Formal_Subt : Entity_Id;
12427 begin
12428 if Has_Discriminants (A_Gen_T) then
12429 if not Has_Discriminants (Act_T) then
12430 Error_Msg_NE
12431 ("actual for & must have discriminants", Actual, Gen_T);
12432 Abandon_Instantiation (Actual);
12434 elsif Is_Constrained (Act_T) then
12435 Error_Msg_NE
12436 ("actual for & must be unconstrained", Actual, Gen_T);
12437 Abandon_Instantiation (Actual);
12439 else
12440 Formal_Discr := First_Discriminant (A_Gen_T);
12441 Actual_Discr := First_Discriminant (Act_T);
12442 while Formal_Discr /= Empty loop
12443 if Actual_Discr = Empty then
12444 Error_Msg_NE
12445 ("discriminants on actual do not match formal",
12446 Actual, Gen_T);
12447 Abandon_Instantiation (Actual);
12448 end if;
12450 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12452 -- Access discriminants match if designated types do
12454 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12455 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12456 E_Anonymous_Access_Type
12457 and then
12458 Get_Instance_Of
12459 (Designated_Type (Base_Type (Formal_Subt))) =
12460 Designated_Type (Base_Type (Etype (Actual_Discr)))
12461 then
12462 null;
12464 elsif Base_Type (Formal_Subt) /=
12465 Base_Type (Etype (Actual_Discr))
12466 then
12467 Error_Msg_NE
12468 ("types of actual discriminants must match formal",
12469 Actual, Gen_T);
12470 Abandon_Instantiation (Actual);
12472 elsif not Subtypes_Statically_Match
12473 (Formal_Subt, Etype (Actual_Discr))
12474 and then Ada_Version >= Ada_95
12475 then
12476 Error_Msg_NE
12477 ("subtypes of actual discriminants must match formal",
12478 Actual, Gen_T);
12479 Abandon_Instantiation (Actual);
12480 end if;
12482 Next_Discriminant (Formal_Discr);
12483 Next_Discriminant (Actual_Discr);
12484 end loop;
12486 if Actual_Discr /= Empty then
12487 Error_Msg_NE
12488 ("discriminants on actual do not match formal",
12489 Actual, Gen_T);
12490 Abandon_Instantiation (Actual);
12491 end if;
12492 end if;
12493 end if;
12494 end Validate_Discriminated_Formal_Type;
12496 ---------------------------------------
12497 -- Validate_Incomplete_Type_Instance --
12498 ---------------------------------------
12500 procedure Validate_Incomplete_Type_Instance is
12501 begin
12502 if not Is_Tagged_Type (Act_T)
12503 and then Is_Tagged_Type (A_Gen_T)
12504 then
12505 Error_Msg_NE
12506 ("actual for & must be a tagged type", Actual, Gen_T);
12507 end if;
12509 Validate_Discriminated_Formal_Type;
12510 end Validate_Incomplete_Type_Instance;
12512 --------------------------------------
12513 -- Validate_Interface_Type_Instance --
12514 --------------------------------------
12516 procedure Validate_Interface_Type_Instance is
12517 begin
12518 if not Is_Interface (Act_T) then
12519 Error_Msg_NE
12520 ("actual for formal interface type must be an interface",
12521 Actual, Gen_T);
12523 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12524 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12525 or else Is_Protected_Interface (A_Gen_T) /=
12526 Is_Protected_Interface (Act_T)
12527 or else Is_Synchronized_Interface (A_Gen_T) /=
12528 Is_Synchronized_Interface (Act_T)
12529 then
12530 Error_Msg_NE
12531 ("actual for interface& does not match (RM 12.5.5(4))",
12532 Actual, Gen_T);
12533 end if;
12534 end Validate_Interface_Type_Instance;
12536 ------------------------------------
12537 -- Validate_Private_Type_Instance --
12538 ------------------------------------
12540 procedure Validate_Private_Type_Instance is
12541 begin
12542 if Is_Limited_Type (Act_T)
12543 and then not Is_Limited_Type (A_Gen_T)
12544 then
12545 if In_Instance then
12546 null;
12547 else
12548 Error_Msg_NE
12549 ("actual for non-limited & cannot be a limited type", Actual,
12550 Gen_T);
12551 Explain_Limited_Type (Act_T, Actual);
12552 Abandon_Instantiation (Actual);
12553 end if;
12555 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12556 and then not Has_Preelaborable_Initialization (Act_T)
12557 then
12558 Error_Msg_NE
12559 ("actual for & must have preelaborable initialization", Actual,
12560 Gen_T);
12562 elsif not Is_Definite_Subtype (Act_T)
12563 and then Is_Definite_Subtype (A_Gen_T)
12564 and then Ada_Version >= Ada_95
12565 then
12566 Error_Msg_NE
12567 ("actual for & must be a definite subtype", Actual, Gen_T);
12569 elsif not Is_Tagged_Type (Act_T)
12570 and then Is_Tagged_Type (A_Gen_T)
12571 then
12572 Error_Msg_NE
12573 ("actual for & must be a tagged type", Actual, Gen_T);
12574 end if;
12576 Validate_Discriminated_Formal_Type;
12577 Ancestor := Gen_T;
12578 end Validate_Private_Type_Instance;
12580 -- Start of processing for Instantiate_Type
12582 begin
12583 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12584 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12585 return New_List (Error);
12587 elsif not Is_Entity_Name (Actual)
12588 or else not Is_Type (Entity (Actual))
12589 then
12590 Error_Msg_NE
12591 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12592 Abandon_Instantiation (Actual);
12594 else
12595 Act_T := Entity (Actual);
12597 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12598 -- as a generic actual parameter if the corresponding formal type
12599 -- does not have a known_discriminant_part, or is a formal derived
12600 -- type that is an Unchecked_Union type.
12602 if Is_Unchecked_Union (Base_Type (Act_T)) then
12603 if not Has_Discriminants (A_Gen_T)
12604 or else (Is_Derived_Type (A_Gen_T)
12605 and then Is_Unchecked_Union (A_Gen_T))
12606 then
12607 null;
12608 else
12609 Error_Msg_N ("unchecked union cannot be the actual for a "
12610 & "discriminated formal type", Act_T);
12612 end if;
12613 end if;
12615 -- Deal with fixed/floating restrictions
12617 if Is_Floating_Point_Type (Act_T) then
12618 Check_Restriction (No_Floating_Point, Actual);
12619 elsif Is_Fixed_Point_Type (Act_T) then
12620 Check_Restriction (No_Fixed_Point, Actual);
12621 end if;
12623 -- Deal with error of using incomplete type as generic actual.
12624 -- This includes limited views of a type, even if the non-limited
12625 -- view may be available.
12627 if Ekind (Act_T) = E_Incomplete_Type
12628 or else (Is_Class_Wide_Type (Act_T)
12629 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12630 then
12631 -- If the formal is an incomplete type, the actual can be
12632 -- incomplete as well.
12634 if Ekind (A_Gen_T) = E_Incomplete_Type then
12635 null;
12637 elsif Is_Class_Wide_Type (Act_T)
12638 or else No (Full_View (Act_T))
12639 then
12640 Error_Msg_N ("premature use of incomplete type", Actual);
12641 Abandon_Instantiation (Actual);
12642 else
12643 Act_T := Full_View (Act_T);
12644 Set_Entity (Actual, Act_T);
12646 if Has_Private_Component (Act_T) then
12647 Error_Msg_N
12648 ("premature use of type with private component", Actual);
12649 end if;
12650 end if;
12652 -- Deal with error of premature use of private type as generic actual
12654 elsif Is_Private_Type (Act_T)
12655 and then Is_Private_Type (Base_Type (Act_T))
12656 and then not Is_Generic_Type (Act_T)
12657 and then not Is_Derived_Type (Act_T)
12658 and then No (Full_View (Root_Type (Act_T)))
12659 then
12660 -- If the formal is an incomplete type, the actual can be
12661 -- private or incomplete as well.
12663 if Ekind (A_Gen_T) = E_Incomplete_Type then
12664 null;
12665 else
12666 Error_Msg_N ("premature use of private type", Actual);
12667 end if;
12669 elsif Has_Private_Component (Act_T) then
12670 Error_Msg_N
12671 ("premature use of type with private component", Actual);
12672 end if;
12674 Set_Instance_Of (A_Gen_T, Act_T);
12676 -- If the type is generic, the class-wide type may also be used
12678 if Is_Tagged_Type (A_Gen_T)
12679 and then Is_Tagged_Type (Act_T)
12680 and then not Is_Class_Wide_Type (A_Gen_T)
12681 then
12682 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12683 Class_Wide_Type (Act_T));
12684 end if;
12686 if not Is_Abstract_Type (A_Gen_T)
12687 and then Is_Abstract_Type (Act_T)
12688 then
12689 Error_Msg_N
12690 ("actual of non-abstract formal cannot be abstract", Actual);
12691 end if;
12693 -- A generic scalar type is a first subtype for which we generate
12694 -- an anonymous base type. Indicate that the instance of this base
12695 -- is the base type of the actual.
12697 if Is_Scalar_Type (A_Gen_T) then
12698 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12699 end if;
12700 end if;
12702 if Error_Posted (Act_T) then
12703 null;
12704 else
12705 case Nkind (Def) is
12706 when N_Formal_Private_Type_Definition =>
12707 Validate_Private_Type_Instance;
12709 when N_Formal_Incomplete_Type_Definition =>
12710 Validate_Incomplete_Type_Instance;
12712 when N_Formal_Derived_Type_Definition =>
12713 Validate_Derived_Type_Instance;
12715 when N_Formal_Discrete_Type_Definition =>
12716 if not Is_Discrete_Type (Act_T) then
12717 Error_Msg_NE
12718 ("expect discrete type in instantiation of&",
12719 Actual, Gen_T);
12720 Abandon_Instantiation (Actual);
12721 end if;
12723 Diagnose_Predicated_Actual;
12725 when N_Formal_Signed_Integer_Type_Definition =>
12726 if not Is_Signed_Integer_Type (Act_T) then
12727 Error_Msg_NE
12728 ("expect signed integer type in instantiation of&",
12729 Actual, Gen_T);
12730 Abandon_Instantiation (Actual);
12731 end if;
12733 Diagnose_Predicated_Actual;
12735 when N_Formal_Modular_Type_Definition =>
12736 if not Is_Modular_Integer_Type (Act_T) then
12737 Error_Msg_NE
12738 ("expect modular type in instantiation of &",
12739 Actual, Gen_T);
12740 Abandon_Instantiation (Actual);
12741 end if;
12743 Diagnose_Predicated_Actual;
12745 when N_Formal_Floating_Point_Definition =>
12746 if not Is_Floating_Point_Type (Act_T) then
12747 Error_Msg_NE
12748 ("expect float type in instantiation of &", Actual, Gen_T);
12749 Abandon_Instantiation (Actual);
12750 end if;
12752 when N_Formal_Ordinary_Fixed_Point_Definition =>
12753 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12754 Error_Msg_NE
12755 ("expect ordinary fixed point type in instantiation of &",
12756 Actual, Gen_T);
12757 Abandon_Instantiation (Actual);
12758 end if;
12760 when N_Formal_Decimal_Fixed_Point_Definition =>
12761 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12762 Error_Msg_NE
12763 ("expect decimal type in instantiation of &",
12764 Actual, Gen_T);
12765 Abandon_Instantiation (Actual);
12766 end if;
12768 when N_Array_Type_Definition =>
12769 Validate_Array_Type_Instance;
12771 when N_Access_To_Object_Definition =>
12772 Validate_Access_Type_Instance;
12774 when N_Access_Function_Definition |
12775 N_Access_Procedure_Definition =>
12776 Validate_Access_Subprogram_Instance;
12778 when N_Record_Definition =>
12779 Validate_Interface_Type_Instance;
12781 when N_Derived_Type_Definition =>
12782 Validate_Derived_Interface_Type_Instance;
12784 when others =>
12785 raise Program_Error;
12787 end case;
12788 end if;
12790 Subt := New_Copy (Gen_T);
12792 -- Use adjusted sloc of subtype name as the location for other nodes in
12793 -- the subtype declaration.
12795 Loc := Sloc (Subt);
12797 Decl_Node :=
12798 Make_Subtype_Declaration (Loc,
12799 Defining_Identifier => Subt,
12800 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12802 if Is_Private_Type (Act_T) then
12803 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12805 elsif Is_Access_Type (Act_T)
12806 and then Is_Private_Type (Designated_Type (Act_T))
12807 then
12808 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12809 end if;
12811 -- In Ada 2012 the actual may be a limited view. Indicate that
12812 -- the local subtype must be treated as such.
12814 if From_Limited_With (Act_T) then
12815 Set_Ekind (Subt, E_Incomplete_Subtype);
12816 Set_From_Limited_With (Subt);
12817 end if;
12819 Decl_Nodes := New_List (Decl_Node);
12821 -- Flag actual derived types so their elaboration produces the
12822 -- appropriate renamings for the primitive operations of the ancestor.
12823 -- Flag actual for formal private types as well, to determine whether
12824 -- operations in the private part may override inherited operations.
12825 -- If the formal has an interface list, the ancestor is not the
12826 -- parent, but the analyzed formal that includes the interface
12827 -- operations of all its progenitors.
12829 -- Same treatment for formal private types, so we can check whether the
12830 -- type is tagged limited when validating derivations in the private
12831 -- part. (See AI05-096).
12833 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12834 if Present (Interface_List (Def)) then
12835 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12836 else
12837 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12838 end if;
12840 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12841 N_Formal_Incomplete_Type_Definition)
12842 then
12843 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12844 end if;
12846 -- If the actual is a synchronized type that implements an interface,
12847 -- the primitive operations are attached to the corresponding record,
12848 -- and we have to treat it as an additional generic actual, so that its
12849 -- primitive operations become visible in the instance. The task or
12850 -- protected type itself does not carry primitive operations.
12852 if Is_Concurrent_Type (Act_T)
12853 and then Is_Tagged_Type (Act_T)
12854 and then Present (Corresponding_Record_Type (Act_T))
12855 and then Present (Ancestor)
12856 and then Is_Interface (Ancestor)
12857 then
12858 declare
12859 Corr_Rec : constant Entity_Id :=
12860 Corresponding_Record_Type (Act_T);
12861 New_Corr : Entity_Id;
12862 Corr_Decl : Node_Id;
12864 begin
12865 New_Corr := Make_Temporary (Loc, 'S');
12866 Corr_Decl :=
12867 Make_Subtype_Declaration (Loc,
12868 Defining_Identifier => New_Corr,
12869 Subtype_Indication =>
12870 New_Occurrence_Of (Corr_Rec, Loc));
12871 Append_To (Decl_Nodes, Corr_Decl);
12873 if Ekind (Act_T) = E_Task_Type then
12874 Set_Ekind (Subt, E_Task_Subtype);
12875 else
12876 Set_Ekind (Subt, E_Protected_Subtype);
12877 end if;
12879 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12880 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12881 Set_Generic_Parent_Type (Decl_Node, Empty);
12882 end;
12883 end if;
12885 -- For a floating-point type, capture dimension info if any, because
12886 -- the generated subtype declaration does not come from source and
12887 -- will not process dimensions.
12889 if Is_Floating_Point_Type (Act_T) then
12890 Copy_Dimensions (Act_T, Subt);
12891 end if;
12893 return Decl_Nodes;
12894 end Instantiate_Type;
12896 ---------------------
12897 -- Is_In_Main_Unit --
12898 ---------------------
12900 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12901 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12902 Current_Unit : Node_Id;
12904 begin
12905 if Unum = Main_Unit then
12906 return True;
12908 -- If the current unit is a subunit then it is either the main unit or
12909 -- is being compiled as part of the main unit.
12911 elsif Nkind (N) = N_Compilation_Unit then
12912 return Nkind (Unit (N)) = N_Subunit;
12913 end if;
12915 Current_Unit := Parent (N);
12916 while Present (Current_Unit)
12917 and then Nkind (Current_Unit) /= N_Compilation_Unit
12918 loop
12919 Current_Unit := Parent (Current_Unit);
12920 end loop;
12922 -- The instantiation node is in the main unit, or else the current node
12923 -- (perhaps as the result of nested instantiations) is in the main unit,
12924 -- or in the declaration of the main unit, which in this last case must
12925 -- be a body.
12927 return
12928 Current_Unit = Cunit (Main_Unit)
12929 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12930 or else (Present (Current_Unit)
12931 and then Present (Library_Unit (Current_Unit))
12932 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12933 end Is_In_Main_Unit;
12935 ----------------------------
12936 -- Load_Parent_Of_Generic --
12937 ----------------------------
12939 procedure Load_Parent_Of_Generic
12940 (N : Node_Id;
12941 Spec : Node_Id;
12942 Body_Optional : Boolean := False)
12944 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12945 Saved_Style_Check : constant Boolean := Style_Check;
12946 Saved_Warnings : constant Warning_Record := Save_Warnings;
12947 True_Parent : Node_Id;
12948 Inst_Node : Node_Id;
12949 OK : Boolean;
12950 Previous_Instances : constant Elist_Id := New_Elmt_List;
12952 procedure Collect_Previous_Instances (Decls : List_Id);
12953 -- Collect all instantiations in the given list of declarations, that
12954 -- precede the generic that we need to load. If the bodies of these
12955 -- instantiations are available, we must analyze them, to ensure that
12956 -- the public symbols generated are the same when the unit is compiled
12957 -- to generate code, and when it is compiled in the context of a unit
12958 -- that needs a particular nested instance. This process is applied to
12959 -- both package and subprogram instances.
12961 --------------------------------
12962 -- Collect_Previous_Instances --
12963 --------------------------------
12965 procedure Collect_Previous_Instances (Decls : List_Id) is
12966 Decl : Node_Id;
12968 begin
12969 Decl := First (Decls);
12970 while Present (Decl) loop
12971 if Sloc (Decl) >= Sloc (Inst_Node) then
12972 return;
12974 -- If Decl is an instantiation, then record it as requiring
12975 -- instantiation of the corresponding body, except if it is an
12976 -- abbreviated instantiation generated internally for conformance
12977 -- checking purposes only for the case of a formal package
12978 -- declared without a box (see Instantiate_Formal_Package). Such
12979 -- an instantiation does not generate any code (the actual code
12980 -- comes from actual) and thus does not need to be analyzed here.
12981 -- If the instantiation appears with a generic package body it is
12982 -- not analyzed here either.
12984 elsif Nkind (Decl) = N_Package_Instantiation
12985 and then not Is_Internal (Defining_Entity (Decl))
12986 then
12987 Append_Elmt (Decl, Previous_Instances);
12989 -- For a subprogram instantiation, omit instantiations intrinsic
12990 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12992 elsif Nkind_In (Decl, N_Function_Instantiation,
12993 N_Procedure_Instantiation)
12994 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12995 then
12996 Append_Elmt (Decl, Previous_Instances);
12998 elsif Nkind (Decl) = N_Package_Declaration then
12999 Collect_Previous_Instances
13000 (Visible_Declarations (Specification (Decl)));
13001 Collect_Previous_Instances
13002 (Private_Declarations (Specification (Decl)));
13004 -- Previous non-generic bodies may contain instances as well
13006 elsif Nkind (Decl) = N_Package_Body
13007 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13008 then
13009 Collect_Previous_Instances (Declarations (Decl));
13011 elsif Nkind (Decl) = N_Subprogram_Body
13012 and then not Acts_As_Spec (Decl)
13013 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13014 then
13015 Collect_Previous_Instances (Declarations (Decl));
13016 end if;
13018 Next (Decl);
13019 end loop;
13020 end Collect_Previous_Instances;
13022 -- Start of processing for Load_Parent_Of_Generic
13024 begin
13025 if not In_Same_Source_Unit (N, Spec)
13026 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13027 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13028 and then not Is_In_Main_Unit (Spec))
13029 then
13030 -- Find body of parent of spec, and analyze it. A special case arises
13031 -- when the parent is an instantiation, that is to say when we are
13032 -- currently instantiating a nested generic. In that case, there is
13033 -- no separate file for the body of the enclosing instance. Instead,
13034 -- the enclosing body must be instantiated as if it were a pending
13035 -- instantiation, in order to produce the body for the nested generic
13036 -- we require now. Note that in that case the generic may be defined
13037 -- in a package body, the instance defined in the same package body,
13038 -- and the original enclosing body may not be in the main unit.
13040 Inst_Node := Empty;
13042 True_Parent := Parent (Spec);
13043 while Present (True_Parent)
13044 and then Nkind (True_Parent) /= N_Compilation_Unit
13045 loop
13046 if Nkind (True_Parent) = N_Package_Declaration
13047 and then
13048 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13049 then
13050 -- Parent is a compilation unit that is an instantiation.
13051 -- Instantiation node has been replaced with package decl.
13053 Inst_Node := Original_Node (True_Parent);
13054 exit;
13056 elsif Nkind (True_Parent) = N_Package_Declaration
13057 and then Present (Generic_Parent (Specification (True_Parent)))
13058 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13059 then
13060 -- Parent is an instantiation within another specification.
13061 -- Declaration for instance has been inserted before original
13062 -- instantiation node. A direct link would be preferable?
13064 Inst_Node := Next (True_Parent);
13065 while Present (Inst_Node)
13066 and then Nkind (Inst_Node) /= N_Package_Instantiation
13067 loop
13068 Next (Inst_Node);
13069 end loop;
13071 -- If the instance appears within a generic, and the generic
13072 -- unit is defined within a formal package of the enclosing
13073 -- generic, there is no generic body available, and none
13074 -- needed. A more precise test should be used ???
13076 if No (Inst_Node) then
13077 return;
13078 end if;
13080 exit;
13082 else
13083 True_Parent := Parent (True_Parent);
13084 end if;
13085 end loop;
13087 -- Case where we are currently instantiating a nested generic
13089 if Present (Inst_Node) then
13090 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13092 -- Instantiation node and declaration of instantiated package
13093 -- were exchanged when only the declaration was needed.
13094 -- Restore instantiation node before proceeding with body.
13096 Set_Unit (Parent (True_Parent), Inst_Node);
13097 end if;
13099 -- Now complete instantiation of enclosing body, if it appears in
13100 -- some other unit. If it appears in the current unit, the body
13101 -- will have been instantiated already.
13103 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13105 -- We need to determine the expander mode to instantiate the
13106 -- enclosing body. Because the generic body we need may use
13107 -- global entities declared in the enclosing package (including
13108 -- aggregates) it is in general necessary to compile this body
13109 -- with expansion enabled, except if we are within a generic
13110 -- package, in which case the usual generic rule applies.
13112 declare
13113 Exp_Status : Boolean := True;
13114 Scop : Entity_Id;
13116 begin
13117 -- Loop through scopes looking for generic package
13119 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13120 while Present (Scop)
13121 and then Scop /= Standard_Standard
13122 loop
13123 if Ekind (Scop) = E_Generic_Package then
13124 Exp_Status := False;
13125 exit;
13126 end if;
13128 Scop := Scope (Scop);
13129 end loop;
13131 -- Collect previous instantiations in the unit that contains
13132 -- the desired generic.
13134 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13135 and then not Body_Optional
13136 then
13137 declare
13138 Decl : Elmt_Id;
13139 Info : Pending_Body_Info;
13140 Par : Node_Id;
13142 begin
13143 Par := Parent (Inst_Node);
13144 while Present (Par) loop
13145 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13146 Par := Parent (Par);
13147 end loop;
13149 pragma Assert (Present (Par));
13151 if Nkind (Par) = N_Package_Body then
13152 Collect_Previous_Instances (Declarations (Par));
13154 elsif Nkind (Par) = N_Package_Declaration then
13155 Collect_Previous_Instances
13156 (Visible_Declarations (Specification (Par)));
13157 Collect_Previous_Instances
13158 (Private_Declarations (Specification (Par)));
13160 else
13161 -- Enclosing unit is a subprogram body. In this
13162 -- case all instance bodies are processed in order
13163 -- and there is no need to collect them separately.
13165 null;
13166 end if;
13168 Decl := First_Elmt (Previous_Instances);
13169 while Present (Decl) loop
13170 Info :=
13171 (Inst_Node => Node (Decl),
13172 Act_Decl =>
13173 Instance_Spec (Node (Decl)),
13174 Expander_Status => Exp_Status,
13175 Current_Sem_Unit =>
13176 Get_Code_Unit (Sloc (Node (Decl))),
13177 Scope_Suppress => Scope_Suppress,
13178 Local_Suppress_Stack_Top =>
13179 Local_Suppress_Stack_Top,
13180 Version => Ada_Version,
13181 Version_Pragma => Ada_Version_Pragma,
13182 Warnings => Save_Warnings,
13183 SPARK_Mode => SPARK_Mode,
13184 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13186 -- Package instance
13189 Nkind (Node (Decl)) = N_Package_Instantiation
13190 then
13191 Instantiate_Package_Body
13192 (Info, Body_Optional => True);
13194 -- Subprogram instance
13196 else
13197 -- The instance_spec is in the wrapper package,
13198 -- usually followed by its local renaming
13199 -- declaration. See Build_Subprogram_Renaming
13200 -- for details. If the instance carries aspects,
13201 -- these result in the corresponding pragmas,
13202 -- inserted after the subprogram declaration.
13203 -- They must be skipped as well when retrieving
13204 -- the desired spec. A direct link would be
13205 -- more robust ???
13207 declare
13208 Decl : Node_Id :=
13209 (Last (Visible_Declarations
13210 (Specification (Info.Act_Decl))));
13211 begin
13212 while Nkind_In (Decl,
13213 N_Subprogram_Renaming_Declaration, N_Pragma)
13214 loop
13215 Decl := Prev (Decl);
13216 end loop;
13218 Info.Act_Decl := Decl;
13219 end;
13221 Instantiate_Subprogram_Body
13222 (Info, Body_Optional => True);
13223 end if;
13225 Next_Elmt (Decl);
13226 end loop;
13227 end;
13228 end if;
13230 Instantiate_Package_Body
13231 (Body_Info =>
13232 ((Inst_Node => Inst_Node,
13233 Act_Decl => True_Parent,
13234 Expander_Status => Exp_Status,
13235 Current_Sem_Unit => Get_Code_Unit
13236 (Sloc (Inst_Node)),
13237 Scope_Suppress => Scope_Suppress,
13238 Local_Suppress_Stack_Top => 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)),
13244 Body_Optional => Body_Optional);
13245 end;
13246 end if;
13248 -- Case where we are not instantiating a nested generic
13250 else
13251 Opt.Style_Check := False;
13252 Expander_Mode_Save_And_Set (True);
13253 Load_Needed_Body (Comp_Unit, OK);
13254 Opt.Style_Check := Saved_Style_Check;
13255 Restore_Warnings (Saved_Warnings);
13256 Expander_Mode_Restore;
13258 if not OK
13259 and then Unit_Requires_Body (Defining_Entity (Spec))
13260 and then not Body_Optional
13261 then
13262 declare
13263 Bname : constant Unit_Name_Type :=
13264 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13266 begin
13267 -- In CodePeer mode, the missing body may make the analysis
13268 -- incomplete, but we do not treat it as fatal.
13270 if CodePeer_Mode then
13271 return;
13273 else
13274 Error_Msg_Unit_1 := Bname;
13275 Error_Msg_N ("this instantiation requires$!", N);
13276 Error_Msg_File_1 :=
13277 Get_File_Name (Bname, Subunit => False);
13278 Error_Msg_N ("\but file{ was not found!", N);
13279 raise Unrecoverable_Error;
13280 end if;
13281 end;
13282 end if;
13283 end if;
13284 end if;
13286 -- If loading parent of the generic caused an instantiation circularity,
13287 -- we abandon compilation at this point, because otherwise in some cases
13288 -- we get into trouble with infinite recursions after this point.
13290 if Circularity_Detected then
13291 raise Unrecoverable_Error;
13292 end if;
13293 end Load_Parent_Of_Generic;
13295 ---------------------------------
13296 -- Map_Formal_Package_Entities --
13297 ---------------------------------
13299 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13300 E1 : Entity_Id;
13301 E2 : Entity_Id;
13303 begin
13304 Set_Instance_Of (Form, Act);
13306 -- Traverse formal and actual package to map the corresponding entities.
13307 -- We skip over internal entities that may be generated during semantic
13308 -- analysis, and find the matching entities by name, given that they
13309 -- must appear in the same order.
13311 E1 := First_Entity (Form);
13312 E2 := First_Entity (Act);
13313 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13314 -- Could this test be a single condition??? Seems like it could, and
13315 -- isn't FPE (Form) a constant anyway???
13317 if not Is_Internal (E1)
13318 and then Present (Parent (E1))
13319 and then not Is_Class_Wide_Type (E1)
13320 and then not Is_Internal_Name (Chars (E1))
13321 then
13322 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13323 Next_Entity (E2);
13324 end loop;
13326 if No (E2) then
13327 exit;
13328 else
13329 Set_Instance_Of (E1, E2);
13331 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13332 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13333 end if;
13335 if Is_Constrained (E1) then
13336 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13337 end if;
13339 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13340 Map_Formal_Package_Entities (E1, E2);
13341 end if;
13342 end if;
13343 end if;
13345 Next_Entity (E1);
13346 end loop;
13347 end Map_Formal_Package_Entities;
13349 -----------------------
13350 -- Move_Freeze_Nodes --
13351 -----------------------
13353 procedure Move_Freeze_Nodes
13354 (Out_Of : Entity_Id;
13355 After : Node_Id;
13356 L : List_Id)
13358 Decl : Node_Id;
13359 Next_Decl : Node_Id;
13360 Next_Node : Node_Id := After;
13361 Spec : Node_Id;
13363 function Is_Outer_Type (T : Entity_Id) return Boolean;
13364 -- Check whether entity is declared in a scope external to that of the
13365 -- generic unit.
13367 -------------------
13368 -- Is_Outer_Type --
13369 -------------------
13371 function Is_Outer_Type (T : Entity_Id) return Boolean is
13372 Scop : Entity_Id := Scope (T);
13374 begin
13375 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13376 return True;
13378 else
13379 while Scop /= Standard_Standard loop
13380 if Scop = Out_Of then
13381 return False;
13382 else
13383 Scop := Scope (Scop);
13384 end if;
13385 end loop;
13387 return True;
13388 end if;
13389 end Is_Outer_Type;
13391 -- Start of processing for Move_Freeze_Nodes
13393 begin
13394 if No (L) then
13395 return;
13396 end if;
13398 -- First remove the freeze nodes that may appear before all other
13399 -- declarations.
13401 Decl := First (L);
13402 while Present (Decl)
13403 and then Nkind (Decl) = N_Freeze_Entity
13404 and then Is_Outer_Type (Entity (Decl))
13405 loop
13406 Decl := Remove_Head (L);
13407 Insert_After (Next_Node, Decl);
13408 Set_Analyzed (Decl, False);
13409 Next_Node := Decl;
13410 Decl := First (L);
13411 end loop;
13413 -- Next scan the list of declarations and remove each freeze node that
13414 -- appears ahead of the current node.
13416 while Present (Decl) loop
13417 while Present (Next (Decl))
13418 and then Nkind (Next (Decl)) = N_Freeze_Entity
13419 and then Is_Outer_Type (Entity (Next (Decl)))
13420 loop
13421 Next_Decl := Remove_Next (Decl);
13422 Insert_After (Next_Node, Next_Decl);
13423 Set_Analyzed (Next_Decl, False);
13424 Next_Node := Next_Decl;
13425 end loop;
13427 -- If the declaration is a nested package or concurrent type, then
13428 -- recurse. Nested generic packages will have been processed from the
13429 -- inside out.
13431 case Nkind (Decl) is
13432 when N_Package_Declaration =>
13433 Spec := Specification (Decl);
13435 when N_Task_Type_Declaration =>
13436 Spec := Task_Definition (Decl);
13438 when N_Protected_Type_Declaration =>
13439 Spec := Protected_Definition (Decl);
13441 when others =>
13442 Spec := Empty;
13443 end case;
13445 if Present (Spec) then
13446 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13447 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13448 end if;
13450 Next (Decl);
13451 end loop;
13452 end Move_Freeze_Nodes;
13454 ----------------
13455 -- Next_Assoc --
13456 ----------------
13458 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13459 begin
13460 return Generic_Renamings.Table (E).Next_In_HTable;
13461 end Next_Assoc;
13463 ------------------------
13464 -- Preanalyze_Actuals --
13465 ------------------------
13467 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13468 Assoc : Node_Id;
13469 Act : Node_Id;
13470 Errs : constant Nat := Serious_Errors_Detected;
13472 Cur : Entity_Id := Empty;
13473 -- Current homograph of the instance name
13475 Vis : Boolean;
13476 -- Saved visibility status of the current homograph
13478 begin
13479 Assoc := First (Generic_Associations (N));
13481 -- If the instance is a child unit, its name may hide an outer homonym,
13482 -- so make it invisible to perform name resolution on the actuals.
13484 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13485 and then Present
13486 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13487 then
13488 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13490 if Is_Compilation_Unit (Cur) then
13491 Vis := Is_Immediately_Visible (Cur);
13492 Set_Is_Immediately_Visible (Cur, False);
13493 else
13494 Cur := Empty;
13495 end if;
13496 end if;
13498 while Present (Assoc) loop
13499 if Nkind (Assoc) /= N_Others_Choice then
13500 Act := Explicit_Generic_Actual_Parameter (Assoc);
13502 -- Within a nested instantiation, a defaulted actual is an empty
13503 -- association, so nothing to analyze. If the subprogram actual
13504 -- is an attribute, analyze prefix only, because actual is not a
13505 -- complete attribute reference.
13507 -- If actual is an allocator, analyze expression only. The full
13508 -- analysis can generate code, and if instance is a compilation
13509 -- unit we have to wait until the package instance is installed
13510 -- to have a proper place to insert this code.
13512 -- String literals may be operators, but at this point we do not
13513 -- know whether the actual is a formal subprogram or a string.
13515 if No (Act) then
13516 null;
13518 elsif Nkind (Act) = N_Attribute_Reference then
13519 Analyze (Prefix (Act));
13521 elsif Nkind (Act) = N_Explicit_Dereference then
13522 Analyze (Prefix (Act));
13524 elsif Nkind (Act) = N_Allocator then
13525 declare
13526 Expr : constant Node_Id := Expression (Act);
13528 begin
13529 if Nkind (Expr) = N_Subtype_Indication then
13530 Analyze (Subtype_Mark (Expr));
13532 -- Analyze separately each discriminant constraint, when
13533 -- given with a named association.
13535 declare
13536 Constr : Node_Id;
13538 begin
13539 Constr := First (Constraints (Constraint (Expr)));
13540 while Present (Constr) loop
13541 if Nkind (Constr) = N_Discriminant_Association then
13542 Analyze (Expression (Constr));
13543 else
13544 Analyze (Constr);
13545 end if;
13547 Next (Constr);
13548 end loop;
13549 end;
13551 else
13552 Analyze (Expr);
13553 end if;
13554 end;
13556 elsif Nkind (Act) /= N_Operator_Symbol then
13557 Analyze (Act);
13559 -- Within a package instance, mark actuals that are limited
13560 -- views, so their use can be moved to the body of the
13561 -- enclosing unit.
13563 if Is_Entity_Name (Act)
13564 and then Is_Type (Entity (Act))
13565 and then From_Limited_With (Entity (Act))
13566 and then Present (Inst)
13567 then
13568 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13569 end if;
13570 end if;
13572 if Errs /= Serious_Errors_Detected then
13574 -- Do a minimal analysis of the generic, to prevent spurious
13575 -- warnings complaining about the generic being unreferenced,
13576 -- before abandoning the instantiation.
13578 Analyze (Name (N));
13580 if Is_Entity_Name (Name (N))
13581 and then Etype (Name (N)) /= Any_Type
13582 then
13583 Generate_Reference (Entity (Name (N)), Name (N));
13584 Set_Is_Instantiated (Entity (Name (N)));
13585 end if;
13587 if Present (Cur) then
13589 -- For the case of a child instance hiding an outer homonym,
13590 -- provide additional warning which might explain the error.
13592 Set_Is_Immediately_Visible (Cur, Vis);
13593 Error_Msg_NE
13594 ("& hides outer unit with the same name??",
13595 N, Defining_Unit_Name (N));
13596 end if;
13598 Abandon_Instantiation (Act);
13599 end if;
13600 end if;
13602 Next (Assoc);
13603 end loop;
13605 if Present (Cur) then
13606 Set_Is_Immediately_Visible (Cur, Vis);
13607 end if;
13608 end Preanalyze_Actuals;
13610 -------------------
13611 -- Remove_Parent --
13612 -------------------
13614 procedure Remove_Parent (In_Body : Boolean := False) is
13615 S : Entity_Id := Current_Scope;
13616 -- S is the scope containing the instantiation just completed. The scope
13617 -- stack contains the parent instances of the instantiation, followed by
13618 -- the original S.
13620 Cur_P : Entity_Id;
13621 E : Entity_Id;
13622 P : Entity_Id;
13623 Hidden : Elmt_Id;
13625 begin
13626 -- After child instantiation is complete, remove from scope stack the
13627 -- extra copy of the current scope, and then remove parent instances.
13629 if not In_Body then
13630 Pop_Scope;
13632 while Current_Scope /= S loop
13633 P := Current_Scope;
13634 End_Package_Scope (Current_Scope);
13636 if In_Open_Scopes (P) then
13637 E := First_Entity (P);
13638 while Present (E) loop
13639 Set_Is_Immediately_Visible (E, True);
13640 Next_Entity (E);
13641 end loop;
13643 -- If instantiation is declared in a block, it is the enclosing
13644 -- scope that might be a parent instance. Note that only one
13645 -- block can be involved, because the parent instances have
13646 -- been installed within it.
13648 if Ekind (P) = E_Block then
13649 Cur_P := Scope (P);
13650 else
13651 Cur_P := P;
13652 end if;
13654 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13655 -- We are within an instance of some sibling. Retain
13656 -- visibility of parent, for proper subsequent cleanup, and
13657 -- reinstall private declarations as well.
13659 Set_In_Private_Part (P);
13660 Install_Private_Declarations (P);
13661 end if;
13663 -- If the ultimate parent is a top-level unit recorded in
13664 -- Instance_Parent_Unit, then reset its visibility to what it was
13665 -- before instantiation. (It's not clear what the purpose is of
13666 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13667 -- present before the ultimate parent test was added.???)
13669 elsif not In_Open_Scopes (Scope (P))
13670 or else (P = Instance_Parent_Unit
13671 and then not Parent_Unit_Visible)
13672 then
13673 Set_Is_Immediately_Visible (P, False);
13675 -- If the current scope is itself an instantiation of a generic
13676 -- nested within P, and we are in the private part of body of this
13677 -- instantiation, restore the full views of P, that were removed
13678 -- in End_Package_Scope above. This obscure case can occur when a
13679 -- subunit of a generic contains an instance of a child unit of
13680 -- its generic parent unit.
13682 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13683 declare
13684 Par : constant Entity_Id :=
13685 Generic_Parent (Package_Specification (S));
13686 begin
13687 if Present (Par)
13688 and then P = Scope (Par)
13689 and then (In_Package_Body (S) or else In_Private_Part (S))
13690 then
13691 Set_In_Private_Part (P);
13692 Install_Private_Declarations (P);
13693 end if;
13694 end;
13695 end if;
13696 end loop;
13698 -- Reset visibility of entities in the enclosing scope
13700 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13702 Hidden := First_Elmt (Hidden_Entities);
13703 while Present (Hidden) loop
13704 Set_Is_Immediately_Visible (Node (Hidden), True);
13705 Next_Elmt (Hidden);
13706 end loop;
13708 else
13709 -- Each body is analyzed separately, and there is no context that
13710 -- needs preserving from one body instance to the next, so remove all
13711 -- parent scopes that have been installed.
13713 while Present (S) loop
13714 End_Package_Scope (S);
13715 Set_Is_Immediately_Visible (S, False);
13716 S := Current_Scope;
13717 exit when S = Standard_Standard;
13718 end loop;
13719 end if;
13720 end Remove_Parent;
13722 -----------------
13723 -- Restore_Env --
13724 -----------------
13726 procedure Restore_Env is
13727 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13729 begin
13730 if No (Current_Instantiated_Parent.Act_Id) then
13731 -- Restore environment after subprogram inlining
13733 Restore_Private_Views (Empty);
13734 end if;
13736 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13737 Exchanged_Views := Saved.Exchanged_Views;
13738 Hidden_Entities := Saved.Hidden_Entities;
13739 Current_Sem_Unit := Saved.Current_Sem_Unit;
13740 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13741 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13743 Restore_Opt_Config_Switches (Saved.Switches);
13745 Instance_Envs.Decrement_Last;
13746 end Restore_Env;
13748 ---------------------------
13749 -- Restore_Private_Views --
13750 ---------------------------
13752 procedure Restore_Private_Views
13753 (Pack_Id : Entity_Id;
13754 Is_Package : Boolean := True)
13756 M : Elmt_Id;
13757 E : Entity_Id;
13758 Typ : Entity_Id;
13759 Dep_Elmt : Elmt_Id;
13760 Dep_Typ : Node_Id;
13762 procedure Restore_Nested_Formal (Formal : Entity_Id);
13763 -- Hide the generic formals of formal packages declared with box which
13764 -- were reachable in the current instantiation.
13766 ---------------------------
13767 -- Restore_Nested_Formal --
13768 ---------------------------
13770 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13771 Ent : Entity_Id;
13773 begin
13774 if Present (Renamed_Object (Formal))
13775 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13776 then
13777 return;
13779 elsif Present (Associated_Formal_Package (Formal)) then
13780 Ent := First_Entity (Formal);
13781 while Present (Ent) loop
13782 exit when Ekind (Ent) = E_Package
13783 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13785 Set_Is_Hidden (Ent);
13786 Set_Is_Potentially_Use_Visible (Ent, False);
13788 -- If package, then recurse
13790 if Ekind (Ent) = E_Package then
13791 Restore_Nested_Formal (Ent);
13792 end if;
13794 Next_Entity (Ent);
13795 end loop;
13796 end if;
13797 end Restore_Nested_Formal;
13799 -- Start of processing for Restore_Private_Views
13801 begin
13802 M := First_Elmt (Exchanged_Views);
13803 while Present (M) loop
13804 Typ := Node (M);
13806 -- Subtypes of types whose views have been exchanged, and that are
13807 -- defined within the instance, were not on the Private_Dependents
13808 -- list on entry to the instance, so they have to be exchanged
13809 -- explicitly now, in order to remain consistent with the view of the
13810 -- parent type.
13812 if Ekind_In (Typ, E_Private_Type,
13813 E_Limited_Private_Type,
13814 E_Record_Type_With_Private)
13815 then
13816 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13817 while Present (Dep_Elmt) loop
13818 Dep_Typ := Node (Dep_Elmt);
13820 if Scope (Dep_Typ) = Pack_Id
13821 and then Present (Full_View (Dep_Typ))
13822 then
13823 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13824 Exchange_Declarations (Dep_Typ);
13825 end if;
13827 Next_Elmt (Dep_Elmt);
13828 end loop;
13829 end if;
13831 Exchange_Declarations (Node (M));
13832 Next_Elmt (M);
13833 end loop;
13835 if No (Pack_Id) then
13836 return;
13837 end if;
13839 -- Make the generic formal parameters private, and make the formal types
13840 -- into subtypes of the actuals again.
13842 E := First_Entity (Pack_Id);
13843 while Present (E) loop
13844 Set_Is_Hidden (E, True);
13846 if Is_Type (E)
13847 and then Nkind (Parent (E)) = N_Subtype_Declaration
13848 then
13849 -- If the actual for E is itself a generic actual type from
13850 -- an enclosing instance, E is still a generic actual type
13851 -- outside of the current instance. This matter when resolving
13852 -- an overloaded call that may be ambiguous in the enclosing
13853 -- instance, when two of its actuals coincide.
13855 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13856 and then Is_Generic_Actual_Type
13857 (Entity (Subtype_Indication (Parent (E))))
13858 then
13859 null;
13860 else
13861 Set_Is_Generic_Actual_Type (E, False);
13862 end if;
13864 -- An unusual case of aliasing: the actual may also be directly
13865 -- visible in the generic, and be private there, while it is fully
13866 -- visible in the context of the instance. The internal subtype
13867 -- is private in the instance but has full visibility like its
13868 -- parent in the enclosing scope. This enforces the invariant that
13869 -- the privacy status of all private dependents of a type coincide
13870 -- with that of the parent type. This can only happen when a
13871 -- generic child unit is instantiated within a sibling.
13873 if Is_Private_Type (E)
13874 and then not Is_Private_Type (Etype (E))
13875 then
13876 Exchange_Declarations (E);
13877 end if;
13879 elsif Ekind (E) = E_Package then
13881 -- The end of the renaming list is the renaming of the generic
13882 -- package itself. If the instance is a subprogram, all entities
13883 -- in the corresponding package are renamings. If this entity is
13884 -- a formal package, make its own formals private as well. The
13885 -- actual in this case is itself the renaming of an instantiation.
13886 -- If the entity is not a package renaming, it is the entity
13887 -- created to validate formal package actuals: ignore it.
13889 -- If the actual is itself a formal package for the enclosing
13890 -- generic, or the actual for such a formal package, it remains
13891 -- visible on exit from the instance, and therefore nothing needs
13892 -- to be done either, except to keep it accessible.
13894 if Is_Package and then Renamed_Object (E) = Pack_Id then
13895 exit;
13897 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13898 null;
13900 elsif
13901 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13902 then
13903 Set_Is_Hidden (E, False);
13905 else
13906 declare
13907 Act_P : constant Entity_Id := Renamed_Object (E);
13908 Id : Entity_Id;
13910 begin
13911 Id := First_Entity (Act_P);
13912 while Present (Id)
13913 and then Id /= First_Private_Entity (Act_P)
13914 loop
13915 exit when Ekind (Id) = E_Package
13916 and then Renamed_Object (Id) = Act_P;
13918 Set_Is_Hidden (Id, True);
13919 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13921 if Ekind (Id) = E_Package then
13922 Restore_Nested_Formal (Id);
13923 end if;
13925 Next_Entity (Id);
13926 end loop;
13927 end;
13928 end if;
13929 end if;
13931 Next_Entity (E);
13932 end loop;
13933 end Restore_Private_Views;
13935 --------------
13936 -- Save_Env --
13937 --------------
13939 procedure Save_Env
13940 (Gen_Unit : Entity_Id;
13941 Act_Unit : Entity_Id)
13943 begin
13944 Init_Env;
13945 Set_Instance_Env (Gen_Unit, Act_Unit);
13946 end Save_Env;
13948 ----------------------------
13949 -- Save_Global_References --
13950 ----------------------------
13952 procedure Save_Global_References (Templ : Node_Id) is
13954 -- ??? it is horrible to use global variables in highly recursive code
13956 E : Entity_Id;
13957 -- The entity of the current associated node
13959 Gen_Scope : Entity_Id;
13960 -- The scope of the generic for which references are being saved
13962 N2 : Node_Id;
13963 -- The current associated node
13965 function Is_Global (E : Entity_Id) return Boolean;
13966 -- Check whether entity is defined outside of generic unit. Examine the
13967 -- scope of an entity, and the scope of the scope, etc, until we find
13968 -- either Standard, in which case the entity is global, or the generic
13969 -- unit itself, which indicates that the entity is local. If the entity
13970 -- is the generic unit itself, as in the case of a recursive call, or
13971 -- the enclosing generic unit, if different from the current scope, then
13972 -- it is local as well, because it will be replaced at the point of
13973 -- instantiation. On the other hand, if it is a reference to a child
13974 -- unit of a common ancestor, which appears in an instantiation, it is
13975 -- global because it is used to denote a specific compilation unit at
13976 -- the time the instantiations will be analyzed.
13978 procedure Qualify_Universal_Operands
13979 (Op : Node_Id;
13980 Func_Call : Node_Id);
13981 -- Op denotes a binary or unary operator in generic template Templ. Node
13982 -- Func_Call is the function call alternative of the operator within the
13983 -- the analyzed copy of the template. Change each operand which yields a
13984 -- universal type by wrapping it into a qualified expression
13986 -- Actual_Typ'(Operand)
13988 -- where Actual_Typ is the type of corresponding actual parameter of
13989 -- Operand in Func_Call.
13991 procedure Reset_Entity (N : Node_Id);
13992 -- Save semantic information on global entity so that it is not resolved
13993 -- again at instantiation time.
13995 procedure Save_Entity_Descendants (N : Node_Id);
13996 -- Apply Save_Global_References to the two syntactic descendants of
13997 -- non-terminal nodes that carry an Associated_Node and are processed
13998 -- through Reset_Entity. Once the global entity (if any) has been
13999 -- captured together with its type, only two syntactic descendants need
14000 -- to be traversed to complete the processing of the tree rooted at N.
14001 -- This applies to Selected_Components, Expanded_Names, and to Operator
14002 -- nodes. N can also be a character literal, identifier, or operator
14003 -- symbol node, but the call has no effect in these cases.
14005 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14006 -- Default actuals in nested instances must be handled specially
14007 -- because there is no link to them from the original tree. When an
14008 -- actual subprogram is given by a default, we add an explicit generic
14009 -- association for it in the instantiation node. When we save the
14010 -- global references on the name of the instance, we recover the list
14011 -- of generic associations, and add an explicit one to the original
14012 -- generic tree, through which a global actual can be preserved.
14013 -- Similarly, if a child unit is instantiated within a sibling, in the
14014 -- context of the parent, we must preserve the identifier of the parent
14015 -- so that it can be properly resolved in a subsequent instantiation.
14017 procedure Save_Global_Descendant (D : Union_Id);
14018 -- Apply Save_References recursively to the descendants of node D
14020 procedure Save_References (N : Node_Id);
14021 -- This is the recursive procedure that does the work, once the
14022 -- enclosing generic scope has been established.
14024 ---------------
14025 -- Is_Global --
14026 ---------------
14028 function Is_Global (E : Entity_Id) return Boolean is
14029 Se : Entity_Id;
14031 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14032 -- Determine whether the parent node of a reference to a child unit
14033 -- denotes an instantiation or a formal package, in which case the
14034 -- reference to the child unit is global, even if it appears within
14035 -- the current scope (e.g. when the instance appears within the body
14036 -- of an ancestor).
14038 ----------------------
14039 -- Is_Instance_Node --
14040 ----------------------
14042 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14043 begin
14044 return Nkind (Decl) in N_Generic_Instantiation
14045 or else
14046 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14047 end Is_Instance_Node;
14049 -- Start of processing for Is_Global
14051 begin
14052 if E = Gen_Scope then
14053 return False;
14055 elsif E = Standard_Standard then
14056 return True;
14058 elsif Is_Child_Unit (E)
14059 and then (Is_Instance_Node (Parent (N2))
14060 or else (Nkind (Parent (N2)) = N_Expanded_Name
14061 and then N2 = Selector_Name (Parent (N2))
14062 and then
14063 Is_Instance_Node (Parent (Parent (N2)))))
14064 then
14065 return True;
14067 else
14068 Se := Scope (E);
14069 while Se /= Gen_Scope loop
14070 if Se = Standard_Standard then
14071 return True;
14072 else
14073 Se := Scope (Se);
14074 end if;
14075 end loop;
14077 return False;
14078 end if;
14079 end Is_Global;
14081 --------------------------------
14082 -- Qualify_Universal_Operands --
14083 --------------------------------
14085 procedure Qualify_Universal_Operands
14086 (Op : Node_Id;
14087 Func_Call : Node_Id)
14089 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14090 -- Rewrite operand Opnd as a qualified expression of the form
14092 -- Actual_Typ'(Opnd)
14094 -- where Actual is the corresponding actual parameter of Opnd in
14095 -- function call Func_Call.
14097 function Qualify_Type
14098 (Loc : Source_Ptr;
14099 Typ : Entity_Id) return Node_Id;
14100 -- Qualify type Typ by creating a selected component of the form
14102 -- Scope_Of_Typ.Typ
14104 ---------------------
14105 -- Qualify_Operand --
14106 ---------------------
14108 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14109 Loc : constant Source_Ptr := Sloc (Opnd);
14110 Typ : constant Entity_Id := Etype (Actual);
14111 Mark : Node_Id;
14112 Qual : Node_Id;
14114 begin
14115 -- Qualify the operand when it is of a universal type. Note that
14116 -- the template is unanalyzed and it is not possible to directly
14117 -- query the type. This transformation is not done when the type
14118 -- of the actual is internally generated because the type will be
14119 -- regenerated in the instance.
14121 if Yields_Universal_Type (Opnd)
14122 and then Comes_From_Source (Typ)
14123 and then not Is_Hidden (Typ)
14124 then
14125 -- The type of the actual may be a global reference. Save this
14126 -- information by creating a reference to it.
14128 if Is_Global (Typ) then
14129 Mark := New_Occurrence_Of (Typ, Loc);
14131 -- Otherwise rely on resolution to find the proper type within
14132 -- the instance.
14134 else
14135 Mark := Qualify_Type (Loc, Typ);
14136 end if;
14138 Qual :=
14139 Make_Qualified_Expression (Loc,
14140 Subtype_Mark => Mark,
14141 Expression => Relocate_Node (Opnd));
14143 -- Mark the qualification to distinguish it from other source
14144 -- constructs and signal the instantiation mechanism that this
14145 -- node requires special processing. See Copy_Generic_Node for
14146 -- details.
14148 Set_Is_Qualified_Universal_Literal (Qual);
14150 Rewrite (Opnd, Qual);
14151 end if;
14152 end Qualify_Operand;
14154 ------------------
14155 -- Qualify_Type --
14156 ------------------
14158 function Qualify_Type
14159 (Loc : Source_Ptr;
14160 Typ : Entity_Id) return Node_Id
14162 Scop : constant Entity_Id := Scope (Typ);
14163 Result : Node_Id;
14165 begin
14166 Result := Make_Identifier (Loc, Chars (Typ));
14168 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14169 Result :=
14170 Make_Selected_Component (Loc,
14171 Prefix => Make_Identifier (Loc, Chars (Scop)),
14172 Selector_Name => Result);
14173 end if;
14175 return Result;
14176 end Qualify_Type;
14178 -- Local variables
14180 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14182 -- Start of processing for Qualify_Universal_Operands
14184 begin
14185 if Nkind (Op) in N_Binary_Op then
14186 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14187 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14189 elsif Nkind (Op) in N_Unary_Op then
14190 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14191 end if;
14192 end Qualify_Universal_Operands;
14194 ------------------
14195 -- Reset_Entity --
14196 ------------------
14198 procedure Reset_Entity (N : Node_Id) is
14199 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14200 -- If the type of N2 is global to the generic unit, save the type in
14201 -- the generic node. Just as we perform name capture for explicit
14202 -- references within the generic, we must capture the global types
14203 -- of local entities because they may participate in resolution in
14204 -- the instance.
14206 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14207 -- Find the ultimate ancestor of the current unit. If it is not a
14208 -- generic unit, then the name of the current unit in the prefix of
14209 -- an expanded name must be replaced with its generic homonym to
14210 -- ensure that it will be properly resolved in an instance.
14212 ---------------------
14213 -- Set_Global_Type --
14214 ---------------------
14216 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14217 Typ : constant Entity_Id := Etype (N2);
14219 begin
14220 Set_Etype (N, Typ);
14222 -- If the entity of N is not the associated node, this is a
14223 -- nested generic and it has an associated node as well, whose
14224 -- type is already the full view (see below). Indicate that the
14225 -- original node has a private view.
14227 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14228 Set_Has_Private_View (N);
14229 end if;
14231 -- If not a private type, nothing else to do
14233 if not Is_Private_Type (Typ) then
14234 if Is_Array_Type (Typ)
14235 and then Is_Private_Type (Component_Type (Typ))
14236 then
14237 Set_Has_Private_View (N);
14238 end if;
14240 -- If it is a derivation of a private type in a context where no
14241 -- full view is needed, nothing to do either.
14243 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14244 null;
14246 -- Otherwise mark the type for flipping and use the full view when
14247 -- available.
14249 else
14250 Set_Has_Private_View (N);
14252 if Present (Full_View (Typ)) then
14253 Set_Etype (N2, Full_View (Typ));
14254 end if;
14255 end if;
14257 if Is_Floating_Point_Type (Typ)
14258 and then Has_Dimension_System (Typ)
14259 then
14260 Copy_Dimensions (N2, N);
14261 end if;
14262 end Set_Global_Type;
14264 ------------------
14265 -- Top_Ancestor --
14266 ------------------
14268 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14269 Par : Entity_Id;
14271 begin
14272 Par := E;
14273 while Is_Child_Unit (Par) loop
14274 Par := Scope (Par);
14275 end loop;
14277 return Par;
14278 end Top_Ancestor;
14280 -- Start of processing for Reset_Entity
14282 begin
14283 N2 := Get_Associated_Node (N);
14284 E := Entity (N2);
14286 if Present (E) then
14288 -- If the node is an entry call to an entry in an enclosing task,
14289 -- it is rewritten as a selected component. No global entity to
14290 -- preserve in this case, since the expansion will be redone in
14291 -- the instance.
14293 if not Nkind_In (E, N_Defining_Character_Literal,
14294 N_Defining_Identifier,
14295 N_Defining_Operator_Symbol)
14296 then
14297 Set_Associated_Node (N, Empty);
14298 Set_Etype (N, Empty);
14299 return;
14300 end if;
14302 -- If the entity is an itype created as a subtype of an access
14303 -- type with a null exclusion restore source entity for proper
14304 -- visibility. The itype will be created anew in the instance.
14306 if Is_Itype (E)
14307 and then Ekind (E) = E_Access_Subtype
14308 and then Is_Entity_Name (N)
14309 and then Chars (Etype (E)) = Chars (N)
14310 then
14311 E := Etype (E);
14312 Set_Entity (N2, E);
14313 Set_Etype (N2, E);
14314 end if;
14316 if Is_Global (E) then
14318 -- If the entity is a package renaming that is the prefix of
14319 -- an expanded name, it has been rewritten as the renamed
14320 -- package, which is necessary semantically but complicates
14321 -- ASIS tree traversal, so we recover the original entity to
14322 -- expose the renaming. Take into account that the context may
14323 -- be a nested generic, that the original node may itself have
14324 -- an associated node that had better be an entity, and that
14325 -- the current node is still a selected component.
14327 if Ekind (E) = E_Package
14328 and then Nkind (N) = N_Selected_Component
14329 and then Nkind (Parent (N)) = N_Expanded_Name
14330 and then Present (Original_Node (N2))
14331 and then Is_Entity_Name (Original_Node (N2))
14332 and then Present (Entity (Original_Node (N2)))
14333 then
14334 if Is_Global (Entity (Original_Node (N2))) then
14335 N2 := Original_Node (N2);
14336 Set_Associated_Node (N, N2);
14337 Set_Global_Type (N, N2);
14339 -- Renaming is local, and will be resolved in instance
14341 else
14342 Set_Associated_Node (N, Empty);
14343 Set_Etype (N, Empty);
14344 end if;
14346 else
14347 Set_Global_Type (N, N2);
14348 end if;
14350 elsif Nkind (N) = N_Op_Concat
14351 and then Is_Generic_Type (Etype (N2))
14352 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14353 or else
14354 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14355 and then Is_Intrinsic_Subprogram (E)
14356 then
14357 null;
14359 -- Entity is local. Mark generic node as unresolved. Note that now
14360 -- it does not have an entity.
14362 else
14363 Set_Associated_Node (N, Empty);
14364 Set_Etype (N, Empty);
14365 end if;
14367 if Nkind (Parent (N)) in N_Generic_Instantiation
14368 and then N = Name (Parent (N))
14369 then
14370 Save_Global_Defaults (Parent (N), Parent (N2));
14371 end if;
14373 elsif Nkind (Parent (N)) = N_Selected_Component
14374 and then Nkind (Parent (N2)) = N_Expanded_Name
14375 then
14376 if Is_Global (Entity (Parent (N2))) then
14377 Change_Selected_Component_To_Expanded_Name (Parent (N));
14378 Set_Associated_Node (Parent (N), Parent (N2));
14379 Set_Global_Type (Parent (N), Parent (N2));
14380 Save_Entity_Descendants (N);
14382 -- If this is a reference to the current generic entity, replace
14383 -- by the name of the generic homonym of the current package. This
14384 -- is because in an instantiation Par.P.Q will not resolve to the
14385 -- name of the instance, whose enclosing scope is not necessarily
14386 -- Par. We use the generic homonym rather that the name of the
14387 -- generic itself because it may be hidden by a local declaration.
14389 elsif In_Open_Scopes (Entity (Parent (N2)))
14390 and then not
14391 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14392 then
14393 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14394 Rewrite (Parent (N),
14395 Make_Identifier (Sloc (N),
14396 Chars =>
14397 Chars (Generic_Homonym (Entity (Parent (N2))))));
14398 else
14399 Rewrite (Parent (N),
14400 Make_Identifier (Sloc (N),
14401 Chars => Chars (Selector_Name (Parent (N2)))));
14402 end if;
14403 end if;
14405 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14406 and then Parent (N) = Name (Parent (Parent (N)))
14407 then
14408 Save_Global_Defaults
14409 (Parent (Parent (N)), Parent (Parent (N2)));
14410 end if;
14412 -- A selected component may denote a static constant that has been
14413 -- folded. If the static constant is global to the generic, capture
14414 -- its value. Otherwise the folding will happen in any instantiation.
14416 elsif Nkind (Parent (N)) = N_Selected_Component
14417 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14418 then
14419 if Present (Entity (Original_Node (Parent (N2))))
14420 and then Is_Global (Entity (Original_Node (Parent (N2))))
14421 then
14422 Rewrite (Parent (N), New_Copy (Parent (N2)));
14423 Set_Analyzed (Parent (N), False);
14424 end if;
14426 -- A selected component may be transformed into a parameterless
14427 -- function call. If the called entity is global, rewrite the node
14428 -- appropriately, i.e. as an extended name for the global entity.
14430 elsif Nkind (Parent (N)) = N_Selected_Component
14431 and then Nkind (Parent (N2)) = N_Function_Call
14432 and then N = Selector_Name (Parent (N))
14433 then
14434 if No (Parameter_Associations (Parent (N2))) then
14435 if Is_Global (Entity (Name (Parent (N2)))) then
14436 Change_Selected_Component_To_Expanded_Name (Parent (N));
14437 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14438 Set_Global_Type (Parent (N), Name (Parent (N2)));
14439 Save_Entity_Descendants (N);
14441 else
14442 Set_Is_Prefixed_Call (Parent (N));
14443 Set_Associated_Node (N, Empty);
14444 Set_Etype (N, Empty);
14445 end if;
14447 -- In Ada 2005, X.F may be a call to a primitive operation,
14448 -- rewritten as F (X). This rewriting will be done again in an
14449 -- instance, so keep the original node. Global entities will be
14450 -- captured as for other constructs. Indicate that this must
14451 -- resolve as a call, to prevent accidental overloading in the
14452 -- instance, if both a component and a primitive operation appear
14453 -- as candidates.
14455 else
14456 Set_Is_Prefixed_Call (Parent (N));
14457 end if;
14459 -- Entity is local. Reset in generic unit, so that node is resolved
14460 -- anew at the point of instantiation.
14462 else
14463 Set_Associated_Node (N, Empty);
14464 Set_Etype (N, Empty);
14465 end if;
14466 end Reset_Entity;
14468 -----------------------------
14469 -- Save_Entity_Descendants --
14470 -----------------------------
14472 procedure Save_Entity_Descendants (N : Node_Id) is
14473 begin
14474 case Nkind (N) is
14475 when N_Binary_Op =>
14476 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14477 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14479 when N_Unary_Op =>
14480 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14482 when N_Expanded_Name |
14483 N_Selected_Component =>
14484 Save_Global_Descendant (Union_Id (Prefix (N)));
14485 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14487 when N_Identifier |
14488 N_Character_Literal |
14489 N_Operator_Symbol =>
14490 null;
14492 when others =>
14493 raise Program_Error;
14494 end case;
14495 end Save_Entity_Descendants;
14497 --------------------------
14498 -- Save_Global_Defaults --
14499 --------------------------
14501 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14502 Loc : constant Source_Ptr := Sloc (N1);
14503 Assoc2 : constant List_Id := Generic_Associations (N2);
14504 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14505 Assoc1 : List_Id;
14506 Act1 : Node_Id;
14507 Act2 : Node_Id;
14508 Def : Node_Id;
14509 Ndec : Node_Id;
14510 Subp : Entity_Id;
14511 Actual : Entity_Id;
14513 begin
14514 Assoc1 := Generic_Associations (N1);
14516 if Present (Assoc1) then
14517 Act1 := First (Assoc1);
14518 else
14519 Act1 := Empty;
14520 Set_Generic_Associations (N1, New_List);
14521 Assoc1 := Generic_Associations (N1);
14522 end if;
14524 if Present (Assoc2) then
14525 Act2 := First (Assoc2);
14526 else
14527 return;
14528 end if;
14530 while Present (Act1) and then Present (Act2) loop
14531 Next (Act1);
14532 Next (Act2);
14533 end loop;
14535 -- Find the associations added for default subprograms
14537 if Present (Act2) then
14538 while Nkind (Act2) /= N_Generic_Association
14539 or else No (Entity (Selector_Name (Act2)))
14540 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14541 loop
14542 Next (Act2);
14543 end loop;
14545 -- Add a similar association if the default is global. The
14546 -- renaming declaration for the actual has been analyzed, and
14547 -- its alias is the program it renames. Link the actual in the
14548 -- original generic tree with the node in the analyzed tree.
14550 while Present (Act2) loop
14551 Subp := Entity (Selector_Name (Act2));
14552 Def := Explicit_Generic_Actual_Parameter (Act2);
14554 -- Following test is defence against rubbish errors
14556 if No (Alias (Subp)) then
14557 return;
14558 end if;
14560 -- Retrieve the resolved actual from the renaming declaration
14561 -- created for the instantiated formal.
14563 Actual := Entity (Name (Parent (Parent (Subp))));
14564 Set_Entity (Def, Actual);
14565 Set_Etype (Def, Etype (Actual));
14567 if Is_Global (Actual) then
14568 Ndec :=
14569 Make_Generic_Association (Loc,
14570 Selector_Name =>
14571 New_Occurrence_Of (Subp, Loc),
14572 Explicit_Generic_Actual_Parameter =>
14573 New_Occurrence_Of (Actual, Loc));
14575 Set_Associated_Node
14576 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14578 Append (Ndec, Assoc1);
14580 -- If there are other defaults, add a dummy association in case
14581 -- there are other defaulted formals with the same name.
14583 elsif Present (Next (Act2)) then
14584 Ndec :=
14585 Make_Generic_Association (Loc,
14586 Selector_Name =>
14587 New_Occurrence_Of (Subp, Loc),
14588 Explicit_Generic_Actual_Parameter => Empty);
14590 Append (Ndec, Assoc1);
14591 end if;
14593 Next (Act2);
14594 end loop;
14595 end if;
14597 if Nkind (Name (N1)) = N_Identifier
14598 and then Is_Child_Unit (Gen_Id)
14599 and then Is_Global (Gen_Id)
14600 and then Is_Generic_Unit (Scope (Gen_Id))
14601 and then In_Open_Scopes (Scope (Gen_Id))
14602 then
14603 -- This is an instantiation of a child unit within a sibling, so
14604 -- that the generic parent is in scope. An eventual instance must
14605 -- occur within the scope of an instance of the parent. Make name
14606 -- in instance into an expanded name, to preserve the identifier
14607 -- of the parent, so it can be resolved subsequently.
14609 Rewrite (Name (N2),
14610 Make_Expanded_Name (Loc,
14611 Chars => Chars (Gen_Id),
14612 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14613 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14614 Set_Entity (Name (N2), Gen_Id);
14616 Rewrite (Name (N1),
14617 Make_Expanded_Name (Loc,
14618 Chars => Chars (Gen_Id),
14619 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14620 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14622 Set_Associated_Node (Name (N1), Name (N2));
14623 Set_Associated_Node (Prefix (Name (N1)), Empty);
14624 Set_Associated_Node
14625 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14626 Set_Etype (Name (N1), Etype (Gen_Id));
14627 end if;
14628 end Save_Global_Defaults;
14630 ----------------------------
14631 -- Save_Global_Descendant --
14632 ----------------------------
14634 procedure Save_Global_Descendant (D : Union_Id) is
14635 N1 : Node_Id;
14637 begin
14638 if D in Node_Range then
14639 if D = Union_Id (Empty) then
14640 null;
14642 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14643 Save_References (Node_Id (D));
14644 end if;
14646 elsif D in List_Range then
14647 pragma Assert (D /= Union_Id (No_List));
14648 -- Because No_List = Empty, which is in Node_Range above
14650 if Is_Empty_List (List_Id (D)) then
14651 null;
14653 else
14654 N1 := First (List_Id (D));
14655 while Present (N1) loop
14656 Save_References (N1);
14657 Next (N1);
14658 end loop;
14659 end if;
14661 -- Element list or other non-node field, nothing to do
14663 else
14664 null;
14665 end if;
14666 end Save_Global_Descendant;
14668 ---------------------
14669 -- Save_References --
14670 ---------------------
14672 -- This is the recursive procedure that does the work once the enclosing
14673 -- generic scope has been established. We have to treat specially a
14674 -- number of node rewritings that are required by semantic processing
14675 -- and which change the kind of nodes in the generic copy: typically
14676 -- constant-folding, replacing an operator node by a string literal, or
14677 -- a selected component by an expanded name. In each of those cases, the
14678 -- transformation is propagated to the generic unit.
14680 procedure Save_References (N : Node_Id) is
14681 Loc : constant Source_Ptr := Sloc (N);
14683 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14684 -- Determine whether arbitrary node Nod requires delayed capture of
14685 -- global references within its aspect specifications.
14687 procedure Save_References_In_Aggregate (N : Node_Id);
14688 -- Save all global references in [extension] aggregate node N
14690 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14691 -- Save all global references in a character literal or operator
14692 -- symbol denoted by N.
14694 procedure Save_References_In_Descendants (N : Node_Id);
14695 -- Save all global references in all descendants of node N
14697 procedure Save_References_In_Identifier (N : Node_Id);
14698 -- Save all global references in identifier node N
14700 procedure Save_References_In_Operator (N : Node_Id);
14701 -- Save all global references in operator node N
14703 procedure Save_References_In_Pragma (Prag : Node_Id);
14704 -- Save all global references found within the expression of pragma
14705 -- Prag.
14707 ---------------------------
14708 -- Requires_Delayed_Save --
14709 ---------------------------
14711 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14712 begin
14713 -- Generic packages and subprograms require delayed capture of
14714 -- global references within their aspects due to the timing of
14715 -- annotation analysis.
14717 if Nkind_In (Nod, N_Generic_Package_Declaration,
14718 N_Generic_Subprogram_Declaration,
14719 N_Package_Body,
14720 N_Package_Body_Stub,
14721 N_Subprogram_Body,
14722 N_Subprogram_Body_Stub)
14723 then
14724 -- Since the capture of global references is done on the
14725 -- unanalyzed generic template, there is no information around
14726 -- to infer the context. Use the Associated_Entity linkages to
14727 -- peek into the analyzed generic copy and determine what the
14728 -- template corresponds to.
14730 if Nod = Templ then
14731 return
14732 Is_Generic_Declaration_Or_Body
14733 (Unit_Declaration_Node
14734 (Associated_Entity (Defining_Entity (Nod))));
14736 -- Otherwise the generic unit being processed is not the top
14737 -- level template. It is safe to capture of global references
14738 -- within the generic unit because at this point the top level
14739 -- copy is fully analyzed.
14741 else
14742 return False;
14743 end if;
14745 -- Otherwise capture the global references without interference
14747 else
14748 return False;
14749 end if;
14750 end Requires_Delayed_Save;
14752 ----------------------------------
14753 -- Save_References_In_Aggregate --
14754 ----------------------------------
14756 procedure Save_References_In_Aggregate (N : Node_Id) is
14757 Nam : Node_Id;
14758 Qual : Node_Id := Empty;
14759 Typ : Entity_Id := Empty;
14761 use Atree.Unchecked_Access;
14762 -- This code section is part of implementing an untyped tree
14763 -- traversal, so it needs direct access to node fields.
14765 begin
14766 N2 := Get_Associated_Node (N);
14768 if Present (N2) then
14769 Typ := Etype (N2);
14771 -- In an instance within a generic, use the name of the actual
14772 -- and not the original generic parameter. If the actual is
14773 -- global in the current generic it must be preserved for its
14774 -- instantiation.
14776 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14777 and then Present (Generic_Parent_Type (Parent (Typ)))
14778 then
14779 Typ := Base_Type (Typ);
14780 Set_Etype (N2, Typ);
14781 end if;
14782 end if;
14784 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14785 Set_Associated_Node (N, Empty);
14787 -- If the aggregate is an actual in a call, it has been
14788 -- resolved in the current context, to some local type. The
14789 -- enclosing call may have been disambiguated by the aggregate,
14790 -- and this disambiguation might fail at instantiation time
14791 -- because the type to which the aggregate did resolve is not
14792 -- preserved. In order to preserve some of this information,
14793 -- wrap the aggregate in a qualified expression, using the id
14794 -- of its type. For further disambiguation we qualify the type
14795 -- name with its scope (if visible) because both id's will have
14796 -- corresponding entities in an instance. This resolves most of
14797 -- the problems with missing type information on aggregates in
14798 -- instances.
14800 if Present (N2)
14801 and then Nkind (N2) = Nkind (N)
14802 and then Nkind (Parent (N2)) in N_Subprogram_Call
14803 and then Present (Typ)
14804 and then Comes_From_Source (Typ)
14805 then
14806 Nam := Make_Identifier (Loc, Chars (Typ));
14808 if Is_Immediately_Visible (Scope (Typ)) then
14809 Nam :=
14810 Make_Selected_Component (Loc,
14811 Prefix =>
14812 Make_Identifier (Loc, Chars (Scope (Typ))),
14813 Selector_Name => Nam);
14814 end if;
14816 Qual :=
14817 Make_Qualified_Expression (Loc,
14818 Subtype_Mark => Nam,
14819 Expression => Relocate_Node (N));
14820 end if;
14821 end if;
14823 Save_Global_Descendant (Field1 (N));
14824 Save_Global_Descendant (Field2 (N));
14825 Save_Global_Descendant (Field3 (N));
14826 Save_Global_Descendant (Field5 (N));
14828 if Present (Qual) then
14829 Rewrite (N, Qual);
14830 end if;
14831 end Save_References_In_Aggregate;
14833 ----------------------------------------------
14834 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14835 ----------------------------------------------
14837 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14838 begin
14839 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14840 Reset_Entity (N);
14842 elsif Nkind (N) = N_Operator_Symbol
14843 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14844 then
14845 Change_Operator_Symbol_To_String_Literal (N);
14846 end if;
14847 end Save_References_In_Char_Lit_Or_Op_Symbol;
14849 ------------------------------------
14850 -- Save_References_In_Descendants --
14851 ------------------------------------
14853 procedure Save_References_In_Descendants (N : Node_Id) is
14854 use Atree.Unchecked_Access;
14855 -- This code section is part of implementing an untyped tree
14856 -- traversal, so it needs direct access to node fields.
14858 begin
14859 Save_Global_Descendant (Field1 (N));
14860 Save_Global_Descendant (Field2 (N));
14861 Save_Global_Descendant (Field3 (N));
14862 Save_Global_Descendant (Field4 (N));
14863 Save_Global_Descendant (Field5 (N));
14864 end Save_References_In_Descendants;
14866 -----------------------------------
14867 -- Save_References_In_Identifier --
14868 -----------------------------------
14870 procedure Save_References_In_Identifier (N : Node_Id) is
14871 begin
14872 -- The node did not undergo a transformation
14874 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14875 declare
14876 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
14877 Orig_N2_Parent : constant Node_Id :=
14878 Original_Node (Parent (Aux_N2));
14879 begin
14880 -- The parent of this identifier is a selected component
14881 -- which denotes a named number that was constant folded.
14882 -- Preserve the original name for ASIS and link the parent
14883 -- with its expanded name. The constant folding will be
14884 -- repeated in the instance.
14886 if Nkind (Parent (N)) = N_Selected_Component
14887 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
14888 N_Real_Literal)
14889 and then Is_Entity_Name (Orig_N2_Parent)
14890 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
14891 and then Is_Global (Entity (Orig_N2_Parent))
14892 then
14893 N2 := Aux_N2;
14894 Set_Associated_Node
14895 (Parent (N), Original_Node (Parent (N2)));
14897 -- Common case
14899 else
14900 -- If this is a discriminant reference, always save it.
14901 -- It is used in the instance to find the corresponding
14902 -- discriminant positionally rather than by name.
14904 Set_Original_Discriminant
14905 (N, Original_Discriminant (Get_Associated_Node (N)));
14906 end if;
14908 Reset_Entity (N);
14909 end;
14911 -- The analysis of the generic copy transformed the identifier
14912 -- into another construct. Propagate the changes to the template.
14914 else
14915 N2 := Get_Associated_Node (N);
14917 -- The identifier denotes a call to a parameterless function.
14918 -- Mark the node as resolved when the function is external.
14920 if Nkind (N2) = N_Function_Call then
14921 E := Entity (Name (N2));
14923 if Present (E) and then Is_Global (E) then
14924 Set_Etype (N, Etype (N2));
14925 else
14926 Set_Associated_Node (N, Empty);
14927 Set_Etype (N, Empty);
14928 end if;
14930 -- The identifier denotes a named number that was constant
14931 -- folded. Preserve the original name for ASIS and undo the
14932 -- constant folding which will be repeated in the instance.
14934 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14935 and then Is_Entity_Name (Original_Node (N2))
14936 then
14937 Set_Associated_Node (N, Original_Node (N2));
14938 Reset_Entity (N);
14940 -- The identifier resolved to a string literal. Propagate this
14941 -- information to the generic template.
14943 elsif Nkind (N2) = N_String_Literal then
14944 Rewrite (N, New_Copy (N2));
14946 -- The identifier is rewritten as a dereference if it is the
14947 -- prefix of an implicit dereference. Preserve the original
14948 -- tree as the analysis of the instance will expand the node
14949 -- again, but preserve the resolved entity if it is global.
14951 elsif Nkind (N2) = N_Explicit_Dereference then
14952 if Is_Entity_Name (Prefix (N2))
14953 and then Present (Entity (Prefix (N2)))
14954 and then Is_Global (Entity (Prefix (N2)))
14955 then
14956 Set_Associated_Node (N, Prefix (N2));
14958 elsif Nkind (Prefix (N2)) = N_Function_Call
14959 and then Present (Entity (Name (Prefix (N2))))
14960 and then Is_Global (Entity (Name (Prefix (N2))))
14961 then
14962 Rewrite (N,
14963 Make_Explicit_Dereference (Loc,
14964 Prefix =>
14965 Make_Function_Call (Loc,
14966 Name =>
14967 New_Occurrence_Of
14968 (Entity (Name (Prefix (N2))), Loc))));
14970 else
14971 Set_Associated_Node (N, Empty);
14972 Set_Etype (N, Empty);
14973 end if;
14975 -- The subtype mark of a nominally unconstrained object is
14976 -- rewritten as a subtype indication using the bounds of the
14977 -- expression. Recover the original subtype mark.
14979 elsif Nkind (N2) = N_Subtype_Indication
14980 and then Is_Entity_Name (Original_Node (N2))
14981 then
14982 Set_Associated_Node (N, Original_Node (N2));
14983 Reset_Entity (N);
14984 end if;
14985 end if;
14986 end Save_References_In_Identifier;
14988 ---------------------------------
14989 -- Save_References_In_Operator --
14990 ---------------------------------
14992 procedure Save_References_In_Operator (N : Node_Id) is
14993 begin
14994 -- The node did not undergo a transformation
14996 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14997 if Nkind (N) = N_Op_Concat then
14998 Set_Is_Component_Left_Opnd (N,
14999 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15001 Set_Is_Component_Right_Opnd (N,
15002 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15003 end if;
15005 Reset_Entity (N);
15007 -- The analysis of the generic copy transformed the operator into
15008 -- some other construct. Propagate the changes to the template if
15009 -- applicable.
15011 else
15012 N2 := Get_Associated_Node (N);
15014 -- The operator resoved to a function call
15016 if Nkind (N2) = N_Function_Call then
15018 -- Add explicit qualifications in the generic template for
15019 -- all operands of universal type. This aids resolution by
15020 -- preserving the actual type of a literal or an attribute
15021 -- that yields a universal result.
15023 Qualify_Universal_Operands (N, N2);
15025 E := Entity (Name (N2));
15027 if Present (E) and then Is_Global (E) then
15028 Set_Etype (N, Etype (N2));
15029 else
15030 Set_Associated_Node (N, Empty);
15031 Set_Etype (N, Empty);
15032 end if;
15034 -- The operator was folded into a literal
15036 elsif Nkind_In (N2, N_Integer_Literal,
15037 N_Real_Literal,
15038 N_String_Literal)
15039 then
15040 if Present (Original_Node (N2))
15041 and then Nkind (Original_Node (N2)) = Nkind (N)
15042 then
15043 -- Operation was constant-folded. Whenever possible,
15044 -- recover semantic information from unfolded node,
15045 -- for ASIS use.
15047 Set_Associated_Node (N, Original_Node (N2));
15049 if Nkind (N) = N_Op_Concat then
15050 Set_Is_Component_Left_Opnd (N,
15051 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15052 Set_Is_Component_Right_Opnd (N,
15053 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15054 end if;
15056 Reset_Entity (N);
15058 -- Propagate the constant folding back to the template
15060 else
15061 Rewrite (N, New_Copy (N2));
15062 Set_Analyzed (N, False);
15063 end if;
15065 -- The operator was folded into an enumeration literal. Retain
15066 -- the entity to avoid spurious ambiguities if it is overloaded
15067 -- at the point of instantiation or inlining.
15069 elsif Nkind (N2) = N_Identifier
15070 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15071 then
15072 Rewrite (N, New_Copy (N2));
15073 Set_Analyzed (N, False);
15074 end if;
15075 end if;
15077 -- Complete the operands check if node has not been constant
15078 -- folded.
15080 if Nkind (N) in N_Op then
15081 Save_Entity_Descendants (N);
15082 end if;
15083 end Save_References_In_Operator;
15085 -------------------------------
15086 -- Save_References_In_Pragma --
15087 -------------------------------
15089 procedure Save_References_In_Pragma (Prag : Node_Id) is
15090 Context : Node_Id;
15091 Do_Save : Boolean := True;
15093 use Atree.Unchecked_Access;
15094 -- This code section is part of implementing an untyped tree
15095 -- traversal, so it needs direct access to node fields.
15097 begin
15098 -- Do not save global references in pragmas generated from aspects
15099 -- because the pragmas will be regenerated at instantiation time.
15101 if From_Aspect_Specification (Prag) then
15102 Do_Save := False;
15104 -- The capture of global references within contract-related source
15105 -- pragmas associated with generic packages, subprograms or their
15106 -- respective bodies must be delayed due to timing of annotation
15107 -- analysis. Global references are still captured in routine
15108 -- Save_Global_References_In_Contract.
15110 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15111 if Is_Package_Contract_Annotation (Prag) then
15112 Context := Find_Related_Package_Or_Body (Prag);
15113 else
15114 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15115 Context := Find_Related_Declaration_Or_Body (Prag);
15116 end if;
15118 -- The use of Original_Node accounts for the case when the
15119 -- related context is generic template.
15121 if Requires_Delayed_Save (Original_Node (Context)) then
15122 Do_Save := False;
15123 end if;
15124 end if;
15126 -- For all other cases, save all global references within the
15127 -- descendants, but skip the following semantic fields:
15129 -- Field1 - Next_Pragma
15130 -- Field3 - Corresponding_Aspect
15131 -- Field5 - Next_Rep_Item
15133 if Do_Save then
15134 Save_Global_Descendant (Field2 (Prag));
15135 Save_Global_Descendant (Field4 (Prag));
15136 end if;
15137 end Save_References_In_Pragma;
15139 -- Start of processing for Save_References
15141 begin
15142 if N = Empty then
15143 null;
15145 -- Aggregates
15147 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15148 Save_References_In_Aggregate (N);
15150 -- Character literals, operator symbols
15152 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15153 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15155 -- Defining identifiers
15157 elsif Nkind (N) in N_Entity then
15158 null;
15160 -- Identifiers
15162 elsif Nkind (N) = N_Identifier then
15163 Save_References_In_Identifier (N);
15165 -- Operators
15167 elsif Nkind (N) in N_Op then
15168 Save_References_In_Operator (N);
15170 -- Pragmas
15172 elsif Nkind (N) = N_Pragma then
15173 Save_References_In_Pragma (N);
15175 else
15176 Save_References_In_Descendants (N);
15177 end if;
15179 -- Save all global references found within the aspect specifications
15180 -- of the related node.
15182 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15184 -- The capture of global references within aspects associated with
15185 -- generic packages, subprograms or their bodies must be delayed
15186 -- due to timing of annotation analysis. Global references are
15187 -- still captured in routine Save_Global_References_In_Contract.
15189 if Requires_Delayed_Save (N) then
15190 null;
15192 -- Otherwise save all global references within the aspects
15194 else
15195 Save_Global_References_In_Aspects (N);
15196 end if;
15197 end if;
15198 end Save_References;
15200 -- Start of processing for Save_Global_References
15202 begin
15203 Gen_Scope := Current_Scope;
15205 -- If the generic unit is a child unit, references to entities in the
15206 -- parent are treated as local, because they will be resolved anew in
15207 -- the context of the instance of the parent.
15209 while Is_Child_Unit (Gen_Scope)
15210 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15211 loop
15212 Gen_Scope := Scope (Gen_Scope);
15213 end loop;
15215 Save_References (Templ);
15216 end Save_Global_References;
15218 ---------------------------------------
15219 -- Save_Global_References_In_Aspects --
15220 ---------------------------------------
15222 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15223 Asp : Node_Id;
15224 Expr : Node_Id;
15226 begin
15227 Asp := First (Aspect_Specifications (N));
15228 while Present (Asp) loop
15229 Expr := Expression (Asp);
15231 if Present (Expr) then
15232 Save_Global_References (Expr);
15233 end if;
15235 Next (Asp);
15236 end loop;
15237 end Save_Global_References_In_Aspects;
15239 ------------------------------------------
15240 -- Set_Copied_Sloc_For_Inherited_Pragma --
15241 ------------------------------------------
15243 procedure Set_Copied_Sloc_For_Inherited_Pragma
15244 (N : Node_Id;
15245 E : Entity_Id)
15247 begin
15248 Create_Instantiation_Source (N, E,
15249 Inlined_Body => False,
15250 Inherited_Pragma => True,
15251 Factor => S_Adjustment);
15252 end Set_Copied_Sloc_For_Inherited_Pragma;
15254 --------------------------------------
15255 -- Set_Copied_Sloc_For_Inlined_Body --
15256 --------------------------------------
15258 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15259 begin
15260 Create_Instantiation_Source (N, E,
15261 Inlined_Body => True,
15262 Inherited_Pragma => False,
15263 Factor => S_Adjustment);
15264 end Set_Copied_Sloc_For_Inlined_Body;
15266 ---------------------
15267 -- Set_Instance_Of --
15268 ---------------------
15270 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15271 begin
15272 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15273 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15274 Generic_Renamings.Increment_Last;
15275 end Set_Instance_Of;
15277 --------------------
15278 -- Set_Next_Assoc --
15279 --------------------
15281 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15282 begin
15283 Generic_Renamings.Table (E).Next_In_HTable := Next;
15284 end Set_Next_Assoc;
15286 -------------------
15287 -- Start_Generic --
15288 -------------------
15290 procedure Start_Generic is
15291 begin
15292 -- ??? More things could be factored out in this routine.
15293 -- Should probably be done at a later stage.
15295 Generic_Flags.Append (Inside_A_Generic);
15296 Inside_A_Generic := True;
15298 Expander_Mode_Save_And_Set (False);
15299 end Start_Generic;
15301 ----------------------
15302 -- Set_Instance_Env --
15303 ----------------------
15305 procedure Set_Instance_Env
15306 (Gen_Unit : Entity_Id;
15307 Act_Unit : Entity_Id)
15309 Assertion_Status : constant Boolean := Assertions_Enabled;
15310 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15311 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15313 begin
15314 -- Regardless of the current mode, predefined units are analyzed in the
15315 -- most current Ada mode, and earlier version Ada checks do not apply
15316 -- to predefined units. Nothing needs to be done for non-internal units.
15317 -- These are always analyzed in the current mode.
15319 if Is_Internal_File_Name
15320 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15321 Renamings_Included => True)
15322 then
15323 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15325 -- In Ada2012 we may want to enable assertions in an instance of a
15326 -- predefined unit, in which case we need to preserve the current
15327 -- setting for the Assertions_Enabled flag. This will become more
15328 -- critical when pre/postconditions are added to predefined units,
15329 -- as is already the case for some numeric libraries.
15331 if Ada_Version >= Ada_2012 then
15332 Assertions_Enabled := Assertion_Status;
15333 end if;
15335 -- SPARK_Mode for an instance is the one applicable at the point of
15336 -- instantiation.
15338 SPARK_Mode := Save_SPARK_Mode;
15339 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15340 end if;
15342 Current_Instantiated_Parent :=
15343 (Gen_Id => Gen_Unit,
15344 Act_Id => Act_Unit,
15345 Next_In_HTable => Assoc_Null);
15346 end Set_Instance_Env;
15348 -----------------
15349 -- Switch_View --
15350 -----------------
15352 procedure Switch_View (T : Entity_Id) is
15353 BT : constant Entity_Id := Base_Type (T);
15354 Priv_Elmt : Elmt_Id := No_Elmt;
15355 Priv_Sub : Entity_Id;
15357 begin
15358 -- T may be private but its base type may have been exchanged through
15359 -- some other occurrence, in which case there is nothing to switch
15360 -- besides T itself. Note that a private dependent subtype of a private
15361 -- type might not have been switched even if the base type has been,
15362 -- because of the last branch of Check_Private_View (see comment there).
15364 if not Is_Private_Type (BT) then
15365 Prepend_Elmt (Full_View (T), Exchanged_Views);
15366 Exchange_Declarations (T);
15367 return;
15368 end if;
15370 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15372 if Present (Full_View (BT)) then
15373 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15374 Exchange_Declarations (BT);
15375 end if;
15377 while Present (Priv_Elmt) loop
15378 Priv_Sub := (Node (Priv_Elmt));
15380 -- We avoid flipping the subtype if the Etype of its full view is
15381 -- private because this would result in a malformed subtype. This
15382 -- occurs when the Etype of the subtype full view is the full view of
15383 -- the base type (and since the base types were just switched, the
15384 -- subtype is pointing to the wrong view). This is currently the case
15385 -- for tagged record types, access types (maybe more?) and needs to
15386 -- be resolved. ???
15388 if Present (Full_View (Priv_Sub))
15389 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15390 then
15391 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15392 Exchange_Declarations (Priv_Sub);
15393 end if;
15395 Next_Elmt (Priv_Elmt);
15396 end loop;
15397 end Switch_View;
15399 -----------------
15400 -- True_Parent --
15401 -----------------
15403 function True_Parent (N : Node_Id) return Node_Id is
15404 begin
15405 if Nkind (Parent (N)) = N_Subunit then
15406 return Parent (Corresponding_Stub (Parent (N)));
15407 else
15408 return Parent (N);
15409 end if;
15410 end True_Parent;
15412 -----------------------------
15413 -- Valid_Default_Attribute --
15414 -----------------------------
15416 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15417 Attr_Id : constant Attribute_Id :=
15418 Get_Attribute_Id (Attribute_Name (Def));
15419 T : constant Entity_Id := Entity (Prefix (Def));
15420 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15421 F : Entity_Id;
15422 Num_F : Nat;
15423 OK : Boolean;
15425 begin
15426 if No (T) or else T = Any_Id then
15427 return;
15428 end if;
15430 Num_F := 0;
15431 F := First_Formal (Nam);
15432 while Present (F) loop
15433 Num_F := Num_F + 1;
15434 Next_Formal (F);
15435 end loop;
15437 case Attr_Id is
15438 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15439 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15440 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15441 Attribute_Unbiased_Rounding =>
15442 OK := Is_Fun
15443 and then Num_F = 1
15444 and then Is_Floating_Point_Type (T);
15446 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15447 Attribute_Value | Attribute_Wide_Image |
15448 Attribute_Wide_Value =>
15449 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15451 when Attribute_Max | Attribute_Min =>
15452 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15454 when Attribute_Input =>
15455 OK := (Is_Fun and then Num_F = 1);
15457 when Attribute_Output | Attribute_Read | Attribute_Write =>
15458 OK := (not Is_Fun and then Num_F = 2);
15460 when others =>
15461 OK := False;
15462 end case;
15464 if not OK then
15465 Error_Msg_N
15466 ("attribute reference has wrong profile for subprogram", Def);
15467 end if;
15468 end Valid_Default_Attribute;
15470 end Sem_Ch12;