2016-06-14 Javier Miranda <miranda@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob78c161f0ab0e01f576183dab5c24930900d25689
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.
718 procedure Install_Formal_Packages (Par : Entity_Id);
719 -- Install the visible part of any formal of the parent that is a formal
720 -- package. Note that for the case of a formal package with a box, this
721 -- includes the formal part of the formal package (12.7(10/2)).
723 procedure Install_Hidden_Primitives
724 (Prims_List : in out Elist_Id;
725 Gen_T : Entity_Id;
726 Act_T : Entity_Id);
727 -- Remove suffix 'P' from hidden primitives of Act_T to match the
728 -- visibility of primitives of Gen_T. The list of primitives to which
729 -- the suffix is removed is added to Prims_List to restore them later.
731 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
732 -- When compiling an instance of a child unit the parent (which is
733 -- itself an instance) is an enclosing scope that must be made
734 -- immediately visible. This procedure is also used to install the non-
735 -- generic parent of a generic child unit when compiling its body, so
736 -- that full views of types in the parent are made visible.
738 -- The functions Instantiate_XXX perform various legality checks and build
739 -- the declarations for instantiated generic parameters. In all of these
740 -- Formal is the entity in the generic unit, Actual is the entity of
741 -- expression in the generic associations, and Analyzed_Formal is the
742 -- formal in the generic copy, which contains the semantic information to
743 -- be used to validate the actual.
745 function Instantiate_Object
746 (Formal : Node_Id;
747 Actual : Node_Id;
748 Analyzed_Formal : Node_Id) return List_Id;
750 function Instantiate_Type
751 (Formal : Node_Id;
752 Actual : Node_Id;
753 Analyzed_Formal : Node_Id;
754 Actual_Decls : List_Id) return List_Id;
756 function Instantiate_Formal_Subprogram
757 (Formal : Node_Id;
758 Actual : Node_Id;
759 Analyzed_Formal : Node_Id) return Node_Id;
761 function Instantiate_Formal_Package
762 (Formal : Node_Id;
763 Actual : Node_Id;
764 Analyzed_Formal : Node_Id) return List_Id;
765 -- If the formal package is declared with a box, special visibility rules
766 -- apply to its formals: they are in the visible part of the package. This
767 -- is true in the declarative region of the formal package, that is to say
768 -- in the enclosing generic or instantiation. For an instantiation, the
769 -- parameters of the formal package are made visible in an explicit step.
770 -- Furthermore, if the actual has a visible USE clause, these formals must
771 -- be made potentially use-visible as well. On exit from the enclosing
772 -- instantiation, the reverse must be done.
774 -- For a formal package declared without a box, there are conformance rules
775 -- that apply to the actuals in the generic declaration and the actuals of
776 -- the actual package in the enclosing instantiation. The simplest way to
777 -- apply these rules is to repeat the instantiation of the formal package
778 -- in the context of the enclosing instance, and compare the generic
779 -- associations of this instantiation with those of the actual package.
780 -- This internal instantiation only needs to contain the renamings of the
781 -- formals: the visible and private declarations themselves need not be
782 -- created.
784 -- In Ada 2005, the formal package may be only partially parameterized.
785 -- In that case the visibility step must make visible those actuals whose
786 -- corresponding formals were given with a box. A final complication
787 -- involves inherited operations from formal derived types, which must
788 -- be visible if the type is.
790 function Is_In_Main_Unit (N : Node_Id) return Boolean;
791 -- Test if given node is in the main unit
793 procedure Load_Parent_Of_Generic
794 (N : Node_Id;
795 Spec : Node_Id;
796 Body_Optional : Boolean := False);
797 -- If the generic appears in a separate non-generic library unit, load the
798 -- corresponding body to retrieve the body of the generic. N is the node
799 -- for the generic instantiation, Spec is the generic package declaration.
801 -- Body_Optional is a flag that indicates that the body is being loaded to
802 -- ensure that temporaries are generated consistently when there are other
803 -- instances in the current declarative part that precede the one being
804 -- loaded. In that case a missing body is acceptable.
806 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
807 -- Within the generic part, entities in the formal package are
808 -- visible. To validate subsequent type declarations, indicate
809 -- the correspondence between the entities in the analyzed formal,
810 -- and the entities in the actual package. There are three packages
811 -- involved in the instantiation of a formal package: the parent
812 -- generic P1 which appears in the generic declaration, the fake
813 -- instantiation P2 which appears in the analyzed generic, and whose
814 -- visible entities may be used in subsequent formals, and the actual
815 -- P3 in the instance. To validate subsequent formals, me indicate
816 -- that the entities in P2 are mapped into those of P3. The mapping of
817 -- entities has to be done recursively for nested packages.
819 procedure Move_Freeze_Nodes
820 (Out_Of : Entity_Id;
821 After : Node_Id;
822 L : List_Id);
823 -- Freeze nodes can be generated in the analysis of a generic unit, but
824 -- will not be seen by the back-end. It is necessary to move those nodes
825 -- to the enclosing scope if they freeze an outer entity. We place them
826 -- at the end of the enclosing generic package, which is semantically
827 -- neutral.
829 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
830 -- Analyze actuals to perform name resolution. Full resolution is done
831 -- later, when the expected types are known, but names have to be captured
832 -- before installing parents of generics, that are not visible for the
833 -- actuals themselves.
835 -- If Inst is present, it is the entity of the package instance. This
836 -- entity is marked as having a limited_view actual when some actual is
837 -- a limited view. This is used to place the instance body properly.
839 procedure Remove_Parent (In_Body : Boolean := False);
840 -- Reverse effect after instantiation of child is complete
842 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
843 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
844 -- set to No_Elist.
846 procedure Set_Instance_Env
847 (Gen_Unit : Entity_Id;
848 Act_Unit : Entity_Id);
849 -- Save current instance on saved environment, to be used to determine
850 -- the global status of entities in nested instances. Part of Save_Env.
851 -- called after verifying that the generic unit is legal for the instance,
852 -- The procedure also examines whether the generic unit is a predefined
853 -- unit, in order to set configuration switches accordingly. As a result
854 -- the procedure must be called after analyzing and freezing the actuals.
856 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
857 -- Associate analyzed generic parameter with corresponding instance. Used
858 -- for semantic checks at instantiation time.
860 function True_Parent (N : Node_Id) return Node_Id;
861 -- For a subunit, return parent of corresponding stub, else return
862 -- parent of node.
864 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
865 -- Verify that an attribute that appears as the default for a formal
866 -- subprogram is a function or procedure with the correct profile.
868 -------------------------------------------
869 -- Data Structures for Generic Renamings --
870 -------------------------------------------
872 -- The map Generic_Renamings associates generic entities with their
873 -- corresponding actuals. Currently used to validate type instances. It
874 -- will eventually be used for all generic parameters to eliminate the
875 -- need for overload resolution in the instance.
877 type Assoc_Ptr is new Int;
879 Assoc_Null : constant Assoc_Ptr := -1;
881 type Assoc is record
882 Gen_Id : Entity_Id;
883 Act_Id : Entity_Id;
884 Next_In_HTable : Assoc_Ptr;
885 end record;
887 package Generic_Renamings is new Table.Table
888 (Table_Component_Type => Assoc,
889 Table_Index_Type => Assoc_Ptr,
890 Table_Low_Bound => 0,
891 Table_Initial => 10,
892 Table_Increment => 100,
893 Table_Name => "Generic_Renamings");
895 -- Variable to hold enclosing instantiation. When the environment is
896 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
898 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
900 -- Hash table for associations
902 HTable_Size : constant := 37;
903 type HTable_Range is range 0 .. HTable_Size - 1;
905 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
906 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
907 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
908 function Hash (F : Entity_Id) return HTable_Range;
910 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
911 Header_Num => HTable_Range,
912 Element => Assoc,
913 Elmt_Ptr => Assoc_Ptr,
914 Null_Ptr => Assoc_Null,
915 Set_Next => Set_Next_Assoc,
916 Next => Next_Assoc,
917 Key => Entity_Id,
918 Get_Key => Get_Gen_Id,
919 Hash => Hash,
920 Equal => "=");
922 Exchanged_Views : Elist_Id;
923 -- This list holds the private views that have been exchanged during
924 -- instantiation to restore the visibility of the generic declaration.
925 -- (see comments above). After instantiation, the current visibility is
926 -- reestablished by means of a traversal of this list.
928 Hidden_Entities : Elist_Id;
929 -- This list holds the entities of the current scope that are removed
930 -- from immediate visibility when instantiating a child unit. Their
931 -- visibility is restored in Remove_Parent.
933 -- Because instantiations can be recursive, the following must be saved
934 -- on entry and restored on exit from an instantiation (spec or body).
935 -- This is done by the two procedures Save_Env and Restore_Env. For
936 -- package and subprogram instantiations (but not for the body instances)
937 -- the action of Save_Env is done in two steps: Init_Env is called before
938 -- Check_Generic_Child_Unit, because setting the parent instances requires
939 -- that the visibility data structures be properly initialized. Once the
940 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
942 Parent_Unit_Visible : Boolean := False;
943 -- Parent_Unit_Visible is used when the generic is a child unit, and
944 -- indicates whether the ultimate parent of the generic is visible in the
945 -- instantiation environment. It is used to reset the visibility of the
946 -- parent at the end of the instantiation (see Remove_Parent).
948 Instance_Parent_Unit : Entity_Id := Empty;
949 -- This records the ultimate parent unit of an instance of a generic
950 -- child unit and is used in conjunction with Parent_Unit_Visible to
951 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
953 type Instance_Env is record
954 Instantiated_Parent : Assoc;
955 Exchanged_Views : Elist_Id;
956 Hidden_Entities : Elist_Id;
957 Current_Sem_Unit : Unit_Number_Type;
958 Parent_Unit_Visible : Boolean := False;
959 Instance_Parent_Unit : Entity_Id := Empty;
960 Switches : Config_Switches_Type;
961 end record;
963 package Instance_Envs is new Table.Table (
964 Table_Component_Type => Instance_Env,
965 Table_Index_Type => Int,
966 Table_Low_Bound => 0,
967 Table_Initial => 32,
968 Table_Increment => 100,
969 Table_Name => "Instance_Envs");
971 procedure Restore_Private_Views
972 (Pack_Id : Entity_Id;
973 Is_Package : Boolean := True);
974 -- Restore the private views of external types, and unmark the generic
975 -- renamings of actuals, so that they become compatible subtypes again.
976 -- For subprograms, Pack_Id is the package constructed to hold the
977 -- renamings.
979 procedure Switch_View (T : Entity_Id);
980 -- Switch the partial and full views of a type and its private
981 -- dependents (i.e. its subtypes and derived types).
983 ------------------------------------
984 -- Structures for Error Reporting --
985 ------------------------------------
987 Instantiation_Node : Node_Id;
988 -- Used by subprograms that validate instantiation of formal parameters
989 -- where there might be no actual on which to place the error message.
990 -- Also used to locate the instantiation node for generic subunits.
992 Instantiation_Error : exception;
993 -- When there is a semantic error in the generic parameter matching,
994 -- there is no point in continuing the instantiation, because the
995 -- number of cascaded errors is unpredictable. This exception aborts
996 -- the instantiation process altogether.
998 S_Adjustment : Sloc_Adjustment;
999 -- Offset created for each node in an instantiation, in order to keep
1000 -- track of the source position of the instantiation in each of its nodes.
1001 -- A subsequent semantic error or warning on a construct of the instance
1002 -- points to both places: the original generic node, and the point of
1003 -- instantiation. See Sinput and Sinput.L for additional details.
1005 ------------------------------------------------------------
1006 -- Data structure for keeping track when inside a Generic --
1007 ------------------------------------------------------------
1009 -- The following table is used to save values of the Inside_A_Generic
1010 -- flag (see spec of Sem) when they are saved by Start_Generic.
1012 package Generic_Flags is new Table.Table (
1013 Table_Component_Type => Boolean,
1014 Table_Index_Type => Int,
1015 Table_Low_Bound => 0,
1016 Table_Initial => 32,
1017 Table_Increment => 200,
1018 Table_Name => "Generic_Flags");
1020 ---------------------------
1021 -- Abandon_Instantiation --
1022 ---------------------------
1024 procedure Abandon_Instantiation (N : Node_Id) is
1025 begin
1026 Error_Msg_N ("\instantiation abandoned!", N);
1027 raise Instantiation_Error;
1028 end Abandon_Instantiation;
1030 --------------------------------
1031 -- Add_Pending_Instantiation --
1032 --------------------------------
1034 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1035 begin
1037 -- Add to the instantiation node and the corresponding unit declaration
1038 -- the current values of global flags to be used when analyzing the
1039 -- instance body.
1041 Pending_Instantiations.Append
1042 ((Inst_Node => Inst,
1043 Act_Decl => Act_Decl,
1044 Expander_Status => Expander_Active,
1045 Current_Sem_Unit => Current_Sem_Unit,
1046 Scope_Suppress => Scope_Suppress,
1047 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1048 Version => Ada_Version,
1049 Version_Pragma => Ada_Version_Pragma,
1050 Warnings => Save_Warnings,
1051 SPARK_Mode => SPARK_Mode,
1052 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1053 end Add_Pending_Instantiation;
1055 --------------------------
1056 -- Analyze_Associations --
1057 --------------------------
1059 function Analyze_Associations
1060 (I_Node : Node_Id;
1061 Formals : List_Id;
1062 F_Copy : List_Id) return List_Id
1064 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1065 Assoc : constant List_Id := New_List;
1066 Default_Actuals : constant List_Id := New_List;
1067 Gen_Unit : constant Entity_Id :=
1068 Defining_Entity (Parent (F_Copy));
1070 Actuals : List_Id;
1071 Actual : Node_Id;
1072 Analyzed_Formal : Node_Id;
1073 First_Named : Node_Id := Empty;
1074 Formal : Node_Id;
1075 Match : Node_Id;
1076 Named : Node_Id;
1077 Saved_Formal : Node_Id;
1079 Default_Formals : constant List_Id := New_List;
1080 -- If an Others_Choice is present, some of the formals may be defaulted.
1081 -- To simplify the treatment of visibility in an instance, we introduce
1082 -- individual defaults for each such formal. These defaults are
1083 -- appended to the list of associations and replace the Others_Choice.
1085 Found_Assoc : Node_Id;
1086 -- Association for the current formal being match. Empty if there are
1087 -- no remaining actuals, or if there is no named association with the
1088 -- name of the formal.
1090 Is_Named_Assoc : Boolean;
1091 Num_Matched : Nat := 0;
1092 Num_Actuals : Nat := 0;
1094 Others_Present : Boolean := False;
1095 Others_Choice : Node_Id := Empty;
1096 -- In Ada 2005, indicates partial parameterization of a formal
1097 -- package. As usual an other association must be last in the list.
1099 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1100 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1101 -- cannot have a named association for it. AI05-0025 extends this rule
1102 -- to formals of formal packages by AI05-0025, and it also applies to
1103 -- box-initialized formals.
1105 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1106 -- Determine whether the parameter types and the return type of Subp
1107 -- are fully defined at the point of instantiation.
1109 function Matching_Actual
1110 (F : Entity_Id;
1111 A_F : Entity_Id) return Node_Id;
1112 -- Find actual that corresponds to a given a formal parameter. If the
1113 -- actuals are positional, return the next one, if any. If the actuals
1114 -- are named, scan the parameter associations to find the right one.
1115 -- A_F is the corresponding entity in the analyzed generic,which is
1116 -- placed on the selector name for ASIS use.
1118 -- In Ada 2005, a named association may be given with a box, in which
1119 -- case Matching_Actual sets Found_Assoc to the generic association,
1120 -- but return Empty for the actual itself. In this case the code below
1121 -- creates a corresponding declaration for the formal.
1123 function Partial_Parameterization return Boolean;
1124 -- Ada 2005: if no match is found for a given formal, check if the
1125 -- association for it includes a box, or whether the associations
1126 -- include an Others clause.
1128 procedure Process_Default (F : Entity_Id);
1129 -- Add a copy of the declaration of generic formal F to the list of
1130 -- associations, and add an explicit box association for F if there
1131 -- is none yet, and the default comes from an Others_Choice.
1133 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1134 -- Determine whether Subp renames one of the subprograms defined in the
1135 -- generated package Standard.
1137 procedure Set_Analyzed_Formal;
1138 -- Find the node in the generic copy that corresponds to a given formal.
1139 -- The semantic information on this node is used to perform legality
1140 -- checks on the actuals. Because semantic analysis can introduce some
1141 -- anonymous entities or modify the declaration node itself, the
1142 -- correspondence between the two lists is not one-one. In addition to
1143 -- anonymous types, the presence a formal equality will introduce an
1144 -- implicit declaration for the corresponding inequality.
1146 ----------------------------------------
1147 -- Check_Overloaded_Formal_Subprogram --
1148 ----------------------------------------
1150 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1151 Temp_Formal : Entity_Id;
1153 begin
1154 Temp_Formal := First (Formals);
1155 while Present (Temp_Formal) loop
1156 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1157 and then Temp_Formal /= Formal
1158 and then
1159 Chars (Defining_Unit_Name (Specification (Formal))) =
1160 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1161 then
1162 if Present (Found_Assoc) then
1163 Error_Msg_N
1164 ("named association not allowed for overloaded formal",
1165 Found_Assoc);
1167 else
1168 Error_Msg_N
1169 ("named association not allowed for overloaded formal",
1170 Others_Choice);
1171 end if;
1173 Abandon_Instantiation (Instantiation_Node);
1174 end if;
1176 Next (Temp_Formal);
1177 end loop;
1178 end Check_Overloaded_Formal_Subprogram;
1180 -------------------------------
1181 -- Has_Fully_Defined_Profile --
1182 -------------------------------
1184 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1185 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1186 -- Determine whethet type Typ is fully defined
1188 ---------------------------
1189 -- Is_Fully_Defined_Type --
1190 ---------------------------
1192 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1193 begin
1194 -- A private type without a full view is not fully defined
1196 if Is_Private_Type (Typ)
1197 and then No (Full_View (Typ))
1198 then
1199 return False;
1201 -- An incomplete type is never fully defined
1203 elsif Is_Incomplete_Type (Typ) then
1204 return False;
1206 -- All other types are fully defined
1208 else
1209 return True;
1210 end if;
1211 end Is_Fully_Defined_Type;
1213 -- Local declarations
1215 Param : Entity_Id;
1217 -- Start of processing for Has_Fully_Defined_Profile
1219 begin
1220 -- Check the parameters
1222 Param := First_Formal (Subp);
1223 while Present (Param) loop
1224 if not Is_Fully_Defined_Type (Etype (Param)) then
1225 return False;
1226 end if;
1228 Next_Formal (Param);
1229 end loop;
1231 -- Check the return type
1233 return Is_Fully_Defined_Type (Etype (Subp));
1234 end Has_Fully_Defined_Profile;
1236 ---------------------
1237 -- Matching_Actual --
1238 ---------------------
1240 function Matching_Actual
1241 (F : Entity_Id;
1242 A_F : Entity_Id) return Node_Id
1244 Prev : Node_Id;
1245 Act : Node_Id;
1247 begin
1248 Is_Named_Assoc := False;
1250 -- End of list of purely positional parameters
1252 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1253 Found_Assoc := Empty;
1254 Act := Empty;
1256 -- Case of positional parameter corresponding to current formal
1258 elsif No (Selector_Name (Actual)) then
1259 Found_Assoc := Actual;
1260 Act := Explicit_Generic_Actual_Parameter (Actual);
1261 Num_Matched := Num_Matched + 1;
1262 Next (Actual);
1264 -- Otherwise scan list of named actuals to find the one with the
1265 -- desired name. All remaining actuals have explicit names.
1267 else
1268 Is_Named_Assoc := True;
1269 Found_Assoc := Empty;
1270 Act := Empty;
1271 Prev := Empty;
1273 while Present (Actual) loop
1274 if Chars (Selector_Name (Actual)) = Chars (F) then
1275 Set_Entity (Selector_Name (Actual), A_F);
1276 Set_Etype (Selector_Name (Actual), Etype (A_F));
1277 Generate_Reference (A_F, Selector_Name (Actual));
1278 Found_Assoc := Actual;
1279 Act := Explicit_Generic_Actual_Parameter (Actual);
1280 Num_Matched := Num_Matched + 1;
1281 exit;
1282 end if;
1284 Prev := Actual;
1285 Next (Actual);
1286 end loop;
1288 -- Reset for subsequent searches. In most cases the named
1289 -- associations are in order. If they are not, we reorder them
1290 -- to avoid scanning twice the same actual. This is not just a
1291 -- question of efficiency: there may be multiple defaults with
1292 -- boxes that have the same name. In a nested instantiation we
1293 -- insert actuals for those defaults, and cannot rely on their
1294 -- names to disambiguate them.
1296 if Actual = First_Named then
1297 Next (First_Named);
1299 elsif Present (Actual) then
1300 Insert_Before (First_Named, Remove_Next (Prev));
1301 end if;
1303 Actual := First_Named;
1304 end if;
1306 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1307 Set_Used_As_Generic_Actual (Entity (Act));
1308 end if;
1310 return Act;
1311 end Matching_Actual;
1313 ------------------------------
1314 -- Partial_Parameterization --
1315 ------------------------------
1317 function Partial_Parameterization return Boolean is
1318 begin
1319 return Others_Present
1320 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1321 end Partial_Parameterization;
1323 ---------------------
1324 -- Process_Default --
1325 ---------------------
1327 procedure Process_Default (F : Entity_Id) is
1328 Loc : constant Source_Ptr := Sloc (I_Node);
1329 F_Id : constant Entity_Id := Defining_Entity (F);
1330 Decl : Node_Id;
1331 Default : Node_Id;
1332 Id : Entity_Id;
1334 begin
1335 -- Append copy of formal declaration to associations, and create new
1336 -- defining identifier for it.
1338 Decl := New_Copy_Tree (F);
1339 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1341 if Nkind (F) in N_Formal_Subprogram_Declaration then
1342 Set_Defining_Unit_Name (Specification (Decl), Id);
1344 else
1345 Set_Defining_Identifier (Decl, Id);
1346 end if;
1348 Append (Decl, Assoc);
1350 if No (Found_Assoc) then
1351 Default :=
1352 Make_Generic_Association (Loc,
1353 Selector_Name =>
1354 New_Occurrence_Of (Id, Loc),
1355 Explicit_Generic_Actual_Parameter => Empty);
1356 Set_Box_Present (Default);
1357 Append (Default, Default_Formals);
1358 end if;
1359 end Process_Default;
1361 ---------------------------------
1362 -- Renames_Standard_Subprogram --
1363 ---------------------------------
1365 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1366 Id : Entity_Id;
1368 begin
1369 Id := Alias (Subp);
1370 while Present (Id) loop
1371 if Scope (Id) = Standard_Standard then
1372 return True;
1373 end if;
1375 Id := Alias (Id);
1376 end loop;
1378 return False;
1379 end Renames_Standard_Subprogram;
1381 -------------------------
1382 -- Set_Analyzed_Formal --
1383 -------------------------
1385 procedure Set_Analyzed_Formal is
1386 Kind : Node_Kind;
1388 begin
1389 while Present (Analyzed_Formal) loop
1390 Kind := Nkind (Analyzed_Formal);
1392 case Nkind (Formal) is
1394 when N_Formal_Subprogram_Declaration =>
1395 exit when Kind in N_Formal_Subprogram_Declaration
1396 and then
1397 Chars
1398 (Defining_Unit_Name (Specification (Formal))) =
1399 Chars
1400 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1402 when N_Formal_Package_Declaration =>
1403 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1404 N_Generic_Package_Declaration,
1405 N_Package_Declaration);
1407 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1409 when others =>
1411 -- Skip freeze nodes, and nodes inserted to replace
1412 -- unrecognized pragmas.
1414 exit when
1415 Kind not in N_Formal_Subprogram_Declaration
1416 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1417 N_Freeze_Entity,
1418 N_Null_Statement,
1419 N_Itype_Reference)
1420 and then Chars (Defining_Identifier (Formal)) =
1421 Chars (Defining_Identifier (Analyzed_Formal));
1422 end case;
1424 Next (Analyzed_Formal);
1425 end loop;
1426 end Set_Analyzed_Formal;
1428 -- Start of processing for Analyze_Associations
1430 begin
1431 Actuals := Generic_Associations (I_Node);
1433 if Present (Actuals) then
1435 -- Check for an Others choice, indicating a partial parameterization
1436 -- for a formal package.
1438 Actual := First (Actuals);
1439 while Present (Actual) loop
1440 if Nkind (Actual) = N_Others_Choice then
1441 Others_Present := True;
1442 Others_Choice := Actual;
1444 if Present (Next (Actual)) then
1445 Error_Msg_N ("others must be last association", Actual);
1446 end if;
1448 -- This subprogram is used both for formal packages and for
1449 -- instantiations. For the latter, associations must all be
1450 -- explicit.
1452 if Nkind (I_Node) /= N_Formal_Package_Declaration
1453 and then Comes_From_Source (I_Node)
1454 then
1455 Error_Msg_N
1456 ("others association not allowed in an instance",
1457 Actual);
1458 end if;
1460 -- In any case, nothing to do after the others association
1462 exit;
1464 elsif Box_Present (Actual)
1465 and then Comes_From_Source (I_Node)
1466 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1467 then
1468 Error_Msg_N
1469 ("box association not allowed in an instance", Actual);
1470 end if;
1472 Next (Actual);
1473 end loop;
1475 -- If named associations are present, save first named association
1476 -- (it may of course be Empty) to facilitate subsequent name search.
1478 First_Named := First (Actuals);
1479 while Present (First_Named)
1480 and then Nkind (First_Named) /= N_Others_Choice
1481 and then No (Selector_Name (First_Named))
1482 loop
1483 Num_Actuals := Num_Actuals + 1;
1484 Next (First_Named);
1485 end loop;
1486 end if;
1488 Named := First_Named;
1489 while Present (Named) loop
1490 if Nkind (Named) /= N_Others_Choice
1491 and then No (Selector_Name (Named))
1492 then
1493 Error_Msg_N ("invalid positional actual after named one", Named);
1494 Abandon_Instantiation (Named);
1495 end if;
1497 -- A named association may lack an actual parameter, if it was
1498 -- introduced for a default subprogram that turns out to be local
1499 -- to the outer instantiation. If it has a box association it must
1500 -- correspond to some formal in the generic.
1502 if Nkind (Named) /= N_Others_Choice
1503 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1504 or else Box_Present (Named))
1505 then
1506 Num_Actuals := Num_Actuals + 1;
1507 end if;
1509 Next (Named);
1510 end loop;
1512 if Present (Formals) then
1513 Formal := First_Non_Pragma (Formals);
1514 Analyzed_Formal := First_Non_Pragma (F_Copy);
1516 if Present (Actuals) then
1517 Actual := First (Actuals);
1519 -- All formals should have default values
1521 else
1522 Actual := Empty;
1523 end if;
1525 while Present (Formal) loop
1526 Set_Analyzed_Formal;
1527 Saved_Formal := Next_Non_Pragma (Formal);
1529 case Nkind (Formal) is
1530 when N_Formal_Object_Declaration =>
1531 Match :=
1532 Matching_Actual
1533 (Defining_Identifier (Formal),
1534 Defining_Identifier (Analyzed_Formal));
1536 if No (Match) and then Partial_Parameterization then
1537 Process_Default (Formal);
1539 else
1540 Append_List
1541 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1542 Assoc);
1544 -- For a defaulted in_parameter, create an entry in the
1545 -- the list of defaulted actuals, for GNATProve use. Do
1546 -- not included these defaults for an instance nested
1547 -- within a generic, because the defaults are also used
1548 -- in the analysis of the enclosing generic, and only
1549 -- defaulted subprograms are relevant there.
1551 if No (Match) and then not Inside_A_Generic then
1552 Append_To (Default_Actuals,
1553 Make_Generic_Association (Sloc (I_Node),
1554 Selector_Name =>
1555 New_Occurrence_Of
1556 (Defining_Identifier (Formal), Sloc (I_Node)),
1557 Explicit_Generic_Actual_Parameter =>
1558 New_Copy_Tree (Default_Expression (Formal))));
1559 end if;
1560 end if;
1562 -- If the object is a call to an expression function, this
1563 -- is a freezing point for it.
1565 if Is_Entity_Name (Match)
1566 and then Present (Entity (Match))
1567 and then Nkind
1568 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1569 = N_Expression_Function
1570 then
1571 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1572 end if;
1574 when N_Formal_Type_Declaration =>
1575 Match :=
1576 Matching_Actual
1577 (Defining_Identifier (Formal),
1578 Defining_Identifier (Analyzed_Formal));
1580 if No (Match) then
1581 if Partial_Parameterization then
1582 Process_Default (Formal);
1584 else
1585 Error_Msg_Sloc := Sloc (Gen_Unit);
1586 Error_Msg_NE
1587 ("missing actual&",
1588 Instantiation_Node, Defining_Identifier (Formal));
1589 Error_Msg_NE
1590 ("\in instantiation of & declared#",
1591 Instantiation_Node, Gen_Unit);
1592 Abandon_Instantiation (Instantiation_Node);
1593 end if;
1595 else
1596 Analyze (Match);
1597 Append_List
1598 (Instantiate_Type
1599 (Formal, Match, Analyzed_Formal, Assoc),
1600 Assoc);
1602 -- An instantiation is a freeze point for the actuals,
1603 -- unless this is a rewritten formal package, or the
1604 -- formal is an Ada 2012 formal incomplete type.
1606 if Nkind (I_Node) = N_Formal_Package_Declaration
1607 or else
1608 (Ada_Version >= Ada_2012
1609 and then
1610 Ekind (Defining_Identifier (Analyzed_Formal)) =
1611 E_Incomplete_Type)
1612 then
1613 null;
1615 else
1616 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1617 end if;
1618 end if;
1620 -- A remote access-to-class-wide type is not a legal actual
1621 -- for a generic formal of an access type (E.2.2(17/2)).
1622 -- In GNAT an exception to this rule is introduced when
1623 -- the formal is marked as remote using implementation
1624 -- defined aspect/pragma Remote_Access_Type. In that case
1625 -- the actual must be remote as well.
1627 -- If the current instantiation is the construction of a
1628 -- local copy for a formal package the actuals may be
1629 -- defaulted, and there is no matching actual to check.
1631 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1632 and then
1633 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1634 N_Access_To_Object_Definition
1635 and then Present (Match)
1636 then
1637 declare
1638 Formal_Ent : constant Entity_Id :=
1639 Defining_Identifier (Analyzed_Formal);
1640 begin
1641 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1642 = Is_Remote_Types (Formal_Ent)
1643 then
1644 -- Remoteness of formal and actual match
1646 null;
1648 elsif Is_Remote_Types (Formal_Ent) then
1650 -- Remote formal, non-remote actual
1652 Error_Msg_NE
1653 ("actual for& must be remote", Match, Formal_Ent);
1655 else
1656 -- Non-remote formal, remote actual
1658 Error_Msg_NE
1659 ("actual for& may not be remote",
1660 Match, Formal_Ent);
1661 end if;
1662 end;
1663 end if;
1665 when N_Formal_Subprogram_Declaration =>
1666 Match :=
1667 Matching_Actual
1668 (Defining_Unit_Name (Specification (Formal)),
1669 Defining_Unit_Name (Specification (Analyzed_Formal)));
1671 -- If the formal subprogram has the same name as another
1672 -- formal subprogram of the generic, then a named
1673 -- association is illegal (12.3(9)). Exclude named
1674 -- associations that are generated for a nested instance.
1676 if Present (Match)
1677 and then Is_Named_Assoc
1678 and then Comes_From_Source (Found_Assoc)
1679 then
1680 Check_Overloaded_Formal_Subprogram (Formal);
1681 end if;
1683 -- If there is no corresponding actual, this may be case
1684 -- of partial parameterization, or else the formal has a
1685 -- default or a box.
1687 if No (Match) and then Partial_Parameterization then
1688 Process_Default (Formal);
1690 if Nkind (I_Node) = N_Formal_Package_Declaration then
1691 Check_Overloaded_Formal_Subprogram (Formal);
1692 end if;
1694 else
1695 Append_To (Assoc,
1696 Instantiate_Formal_Subprogram
1697 (Formal, Match, Analyzed_Formal));
1699 -- An instantiation is a freeze point for the actuals,
1700 -- unless this is a rewritten formal package.
1702 if Nkind (I_Node) /= N_Formal_Package_Declaration
1703 and then Nkind (Match) = N_Identifier
1704 and then Is_Subprogram (Entity (Match))
1706 -- The actual subprogram may rename a routine defined
1707 -- in Standard. Avoid freezing such renamings because
1708 -- subprograms coming from Standard cannot be frozen.
1710 and then
1711 not Renames_Standard_Subprogram (Entity (Match))
1713 -- If the actual subprogram comes from a different
1714 -- unit, it is already frozen, either by a body in
1715 -- that unit or by the end of the declarative part
1716 -- of the unit. This check avoids the freezing of
1717 -- subprograms defined in Standard which are used
1718 -- as generic actuals.
1720 and then In_Same_Code_Unit (Entity (Match), I_Node)
1721 and then Has_Fully_Defined_Profile (Entity (Match))
1722 then
1723 -- Mark the subprogram as having a delayed freeze
1724 -- since this may be an out-of-order action.
1726 Set_Has_Delayed_Freeze (Entity (Match));
1727 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1728 end if;
1729 end if;
1731 -- If this is a nested generic, preserve default for later
1732 -- instantiations. We do this as well for GNATProve use,
1733 -- so that the list of generic associations is complete.
1735 if No (Match) and then Box_Present (Formal) then
1736 declare
1737 Subp : constant Entity_Id :=
1738 Defining_Unit_Name (Specification (Last (Assoc)));
1740 begin
1741 Append_To (Default_Actuals,
1742 Make_Generic_Association (Sloc (I_Node),
1743 Selector_Name =>
1744 New_Occurrence_Of (Subp, Sloc (I_Node)),
1745 Explicit_Generic_Actual_Parameter =>
1746 New_Occurrence_Of (Subp, Sloc (I_Node))));
1747 end;
1748 end if;
1750 when N_Formal_Package_Declaration =>
1751 Match :=
1752 Matching_Actual
1753 (Defining_Identifier (Formal),
1754 Defining_Identifier (Original_Node (Analyzed_Formal)));
1756 if No (Match) then
1757 if Partial_Parameterization then
1758 Process_Default (Formal);
1760 else
1761 Error_Msg_Sloc := Sloc (Gen_Unit);
1762 Error_Msg_NE
1763 ("missing actual&",
1764 Instantiation_Node, Defining_Identifier (Formal));
1765 Error_Msg_NE
1766 ("\in instantiation of & declared#",
1767 Instantiation_Node, Gen_Unit);
1769 Abandon_Instantiation (Instantiation_Node);
1770 end if;
1772 else
1773 Analyze (Match);
1774 Append_List
1775 (Instantiate_Formal_Package
1776 (Formal, Match, Analyzed_Formal),
1777 Assoc);
1778 end if;
1780 -- For use type and use package appearing in the generic part,
1781 -- we have already copied them, so we can just move them where
1782 -- they belong (we mustn't recopy them since this would mess up
1783 -- the Sloc values).
1785 when N_Use_Package_Clause |
1786 N_Use_Type_Clause =>
1787 if Nkind (Original_Node (I_Node)) =
1788 N_Formal_Package_Declaration
1789 then
1790 Append (New_Copy_Tree (Formal), Assoc);
1791 else
1792 Remove (Formal);
1793 Append (Formal, Assoc);
1794 end if;
1796 when others =>
1797 raise Program_Error;
1799 end case;
1801 Formal := Saved_Formal;
1802 Next_Non_Pragma (Analyzed_Formal);
1803 end loop;
1805 if Num_Actuals > Num_Matched then
1806 Error_Msg_Sloc := Sloc (Gen_Unit);
1808 if Present (Selector_Name (Actual)) then
1809 Error_Msg_NE
1810 ("unmatched actual &", Actual, Selector_Name (Actual));
1811 Error_Msg_NE
1812 ("\in instantiation of & declared#", Actual, Gen_Unit);
1813 else
1814 Error_Msg_NE
1815 ("unmatched actual in instantiation of & declared#",
1816 Actual, Gen_Unit);
1817 end if;
1818 end if;
1820 elsif Present (Actuals) then
1821 Error_Msg_N
1822 ("too many actuals in generic instantiation", Instantiation_Node);
1823 end if;
1825 -- An instantiation freezes all generic actuals. The only exceptions
1826 -- to this are incomplete types and subprograms which are not fully
1827 -- defined at the point of instantiation.
1829 declare
1830 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1831 begin
1832 while Present (Elmt) loop
1833 Freeze_Before (I_Node, Node (Elmt));
1834 Next_Elmt (Elmt);
1835 end loop;
1836 end;
1838 -- If there are default subprograms, normalize the tree by adding
1839 -- explicit associations for them. This is required if the instance
1840 -- appears within a generic.
1842 if not Is_Empty_List (Default_Actuals) then
1843 declare
1844 Default : Node_Id;
1846 begin
1847 Default := First (Default_Actuals);
1848 while Present (Default) loop
1849 Mark_Rewrite_Insertion (Default);
1850 Next (Default);
1851 end loop;
1853 if No (Actuals) then
1854 Set_Generic_Associations (I_Node, Default_Actuals);
1855 else
1856 Append_List_To (Actuals, Default_Actuals);
1857 end if;
1858 end;
1859 end if;
1861 -- If this is a formal package, normalize the parameter list by adding
1862 -- explicit box associations for the formals that are covered by an
1863 -- Others_Choice.
1865 if not Is_Empty_List (Default_Formals) then
1866 Append_List (Default_Formals, Formals);
1867 end if;
1869 return Assoc;
1870 end Analyze_Associations;
1872 -------------------------------
1873 -- Analyze_Formal_Array_Type --
1874 -------------------------------
1876 procedure Analyze_Formal_Array_Type
1877 (T : in out Entity_Id;
1878 Def : Node_Id)
1880 DSS : Node_Id;
1882 begin
1883 -- Treated like a non-generic array declaration, with additional
1884 -- semantic checks.
1886 Enter_Name (T);
1888 if Nkind (Def) = N_Constrained_Array_Definition then
1889 DSS := First (Discrete_Subtype_Definitions (Def));
1890 while Present (DSS) loop
1891 if Nkind_In (DSS, N_Subtype_Indication,
1892 N_Range,
1893 N_Attribute_Reference)
1894 then
1895 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1896 end if;
1898 Next (DSS);
1899 end loop;
1900 end if;
1902 Array_Type_Declaration (T, Def);
1903 Set_Is_Generic_Type (Base_Type (T));
1905 if Ekind (Component_Type (T)) = E_Incomplete_Type
1906 and then No (Full_View (Component_Type (T)))
1907 then
1908 Error_Msg_N ("premature usage of incomplete type", Def);
1910 -- Check that range constraint is not allowed on the component type
1911 -- of a generic formal array type (AARM 12.5.3(3))
1913 elsif Is_Internal (Component_Type (T))
1914 and then Present (Subtype_Indication (Component_Definition (Def)))
1915 and then Nkind (Original_Node
1916 (Subtype_Indication (Component_Definition (Def)))) =
1917 N_Subtype_Indication
1918 then
1919 Error_Msg_N
1920 ("in a formal, a subtype indication can only be "
1921 & "a subtype mark (RM 12.5.3(3))",
1922 Subtype_Indication (Component_Definition (Def)));
1923 end if;
1925 end Analyze_Formal_Array_Type;
1927 ---------------------------------------------
1928 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1929 ---------------------------------------------
1931 -- As for other generic types, we create a valid type representation with
1932 -- legal but arbitrary attributes, whose values are never considered
1933 -- static. For all scalar types we introduce an anonymous base type, with
1934 -- the same attributes. We choose the corresponding integer type to be
1935 -- Standard_Integer.
1936 -- Here and in other similar routines, the Sloc of the generated internal
1937 -- type must be the same as the sloc of the defining identifier of the
1938 -- formal type declaration, to provide proper source navigation.
1940 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1941 (T : Entity_Id;
1942 Def : Node_Id)
1944 Loc : constant Source_Ptr := Sloc (Def);
1946 Base : constant Entity_Id :=
1947 New_Internal_Entity
1948 (E_Decimal_Fixed_Point_Type,
1949 Current_Scope,
1950 Sloc (Defining_Identifier (Parent (Def))), 'G');
1952 Int_Base : constant Entity_Id := Standard_Integer;
1953 Delta_Val : constant Ureal := Ureal_1;
1954 Digs_Val : constant Uint := Uint_6;
1956 function Make_Dummy_Bound return Node_Id;
1957 -- Return a properly typed universal real literal to use as a bound
1959 ----------------------
1960 -- Make_Dummy_Bound --
1961 ----------------------
1963 function Make_Dummy_Bound return Node_Id is
1964 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1965 begin
1966 Set_Etype (Bound, Universal_Real);
1967 return Bound;
1968 end Make_Dummy_Bound;
1970 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1972 begin
1973 Enter_Name (T);
1975 Set_Etype (Base, Base);
1976 Set_Size_Info (Base, Int_Base);
1977 Set_RM_Size (Base, RM_Size (Int_Base));
1978 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1979 Set_Digits_Value (Base, Digs_Val);
1980 Set_Delta_Value (Base, Delta_Val);
1981 Set_Small_Value (Base, Delta_Val);
1982 Set_Scalar_Range (Base,
1983 Make_Range (Loc,
1984 Low_Bound => Make_Dummy_Bound,
1985 High_Bound => Make_Dummy_Bound));
1987 Set_Is_Generic_Type (Base);
1988 Set_Parent (Base, Parent (Def));
1990 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1991 Set_Etype (T, Base);
1992 Set_Size_Info (T, Int_Base);
1993 Set_RM_Size (T, RM_Size (Int_Base));
1994 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1995 Set_Digits_Value (T, Digs_Val);
1996 Set_Delta_Value (T, Delta_Val);
1997 Set_Small_Value (T, Delta_Val);
1998 Set_Scalar_Range (T, Scalar_Range (Base));
1999 Set_Is_Constrained (T);
2001 Check_Restriction (No_Fixed_Point, Def);
2002 end Analyze_Formal_Decimal_Fixed_Point_Type;
2004 -------------------------------------------
2005 -- Analyze_Formal_Derived_Interface_Type --
2006 -------------------------------------------
2008 procedure Analyze_Formal_Derived_Interface_Type
2009 (N : Node_Id;
2010 T : Entity_Id;
2011 Def : Node_Id)
2013 Loc : constant Source_Ptr := Sloc (Def);
2015 begin
2016 -- Rewrite as a type declaration of a derived type. This ensures that
2017 -- the interface list and primitive operations are properly captured.
2019 Rewrite (N,
2020 Make_Full_Type_Declaration (Loc,
2021 Defining_Identifier => T,
2022 Type_Definition => Def));
2023 Analyze (N);
2024 Set_Is_Generic_Type (T);
2025 end Analyze_Formal_Derived_Interface_Type;
2027 ---------------------------------
2028 -- Analyze_Formal_Derived_Type --
2029 ---------------------------------
2031 procedure Analyze_Formal_Derived_Type
2032 (N : Node_Id;
2033 T : Entity_Id;
2034 Def : Node_Id)
2036 Loc : constant Source_Ptr := Sloc (Def);
2037 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2038 New_N : Node_Id;
2040 begin
2041 Set_Is_Generic_Type (T);
2043 if Private_Present (Def) then
2044 New_N :=
2045 Make_Private_Extension_Declaration (Loc,
2046 Defining_Identifier => T,
2047 Discriminant_Specifications => Discriminant_Specifications (N),
2048 Unknown_Discriminants_Present => Unk_Disc,
2049 Subtype_Indication => Subtype_Mark (Def),
2050 Interface_List => Interface_List (Def));
2052 Set_Abstract_Present (New_N, Abstract_Present (Def));
2053 Set_Limited_Present (New_N, Limited_Present (Def));
2054 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2056 else
2057 New_N :=
2058 Make_Full_Type_Declaration (Loc,
2059 Defining_Identifier => T,
2060 Discriminant_Specifications =>
2061 Discriminant_Specifications (Parent (T)),
2062 Type_Definition =>
2063 Make_Derived_Type_Definition (Loc,
2064 Subtype_Indication => Subtype_Mark (Def)));
2066 Set_Abstract_Present
2067 (Type_Definition (New_N), Abstract_Present (Def));
2068 Set_Limited_Present
2069 (Type_Definition (New_N), Limited_Present (Def));
2070 end if;
2072 Rewrite (N, New_N);
2073 Analyze (N);
2075 if Unk_Disc then
2076 if not Is_Composite_Type (T) then
2077 Error_Msg_N
2078 ("unknown discriminants not allowed for elementary types", N);
2079 else
2080 Set_Has_Unknown_Discriminants (T);
2081 Set_Is_Constrained (T, False);
2082 end if;
2083 end if;
2085 -- If the parent type has a known size, so does the formal, which makes
2086 -- legal representation clauses that involve the formal.
2088 Set_Size_Known_At_Compile_Time
2089 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2090 end Analyze_Formal_Derived_Type;
2092 ----------------------------------
2093 -- Analyze_Formal_Discrete_Type --
2094 ----------------------------------
2096 -- The operations defined for a discrete types are those of an enumeration
2097 -- type. The size is set to an arbitrary value, for use in analyzing the
2098 -- generic unit.
2100 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2101 Loc : constant Source_Ptr := Sloc (Def);
2102 Lo : Node_Id;
2103 Hi : Node_Id;
2105 Base : constant Entity_Id :=
2106 New_Internal_Entity
2107 (E_Floating_Point_Type, Current_Scope,
2108 Sloc (Defining_Identifier (Parent (Def))), 'G');
2110 begin
2111 Enter_Name (T);
2112 Set_Ekind (T, E_Enumeration_Subtype);
2113 Set_Etype (T, Base);
2114 Init_Size (T, 8);
2115 Init_Alignment (T);
2116 Set_Is_Generic_Type (T);
2117 Set_Is_Constrained (T);
2119 -- For semantic analysis, the bounds of the type must be set to some
2120 -- non-static value. The simplest is to create attribute nodes for those
2121 -- bounds, that refer to the type itself. These bounds are never
2122 -- analyzed but serve as place-holders.
2124 Lo :=
2125 Make_Attribute_Reference (Loc,
2126 Attribute_Name => Name_First,
2127 Prefix => New_Occurrence_Of (T, Loc));
2128 Set_Etype (Lo, T);
2130 Hi :=
2131 Make_Attribute_Reference (Loc,
2132 Attribute_Name => Name_Last,
2133 Prefix => New_Occurrence_Of (T, Loc));
2134 Set_Etype (Hi, T);
2136 Set_Scalar_Range (T,
2137 Make_Range (Loc,
2138 Low_Bound => Lo,
2139 High_Bound => Hi));
2141 Set_Ekind (Base, E_Enumeration_Type);
2142 Set_Etype (Base, Base);
2143 Init_Size (Base, 8);
2144 Init_Alignment (Base);
2145 Set_Is_Generic_Type (Base);
2146 Set_Scalar_Range (Base, Scalar_Range (T));
2147 Set_Parent (Base, Parent (Def));
2148 end Analyze_Formal_Discrete_Type;
2150 ----------------------------------
2151 -- Analyze_Formal_Floating_Type --
2152 ---------------------------------
2154 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2155 Base : constant Entity_Id :=
2156 New_Internal_Entity
2157 (E_Floating_Point_Type, Current_Scope,
2158 Sloc (Defining_Identifier (Parent (Def))), 'G');
2160 begin
2161 -- The various semantic attributes are taken from the predefined type
2162 -- Float, just so that all of them are initialized. Their values are
2163 -- never used because no constant folding or expansion takes place in
2164 -- the generic itself.
2166 Enter_Name (T);
2167 Set_Ekind (T, E_Floating_Point_Subtype);
2168 Set_Etype (T, Base);
2169 Set_Size_Info (T, (Standard_Float));
2170 Set_RM_Size (T, RM_Size (Standard_Float));
2171 Set_Digits_Value (T, Digits_Value (Standard_Float));
2172 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2173 Set_Is_Constrained (T);
2175 Set_Is_Generic_Type (Base);
2176 Set_Etype (Base, Base);
2177 Set_Size_Info (Base, (Standard_Float));
2178 Set_RM_Size (Base, RM_Size (Standard_Float));
2179 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2180 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2181 Set_Parent (Base, Parent (Def));
2183 Check_Restriction (No_Floating_Point, Def);
2184 end Analyze_Formal_Floating_Type;
2186 -----------------------------------
2187 -- Analyze_Formal_Interface_Type;--
2188 -----------------------------------
2190 procedure Analyze_Formal_Interface_Type
2191 (N : Node_Id;
2192 T : Entity_Id;
2193 Def : Node_Id)
2195 Loc : constant Source_Ptr := Sloc (N);
2196 New_N : Node_Id;
2198 begin
2199 New_N :=
2200 Make_Full_Type_Declaration (Loc,
2201 Defining_Identifier => T,
2202 Type_Definition => Def);
2204 Rewrite (N, New_N);
2205 Analyze (N);
2206 Set_Is_Generic_Type (T);
2207 end Analyze_Formal_Interface_Type;
2209 ---------------------------------
2210 -- Analyze_Formal_Modular_Type --
2211 ---------------------------------
2213 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2214 begin
2215 -- Apart from their entity kind, generic modular types are treated like
2216 -- signed integer types, and have the same attributes.
2218 Analyze_Formal_Signed_Integer_Type (T, Def);
2219 Set_Ekind (T, E_Modular_Integer_Subtype);
2220 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2222 end Analyze_Formal_Modular_Type;
2224 ---------------------------------------
2225 -- Analyze_Formal_Object_Declaration --
2226 ---------------------------------------
2228 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2229 E : constant Node_Id := Default_Expression (N);
2230 Id : constant Node_Id := Defining_Identifier (N);
2231 K : Entity_Kind;
2232 T : Node_Id;
2234 begin
2235 Enter_Name (Id);
2237 -- Determine the mode of the formal object
2239 if Out_Present (N) then
2240 K := E_Generic_In_Out_Parameter;
2242 if not In_Present (N) then
2243 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2244 end if;
2246 else
2247 K := E_Generic_In_Parameter;
2248 end if;
2250 if Present (Subtype_Mark (N)) then
2251 Find_Type (Subtype_Mark (N));
2252 T := Entity (Subtype_Mark (N));
2254 -- Verify that there is no redundant null exclusion
2256 if Null_Exclusion_Present (N) then
2257 if not Is_Access_Type (T) then
2258 Error_Msg_N
2259 ("null exclusion can only apply to an access type", N);
2261 elsif Can_Never_Be_Null (T) then
2262 Error_Msg_NE
2263 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2264 end if;
2265 end if;
2267 -- Ada 2005 (AI-423): Formal object with an access definition
2269 else
2270 Check_Access_Definition (N);
2271 T := Access_Definition
2272 (Related_Nod => N,
2273 N => Access_Definition (N));
2274 end if;
2276 if Ekind (T) = E_Incomplete_Type then
2277 declare
2278 Error_Node : Node_Id;
2280 begin
2281 if Present (Subtype_Mark (N)) then
2282 Error_Node := Subtype_Mark (N);
2283 else
2284 Check_Access_Definition (N);
2285 Error_Node := Access_Definition (N);
2286 end if;
2288 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2289 end;
2290 end if;
2292 if K = E_Generic_In_Parameter then
2294 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2296 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2297 Error_Msg_N
2298 ("generic formal of mode IN must not be of limited type", N);
2299 Explain_Limited_Type (T, N);
2300 end if;
2302 if Is_Abstract_Type (T) then
2303 Error_Msg_N
2304 ("generic formal of mode IN must not be of abstract type", N);
2305 end if;
2307 if Present (E) then
2308 Preanalyze_Spec_Expression (E, T);
2310 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2311 Error_Msg_N
2312 ("initialization not allowed for limited types", E);
2313 Explain_Limited_Type (T, E);
2314 end if;
2315 end if;
2317 Set_Ekind (Id, K);
2318 Set_Etype (Id, T);
2320 -- Case of generic IN OUT parameter
2322 else
2323 -- If the formal has an unconstrained type, construct its actual
2324 -- subtype, as is done for subprogram formals. In this fashion, all
2325 -- its uses can refer to specific bounds.
2327 Set_Ekind (Id, K);
2328 Set_Etype (Id, T);
2330 if (Is_Array_Type (T) and then not Is_Constrained (T))
2331 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2332 then
2333 declare
2334 Non_Freezing_Ref : constant Node_Id :=
2335 New_Occurrence_Of (Id, Sloc (Id));
2336 Decl : Node_Id;
2338 begin
2339 -- Make sure the actual subtype doesn't generate bogus freezing
2341 Set_Must_Not_Freeze (Non_Freezing_Ref);
2342 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2343 Insert_Before_And_Analyze (N, Decl);
2344 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2345 end;
2346 else
2347 Set_Actual_Subtype (Id, T);
2348 end if;
2350 if Present (E) then
2351 Error_Msg_N
2352 ("initialization not allowed for `IN OUT` formals", N);
2353 end if;
2354 end if;
2356 if Has_Aspects (N) then
2357 Analyze_Aspect_Specifications (N, Id);
2358 end if;
2359 end Analyze_Formal_Object_Declaration;
2361 ----------------------------------------------
2362 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2363 ----------------------------------------------
2365 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2366 (T : Entity_Id;
2367 Def : Node_Id)
2369 Loc : constant Source_Ptr := Sloc (Def);
2370 Base : constant Entity_Id :=
2371 New_Internal_Entity
2372 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2373 Sloc (Defining_Identifier (Parent (Def))), 'G');
2375 begin
2376 -- The semantic attributes are set for completeness only, their values
2377 -- will never be used, since all properties of the type are non-static.
2379 Enter_Name (T);
2380 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2381 Set_Etype (T, Base);
2382 Set_Size_Info (T, Standard_Integer);
2383 Set_RM_Size (T, RM_Size (Standard_Integer));
2384 Set_Small_Value (T, Ureal_1);
2385 Set_Delta_Value (T, Ureal_1);
2386 Set_Scalar_Range (T,
2387 Make_Range (Loc,
2388 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2389 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2390 Set_Is_Constrained (T);
2392 Set_Is_Generic_Type (Base);
2393 Set_Etype (Base, Base);
2394 Set_Size_Info (Base, Standard_Integer);
2395 Set_RM_Size (Base, RM_Size (Standard_Integer));
2396 Set_Small_Value (Base, Ureal_1);
2397 Set_Delta_Value (Base, Ureal_1);
2398 Set_Scalar_Range (Base, Scalar_Range (T));
2399 Set_Parent (Base, Parent (Def));
2401 Check_Restriction (No_Fixed_Point, Def);
2402 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2404 ----------------------------------------
2405 -- Analyze_Formal_Package_Declaration --
2406 ----------------------------------------
2408 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2409 Gen_Id : constant Node_Id := Name (N);
2410 Loc : constant Source_Ptr := Sloc (N);
2411 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2412 Formal : Entity_Id;
2413 Gen_Decl : Node_Id;
2414 Gen_Unit : Entity_Id;
2415 Renaming : Node_Id;
2417 Vis_Prims_List : Elist_Id := No_Elist;
2418 -- List of primitives made temporarily visible in the instantiation
2419 -- to match the visibility of the formal type.
2421 function Build_Local_Package return Node_Id;
2422 -- The formal package is rewritten so that its parameters are replaced
2423 -- with corresponding declarations. For parameters with bona fide
2424 -- associations these declarations are created by Analyze_Associations
2425 -- as for a regular instantiation. For boxed parameters, we preserve
2426 -- the formal declarations and analyze them, in order to introduce
2427 -- entities of the right kind in the environment of the formal.
2429 -------------------------
2430 -- Build_Local_Package --
2431 -------------------------
2433 function Build_Local_Package return Node_Id is
2434 Decls : List_Id;
2435 Pack_Decl : Node_Id;
2437 begin
2438 -- Within the formal, the name of the generic package is a renaming
2439 -- of the formal (as for a regular instantiation).
2441 Pack_Decl :=
2442 Make_Package_Declaration (Loc,
2443 Specification =>
2444 Copy_Generic_Node
2445 (Specification (Original_Node (Gen_Decl)),
2446 Empty, Instantiating => True));
2448 Renaming :=
2449 Make_Package_Renaming_Declaration (Loc,
2450 Defining_Unit_Name =>
2451 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2452 Name => New_Occurrence_Of (Formal, Loc));
2454 if Nkind (Gen_Id) = N_Identifier
2455 and then Chars (Gen_Id) = Chars (Pack_Id)
2456 then
2457 Error_Msg_NE
2458 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2459 end if;
2461 -- If the formal is declared with a box, or with an others choice,
2462 -- create corresponding declarations for all entities in the formal
2463 -- part, so that names with the proper types are available in the
2464 -- specification of the formal package.
2466 -- On the other hand, if there are no associations, then all the
2467 -- formals must have defaults, and this will be checked by the
2468 -- call to Analyze_Associations.
2470 if Box_Present (N)
2471 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2472 then
2473 declare
2474 Formal_Decl : Node_Id;
2476 begin
2477 -- TBA : for a formal package, need to recurse ???
2479 Decls := New_List;
2480 Formal_Decl :=
2481 First
2482 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2483 while Present (Formal_Decl) loop
2484 Append_To
2485 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2486 Next (Formal_Decl);
2487 end loop;
2488 end;
2490 -- If generic associations are present, use Analyze_Associations to
2491 -- create the proper renaming declarations.
2493 else
2494 declare
2495 Act_Tree : constant Node_Id :=
2496 Copy_Generic_Node
2497 (Original_Node (Gen_Decl), Empty,
2498 Instantiating => True);
2500 begin
2501 Generic_Renamings.Set_Last (0);
2502 Generic_Renamings_HTable.Reset;
2503 Instantiation_Node := N;
2505 Decls :=
2506 Analyze_Associations
2507 (I_Node => Original_Node (N),
2508 Formals => Generic_Formal_Declarations (Act_Tree),
2509 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2511 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2512 end;
2513 end if;
2515 Append (Renaming, To => Decls);
2517 -- Add generated declarations ahead of local declarations in
2518 -- the package.
2520 if No (Visible_Declarations (Specification (Pack_Decl))) then
2521 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2522 else
2523 Insert_List_Before
2524 (First (Visible_Declarations (Specification (Pack_Decl))),
2525 Decls);
2526 end if;
2528 return Pack_Decl;
2529 end Build_Local_Package;
2531 -- Local variables
2533 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2534 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2536 Associations : Boolean := True;
2537 New_N : Node_Id;
2538 Parent_Installed : Boolean := False;
2539 Parent_Instance : Entity_Id;
2540 Renaming_In_Par : Entity_Id;
2542 -- Start of processing for Analyze_Formal_Package_Declaration
2544 begin
2545 Check_Text_IO_Special_Unit (Gen_Id);
2547 Init_Env;
2548 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2549 Gen_Unit := Entity (Gen_Id);
2551 -- Check for a formal package that is a package renaming
2553 if Present (Renamed_Object (Gen_Unit)) then
2555 -- Indicate that unit is used, before replacing it with renamed
2556 -- entity for use below.
2558 if In_Extended_Main_Source_Unit (N) then
2559 Set_Is_Instantiated (Gen_Unit);
2560 Generate_Reference (Gen_Unit, N);
2561 end if;
2563 Gen_Unit := Renamed_Object (Gen_Unit);
2564 end if;
2566 if Ekind (Gen_Unit) /= E_Generic_Package then
2567 Error_Msg_N ("expect generic package name", Gen_Id);
2568 Restore_Env;
2569 goto Leave;
2571 elsif Gen_Unit = Current_Scope then
2572 Error_Msg_N
2573 ("generic package cannot be used as a formal package of itself",
2574 Gen_Id);
2575 Restore_Env;
2576 goto Leave;
2578 elsif In_Open_Scopes (Gen_Unit) then
2579 if Is_Compilation_Unit (Gen_Unit)
2580 and then Is_Child_Unit (Current_Scope)
2581 then
2582 -- Special-case the error when the formal is a parent, and
2583 -- continue analysis to minimize cascaded errors.
2585 Error_Msg_N
2586 ("generic parent cannot be used as formal package "
2587 & "of a child unit", Gen_Id);
2589 else
2590 Error_Msg_N
2591 ("generic package cannot be used as a formal package "
2592 & "within itself", Gen_Id);
2593 Restore_Env;
2594 goto Leave;
2595 end if;
2596 end if;
2598 -- Check that name of formal package does not hide name of generic,
2599 -- or its leading prefix. This check must be done separately because
2600 -- the name of the generic has already been analyzed.
2602 declare
2603 Gen_Name : Entity_Id;
2605 begin
2606 Gen_Name := Gen_Id;
2607 while Nkind (Gen_Name) = N_Expanded_Name loop
2608 Gen_Name := Prefix (Gen_Name);
2609 end loop;
2611 if Chars (Gen_Name) = Chars (Pack_Id) then
2612 Error_Msg_NE
2613 ("& is hidden within declaration of formal package",
2614 Gen_Id, Gen_Name);
2615 end if;
2616 end;
2618 if Box_Present (N)
2619 or else No (Generic_Associations (N))
2620 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2621 then
2622 Associations := False;
2623 end if;
2625 -- If there are no generic associations, the generic parameters appear
2626 -- as local entities and are instantiated like them. We copy the generic
2627 -- package declaration as if it were an instantiation, and analyze it
2628 -- like a regular package, except that we treat the formals as
2629 -- additional visible components.
2631 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2633 if In_Extended_Main_Source_Unit (N) then
2634 Set_Is_Instantiated (Gen_Unit);
2635 Generate_Reference (Gen_Unit, N);
2636 end if;
2638 Formal := New_Copy (Pack_Id);
2639 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2641 -- Make local generic without formals. The formals will be replaced with
2642 -- internal declarations.
2644 begin
2645 New_N := Build_Local_Package;
2647 -- If there are errors in the parameter list, Analyze_Associations
2648 -- raises Instantiation_Error. Patch the declaration to prevent further
2649 -- exception propagation.
2651 exception
2652 when Instantiation_Error =>
2653 Enter_Name (Formal);
2654 Set_Ekind (Formal, E_Variable);
2655 Set_Etype (Formal, Any_Type);
2656 Restore_Hidden_Primitives (Vis_Prims_List);
2658 if Parent_Installed then
2659 Remove_Parent;
2660 end if;
2662 goto Leave;
2663 end;
2665 Rewrite (N, New_N);
2666 Set_Defining_Unit_Name (Specification (New_N), Formal);
2667 Set_Generic_Parent (Specification (N), Gen_Unit);
2668 Set_Instance_Env (Gen_Unit, Formal);
2669 Set_Is_Generic_Instance (Formal);
2671 Enter_Name (Formal);
2672 Set_Ekind (Formal, E_Package);
2673 Set_Etype (Formal, Standard_Void_Type);
2674 Set_Inner_Instances (Formal, New_Elmt_List);
2675 Push_Scope (Formal);
2677 -- Manually set the SPARK_Mode from the context because the package
2678 -- declaration is never analyzed.
2680 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2681 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2682 Set_SPARK_Pragma_Inherited (Formal);
2683 Set_SPARK_Aux_Pragma_Inherited (Formal);
2685 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2687 -- Similarly, we have to make the name of the formal visible in the
2688 -- parent instance, to resolve properly fully qualified names that
2689 -- may appear in the generic unit. The parent instance has been
2690 -- placed on the scope stack ahead of the current scope.
2692 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2694 Renaming_In_Par :=
2695 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2696 Set_Ekind (Renaming_In_Par, E_Package);
2697 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2698 Set_Scope (Renaming_In_Par, Parent_Instance);
2699 Set_Parent (Renaming_In_Par, Parent (Formal));
2700 Set_Renamed_Object (Renaming_In_Par, Formal);
2701 Append_Entity (Renaming_In_Par, Parent_Instance);
2702 end if;
2704 -- A formal package declaration behaves as a package instantiation with
2705 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2706 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2707 -- all SPARK_Mode pragmas within the generic_package_name.
2709 if SPARK_Mode /= On then
2710 Ignore_Pragma_SPARK_Mode := True;
2711 end if;
2713 Analyze (Specification (N));
2715 -- The formals for which associations are provided are not visible
2716 -- outside of the formal package. The others are still declared by a
2717 -- formal parameter declaration.
2719 -- If there are no associations, the only local entity to hide is the
2720 -- generated package renaming itself.
2722 declare
2723 E : Entity_Id;
2725 begin
2726 E := First_Entity (Formal);
2727 while Present (E) loop
2728 if Associations and then not Is_Generic_Formal (E) then
2729 Set_Is_Hidden (E);
2730 end if;
2732 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2733 Set_Is_Hidden (E);
2734 exit;
2735 end if;
2737 Next_Entity (E);
2738 end loop;
2739 end;
2741 End_Package_Scope (Formal);
2742 Restore_Hidden_Primitives (Vis_Prims_List);
2744 if Parent_Installed then
2745 Remove_Parent;
2746 end if;
2748 Restore_Env;
2750 -- Inside the generic unit, the formal package is a regular package, but
2751 -- no body is needed for it. Note that after instantiation, the defining
2752 -- unit name we need is in the new tree and not in the original (see
2753 -- Package_Instantiation). A generic formal package is an instance, and
2754 -- can be used as an actual for an inner instance.
2756 Set_Has_Completion (Formal, True);
2758 -- Add semantic information to the original defining identifier for ASIS
2759 -- use.
2761 Set_Ekind (Pack_Id, E_Package);
2762 Set_Etype (Pack_Id, Standard_Void_Type);
2763 Set_Scope (Pack_Id, Scope (Formal));
2764 Set_Has_Completion (Pack_Id, True);
2766 <<Leave>>
2767 if Has_Aspects (N) then
2768 Analyze_Aspect_Specifications (N, Pack_Id);
2769 end if;
2771 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2772 end Analyze_Formal_Package_Declaration;
2774 ---------------------------------
2775 -- Analyze_Formal_Private_Type --
2776 ---------------------------------
2778 procedure Analyze_Formal_Private_Type
2779 (N : Node_Id;
2780 T : Entity_Id;
2781 Def : Node_Id)
2783 begin
2784 New_Private_Type (N, T, Def);
2786 -- Set the size to an arbitrary but legal value
2788 Set_Size_Info (T, Standard_Integer);
2789 Set_RM_Size (T, RM_Size (Standard_Integer));
2790 end Analyze_Formal_Private_Type;
2792 ------------------------------------
2793 -- Analyze_Formal_Incomplete_Type --
2794 ------------------------------------
2796 procedure Analyze_Formal_Incomplete_Type
2797 (T : Entity_Id;
2798 Def : Node_Id)
2800 begin
2801 Enter_Name (T);
2802 Set_Ekind (T, E_Incomplete_Type);
2803 Set_Etype (T, T);
2804 Set_Private_Dependents (T, New_Elmt_List);
2806 if Tagged_Present (Def) then
2807 Set_Is_Tagged_Type (T);
2808 Make_Class_Wide_Type (T);
2809 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2810 end if;
2811 end Analyze_Formal_Incomplete_Type;
2813 ----------------------------------------
2814 -- Analyze_Formal_Signed_Integer_Type --
2815 ----------------------------------------
2817 procedure Analyze_Formal_Signed_Integer_Type
2818 (T : Entity_Id;
2819 Def : Node_Id)
2821 Base : constant Entity_Id :=
2822 New_Internal_Entity
2823 (E_Signed_Integer_Type,
2824 Current_Scope,
2825 Sloc (Defining_Identifier (Parent (Def))), 'G');
2827 begin
2828 Enter_Name (T);
2830 Set_Ekind (T, E_Signed_Integer_Subtype);
2831 Set_Etype (T, Base);
2832 Set_Size_Info (T, Standard_Integer);
2833 Set_RM_Size (T, RM_Size (Standard_Integer));
2834 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2835 Set_Is_Constrained (T);
2837 Set_Is_Generic_Type (Base);
2838 Set_Size_Info (Base, Standard_Integer);
2839 Set_RM_Size (Base, RM_Size (Standard_Integer));
2840 Set_Etype (Base, Base);
2841 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2842 Set_Parent (Base, Parent (Def));
2843 end Analyze_Formal_Signed_Integer_Type;
2845 -------------------------------------------
2846 -- Analyze_Formal_Subprogram_Declaration --
2847 -------------------------------------------
2849 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2850 Spec : constant Node_Id := Specification (N);
2851 Def : constant Node_Id := Default_Name (N);
2852 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2853 Subp : Entity_Id;
2855 begin
2856 if Nam = Error then
2857 return;
2858 end if;
2860 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2861 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2862 goto Leave;
2863 end if;
2865 Analyze_Subprogram_Declaration (N);
2866 Set_Is_Formal_Subprogram (Nam);
2867 Set_Has_Completion (Nam);
2869 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2870 Set_Is_Abstract_Subprogram (Nam);
2872 Set_Is_Dispatching_Operation (Nam);
2874 -- A formal abstract procedure cannot have a null default
2875 -- (RM 12.6(4.1/2)).
2877 if Nkind (Spec) = N_Procedure_Specification
2878 and then Null_Present (Spec)
2879 then
2880 Error_Msg_N
2881 ("a formal abstract subprogram cannot default to null", Spec);
2882 end if;
2884 declare
2885 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2886 begin
2887 if No (Ctrl_Type) then
2888 Error_Msg_N
2889 ("abstract formal subprogram must have a controlling type",
2892 elsif Ada_Version >= Ada_2012
2893 and then Is_Incomplete_Type (Ctrl_Type)
2894 then
2895 Error_Msg_NE
2896 ("controlling type of abstract formal subprogram cannot "
2897 & "be incomplete type", N, Ctrl_Type);
2899 else
2900 Check_Controlling_Formals (Ctrl_Type, Nam);
2901 end if;
2902 end;
2903 end if;
2905 -- Default name is resolved at the point of instantiation
2907 if Box_Present (N) then
2908 null;
2910 -- Else default is bound at the point of generic declaration
2912 elsif Present (Def) then
2913 if Nkind (Def) = N_Operator_Symbol then
2914 Find_Direct_Name (Def);
2916 elsif Nkind (Def) /= N_Attribute_Reference then
2917 Analyze (Def);
2919 else
2920 -- For an attribute reference, analyze the prefix and verify
2921 -- that it has the proper profile for the subprogram.
2923 Analyze (Prefix (Def));
2924 Valid_Default_Attribute (Nam, Def);
2925 goto Leave;
2926 end if;
2928 -- Default name may be overloaded, in which case the interpretation
2929 -- with the correct profile must be selected, as for a renaming.
2930 -- If the definition is an indexed component, it must denote a
2931 -- member of an entry family. If it is a selected component, it
2932 -- can be a protected operation.
2934 if Etype (Def) = Any_Type then
2935 goto Leave;
2937 elsif Nkind (Def) = N_Selected_Component then
2938 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2939 Error_Msg_N ("expect valid subprogram name as default", Def);
2940 end if;
2942 elsif Nkind (Def) = N_Indexed_Component then
2943 if Is_Entity_Name (Prefix (Def)) then
2944 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2945 Error_Msg_N ("expect valid subprogram name as default", Def);
2946 end if;
2948 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2949 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2950 E_Entry_Family
2951 then
2952 Error_Msg_N ("expect valid subprogram name as default", Def);
2953 end if;
2955 else
2956 Error_Msg_N ("expect valid subprogram name as default", Def);
2957 goto Leave;
2958 end if;
2960 elsif Nkind (Def) = N_Character_Literal then
2962 -- Needs some type checks: subprogram should be parameterless???
2964 Resolve (Def, (Etype (Nam)));
2966 elsif not Is_Entity_Name (Def)
2967 or else not Is_Overloadable (Entity (Def))
2968 then
2969 Error_Msg_N ("expect valid subprogram name as default", Def);
2970 goto Leave;
2972 elsif not Is_Overloaded (Def) then
2973 Subp := Entity (Def);
2975 if Subp = Nam then
2976 Error_Msg_N ("premature usage of formal subprogram", Def);
2978 elsif not Entity_Matches_Spec (Subp, Nam) then
2979 Error_Msg_N ("no visible entity matches specification", Def);
2980 end if;
2982 -- More than one interpretation, so disambiguate as for a renaming
2984 else
2985 declare
2986 I : Interp_Index;
2987 I1 : Interp_Index := 0;
2988 It : Interp;
2989 It1 : Interp;
2991 begin
2992 Subp := Any_Id;
2993 Get_First_Interp (Def, I, It);
2994 while Present (It.Nam) loop
2995 if Entity_Matches_Spec (It.Nam, Nam) then
2996 if Subp /= Any_Id then
2997 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2999 if It1 = No_Interp then
3000 Error_Msg_N ("ambiguous default subprogram", Def);
3001 else
3002 Subp := It1.Nam;
3003 end if;
3005 exit;
3007 else
3008 I1 := I;
3009 Subp := It.Nam;
3010 end if;
3011 end if;
3013 Get_Next_Interp (I, It);
3014 end loop;
3015 end;
3017 if Subp /= Any_Id then
3019 -- Subprogram found, generate reference to it
3021 Set_Entity (Def, Subp);
3022 Generate_Reference (Subp, Def);
3024 if Subp = Nam then
3025 Error_Msg_N ("premature usage of formal subprogram", Def);
3027 elsif Ekind (Subp) /= E_Operator then
3028 Check_Mode_Conformant (Subp, Nam);
3029 end if;
3031 else
3032 Error_Msg_N ("no visible subprogram matches specification", N);
3033 end if;
3034 end if;
3035 end if;
3037 <<Leave>>
3038 if Has_Aspects (N) then
3039 Analyze_Aspect_Specifications (N, Nam);
3040 end if;
3042 end Analyze_Formal_Subprogram_Declaration;
3044 -------------------------------------
3045 -- Analyze_Formal_Type_Declaration --
3046 -------------------------------------
3048 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3049 Def : constant Node_Id := Formal_Type_Definition (N);
3050 T : Entity_Id;
3052 begin
3053 T := Defining_Identifier (N);
3055 if Present (Discriminant_Specifications (N))
3056 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3057 then
3058 Error_Msg_N
3059 ("discriminants not allowed for this formal type", T);
3060 end if;
3062 -- Enter the new name, and branch to specific routine
3064 case Nkind (Def) is
3065 when N_Formal_Private_Type_Definition =>
3066 Analyze_Formal_Private_Type (N, T, Def);
3068 when N_Formal_Derived_Type_Definition =>
3069 Analyze_Formal_Derived_Type (N, T, Def);
3071 when N_Formal_Incomplete_Type_Definition =>
3072 Analyze_Formal_Incomplete_Type (T, Def);
3074 when N_Formal_Discrete_Type_Definition =>
3075 Analyze_Formal_Discrete_Type (T, Def);
3077 when N_Formal_Signed_Integer_Type_Definition =>
3078 Analyze_Formal_Signed_Integer_Type (T, Def);
3080 when N_Formal_Modular_Type_Definition =>
3081 Analyze_Formal_Modular_Type (T, Def);
3083 when N_Formal_Floating_Point_Definition =>
3084 Analyze_Formal_Floating_Type (T, Def);
3086 when N_Formal_Ordinary_Fixed_Point_Definition =>
3087 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3089 when N_Formal_Decimal_Fixed_Point_Definition =>
3090 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3092 when N_Array_Type_Definition =>
3093 Analyze_Formal_Array_Type (T, Def);
3095 when N_Access_To_Object_Definition |
3096 N_Access_Function_Definition |
3097 N_Access_Procedure_Definition =>
3098 Analyze_Generic_Access_Type (T, Def);
3100 -- Ada 2005: a interface declaration is encoded as an abstract
3101 -- record declaration or a abstract type derivation.
3103 when N_Record_Definition =>
3104 Analyze_Formal_Interface_Type (N, T, Def);
3106 when N_Derived_Type_Definition =>
3107 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3109 when N_Error =>
3110 null;
3112 when others =>
3113 raise Program_Error;
3115 end case;
3117 Set_Is_Generic_Type (T);
3119 if Has_Aspects (N) then
3120 Analyze_Aspect_Specifications (N, T);
3121 end if;
3122 end Analyze_Formal_Type_Declaration;
3124 ------------------------------------
3125 -- Analyze_Function_Instantiation --
3126 ------------------------------------
3128 procedure Analyze_Function_Instantiation (N : Node_Id) is
3129 begin
3130 Analyze_Subprogram_Instantiation (N, E_Function);
3131 end Analyze_Function_Instantiation;
3133 ---------------------------------
3134 -- Analyze_Generic_Access_Type --
3135 ---------------------------------
3137 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3138 begin
3139 Enter_Name (T);
3141 if Nkind (Def) = N_Access_To_Object_Definition then
3142 Access_Type_Declaration (T, Def);
3144 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3145 and then No (Full_View (Designated_Type (T)))
3146 and then not Is_Generic_Type (Designated_Type (T))
3147 then
3148 Error_Msg_N ("premature usage of incomplete type", Def);
3150 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3151 Error_Msg_N
3152 ("only a subtype mark is allowed in a formal", Def);
3153 end if;
3155 else
3156 Access_Subprogram_Declaration (T, Def);
3157 end if;
3158 end Analyze_Generic_Access_Type;
3160 ---------------------------------
3161 -- Analyze_Generic_Formal_Part --
3162 ---------------------------------
3164 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3165 Gen_Parm_Decl : Node_Id;
3167 begin
3168 -- The generic formals are processed in the scope of the generic unit,
3169 -- where they are immediately visible. The scope is installed by the
3170 -- caller.
3172 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3173 while Present (Gen_Parm_Decl) loop
3174 Analyze (Gen_Parm_Decl);
3175 Next (Gen_Parm_Decl);
3176 end loop;
3178 Generate_Reference_To_Generic_Formals (Current_Scope);
3179 end Analyze_Generic_Formal_Part;
3181 ------------------------------------------
3182 -- Analyze_Generic_Package_Declaration --
3183 ------------------------------------------
3185 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3186 Loc : constant Source_Ptr := Sloc (N);
3187 Decls : constant List_Id :=
3188 Visible_Declarations (Specification (N));
3189 Decl : Node_Id;
3190 Id : Entity_Id;
3191 New_N : Node_Id;
3192 Renaming : Node_Id;
3193 Save_Parent : Node_Id;
3195 begin
3196 Check_SPARK_05_Restriction ("generic is not allowed", N);
3198 -- We introduce a renaming of the enclosing package, to have a usable
3199 -- entity as the prefix of an expanded name for a local entity of the
3200 -- form Par.P.Q, where P is the generic package. This is because a local
3201 -- entity named P may hide it, so that the usual visibility rules in
3202 -- the instance will not resolve properly.
3204 Renaming :=
3205 Make_Package_Renaming_Declaration (Loc,
3206 Defining_Unit_Name =>
3207 Make_Defining_Identifier (Loc,
3208 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3209 Name =>
3210 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3212 if Present (Decls) then
3213 Decl := First (Decls);
3214 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3215 Next (Decl);
3216 end loop;
3218 if Present (Decl) then
3219 Insert_Before (Decl, Renaming);
3220 else
3221 Append (Renaming, Visible_Declarations (Specification (N)));
3222 end if;
3224 else
3225 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3226 end if;
3228 -- Create copy of generic unit, and save for instantiation. If the unit
3229 -- is a child unit, do not copy the specifications for the parent, which
3230 -- are not part of the generic tree.
3232 Save_Parent := Parent_Spec (N);
3233 Set_Parent_Spec (N, Empty);
3235 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3236 Set_Parent_Spec (New_N, Save_Parent);
3237 Rewrite (N, New_N);
3239 -- Once the contents of the generic copy and the template are swapped,
3240 -- do the same for their respective aspect specifications.
3242 Exchange_Aspects (N, New_N);
3244 -- Collect all contract-related source pragmas found within the template
3245 -- and attach them to the contract of the package spec. This contract is
3246 -- used in the capture of global references within annotations.
3248 Create_Generic_Contract (N);
3250 Id := Defining_Entity (N);
3251 Generate_Definition (Id);
3253 -- Expansion is not applied to generic units
3255 Start_Generic;
3257 Enter_Name (Id);
3258 Set_Ekind (Id, E_Generic_Package);
3259 Set_Etype (Id, Standard_Void_Type);
3261 -- A generic package declared within a Ghost region is rendered Ghost
3262 -- (SPARK RM 6.9(2)).
3264 if Ghost_Mode > None then
3265 Set_Is_Ghost_Entity (Id);
3266 end if;
3268 -- Analyze aspects now, so that generated pragmas appear in the
3269 -- declarations before building and analyzing the generic copy.
3271 if Has_Aspects (N) then
3272 Analyze_Aspect_Specifications (N, Id);
3273 end if;
3275 Push_Scope (Id);
3276 Enter_Generic_Scope (Id);
3277 Set_Inner_Instances (Id, New_Elmt_List);
3279 Set_Categorization_From_Pragmas (N);
3280 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3282 -- Link the declaration of the generic homonym in the generic copy to
3283 -- the package it renames, so that it is always resolved properly.
3285 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3286 Set_Entity (Associated_Node (Name (Renaming)), Id);
3288 -- For a library unit, we have reconstructed the entity for the unit,
3289 -- and must reset it in the library tables.
3291 if Nkind (Parent (N)) = N_Compilation_Unit then
3292 Set_Cunit_Entity (Current_Sem_Unit, Id);
3293 end if;
3295 Analyze_Generic_Formal_Part (N);
3297 -- After processing the generic formals, analysis proceeds as for a
3298 -- non-generic package.
3300 Analyze (Specification (N));
3302 Validate_Categorization_Dependency (N, Id);
3304 End_Generic;
3306 End_Package_Scope (Id);
3307 Exit_Generic_Scope (Id);
3309 if Nkind (Parent (N)) /= N_Compilation_Unit then
3310 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3311 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3312 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3314 else
3315 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3316 Validate_RT_RAT_Component (N);
3318 -- If this is a spec without a body, check that generic parameters
3319 -- are referenced.
3321 if not Body_Required (Parent (N)) then
3322 Check_References (Id);
3323 end if;
3324 end if;
3326 -- If there is a specified storage pool in the context, create an
3327 -- aspect on the package declaration, so that it is used in any
3328 -- instance that does not override it.
3330 if Present (Default_Pool) then
3331 declare
3332 ASN : Node_Id;
3334 begin
3335 ASN :=
3336 Make_Aspect_Specification (Loc,
3337 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3338 Expression => New_Copy (Default_Pool));
3340 if No (Aspect_Specifications (Specification (N))) then
3341 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3342 else
3343 Append (ASN, Aspect_Specifications (Specification (N)));
3344 end if;
3345 end;
3346 end if;
3347 end Analyze_Generic_Package_Declaration;
3349 --------------------------------------------
3350 -- Analyze_Generic_Subprogram_Declaration --
3351 --------------------------------------------
3353 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3354 Formals : List_Id;
3355 Id : Entity_Id;
3356 New_N : Node_Id;
3357 Result_Type : Entity_Id;
3358 Save_Parent : Node_Id;
3359 Spec : Node_Id;
3360 Typ : Entity_Id;
3362 begin
3363 Check_SPARK_05_Restriction ("generic is not allowed", N);
3365 -- Create copy of generic unit, and save for instantiation. If the unit
3366 -- is a child unit, do not copy the specifications for the parent, which
3367 -- are not part of the generic tree.
3369 Save_Parent := Parent_Spec (N);
3370 Set_Parent_Spec (N, Empty);
3372 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3373 Set_Parent_Spec (New_N, Save_Parent);
3374 Rewrite (N, New_N);
3376 -- Once the contents of the generic copy and the template are swapped,
3377 -- do the same for their respective aspect specifications.
3379 Exchange_Aspects (N, New_N);
3381 -- Collect all contract-related source pragmas found within the template
3382 -- and attach them to the contract of the subprogram spec. This contract
3383 -- is used in the capture of global references within annotations.
3385 Create_Generic_Contract (N);
3387 Spec := Specification (N);
3388 Id := Defining_Entity (Spec);
3389 Generate_Definition (Id);
3391 if Nkind (Id) = N_Defining_Operator_Symbol then
3392 Error_Msg_N
3393 ("operator symbol not allowed for generic subprogram", Id);
3394 end if;
3396 Start_Generic;
3398 Enter_Name (Id);
3399 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3401 -- Analyze the aspects of the generic copy to ensure that all generated
3402 -- pragmas (if any) perform their semantic effects.
3404 if Has_Aspects (N) then
3405 Analyze_Aspect_Specifications (N, Id);
3406 end if;
3408 Push_Scope (Id);
3409 Enter_Generic_Scope (Id);
3410 Set_Inner_Instances (Id, New_Elmt_List);
3411 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3413 Analyze_Generic_Formal_Part (N);
3415 Formals := Parameter_Specifications (Spec);
3417 if Nkind (Spec) = N_Function_Specification then
3418 Set_Ekind (Id, E_Generic_Function);
3419 else
3420 Set_Ekind (Id, E_Generic_Procedure);
3421 end if;
3423 if Present (Formals) then
3424 Process_Formals (Formals, Spec);
3425 end if;
3427 if Nkind (Spec) = N_Function_Specification then
3428 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3429 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3430 Set_Etype (Id, Result_Type);
3432 -- Check restriction imposed by AI05-073: a generic function
3433 -- cannot return an abstract type or an access to such.
3435 -- This is a binding interpretation should it apply to earlier
3436 -- versions of Ada as well as Ada 2012???
3438 if Is_Abstract_Type (Designated_Type (Result_Type))
3439 and then Ada_Version >= Ada_2012
3440 then
3441 Error_Msg_N
3442 ("generic function cannot have an access result "
3443 & "that designates an abstract type", Spec);
3444 end if;
3446 else
3447 Find_Type (Result_Definition (Spec));
3448 Typ := Entity (Result_Definition (Spec));
3450 if Is_Abstract_Type (Typ)
3451 and then Ada_Version >= Ada_2012
3452 then
3453 Error_Msg_N
3454 ("generic function cannot have abstract result type", Spec);
3455 end if;
3457 -- If a null exclusion is imposed on the result type, then create
3458 -- a null-excluding itype (an access subtype) and use it as the
3459 -- function's Etype.
3461 if Is_Access_Type (Typ)
3462 and then Null_Exclusion_Present (Spec)
3463 then
3464 Set_Etype (Id,
3465 Create_Null_Excluding_Itype
3466 (T => Typ,
3467 Related_Nod => Spec,
3468 Scope_Id => Defining_Unit_Name (Spec)));
3469 else
3470 Set_Etype (Id, Typ);
3471 end if;
3472 end if;
3474 else
3475 Set_Etype (Id, Standard_Void_Type);
3476 end if;
3478 -- A generic subprogram declared within a Ghost region is rendered Ghost
3479 -- (SPARK RM 6.9(2)).
3481 if Ghost_Mode > None then
3482 Set_Is_Ghost_Entity (Id);
3483 end if;
3485 -- For a library unit, we have reconstructed the entity for the unit,
3486 -- and must reset it in the library tables. We also make sure that
3487 -- Body_Required is set properly in the original compilation unit node.
3489 if Nkind (Parent (N)) = N_Compilation_Unit then
3490 Set_Cunit_Entity (Current_Sem_Unit, Id);
3491 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3492 end if;
3494 Set_Categorization_From_Pragmas (N);
3495 Validate_Categorization_Dependency (N, Id);
3497 -- Capture all global references that occur within the profile of the
3498 -- generic subprogram. Aspects are not part of this processing because
3499 -- they must be delayed. If processed now, Save_Global_References will
3500 -- destroy the Associated_Node links and prevent the capture of global
3501 -- references when the contract of the generic subprogram is analyzed.
3503 Save_Global_References (Original_Node (N));
3505 End_Generic;
3506 End_Scope;
3507 Exit_Generic_Scope (Id);
3508 Generate_Reference_To_Formals (Id);
3510 List_Inherited_Pre_Post_Aspects (Id);
3511 end Analyze_Generic_Subprogram_Declaration;
3513 -----------------------------------
3514 -- Analyze_Package_Instantiation --
3515 -----------------------------------
3517 procedure Analyze_Package_Instantiation (N : Node_Id) is
3518 Loc : constant Source_Ptr := Sloc (N);
3519 Gen_Id : constant Node_Id := Name (N);
3521 Act_Decl : Node_Id;
3522 Act_Decl_Name : Node_Id;
3523 Act_Decl_Id : Entity_Id;
3524 Act_Spec : Node_Id;
3525 Act_Tree : Node_Id;
3527 Gen_Decl : Node_Id;
3528 Gen_Spec : Node_Id;
3529 Gen_Unit : Entity_Id;
3531 Is_Actual_Pack : constant Boolean :=
3532 Is_Internal (Defining_Entity (N));
3534 Env_Installed : Boolean := False;
3535 Parent_Installed : Boolean := False;
3536 Renaming_List : List_Id;
3537 Unit_Renaming : Node_Id;
3538 Needs_Body : Boolean;
3539 Inline_Now : Boolean := False;
3540 Has_Inline_Always : Boolean := False;
3542 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3543 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3545 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3546 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3547 -- Save the SPARK_Mode-related data for restore on exit
3549 Save_Style_Check : constant Boolean := Style_Check;
3550 -- Save style check mode for restore on exit
3552 procedure Delay_Descriptors (E : Entity_Id);
3553 -- Delay generation of subprogram descriptors for given entity
3555 function Might_Inline_Subp return Boolean;
3556 -- If inlining is active and the generic contains inlined subprograms,
3557 -- we instantiate the body. This may cause superfluous instantiations,
3558 -- but it is simpler than detecting the need for the body at the point
3559 -- of inlining, when the context of the instance is not available.
3561 -----------------------
3562 -- Delay_Descriptors --
3563 -----------------------
3565 procedure Delay_Descriptors (E : Entity_Id) is
3566 begin
3567 if not Delay_Subprogram_Descriptors (E) then
3568 Set_Delay_Subprogram_Descriptors (E);
3569 Pending_Descriptor.Append (E);
3570 end if;
3571 end Delay_Descriptors;
3573 -----------------------
3574 -- Might_Inline_Subp --
3575 -----------------------
3577 function Might_Inline_Subp return Boolean is
3578 E : Entity_Id;
3580 begin
3581 if not Inline_Processing_Required then
3582 return False;
3584 else
3585 E := First_Entity (Gen_Unit);
3586 while Present (E) loop
3587 if Is_Subprogram (E) and then Is_Inlined (E) then
3588 -- Remember if there are any subprograms with Inline_Always
3590 if Has_Pragma_Inline_Always (E) then
3591 Has_Inline_Always := True;
3592 end if;
3594 return True;
3595 end if;
3597 Next_Entity (E);
3598 end loop;
3599 end if;
3601 return False;
3602 end Might_Inline_Subp;
3604 -- Local declarations
3606 Vis_Prims_List : Elist_Id := No_Elist;
3607 -- List of primitives made temporarily visible in the instantiation
3608 -- to match the visibility of the formal type
3610 -- Start of processing for Analyze_Package_Instantiation
3612 begin
3613 Check_SPARK_05_Restriction ("generic is not allowed", N);
3615 -- Very first thing: check for Text_IO special unit in case we are
3616 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3618 Check_Text_IO_Special_Unit (Name (N));
3620 -- Make node global for error reporting
3622 Instantiation_Node := N;
3624 -- Turn off style checking in instances. If the check is enabled on the
3625 -- generic unit, a warning in an instance would just be noise. If not
3626 -- enabled on the generic, then a warning in an instance is just wrong.
3628 Style_Check := False;
3630 -- Case of instantiation of a generic package
3632 if Nkind (N) = N_Package_Instantiation then
3633 Act_Decl_Id := New_Copy (Defining_Entity (N));
3634 Set_Comes_From_Source (Act_Decl_Id, True);
3636 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3637 Act_Decl_Name :=
3638 Make_Defining_Program_Unit_Name (Loc,
3639 Name =>
3640 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3641 Defining_Identifier => Act_Decl_Id);
3642 else
3643 Act_Decl_Name := Act_Decl_Id;
3644 end if;
3646 -- Case of instantiation of a formal package
3648 else
3649 Act_Decl_Id := Defining_Identifier (N);
3650 Act_Decl_Name := Act_Decl_Id;
3651 end if;
3653 Generate_Definition (Act_Decl_Id);
3654 Set_Ekind (Act_Decl_Id, E_Package);
3656 -- Initialize list of incomplete actuals before analysis
3658 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3660 Preanalyze_Actuals (N, Act_Decl_Id);
3662 Init_Env;
3663 Env_Installed := True;
3665 -- Reset renaming map for formal types. The mapping is established
3666 -- when analyzing the generic associations, but some mappings are
3667 -- inherited from formal packages of parent units, and these are
3668 -- constructed when the parents are installed.
3670 Generic_Renamings.Set_Last (0);
3671 Generic_Renamings_HTable.Reset;
3673 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3674 Gen_Unit := Entity (Gen_Id);
3676 -- Verify that it is the name of a generic package
3678 -- A visibility glitch: if the instance is a child unit and the generic
3679 -- is the generic unit of a parent instance (i.e. both the parent and
3680 -- the child units are instances of the same package) the name now
3681 -- denotes the renaming within the parent, not the intended generic
3682 -- unit. See if there is a homonym that is the desired generic. The
3683 -- renaming declaration must be visible inside the instance of the
3684 -- child, but not when analyzing the name in the instantiation itself.
3686 if Ekind (Gen_Unit) = E_Package
3687 and then Present (Renamed_Entity (Gen_Unit))
3688 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3689 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3690 and then Present (Homonym (Gen_Unit))
3691 then
3692 Gen_Unit := Homonym (Gen_Unit);
3693 end if;
3695 if Etype (Gen_Unit) = Any_Type then
3696 Restore_Env;
3697 goto Leave;
3699 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3701 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3703 if From_Limited_With (Gen_Unit) then
3704 Error_Msg_N
3705 ("cannot instantiate a limited withed package", Gen_Id);
3706 else
3707 Error_Msg_NE
3708 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3709 end if;
3711 Restore_Env;
3712 goto Leave;
3713 end if;
3715 if In_Extended_Main_Source_Unit (N) then
3716 Set_Is_Instantiated (Gen_Unit);
3717 Generate_Reference (Gen_Unit, N);
3719 if Present (Renamed_Object (Gen_Unit)) then
3720 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3721 Generate_Reference (Renamed_Object (Gen_Unit), N);
3722 end if;
3723 end if;
3725 if Nkind (Gen_Id) = N_Identifier
3726 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3727 then
3728 Error_Msg_NE
3729 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3731 elsif Nkind (Gen_Id) = N_Expanded_Name
3732 and then Is_Child_Unit (Gen_Unit)
3733 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3734 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3735 then
3736 Error_Msg_N
3737 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3738 end if;
3740 Set_Entity (Gen_Id, Gen_Unit);
3742 -- If generic is a renaming, get original generic unit
3744 if Present (Renamed_Object (Gen_Unit))
3745 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3746 then
3747 Gen_Unit := Renamed_Object (Gen_Unit);
3748 end if;
3750 -- Verify that there are no circular instantiations
3752 if In_Open_Scopes (Gen_Unit) then
3753 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3754 Restore_Env;
3755 goto Leave;
3757 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3758 Error_Msg_Node_2 := Current_Scope;
3759 Error_Msg_NE
3760 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3761 Circularity_Detected := True;
3762 Restore_Env;
3763 goto Leave;
3765 else
3766 -- If the context of the instance is subject to SPARK_Mode "off" or
3767 -- the annotation is altogether missing, set the global flag which
3768 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3769 -- the instance.
3771 if SPARK_Mode /= On then
3772 Ignore_Pragma_SPARK_Mode := True;
3773 end if;
3775 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3776 Gen_Spec := Specification (Gen_Decl);
3778 -- Initialize renamings map, for error checking, and the list that
3779 -- holds private entities whose views have changed between generic
3780 -- definition and instantiation. If this is the instance created to
3781 -- validate an actual package, the instantiation environment is that
3782 -- of the enclosing instance.
3784 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3786 -- Copy original generic tree, to produce text for instantiation
3788 Act_Tree :=
3789 Copy_Generic_Node
3790 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3792 Act_Spec := Specification (Act_Tree);
3794 -- If this is the instance created to validate an actual package,
3795 -- only the formals matter, do not examine the package spec itself.
3797 if Is_Actual_Pack then
3798 Set_Visible_Declarations (Act_Spec, New_List);
3799 Set_Private_Declarations (Act_Spec, New_List);
3800 end if;
3802 Renaming_List :=
3803 Analyze_Associations
3804 (I_Node => N,
3805 Formals => Generic_Formal_Declarations (Act_Tree),
3806 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3808 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3810 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3811 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3812 Set_Is_Generic_Instance (Act_Decl_Id);
3813 Set_Generic_Parent (Act_Spec, Gen_Unit);
3815 -- References to the generic in its own declaration or its body are
3816 -- references to the instance. Add a renaming declaration for the
3817 -- generic unit itself. This declaration, as well as the renaming
3818 -- declarations for the generic formals, must remain private to the
3819 -- unit: the formals, because this is the language semantics, and
3820 -- the unit because its use is an artifact of the implementation.
3822 Unit_Renaming :=
3823 Make_Package_Renaming_Declaration (Loc,
3824 Defining_Unit_Name =>
3825 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3826 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3828 Append (Unit_Renaming, Renaming_List);
3830 -- The renaming declarations are the first local declarations of the
3831 -- new unit.
3833 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3834 Insert_List_Before
3835 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3836 else
3837 Set_Visible_Declarations (Act_Spec, Renaming_List);
3838 end if;
3840 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3842 -- Propagate the aspect specifications from the package declaration
3843 -- template to the instantiated version of the package declaration.
3845 if Has_Aspects (Act_Tree) then
3846 Set_Aspect_Specifications (Act_Decl,
3847 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3848 end if;
3850 -- The generic may have a generated Default_Storage_Pool aspect,
3851 -- set at the point of generic declaration. If the instance has
3852 -- that aspect, it overrides the one inherited from the generic.
3854 if Has_Aspects (Gen_Spec) then
3855 if No (Aspect_Specifications (N)) then
3856 Set_Aspect_Specifications (N,
3857 (New_Copy_List_Tree
3858 (Aspect_Specifications (Gen_Spec))));
3860 else
3861 declare
3862 ASN1, ASN2 : Node_Id;
3864 begin
3865 ASN1 := First (Aspect_Specifications (N));
3866 while Present (ASN1) loop
3867 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3868 then
3869 -- If generic carries a default storage pool, remove
3870 -- it in favor of the instance one.
3872 ASN2 := First (Aspect_Specifications (Gen_Spec));
3873 while Present (ASN2) loop
3874 if Chars (Identifier (ASN2)) =
3875 Name_Default_Storage_Pool
3876 then
3877 Remove (ASN2);
3878 exit;
3879 end if;
3881 Next (ASN2);
3882 end loop;
3883 end if;
3885 Next (ASN1);
3886 end loop;
3888 Prepend_List_To (Aspect_Specifications (N),
3889 (New_Copy_List_Tree
3890 (Aspect_Specifications (Gen_Spec))));
3891 end;
3892 end if;
3893 end if;
3895 -- Save the instantiation node, for subsequent instantiation of the
3896 -- body, if there is one and we are generating code for the current
3897 -- unit. Mark unit as having a body (avoids premature error message).
3899 -- We instantiate the body if we are generating code, if we are
3900 -- generating cross-reference information, or if we are building
3901 -- trees for ASIS use or GNATprove use.
3903 declare
3904 Enclosing_Body_Present : Boolean := False;
3905 -- If the generic unit is not a compilation unit, then a body may
3906 -- be present in its parent even if none is required. We create a
3907 -- tentative pending instantiation for the body, which will be
3908 -- discarded if none is actually present.
3910 Scop : Entity_Id;
3912 begin
3913 if Scope (Gen_Unit) /= Standard_Standard
3914 and then not Is_Child_Unit (Gen_Unit)
3915 then
3916 Scop := Scope (Gen_Unit);
3917 while Present (Scop) and then Scop /= Standard_Standard loop
3918 if Unit_Requires_Body (Scop) then
3919 Enclosing_Body_Present := True;
3920 exit;
3922 elsif In_Open_Scopes (Scop)
3923 and then In_Package_Body (Scop)
3924 then
3925 Enclosing_Body_Present := True;
3926 exit;
3927 end if;
3929 exit when Is_Compilation_Unit (Scop);
3930 Scop := Scope (Scop);
3931 end loop;
3932 end if;
3934 -- If front-end inlining is enabled or there are any subprograms
3935 -- marked with Inline_Always, and this is a unit for which code
3936 -- will be generated, we instantiate the body at once.
3938 -- This is done if the instance is not the main unit, and if the
3939 -- generic is not a child unit of another generic, to avoid scope
3940 -- problems and the reinstallation of parent instances.
3942 if Expander_Active
3943 and then (not Is_Child_Unit (Gen_Unit)
3944 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3945 and then Might_Inline_Subp
3946 and then not Is_Actual_Pack
3947 then
3948 if not Back_End_Inlining
3949 and then (Front_End_Inlining or else Has_Inline_Always)
3950 and then (Is_In_Main_Unit (N)
3951 or else In_Main_Context (Current_Scope))
3952 and then Nkind (Parent (N)) /= N_Compilation_Unit
3953 then
3954 Inline_Now := True;
3956 -- In configurable_run_time mode we force the inlining of
3957 -- predefined subprograms marked Inline_Always, to minimize
3958 -- the use of the run-time library.
3960 elsif Is_Predefined_File_Name
3961 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3962 and then Configurable_Run_Time_Mode
3963 and then Nkind (Parent (N)) /= N_Compilation_Unit
3964 then
3965 Inline_Now := True;
3966 end if;
3968 -- If the current scope is itself an instance within a child
3969 -- unit, there will be duplications in the scope stack, and the
3970 -- unstacking mechanism in Inline_Instance_Body will fail.
3971 -- This loses some rare cases of optimization, and might be
3972 -- improved some day, if we can find a proper abstraction for
3973 -- "the complete compilation context" that can be saved and
3974 -- restored. ???
3976 if Is_Generic_Instance (Current_Scope) then
3977 declare
3978 Curr_Unit : constant Entity_Id :=
3979 Cunit_Entity (Current_Sem_Unit);
3980 begin
3981 if Curr_Unit /= Current_Scope
3982 and then Is_Child_Unit (Curr_Unit)
3983 then
3984 Inline_Now := False;
3985 end if;
3986 end;
3987 end if;
3988 end if;
3990 Needs_Body :=
3991 (Unit_Requires_Body (Gen_Unit)
3992 or else Enclosing_Body_Present
3993 or else Present (Corresponding_Body (Gen_Decl)))
3994 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3995 and then not Is_Actual_Pack
3996 and then not Inline_Now
3997 and then (Operating_Mode = Generate_Code
3999 -- Need comment for this check ???
4001 or else (Operating_Mode = Check_Semantics
4002 and then (ASIS_Mode or GNATprove_Mode)));
4004 -- If front-end inlining is enabled or there are any subprograms
4005 -- marked with Inline_Always, do not instantiate body when within
4006 -- a generic context.
4008 if ((Front_End_Inlining or else Has_Inline_Always)
4009 and then not Expander_Active)
4010 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4011 then
4012 Needs_Body := False;
4013 end if;
4015 -- If the current context is generic, and the package being
4016 -- instantiated is declared within a formal package, there is no
4017 -- body to instantiate until the enclosing generic is instantiated
4018 -- and there is an actual for the formal package. If the formal
4019 -- package has parameters, we build a regular package instance for
4020 -- it, that precedes the original formal package declaration.
4022 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4023 declare
4024 Decl : constant Node_Id :=
4025 Original_Node
4026 (Unit_Declaration_Node (Scope (Gen_Unit)));
4027 begin
4028 if Nkind (Decl) = N_Formal_Package_Declaration
4029 or else (Nkind (Decl) = N_Package_Declaration
4030 and then Is_List_Member (Decl)
4031 and then Present (Next (Decl))
4032 and then
4033 Nkind (Next (Decl)) =
4034 N_Formal_Package_Declaration)
4035 then
4036 Needs_Body := False;
4037 end if;
4038 end;
4039 end if;
4040 end;
4042 -- For RCI unit calling stubs, we omit the instance body if the
4043 -- instance is the RCI library unit itself.
4045 -- However there is a special case for nested instances: in this case
4046 -- we do generate the instance body, as it might be required, e.g.
4047 -- because it provides stream attributes for some type used in the
4048 -- profile of a remote subprogram. This is consistent with 12.3(12),
4049 -- which indicates that the instance body occurs at the place of the
4050 -- instantiation, and thus is part of the RCI declaration, which is
4051 -- present on all client partitions (this is E.2.3(18)).
4053 -- Note that AI12-0002 may make it illegal at some point to have
4054 -- stream attributes defined in an RCI unit, in which case this
4055 -- special case will become unnecessary. In the meantime, there
4056 -- is known application code in production that depends on this
4057 -- being possible, so we definitely cannot eliminate the body in
4058 -- the case of nested instances for the time being.
4060 -- When we generate a nested instance body, calling stubs for any
4061 -- relevant subprogram will be be inserted immediately after the
4062 -- subprogram declarations, and will take precedence over the
4063 -- subsequent (original) body. (The stub and original body will be
4064 -- complete homographs, but this is permitted in an instance).
4065 -- (Could we do better and remove the original body???)
4067 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4068 and then Comes_From_Source (N)
4069 and then Nkind (Parent (N)) = N_Compilation_Unit
4070 then
4071 Needs_Body := False;
4072 end if;
4074 if Needs_Body then
4076 -- Here is a defence against a ludicrous number of instantiations
4077 -- caused by a circular set of instantiation attempts.
4079 if Pending_Instantiations.Last > Maximum_Instantiations then
4080 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4081 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4082 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4083 raise Unrecoverable_Error;
4084 end if;
4086 -- Indicate that the enclosing scopes contain an instantiation,
4087 -- and that cleanup actions should be delayed until after the
4088 -- instance body is expanded.
4090 Check_Forward_Instantiation (Gen_Decl);
4091 if Nkind (N) = N_Package_Instantiation then
4092 declare
4093 Enclosing_Master : Entity_Id;
4095 begin
4096 -- Loop to search enclosing masters
4098 Enclosing_Master := Current_Scope;
4099 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4100 if Ekind (Enclosing_Master) = E_Package then
4101 if Is_Compilation_Unit (Enclosing_Master) then
4102 if In_Package_Body (Enclosing_Master) then
4103 Delay_Descriptors
4104 (Body_Entity (Enclosing_Master));
4105 else
4106 Delay_Descriptors
4107 (Enclosing_Master);
4108 end if;
4110 exit Scope_Loop;
4112 else
4113 Enclosing_Master := Scope (Enclosing_Master);
4114 end if;
4116 elsif Is_Generic_Unit (Enclosing_Master)
4117 or else Ekind (Enclosing_Master) = E_Void
4118 then
4119 -- Cleanup actions will eventually be performed on the
4120 -- enclosing subprogram or package instance, if any.
4121 -- Enclosing scope is void in the formal part of a
4122 -- generic subprogram.
4124 exit Scope_Loop;
4126 else
4127 if Ekind (Enclosing_Master) = E_Entry
4128 and then
4129 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4130 then
4131 if not Expander_Active then
4132 exit Scope_Loop;
4133 else
4134 Enclosing_Master :=
4135 Protected_Body_Subprogram (Enclosing_Master);
4136 end if;
4137 end if;
4139 Set_Delay_Cleanups (Enclosing_Master);
4141 while Ekind (Enclosing_Master) = E_Block loop
4142 Enclosing_Master := Scope (Enclosing_Master);
4143 end loop;
4145 if Is_Subprogram (Enclosing_Master) then
4146 Delay_Descriptors (Enclosing_Master);
4148 elsif Is_Task_Type (Enclosing_Master) then
4149 declare
4150 TBP : constant Node_Id :=
4151 Get_Task_Body_Procedure
4152 (Enclosing_Master);
4153 begin
4154 if Present (TBP) then
4155 Delay_Descriptors (TBP);
4156 Set_Delay_Cleanups (TBP);
4157 end if;
4158 end;
4159 end if;
4161 exit Scope_Loop;
4162 end if;
4163 end loop Scope_Loop;
4164 end;
4166 -- Make entry in table
4168 Add_Pending_Instantiation (N, Act_Decl);
4169 end if;
4170 end if;
4172 Set_Categorization_From_Pragmas (Act_Decl);
4174 if Parent_Installed then
4175 Hide_Current_Scope;
4176 end if;
4178 Set_Instance_Spec (N, Act_Decl);
4180 -- If not a compilation unit, insert the package declaration before
4181 -- the original instantiation node.
4183 if Nkind (Parent (N)) /= N_Compilation_Unit then
4184 Mark_Rewrite_Insertion (Act_Decl);
4185 Insert_Before (N, Act_Decl);
4187 if Has_Aspects (N) then
4188 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4190 -- The pragma created for a Default_Storage_Pool aspect must
4191 -- appear ahead of the declarations in the instance spec.
4192 -- Analysis has placed it after the instance node, so remove
4193 -- it and reinsert it properly now.
4195 declare
4196 ASN : constant Node_Id := First (Aspect_Specifications (N));
4197 A_Name : constant Name_Id := Chars (Identifier (ASN));
4198 Decl : Node_Id;
4200 begin
4201 if A_Name = Name_Default_Storage_Pool then
4202 if No (Visible_Declarations (Act_Spec)) then
4203 Set_Visible_Declarations (Act_Spec, New_List);
4204 end if;
4206 Decl := Next (N);
4207 while Present (Decl) loop
4208 if Nkind (Decl) = N_Pragma then
4209 Remove (Decl);
4210 Prepend (Decl, Visible_Declarations (Act_Spec));
4211 exit;
4212 end if;
4214 Next (Decl);
4215 end loop;
4216 end if;
4217 end;
4218 end if;
4220 Analyze (Act_Decl);
4222 -- For an instantiation that is a compilation unit, place
4223 -- declaration on current node so context is complete for analysis
4224 -- (including nested instantiations). If this is the main unit,
4225 -- the declaration eventually replaces the instantiation node.
4226 -- If the instance body is created later, it replaces the
4227 -- instance node, and the declaration is attached to it
4228 -- (see Build_Instance_Compilation_Unit_Nodes).
4230 else
4231 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4233 -- The entity for the current unit is the newly created one,
4234 -- and all semantic information is attached to it.
4236 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4238 -- If this is the main unit, replace the main entity as well
4240 if Current_Sem_Unit = Main_Unit then
4241 Main_Unit_Entity := Act_Decl_Id;
4242 end if;
4243 end if;
4245 Set_Unit (Parent (N), Act_Decl);
4246 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4247 Set_Package_Instantiation (Act_Decl_Id, N);
4249 -- Process aspect specifications of the instance node, if any, to
4250 -- take into account categorization pragmas before analyzing the
4251 -- instance.
4253 if Has_Aspects (N) then
4254 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4255 end if;
4257 Analyze (Act_Decl);
4258 Set_Unit (Parent (N), N);
4259 Set_Body_Required (Parent (N), False);
4261 -- We never need elaboration checks on instantiations, since by
4262 -- definition, the body instantiation is elaborated at the same
4263 -- time as the spec instantiation.
4265 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4266 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4267 end if;
4269 Check_Elab_Instantiation (N);
4271 if ABE_Is_Certain (N) and then Needs_Body then
4272 Pending_Instantiations.Decrement_Last;
4273 end if;
4275 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4277 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4278 First_Private_Entity (Act_Decl_Id));
4280 -- If the instantiation will receive a body, the unit will be
4281 -- transformed into a package body, and receive its own elaboration
4282 -- entity. Otherwise, the nature of the unit is now a package
4283 -- declaration.
4285 if Nkind (Parent (N)) = N_Compilation_Unit
4286 and then not Needs_Body
4287 then
4288 Rewrite (N, Act_Decl);
4289 end if;
4291 if Present (Corresponding_Body (Gen_Decl))
4292 or else Unit_Requires_Body (Gen_Unit)
4293 then
4294 Set_Has_Completion (Act_Decl_Id);
4295 end if;
4297 Check_Formal_Packages (Act_Decl_Id);
4299 Restore_Hidden_Primitives (Vis_Prims_List);
4300 Restore_Private_Views (Act_Decl_Id);
4302 Inherit_Context (Gen_Decl, N);
4304 if Parent_Installed then
4305 Remove_Parent;
4306 end if;
4308 Restore_Env;
4309 Env_Installed := False;
4310 end if;
4312 Validate_Categorization_Dependency (N, Act_Decl_Id);
4314 -- There used to be a check here to prevent instantiations in local
4315 -- contexts if the No_Local_Allocators restriction was active. This
4316 -- check was removed by a binding interpretation in AI-95-00130/07,
4317 -- but we retain the code for documentation purposes.
4319 -- if Ekind (Act_Decl_Id) /= E_Void
4320 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4321 -- then
4322 -- Check_Restriction (No_Local_Allocators, N);
4323 -- end if;
4325 if Inline_Now then
4326 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4327 end if;
4329 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4330 -- be used as defining identifiers for a formal package and for the
4331 -- corresponding expanded package.
4333 if Nkind (N) = N_Formal_Package_Declaration then
4334 Act_Decl_Id := New_Copy (Defining_Entity (N));
4335 Set_Comes_From_Source (Act_Decl_Id, True);
4336 Set_Is_Generic_Instance (Act_Decl_Id, False);
4337 Set_Defining_Identifier (N, Act_Decl_Id);
4338 end if;
4340 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4341 SPARK_Mode := Save_SM;
4342 SPARK_Mode_Pragma := Save_SMP;
4343 Style_Check := Save_Style_Check;
4345 if SPARK_Mode = On then
4346 Dynamic_Elaboration_Checks := False;
4347 end if;
4349 -- Check that if N is an instantiation of System.Dim_Float_IO or
4350 -- System.Dim_Integer_IO, the formal type has a dimension system.
4352 if Nkind (N) = N_Package_Instantiation
4353 and then Is_Dim_IO_Package_Instantiation (N)
4354 then
4355 declare
4356 Assoc : constant Node_Id := First (Generic_Associations (N));
4357 begin
4358 if not Has_Dimension_System
4359 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4360 then
4361 Error_Msg_N ("type with a dimension system expected", Assoc);
4362 end if;
4363 end;
4364 end if;
4366 <<Leave>>
4367 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4368 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4369 end if;
4371 exception
4372 when Instantiation_Error =>
4373 if Parent_Installed then
4374 Remove_Parent;
4375 end if;
4377 if Env_Installed then
4378 Restore_Env;
4379 end if;
4381 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4382 SPARK_Mode := Save_SM;
4383 SPARK_Mode_Pragma := Save_SMP;
4384 Style_Check := Save_Style_Check;
4386 if SPARK_Mode = On then
4387 Dynamic_Elaboration_Checks := False;
4388 end if;
4389 end Analyze_Package_Instantiation;
4391 --------------------------
4392 -- Inline_Instance_Body --
4393 --------------------------
4395 procedure Inline_Instance_Body
4396 (N : Node_Id;
4397 Gen_Unit : Entity_Id;
4398 Act_Decl : Node_Id)
4400 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4401 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4402 Gen_Comp : constant Entity_Id :=
4403 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4405 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4406 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4407 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4408 -- to provide a clean environment for analysis of the inlined body will
4409 -- eliminate any previously set SPARK_Mode.
4411 Scope_Stack_Depth : constant Pos :=
4412 Scope_Stack.Last - Scope_Stack.First + 1;
4414 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4415 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4416 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4417 Curr_Scope : Entity_Id := Empty;
4418 List : Elist_Id;
4419 Num_Inner : Nat := 0;
4420 Num_Scopes : Nat := 0;
4421 N_Instances : Nat := 0;
4422 Removed : Boolean := False;
4423 S : Entity_Id;
4424 Vis : Boolean;
4426 begin
4427 -- Case of generic unit defined in another unit. We must remove the
4428 -- complete context of the current unit to install that of the generic.
4430 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4432 -- Add some comments for the following two loops ???
4434 S := Current_Scope;
4435 while Present (S) and then S /= Standard_Standard loop
4436 loop
4437 Num_Scopes := Num_Scopes + 1;
4439 Use_Clauses (Num_Scopes) :=
4440 (Scope_Stack.Table
4441 (Scope_Stack.Last - Num_Scopes + 1).
4442 First_Use_Clause);
4443 End_Use_Clauses (Use_Clauses (Num_Scopes));
4445 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4446 or else Scope_Stack.Table
4447 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4448 end loop;
4450 exit when Is_Generic_Instance (S)
4451 and then (In_Package_Body (S)
4452 or else Ekind (S) = E_Procedure
4453 or else Ekind (S) = E_Function);
4454 S := Scope (S);
4455 end loop;
4457 Vis := Is_Immediately_Visible (Gen_Comp);
4459 -- Find and save all enclosing instances
4461 S := Current_Scope;
4463 while Present (S)
4464 and then S /= Standard_Standard
4465 loop
4466 if Is_Generic_Instance (S) then
4467 N_Instances := N_Instances + 1;
4468 Instances (N_Instances) := S;
4470 exit when In_Package_Body (S);
4471 end if;
4473 S := Scope (S);
4474 end loop;
4476 -- Remove context of current compilation unit, unless we are within a
4477 -- nested package instantiation, in which case the context has been
4478 -- removed previously.
4480 -- If current scope is the body of a child unit, remove context of
4481 -- spec as well. If an enclosing scope is an instance body, the
4482 -- context has already been removed, but the entities in the body
4483 -- must be made invisible as well.
4485 S := Current_Scope;
4486 while Present (S) and then S /= Standard_Standard loop
4487 if Is_Generic_Instance (S)
4488 and then (In_Package_Body (S)
4489 or else Ekind_In (S, E_Procedure, E_Function))
4490 then
4491 -- We still have to remove the entities of the enclosing
4492 -- instance from direct visibility.
4494 declare
4495 E : Entity_Id;
4496 begin
4497 E := First_Entity (S);
4498 while Present (E) loop
4499 Set_Is_Immediately_Visible (E, False);
4500 Next_Entity (E);
4501 end loop;
4502 end;
4504 exit;
4505 end if;
4507 if S = Curr_Unit
4508 or else (Ekind (Curr_Unit) = E_Package_Body
4509 and then S = Spec_Entity (Curr_Unit))
4510 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4511 and then S = Corresponding_Spec
4512 (Unit_Declaration_Node (Curr_Unit)))
4513 then
4514 Removed := True;
4516 -- Remove entities in current scopes from visibility, so that
4517 -- instance body is compiled in a clean environment.
4519 List := Save_Scope_Stack (Handle_Use => False);
4521 if Is_Child_Unit (S) then
4523 -- Remove child unit from stack, as well as inner scopes.
4524 -- Removing the context of a child unit removes parent units
4525 -- as well.
4527 while Current_Scope /= S loop
4528 Num_Inner := Num_Inner + 1;
4529 Inner_Scopes (Num_Inner) := Current_Scope;
4530 Pop_Scope;
4531 end loop;
4533 Pop_Scope;
4534 Remove_Context (Curr_Comp);
4535 Curr_Scope := S;
4537 else
4538 Remove_Context (Curr_Comp);
4539 end if;
4541 if Ekind (Curr_Unit) = E_Package_Body then
4542 Remove_Context (Library_Unit (Curr_Comp));
4543 end if;
4544 end if;
4546 S := Scope (S);
4547 end loop;
4549 pragma Assert (Num_Inner < Num_Scopes);
4551 -- The inlined package body must be analyzed with the SPARK_Mode of
4552 -- the enclosing context, otherwise the body may cause bogus errors
4553 -- if a configuration SPARK_Mode pragma in in effect.
4555 Push_Scope (Standard_Standard);
4556 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4557 Instantiate_Package_Body
4558 (Body_Info =>
4559 ((Inst_Node => N,
4560 Act_Decl => Act_Decl,
4561 Expander_Status => Expander_Active,
4562 Current_Sem_Unit => Current_Sem_Unit,
4563 Scope_Suppress => Scope_Suppress,
4564 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4565 Version => Ada_Version,
4566 Version_Pragma => Ada_Version_Pragma,
4567 Warnings => Save_Warnings,
4568 SPARK_Mode => Save_SM,
4569 SPARK_Mode_Pragma => Save_SMP)),
4570 Inlined_Body => True);
4572 Pop_Scope;
4574 -- Restore context
4576 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4578 -- Reset Generic_Instance flag so that use clauses can be installed
4579 -- in the proper order. (See Use_One_Package for effect of enclosing
4580 -- instances on processing of use clauses).
4582 for J in 1 .. N_Instances loop
4583 Set_Is_Generic_Instance (Instances (J), False);
4584 end loop;
4586 if Removed then
4587 Install_Context (Curr_Comp);
4589 if Present (Curr_Scope)
4590 and then Is_Child_Unit (Curr_Scope)
4591 then
4592 Push_Scope (Curr_Scope);
4593 Set_Is_Immediately_Visible (Curr_Scope);
4595 -- Finally, restore inner scopes as well
4597 for J in reverse 1 .. Num_Inner loop
4598 Push_Scope (Inner_Scopes (J));
4599 end loop;
4600 end if;
4602 Restore_Scope_Stack (List, Handle_Use => False);
4604 if Present (Curr_Scope)
4605 and then
4606 (In_Private_Part (Curr_Scope)
4607 or else In_Package_Body (Curr_Scope))
4608 then
4609 -- Install private declaration of ancestor units, which are
4610 -- currently available. Restore_Scope_Stack and Install_Context
4611 -- only install the visible part of parents.
4613 declare
4614 Par : Entity_Id;
4615 begin
4616 Par := Scope (Curr_Scope);
4617 while (Present (Par)) and then Par /= Standard_Standard loop
4618 Install_Private_Declarations (Par);
4619 Par := Scope (Par);
4620 end loop;
4621 end;
4622 end if;
4623 end if;
4625 -- Restore use clauses. For a child unit, use clauses in the parents
4626 -- are restored when installing the context, so only those in inner
4627 -- scopes (and those local to the child unit itself) need to be
4628 -- installed explicitly.
4630 if Is_Child_Unit (Curr_Unit) and then Removed then
4631 for J in reverse 1 .. Num_Inner + 1 loop
4632 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4633 Use_Clauses (J);
4634 Install_Use_Clauses (Use_Clauses (J));
4635 end loop;
4637 else
4638 for J in reverse 1 .. Num_Scopes loop
4639 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4640 Use_Clauses (J);
4641 Install_Use_Clauses (Use_Clauses (J));
4642 end loop;
4643 end if;
4645 -- Restore status of instances. If one of them is a body, make its
4646 -- local entities visible again.
4648 declare
4649 E : Entity_Id;
4650 Inst : Entity_Id;
4652 begin
4653 for J in 1 .. N_Instances loop
4654 Inst := Instances (J);
4655 Set_Is_Generic_Instance (Inst, True);
4657 if In_Package_Body (Inst)
4658 or else Ekind_In (S, E_Procedure, E_Function)
4659 then
4660 E := First_Entity (Instances (J));
4661 while Present (E) loop
4662 Set_Is_Immediately_Visible (E);
4663 Next_Entity (E);
4664 end loop;
4665 end if;
4666 end loop;
4667 end;
4669 -- If generic unit is in current unit, current context is correct. Note
4670 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4671 -- enclosing scopes were removed.
4673 else
4674 Instantiate_Package_Body
4675 (Body_Info =>
4676 ((Inst_Node => N,
4677 Act_Decl => Act_Decl,
4678 Expander_Status => Expander_Active,
4679 Current_Sem_Unit => Current_Sem_Unit,
4680 Scope_Suppress => Scope_Suppress,
4681 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4682 Version => Ada_Version,
4683 Version_Pragma => Ada_Version_Pragma,
4684 Warnings => Save_Warnings,
4685 SPARK_Mode => SPARK_Mode,
4686 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4687 Inlined_Body => True);
4688 end if;
4689 end Inline_Instance_Body;
4691 -------------------------------------
4692 -- Analyze_Procedure_Instantiation --
4693 -------------------------------------
4695 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4696 begin
4697 Analyze_Subprogram_Instantiation (N, E_Procedure);
4698 end Analyze_Procedure_Instantiation;
4700 -----------------------------------
4701 -- Need_Subprogram_Instance_Body --
4702 -----------------------------------
4704 function Need_Subprogram_Instance_Body
4705 (N : Node_Id;
4706 Subp : Entity_Id) return Boolean
4709 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4710 -- Return True if E is an inlined subprogram, an inlined renaming or a
4711 -- subprogram nested in an inlined subprogram. The inlining machinery
4712 -- totally disregards nested subprograms since it considers that they
4713 -- will always be compiled if the parent is (see Inline.Is_Nested).
4715 ------------------------------------
4716 -- Is_Inlined_Or_Child_Of_Inlined --
4717 ------------------------------------
4719 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4720 Scop : Entity_Id;
4722 begin
4723 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4724 return True;
4725 end if;
4727 Scop := Scope (E);
4728 while Scop /= Standard_Standard loop
4729 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4730 return True;
4731 end if;
4733 Scop := Scope (Scop);
4734 end loop;
4736 return False;
4737 end Is_Inlined_Or_Child_Of_Inlined;
4739 begin
4740 -- Must be in the main unit or inlined (or child of inlined)
4742 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4744 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4746 and then (Operating_Mode = Generate_Code
4747 or else (Operating_Mode = Check_Semantics
4748 and then (ASIS_Mode or GNATprove_Mode)))
4750 -- The body is needed when generating code (full expansion), in ASIS
4751 -- mode for other tools, and in GNATprove mode (special expansion) for
4752 -- formal verification of the body itself.
4754 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4756 -- No point in inlining if ABE is inevitable
4758 and then not ABE_Is_Certain (N)
4760 -- Or if subprogram is eliminated
4762 and then not Is_Eliminated (Subp)
4763 then
4764 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4765 return True;
4767 -- Here if not inlined, or we ignore the inlining
4769 else
4770 return False;
4771 end if;
4772 end Need_Subprogram_Instance_Body;
4774 --------------------------------------
4775 -- Analyze_Subprogram_Instantiation --
4776 --------------------------------------
4778 procedure Analyze_Subprogram_Instantiation
4779 (N : Node_Id;
4780 K : Entity_Kind)
4782 Loc : constant Source_Ptr := Sloc (N);
4783 Gen_Id : constant Node_Id := Name (N);
4785 Anon_Id : constant Entity_Id :=
4786 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4787 Chars => New_External_Name
4788 (Chars (Defining_Entity (N)), 'R'));
4790 Act_Decl_Id : Entity_Id;
4791 Act_Decl : Node_Id;
4792 Act_Spec : Node_Id;
4793 Act_Tree : Node_Id;
4795 Env_Installed : Boolean := False;
4796 Gen_Unit : Entity_Id;
4797 Gen_Decl : Node_Id;
4798 Pack_Id : Entity_Id;
4799 Parent_Installed : Boolean := False;
4801 Renaming_List : List_Id;
4802 -- The list of declarations that link formals and actuals of the
4803 -- instance. These are subtype declarations for formal types, and
4804 -- renaming declarations for other formals. The subprogram declaration
4805 -- for the instance is then appended to the list, and the last item on
4806 -- the list is the renaming declaration for the instance.
4808 procedure Analyze_Instance_And_Renamings;
4809 -- The instance must be analyzed in a context that includes the mappings
4810 -- of generic parameters into actuals. We create a package declaration
4811 -- for this purpose, and a subprogram with an internal name within the
4812 -- package. The subprogram instance is simply an alias for the internal
4813 -- subprogram, declared in the current scope.
4815 procedure Build_Subprogram_Renaming;
4816 -- If the subprogram is recursive, there are occurrences of the name of
4817 -- the generic within the body, which must resolve to the current
4818 -- instance. We add a renaming declaration after the declaration, which
4819 -- is available in the instance body, as well as in the analysis of
4820 -- aspects that appear in the generic. This renaming declaration is
4821 -- inserted after the instance declaration which it renames.
4823 ------------------------------------
4824 -- Analyze_Instance_And_Renamings --
4825 ------------------------------------
4827 procedure Analyze_Instance_And_Renamings is
4828 Def_Ent : constant Entity_Id := Defining_Entity (N);
4829 Pack_Decl : Node_Id;
4831 begin
4832 if Nkind (Parent (N)) = N_Compilation_Unit then
4834 -- For the case of a compilation unit, the container package has
4835 -- the same name as the instantiation, to insure that the binder
4836 -- calls the elaboration procedure with the right name. Copy the
4837 -- entity of the instance, which may have compilation level flags
4838 -- (e.g. Is_Child_Unit) set.
4840 Pack_Id := New_Copy (Def_Ent);
4842 else
4843 -- Otherwise we use the name of the instantiation concatenated
4844 -- with its source position to ensure uniqueness if there are
4845 -- several instantiations with the same name.
4847 Pack_Id :=
4848 Make_Defining_Identifier (Loc,
4849 Chars => New_External_Name
4850 (Related_Id => Chars (Def_Ent),
4851 Suffix => "GP",
4852 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4853 end if;
4855 Pack_Decl :=
4856 Make_Package_Declaration (Loc,
4857 Specification => Make_Package_Specification (Loc,
4858 Defining_Unit_Name => Pack_Id,
4859 Visible_Declarations => Renaming_List,
4860 End_Label => Empty));
4862 Set_Instance_Spec (N, Pack_Decl);
4863 Set_Is_Generic_Instance (Pack_Id);
4864 Set_Debug_Info_Needed (Pack_Id);
4866 -- Case of not a compilation unit
4868 if Nkind (Parent (N)) /= N_Compilation_Unit then
4869 Mark_Rewrite_Insertion (Pack_Decl);
4870 Insert_Before (N, Pack_Decl);
4871 Set_Has_Completion (Pack_Id);
4873 -- Case of an instantiation that is a compilation unit
4875 -- Place declaration on current node so context is complete for
4876 -- analysis (including nested instantiations), and for use in a
4877 -- context_clause (see Analyze_With_Clause).
4879 else
4880 Set_Unit (Parent (N), Pack_Decl);
4881 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4882 end if;
4884 Analyze (Pack_Decl);
4885 Check_Formal_Packages (Pack_Id);
4886 Set_Is_Generic_Instance (Pack_Id, False);
4888 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4889 -- above???
4891 -- Body of the enclosing package is supplied when instantiating the
4892 -- subprogram body, after semantic analysis is completed.
4894 if Nkind (Parent (N)) = N_Compilation_Unit then
4896 -- Remove package itself from visibility, so it does not
4897 -- conflict with subprogram.
4899 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4901 -- Set name and scope of internal subprogram so that the proper
4902 -- external name will be generated. The proper scope is the scope
4903 -- of the wrapper package. We need to generate debugging info for
4904 -- the internal subprogram, so set flag accordingly.
4906 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4907 Set_Scope (Anon_Id, Scope (Pack_Id));
4909 -- Mark wrapper package as referenced, to avoid spurious warnings
4910 -- if the instantiation appears in various with_ clauses of
4911 -- subunits of the main unit.
4913 Set_Referenced (Pack_Id);
4914 end if;
4916 Set_Is_Generic_Instance (Anon_Id);
4917 Set_Debug_Info_Needed (Anon_Id);
4918 Act_Decl_Id := New_Copy (Anon_Id);
4920 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4921 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4922 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4924 -- Subprogram instance comes from source only if generic does
4926 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4928 -- If the instance is a child unit, mark the Id accordingly. Mark
4929 -- the anonymous entity as well, which is the real subprogram and
4930 -- which is used when the instance appears in a context clause.
4931 -- Similarly, propagate the Is_Eliminated flag to handle properly
4932 -- nested eliminated subprograms.
4934 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4935 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4936 New_Overloaded_Entity (Act_Decl_Id);
4937 Check_Eliminated (Act_Decl_Id);
4938 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4940 -- In compilation unit case, kill elaboration checks on the
4941 -- instantiation, since they are never needed -- the body is
4942 -- instantiated at the same point as the spec.
4944 if Nkind (Parent (N)) = N_Compilation_Unit then
4945 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4946 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4947 Set_Is_Compilation_Unit (Anon_Id);
4949 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4950 end if;
4952 -- The instance is not a freezing point for the new subprogram.
4953 -- The anonymous subprogram may have a freeze node, created for
4954 -- some delayed aspects. This freeze node must not be inherited
4955 -- by the visible subprogram entity.
4957 Set_Is_Frozen (Act_Decl_Id, False);
4958 Set_Freeze_Node (Act_Decl_Id, Empty);
4960 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4961 Valid_Operator_Definition (Act_Decl_Id);
4962 end if;
4964 Set_Alias (Act_Decl_Id, Anon_Id);
4965 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4966 Set_Has_Completion (Act_Decl_Id);
4967 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4969 if Nkind (Parent (N)) = N_Compilation_Unit then
4970 Set_Body_Required (Parent (N), False);
4971 end if;
4972 end Analyze_Instance_And_Renamings;
4974 -------------------------------
4975 -- Build_Subprogram_Renaming --
4976 -------------------------------
4978 procedure Build_Subprogram_Renaming is
4979 Renaming_Decl : Node_Id;
4980 Unit_Renaming : Node_Id;
4982 begin
4983 Unit_Renaming :=
4984 Make_Subprogram_Renaming_Declaration (Loc,
4985 Specification =>
4986 Copy_Generic_Node
4987 (Specification (Original_Node (Gen_Decl)),
4988 Empty,
4989 Instantiating => True),
4990 Name => New_Occurrence_Of (Anon_Id, Loc));
4992 -- The generic may be a a child unit. The renaming needs an
4993 -- identifier with the proper name.
4995 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4996 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4998 -- If there is a formal subprogram with the same name as the unit
4999 -- itself, do not add this renaming declaration, to prevent
5000 -- ambiguities when there is a call with that name in the body.
5001 -- This is a partial and ugly fix for one ACATS test. ???
5003 Renaming_Decl := First (Renaming_List);
5004 while Present (Renaming_Decl) loop
5005 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5006 and then
5007 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5008 then
5009 exit;
5010 end if;
5012 Next (Renaming_Decl);
5013 end loop;
5015 if No (Renaming_Decl) then
5016 Append (Unit_Renaming, Renaming_List);
5017 end if;
5018 end Build_Subprogram_Renaming;
5020 -- Local variables
5022 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5023 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5025 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5026 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5027 -- Save the SPARK_Mode-related data for restore on exit
5029 Vis_Prims_List : Elist_Id := No_Elist;
5030 -- List of primitives made temporarily visible in the instantiation
5031 -- to match the visibility of the formal type
5033 -- Start of processing for Analyze_Subprogram_Instantiation
5035 begin
5036 Check_SPARK_05_Restriction ("generic is not allowed", N);
5038 -- Very first thing: check for special Text_IO unit in case we are
5039 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5040 -- such an instantiation is bogus (these are packages, not subprograms),
5041 -- but we get a better error message if we do this.
5043 Check_Text_IO_Special_Unit (Gen_Id);
5045 -- Make node global for error reporting
5047 Instantiation_Node := N;
5049 -- For package instantiations we turn off style checks, because they
5050 -- will have been emitted in the generic. For subprogram instantiations
5051 -- we want to apply at least the check on overriding indicators so we
5052 -- do not modify the style check status.
5054 -- The renaming declarations for the actuals do not come from source and
5055 -- will not generate spurious warnings.
5057 Preanalyze_Actuals (N);
5059 Init_Env;
5060 Env_Installed := True;
5061 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5062 Gen_Unit := Entity (Gen_Id);
5064 Generate_Reference (Gen_Unit, Gen_Id);
5066 if Nkind (Gen_Id) = N_Identifier
5067 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5068 then
5069 Error_Msg_NE
5070 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5071 end if;
5073 if Etype (Gen_Unit) = Any_Type then
5074 Restore_Env;
5075 return;
5076 end if;
5078 -- Verify that it is a generic subprogram of the right kind, and that
5079 -- it does not lead to a circular instantiation.
5081 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5082 Error_Msg_NE
5083 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5085 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5086 Error_Msg_NE
5087 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5089 elsif In_Open_Scopes (Gen_Unit) then
5090 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5092 else
5093 -- If the context of the instance is subject to SPARK_Mode "off" or
5094 -- the annotation is altogether missing, set the global flag which
5095 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5096 -- the instance.
5098 if SPARK_Mode /= On then
5099 Ignore_Pragma_SPARK_Mode := True;
5100 end if;
5102 Set_Entity (Gen_Id, Gen_Unit);
5103 Set_Is_Instantiated (Gen_Unit);
5105 if In_Extended_Main_Source_Unit (N) then
5106 Generate_Reference (Gen_Unit, N);
5107 end if;
5109 -- If renaming, get original unit
5111 if Present (Renamed_Object (Gen_Unit))
5112 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5113 E_Generic_Function)
5114 then
5115 Gen_Unit := Renamed_Object (Gen_Unit);
5116 Set_Is_Instantiated (Gen_Unit);
5117 Generate_Reference (Gen_Unit, N);
5118 end if;
5120 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5121 Error_Msg_Node_2 := Current_Scope;
5122 Error_Msg_NE
5123 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5124 Circularity_Detected := True;
5125 Restore_Hidden_Primitives (Vis_Prims_List);
5126 goto Leave;
5127 end if;
5129 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5131 -- Initialize renamings map, for error checking
5133 Generic_Renamings.Set_Last (0);
5134 Generic_Renamings_HTable.Reset;
5136 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5138 -- Copy original generic tree, to produce text for instantiation
5140 Act_Tree :=
5141 Copy_Generic_Node
5142 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5144 -- Inherit overriding indicator from instance node
5146 Act_Spec := Specification (Act_Tree);
5147 Set_Must_Override (Act_Spec, Must_Override (N));
5148 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5150 Renaming_List :=
5151 Analyze_Associations
5152 (I_Node => N,
5153 Formals => Generic_Formal_Declarations (Act_Tree),
5154 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5156 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5158 -- The subprogram itself cannot contain a nested instance, so the
5159 -- current parent is left empty.
5161 Set_Instance_Env (Gen_Unit, Empty);
5163 -- Build the subprogram declaration, which does not appear in the
5164 -- generic template, and give it a sloc consistent with that of the
5165 -- template.
5167 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5168 Set_Generic_Parent (Act_Spec, Gen_Unit);
5169 Act_Decl :=
5170 Make_Subprogram_Declaration (Sloc (Act_Spec),
5171 Specification => Act_Spec);
5173 -- The aspects have been copied previously, but they have to be
5174 -- linked explicitly to the new subprogram declaration. Explicit
5175 -- pre/postconditions on the instance are analyzed below, in a
5176 -- separate step.
5178 Move_Aspects (Act_Tree, To => Act_Decl);
5179 Set_Categorization_From_Pragmas (Act_Decl);
5181 if Parent_Installed then
5182 Hide_Current_Scope;
5183 end if;
5185 Append (Act_Decl, Renaming_List);
5187 -- Contract-related source pragmas that follow a generic subprogram
5188 -- must be instantiated explicitly because they are not part of the
5189 -- subprogram template.
5191 Instantiate_Subprogram_Contract
5192 (Original_Node (Gen_Decl), Renaming_List);
5194 Build_Subprogram_Renaming;
5195 Analyze_Instance_And_Renamings;
5197 -- If the generic is marked Import (Intrinsic), then so is the
5198 -- instance. This indicates that there is no body to instantiate. If
5199 -- generic is marked inline, so it the instance, and the anonymous
5200 -- subprogram it renames. If inlined, or else if inlining is enabled
5201 -- for the compilation, we generate the instance body even if it is
5202 -- not within the main unit.
5204 if Is_Intrinsic_Subprogram (Gen_Unit) then
5205 Set_Is_Intrinsic_Subprogram (Anon_Id);
5206 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5208 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5209 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5210 end if;
5211 end if;
5213 -- Inherit convention from generic unit. Intrinsic convention, as for
5214 -- an instance of unchecked conversion, is not inherited because an
5215 -- explicit Ada instance has been created.
5217 if Has_Convention_Pragma (Gen_Unit)
5218 and then Convention (Gen_Unit) /= Convention_Intrinsic
5219 then
5220 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5221 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5222 end if;
5224 Generate_Definition (Act_Decl_Id);
5226 -- Inherit all inlining-related flags which apply to the generic in
5227 -- the subprogram and its declaration.
5229 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5230 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5232 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5233 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5235 Set_Has_Pragma_Inline_Always
5236 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5237 Set_Has_Pragma_Inline_Always
5238 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5240 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5241 Check_Elab_Instantiation (N);
5242 end if;
5244 if Is_Dispatching_Operation (Act_Decl_Id)
5245 and then Ada_Version >= Ada_2005
5246 then
5247 declare
5248 Formal : Entity_Id;
5250 begin
5251 Formal := First_Formal (Act_Decl_Id);
5252 while Present (Formal) loop
5253 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5254 and then Is_Controlling_Formal (Formal)
5255 and then not Can_Never_Be_Null (Formal)
5256 then
5257 Error_Msg_NE
5258 ("access parameter& is controlling,", N, Formal);
5259 Error_Msg_NE
5260 ("\corresponding parameter of & must be "
5261 & "explicitly null-excluding", N, Gen_Id);
5262 end if;
5264 Next_Formal (Formal);
5265 end loop;
5266 end;
5267 end if;
5269 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5271 Validate_Categorization_Dependency (N, Act_Decl_Id);
5273 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5274 Inherit_Context (Gen_Decl, N);
5276 Restore_Private_Views (Pack_Id, False);
5278 -- If the context requires a full instantiation, mark node for
5279 -- subsequent construction of the body.
5281 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5282 Check_Forward_Instantiation (Gen_Decl);
5284 -- The wrapper package is always delayed, because it does not
5285 -- constitute a freeze point, but to insure that the freeze node
5286 -- is placed properly, it is created directly when instantiating
5287 -- the body (otherwise the freeze node might appear to early for
5288 -- nested instantiations). For ASIS purposes, indicate that the
5289 -- wrapper package has replaced the instantiation node.
5291 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5292 Rewrite (N, Unit (Parent (N)));
5293 Set_Unit (Parent (N), N);
5294 end if;
5296 -- Replace instance node for library-level instantiations of
5297 -- intrinsic subprograms, for ASIS use.
5299 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5300 Rewrite (N, Unit (Parent (N)));
5301 Set_Unit (Parent (N), N);
5302 end if;
5304 if Parent_Installed then
5305 Remove_Parent;
5306 end if;
5308 Restore_Hidden_Primitives (Vis_Prims_List);
5309 Restore_Env;
5310 Env_Installed := False;
5311 Generic_Renamings.Set_Last (0);
5312 Generic_Renamings_HTable.Reset;
5314 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5315 SPARK_Mode := Save_SM;
5316 SPARK_Mode_Pragma := Save_SMP;
5318 if SPARK_Mode = On then
5319 Dynamic_Elaboration_Checks := False;
5320 end if;
5321 end if;
5323 <<Leave>>
5324 if Has_Aspects (N) then
5325 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5326 end if;
5328 exception
5329 when Instantiation_Error =>
5330 if Parent_Installed then
5331 Remove_Parent;
5332 end if;
5334 if Env_Installed then
5335 Restore_Env;
5336 end if;
5338 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5339 SPARK_Mode := Save_SM;
5340 SPARK_Mode_Pragma := Save_SMP;
5342 if SPARK_Mode = On then
5343 Dynamic_Elaboration_Checks := False;
5344 end if;
5345 end Analyze_Subprogram_Instantiation;
5347 -------------------------
5348 -- Get_Associated_Node --
5349 -------------------------
5351 function Get_Associated_Node (N : Node_Id) return Node_Id is
5352 Assoc : Node_Id;
5354 begin
5355 Assoc := Associated_Node (N);
5357 if Nkind (Assoc) /= Nkind (N) then
5358 return Assoc;
5360 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5361 return Assoc;
5363 else
5364 -- If the node is part of an inner generic, it may itself have been
5365 -- remapped into a further generic copy. Associated_Node is otherwise
5366 -- used for the entity of the node, and will be of a different node
5367 -- kind, or else N has been rewritten as a literal or function call.
5369 while Present (Associated_Node (Assoc))
5370 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5371 loop
5372 Assoc := Associated_Node (Assoc);
5373 end loop;
5375 -- Follow and additional link in case the final node was rewritten.
5376 -- This can only happen with nested generic units.
5378 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5379 and then Present (Associated_Node (Assoc))
5380 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5381 N_Explicit_Dereference,
5382 N_Integer_Literal,
5383 N_Real_Literal,
5384 N_String_Literal))
5385 then
5386 Assoc := Associated_Node (Assoc);
5387 end if;
5389 -- An additional special case: an unconstrained type in an object
5390 -- declaration may have been rewritten as a local subtype constrained
5391 -- by the expression in the declaration. We need to recover the
5392 -- original entity which may be global.
5394 if Present (Original_Node (Assoc))
5395 and then Nkind (Parent (N)) = N_Object_Declaration
5396 then
5397 Assoc := Original_Node (Assoc);
5398 end if;
5400 return Assoc;
5401 end if;
5402 end Get_Associated_Node;
5404 ----------------------------
5405 -- Build_Function_Wrapper --
5406 ----------------------------
5408 function Build_Function_Wrapper
5409 (Formal_Subp : Entity_Id;
5410 Actual_Subp : Entity_Id) return Node_Id
5412 Loc : constant Source_Ptr := Sloc (Current_Scope);
5413 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5414 Actuals : List_Id;
5415 Decl : Node_Id;
5416 Func_Name : Node_Id;
5417 Func : Entity_Id;
5418 Parm_Type : Node_Id;
5419 Profile : List_Id := New_List;
5420 Spec : Node_Id;
5421 Act_F : Entity_Id;
5422 Form_F : Entity_Id;
5423 New_F : Entity_Id;
5425 begin
5426 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5428 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5429 Set_Ekind (Func, E_Function);
5430 Set_Is_Generic_Actual_Subprogram (Func);
5432 Actuals := New_List;
5433 Profile := New_List;
5435 Act_F := First_Formal (Actual_Subp);
5436 Form_F := First_Formal (Formal_Subp);
5437 while Present (Form_F) loop
5439 -- Create new formal for profile of wrapper, and add a reference
5440 -- to it in the list of actuals for the enclosing call. The name
5441 -- must be that of the formal in the formal subprogram, because
5442 -- calls to it in the generic body may use named associations.
5444 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5446 Parm_Type :=
5447 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5449 Append_To (Profile,
5450 Make_Parameter_Specification (Loc,
5451 Defining_Identifier => New_F,
5452 Parameter_Type => Parm_Type));
5454 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5455 Next_Formal (Form_F);
5457 if Present (Act_F) then
5458 Next_Formal (Act_F);
5459 end if;
5460 end loop;
5462 Spec :=
5463 Make_Function_Specification (Loc,
5464 Defining_Unit_Name => Func,
5465 Parameter_Specifications => Profile,
5466 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5468 Decl :=
5469 Make_Expression_Function (Loc,
5470 Specification => Spec,
5471 Expression =>
5472 Make_Function_Call (Loc,
5473 Name => Func_Name,
5474 Parameter_Associations => Actuals));
5476 return Decl;
5477 end Build_Function_Wrapper;
5479 ----------------------------
5480 -- Build_Operator_Wrapper --
5481 ----------------------------
5483 function Build_Operator_Wrapper
5484 (Formal_Subp : Entity_Id;
5485 Actual_Subp : Entity_Id) return Node_Id
5487 Loc : constant Source_Ptr := Sloc (Current_Scope);
5488 Ret_Type : constant Entity_Id :=
5489 Get_Instance_Of (Etype (Formal_Subp));
5490 Op_Type : constant Entity_Id :=
5491 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5492 Is_Binary : constant Boolean :=
5493 Present (Next_Formal (First_Formal (Formal_Subp)));
5495 Decl : Node_Id;
5496 Expr : Node_Id;
5497 F1, F2 : Entity_Id;
5498 Func : Entity_Id;
5499 Op_Name : Name_Id;
5500 Spec : Node_Id;
5501 L, R : Node_Id;
5503 begin
5504 Op_Name := Chars (Actual_Subp);
5506 -- Create entities for wrapper function and its formals
5508 F1 := Make_Temporary (Loc, 'A');
5509 F2 := Make_Temporary (Loc, 'B');
5510 L := New_Occurrence_Of (F1, Loc);
5511 R := New_Occurrence_Of (F2, Loc);
5513 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5514 Set_Ekind (Func, E_Function);
5515 Set_Is_Generic_Actual_Subprogram (Func);
5517 Spec :=
5518 Make_Function_Specification (Loc,
5519 Defining_Unit_Name => Func,
5520 Parameter_Specifications => New_List (
5521 Make_Parameter_Specification (Loc,
5522 Defining_Identifier => F1,
5523 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5524 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5526 if Is_Binary then
5527 Append_To (Parameter_Specifications (Spec),
5528 Make_Parameter_Specification (Loc,
5529 Defining_Identifier => F2,
5530 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5531 end if;
5533 -- Build expression as a function call, or as an operator node
5534 -- that corresponds to the name of the actual, starting with
5535 -- binary operators.
5537 if Op_Name not in Any_Operator_Name then
5538 Expr :=
5539 Make_Function_Call (Loc,
5540 Name =>
5541 New_Occurrence_Of (Actual_Subp, Loc),
5542 Parameter_Associations => New_List (L));
5544 if Is_Binary then
5545 Append_To (Parameter_Associations (Expr), R);
5546 end if;
5548 -- Binary operators
5550 elsif Is_Binary then
5551 if Op_Name = Name_Op_And then
5552 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5553 elsif Op_Name = Name_Op_Or then
5554 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5555 elsif Op_Name = Name_Op_Xor then
5556 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5557 elsif Op_Name = Name_Op_Eq then
5558 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5559 elsif Op_Name = Name_Op_Ne then
5560 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5561 elsif Op_Name = Name_Op_Le then
5562 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5563 elsif Op_Name = Name_Op_Gt then
5564 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5565 elsif Op_Name = Name_Op_Ge then
5566 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5567 elsif Op_Name = Name_Op_Lt then
5568 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5569 elsif Op_Name = Name_Op_Add then
5570 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5571 elsif Op_Name = Name_Op_Subtract then
5572 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5573 elsif Op_Name = Name_Op_Concat then
5574 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5575 elsif Op_Name = Name_Op_Multiply then
5576 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5577 elsif Op_Name = Name_Op_Divide then
5578 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5579 elsif Op_Name = Name_Op_Mod then
5580 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5581 elsif Op_Name = Name_Op_Rem then
5582 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5583 elsif Op_Name = Name_Op_Expon then
5584 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5585 end if;
5587 -- Unary operators
5589 else
5590 if Op_Name = Name_Op_Add then
5591 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5592 elsif Op_Name = Name_Op_Subtract then
5593 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5594 elsif Op_Name = Name_Op_Abs then
5595 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5596 elsif Op_Name = Name_Op_Not then
5597 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5598 end if;
5599 end if;
5601 Decl :=
5602 Make_Expression_Function (Loc,
5603 Specification => Spec,
5604 Expression => Expr);
5606 return Decl;
5607 end Build_Operator_Wrapper;
5609 -------------------------------------------
5610 -- Build_Instance_Compilation_Unit_Nodes --
5611 -------------------------------------------
5613 procedure Build_Instance_Compilation_Unit_Nodes
5614 (N : Node_Id;
5615 Act_Body : Node_Id;
5616 Act_Decl : Node_Id)
5618 Decl_Cunit : Node_Id;
5619 Body_Cunit : Node_Id;
5620 Citem : Node_Id;
5621 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5622 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5624 begin
5625 -- A new compilation unit node is built for the instance declaration
5627 Decl_Cunit :=
5628 Make_Compilation_Unit (Sloc (N),
5629 Context_Items => Empty_List,
5630 Unit => Act_Decl,
5631 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5633 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5635 -- The new compilation unit is linked to its body, but both share the
5636 -- same file, so we do not set Body_Required on the new unit so as not
5637 -- to create a spurious dependency on a non-existent body in the ali.
5638 -- This simplifies CodePeer unit traversal.
5640 -- We use the original instantiation compilation unit as the resulting
5641 -- compilation unit of the instance, since this is the main unit.
5643 Rewrite (N, Act_Body);
5645 -- Propagate the aspect specifications from the package body template to
5646 -- the instantiated version of the package body.
5648 if Has_Aspects (Act_Body) then
5649 Set_Aspect_Specifications
5650 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5651 end if;
5653 Body_Cunit := Parent (N);
5655 -- The two compilation unit nodes are linked by the Library_Unit field
5657 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5658 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5660 -- Preserve the private nature of the package if needed
5662 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5664 -- If the instance is not the main unit, its context, categorization
5665 -- and elaboration entity are not relevant to the compilation.
5667 if Body_Cunit /= Cunit (Main_Unit) then
5668 Make_Instance_Unit (Body_Cunit, In_Main => False);
5669 return;
5670 end if;
5672 -- The context clause items on the instantiation, which are now attached
5673 -- to the body compilation unit (since the body overwrote the original
5674 -- instantiation node), semantically belong on the spec, so copy them
5675 -- there. It's harmless to leave them on the body as well. In fact one
5676 -- could argue that they belong in both places.
5678 Citem := First (Context_Items (Body_Cunit));
5679 while Present (Citem) loop
5680 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5681 Next (Citem);
5682 end loop;
5684 -- Propagate categorization flags on packages, so that they appear in
5685 -- the ali file for the spec of the unit.
5687 if Ekind (New_Main) = E_Package then
5688 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5689 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5690 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5691 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5692 Set_Is_Remote_Call_Interface
5693 (Old_Main, Is_Remote_Call_Interface (New_Main));
5694 end if;
5696 -- Make entry in Units table, so that binder can generate call to
5697 -- elaboration procedure for body, if any.
5699 Make_Instance_Unit (Body_Cunit, In_Main => True);
5700 Main_Unit_Entity := New_Main;
5701 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5703 -- Build elaboration entity, since the instance may certainly generate
5704 -- elaboration code requiring a flag for protection.
5706 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5707 end Build_Instance_Compilation_Unit_Nodes;
5709 -----------------------------
5710 -- Check_Access_Definition --
5711 -----------------------------
5713 procedure Check_Access_Definition (N : Node_Id) is
5714 begin
5715 pragma Assert
5716 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5717 null;
5718 end Check_Access_Definition;
5720 -----------------------------------
5721 -- Check_Formal_Package_Instance --
5722 -----------------------------------
5724 -- If the formal has specific parameters, they must match those of the
5725 -- actual. Both of them are instances, and the renaming declarations for
5726 -- their formal parameters appear in the same order in both. The analyzed
5727 -- formal has been analyzed in the context of the current instance.
5729 procedure Check_Formal_Package_Instance
5730 (Formal_Pack : Entity_Id;
5731 Actual_Pack : Entity_Id)
5733 E1 : Entity_Id := First_Entity (Actual_Pack);
5734 E2 : Entity_Id := First_Entity (Formal_Pack);
5736 Expr1 : Node_Id;
5737 Expr2 : Node_Id;
5739 procedure Check_Mismatch (B : Boolean);
5740 -- Common error routine for mismatch between the parameters of the
5741 -- actual instance and those of the formal package.
5743 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5744 -- The formal may come from a nested formal package, and the actual may
5745 -- have been constant-folded. To determine whether the two denote the
5746 -- same entity we may have to traverse several definitions to recover
5747 -- the ultimate entity that they refer to.
5749 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5750 -- The formal and the actual must be identical, but if both are
5751 -- given by attributes they end up renaming different generated bodies,
5752 -- and we must verify that the attributes themselves match.
5754 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5755 -- Similarly, if the formal comes from a nested formal package, the
5756 -- actual may designate the formal through multiple renamings, which
5757 -- have to be followed to determine the original variable in question.
5759 --------------------
5760 -- Check_Mismatch --
5761 --------------------
5763 procedure Check_Mismatch (B : Boolean) is
5764 -- A Formal_Type_Declaration for a derived private type is rewritten
5765 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5766 -- which is why we examine the original node.
5768 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5770 begin
5771 if Kind = N_Formal_Type_Declaration then
5772 return;
5774 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5775 N_Formal_Package_Declaration)
5776 or else Kind in N_Formal_Subprogram_Declaration
5777 then
5778 null;
5780 -- Ada 2012: If both formal and actual are incomplete types they
5781 -- are conformant.
5783 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5784 null;
5786 elsif B then
5787 Error_Msg_NE
5788 ("actual for & in actual instance does not match formal",
5789 Parent (Actual_Pack), E1);
5790 end if;
5791 end Check_Mismatch;
5793 --------------------------------
5794 -- Same_Instantiated_Constant --
5795 --------------------------------
5797 function Same_Instantiated_Constant
5798 (E1, E2 : Entity_Id) return Boolean
5800 Ent : Entity_Id;
5802 begin
5803 Ent := E2;
5804 while Present (Ent) loop
5805 if E1 = Ent then
5806 return True;
5808 elsif Ekind (Ent) /= E_Constant then
5809 return False;
5811 elsif Is_Entity_Name (Constant_Value (Ent)) then
5812 if Entity (Constant_Value (Ent)) = E1 then
5813 return True;
5814 else
5815 Ent := Entity (Constant_Value (Ent));
5816 end if;
5818 -- The actual may be a constant that has been folded. Recover
5819 -- original name.
5821 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5822 Ent := Entity (Original_Node (Constant_Value (Ent)));
5824 else
5825 return False;
5826 end if;
5827 end loop;
5829 return False;
5830 end Same_Instantiated_Constant;
5832 --------------------------------
5833 -- Same_Instantiated_Function --
5834 --------------------------------
5836 function Same_Instantiated_Function
5837 (E1, E2 : Entity_Id) return Boolean
5839 U1, U2 : Node_Id;
5840 begin
5841 if Alias (E1) = Alias (E2) then
5842 return True;
5844 elsif Present (Alias (E2)) then
5845 U1 := Original_Node (Unit_Declaration_Node (E1));
5846 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5848 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5849 and then Nkind (Name (U1)) = N_Attribute_Reference
5851 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5852 and then Nkind (Name (U2)) = N_Attribute_Reference
5854 and then
5855 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5856 else
5857 return False;
5858 end if;
5859 end Same_Instantiated_Function;
5861 --------------------------------
5862 -- Same_Instantiated_Variable --
5863 --------------------------------
5865 function Same_Instantiated_Variable
5866 (E1, E2 : Entity_Id) return Boolean
5868 function Original_Entity (E : Entity_Id) return Entity_Id;
5869 -- Follow chain of renamings to the ultimate ancestor
5871 ---------------------
5872 -- Original_Entity --
5873 ---------------------
5875 function Original_Entity (E : Entity_Id) return Entity_Id is
5876 Orig : Entity_Id;
5878 begin
5879 Orig := E;
5880 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5881 and then Present (Renamed_Object (Orig))
5882 and then Is_Entity_Name (Renamed_Object (Orig))
5883 loop
5884 Orig := Entity (Renamed_Object (Orig));
5885 end loop;
5887 return Orig;
5888 end Original_Entity;
5890 -- Start of processing for Same_Instantiated_Variable
5892 begin
5893 return Ekind (E1) = Ekind (E2)
5894 and then Original_Entity (E1) = Original_Entity (E2);
5895 end Same_Instantiated_Variable;
5897 -- Start of processing for Check_Formal_Package_Instance
5899 begin
5900 while Present (E1) and then Present (E2) loop
5901 exit when Ekind (E1) = E_Package
5902 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5904 -- If the formal is the renaming of the formal package, this
5905 -- is the end of its formal part, which may occur before the
5906 -- end of the formal part in the actual in the presence of
5907 -- defaulted parameters in the formal package.
5909 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5910 and then Renamed_Entity (E2) = Scope (E2);
5912 -- The analysis of the actual may generate additional internal
5913 -- entities. If the formal is defaulted, there is no corresponding
5914 -- analysis and the internal entities must be skipped, until we
5915 -- find corresponding entities again.
5917 if Comes_From_Source (E2)
5918 and then not Comes_From_Source (E1)
5919 and then Chars (E1) /= Chars (E2)
5920 then
5921 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5922 Next_Entity (E1);
5923 end loop;
5924 end if;
5926 if No (E1) then
5927 return;
5929 -- If the formal entity comes from a formal declaration, it was
5930 -- defaulted in the formal package, and no check is needed on it.
5932 elsif Nkind_In (Original_Node (Parent (E2)),
5933 N_Formal_Object_Declaration,
5934 N_Formal_Type_Declaration)
5935 then
5936 goto Next_E;
5938 -- Ditto for defaulted formal subprograms.
5940 elsif Is_Overloadable (E1)
5941 and then Nkind (Unit_Declaration_Node (E2)) in
5942 N_Formal_Subprogram_Declaration
5943 then
5944 goto Next_E;
5946 elsif Is_Type (E1) then
5948 -- Subtypes must statically match. E1, E2 are the local entities
5949 -- that are subtypes of the actuals. Itypes generated for other
5950 -- parameters need not be checked, the check will be performed
5951 -- on the parameters themselves.
5953 -- If E2 is a formal type declaration, it is a defaulted parameter
5954 -- and needs no checking.
5956 if not Is_Itype (E1) and then not Is_Itype (E2) then
5957 Check_Mismatch
5958 (not Is_Type (E2)
5959 or else Etype (E1) /= Etype (E2)
5960 or else not Subtypes_Statically_Match (E1, E2));
5961 end if;
5963 elsif Ekind (E1) = E_Constant then
5965 -- IN parameters must denote the same static value, or the same
5966 -- constant, or the literal null.
5968 Expr1 := Expression (Parent (E1));
5970 if Ekind (E2) /= E_Constant then
5971 Check_Mismatch (True);
5972 goto Next_E;
5973 else
5974 Expr2 := Expression (Parent (E2));
5975 end if;
5977 if Is_OK_Static_Expression (Expr1) then
5978 if not Is_OK_Static_Expression (Expr2) then
5979 Check_Mismatch (True);
5981 elsif Is_Discrete_Type (Etype (E1)) then
5982 declare
5983 V1 : constant Uint := Expr_Value (Expr1);
5984 V2 : constant Uint := Expr_Value (Expr2);
5985 begin
5986 Check_Mismatch (V1 /= V2);
5987 end;
5989 elsif Is_Real_Type (Etype (E1)) then
5990 declare
5991 V1 : constant Ureal := Expr_Value_R (Expr1);
5992 V2 : constant Ureal := Expr_Value_R (Expr2);
5993 begin
5994 Check_Mismatch (V1 /= V2);
5995 end;
5997 elsif Is_String_Type (Etype (E1))
5998 and then Nkind (Expr1) = N_String_Literal
5999 then
6000 if Nkind (Expr2) /= N_String_Literal then
6001 Check_Mismatch (True);
6002 else
6003 Check_Mismatch
6004 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6005 end if;
6006 end if;
6008 elsif Is_Entity_Name (Expr1) then
6009 if Is_Entity_Name (Expr2) then
6010 if Entity (Expr1) = Entity (Expr2) then
6011 null;
6012 else
6013 Check_Mismatch
6014 (not Same_Instantiated_Constant
6015 (Entity (Expr1), Entity (Expr2)));
6016 end if;
6018 else
6019 Check_Mismatch (True);
6020 end if;
6022 elsif Is_Entity_Name (Original_Node (Expr1))
6023 and then Is_Entity_Name (Expr2)
6024 and then Same_Instantiated_Constant
6025 (Entity (Original_Node (Expr1)), Entity (Expr2))
6026 then
6027 null;
6029 elsif Nkind (Expr1) = N_Null then
6030 Check_Mismatch (Nkind (Expr1) /= N_Null);
6032 else
6033 Check_Mismatch (True);
6034 end if;
6036 elsif Ekind (E1) = E_Variable then
6037 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6039 elsif Ekind (E1) = E_Package then
6040 Check_Mismatch
6041 (Ekind (E1) /= Ekind (E2)
6042 or else Renamed_Object (E1) /= Renamed_Object (E2));
6044 elsif Is_Overloadable (E1) then
6046 -- Verify that the actual subprograms match. Note that actuals
6047 -- that are attributes are rewritten as subprograms. If the
6048 -- subprogram in the formal package is defaulted, no check is
6049 -- needed. Note that this can only happen in Ada 2005 when the
6050 -- formal package can be partially parameterized.
6052 if Nkind (Unit_Declaration_Node (E1)) =
6053 N_Subprogram_Renaming_Declaration
6054 and then From_Default (Unit_Declaration_Node (E1))
6055 then
6056 null;
6058 -- If the formal package has an "others" box association that
6059 -- covers this formal, there is no need for a check either.
6061 elsif Nkind (Unit_Declaration_Node (E2)) in
6062 N_Formal_Subprogram_Declaration
6063 and then Box_Present (Unit_Declaration_Node (E2))
6064 then
6065 null;
6067 -- No check needed if subprogram is a defaulted null procedure
6069 elsif No (Alias (E2))
6070 and then Ekind (E2) = E_Procedure
6071 and then
6072 Null_Present (Specification (Unit_Declaration_Node (E2)))
6073 then
6074 null;
6076 -- Otherwise the actual in the formal and the actual in the
6077 -- instantiation of the formal must match, up to renamings.
6079 else
6080 Check_Mismatch
6081 (Ekind (E2) /= Ekind (E1)
6082 or else not Same_Instantiated_Function (E1, E2));
6083 end if;
6085 else
6086 raise Program_Error;
6087 end if;
6089 <<Next_E>>
6090 Next_Entity (E1);
6091 Next_Entity (E2);
6092 end loop;
6093 end Check_Formal_Package_Instance;
6095 ---------------------------
6096 -- Check_Formal_Packages --
6097 ---------------------------
6099 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6100 E : Entity_Id;
6101 Formal_P : Entity_Id;
6102 Formal_Decl : Node_Id;
6104 begin
6105 -- Iterate through the declarations in the instance, looking for package
6106 -- renaming declarations that denote instances of formal packages. Stop
6107 -- when we find the renaming of the current package itself. The
6108 -- declaration for a formal package without a box is followed by an
6109 -- internal entity that repeats the instantiation.
6111 E := First_Entity (P_Id);
6112 while Present (E) loop
6113 if Ekind (E) = E_Package then
6114 if Renamed_Object (E) = P_Id then
6115 exit;
6117 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6118 null;
6120 else
6121 Formal_Decl := Parent (Associated_Formal_Package (E));
6123 -- Nothing to check if the formal has a box or an others_clause
6124 -- (necessarily with a box).
6126 if Box_Present (Formal_Decl) then
6127 null;
6129 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6130 N_Others_Choice
6131 then
6132 -- The internal validating package was generated but formal
6133 -- and instance are known to be compatible.
6135 Formal_P := Next_Entity (E);
6136 Remove (Unit_Declaration_Node (Formal_P));
6138 else
6139 Formal_P := Next_Entity (E);
6140 Check_Formal_Package_Instance (Formal_P, E);
6142 -- After checking, remove the internal validating package.
6143 -- It is only needed for semantic checks, and as it may
6144 -- contain generic formal declarations it should not reach
6145 -- gigi.
6147 Remove (Unit_Declaration_Node (Formal_P));
6148 end if;
6149 end if;
6150 end if;
6152 Next_Entity (E);
6153 end loop;
6154 end Check_Formal_Packages;
6156 ---------------------------------
6157 -- Check_Forward_Instantiation --
6158 ---------------------------------
6160 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6161 S : Entity_Id;
6162 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6164 begin
6165 -- The instantiation appears before the generic body if we are in the
6166 -- scope of the unit containing the generic, either in its spec or in
6167 -- the package body, and before the generic body.
6169 if Ekind (Gen_Comp) = E_Package_Body then
6170 Gen_Comp := Spec_Entity (Gen_Comp);
6171 end if;
6173 if In_Open_Scopes (Gen_Comp)
6174 and then No (Corresponding_Body (Decl))
6175 then
6176 S := Current_Scope;
6178 while Present (S)
6179 and then not Is_Compilation_Unit (S)
6180 and then not Is_Child_Unit (S)
6181 loop
6182 if Ekind (S) = E_Package then
6183 Set_Has_Forward_Instantiation (S);
6184 end if;
6186 S := Scope (S);
6187 end loop;
6188 end if;
6189 end Check_Forward_Instantiation;
6191 ---------------------------
6192 -- Check_Generic_Actuals --
6193 ---------------------------
6195 -- The visibility of the actuals may be different between the point of
6196 -- generic instantiation and the instantiation of the body.
6198 procedure Check_Generic_Actuals
6199 (Instance : Entity_Id;
6200 Is_Formal_Box : Boolean)
6202 E : Entity_Id;
6203 Astype : Entity_Id;
6205 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6206 -- For a formal that is an array type, the component type is often a
6207 -- previous formal in the same unit. The privacy status of the component
6208 -- type will have been examined earlier in the traversal of the
6209 -- corresponding actuals, and this status should not be modified for
6210 -- the array (sub)type itself. However, if the base type of the array
6211 -- (sub)type is private, its full view must be restored in the body to
6212 -- be consistent with subsequent index subtypes, etc.
6214 -- To detect this case we have to rescan the list of formals, which is
6215 -- usually short enough to ignore the resulting inefficiency.
6217 -----------------------------
6218 -- Denotes_Previous_Actual --
6219 -----------------------------
6221 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6222 Prev : Entity_Id;
6224 begin
6225 Prev := First_Entity (Instance);
6226 while Present (Prev) loop
6227 if Is_Type (Prev)
6228 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6229 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6230 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6231 then
6232 return True;
6234 elsif Prev = E then
6235 return False;
6237 else
6238 Next_Entity (Prev);
6239 end if;
6240 end loop;
6242 return False;
6243 end Denotes_Previous_Actual;
6245 -- Start of processing for Check_Generic_Actuals
6247 begin
6248 E := First_Entity (Instance);
6249 while Present (E) loop
6250 if Is_Type (E)
6251 and then Nkind (Parent (E)) = N_Subtype_Declaration
6252 and then Scope (Etype (E)) /= Instance
6253 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6254 then
6255 if Is_Array_Type (E)
6256 and then not Is_Private_Type (Etype (E))
6257 and then Denotes_Previous_Actual (Component_Type (E))
6258 then
6259 null;
6260 else
6261 Check_Private_View (Subtype_Indication (Parent (E)));
6262 end if;
6264 Set_Is_Generic_Actual_Type (E, True);
6265 Set_Is_Hidden (E, False);
6266 Set_Is_Potentially_Use_Visible (E,
6267 In_Use (Instance));
6269 -- We constructed the generic actual type as a subtype of the
6270 -- supplied type. This means that it normally would not inherit
6271 -- subtype specific attributes of the actual, which is wrong for
6272 -- the generic case.
6274 Astype := Ancestor_Subtype (E);
6276 if No (Astype) then
6278 -- This can happen when E is an itype that is the full view of
6279 -- a private type completed, e.g. with a constrained array. In
6280 -- that case, use the first subtype, which will carry size
6281 -- information. The base type itself is unconstrained and will
6282 -- not carry it.
6284 Astype := First_Subtype (E);
6285 end if;
6287 Set_Size_Info (E, (Astype));
6288 Set_RM_Size (E, RM_Size (Astype));
6289 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6291 if Is_Discrete_Or_Fixed_Point_Type (E) then
6292 Set_RM_Size (E, RM_Size (Astype));
6294 -- In nested instances, the base type of an access actual may
6295 -- itself be private, and need to be exchanged.
6297 elsif Is_Access_Type (E)
6298 and then Is_Private_Type (Etype (E))
6299 then
6300 Check_Private_View
6301 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6302 end if;
6304 elsif Ekind (E) = E_Package then
6306 -- If this is the renaming for the current instance, we're done.
6307 -- Otherwise it is a formal package. If the corresponding formal
6308 -- was declared with a box, the (instantiations of the) generic
6309 -- formal part are also visible. Otherwise, ignore the entity
6310 -- created to validate the actuals.
6312 if Renamed_Object (E) = Instance then
6313 exit;
6315 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6316 null;
6318 -- The visibility of a formal of an enclosing generic is already
6319 -- correct.
6321 elsif Denotes_Formal_Package (E) then
6322 null;
6324 elsif Present (Associated_Formal_Package (E))
6325 and then not Is_Generic_Formal (E)
6326 then
6327 if Box_Present (Parent (Associated_Formal_Package (E))) then
6328 Check_Generic_Actuals (Renamed_Object (E), True);
6330 else
6331 Check_Generic_Actuals (Renamed_Object (E), False);
6332 end if;
6334 Set_Is_Hidden (E, False);
6335 end if;
6337 -- If this is a subprogram instance (in a wrapper package) the
6338 -- actual is fully visible.
6340 elsif Is_Wrapper_Package (Instance) then
6341 Set_Is_Hidden (E, False);
6343 -- If the formal package is declared with a box, or if the formal
6344 -- parameter is defaulted, it is visible in the body.
6346 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6347 Set_Is_Hidden (E, False);
6348 end if;
6350 if Ekind (E) = E_Constant then
6352 -- If the type of the actual is a private type declared in the
6353 -- enclosing scope of the generic unit, the body of the generic
6354 -- sees the full view of the type (because it has to appear in
6355 -- the corresponding package body). If the type is private now,
6356 -- exchange views to restore the proper visiblity in the instance.
6358 declare
6359 Typ : constant Entity_Id := Base_Type (Etype (E));
6360 -- The type of the actual
6362 Gen_Id : Entity_Id;
6363 -- The generic unit
6365 Parent_Scope : Entity_Id;
6366 -- The enclosing scope of the generic unit
6368 begin
6369 if Is_Wrapper_Package (Instance) then
6370 Gen_Id :=
6371 Generic_Parent
6372 (Specification
6373 (Unit_Declaration_Node
6374 (Related_Instance (Instance))));
6375 else
6376 Gen_Id :=
6377 Generic_Parent (Package_Specification (Instance));
6378 end if;
6380 Parent_Scope := Scope (Gen_Id);
6382 -- The exchange is only needed if the generic is defined
6383 -- within a package which is not a common ancestor of the
6384 -- scope of the instance, and is not already in scope.
6386 if Is_Private_Type (Typ)
6387 and then Scope (Typ) = Parent_Scope
6388 and then Scope (Instance) /= Parent_Scope
6389 and then Ekind (Parent_Scope) = E_Package
6390 and then not Is_Child_Unit (Gen_Id)
6391 then
6392 Switch_View (Typ);
6394 -- If the type of the entity is a subtype, it may also have
6395 -- to be made visible, together with the base type of its
6396 -- full view, after exchange.
6398 if Is_Private_Type (Etype (E)) then
6399 Switch_View (Etype (E));
6400 Switch_View (Base_Type (Etype (E)));
6401 end if;
6402 end if;
6403 end;
6404 end if;
6406 Next_Entity (E);
6407 end loop;
6408 end Check_Generic_Actuals;
6410 ------------------------------
6411 -- Check_Generic_Child_Unit --
6412 ------------------------------
6414 procedure Check_Generic_Child_Unit
6415 (Gen_Id : Node_Id;
6416 Parent_Installed : in out Boolean)
6418 Loc : constant Source_Ptr := Sloc (Gen_Id);
6419 Gen_Par : Entity_Id := Empty;
6420 E : Entity_Id;
6421 Inst_Par : Entity_Id;
6422 S : Node_Id;
6424 function Find_Generic_Child
6425 (Scop : Entity_Id;
6426 Id : Node_Id) return Entity_Id;
6427 -- Search generic parent for possible child unit with the given name
6429 function In_Enclosing_Instance return Boolean;
6430 -- Within an instance of the parent, the child unit may be denoted by
6431 -- a simple name, or an abbreviated expanded name. Examine enclosing
6432 -- scopes to locate a possible parent instantiation.
6434 ------------------------
6435 -- Find_Generic_Child --
6436 ------------------------
6438 function Find_Generic_Child
6439 (Scop : Entity_Id;
6440 Id : Node_Id) return Entity_Id
6442 E : Entity_Id;
6444 begin
6445 -- If entity of name is already set, instance has already been
6446 -- resolved, e.g. in an enclosing instantiation.
6448 if Present (Entity (Id)) then
6449 if Scope (Entity (Id)) = Scop then
6450 return Entity (Id);
6451 else
6452 return Empty;
6453 end if;
6455 else
6456 E := First_Entity (Scop);
6457 while Present (E) loop
6458 if Chars (E) = Chars (Id)
6459 and then Is_Child_Unit (E)
6460 then
6461 if Is_Child_Unit (E)
6462 and then not Is_Visible_Lib_Unit (E)
6463 then
6464 Error_Msg_NE
6465 ("generic child unit& is not visible", Gen_Id, E);
6466 end if;
6468 Set_Entity (Id, E);
6469 return E;
6470 end if;
6472 Next_Entity (E);
6473 end loop;
6475 return Empty;
6476 end if;
6477 end Find_Generic_Child;
6479 ---------------------------
6480 -- In_Enclosing_Instance --
6481 ---------------------------
6483 function In_Enclosing_Instance return Boolean is
6484 Enclosing_Instance : Node_Id;
6485 Instance_Decl : Node_Id;
6487 begin
6488 -- We do not inline any call that contains instantiations, except
6489 -- for instantiations of Unchecked_Conversion, so if we are within
6490 -- an inlined body the current instance does not require parents.
6492 if In_Inlined_Body then
6493 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6494 return False;
6495 end if;
6497 -- Loop to check enclosing scopes
6499 Enclosing_Instance := Current_Scope;
6500 while Present (Enclosing_Instance) loop
6501 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6503 if Ekind (Enclosing_Instance) = E_Package
6504 and then Is_Generic_Instance (Enclosing_Instance)
6505 and then Present
6506 (Generic_Parent (Specification (Instance_Decl)))
6507 then
6508 -- Check whether the generic we are looking for is a child of
6509 -- this instance.
6511 E := Find_Generic_Child
6512 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6513 exit when Present (E);
6515 else
6516 E := Empty;
6517 end if;
6519 Enclosing_Instance := Scope (Enclosing_Instance);
6520 end loop;
6522 if No (E) then
6524 -- Not a child unit
6526 Analyze (Gen_Id);
6527 return False;
6529 else
6530 Rewrite (Gen_Id,
6531 Make_Expanded_Name (Loc,
6532 Chars => Chars (E),
6533 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6534 Selector_Name => New_Occurrence_Of (E, Loc)));
6536 Set_Entity (Gen_Id, E);
6537 Set_Etype (Gen_Id, Etype (E));
6538 Parent_Installed := False; -- Already in scope.
6539 return True;
6540 end if;
6541 end In_Enclosing_Instance;
6543 -- Start of processing for Check_Generic_Child_Unit
6545 begin
6546 -- If the name of the generic is given by a selected component, it may
6547 -- be the name of a generic child unit, and the prefix is the name of an
6548 -- instance of the parent, in which case the child unit must be visible.
6549 -- If this instance is not in scope, it must be placed there and removed
6550 -- after instantiation, because what is being instantiated is not the
6551 -- original child, but the corresponding child present in the instance
6552 -- of the parent.
6554 -- If the child is instantiated within the parent, it can be given by
6555 -- a simple name. In this case the instance is already in scope, but
6556 -- the child generic must be recovered from the generic parent as well.
6558 if Nkind (Gen_Id) = N_Selected_Component then
6559 S := Selector_Name (Gen_Id);
6560 Analyze (Prefix (Gen_Id));
6561 Inst_Par := Entity (Prefix (Gen_Id));
6563 if Ekind (Inst_Par) = E_Package
6564 and then Present (Renamed_Object (Inst_Par))
6565 then
6566 Inst_Par := Renamed_Object (Inst_Par);
6567 end if;
6569 if Ekind (Inst_Par) = E_Package then
6570 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6571 Gen_Par := Generic_Parent (Parent (Inst_Par));
6573 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6574 and then
6575 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6576 then
6577 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6578 end if;
6580 elsif Ekind (Inst_Par) = E_Generic_Package
6581 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6582 then
6583 -- A formal package may be a real child package, and not the
6584 -- implicit instance within a parent. In this case the child is
6585 -- not visible and has to be retrieved explicitly as well.
6587 Gen_Par := Inst_Par;
6588 end if;
6590 if Present (Gen_Par) then
6592 -- The prefix denotes an instantiation. The entity itself may be a
6593 -- nested generic, or a child unit.
6595 E := Find_Generic_Child (Gen_Par, S);
6597 if Present (E) then
6598 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6599 Set_Entity (Gen_Id, E);
6600 Set_Etype (Gen_Id, Etype (E));
6601 Set_Entity (S, E);
6602 Set_Etype (S, Etype (E));
6604 -- Indicate that this is a reference to the parent
6606 if In_Extended_Main_Source_Unit (Gen_Id) then
6607 Set_Is_Instantiated (Inst_Par);
6608 end if;
6610 -- A common mistake is to replicate the naming scheme of a
6611 -- hierarchy by instantiating a generic child directly, rather
6612 -- than the implicit child in a parent instance:
6614 -- generic .. package Gpar is ..
6615 -- generic .. package Gpar.Child is ..
6616 -- package Par is new Gpar ();
6618 -- with Gpar.Child;
6619 -- package Par.Child is new Gpar.Child ();
6620 -- rather than Par.Child
6622 -- In this case the instantiation is within Par, which is an
6623 -- instance, but Gpar does not denote Par because we are not IN
6624 -- the instance of Gpar, so this is illegal. The test below
6625 -- recognizes this particular case.
6627 if Is_Child_Unit (E)
6628 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6629 and then (not In_Instance
6630 or else Nkind (Parent (Parent (Gen_Id))) =
6631 N_Compilation_Unit)
6632 then
6633 Error_Msg_N
6634 ("prefix of generic child unit must be instance of parent",
6635 Gen_Id);
6636 end if;
6638 if not In_Open_Scopes (Inst_Par)
6639 and then Nkind (Parent (Gen_Id)) not in
6640 N_Generic_Renaming_Declaration
6641 then
6642 Install_Parent (Inst_Par);
6643 Parent_Installed := True;
6645 elsif In_Open_Scopes (Inst_Par) then
6647 -- If the parent is already installed, install the actuals
6648 -- for its formal packages. This is necessary when the child
6649 -- instance is a child of the parent instance: in this case,
6650 -- the parent is placed on the scope stack but the formal
6651 -- packages are not made visible.
6653 Install_Formal_Packages (Inst_Par);
6654 end if;
6656 else
6657 -- If the generic parent does not contain an entity that
6658 -- corresponds to the selector, the instance doesn't either.
6659 -- Analyzing the node will yield the appropriate error message.
6660 -- If the entity is not a child unit, then it is an inner
6661 -- generic in the parent.
6663 Analyze (Gen_Id);
6664 end if;
6666 else
6667 Analyze (Gen_Id);
6669 if Is_Child_Unit (Entity (Gen_Id))
6670 and then
6671 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6672 and then not In_Open_Scopes (Inst_Par)
6673 then
6674 Install_Parent (Inst_Par);
6675 Parent_Installed := True;
6677 -- The generic unit may be the renaming of the implicit child
6678 -- present in an instance. In that case the parent instance is
6679 -- obtained from the name of the renamed entity.
6681 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6682 and then Present (Renamed_Entity (Entity (Gen_Id)))
6683 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6684 then
6685 declare
6686 Renamed_Package : constant Node_Id :=
6687 Name (Parent (Entity (Gen_Id)));
6688 begin
6689 if Nkind (Renamed_Package) = N_Expanded_Name then
6690 Inst_Par := Entity (Prefix (Renamed_Package));
6691 Install_Parent (Inst_Par);
6692 Parent_Installed := True;
6693 end if;
6694 end;
6695 end if;
6696 end if;
6698 elsif Nkind (Gen_Id) = N_Expanded_Name then
6700 -- Entity already present, analyze prefix, whose meaning may be
6701 -- an instance in the current context. If it is an instance of
6702 -- a relative within another, the proper parent may still have
6703 -- to be installed, if they are not of the same generation.
6705 Analyze (Prefix (Gen_Id));
6707 -- In the unlikely case that a local declaration hides the name
6708 -- of the parent package, locate it on the homonym chain. If the
6709 -- context is an instance of the parent, the renaming entity is
6710 -- flagged as such.
6712 Inst_Par := Entity (Prefix (Gen_Id));
6713 while Present (Inst_Par)
6714 and then not Is_Package_Or_Generic_Package (Inst_Par)
6715 loop
6716 Inst_Par := Homonym (Inst_Par);
6717 end loop;
6719 pragma Assert (Present (Inst_Par));
6720 Set_Entity (Prefix (Gen_Id), Inst_Par);
6722 if In_Enclosing_Instance then
6723 null;
6725 elsif Present (Entity (Gen_Id))
6726 and then Is_Child_Unit (Entity (Gen_Id))
6727 and then not In_Open_Scopes (Inst_Par)
6728 then
6729 Install_Parent (Inst_Par);
6730 Parent_Installed := True;
6731 end if;
6733 elsif In_Enclosing_Instance then
6735 -- The child unit is found in some enclosing scope
6737 null;
6739 else
6740 Analyze (Gen_Id);
6742 -- If this is the renaming of the implicit child in a parent
6743 -- instance, recover the parent name and install it.
6745 if Is_Entity_Name (Gen_Id) then
6746 E := Entity (Gen_Id);
6748 if Is_Generic_Unit (E)
6749 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6750 and then Is_Child_Unit (Renamed_Object (E))
6751 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6752 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6753 then
6754 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6755 Inst_Par := Entity (Prefix (Gen_Id));
6757 if not In_Open_Scopes (Inst_Par) then
6758 Install_Parent (Inst_Par);
6759 Parent_Installed := True;
6760 end if;
6762 -- If it is a child unit of a non-generic parent, it may be
6763 -- use-visible and given by a direct name. Install parent as
6764 -- for other cases.
6766 elsif Is_Generic_Unit (E)
6767 and then Is_Child_Unit (E)
6768 and then
6769 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6770 and then not Is_Generic_Unit (Scope (E))
6771 then
6772 if not In_Open_Scopes (Scope (E)) then
6773 Install_Parent (Scope (E));
6774 Parent_Installed := True;
6775 end if;
6776 end if;
6777 end if;
6778 end if;
6779 end Check_Generic_Child_Unit;
6781 -----------------------------
6782 -- Check_Hidden_Child_Unit --
6783 -----------------------------
6785 procedure Check_Hidden_Child_Unit
6786 (N : Node_Id;
6787 Gen_Unit : Entity_Id;
6788 Act_Decl_Id : Entity_Id)
6790 Gen_Id : constant Node_Id := Name (N);
6792 begin
6793 if Is_Child_Unit (Gen_Unit)
6794 and then Is_Child_Unit (Act_Decl_Id)
6795 and then Nkind (Gen_Id) = N_Expanded_Name
6796 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6797 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6798 then
6799 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6800 Error_Msg_NE
6801 ("generic unit & is implicitly declared in &",
6802 Defining_Unit_Name (N), Gen_Unit);
6803 Error_Msg_N ("\instance must have different name",
6804 Defining_Unit_Name (N));
6805 end if;
6806 end Check_Hidden_Child_Unit;
6808 ------------------------
6809 -- Check_Private_View --
6810 ------------------------
6812 procedure Check_Private_View (N : Node_Id) is
6813 T : constant Entity_Id := Etype (N);
6814 BT : Entity_Id;
6816 begin
6817 -- Exchange views if the type was not private in the generic but is
6818 -- private at the point of instantiation. Do not exchange views if
6819 -- the scope of the type is in scope. This can happen if both generic
6820 -- and instance are sibling units, or if type is defined in a parent.
6821 -- In this case the visibility of the type will be correct for all
6822 -- semantic checks.
6824 if Present (T) then
6825 BT := Base_Type (T);
6827 if Is_Private_Type (T)
6828 and then not Has_Private_View (N)
6829 and then Present (Full_View (T))
6830 and then not In_Open_Scopes (Scope (T))
6831 then
6832 -- In the generic, the full type was visible. Save the private
6833 -- entity, for subsequent exchange.
6835 Switch_View (T);
6837 elsif Has_Private_View (N)
6838 and then not Is_Private_Type (T)
6839 and then not Has_Been_Exchanged (T)
6840 and then Etype (Get_Associated_Node (N)) /= T
6841 then
6842 -- Only the private declaration was visible in the generic. If
6843 -- the type appears in a subtype declaration, the subtype in the
6844 -- instance must have a view compatible with that of its parent,
6845 -- which must be exchanged (see corresponding code in Restore_
6846 -- Private_Views). Otherwise, if the type is defined in a parent
6847 -- unit, leave full visibility within instance, which is safe.
6849 if In_Open_Scopes (Scope (Base_Type (T)))
6850 and then not Is_Private_Type (Base_Type (T))
6851 and then Comes_From_Source (Base_Type (T))
6852 then
6853 null;
6855 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6856 or else not In_Private_Part (Scope (Base_Type (T)))
6857 then
6858 Prepend_Elmt (T, Exchanged_Views);
6859 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6860 end if;
6862 -- For composite types with inconsistent representation exchange
6863 -- component types accordingly.
6865 elsif Is_Access_Type (T)
6866 and then Is_Private_Type (Designated_Type (T))
6867 and then not Has_Private_View (N)
6868 and then Present (Full_View (Designated_Type (T)))
6869 then
6870 Switch_View (Designated_Type (T));
6872 elsif Is_Array_Type (T) then
6873 if Is_Private_Type (Component_Type (T))
6874 and then not Has_Private_View (N)
6875 and then Present (Full_View (Component_Type (T)))
6876 then
6877 Switch_View (Component_Type (T));
6878 end if;
6880 -- The normal exchange mechanism relies on the setting of a
6881 -- flag on the reference in the generic. However, an additional
6882 -- mechanism is needed for types that are not explicitly
6883 -- mentioned in the generic, but may be needed in expanded code
6884 -- in the instance. This includes component types of arrays and
6885 -- designated types of access types. This processing must also
6886 -- include the index types of arrays which we take care of here.
6888 declare
6889 Indx : Node_Id;
6890 Typ : Entity_Id;
6892 begin
6893 Indx := First_Index (T);
6894 while Present (Indx) loop
6895 Typ := Base_Type (Etype (Indx));
6897 if Is_Private_Type (Typ)
6898 and then Present (Full_View (Typ))
6899 then
6900 Switch_View (Typ);
6901 end if;
6903 Next_Index (Indx);
6904 end loop;
6905 end;
6907 elsif Is_Private_Type (T)
6908 and then Present (Full_View (T))
6909 and then Is_Array_Type (Full_View (T))
6910 and then Is_Private_Type (Component_Type (Full_View (T)))
6911 then
6912 Switch_View (T);
6914 -- Finally, a non-private subtype may have a private base type, which
6915 -- must be exchanged for consistency. This can happen when a package
6916 -- body is instantiated, when the scope stack is empty but in fact
6917 -- the subtype and the base type are declared in an enclosing scope.
6919 -- Note that in this case we introduce an inconsistency in the view
6920 -- set, because we switch the base type BT, but there could be some
6921 -- private dependent subtypes of BT which remain unswitched. Such
6922 -- subtypes might need to be switched at a later point (see specific
6923 -- provision for that case in Switch_View).
6925 elsif not Is_Private_Type (T)
6926 and then not Has_Private_View (N)
6927 and then Is_Private_Type (BT)
6928 and then Present (Full_View (BT))
6929 and then not Is_Generic_Type (BT)
6930 and then not In_Open_Scopes (BT)
6931 then
6932 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6933 Exchange_Declarations (BT);
6934 end if;
6935 end if;
6936 end Check_Private_View;
6938 -----------------------------
6939 -- Check_Hidden_Primitives --
6940 -----------------------------
6942 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6943 Actual : Node_Id;
6944 Gen_T : Entity_Id;
6945 Result : Elist_Id := No_Elist;
6947 begin
6948 if No (Assoc_List) then
6949 return No_Elist;
6950 end if;
6952 -- Traverse the list of associations between formals and actuals
6953 -- searching for renamings of tagged types
6955 Actual := First (Assoc_List);
6956 while Present (Actual) loop
6957 if Nkind (Actual) = N_Subtype_Declaration then
6958 Gen_T := Generic_Parent_Type (Actual);
6960 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6962 -- Traverse the list of primitives of the actual types
6963 -- searching for hidden primitives that are visible in the
6964 -- corresponding generic formal; leave them visible and
6965 -- append them to Result to restore their decoration later.
6967 Install_Hidden_Primitives
6968 (Prims_List => Result,
6969 Gen_T => Gen_T,
6970 Act_T => Entity (Subtype_Indication (Actual)));
6971 end if;
6972 end if;
6974 Next (Actual);
6975 end loop;
6977 return Result;
6978 end Check_Hidden_Primitives;
6980 --------------------------
6981 -- Contains_Instance_Of --
6982 --------------------------
6984 function Contains_Instance_Of
6985 (Inner : Entity_Id;
6986 Outer : Entity_Id;
6987 N : Node_Id) return Boolean
6989 Elmt : Elmt_Id;
6990 Scop : Entity_Id;
6992 begin
6993 Scop := Outer;
6995 -- Verify that there are no circular instantiations. We check whether
6996 -- the unit contains an instance of the current scope or some enclosing
6997 -- scope (in case one of the instances appears in a subunit). Longer
6998 -- circularities involving subunits might seem too pathological to
6999 -- consider, but they were not too pathological for the authors of
7000 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7001 -- enclosing generic scopes as containing an instance.
7003 loop
7004 -- Within a generic subprogram body, the scope is not generic, to
7005 -- allow for recursive subprograms. Use the declaration to determine
7006 -- whether this is a generic unit.
7008 if Ekind (Scop) = E_Generic_Package
7009 or else (Is_Subprogram (Scop)
7010 and then Nkind (Unit_Declaration_Node (Scop)) =
7011 N_Generic_Subprogram_Declaration)
7012 then
7013 Elmt := First_Elmt (Inner_Instances (Inner));
7015 while Present (Elmt) loop
7016 if Node (Elmt) = Scop then
7017 Error_Msg_Node_2 := Inner;
7018 Error_Msg_NE
7019 ("circular Instantiation: & instantiated within &!",
7020 N, Scop);
7021 return True;
7023 elsif Node (Elmt) = Inner then
7024 return True;
7026 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7027 Error_Msg_Node_2 := Inner;
7028 Error_Msg_NE
7029 ("circular Instantiation: & instantiated within &!",
7030 N, Node (Elmt));
7031 return True;
7032 end if;
7034 Next_Elmt (Elmt);
7035 end loop;
7037 -- Indicate that Inner is being instantiated within Scop
7039 Append_Elmt (Inner, Inner_Instances (Scop));
7040 end if;
7042 if Scop = Standard_Standard then
7043 exit;
7044 else
7045 Scop := Scope (Scop);
7046 end if;
7047 end loop;
7049 return False;
7050 end Contains_Instance_Of;
7052 -----------------------
7053 -- Copy_Generic_Node --
7054 -----------------------
7056 function Copy_Generic_Node
7057 (N : Node_Id;
7058 Parent_Id : Node_Id;
7059 Instantiating : Boolean) return Node_Id
7061 Ent : Entity_Id;
7062 New_N : Node_Id;
7064 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7065 -- Check the given value of one of the Fields referenced by the current
7066 -- node to determine whether to copy it recursively. The field may hold
7067 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7068 -- Char) in which case it need not be copied.
7070 procedure Copy_Descendants;
7071 -- Common utility for various nodes
7073 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7074 -- Make copy of element list
7076 function Copy_Generic_List
7077 (L : List_Id;
7078 Parent_Id : Node_Id) return List_Id;
7079 -- Apply Copy_Node recursively to the members of a node list
7081 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7082 -- True if an identifier is part of the defining program unit name of
7083 -- a child unit. The entity of such an identifier must be kept (for
7084 -- ASIS use) even though as the name of an enclosing generic it would
7085 -- otherwise not be preserved in the generic tree.
7087 ----------------------
7088 -- Copy_Descendants --
7089 ----------------------
7091 procedure Copy_Descendants is
7092 use Atree.Unchecked_Access;
7093 -- This code section is part of the implementation of an untyped
7094 -- tree traversal, so it needs direct access to node fields.
7096 begin
7097 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7098 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7099 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7100 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7101 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7102 end Copy_Descendants;
7104 -----------------------------
7105 -- Copy_Generic_Descendant --
7106 -----------------------------
7108 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7109 begin
7110 if D = Union_Id (Empty) then
7111 return D;
7113 elsif D in Node_Range then
7114 return Union_Id
7115 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7117 elsif D in List_Range then
7118 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7120 elsif D in Elist_Range then
7121 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7123 -- Nothing else is copyable (e.g. Uint values), return as is
7125 else
7126 return D;
7127 end if;
7128 end Copy_Generic_Descendant;
7130 ------------------------
7131 -- Copy_Generic_Elist --
7132 ------------------------
7134 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7135 M : Elmt_Id;
7136 L : Elist_Id;
7138 begin
7139 if Present (E) then
7140 L := New_Elmt_List;
7141 M := First_Elmt (E);
7142 while Present (M) loop
7143 Append_Elmt
7144 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7145 Next_Elmt (M);
7146 end loop;
7148 return L;
7150 else
7151 return No_Elist;
7152 end if;
7153 end Copy_Generic_Elist;
7155 -----------------------
7156 -- Copy_Generic_List --
7157 -----------------------
7159 function Copy_Generic_List
7160 (L : List_Id;
7161 Parent_Id : Node_Id) return List_Id
7163 N : Node_Id;
7164 New_L : List_Id;
7166 begin
7167 if Present (L) then
7168 New_L := New_List;
7169 Set_Parent (New_L, Parent_Id);
7171 N := First (L);
7172 while Present (N) loop
7173 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7174 Next (N);
7175 end loop;
7177 return New_L;
7179 else
7180 return No_List;
7181 end if;
7182 end Copy_Generic_List;
7184 ---------------------------
7185 -- In_Defining_Unit_Name --
7186 ---------------------------
7188 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7189 begin
7190 return
7191 Present (Parent (Nam))
7192 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7193 or else
7194 (Nkind (Parent (Nam)) = N_Expanded_Name
7195 and then In_Defining_Unit_Name (Parent (Nam))));
7196 end In_Defining_Unit_Name;
7198 -- Start of processing for Copy_Generic_Node
7200 begin
7201 if N = Empty then
7202 return N;
7203 end if;
7205 New_N := New_Copy (N);
7207 -- Copy aspects if present
7209 if Has_Aspects (N) then
7210 Set_Has_Aspects (New_N, False);
7211 Set_Aspect_Specifications
7212 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7213 end if;
7215 if Instantiating then
7216 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7217 end if;
7219 if not Is_List_Member (N) then
7220 Set_Parent (New_N, Parent_Id);
7221 end if;
7223 -- Special casing for identifiers and other entity names and operators
7225 if Nkind_In (New_N, N_Character_Literal,
7226 N_Expanded_Name,
7227 N_Identifier,
7228 N_Operator_Symbol)
7229 or else Nkind (New_N) in N_Op
7230 then
7231 if not Instantiating then
7233 -- Link both nodes in order to assign subsequently the entity of
7234 -- the copy to the original node, in case this is a global
7235 -- reference.
7237 Set_Associated_Node (N, New_N);
7239 -- If we are within an instantiation, this is a nested generic
7240 -- that has already been analyzed at the point of definition.
7241 -- We must preserve references that were global to the enclosing
7242 -- parent at that point. Other occurrences, whether global or
7243 -- local to the current generic, must be resolved anew, so we
7244 -- reset the entity in the generic copy. A global reference has a
7245 -- smaller depth than the parent, or else the same depth in case
7246 -- both are distinct compilation units.
7248 -- A child unit is implicitly declared within the enclosing parent
7249 -- but is in fact global to it, and must be preserved.
7251 -- It is also possible for Current_Instantiated_Parent to be
7252 -- defined, and for this not to be a nested generic, namely if
7253 -- the unit is loaded through Rtsfind. In that case, the entity of
7254 -- New_N is only a link to the associated node, and not a defining
7255 -- occurrence.
7257 -- The entities for parent units in the defining_program_unit of a
7258 -- generic child unit are established when the context of the unit
7259 -- is first analyzed, before the generic copy is made. They are
7260 -- preserved in the copy for use in ASIS queries.
7262 Ent := Entity (New_N);
7264 if No (Current_Instantiated_Parent.Gen_Id) then
7265 if No (Ent)
7266 or else Nkind (Ent) /= N_Defining_Identifier
7267 or else not In_Defining_Unit_Name (N)
7268 then
7269 Set_Associated_Node (New_N, Empty);
7270 end if;
7272 elsif No (Ent)
7273 or else
7274 not Nkind_In (Ent, N_Defining_Identifier,
7275 N_Defining_Character_Literal,
7276 N_Defining_Operator_Symbol)
7277 or else No (Scope (Ent))
7278 or else
7279 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7280 and then not Is_Child_Unit (Ent))
7281 or else
7282 (Scope_Depth (Scope (Ent)) >
7283 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7284 and then
7285 Get_Source_Unit (Ent) =
7286 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7287 then
7288 Set_Associated_Node (New_N, Empty);
7289 end if;
7291 -- Case of instantiating identifier or some other name or operator
7293 else
7294 -- If the associated node is still defined, the entity in it
7295 -- is global, and must be copied to the instance. If this copy
7296 -- is being made for a body to inline, it is applied to an
7297 -- instantiated tree, and the entity is already present and
7298 -- must be also preserved.
7300 declare
7301 Assoc : constant Node_Id := Get_Associated_Node (N);
7303 begin
7304 if Present (Assoc) then
7305 if Nkind (Assoc) = Nkind (N) then
7306 Set_Entity (New_N, Entity (Assoc));
7307 Check_Private_View (N);
7309 -- The node is a reference to a global type and acts as the
7310 -- subtype mark of a qualified expression created in order
7311 -- to aid resolution of accidental overloading in instances.
7312 -- Since N is a reference to a type, the Associated_Node of
7313 -- N denotes an entity rather than another identifier. See
7314 -- Qualify_Universal_Operands for details.
7316 elsif Nkind (N) = N_Identifier
7317 and then Nkind (Parent (N)) = N_Qualified_Expression
7318 and then Subtype_Mark (Parent (N)) = N
7319 and then Is_Qualified_Universal_Literal (Parent (N))
7320 then
7321 Set_Entity (New_N, Assoc);
7323 -- The name in the call may be a selected component if the
7324 -- call has not been analyzed yet, as may be the case for
7325 -- pre/post conditions in a generic unit.
7327 elsif Nkind (Assoc) = N_Function_Call
7328 and then Is_Entity_Name (Name (Assoc))
7329 then
7330 Set_Entity (New_N, Entity (Name (Assoc)));
7332 elsif Nkind_In (Assoc, N_Defining_Identifier,
7333 N_Defining_Character_Literal,
7334 N_Defining_Operator_Symbol)
7335 and then Expander_Active
7336 then
7337 -- Inlining case: we are copying a tree that contains
7338 -- global entities, which are preserved in the copy to be
7339 -- used for subsequent inlining.
7341 null;
7343 else
7344 Set_Entity (New_N, Empty);
7345 end if;
7346 end if;
7347 end;
7348 end if;
7350 -- For expanded name, we must copy the Prefix and Selector_Name
7352 if Nkind (N) = N_Expanded_Name then
7353 Set_Prefix
7354 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7356 Set_Selector_Name (New_N,
7357 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7359 -- For operators, we must copy the right operand
7361 elsif Nkind (N) in N_Op then
7362 Set_Right_Opnd (New_N,
7363 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7365 -- And for binary operators, the left operand as well
7367 if Nkind (N) in N_Binary_Op then
7368 Set_Left_Opnd (New_N,
7369 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7370 end if;
7371 end if;
7373 -- Establish a link between an entity from the generic template and the
7374 -- corresponding entity in the generic copy to be analyzed.
7376 elsif Nkind (N) in N_Entity then
7377 if not Instantiating then
7378 Set_Associated_Entity (N, New_N);
7379 end if;
7381 -- Clear any existing link the copy may inherit from the replicated
7382 -- generic template entity.
7384 Set_Associated_Entity (New_N, Empty);
7386 -- Special casing for stubs
7388 elsif Nkind (N) in N_Body_Stub then
7390 -- In any case, we must copy the specification or defining
7391 -- identifier as appropriate.
7393 if Nkind (N) = N_Subprogram_Body_Stub then
7394 Set_Specification (New_N,
7395 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7397 else
7398 Set_Defining_Identifier (New_N,
7399 Copy_Generic_Node
7400 (Defining_Identifier (N), New_N, Instantiating));
7401 end if;
7403 -- If we are not instantiating, then this is where we load and
7404 -- analyze subunits, i.e. at the point where the stub occurs. A
7405 -- more permissive system might defer this analysis to the point
7406 -- of instantiation, but this seems too complicated for now.
7408 if not Instantiating then
7409 declare
7410 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7411 Subunit : Node_Id;
7412 Unum : Unit_Number_Type;
7413 New_Body : Node_Id;
7415 begin
7416 -- Make sure that, if it is a subunit of the main unit that is
7417 -- preprocessed and if -gnateG is specified, the preprocessed
7418 -- file will be written.
7420 Lib.Analysing_Subunit_Of_Main :=
7421 Lib.In_Extended_Main_Source_Unit (N);
7422 Unum :=
7423 Load_Unit
7424 (Load_Name => Subunit_Name,
7425 Required => False,
7426 Subunit => True,
7427 Error_Node => N);
7428 Lib.Analysing_Subunit_Of_Main := False;
7430 -- If the proper body is not found, a warning message will be
7431 -- emitted when analyzing the stub, or later at the point of
7432 -- instantiation. Here we just leave the stub as is.
7434 if Unum = No_Unit then
7435 Subunits_Missing := True;
7436 goto Subunit_Not_Found;
7437 end if;
7439 Subunit := Cunit (Unum);
7441 if Nkind (Unit (Subunit)) /= N_Subunit then
7442 Error_Msg_N
7443 ("found child unit instead of expected SEPARATE subunit",
7444 Subunit);
7445 Error_Msg_Sloc := Sloc (N);
7446 Error_Msg_N ("\to complete stub #", Subunit);
7447 goto Subunit_Not_Found;
7448 end if;
7450 -- We must create a generic copy of the subunit, in order to
7451 -- perform semantic analysis on it, and we must replace the
7452 -- stub in the original generic unit with the subunit, in order
7453 -- to preserve non-local references within.
7455 -- Only the proper body needs to be copied. Library_Unit and
7456 -- context clause are simply inherited by the generic copy.
7457 -- Note that the copy (which may be recursive if there are
7458 -- nested subunits) must be done first, before attaching it to
7459 -- the enclosing generic.
7461 New_Body :=
7462 Copy_Generic_Node
7463 (Proper_Body (Unit (Subunit)),
7464 Empty, Instantiating => False);
7466 -- Now place the original proper body in the original generic
7467 -- unit. This is a body, not a compilation unit.
7469 Rewrite (N, Proper_Body (Unit (Subunit)));
7470 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7471 Set_Was_Originally_Stub (N);
7473 -- Finally replace the body of the subunit with its copy, and
7474 -- make this new subunit into the library unit of the generic
7475 -- copy, which does not have stubs any longer.
7477 Set_Proper_Body (Unit (Subunit), New_Body);
7478 Set_Library_Unit (New_N, Subunit);
7479 Inherit_Context (Unit (Subunit), N);
7480 end;
7482 -- If we are instantiating, this must be an error case, since
7483 -- otherwise we would have replaced the stub node by the proper body
7484 -- that corresponds. So just ignore it in the copy (i.e. we have
7485 -- copied it, and that is good enough).
7487 else
7488 null;
7489 end if;
7491 <<Subunit_Not_Found>> null;
7493 -- If the node is a compilation unit, it is the subunit of a stub, which
7494 -- has been loaded already (see code below). In this case, the library
7495 -- unit field of N points to the parent unit (which is a compilation
7496 -- unit) and need not (and cannot) be copied.
7498 -- When the proper body of the stub is analyzed, the library_unit link
7499 -- is used to establish the proper context (see sem_ch10).
7501 -- The other fields of a compilation unit are copied as usual
7503 elsif Nkind (N) = N_Compilation_Unit then
7505 -- This code can only be executed when not instantiating, because in
7506 -- the copy made for an instantiation, the compilation unit node has
7507 -- disappeared at the point that a stub is replaced by its proper
7508 -- body.
7510 pragma Assert (not Instantiating);
7512 Set_Context_Items (New_N,
7513 Copy_Generic_List (Context_Items (N), New_N));
7515 Set_Unit (New_N,
7516 Copy_Generic_Node (Unit (N), New_N, False));
7518 Set_First_Inlined_Subprogram (New_N,
7519 Copy_Generic_Node
7520 (First_Inlined_Subprogram (N), New_N, False));
7522 Set_Aux_Decls_Node (New_N,
7523 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7525 -- For an assignment node, the assignment is known to be semantically
7526 -- legal if we are instantiating the template. This avoids incorrect
7527 -- diagnostics in generated code.
7529 elsif Nkind (N) = N_Assignment_Statement then
7531 -- Copy name and expression fields in usual manner
7533 Set_Name (New_N,
7534 Copy_Generic_Node (Name (N), New_N, Instantiating));
7536 Set_Expression (New_N,
7537 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7539 if Instantiating then
7540 Set_Assignment_OK (Name (New_N), True);
7541 end if;
7543 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7544 if not Instantiating then
7545 Set_Associated_Node (N, New_N);
7547 else
7548 if Present (Get_Associated_Node (N))
7549 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7550 then
7551 -- In the generic the aggregate has some composite type. If at
7552 -- the point of instantiation the type has a private view,
7553 -- install the full view (and that of its ancestors, if any).
7555 declare
7556 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7557 Rt : Entity_Id;
7559 begin
7560 if Present (T) and then Is_Private_Type (T) then
7561 Switch_View (T);
7562 end if;
7564 if Present (T)
7565 and then Is_Tagged_Type (T)
7566 and then Is_Derived_Type (T)
7567 then
7568 Rt := Root_Type (T);
7570 loop
7571 T := Etype (T);
7573 if Is_Private_Type (T) then
7574 Switch_View (T);
7575 end if;
7577 exit when T = Rt;
7578 end loop;
7579 end if;
7580 end;
7581 end if;
7582 end if;
7584 -- Do not copy the associated node, which points to the generic copy
7585 -- of the aggregate.
7587 declare
7588 use Atree.Unchecked_Access;
7589 -- This code section is part of the implementation of an untyped
7590 -- tree traversal, so it needs direct access to node fields.
7592 begin
7593 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7594 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7595 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7596 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7597 end;
7599 -- Allocators do not have an identifier denoting the access type, so we
7600 -- must locate it through the expression to check whether the views are
7601 -- consistent.
7603 elsif Nkind (N) = N_Allocator
7604 and then Nkind (Expression (N)) = N_Qualified_Expression
7605 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7606 and then Instantiating
7607 then
7608 declare
7609 T : constant Node_Id :=
7610 Get_Associated_Node (Subtype_Mark (Expression (N)));
7611 Acc_T : Entity_Id;
7613 begin
7614 if Present (T) then
7616 -- Retrieve the allocator node in the generic copy
7618 Acc_T := Etype (Parent (Parent (T)));
7620 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7621 Switch_View (Acc_T);
7622 end if;
7623 end if;
7625 Copy_Descendants;
7626 end;
7628 -- For a proper body, we must catch the case of a proper body that
7629 -- replaces a stub. This represents the point at which a separate
7630 -- compilation unit, and hence template file, may be referenced, so we
7631 -- must make a new source instantiation entry for the template of the
7632 -- subunit, and ensure that all nodes in the subunit are adjusted using
7633 -- this new source instantiation entry.
7635 elsif Nkind (N) in N_Proper_Body then
7636 declare
7637 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7639 begin
7640 if Instantiating and then Was_Originally_Stub (N) then
7641 Create_Instantiation_Source
7642 (Instantiation_Node,
7643 Defining_Entity (N),
7644 False,
7645 S_Adjustment);
7646 end if;
7648 -- Now copy the fields of the proper body, using the new
7649 -- adjustment factor if one was needed as per test above.
7651 Copy_Descendants;
7653 -- Restore the original adjustment factor in case changed
7655 S_Adjustment := Save_Adjustment;
7656 end;
7658 elsif Nkind (N) = N_Pragma and then Instantiating then
7660 -- Do not copy Comment or Ident pragmas their content is relevant to
7661 -- the generic unit, not to the instantiating unit.
7663 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7664 New_N := Make_Null_Statement (Sloc (N));
7666 -- Do not copy pragmas generated from aspects because the pragmas do
7667 -- not carry any semantic information, plus they will be regenerated
7668 -- in the instance.
7670 -- However, generating C we need to copy them since postconditions
7671 -- are inlined by the front end, and the front-end inlining machinery
7672 -- relies on this routine to perform inlining.
7674 elsif From_Aspect_Specification (N)
7675 and then not Modify_Tree_For_C
7676 then
7677 New_N := Make_Null_Statement (Sloc (N));
7679 else
7680 Copy_Descendants;
7681 end if;
7683 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7685 -- No descendant fields need traversing
7687 null;
7689 elsif Nkind (N) = N_String_Literal
7690 and then Present (Etype (N))
7691 and then Instantiating
7692 then
7693 -- If the string is declared in an outer scope, the string_literal
7694 -- subtype created for it may have the wrong scope. Force reanalysis
7695 -- of the constant to generate a new itype in the proper context.
7697 Set_Etype (New_N, Empty);
7698 Set_Analyzed (New_N, False);
7700 -- For the remaining nodes, copy their descendants recursively
7702 else
7703 Copy_Descendants;
7705 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7706 Set_Generic_Parent (Specification (New_N), N);
7708 -- Should preserve Corresponding_Spec??? (12.3(14))
7709 end if;
7710 end if;
7712 -- Propagate dimensions if present, so that they are reflected in the
7713 -- instance.
7715 if Nkind (N) in N_Has_Etype
7716 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7717 and then Present (Etype (N))
7718 and then Is_Floating_Point_Type (Etype (N))
7719 and then Has_Dimension_System (Etype (N))
7720 then
7721 Copy_Dimensions (N, New_N);
7722 end if;
7724 return New_N;
7725 end Copy_Generic_Node;
7727 ----------------------------
7728 -- Denotes_Formal_Package --
7729 ----------------------------
7731 function Denotes_Formal_Package
7732 (Pack : Entity_Id;
7733 On_Exit : Boolean := False;
7734 Instance : Entity_Id := Empty) return Boolean
7736 Par : Entity_Id;
7737 Scop : constant Entity_Id := Scope (Pack);
7738 E : Entity_Id;
7740 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7741 -- The package in question may be an actual for a previous formal
7742 -- package P of the current instance, so examine its actuals as well.
7743 -- This must be recursive over other formal packages.
7745 ----------------------------------
7746 -- Is_Actual_Of_Previous_Formal --
7747 ----------------------------------
7749 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7750 E1 : Entity_Id;
7752 begin
7753 E1 := First_Entity (P);
7754 while Present (E1) and then E1 /= Instance loop
7755 if Ekind (E1) = E_Package
7756 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7757 then
7758 if Renamed_Object (E1) = Pack then
7759 return True;
7761 elsif E1 = P or else Renamed_Object (E1) = P then
7762 return False;
7764 elsif Is_Actual_Of_Previous_Formal (E1) then
7765 return True;
7766 end if;
7767 end if;
7769 Next_Entity (E1);
7770 end loop;
7772 return False;
7773 end Is_Actual_Of_Previous_Formal;
7775 -- Start of processing for Denotes_Formal_Package
7777 begin
7778 if On_Exit then
7779 Par :=
7780 Instance_Envs.Table
7781 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7782 else
7783 Par := Current_Instantiated_Parent.Act_Id;
7784 end if;
7786 if Ekind (Scop) = E_Generic_Package
7787 or else Nkind (Unit_Declaration_Node (Scop)) =
7788 N_Generic_Subprogram_Declaration
7789 then
7790 return True;
7792 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7793 N_Formal_Package_Declaration
7794 then
7795 return True;
7797 elsif No (Par) then
7798 return False;
7800 else
7801 -- Check whether this package is associated with a formal package of
7802 -- the enclosing instantiation. Iterate over the list of renamings.
7804 E := First_Entity (Par);
7805 while Present (E) loop
7806 if Ekind (E) /= E_Package
7807 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7808 then
7809 null;
7811 elsif Renamed_Object (E) = Par then
7812 return False;
7814 elsif Renamed_Object (E) = Pack then
7815 return True;
7817 elsif Is_Actual_Of_Previous_Formal (E) then
7818 return True;
7820 end if;
7822 Next_Entity (E);
7823 end loop;
7825 return False;
7826 end if;
7827 end Denotes_Formal_Package;
7829 -----------------
7830 -- End_Generic --
7831 -----------------
7833 procedure End_Generic is
7834 begin
7835 -- ??? More things could be factored out in this routine. Should
7836 -- probably be done at a later stage.
7838 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7839 Generic_Flags.Decrement_Last;
7841 Expander_Mode_Restore;
7842 end End_Generic;
7844 -------------
7845 -- Earlier --
7846 -------------
7848 function Earlier (N1, N2 : Node_Id) return Boolean is
7849 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7850 -- Find distance from given node to enclosing compilation unit
7852 ----------------
7853 -- Find_Depth --
7854 ----------------
7856 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7857 begin
7858 while Present (P)
7859 and then Nkind (P) /= N_Compilation_Unit
7860 loop
7861 P := True_Parent (P);
7862 D := D + 1;
7863 end loop;
7864 end Find_Depth;
7866 -- Local declarations
7868 D1 : Integer := 0;
7869 D2 : Integer := 0;
7870 P1 : Node_Id := N1;
7871 P2 : Node_Id := N2;
7872 T1 : Source_Ptr;
7873 T2 : Source_Ptr;
7875 -- Start of processing for Earlier
7877 begin
7878 Find_Depth (P1, D1);
7879 Find_Depth (P2, D2);
7881 if P1 /= P2 then
7882 return False;
7883 else
7884 P1 := N1;
7885 P2 := N2;
7886 end if;
7888 while D1 > D2 loop
7889 P1 := True_Parent (P1);
7890 D1 := D1 - 1;
7891 end loop;
7893 while D2 > D1 loop
7894 P2 := True_Parent (P2);
7895 D2 := D2 - 1;
7896 end loop;
7898 -- At this point P1 and P2 are at the same distance from the root.
7899 -- We examine their parents until we find a common declarative list.
7900 -- If we reach the root, N1 and N2 do not descend from the same
7901 -- declarative list (e.g. one is nested in the declarative part and
7902 -- the other is in a block in the statement part) and the earlier
7903 -- one is already frozen.
7905 while not Is_List_Member (P1)
7906 or else not Is_List_Member (P2)
7907 or else List_Containing (P1) /= List_Containing (P2)
7908 loop
7909 P1 := True_Parent (P1);
7910 P2 := True_Parent (P2);
7912 if Nkind (Parent (P1)) = N_Subunit then
7913 P1 := Corresponding_Stub (Parent (P1));
7914 end if;
7916 if Nkind (Parent (P2)) = N_Subunit then
7917 P2 := Corresponding_Stub (Parent (P2));
7918 end if;
7920 if P1 = P2 then
7921 return False;
7922 end if;
7923 end loop;
7925 -- Expanded code usually shares the source location of the original
7926 -- construct it was generated for. This however may not necessarily
7927 -- reflect the true location of the code within the tree.
7929 -- Before comparing the slocs of the two nodes, make sure that we are
7930 -- working with correct source locations. Assume that P1 is to the left
7931 -- of P2. If either one does not come from source, traverse the common
7932 -- list heading towards the other node and locate the first source
7933 -- statement.
7935 -- P1 P2
7936 -- ----+===+===+--------------+===+===+----
7937 -- expanded code expanded code
7939 if not Comes_From_Source (P1) then
7940 while Present (P1) loop
7942 -- Neither P2 nor a source statement were located during the
7943 -- search. If we reach the end of the list, then P1 does not
7944 -- occur earlier than P2.
7946 -- ---->
7947 -- start --- P2 ----- P1 --- end
7949 if No (Next (P1)) then
7950 return False;
7952 -- We encounter P2 while going to the right of the list. This
7953 -- means that P1 does indeed appear earlier.
7955 -- ---->
7956 -- start --- P1 ===== P2 --- end
7957 -- expanded code in between
7959 elsif P1 = P2 then
7960 return True;
7962 -- No need to look any further since we have located a source
7963 -- statement.
7965 elsif Comes_From_Source (P1) then
7966 exit;
7967 end if;
7969 -- Keep going right
7971 Next (P1);
7972 end loop;
7973 end if;
7975 if not Comes_From_Source (P2) then
7976 while Present (P2) loop
7978 -- Neither P1 nor a source statement were located during the
7979 -- search. If we reach the start of the list, then P1 does not
7980 -- occur earlier than P2.
7982 -- <----
7983 -- start --- P2 --- P1 --- end
7985 if No (Prev (P2)) then
7986 return False;
7988 -- We encounter P1 while going to the left of the list. This
7989 -- means that P1 does indeed appear earlier.
7991 -- <----
7992 -- start --- P1 ===== P2 --- end
7993 -- expanded code in between
7995 elsif P2 = P1 then
7996 return True;
7998 -- No need to look any further since we have located a source
7999 -- statement.
8001 elsif Comes_From_Source (P2) then
8002 exit;
8003 end if;
8005 -- Keep going left
8007 Prev (P2);
8008 end loop;
8009 end if;
8011 -- At this point either both nodes came from source or we approximated
8012 -- their source locations through neighboring source statements.
8014 T1 := Top_Level_Location (Sloc (P1));
8015 T2 := Top_Level_Location (Sloc (P2));
8017 -- When two nodes come from the same instance, they have identical top
8018 -- level locations. To determine proper relation within the tree, check
8019 -- their locations within the template.
8021 if T1 = T2 then
8022 return Sloc (P1) < Sloc (P2);
8024 -- The two nodes either come from unrelated instances or do not come
8025 -- from instantiated code at all.
8027 else
8028 return T1 < T2;
8029 end if;
8030 end Earlier;
8032 ----------------------
8033 -- Find_Actual_Type --
8034 ----------------------
8036 function Find_Actual_Type
8037 (Typ : Entity_Id;
8038 Gen_Type : Entity_Id) return Entity_Id
8040 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8041 T : Entity_Id;
8043 begin
8044 -- Special processing only applies to child units
8046 if not Is_Child_Unit (Gen_Scope) then
8047 return Get_Instance_Of (Typ);
8049 -- If designated or component type is itself a formal of the child unit,
8050 -- its instance is available.
8052 elsif Scope (Typ) = Gen_Scope then
8053 return Get_Instance_Of (Typ);
8055 -- If the array or access type is not declared in the parent unit,
8056 -- no special processing needed.
8058 elsif not Is_Generic_Type (Typ)
8059 and then Scope (Gen_Scope) /= Scope (Typ)
8060 then
8061 return Get_Instance_Of (Typ);
8063 -- Otherwise, retrieve designated or component type by visibility
8065 else
8066 T := Current_Entity (Typ);
8067 while Present (T) loop
8068 if In_Open_Scopes (Scope (T)) then
8069 return T;
8070 elsif Is_Generic_Actual_Type (T) then
8071 return T;
8072 end if;
8074 T := Homonym (T);
8075 end loop;
8077 return Typ;
8078 end if;
8079 end Find_Actual_Type;
8081 ----------------------------
8082 -- Freeze_Subprogram_Body --
8083 ----------------------------
8085 procedure Freeze_Subprogram_Body
8086 (Inst_Node : Node_Id;
8087 Gen_Body : Node_Id;
8088 Pack_Id : Entity_Id)
8090 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8091 Par : constant Entity_Id := Scope (Gen_Unit);
8092 E_G_Id : Entity_Id;
8093 Enc_G : Entity_Id;
8094 Enc_I : Node_Id;
8095 F_Node : Node_Id;
8097 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8098 -- Find innermost package body that encloses the given node, and which
8099 -- is not a compilation unit. Freeze nodes for the instance, or for its
8100 -- enclosing body, may be inserted after the enclosing_body of the
8101 -- generic unit. Used to determine proper placement of freeze node for
8102 -- both package and subprogram instances.
8104 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8105 -- Find entity for given package body, and locate or create a freeze
8106 -- node for it.
8108 ----------------------------
8109 -- Enclosing_Package_Body --
8110 ----------------------------
8112 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8113 P : Node_Id;
8115 begin
8116 P := Parent (N);
8117 while Present (P)
8118 and then Nkind (Parent (P)) /= N_Compilation_Unit
8119 loop
8120 if Nkind (P) = N_Package_Body then
8121 if Nkind (Parent (P)) = N_Subunit then
8122 return Corresponding_Stub (Parent (P));
8123 else
8124 return P;
8125 end if;
8126 end if;
8128 P := True_Parent (P);
8129 end loop;
8131 return Empty;
8132 end Enclosing_Package_Body;
8134 -------------------------
8135 -- Package_Freeze_Node --
8136 -------------------------
8138 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8139 Id : Entity_Id;
8141 begin
8142 if Nkind (B) = N_Package_Body then
8143 Id := Corresponding_Spec (B);
8144 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8145 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8146 end if;
8148 Ensure_Freeze_Node (Id);
8149 return Freeze_Node (Id);
8150 end Package_Freeze_Node;
8152 -- Start of processing for Freeze_Subprogram_Body
8154 begin
8155 -- If the instance and the generic body appear within the same unit, and
8156 -- the instance precedes the generic, the freeze node for the instance
8157 -- must appear after that of the generic. If the generic is nested
8158 -- within another instance I2, then current instance must be frozen
8159 -- after I2. In both cases, the freeze nodes are those of enclosing
8160 -- packages. Otherwise, the freeze node is placed at the end of the
8161 -- current declarative part.
8163 Enc_G := Enclosing_Package_Body (Gen_Body);
8164 Enc_I := Enclosing_Package_Body (Inst_Node);
8165 Ensure_Freeze_Node (Pack_Id);
8166 F_Node := Freeze_Node (Pack_Id);
8168 if Is_Generic_Instance (Par)
8169 and then Present (Freeze_Node (Par))
8170 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8171 then
8172 -- The parent was a premature instantiation. Insert freeze node at
8173 -- the end the current declarative part.
8175 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8176 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8178 -- Handle the following case:
8180 -- package Parent_Inst is new ...
8181 -- Parent_Inst []
8183 -- procedure P ... -- this body freezes Parent_Inst
8185 -- package Inst is new ...
8187 -- In this particular scenario, the freeze node for Inst must be
8188 -- inserted in the same manner as that of Parent_Inst - before the
8189 -- next source body or at the end of the declarative list (body not
8190 -- available). If body P did not exist and Parent_Inst was frozen
8191 -- after Inst, either by a body following Inst or at the end of the
8192 -- declarative region, the freeze node for Inst must be inserted
8193 -- after that of Parent_Inst. This relation is established by
8194 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8196 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8197 List_Containing (Inst_Node)
8198 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8199 then
8200 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8202 else
8203 Insert_After (Freeze_Node (Par), F_Node);
8204 end if;
8206 -- The body enclosing the instance should be frozen after the body that
8207 -- includes the generic, because the body of the instance may make
8208 -- references to entities therein. If the two are not in the same
8209 -- declarative part, or if the one enclosing the instance is frozen
8210 -- already, freeze the instance at the end of the current declarative
8211 -- part.
8213 elsif Is_Generic_Instance (Par)
8214 and then Present (Freeze_Node (Par))
8215 and then Present (Enc_I)
8216 then
8217 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8218 or else
8219 (Nkind (Enc_I) = N_Package_Body
8220 and then
8221 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8222 then
8223 -- The enclosing package may contain several instances. Rather
8224 -- than computing the earliest point at which to insert its freeze
8225 -- node, we place it at the end of the declarative part of the
8226 -- parent of the generic.
8228 Insert_Freeze_Node_For_Instance
8229 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8230 end if;
8232 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8234 elsif Present (Enc_G)
8235 and then Present (Enc_I)
8236 and then Enc_G /= Enc_I
8237 and then Earlier (Inst_Node, Gen_Body)
8238 then
8239 if Nkind (Enc_G) = N_Package_Body then
8240 E_G_Id :=
8241 Corresponding_Spec (Enc_G);
8242 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8243 E_G_Id :=
8244 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8245 end if;
8247 -- Freeze package that encloses instance, and place node after the
8248 -- package that encloses generic. If enclosing package is already
8249 -- frozen we have to assume it is at the proper place. This may be a
8250 -- potential ABE that requires dynamic checking. Do not add a freeze
8251 -- node if the package that encloses the generic is inside the body
8252 -- that encloses the instance, because the freeze node would be in
8253 -- the wrong scope. Additional contortions needed if the bodies are
8254 -- within a subunit.
8256 declare
8257 Enclosing_Body : Node_Id;
8259 begin
8260 if Nkind (Enc_I) = N_Package_Body_Stub then
8261 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8262 else
8263 Enclosing_Body := Enc_I;
8264 end if;
8266 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8267 Insert_Freeze_Node_For_Instance
8268 (Enc_G, Package_Freeze_Node (Enc_I));
8269 end if;
8270 end;
8272 -- Freeze enclosing subunit before instance
8274 Ensure_Freeze_Node (E_G_Id);
8276 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8277 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8278 end if;
8280 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8282 else
8283 -- If none of the above, insert freeze node at the end of the current
8284 -- declarative part.
8286 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8287 end if;
8288 end Freeze_Subprogram_Body;
8290 ----------------
8291 -- Get_Gen_Id --
8292 ----------------
8294 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8295 begin
8296 return Generic_Renamings.Table (E).Gen_Id;
8297 end Get_Gen_Id;
8299 ---------------------
8300 -- Get_Instance_Of --
8301 ---------------------
8303 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8304 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8306 begin
8307 if Res /= Assoc_Null then
8308 return Generic_Renamings.Table (Res).Act_Id;
8310 else
8311 -- On exit, entity is not instantiated: not a generic parameter, or
8312 -- else parameter of an inner generic unit.
8314 return A;
8315 end if;
8316 end Get_Instance_Of;
8318 ------------------------------------
8319 -- Get_Package_Instantiation_Node --
8320 ------------------------------------
8322 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8323 Decl : Node_Id := Unit_Declaration_Node (A);
8324 Inst : Node_Id;
8326 begin
8327 -- If the Package_Instantiation attribute has been set on the package
8328 -- entity, then use it directly when it (or its Original_Node) refers
8329 -- to an N_Package_Instantiation node. In principle it should be
8330 -- possible to have this field set in all cases, which should be
8331 -- investigated, and would allow this function to be significantly
8332 -- simplified. ???
8334 Inst := Package_Instantiation (A);
8336 if Present (Inst) then
8337 if Nkind (Inst) = N_Package_Instantiation then
8338 return Inst;
8340 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8341 return Original_Node (Inst);
8342 end if;
8343 end if;
8345 -- If the instantiation is a compilation unit that does not need body
8346 -- then the instantiation node has been rewritten as a package
8347 -- declaration for the instance, and we return the original node.
8349 -- If it is a compilation unit and the instance node has not been
8350 -- rewritten, then it is still the unit of the compilation. Finally, if
8351 -- a body is present, this is a parent of the main unit whose body has
8352 -- been compiled for inlining purposes, and the instantiation node has
8353 -- been rewritten with the instance body.
8355 -- Otherwise the instantiation node appears after the declaration. If
8356 -- the entity is a formal package, the declaration may have been
8357 -- rewritten as a generic declaration (in the case of a formal with box)
8358 -- or left as a formal package declaration if it has actuals, and is
8359 -- found with a forward search.
8361 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8362 if Nkind (Decl) = N_Package_Declaration
8363 and then Present (Corresponding_Body (Decl))
8364 then
8365 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8366 end if;
8368 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8369 return Original_Node (Decl);
8370 else
8371 return Unit (Parent (Decl));
8372 end if;
8374 elsif Nkind (Decl) = N_Package_Declaration
8375 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8376 then
8377 return Original_Node (Decl);
8379 else
8380 Inst := Next (Decl);
8381 while not Nkind_In (Inst, N_Package_Instantiation,
8382 N_Formal_Package_Declaration)
8383 loop
8384 Next (Inst);
8385 end loop;
8387 return Inst;
8388 end if;
8389 end Get_Package_Instantiation_Node;
8391 ------------------------
8392 -- Has_Been_Exchanged --
8393 ------------------------
8395 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8396 Next : Elmt_Id;
8398 begin
8399 Next := First_Elmt (Exchanged_Views);
8400 while Present (Next) loop
8401 if Full_View (Node (Next)) = E then
8402 return True;
8403 end if;
8405 Next_Elmt (Next);
8406 end loop;
8408 return False;
8409 end Has_Been_Exchanged;
8411 ----------
8412 -- Hash --
8413 ----------
8415 function Hash (F : Entity_Id) return HTable_Range is
8416 begin
8417 return HTable_Range (F mod HTable_Size);
8418 end Hash;
8420 ------------------------
8421 -- Hide_Current_Scope --
8422 ------------------------
8424 procedure Hide_Current_Scope is
8425 C : constant Entity_Id := Current_Scope;
8426 E : Entity_Id;
8428 begin
8429 Set_Is_Hidden_Open_Scope (C);
8431 E := First_Entity (C);
8432 while Present (E) loop
8433 if Is_Immediately_Visible (E) then
8434 Set_Is_Immediately_Visible (E, False);
8435 Append_Elmt (E, Hidden_Entities);
8436 end if;
8438 Next_Entity (E);
8439 end loop;
8441 -- Make the scope name invisible as well. This is necessary, but might
8442 -- conflict with calls to Rtsfind later on, in case the scope is a
8443 -- predefined one. There is no clean solution to this problem, so for
8444 -- now we depend on the user not redefining Standard itself in one of
8445 -- the parent units.
8447 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8448 Set_Is_Immediately_Visible (C, False);
8449 Append_Elmt (C, Hidden_Entities);
8450 end if;
8452 end Hide_Current_Scope;
8454 --------------
8455 -- Init_Env --
8456 --------------
8458 procedure Init_Env is
8459 Saved : Instance_Env;
8461 begin
8462 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8463 Saved.Exchanged_Views := Exchanged_Views;
8464 Saved.Hidden_Entities := Hidden_Entities;
8465 Saved.Current_Sem_Unit := Current_Sem_Unit;
8466 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8467 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8469 -- Save configuration switches. These may be reset if the unit is a
8470 -- predefined unit, and the current mode is not Ada 2005.
8472 Save_Opt_Config_Switches (Saved.Switches);
8474 Instance_Envs.Append (Saved);
8476 Exchanged_Views := New_Elmt_List;
8477 Hidden_Entities := New_Elmt_List;
8479 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8480 -- this is set properly in Set_Instance_Env.
8482 Current_Instantiated_Parent :=
8483 (Current_Scope, Current_Scope, Assoc_Null);
8484 end Init_Env;
8486 ------------------------------
8487 -- In_Same_Declarative_Part --
8488 ------------------------------
8490 function In_Same_Declarative_Part
8491 (F_Node : Node_Id;
8492 Inst : Node_Id) return Boolean
8494 Decls : constant Node_Id := Parent (F_Node);
8495 Nod : Node_Id;
8497 begin
8498 Nod := Parent (Inst);
8499 while Present (Nod) loop
8500 if Nod = Decls then
8501 return True;
8503 elsif Nkind_In (Nod, N_Subprogram_Body,
8504 N_Package_Body,
8505 N_Package_Declaration,
8506 N_Task_Body,
8507 N_Protected_Body,
8508 N_Block_Statement)
8509 then
8510 return False;
8512 elsif Nkind (Nod) = N_Subunit then
8513 Nod := Corresponding_Stub (Nod);
8515 elsif Nkind (Nod) = N_Compilation_Unit then
8516 return False;
8518 else
8519 Nod := Parent (Nod);
8520 end if;
8521 end loop;
8523 return False;
8524 end In_Same_Declarative_Part;
8526 ---------------------
8527 -- In_Main_Context --
8528 ---------------------
8530 function In_Main_Context (E : Entity_Id) return Boolean is
8531 Context : List_Id;
8532 Clause : Node_Id;
8533 Nam : Node_Id;
8535 begin
8536 if not Is_Compilation_Unit (E)
8537 or else Ekind (E) /= E_Package
8538 or else In_Private_Part (E)
8539 then
8540 return False;
8541 end if;
8543 Context := Context_Items (Cunit (Main_Unit));
8545 Clause := First (Context);
8546 while Present (Clause) loop
8547 if Nkind (Clause) = N_With_Clause then
8548 Nam := Name (Clause);
8550 -- If the current scope is part of the context of the main unit,
8551 -- analysis of the corresponding with_clause is not complete, and
8552 -- the entity is not set. We use the Chars field directly, which
8553 -- might produce false positives in rare cases, but guarantees
8554 -- that we produce all the instance bodies we will need.
8556 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8557 or else (Nkind (Nam) = N_Selected_Component
8558 and then Chars (Selector_Name (Nam)) = Chars (E))
8559 then
8560 return True;
8561 end if;
8562 end if;
8564 Next (Clause);
8565 end loop;
8567 return False;
8568 end In_Main_Context;
8570 ---------------------
8571 -- Inherit_Context --
8572 ---------------------
8574 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8575 Current_Context : List_Id;
8576 Current_Unit : Node_Id;
8577 Item : Node_Id;
8578 New_I : Node_Id;
8580 Clause : Node_Id;
8581 OK : Boolean;
8582 Lib_Unit : Node_Id;
8584 begin
8585 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8587 -- The inherited context is attached to the enclosing compilation
8588 -- unit. This is either the main unit, or the declaration for the
8589 -- main unit (in case the instantiation appears within the package
8590 -- declaration and the main unit is its body).
8592 Current_Unit := Parent (Inst);
8593 while Present (Current_Unit)
8594 and then Nkind (Current_Unit) /= N_Compilation_Unit
8595 loop
8596 Current_Unit := Parent (Current_Unit);
8597 end loop;
8599 Current_Context := Context_Items (Current_Unit);
8601 Item := First (Context_Items (Parent (Gen_Decl)));
8602 while Present (Item) loop
8603 if Nkind (Item) = N_With_Clause then
8604 Lib_Unit := Library_Unit (Item);
8606 -- Take care to prevent direct cyclic with's
8608 if Lib_Unit /= Current_Unit then
8610 -- Do not add a unit if it is already in the context
8612 Clause := First (Current_Context);
8613 OK := True;
8614 while Present (Clause) loop
8615 if Nkind (Clause) = N_With_Clause and then
8616 Library_Unit (Clause) = Lib_Unit
8617 then
8618 OK := False;
8619 exit;
8620 end if;
8622 Next (Clause);
8623 end loop;
8625 if OK then
8626 New_I := New_Copy (Item);
8627 Set_Implicit_With (New_I, True);
8628 Set_Implicit_With_From_Instantiation (New_I, True);
8629 Append (New_I, Current_Context);
8630 end if;
8631 end if;
8632 end if;
8634 Next (Item);
8635 end loop;
8636 end if;
8637 end Inherit_Context;
8639 ----------------
8640 -- Initialize --
8641 ----------------
8643 procedure Initialize is
8644 begin
8645 Generic_Renamings.Init;
8646 Instance_Envs.Init;
8647 Generic_Flags.Init;
8648 Generic_Renamings_HTable.Reset;
8649 Circularity_Detected := False;
8650 Exchanged_Views := No_Elist;
8651 Hidden_Entities := No_Elist;
8652 end Initialize;
8654 -------------------------------------
8655 -- Insert_Freeze_Node_For_Instance --
8656 -------------------------------------
8658 procedure Insert_Freeze_Node_For_Instance
8659 (N : Node_Id;
8660 F_Node : Node_Id)
8662 Decl : Node_Id;
8663 Decls : List_Id;
8664 Inst : Entity_Id;
8665 Par_N : Node_Id;
8667 function Enclosing_Body (N : Node_Id) return Node_Id;
8668 -- Find enclosing package or subprogram body, if any. Freeze node may
8669 -- be placed at end of current declarative list if previous instance
8670 -- and current one have different enclosing bodies.
8672 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8673 -- Find the local instance, if any, that declares the generic that is
8674 -- being instantiated. If present, the freeze node for this instance
8675 -- must follow the freeze node for the previous instance.
8677 --------------------
8678 -- Enclosing_Body --
8679 --------------------
8681 function Enclosing_Body (N : Node_Id) return Node_Id is
8682 P : Node_Id;
8684 begin
8685 P := Parent (N);
8686 while Present (P)
8687 and then Nkind (Parent (P)) /= N_Compilation_Unit
8688 loop
8689 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8690 if Nkind (Parent (P)) = N_Subunit then
8691 return Corresponding_Stub (Parent (P));
8692 else
8693 return P;
8694 end if;
8695 end if;
8697 P := True_Parent (P);
8698 end loop;
8700 return Empty;
8701 end Enclosing_Body;
8703 -----------------------
8704 -- Previous_Instance --
8705 -----------------------
8707 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8708 S : Entity_Id;
8710 begin
8711 S := Scope (Gen);
8712 while Present (S) and then S /= Standard_Standard loop
8713 if Is_Generic_Instance (S)
8714 and then In_Same_Source_Unit (S, N)
8715 then
8716 return S;
8717 end if;
8719 S := Scope (S);
8720 end loop;
8722 return Empty;
8723 end Previous_Instance;
8725 -- Start of processing for Insert_Freeze_Node_For_Instance
8727 begin
8728 if not Is_List_Member (F_Node) then
8729 Decl := N;
8730 Decls := List_Containing (N);
8731 Inst := Entity (F_Node);
8732 Par_N := Parent (Decls);
8734 -- When processing a subprogram instantiation, utilize the actual
8735 -- subprogram instantiation rather than its package wrapper as it
8736 -- carries all the context information.
8738 if Is_Wrapper_Package (Inst) then
8739 Inst := Related_Instance (Inst);
8740 end if;
8742 -- If this is a package instance, check whether the generic is
8743 -- declared in a previous instance and the current instance is
8744 -- not within the previous one.
8746 if Present (Generic_Parent (Parent (Inst)))
8747 and then Is_In_Main_Unit (N)
8748 then
8749 declare
8750 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8751 Par_I : constant Entity_Id :=
8752 Previous_Instance
8753 (Generic_Parent (Parent (Inst)));
8754 Scop : Entity_Id;
8756 begin
8757 if Present (Par_I)
8758 and then Earlier (N, Freeze_Node (Par_I))
8759 then
8760 Scop := Scope (Inst);
8762 -- If the current instance is within the one that contains
8763 -- the generic, the freeze node for the current one must
8764 -- appear in the current declarative part. Ditto, if the
8765 -- current instance is within another package instance or
8766 -- within a body that does not enclose the current instance.
8767 -- In these three cases the freeze node of the previous
8768 -- instance is not relevant.
8770 while Present (Scop) and then Scop /= Standard_Standard loop
8771 exit when Scop = Par_I
8772 or else
8773 (Is_Generic_Instance (Scop)
8774 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8775 Scop := Scope (Scop);
8776 end loop;
8778 -- Previous instance encloses current instance
8780 if Scop = Par_I then
8781 null;
8783 -- If the next node is a source body we must freeze in
8784 -- the current scope as well.
8786 elsif Present (Next (N))
8787 and then Nkind_In (Next (N), N_Subprogram_Body,
8788 N_Package_Body)
8789 and then Comes_From_Source (Next (N))
8790 then
8791 null;
8793 -- Current instance is within an unrelated instance
8795 elsif Is_Generic_Instance (Scop) then
8796 null;
8798 -- Current instance is within an unrelated body
8800 elsif Present (Enclosing_N)
8801 and then Enclosing_N /= Enclosing_Body (Par_I)
8802 then
8803 null;
8805 else
8806 Insert_After (Freeze_Node (Par_I), F_Node);
8807 return;
8808 end if;
8809 end if;
8810 end;
8811 end if;
8813 -- When the instantiation occurs in a package declaration, append the
8814 -- freeze node to the private declarations (if any).
8816 if Nkind (Par_N) = N_Package_Specification
8817 and then Decls = Visible_Declarations (Par_N)
8818 and then Present (Private_Declarations (Par_N))
8819 and then not Is_Empty_List (Private_Declarations (Par_N))
8820 then
8821 Decls := Private_Declarations (Par_N);
8822 Decl := First (Decls);
8823 end if;
8825 -- Determine the proper freeze point of a package instantiation. We
8826 -- adhere to the general rule of a package or subprogram body causing
8827 -- freezing of anything before it in the same declarative region. In
8828 -- this case, the proper freeze point of a package instantiation is
8829 -- before the first source body which follows, or before a stub. This
8830 -- ensures that entities coming from the instance are already frozen
8831 -- and usable in source bodies.
8833 if Nkind (Par_N) /= N_Package_Declaration
8834 and then Ekind (Inst) = E_Package
8835 and then Is_Generic_Instance (Inst)
8836 and then
8837 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8838 then
8839 while Present (Decl) loop
8840 if (Nkind (Decl) in N_Unit_Body
8841 or else
8842 Nkind (Decl) in N_Body_Stub)
8843 and then Comes_From_Source (Decl)
8844 then
8845 Insert_Before (Decl, F_Node);
8846 return;
8847 end if;
8849 Next (Decl);
8850 end loop;
8851 end if;
8853 -- In a package declaration, or if no previous body, insert at end
8854 -- of list.
8856 Set_Sloc (F_Node, Sloc (Last (Decls)));
8857 Insert_After (Last (Decls), F_Node);
8858 end if;
8859 end Insert_Freeze_Node_For_Instance;
8861 ------------------
8862 -- Install_Body --
8863 ------------------
8865 procedure Install_Body
8866 (Act_Body : Node_Id;
8867 N : Node_Id;
8868 Gen_Body : Node_Id;
8869 Gen_Decl : Node_Id)
8871 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8872 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8873 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8874 Par : constant Entity_Id := Scope (Gen_Id);
8875 Gen_Unit : constant Node_Id :=
8876 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8877 Orig_Body : Node_Id := Gen_Body;
8878 F_Node : Node_Id;
8879 Body_Unit : Node_Id;
8881 Must_Delay : Boolean;
8883 function In_Same_Enclosing_Subp return Boolean;
8884 -- Check whether instance and generic body are within same subprogram.
8886 function True_Sloc (N : Node_Id) return Source_Ptr;
8887 -- If the instance is nested inside a generic unit, the Sloc of the
8888 -- instance indicates the place of the original definition, not the
8889 -- point of the current enclosing instance. Pending a better usage of
8890 -- Slocs to indicate instantiation places, we determine the place of
8891 -- origin of a node by finding the maximum sloc of any ancestor node.
8892 -- Why is this not equivalent to Top_Level_Location ???
8894 ----------------------------
8895 -- In_Same_Enclosing_Subp --
8896 ----------------------------
8898 function In_Same_Enclosing_Subp return Boolean is
8899 Scop : Entity_Id;
8900 Subp : Entity_Id;
8902 begin
8903 Scop := Scope (Act_Id);
8904 while Scop /= Standard_Standard
8905 and then not Is_Overloadable (Scop)
8906 loop
8907 Scop := Scope (Scop);
8908 end loop;
8910 if Scop = Standard_Standard then
8911 return False;
8912 else
8913 Subp := Scop;
8914 end if;
8916 Scop := Scope (Gen_Id);
8917 while Scop /= Standard_Standard loop
8918 if Scop = Subp then
8919 return True;
8920 else
8921 Scop := Scope (Scop);
8922 end if;
8923 end loop;
8925 return False;
8926 end In_Same_Enclosing_Subp;
8928 ---------------
8929 -- True_Sloc --
8930 ---------------
8932 function True_Sloc (N : Node_Id) return Source_Ptr is
8933 Res : Source_Ptr;
8934 N1 : Node_Id;
8936 begin
8937 Res := Sloc (N);
8938 N1 := N;
8939 while Present (N1) and then N1 /= Act_Unit loop
8940 if Sloc (N1) > Res then
8941 Res := Sloc (N1);
8942 end if;
8944 N1 := Parent (N1);
8945 end loop;
8947 return Res;
8948 end True_Sloc;
8950 -- Start of processing for Install_Body
8952 begin
8953 -- Handle first the case of an instance with incomplete actual types.
8954 -- The instance body cannot be placed after the declaration because
8955 -- full views have not been seen yet. Any use of the non-limited views
8956 -- in the instance body requires the presence of a regular with_clause
8957 -- in the enclosing unit, and will fail if this with_clause is missing.
8958 -- We place the instance body at the beginning of the enclosing body,
8959 -- which is the unit being compiled. The freeze node for the instance
8960 -- is then placed after the instance body.
8962 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8963 and then Expander_Active
8964 and then Ekind (Scope (Act_Id)) = E_Package
8965 then
8966 declare
8967 Scop : constant Entity_Id := Scope (Act_Id);
8968 Body_Id : constant Node_Id :=
8969 Corresponding_Body (Unit_Declaration_Node (Scop));
8971 begin
8972 Ensure_Freeze_Node (Act_Id);
8973 F_Node := Freeze_Node (Act_Id);
8974 if Present (Body_Id) then
8975 Set_Is_Frozen (Act_Id, False);
8976 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8977 if Is_List_Member (F_Node) then
8978 Remove (F_Node);
8979 end if;
8981 Insert_After (Act_Body, F_Node);
8982 end if;
8983 end;
8984 return;
8985 end if;
8987 -- If the body is a subunit, the freeze point is the corresponding stub
8988 -- in the current compilation, not the subunit itself.
8990 if Nkind (Parent (Gen_Body)) = N_Subunit then
8991 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8992 else
8993 Orig_Body := Gen_Body;
8994 end if;
8996 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8998 -- If the instantiation and the generic definition appear in the same
8999 -- package declaration, this is an early instantiation. If they appear
9000 -- in the same declarative part, it is an early instantiation only if
9001 -- the generic body appears textually later, and the generic body is
9002 -- also in the main unit.
9004 -- If instance is nested within a subprogram, and the generic body
9005 -- is not, the instance is delayed because the enclosing body is. If
9006 -- instance and body are within the same scope, or the same subprogram
9007 -- body, indicate explicitly that the instance is delayed.
9009 Must_Delay :=
9010 (Gen_Unit = Act_Unit
9011 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
9012 N_Generic_Package_Declaration)
9013 or else (Gen_Unit = Body_Unit
9014 and then True_Sloc (N) < Sloc (Orig_Body)))
9015 and then Is_In_Main_Unit (Gen_Unit)
9016 and then (Scope (Act_Id) = Scope (Gen_Id)
9017 or else In_Same_Enclosing_Subp));
9019 -- If this is an early instantiation, the freeze node is placed after
9020 -- the generic body. Otherwise, if the generic appears in an instance,
9021 -- we cannot freeze the current instance until the outer one is frozen.
9022 -- This is only relevant if the current instance is nested within some
9023 -- inner scope not itself within the outer instance. If this scope is
9024 -- a package body in the same declarative part as the outer instance,
9025 -- then that body needs to be frozen after the outer instance. Finally,
9026 -- if no delay is needed, we place the freeze node at the end of the
9027 -- current declarative part.
9029 if Expander_Active then
9030 Ensure_Freeze_Node (Act_Id);
9031 F_Node := Freeze_Node (Act_Id);
9033 if Must_Delay then
9034 Insert_After (Orig_Body, F_Node);
9036 elsif Is_Generic_Instance (Par)
9037 and then Present (Freeze_Node (Par))
9038 and then Scope (Act_Id) /= Par
9039 then
9040 -- Freeze instance of inner generic after instance of enclosing
9041 -- generic.
9043 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9045 -- Handle the following case:
9047 -- package Parent_Inst is new ...
9048 -- Parent_Inst []
9050 -- procedure P ... -- this body freezes Parent_Inst
9052 -- package Inst is new ...
9054 -- In this particular scenario, the freeze node for Inst must
9055 -- be inserted in the same manner as that of Parent_Inst,
9056 -- before the next source body or at the end of the declarative
9057 -- list (body not available). If body P did not exist and
9058 -- Parent_Inst was frozen after Inst, either by a body
9059 -- following Inst or at the end of the declarative region,
9060 -- the freeze node for Inst must be inserted after that of
9061 -- Parent_Inst. This relation is established by comparing
9062 -- the Slocs of Parent_Inst freeze node and Inst.
9064 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9065 List_Containing (N)
9066 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9067 then
9068 Insert_Freeze_Node_For_Instance (N, F_Node);
9069 else
9070 Insert_After (Freeze_Node (Par), F_Node);
9071 end if;
9073 -- Freeze package enclosing instance of inner generic after
9074 -- instance of enclosing generic.
9076 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9077 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9078 then
9079 declare
9080 Enclosing : Entity_Id;
9082 begin
9083 Enclosing := Corresponding_Spec (Parent (N));
9085 if No (Enclosing) then
9086 Enclosing := Defining_Entity (Parent (N));
9087 end if;
9089 Insert_Freeze_Node_For_Instance (N, F_Node);
9090 Ensure_Freeze_Node (Enclosing);
9092 if not Is_List_Member (Freeze_Node (Enclosing)) then
9094 -- The enclosing context is a subunit, insert the freeze
9095 -- node after the stub.
9097 if Nkind (Parent (Parent (N))) = N_Subunit then
9098 Insert_Freeze_Node_For_Instance
9099 (Corresponding_Stub (Parent (Parent (N))),
9100 Freeze_Node (Enclosing));
9102 -- The enclosing context is a package with a stub body
9103 -- which has already been replaced by the real body.
9104 -- Insert the freeze node after the actual body.
9106 elsif Ekind (Enclosing) = E_Package
9107 and then Present (Body_Entity (Enclosing))
9108 and then Was_Originally_Stub
9109 (Parent (Body_Entity (Enclosing)))
9110 then
9111 Insert_Freeze_Node_For_Instance
9112 (Parent (Body_Entity (Enclosing)),
9113 Freeze_Node (Enclosing));
9115 -- The parent instance has been frozen before the body of
9116 -- the enclosing package, insert the freeze node after
9117 -- the body.
9119 elsif List_Containing (Freeze_Node (Par)) =
9120 List_Containing (Parent (N))
9121 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9122 then
9123 Insert_Freeze_Node_For_Instance
9124 (Parent (N), Freeze_Node (Enclosing));
9126 else
9127 Insert_After
9128 (Freeze_Node (Par), Freeze_Node (Enclosing));
9129 end if;
9130 end if;
9131 end;
9133 else
9134 Insert_Freeze_Node_For_Instance (N, F_Node);
9135 end if;
9137 else
9138 Insert_Freeze_Node_For_Instance (N, F_Node);
9139 end if;
9140 end if;
9142 Set_Is_Frozen (Act_Id);
9143 Insert_Before (N, Act_Body);
9144 Mark_Rewrite_Insertion (Act_Body);
9145 end Install_Body;
9147 -----------------------------
9148 -- Install_Formal_Packages --
9149 -----------------------------
9151 procedure Install_Formal_Packages (Par : Entity_Id) is
9152 E : Entity_Id;
9153 Gen : Entity_Id;
9154 Gen_E : Entity_Id := Empty;
9156 begin
9157 E := First_Entity (Par);
9159 -- If we are installing an instance parent, locate the formal packages
9160 -- of its generic parent.
9162 if Is_Generic_Instance (Par) then
9163 Gen := Generic_Parent (Package_Specification (Par));
9164 Gen_E := First_Entity (Gen);
9165 end if;
9167 while Present (E) loop
9168 if Ekind (E) = E_Package
9169 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9170 then
9171 -- If this is the renaming for the parent instance, done
9173 if Renamed_Object (E) = Par then
9174 exit;
9176 -- The visibility of a formal of an enclosing generic is already
9177 -- correct.
9179 elsif Denotes_Formal_Package (E) then
9180 null;
9182 elsif Present (Associated_Formal_Package (E)) then
9183 Check_Generic_Actuals (Renamed_Object (E), True);
9184 Set_Is_Hidden (E, False);
9186 -- Find formal package in generic unit that corresponds to
9187 -- (instance of) formal package in instance.
9189 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9190 Next_Entity (Gen_E);
9191 end loop;
9193 if Present (Gen_E) then
9194 Map_Formal_Package_Entities (Gen_E, E);
9195 end if;
9196 end if;
9197 end if;
9199 Next_Entity (E);
9201 if Present (Gen_E) then
9202 Next_Entity (Gen_E);
9203 end if;
9204 end loop;
9205 end Install_Formal_Packages;
9207 --------------------
9208 -- Install_Parent --
9209 --------------------
9211 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9212 Ancestors : constant Elist_Id := New_Elmt_List;
9213 S : constant Entity_Id := Current_Scope;
9214 Inst_Par : Entity_Id;
9215 First_Par : Entity_Id;
9216 Inst_Node : Node_Id;
9217 Gen_Par : Entity_Id;
9218 First_Gen : Entity_Id;
9219 Elmt : Elmt_Id;
9221 procedure Install_Noninstance_Specs (Par : Entity_Id);
9222 -- Install the scopes of noninstance parent units ending with Par
9224 procedure Install_Spec (Par : Entity_Id);
9225 -- The child unit is within the declarative part of the parent, so the
9226 -- declarations within the parent are immediately visible.
9228 -------------------------------
9229 -- Install_Noninstance_Specs --
9230 -------------------------------
9232 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9233 begin
9234 if Present (Par)
9235 and then Par /= Standard_Standard
9236 and then not In_Open_Scopes (Par)
9237 then
9238 Install_Noninstance_Specs (Scope (Par));
9239 Install_Spec (Par);
9240 end if;
9241 end Install_Noninstance_Specs;
9243 ------------------
9244 -- Install_Spec --
9245 ------------------
9247 procedure Install_Spec (Par : Entity_Id) is
9248 Spec : constant Node_Id := Package_Specification (Par);
9250 begin
9251 -- If this parent of the child instance is a top-level unit,
9252 -- then record the unit and its visibility for later resetting in
9253 -- Remove_Parent. We exclude units that are generic instances, as we
9254 -- only want to record this information for the ultimate top-level
9255 -- noninstance parent (is that always correct???).
9257 if Scope (Par) = Standard_Standard
9258 and then not Is_Generic_Instance (Par)
9259 then
9260 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9261 Instance_Parent_Unit := Par;
9262 end if;
9264 -- Open the parent scope and make it and its declarations visible.
9265 -- If this point is not within a body, then only the visible
9266 -- declarations should be made visible, and installation of the
9267 -- private declarations is deferred until the appropriate point
9268 -- within analysis of the spec being instantiated (see the handling
9269 -- of parent visibility in Analyze_Package_Specification). This is
9270 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9271 -- private view problems that occur when compiling instantiations of
9272 -- a generic child of that package (Generic_Dispatching_Constructor).
9273 -- If the instance freezes a tagged type, inlinings of operations
9274 -- from Ada.Tags may need the full view of type Tag. If inlining took
9275 -- proper account of establishing visibility of inlined subprograms'
9276 -- parents then it should be possible to remove this
9277 -- special check. ???
9279 Push_Scope (Par);
9280 Set_Is_Immediately_Visible (Par);
9281 Install_Visible_Declarations (Par);
9282 Set_Use (Visible_Declarations (Spec));
9284 if In_Body or else Is_RTU (Par, Ada_Tags) then
9285 Install_Private_Declarations (Par);
9286 Set_Use (Private_Declarations (Spec));
9287 end if;
9288 end Install_Spec;
9290 -- Start of processing for Install_Parent
9292 begin
9293 -- We need to install the parent instance to compile the instantiation
9294 -- of the child, but the child instance must appear in the current
9295 -- scope. Given that we cannot place the parent above the current scope
9296 -- in the scope stack, we duplicate the current scope and unstack both
9297 -- after the instantiation is complete.
9299 -- If the parent is itself the instantiation of a child unit, we must
9300 -- also stack the instantiation of its parent, and so on. Each such
9301 -- ancestor is the prefix of the name in a prior instantiation.
9303 -- If this is a nested instance, the parent unit itself resolves to
9304 -- a renaming of the parent instance, whose declaration we need.
9306 -- Finally, the parent may be a generic (not an instance) when the
9307 -- child unit appears as a formal package.
9309 Inst_Par := P;
9311 if Present (Renamed_Entity (Inst_Par)) then
9312 Inst_Par := Renamed_Entity (Inst_Par);
9313 end if;
9315 First_Par := Inst_Par;
9317 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9319 First_Gen := Gen_Par;
9321 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9323 -- Load grandparent instance as well
9325 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9327 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9328 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9330 if Present (Renamed_Entity (Inst_Par)) then
9331 Inst_Par := Renamed_Entity (Inst_Par);
9332 end if;
9334 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9336 if Present (Gen_Par) then
9337 Prepend_Elmt (Inst_Par, Ancestors);
9339 else
9340 -- Parent is not the name of an instantiation
9342 Install_Noninstance_Specs (Inst_Par);
9343 exit;
9344 end if;
9346 else
9347 -- Previous error
9349 exit;
9350 end if;
9351 end loop;
9353 if Present (First_Gen) then
9354 Append_Elmt (First_Par, Ancestors);
9355 else
9356 Install_Noninstance_Specs (First_Par);
9357 end if;
9359 if not Is_Empty_Elmt_List (Ancestors) then
9360 Elmt := First_Elmt (Ancestors);
9361 while Present (Elmt) loop
9362 Install_Spec (Node (Elmt));
9363 Install_Formal_Packages (Node (Elmt));
9364 Next_Elmt (Elmt);
9365 end loop;
9366 end if;
9368 if not In_Body then
9369 Push_Scope (S);
9370 end if;
9371 end Install_Parent;
9373 -------------------------------
9374 -- Install_Hidden_Primitives --
9375 -------------------------------
9377 procedure Install_Hidden_Primitives
9378 (Prims_List : in out Elist_Id;
9379 Gen_T : Entity_Id;
9380 Act_T : Entity_Id)
9382 Elmt : Elmt_Id;
9383 List : Elist_Id := No_Elist;
9384 Prim_G_Elmt : Elmt_Id;
9385 Prim_A_Elmt : Elmt_Id;
9386 Prim_G : Node_Id;
9387 Prim_A : Node_Id;
9389 begin
9390 -- No action needed in case of serious errors because we cannot trust
9391 -- in the order of primitives
9393 if Serious_Errors_Detected > 0 then
9394 return;
9396 -- No action possible if we don't have available the list of primitive
9397 -- operations
9399 elsif No (Gen_T)
9400 or else not Is_Record_Type (Gen_T)
9401 or else not Is_Tagged_Type (Gen_T)
9402 or else not Is_Record_Type (Act_T)
9403 or else not Is_Tagged_Type (Act_T)
9404 then
9405 return;
9407 -- There is no need to handle interface types since their primitives
9408 -- cannot be hidden
9410 elsif Is_Interface (Gen_T) then
9411 return;
9412 end if;
9414 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9416 if not Is_Class_Wide_Type (Act_T) then
9417 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9418 else
9419 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9420 end if;
9422 loop
9423 -- Skip predefined primitives in the generic formal
9425 while Present (Prim_G_Elmt)
9426 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9427 loop
9428 Next_Elmt (Prim_G_Elmt);
9429 end loop;
9431 -- Skip predefined primitives in the generic actual
9433 while Present (Prim_A_Elmt)
9434 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9435 loop
9436 Next_Elmt (Prim_A_Elmt);
9437 end loop;
9439 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9441 Prim_G := Node (Prim_G_Elmt);
9442 Prim_A := Node (Prim_A_Elmt);
9444 -- There is no need to handle interface primitives because their
9445 -- primitives are not hidden
9447 exit when Present (Interface_Alias (Prim_G));
9449 -- Here we install one hidden primitive
9451 if Chars (Prim_G) /= Chars (Prim_A)
9452 and then Has_Suffix (Prim_A, 'P')
9453 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9454 then
9455 Set_Chars (Prim_A, Chars (Prim_G));
9456 Append_New_Elmt (Prim_A, To => List);
9457 end if;
9459 Next_Elmt (Prim_A_Elmt);
9460 Next_Elmt (Prim_G_Elmt);
9461 end loop;
9463 -- Append the elements to the list of temporarily visible primitives
9464 -- avoiding duplicates.
9466 if Present (List) then
9467 if No (Prims_List) then
9468 Prims_List := New_Elmt_List;
9469 end if;
9471 Elmt := First_Elmt (List);
9472 while Present (Elmt) loop
9473 Append_Unique_Elmt (Node (Elmt), Prims_List);
9474 Next_Elmt (Elmt);
9475 end loop;
9476 end if;
9477 end Install_Hidden_Primitives;
9479 -------------------------------
9480 -- Restore_Hidden_Primitives --
9481 -------------------------------
9483 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9484 Prim_Elmt : Elmt_Id;
9485 Prim : Node_Id;
9487 begin
9488 if Prims_List /= No_Elist then
9489 Prim_Elmt := First_Elmt (Prims_List);
9490 while Present (Prim_Elmt) loop
9491 Prim := Node (Prim_Elmt);
9492 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9493 Next_Elmt (Prim_Elmt);
9494 end loop;
9496 Prims_List := No_Elist;
9497 end if;
9498 end Restore_Hidden_Primitives;
9500 --------------------------------
9501 -- Instantiate_Formal_Package --
9502 --------------------------------
9504 function Instantiate_Formal_Package
9505 (Formal : Node_Id;
9506 Actual : Node_Id;
9507 Analyzed_Formal : Node_Id) return List_Id
9509 Loc : constant Source_Ptr := Sloc (Actual);
9510 Actual_Pack : Entity_Id;
9511 Formal_Pack : Entity_Id;
9512 Gen_Parent : Entity_Id;
9513 Decls : List_Id;
9514 Nod : Node_Id;
9515 Parent_Spec : Node_Id;
9517 procedure Find_Matching_Actual
9518 (F : Node_Id;
9519 Act : in out Entity_Id);
9520 -- We need to associate each formal entity in the formal package with
9521 -- the corresponding entity in the actual package. The actual package
9522 -- has been analyzed and possibly expanded, and as a result there is
9523 -- no one-to-one correspondence between the two lists (for example,
9524 -- the actual may include subtypes, itypes, and inherited primitive
9525 -- operations, interspersed among the renaming declarations for the
9526 -- actuals). We retrieve the corresponding actual by name because each
9527 -- actual has the same name as the formal, and they do appear in the
9528 -- same order.
9530 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9531 -- Retrieve entity of defining entity of generic formal parameter.
9532 -- Only the declarations of formals need to be considered when
9533 -- linking them to actuals, but the declarative list may include
9534 -- internal entities generated during analysis, and those are ignored.
9536 procedure Match_Formal_Entity
9537 (Formal_Node : Node_Id;
9538 Formal_Ent : Entity_Id;
9539 Actual_Ent : Entity_Id);
9540 -- Associates the formal entity with the actual. In the case where
9541 -- Formal_Ent is a formal package, this procedure iterates through all
9542 -- of its formals and enters associations between the actuals occurring
9543 -- in the formal package's corresponding actual package (given by
9544 -- Actual_Ent) and the formal package's formal parameters. This
9545 -- procedure recurses if any of the parameters is itself a package.
9547 function Is_Instance_Of
9548 (Act_Spec : Entity_Id;
9549 Gen_Anc : Entity_Id) return Boolean;
9550 -- The actual can be an instantiation of a generic within another
9551 -- instance, in which case there is no direct link from it to the
9552 -- original generic ancestor. In that case, we recognize that the
9553 -- ultimate ancestor is the same by examining names and scopes.
9555 procedure Process_Nested_Formal (Formal : Entity_Id);
9556 -- If the current formal is declared with a box, its own formals are
9557 -- visible in the instance, as they were in the generic, and their
9558 -- Hidden flag must be reset. If some of these formals are themselves
9559 -- packages declared with a box, the processing must be recursive.
9561 --------------------------
9562 -- Find_Matching_Actual --
9563 --------------------------
9565 procedure Find_Matching_Actual
9566 (F : Node_Id;
9567 Act : in out Entity_Id)
9569 Formal_Ent : Entity_Id;
9571 begin
9572 case Nkind (Original_Node (F)) is
9573 when N_Formal_Object_Declaration |
9574 N_Formal_Type_Declaration =>
9575 Formal_Ent := Defining_Identifier (F);
9577 while Chars (Act) /= Chars (Formal_Ent) loop
9578 Next_Entity (Act);
9579 end loop;
9581 when N_Formal_Subprogram_Declaration |
9582 N_Formal_Package_Declaration |
9583 N_Package_Declaration |
9584 N_Generic_Package_Declaration =>
9585 Formal_Ent := Defining_Entity (F);
9587 while Chars (Act) /= Chars (Formal_Ent) loop
9588 Next_Entity (Act);
9589 end loop;
9591 when others =>
9592 raise Program_Error;
9593 end case;
9594 end Find_Matching_Actual;
9596 -------------------------
9597 -- Match_Formal_Entity --
9598 -------------------------
9600 procedure Match_Formal_Entity
9601 (Formal_Node : Node_Id;
9602 Formal_Ent : Entity_Id;
9603 Actual_Ent : Entity_Id)
9605 Act_Pkg : Entity_Id;
9607 begin
9608 Set_Instance_Of (Formal_Ent, Actual_Ent);
9610 if Ekind (Actual_Ent) = E_Package then
9612 -- Record associations for each parameter
9614 Act_Pkg := Actual_Ent;
9616 declare
9617 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9618 F_Ent : Entity_Id;
9619 F_Node : Node_Id;
9621 Gen_Decl : Node_Id;
9622 Formals : List_Id;
9623 Actual : Entity_Id;
9625 begin
9626 -- Retrieve the actual given in the formal package declaration
9628 Actual := Entity (Name (Original_Node (Formal_Node)));
9630 -- The actual in the formal package declaration may be a
9631 -- renamed generic package, in which case we want to retrieve
9632 -- the original generic in order to traverse its formal part.
9634 if Present (Renamed_Entity (Actual)) then
9635 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9636 else
9637 Gen_Decl := Unit_Declaration_Node (Actual);
9638 end if;
9640 Formals := Generic_Formal_Declarations (Gen_Decl);
9642 if Present (Formals) then
9643 F_Node := First_Non_Pragma (Formals);
9644 else
9645 F_Node := Empty;
9646 end if;
9648 while Present (A_Ent)
9649 and then Present (F_Node)
9650 and then A_Ent /= First_Private_Entity (Act_Pkg)
9651 loop
9652 F_Ent := Get_Formal_Entity (F_Node);
9654 if Present (F_Ent) then
9656 -- This is a formal of the original package. Record
9657 -- association and recurse.
9659 Find_Matching_Actual (F_Node, A_Ent);
9660 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9661 Next_Entity (A_Ent);
9662 end if;
9664 Next_Non_Pragma (F_Node);
9665 end loop;
9666 end;
9667 end if;
9668 end Match_Formal_Entity;
9670 -----------------------
9671 -- Get_Formal_Entity --
9672 -----------------------
9674 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9675 Kind : constant Node_Kind := Nkind (Original_Node (N));
9676 begin
9677 case Kind is
9678 when N_Formal_Object_Declaration =>
9679 return Defining_Identifier (N);
9681 when N_Formal_Type_Declaration =>
9682 return Defining_Identifier (N);
9684 when N_Formal_Subprogram_Declaration =>
9685 return Defining_Unit_Name (Specification (N));
9687 when N_Formal_Package_Declaration =>
9688 return Defining_Identifier (Original_Node (N));
9690 when N_Generic_Package_Declaration =>
9691 return Defining_Identifier (Original_Node (N));
9693 -- All other declarations are introduced by semantic analysis and
9694 -- have no match in the actual.
9696 when others =>
9697 return Empty;
9698 end case;
9699 end Get_Formal_Entity;
9701 --------------------
9702 -- Is_Instance_Of --
9703 --------------------
9705 function Is_Instance_Of
9706 (Act_Spec : Entity_Id;
9707 Gen_Anc : Entity_Id) return Boolean
9709 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9711 begin
9712 if No (Gen_Par) then
9713 return False;
9715 -- Simplest case: the generic parent of the actual is the formal
9717 elsif Gen_Par = Gen_Anc then
9718 return True;
9720 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9721 return False;
9723 -- The actual may be obtained through several instantiations. Its
9724 -- scope must itself be an instance of a generic declared in the
9725 -- same scope as the formal. Any other case is detected above.
9727 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9728 return False;
9730 else
9731 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9732 end if;
9733 end Is_Instance_Of;
9735 ---------------------------
9736 -- Process_Nested_Formal --
9737 ---------------------------
9739 procedure Process_Nested_Formal (Formal : Entity_Id) is
9740 Ent : Entity_Id;
9742 begin
9743 if Present (Associated_Formal_Package (Formal))
9744 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9745 then
9746 Ent := First_Entity (Formal);
9747 while Present (Ent) loop
9748 Set_Is_Hidden (Ent, False);
9749 Set_Is_Visible_Formal (Ent);
9750 Set_Is_Potentially_Use_Visible
9751 (Ent, Is_Potentially_Use_Visible (Formal));
9753 if Ekind (Ent) = E_Package then
9754 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9755 Process_Nested_Formal (Ent);
9756 end if;
9758 Next_Entity (Ent);
9759 end loop;
9760 end if;
9761 end Process_Nested_Formal;
9763 -- Start of processing for Instantiate_Formal_Package
9765 begin
9766 Analyze (Actual);
9768 if not Is_Entity_Name (Actual)
9769 or else Ekind (Entity (Actual)) /= E_Package
9770 then
9771 Error_Msg_N
9772 ("expect package instance to instantiate formal", Actual);
9773 Abandon_Instantiation (Actual);
9774 raise Program_Error;
9776 else
9777 Actual_Pack := Entity (Actual);
9778 Set_Is_Instantiated (Actual_Pack);
9780 -- The actual may be a renamed package, or an outer generic formal
9781 -- package whose instantiation is converted into a renaming.
9783 if Present (Renamed_Object (Actual_Pack)) then
9784 Actual_Pack := Renamed_Object (Actual_Pack);
9785 end if;
9787 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9788 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9789 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9790 else
9791 Gen_Parent :=
9792 Generic_Parent (Specification (Analyzed_Formal));
9793 Formal_Pack :=
9794 Defining_Unit_Name (Specification (Analyzed_Formal));
9795 end if;
9797 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9798 Parent_Spec := Package_Specification (Actual_Pack);
9799 else
9800 Parent_Spec := Parent (Actual_Pack);
9801 end if;
9803 if Gen_Parent = Any_Id then
9804 Error_Msg_N
9805 ("previous error in declaration of formal package", Actual);
9806 Abandon_Instantiation (Actual);
9808 elsif
9809 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9810 then
9811 null;
9813 else
9814 Error_Msg_NE
9815 ("actual parameter must be instance of&", Actual, Gen_Parent);
9816 Abandon_Instantiation (Actual);
9817 end if;
9819 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9820 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9822 Nod :=
9823 Make_Package_Renaming_Declaration (Loc,
9824 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9825 Name => New_Occurrence_Of (Actual_Pack, Loc));
9827 Set_Associated_Formal_Package
9828 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9829 Decls := New_List (Nod);
9831 -- If the formal F has a box, then the generic declarations are
9832 -- visible in the generic G. In an instance of G, the corresponding
9833 -- entities in the actual for F (which are the actuals for the
9834 -- instantiation of the generic that F denotes) must also be made
9835 -- visible for analysis of the current instance. On exit from the
9836 -- current instance, those entities are made private again. If the
9837 -- actual is currently in use, these entities are also use-visible.
9839 -- The loop through the actual entities also steps through the formal
9840 -- entities and enters associations from formals to actuals into the
9841 -- renaming map. This is necessary to properly handle checking of
9842 -- actual parameter associations for later formals that depend on
9843 -- actuals declared in the formal package.
9845 -- In Ada 2005, partial parameterization requires that we make
9846 -- visible the actuals corresponding to formals that were defaulted
9847 -- in the formal package. There formals are identified because they
9848 -- remain formal generics within the formal package, rather than
9849 -- being renamings of the actuals supplied.
9851 declare
9852 Gen_Decl : constant Node_Id :=
9853 Unit_Declaration_Node (Gen_Parent);
9854 Formals : constant List_Id :=
9855 Generic_Formal_Declarations (Gen_Decl);
9857 Actual_Ent : Entity_Id;
9858 Actual_Of_Formal : Node_Id;
9859 Formal_Node : Node_Id;
9860 Formal_Ent : Entity_Id;
9862 begin
9863 if Present (Formals) then
9864 Formal_Node := First_Non_Pragma (Formals);
9865 else
9866 Formal_Node := Empty;
9867 end if;
9869 Actual_Ent := First_Entity (Actual_Pack);
9870 Actual_Of_Formal :=
9871 First (Visible_Declarations (Specification (Analyzed_Formal)));
9872 while Present (Actual_Ent)
9873 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9874 loop
9875 if Present (Formal_Node) then
9876 Formal_Ent := Get_Formal_Entity (Formal_Node);
9878 if Present (Formal_Ent) then
9879 Find_Matching_Actual (Formal_Node, Actual_Ent);
9880 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9882 -- We iterate at the same time over the actuals of the
9883 -- local package created for the formal, to determine
9884 -- which one of the formals of the original generic were
9885 -- defaulted in the formal. The corresponding actual
9886 -- entities are visible in the enclosing instance.
9888 if Box_Present (Formal)
9889 or else
9890 (Present (Actual_Of_Formal)
9891 and then
9892 Is_Generic_Formal
9893 (Get_Formal_Entity (Actual_Of_Formal)))
9894 then
9895 Set_Is_Hidden (Actual_Ent, False);
9896 Set_Is_Visible_Formal (Actual_Ent);
9897 Set_Is_Potentially_Use_Visible
9898 (Actual_Ent, In_Use (Actual_Pack));
9900 if Ekind (Actual_Ent) = E_Package then
9901 Process_Nested_Formal (Actual_Ent);
9902 end if;
9904 else
9905 Set_Is_Hidden (Actual_Ent);
9906 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9907 end if;
9908 end if;
9910 Next_Non_Pragma (Formal_Node);
9911 Next (Actual_Of_Formal);
9913 else
9914 -- No further formals to match, but the generic part may
9915 -- contain inherited operation that are not hidden in the
9916 -- enclosing instance.
9918 Next_Entity (Actual_Ent);
9919 end if;
9920 end loop;
9922 -- Inherited subprograms generated by formal derived types are
9923 -- also visible if the types are.
9925 Actual_Ent := First_Entity (Actual_Pack);
9926 while Present (Actual_Ent)
9927 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9928 loop
9929 if Is_Overloadable (Actual_Ent)
9930 and then
9931 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9932 and then
9933 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9934 then
9935 Set_Is_Hidden (Actual_Ent, False);
9936 Set_Is_Potentially_Use_Visible
9937 (Actual_Ent, In_Use (Actual_Pack));
9938 end if;
9940 Next_Entity (Actual_Ent);
9941 end loop;
9942 end;
9944 -- If the formal is not declared with a box, reanalyze it as an
9945 -- abbreviated instantiation, to verify the matching rules of 12.7.
9946 -- The actual checks are performed after the generic associations
9947 -- have been analyzed, to guarantee the same visibility for this
9948 -- instantiation and for the actuals.
9950 -- In Ada 2005, the generic associations for the formal can include
9951 -- defaulted parameters. These are ignored during check. This
9952 -- internal instantiation is removed from the tree after conformance
9953 -- checking, because it contains formal declarations for those
9954 -- defaulted parameters, and those should not reach the back-end.
9956 if not Box_Present (Formal) then
9957 declare
9958 I_Pack : constant Entity_Id :=
9959 Make_Temporary (Sloc (Actual), 'P');
9961 begin
9962 Set_Is_Internal (I_Pack);
9964 Append_To (Decls,
9965 Make_Package_Instantiation (Sloc (Actual),
9966 Defining_Unit_Name => I_Pack,
9967 Name =>
9968 New_Occurrence_Of
9969 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9970 Generic_Associations => Generic_Associations (Formal)));
9971 end;
9972 end if;
9974 return Decls;
9975 end if;
9976 end Instantiate_Formal_Package;
9978 -----------------------------------
9979 -- Instantiate_Formal_Subprogram --
9980 -----------------------------------
9982 function Instantiate_Formal_Subprogram
9983 (Formal : Node_Id;
9984 Actual : Node_Id;
9985 Analyzed_Formal : Node_Id) return Node_Id
9987 Analyzed_S : constant Entity_Id :=
9988 Defining_Unit_Name (Specification (Analyzed_Formal));
9989 Formal_Sub : constant Entity_Id :=
9990 Defining_Unit_Name (Specification (Formal));
9992 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9993 -- If the generic is a child unit, the parent has been installed on the
9994 -- scope stack, but a default subprogram cannot resolve to something
9995 -- on the parent because that parent is not really part of the visible
9996 -- context (it is there to resolve explicit local entities). If the
9997 -- default has resolved in this way, we remove the entity from immediate
9998 -- visibility and analyze the node again to emit an error message or
9999 -- find another visible candidate.
10001 procedure Valid_Actual_Subprogram (Act : Node_Id);
10002 -- Perform legality check and raise exception on failure
10004 -----------------------
10005 -- From_Parent_Scope --
10006 -----------------------
10008 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10009 Gen_Scope : Node_Id;
10011 begin
10012 Gen_Scope := Scope (Analyzed_S);
10013 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10014 if Scope (Subp) = Scope (Gen_Scope) then
10015 return True;
10016 end if;
10018 Gen_Scope := Scope (Gen_Scope);
10019 end loop;
10021 return False;
10022 end From_Parent_Scope;
10024 -----------------------------
10025 -- Valid_Actual_Subprogram --
10026 -----------------------------
10028 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10029 Act_E : Entity_Id;
10031 begin
10032 if Is_Entity_Name (Act) then
10033 Act_E := Entity (Act);
10035 elsif Nkind (Act) = N_Selected_Component
10036 and then Is_Entity_Name (Selector_Name (Act))
10037 then
10038 Act_E := Entity (Selector_Name (Act));
10040 else
10041 Act_E := Empty;
10042 end if;
10044 if (Present (Act_E) and then Is_Overloadable (Act_E))
10045 or else Nkind_In (Act, N_Attribute_Reference,
10046 N_Indexed_Component,
10047 N_Character_Literal,
10048 N_Explicit_Dereference)
10049 then
10050 return;
10051 end if;
10053 Error_Msg_NE
10054 ("expect subprogram or entry name in instantiation of &",
10055 Instantiation_Node, Formal_Sub);
10056 Abandon_Instantiation (Instantiation_Node);
10057 end Valid_Actual_Subprogram;
10059 -- Local variables
10061 Decl_Node : Node_Id;
10062 Loc : Source_Ptr;
10063 Nam : Node_Id;
10064 New_Spec : Node_Id;
10065 New_Subp : Entity_Id;
10067 -- Start of processing for Instantiate_Formal_Subprogram
10069 begin
10070 New_Spec := New_Copy_Tree (Specification (Formal));
10072 -- The tree copy has created the proper instantiation sloc for the
10073 -- new specification. Use this location for all other constructed
10074 -- declarations.
10076 Loc := Sloc (Defining_Unit_Name (New_Spec));
10078 -- Create new entity for the actual (New_Copy_Tree does not), and
10079 -- indicate that it is an actual.
10081 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10082 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10083 Set_Is_Generic_Actual_Subprogram (New_Subp);
10084 Set_Defining_Unit_Name (New_Spec, New_Subp);
10086 -- Create new entities for the each of the formals in the specification
10087 -- of the renaming declaration built for the actual.
10089 if Present (Parameter_Specifications (New_Spec)) then
10090 declare
10091 F : Node_Id;
10092 F_Id : Entity_Id;
10094 begin
10095 F := First (Parameter_Specifications (New_Spec));
10096 while Present (F) loop
10097 F_Id := Defining_Identifier (F);
10099 Set_Defining_Identifier (F,
10100 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10101 Next (F);
10102 end loop;
10103 end;
10104 end if;
10106 -- Find entity of actual. If the actual is an attribute reference, it
10107 -- cannot be resolved here (its formal is missing) but is handled
10108 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10109 -- fully resolved subsequently, when the renaming declaration for the
10110 -- formal is analyzed. If it is an explicit dereference, resolve the
10111 -- prefix but not the actual itself, to prevent interpretation as call.
10113 if Present (Actual) then
10114 Loc := Sloc (Actual);
10115 Set_Sloc (New_Spec, Loc);
10117 if Nkind (Actual) = N_Operator_Symbol then
10118 Find_Direct_Name (Actual);
10120 elsif Nkind (Actual) = N_Explicit_Dereference then
10121 Analyze (Prefix (Actual));
10123 elsif Nkind (Actual) /= N_Attribute_Reference then
10124 Analyze (Actual);
10125 end if;
10127 Valid_Actual_Subprogram (Actual);
10128 Nam := Actual;
10130 elsif Present (Default_Name (Formal)) then
10131 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10132 N_Selected_Component,
10133 N_Indexed_Component,
10134 N_Character_Literal)
10135 and then Present (Entity (Default_Name (Formal)))
10136 then
10137 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10138 else
10139 Nam := New_Copy (Default_Name (Formal));
10140 Set_Sloc (Nam, Loc);
10141 end if;
10143 elsif Box_Present (Formal) then
10145 -- Actual is resolved at the point of instantiation. Create an
10146 -- identifier or operator with the same name as the formal.
10148 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10149 Nam :=
10150 Make_Operator_Symbol (Loc,
10151 Chars => Chars (Formal_Sub),
10152 Strval => No_String);
10153 else
10154 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10155 end if;
10157 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10158 and then Null_Present (Specification (Formal))
10159 then
10160 -- Generate null body for procedure, for use in the instance
10162 Decl_Node :=
10163 Make_Subprogram_Body (Loc,
10164 Specification => New_Spec,
10165 Declarations => New_List,
10166 Handled_Statement_Sequence =>
10167 Make_Handled_Sequence_Of_Statements (Loc,
10168 Statements => New_List (Make_Null_Statement (Loc))));
10170 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10171 return Decl_Node;
10173 else
10174 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10175 Error_Msg_NE
10176 ("missing actual&", Instantiation_Node, Formal_Sub);
10177 Error_Msg_NE
10178 ("\in instantiation of & declared#",
10179 Instantiation_Node, Scope (Analyzed_S));
10180 Abandon_Instantiation (Instantiation_Node);
10181 end if;
10183 Decl_Node :=
10184 Make_Subprogram_Renaming_Declaration (Loc,
10185 Specification => New_Spec,
10186 Name => Nam);
10188 -- If we do not have an actual and the formal specified <> then set to
10189 -- get proper default.
10191 if No (Actual) and then Box_Present (Formal) then
10192 Set_From_Default (Decl_Node);
10193 end if;
10195 -- Gather possible interpretations for the actual before analyzing the
10196 -- instance. If overloaded, it will be resolved when analyzing the
10197 -- renaming declaration.
10199 if Box_Present (Formal) and then No (Actual) then
10200 Analyze (Nam);
10202 if Is_Child_Unit (Scope (Analyzed_S))
10203 and then Present (Entity (Nam))
10204 then
10205 if not Is_Overloaded (Nam) then
10206 if From_Parent_Scope (Entity (Nam)) then
10207 Set_Is_Immediately_Visible (Entity (Nam), False);
10208 Set_Entity (Nam, Empty);
10209 Set_Etype (Nam, Empty);
10211 Analyze (Nam);
10212 Set_Is_Immediately_Visible (Entity (Nam));
10213 end if;
10215 else
10216 declare
10217 I : Interp_Index;
10218 It : Interp;
10220 begin
10221 Get_First_Interp (Nam, I, It);
10222 while Present (It.Nam) loop
10223 if From_Parent_Scope (It.Nam) then
10224 Remove_Interp (I);
10225 end if;
10227 Get_Next_Interp (I, It);
10228 end loop;
10229 end;
10230 end if;
10231 end if;
10232 end if;
10234 -- The generic instantiation freezes the actual. This can only be done
10235 -- once the actual is resolved, in the analysis of the renaming
10236 -- declaration. To make the formal subprogram entity available, we set
10237 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10238 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10239 -- of formal abstract subprograms.
10241 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10243 -- We cannot analyze the renaming declaration, and thus find the actual,
10244 -- until all the actuals are assembled in the instance. For subsequent
10245 -- checks of other actuals, indicate the node that will hold the
10246 -- instance of this formal.
10248 Set_Instance_Of (Analyzed_S, Nam);
10250 if Nkind (Actual) = N_Selected_Component
10251 and then Is_Task_Type (Etype (Prefix (Actual)))
10252 and then not Is_Frozen (Etype (Prefix (Actual)))
10253 then
10254 -- The renaming declaration will create a body, which must appear
10255 -- outside of the instantiation, We move the renaming declaration
10256 -- out of the instance, and create an additional renaming inside,
10257 -- to prevent freezing anomalies.
10259 declare
10260 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10262 begin
10263 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10264 Insert_Before (Instantiation_Node, Decl_Node);
10265 Analyze (Decl_Node);
10267 -- Now create renaming within the instance
10269 Decl_Node :=
10270 Make_Subprogram_Renaming_Declaration (Loc,
10271 Specification => New_Copy_Tree (New_Spec),
10272 Name => New_Occurrence_Of (Anon_Id, Loc));
10274 Set_Defining_Unit_Name (Specification (Decl_Node),
10275 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10276 end;
10277 end if;
10279 return Decl_Node;
10280 end Instantiate_Formal_Subprogram;
10282 ------------------------
10283 -- Instantiate_Object --
10284 ------------------------
10286 function Instantiate_Object
10287 (Formal : Node_Id;
10288 Actual : Node_Id;
10289 Analyzed_Formal : Node_Id) return List_Id
10291 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10292 A_Gen_Obj : constant Entity_Id :=
10293 Defining_Identifier (Analyzed_Formal);
10294 Acc_Def : Node_Id := Empty;
10295 Act_Assoc : constant Node_Id := Parent (Actual);
10296 Actual_Decl : Node_Id := Empty;
10297 Decl_Node : Node_Id;
10298 Def : Node_Id;
10299 Ftyp : Entity_Id;
10300 List : constant List_Id := New_List;
10301 Loc : constant Source_Ptr := Sloc (Actual);
10302 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10303 Subt_Decl : Node_Id := Empty;
10304 Subt_Mark : Node_Id := Empty;
10306 function Copy_Access_Def return Node_Id;
10307 -- If formal is an anonymous access, copy access definition of formal
10308 -- for generated object declaration.
10310 ---------------------
10311 -- Copy_Access_Def --
10312 ---------------------
10314 function Copy_Access_Def return Node_Id is
10315 begin
10316 Def := New_Copy_Tree (Acc_Def);
10318 -- In addition, if formal is an access to subprogram we need to
10319 -- generate new formals for the signature of the default, so that
10320 -- the tree is properly formatted for ASIS use.
10322 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10323 declare
10324 Par_Spec : Node_Id;
10325 begin
10326 Par_Spec :=
10327 First (Parameter_Specifications
10328 (Access_To_Subprogram_Definition (Def)));
10329 while Present (Par_Spec) loop
10330 Set_Defining_Identifier (Par_Spec,
10331 Make_Defining_Identifier (Sloc (Acc_Def),
10332 Chars => Chars (Defining_Identifier (Par_Spec))));
10333 Next (Par_Spec);
10334 end loop;
10335 end;
10336 end if;
10338 return Def;
10339 end Copy_Access_Def;
10341 -- Start of processing for Instantiate_Object
10343 begin
10344 -- Formal may be an anonymous access
10346 if Present (Subtype_Mark (Formal)) then
10347 Subt_Mark := Subtype_Mark (Formal);
10348 else
10349 Check_Access_Definition (Formal);
10350 Acc_Def := Access_Definition (Formal);
10351 end if;
10353 -- Sloc for error message on missing actual
10355 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10357 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10358 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10359 end if;
10361 Set_Parent (List, Parent (Actual));
10363 -- OUT present
10365 if Out_Present (Formal) then
10367 -- An IN OUT generic actual must be a name. The instantiation is a
10368 -- renaming declaration. The actual is the name being renamed. We
10369 -- use the actual directly, rather than a copy, because it is not
10370 -- used further in the list of actuals, and because a copy or a use
10371 -- of relocate_node is incorrect if the instance is nested within a
10372 -- generic. In order to simplify ASIS searches, the Generic_Parent
10373 -- field links the declaration to the generic association.
10375 if No (Actual) then
10376 Error_Msg_NE
10377 ("missing actual &",
10378 Instantiation_Node, Gen_Obj);
10379 Error_Msg_NE
10380 ("\in instantiation of & declared#",
10381 Instantiation_Node, Scope (A_Gen_Obj));
10382 Abandon_Instantiation (Instantiation_Node);
10383 end if;
10385 if Present (Subt_Mark) then
10386 Decl_Node :=
10387 Make_Object_Renaming_Declaration (Loc,
10388 Defining_Identifier => New_Copy (Gen_Obj),
10389 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10390 Name => Actual);
10392 else pragma Assert (Present (Acc_Def));
10393 Decl_Node :=
10394 Make_Object_Renaming_Declaration (Loc,
10395 Defining_Identifier => New_Copy (Gen_Obj),
10396 Access_Definition => New_Copy_Tree (Acc_Def),
10397 Name => Actual);
10398 end if;
10400 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10402 -- The analysis of the actual may produce Insert_Action nodes, so
10403 -- the declaration must have a context in which to attach them.
10405 Append (Decl_Node, List);
10406 Analyze (Actual);
10408 -- Return if the analysis of the actual reported some error
10410 if Etype (Actual) = Any_Type then
10411 return List;
10412 end if;
10414 -- This check is performed here because Analyze_Object_Renaming will
10415 -- not check it when Comes_From_Source is False. Note though that the
10416 -- check for the actual being the name of an object will be performed
10417 -- in Analyze_Object_Renaming.
10419 if Is_Object_Reference (Actual)
10420 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10421 then
10422 Error_Msg_N
10423 ("illegal discriminant-dependent component for in out parameter",
10424 Actual);
10425 end if;
10427 -- The actual has to be resolved in order to check that it is a
10428 -- variable (due to cases such as F (1), where F returns access to
10429 -- an array, and for overloaded prefixes).
10431 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10433 -- If the type of the formal is not itself a formal, and the current
10434 -- unit is a child unit, the formal type must be declared in a
10435 -- parent, and must be retrieved by visibility.
10437 if Ftyp = Orig_Ftyp
10438 and then Is_Generic_Unit (Scope (Ftyp))
10439 and then Is_Child_Unit (Scope (A_Gen_Obj))
10440 then
10441 declare
10442 Temp : constant Node_Id :=
10443 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10444 begin
10445 Set_Entity (Temp, Empty);
10446 Find_Type (Temp);
10447 Ftyp := Entity (Temp);
10448 end;
10449 end if;
10451 if Is_Private_Type (Ftyp)
10452 and then not Is_Private_Type (Etype (Actual))
10453 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10454 or else Base_Type (Etype (Actual)) = Ftyp)
10455 then
10456 -- If the actual has the type of the full view of the formal, or
10457 -- else a non-private subtype of the formal, then the visibility
10458 -- of the formal type has changed. Add to the actuals a subtype
10459 -- declaration that will force the exchange of views in the body
10460 -- of the instance as well.
10462 Subt_Decl :=
10463 Make_Subtype_Declaration (Loc,
10464 Defining_Identifier => Make_Temporary (Loc, 'P'),
10465 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10467 Prepend (Subt_Decl, List);
10469 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10470 Exchange_Declarations (Ftyp);
10471 end if;
10473 Resolve (Actual, Ftyp);
10475 if not Denotes_Variable (Actual) then
10476 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10478 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10480 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10481 -- the type of the actual shall resolve to a specific anonymous
10482 -- access type.
10484 if Ada_Version < Ada_2005
10485 or else Ekind (Base_Type (Ftyp)) /=
10486 E_Anonymous_Access_Type
10487 or else Ekind (Base_Type (Etype (Actual))) /=
10488 E_Anonymous_Access_Type
10489 then
10490 Error_Msg_NE
10491 ("type of actual does not match type of&", Actual, Gen_Obj);
10492 end if;
10493 end if;
10495 Note_Possible_Modification (Actual, Sure => True);
10497 -- Check for instantiation of atomic/volatile actual for
10498 -- non-atomic/volatile formal (RM C.6 (12)).
10500 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10501 Error_Msg_N
10502 ("cannot instantiate non-atomic formal object "
10503 & "with atomic actual", Actual);
10505 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10506 then
10507 Error_Msg_N
10508 ("cannot instantiate non-volatile formal object "
10509 & "with volatile actual", Actual);
10510 end if;
10512 -- Formal in-parameter
10514 else
10515 -- The instantiation of a generic formal in-parameter is constant
10516 -- declaration. The actual is the expression for that declaration.
10517 -- Its type is a full copy of the type of the formal. This may be
10518 -- an access to subprogram, for which we need to generate entities
10519 -- for the formals in the new signature.
10521 if Present (Actual) then
10522 if Present (Subt_Mark) then
10523 Def := New_Copy_Tree (Subt_Mark);
10524 else pragma Assert (Present (Acc_Def));
10525 Def := Copy_Access_Def;
10526 end if;
10528 Decl_Node :=
10529 Make_Object_Declaration (Loc,
10530 Defining_Identifier => New_Copy (Gen_Obj),
10531 Constant_Present => True,
10532 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10533 Object_Definition => Def,
10534 Expression => Actual);
10536 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10538 -- A generic formal object of a tagged type is defined to be
10539 -- aliased so the new constant must also be treated as aliased.
10541 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10542 Set_Aliased_Present (Decl_Node);
10543 end if;
10545 Append (Decl_Node, List);
10547 -- No need to repeat (pre-)analysis of some expression nodes
10548 -- already handled in Preanalyze_Actuals.
10550 if Nkind (Actual) /= N_Allocator then
10551 Analyze (Actual);
10553 -- Return if the analysis of the actual reported some error
10555 if Etype (Actual) = Any_Type then
10556 return List;
10557 end if;
10558 end if;
10560 declare
10561 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10562 Typ : Entity_Id;
10564 begin
10565 Typ := Get_Instance_Of (Formal_Type);
10567 -- If the actual appears in the current or an enclosing scope,
10568 -- use its type directly. This is relevant if it has an actual
10569 -- subtype that is distinct from its nominal one. This cannot
10570 -- be done in general because the type of the actual may
10571 -- depend on other actuals, and only be fully determined when
10572 -- the enclosing instance is analyzed.
10574 if Present (Etype (Actual))
10575 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10576 then
10577 Freeze_Before (Instantiation_Node, Etype (Actual));
10578 else
10579 Freeze_Before (Instantiation_Node, Typ);
10580 end if;
10582 -- If the actual is an aggregate, perform name resolution on
10583 -- its components (the analysis of an aggregate does not do it)
10584 -- to capture local names that may be hidden if the generic is
10585 -- a child unit.
10587 if Nkind (Actual) = N_Aggregate then
10588 Preanalyze_And_Resolve (Actual, Typ);
10589 end if;
10591 if Is_Limited_Type (Typ)
10592 and then not OK_For_Limited_Init (Typ, Actual)
10593 then
10594 Error_Msg_N
10595 ("initialization not allowed for limited types", Actual);
10596 Explain_Limited_Type (Typ, Actual);
10597 end if;
10598 end;
10600 elsif Present (Default_Expression (Formal)) then
10602 -- Use default to construct declaration
10604 if Present (Subt_Mark) then
10605 Def := New_Copy (Subt_Mark);
10606 else pragma Assert (Present (Acc_Def));
10607 Def := Copy_Access_Def;
10608 end if;
10610 Decl_Node :=
10611 Make_Object_Declaration (Sloc (Formal),
10612 Defining_Identifier => New_Copy (Gen_Obj),
10613 Constant_Present => True,
10614 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10615 Object_Definition => Def,
10616 Expression => New_Copy_Tree
10617 (Default_Expression (Formal)));
10619 Append (Decl_Node, List);
10620 Set_Analyzed (Expression (Decl_Node), False);
10622 else
10623 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10624 Error_Msg_NE ("\in instantiation of & declared#",
10625 Instantiation_Node, Scope (A_Gen_Obj));
10627 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10629 -- Create dummy constant declaration so that instance can be
10630 -- analyzed, to minimize cascaded visibility errors.
10632 if Present (Subt_Mark) then
10633 Def := Subt_Mark;
10634 else pragma Assert (Present (Acc_Def));
10635 Def := Acc_Def;
10636 end if;
10638 Decl_Node :=
10639 Make_Object_Declaration (Loc,
10640 Defining_Identifier => New_Copy (Gen_Obj),
10641 Constant_Present => True,
10642 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10643 Object_Definition => New_Copy (Def),
10644 Expression =>
10645 Make_Attribute_Reference (Sloc (Gen_Obj),
10646 Attribute_Name => Name_First,
10647 Prefix => New_Copy (Def)));
10649 Append (Decl_Node, List);
10651 else
10652 Abandon_Instantiation (Instantiation_Node);
10653 end if;
10654 end if;
10655 end if;
10657 if Nkind (Actual) in N_Has_Entity then
10658 Actual_Decl := Parent (Entity (Actual));
10659 end if;
10661 -- Ada 2005 (AI-423): For a formal object declaration with a null
10662 -- exclusion or an access definition that has a null exclusion: If the
10663 -- actual matching the formal object declaration denotes a generic
10664 -- formal object of another generic unit G, and the instantiation
10665 -- containing the actual occurs within the body of G or within the body
10666 -- of a generic unit declared within the declarative region of G, then
10667 -- the declaration of the formal object of G must have a null exclusion.
10668 -- Otherwise, the subtype of the actual matching the formal object
10669 -- declaration shall exclude null.
10671 if Ada_Version >= Ada_2005
10672 and then Present (Actual_Decl)
10673 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10674 N_Object_Declaration)
10675 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10676 and then not Has_Null_Exclusion (Actual_Decl)
10677 and then Has_Null_Exclusion (Analyzed_Formal)
10678 then
10679 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10680 Error_Msg_N
10681 ("actual must exclude null to match generic formal#", Actual);
10682 end if;
10684 -- An effectively volatile object cannot be used as an actual in a
10685 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10686 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10687 -- legality rule.
10689 if SPARK_Mode = On
10690 and then Present (Actual)
10691 and then Is_Effectively_Volatile_Object (Actual)
10692 then
10693 Error_Msg_N
10694 ("volatile object cannot act as actual in generic instantiation",
10695 Actual);
10696 end if;
10698 return List;
10699 end Instantiate_Object;
10701 ------------------------------
10702 -- Instantiate_Package_Body --
10703 ------------------------------
10705 procedure Instantiate_Package_Body
10706 (Body_Info : Pending_Body_Info;
10707 Inlined_Body : Boolean := False;
10708 Body_Optional : Boolean := False)
10710 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10711 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10712 Loc : constant Source_Ptr := Sloc (Inst_Node);
10714 Gen_Id : constant Node_Id := Name (Inst_Node);
10715 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10716 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10717 Act_Spec : constant Node_Id := Specification (Act_Decl);
10718 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10720 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10721 Save_Style_Check : constant Boolean := Style_Check;
10723 Act_Body : Node_Id;
10724 Act_Body_Id : Entity_Id;
10725 Act_Body_Name : Node_Id;
10726 Gen_Body : Node_Id;
10727 Gen_Body_Id : Node_Id;
10728 Par_Ent : Entity_Id := Empty;
10729 Par_Vis : Boolean := False;
10731 Parent_Installed : Boolean := False;
10733 Vis_Prims_List : Elist_Id := No_Elist;
10734 -- List of primitives made temporarily visible in the instantiation
10735 -- to match the visibility of the formal type
10737 procedure Check_Initialized_Types;
10738 -- In a generic package body, an entity of a generic private type may
10739 -- appear uninitialized. This is suspicious, unless the actual is a
10740 -- fully initialized type.
10742 -----------------------------
10743 -- Check_Initialized_Types --
10744 -----------------------------
10746 procedure Check_Initialized_Types is
10747 Decl : Node_Id;
10748 Formal : Entity_Id;
10749 Actual : Entity_Id;
10750 Uninit_Var : Entity_Id;
10752 begin
10753 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10754 while Present (Decl) loop
10755 Uninit_Var := Empty;
10757 if Nkind (Decl) = N_Private_Extension_Declaration then
10758 Uninit_Var := Uninitialized_Variable (Decl);
10760 elsif Nkind (Decl) = N_Formal_Type_Declaration
10761 and then Nkind (Formal_Type_Definition (Decl)) =
10762 N_Formal_Private_Type_Definition
10763 then
10764 Uninit_Var :=
10765 Uninitialized_Variable (Formal_Type_Definition (Decl));
10766 end if;
10768 if Present (Uninit_Var) then
10769 Formal := Defining_Identifier (Decl);
10770 Actual := First_Entity (Act_Decl_Id);
10772 -- For each formal there is a subtype declaration that renames
10773 -- the actual and has the same name as the formal. Locate the
10774 -- formal for warning message about uninitialized variables
10775 -- in the generic, for which the actual type should be a fully
10776 -- initialized type.
10778 while Present (Actual) loop
10779 exit when Ekind (Actual) = E_Package
10780 and then Present (Renamed_Object (Actual));
10782 if Chars (Actual) = Chars (Formal)
10783 and then not Is_Scalar_Type (Actual)
10784 and then not Is_Fully_Initialized_Type (Actual)
10785 and then Warn_On_No_Value_Assigned
10786 then
10787 Error_Msg_Node_2 := Formal;
10788 Error_Msg_NE
10789 ("generic unit has uninitialized variable& of "
10790 & "formal private type &?v?", Actual, Uninit_Var);
10791 Error_Msg_NE
10792 ("actual type for& should be fully initialized type?v?",
10793 Actual, Formal);
10794 exit;
10795 end if;
10797 Next_Entity (Actual);
10798 end loop;
10799 end if;
10801 Next (Decl);
10802 end loop;
10803 end Check_Initialized_Types;
10805 -- Start of processing for Instantiate_Package_Body
10807 begin
10808 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10810 -- The instance body may already have been processed, as the parent of
10811 -- another instance that is inlined (Load_Parent_Of_Generic).
10813 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10814 return;
10815 end if;
10817 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10819 -- Re-establish the state of information on which checks are suppressed.
10820 -- This information was set in Body_Info at the point of instantiation,
10821 -- and now we restore it so that the instance is compiled using the
10822 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10824 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10825 Scope_Suppress := Body_Info.Scope_Suppress;
10826 Opt.Ada_Version := Body_Info.Version;
10827 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10828 Restore_Warnings (Body_Info.Warnings);
10829 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10830 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10832 if No (Gen_Body_Id) then
10834 -- Do not look for parent of generic body if none is required.
10835 -- This may happen when the routine is called as part of the
10836 -- Pending_Instantiations processing, when nested instances
10837 -- may precede the one generated from the main unit.
10839 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10840 and then Body_Optional
10841 then
10842 return;
10843 else
10844 Load_Parent_Of_Generic
10845 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10846 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10847 end if;
10848 end if;
10850 -- Establish global variable for sloc adjustment and for error recovery
10851 -- In the case of an instance body for an instantiation with actuals
10852 -- from a limited view, the instance body is placed at the beginning
10853 -- of the enclosing package body: use the body entity as the source
10854 -- location for nodes of the instance body.
10856 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10857 declare
10858 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10859 Body_Id : constant Node_Id :=
10860 Corresponding_Body (Unit_Declaration_Node (Scop));
10862 begin
10863 Instantiation_Node := Body_Id;
10864 end;
10865 else
10866 Instantiation_Node := Inst_Node;
10867 end if;
10869 if Present (Gen_Body_Id) then
10870 Save_Env (Gen_Unit, Act_Decl_Id);
10871 Style_Check := False;
10873 -- If the context of the instance is subject to SPARK_Mode "off" or
10874 -- the annotation is altogether missing, set the global flag which
10875 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10876 -- the instance.
10878 if SPARK_Mode /= On then
10879 Ignore_Pragma_SPARK_Mode := True;
10880 end if;
10882 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10883 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10885 Create_Instantiation_Source
10886 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10888 Act_Body :=
10889 Copy_Generic_Node
10890 (Original_Node (Gen_Body), Empty, Instantiating => True);
10892 -- Create proper (possibly qualified) defining name for the body, to
10893 -- correspond to the one in the spec.
10895 Act_Body_Id :=
10896 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
10897 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
10899 -- Some attributes of spec entity are not inherited by body entity
10901 Set_Handler_Records (Act_Body_Id, No_List);
10903 if Nkind (Defining_Unit_Name (Act_Spec)) =
10904 N_Defining_Program_Unit_Name
10905 then
10906 Act_Body_Name :=
10907 Make_Defining_Program_Unit_Name (Loc,
10908 Name =>
10909 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10910 Defining_Identifier => Act_Body_Id);
10911 else
10912 Act_Body_Name := Act_Body_Id;
10913 end if;
10915 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10917 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10918 Check_Generic_Actuals (Act_Decl_Id, False);
10919 Check_Initialized_Types;
10921 -- Install primitives hidden at the point of the instantiation but
10922 -- visible when processing the generic formals
10924 declare
10925 E : Entity_Id;
10927 begin
10928 E := First_Entity (Act_Decl_Id);
10929 while Present (E) loop
10930 if Is_Type (E)
10931 and then Is_Generic_Actual_Type (E)
10932 and then Is_Tagged_Type (E)
10933 then
10934 Install_Hidden_Primitives
10935 (Prims_List => Vis_Prims_List,
10936 Gen_T => Generic_Parent_Type (Parent (E)),
10937 Act_T => E);
10938 end if;
10940 Next_Entity (E);
10941 end loop;
10942 end;
10944 -- If it is a child unit, make the parent instance (which is an
10945 -- instance of the parent of the generic) visible. The parent
10946 -- instance is the prefix of the name of the generic unit.
10948 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10949 and then Nkind (Gen_Id) = N_Expanded_Name
10950 then
10951 Par_Ent := Entity (Prefix (Gen_Id));
10952 Par_Vis := Is_Immediately_Visible (Par_Ent);
10953 Install_Parent (Par_Ent, In_Body => True);
10954 Parent_Installed := True;
10956 elsif Is_Child_Unit (Gen_Unit) then
10957 Par_Ent := Scope (Gen_Unit);
10958 Par_Vis := Is_Immediately_Visible (Par_Ent);
10959 Install_Parent (Par_Ent, In_Body => True);
10960 Parent_Installed := True;
10961 end if;
10963 -- If the instantiation is a library unit, and this is the main unit,
10964 -- then build the resulting compilation unit nodes for the instance.
10965 -- If this is a compilation unit but it is not the main unit, then it
10966 -- is the body of a unit in the context, that is being compiled
10967 -- because it is encloses some inlined unit or another generic unit
10968 -- being instantiated. In that case, this body is not part of the
10969 -- current compilation, and is not attached to the tree, but its
10970 -- parent must be set for analysis.
10972 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10974 -- Replace instance node with body of instance, and create new
10975 -- node for corresponding instance declaration.
10977 Build_Instance_Compilation_Unit_Nodes
10978 (Inst_Node, Act_Body, Act_Decl);
10979 Analyze (Inst_Node);
10981 if Parent (Inst_Node) = Cunit (Main_Unit) then
10983 -- If the instance is a child unit itself, then set the scope
10984 -- of the expanded body to be the parent of the instantiation
10985 -- (ensuring that the fully qualified name will be generated
10986 -- for the elaboration subprogram).
10988 if Nkind (Defining_Unit_Name (Act_Spec)) =
10989 N_Defining_Program_Unit_Name
10990 then
10991 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10992 end if;
10993 end if;
10995 -- Case where instantiation is not a library unit
10997 else
10998 -- If this is an early instantiation, i.e. appears textually
10999 -- before the corresponding body and must be elaborated first,
11000 -- indicate that the body instance is to be delayed.
11002 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11004 -- Now analyze the body. We turn off all checks if this is an
11005 -- internal unit, since there is no reason to have checks on for
11006 -- any predefined run-time library code. All such code is designed
11007 -- to be compiled with checks off.
11009 -- Note that we do NOT apply this criterion to children of GNAT
11010 -- The latter units must suppress checks explicitly if needed.
11012 -- We also do not suppress checks in CodePeer mode where we are
11013 -- interested in finding possible runtime errors.
11015 if not CodePeer_Mode
11016 and then Is_Predefined_File_Name
11017 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11018 then
11019 Analyze (Act_Body, Suppress => All_Checks);
11020 else
11021 Analyze (Act_Body);
11022 end if;
11023 end if;
11025 Inherit_Context (Gen_Body, Inst_Node);
11027 -- Remove the parent instances if they have been placed on the scope
11028 -- stack to compile the body.
11030 if Parent_Installed then
11031 Remove_Parent (In_Body => True);
11033 -- Restore the previous visibility of the parent
11035 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11036 end if;
11038 Restore_Hidden_Primitives (Vis_Prims_List);
11039 Restore_Private_Views (Act_Decl_Id);
11041 -- Remove the current unit from visibility if this is an instance
11042 -- that is not elaborated on the fly for inlining purposes.
11044 if not Inlined_Body then
11045 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11046 end if;
11048 Restore_Env;
11049 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11050 Style_Check := Save_Style_Check;
11052 -- If we have no body, and the unit requires a body, then complain. This
11053 -- complaint is suppressed if we have detected other errors (since a
11054 -- common reason for missing the body is that it had errors).
11055 -- In CodePeer mode, a warning has been emitted already, no need for
11056 -- further messages.
11058 elsif Unit_Requires_Body (Gen_Unit)
11059 and then not Body_Optional
11060 then
11061 if CodePeer_Mode then
11062 null;
11064 elsif Serious_Errors_Detected = 0 then
11065 Error_Msg_NE
11066 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11068 -- Don't attempt to perform any cleanup actions if some other error
11069 -- was already detected, since this can cause blowups.
11071 else
11072 return;
11073 end if;
11075 -- Case of package that does not need a body
11077 else
11078 -- If the instantiation of the declaration is a library unit, rewrite
11079 -- the original package instantiation as a package declaration in the
11080 -- compilation unit node.
11082 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11083 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11084 Rewrite (Inst_Node, Act_Decl);
11086 -- Generate elaboration entity, in case spec has elaboration code.
11087 -- This cannot be done when the instance is analyzed, because it
11088 -- is not known yet whether the body exists.
11090 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11091 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11093 -- If the instantiation is not a library unit, then append the
11094 -- declaration to the list of implicitly generated entities, unless
11095 -- it is already a list member which means that it was already
11096 -- processed
11098 elsif not Is_List_Member (Act_Decl) then
11099 Mark_Rewrite_Insertion (Act_Decl);
11100 Insert_Before (Inst_Node, Act_Decl);
11101 end if;
11102 end if;
11104 Expander_Mode_Restore;
11105 end Instantiate_Package_Body;
11107 ---------------------------------
11108 -- Instantiate_Subprogram_Body --
11109 ---------------------------------
11111 procedure Instantiate_Subprogram_Body
11112 (Body_Info : Pending_Body_Info;
11113 Body_Optional : Boolean := False)
11115 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11116 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11117 Loc : constant Source_Ptr := Sloc (Inst_Node);
11118 Gen_Id : constant Node_Id := Name (Inst_Node);
11119 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11120 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11121 Act_Decl_Id : constant Entity_Id :=
11122 Defining_Unit_Name (Specification (Act_Decl));
11123 Pack_Id : constant Entity_Id :=
11124 Defining_Unit_Name (Parent (Act_Decl));
11126 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11127 Saved_Style_Check : constant Boolean := Style_Check;
11128 Saved_Warnings : constant Warning_Record := Save_Warnings;
11130 Act_Body : Node_Id;
11131 Act_Body_Id : Entity_Id;
11132 Gen_Body : Node_Id;
11133 Gen_Body_Id : Node_Id;
11134 Pack_Body : Node_Id;
11135 Par_Ent : Entity_Id := Empty;
11136 Par_Vis : Boolean := False;
11137 Ret_Expr : Node_Id;
11139 Parent_Installed : Boolean := False;
11141 begin
11142 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11144 -- Subprogram body may have been created already because of an inline
11145 -- pragma, or because of multiple elaborations of the enclosing package
11146 -- when several instances of the subprogram appear in the main unit.
11148 if Present (Corresponding_Body (Act_Decl)) then
11149 return;
11150 end if;
11152 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11154 -- Re-establish the state of information on which checks are suppressed.
11155 -- This information was set in Body_Info at the point of instantiation,
11156 -- and now we restore it so that the instance is compiled using the
11157 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11159 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11160 Scope_Suppress := Body_Info.Scope_Suppress;
11161 Opt.Ada_Version := Body_Info.Version;
11162 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11163 Restore_Warnings (Body_Info.Warnings);
11164 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11165 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11167 if No (Gen_Body_Id) then
11169 -- For imported generic subprogram, no body to compile, complete
11170 -- the spec entity appropriately.
11172 if Is_Imported (Gen_Unit) then
11173 Set_Is_Imported (Act_Decl_Id);
11174 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11175 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11176 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11177 Set_Has_Completion (Act_Decl_Id);
11178 return;
11180 -- For other cases, compile the body
11182 else
11183 Load_Parent_Of_Generic
11184 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11185 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11186 end if;
11187 end if;
11189 Instantiation_Node := Inst_Node;
11191 if Present (Gen_Body_Id) then
11192 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11194 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11196 -- Either body is not present, or context is non-expanding, as
11197 -- when compiling a subunit. Mark the instance as completed, and
11198 -- diagnose a missing body when needed.
11200 if Expander_Active
11201 and then Operating_Mode = Generate_Code
11202 then
11203 Error_Msg_N
11204 ("missing proper body for instantiation", Gen_Body);
11205 end if;
11207 Set_Has_Completion (Act_Decl_Id);
11208 return;
11209 end if;
11211 Save_Env (Gen_Unit, Act_Decl_Id);
11212 Style_Check := False;
11214 -- If the context of the instance is subject to SPARK_Mode "off" or
11215 -- the annotation is altogether missing, set the global flag which
11216 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11217 -- the instance.
11219 if SPARK_Mode /= On then
11220 Ignore_Pragma_SPARK_Mode := True;
11221 end if;
11223 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11224 Create_Instantiation_Source
11225 (Inst_Node,
11226 Gen_Body_Id,
11227 False,
11228 S_Adjustment);
11230 Act_Body :=
11231 Copy_Generic_Node
11232 (Original_Node (Gen_Body), Empty, Instantiating => True);
11234 -- Create proper defining name for the body, to correspond to the one
11235 -- in the spec.
11237 Act_Body_Id :=
11238 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11240 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11241 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11243 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11244 Set_Has_Completion (Act_Decl_Id);
11245 Check_Generic_Actuals (Pack_Id, False);
11247 -- Generate a reference to link the visible subprogram instance to
11248 -- the generic body, which for navigation purposes is the only
11249 -- available source for the instance.
11251 Generate_Reference
11252 (Related_Instance (Pack_Id),
11253 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11255 -- If it is a child unit, make the parent instance (which is an
11256 -- instance of the parent of the generic) visible. The parent
11257 -- instance is the prefix of the name of the generic unit.
11259 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11260 and then Nkind (Gen_Id) = N_Expanded_Name
11261 then
11262 Par_Ent := Entity (Prefix (Gen_Id));
11263 Par_Vis := Is_Immediately_Visible (Par_Ent);
11264 Install_Parent (Par_Ent, In_Body => True);
11265 Parent_Installed := True;
11267 elsif Is_Child_Unit (Gen_Unit) then
11268 Par_Ent := Scope (Gen_Unit);
11269 Par_Vis := Is_Immediately_Visible (Par_Ent);
11270 Install_Parent (Par_Ent, In_Body => True);
11271 Parent_Installed := True;
11272 end if;
11274 -- Subprogram body is placed in the body of wrapper package,
11275 -- whose spec contains the subprogram declaration as well as
11276 -- the renaming declarations for the generic parameters.
11278 Pack_Body :=
11279 Make_Package_Body (Loc,
11280 Defining_Unit_Name => New_Copy (Pack_Id),
11281 Declarations => New_List (Act_Body));
11283 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11285 -- If the instantiation is a library unit, then build resulting
11286 -- compilation unit nodes for the instance. The declaration of
11287 -- the enclosing package is the grandparent of the subprogram
11288 -- declaration. First replace the instantiation node as the unit
11289 -- of the corresponding compilation.
11291 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11292 if Parent (Inst_Node) = Cunit (Main_Unit) then
11293 Set_Unit (Parent (Inst_Node), Inst_Node);
11294 Build_Instance_Compilation_Unit_Nodes
11295 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11296 Analyze (Inst_Node);
11297 else
11298 Set_Parent (Pack_Body, Parent (Inst_Node));
11299 Analyze (Pack_Body);
11300 end if;
11302 else
11303 Insert_Before (Inst_Node, Pack_Body);
11304 Mark_Rewrite_Insertion (Pack_Body);
11305 Analyze (Pack_Body);
11307 if Expander_Active then
11308 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11309 end if;
11310 end if;
11312 Inherit_Context (Gen_Body, Inst_Node);
11314 Restore_Private_Views (Pack_Id, False);
11316 if Parent_Installed then
11317 Remove_Parent (In_Body => True);
11319 -- Restore the previous visibility of the parent
11321 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11322 end if;
11324 Restore_Env;
11325 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11326 Style_Check := Saved_Style_Check;
11327 Restore_Warnings (Saved_Warnings);
11329 -- Body not found. Error was emitted already. If there were no previous
11330 -- errors, this may be an instance whose scope is a premature instance.
11331 -- In that case we must insure that the (legal) program does raise
11332 -- program error if executed. We generate a subprogram body for this
11333 -- purpose. See DEC ac30vso.
11335 -- Should not reference proprietary DEC tests in comments ???
11337 elsif Serious_Errors_Detected = 0
11338 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11339 then
11340 if Body_Optional then
11341 return;
11343 elsif Ekind (Act_Decl_Id) = E_Procedure then
11344 Act_Body :=
11345 Make_Subprogram_Body (Loc,
11346 Specification =>
11347 Make_Procedure_Specification (Loc,
11348 Defining_Unit_Name =>
11349 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11350 Parameter_Specifications =>
11351 New_Copy_List
11352 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11354 Declarations => Empty_List,
11355 Handled_Statement_Sequence =>
11356 Make_Handled_Sequence_Of_Statements (Loc,
11357 Statements =>
11358 New_List (
11359 Make_Raise_Program_Error (Loc,
11360 Reason =>
11361 PE_Access_Before_Elaboration))));
11363 else
11364 Ret_Expr :=
11365 Make_Raise_Program_Error (Loc,
11366 Reason => PE_Access_Before_Elaboration);
11368 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11369 Set_Analyzed (Ret_Expr);
11371 Act_Body :=
11372 Make_Subprogram_Body (Loc,
11373 Specification =>
11374 Make_Function_Specification (Loc,
11375 Defining_Unit_Name =>
11376 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11377 Parameter_Specifications =>
11378 New_Copy_List
11379 (Parameter_Specifications (Parent (Act_Decl_Id))),
11380 Result_Definition =>
11381 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11383 Declarations => Empty_List,
11384 Handled_Statement_Sequence =>
11385 Make_Handled_Sequence_Of_Statements (Loc,
11386 Statements =>
11387 New_List
11388 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11389 end if;
11391 Pack_Body :=
11392 Make_Package_Body (Loc,
11393 Defining_Unit_Name => New_Copy (Pack_Id),
11394 Declarations => New_List (Act_Body));
11396 Insert_After (Inst_Node, Pack_Body);
11397 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11398 Analyze (Pack_Body);
11399 end if;
11401 Expander_Mode_Restore;
11402 end Instantiate_Subprogram_Body;
11404 ----------------------
11405 -- Instantiate_Type --
11406 ----------------------
11408 function Instantiate_Type
11409 (Formal : Node_Id;
11410 Actual : Node_Id;
11411 Analyzed_Formal : Node_Id;
11412 Actual_Decls : List_Id) return List_Id
11414 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11415 A_Gen_T : constant Entity_Id :=
11416 Defining_Identifier (Analyzed_Formal);
11417 Ancestor : Entity_Id := Empty;
11418 Def : constant Node_Id := Formal_Type_Definition (Formal);
11419 Act_T : Entity_Id;
11420 Decl_Node : Node_Id;
11421 Decl_Nodes : List_Id;
11422 Loc : Source_Ptr;
11423 Subt : Entity_Id;
11425 procedure Diagnose_Predicated_Actual;
11426 -- There are a number of constructs in which a discrete type with
11427 -- predicates is illegal, e.g. as an index in an array type declaration.
11428 -- If a generic type is used is such a construct in a generic package
11429 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11430 -- of the generic contract that the actual cannot have predicates.
11432 procedure Validate_Array_Type_Instance;
11433 procedure Validate_Access_Subprogram_Instance;
11434 procedure Validate_Access_Type_Instance;
11435 procedure Validate_Derived_Type_Instance;
11436 procedure Validate_Derived_Interface_Type_Instance;
11437 procedure Validate_Discriminated_Formal_Type;
11438 procedure Validate_Interface_Type_Instance;
11439 procedure Validate_Private_Type_Instance;
11440 procedure Validate_Incomplete_Type_Instance;
11441 -- These procedures perform validation tests for the named case.
11442 -- Validate_Discriminated_Formal_Type is shared by formal private
11443 -- types and Ada 2012 formal incomplete types.
11445 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11446 -- Check that base types are the same and that the subtypes match
11447 -- statically. Used in several of the above.
11449 ---------------------------------
11450 -- Diagnose_Predicated_Actual --
11451 ---------------------------------
11453 procedure Diagnose_Predicated_Actual is
11454 begin
11455 if No_Predicate_On_Actual (A_Gen_T)
11456 and then Has_Predicates (Act_T)
11457 then
11458 Error_Msg_NE
11459 ("actual for& cannot be a type with predicate",
11460 Instantiation_Node, A_Gen_T);
11462 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11463 and then Has_Predicates (Act_T)
11464 and then not Has_Static_Predicate_Aspect (Act_T)
11465 then
11466 Error_Msg_NE
11467 ("actual for& cannot be a type with a dynamic predicate",
11468 Instantiation_Node, A_Gen_T);
11469 end if;
11470 end Diagnose_Predicated_Actual;
11472 --------------------
11473 -- Subtypes_Match --
11474 --------------------
11476 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11477 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11479 begin
11480 -- Some detailed comments would be useful here ???
11482 return ((Base_Type (T) = Act_T
11483 or else Base_Type (T) = Base_Type (Act_T))
11484 and then Subtypes_Statically_Match (T, Act_T))
11486 or else (Is_Class_Wide_Type (Gen_T)
11487 and then Is_Class_Wide_Type (Act_T)
11488 and then Subtypes_Match
11489 (Get_Instance_Of (Root_Type (Gen_T)),
11490 Root_Type (Act_T)))
11492 or else
11493 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11494 E_Anonymous_Access_Type)
11495 and then Ekind (Act_T) = Ekind (Gen_T)
11496 and then Subtypes_Statically_Match
11497 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11498 end Subtypes_Match;
11500 -----------------------------------------
11501 -- Validate_Access_Subprogram_Instance --
11502 -----------------------------------------
11504 procedure Validate_Access_Subprogram_Instance is
11505 begin
11506 if not Is_Access_Type (Act_T)
11507 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11508 then
11509 Error_Msg_NE
11510 ("expect access type in instantiation of &", Actual, Gen_T);
11511 Abandon_Instantiation (Actual);
11512 end if;
11514 -- According to AI05-288, actuals for access_to_subprograms must be
11515 -- subtype conformant with the generic formal. Previous to AI05-288
11516 -- only mode conformance was required.
11518 -- This is a binding interpretation that applies to previous versions
11519 -- of the language, no need to maintain previous weaker checks.
11521 Check_Subtype_Conformant
11522 (Designated_Type (Act_T),
11523 Designated_Type (A_Gen_T),
11524 Actual,
11525 Get_Inst => True);
11527 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11528 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11529 Error_Msg_NE
11530 ("protected access type not allowed for formal &",
11531 Actual, Gen_T);
11532 end if;
11534 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11535 Error_Msg_NE
11536 ("expect protected access type for formal &",
11537 Actual, Gen_T);
11538 end if;
11540 -- If the formal has a specified convention (which in most cases
11541 -- will be StdCall) verify that the actual has the same convention.
11543 if Has_Convention_Pragma (A_Gen_T)
11544 and then Convention (A_Gen_T) /= Convention (Act_T)
11545 then
11546 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11547 Error_Msg_NE
11548 ("actual for formal & must have convention %", Actual, Gen_T);
11549 end if;
11550 end Validate_Access_Subprogram_Instance;
11552 -----------------------------------
11553 -- Validate_Access_Type_Instance --
11554 -----------------------------------
11556 procedure Validate_Access_Type_Instance is
11557 Desig_Type : constant Entity_Id :=
11558 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11559 Desig_Act : Entity_Id;
11561 begin
11562 if not Is_Access_Type (Act_T) then
11563 Error_Msg_NE
11564 ("expect access type in instantiation of &", Actual, Gen_T);
11565 Abandon_Instantiation (Actual);
11566 end if;
11568 if Is_Access_Constant (A_Gen_T) then
11569 if not Is_Access_Constant (Act_T) then
11570 Error_Msg_N
11571 ("actual type must be access-to-constant type", Actual);
11572 Abandon_Instantiation (Actual);
11573 end if;
11574 else
11575 if Is_Access_Constant (Act_T) then
11576 Error_Msg_N
11577 ("actual type must be access-to-variable type", Actual);
11578 Abandon_Instantiation (Actual);
11580 elsif Ekind (A_Gen_T) = E_General_Access_Type
11581 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11582 then
11583 Error_Msg_N -- CODEFIX
11584 ("actual must be general access type!", Actual);
11585 Error_Msg_NE -- CODEFIX
11586 ("add ALL to }!", Actual, Act_T);
11587 Abandon_Instantiation (Actual);
11588 end if;
11589 end if;
11591 -- The designated subtypes, that is to say the subtypes introduced
11592 -- by an access type declaration (and not by a subtype declaration)
11593 -- must match.
11595 Desig_Act := Designated_Type (Base_Type (Act_T));
11597 -- The designated type may have been introduced through a limited_
11598 -- with clause, in which case retrieve the non-limited view. This
11599 -- applies to incomplete types as well as to class-wide types.
11601 if From_Limited_With (Desig_Act) then
11602 Desig_Act := Available_View (Desig_Act);
11603 end if;
11605 if not Subtypes_Match (Desig_Type, Desig_Act) then
11606 Error_Msg_NE
11607 ("designated type of actual does not match that of formal &",
11608 Actual, Gen_T);
11610 if not Predicates_Match (Desig_Type, Desig_Act) then
11611 Error_Msg_N ("\predicates do not match", Actual);
11612 end if;
11614 Abandon_Instantiation (Actual);
11616 elsif Is_Access_Type (Designated_Type (Act_T))
11617 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11619 Is_Constrained (Designated_Type (Desig_Type))
11620 then
11621 Error_Msg_NE
11622 ("designated type of actual does not match that of formal &",
11623 Actual, Gen_T);
11625 if not Predicates_Match (Desig_Type, Desig_Act) then
11626 Error_Msg_N ("\predicates do not match", Actual);
11627 end if;
11629 Abandon_Instantiation (Actual);
11630 end if;
11632 -- Ada 2005: null-exclusion indicators of the two types must agree
11634 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11635 Error_Msg_NE
11636 ("non null exclusion of actual and formal & do not match",
11637 Actual, Gen_T);
11638 end if;
11639 end Validate_Access_Type_Instance;
11641 ----------------------------------
11642 -- Validate_Array_Type_Instance --
11643 ----------------------------------
11645 procedure Validate_Array_Type_Instance is
11646 I1 : Node_Id;
11647 I2 : Node_Id;
11648 T2 : Entity_Id;
11650 function Formal_Dimensions return Nat;
11651 -- Count number of dimensions in array type formal
11653 -----------------------
11654 -- Formal_Dimensions --
11655 -----------------------
11657 function Formal_Dimensions return Nat is
11658 Num : Nat := 0;
11659 Index : Node_Id;
11661 begin
11662 if Nkind (Def) = N_Constrained_Array_Definition then
11663 Index := First (Discrete_Subtype_Definitions (Def));
11664 else
11665 Index := First (Subtype_Marks (Def));
11666 end if;
11668 while Present (Index) loop
11669 Num := Num + 1;
11670 Next_Index (Index);
11671 end loop;
11673 return Num;
11674 end Formal_Dimensions;
11676 -- Start of processing for Validate_Array_Type_Instance
11678 begin
11679 if not Is_Array_Type (Act_T) then
11680 Error_Msg_NE
11681 ("expect array type in instantiation of &", Actual, Gen_T);
11682 Abandon_Instantiation (Actual);
11684 elsif Nkind (Def) = N_Constrained_Array_Definition then
11685 if not (Is_Constrained (Act_T)) then
11686 Error_Msg_NE
11687 ("expect constrained array in instantiation of &",
11688 Actual, Gen_T);
11689 Abandon_Instantiation (Actual);
11690 end if;
11692 else
11693 if Is_Constrained (Act_T) then
11694 Error_Msg_NE
11695 ("expect unconstrained array in instantiation of &",
11696 Actual, Gen_T);
11697 Abandon_Instantiation (Actual);
11698 end if;
11699 end if;
11701 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11702 Error_Msg_NE
11703 ("dimensions of actual do not match formal &", Actual, Gen_T);
11704 Abandon_Instantiation (Actual);
11705 end if;
11707 I1 := First_Index (A_Gen_T);
11708 I2 := First_Index (Act_T);
11709 for J in 1 .. Formal_Dimensions loop
11711 -- If the indexes of the actual were given by a subtype_mark,
11712 -- the index was transformed into a range attribute. Retrieve
11713 -- the original type mark for checking.
11715 if Is_Entity_Name (Original_Node (I2)) then
11716 T2 := Entity (Original_Node (I2));
11717 else
11718 T2 := Etype (I2);
11719 end if;
11721 if not Subtypes_Match
11722 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11723 then
11724 Error_Msg_NE
11725 ("index types of actual do not match those of formal &",
11726 Actual, Gen_T);
11727 Abandon_Instantiation (Actual);
11728 end if;
11730 Next_Index (I1);
11731 Next_Index (I2);
11732 end loop;
11734 -- Check matching subtypes. Note that there are complex visibility
11735 -- issues when the generic is a child unit and some aspect of the
11736 -- generic type is declared in a parent unit of the generic. We do
11737 -- the test to handle this special case only after a direct check
11738 -- for static matching has failed. The case where both the component
11739 -- type and the array type are separate formals, and the component
11740 -- type is a private view may also require special checking in
11741 -- Subtypes_Match.
11743 if Subtypes_Match
11744 (Component_Type (A_Gen_T), Component_Type (Act_T))
11745 or else
11746 Subtypes_Match
11747 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11748 Component_Type (Act_T))
11749 then
11750 null;
11751 else
11752 Error_Msg_NE
11753 ("component subtype of actual does not match that of formal &",
11754 Actual, Gen_T);
11755 Abandon_Instantiation (Actual);
11756 end if;
11758 if Has_Aliased_Components (A_Gen_T)
11759 and then not Has_Aliased_Components (Act_T)
11760 then
11761 Error_Msg_NE
11762 ("actual must have aliased components to match formal type &",
11763 Actual, Gen_T);
11764 end if;
11765 end Validate_Array_Type_Instance;
11767 -----------------------------------------------
11768 -- Validate_Derived_Interface_Type_Instance --
11769 -----------------------------------------------
11771 procedure Validate_Derived_Interface_Type_Instance is
11772 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11773 Elmt : Elmt_Id;
11775 begin
11776 -- First apply interface instance checks
11778 Validate_Interface_Type_Instance;
11780 -- Verify that immediate parent interface is an ancestor of
11781 -- the actual.
11783 if Present (Par)
11784 and then not Interface_Present_In_Ancestor (Act_T, Par)
11785 then
11786 Error_Msg_NE
11787 ("interface actual must include progenitor&", Actual, Par);
11788 end if;
11790 -- Now verify that the actual includes all other ancestors of
11791 -- the formal.
11793 Elmt := First_Elmt (Interfaces (A_Gen_T));
11794 while Present (Elmt) loop
11795 if not Interface_Present_In_Ancestor
11796 (Act_T, Get_Instance_Of (Node (Elmt)))
11797 then
11798 Error_Msg_NE
11799 ("interface actual must include progenitor&",
11800 Actual, Node (Elmt));
11801 end if;
11803 Next_Elmt (Elmt);
11804 end loop;
11805 end Validate_Derived_Interface_Type_Instance;
11807 ------------------------------------
11808 -- Validate_Derived_Type_Instance --
11809 ------------------------------------
11811 procedure Validate_Derived_Type_Instance is
11812 Actual_Discr : Entity_Id;
11813 Ancestor_Discr : Entity_Id;
11815 begin
11816 -- If the parent type in the generic declaration is itself a previous
11817 -- formal type, then it is local to the generic and absent from the
11818 -- analyzed generic definition. In that case the ancestor is the
11819 -- instance of the formal (which must have been instantiated
11820 -- previously), unless the ancestor is itself a formal derived type.
11821 -- In this latter case (which is the subject of Corrigendum 8652/0038
11822 -- (AI-202) the ancestor of the formals is the ancestor of its
11823 -- parent. Otherwise, the analyzed generic carries the parent type.
11824 -- If the parent type is defined in a previous formal package, then
11825 -- the scope of that formal package is that of the generic type
11826 -- itself, and it has already been mapped into the corresponding type
11827 -- in the actual package.
11829 -- Common case: parent type defined outside of the generic
11831 if Is_Entity_Name (Subtype_Mark (Def))
11832 and then Present (Entity (Subtype_Mark (Def)))
11833 then
11834 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11836 -- Check whether parent is defined in a previous formal package
11838 elsif
11839 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11840 then
11841 Ancestor :=
11842 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11844 -- The type may be a local derivation, or a type extension of a
11845 -- previous formal, or of a formal of a parent package.
11847 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11848 or else
11849 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11850 then
11851 -- Check whether the parent is another derived formal type in the
11852 -- same generic unit.
11854 if Etype (A_Gen_T) /= A_Gen_T
11855 and then Is_Generic_Type (Etype (A_Gen_T))
11856 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11857 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11858 then
11859 -- Locate ancestor of parent from the subtype declaration
11860 -- created for the actual.
11862 declare
11863 Decl : Node_Id;
11865 begin
11866 Decl := First (Actual_Decls);
11867 while Present (Decl) loop
11868 if Nkind (Decl) = N_Subtype_Declaration
11869 and then Chars (Defining_Identifier (Decl)) =
11870 Chars (Etype (A_Gen_T))
11871 then
11872 Ancestor := Generic_Parent_Type (Decl);
11873 exit;
11874 else
11875 Next (Decl);
11876 end if;
11877 end loop;
11878 end;
11880 pragma Assert (Present (Ancestor));
11882 -- The ancestor itself may be a previous formal that has been
11883 -- instantiated.
11885 Ancestor := Get_Instance_Of (Ancestor);
11887 else
11888 Ancestor :=
11889 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11890 end if;
11892 -- Check whether parent is a previous formal of the current generic
11894 elsif Is_Derived_Type (A_Gen_T)
11895 and then Is_Generic_Type (Etype (A_Gen_T))
11896 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11897 then
11898 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11900 -- An unusual case: the actual is a type declared in a parent unit,
11901 -- but is not a formal type so there is no instance_of for it.
11902 -- Retrieve it by analyzing the record extension.
11904 elsif Is_Child_Unit (Scope (A_Gen_T))
11905 and then In_Open_Scopes (Scope (Act_T))
11906 and then Is_Generic_Instance (Scope (Act_T))
11907 then
11908 Analyze (Subtype_Mark (Def));
11909 Ancestor := Entity (Subtype_Mark (Def));
11911 else
11912 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11913 end if;
11915 -- If the formal derived type has pragma Preelaborable_Initialization
11916 -- then the actual type must have preelaborable initialization.
11918 if Known_To_Have_Preelab_Init (A_Gen_T)
11919 and then not Has_Preelaborable_Initialization (Act_T)
11920 then
11921 Error_Msg_NE
11922 ("actual for & must have preelaborable initialization",
11923 Actual, Gen_T);
11924 end if;
11926 -- Ada 2005 (AI-251)
11928 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11929 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11930 Error_Msg_NE
11931 ("(Ada 2005) expected type implementing & in instantiation",
11932 Actual, Ancestor);
11933 end if;
11935 -- Finally verify that the (instance of) the ancestor is an ancestor
11936 -- of the actual.
11938 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11939 Error_Msg_NE
11940 ("expect type derived from & in instantiation",
11941 Actual, First_Subtype (Ancestor));
11942 Abandon_Instantiation (Actual);
11943 end if;
11945 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11946 -- that the formal type declaration has been rewritten as a private
11947 -- extension.
11949 if Ada_Version >= Ada_2005
11950 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11951 and then Synchronized_Present (Parent (A_Gen_T))
11952 then
11953 -- The actual must be a synchronized tagged type
11955 if not Is_Tagged_Type (Act_T) then
11956 Error_Msg_N
11957 ("actual of synchronized type must be tagged", Actual);
11958 Abandon_Instantiation (Actual);
11960 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11961 and then Nkind (Type_Definition (Parent (Act_T))) =
11962 N_Derived_Type_Definition
11963 and then not Synchronized_Present
11964 (Type_Definition (Parent (Act_T)))
11965 then
11966 Error_Msg_N
11967 ("actual of synchronized type must be synchronized", Actual);
11968 Abandon_Instantiation (Actual);
11969 end if;
11970 end if;
11972 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11973 -- removes the second instance of the phrase "or allow pass by copy".
11975 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11976 Error_Msg_N
11977 ("cannot have atomic actual type for non-atomic formal type",
11978 Actual);
11980 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11981 Error_Msg_N
11982 ("cannot have volatile actual type for non-volatile formal type",
11983 Actual);
11984 end if;
11986 -- It should not be necessary to check for unknown discriminants on
11987 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11988 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11989 -- needs fixing. ???
11991 if Is_Definite_Subtype (A_Gen_T)
11992 and then not Unknown_Discriminants_Present (Formal)
11993 and then not Is_Definite_Subtype (Act_T)
11994 then
11995 Error_Msg_N ("actual subtype must be constrained", Actual);
11996 Abandon_Instantiation (Actual);
11997 end if;
11999 if not Unknown_Discriminants_Present (Formal) then
12000 if Is_Constrained (Ancestor) then
12001 if not Is_Constrained (Act_T) then
12002 Error_Msg_N ("actual subtype must be constrained", Actual);
12003 Abandon_Instantiation (Actual);
12004 end if;
12006 -- Ancestor is unconstrained, Check if generic formal and actual
12007 -- agree on constrainedness. The check only applies to array types
12008 -- and discriminated types.
12010 elsif Is_Constrained (Act_T) then
12011 if Ekind (Ancestor) = E_Access_Type
12012 or else (not Is_Constrained (A_Gen_T)
12013 and then Is_Composite_Type (A_Gen_T))
12014 then
12015 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12016 Abandon_Instantiation (Actual);
12017 end if;
12019 -- A class-wide type is only allowed if the formal has unknown
12020 -- discriminants.
12022 elsif Is_Class_Wide_Type (Act_T)
12023 and then not Has_Unknown_Discriminants (Ancestor)
12024 then
12025 Error_Msg_NE
12026 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12027 Abandon_Instantiation (Actual);
12029 -- Otherwise, the formal and actual must have the same number
12030 -- of discriminants and each discriminant of the actual must
12031 -- correspond to a discriminant of the formal.
12033 elsif Has_Discriminants (Act_T)
12034 and then not Has_Unknown_Discriminants (Act_T)
12035 and then Has_Discriminants (Ancestor)
12036 then
12037 Actual_Discr := First_Discriminant (Act_T);
12038 Ancestor_Discr := First_Discriminant (Ancestor);
12039 while Present (Actual_Discr)
12040 and then Present (Ancestor_Discr)
12041 loop
12042 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12043 No (Corresponding_Discriminant (Actual_Discr))
12044 then
12045 Error_Msg_NE
12046 ("discriminant & does not correspond "
12047 & "to ancestor discriminant", Actual, Actual_Discr);
12048 Abandon_Instantiation (Actual);
12049 end if;
12051 Next_Discriminant (Actual_Discr);
12052 Next_Discriminant (Ancestor_Discr);
12053 end loop;
12055 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12056 Error_Msg_NE
12057 ("actual for & must have same number of discriminants",
12058 Actual, Gen_T);
12059 Abandon_Instantiation (Actual);
12060 end if;
12062 -- This case should be caught by the earlier check for
12063 -- constrainedness, but the check here is added for completeness.
12065 elsif Has_Discriminants (Act_T)
12066 and then not Has_Unknown_Discriminants (Act_T)
12067 then
12068 Error_Msg_NE
12069 ("actual for & must not have discriminants", Actual, Gen_T);
12070 Abandon_Instantiation (Actual);
12072 elsif Has_Discriminants (Ancestor) then
12073 Error_Msg_NE
12074 ("actual for & must have known discriminants", Actual, Gen_T);
12075 Abandon_Instantiation (Actual);
12076 end if;
12078 if not Subtypes_Statically_Compatible
12079 (Act_T, Ancestor, Formal_Derived_Matching => True)
12080 then
12081 Error_Msg_N
12082 ("constraint on actual is incompatible with formal", Actual);
12083 Abandon_Instantiation (Actual);
12084 end if;
12085 end if;
12087 -- If the formal and actual types are abstract, check that there
12088 -- are no abstract primitives of the actual type that correspond to
12089 -- nonabstract primitives of the formal type (second sentence of
12090 -- RM95 3.9.3(9)).
12092 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12093 Check_Abstract_Primitives : declare
12094 Gen_Prims : constant Elist_Id :=
12095 Primitive_Operations (A_Gen_T);
12096 Gen_Elmt : Elmt_Id;
12097 Gen_Subp : Entity_Id;
12098 Anc_Subp : Entity_Id;
12099 Anc_Formal : Entity_Id;
12100 Anc_F_Type : Entity_Id;
12102 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12103 Act_Elmt : Elmt_Id;
12104 Act_Subp : Entity_Id;
12105 Act_Formal : Entity_Id;
12106 Act_F_Type : Entity_Id;
12108 Subprograms_Correspond : Boolean;
12110 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12111 -- Returns true if T2 is derived directly or indirectly from
12112 -- T1, including derivations from interfaces. T1 and T2 are
12113 -- required to be specific tagged base types.
12115 ------------------------
12116 -- Is_Tagged_Ancestor --
12117 ------------------------
12119 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12121 Intfc_Elmt : Elmt_Id;
12123 begin
12124 -- The predicate is satisfied if the types are the same
12126 if T1 = T2 then
12127 return True;
12129 -- If we've reached the top of the derivation chain then
12130 -- we know that T1 is not an ancestor of T2.
12132 elsif Etype (T2) = T2 then
12133 return False;
12135 -- Proceed to check T2's immediate parent
12137 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12138 return True;
12140 -- Finally, check to see if T1 is an ancestor of any of T2's
12141 -- progenitors.
12143 else
12144 Intfc_Elmt := First_Elmt (Interfaces (T2));
12145 while Present (Intfc_Elmt) loop
12146 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12147 return True;
12148 end if;
12150 Next_Elmt (Intfc_Elmt);
12151 end loop;
12152 end if;
12154 return False;
12155 end Is_Tagged_Ancestor;
12157 -- Start of processing for Check_Abstract_Primitives
12159 begin
12160 -- Loop over all of the formal derived type's primitives
12162 Gen_Elmt := First_Elmt (Gen_Prims);
12163 while Present (Gen_Elmt) loop
12164 Gen_Subp := Node (Gen_Elmt);
12166 -- If the primitive of the formal is not abstract, then
12167 -- determine whether there is a corresponding primitive of
12168 -- the actual type that's abstract.
12170 if not Is_Abstract_Subprogram (Gen_Subp) then
12171 Act_Elmt := First_Elmt (Act_Prims);
12172 while Present (Act_Elmt) loop
12173 Act_Subp := Node (Act_Elmt);
12175 -- If we find an abstract primitive of the actual,
12176 -- then we need to test whether it corresponds to the
12177 -- subprogram from which the generic formal primitive
12178 -- is inherited.
12180 if Is_Abstract_Subprogram (Act_Subp) then
12181 Anc_Subp := Alias (Gen_Subp);
12183 -- Test whether we have a corresponding primitive
12184 -- by comparing names, kinds, formal types, and
12185 -- result types.
12187 if Chars (Anc_Subp) = Chars (Act_Subp)
12188 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12189 then
12190 Anc_Formal := First_Formal (Anc_Subp);
12191 Act_Formal := First_Formal (Act_Subp);
12192 while Present (Anc_Formal)
12193 and then Present (Act_Formal)
12194 loop
12195 Anc_F_Type := Etype (Anc_Formal);
12196 Act_F_Type := Etype (Act_Formal);
12198 if Ekind (Anc_F_Type) =
12199 E_Anonymous_Access_Type
12200 then
12201 Anc_F_Type := Designated_Type (Anc_F_Type);
12203 if Ekind (Act_F_Type) =
12204 E_Anonymous_Access_Type
12205 then
12206 Act_F_Type :=
12207 Designated_Type (Act_F_Type);
12208 else
12209 exit;
12210 end if;
12212 elsif
12213 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12214 then
12215 exit;
12216 end if;
12218 Anc_F_Type := Base_Type (Anc_F_Type);
12219 Act_F_Type := Base_Type (Act_F_Type);
12221 -- If the formal is controlling, then the
12222 -- the type of the actual primitive's formal
12223 -- must be derived directly or indirectly
12224 -- from the type of the ancestor primitive's
12225 -- formal.
12227 if Is_Controlling_Formal (Anc_Formal) then
12228 if not Is_Tagged_Ancestor
12229 (Anc_F_Type, Act_F_Type)
12230 then
12231 exit;
12232 end if;
12234 -- Otherwise the types of the formals must
12235 -- be the same.
12237 elsif Anc_F_Type /= Act_F_Type then
12238 exit;
12239 end if;
12241 Next_Entity (Anc_Formal);
12242 Next_Entity (Act_Formal);
12243 end loop;
12245 -- If we traversed through all of the formals
12246 -- then so far the subprograms correspond, so
12247 -- now check that any result types correspond.
12249 if No (Anc_Formal) and then No (Act_Formal) then
12250 Subprograms_Correspond := True;
12252 if Ekind (Act_Subp) = E_Function then
12253 Anc_F_Type := Etype (Anc_Subp);
12254 Act_F_Type := Etype (Act_Subp);
12256 if Ekind (Anc_F_Type) =
12257 E_Anonymous_Access_Type
12258 then
12259 Anc_F_Type :=
12260 Designated_Type (Anc_F_Type);
12262 if Ekind (Act_F_Type) =
12263 E_Anonymous_Access_Type
12264 then
12265 Act_F_Type :=
12266 Designated_Type (Act_F_Type);
12267 else
12268 Subprograms_Correspond := False;
12269 end if;
12271 elsif
12272 Ekind (Act_F_Type)
12273 = E_Anonymous_Access_Type
12274 then
12275 Subprograms_Correspond := False;
12276 end if;
12278 Anc_F_Type := Base_Type (Anc_F_Type);
12279 Act_F_Type := Base_Type (Act_F_Type);
12281 -- Now either the result types must be
12282 -- the same or, if the result type is
12283 -- controlling, the result type of the
12284 -- actual primitive must descend from the
12285 -- result type of the ancestor primitive.
12287 if Subprograms_Correspond
12288 and then Anc_F_Type /= Act_F_Type
12289 and then
12290 Has_Controlling_Result (Anc_Subp)
12291 and then not Is_Tagged_Ancestor
12292 (Anc_F_Type, Act_F_Type)
12293 then
12294 Subprograms_Correspond := False;
12295 end if;
12296 end if;
12298 -- Found a matching subprogram belonging to
12299 -- formal ancestor type, so actual subprogram
12300 -- corresponds and this violates 3.9.3(9).
12302 if Subprograms_Correspond then
12303 Error_Msg_NE
12304 ("abstract subprogram & overrides "
12305 & "nonabstract subprogram of ancestor",
12306 Actual, Act_Subp);
12307 end if;
12308 end if;
12309 end if;
12310 end if;
12312 Next_Elmt (Act_Elmt);
12313 end loop;
12314 end if;
12316 Next_Elmt (Gen_Elmt);
12317 end loop;
12318 end Check_Abstract_Primitives;
12319 end if;
12321 -- Verify that limitedness matches. If parent is a limited
12322 -- interface then the generic formal is not unless declared
12323 -- explicitly so. If not declared limited, the actual cannot be
12324 -- limited (see AI05-0087).
12326 -- Even though this AI is a binding interpretation, we enable the
12327 -- check only in Ada 2012 mode, because this improper construct
12328 -- shows up in user code and in existing B-tests.
12330 if Is_Limited_Type (Act_T)
12331 and then not Is_Limited_Type (A_Gen_T)
12332 and then Ada_Version >= Ada_2012
12333 then
12334 if In_Instance then
12335 null;
12336 else
12337 Error_Msg_NE
12338 ("actual for non-limited & cannot be a limited type",
12339 Actual, Gen_T);
12340 Explain_Limited_Type (Act_T, Actual);
12341 Abandon_Instantiation (Actual);
12342 end if;
12343 end if;
12344 end Validate_Derived_Type_Instance;
12346 ----------------------------------------
12347 -- Validate_Discriminated_Formal_Type --
12348 ----------------------------------------
12350 procedure Validate_Discriminated_Formal_Type is
12351 Formal_Discr : Entity_Id;
12352 Actual_Discr : Entity_Id;
12353 Formal_Subt : Entity_Id;
12355 begin
12356 if Has_Discriminants (A_Gen_T) then
12357 if not Has_Discriminants (Act_T) then
12358 Error_Msg_NE
12359 ("actual for & must have discriminants", Actual, Gen_T);
12360 Abandon_Instantiation (Actual);
12362 elsif Is_Constrained (Act_T) then
12363 Error_Msg_NE
12364 ("actual for & must be unconstrained", Actual, Gen_T);
12365 Abandon_Instantiation (Actual);
12367 else
12368 Formal_Discr := First_Discriminant (A_Gen_T);
12369 Actual_Discr := First_Discriminant (Act_T);
12370 while Formal_Discr /= Empty loop
12371 if Actual_Discr = Empty then
12372 Error_Msg_NE
12373 ("discriminants on actual do not match formal",
12374 Actual, Gen_T);
12375 Abandon_Instantiation (Actual);
12376 end if;
12378 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12380 -- Access discriminants match if designated types do
12382 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12383 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12384 E_Anonymous_Access_Type
12385 and then
12386 Get_Instance_Of
12387 (Designated_Type (Base_Type (Formal_Subt))) =
12388 Designated_Type (Base_Type (Etype (Actual_Discr)))
12389 then
12390 null;
12392 elsif Base_Type (Formal_Subt) /=
12393 Base_Type (Etype (Actual_Discr))
12394 then
12395 Error_Msg_NE
12396 ("types of actual discriminants must match formal",
12397 Actual, Gen_T);
12398 Abandon_Instantiation (Actual);
12400 elsif not Subtypes_Statically_Match
12401 (Formal_Subt, Etype (Actual_Discr))
12402 and then Ada_Version >= Ada_95
12403 then
12404 Error_Msg_NE
12405 ("subtypes of actual discriminants must match formal",
12406 Actual, Gen_T);
12407 Abandon_Instantiation (Actual);
12408 end if;
12410 Next_Discriminant (Formal_Discr);
12411 Next_Discriminant (Actual_Discr);
12412 end loop;
12414 if Actual_Discr /= Empty then
12415 Error_Msg_NE
12416 ("discriminants on actual do not match formal",
12417 Actual, Gen_T);
12418 Abandon_Instantiation (Actual);
12419 end if;
12420 end if;
12421 end if;
12422 end Validate_Discriminated_Formal_Type;
12424 ---------------------------------------
12425 -- Validate_Incomplete_Type_Instance --
12426 ---------------------------------------
12428 procedure Validate_Incomplete_Type_Instance is
12429 begin
12430 if not Is_Tagged_Type (Act_T)
12431 and then Is_Tagged_Type (A_Gen_T)
12432 then
12433 Error_Msg_NE
12434 ("actual for & must be a tagged type", Actual, Gen_T);
12435 end if;
12437 Validate_Discriminated_Formal_Type;
12438 end Validate_Incomplete_Type_Instance;
12440 --------------------------------------
12441 -- Validate_Interface_Type_Instance --
12442 --------------------------------------
12444 procedure Validate_Interface_Type_Instance is
12445 begin
12446 if not Is_Interface (Act_T) then
12447 Error_Msg_NE
12448 ("actual for formal interface type must be an interface",
12449 Actual, Gen_T);
12451 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12452 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12453 or else Is_Protected_Interface (A_Gen_T) /=
12454 Is_Protected_Interface (Act_T)
12455 or else Is_Synchronized_Interface (A_Gen_T) /=
12456 Is_Synchronized_Interface (Act_T)
12457 then
12458 Error_Msg_NE
12459 ("actual for interface& does not match (RM 12.5.5(4))",
12460 Actual, Gen_T);
12461 end if;
12462 end Validate_Interface_Type_Instance;
12464 ------------------------------------
12465 -- Validate_Private_Type_Instance --
12466 ------------------------------------
12468 procedure Validate_Private_Type_Instance is
12469 begin
12470 if Is_Limited_Type (Act_T)
12471 and then not Is_Limited_Type (A_Gen_T)
12472 then
12473 if In_Instance then
12474 null;
12475 else
12476 Error_Msg_NE
12477 ("actual for non-limited & cannot be a limited type", Actual,
12478 Gen_T);
12479 Explain_Limited_Type (Act_T, Actual);
12480 Abandon_Instantiation (Actual);
12481 end if;
12483 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12484 and then not Has_Preelaborable_Initialization (Act_T)
12485 then
12486 Error_Msg_NE
12487 ("actual for & must have preelaborable initialization", Actual,
12488 Gen_T);
12490 elsif not Is_Definite_Subtype (Act_T)
12491 and then Is_Definite_Subtype (A_Gen_T)
12492 and then Ada_Version >= Ada_95
12493 then
12494 Error_Msg_NE
12495 ("actual for & must be a definite subtype", Actual, Gen_T);
12497 elsif not Is_Tagged_Type (Act_T)
12498 and then Is_Tagged_Type (A_Gen_T)
12499 then
12500 Error_Msg_NE
12501 ("actual for & must be a tagged type", Actual, Gen_T);
12502 end if;
12504 Validate_Discriminated_Formal_Type;
12505 Ancestor := Gen_T;
12506 end Validate_Private_Type_Instance;
12508 -- Start of processing for Instantiate_Type
12510 begin
12511 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12512 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12513 return New_List (Error);
12515 elsif not Is_Entity_Name (Actual)
12516 or else not Is_Type (Entity (Actual))
12517 then
12518 Error_Msg_NE
12519 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12520 Abandon_Instantiation (Actual);
12522 else
12523 Act_T := Entity (Actual);
12525 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12526 -- as a generic actual parameter if the corresponding formal type
12527 -- does not have a known_discriminant_part, or is a formal derived
12528 -- type that is an Unchecked_Union type.
12530 if Is_Unchecked_Union (Base_Type (Act_T)) then
12531 if not Has_Discriminants (A_Gen_T)
12532 or else (Is_Derived_Type (A_Gen_T)
12533 and then Is_Unchecked_Union (A_Gen_T))
12534 then
12535 null;
12536 else
12537 Error_Msg_N ("unchecked union cannot be the actual for a "
12538 & "discriminated formal type", Act_T);
12540 end if;
12541 end if;
12543 -- Deal with fixed/floating restrictions
12545 if Is_Floating_Point_Type (Act_T) then
12546 Check_Restriction (No_Floating_Point, Actual);
12547 elsif Is_Fixed_Point_Type (Act_T) then
12548 Check_Restriction (No_Fixed_Point, Actual);
12549 end if;
12551 -- Deal with error of using incomplete type as generic actual.
12552 -- This includes limited views of a type, even if the non-limited
12553 -- view may be available.
12555 if Ekind (Act_T) = E_Incomplete_Type
12556 or else (Is_Class_Wide_Type (Act_T)
12557 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12558 then
12559 -- If the formal is an incomplete type, the actual can be
12560 -- incomplete as well.
12562 if Ekind (A_Gen_T) = E_Incomplete_Type then
12563 null;
12565 elsif Is_Class_Wide_Type (Act_T)
12566 or else No (Full_View (Act_T))
12567 then
12568 Error_Msg_N ("premature use of incomplete type", Actual);
12569 Abandon_Instantiation (Actual);
12570 else
12571 Act_T := Full_View (Act_T);
12572 Set_Entity (Actual, Act_T);
12574 if Has_Private_Component (Act_T) then
12575 Error_Msg_N
12576 ("premature use of type with private component", Actual);
12577 end if;
12578 end if;
12580 -- Deal with error of premature use of private type as generic actual
12582 elsif Is_Private_Type (Act_T)
12583 and then Is_Private_Type (Base_Type (Act_T))
12584 and then not Is_Generic_Type (Act_T)
12585 and then not Is_Derived_Type (Act_T)
12586 and then No (Full_View (Root_Type (Act_T)))
12587 then
12588 -- If the formal is an incomplete type, the actual can be
12589 -- private or incomplete as well.
12591 if Ekind (A_Gen_T) = E_Incomplete_Type then
12592 null;
12593 else
12594 Error_Msg_N ("premature use of private type", Actual);
12595 end if;
12597 elsif Has_Private_Component (Act_T) then
12598 Error_Msg_N
12599 ("premature use of type with private component", Actual);
12600 end if;
12602 Set_Instance_Of (A_Gen_T, Act_T);
12604 -- If the type is generic, the class-wide type may also be used
12606 if Is_Tagged_Type (A_Gen_T)
12607 and then Is_Tagged_Type (Act_T)
12608 and then not Is_Class_Wide_Type (A_Gen_T)
12609 then
12610 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12611 Class_Wide_Type (Act_T));
12612 end if;
12614 if not Is_Abstract_Type (A_Gen_T)
12615 and then Is_Abstract_Type (Act_T)
12616 then
12617 Error_Msg_N
12618 ("actual of non-abstract formal cannot be abstract", Actual);
12619 end if;
12621 -- A generic scalar type is a first subtype for which we generate
12622 -- an anonymous base type. Indicate that the instance of this base
12623 -- is the base type of the actual.
12625 if Is_Scalar_Type (A_Gen_T) then
12626 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12627 end if;
12628 end if;
12630 if Error_Posted (Act_T) then
12631 null;
12632 else
12633 case Nkind (Def) is
12634 when N_Formal_Private_Type_Definition =>
12635 Validate_Private_Type_Instance;
12637 when N_Formal_Incomplete_Type_Definition =>
12638 Validate_Incomplete_Type_Instance;
12640 when N_Formal_Derived_Type_Definition =>
12641 Validate_Derived_Type_Instance;
12643 when N_Formal_Discrete_Type_Definition =>
12644 if not Is_Discrete_Type (Act_T) then
12645 Error_Msg_NE
12646 ("expect discrete type in instantiation of&",
12647 Actual, Gen_T);
12648 Abandon_Instantiation (Actual);
12649 end if;
12651 Diagnose_Predicated_Actual;
12653 when N_Formal_Signed_Integer_Type_Definition =>
12654 if not Is_Signed_Integer_Type (Act_T) then
12655 Error_Msg_NE
12656 ("expect signed integer type in instantiation of&",
12657 Actual, Gen_T);
12658 Abandon_Instantiation (Actual);
12659 end if;
12661 Diagnose_Predicated_Actual;
12663 when N_Formal_Modular_Type_Definition =>
12664 if not Is_Modular_Integer_Type (Act_T) then
12665 Error_Msg_NE
12666 ("expect modular type in instantiation of &",
12667 Actual, Gen_T);
12668 Abandon_Instantiation (Actual);
12669 end if;
12671 Diagnose_Predicated_Actual;
12673 when N_Formal_Floating_Point_Definition =>
12674 if not Is_Floating_Point_Type (Act_T) then
12675 Error_Msg_NE
12676 ("expect float type in instantiation of &", Actual, Gen_T);
12677 Abandon_Instantiation (Actual);
12678 end if;
12680 when N_Formal_Ordinary_Fixed_Point_Definition =>
12681 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12682 Error_Msg_NE
12683 ("expect ordinary fixed point type in instantiation of &",
12684 Actual, Gen_T);
12685 Abandon_Instantiation (Actual);
12686 end if;
12688 when N_Formal_Decimal_Fixed_Point_Definition =>
12689 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12690 Error_Msg_NE
12691 ("expect decimal type in instantiation of &",
12692 Actual, Gen_T);
12693 Abandon_Instantiation (Actual);
12694 end if;
12696 when N_Array_Type_Definition =>
12697 Validate_Array_Type_Instance;
12699 when N_Access_To_Object_Definition =>
12700 Validate_Access_Type_Instance;
12702 when N_Access_Function_Definition |
12703 N_Access_Procedure_Definition =>
12704 Validate_Access_Subprogram_Instance;
12706 when N_Record_Definition =>
12707 Validate_Interface_Type_Instance;
12709 when N_Derived_Type_Definition =>
12710 Validate_Derived_Interface_Type_Instance;
12712 when others =>
12713 raise Program_Error;
12715 end case;
12716 end if;
12718 Subt := New_Copy (Gen_T);
12720 -- Use adjusted sloc of subtype name as the location for other nodes in
12721 -- the subtype declaration.
12723 Loc := Sloc (Subt);
12725 Decl_Node :=
12726 Make_Subtype_Declaration (Loc,
12727 Defining_Identifier => Subt,
12728 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12730 if Is_Private_Type (Act_T) then
12731 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12733 elsif Is_Access_Type (Act_T)
12734 and then Is_Private_Type (Designated_Type (Act_T))
12735 then
12736 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12737 end if;
12739 -- In Ada 2012 the actual may be a limited view. Indicate that
12740 -- the local subtype must be treated as such.
12742 if From_Limited_With (Act_T) then
12743 Set_Ekind (Subt, E_Incomplete_Subtype);
12744 Set_From_Limited_With (Subt);
12745 end if;
12747 Decl_Nodes := New_List (Decl_Node);
12749 -- Flag actual derived types so their elaboration produces the
12750 -- appropriate renamings for the primitive operations of the ancestor.
12751 -- Flag actual for formal private types as well, to determine whether
12752 -- operations in the private part may override inherited operations.
12753 -- If the formal has an interface list, the ancestor is not the
12754 -- parent, but the analyzed formal that includes the interface
12755 -- operations of all its progenitors.
12757 -- Same treatment for formal private types, so we can check whether the
12758 -- type is tagged limited when validating derivations in the private
12759 -- part. (See AI05-096).
12761 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12762 if Present (Interface_List (Def)) then
12763 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12764 else
12765 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12766 end if;
12768 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12769 N_Formal_Incomplete_Type_Definition)
12770 then
12771 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12772 end if;
12774 -- If the actual is a synchronized type that implements an interface,
12775 -- the primitive operations are attached to the corresponding record,
12776 -- and we have to treat it as an additional generic actual, so that its
12777 -- primitive operations become visible in the instance. The task or
12778 -- protected type itself does not carry primitive operations.
12780 if Is_Concurrent_Type (Act_T)
12781 and then Is_Tagged_Type (Act_T)
12782 and then Present (Corresponding_Record_Type (Act_T))
12783 and then Present (Ancestor)
12784 and then Is_Interface (Ancestor)
12785 then
12786 declare
12787 Corr_Rec : constant Entity_Id :=
12788 Corresponding_Record_Type (Act_T);
12789 New_Corr : Entity_Id;
12790 Corr_Decl : Node_Id;
12792 begin
12793 New_Corr := Make_Temporary (Loc, 'S');
12794 Corr_Decl :=
12795 Make_Subtype_Declaration (Loc,
12796 Defining_Identifier => New_Corr,
12797 Subtype_Indication =>
12798 New_Occurrence_Of (Corr_Rec, Loc));
12799 Append_To (Decl_Nodes, Corr_Decl);
12801 if Ekind (Act_T) = E_Task_Type then
12802 Set_Ekind (Subt, E_Task_Subtype);
12803 else
12804 Set_Ekind (Subt, E_Protected_Subtype);
12805 end if;
12807 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12808 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12809 Set_Generic_Parent_Type (Decl_Node, Empty);
12810 end;
12811 end if;
12813 -- For a floating-point type, capture dimension info if any, because
12814 -- the generated subtype declaration does not come from source and
12815 -- will not process dimensions.
12817 if Is_Floating_Point_Type (Act_T) then
12818 Copy_Dimensions (Act_T, Subt);
12819 end if;
12821 return Decl_Nodes;
12822 end Instantiate_Type;
12824 ---------------------
12825 -- Is_In_Main_Unit --
12826 ---------------------
12828 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12829 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12830 Current_Unit : Node_Id;
12832 begin
12833 if Unum = Main_Unit then
12834 return True;
12836 -- If the current unit is a subunit then it is either the main unit or
12837 -- is being compiled as part of the main unit.
12839 elsif Nkind (N) = N_Compilation_Unit then
12840 return Nkind (Unit (N)) = N_Subunit;
12841 end if;
12843 Current_Unit := Parent (N);
12844 while Present (Current_Unit)
12845 and then Nkind (Current_Unit) /= N_Compilation_Unit
12846 loop
12847 Current_Unit := Parent (Current_Unit);
12848 end loop;
12850 -- The instantiation node is in the main unit, or else the current node
12851 -- (perhaps as the result of nested instantiations) is in the main unit,
12852 -- or in the declaration of the main unit, which in this last case must
12853 -- be a body.
12855 return Unum = Main_Unit
12856 or else Current_Unit = Cunit (Main_Unit)
12857 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12858 or else (Present (Library_Unit (Current_Unit))
12859 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12860 end Is_In_Main_Unit;
12862 ----------------------------
12863 -- Load_Parent_Of_Generic --
12864 ----------------------------
12866 procedure Load_Parent_Of_Generic
12867 (N : Node_Id;
12868 Spec : Node_Id;
12869 Body_Optional : Boolean := False)
12871 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12872 Saved_Style_Check : constant Boolean := Style_Check;
12873 Saved_Warnings : constant Warning_Record := Save_Warnings;
12874 True_Parent : Node_Id;
12875 Inst_Node : Node_Id;
12876 OK : Boolean;
12877 Previous_Instances : constant Elist_Id := New_Elmt_List;
12879 procedure Collect_Previous_Instances (Decls : List_Id);
12880 -- Collect all instantiations in the given list of declarations, that
12881 -- precede the generic that we need to load. If the bodies of these
12882 -- instantiations are available, we must analyze them, to ensure that
12883 -- the public symbols generated are the same when the unit is compiled
12884 -- to generate code, and when it is compiled in the context of a unit
12885 -- that needs a particular nested instance. This process is applied to
12886 -- both package and subprogram instances.
12888 --------------------------------
12889 -- Collect_Previous_Instances --
12890 --------------------------------
12892 procedure Collect_Previous_Instances (Decls : List_Id) is
12893 Decl : Node_Id;
12895 begin
12896 Decl := First (Decls);
12897 while Present (Decl) loop
12898 if Sloc (Decl) >= Sloc (Inst_Node) then
12899 return;
12901 -- If Decl is an instantiation, then record it as requiring
12902 -- instantiation of the corresponding body, except if it is an
12903 -- abbreviated instantiation generated internally for conformance
12904 -- checking purposes only for the case of a formal package
12905 -- declared without a box (see Instantiate_Formal_Package). Such
12906 -- an instantiation does not generate any code (the actual code
12907 -- comes from actual) and thus does not need to be analyzed here.
12908 -- If the instantiation appears with a generic package body it is
12909 -- not analyzed here either.
12911 elsif Nkind (Decl) = N_Package_Instantiation
12912 and then not Is_Internal (Defining_Entity (Decl))
12913 then
12914 Append_Elmt (Decl, Previous_Instances);
12916 -- For a subprogram instantiation, omit instantiations intrinsic
12917 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12919 elsif Nkind_In (Decl, N_Function_Instantiation,
12920 N_Procedure_Instantiation)
12921 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12922 then
12923 Append_Elmt (Decl, Previous_Instances);
12925 elsif Nkind (Decl) = N_Package_Declaration then
12926 Collect_Previous_Instances
12927 (Visible_Declarations (Specification (Decl)));
12928 Collect_Previous_Instances
12929 (Private_Declarations (Specification (Decl)));
12931 -- Previous non-generic bodies may contain instances as well
12933 elsif Nkind (Decl) = N_Package_Body
12934 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12935 then
12936 Collect_Previous_Instances (Declarations (Decl));
12938 elsif Nkind (Decl) = N_Subprogram_Body
12939 and then not Acts_As_Spec (Decl)
12940 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12941 then
12942 Collect_Previous_Instances (Declarations (Decl));
12943 end if;
12945 Next (Decl);
12946 end loop;
12947 end Collect_Previous_Instances;
12949 -- Start of processing for Load_Parent_Of_Generic
12951 begin
12952 if not In_Same_Source_Unit (N, Spec)
12953 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12954 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12955 and then not Is_In_Main_Unit (Spec))
12956 then
12957 -- Find body of parent of spec, and analyze it. A special case arises
12958 -- when the parent is an instantiation, that is to say when we are
12959 -- currently instantiating a nested generic. In that case, there is
12960 -- no separate file for the body of the enclosing instance. Instead,
12961 -- the enclosing body must be instantiated as if it were a pending
12962 -- instantiation, in order to produce the body for the nested generic
12963 -- we require now. Note that in that case the generic may be defined
12964 -- in a package body, the instance defined in the same package body,
12965 -- and the original enclosing body may not be in the main unit.
12967 Inst_Node := Empty;
12969 True_Parent := Parent (Spec);
12970 while Present (True_Parent)
12971 and then Nkind (True_Parent) /= N_Compilation_Unit
12972 loop
12973 if Nkind (True_Parent) = N_Package_Declaration
12974 and then
12975 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12976 then
12977 -- Parent is a compilation unit that is an instantiation.
12978 -- Instantiation node has been replaced with package decl.
12980 Inst_Node := Original_Node (True_Parent);
12981 exit;
12983 elsif Nkind (True_Parent) = N_Package_Declaration
12984 and then Present (Generic_Parent (Specification (True_Parent)))
12985 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12986 then
12987 -- Parent is an instantiation within another specification.
12988 -- Declaration for instance has been inserted before original
12989 -- instantiation node. A direct link would be preferable?
12991 Inst_Node := Next (True_Parent);
12992 while Present (Inst_Node)
12993 and then Nkind (Inst_Node) /= N_Package_Instantiation
12994 loop
12995 Next (Inst_Node);
12996 end loop;
12998 -- If the instance appears within a generic, and the generic
12999 -- unit is defined within a formal package of the enclosing
13000 -- generic, there is no generic body available, and none
13001 -- needed. A more precise test should be used ???
13003 if No (Inst_Node) then
13004 return;
13005 end if;
13007 exit;
13009 else
13010 True_Parent := Parent (True_Parent);
13011 end if;
13012 end loop;
13014 -- Case where we are currently instantiating a nested generic
13016 if Present (Inst_Node) then
13017 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13019 -- Instantiation node and declaration of instantiated package
13020 -- were exchanged when only the declaration was needed.
13021 -- Restore instantiation node before proceeding with body.
13023 Set_Unit (Parent (True_Parent), Inst_Node);
13024 end if;
13026 -- Now complete instantiation of enclosing body, if it appears in
13027 -- some other unit. If it appears in the current unit, the body
13028 -- will have been instantiated already.
13030 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13032 -- We need to determine the expander mode to instantiate the
13033 -- enclosing body. Because the generic body we need may use
13034 -- global entities declared in the enclosing package (including
13035 -- aggregates) it is in general necessary to compile this body
13036 -- with expansion enabled, except if we are within a generic
13037 -- package, in which case the usual generic rule applies.
13039 declare
13040 Exp_Status : Boolean := True;
13041 Scop : Entity_Id;
13043 begin
13044 -- Loop through scopes looking for generic package
13046 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13047 while Present (Scop)
13048 and then Scop /= Standard_Standard
13049 loop
13050 if Ekind (Scop) = E_Generic_Package then
13051 Exp_Status := False;
13052 exit;
13053 end if;
13055 Scop := Scope (Scop);
13056 end loop;
13058 -- Collect previous instantiations in the unit that contains
13059 -- the desired generic.
13061 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13062 and then not Body_Optional
13063 then
13064 declare
13065 Decl : Elmt_Id;
13066 Info : Pending_Body_Info;
13067 Par : Node_Id;
13069 begin
13070 Par := Parent (Inst_Node);
13071 while Present (Par) loop
13072 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13073 Par := Parent (Par);
13074 end loop;
13076 pragma Assert (Present (Par));
13078 if Nkind (Par) = N_Package_Body then
13079 Collect_Previous_Instances (Declarations (Par));
13081 elsif Nkind (Par) = N_Package_Declaration then
13082 Collect_Previous_Instances
13083 (Visible_Declarations (Specification (Par)));
13084 Collect_Previous_Instances
13085 (Private_Declarations (Specification (Par)));
13087 else
13088 -- Enclosing unit is a subprogram body. In this
13089 -- case all instance bodies are processed in order
13090 -- and there is no need to collect them separately.
13092 null;
13093 end if;
13095 Decl := First_Elmt (Previous_Instances);
13096 while Present (Decl) loop
13097 Info :=
13098 (Inst_Node => Node (Decl),
13099 Act_Decl =>
13100 Instance_Spec (Node (Decl)),
13101 Expander_Status => Exp_Status,
13102 Current_Sem_Unit =>
13103 Get_Code_Unit (Sloc (Node (Decl))),
13104 Scope_Suppress => Scope_Suppress,
13105 Local_Suppress_Stack_Top =>
13106 Local_Suppress_Stack_Top,
13107 Version => Ada_Version,
13108 Version_Pragma => Ada_Version_Pragma,
13109 Warnings => Save_Warnings,
13110 SPARK_Mode => SPARK_Mode,
13111 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13113 -- Package instance
13116 Nkind (Node (Decl)) = N_Package_Instantiation
13117 then
13118 Instantiate_Package_Body
13119 (Info, Body_Optional => True);
13121 -- Subprogram instance
13123 else
13124 -- The instance_spec is in the wrapper package,
13125 -- usually followed by its local renaming
13126 -- declaration. See Build_Subprogram_Renaming
13127 -- for details. If the instance carries aspects,
13128 -- these result in the corresponding pragmas,
13129 -- inserted after the subprogram declaration.
13130 -- They must be skipped as well when retrieving
13131 -- the desired spec. A direct link would be
13132 -- more robust ???
13134 declare
13135 Decl : Node_Id :=
13136 (Last (Visible_Declarations
13137 (Specification (Info.Act_Decl))));
13138 begin
13139 while Nkind_In (Decl,
13140 N_Subprogram_Renaming_Declaration, N_Pragma)
13141 loop
13142 Decl := Prev (Decl);
13143 end loop;
13145 Info.Act_Decl := Decl;
13146 end;
13148 Instantiate_Subprogram_Body
13149 (Info, Body_Optional => True);
13150 end if;
13152 Next_Elmt (Decl);
13153 end loop;
13154 end;
13155 end if;
13157 Instantiate_Package_Body
13158 (Body_Info =>
13159 ((Inst_Node => Inst_Node,
13160 Act_Decl => True_Parent,
13161 Expander_Status => Exp_Status,
13162 Current_Sem_Unit => Get_Code_Unit
13163 (Sloc (Inst_Node)),
13164 Scope_Suppress => Scope_Suppress,
13165 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13166 Version => Ada_Version,
13167 Version_Pragma => Ada_Version_Pragma,
13168 Warnings => Save_Warnings,
13169 SPARK_Mode => SPARK_Mode,
13170 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13171 Body_Optional => Body_Optional);
13172 end;
13173 end if;
13175 -- Case where we are not instantiating a nested generic
13177 else
13178 Opt.Style_Check := False;
13179 Expander_Mode_Save_And_Set (True);
13180 Load_Needed_Body (Comp_Unit, OK);
13181 Opt.Style_Check := Saved_Style_Check;
13182 Restore_Warnings (Saved_Warnings);
13183 Expander_Mode_Restore;
13185 if not OK
13186 and then Unit_Requires_Body (Defining_Entity (Spec))
13187 and then not Body_Optional
13188 then
13189 declare
13190 Bname : constant Unit_Name_Type :=
13191 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13193 begin
13194 -- In CodePeer mode, the missing body may make the analysis
13195 -- incomplete, but we do not treat it as fatal.
13197 if CodePeer_Mode then
13198 return;
13200 else
13201 Error_Msg_Unit_1 := Bname;
13202 Error_Msg_N ("this instantiation requires$!", N);
13203 Error_Msg_File_1 :=
13204 Get_File_Name (Bname, Subunit => False);
13205 Error_Msg_N ("\but file{ was not found!", N);
13206 raise Unrecoverable_Error;
13207 end if;
13208 end;
13209 end if;
13210 end if;
13211 end if;
13213 -- If loading parent of the generic caused an instantiation circularity,
13214 -- we abandon compilation at this point, because otherwise in some cases
13215 -- we get into trouble with infinite recursions after this point.
13217 if Circularity_Detected then
13218 raise Unrecoverable_Error;
13219 end if;
13220 end Load_Parent_Of_Generic;
13222 ---------------------------------
13223 -- Map_Formal_Package_Entities --
13224 ---------------------------------
13226 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13227 E1 : Entity_Id;
13228 E2 : Entity_Id;
13230 begin
13231 Set_Instance_Of (Form, Act);
13233 -- Traverse formal and actual package to map the corresponding entities.
13234 -- We skip over internal entities that may be generated during semantic
13235 -- analysis, and find the matching entities by name, given that they
13236 -- must appear in the same order.
13238 E1 := First_Entity (Form);
13239 E2 := First_Entity (Act);
13240 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13241 -- Could this test be a single condition??? Seems like it could, and
13242 -- isn't FPE (Form) a constant anyway???
13244 if not Is_Internal (E1)
13245 and then Present (Parent (E1))
13246 and then not Is_Class_Wide_Type (E1)
13247 and then not Is_Internal_Name (Chars (E1))
13248 then
13249 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13250 Next_Entity (E2);
13251 end loop;
13253 if No (E2) then
13254 exit;
13255 else
13256 Set_Instance_Of (E1, E2);
13258 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13259 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13260 end if;
13262 if Is_Constrained (E1) then
13263 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13264 end if;
13266 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13267 Map_Formal_Package_Entities (E1, E2);
13268 end if;
13269 end if;
13270 end if;
13272 Next_Entity (E1);
13273 end loop;
13274 end Map_Formal_Package_Entities;
13276 -----------------------
13277 -- Move_Freeze_Nodes --
13278 -----------------------
13280 procedure Move_Freeze_Nodes
13281 (Out_Of : Entity_Id;
13282 After : Node_Id;
13283 L : List_Id)
13285 Decl : Node_Id;
13286 Next_Decl : Node_Id;
13287 Next_Node : Node_Id := After;
13288 Spec : Node_Id;
13290 function Is_Outer_Type (T : Entity_Id) return Boolean;
13291 -- Check whether entity is declared in a scope external to that of the
13292 -- generic unit.
13294 -------------------
13295 -- Is_Outer_Type --
13296 -------------------
13298 function Is_Outer_Type (T : Entity_Id) return Boolean is
13299 Scop : Entity_Id := Scope (T);
13301 begin
13302 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13303 return True;
13305 else
13306 while Scop /= Standard_Standard loop
13307 if Scop = Out_Of then
13308 return False;
13309 else
13310 Scop := Scope (Scop);
13311 end if;
13312 end loop;
13314 return True;
13315 end if;
13316 end Is_Outer_Type;
13318 -- Start of processing for Move_Freeze_Nodes
13320 begin
13321 if No (L) then
13322 return;
13323 end if;
13325 -- First remove the freeze nodes that may appear before all other
13326 -- declarations.
13328 Decl := First (L);
13329 while Present (Decl)
13330 and then Nkind (Decl) = N_Freeze_Entity
13331 and then Is_Outer_Type (Entity (Decl))
13332 loop
13333 Decl := Remove_Head (L);
13334 Insert_After (Next_Node, Decl);
13335 Set_Analyzed (Decl, False);
13336 Next_Node := Decl;
13337 Decl := First (L);
13338 end loop;
13340 -- Next scan the list of declarations and remove each freeze node that
13341 -- appears ahead of the current node.
13343 while Present (Decl) loop
13344 while Present (Next (Decl))
13345 and then Nkind (Next (Decl)) = N_Freeze_Entity
13346 and then Is_Outer_Type (Entity (Next (Decl)))
13347 loop
13348 Next_Decl := Remove_Next (Decl);
13349 Insert_After (Next_Node, Next_Decl);
13350 Set_Analyzed (Next_Decl, False);
13351 Next_Node := Next_Decl;
13352 end loop;
13354 -- If the declaration is a nested package or concurrent type, then
13355 -- recurse. Nested generic packages will have been processed from the
13356 -- inside out.
13358 case Nkind (Decl) is
13359 when N_Package_Declaration =>
13360 Spec := Specification (Decl);
13362 when N_Task_Type_Declaration =>
13363 Spec := Task_Definition (Decl);
13365 when N_Protected_Type_Declaration =>
13366 Spec := Protected_Definition (Decl);
13368 when others =>
13369 Spec := Empty;
13370 end case;
13372 if Present (Spec) then
13373 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13374 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13375 end if;
13377 Next (Decl);
13378 end loop;
13379 end Move_Freeze_Nodes;
13381 ----------------
13382 -- Next_Assoc --
13383 ----------------
13385 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13386 begin
13387 return Generic_Renamings.Table (E).Next_In_HTable;
13388 end Next_Assoc;
13390 ------------------------
13391 -- Preanalyze_Actuals --
13392 ------------------------
13394 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13395 Assoc : Node_Id;
13396 Act : Node_Id;
13397 Errs : constant Nat := Serious_Errors_Detected;
13399 Cur : Entity_Id := Empty;
13400 -- Current homograph of the instance name
13402 Vis : Boolean;
13403 -- Saved visibility status of the current homograph
13405 begin
13406 Assoc := First (Generic_Associations (N));
13408 -- If the instance is a child unit, its name may hide an outer homonym,
13409 -- so make it invisible to perform name resolution on the actuals.
13411 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13412 and then Present
13413 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13414 then
13415 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13417 if Is_Compilation_Unit (Cur) then
13418 Vis := Is_Immediately_Visible (Cur);
13419 Set_Is_Immediately_Visible (Cur, False);
13420 else
13421 Cur := Empty;
13422 end if;
13423 end if;
13425 while Present (Assoc) loop
13426 if Nkind (Assoc) /= N_Others_Choice then
13427 Act := Explicit_Generic_Actual_Parameter (Assoc);
13429 -- Within a nested instantiation, a defaulted actual is an empty
13430 -- association, so nothing to analyze. If the subprogram actual
13431 -- is an attribute, analyze prefix only, because actual is not a
13432 -- complete attribute reference.
13434 -- If actual is an allocator, analyze expression only. The full
13435 -- analysis can generate code, and if instance is a compilation
13436 -- unit we have to wait until the package instance is installed
13437 -- to have a proper place to insert this code.
13439 -- String literals may be operators, but at this point we do not
13440 -- know whether the actual is a formal subprogram or a string.
13442 if No (Act) then
13443 null;
13445 elsif Nkind (Act) = N_Attribute_Reference then
13446 Analyze (Prefix (Act));
13448 elsif Nkind (Act) = N_Explicit_Dereference then
13449 Analyze (Prefix (Act));
13451 elsif Nkind (Act) = N_Allocator then
13452 declare
13453 Expr : constant Node_Id := Expression (Act);
13455 begin
13456 if Nkind (Expr) = N_Subtype_Indication then
13457 Analyze (Subtype_Mark (Expr));
13459 -- Analyze separately each discriminant constraint, when
13460 -- given with a named association.
13462 declare
13463 Constr : Node_Id;
13465 begin
13466 Constr := First (Constraints (Constraint (Expr)));
13467 while Present (Constr) loop
13468 if Nkind (Constr) = N_Discriminant_Association then
13469 Analyze (Expression (Constr));
13470 else
13471 Analyze (Constr);
13472 end if;
13474 Next (Constr);
13475 end loop;
13476 end;
13478 else
13479 Analyze (Expr);
13480 end if;
13481 end;
13483 elsif Nkind (Act) /= N_Operator_Symbol then
13484 Analyze (Act);
13486 -- Within a package instance, mark actuals that are limited
13487 -- views, so their use can be moved to the body of the
13488 -- enclosing unit.
13490 if Is_Entity_Name (Act)
13491 and then Is_Type (Entity (Act))
13492 and then From_Limited_With (Entity (Act))
13493 and then Present (Inst)
13494 then
13495 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13496 end if;
13497 end if;
13499 if Errs /= Serious_Errors_Detected then
13501 -- Do a minimal analysis of the generic, to prevent spurious
13502 -- warnings complaining about the generic being unreferenced,
13503 -- before abandoning the instantiation.
13505 Analyze (Name (N));
13507 if Is_Entity_Name (Name (N))
13508 and then Etype (Name (N)) /= Any_Type
13509 then
13510 Generate_Reference (Entity (Name (N)), Name (N));
13511 Set_Is_Instantiated (Entity (Name (N)));
13512 end if;
13514 if Present (Cur) then
13516 -- For the case of a child instance hiding an outer homonym,
13517 -- provide additional warning which might explain the error.
13519 Set_Is_Immediately_Visible (Cur, Vis);
13520 Error_Msg_NE
13521 ("& hides outer unit with the same name??",
13522 N, Defining_Unit_Name (N));
13523 end if;
13525 Abandon_Instantiation (Act);
13526 end if;
13527 end if;
13529 Next (Assoc);
13530 end loop;
13532 if Present (Cur) then
13533 Set_Is_Immediately_Visible (Cur, Vis);
13534 end if;
13535 end Preanalyze_Actuals;
13537 -------------------
13538 -- Remove_Parent --
13539 -------------------
13541 procedure Remove_Parent (In_Body : Boolean := False) is
13542 S : Entity_Id := Current_Scope;
13543 -- S is the scope containing the instantiation just completed. The scope
13544 -- stack contains the parent instances of the instantiation, followed by
13545 -- the original S.
13547 Cur_P : Entity_Id;
13548 E : Entity_Id;
13549 P : Entity_Id;
13550 Hidden : Elmt_Id;
13552 begin
13553 -- After child instantiation is complete, remove from scope stack the
13554 -- extra copy of the current scope, and then remove parent instances.
13556 if not In_Body then
13557 Pop_Scope;
13559 while Current_Scope /= S loop
13560 P := Current_Scope;
13561 End_Package_Scope (Current_Scope);
13563 if In_Open_Scopes (P) then
13564 E := First_Entity (P);
13565 while Present (E) loop
13566 Set_Is_Immediately_Visible (E, True);
13567 Next_Entity (E);
13568 end loop;
13570 -- If instantiation is declared in a block, it is the enclosing
13571 -- scope that might be a parent instance. Note that only one
13572 -- block can be involved, because the parent instances have
13573 -- been installed within it.
13575 if Ekind (P) = E_Block then
13576 Cur_P := Scope (P);
13577 else
13578 Cur_P := P;
13579 end if;
13581 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13582 -- We are within an instance of some sibling. Retain
13583 -- visibility of parent, for proper subsequent cleanup, and
13584 -- reinstall private declarations as well.
13586 Set_In_Private_Part (P);
13587 Install_Private_Declarations (P);
13588 end if;
13590 -- If the ultimate parent is a top-level unit recorded in
13591 -- Instance_Parent_Unit, then reset its visibility to what it was
13592 -- before instantiation. (It's not clear what the purpose is of
13593 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13594 -- present before the ultimate parent test was added.???)
13596 elsif not In_Open_Scopes (Scope (P))
13597 or else (P = Instance_Parent_Unit
13598 and then not Parent_Unit_Visible)
13599 then
13600 Set_Is_Immediately_Visible (P, False);
13602 -- If the current scope is itself an instantiation of a generic
13603 -- nested within P, and we are in the private part of body of this
13604 -- instantiation, restore the full views of P, that were removed
13605 -- in End_Package_Scope above. This obscure case can occur when a
13606 -- subunit of a generic contains an instance of a child unit of
13607 -- its generic parent unit.
13609 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13610 declare
13611 Par : constant Entity_Id :=
13612 Generic_Parent (Package_Specification (S));
13613 begin
13614 if Present (Par)
13615 and then P = Scope (Par)
13616 and then (In_Package_Body (S) or else In_Private_Part (S))
13617 then
13618 Set_In_Private_Part (P);
13619 Install_Private_Declarations (P);
13620 end if;
13621 end;
13622 end if;
13623 end loop;
13625 -- Reset visibility of entities in the enclosing scope
13627 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13629 Hidden := First_Elmt (Hidden_Entities);
13630 while Present (Hidden) loop
13631 Set_Is_Immediately_Visible (Node (Hidden), True);
13632 Next_Elmt (Hidden);
13633 end loop;
13635 else
13636 -- Each body is analyzed separately, and there is no context that
13637 -- needs preserving from one body instance to the next, so remove all
13638 -- parent scopes that have been installed.
13640 while Present (S) loop
13641 End_Package_Scope (S);
13642 Set_Is_Immediately_Visible (S, False);
13643 S := Current_Scope;
13644 exit when S = Standard_Standard;
13645 end loop;
13646 end if;
13647 end Remove_Parent;
13649 -----------------
13650 -- Restore_Env --
13651 -----------------
13653 procedure Restore_Env is
13654 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13656 begin
13657 if No (Current_Instantiated_Parent.Act_Id) then
13658 -- Restore environment after subprogram inlining
13660 Restore_Private_Views (Empty);
13661 end if;
13663 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13664 Exchanged_Views := Saved.Exchanged_Views;
13665 Hidden_Entities := Saved.Hidden_Entities;
13666 Current_Sem_Unit := Saved.Current_Sem_Unit;
13667 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13668 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13670 Restore_Opt_Config_Switches (Saved.Switches);
13672 Instance_Envs.Decrement_Last;
13673 end Restore_Env;
13675 ---------------------------
13676 -- Restore_Private_Views --
13677 ---------------------------
13679 procedure Restore_Private_Views
13680 (Pack_Id : Entity_Id;
13681 Is_Package : Boolean := True)
13683 M : Elmt_Id;
13684 E : Entity_Id;
13685 Typ : Entity_Id;
13686 Dep_Elmt : Elmt_Id;
13687 Dep_Typ : Node_Id;
13689 procedure Restore_Nested_Formal (Formal : Entity_Id);
13690 -- Hide the generic formals of formal packages declared with box which
13691 -- were reachable in the current instantiation.
13693 ---------------------------
13694 -- Restore_Nested_Formal --
13695 ---------------------------
13697 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13698 Ent : Entity_Id;
13700 begin
13701 if Present (Renamed_Object (Formal))
13702 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13703 then
13704 return;
13706 elsif Present (Associated_Formal_Package (Formal)) then
13707 Ent := First_Entity (Formal);
13708 while Present (Ent) loop
13709 exit when Ekind (Ent) = E_Package
13710 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13712 Set_Is_Hidden (Ent);
13713 Set_Is_Potentially_Use_Visible (Ent, False);
13715 -- If package, then recurse
13717 if Ekind (Ent) = E_Package then
13718 Restore_Nested_Formal (Ent);
13719 end if;
13721 Next_Entity (Ent);
13722 end loop;
13723 end if;
13724 end Restore_Nested_Formal;
13726 -- Start of processing for Restore_Private_Views
13728 begin
13729 M := First_Elmt (Exchanged_Views);
13730 while Present (M) loop
13731 Typ := Node (M);
13733 -- Subtypes of types whose views have been exchanged, and that are
13734 -- defined within the instance, were not on the Private_Dependents
13735 -- list on entry to the instance, so they have to be exchanged
13736 -- explicitly now, in order to remain consistent with the view of the
13737 -- parent type.
13739 if Ekind_In (Typ, E_Private_Type,
13740 E_Limited_Private_Type,
13741 E_Record_Type_With_Private)
13742 then
13743 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13744 while Present (Dep_Elmt) loop
13745 Dep_Typ := Node (Dep_Elmt);
13747 if Scope (Dep_Typ) = Pack_Id
13748 and then Present (Full_View (Dep_Typ))
13749 then
13750 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13751 Exchange_Declarations (Dep_Typ);
13752 end if;
13754 Next_Elmt (Dep_Elmt);
13755 end loop;
13756 end if;
13758 Exchange_Declarations (Node (M));
13759 Next_Elmt (M);
13760 end loop;
13762 if No (Pack_Id) then
13763 return;
13764 end if;
13766 -- Make the generic formal parameters private, and make the formal types
13767 -- into subtypes of the actuals again.
13769 E := First_Entity (Pack_Id);
13770 while Present (E) loop
13771 Set_Is_Hidden (E, True);
13773 if Is_Type (E)
13774 and then Nkind (Parent (E)) = N_Subtype_Declaration
13775 then
13776 -- If the actual for E is itself a generic actual type from
13777 -- an enclosing instance, E is still a generic actual type
13778 -- outside of the current instance. This matter when resolving
13779 -- an overloaded call that may be ambiguous in the enclosing
13780 -- instance, when two of its actuals coincide.
13782 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13783 and then Is_Generic_Actual_Type
13784 (Entity (Subtype_Indication (Parent (E))))
13785 then
13786 null;
13787 else
13788 Set_Is_Generic_Actual_Type (E, False);
13789 end if;
13791 -- An unusual case of aliasing: the actual may also be directly
13792 -- visible in the generic, and be private there, while it is fully
13793 -- visible in the context of the instance. The internal subtype
13794 -- is private in the instance but has full visibility like its
13795 -- parent in the enclosing scope. This enforces the invariant that
13796 -- the privacy status of all private dependents of a type coincide
13797 -- with that of the parent type. This can only happen when a
13798 -- generic child unit is instantiated within a sibling.
13800 if Is_Private_Type (E)
13801 and then not Is_Private_Type (Etype (E))
13802 then
13803 Exchange_Declarations (E);
13804 end if;
13806 elsif Ekind (E) = E_Package then
13808 -- The end of the renaming list is the renaming of the generic
13809 -- package itself. If the instance is a subprogram, all entities
13810 -- in the corresponding package are renamings. If this entity is
13811 -- a formal package, make its own formals private as well. The
13812 -- actual in this case is itself the renaming of an instantiation.
13813 -- If the entity is not a package renaming, it is the entity
13814 -- created to validate formal package actuals: ignore it.
13816 -- If the actual is itself a formal package for the enclosing
13817 -- generic, or the actual for such a formal package, it remains
13818 -- visible on exit from the instance, and therefore nothing needs
13819 -- to be done either, except to keep it accessible.
13821 if Is_Package and then Renamed_Object (E) = Pack_Id then
13822 exit;
13824 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13825 null;
13827 elsif
13828 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13829 then
13830 Set_Is_Hidden (E, False);
13832 else
13833 declare
13834 Act_P : constant Entity_Id := Renamed_Object (E);
13835 Id : Entity_Id;
13837 begin
13838 Id := First_Entity (Act_P);
13839 while Present (Id)
13840 and then Id /= First_Private_Entity (Act_P)
13841 loop
13842 exit when Ekind (Id) = E_Package
13843 and then Renamed_Object (Id) = Act_P;
13845 Set_Is_Hidden (Id, True);
13846 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13848 if Ekind (Id) = E_Package then
13849 Restore_Nested_Formal (Id);
13850 end if;
13852 Next_Entity (Id);
13853 end loop;
13854 end;
13855 end if;
13856 end if;
13858 Next_Entity (E);
13859 end loop;
13860 end Restore_Private_Views;
13862 --------------
13863 -- Save_Env --
13864 --------------
13866 procedure Save_Env
13867 (Gen_Unit : Entity_Id;
13868 Act_Unit : Entity_Id)
13870 begin
13871 Init_Env;
13872 Set_Instance_Env (Gen_Unit, Act_Unit);
13873 end Save_Env;
13875 ----------------------------
13876 -- Save_Global_References --
13877 ----------------------------
13879 procedure Save_Global_References (Templ : Node_Id) is
13881 -- ??? it is horrible to use global variables in highly recursive code
13883 E : Entity_Id;
13884 -- The entity of the current associated node
13886 Gen_Scope : Entity_Id;
13887 -- The scope of the generic for which references are being saved
13889 N2 : Node_Id;
13890 -- The current associated node
13892 function Is_Global (E : Entity_Id) return Boolean;
13893 -- Check whether entity is defined outside of generic unit. Examine the
13894 -- scope of an entity, and the scope of the scope, etc, until we find
13895 -- either Standard, in which case the entity is global, or the generic
13896 -- unit itself, which indicates that the entity is local. If the entity
13897 -- is the generic unit itself, as in the case of a recursive call, or
13898 -- the enclosing generic unit, if different from the current scope, then
13899 -- it is local as well, because it will be replaced at the point of
13900 -- instantiation. On the other hand, if it is a reference to a child
13901 -- unit of a common ancestor, which appears in an instantiation, it is
13902 -- global because it is used to denote a specific compilation unit at
13903 -- the time the instantiations will be analyzed.
13905 procedure Qualify_Universal_Operands
13906 (Op : Node_Id;
13907 Func_Call : Node_Id);
13908 -- Op denotes a binary or unary operator in generic template Templ. Node
13909 -- Func_Call is the function call alternative of the operator within the
13910 -- the analyzed copy of the template. Change each operand which yields a
13911 -- universal type by wrapping it into a qualified expression
13913 -- Actual_Typ'(Operand)
13915 -- where Actual_Typ is the type of corresponding actual parameter of
13916 -- Operand in Func_Call.
13918 procedure Reset_Entity (N : Node_Id);
13919 -- Save semantic information on global entity so that it is not resolved
13920 -- again at instantiation time.
13922 procedure Save_Entity_Descendants (N : Node_Id);
13923 -- Apply Save_Global_References to the two syntactic descendants of
13924 -- non-terminal nodes that carry an Associated_Node and are processed
13925 -- through Reset_Entity. Once the global entity (if any) has been
13926 -- captured together with its type, only two syntactic descendants need
13927 -- to be traversed to complete the processing of the tree rooted at N.
13928 -- This applies to Selected_Components, Expanded_Names, and to Operator
13929 -- nodes. N can also be a character literal, identifier, or operator
13930 -- symbol node, but the call has no effect in these cases.
13932 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13933 -- Default actuals in nested instances must be handled specially
13934 -- because there is no link to them from the original tree. When an
13935 -- actual subprogram is given by a default, we add an explicit generic
13936 -- association for it in the instantiation node. When we save the
13937 -- global references on the name of the instance, we recover the list
13938 -- of generic associations, and add an explicit one to the original
13939 -- generic tree, through which a global actual can be preserved.
13940 -- Similarly, if a child unit is instantiated within a sibling, in the
13941 -- context of the parent, we must preserve the identifier of the parent
13942 -- so that it can be properly resolved in a subsequent instantiation.
13944 procedure Save_Global_Descendant (D : Union_Id);
13945 -- Apply Save_References recursively to the descendants of node D
13947 procedure Save_References (N : Node_Id);
13948 -- This is the recursive procedure that does the work, once the
13949 -- enclosing generic scope has been established.
13951 ---------------
13952 -- Is_Global --
13953 ---------------
13955 function Is_Global (E : Entity_Id) return Boolean is
13956 Se : Entity_Id;
13958 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13959 -- Determine whether the parent node of a reference to a child unit
13960 -- denotes an instantiation or a formal package, in which case the
13961 -- reference to the child unit is global, even if it appears within
13962 -- the current scope (e.g. when the instance appears within the body
13963 -- of an ancestor).
13965 ----------------------
13966 -- Is_Instance_Node --
13967 ----------------------
13969 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13970 begin
13971 return Nkind (Decl) in N_Generic_Instantiation
13972 or else
13973 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13974 end Is_Instance_Node;
13976 -- Start of processing for Is_Global
13978 begin
13979 if E = Gen_Scope then
13980 return False;
13982 elsif E = Standard_Standard then
13983 return True;
13985 elsif Is_Child_Unit (E)
13986 and then (Is_Instance_Node (Parent (N2))
13987 or else (Nkind (Parent (N2)) = N_Expanded_Name
13988 and then N2 = Selector_Name (Parent (N2))
13989 and then
13990 Is_Instance_Node (Parent (Parent (N2)))))
13991 then
13992 return True;
13994 else
13995 Se := Scope (E);
13996 while Se /= Gen_Scope loop
13997 if Se = Standard_Standard then
13998 return True;
13999 else
14000 Se := Scope (Se);
14001 end if;
14002 end loop;
14004 return False;
14005 end if;
14006 end Is_Global;
14008 --------------------------------
14009 -- Qualify_Universal_Operands --
14010 --------------------------------
14012 procedure Qualify_Universal_Operands
14013 (Op : Node_Id;
14014 Func_Call : Node_Id)
14016 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14017 -- Rewrite operand Opnd as a qualified expression of the form
14019 -- Actual_Typ'(Opnd)
14021 -- where Actual is the corresponding actual parameter of Opnd in
14022 -- function call Func_Call.
14024 function Qualify_Type
14025 (Loc : Source_Ptr;
14026 Typ : Entity_Id) return Node_Id;
14027 -- Qualify type Typ by creating a selected component of the form
14029 -- Scope_Of_Typ.Typ
14031 ---------------------
14032 -- Qualify_Operand --
14033 ---------------------
14035 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14036 Loc : constant Source_Ptr := Sloc (Opnd);
14037 Typ : constant Entity_Id := Etype (Actual);
14038 Mark : Node_Id;
14039 Qual : Node_Id;
14041 begin
14042 -- Qualify the operand when it is of a universal type. Note that
14043 -- the template is unanalyzed and it is not possible to directly
14044 -- query the type. This transformation is not done when the type
14045 -- of the actual is internally generated because the type will be
14046 -- regenerated in the instance.
14048 if Yields_Universal_Type (Opnd)
14049 and then Comes_From_Source (Typ)
14050 and then not Is_Hidden (Typ)
14051 then
14052 -- The type of the actual may be a global reference. Save this
14053 -- information by creating a reference to it.
14055 if Is_Global (Typ) then
14056 Mark := New_Occurrence_Of (Typ, Loc);
14058 -- Otherwise rely on resolution to find the proper type within
14059 -- the instance.
14061 else
14062 Mark := Qualify_Type (Loc, Typ);
14063 end if;
14065 Qual :=
14066 Make_Qualified_Expression (Loc,
14067 Subtype_Mark => Mark,
14068 Expression => Relocate_Node (Opnd));
14070 -- Mark the qualification to distinguish it from other source
14071 -- constructs and signal the instantiation mechanism that this
14072 -- node requires special processing. See Copy_Generic_Node for
14073 -- details.
14075 Set_Is_Qualified_Universal_Literal (Qual);
14077 Rewrite (Opnd, Qual);
14078 end if;
14079 end Qualify_Operand;
14081 ------------------
14082 -- Qualify_Type --
14083 ------------------
14085 function Qualify_Type
14086 (Loc : Source_Ptr;
14087 Typ : Entity_Id) return Node_Id
14089 Scop : constant Entity_Id := Scope (Typ);
14090 Result : Node_Id;
14092 begin
14093 Result := Make_Identifier (Loc, Chars (Typ));
14095 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14096 Result :=
14097 Make_Selected_Component (Loc,
14098 Prefix => Make_Identifier (Loc, Chars (Scop)),
14099 Selector_Name => Result);
14100 end if;
14102 return Result;
14103 end Qualify_Type;
14105 -- Local variables
14107 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14109 -- Start of processing for Qualify_Universal_Operands
14111 begin
14112 if Nkind (Op) in N_Binary_Op then
14113 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14114 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14116 elsif Nkind (Op) in N_Unary_Op then
14117 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14118 end if;
14119 end Qualify_Universal_Operands;
14121 ------------------
14122 -- Reset_Entity --
14123 ------------------
14125 procedure Reset_Entity (N : Node_Id) is
14126 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14127 -- If the type of N2 is global to the generic unit, save the type in
14128 -- the generic node. Just as we perform name capture for explicit
14129 -- references within the generic, we must capture the global types
14130 -- of local entities because they may participate in resolution in
14131 -- the instance.
14133 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14134 -- Find the ultimate ancestor of the current unit. If it is not a
14135 -- generic unit, then the name of the current unit in the prefix of
14136 -- an expanded name must be replaced with its generic homonym to
14137 -- ensure that it will be properly resolved in an instance.
14139 ---------------------
14140 -- Set_Global_Type --
14141 ---------------------
14143 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14144 Typ : constant Entity_Id := Etype (N2);
14146 begin
14147 Set_Etype (N, Typ);
14149 -- If the entity of N is not the associated node, this is a
14150 -- nested generic and it has an associated node as well, whose
14151 -- type is already the full view (see below). Indicate that the
14152 -- original node has a private view.
14154 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14155 Set_Has_Private_View (N);
14156 end if;
14158 -- If not a private type, nothing else to do
14160 if not Is_Private_Type (Typ) then
14161 if Is_Array_Type (Typ)
14162 and then Is_Private_Type (Component_Type (Typ))
14163 then
14164 Set_Has_Private_View (N);
14165 end if;
14167 -- If it is a derivation of a private type in a context where no
14168 -- full view is needed, nothing to do either.
14170 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14171 null;
14173 -- Otherwise mark the type for flipping and use the full view when
14174 -- available.
14176 else
14177 Set_Has_Private_View (N);
14179 if Present (Full_View (Typ)) then
14180 Set_Etype (N2, Full_View (Typ));
14181 end if;
14182 end if;
14184 if Is_Floating_Point_Type (Typ)
14185 and then Has_Dimension_System (Typ)
14186 then
14187 Copy_Dimensions (N2, N);
14188 end if;
14190 end Set_Global_Type;
14192 ------------------
14193 -- Top_Ancestor --
14194 ------------------
14196 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14197 Par : Entity_Id;
14199 begin
14200 Par := E;
14201 while Is_Child_Unit (Par) loop
14202 Par := Scope (Par);
14203 end loop;
14205 return Par;
14206 end Top_Ancestor;
14208 -- Start of processing for Reset_Entity
14210 begin
14211 N2 := Get_Associated_Node (N);
14212 E := Entity (N2);
14214 if Present (E) then
14216 -- If the node is an entry call to an entry in an enclosing task,
14217 -- it is rewritten as a selected component. No global entity to
14218 -- preserve in this case, since the expansion will be redone in
14219 -- the instance.
14221 if not Nkind_In (E, N_Defining_Character_Literal,
14222 N_Defining_Identifier,
14223 N_Defining_Operator_Symbol)
14224 then
14225 Set_Associated_Node (N, Empty);
14226 Set_Etype (N, Empty);
14227 return;
14228 end if;
14230 -- If the entity is an itype created as a subtype of an access
14231 -- type with a null exclusion restore source entity for proper
14232 -- visibility. The itype will be created anew in the instance.
14234 if Is_Itype (E)
14235 and then Ekind (E) = E_Access_Subtype
14236 and then Is_Entity_Name (N)
14237 and then Chars (Etype (E)) = Chars (N)
14238 then
14239 E := Etype (E);
14240 Set_Entity (N2, E);
14241 Set_Etype (N2, E);
14242 end if;
14244 if Is_Global (E) then
14246 -- If the entity is a package renaming that is the prefix of
14247 -- an expanded name, it has been rewritten as the renamed
14248 -- package, which is necessary semantically but complicates
14249 -- ASIS tree traversal, so we recover the original entity to
14250 -- expose the renaming. Take into account that the context may
14251 -- be a nested generic, that the original node may itself have
14252 -- an associated node that had better be an entity, and that
14253 -- the current node is still a selected component.
14255 if Ekind (E) = E_Package
14256 and then Nkind (N) = N_Selected_Component
14257 and then Nkind (Parent (N)) = N_Expanded_Name
14258 and then Present (Original_Node (N2))
14259 and then Is_Entity_Name (Original_Node (N2))
14260 and then Present (Entity (Original_Node (N2)))
14261 then
14262 if Is_Global (Entity (Original_Node (N2))) then
14263 N2 := Original_Node (N2);
14264 Set_Associated_Node (N, N2);
14265 Set_Global_Type (N, N2);
14267 -- Renaming is local, and will be resolved in instance
14269 else
14270 Set_Associated_Node (N, Empty);
14271 Set_Etype (N, Empty);
14272 end if;
14274 else
14275 Set_Global_Type (N, N2);
14276 end if;
14278 elsif Nkind (N) = N_Op_Concat
14279 and then Is_Generic_Type (Etype (N2))
14280 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14281 or else
14282 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14283 and then Is_Intrinsic_Subprogram (E)
14284 then
14285 null;
14287 -- Entity is local. Mark generic node as unresolved. Note that now
14288 -- it does not have an entity.
14290 else
14291 Set_Associated_Node (N, Empty);
14292 Set_Etype (N, Empty);
14293 end if;
14295 if Nkind (Parent (N)) in N_Generic_Instantiation
14296 and then N = Name (Parent (N))
14297 then
14298 Save_Global_Defaults (Parent (N), Parent (N2));
14299 end if;
14301 elsif Nkind (Parent (N)) = N_Selected_Component
14302 and then Nkind (Parent (N2)) = N_Expanded_Name
14303 then
14304 if Is_Global (Entity (Parent (N2))) then
14305 Change_Selected_Component_To_Expanded_Name (Parent (N));
14306 Set_Associated_Node (Parent (N), Parent (N2));
14307 Set_Global_Type (Parent (N), Parent (N2));
14308 Save_Entity_Descendants (N);
14310 -- If this is a reference to the current generic entity, replace
14311 -- by the name of the generic homonym of the current package. This
14312 -- is because in an instantiation Par.P.Q will not resolve to the
14313 -- name of the instance, whose enclosing scope is not necessarily
14314 -- Par. We use the generic homonym rather that the name of the
14315 -- generic itself because it may be hidden by a local declaration.
14317 elsif In_Open_Scopes (Entity (Parent (N2)))
14318 and then not
14319 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14320 then
14321 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14322 Rewrite (Parent (N),
14323 Make_Identifier (Sloc (N),
14324 Chars =>
14325 Chars (Generic_Homonym (Entity (Parent (N2))))));
14326 else
14327 Rewrite (Parent (N),
14328 Make_Identifier (Sloc (N),
14329 Chars => Chars (Selector_Name (Parent (N2)))));
14330 end if;
14331 end if;
14333 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14334 and then Parent (N) = Name (Parent (Parent (N)))
14335 then
14336 Save_Global_Defaults
14337 (Parent (Parent (N)), Parent (Parent (N2)));
14338 end if;
14340 -- A selected component may denote a static constant that has been
14341 -- folded. If the static constant is global to the generic, capture
14342 -- its value. Otherwise the folding will happen in any instantiation.
14344 elsif Nkind (Parent (N)) = N_Selected_Component
14345 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14346 then
14347 if Present (Entity (Original_Node (Parent (N2))))
14348 and then Is_Global (Entity (Original_Node (Parent (N2))))
14349 then
14350 Rewrite (Parent (N), New_Copy (Parent (N2)));
14351 Set_Analyzed (Parent (N), False);
14352 end if;
14354 -- A selected component may be transformed into a parameterless
14355 -- function call. If the called entity is global, rewrite the node
14356 -- appropriately, i.e. as an extended name for the global entity.
14358 elsif Nkind (Parent (N)) = N_Selected_Component
14359 and then Nkind (Parent (N2)) = N_Function_Call
14360 and then N = Selector_Name (Parent (N))
14361 then
14362 if No (Parameter_Associations (Parent (N2))) then
14363 if Is_Global (Entity (Name (Parent (N2)))) then
14364 Change_Selected_Component_To_Expanded_Name (Parent (N));
14365 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14366 Set_Global_Type (Parent (N), Name (Parent (N2)));
14367 Save_Entity_Descendants (N);
14369 else
14370 Set_Is_Prefixed_Call (Parent (N));
14371 Set_Associated_Node (N, Empty);
14372 Set_Etype (N, Empty);
14373 end if;
14375 -- In Ada 2005, X.F may be a call to a primitive operation,
14376 -- rewritten as F (X). This rewriting will be done again in an
14377 -- instance, so keep the original node. Global entities will be
14378 -- captured as for other constructs. Indicate that this must
14379 -- resolve as a call, to prevent accidental overloading in the
14380 -- instance, if both a component and a primitive operation appear
14381 -- as candidates.
14383 else
14384 Set_Is_Prefixed_Call (Parent (N));
14385 end if;
14387 -- Entity is local. Reset in generic unit, so that node is resolved
14388 -- anew at the point of instantiation.
14390 else
14391 Set_Associated_Node (N, Empty);
14392 Set_Etype (N, Empty);
14393 end if;
14394 end Reset_Entity;
14396 -----------------------------
14397 -- Save_Entity_Descendants --
14398 -----------------------------
14400 procedure Save_Entity_Descendants (N : Node_Id) is
14401 begin
14402 case Nkind (N) is
14403 when N_Binary_Op =>
14404 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14405 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14407 when N_Unary_Op =>
14408 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14410 when N_Expanded_Name |
14411 N_Selected_Component =>
14412 Save_Global_Descendant (Union_Id (Prefix (N)));
14413 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14415 when N_Identifier |
14416 N_Character_Literal |
14417 N_Operator_Symbol =>
14418 null;
14420 when others =>
14421 raise Program_Error;
14422 end case;
14423 end Save_Entity_Descendants;
14425 --------------------------
14426 -- Save_Global_Defaults --
14427 --------------------------
14429 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14430 Loc : constant Source_Ptr := Sloc (N1);
14431 Assoc2 : constant List_Id := Generic_Associations (N2);
14432 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14433 Assoc1 : List_Id;
14434 Act1 : Node_Id;
14435 Act2 : Node_Id;
14436 Def : Node_Id;
14437 Ndec : Node_Id;
14438 Subp : Entity_Id;
14439 Actual : Entity_Id;
14441 begin
14442 Assoc1 := Generic_Associations (N1);
14444 if Present (Assoc1) then
14445 Act1 := First (Assoc1);
14446 else
14447 Act1 := Empty;
14448 Set_Generic_Associations (N1, New_List);
14449 Assoc1 := Generic_Associations (N1);
14450 end if;
14452 if Present (Assoc2) then
14453 Act2 := First (Assoc2);
14454 else
14455 return;
14456 end if;
14458 while Present (Act1) and then Present (Act2) loop
14459 Next (Act1);
14460 Next (Act2);
14461 end loop;
14463 -- Find the associations added for default subprograms
14465 if Present (Act2) then
14466 while Nkind (Act2) /= N_Generic_Association
14467 or else No (Entity (Selector_Name (Act2)))
14468 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14469 loop
14470 Next (Act2);
14471 end loop;
14473 -- Add a similar association if the default is global. The
14474 -- renaming declaration for the actual has been analyzed, and
14475 -- its alias is the program it renames. Link the actual in the
14476 -- original generic tree with the node in the analyzed tree.
14478 while Present (Act2) loop
14479 Subp := Entity (Selector_Name (Act2));
14480 Def := Explicit_Generic_Actual_Parameter (Act2);
14482 -- Following test is defence against rubbish errors
14484 if No (Alias (Subp)) then
14485 return;
14486 end if;
14488 -- Retrieve the resolved actual from the renaming declaration
14489 -- created for the instantiated formal.
14491 Actual := Entity (Name (Parent (Parent (Subp))));
14492 Set_Entity (Def, Actual);
14493 Set_Etype (Def, Etype (Actual));
14495 if Is_Global (Actual) then
14496 Ndec :=
14497 Make_Generic_Association (Loc,
14498 Selector_Name =>
14499 New_Occurrence_Of (Subp, Loc),
14500 Explicit_Generic_Actual_Parameter =>
14501 New_Occurrence_Of (Actual, Loc));
14503 Set_Associated_Node
14504 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14506 Append (Ndec, Assoc1);
14508 -- If there are other defaults, add a dummy association in case
14509 -- there are other defaulted formals with the same name.
14511 elsif Present (Next (Act2)) then
14512 Ndec :=
14513 Make_Generic_Association (Loc,
14514 Selector_Name =>
14515 New_Occurrence_Of (Subp, Loc),
14516 Explicit_Generic_Actual_Parameter => Empty);
14518 Append (Ndec, Assoc1);
14519 end if;
14521 Next (Act2);
14522 end loop;
14523 end if;
14525 if Nkind (Name (N1)) = N_Identifier
14526 and then Is_Child_Unit (Gen_Id)
14527 and then Is_Global (Gen_Id)
14528 and then Is_Generic_Unit (Scope (Gen_Id))
14529 and then In_Open_Scopes (Scope (Gen_Id))
14530 then
14531 -- This is an instantiation of a child unit within a sibling, so
14532 -- that the generic parent is in scope. An eventual instance must
14533 -- occur within the scope of an instance of the parent. Make name
14534 -- in instance into an expanded name, to preserve the identifier
14535 -- of the parent, so it can be resolved subsequently.
14537 Rewrite (Name (N2),
14538 Make_Expanded_Name (Loc,
14539 Chars => Chars (Gen_Id),
14540 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14541 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14542 Set_Entity (Name (N2), Gen_Id);
14544 Rewrite (Name (N1),
14545 Make_Expanded_Name (Loc,
14546 Chars => Chars (Gen_Id),
14547 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14548 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14550 Set_Associated_Node (Name (N1), Name (N2));
14551 Set_Associated_Node (Prefix (Name (N1)), Empty);
14552 Set_Associated_Node
14553 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14554 Set_Etype (Name (N1), Etype (Gen_Id));
14555 end if;
14556 end Save_Global_Defaults;
14558 ----------------------------
14559 -- Save_Global_Descendant --
14560 ----------------------------
14562 procedure Save_Global_Descendant (D : Union_Id) is
14563 N1 : Node_Id;
14565 begin
14566 if D in Node_Range then
14567 if D = Union_Id (Empty) then
14568 null;
14570 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14571 Save_References (Node_Id (D));
14572 end if;
14574 elsif D in List_Range then
14575 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14576 null;
14578 else
14579 N1 := First (List_Id (D));
14580 while Present (N1) loop
14581 Save_References (N1);
14582 Next (N1);
14583 end loop;
14584 end if;
14586 -- Element list or other non-node field, nothing to do
14588 else
14589 null;
14590 end if;
14591 end Save_Global_Descendant;
14593 ---------------------
14594 -- Save_References --
14595 ---------------------
14597 -- This is the recursive procedure that does the work once the enclosing
14598 -- generic scope has been established. We have to treat specially a
14599 -- number of node rewritings that are required by semantic processing
14600 -- and which change the kind of nodes in the generic copy: typically
14601 -- constant-folding, replacing an operator node by a string literal, or
14602 -- a selected component by an expanded name. In each of those cases, the
14603 -- transformation is propagated to the generic unit.
14605 procedure Save_References (N : Node_Id) is
14606 Loc : constant Source_Ptr := Sloc (N);
14608 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14609 -- Determine whether arbitrary node Nod requires delayed capture of
14610 -- global references within its aspect specifications.
14612 procedure Save_References_In_Aggregate (N : Node_Id);
14613 -- Save all global references in [extension] aggregate node N
14615 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14616 -- Save all global references in a character literal or operator
14617 -- symbol denoted by N.
14619 procedure Save_References_In_Descendants (N : Node_Id);
14620 -- Save all global references in all descendants of node N
14622 procedure Save_References_In_Identifier (N : Node_Id);
14623 -- Save all global references in identifier node N
14625 procedure Save_References_In_Operator (N : Node_Id);
14626 -- Save all global references in operator node N
14628 procedure Save_References_In_Pragma (Prag : Node_Id);
14629 -- Save all global references found within the expression of pragma
14630 -- Prag.
14632 ---------------------------
14633 -- Requires_Delayed_Save --
14634 ---------------------------
14636 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14637 begin
14638 -- Generic packages and subprograms require delayed capture of
14639 -- global references within their aspects due to the timing of
14640 -- annotation analysis.
14642 if Nkind_In (Nod, N_Generic_Package_Declaration,
14643 N_Generic_Subprogram_Declaration,
14644 N_Package_Body,
14645 N_Package_Body_Stub,
14646 N_Subprogram_Body,
14647 N_Subprogram_Body_Stub)
14648 then
14649 -- Since the capture of global references is done on the
14650 -- unanalyzed generic template, there is no information around
14651 -- to infer the context. Use the Associated_Entity linkages to
14652 -- peek into the analyzed generic copy and determine what the
14653 -- template corresponds to.
14655 if Nod = Templ then
14656 return
14657 Is_Generic_Declaration_Or_Body
14658 (Unit_Declaration_Node
14659 (Associated_Entity (Defining_Entity (Nod))));
14661 -- Otherwise the generic unit being processed is not the top
14662 -- level template. It is safe to capture of global references
14663 -- within the generic unit because at this point the top level
14664 -- copy is fully analyzed.
14666 else
14667 return False;
14668 end if;
14670 -- Otherwise capture the global references without interference
14672 else
14673 return False;
14674 end if;
14675 end Requires_Delayed_Save;
14677 ----------------------------------
14678 -- Save_References_In_Aggregate --
14679 ----------------------------------
14681 procedure Save_References_In_Aggregate (N : Node_Id) is
14682 Nam : Node_Id;
14683 Qual : Node_Id := Empty;
14684 Typ : Entity_Id := Empty;
14686 use Atree.Unchecked_Access;
14687 -- This code section is part of implementing an untyped tree
14688 -- traversal, so it needs direct access to node fields.
14690 begin
14691 N2 := Get_Associated_Node (N);
14693 if Present (N2) then
14694 Typ := Etype (N2);
14696 -- In an instance within a generic, use the name of the actual
14697 -- and not the original generic parameter. If the actual is
14698 -- global in the current generic it must be preserved for its
14699 -- instantiation.
14701 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14702 and then Present (Generic_Parent_Type (Parent (Typ)))
14703 then
14704 Typ := Base_Type (Typ);
14705 Set_Etype (N2, Typ);
14706 end if;
14707 end if;
14709 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14710 Set_Associated_Node (N, Empty);
14712 -- If the aggregate is an actual in a call, it has been
14713 -- resolved in the current context, to some local type. The
14714 -- enclosing call may have been disambiguated by the aggregate,
14715 -- and this disambiguation might fail at instantiation time
14716 -- because the type to which the aggregate did resolve is not
14717 -- preserved. In order to preserve some of this information,
14718 -- wrap the aggregate in a qualified expression, using the id
14719 -- of its type. For further disambiguation we qualify the type
14720 -- name with its scope (if visible) because both id's will have
14721 -- corresponding entities in an instance. This resolves most of
14722 -- the problems with missing type information on aggregates in
14723 -- instances.
14725 if Present (N2)
14726 and then Nkind (N2) = Nkind (N)
14727 and then Nkind (Parent (N2)) in N_Subprogram_Call
14728 and then Present (Typ)
14729 and then Comes_From_Source (Typ)
14730 then
14731 Nam := Make_Identifier (Loc, Chars (Typ));
14733 if Is_Immediately_Visible (Scope (Typ)) then
14734 Nam :=
14735 Make_Selected_Component (Loc,
14736 Prefix =>
14737 Make_Identifier (Loc, Chars (Scope (Typ))),
14738 Selector_Name => Nam);
14739 end if;
14741 Qual :=
14742 Make_Qualified_Expression (Loc,
14743 Subtype_Mark => Nam,
14744 Expression => Relocate_Node (N));
14745 end if;
14746 end if;
14748 Save_Global_Descendant (Field1 (N));
14749 Save_Global_Descendant (Field2 (N));
14750 Save_Global_Descendant (Field3 (N));
14751 Save_Global_Descendant (Field5 (N));
14753 if Present (Qual) then
14754 Rewrite (N, Qual);
14755 end if;
14756 end Save_References_In_Aggregate;
14758 ----------------------------------------------
14759 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14760 ----------------------------------------------
14762 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14763 begin
14764 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14765 Reset_Entity (N);
14767 elsif Nkind (N) = N_Operator_Symbol
14768 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14769 then
14770 Change_Operator_Symbol_To_String_Literal (N);
14771 end if;
14772 end Save_References_In_Char_Lit_Or_Op_Symbol;
14774 ------------------------------------
14775 -- Save_References_In_Descendants --
14776 ------------------------------------
14778 procedure Save_References_In_Descendants (N : Node_Id) is
14779 use Atree.Unchecked_Access;
14780 -- This code section is part of implementing an untyped tree
14781 -- traversal, so it needs direct access to node fields.
14783 begin
14784 Save_Global_Descendant (Field1 (N));
14785 Save_Global_Descendant (Field2 (N));
14786 Save_Global_Descendant (Field3 (N));
14787 Save_Global_Descendant (Field4 (N));
14788 Save_Global_Descendant (Field5 (N));
14789 end Save_References_In_Descendants;
14791 -----------------------------------
14792 -- Save_References_In_Identifier --
14793 -----------------------------------
14795 procedure Save_References_In_Identifier (N : Node_Id) is
14796 begin
14797 -- The node did not undergo a transformation
14799 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14801 -- If this is a discriminant reference, always save it. It is
14802 -- used in the instance to find the corresponding discriminant
14803 -- positionally rather than by name.
14805 Set_Original_Discriminant
14806 (N, Original_Discriminant (Get_Associated_Node (N)));
14807 Reset_Entity (N);
14809 -- The analysis of the generic copy transformed the identifier
14810 -- into another construct. Propagate the changes to the template.
14812 else
14813 N2 := Get_Associated_Node (N);
14815 -- The identifier denotes a call to a parameterless function.
14816 -- Mark the node as resolved when the function is external.
14818 if Nkind (N2) = N_Function_Call then
14819 E := Entity (Name (N2));
14821 if Present (E) and then Is_Global (E) then
14822 Set_Etype (N, Etype (N2));
14823 else
14824 Set_Associated_Node (N, Empty);
14825 Set_Etype (N, Empty);
14826 end if;
14828 -- The identifier denotes a named number that was constant
14829 -- folded. Preserve the original name for ASIS and undo the
14830 -- constant folding which will be repeated in the instance.
14832 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14833 and then Is_Entity_Name (Original_Node (N2))
14834 then
14835 Set_Associated_Node (N, Original_Node (N2));
14836 Reset_Entity (N);
14838 -- The identifier resolved to a string literal. Propagate this
14839 -- information to the generic template.
14841 elsif Nkind (N2) = N_String_Literal then
14842 Rewrite (N, New_Copy (N2));
14844 -- The identifier is rewritten as a dereference if it is the
14845 -- prefix of an implicit dereference. Preserve the original
14846 -- tree as the analysis of the instance will expand the node
14847 -- again, but preserve the resolved entity if it is global.
14849 elsif Nkind (N2) = N_Explicit_Dereference then
14850 if Is_Entity_Name (Prefix (N2))
14851 and then Present (Entity (Prefix (N2)))
14852 and then Is_Global (Entity (Prefix (N2)))
14853 then
14854 Set_Associated_Node (N, Prefix (N2));
14856 elsif Nkind (Prefix (N2)) = N_Function_Call
14857 and then Present (Entity (Name (Prefix (N2))))
14858 and then Is_Global (Entity (Name (Prefix (N2))))
14859 then
14860 Rewrite (N,
14861 Make_Explicit_Dereference (Loc,
14862 Prefix =>
14863 Make_Function_Call (Loc,
14864 Name =>
14865 New_Occurrence_Of
14866 (Entity (Name (Prefix (N2))), Loc))));
14868 else
14869 Set_Associated_Node (N, Empty);
14870 Set_Etype (N, Empty);
14871 end if;
14873 -- The subtype mark of a nominally unconstrained object is
14874 -- rewritten as a subtype indication using the bounds of the
14875 -- expression. Recover the original subtype mark.
14877 elsif Nkind (N2) = N_Subtype_Indication
14878 and then Is_Entity_Name (Original_Node (N2))
14879 then
14880 Set_Associated_Node (N, Original_Node (N2));
14881 Reset_Entity (N);
14882 end if;
14883 end if;
14884 end Save_References_In_Identifier;
14886 ---------------------------------
14887 -- Save_References_In_Operator --
14888 ---------------------------------
14890 procedure Save_References_In_Operator (N : Node_Id) is
14891 begin
14892 -- The node did not undergo a transformation
14894 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14895 if Nkind (N) = N_Op_Concat then
14896 Set_Is_Component_Left_Opnd (N,
14897 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14899 Set_Is_Component_Right_Opnd (N,
14900 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14901 end if;
14903 Reset_Entity (N);
14905 -- The analysis of the generic copy transformed the operator into
14906 -- some other construct. Propagate the changes to the template if
14907 -- applicable.
14909 else
14910 N2 := Get_Associated_Node (N);
14912 -- The operator resoved to a function call
14914 if Nkind (N2) = N_Function_Call then
14916 -- Add explicit qualifications in the generic template for
14917 -- all operands of universal type. This aids resolution by
14918 -- preserving the actual type of a literal or an attribute
14919 -- that yields a universal result.
14921 Qualify_Universal_Operands (N, N2);
14923 E := Entity (Name (N2));
14925 if Present (E) and then Is_Global (E) then
14926 Set_Etype (N, Etype (N2));
14927 else
14928 Set_Associated_Node (N, Empty);
14929 Set_Etype (N, Empty);
14930 end if;
14932 -- The operator was folded into a literal
14934 elsif Nkind_In (N2, N_Integer_Literal,
14935 N_Real_Literal,
14936 N_String_Literal)
14937 then
14938 if Present (Original_Node (N2))
14939 and then Nkind (Original_Node (N2)) = Nkind (N)
14940 then
14941 -- Operation was constant-folded. Whenever possible,
14942 -- recover semantic information from unfolded node,
14943 -- for ASIS use.
14945 Set_Associated_Node (N, Original_Node (N2));
14947 if Nkind (N) = N_Op_Concat then
14948 Set_Is_Component_Left_Opnd (N,
14949 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14950 Set_Is_Component_Right_Opnd (N,
14951 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14952 end if;
14954 Reset_Entity (N);
14956 -- Propagate the constant folding back to the template
14958 else
14959 Rewrite (N, New_Copy (N2));
14960 Set_Analyzed (N, False);
14961 end if;
14963 -- The operator was folded into an enumeration literal. Retain
14964 -- the entity to avoid spurious ambiguities if it is overloaded
14965 -- at the point of instantiation or inlining.
14967 elsif Nkind (N2) = N_Identifier
14968 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14969 then
14970 Rewrite (N, New_Copy (N2));
14971 Set_Analyzed (N, False);
14972 end if;
14973 end if;
14975 -- Complete the operands check if node has not been constant
14976 -- folded.
14978 if Nkind (N) in N_Op then
14979 Save_Entity_Descendants (N);
14980 end if;
14981 end Save_References_In_Operator;
14983 -------------------------------
14984 -- Save_References_In_Pragma --
14985 -------------------------------
14987 procedure Save_References_In_Pragma (Prag : Node_Id) is
14988 Context : Node_Id;
14989 Do_Save : Boolean := True;
14991 use Atree.Unchecked_Access;
14992 -- This code section is part of implementing an untyped tree
14993 -- traversal, so it needs direct access to node fields.
14995 begin
14996 -- Do not save global references in pragmas generated from aspects
14997 -- because the pragmas will be regenerated at instantiation time.
14999 if From_Aspect_Specification (Prag) then
15000 Do_Save := False;
15002 -- The capture of global references within contract-related source
15003 -- pragmas associated with generic packages, subprograms or their
15004 -- respective bodies must be delayed due to timing of annotation
15005 -- analysis. Global references are still captured in routine
15006 -- Save_Global_References_In_Contract.
15008 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15009 if Is_Package_Contract_Annotation (Prag) then
15010 Context := Find_Related_Package_Or_Body (Prag);
15011 else
15012 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15013 Context := Find_Related_Declaration_Or_Body (Prag);
15014 end if;
15016 -- The use of Original_Node accounts for the case when the
15017 -- related context is generic template.
15019 if Requires_Delayed_Save (Original_Node (Context)) then
15020 Do_Save := False;
15021 end if;
15022 end if;
15024 -- For all other cases, save all global references within the
15025 -- descendants, but skip the following semantic fields:
15027 -- Field1 - Next_Pragma
15028 -- Field3 - Corresponding_Aspect
15029 -- Field5 - Next_Rep_Item
15031 if Do_Save then
15032 Save_Global_Descendant (Field2 (Prag));
15033 Save_Global_Descendant (Field4 (Prag));
15034 end if;
15035 end Save_References_In_Pragma;
15037 -- Start of processing for Save_References
15039 begin
15040 if N = Empty then
15041 null;
15043 -- Aggregates
15045 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15046 Save_References_In_Aggregate (N);
15048 -- Character literals, operator symbols
15050 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15051 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15053 -- Defining identifiers
15055 elsif Nkind (N) in N_Entity then
15056 null;
15058 -- Identifiers
15060 elsif Nkind (N) = N_Identifier then
15061 Save_References_In_Identifier (N);
15063 -- Operators
15065 elsif Nkind (N) in N_Op then
15066 Save_References_In_Operator (N);
15068 -- Pragmas
15070 elsif Nkind (N) = N_Pragma then
15071 Save_References_In_Pragma (N);
15073 else
15074 Save_References_In_Descendants (N);
15075 end if;
15077 -- Save all global references found within the aspect specifications
15078 -- of the related node.
15080 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15082 -- The capture of global references within aspects associated with
15083 -- generic packages, subprograms or their bodies must be delayed
15084 -- due to timing of annotation analysis. Global references are
15085 -- still captured in routine Save_Global_References_In_Contract.
15087 if Requires_Delayed_Save (N) then
15088 null;
15090 -- Otherwise save all global references within the aspects
15092 else
15093 Save_Global_References_In_Aspects (N);
15094 end if;
15095 end if;
15096 end Save_References;
15098 -- Start of processing for Save_Global_References
15100 begin
15101 Gen_Scope := Current_Scope;
15103 -- If the generic unit is a child unit, references to entities in the
15104 -- parent are treated as local, because they will be resolved anew in
15105 -- the context of the instance of the parent.
15107 while Is_Child_Unit (Gen_Scope)
15108 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15109 loop
15110 Gen_Scope := Scope (Gen_Scope);
15111 end loop;
15113 Save_References (Templ);
15114 end Save_Global_References;
15116 ---------------------------------------
15117 -- Save_Global_References_In_Aspects --
15118 ---------------------------------------
15120 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15121 Asp : Node_Id;
15122 Expr : Node_Id;
15124 begin
15125 Asp := First (Aspect_Specifications (N));
15126 while Present (Asp) loop
15127 Expr := Expression (Asp);
15129 if Present (Expr) then
15130 Save_Global_References (Expr);
15131 end if;
15133 Next (Asp);
15134 end loop;
15135 end Save_Global_References_In_Aspects;
15137 --------------------------------------
15138 -- Set_Copied_Sloc_For_Inlined_Body --
15139 --------------------------------------
15141 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15142 begin
15143 Create_Instantiation_Source (N, E, True, S_Adjustment);
15144 end Set_Copied_Sloc_For_Inlined_Body;
15146 ---------------------
15147 -- Set_Instance_Of --
15148 ---------------------
15150 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15151 begin
15152 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15153 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15154 Generic_Renamings.Increment_Last;
15155 end Set_Instance_Of;
15157 --------------------
15158 -- Set_Next_Assoc --
15159 --------------------
15161 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15162 begin
15163 Generic_Renamings.Table (E).Next_In_HTable := Next;
15164 end Set_Next_Assoc;
15166 -------------------
15167 -- Start_Generic --
15168 -------------------
15170 procedure Start_Generic is
15171 begin
15172 -- ??? More things could be factored out in this routine.
15173 -- Should probably be done at a later stage.
15175 Generic_Flags.Append (Inside_A_Generic);
15176 Inside_A_Generic := True;
15178 Expander_Mode_Save_And_Set (False);
15179 end Start_Generic;
15181 ----------------------
15182 -- Set_Instance_Env --
15183 ----------------------
15185 procedure Set_Instance_Env
15186 (Gen_Unit : Entity_Id;
15187 Act_Unit : Entity_Id)
15189 Assertion_Status : constant Boolean := Assertions_Enabled;
15190 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15191 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15193 begin
15194 -- Regardless of the current mode, predefined units are analyzed in the
15195 -- most current Ada mode, and earlier version Ada checks do not apply
15196 -- to predefined units. Nothing needs to be done for non-internal units.
15197 -- These are always analyzed in the current mode.
15199 if Is_Internal_File_Name
15200 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15201 Renamings_Included => True)
15202 then
15203 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15205 -- In Ada2012 we may want to enable assertions in an instance of a
15206 -- predefined unit, in which case we need to preserve the current
15207 -- setting for the Assertions_Enabled flag. This will become more
15208 -- critical when pre/postconditions are added to predefined units,
15209 -- as is already the case for some numeric libraries.
15211 if Ada_Version >= Ada_2012 then
15212 Assertions_Enabled := Assertion_Status;
15213 end if;
15215 -- SPARK_Mode for an instance is the one applicable at the point of
15216 -- instantiation.
15218 SPARK_Mode := Save_SPARK_Mode;
15219 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15221 -- Make sure dynamic elaboration checks are off in SPARK Mode
15223 if SPARK_Mode = On then
15224 Dynamic_Elaboration_Checks := False;
15225 end if;
15226 end if;
15228 Current_Instantiated_Parent :=
15229 (Gen_Id => Gen_Unit,
15230 Act_Id => Act_Unit,
15231 Next_In_HTable => Assoc_Null);
15232 end Set_Instance_Env;
15234 -----------------
15235 -- Switch_View --
15236 -----------------
15238 procedure Switch_View (T : Entity_Id) is
15239 BT : constant Entity_Id := Base_Type (T);
15240 Priv_Elmt : Elmt_Id := No_Elmt;
15241 Priv_Sub : Entity_Id;
15243 begin
15244 -- T may be private but its base type may have been exchanged through
15245 -- some other occurrence, in which case there is nothing to switch
15246 -- besides T itself. Note that a private dependent subtype of a private
15247 -- type might not have been switched even if the base type has been,
15248 -- because of the last branch of Check_Private_View (see comment there).
15250 if not Is_Private_Type (BT) then
15251 Prepend_Elmt (Full_View (T), Exchanged_Views);
15252 Exchange_Declarations (T);
15253 return;
15254 end if;
15256 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15258 if Present (Full_View (BT)) then
15259 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15260 Exchange_Declarations (BT);
15261 end if;
15263 while Present (Priv_Elmt) loop
15264 Priv_Sub := (Node (Priv_Elmt));
15266 -- We avoid flipping the subtype if the Etype of its full view is
15267 -- private because this would result in a malformed subtype. This
15268 -- occurs when the Etype of the subtype full view is the full view of
15269 -- the base type (and since the base types were just switched, the
15270 -- subtype is pointing to the wrong view). This is currently the case
15271 -- for tagged record types, access types (maybe more?) and needs to
15272 -- be resolved. ???
15274 if Present (Full_View (Priv_Sub))
15275 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15276 then
15277 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15278 Exchange_Declarations (Priv_Sub);
15279 end if;
15281 Next_Elmt (Priv_Elmt);
15282 end loop;
15283 end Switch_View;
15285 -----------------
15286 -- True_Parent --
15287 -----------------
15289 function True_Parent (N : Node_Id) return Node_Id is
15290 begin
15291 if Nkind (Parent (N)) = N_Subunit then
15292 return Parent (Corresponding_Stub (Parent (N)));
15293 else
15294 return Parent (N);
15295 end if;
15296 end True_Parent;
15298 -----------------------------
15299 -- Valid_Default_Attribute --
15300 -----------------------------
15302 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15303 Attr_Id : constant Attribute_Id :=
15304 Get_Attribute_Id (Attribute_Name (Def));
15305 T : constant Entity_Id := Entity (Prefix (Def));
15306 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15307 F : Entity_Id;
15308 Num_F : Nat;
15309 OK : Boolean;
15311 begin
15312 if No (T) or else T = Any_Id then
15313 return;
15314 end if;
15316 Num_F := 0;
15317 F := First_Formal (Nam);
15318 while Present (F) loop
15319 Num_F := Num_F + 1;
15320 Next_Formal (F);
15321 end loop;
15323 case Attr_Id is
15324 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15325 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15326 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15327 Attribute_Unbiased_Rounding =>
15328 OK := Is_Fun
15329 and then Num_F = 1
15330 and then Is_Floating_Point_Type (T);
15332 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15333 Attribute_Value | Attribute_Wide_Image |
15334 Attribute_Wide_Value =>
15335 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15337 when Attribute_Max | Attribute_Min =>
15338 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15340 when Attribute_Input =>
15341 OK := (Is_Fun and then Num_F = 1);
15343 when Attribute_Output | Attribute_Read | Attribute_Write =>
15344 OK := (not Is_Fun and then Num_F = 2);
15346 when others =>
15347 OK := False;
15348 end case;
15350 if not OK then
15351 Error_Msg_N
15352 ("attribute reference has wrong profile for subprogram", Def);
15353 end if;
15354 end Valid_Default_Attribute;
15356 end Sem_Ch12;