Remove obsolete ECOFF support.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob23f9ca7c223b2be2b9ff9a17584613c31b4d0943
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-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Rident; use Rident;
47 with Restrict; use Restrict;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Sinput; use Sinput;
72 with Sinput.L; use Sinput.L;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
75 with Uname; use Uname;
76 with Table;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Warnsw; use Warnsw;
82 with GNAT.HTable;
84 package body Sem_Ch12 is
86 ----------------------------------------------------------
87 -- Implementation of Generic Analysis and Instantiation --
88 ----------------------------------------------------------
90 -- GNAT implements generics by macro expansion. No attempt is made to share
91 -- generic instantiations (for now). Analysis of a generic definition does
92 -- not perform any expansion action, but the expander must be called on the
93 -- tree for each instantiation, because the expansion may of course depend
94 -- on the generic actuals. All of this is best achieved as follows:
96 -- a) Semantic analysis of a generic unit is performed on a copy of the
97 -- tree for the generic unit. All tree modifications that follow analysis
98 -- do not affect the original tree. Links are kept between the original
99 -- tree and the copy, in order to recognize non-local references within
100 -- the generic, and propagate them to each instance (recall that name
101 -- resolution is done on the generic declaration: generics are not really
102 -- macros). This is summarized in the following diagram:
104 -- .-----------. .----------.
105 -- | semantic |<--------------| generic |
106 -- | copy | | unit |
107 -- | |==============>| |
108 -- |___________| global |__________|
109 -- references | | |
110 -- | | |
111 -- .-----|--|.
112 -- | .-----|---.
113 -- | | .----------.
114 -- | | | generic |
115 -- |__| | |
116 -- |__| instance |
117 -- |__________|
119 -- b) Each instantiation copies the original tree, and inserts into it a
120 -- series of declarations that describe the mapping between generic formals
121 -- and actuals. For example, a generic In OUT parameter is an object
122 -- renaming of the corresponding actual, etc. Generic IN parameters are
123 -- constant declarations.
125 -- c) In order to give the right visibility for these renamings, we use
126 -- a different scheme for package and subprogram instantiations. For
127 -- packages, the list of renamings is inserted into the package
128 -- specification, before the visible declarations of the package. The
129 -- renamings are analyzed before any of the text of the instance, and are
130 -- thus visible at the right place. Furthermore, outside of the instance,
131 -- the generic parameters are visible and denote their corresponding
132 -- actuals.
134 -- For subprograms, we create a container package to hold the renamings
135 -- and the subprogram instance itself. Analysis of the package makes the
136 -- renaming declarations visible to the subprogram. After analyzing the
137 -- package, the defining entity for the subprogram is touched-up so that
138 -- it appears declared in the current scope, and not inside the container
139 -- package.
141 -- If the instantiation is a compilation unit, the container package is
142 -- given the same name as the subprogram instance. This ensures that
143 -- the elaboration procedure called by the binder, using the compilation
144 -- unit name, calls in fact the elaboration procedure for the package.
146 -- Not surprisingly, private types complicate this approach. By saving in
147 -- the original generic object the non-local references, we guarantee that
148 -- the proper entities are referenced at the point of instantiation.
149 -- However, for private types, this by itself does not insure that the
150 -- proper VIEW of the entity is used (the full type may be visible at the
151 -- point of generic definition, but not at instantiation, or vice-versa).
152 -- In order to reference the proper view, we special-case any reference
153 -- to private types in the generic object, by saving both views, one in
154 -- the generic and one in the semantic copy. At time of instantiation, we
155 -- check whether the two views are consistent, and exchange declarations if
156 -- necessary, in order to restore the correct visibility. Similarly, if
157 -- the instance view is private when the generic view was not, we perform
158 -- the exchange. After completing the instantiation, we restore the
159 -- current visibility. The flag Has_Private_View marks identifiers in the
160 -- the generic unit that require checking.
162 -- Visibility within nested generic units requires special handling.
163 -- Consider the following scheme:
165 -- type Global is ... -- outside of generic unit.
166 -- generic ...
167 -- package Outer is
168 -- ...
169 -- type Semi_Global is ... -- global to inner.
171 -- generic ... -- 1
172 -- procedure inner (X1 : Global; X2 : Semi_Global);
174 -- procedure in2 is new inner (...); -- 4
175 -- end Outer;
177 -- package New_Outer is new Outer (...); -- 2
178 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
180 -- The semantic analysis of Outer captures all occurrences of Global.
181 -- The semantic analysis of Inner (at 1) captures both occurrences of
182 -- Global and Semi_Global.
184 -- At point 2 (instantiation of Outer), we also produce a generic copy
185 -- of Inner, even though Inner is, at that point, not being instantiated.
186 -- (This is just part of the semantic analysis of New_Outer).
188 -- Critically, references to Global within Inner must be preserved, while
189 -- references to Semi_Global should not preserved, because they must now
190 -- resolve to an entity within New_Outer. To distinguish between these, we
191 -- use a global variable, Current_Instantiated_Parent, which is set when
192 -- performing a generic copy during instantiation (at 2). This variable is
193 -- used when performing a generic copy that is not an instantiation, but
194 -- that is nested within one, as the occurrence of 1 within 2. The analysis
195 -- of a nested generic only preserves references that are global to the
196 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
197 -- determine whether a reference is external to the given parent.
199 -- The instantiation at point 3 requires no special treatment. The method
200 -- works as well for further nestings of generic units, but of course the
201 -- variable Current_Instantiated_Parent must be stacked because nested
202 -- instantiations can occur, e.g. the occurrence of 4 within 2.
204 -- The instantiation of package and subprogram bodies is handled in a
205 -- similar manner, except that it is delayed until after semantic
206 -- analysis is complete. In this fashion complex cross-dependencies
207 -- between several package declarations and bodies containing generics
208 -- can be compiled which otherwise would diagnose spurious circularities.
210 -- For example, it is possible to compile two packages A and B that
211 -- have the following structure:
213 -- package A is package B is
214 -- generic ... generic ...
215 -- package G_A is package G_B is
217 -- with B; with A;
218 -- package body A is package body B is
219 -- package N_B is new G_B (..) package N_A is new G_A (..)
221 -- The table Pending_Instantiations in package Inline is used to keep
222 -- track of body instantiations that are delayed in this manner. Inline
223 -- handles the actual calls to do the body instantiations. This activity
224 -- is part of Inline, since the processing occurs at the same point, and
225 -- for essentially the same reason, as the handling of inlined routines.
227 ----------------------------------------------
228 -- Detection of Instantiation Circularities --
229 ----------------------------------------------
231 -- If we have a chain of instantiations that is circular, this is static
232 -- error which must be detected at compile time. The detection of these
233 -- circularities is carried out at the point that we insert a generic
234 -- instance spec or body. If there is a circularity, then the analysis of
235 -- the offending spec or body will eventually result in trying to load the
236 -- same unit again, and we detect this problem as we analyze the package
237 -- instantiation for the second time.
239 -- At least in some cases after we have detected the circularity, we get
240 -- into trouble if we try to keep going. The following flag is set if a
241 -- circularity is detected, and used to abandon compilation after the
242 -- messages have been posted.
244 -----------------------------------------
245 -- Implementation of Generic Contracts --
246 -----------------------------------------
248 -- A "contract" is a collection of aspects and pragmas that either verify a
249 -- property of a construct at runtime or classify the data flow to and from
250 -- the construct in some fashion.
252 -- Generic packages, subprograms and their respective bodies may be subject
253 -- to the following contract-related aspects or pragmas collectively known
254 -- as annotations:
256 -- package subprogram [body]
257 -- Abstract_State Contract_Cases
258 -- Initial_Condition Depends
259 -- Initializes Extensions_Visible
260 -- Global
261 -- package body Post
262 -- Refined_State Post_Class
263 -- Postcondition
264 -- Pre
265 -- Pre_Class
266 -- Precondition
267 -- Refined_Depends
268 -- Refined_Global
269 -- Refined_Post
270 -- Test_Case
272 -- Most package contract annotations utilize forward references to classify
273 -- data declared within the package [body]. Subprogram annotations then use
274 -- the classifications to further refine them. These inter dependencies are
275 -- problematic with respect to the implementation of generics because their
276 -- analysis, capture of global references and instantiation does not mesh
277 -- well with the existing mechanism.
279 -- 1) Analysis of generic contracts is carried out the same way non-generic
280 -- contracts are analyzed:
282 -- 1.1) General rule - a contract is analyzed after all related aspects
283 -- and pragmas are analyzed. This is done by routines
285 -- Analyze_Package_Body_Contract
286 -- Analyze_Package_Contract
287 -- Analyze_Subprogram_Body_Contract
288 -- Analyze_Subprogram_Contract
290 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
291 -- are processed.
293 -- 1.3) Compilation unit body - the contract is analyzed at the end of
294 -- the body declaration list.
296 -- 1.4) Package - the contract is analyzed at the end of the private or
297 -- visible declarations, prior to analyzing the contracts of any nested
298 -- packages or subprograms.
300 -- 1.5) Package body - the contract is analyzed at the end of the body
301 -- declaration list, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
304 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
305 -- package or a subprogram, then its contract is analyzed at the end of
306 -- the enclosing declarations, otherwise the subprogram is a compilation
307 -- unit 1.2).
309 -- 1.7) Subprogram body - if the subprogram body is declared inside a
310 -- block, a package body or a subprogram body, then its contract is
311 -- analyzed at the end of the enclosing declarations, otherwise the
312 -- subprogram is a compilation unit 1.3).
314 -- 2) Capture of global references within contracts is done after capturing
315 -- global references within the generic template. There are two reasons for
316 -- this delay - pragma annotations are not part of the generic template in
317 -- the case of a generic subprogram declaration, and analysis of contracts
318 -- is delayed.
320 -- Contract-related source pragmas within generic templates are prepared
321 -- for delayed capture of global references by routine
323 -- Create_Generic_Contract
325 -- The routine associates these pragmas with the contract of the template.
326 -- In the case of a generic subprogram declaration, the routine creates
327 -- generic templates for the pragmas declared after the subprogram because
328 -- they are not part of the template.
330 -- generic -- template starts
331 -- procedure Gen_Proc (Input : Integer); -- template ends
332 -- pragma Precondition (Input > 0); -- requires own template
334 -- 2.1) The capture of global references with aspect specifications and
335 -- source pragmas that apply to a generic unit must be suppressed when
336 -- the generic template is being processed because the contracts have not
337 -- been analyzed yet. Any attempts to capture global references at that
338 -- point will destroy the Associated_Node linkages and leave the template
339 -- undecorated. This delay is controlled by routine
341 -- Requires_Delayed_Save
343 -- 2.2) The real capture of global references within a contract is done
344 -- after the contract has been analyzed, by routine
346 -- Save_Global_References_In_Contract
348 -- 3) The instantiation of a generic contract occurs as part of the
349 -- instantiation of the contract owner. Generic subprogram declarations
350 -- require additional processing when the contract is specified by pragmas
351 -- because the pragmas are not part of the generic template. This is done
352 -- by routine
354 -- Instantiate_Subprogram_Contract
356 Circularity_Detected : Boolean := False;
357 -- This should really be reset on encountering a new main unit, but in
358 -- practice we are not using multiple main units so it is not critical.
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formal into this local package. The result is a
385 -- a package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back-end, so it can freely violate the placement
388 -- rules for generic formal declarations.
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formal. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted (i.e.
401 -- which still appear as generic formals.
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
407 -----------------------
408 -- Local subprograms --
409 -----------------------
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
418 Def : Node_Id);
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
424 -- The following procedures treat other kinds of formal parameters
426 procedure Analyze_Formal_Derived_Interface_Type
427 (N : Node_Id;
428 T : Entity_Id;
429 Def : Node_Id);
431 procedure Analyze_Formal_Derived_Type
432 (N : Node_Id;
433 T : Entity_Id;
434 Def : Node_Id);
436 procedure Analyze_Formal_Interface_Type
437 (N : Node_Id;
438 T : Entity_Id;
439 Def : Node_Id);
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
445 -- generic.
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Private_Type
457 (N : Node_Id;
458 T : Entity_Id;
459 Def : Node_Id);
460 -- Creates a new private type, which does not require completion
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
471 function Analyze_Associations
472 (I_Node : Node_Id;
473 Formals : List_Id;
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
481 procedure Analyze_Subprogram_Instantiation
482 (N : Node_Id;
483 K : Entity_Kind);
485 procedure Build_Instance_Compilation_Unit_Nodes
486 (N : Node_Id;
487 Act_Body : Node_Id;
488 Act_Decl : Node_Id);
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
499 procedure Check_Access_Definition (N : Node_Id);
500 -- Subsidiary routine to null exclusion processing. Perform an assertion
501 -- check on Ada version and the presence of an access definition in N.
503 procedure Check_Formal_Packages (P_Id : Entity_Id);
504 -- Apply the following to all formal packages in generic associations
506 procedure Check_Formal_Package_Instance
507 (Formal_Pack : Entity_Id;
508 Actual_Pack : Entity_Id);
509 -- Verify that the actuals of the actual instance match the actuals of
510 -- the template for a formal package that is not declared with a box.
512 procedure Check_Forward_Instantiation (Decl : Node_Id);
513 -- If the generic is a local entity and the corresponding body has not
514 -- been seen yet, flag enclosing packages to indicate that it will be
515 -- elaborated after the generic body. Subprograms declared in the same
516 -- package cannot be inlined by the front end because front-end inlining
517 -- requires a strict linear order of elaboration.
519 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
520 -- Check if some association between formals and actuals requires to make
521 -- visible primitives of a tagged type, and make those primitives visible.
522 -- Return the list of primitives whose visibility is modified (to restore
523 -- their visibility later through Restore_Hidden_Primitives). If no
524 -- candidate is found then return No_Elist.
526 procedure Check_Hidden_Child_Unit
527 (N : Node_Id;
528 Gen_Unit : Entity_Id;
529 Act_Decl_Id : Entity_Id);
530 -- If the generic unit is an implicit child instance within a parent
531 -- instance, we need to make an explicit test that it is not hidden by
532 -- a child instance of the same name and parent.
534 procedure Check_Generic_Actuals
535 (Instance : Entity_Id;
536 Is_Formal_Box : Boolean);
537 -- Similar to previous one. Check the actuals in the instantiation,
538 -- whose views can change between the point of instantiation and the point
539 -- of instantiation of the body. In addition, mark the generic renamings
540 -- as generic actuals, so that they are not compatible with other actuals.
541 -- Recurse on an actual that is a formal package whose declaration has
542 -- a box.
544 function Contains_Instance_Of
545 (Inner : Entity_Id;
546 Outer : Entity_Id;
547 N : Node_Id) return Boolean;
548 -- Inner is instantiated within the generic Outer. Check whether Inner
549 -- directly or indirectly contains an instance of Outer or of one of its
550 -- parents, in the case of a subunit. Each generic unit holds a list of
551 -- the entities instantiated within (at any depth). This procedure
552 -- determines whether the set of such lists contains a cycle, i.e. an
553 -- illegal circular instantiation.
555 function Denotes_Formal_Package
556 (Pack : Entity_Id;
557 On_Exit : Boolean := False;
558 Instance : Entity_Id := Empty) return Boolean;
559 -- Returns True if E is a formal package of an enclosing generic, or
560 -- the actual for such a formal in an enclosing instantiation. If such
561 -- a package is used as a formal in an nested generic, or as an actual
562 -- in a nested instantiation, the visibility of ITS formals should not
563 -- be modified. When called from within Restore_Private_Views, the flag
564 -- On_Exit is true, to indicate that the search for a possible enclosing
565 -- instance should ignore the current one. In that case Instance denotes
566 -- the declaration for which this is an actual. This declaration may be
567 -- an instantiation in the source, or the internal instantiation that
568 -- corresponds to the actual for a formal package.
570 function Earlier (N1, N2 : Node_Id) return Boolean;
571 -- Yields True if N1 and N2 appear in the same compilation unit,
572 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
573 -- traversal of the tree for the unit. Used to determine the placement
574 -- of freeze nodes for instance bodies that may depend on other instances.
576 function Find_Actual_Type
577 (Typ : Entity_Id;
578 Gen_Type : Entity_Id) return Entity_Id;
579 -- When validating the actual types of a child instance, check whether
580 -- the formal is a formal type of the parent unit, and retrieve the current
581 -- actual for it. Typ is the entity in the analyzed formal type declaration
582 -- (component or index type of an array type, or designated type of an
583 -- access formal) and Gen_Type is the enclosing analyzed formal array
584 -- or access type. The desired actual may be a formal of a parent, or may
585 -- be declared in a formal package of a parent. In both cases it is a
586 -- generic actual type because it appears within a visible instance.
587 -- Finally, it may be declared in a parent unit without being a formal
588 -- of that unit, in which case it must be retrieved by visibility.
589 -- Ambiguities may still arise if two homonyms are declared in two formal
590 -- packages, and the prefix of the formal type may be needed to resolve
591 -- the ambiguity in the instance ???
593 procedure Freeze_Subprogram_Body
594 (Inst_Node : Node_Id;
595 Gen_Body : Node_Id;
596 Pack_Id : Entity_Id);
597 -- The generic body may appear textually after the instance, including
598 -- in the proper body of a stub, or within a different package instance.
599 -- Given that the instance can only be elaborated after the generic, we
600 -- place freeze_nodes for the instance and/or for packages that may enclose
601 -- the instance and the generic, so that the back-end can establish the
602 -- proper order of elaboration.
604 function Get_Associated_Node (N : Node_Id) return Node_Id;
605 -- In order to propagate semantic information back from the analyzed copy
606 -- to the original generic, we maintain links between selected nodes in the
607 -- generic and their corresponding copies. At the end of generic analysis,
608 -- the routine Save_Global_References traverses the generic tree, examines
609 -- the semantic information, and preserves the links to those nodes that
610 -- contain global information. At instantiation, the information from the
611 -- associated node is placed on the new copy, so that name resolution is
612 -- not repeated.
614 -- Three kinds of source nodes have associated nodes:
616 -- a) those that can reference (denote) entities, that is identifiers,
617 -- character literals, expanded_names, operator symbols, operators,
618 -- and attribute reference nodes. These nodes have an Entity field
619 -- and are the set of nodes that are in N_Has_Entity.
621 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
623 -- c) selected components (N_Selected_Component)
625 -- For the first class, the associated node preserves the entity if it is
626 -- global. If the generic contains nested instantiations, the associated
627 -- node itself has been recopied, and a chain of them must be followed.
629 -- For aggregates, the associated node allows retrieval of the type, which
630 -- may otherwise not appear in the generic. The view of this type may be
631 -- different between generic and instantiation, and the full view can be
632 -- installed before the instantiation is analyzed. For aggregates of type
633 -- extensions, the same view exchange may have to be performed for some of
634 -- the ancestor types, if their view is private at the point of
635 -- instantiation.
637 -- Nodes that are selected components in the parse tree may be rewritten
638 -- as expanded names after resolution, and must be treated as potential
639 -- entity holders, which is why they also have an Associated_Node.
641 -- Nodes that do not come from source, such as freeze nodes, do not appear
642 -- in the generic tree, and need not have an associated node.
644 -- The associated node is stored in the Associated_Node field. Note that
645 -- this field overlaps Entity, which is fine, because the whole point is
646 -- that we don't need or want the normal Entity field in this situation.
648 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
649 -- Traverse the Exchanged_Views list to see if a type was private
650 -- and has already been flipped during this phase of instantiation.
652 procedure Hide_Current_Scope;
653 -- When instantiating a generic child unit, the parent context must be
654 -- present, but the instance and all entities that may be generated
655 -- must be inserted in the current scope. We leave the current scope
656 -- on the stack, but make its entities invisible to avoid visibility
657 -- problems. This is reversed at the end of the instantiation. This is
658 -- not done for the instantiation of the bodies, which only require the
659 -- instances of the generic parents to be in scope.
661 function In_Same_Declarative_Part
662 (F_Node : Node_Id;
663 Inst : Node_Id) return Boolean;
664 -- True if the instantiation Inst and the given freeze_node F_Node appear
665 -- within the same declarative part, ignoring subunits, but with no inter-
666 -- vening subprograms or concurrent units. Used to find the proper plave
667 -- for the freeze node of an instance, when the generic is declared in a
668 -- previous instance. If predicate is true, the freeze node of the instance
669 -- can be placed after the freeze node of the previous instance, Otherwise
670 -- it has to be placed at the end of the current declarative part.
672 function In_Main_Context (E : Entity_Id) return Boolean;
673 -- Check whether an instantiation is in the context of the main unit.
674 -- Used to determine whether its body should be elaborated to allow
675 -- front-end inlining.
677 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
678 -- Add the context clause of the unit containing a generic unit to a
679 -- compilation unit that is, or contains, an instantiation.
681 procedure Init_Env;
682 -- Establish environment for subsequent instantiation. Separated from
683 -- Save_Env because data-structures for visibility handling must be
684 -- initialized before call to Check_Generic_Child_Unit.
686 procedure Inline_Instance_Body
687 (N : Node_Id;
688 Gen_Unit : Entity_Id;
689 Act_Decl : Node_Id);
690 -- If front-end inlining is requested, instantiate the package body,
691 -- and preserve the visibility of its compilation unit, to insure
692 -- that successive instantiations succeed.
694 procedure Insert_Freeze_Node_For_Instance
695 (N : Node_Id;
696 F_Node : Node_Id);
697 -- N denotes a package or a subprogram instantiation and F_Node is the
698 -- associated freeze node. Insert the freeze node before the first source
699 -- body which follows immediately after N. If no such body is found, the
700 -- freeze node is inserted at the end of the declarative region which
701 -- contains N.
703 procedure Install_Body
704 (Act_Body : Node_Id;
705 N : Node_Id;
706 Gen_Body : Node_Id;
707 Gen_Decl : Node_Id);
708 -- If the instantiation happens textually before the body of the generic,
709 -- the instantiation of the body must be analyzed after the generic body,
710 -- and not at the point of instantiation. Such early instantiations can
711 -- happen if the generic and the instance appear in a package declaration
712 -- because the generic body can only appear in the corresponding package
713 -- body. Early instantiations can also appear if generic, instance and
714 -- body are all in the declarative part of a subprogram or entry. Entities
715 -- of packages that are early instantiations are delayed, and their freeze
716 -- node appears after the generic body. This rather complex machinery is
717 -- needed when nested instantiations are present, because the source does
718 -- not carry any indication of where the corresponding instance bodies must
719 -- be installed and frozen.
721 procedure Install_Formal_Packages (Par : Entity_Id);
722 -- Install the visible part of any formal of the parent that is a formal
723 -- package. Note that for the case of a formal package with a box, this
724 -- includes the formal part of the formal package (12.7(10/2)).
726 procedure Install_Hidden_Primitives
727 (Prims_List : in out Elist_Id;
728 Gen_T : Entity_Id;
729 Act_T : Entity_Id);
730 -- Remove suffix 'P' from hidden primitives of Act_T to match the
731 -- visibility of primitives of Gen_T. The list of primitives to which
732 -- the suffix is removed is added to Prims_List to restore them later.
734 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
735 -- When compiling an instance of a child unit the parent (which is
736 -- itself an instance) is an enclosing scope that must be made
737 -- immediately visible. This procedure is also used to install the non-
738 -- generic parent of a generic child unit when compiling its body, so
739 -- that full views of types in the parent are made visible.
741 -- The functions Instantiate_XXX perform various legality checks and build
742 -- the declarations for instantiated generic parameters. In all of these
743 -- Formal is the entity in the generic unit, Actual is the entity of
744 -- expression in the generic associations, and Analyzed_Formal is the
745 -- formal in the generic copy, which contains the semantic information to
746 -- be used to validate the actual.
748 function Instantiate_Object
749 (Formal : Node_Id;
750 Actual : Node_Id;
751 Analyzed_Formal : Node_Id) return List_Id;
753 function Instantiate_Type
754 (Formal : Node_Id;
755 Actual : Node_Id;
756 Analyzed_Formal : Node_Id;
757 Actual_Decls : List_Id) return List_Id;
759 function Instantiate_Formal_Subprogram
760 (Formal : Node_Id;
761 Actual : Node_Id;
762 Analyzed_Formal : Node_Id) return Node_Id;
764 function Instantiate_Formal_Package
765 (Formal : Node_Id;
766 Actual : Node_Id;
767 Analyzed_Formal : Node_Id) return List_Id;
768 -- If the formal package is declared with a box, special visibility rules
769 -- apply to its formals: they are in the visible part of the package. This
770 -- is true in the declarative region of the formal package, that is to say
771 -- in the enclosing generic or instantiation. For an instantiation, the
772 -- parameters of the formal package are made visible in an explicit step.
773 -- Furthermore, if the actual has a visible USE clause, these formals must
774 -- be made potentially use-visible as well. On exit from the enclosing
775 -- instantiation, the reverse must be done.
777 -- For a formal package declared without a box, there are conformance rules
778 -- that apply to the actuals in the generic declaration and the actuals of
779 -- the actual package in the enclosing instantiation. The simplest way to
780 -- apply these rules is to repeat the instantiation of the formal package
781 -- in the context of the enclosing instance, and compare the generic
782 -- associations of this instantiation with those of the actual package.
783 -- This internal instantiation only needs to contain the renamings of the
784 -- formals: the visible and private declarations themselves need not be
785 -- created.
787 -- In Ada 2005, the formal package may be only partially parameterized.
788 -- In that case the visibility step must make visible those actuals whose
789 -- corresponding formals were given with a box. A final complication
790 -- involves inherited operations from formal derived types, which must
791 -- be visible if the type is.
793 function Is_In_Main_Unit (N : Node_Id) return Boolean;
794 -- Test if given node is in the main unit
796 procedure Load_Parent_Of_Generic
797 (N : Node_Id;
798 Spec : Node_Id;
799 Body_Optional : Boolean := False);
800 -- If the generic appears in a separate non-generic library unit, load the
801 -- corresponding body to retrieve the body of the generic. N is the node
802 -- for the generic instantiation, Spec is the generic package declaration.
804 -- Body_Optional is a flag that indicates that the body is being loaded to
805 -- ensure that temporaries are generated consistently when there are other
806 -- instances in the current declarative part that precede the one being
807 -- loaded. In that case a missing body is acceptable.
809 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
810 -- Within the generic part, entities in the formal package are
811 -- visible. To validate subsequent type declarations, indicate
812 -- the correspondence between the entities in the analyzed formal,
813 -- and the entities in the actual package. There are three packages
814 -- involved in the instantiation of a formal package: the parent
815 -- generic P1 which appears in the generic declaration, the fake
816 -- instantiation P2 which appears in the analyzed generic, and whose
817 -- visible entities may be used in subsequent formals, and the actual
818 -- P3 in the instance. To validate subsequent formals, me indicate
819 -- that the entities in P2 are mapped into those of P3. The mapping of
820 -- entities has to be done recursively for nested packages.
822 procedure Move_Freeze_Nodes
823 (Out_Of : Entity_Id;
824 After : Node_Id;
825 L : List_Id);
826 -- Freeze nodes can be generated in the analysis of a generic unit, but
827 -- will not be seen by the back-end. It is necessary to move those nodes
828 -- to the enclosing scope if they freeze an outer entity. We place them
829 -- at the end of the enclosing generic package, which is semantically
830 -- neutral.
832 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
833 -- Analyze actuals to perform name resolution. Full resolution is done
834 -- later, when the expected types are known, but names have to be captured
835 -- before installing parents of generics, that are not visible for the
836 -- actuals themselves.
838 -- If Inst is present, it is the entity of the package instance. This
839 -- entity is marked as having a limited_view actual when some actual is
840 -- a limited view. This is used to place the instance body properly.
842 procedure Provide_Completing_Bodies (N : Node_Id);
843 -- Generate completing bodies for all subprograms found within package or
844 -- subprogram declaration N.
846 procedure Remove_Parent (In_Body : Boolean := False);
847 -- Reverse effect after instantiation of child is complete
849 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
850 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
851 -- set to No_Elist.
853 procedure Set_Instance_Env
854 (Gen_Unit : Entity_Id;
855 Act_Unit : Entity_Id);
856 -- Save current instance on saved environment, to be used to determine
857 -- the global status of entities in nested instances. Part of Save_Env.
858 -- called after verifying that the generic unit is legal for the instance,
859 -- The procedure also examines whether the generic unit is a predefined
860 -- unit, in order to set configuration switches accordingly. As a result
861 -- the procedure must be called after analyzing and freezing the actuals.
863 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
864 -- Associate analyzed generic parameter with corresponding instance. Used
865 -- for semantic checks at instantiation time.
867 function True_Parent (N : Node_Id) return Node_Id;
868 -- For a subunit, return parent of corresponding stub, else return
869 -- parent of node.
871 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
872 -- Verify that an attribute that appears as the default for a formal
873 -- subprogram is a function or procedure with the correct profile.
875 -------------------------------------------
876 -- Data Structures for Generic Renamings --
877 -------------------------------------------
879 -- The map Generic_Renamings associates generic entities with their
880 -- corresponding actuals. Currently used to validate type instances. It
881 -- will eventually be used for all generic parameters to eliminate the
882 -- need for overload resolution in the instance.
884 type Assoc_Ptr is new Int;
886 Assoc_Null : constant Assoc_Ptr := -1;
888 type Assoc is record
889 Gen_Id : Entity_Id;
890 Act_Id : Entity_Id;
891 Next_In_HTable : Assoc_Ptr;
892 end record;
894 package Generic_Renamings is new Table.Table
895 (Table_Component_Type => Assoc,
896 Table_Index_Type => Assoc_Ptr,
897 Table_Low_Bound => 0,
898 Table_Initial => 10,
899 Table_Increment => 100,
900 Table_Name => "Generic_Renamings");
902 -- Variable to hold enclosing instantiation. When the environment is
903 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
905 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
907 -- Hash table for associations
909 HTable_Size : constant := 37;
910 type HTable_Range is range 0 .. HTable_Size - 1;
912 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
913 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
914 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
915 function Hash (F : Entity_Id) return HTable_Range;
917 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
918 Header_Num => HTable_Range,
919 Element => Assoc,
920 Elmt_Ptr => Assoc_Ptr,
921 Null_Ptr => Assoc_Null,
922 Set_Next => Set_Next_Assoc,
923 Next => Next_Assoc,
924 Key => Entity_Id,
925 Get_Key => Get_Gen_Id,
926 Hash => Hash,
927 Equal => "=");
929 Exchanged_Views : Elist_Id;
930 -- This list holds the private views that have been exchanged during
931 -- instantiation to restore the visibility of the generic declaration.
932 -- (see comments above). After instantiation, the current visibility is
933 -- reestablished by means of a traversal of this list.
935 Hidden_Entities : Elist_Id;
936 -- This list holds the entities of the current scope that are removed
937 -- from immediate visibility when instantiating a child unit. Their
938 -- visibility is restored in Remove_Parent.
940 -- Because instantiations can be recursive, the following must be saved
941 -- on entry and restored on exit from an instantiation (spec or body).
942 -- This is done by the two procedures Save_Env and Restore_Env. For
943 -- package and subprogram instantiations (but not for the body instances)
944 -- the action of Save_Env is done in two steps: Init_Env is called before
945 -- Check_Generic_Child_Unit, because setting the parent instances requires
946 -- that the visibility data structures be properly initialized. Once the
947 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
949 Parent_Unit_Visible : Boolean := False;
950 -- Parent_Unit_Visible is used when the generic is a child unit, and
951 -- indicates whether the ultimate parent of the generic is visible in the
952 -- instantiation environment. It is used to reset the visibility of the
953 -- parent at the end of the instantiation (see Remove_Parent).
955 Instance_Parent_Unit : Entity_Id := Empty;
956 -- This records the ultimate parent unit of an instance of a generic
957 -- child unit and is used in conjunction with Parent_Unit_Visible to
958 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
960 type Instance_Env is record
961 Instantiated_Parent : Assoc;
962 Exchanged_Views : Elist_Id;
963 Hidden_Entities : Elist_Id;
964 Current_Sem_Unit : Unit_Number_Type;
965 Parent_Unit_Visible : Boolean := False;
966 Instance_Parent_Unit : Entity_Id := Empty;
967 Switches : Config_Switches_Type;
968 end record;
970 package Instance_Envs is new Table.Table (
971 Table_Component_Type => Instance_Env,
972 Table_Index_Type => Int,
973 Table_Low_Bound => 0,
974 Table_Initial => 32,
975 Table_Increment => 100,
976 Table_Name => "Instance_Envs");
978 procedure Restore_Private_Views
979 (Pack_Id : Entity_Id;
980 Is_Package : Boolean := True);
981 -- Restore the private views of external types, and unmark the generic
982 -- renamings of actuals, so that they become compatible subtypes again.
983 -- For subprograms, Pack_Id is the package constructed to hold the
984 -- renamings.
986 procedure Switch_View (T : Entity_Id);
987 -- Switch the partial and full views of a type and its private
988 -- dependents (i.e. its subtypes and derived types).
990 ------------------------------------
991 -- Structures for Error Reporting --
992 ------------------------------------
994 Instantiation_Node : Node_Id;
995 -- Used by subprograms that validate instantiation of formal parameters
996 -- where there might be no actual on which to place the error message.
997 -- Also used to locate the instantiation node for generic subunits.
999 Instantiation_Error : exception;
1000 -- When there is a semantic error in the generic parameter matching,
1001 -- there is no point in continuing the instantiation, because the
1002 -- number of cascaded errors is unpredictable. This exception aborts
1003 -- the instantiation process altogether.
1005 S_Adjustment : Sloc_Adjustment;
1006 -- Offset created for each node in an instantiation, in order to keep
1007 -- track of the source position of the instantiation in each of its nodes.
1008 -- A subsequent semantic error or warning on a construct of the instance
1009 -- points to both places: the original generic node, and the point of
1010 -- instantiation. See Sinput and Sinput.L for additional details.
1012 ------------------------------------------------------------
1013 -- Data structure for keeping track when inside a Generic --
1014 ------------------------------------------------------------
1016 -- The following table is used to save values of the Inside_A_Generic
1017 -- flag (see spec of Sem) when they are saved by Start_Generic.
1019 package Generic_Flags is new Table.Table (
1020 Table_Component_Type => Boolean,
1021 Table_Index_Type => Int,
1022 Table_Low_Bound => 0,
1023 Table_Initial => 32,
1024 Table_Increment => 200,
1025 Table_Name => "Generic_Flags");
1027 ---------------------------
1028 -- Abandon_Instantiation --
1029 ---------------------------
1031 procedure Abandon_Instantiation (N : Node_Id) is
1032 begin
1033 Error_Msg_N ("\instantiation abandoned!", N);
1034 raise Instantiation_Error;
1035 end Abandon_Instantiation;
1037 --------------------------------
1038 -- Add_Pending_Instantiation --
1039 --------------------------------
1041 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1042 begin
1044 -- Add to the instantiation node and the corresponding unit declaration
1045 -- the current values of global flags to be used when analyzing the
1046 -- instance body.
1048 Pending_Instantiations.Append
1049 ((Inst_Node => Inst,
1050 Act_Decl => Act_Decl,
1051 Expander_Status => Expander_Active,
1052 Current_Sem_Unit => Current_Sem_Unit,
1053 Scope_Suppress => Scope_Suppress,
1054 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1055 Version => Ada_Version,
1056 Version_Pragma => Ada_Version_Pragma,
1057 Warnings => Save_Warnings,
1058 SPARK_Mode => SPARK_Mode,
1059 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1060 end Add_Pending_Instantiation;
1062 ----------------------------------
1063 -- Adjust_Inherited_Pragma_Sloc --
1064 ----------------------------------
1066 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1067 begin
1068 Adjust_Instantiation_Sloc (N, S_Adjustment);
1069 end Adjust_Inherited_Pragma_Sloc;
1071 --------------------------
1072 -- Analyze_Associations --
1073 --------------------------
1075 function Analyze_Associations
1076 (I_Node : Node_Id;
1077 Formals : List_Id;
1078 F_Copy : List_Id) return List_Id
1080 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1081 Assoc_List : constant List_Id := New_List;
1082 Default_Actuals : constant List_Id := New_List;
1083 Gen_Unit : constant Entity_Id :=
1084 Defining_Entity (Parent (F_Copy));
1086 Actuals : List_Id;
1087 Actual : Node_Id;
1088 Analyzed_Formal : Node_Id;
1089 First_Named : Node_Id := Empty;
1090 Formal : Node_Id;
1091 Match : Node_Id;
1092 Named : Node_Id;
1093 Saved_Formal : Node_Id;
1095 Default_Formals : constant List_Id := New_List;
1096 -- If an Others_Choice is present, some of the formals may be defaulted.
1097 -- To simplify the treatment of visibility in an instance, we introduce
1098 -- individual defaults for each such formal. These defaults are
1099 -- appended to the list of associations and replace the Others_Choice.
1101 Found_Assoc : Node_Id;
1102 -- Association for the current formal being match. Empty if there are
1103 -- no remaining actuals, or if there is no named association with the
1104 -- name of the formal.
1106 Is_Named_Assoc : Boolean;
1107 Num_Matched : Nat := 0;
1108 Num_Actuals : Nat := 0;
1110 Others_Present : Boolean := False;
1111 Others_Choice : Node_Id := Empty;
1112 -- In Ada 2005, indicates partial parameterization of a formal
1113 -- package. As usual an other association must be last in the list.
1115 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1116 -- Warn if an actual fixed-point type has user-defined arithmetic
1117 -- operations, but there is no corresponding formal in the generic,
1118 -- in which case the predefined operations will be used. This merits
1119 -- a warning because of the special semantics of fixed point ops.
1121 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1122 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1123 -- cannot have a named association for it. AI05-0025 extends this rule
1124 -- to formals of formal packages by AI05-0025, and it also applies to
1125 -- box-initialized formals.
1127 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1128 -- Determine whether the parameter types and the return type of Subp
1129 -- are fully defined at the point of instantiation.
1131 function Matching_Actual
1132 (F : Entity_Id;
1133 A_F : Entity_Id) return Node_Id;
1134 -- Find actual that corresponds to a given a formal parameter. If the
1135 -- actuals are positional, return the next one, if any. If the actuals
1136 -- are named, scan the parameter associations to find the right one.
1137 -- A_F is the corresponding entity in the analyzed generic, which is
1138 -- placed on the selector name for ASIS use.
1140 -- In Ada 2005, a named association may be given with a box, in which
1141 -- case Matching_Actual sets Found_Assoc to the generic association,
1142 -- but return Empty for the actual itself. In this case the code below
1143 -- creates a corresponding declaration for the formal.
1145 function Partial_Parameterization return Boolean;
1146 -- Ada 2005: if no match is found for a given formal, check if the
1147 -- association for it includes a box, or whether the associations
1148 -- include an Others clause.
1150 procedure Process_Default (F : Entity_Id);
1151 -- Add a copy of the declaration of generic formal F to the list of
1152 -- associations, and add an explicit box association for F if there
1153 -- is none yet, and the default comes from an Others_Choice.
1155 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1156 -- Determine whether Subp renames one of the subprograms defined in the
1157 -- generated package Standard.
1159 procedure Set_Analyzed_Formal;
1160 -- Find the node in the generic copy that corresponds to a given formal.
1161 -- The semantic information on this node is used to perform legality
1162 -- checks on the actuals. Because semantic analysis can introduce some
1163 -- anonymous entities or modify the declaration node itself, the
1164 -- correspondence between the two lists is not one-one. In addition to
1165 -- anonymous types, the presence a formal equality will introduce an
1166 -- implicit declaration for the corresponding inequality.
1168 ----------------------------------------
1169 -- Check_Overloaded_Formal_Subprogram --
1170 ----------------------------------------
1172 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1173 Temp_Formal : Entity_Id;
1175 begin
1176 Temp_Formal := First (Formals);
1177 while Present (Temp_Formal) loop
1178 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1179 and then Temp_Formal /= Formal
1180 and then
1181 Chars (Defining_Unit_Name (Specification (Formal))) =
1182 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1183 then
1184 if Present (Found_Assoc) then
1185 Error_Msg_N
1186 ("named association not allowed for overloaded formal",
1187 Found_Assoc);
1189 else
1190 Error_Msg_N
1191 ("named association not allowed for overloaded formal",
1192 Others_Choice);
1193 end if;
1195 Abandon_Instantiation (Instantiation_Node);
1196 end if;
1198 Next (Temp_Formal);
1199 end loop;
1200 end Check_Overloaded_Formal_Subprogram;
1202 -------------------------------
1203 -- Check_Fixed_Point_Actual --
1204 -------------------------------
1206 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1207 Typ : constant Entity_Id := Entity (Actual);
1208 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1209 Elem : Elmt_Id;
1210 Formal : Node_Id;
1211 Op : Entity_Id;
1213 begin
1214 -- Locate primitive operations of the type that are arithmetic
1215 -- operations.
1217 Elem := First_Elmt (Prims);
1218 while Present (Elem) loop
1219 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1221 -- Check whether the generic unit has a formal subprogram of
1222 -- the same name. This does not check types but is good enough
1223 -- to justify a warning.
1225 Formal := First_Non_Pragma (Formals);
1226 Op := Alias (Node (Elem));
1228 while Present (Formal) loop
1229 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1230 and then Chars (Defining_Entity (Formal)) =
1231 Chars (Node (Elem))
1232 then
1233 exit;
1235 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1236 declare
1237 Assoc : Node_Id;
1238 Ent : Entity_Id;
1240 begin
1241 -- Locate corresponding actual, and check whether it
1242 -- includes a fixed-point type.
1244 Assoc := First (Assoc_List);
1245 while Present (Assoc) loop
1246 exit when
1247 Nkind (Assoc) = N_Package_Renaming_Declaration
1248 and then Chars (Defining_Unit_Name (Assoc)) =
1249 Chars (Defining_Identifier (Formal));
1251 Next (Assoc);
1252 end loop;
1254 if Present (Assoc) then
1256 -- If formal package declares a fixed-point type,
1257 -- and the user-defined operator is derived from
1258 -- a generic instance package, the fixed-point type
1259 -- does not use the corresponding predefined op.
1261 Ent := First_Entity (Entity (Name (Assoc)));
1262 while Present (Ent) loop
1263 if Is_Fixed_Point_Type (Ent)
1264 and then Present (Op)
1265 and then Is_Generic_Instance (Scope (Op))
1266 then
1267 return;
1268 end if;
1270 Next_Entity (Ent);
1271 end loop;
1272 end if;
1273 end;
1274 end if;
1276 Next (Formal);
1277 end loop;
1279 if No (Formal) then
1280 Error_Msg_Sloc := Sloc (Node (Elem));
1281 Error_Msg_NE
1282 ("?instance does not use primitive operation&#",
1283 Actual, Node (Elem));
1284 end if;
1285 end if;
1287 Next_Elmt (Elem);
1288 end loop;
1289 end Check_Fixed_Point_Actual;
1291 -------------------------------
1292 -- Has_Fully_Defined_Profile --
1293 -------------------------------
1295 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1296 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1297 -- Determine whethet type Typ is fully defined
1299 ---------------------------
1300 -- Is_Fully_Defined_Type --
1301 ---------------------------
1303 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1304 begin
1305 -- A private type without a full view is not fully defined
1307 if Is_Private_Type (Typ)
1308 and then No (Full_View (Typ))
1309 then
1310 return False;
1312 -- An incomplete type is never fully defined
1314 elsif Is_Incomplete_Type (Typ) then
1315 return False;
1317 -- All other types are fully defined
1319 else
1320 return True;
1321 end if;
1322 end Is_Fully_Defined_Type;
1324 -- Local declarations
1326 Param : Entity_Id;
1328 -- Start of processing for Has_Fully_Defined_Profile
1330 begin
1331 -- Check the parameters
1333 Param := First_Formal (Subp);
1334 while Present (Param) loop
1335 if not Is_Fully_Defined_Type (Etype (Param)) then
1336 return False;
1337 end if;
1339 Next_Formal (Param);
1340 end loop;
1342 -- Check the return type
1344 return Is_Fully_Defined_Type (Etype (Subp));
1345 end Has_Fully_Defined_Profile;
1347 ---------------------
1348 -- Matching_Actual --
1349 ---------------------
1351 function Matching_Actual
1352 (F : Entity_Id;
1353 A_F : Entity_Id) return Node_Id
1355 Prev : Node_Id;
1356 Act : Node_Id;
1358 begin
1359 Is_Named_Assoc := False;
1361 -- End of list of purely positional parameters
1363 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1364 Found_Assoc := Empty;
1365 Act := Empty;
1367 -- Case of positional parameter corresponding to current formal
1369 elsif No (Selector_Name (Actual)) then
1370 Found_Assoc := Actual;
1371 Act := Explicit_Generic_Actual_Parameter (Actual);
1372 Num_Matched := Num_Matched + 1;
1373 Next (Actual);
1375 -- Otherwise scan list of named actuals to find the one with the
1376 -- desired name. All remaining actuals have explicit names.
1378 else
1379 Is_Named_Assoc := True;
1380 Found_Assoc := Empty;
1381 Act := Empty;
1382 Prev := Empty;
1384 while Present (Actual) loop
1385 if Nkind (Actual) = N_Others_Choice then
1386 Found_Assoc := Empty;
1387 Act := Empty;
1389 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1390 Set_Entity (Selector_Name (Actual), A_F);
1391 Set_Etype (Selector_Name (Actual), Etype (A_F));
1392 Generate_Reference (A_F, Selector_Name (Actual));
1394 Found_Assoc := Actual;
1395 Act := Explicit_Generic_Actual_Parameter (Actual);
1396 Num_Matched := Num_Matched + 1;
1397 exit;
1398 end if;
1400 Prev := Actual;
1401 Next (Actual);
1402 end loop;
1404 -- Reset for subsequent searches. In most cases the named
1405 -- associations are in order. If they are not, we reorder them
1406 -- to avoid scanning twice the same actual. This is not just a
1407 -- question of efficiency: there may be multiple defaults with
1408 -- boxes that have the same name. In a nested instantiation we
1409 -- insert actuals for those defaults, and cannot rely on their
1410 -- names to disambiguate them.
1412 if Actual = First_Named then
1413 Next (First_Named);
1415 elsif Present (Actual) then
1416 Insert_Before (First_Named, Remove_Next (Prev));
1417 end if;
1419 Actual := First_Named;
1420 end if;
1422 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1423 Set_Used_As_Generic_Actual (Entity (Act));
1424 end if;
1426 return Act;
1427 end Matching_Actual;
1429 ------------------------------
1430 -- Partial_Parameterization --
1431 ------------------------------
1433 function Partial_Parameterization return Boolean is
1434 begin
1435 return Others_Present
1436 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1437 end Partial_Parameterization;
1439 ---------------------
1440 -- Process_Default --
1441 ---------------------
1443 procedure Process_Default (F : Entity_Id) is
1444 Loc : constant Source_Ptr := Sloc (I_Node);
1445 F_Id : constant Entity_Id := Defining_Entity (F);
1446 Decl : Node_Id;
1447 Default : Node_Id;
1448 Id : Entity_Id;
1450 begin
1451 -- Append copy of formal declaration to associations, and create new
1452 -- defining identifier for it.
1454 Decl := New_Copy_Tree (F);
1455 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1457 if Nkind (F) in N_Formal_Subprogram_Declaration then
1458 Set_Defining_Unit_Name (Specification (Decl), Id);
1460 else
1461 Set_Defining_Identifier (Decl, Id);
1462 end if;
1464 Append (Decl, Assoc_List);
1466 if No (Found_Assoc) then
1467 Default :=
1468 Make_Generic_Association (Loc,
1469 Selector_Name =>
1470 New_Occurrence_Of (Id, Loc),
1471 Explicit_Generic_Actual_Parameter => Empty);
1472 Set_Box_Present (Default);
1473 Append (Default, Default_Formals);
1474 end if;
1475 end Process_Default;
1477 ---------------------------------
1478 -- Renames_Standard_Subprogram --
1479 ---------------------------------
1481 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1482 Id : Entity_Id;
1484 begin
1485 Id := Alias (Subp);
1486 while Present (Id) loop
1487 if Scope (Id) = Standard_Standard then
1488 return True;
1489 end if;
1491 Id := Alias (Id);
1492 end loop;
1494 return False;
1495 end Renames_Standard_Subprogram;
1497 -------------------------
1498 -- Set_Analyzed_Formal --
1499 -------------------------
1501 procedure Set_Analyzed_Formal is
1502 Kind : Node_Kind;
1504 begin
1505 while Present (Analyzed_Formal) loop
1506 Kind := Nkind (Analyzed_Formal);
1508 case Nkind (Formal) is
1509 when N_Formal_Subprogram_Declaration =>
1510 exit when Kind in N_Formal_Subprogram_Declaration
1511 and then
1512 Chars
1513 (Defining_Unit_Name (Specification (Formal))) =
1514 Chars
1515 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1517 when N_Formal_Package_Declaration =>
1518 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1519 N_Generic_Package_Declaration,
1520 N_Package_Declaration);
1522 when N_Use_Package_Clause
1523 | N_Use_Type_Clause
1525 exit;
1527 when others =>
1529 -- Skip freeze nodes, and nodes inserted to replace
1530 -- unrecognized pragmas.
1532 exit when
1533 Kind not in N_Formal_Subprogram_Declaration
1534 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1535 N_Freeze_Entity,
1536 N_Null_Statement,
1537 N_Itype_Reference)
1538 and then Chars (Defining_Identifier (Formal)) =
1539 Chars (Defining_Identifier (Analyzed_Formal));
1540 end case;
1542 Next (Analyzed_Formal);
1543 end loop;
1544 end Set_Analyzed_Formal;
1546 -- Start of processing for Analyze_Associations
1548 begin
1549 Actuals := Generic_Associations (I_Node);
1551 if Present (Actuals) then
1553 -- Check for an Others choice, indicating a partial parameterization
1554 -- for a formal package.
1556 Actual := First (Actuals);
1557 while Present (Actual) loop
1558 if Nkind (Actual) = N_Others_Choice then
1559 Others_Present := True;
1560 Others_Choice := Actual;
1562 if Present (Next (Actual)) then
1563 Error_Msg_N ("others must be last association", Actual);
1564 end if;
1566 -- This subprogram is used both for formal packages and for
1567 -- instantiations. For the latter, associations must all be
1568 -- explicit.
1570 if Nkind (I_Node) /= N_Formal_Package_Declaration
1571 and then Comes_From_Source (I_Node)
1572 then
1573 Error_Msg_N
1574 ("others association not allowed in an instance",
1575 Actual);
1576 end if;
1578 -- In any case, nothing to do after the others association
1580 exit;
1582 elsif Box_Present (Actual)
1583 and then Comes_From_Source (I_Node)
1584 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1585 then
1586 Error_Msg_N
1587 ("box association not allowed in an instance", Actual);
1588 end if;
1590 Next (Actual);
1591 end loop;
1593 -- If named associations are present, save first named association
1594 -- (it may of course be Empty) to facilitate subsequent name search.
1596 First_Named := First (Actuals);
1597 while Present (First_Named)
1598 and then Nkind (First_Named) /= N_Others_Choice
1599 and then No (Selector_Name (First_Named))
1600 loop
1601 Num_Actuals := Num_Actuals + 1;
1602 Next (First_Named);
1603 end loop;
1604 end if;
1606 Named := First_Named;
1607 while Present (Named) loop
1608 if Nkind (Named) /= N_Others_Choice
1609 and then No (Selector_Name (Named))
1610 then
1611 Error_Msg_N ("invalid positional actual after named one", Named);
1612 Abandon_Instantiation (Named);
1613 end if;
1615 -- A named association may lack an actual parameter, if it was
1616 -- introduced for a default subprogram that turns out to be local
1617 -- to the outer instantiation. If it has a box association it must
1618 -- correspond to some formal in the generic.
1620 if Nkind (Named) /= N_Others_Choice
1621 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1622 or else Box_Present (Named))
1623 then
1624 Num_Actuals := Num_Actuals + 1;
1625 end if;
1627 Next (Named);
1628 end loop;
1630 if Present (Formals) then
1631 Formal := First_Non_Pragma (Formals);
1632 Analyzed_Formal := First_Non_Pragma (F_Copy);
1634 if Present (Actuals) then
1635 Actual := First (Actuals);
1637 -- All formals should have default values
1639 else
1640 Actual := Empty;
1641 end if;
1643 while Present (Formal) loop
1644 Set_Analyzed_Formal;
1645 Saved_Formal := Next_Non_Pragma (Formal);
1647 case Nkind (Formal) is
1648 when N_Formal_Object_Declaration =>
1649 Match :=
1650 Matching_Actual
1651 (Defining_Identifier (Formal),
1652 Defining_Identifier (Analyzed_Formal));
1654 if No (Match) and then Partial_Parameterization then
1655 Process_Default (Formal);
1657 else
1658 Append_List
1659 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1660 Assoc_List);
1662 -- For a defaulted in_parameter, create an entry in the
1663 -- the list of defaulted actuals, for GNATProve use. Do
1664 -- not included these defaults for an instance nested
1665 -- within a generic, because the defaults are also used
1666 -- in the analysis of the enclosing generic, and only
1667 -- defaulted subprograms are relevant there.
1669 if No (Match) and then not Inside_A_Generic then
1670 Append_To (Default_Actuals,
1671 Make_Generic_Association (Sloc (I_Node),
1672 Selector_Name =>
1673 New_Occurrence_Of
1674 (Defining_Identifier (Formal), Sloc (I_Node)),
1675 Explicit_Generic_Actual_Parameter =>
1676 New_Copy_Tree (Default_Expression (Formal))));
1677 end if;
1678 end if;
1680 -- If the object is a call to an expression function, this
1681 -- is a freezing point for it.
1683 if Is_Entity_Name (Match)
1684 and then Present (Entity (Match))
1685 and then Nkind
1686 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1687 = N_Expression_Function
1688 then
1689 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1690 end if;
1692 when N_Formal_Type_Declaration =>
1693 Match :=
1694 Matching_Actual
1695 (Defining_Identifier (Formal),
1696 Defining_Identifier (Analyzed_Formal));
1698 if No (Match) then
1699 if Partial_Parameterization then
1700 Process_Default (Formal);
1702 else
1703 Error_Msg_Sloc := Sloc (Gen_Unit);
1704 Error_Msg_NE
1705 ("missing actual&",
1706 Instantiation_Node, Defining_Identifier (Formal));
1707 Error_Msg_NE
1708 ("\in instantiation of & declared#",
1709 Instantiation_Node, Gen_Unit);
1710 Abandon_Instantiation (Instantiation_Node);
1711 end if;
1713 else
1714 Analyze (Match);
1715 Append_List
1716 (Instantiate_Type
1717 (Formal, Match, Analyzed_Formal, Assoc_List),
1718 Assoc_List);
1720 if Is_Fixed_Point_Type (Entity (Match)) then
1721 Check_Fixed_Point_Actual (Match);
1722 end if;
1724 -- An instantiation is a freeze point for the actuals,
1725 -- unless this is a rewritten formal package, or the
1726 -- formal is an Ada 2012 formal incomplete type.
1728 if Nkind (I_Node) = N_Formal_Package_Declaration
1729 or else
1730 (Ada_Version >= Ada_2012
1731 and then
1732 Ekind (Defining_Identifier (Analyzed_Formal)) =
1733 E_Incomplete_Type)
1734 then
1735 null;
1737 else
1738 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1739 end if;
1740 end if;
1742 -- A remote access-to-class-wide type is not a legal actual
1743 -- for a generic formal of an access type (E.2.2(17/2)).
1744 -- In GNAT an exception to this rule is introduced when
1745 -- the formal is marked as remote using implementation
1746 -- defined aspect/pragma Remote_Access_Type. In that case
1747 -- the actual must be remote as well.
1749 -- If the current instantiation is the construction of a
1750 -- local copy for a formal package the actuals may be
1751 -- defaulted, and there is no matching actual to check.
1753 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1754 and then
1755 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1756 N_Access_To_Object_Definition
1757 and then Present (Match)
1758 then
1759 declare
1760 Formal_Ent : constant Entity_Id :=
1761 Defining_Identifier (Analyzed_Formal);
1762 begin
1763 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1764 = Is_Remote_Types (Formal_Ent)
1765 then
1766 -- Remoteness of formal and actual match
1768 null;
1770 elsif Is_Remote_Types (Formal_Ent) then
1772 -- Remote formal, non-remote actual
1774 Error_Msg_NE
1775 ("actual for& must be remote", Match, Formal_Ent);
1777 else
1778 -- Non-remote formal, remote actual
1780 Error_Msg_NE
1781 ("actual for& may not be remote",
1782 Match, Formal_Ent);
1783 end if;
1784 end;
1785 end if;
1787 when N_Formal_Subprogram_Declaration =>
1788 Match :=
1789 Matching_Actual
1790 (Defining_Unit_Name (Specification (Formal)),
1791 Defining_Unit_Name (Specification (Analyzed_Formal)));
1793 -- If the formal subprogram has the same name as another
1794 -- formal subprogram of the generic, then a named
1795 -- association is illegal (12.3(9)). Exclude named
1796 -- associations that are generated for a nested instance.
1798 if Present (Match)
1799 and then Is_Named_Assoc
1800 and then Comes_From_Source (Found_Assoc)
1801 then
1802 Check_Overloaded_Formal_Subprogram (Formal);
1803 end if;
1805 -- If there is no corresponding actual, this may be case
1806 -- of partial parameterization, or else the formal has a
1807 -- default or a box.
1809 if No (Match) and then Partial_Parameterization then
1810 Process_Default (Formal);
1812 if Nkind (I_Node) = N_Formal_Package_Declaration then
1813 Check_Overloaded_Formal_Subprogram (Formal);
1814 end if;
1816 else
1817 Append_To (Assoc_List,
1818 Instantiate_Formal_Subprogram
1819 (Formal, Match, Analyzed_Formal));
1821 -- An instantiation is a freeze point for the actuals,
1822 -- unless this is a rewritten formal package.
1824 if Nkind (I_Node) /= N_Formal_Package_Declaration
1825 and then Nkind (Match) = N_Identifier
1826 and then Is_Subprogram (Entity (Match))
1828 -- The actual subprogram may rename a routine defined
1829 -- in Standard. Avoid freezing such renamings because
1830 -- subprograms coming from Standard cannot be frozen.
1832 and then
1833 not Renames_Standard_Subprogram (Entity (Match))
1835 -- If the actual subprogram comes from a different
1836 -- unit, it is already frozen, either by a body in
1837 -- that unit or by the end of the declarative part
1838 -- of the unit. This check avoids the freezing of
1839 -- subprograms defined in Standard which are used
1840 -- as generic actuals.
1842 and then In_Same_Code_Unit (Entity (Match), I_Node)
1843 and then Has_Fully_Defined_Profile (Entity (Match))
1844 then
1845 -- Mark the subprogram as having a delayed freeze
1846 -- since this may be an out-of-order action.
1848 Set_Has_Delayed_Freeze (Entity (Match));
1849 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1850 end if;
1851 end if;
1853 -- If this is a nested generic, preserve default for later
1854 -- instantiations. We do this as well for GNATProve use,
1855 -- so that the list of generic associations is complete.
1857 if No (Match) and then Box_Present (Formal) then
1858 declare
1859 Subp : constant Entity_Id :=
1860 Defining_Unit_Name
1861 (Specification (Last (Assoc_List)));
1863 begin
1864 Append_To (Default_Actuals,
1865 Make_Generic_Association (Sloc (I_Node),
1866 Selector_Name =>
1867 New_Occurrence_Of (Subp, Sloc (I_Node)),
1868 Explicit_Generic_Actual_Parameter =>
1869 New_Occurrence_Of (Subp, Sloc (I_Node))));
1870 end;
1871 end if;
1873 when N_Formal_Package_Declaration =>
1874 Match :=
1875 Matching_Actual
1876 (Defining_Identifier (Formal),
1877 Defining_Identifier (Original_Node (Analyzed_Formal)));
1879 if No (Match) then
1880 if Partial_Parameterization then
1881 Process_Default (Formal);
1883 else
1884 Error_Msg_Sloc := Sloc (Gen_Unit);
1885 Error_Msg_NE
1886 ("missing actual&",
1887 Instantiation_Node, Defining_Identifier (Formal));
1888 Error_Msg_NE
1889 ("\in instantiation of & declared#",
1890 Instantiation_Node, Gen_Unit);
1892 Abandon_Instantiation (Instantiation_Node);
1893 end if;
1895 else
1896 Analyze (Match);
1897 Append_List
1898 (Instantiate_Formal_Package
1899 (Formal, Match, Analyzed_Formal),
1900 Assoc_List);
1902 -- Determine whether the actual package needs an explicit
1903 -- freeze node. This is only the case if the actual is
1904 -- declared in the same unit and has a body. Normally
1905 -- packages do not have explicit freeze nodes, and gigi
1906 -- only uses them to elaborate entities in a package
1907 -- body.
1909 Explicit_Freeze_Check : declare
1910 Actual : constant Entity_Id := Entity (Match);
1911 Gen_Par : Entity_Id;
1913 Needs_Freezing : Boolean;
1914 S : Entity_Id;
1916 procedure Check_Generic_Parent;
1917 -- The actual may be an instantiation of a unit
1918 -- declared in a previous instantiation. If that
1919 -- one is also in the current compilation, it must
1920 -- itself be frozen before the actual. The actual
1921 -- may be an instantiation of a generic child unit,
1922 -- in which case the same applies to the instance
1923 -- of the parent which must be frozen before the
1924 -- actual.
1925 -- Should this itself be recursive ???
1927 --------------------------
1928 -- Check_Generic_Parent --
1929 --------------------------
1931 procedure Check_Generic_Parent is
1932 Inst : constant Node_Id :=
1933 Next (Unit_Declaration_Node (Actual));
1934 Par : Entity_Id;
1936 begin
1937 Par := Empty;
1939 if Nkind (Parent (Actual)) = N_Package_Specification
1940 then
1941 Par := Scope (Generic_Parent (Parent (Actual)));
1943 if Is_Generic_Instance (Par) then
1944 null;
1946 -- If the actual is a child generic unit, check
1947 -- whether the instantiation of the parent is
1948 -- also local and must also be frozen now. We
1949 -- must retrieve the instance node to locate the
1950 -- parent instance if any.
1952 elsif Ekind (Par) = E_Generic_Package
1953 and then Is_Child_Unit (Gen_Par)
1954 and then Ekind (Scope (Gen_Par)) =
1955 E_Generic_Package
1956 then
1957 if Nkind (Inst) = N_Package_Instantiation
1958 and then Nkind (Name (Inst)) =
1959 N_Expanded_Name
1960 then
1961 -- Retrieve entity of parent instance
1963 Par := Entity (Prefix (Name (Inst)));
1964 end if;
1966 else
1967 Par := Empty;
1968 end if;
1969 end if;
1971 if Present (Par)
1972 and then Is_Generic_Instance (Par)
1973 and then Scope (Par) = Current_Scope
1974 and then
1975 (No (Freeze_Node (Par))
1976 or else
1977 not Is_List_Member (Freeze_Node (Par)))
1978 then
1979 Set_Has_Delayed_Freeze (Par);
1980 Append_Elmt (Par, Actuals_To_Freeze);
1981 end if;
1982 end Check_Generic_Parent;
1984 -- Start of processing for Explicit_Freeze_Check
1986 begin
1987 if Present (Renamed_Entity (Actual)) then
1988 Gen_Par :=
1989 Generic_Parent (Specification
1990 (Unit_Declaration_Node
1991 (Renamed_Entity (Actual))));
1992 else
1993 Gen_Par :=
1994 Generic_Parent (Specification
1995 (Unit_Declaration_Node (Actual)));
1996 end if;
1998 if not Expander_Active
1999 or else not Has_Completion (Actual)
2000 or else not In_Same_Source_Unit (I_Node, Actual)
2001 or else Is_Frozen (Actual)
2002 or else
2003 (Present (Renamed_Entity (Actual))
2004 and then
2005 not In_Same_Source_Unit
2006 (I_Node, (Renamed_Entity (Actual))))
2007 then
2008 null;
2010 else
2011 -- Finally we want to exclude such freeze nodes
2012 -- from statement sequences, which freeze
2013 -- everything before them.
2014 -- Is this strictly necessary ???
2016 Needs_Freezing := True;
2018 S := Current_Scope;
2019 while Present (S) loop
2020 if Ekind_In (S, E_Block,
2021 E_Function,
2022 E_Loop,
2023 E_Procedure)
2024 then
2025 Needs_Freezing := False;
2026 exit;
2027 end if;
2029 S := Scope (S);
2030 end loop;
2032 if Needs_Freezing then
2033 Check_Generic_Parent;
2035 -- If the actual is a renaming of a proper
2036 -- instance of the formal package, indicate
2037 -- that it is the instance that must be frozen.
2039 if Nkind (Parent (Actual)) =
2040 N_Package_Renaming_Declaration
2041 then
2042 Set_Has_Delayed_Freeze
2043 (Renamed_Entity (Actual));
2044 Append_Elmt
2045 (Renamed_Entity (Actual),
2046 Actuals_To_Freeze);
2047 else
2048 Set_Has_Delayed_Freeze (Actual);
2049 Append_Elmt (Actual, Actuals_To_Freeze);
2050 end if;
2051 end if;
2052 end if;
2053 end Explicit_Freeze_Check;
2054 end if;
2056 -- For use type and use package appearing in the generic part,
2057 -- we have already copied them, so we can just move them where
2058 -- they belong (we mustn't recopy them since this would mess up
2059 -- the Sloc values).
2061 when N_Use_Package_Clause
2062 | N_Use_Type_Clause
2064 if Nkind (Original_Node (I_Node)) =
2065 N_Formal_Package_Declaration
2066 then
2067 Append (New_Copy_Tree (Formal), Assoc_List);
2068 else
2069 Remove (Formal);
2070 Append (Formal, Assoc_List);
2071 end if;
2073 when others =>
2074 raise Program_Error;
2075 end case;
2077 Formal := Saved_Formal;
2078 Next_Non_Pragma (Analyzed_Formal);
2079 end loop;
2081 if Num_Actuals > Num_Matched then
2082 Error_Msg_Sloc := Sloc (Gen_Unit);
2084 if Present (Selector_Name (Actual)) then
2085 Error_Msg_NE
2086 ("unmatched actual &", Actual, Selector_Name (Actual));
2087 Error_Msg_NE
2088 ("\in instantiation of & declared#", Actual, Gen_Unit);
2089 else
2090 Error_Msg_NE
2091 ("unmatched actual in instantiation of & declared#",
2092 Actual, Gen_Unit);
2093 end if;
2094 end if;
2096 elsif Present (Actuals) then
2097 Error_Msg_N
2098 ("too many actuals in generic instantiation", Instantiation_Node);
2099 end if;
2101 -- An instantiation freezes all generic actuals. The only exceptions
2102 -- to this are incomplete types and subprograms which are not fully
2103 -- defined at the point of instantiation.
2105 declare
2106 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2107 begin
2108 while Present (Elmt) loop
2109 Freeze_Before (I_Node, Node (Elmt));
2110 Next_Elmt (Elmt);
2111 end loop;
2112 end;
2114 -- If there are default subprograms, normalize the tree by adding
2115 -- explicit associations for them. This is required if the instance
2116 -- appears within a generic.
2118 if not Is_Empty_List (Default_Actuals) then
2119 declare
2120 Default : Node_Id;
2122 begin
2123 Default := First (Default_Actuals);
2124 while Present (Default) loop
2125 Mark_Rewrite_Insertion (Default);
2126 Next (Default);
2127 end loop;
2129 if No (Actuals) then
2130 Set_Generic_Associations (I_Node, Default_Actuals);
2131 else
2132 Append_List_To (Actuals, Default_Actuals);
2133 end if;
2134 end;
2135 end if;
2137 -- If this is a formal package, normalize the parameter list by adding
2138 -- explicit box associations for the formals that are covered by an
2139 -- Others_Choice.
2141 if not Is_Empty_List (Default_Formals) then
2142 Append_List (Default_Formals, Formals);
2143 end if;
2145 return Assoc_List;
2146 end Analyze_Associations;
2148 -------------------------------
2149 -- Analyze_Formal_Array_Type --
2150 -------------------------------
2152 procedure Analyze_Formal_Array_Type
2153 (T : in out Entity_Id;
2154 Def : Node_Id)
2156 DSS : Node_Id;
2158 begin
2159 -- Treated like a non-generic array declaration, with additional
2160 -- semantic checks.
2162 Enter_Name (T);
2164 if Nkind (Def) = N_Constrained_Array_Definition then
2165 DSS := First (Discrete_Subtype_Definitions (Def));
2166 while Present (DSS) loop
2167 if Nkind_In (DSS, N_Subtype_Indication,
2168 N_Range,
2169 N_Attribute_Reference)
2170 then
2171 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2172 end if;
2174 Next (DSS);
2175 end loop;
2176 end if;
2178 Array_Type_Declaration (T, Def);
2179 Set_Is_Generic_Type (Base_Type (T));
2181 if Ekind (Component_Type (T)) = E_Incomplete_Type
2182 and then No (Full_View (Component_Type (T)))
2183 then
2184 Error_Msg_N ("premature usage of incomplete type", Def);
2186 -- Check that range constraint is not allowed on the component type
2187 -- of a generic formal array type (AARM 12.5.3(3))
2189 elsif Is_Internal (Component_Type (T))
2190 and then Present (Subtype_Indication (Component_Definition (Def)))
2191 and then Nkind (Original_Node
2192 (Subtype_Indication (Component_Definition (Def)))) =
2193 N_Subtype_Indication
2194 then
2195 Error_Msg_N
2196 ("in a formal, a subtype indication can only be "
2197 & "a subtype mark (RM 12.5.3(3))",
2198 Subtype_Indication (Component_Definition (Def)));
2199 end if;
2201 end Analyze_Formal_Array_Type;
2203 ---------------------------------------------
2204 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2205 ---------------------------------------------
2207 -- As for other generic types, we create a valid type representation with
2208 -- legal but arbitrary attributes, whose values are never considered
2209 -- static. For all scalar types we introduce an anonymous base type, with
2210 -- the same attributes. We choose the corresponding integer type to be
2211 -- Standard_Integer.
2212 -- Here and in other similar routines, the Sloc of the generated internal
2213 -- type must be the same as the sloc of the defining identifier of the
2214 -- formal type declaration, to provide proper source navigation.
2216 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2217 (T : Entity_Id;
2218 Def : Node_Id)
2220 Loc : constant Source_Ptr := Sloc (Def);
2222 Base : constant Entity_Id :=
2223 New_Internal_Entity
2224 (E_Decimal_Fixed_Point_Type,
2225 Current_Scope,
2226 Sloc (Defining_Identifier (Parent (Def))), 'G');
2228 Int_Base : constant Entity_Id := Standard_Integer;
2229 Delta_Val : constant Ureal := Ureal_1;
2230 Digs_Val : constant Uint := Uint_6;
2232 function Make_Dummy_Bound return Node_Id;
2233 -- Return a properly typed universal real literal to use as a bound
2235 ----------------------
2236 -- Make_Dummy_Bound --
2237 ----------------------
2239 function Make_Dummy_Bound return Node_Id is
2240 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2241 begin
2242 Set_Etype (Bound, Universal_Real);
2243 return Bound;
2244 end Make_Dummy_Bound;
2246 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2248 begin
2249 Enter_Name (T);
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, Int_Base);
2253 Set_RM_Size (Base, RM_Size (Int_Base));
2254 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2255 Set_Digits_Value (Base, Digs_Val);
2256 Set_Delta_Value (Base, Delta_Val);
2257 Set_Small_Value (Base, Delta_Val);
2258 Set_Scalar_Range (Base,
2259 Make_Range (Loc,
2260 Low_Bound => Make_Dummy_Bound,
2261 High_Bound => Make_Dummy_Bound));
2263 Set_Is_Generic_Type (Base);
2264 Set_Parent (Base, Parent (Def));
2266 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2267 Set_Etype (T, Base);
2268 Set_Size_Info (T, Int_Base);
2269 Set_RM_Size (T, RM_Size (Int_Base));
2270 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2271 Set_Digits_Value (T, Digs_Val);
2272 Set_Delta_Value (T, Delta_Val);
2273 Set_Small_Value (T, Delta_Val);
2274 Set_Scalar_Range (T, Scalar_Range (Base));
2275 Set_Is_Constrained (T);
2277 Check_Restriction (No_Fixed_Point, Def);
2278 end Analyze_Formal_Decimal_Fixed_Point_Type;
2280 -------------------------------------------
2281 -- Analyze_Formal_Derived_Interface_Type --
2282 -------------------------------------------
2284 procedure Analyze_Formal_Derived_Interface_Type
2285 (N : Node_Id;
2286 T : Entity_Id;
2287 Def : Node_Id)
2289 Loc : constant Source_Ptr := Sloc (Def);
2291 begin
2292 -- Rewrite as a type declaration of a derived type. This ensures that
2293 -- the interface list and primitive operations are properly captured.
2295 Rewrite (N,
2296 Make_Full_Type_Declaration (Loc,
2297 Defining_Identifier => T,
2298 Type_Definition => Def));
2299 Analyze (N);
2300 Set_Is_Generic_Type (T);
2301 end Analyze_Formal_Derived_Interface_Type;
2303 ---------------------------------
2304 -- Analyze_Formal_Derived_Type --
2305 ---------------------------------
2307 procedure Analyze_Formal_Derived_Type
2308 (N : Node_Id;
2309 T : Entity_Id;
2310 Def : Node_Id)
2312 Loc : constant Source_Ptr := Sloc (Def);
2313 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2314 New_N : Node_Id;
2316 begin
2317 Set_Is_Generic_Type (T);
2319 if Private_Present (Def) then
2320 New_N :=
2321 Make_Private_Extension_Declaration (Loc,
2322 Defining_Identifier => T,
2323 Discriminant_Specifications => Discriminant_Specifications (N),
2324 Unknown_Discriminants_Present => Unk_Disc,
2325 Subtype_Indication => Subtype_Mark (Def),
2326 Interface_List => Interface_List (Def));
2328 Set_Abstract_Present (New_N, Abstract_Present (Def));
2329 Set_Limited_Present (New_N, Limited_Present (Def));
2330 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2332 else
2333 New_N :=
2334 Make_Full_Type_Declaration (Loc,
2335 Defining_Identifier => T,
2336 Discriminant_Specifications =>
2337 Discriminant_Specifications (Parent (T)),
2338 Type_Definition =>
2339 Make_Derived_Type_Definition (Loc,
2340 Subtype_Indication => Subtype_Mark (Def)));
2342 Set_Abstract_Present
2343 (Type_Definition (New_N), Abstract_Present (Def));
2344 Set_Limited_Present
2345 (Type_Definition (New_N), Limited_Present (Def));
2346 end if;
2348 Rewrite (N, New_N);
2349 Analyze (N);
2351 if Unk_Disc then
2352 if not Is_Composite_Type (T) then
2353 Error_Msg_N
2354 ("unknown discriminants not allowed for elementary types", N);
2355 else
2356 Set_Has_Unknown_Discriminants (T);
2357 Set_Is_Constrained (T, False);
2358 end if;
2359 end if;
2361 -- If the parent type has a known size, so does the formal, which makes
2362 -- legal representation clauses that involve the formal.
2364 Set_Size_Known_At_Compile_Time
2365 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2366 end Analyze_Formal_Derived_Type;
2368 ----------------------------------
2369 -- Analyze_Formal_Discrete_Type --
2370 ----------------------------------
2372 -- The operations defined for a discrete types are those of an enumeration
2373 -- type. The size is set to an arbitrary value, for use in analyzing the
2374 -- generic unit.
2376 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2377 Loc : constant Source_Ptr := Sloc (Def);
2378 Lo : Node_Id;
2379 Hi : Node_Id;
2381 Base : constant Entity_Id :=
2382 New_Internal_Entity
2383 (E_Floating_Point_Type, Current_Scope,
2384 Sloc (Defining_Identifier (Parent (Def))), 'G');
2386 begin
2387 Enter_Name (T);
2388 Set_Ekind (T, E_Enumeration_Subtype);
2389 Set_Etype (T, Base);
2390 Init_Size (T, 8);
2391 Init_Alignment (T);
2392 Set_Is_Generic_Type (T);
2393 Set_Is_Constrained (T);
2395 -- For semantic analysis, the bounds of the type must be set to some
2396 -- non-static value. The simplest is to create attribute nodes for those
2397 -- bounds, that refer to the type itself. These bounds are never
2398 -- analyzed but serve as place-holders.
2400 Lo :=
2401 Make_Attribute_Reference (Loc,
2402 Attribute_Name => Name_First,
2403 Prefix => New_Occurrence_Of (T, Loc));
2404 Set_Etype (Lo, T);
2406 Hi :=
2407 Make_Attribute_Reference (Loc,
2408 Attribute_Name => Name_Last,
2409 Prefix => New_Occurrence_Of (T, Loc));
2410 Set_Etype (Hi, T);
2412 Set_Scalar_Range (T,
2413 Make_Range (Loc,
2414 Low_Bound => Lo,
2415 High_Bound => Hi));
2417 Set_Ekind (Base, E_Enumeration_Type);
2418 Set_Etype (Base, Base);
2419 Init_Size (Base, 8);
2420 Init_Alignment (Base);
2421 Set_Is_Generic_Type (Base);
2422 Set_Scalar_Range (Base, Scalar_Range (T));
2423 Set_Parent (Base, Parent (Def));
2424 end Analyze_Formal_Discrete_Type;
2426 ----------------------------------
2427 -- Analyze_Formal_Floating_Type --
2428 ---------------------------------
2430 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2431 Base : constant Entity_Id :=
2432 New_Internal_Entity
2433 (E_Floating_Point_Type, Current_Scope,
2434 Sloc (Defining_Identifier (Parent (Def))), 'G');
2436 begin
2437 -- The various semantic attributes are taken from the predefined type
2438 -- Float, just so that all of them are initialized. Their values are
2439 -- never used because no constant folding or expansion takes place in
2440 -- the generic itself.
2442 Enter_Name (T);
2443 Set_Ekind (T, E_Floating_Point_Subtype);
2444 Set_Etype (T, Base);
2445 Set_Size_Info (T, (Standard_Float));
2446 Set_RM_Size (T, RM_Size (Standard_Float));
2447 Set_Digits_Value (T, Digits_Value (Standard_Float));
2448 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2449 Set_Is_Constrained (T);
2451 Set_Is_Generic_Type (Base);
2452 Set_Etype (Base, Base);
2453 Set_Size_Info (Base, (Standard_Float));
2454 Set_RM_Size (Base, RM_Size (Standard_Float));
2455 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2456 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2457 Set_Parent (Base, Parent (Def));
2459 Check_Restriction (No_Floating_Point, Def);
2460 end Analyze_Formal_Floating_Type;
2462 -----------------------------------
2463 -- Analyze_Formal_Interface_Type;--
2464 -----------------------------------
2466 procedure Analyze_Formal_Interface_Type
2467 (N : Node_Id;
2468 T : Entity_Id;
2469 Def : Node_Id)
2471 Loc : constant Source_Ptr := Sloc (N);
2472 New_N : Node_Id;
2474 begin
2475 New_N :=
2476 Make_Full_Type_Declaration (Loc,
2477 Defining_Identifier => T,
2478 Type_Definition => Def);
2480 Rewrite (N, New_N);
2481 Analyze (N);
2482 Set_Is_Generic_Type (T);
2483 end Analyze_Formal_Interface_Type;
2485 ---------------------------------
2486 -- Analyze_Formal_Modular_Type --
2487 ---------------------------------
2489 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2490 begin
2491 -- Apart from their entity kind, generic modular types are treated like
2492 -- signed integer types, and have the same attributes.
2494 Analyze_Formal_Signed_Integer_Type (T, Def);
2495 Set_Ekind (T, E_Modular_Integer_Subtype);
2496 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2498 end Analyze_Formal_Modular_Type;
2500 ---------------------------------------
2501 -- Analyze_Formal_Object_Declaration --
2502 ---------------------------------------
2504 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2505 E : constant Node_Id := Default_Expression (N);
2506 Id : constant Node_Id := Defining_Identifier (N);
2507 K : Entity_Kind;
2508 T : Node_Id;
2510 begin
2511 Enter_Name (Id);
2513 -- Determine the mode of the formal object
2515 if Out_Present (N) then
2516 K := E_Generic_In_Out_Parameter;
2518 if not In_Present (N) then
2519 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2520 end if;
2522 else
2523 K := E_Generic_In_Parameter;
2524 end if;
2526 if Present (Subtype_Mark (N)) then
2527 Find_Type (Subtype_Mark (N));
2528 T := Entity (Subtype_Mark (N));
2530 -- Verify that there is no redundant null exclusion
2532 if Null_Exclusion_Present (N) then
2533 if not Is_Access_Type (T) then
2534 Error_Msg_N
2535 ("null exclusion can only apply to an access type", N);
2537 elsif Can_Never_Be_Null (T) then
2538 Error_Msg_NE
2539 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2540 end if;
2541 end if;
2543 -- Ada 2005 (AI-423): Formal object with an access definition
2545 else
2546 Check_Access_Definition (N);
2547 T := Access_Definition
2548 (Related_Nod => N,
2549 N => Access_Definition (N));
2550 end if;
2552 if Ekind (T) = E_Incomplete_Type then
2553 declare
2554 Error_Node : Node_Id;
2556 begin
2557 if Present (Subtype_Mark (N)) then
2558 Error_Node := Subtype_Mark (N);
2559 else
2560 Check_Access_Definition (N);
2561 Error_Node := Access_Definition (N);
2562 end if;
2564 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2565 end;
2566 end if;
2568 if K = E_Generic_In_Parameter then
2570 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2572 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2573 Error_Msg_N
2574 ("generic formal of mode IN must not be of limited type", N);
2575 Explain_Limited_Type (T, N);
2576 end if;
2578 if Is_Abstract_Type (T) then
2579 Error_Msg_N
2580 ("generic formal of mode IN must not be of abstract type", N);
2581 end if;
2583 if Present (E) then
2584 Preanalyze_Spec_Expression (E, T);
2586 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2587 Error_Msg_N
2588 ("initialization not allowed for limited types", E);
2589 Explain_Limited_Type (T, E);
2590 end if;
2591 end if;
2593 Set_Ekind (Id, K);
2594 Set_Etype (Id, T);
2596 -- Case of generic IN OUT parameter
2598 else
2599 -- If the formal has an unconstrained type, construct its actual
2600 -- subtype, as is done for subprogram formals. In this fashion, all
2601 -- its uses can refer to specific bounds.
2603 Set_Ekind (Id, K);
2604 Set_Etype (Id, T);
2606 if (Is_Array_Type (T) and then not Is_Constrained (T))
2607 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2608 then
2609 declare
2610 Non_Freezing_Ref : constant Node_Id :=
2611 New_Occurrence_Of (Id, Sloc (Id));
2612 Decl : Node_Id;
2614 begin
2615 -- Make sure the actual subtype doesn't generate bogus freezing
2617 Set_Must_Not_Freeze (Non_Freezing_Ref);
2618 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2619 Insert_Before_And_Analyze (N, Decl);
2620 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2621 end;
2622 else
2623 Set_Actual_Subtype (Id, T);
2624 end if;
2626 if Present (E) then
2627 Error_Msg_N
2628 ("initialization not allowed for `IN OUT` formals", N);
2629 end if;
2630 end if;
2632 if Has_Aspects (N) then
2633 Analyze_Aspect_Specifications (N, Id);
2634 end if;
2635 end Analyze_Formal_Object_Declaration;
2637 ----------------------------------------------
2638 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2639 ----------------------------------------------
2641 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2642 (T : Entity_Id;
2643 Def : Node_Id)
2645 Loc : constant Source_Ptr := Sloc (Def);
2646 Base : constant Entity_Id :=
2647 New_Internal_Entity
2648 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2649 Sloc (Defining_Identifier (Parent (Def))), 'G');
2651 begin
2652 -- The semantic attributes are set for completeness only, their values
2653 -- will never be used, since all properties of the type are non-static.
2655 Enter_Name (T);
2656 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2657 Set_Etype (T, Base);
2658 Set_Size_Info (T, Standard_Integer);
2659 Set_RM_Size (T, RM_Size (Standard_Integer));
2660 Set_Small_Value (T, Ureal_1);
2661 Set_Delta_Value (T, Ureal_1);
2662 Set_Scalar_Range (T,
2663 Make_Range (Loc,
2664 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2665 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2666 Set_Is_Constrained (T);
2668 Set_Is_Generic_Type (Base);
2669 Set_Etype (Base, Base);
2670 Set_Size_Info (Base, Standard_Integer);
2671 Set_RM_Size (Base, RM_Size (Standard_Integer));
2672 Set_Small_Value (Base, Ureal_1);
2673 Set_Delta_Value (Base, Ureal_1);
2674 Set_Scalar_Range (Base, Scalar_Range (T));
2675 Set_Parent (Base, Parent (Def));
2677 Check_Restriction (No_Fixed_Point, Def);
2678 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2680 ----------------------------------------
2681 -- Analyze_Formal_Package_Declaration --
2682 ----------------------------------------
2684 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2685 Gen_Id : constant Node_Id := Name (N);
2686 Loc : constant Source_Ptr := Sloc (N);
2687 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2688 Formal : Entity_Id;
2689 Gen_Decl : Node_Id;
2690 Gen_Unit : Entity_Id;
2691 Renaming : Node_Id;
2693 Vis_Prims_List : Elist_Id := No_Elist;
2694 -- List of primitives made temporarily visible in the instantiation
2695 -- to match the visibility of the formal type.
2697 function Build_Local_Package return Node_Id;
2698 -- The formal package is rewritten so that its parameters are replaced
2699 -- with corresponding declarations. For parameters with bona fide
2700 -- associations these declarations are created by Analyze_Associations
2701 -- as for a regular instantiation. For boxed parameters, we preserve
2702 -- the formal declarations and analyze them, in order to introduce
2703 -- entities of the right kind in the environment of the formal.
2705 -------------------------
2706 -- Build_Local_Package --
2707 -------------------------
2709 function Build_Local_Package return Node_Id is
2710 Decls : List_Id;
2711 Pack_Decl : Node_Id;
2713 begin
2714 -- Within the formal, the name of the generic package is a renaming
2715 -- of the formal (as for a regular instantiation).
2717 Pack_Decl :=
2718 Make_Package_Declaration (Loc,
2719 Specification =>
2720 Copy_Generic_Node
2721 (Specification (Original_Node (Gen_Decl)),
2722 Empty, Instantiating => True));
2724 Renaming :=
2725 Make_Package_Renaming_Declaration (Loc,
2726 Defining_Unit_Name =>
2727 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2728 Name => New_Occurrence_Of (Formal, Loc));
2730 if Nkind (Gen_Id) = N_Identifier
2731 and then Chars (Gen_Id) = Chars (Pack_Id)
2732 then
2733 Error_Msg_NE
2734 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2735 end if;
2737 -- If the formal is declared with a box, or with an others choice,
2738 -- create corresponding declarations for all entities in the formal
2739 -- part, so that names with the proper types are available in the
2740 -- specification of the formal package.
2742 -- On the other hand, if there are no associations, then all the
2743 -- formals must have defaults, and this will be checked by the
2744 -- call to Analyze_Associations.
2746 if Box_Present (N)
2747 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2748 then
2749 declare
2750 Formal_Decl : Node_Id;
2752 begin
2753 -- TBA : for a formal package, need to recurse ???
2755 Decls := New_List;
2756 Formal_Decl :=
2757 First
2758 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2759 while Present (Formal_Decl) loop
2760 Append_To
2761 (Decls,
2762 Copy_Generic_Node
2763 (Formal_Decl, Empty, Instantiating => True));
2764 Next (Formal_Decl);
2765 end loop;
2766 end;
2768 -- If generic associations are present, use Analyze_Associations to
2769 -- create the proper renaming declarations.
2771 else
2772 declare
2773 Act_Tree : constant Node_Id :=
2774 Copy_Generic_Node
2775 (Original_Node (Gen_Decl), Empty,
2776 Instantiating => True);
2778 begin
2779 Generic_Renamings.Set_Last (0);
2780 Generic_Renamings_HTable.Reset;
2781 Instantiation_Node := N;
2783 Decls :=
2784 Analyze_Associations
2785 (I_Node => Original_Node (N),
2786 Formals => Generic_Formal_Declarations (Act_Tree),
2787 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2789 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2790 end;
2791 end if;
2793 Append (Renaming, To => Decls);
2795 -- Add generated declarations ahead of local declarations in
2796 -- the package.
2798 if No (Visible_Declarations (Specification (Pack_Decl))) then
2799 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2800 else
2801 Insert_List_Before
2802 (First (Visible_Declarations (Specification (Pack_Decl))),
2803 Decls);
2804 end if;
2806 return Pack_Decl;
2807 end Build_Local_Package;
2809 -- Local variables
2811 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2812 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2814 Associations : Boolean := True;
2815 New_N : Node_Id;
2816 Parent_Installed : Boolean := False;
2817 Parent_Instance : Entity_Id;
2818 Renaming_In_Par : Entity_Id;
2820 -- Start of processing for Analyze_Formal_Package_Declaration
2822 begin
2823 Check_Text_IO_Special_Unit (Gen_Id);
2825 Init_Env;
2826 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2827 Gen_Unit := Entity (Gen_Id);
2829 -- Check for a formal package that is a package renaming
2831 if Present (Renamed_Object (Gen_Unit)) then
2833 -- Indicate that unit is used, before replacing it with renamed
2834 -- entity for use below.
2836 if In_Extended_Main_Source_Unit (N) then
2837 Set_Is_Instantiated (Gen_Unit);
2838 Generate_Reference (Gen_Unit, N);
2839 end if;
2841 Gen_Unit := Renamed_Object (Gen_Unit);
2842 end if;
2844 if Ekind (Gen_Unit) /= E_Generic_Package then
2845 Error_Msg_N ("expect generic package name", Gen_Id);
2846 Restore_Env;
2847 goto Leave;
2849 elsif Gen_Unit = Current_Scope then
2850 Error_Msg_N
2851 ("generic package cannot be used as a formal package of itself",
2852 Gen_Id);
2853 Restore_Env;
2854 goto Leave;
2856 elsif In_Open_Scopes (Gen_Unit) then
2857 if Is_Compilation_Unit (Gen_Unit)
2858 and then Is_Child_Unit (Current_Scope)
2859 then
2860 -- Special-case the error when the formal is a parent, and
2861 -- continue analysis to minimize cascaded errors.
2863 Error_Msg_N
2864 ("generic parent cannot be used as formal package of a child "
2865 & "unit", Gen_Id);
2867 else
2868 Error_Msg_N
2869 ("generic package cannot be used as a formal package within "
2870 & "itself", Gen_Id);
2871 Restore_Env;
2872 goto Leave;
2873 end if;
2874 end if;
2876 -- Check that name of formal package does not hide name of generic,
2877 -- or its leading prefix. This check must be done separately because
2878 -- the name of the generic has already been analyzed.
2880 declare
2881 Gen_Name : Entity_Id;
2883 begin
2884 Gen_Name := Gen_Id;
2885 while Nkind (Gen_Name) = N_Expanded_Name loop
2886 Gen_Name := Prefix (Gen_Name);
2887 end loop;
2889 if Chars (Gen_Name) = Chars (Pack_Id) then
2890 Error_Msg_NE
2891 ("& is hidden within declaration of formal package",
2892 Gen_Id, Gen_Name);
2893 end if;
2894 end;
2896 if Box_Present (N)
2897 or else No (Generic_Associations (N))
2898 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2899 then
2900 Associations := False;
2901 end if;
2903 -- If there are no generic associations, the generic parameters appear
2904 -- as local entities and are instantiated like them. We copy the generic
2905 -- package declaration as if it were an instantiation, and analyze it
2906 -- like a regular package, except that we treat the formals as
2907 -- additional visible components.
2909 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2911 if In_Extended_Main_Source_Unit (N) then
2912 Set_Is_Instantiated (Gen_Unit);
2913 Generate_Reference (Gen_Unit, N);
2914 end if;
2916 Formal := New_Copy (Pack_Id);
2917 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2919 -- Make local generic without formals. The formals will be replaced with
2920 -- internal declarations.
2922 begin
2923 New_N := Build_Local_Package;
2925 -- If there are errors in the parameter list, Analyze_Associations
2926 -- raises Instantiation_Error. Patch the declaration to prevent further
2927 -- exception propagation.
2929 exception
2930 when Instantiation_Error =>
2931 Enter_Name (Formal);
2932 Set_Ekind (Formal, E_Variable);
2933 Set_Etype (Formal, Any_Type);
2934 Restore_Hidden_Primitives (Vis_Prims_List);
2936 if Parent_Installed then
2937 Remove_Parent;
2938 end if;
2940 goto Leave;
2941 end;
2943 Rewrite (N, New_N);
2944 Set_Defining_Unit_Name (Specification (New_N), Formal);
2945 Set_Generic_Parent (Specification (N), Gen_Unit);
2946 Set_Instance_Env (Gen_Unit, Formal);
2947 Set_Is_Generic_Instance (Formal);
2949 Enter_Name (Formal);
2950 Set_Ekind (Formal, E_Package);
2951 Set_Etype (Formal, Standard_Void_Type);
2952 Set_Inner_Instances (Formal, New_Elmt_List);
2953 Push_Scope (Formal);
2955 -- Manually set the SPARK_Mode from the context because the package
2956 -- declaration is never analyzed.
2958 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2959 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2960 Set_SPARK_Pragma_Inherited (Formal);
2961 Set_SPARK_Aux_Pragma_Inherited (Formal);
2963 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2965 -- Similarly, we have to make the name of the formal visible in the
2966 -- parent instance, to resolve properly fully qualified names that
2967 -- may appear in the generic unit. The parent instance has been
2968 -- placed on the scope stack ahead of the current scope.
2970 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2972 Renaming_In_Par :=
2973 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2974 Set_Ekind (Renaming_In_Par, E_Package);
2975 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2976 Set_Scope (Renaming_In_Par, Parent_Instance);
2977 Set_Parent (Renaming_In_Par, Parent (Formal));
2978 Set_Renamed_Object (Renaming_In_Par, Formal);
2979 Append_Entity (Renaming_In_Par, Parent_Instance);
2980 end if;
2982 -- A formal package declaration behaves as a package instantiation with
2983 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2984 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2985 -- all SPARK_Mode pragmas within the generic_package_name.
2987 if SPARK_Mode /= On then
2988 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2990 -- Mark the formal spec in case the body is instantiated at a later
2991 -- pass. This preserves the original context in effect for the body.
2993 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2994 end if;
2996 Analyze (Specification (N));
2998 -- The formals for which associations are provided are not visible
2999 -- outside of the formal package. The others are still declared by a
3000 -- formal parameter declaration.
3002 -- If there are no associations, the only local entity to hide is the
3003 -- generated package renaming itself.
3005 declare
3006 E : Entity_Id;
3008 begin
3009 E := First_Entity (Formal);
3010 while Present (E) loop
3011 if Associations and then not Is_Generic_Formal (E) then
3012 Set_Is_Hidden (E);
3013 end if;
3015 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3016 Set_Is_Hidden (E);
3017 exit;
3018 end if;
3020 Next_Entity (E);
3021 end loop;
3022 end;
3024 End_Package_Scope (Formal);
3025 Restore_Hidden_Primitives (Vis_Prims_List);
3027 if Parent_Installed then
3028 Remove_Parent;
3029 end if;
3031 Restore_Env;
3033 -- Inside the generic unit, the formal package is a regular package, but
3034 -- no body is needed for it. Note that after instantiation, the defining
3035 -- unit name we need is in the new tree and not in the original (see
3036 -- Package_Instantiation). A generic formal package is an instance, and
3037 -- can be used as an actual for an inner instance.
3039 Set_Has_Completion (Formal, True);
3041 -- Add semantic information to the original defining identifier for ASIS
3042 -- use.
3044 Set_Ekind (Pack_Id, E_Package);
3045 Set_Etype (Pack_Id, Standard_Void_Type);
3046 Set_Scope (Pack_Id, Scope (Formal));
3047 Set_Has_Completion (Pack_Id, True);
3049 <<Leave>>
3050 if Has_Aspects (N) then
3051 Analyze_Aspect_Specifications (N, Pack_Id);
3052 end if;
3054 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3055 end Analyze_Formal_Package_Declaration;
3057 ---------------------------------
3058 -- Analyze_Formal_Private_Type --
3059 ---------------------------------
3061 procedure Analyze_Formal_Private_Type
3062 (N : Node_Id;
3063 T : Entity_Id;
3064 Def : Node_Id)
3066 begin
3067 New_Private_Type (N, T, Def);
3069 -- Set the size to an arbitrary but legal value
3071 Set_Size_Info (T, Standard_Integer);
3072 Set_RM_Size (T, RM_Size (Standard_Integer));
3073 end Analyze_Formal_Private_Type;
3075 ------------------------------------
3076 -- Analyze_Formal_Incomplete_Type --
3077 ------------------------------------
3079 procedure Analyze_Formal_Incomplete_Type
3080 (T : Entity_Id;
3081 Def : Node_Id)
3083 begin
3084 Enter_Name (T);
3085 Set_Ekind (T, E_Incomplete_Type);
3086 Set_Etype (T, T);
3087 Set_Private_Dependents (T, New_Elmt_List);
3089 if Tagged_Present (Def) then
3090 Set_Is_Tagged_Type (T);
3091 Make_Class_Wide_Type (T);
3092 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3093 end if;
3094 end Analyze_Formal_Incomplete_Type;
3096 ----------------------------------------
3097 -- Analyze_Formal_Signed_Integer_Type --
3098 ----------------------------------------
3100 procedure Analyze_Formal_Signed_Integer_Type
3101 (T : Entity_Id;
3102 Def : Node_Id)
3104 Base : constant Entity_Id :=
3105 New_Internal_Entity
3106 (E_Signed_Integer_Type,
3107 Current_Scope,
3108 Sloc (Defining_Identifier (Parent (Def))), 'G');
3110 begin
3111 Enter_Name (T);
3113 Set_Ekind (T, E_Signed_Integer_Subtype);
3114 Set_Etype (T, Base);
3115 Set_Size_Info (T, Standard_Integer);
3116 Set_RM_Size (T, RM_Size (Standard_Integer));
3117 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3118 Set_Is_Constrained (T);
3120 Set_Is_Generic_Type (Base);
3121 Set_Size_Info (Base, Standard_Integer);
3122 Set_RM_Size (Base, RM_Size (Standard_Integer));
3123 Set_Etype (Base, Base);
3124 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3125 Set_Parent (Base, Parent (Def));
3126 end Analyze_Formal_Signed_Integer_Type;
3128 -------------------------------------------
3129 -- Analyze_Formal_Subprogram_Declaration --
3130 -------------------------------------------
3132 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3133 Spec : constant Node_Id := Specification (N);
3134 Def : constant Node_Id := Default_Name (N);
3135 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3136 Subp : Entity_Id;
3138 begin
3139 if Nam = Error then
3140 return;
3141 end if;
3143 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3144 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3145 goto Leave;
3146 end if;
3148 Analyze_Subprogram_Declaration (N);
3149 Set_Is_Formal_Subprogram (Nam);
3150 Set_Has_Completion (Nam);
3152 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3153 Set_Is_Abstract_Subprogram (Nam);
3155 Set_Is_Dispatching_Operation (Nam);
3157 -- A formal abstract procedure cannot have a null default
3158 -- (RM 12.6(4.1/2)).
3160 if Nkind (Spec) = N_Procedure_Specification
3161 and then Null_Present (Spec)
3162 then
3163 Error_Msg_N
3164 ("a formal abstract subprogram cannot default to null", Spec);
3165 end if;
3167 declare
3168 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3169 begin
3170 if No (Ctrl_Type) then
3171 Error_Msg_N
3172 ("abstract formal subprogram must have a controlling type",
3175 elsif Ada_Version >= Ada_2012
3176 and then Is_Incomplete_Type (Ctrl_Type)
3177 then
3178 Error_Msg_NE
3179 ("controlling type of abstract formal subprogram cannot "
3180 & "be incomplete type", N, Ctrl_Type);
3182 else
3183 Check_Controlling_Formals (Ctrl_Type, Nam);
3184 end if;
3185 end;
3186 end if;
3188 -- Default name is resolved at the point of instantiation
3190 if Box_Present (N) then
3191 null;
3193 -- Else default is bound at the point of generic declaration
3195 elsif Present (Def) then
3196 if Nkind (Def) = N_Operator_Symbol then
3197 Find_Direct_Name (Def);
3199 elsif Nkind (Def) /= N_Attribute_Reference then
3200 Analyze (Def);
3202 else
3203 -- For an attribute reference, analyze the prefix and verify
3204 -- that it has the proper profile for the subprogram.
3206 Analyze (Prefix (Def));
3207 Valid_Default_Attribute (Nam, Def);
3208 goto Leave;
3209 end if;
3211 -- Default name may be overloaded, in which case the interpretation
3212 -- with the correct profile must be selected, as for a renaming.
3213 -- If the definition is an indexed component, it must denote a
3214 -- member of an entry family. If it is a selected component, it
3215 -- can be a protected operation.
3217 if Etype (Def) = Any_Type then
3218 goto Leave;
3220 elsif Nkind (Def) = N_Selected_Component then
3221 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3222 Error_Msg_N ("expect valid subprogram name as default", Def);
3223 end if;
3225 elsif Nkind (Def) = N_Indexed_Component then
3226 if Is_Entity_Name (Prefix (Def)) then
3227 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3228 Error_Msg_N ("expect valid subprogram name as default", Def);
3229 end if;
3231 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3232 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3233 E_Entry_Family
3234 then
3235 Error_Msg_N ("expect valid subprogram name as default", Def);
3236 end if;
3238 else
3239 Error_Msg_N ("expect valid subprogram name as default", Def);
3240 goto Leave;
3241 end if;
3243 elsif Nkind (Def) = N_Character_Literal then
3245 -- Needs some type checks: subprogram should be parameterless???
3247 Resolve (Def, (Etype (Nam)));
3249 elsif not Is_Entity_Name (Def)
3250 or else not Is_Overloadable (Entity (Def))
3251 then
3252 Error_Msg_N ("expect valid subprogram name as default", Def);
3253 goto Leave;
3255 elsif not Is_Overloaded (Def) then
3256 Subp := Entity (Def);
3258 if Subp = Nam then
3259 Error_Msg_N ("premature usage of formal subprogram", Def);
3261 elsif not Entity_Matches_Spec (Subp, Nam) then
3262 Error_Msg_N ("no visible entity matches specification", Def);
3263 end if;
3265 -- More than one interpretation, so disambiguate as for a renaming
3267 else
3268 declare
3269 I : Interp_Index;
3270 I1 : Interp_Index := 0;
3271 It : Interp;
3272 It1 : Interp;
3274 begin
3275 Subp := Any_Id;
3276 Get_First_Interp (Def, I, It);
3277 while Present (It.Nam) loop
3278 if Entity_Matches_Spec (It.Nam, Nam) then
3279 if Subp /= Any_Id then
3280 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3282 if It1 = No_Interp then
3283 Error_Msg_N ("ambiguous default subprogram", Def);
3284 else
3285 Subp := It1.Nam;
3286 end if;
3288 exit;
3290 else
3291 I1 := I;
3292 Subp := It.Nam;
3293 end if;
3294 end if;
3296 Get_Next_Interp (I, It);
3297 end loop;
3298 end;
3300 if Subp /= Any_Id then
3302 -- Subprogram found, generate reference to it
3304 Set_Entity (Def, Subp);
3305 Generate_Reference (Subp, Def);
3307 if Subp = Nam then
3308 Error_Msg_N ("premature usage of formal subprogram", Def);
3310 elsif Ekind (Subp) /= E_Operator then
3311 Check_Mode_Conformant (Subp, Nam);
3312 end if;
3314 else
3315 Error_Msg_N ("no visible subprogram matches specification", N);
3316 end if;
3317 end if;
3318 end if;
3320 <<Leave>>
3321 if Has_Aspects (N) then
3322 Analyze_Aspect_Specifications (N, Nam);
3323 end if;
3325 end Analyze_Formal_Subprogram_Declaration;
3327 -------------------------------------
3328 -- Analyze_Formal_Type_Declaration --
3329 -------------------------------------
3331 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3332 Def : constant Node_Id := Formal_Type_Definition (N);
3333 T : Entity_Id;
3335 begin
3336 T := Defining_Identifier (N);
3338 if Present (Discriminant_Specifications (N))
3339 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3340 then
3341 Error_Msg_N
3342 ("discriminants not allowed for this formal type", T);
3343 end if;
3345 -- Enter the new name, and branch to specific routine
3347 case Nkind (Def) is
3348 when N_Formal_Private_Type_Definition =>
3349 Analyze_Formal_Private_Type (N, T, Def);
3351 when N_Formal_Derived_Type_Definition =>
3352 Analyze_Formal_Derived_Type (N, T, Def);
3354 when N_Formal_Incomplete_Type_Definition =>
3355 Analyze_Formal_Incomplete_Type (T, Def);
3357 when N_Formal_Discrete_Type_Definition =>
3358 Analyze_Formal_Discrete_Type (T, Def);
3360 when N_Formal_Signed_Integer_Type_Definition =>
3361 Analyze_Formal_Signed_Integer_Type (T, Def);
3363 when N_Formal_Modular_Type_Definition =>
3364 Analyze_Formal_Modular_Type (T, Def);
3366 when N_Formal_Floating_Point_Definition =>
3367 Analyze_Formal_Floating_Type (T, Def);
3369 when N_Formal_Ordinary_Fixed_Point_Definition =>
3370 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3372 when N_Formal_Decimal_Fixed_Point_Definition =>
3373 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3375 when N_Array_Type_Definition =>
3376 Analyze_Formal_Array_Type (T, Def);
3378 when N_Access_Function_Definition
3379 | N_Access_Procedure_Definition
3380 | N_Access_To_Object_Definition
3382 Analyze_Generic_Access_Type (T, Def);
3384 -- Ada 2005: a interface declaration is encoded as an abstract
3385 -- record declaration or a abstract type derivation.
3387 when N_Record_Definition =>
3388 Analyze_Formal_Interface_Type (N, T, Def);
3390 when N_Derived_Type_Definition =>
3391 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3393 when N_Error =>
3394 null;
3396 when others =>
3397 raise Program_Error;
3398 end case;
3400 Set_Is_Generic_Type (T);
3402 if Has_Aspects (N) then
3403 Analyze_Aspect_Specifications (N, T);
3404 end if;
3405 end Analyze_Formal_Type_Declaration;
3407 ------------------------------------
3408 -- Analyze_Function_Instantiation --
3409 ------------------------------------
3411 procedure Analyze_Function_Instantiation (N : Node_Id) is
3412 begin
3413 Analyze_Subprogram_Instantiation (N, E_Function);
3414 end Analyze_Function_Instantiation;
3416 ---------------------------------
3417 -- Analyze_Generic_Access_Type --
3418 ---------------------------------
3420 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3421 begin
3422 Enter_Name (T);
3424 if Nkind (Def) = N_Access_To_Object_Definition then
3425 Access_Type_Declaration (T, Def);
3427 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3428 and then No (Full_View (Designated_Type (T)))
3429 and then not Is_Generic_Type (Designated_Type (T))
3430 then
3431 Error_Msg_N ("premature usage of incomplete type", Def);
3433 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3434 Error_Msg_N
3435 ("only a subtype mark is allowed in a formal", Def);
3436 end if;
3438 else
3439 Access_Subprogram_Declaration (T, Def);
3440 end if;
3441 end Analyze_Generic_Access_Type;
3443 ---------------------------------
3444 -- Analyze_Generic_Formal_Part --
3445 ---------------------------------
3447 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3448 Gen_Parm_Decl : Node_Id;
3450 begin
3451 -- The generic formals are processed in the scope of the generic unit,
3452 -- where they are immediately visible. The scope is installed by the
3453 -- caller.
3455 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3456 while Present (Gen_Parm_Decl) loop
3457 Analyze (Gen_Parm_Decl);
3458 Next (Gen_Parm_Decl);
3459 end loop;
3461 Generate_Reference_To_Generic_Formals (Current_Scope);
3462 end Analyze_Generic_Formal_Part;
3464 ------------------------------------------
3465 -- Analyze_Generic_Package_Declaration --
3466 ------------------------------------------
3468 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3469 Decls : constant List_Id := Visible_Declarations (Specification (N));
3470 Loc : constant Source_Ptr := Sloc (N);
3472 Decl : Node_Id;
3473 Id : Entity_Id;
3474 New_N : Node_Id;
3475 Renaming : Node_Id;
3476 Save_Parent : Node_Id;
3478 begin
3479 Check_SPARK_05_Restriction ("generic is not allowed", N);
3481 -- We introduce a renaming of the enclosing package, to have a usable
3482 -- entity as the prefix of an expanded name for a local entity of the
3483 -- form Par.P.Q, where P is the generic package. This is because a local
3484 -- entity named P may hide it, so that the usual visibility rules in
3485 -- the instance will not resolve properly.
3487 Renaming :=
3488 Make_Package_Renaming_Declaration (Loc,
3489 Defining_Unit_Name =>
3490 Make_Defining_Identifier (Loc,
3491 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3492 Name =>
3493 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3495 -- The declaration is inserted before other declarations, but before
3496 -- pragmas that may be library-unit pragmas and must appear before other
3497 -- declarations. The pragma Compile_Time_Error is not in this class, and
3498 -- may contain an expression that includes such a qualified name, so the
3499 -- renaming declaration must appear before it.
3501 -- Are there other pragmas that require this special handling ???
3503 if Present (Decls) then
3504 Decl := First (Decls);
3505 while Present (Decl)
3506 and then Nkind (Decl) = N_Pragma
3507 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3508 loop
3509 Next (Decl);
3510 end loop;
3512 if Present (Decl) then
3513 Insert_Before (Decl, Renaming);
3514 else
3515 Append (Renaming, Visible_Declarations (Specification (N)));
3516 end if;
3518 else
3519 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3520 end if;
3522 -- Create copy of generic unit, and save for instantiation. If the unit
3523 -- is a child unit, do not copy the specifications for the parent, which
3524 -- are not part of the generic tree.
3526 Save_Parent := Parent_Spec (N);
3527 Set_Parent_Spec (N, Empty);
3529 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3530 Set_Parent_Spec (New_N, Save_Parent);
3531 Rewrite (N, New_N);
3533 -- Once the contents of the generic copy and the template are swapped,
3534 -- do the same for their respective aspect specifications.
3536 Exchange_Aspects (N, New_N);
3538 -- Collect all contract-related source pragmas found within the template
3539 -- and attach them to the contract of the package spec. This contract is
3540 -- used in the capture of global references within annotations.
3542 Create_Generic_Contract (N);
3544 Id := Defining_Entity (N);
3545 Generate_Definition (Id);
3547 -- Expansion is not applied to generic units
3549 Start_Generic;
3551 Enter_Name (Id);
3552 Set_Ekind (Id, E_Generic_Package);
3553 Set_Etype (Id, Standard_Void_Type);
3555 -- Set SPARK_Mode from context
3557 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3558 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3559 Set_SPARK_Pragma_Inherited (Id);
3560 Set_SPARK_Aux_Pragma_Inherited (Id);
3562 -- Preserve relevant elaboration-related attributes of the context which
3563 -- are no longer available or very expensive to recompute once analysis,
3564 -- resolution, and expansion are over.
3566 Mark_Elaboration_Attributes
3567 (N_Id => Id,
3568 Checks => True);
3570 -- Analyze aspects now, so that generated pragmas appear in the
3571 -- declarations before building and analyzing the generic copy.
3573 if Has_Aspects (N) then
3574 Analyze_Aspect_Specifications (N, Id);
3575 end if;
3577 Push_Scope (Id);
3578 Enter_Generic_Scope (Id);
3579 Set_Inner_Instances (Id, New_Elmt_List);
3581 Set_Categorization_From_Pragmas (N);
3582 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3584 -- Link the declaration of the generic homonym in the generic copy to
3585 -- the package it renames, so that it is always resolved properly.
3587 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3588 Set_Entity (Associated_Node (Name (Renaming)), Id);
3590 -- For a library unit, we have reconstructed the entity for the unit,
3591 -- and must reset it in the library tables.
3593 if Nkind (Parent (N)) = N_Compilation_Unit then
3594 Set_Cunit_Entity (Current_Sem_Unit, Id);
3595 end if;
3597 Analyze_Generic_Formal_Part (N);
3599 -- After processing the generic formals, analysis proceeds as for a
3600 -- non-generic package.
3602 Analyze (Specification (N));
3604 Validate_Categorization_Dependency (N, Id);
3606 End_Generic;
3608 End_Package_Scope (Id);
3609 Exit_Generic_Scope (Id);
3611 -- If the generic appears within a package unit, the body of that unit
3612 -- has to be present for instantiation and inlining.
3614 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3615 Set_Body_Needed_For_Inlining
3616 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3617 end if;
3619 if Nkind (Parent (N)) /= N_Compilation_Unit then
3620 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3621 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3622 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3624 else
3625 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3626 Validate_RT_RAT_Component (N);
3628 -- If this is a spec without a body, check that generic parameters
3629 -- are referenced.
3631 if not Body_Required (Parent (N)) then
3632 Check_References (Id);
3633 end if;
3634 end if;
3636 -- If there is a specified storage pool in the context, create an
3637 -- aspect on the package declaration, so that it is used in any
3638 -- instance that does not override it.
3640 if Present (Default_Pool) then
3641 declare
3642 ASN : Node_Id;
3644 begin
3645 ASN :=
3646 Make_Aspect_Specification (Loc,
3647 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3648 Expression => New_Copy (Default_Pool));
3650 if No (Aspect_Specifications (Specification (N))) then
3651 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3652 else
3653 Append (ASN, Aspect_Specifications (Specification (N)));
3654 end if;
3655 end;
3656 end if;
3657 end Analyze_Generic_Package_Declaration;
3659 --------------------------------------------
3660 -- Analyze_Generic_Subprogram_Declaration --
3661 --------------------------------------------
3663 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3664 Formals : List_Id;
3665 Id : Entity_Id;
3666 New_N : Node_Id;
3667 Result_Type : Entity_Id;
3668 Save_Parent : Node_Id;
3669 Spec : Node_Id;
3670 Typ : Entity_Id;
3672 begin
3673 Check_SPARK_05_Restriction ("generic is not allowed", N);
3675 -- Create copy of generic unit, and save for instantiation. If the unit
3676 -- is a child unit, do not copy the specifications for the parent, which
3677 -- are not part of the generic tree.
3679 Save_Parent := Parent_Spec (N);
3680 Set_Parent_Spec (N, Empty);
3682 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3683 Set_Parent_Spec (New_N, Save_Parent);
3684 Rewrite (N, New_N);
3686 -- Once the contents of the generic copy and the template are swapped,
3687 -- do the same for their respective aspect specifications.
3689 Exchange_Aspects (N, New_N);
3691 -- Collect all contract-related source pragmas found within the template
3692 -- and attach them to the contract of the subprogram spec. This contract
3693 -- is used in the capture of global references within annotations.
3695 Create_Generic_Contract (N);
3697 Spec := Specification (N);
3698 Id := Defining_Entity (Spec);
3699 Generate_Definition (Id);
3701 if Nkind (Id) = N_Defining_Operator_Symbol then
3702 Error_Msg_N
3703 ("operator symbol not allowed for generic subprogram", Id);
3704 end if;
3706 Start_Generic;
3708 Enter_Name (Id);
3709 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3711 -- Analyze the aspects of the generic copy to ensure that all generated
3712 -- pragmas (if any) perform their semantic effects.
3714 if Has_Aspects (N) then
3715 Analyze_Aspect_Specifications (N, Id);
3716 end if;
3718 Push_Scope (Id);
3719 Enter_Generic_Scope (Id);
3720 Set_Inner_Instances (Id, New_Elmt_List);
3721 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3723 Analyze_Generic_Formal_Part (N);
3725 if Nkind (Spec) = N_Function_Specification then
3726 Set_Ekind (Id, E_Generic_Function);
3727 else
3728 Set_Ekind (Id, E_Generic_Procedure);
3729 end if;
3731 -- Set SPARK_Mode from context
3733 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3734 Set_SPARK_Pragma_Inherited (Id);
3736 -- Preserve relevant elaboration-related attributes of the context which
3737 -- are no longer available or very expensive to recompute once analysis,
3738 -- resolution, and expansion are over.
3740 Mark_Elaboration_Attributes
3741 (N_Id => Id,
3742 Checks => True);
3744 Formals := Parameter_Specifications (Spec);
3746 if Present (Formals) then
3747 Process_Formals (Formals, Spec);
3748 end if;
3750 if Nkind (Spec) = N_Function_Specification then
3751 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3752 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3753 Set_Etype (Id, Result_Type);
3755 -- Check restriction imposed by AI05-073: a generic function
3756 -- cannot return an abstract type or an access to such.
3758 -- This is a binding interpretation should it apply to earlier
3759 -- versions of Ada as well as Ada 2012???
3761 if Is_Abstract_Type (Designated_Type (Result_Type))
3762 and then Ada_Version >= Ada_2012
3763 then
3764 Error_Msg_N
3765 ("generic function cannot have an access result "
3766 & "that designates an abstract type", Spec);
3767 end if;
3769 else
3770 Find_Type (Result_Definition (Spec));
3771 Typ := Entity (Result_Definition (Spec));
3773 if Is_Abstract_Type (Typ)
3774 and then Ada_Version >= Ada_2012
3775 then
3776 Error_Msg_N
3777 ("generic function cannot have abstract result type", Spec);
3778 end if;
3780 -- If a null exclusion is imposed on the result type, then create
3781 -- a null-excluding itype (an access subtype) and use it as the
3782 -- function's Etype.
3784 if Is_Access_Type (Typ)
3785 and then Null_Exclusion_Present (Spec)
3786 then
3787 Set_Etype (Id,
3788 Create_Null_Excluding_Itype
3789 (T => Typ,
3790 Related_Nod => Spec,
3791 Scope_Id => Defining_Unit_Name (Spec)));
3792 else
3793 Set_Etype (Id, Typ);
3794 end if;
3795 end if;
3797 else
3798 Set_Etype (Id, Standard_Void_Type);
3799 end if;
3801 -- For a library unit, we have reconstructed the entity for the unit,
3802 -- and must reset it in the library tables. We also make sure that
3803 -- Body_Required is set properly in the original compilation unit node.
3805 if Nkind (Parent (N)) = N_Compilation_Unit then
3806 Set_Cunit_Entity (Current_Sem_Unit, Id);
3807 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3808 end if;
3810 -- If the generic appears within a package unit, the body of that unit
3811 -- has to be present for instantiation and inlining.
3813 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3814 and then Unit_Requires_Body (Id)
3815 then
3816 Set_Body_Needed_For_Inlining
3817 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3818 end if;
3820 Set_Categorization_From_Pragmas (N);
3821 Validate_Categorization_Dependency (N, Id);
3823 -- Capture all global references that occur within the profile of the
3824 -- generic subprogram. Aspects are not part of this processing because
3825 -- they must be delayed. If processed now, Save_Global_References will
3826 -- destroy the Associated_Node links and prevent the capture of global
3827 -- references when the contract of the generic subprogram is analyzed.
3829 Save_Global_References (Original_Node (N));
3831 End_Generic;
3832 End_Scope;
3833 Exit_Generic_Scope (Id);
3834 Generate_Reference_To_Formals (Id);
3836 List_Inherited_Pre_Post_Aspects (Id);
3837 end Analyze_Generic_Subprogram_Declaration;
3839 -----------------------------------
3840 -- Analyze_Package_Instantiation --
3841 -----------------------------------
3843 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3844 -- must be replaced by gotos which jump to the end of the routine in order
3845 -- to restore the Ghost and SPARK modes.
3847 procedure Analyze_Package_Instantiation (N : Node_Id) is
3848 Has_Inline_Always : Boolean := False;
3850 procedure Delay_Descriptors (E : Entity_Id);
3851 -- Delay generation of subprogram descriptors for given entity
3853 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3854 -- If inlining is active and the generic contains inlined subprograms,
3855 -- we instantiate the body. This may cause superfluous instantiations,
3856 -- but it is simpler than detecting the need for the body at the point
3857 -- of inlining, when the context of the instance is not available.
3859 -----------------------
3860 -- Delay_Descriptors --
3861 -----------------------
3863 procedure Delay_Descriptors (E : Entity_Id) is
3864 begin
3865 if not Delay_Subprogram_Descriptors (E) then
3866 Set_Delay_Subprogram_Descriptors (E);
3867 Pending_Descriptor.Append (E);
3868 end if;
3869 end Delay_Descriptors;
3871 -----------------------
3872 -- Might_Inline_Subp --
3873 -----------------------
3875 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3876 E : Entity_Id;
3878 begin
3879 if not Inline_Processing_Required then
3880 return False;
3882 else
3883 E := First_Entity (Gen_Unit);
3884 while Present (E) loop
3885 if Is_Subprogram (E) and then Is_Inlined (E) then
3886 -- Remember if there are any subprograms with Inline_Always
3888 if Has_Pragma_Inline_Always (E) then
3889 Has_Inline_Always := True;
3890 end if;
3892 return True;
3893 end if;
3895 Next_Entity (E);
3896 end loop;
3897 end if;
3899 return False;
3900 end Might_Inline_Subp;
3902 -- Local declarations
3904 Gen_Id : constant Node_Id := Name (N);
3905 Is_Actual_Pack : constant Boolean :=
3906 Is_Internal (Defining_Entity (N));
3907 Loc : constant Source_Ptr := Sloc (N);
3909 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3910 Saved_ISMP : constant Boolean :=
3911 Ignore_SPARK_Mode_Pragmas_In_Instance;
3912 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3913 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3914 -- Save the Ghost and SPARK mode-related data to restore on exit
3916 Saved_Style_Check : constant Boolean := Style_Check;
3917 -- Save style check mode for restore on exit
3919 Act_Decl : Node_Id;
3920 Act_Decl_Name : Node_Id;
3921 Act_Decl_Id : Entity_Id;
3922 Act_Spec : Node_Id;
3923 Act_Tree : Node_Id;
3924 Env_Installed : Boolean := False;
3925 Gen_Decl : Node_Id;
3926 Gen_Spec : Node_Id;
3927 Gen_Unit : Entity_Id;
3928 Inline_Now : Boolean := False;
3929 Needs_Body : Boolean;
3930 Parent_Installed : Boolean := False;
3931 Renaming_List : List_Id;
3932 Unit_Renaming : Node_Id;
3934 Vis_Prims_List : Elist_Id := No_Elist;
3935 -- List of primitives made temporarily visible in the instantiation
3936 -- to match the visibility of the formal type
3938 -- Start of processing for Analyze_Package_Instantiation
3940 begin
3941 -- Preserve relevant elaboration-related attributes of the context which
3942 -- are no longer available or very expensive to recompute once analysis,
3943 -- resolution, and expansion are over.
3945 Mark_Elaboration_Attributes
3946 (N_Id => N,
3947 Checks => True,
3948 Level => True,
3949 Modes => True);
3951 Check_SPARK_05_Restriction ("generic is not allowed", N);
3953 -- Very first thing: check for Text_IO special unit in case we are
3954 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3956 Check_Text_IO_Special_Unit (Name (N));
3958 -- Make node global for error reporting
3960 Instantiation_Node := N;
3962 -- Case of instantiation of a generic package
3964 if Nkind (N) = N_Package_Instantiation then
3965 Act_Decl_Id := New_Copy (Defining_Entity (N));
3966 Set_Comes_From_Source (Act_Decl_Id, True);
3968 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3969 Act_Decl_Name :=
3970 Make_Defining_Program_Unit_Name (Loc,
3971 Name =>
3972 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3973 Defining_Identifier => Act_Decl_Id);
3974 else
3975 Act_Decl_Name := Act_Decl_Id;
3976 end if;
3978 -- Case of instantiation of a formal package
3980 else
3981 Act_Decl_Id := Defining_Identifier (N);
3982 Act_Decl_Name := Act_Decl_Id;
3983 end if;
3985 Generate_Definition (Act_Decl_Id);
3986 Set_Ekind (Act_Decl_Id, E_Package);
3988 -- Initialize list of incomplete actuals before analysis
3990 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3992 Preanalyze_Actuals (N, Act_Decl_Id);
3994 -- Turn off style checking in instances. If the check is enabled on the
3995 -- generic unit, a warning in an instance would just be noise. If not
3996 -- enabled on the generic, then a warning in an instance is just wrong.
3997 -- This must be done after analyzing the actuals, which do come from
3998 -- source and are subject to style checking.
4000 Style_Check := False;
4002 Init_Env;
4003 Env_Installed := True;
4005 -- Reset renaming map for formal types. The mapping is established
4006 -- when analyzing the generic associations, but some mappings are
4007 -- inherited from formal packages of parent units, and these are
4008 -- constructed when the parents are installed.
4010 Generic_Renamings.Set_Last (0);
4011 Generic_Renamings_HTable.Reset;
4013 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4014 Gen_Unit := Entity (Gen_Id);
4016 -- A package instantiation is Ghost when it is subject to pragma Ghost
4017 -- or the generic template is Ghost. Set the mode now to ensure that
4018 -- any nodes generated during analysis and expansion are marked as
4019 -- Ghost.
4021 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4023 -- Verify that it is the name of a generic package
4025 -- A visibility glitch: if the instance is a child unit and the generic
4026 -- is the generic unit of a parent instance (i.e. both the parent and
4027 -- the child units are instances of the same package) the name now
4028 -- denotes the renaming within the parent, not the intended generic
4029 -- unit. See if there is a homonym that is the desired generic. The
4030 -- renaming declaration must be visible inside the instance of the
4031 -- child, but not when analyzing the name in the instantiation itself.
4033 if Ekind (Gen_Unit) = E_Package
4034 and then Present (Renamed_Entity (Gen_Unit))
4035 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4036 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4037 and then Present (Homonym (Gen_Unit))
4038 then
4039 Gen_Unit := Homonym (Gen_Unit);
4040 end if;
4042 if Etype (Gen_Unit) = Any_Type then
4043 Restore_Env;
4044 goto Leave;
4046 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4048 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4050 if From_Limited_With (Gen_Unit) then
4051 Error_Msg_N
4052 ("cannot instantiate a limited withed package", Gen_Id);
4053 else
4054 Error_Msg_NE
4055 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4056 end if;
4058 Restore_Env;
4059 goto Leave;
4060 end if;
4062 if In_Extended_Main_Source_Unit (N) then
4063 Set_Is_Instantiated (Gen_Unit);
4064 Generate_Reference (Gen_Unit, N);
4066 if Present (Renamed_Object (Gen_Unit)) then
4067 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4068 Generate_Reference (Renamed_Object (Gen_Unit), N);
4069 end if;
4070 end if;
4072 if Nkind (Gen_Id) = N_Identifier
4073 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4074 then
4075 Error_Msg_NE
4076 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4078 elsif Nkind (Gen_Id) = N_Expanded_Name
4079 and then Is_Child_Unit (Gen_Unit)
4080 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4081 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4082 then
4083 Error_Msg_N
4084 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4085 end if;
4087 Set_Entity (Gen_Id, Gen_Unit);
4089 -- If generic is a renaming, get original generic unit
4091 if Present (Renamed_Object (Gen_Unit))
4092 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4093 then
4094 Gen_Unit := Renamed_Object (Gen_Unit);
4095 end if;
4097 -- Verify that there are no circular instantiations
4099 if In_Open_Scopes (Gen_Unit) then
4100 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4101 Restore_Env;
4102 goto Leave;
4104 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4105 Error_Msg_Node_2 := Current_Scope;
4106 Error_Msg_NE
4107 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4108 Circularity_Detected := True;
4109 Restore_Env;
4110 goto Leave;
4112 else
4113 -- If the context of the instance is subject to SPARK_Mode "off" or
4114 -- the annotation is altogether missing, set the global flag which
4115 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4116 -- the instance.
4118 if SPARK_Mode /= On then
4119 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4121 -- Mark the instance spec in case the body is instantiated at a
4122 -- later pass. This preserves the original context in effect for
4123 -- the body.
4125 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4126 end if;
4128 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4129 Gen_Spec := Specification (Gen_Decl);
4131 -- Initialize renamings map, for error checking, and the list that
4132 -- holds private entities whose views have changed between generic
4133 -- definition and instantiation. If this is the instance created to
4134 -- validate an actual package, the instantiation environment is that
4135 -- of the enclosing instance.
4137 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4139 -- Copy original generic tree, to produce text for instantiation
4141 Act_Tree :=
4142 Copy_Generic_Node
4143 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4145 Act_Spec := Specification (Act_Tree);
4147 -- If this is the instance created to validate an actual package,
4148 -- only the formals matter, do not examine the package spec itself.
4150 if Is_Actual_Pack then
4151 Set_Visible_Declarations (Act_Spec, New_List);
4152 Set_Private_Declarations (Act_Spec, New_List);
4153 end if;
4155 Renaming_List :=
4156 Analyze_Associations
4157 (I_Node => N,
4158 Formals => Generic_Formal_Declarations (Act_Tree),
4159 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4161 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4163 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4164 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4165 Set_Is_Generic_Instance (Act_Decl_Id);
4166 Set_Generic_Parent (Act_Spec, Gen_Unit);
4168 -- References to the generic in its own declaration or its body are
4169 -- references to the instance. Add a renaming declaration for the
4170 -- generic unit itself. This declaration, as well as the renaming
4171 -- declarations for the generic formals, must remain private to the
4172 -- unit: the formals, because this is the language semantics, and
4173 -- the unit because its use is an artifact of the implementation.
4175 Unit_Renaming :=
4176 Make_Package_Renaming_Declaration (Loc,
4177 Defining_Unit_Name =>
4178 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4179 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4181 Append (Unit_Renaming, Renaming_List);
4183 -- The renaming declarations are the first local declarations of the
4184 -- new unit.
4186 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4187 Insert_List_Before
4188 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4189 else
4190 Set_Visible_Declarations (Act_Spec, Renaming_List);
4191 end if;
4193 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4195 -- Propagate the aspect specifications from the package declaration
4196 -- template to the instantiated version of the package declaration.
4198 if Has_Aspects (Act_Tree) then
4199 Set_Aspect_Specifications (Act_Decl,
4200 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4201 end if;
4203 -- The generic may have a generated Default_Storage_Pool aspect,
4204 -- set at the point of generic declaration. If the instance has
4205 -- that aspect, it overrides the one inherited from the generic.
4207 if Has_Aspects (Gen_Spec) then
4208 if No (Aspect_Specifications (N)) then
4209 Set_Aspect_Specifications (N,
4210 (New_Copy_List_Tree
4211 (Aspect_Specifications (Gen_Spec))));
4213 else
4214 declare
4215 ASN1, ASN2 : Node_Id;
4217 begin
4218 ASN1 := First (Aspect_Specifications (N));
4219 while Present (ASN1) loop
4220 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
4221 then
4222 -- If generic carries a default storage pool, remove
4223 -- it in favor of the instance one.
4225 ASN2 := First (Aspect_Specifications (Gen_Spec));
4226 while Present (ASN2) loop
4227 if Chars (Identifier (ASN2)) =
4228 Name_Default_Storage_Pool
4229 then
4230 Remove (ASN2);
4231 exit;
4232 end if;
4234 Next (ASN2);
4235 end loop;
4236 end if;
4238 Next (ASN1);
4239 end loop;
4241 Prepend_List_To (Aspect_Specifications (N),
4242 (New_Copy_List_Tree
4243 (Aspect_Specifications (Gen_Spec))));
4244 end;
4245 end if;
4246 end if;
4248 -- Save the instantiation node, for subsequent instantiation of the
4249 -- body, if there is one and we are generating code for the current
4250 -- unit. Mark unit as having a body (avoids premature error message).
4252 -- We instantiate the body if we are generating code, if we are
4253 -- generating cross-reference information, or if we are building
4254 -- trees for ASIS use or GNATprove use.
4256 declare
4257 Enclosing_Body_Present : Boolean := False;
4258 -- If the generic unit is not a compilation unit, then a body may
4259 -- be present in its parent even if none is required. We create a
4260 -- tentative pending instantiation for the body, which will be
4261 -- discarded if none is actually present.
4263 Scop : Entity_Id;
4265 begin
4266 if Scope (Gen_Unit) /= Standard_Standard
4267 and then not Is_Child_Unit (Gen_Unit)
4268 then
4269 Scop := Scope (Gen_Unit);
4270 while Present (Scop) and then Scop /= Standard_Standard loop
4271 if Unit_Requires_Body (Scop) then
4272 Enclosing_Body_Present := True;
4273 exit;
4275 elsif In_Open_Scopes (Scop)
4276 and then In_Package_Body (Scop)
4277 then
4278 Enclosing_Body_Present := True;
4279 exit;
4280 end if;
4282 exit when Is_Compilation_Unit (Scop);
4283 Scop := Scope (Scop);
4284 end loop;
4285 end if;
4287 -- If front-end inlining is enabled or there are any subprograms
4288 -- marked with Inline_Always, and this is a unit for which code
4289 -- will be generated, we instantiate the body at once.
4291 -- This is done if the instance is not the main unit, and if the
4292 -- generic is not a child unit of another generic, to avoid scope
4293 -- problems and the reinstallation of parent instances.
4295 if Expander_Active
4296 and then (not Is_Child_Unit (Gen_Unit)
4297 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4298 and then Might_Inline_Subp (Gen_Unit)
4299 and then not Is_Actual_Pack
4300 then
4301 if not Back_End_Inlining
4302 and then (Front_End_Inlining or else Has_Inline_Always)
4303 and then (Is_In_Main_Unit (N)
4304 or else In_Main_Context (Current_Scope))
4305 and then Nkind (Parent (N)) /= N_Compilation_Unit
4306 then
4307 Inline_Now := True;
4309 -- In configurable_run_time mode we force the inlining of
4310 -- predefined subprograms marked Inline_Always, to minimize
4311 -- the use of the run-time library.
4313 elsif In_Predefined_Unit (Gen_Decl)
4314 and then Configurable_Run_Time_Mode
4315 and then Nkind (Parent (N)) /= N_Compilation_Unit
4316 then
4317 Inline_Now := True;
4318 end if;
4320 -- If the current scope is itself an instance within a child
4321 -- unit, there will be duplications in the scope stack, and the
4322 -- unstacking mechanism in Inline_Instance_Body will fail.
4323 -- This loses some rare cases of optimization, and might be
4324 -- improved some day, if we can find a proper abstraction for
4325 -- "the complete compilation context" that can be saved and
4326 -- restored. ???
4328 if Is_Generic_Instance (Current_Scope) then
4329 declare
4330 Curr_Unit : constant Entity_Id :=
4331 Cunit_Entity (Current_Sem_Unit);
4332 begin
4333 if Curr_Unit /= Current_Scope
4334 and then Is_Child_Unit (Curr_Unit)
4335 then
4336 Inline_Now := False;
4337 end if;
4338 end;
4339 end if;
4340 end if;
4342 Needs_Body :=
4343 (Unit_Requires_Body (Gen_Unit)
4344 or else Enclosing_Body_Present
4345 or else Present (Corresponding_Body (Gen_Decl)))
4346 and then (Is_In_Main_Unit (N)
4347 or else Might_Inline_Subp (Gen_Unit))
4348 and then not Is_Actual_Pack
4349 and then not Inline_Now
4350 and then (Operating_Mode = Generate_Code
4352 -- Need comment for this check ???
4354 or else (Operating_Mode = Check_Semantics
4355 and then (ASIS_Mode or GNATprove_Mode)));
4357 -- If front-end inlining is enabled or there are any subprograms
4358 -- marked with Inline_Always, do not instantiate body when within
4359 -- a generic context.
4361 if ((Front_End_Inlining or else Has_Inline_Always)
4362 and then not Expander_Active)
4363 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4364 then
4365 Needs_Body := False;
4366 end if;
4368 -- If the current context is generic, and the package being
4369 -- instantiated is declared within a formal package, there is no
4370 -- body to instantiate until the enclosing generic is instantiated
4371 -- and there is an actual for the formal package. If the formal
4372 -- package has parameters, we build a regular package instance for
4373 -- it, that precedes the original formal package declaration.
4375 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4376 declare
4377 Decl : constant Node_Id :=
4378 Original_Node
4379 (Unit_Declaration_Node (Scope (Gen_Unit)));
4380 begin
4381 if Nkind (Decl) = N_Formal_Package_Declaration
4382 or else (Nkind (Decl) = N_Package_Declaration
4383 and then Is_List_Member (Decl)
4384 and then Present (Next (Decl))
4385 and then
4386 Nkind (Next (Decl)) =
4387 N_Formal_Package_Declaration)
4388 then
4389 Needs_Body := False;
4390 end if;
4391 end;
4392 end if;
4393 end;
4395 -- For RCI unit calling stubs, we omit the instance body if the
4396 -- instance is the RCI library unit itself.
4398 -- However there is a special case for nested instances: in this case
4399 -- we do generate the instance body, as it might be required, e.g.
4400 -- because it provides stream attributes for some type used in the
4401 -- profile of a remote subprogram. This is consistent with 12.3(12),
4402 -- which indicates that the instance body occurs at the place of the
4403 -- instantiation, and thus is part of the RCI declaration, which is
4404 -- present on all client partitions (this is E.2.3(18)).
4406 -- Note that AI12-0002 may make it illegal at some point to have
4407 -- stream attributes defined in an RCI unit, in which case this
4408 -- special case will become unnecessary. In the meantime, there
4409 -- is known application code in production that depends on this
4410 -- being possible, so we definitely cannot eliminate the body in
4411 -- the case of nested instances for the time being.
4413 -- When we generate a nested instance body, calling stubs for any
4414 -- relevant subprogram will be be inserted immediately after the
4415 -- subprogram declarations, and will take precedence over the
4416 -- subsequent (original) body. (The stub and original body will be
4417 -- complete homographs, but this is permitted in an instance).
4418 -- (Could we do better and remove the original body???)
4420 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4421 and then Comes_From_Source (N)
4422 and then Nkind (Parent (N)) = N_Compilation_Unit
4423 then
4424 Needs_Body := False;
4425 end if;
4427 if Needs_Body then
4429 -- Here is a defence against a ludicrous number of instantiations
4430 -- caused by a circular set of instantiation attempts.
4432 if Pending_Instantiations.Last > Maximum_Instantiations then
4433 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4434 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4435 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4436 raise Unrecoverable_Error;
4437 end if;
4439 -- Indicate that the enclosing scopes contain an instantiation,
4440 -- and that cleanup actions should be delayed until after the
4441 -- instance body is expanded.
4443 Check_Forward_Instantiation (Gen_Decl);
4444 if Nkind (N) = N_Package_Instantiation then
4445 declare
4446 Enclosing_Master : Entity_Id;
4448 begin
4449 -- Loop to search enclosing masters
4451 Enclosing_Master := Current_Scope;
4452 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4453 if Ekind (Enclosing_Master) = E_Package then
4454 if Is_Compilation_Unit (Enclosing_Master) then
4455 if In_Package_Body (Enclosing_Master) then
4456 Delay_Descriptors
4457 (Body_Entity (Enclosing_Master));
4458 else
4459 Delay_Descriptors
4460 (Enclosing_Master);
4461 end if;
4463 exit Scope_Loop;
4465 else
4466 Enclosing_Master := Scope (Enclosing_Master);
4467 end if;
4469 elsif Is_Generic_Unit (Enclosing_Master)
4470 or else Ekind (Enclosing_Master) = E_Void
4471 then
4472 -- Cleanup actions will eventually be performed on the
4473 -- enclosing subprogram or package instance, if any.
4474 -- Enclosing scope is void in the formal part of a
4475 -- generic subprogram.
4477 exit Scope_Loop;
4479 else
4480 if Ekind (Enclosing_Master) = E_Entry
4481 and then
4482 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4483 then
4484 if not Expander_Active then
4485 exit Scope_Loop;
4486 else
4487 Enclosing_Master :=
4488 Protected_Body_Subprogram (Enclosing_Master);
4489 end if;
4490 end if;
4492 Set_Delay_Cleanups (Enclosing_Master);
4494 while Ekind (Enclosing_Master) = E_Block loop
4495 Enclosing_Master := Scope (Enclosing_Master);
4496 end loop;
4498 if Is_Subprogram (Enclosing_Master) then
4499 Delay_Descriptors (Enclosing_Master);
4501 elsif Is_Task_Type (Enclosing_Master) then
4502 declare
4503 TBP : constant Node_Id :=
4504 Get_Task_Body_Procedure
4505 (Enclosing_Master);
4506 begin
4507 if Present (TBP) then
4508 Delay_Descriptors (TBP);
4509 Set_Delay_Cleanups (TBP);
4510 end if;
4511 end;
4512 end if;
4514 exit Scope_Loop;
4515 end if;
4516 end loop Scope_Loop;
4517 end;
4519 -- Make entry in table
4521 Add_Pending_Instantiation (N, Act_Decl);
4522 end if;
4523 end if;
4525 Set_Categorization_From_Pragmas (Act_Decl);
4527 if Parent_Installed then
4528 Hide_Current_Scope;
4529 end if;
4531 Set_Instance_Spec (N, Act_Decl);
4533 -- If not a compilation unit, insert the package declaration before
4534 -- the original instantiation node.
4536 if Nkind (Parent (N)) /= N_Compilation_Unit then
4537 Mark_Rewrite_Insertion (Act_Decl);
4538 Insert_Before (N, Act_Decl);
4540 if Has_Aspects (N) then
4541 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4543 -- The pragma created for a Default_Storage_Pool aspect must
4544 -- appear ahead of the declarations in the instance spec.
4545 -- Analysis has placed it after the instance node, so remove
4546 -- it and reinsert it properly now.
4548 declare
4549 ASN : constant Node_Id := First (Aspect_Specifications (N));
4550 A_Name : constant Name_Id := Chars (Identifier (ASN));
4551 Decl : Node_Id;
4553 begin
4554 if A_Name = Name_Default_Storage_Pool then
4555 if No (Visible_Declarations (Act_Spec)) then
4556 Set_Visible_Declarations (Act_Spec, New_List);
4557 end if;
4559 Decl := Next (N);
4560 while Present (Decl) loop
4561 if Nkind (Decl) = N_Pragma then
4562 Remove (Decl);
4563 Prepend (Decl, Visible_Declarations (Act_Spec));
4564 exit;
4565 end if;
4567 Next (Decl);
4568 end loop;
4569 end if;
4570 end;
4571 end if;
4573 Analyze (Act_Decl);
4575 -- For an instantiation that is a compilation unit, place
4576 -- declaration on current node so context is complete for analysis
4577 -- (including nested instantiations). If this is the main unit,
4578 -- the declaration eventually replaces the instantiation node.
4579 -- If the instance body is created later, it replaces the
4580 -- instance node, and the declaration is attached to it
4581 -- (see Build_Instance_Compilation_Unit_Nodes).
4583 else
4584 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4586 -- The entity for the current unit is the newly created one,
4587 -- and all semantic information is attached to it.
4589 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4591 -- If this is the main unit, replace the main entity as well
4593 if Current_Sem_Unit = Main_Unit then
4594 Main_Unit_Entity := Act_Decl_Id;
4595 end if;
4596 end if;
4598 Set_Unit (Parent (N), Act_Decl);
4599 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4600 Set_Package_Instantiation (Act_Decl_Id, N);
4602 -- Process aspect specifications of the instance node, if any, to
4603 -- take into account categorization pragmas before analyzing the
4604 -- instance.
4606 if Has_Aspects (N) then
4607 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4608 end if;
4610 Analyze (Act_Decl);
4611 Set_Unit (Parent (N), N);
4612 Set_Body_Required (Parent (N), False);
4613 end if;
4615 -- Save the scenario for later examination by the ABE Processing
4616 -- phase.
4618 Record_Elaboration_Scenario (N);
4620 -- The instantiation results in a guaranteed ABE
4622 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4624 -- Do not instantiate the corresponding body because gigi cannot
4625 -- handle certain types of premature instantiations.
4627 Pending_Instantiations.Decrement_Last;
4629 -- Create completing bodies for all subprogram declarations since
4630 -- their real bodies will not be instantiated.
4632 Provide_Completing_Bodies (Instance_Spec (N));
4633 end if;
4635 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4637 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4638 First_Private_Entity (Act_Decl_Id));
4640 -- If the instantiation will receive a body, the unit will be
4641 -- transformed into a package body, and receive its own elaboration
4642 -- entity. Otherwise, the nature of the unit is now a package
4643 -- declaration.
4645 if Nkind (Parent (N)) = N_Compilation_Unit
4646 and then not Needs_Body
4647 then
4648 Rewrite (N, Act_Decl);
4649 end if;
4651 if Present (Corresponding_Body (Gen_Decl))
4652 or else Unit_Requires_Body (Gen_Unit)
4653 then
4654 Set_Has_Completion (Act_Decl_Id);
4655 end if;
4657 Check_Formal_Packages (Act_Decl_Id);
4659 Restore_Hidden_Primitives (Vis_Prims_List);
4660 Restore_Private_Views (Act_Decl_Id);
4662 Inherit_Context (Gen_Decl, N);
4664 if Parent_Installed then
4665 Remove_Parent;
4666 end if;
4668 Restore_Env;
4669 Env_Installed := False;
4670 end if;
4672 Validate_Categorization_Dependency (N, Act_Decl_Id);
4674 -- There used to be a check here to prevent instantiations in local
4675 -- contexts if the No_Local_Allocators restriction was active. This
4676 -- check was removed by a binding interpretation in AI-95-00130/07,
4677 -- but we retain the code for documentation purposes.
4679 -- if Ekind (Act_Decl_Id) /= E_Void
4680 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4681 -- then
4682 -- Check_Restriction (No_Local_Allocators, N);
4683 -- end if;
4685 if Inline_Now then
4686 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4687 end if;
4689 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4690 -- be used as defining identifiers for a formal package and for the
4691 -- corresponding expanded package.
4693 if Nkind (N) = N_Formal_Package_Declaration then
4694 Act_Decl_Id := New_Copy (Defining_Entity (N));
4695 Set_Comes_From_Source (Act_Decl_Id, True);
4696 Set_Is_Generic_Instance (Act_Decl_Id, False);
4697 Set_Defining_Identifier (N, Act_Decl_Id);
4698 end if;
4700 -- Check that if N is an instantiation of System.Dim_Float_IO or
4701 -- System.Dim_Integer_IO, the formal type has a dimension system.
4703 if Nkind (N) = N_Package_Instantiation
4704 and then Is_Dim_IO_Package_Instantiation (N)
4705 then
4706 declare
4707 Assoc : constant Node_Id := First (Generic_Associations (N));
4708 begin
4709 if not Has_Dimension_System
4710 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4711 then
4712 Error_Msg_N ("type with a dimension system expected", Assoc);
4713 end if;
4714 end;
4715 end if;
4717 <<Leave>>
4718 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4719 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4720 end if;
4722 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4723 Restore_Ghost_Mode (Saved_GM);
4724 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4725 Style_Check := Saved_Style_Check;
4727 exception
4728 when Instantiation_Error =>
4729 if Parent_Installed then
4730 Remove_Parent;
4731 end if;
4733 if Env_Installed then
4734 Restore_Env;
4735 end if;
4737 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4738 Restore_Ghost_Mode (Saved_GM);
4739 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4740 Style_Check := Saved_Style_Check;
4741 end Analyze_Package_Instantiation;
4743 --------------------------
4744 -- Inline_Instance_Body --
4745 --------------------------
4747 -- WARNING: This routine manages SPARK regions. Return statements must be
4748 -- replaced by gotos which jump to the end of the routine and restore the
4749 -- SPARK mode.
4751 procedure Inline_Instance_Body
4752 (N : Node_Id;
4753 Gen_Unit : Entity_Id;
4754 Act_Decl : Node_Id)
4756 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4757 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4758 Gen_Comp : constant Entity_Id :=
4759 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4761 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4762 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4763 -- Save the SPARK mode-related data to restore on exit. Removing
4764 -- enclosing scopes to provide a clean environment for analysis of
4765 -- the inlined body will eliminate any previously set SPARK_Mode.
4767 Scope_Stack_Depth : constant Pos :=
4768 Scope_Stack.Last - Scope_Stack.First + 1;
4770 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4771 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4772 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4774 Curr_Scope : Entity_Id := Empty;
4775 List : Elist_Id := No_Elist; -- init to avoid warning
4776 N_Instances : Nat := 0;
4777 Num_Inner : Nat := 0;
4778 Num_Scopes : Nat := 0;
4779 Removed : Boolean := False;
4780 S : Entity_Id;
4781 Vis : Boolean;
4783 begin
4784 -- Case of generic unit defined in another unit. We must remove the
4785 -- complete context of the current unit to install that of the generic.
4787 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4789 -- Add some comments for the following two loops ???
4791 S := Current_Scope;
4792 while Present (S) and then S /= Standard_Standard loop
4793 loop
4794 Num_Scopes := Num_Scopes + 1;
4796 Use_Clauses (Num_Scopes) :=
4797 (Scope_Stack.Table
4798 (Scope_Stack.Last - Num_Scopes + 1).
4799 First_Use_Clause);
4800 End_Use_Clauses (Use_Clauses (Num_Scopes));
4802 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4803 or else Scope_Stack.Table
4804 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4805 end loop;
4807 exit when Is_Generic_Instance (S)
4808 and then (In_Package_Body (S)
4809 or else Ekind (S) = E_Procedure
4810 or else Ekind (S) = E_Function);
4811 S := Scope (S);
4812 end loop;
4814 Vis := Is_Immediately_Visible (Gen_Comp);
4816 -- Find and save all enclosing instances
4818 S := Current_Scope;
4820 while Present (S)
4821 and then S /= Standard_Standard
4822 loop
4823 if Is_Generic_Instance (S) then
4824 N_Instances := N_Instances + 1;
4825 Instances (N_Instances) := S;
4827 exit when In_Package_Body (S);
4828 end if;
4830 S := Scope (S);
4831 end loop;
4833 -- Remove context of current compilation unit, unless we are within a
4834 -- nested package instantiation, in which case the context has been
4835 -- removed previously.
4837 -- If current scope is the body of a child unit, remove context of
4838 -- spec as well. If an enclosing scope is an instance body, the
4839 -- context has already been removed, but the entities in the body
4840 -- must be made invisible as well.
4842 S := Current_Scope;
4843 while Present (S) and then S /= Standard_Standard loop
4844 if Is_Generic_Instance (S)
4845 and then (In_Package_Body (S)
4846 or else Ekind_In (S, E_Procedure, E_Function))
4847 then
4848 -- We still have to remove the entities of the enclosing
4849 -- instance from direct visibility.
4851 declare
4852 E : Entity_Id;
4853 begin
4854 E := First_Entity (S);
4855 while Present (E) loop
4856 Set_Is_Immediately_Visible (E, False);
4857 Next_Entity (E);
4858 end loop;
4859 end;
4861 exit;
4862 end if;
4864 if S = Curr_Unit
4865 or else (Ekind (Curr_Unit) = E_Package_Body
4866 and then S = Spec_Entity (Curr_Unit))
4867 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4868 and then S = Corresponding_Spec
4869 (Unit_Declaration_Node (Curr_Unit)))
4870 then
4871 Removed := True;
4873 -- Remove entities in current scopes from visibility, so that
4874 -- instance body is compiled in a clean environment.
4876 List := Save_Scope_Stack (Handle_Use => False);
4878 if Is_Child_Unit (S) then
4880 -- Remove child unit from stack, as well as inner scopes.
4881 -- Removing the context of a child unit removes parent units
4882 -- as well.
4884 while Current_Scope /= S loop
4885 Num_Inner := Num_Inner + 1;
4886 Inner_Scopes (Num_Inner) := Current_Scope;
4887 Pop_Scope;
4888 end loop;
4890 Pop_Scope;
4891 Remove_Context (Curr_Comp);
4892 Curr_Scope := S;
4894 else
4895 Remove_Context (Curr_Comp);
4896 end if;
4898 if Ekind (Curr_Unit) = E_Package_Body then
4899 Remove_Context (Library_Unit (Curr_Comp));
4900 end if;
4901 end if;
4903 S := Scope (S);
4904 end loop;
4906 pragma Assert (Num_Inner < Num_Scopes);
4908 -- The inlined package body must be analyzed with the SPARK_Mode of
4909 -- the enclosing context, otherwise the body may cause bogus errors
4910 -- if a configuration SPARK_Mode pragma in in effect.
4912 Push_Scope (Standard_Standard);
4913 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4914 Instantiate_Package_Body
4915 (Body_Info =>
4916 ((Inst_Node => N,
4917 Act_Decl => Act_Decl,
4918 Expander_Status => Expander_Active,
4919 Current_Sem_Unit => Current_Sem_Unit,
4920 Scope_Suppress => Scope_Suppress,
4921 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4922 Version => Ada_Version,
4923 Version_Pragma => Ada_Version_Pragma,
4924 Warnings => Save_Warnings,
4925 SPARK_Mode => Saved_SM,
4926 SPARK_Mode_Pragma => Saved_SMP)),
4927 Inlined_Body => True);
4929 Pop_Scope;
4931 -- Restore context
4933 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4935 -- Reset Generic_Instance flag so that use clauses can be installed
4936 -- in the proper order. (See Use_One_Package for effect of enclosing
4937 -- instances on processing of use clauses).
4939 for J in 1 .. N_Instances loop
4940 Set_Is_Generic_Instance (Instances (J), False);
4941 end loop;
4943 if Removed then
4944 Install_Context (Curr_Comp, Chain => False);
4946 if Present (Curr_Scope)
4947 and then Is_Child_Unit (Curr_Scope)
4948 then
4949 Push_Scope (Curr_Scope);
4950 Set_Is_Immediately_Visible (Curr_Scope);
4952 -- Finally, restore inner scopes as well
4954 for J in reverse 1 .. Num_Inner loop
4955 Push_Scope (Inner_Scopes (J));
4956 end loop;
4957 end if;
4959 Restore_Scope_Stack (List, Handle_Use => False);
4961 if Present (Curr_Scope)
4962 and then
4963 (In_Private_Part (Curr_Scope)
4964 or else In_Package_Body (Curr_Scope))
4965 then
4966 -- Install private declaration of ancestor units, which are
4967 -- currently available. Restore_Scope_Stack and Install_Context
4968 -- only install the visible part of parents.
4970 declare
4971 Par : Entity_Id;
4972 begin
4973 Par := Scope (Curr_Scope);
4974 while (Present (Par)) and then Par /= Standard_Standard loop
4975 Install_Private_Declarations (Par);
4976 Par := Scope (Par);
4977 end loop;
4978 end;
4979 end if;
4980 end if;
4982 -- Restore use clauses. For a child unit, use clauses in the parents
4983 -- are restored when installing the context, so only those in inner
4984 -- scopes (and those local to the child unit itself) need to be
4985 -- installed explicitly.
4987 if Is_Child_Unit (Curr_Unit) and then Removed then
4988 for J in reverse 1 .. Num_Inner + 1 loop
4989 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4990 Use_Clauses (J);
4991 Install_Use_Clauses (Use_Clauses (J));
4992 end loop;
4994 else
4995 for J in reverse 1 .. Num_Scopes loop
4996 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4997 Use_Clauses (J);
4998 Install_Use_Clauses (Use_Clauses (J));
4999 end loop;
5000 end if;
5002 -- Restore status of instances. If one of them is a body, make its
5003 -- local entities visible again.
5005 declare
5006 E : Entity_Id;
5007 Inst : Entity_Id;
5009 begin
5010 for J in 1 .. N_Instances loop
5011 Inst := Instances (J);
5012 Set_Is_Generic_Instance (Inst, True);
5014 if In_Package_Body (Inst)
5015 or else Ekind_In (S, E_Procedure, E_Function)
5016 then
5017 E := First_Entity (Instances (J));
5018 while Present (E) loop
5019 Set_Is_Immediately_Visible (E);
5020 Next_Entity (E);
5021 end loop;
5022 end if;
5023 end loop;
5024 end;
5026 -- If generic unit is in current unit, current context is correct. Note
5027 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5028 -- enclosing scopes were removed.
5030 else
5031 Instantiate_Package_Body
5032 (Body_Info =>
5033 ((Inst_Node => N,
5034 Act_Decl => Act_Decl,
5035 Expander_Status => Expander_Active,
5036 Current_Sem_Unit => Current_Sem_Unit,
5037 Scope_Suppress => Scope_Suppress,
5038 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5039 Version => Ada_Version,
5040 Version_Pragma => Ada_Version_Pragma,
5041 Warnings => Save_Warnings,
5042 SPARK_Mode => SPARK_Mode,
5043 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
5044 Inlined_Body => True);
5045 end if;
5046 end Inline_Instance_Body;
5048 -------------------------------------
5049 -- Analyze_Procedure_Instantiation --
5050 -------------------------------------
5052 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5053 begin
5054 Analyze_Subprogram_Instantiation (N, E_Procedure);
5055 end Analyze_Procedure_Instantiation;
5057 -----------------------------------
5058 -- Need_Subprogram_Instance_Body --
5059 -----------------------------------
5061 function Need_Subprogram_Instance_Body
5062 (N : Node_Id;
5063 Subp : Entity_Id) return Boolean
5065 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5066 -- Return True if E is an inlined subprogram, an inlined renaming or a
5067 -- subprogram nested in an inlined subprogram. The inlining machinery
5068 -- totally disregards nested subprograms since it considers that they
5069 -- will always be compiled if the parent is (see Inline.Is_Nested).
5071 ------------------------------------
5072 -- Is_Inlined_Or_Child_Of_Inlined --
5073 ------------------------------------
5075 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5076 Scop : Entity_Id;
5078 begin
5079 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5080 return True;
5081 end if;
5083 Scop := Scope (E);
5084 while Scop /= Standard_Standard loop
5085 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5086 return True;
5087 end if;
5089 Scop := Scope (Scop);
5090 end loop;
5092 return False;
5093 end Is_Inlined_Or_Child_Of_Inlined;
5095 begin
5096 -- Must be in the main unit or inlined (or child of inlined)
5098 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5100 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5102 and then (Operating_Mode = Generate_Code
5103 or else (Operating_Mode = Check_Semantics
5104 and then (ASIS_Mode or GNATprove_Mode)))
5106 -- The body is needed when generating code (full expansion), in ASIS
5107 -- mode for other tools, and in GNATprove mode (special expansion) for
5108 -- formal verification of the body itself.
5110 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
5112 -- No point in inlining if ABE is inevitable
5114 and then not Is_Known_Guaranteed_ABE (N)
5116 -- Or if subprogram is eliminated
5118 and then not Is_Eliminated (Subp)
5119 then
5120 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5121 return True;
5123 -- Here if not inlined, or we ignore the inlining
5125 else
5126 return False;
5127 end if;
5128 end Need_Subprogram_Instance_Body;
5130 --------------------------------------
5131 -- Analyze_Subprogram_Instantiation --
5132 --------------------------------------
5134 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5135 -- must be replaced by gotos which jump to the end of the routine in order
5136 -- to restore the Ghost and SPARK modes.
5138 procedure Analyze_Subprogram_Instantiation
5139 (N : Node_Id;
5140 K : Entity_Kind)
5142 Loc : constant Source_Ptr := Sloc (N);
5143 Gen_Id : constant Node_Id := Name (N);
5144 Errs : constant Nat := Serious_Errors_Detected;
5146 Anon_Id : constant Entity_Id :=
5147 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
5148 Chars => New_External_Name
5149 (Chars (Defining_Entity (N)), 'R'));
5151 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5152 Act_Decl : Node_Id;
5153 Act_Spec : Node_Id;
5154 Act_Tree : Node_Id;
5156 Env_Installed : Boolean := False;
5157 Gen_Unit : Entity_Id;
5158 Gen_Decl : Node_Id;
5159 Pack_Id : Entity_Id;
5160 Parent_Installed : Boolean := False;
5162 Renaming_List : List_Id;
5163 -- The list of declarations that link formals and actuals of the
5164 -- instance. These are subtype declarations for formal types, and
5165 -- renaming declarations for other formals. The subprogram declaration
5166 -- for the instance is then appended to the list, and the last item on
5167 -- the list is the renaming declaration for the instance.
5169 procedure Analyze_Instance_And_Renamings;
5170 -- The instance must be analyzed in a context that includes the mappings
5171 -- of generic parameters into actuals. We create a package declaration
5172 -- for this purpose, and a subprogram with an internal name within the
5173 -- package. The subprogram instance is simply an alias for the internal
5174 -- subprogram, declared in the current scope.
5176 procedure Build_Subprogram_Renaming;
5177 -- If the subprogram is recursive, there are occurrences of the name of
5178 -- the generic within the body, which must resolve to the current
5179 -- instance. We add a renaming declaration after the declaration, which
5180 -- is available in the instance body, as well as in the analysis of
5181 -- aspects that appear in the generic. This renaming declaration is
5182 -- inserted after the instance declaration which it renames.
5184 ------------------------------------
5185 -- Analyze_Instance_And_Renamings --
5186 ------------------------------------
5188 procedure Analyze_Instance_And_Renamings is
5189 Def_Ent : constant Entity_Id := Defining_Entity (N);
5190 Pack_Decl : Node_Id;
5192 begin
5193 if Nkind (Parent (N)) = N_Compilation_Unit then
5195 -- For the case of a compilation unit, the container package has
5196 -- the same name as the instantiation, to insure that the binder
5197 -- calls the elaboration procedure with the right name. Copy the
5198 -- entity of the instance, which may have compilation level flags
5199 -- (e.g. Is_Child_Unit) set.
5201 Pack_Id := New_Copy (Def_Ent);
5203 else
5204 -- Otherwise we use the name of the instantiation concatenated
5205 -- with its source position to ensure uniqueness if there are
5206 -- several instantiations with the same name.
5208 Pack_Id :=
5209 Make_Defining_Identifier (Loc,
5210 Chars => New_External_Name
5211 (Related_Id => Chars (Def_Ent),
5212 Suffix => "GP",
5213 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5214 end if;
5216 Pack_Decl :=
5217 Make_Package_Declaration (Loc,
5218 Specification => Make_Package_Specification (Loc,
5219 Defining_Unit_Name => Pack_Id,
5220 Visible_Declarations => Renaming_List,
5221 End_Label => Empty));
5223 Set_Instance_Spec (N, Pack_Decl);
5224 Set_Is_Generic_Instance (Pack_Id);
5225 Set_Debug_Info_Needed (Pack_Id);
5227 -- Case of not a compilation unit
5229 if Nkind (Parent (N)) /= N_Compilation_Unit then
5230 Mark_Rewrite_Insertion (Pack_Decl);
5231 Insert_Before (N, Pack_Decl);
5232 Set_Has_Completion (Pack_Id);
5234 -- Case of an instantiation that is a compilation unit
5236 -- Place declaration on current node so context is complete for
5237 -- analysis (including nested instantiations), and for use in a
5238 -- context_clause (see Analyze_With_Clause).
5240 else
5241 Set_Unit (Parent (N), Pack_Decl);
5242 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5243 end if;
5245 Analyze (Pack_Decl);
5246 Check_Formal_Packages (Pack_Id);
5247 Set_Is_Generic_Instance (Pack_Id, False);
5249 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5250 -- above???
5252 -- Body of the enclosing package is supplied when instantiating the
5253 -- subprogram body, after semantic analysis is completed.
5255 if Nkind (Parent (N)) = N_Compilation_Unit then
5257 -- Remove package itself from visibility, so it does not
5258 -- conflict with subprogram.
5260 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5262 -- Set name and scope of internal subprogram so that the proper
5263 -- external name will be generated. The proper scope is the scope
5264 -- of the wrapper package. We need to generate debugging info for
5265 -- the internal subprogram, so set flag accordingly.
5267 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5268 Set_Scope (Anon_Id, Scope (Pack_Id));
5270 -- Mark wrapper package as referenced, to avoid spurious warnings
5271 -- if the instantiation appears in various with_ clauses of
5272 -- subunits of the main unit.
5274 Set_Referenced (Pack_Id);
5275 end if;
5277 Set_Is_Generic_Instance (Anon_Id);
5278 Set_Debug_Info_Needed (Anon_Id);
5279 Act_Decl_Id := New_Copy (Anon_Id);
5281 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5282 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5283 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5285 -- Subprogram instance comes from source only if generic does
5287 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5289 -- If the instance is a child unit, mark the Id accordingly. Mark
5290 -- the anonymous entity as well, which is the real subprogram and
5291 -- which is used when the instance appears in a context clause.
5292 -- Similarly, propagate the Is_Eliminated flag to handle properly
5293 -- nested eliminated subprograms.
5295 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5296 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5297 New_Overloaded_Entity (Act_Decl_Id);
5298 Check_Eliminated (Act_Decl_Id);
5299 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5301 if Nkind (Parent (N)) = N_Compilation_Unit then
5302 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5303 Set_Is_Compilation_Unit (Anon_Id);
5305 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5306 end if;
5308 -- The instance is not a freezing point for the new subprogram.
5309 -- The anonymous subprogram may have a freeze node, created for
5310 -- some delayed aspects. This freeze node must not be inherited
5311 -- by the visible subprogram entity.
5313 Set_Is_Frozen (Act_Decl_Id, False);
5314 Set_Freeze_Node (Act_Decl_Id, Empty);
5316 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5317 Valid_Operator_Definition (Act_Decl_Id);
5318 end if;
5320 Set_Alias (Act_Decl_Id, Anon_Id);
5321 Set_Has_Completion (Act_Decl_Id);
5322 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5324 if Nkind (Parent (N)) = N_Compilation_Unit then
5325 Set_Body_Required (Parent (N), False);
5326 end if;
5327 end Analyze_Instance_And_Renamings;
5329 -------------------------------
5330 -- Build_Subprogram_Renaming --
5331 -------------------------------
5333 procedure Build_Subprogram_Renaming is
5334 Renaming_Decl : Node_Id;
5335 Unit_Renaming : Node_Id;
5337 begin
5338 Unit_Renaming :=
5339 Make_Subprogram_Renaming_Declaration (Loc,
5340 Specification =>
5341 Copy_Generic_Node
5342 (Specification (Original_Node (Gen_Decl)),
5343 Empty,
5344 Instantiating => True),
5345 Name => New_Occurrence_Of (Anon_Id, Loc));
5347 -- The generic may be a a child unit. The renaming needs an
5348 -- identifier with the proper name.
5350 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5351 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5353 -- If there is a formal subprogram with the same name as the unit
5354 -- itself, do not add this renaming declaration, to prevent
5355 -- ambiguities when there is a call with that name in the body.
5356 -- This is a partial and ugly fix for one ACATS test. ???
5358 Renaming_Decl := First (Renaming_List);
5359 while Present (Renaming_Decl) loop
5360 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5361 and then
5362 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5363 then
5364 exit;
5365 end if;
5367 Next (Renaming_Decl);
5368 end loop;
5370 if No (Renaming_Decl) then
5371 Append (Unit_Renaming, Renaming_List);
5372 end if;
5373 end Build_Subprogram_Renaming;
5375 -- Local variables
5377 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5378 Saved_ISMP : constant Boolean :=
5379 Ignore_SPARK_Mode_Pragmas_In_Instance;
5380 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5381 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5382 -- Save the Ghost and SPARK mode-related data to restore on exit
5384 Vis_Prims_List : Elist_Id := No_Elist;
5385 -- List of primitives made temporarily visible in the instantiation
5386 -- to match the visibility of the formal type
5388 -- Start of processing for Analyze_Subprogram_Instantiation
5390 begin
5391 -- Preserve relevant elaboration-related attributes of the context which
5392 -- are no longer available or very expensive to recompute once analysis,
5393 -- resolution, and expansion are over.
5395 Mark_Elaboration_Attributes
5396 (N_Id => N,
5397 Checks => True,
5398 Level => True,
5399 Modes => True);
5401 Check_SPARK_05_Restriction ("generic is not allowed", N);
5403 -- Very first thing: check for special Text_IO unit in case we are
5404 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5405 -- such an instantiation is bogus (these are packages, not subprograms),
5406 -- but we get a better error message if we do this.
5408 Check_Text_IO_Special_Unit (Gen_Id);
5410 -- Make node global for error reporting
5412 Instantiation_Node := N;
5414 -- For package instantiations we turn off style checks, because they
5415 -- will have been emitted in the generic. For subprogram instantiations
5416 -- we want to apply at least the check on overriding indicators so we
5417 -- do not modify the style check status.
5419 -- The renaming declarations for the actuals do not come from source and
5420 -- will not generate spurious warnings.
5422 Preanalyze_Actuals (N);
5424 Init_Env;
5425 Env_Installed := True;
5426 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5427 Gen_Unit := Entity (Gen_Id);
5429 -- A subprogram instantiation is Ghost when it is subject to pragma
5430 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5431 -- that any nodes generated during analysis and expansion are marked as
5432 -- Ghost.
5434 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5436 Generate_Reference (Gen_Unit, Gen_Id);
5438 if Nkind (Gen_Id) = N_Identifier
5439 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5440 then
5441 Error_Msg_NE
5442 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5443 end if;
5445 if Etype (Gen_Unit) = Any_Type then
5446 Restore_Env;
5447 goto Leave;
5448 end if;
5450 -- Verify that it is a generic subprogram of the right kind, and that
5451 -- it does not lead to a circular instantiation.
5453 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5454 Error_Msg_NE
5455 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5457 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5458 Error_Msg_NE
5459 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5461 elsif In_Open_Scopes (Gen_Unit) then
5462 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5464 else
5465 Set_Entity (Gen_Id, Gen_Unit);
5466 Set_Is_Instantiated (Gen_Unit);
5468 if In_Extended_Main_Source_Unit (N) then
5469 Generate_Reference (Gen_Unit, N);
5470 end if;
5472 -- If renaming, get original unit
5474 if Present (Renamed_Object (Gen_Unit))
5475 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5476 E_Generic_Function)
5477 then
5478 Gen_Unit := Renamed_Object (Gen_Unit);
5479 Set_Is_Instantiated (Gen_Unit);
5480 Generate_Reference (Gen_Unit, N);
5481 end if;
5483 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5484 Error_Msg_Node_2 := Current_Scope;
5485 Error_Msg_NE
5486 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5487 Circularity_Detected := True;
5488 Restore_Hidden_Primitives (Vis_Prims_List);
5489 goto Leave;
5490 end if;
5492 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5494 -- Initialize renamings map, for error checking
5496 Generic_Renamings.Set_Last (0);
5497 Generic_Renamings_HTable.Reset;
5499 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5501 -- Copy original generic tree, to produce text for instantiation
5503 Act_Tree :=
5504 Copy_Generic_Node
5505 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5507 -- Inherit overriding indicator from instance node
5509 Act_Spec := Specification (Act_Tree);
5510 Set_Must_Override (Act_Spec, Must_Override (N));
5511 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5513 Renaming_List :=
5514 Analyze_Associations
5515 (I_Node => N,
5516 Formals => Generic_Formal_Declarations (Act_Tree),
5517 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5519 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5521 -- The subprogram itself cannot contain a nested instance, so the
5522 -- current parent is left empty.
5524 Set_Instance_Env (Gen_Unit, Empty);
5526 -- Build the subprogram declaration, which does not appear in the
5527 -- generic template, and give it a sloc consistent with that of the
5528 -- template.
5530 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5531 Set_Generic_Parent (Act_Spec, Gen_Unit);
5532 Act_Decl :=
5533 Make_Subprogram_Declaration (Sloc (Act_Spec),
5534 Specification => Act_Spec);
5536 -- The aspects have been copied previously, but they have to be
5537 -- linked explicitly to the new subprogram declaration. Explicit
5538 -- pre/postconditions on the instance are analyzed below, in a
5539 -- separate step.
5541 Move_Aspects (Act_Tree, To => Act_Decl);
5542 Set_Categorization_From_Pragmas (Act_Decl);
5544 if Parent_Installed then
5545 Hide_Current_Scope;
5546 end if;
5548 Append (Act_Decl, Renaming_List);
5550 -- Contract-related source pragmas that follow a generic subprogram
5551 -- must be instantiated explicitly because they are not part of the
5552 -- subprogram template.
5554 Instantiate_Subprogram_Contract
5555 (Original_Node (Gen_Decl), Renaming_List);
5557 Build_Subprogram_Renaming;
5559 -- If the context of the instance is subject to SPARK_Mode "off" or
5560 -- the annotation is altogether missing, set the global flag which
5561 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5562 -- the instance. This should be done prior to analyzing the instance.
5564 if SPARK_Mode /= On then
5565 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5566 end if;
5568 -- If the context of an instance is not subject to SPARK_Mode "off",
5569 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5570 -- the latter should be the one applicable to the instance.
5572 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5573 and then Saved_SM /= Off
5574 and then Present (SPARK_Pragma (Gen_Unit))
5575 then
5576 Set_SPARK_Mode (Gen_Unit);
5577 end if;
5579 Analyze_Instance_And_Renamings;
5581 -- Restore SPARK_Mode from the context after analysis of the package
5582 -- declaration, so that the SPARK_Mode on the generic spec does not
5583 -- apply to the pending instance for the instance body.
5585 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5586 and then Saved_SM /= Off
5587 and then Present (SPARK_Pragma (Gen_Unit))
5588 then
5589 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5590 end if;
5592 -- If the generic is marked Import (Intrinsic), then so is the
5593 -- instance. This indicates that there is no body to instantiate. If
5594 -- generic is marked inline, so it the instance, and the anonymous
5595 -- subprogram it renames. If inlined, or else if inlining is enabled
5596 -- for the compilation, we generate the instance body even if it is
5597 -- not within the main unit.
5599 if Is_Intrinsic_Subprogram (Gen_Unit) then
5600 Set_Is_Intrinsic_Subprogram (Anon_Id);
5601 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5603 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5604 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5605 end if;
5606 end if;
5608 -- Inherit convention from generic unit. Intrinsic convention, as for
5609 -- an instance of unchecked conversion, is not inherited because an
5610 -- explicit Ada instance has been created.
5612 if Has_Convention_Pragma (Gen_Unit)
5613 and then Convention (Gen_Unit) /= Convention_Intrinsic
5614 then
5615 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5616 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5617 end if;
5619 Generate_Definition (Act_Decl_Id);
5621 -- Inherit all inlining-related flags which apply to the generic in
5622 -- the subprogram and its declaration.
5624 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5625 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5627 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5628 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5630 -- Propagate No_Return if pragma applied to generic unit. This must
5631 -- be done explicitly because pragma does not appear in generic
5632 -- declaration (unlike the aspect case).
5634 if No_Return (Gen_Unit) then
5635 Set_No_Return (Act_Decl_Id);
5636 Set_No_Return (Anon_Id);
5637 end if;
5639 Set_Has_Pragma_Inline_Always
5640 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5641 Set_Has_Pragma_Inline_Always
5642 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5644 -- Mark both the instance spec and the anonymous package in case the
5645 -- body is instantiated at a later pass. This preserves the original
5646 -- context in effect for the body.
5648 if SPARK_Mode /= On then
5649 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5650 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5651 end if;
5653 -- Save the scenario for later examination by the ABE Processing
5654 -- phase.
5656 Record_Elaboration_Scenario (N);
5658 -- The instantiation results in a guaranteed ABE. Create a completing
5659 -- body for the subprogram declaration because the real body will not
5660 -- be instantiated.
5662 if Is_Known_Guaranteed_ABE (N) then
5663 Provide_Completing_Bodies (Instance_Spec (N));
5664 end if;
5666 if Is_Dispatching_Operation (Act_Decl_Id)
5667 and then Ada_Version >= Ada_2005
5668 then
5669 declare
5670 Formal : Entity_Id;
5672 begin
5673 Formal := First_Formal (Act_Decl_Id);
5674 while Present (Formal) loop
5675 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5676 and then Is_Controlling_Formal (Formal)
5677 and then not Can_Never_Be_Null (Formal)
5678 then
5679 Error_Msg_NE
5680 ("access parameter& is controlling,", N, Formal);
5681 Error_Msg_NE
5682 ("\corresponding parameter of & must be explicitly "
5683 & "null-excluding", N, Gen_Id);
5684 end if;
5686 Next_Formal (Formal);
5687 end loop;
5688 end;
5689 end if;
5691 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5693 Validate_Categorization_Dependency (N, Act_Decl_Id);
5695 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5696 Inherit_Context (Gen_Decl, N);
5698 Restore_Private_Views (Pack_Id, False);
5700 -- If the context requires a full instantiation, mark node for
5701 -- subsequent construction of the body.
5703 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5704 Check_Forward_Instantiation (Gen_Decl);
5706 -- The wrapper package is always delayed, because it does not
5707 -- constitute a freeze point, but to insure that the freeze node
5708 -- is placed properly, it is created directly when instantiating
5709 -- the body (otherwise the freeze node might appear to early for
5710 -- nested instantiations). For ASIS purposes, indicate that the
5711 -- wrapper package has replaced the instantiation node.
5713 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5714 Rewrite (N, Unit (Parent (N)));
5715 Set_Unit (Parent (N), N);
5716 end if;
5718 -- Replace instance node for library-level instantiations of
5719 -- intrinsic subprograms, for ASIS use.
5721 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5722 Rewrite (N, Unit (Parent (N)));
5723 Set_Unit (Parent (N), N);
5724 end if;
5726 if Parent_Installed then
5727 Remove_Parent;
5728 end if;
5730 Restore_Hidden_Primitives (Vis_Prims_List);
5731 Restore_Env;
5732 Env_Installed := False;
5733 Generic_Renamings.Set_Last (0);
5734 Generic_Renamings_HTable.Reset;
5735 end if;
5737 <<Leave>>
5738 -- Analyze aspects in declaration if no errors appear in the instance.
5740 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5741 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5742 end if;
5744 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5745 Restore_Ghost_Mode (Saved_GM);
5746 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5748 exception
5749 when Instantiation_Error =>
5750 if Parent_Installed then
5751 Remove_Parent;
5752 end if;
5754 if Env_Installed then
5755 Restore_Env;
5756 end if;
5758 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5759 Restore_Ghost_Mode (Saved_GM);
5760 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5761 end Analyze_Subprogram_Instantiation;
5763 -------------------------
5764 -- Get_Associated_Node --
5765 -------------------------
5767 function Get_Associated_Node (N : Node_Id) return Node_Id is
5768 Assoc : Node_Id;
5770 begin
5771 Assoc := Associated_Node (N);
5773 if Nkind (Assoc) /= Nkind (N) then
5774 return Assoc;
5776 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5777 return Assoc;
5779 else
5780 -- If the node is part of an inner generic, it may itself have been
5781 -- remapped into a further generic copy. Associated_Node is otherwise
5782 -- used for the entity of the node, and will be of a different node
5783 -- kind, or else N has been rewritten as a literal or function call.
5785 while Present (Associated_Node (Assoc))
5786 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5787 loop
5788 Assoc := Associated_Node (Assoc);
5789 end loop;
5791 -- Follow an additional link in case the final node was rewritten.
5792 -- This can only happen with nested generic units.
5794 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5795 and then Present (Associated_Node (Assoc))
5796 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5797 N_Explicit_Dereference,
5798 N_Integer_Literal,
5799 N_Real_Literal,
5800 N_String_Literal))
5801 then
5802 Assoc := Associated_Node (Assoc);
5803 end if;
5805 -- An additional special case: an unconstrained type in an object
5806 -- declaration may have been rewritten as a local subtype constrained
5807 -- by the expression in the declaration. We need to recover the
5808 -- original entity, which may be global.
5810 if Present (Original_Node (Assoc))
5811 and then Nkind (Parent (N)) = N_Object_Declaration
5812 then
5813 Assoc := Original_Node (Assoc);
5814 end if;
5816 return Assoc;
5817 end if;
5818 end Get_Associated_Node;
5820 ----------------------------
5821 -- Build_Function_Wrapper --
5822 ----------------------------
5824 function Build_Function_Wrapper
5825 (Formal_Subp : Entity_Id;
5826 Actual_Subp : Entity_Id) return Node_Id
5828 Loc : constant Source_Ptr := Sloc (Current_Scope);
5829 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5830 Actuals : List_Id;
5831 Decl : Node_Id;
5832 Func_Name : Node_Id;
5833 Func : Entity_Id;
5834 Parm_Type : Node_Id;
5835 Profile : List_Id := New_List;
5836 Spec : Node_Id;
5837 Act_F : Entity_Id;
5838 Form_F : Entity_Id;
5839 New_F : Entity_Id;
5841 begin
5842 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5844 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5845 Set_Ekind (Func, E_Function);
5846 Set_Is_Generic_Actual_Subprogram (Func);
5848 Actuals := New_List;
5849 Profile := New_List;
5851 Act_F := First_Formal (Actual_Subp);
5852 Form_F := First_Formal (Formal_Subp);
5853 while Present (Form_F) loop
5855 -- Create new formal for profile of wrapper, and add a reference
5856 -- to it in the list of actuals for the enclosing call. The name
5857 -- must be that of the formal in the formal subprogram, because
5858 -- calls to it in the generic body may use named associations.
5860 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5862 Parm_Type :=
5863 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5865 Append_To (Profile,
5866 Make_Parameter_Specification (Loc,
5867 Defining_Identifier => New_F,
5868 Parameter_Type => Parm_Type));
5870 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5871 Next_Formal (Form_F);
5873 if Present (Act_F) then
5874 Next_Formal (Act_F);
5875 end if;
5876 end loop;
5878 Spec :=
5879 Make_Function_Specification (Loc,
5880 Defining_Unit_Name => Func,
5881 Parameter_Specifications => Profile,
5882 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5884 Decl :=
5885 Make_Expression_Function (Loc,
5886 Specification => Spec,
5887 Expression =>
5888 Make_Function_Call (Loc,
5889 Name => Func_Name,
5890 Parameter_Associations => Actuals));
5892 return Decl;
5893 end Build_Function_Wrapper;
5895 ----------------------------
5896 -- Build_Operator_Wrapper --
5897 ----------------------------
5899 function Build_Operator_Wrapper
5900 (Formal_Subp : Entity_Id;
5901 Actual_Subp : Entity_Id) return Node_Id
5903 Loc : constant Source_Ptr := Sloc (Current_Scope);
5904 Ret_Type : constant Entity_Id :=
5905 Get_Instance_Of (Etype (Formal_Subp));
5906 Op_Type : constant Entity_Id :=
5907 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5908 Is_Binary : constant Boolean :=
5909 Present (Next_Formal (First_Formal (Formal_Subp)));
5911 Decl : Node_Id;
5912 Expr : Node_Id := Empty;
5913 F1, F2 : Entity_Id;
5914 Func : Entity_Id;
5915 Op_Name : Name_Id;
5916 Spec : Node_Id;
5917 L, R : Node_Id;
5919 begin
5920 Op_Name := Chars (Actual_Subp);
5922 -- Create entities for wrapper function and its formals
5924 F1 := Make_Temporary (Loc, 'A');
5925 F2 := Make_Temporary (Loc, 'B');
5926 L := New_Occurrence_Of (F1, Loc);
5927 R := New_Occurrence_Of (F2, Loc);
5929 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5930 Set_Ekind (Func, E_Function);
5931 Set_Is_Generic_Actual_Subprogram (Func);
5933 Spec :=
5934 Make_Function_Specification (Loc,
5935 Defining_Unit_Name => Func,
5936 Parameter_Specifications => New_List (
5937 Make_Parameter_Specification (Loc,
5938 Defining_Identifier => F1,
5939 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5940 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5942 if Is_Binary then
5943 Append_To (Parameter_Specifications (Spec),
5944 Make_Parameter_Specification (Loc,
5945 Defining_Identifier => F2,
5946 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5947 end if;
5949 -- Build expression as a function call, or as an operator node
5950 -- that corresponds to the name of the actual, starting with
5951 -- binary operators.
5953 if Op_Name not in Any_Operator_Name then
5954 Expr :=
5955 Make_Function_Call (Loc,
5956 Name =>
5957 New_Occurrence_Of (Actual_Subp, Loc),
5958 Parameter_Associations => New_List (L));
5960 if Is_Binary then
5961 Append_To (Parameter_Associations (Expr), R);
5962 end if;
5964 -- Binary operators
5966 elsif Is_Binary then
5967 if Op_Name = Name_Op_And then
5968 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5969 elsif Op_Name = Name_Op_Or then
5970 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5971 elsif Op_Name = Name_Op_Xor then
5972 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5973 elsif Op_Name = Name_Op_Eq then
5974 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5975 elsif Op_Name = Name_Op_Ne then
5976 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5977 elsif Op_Name = Name_Op_Le then
5978 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5979 elsif Op_Name = Name_Op_Gt then
5980 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5981 elsif Op_Name = Name_Op_Ge then
5982 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5983 elsif Op_Name = Name_Op_Lt then
5984 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5985 elsif Op_Name = Name_Op_Add then
5986 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5987 elsif Op_Name = Name_Op_Subtract then
5988 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5989 elsif Op_Name = Name_Op_Concat then
5990 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5991 elsif Op_Name = Name_Op_Multiply then
5992 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5993 elsif Op_Name = Name_Op_Divide then
5994 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5995 elsif Op_Name = Name_Op_Mod then
5996 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5997 elsif Op_Name = Name_Op_Rem then
5998 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5999 elsif Op_Name = Name_Op_Expon then
6000 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6001 end if;
6003 -- Unary operators
6005 else
6006 if Op_Name = Name_Op_Add then
6007 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6008 elsif Op_Name = Name_Op_Subtract then
6009 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6010 elsif Op_Name = Name_Op_Abs then
6011 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6012 elsif Op_Name = Name_Op_Not then
6013 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6014 end if;
6015 end if;
6017 Decl :=
6018 Make_Expression_Function (Loc,
6019 Specification => Spec,
6020 Expression => Expr);
6022 return Decl;
6023 end Build_Operator_Wrapper;
6025 -------------------------------------------
6026 -- Build_Instance_Compilation_Unit_Nodes --
6027 -------------------------------------------
6029 procedure Build_Instance_Compilation_Unit_Nodes
6030 (N : Node_Id;
6031 Act_Body : Node_Id;
6032 Act_Decl : Node_Id)
6034 Decl_Cunit : Node_Id;
6035 Body_Cunit : Node_Id;
6036 Citem : Node_Id;
6037 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6038 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6040 begin
6041 -- A new compilation unit node is built for the instance declaration
6043 Decl_Cunit :=
6044 Make_Compilation_Unit (Sloc (N),
6045 Context_Items => Empty_List,
6046 Unit => Act_Decl,
6047 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6049 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6051 -- The new compilation unit is linked to its body, but both share the
6052 -- same file, so we do not set Body_Required on the new unit so as not
6053 -- to create a spurious dependency on a non-existent body in the ali.
6054 -- This simplifies CodePeer unit traversal.
6056 -- We use the original instantiation compilation unit as the resulting
6057 -- compilation unit of the instance, since this is the main unit.
6059 Rewrite (N, Act_Body);
6061 -- Propagate the aspect specifications from the package body template to
6062 -- the instantiated version of the package body.
6064 if Has_Aspects (Act_Body) then
6065 Set_Aspect_Specifications
6066 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6067 end if;
6069 Body_Cunit := Parent (N);
6071 -- The two compilation unit nodes are linked by the Library_Unit field
6073 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6074 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6076 -- Preserve the private nature of the package if needed
6078 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6080 -- If the instance is not the main unit, its context, categorization
6081 -- and elaboration entity are not relevant to the compilation.
6083 if Body_Cunit /= Cunit (Main_Unit) then
6084 Make_Instance_Unit (Body_Cunit, In_Main => False);
6085 return;
6086 end if;
6088 -- The context clause items on the instantiation, which are now attached
6089 -- to the body compilation unit (since the body overwrote the original
6090 -- instantiation node), semantically belong on the spec, so copy them
6091 -- there. It's harmless to leave them on the body as well. In fact one
6092 -- could argue that they belong in both places.
6094 Citem := First (Context_Items (Body_Cunit));
6095 while Present (Citem) loop
6096 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6097 Next (Citem);
6098 end loop;
6100 -- Propagate categorization flags on packages, so that they appear in
6101 -- the ali file for the spec of the unit.
6103 if Ekind (New_Main) = E_Package then
6104 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6105 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6106 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6107 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6108 Set_Is_Remote_Call_Interface
6109 (Old_Main, Is_Remote_Call_Interface (New_Main));
6110 end if;
6112 -- Make entry in Units table, so that binder can generate call to
6113 -- elaboration procedure for body, if any.
6115 Make_Instance_Unit (Body_Cunit, In_Main => True);
6116 Main_Unit_Entity := New_Main;
6117 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6119 -- Build elaboration entity, since the instance may certainly generate
6120 -- elaboration code requiring a flag for protection.
6122 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6123 end Build_Instance_Compilation_Unit_Nodes;
6125 -----------------------------
6126 -- Check_Access_Definition --
6127 -----------------------------
6129 procedure Check_Access_Definition (N : Node_Id) is
6130 begin
6131 pragma Assert
6132 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6133 null;
6134 end Check_Access_Definition;
6136 -----------------------------------
6137 -- Check_Formal_Package_Instance --
6138 -----------------------------------
6140 -- If the formal has specific parameters, they must match those of the
6141 -- actual. Both of them are instances, and the renaming declarations for
6142 -- their formal parameters appear in the same order in both. The analyzed
6143 -- formal has been analyzed in the context of the current instance.
6145 procedure Check_Formal_Package_Instance
6146 (Formal_Pack : Entity_Id;
6147 Actual_Pack : Entity_Id)
6149 E1 : Entity_Id := First_Entity (Actual_Pack);
6150 E2 : Entity_Id := First_Entity (Formal_Pack);
6151 Prev_E1 : Entity_Id;
6153 Expr1 : Node_Id;
6154 Expr2 : Node_Id;
6156 procedure Check_Mismatch (B : Boolean);
6157 -- Common error routine for mismatch between the parameters of the
6158 -- actual instance and those of the formal package.
6160 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6161 -- The formal may come from a nested formal package, and the actual may
6162 -- have been constant-folded. To determine whether the two denote the
6163 -- same entity we may have to traverse several definitions to recover
6164 -- the ultimate entity that they refer to.
6166 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6167 -- The formal and the actual must be identical, but if both are
6168 -- given by attributes they end up renaming different generated bodies,
6169 -- and we must verify that the attributes themselves match.
6171 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6172 -- Similarly, if the formal comes from a nested formal package, the
6173 -- actual may designate the formal through multiple renamings, which
6174 -- have to be followed to determine the original variable in question.
6176 --------------------
6177 -- Check_Mismatch --
6178 --------------------
6180 procedure Check_Mismatch (B : Boolean) is
6181 -- A Formal_Type_Declaration for a derived private type is rewritten
6182 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6183 -- which is why we examine the original node.
6185 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6187 begin
6188 if Kind = N_Formal_Type_Declaration then
6189 return;
6191 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6192 N_Formal_Package_Declaration)
6193 or else Kind in N_Formal_Subprogram_Declaration
6194 then
6195 null;
6197 -- Ada 2012: If both formal and actual are incomplete types they
6198 -- are conformant.
6200 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6201 null;
6203 elsif B then
6204 Error_Msg_NE
6205 ("actual for & in actual instance does not match formal",
6206 Parent (Actual_Pack), E1);
6207 end if;
6208 end Check_Mismatch;
6210 --------------------------------
6211 -- Same_Instantiated_Constant --
6212 --------------------------------
6214 function Same_Instantiated_Constant
6215 (E1, E2 : Entity_Id) return Boolean
6217 Ent : Entity_Id;
6219 begin
6220 Ent := E2;
6221 while Present (Ent) loop
6222 if E1 = Ent then
6223 return True;
6225 elsif Ekind (Ent) /= E_Constant then
6226 return False;
6228 elsif Is_Entity_Name (Constant_Value (Ent)) then
6229 if Entity (Constant_Value (Ent)) = E1 then
6230 return True;
6231 else
6232 Ent := Entity (Constant_Value (Ent));
6233 end if;
6235 -- The actual may be a constant that has been folded. Recover
6236 -- original name.
6238 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6239 Ent := Entity (Original_Node (Constant_Value (Ent)));
6241 else
6242 return False;
6243 end if;
6244 end loop;
6246 return False;
6247 end Same_Instantiated_Constant;
6249 --------------------------------
6250 -- Same_Instantiated_Function --
6251 --------------------------------
6253 function Same_Instantiated_Function
6254 (E1, E2 : Entity_Id) return Boolean
6256 U1, U2 : Node_Id;
6257 begin
6258 if Alias (E1) = Alias (E2) then
6259 return True;
6261 elsif Present (Alias (E2)) then
6262 U1 := Original_Node (Unit_Declaration_Node (E1));
6263 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6265 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6266 and then Nkind (Name (U1)) = N_Attribute_Reference
6268 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6269 and then Nkind (Name (U2)) = N_Attribute_Reference
6271 and then
6272 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6273 else
6274 return False;
6275 end if;
6276 end Same_Instantiated_Function;
6278 --------------------------------
6279 -- Same_Instantiated_Variable --
6280 --------------------------------
6282 function Same_Instantiated_Variable
6283 (E1, E2 : Entity_Id) return Boolean
6285 function Original_Entity (E : Entity_Id) return Entity_Id;
6286 -- Follow chain of renamings to the ultimate ancestor
6288 ---------------------
6289 -- Original_Entity --
6290 ---------------------
6292 function Original_Entity (E : Entity_Id) return Entity_Id is
6293 Orig : Entity_Id;
6295 begin
6296 Orig := E;
6297 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6298 and then Present (Renamed_Object (Orig))
6299 and then Is_Entity_Name (Renamed_Object (Orig))
6300 loop
6301 Orig := Entity (Renamed_Object (Orig));
6302 end loop;
6304 return Orig;
6305 end Original_Entity;
6307 -- Start of processing for Same_Instantiated_Variable
6309 begin
6310 return Ekind (E1) = Ekind (E2)
6311 and then Original_Entity (E1) = Original_Entity (E2);
6312 end Same_Instantiated_Variable;
6314 -- Start of processing for Check_Formal_Package_Instance
6316 begin
6317 Prev_E1 := E1;
6318 while Present (E1) and then Present (E2) loop
6319 exit when Ekind (E1) = E_Package
6320 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6322 -- If the formal is the renaming of the formal package, this
6323 -- is the end of its formal part, which may occur before the
6324 -- end of the formal part in the actual in the presence of
6325 -- defaulted parameters in the formal package.
6327 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6328 and then Renamed_Entity (E2) = Scope (E2);
6330 -- The analysis of the actual may generate additional internal
6331 -- entities. If the formal is defaulted, there is no corresponding
6332 -- analysis and the internal entities must be skipped, until we
6333 -- find corresponding entities again.
6335 if Comes_From_Source (E2)
6336 and then not Comes_From_Source (E1)
6337 and then Chars (E1) /= Chars (E2)
6338 then
6339 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6340 Next_Entity (E1);
6341 end loop;
6342 end if;
6344 if No (E1) then
6345 return;
6347 -- Entities may be declared without full declaration, such as
6348 -- itypes and predefined operators (concatenation for arrays, eg).
6349 -- Skip it and keep the formal entity to find a later match for it.
6351 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6352 E1 := Prev_E1;
6353 goto Next_E;
6355 -- If the formal entity comes from a formal declaration, it was
6356 -- defaulted in the formal package, and no check is needed on it.
6358 elsif Nkind_In (Original_Node (Parent (E2)),
6359 N_Formal_Object_Declaration,
6360 N_Formal_Type_Declaration)
6361 then
6362 -- If the formal is a tagged type the corresponding class-wide
6363 -- type has been generated as well, and it must be skipped.
6365 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6366 Next_Entity (E2);
6367 end if;
6369 goto Next_E;
6371 -- Ditto for defaulted formal subprograms.
6373 elsif Is_Overloadable (E1)
6374 and then Nkind (Unit_Declaration_Node (E2)) in
6375 N_Formal_Subprogram_Declaration
6376 then
6377 goto Next_E;
6379 elsif Is_Type (E1) then
6381 -- Subtypes must statically match. E1, E2 are the local entities
6382 -- that are subtypes of the actuals. Itypes generated for other
6383 -- parameters need not be checked, the check will be performed
6384 -- on the parameters themselves.
6386 -- If E2 is a formal type declaration, it is a defaulted parameter
6387 -- and needs no checking.
6389 if not Is_Itype (E1) and then not Is_Itype (E2) then
6390 Check_Mismatch
6391 (not Is_Type (E2)
6392 or else Etype (E1) /= Etype (E2)
6393 or else not Subtypes_Statically_Match (E1, E2));
6394 end if;
6396 elsif Ekind (E1) = E_Constant then
6398 -- IN parameters must denote the same static value, or the same
6399 -- constant, or the literal null.
6401 Expr1 := Expression (Parent (E1));
6403 if Ekind (E2) /= E_Constant then
6404 Check_Mismatch (True);
6405 goto Next_E;
6406 else
6407 Expr2 := Expression (Parent (E2));
6408 end if;
6410 if Is_OK_Static_Expression (Expr1) then
6411 if not Is_OK_Static_Expression (Expr2) then
6412 Check_Mismatch (True);
6414 elsif Is_Discrete_Type (Etype (E1)) then
6415 declare
6416 V1 : constant Uint := Expr_Value (Expr1);
6417 V2 : constant Uint := Expr_Value (Expr2);
6418 begin
6419 Check_Mismatch (V1 /= V2);
6420 end;
6422 elsif Is_Real_Type (Etype (E1)) then
6423 declare
6424 V1 : constant Ureal := Expr_Value_R (Expr1);
6425 V2 : constant Ureal := Expr_Value_R (Expr2);
6426 begin
6427 Check_Mismatch (V1 /= V2);
6428 end;
6430 elsif Is_String_Type (Etype (E1))
6431 and then Nkind (Expr1) = N_String_Literal
6432 then
6433 if Nkind (Expr2) /= N_String_Literal then
6434 Check_Mismatch (True);
6435 else
6436 Check_Mismatch
6437 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6438 end if;
6439 end if;
6441 elsif Is_Entity_Name (Expr1) then
6442 if Is_Entity_Name (Expr2) then
6443 if Entity (Expr1) = Entity (Expr2) then
6444 null;
6445 else
6446 Check_Mismatch
6447 (not Same_Instantiated_Constant
6448 (Entity (Expr1), Entity (Expr2)));
6449 end if;
6451 else
6452 Check_Mismatch (True);
6453 end if;
6455 elsif Is_Entity_Name (Original_Node (Expr1))
6456 and then Is_Entity_Name (Expr2)
6457 and then Same_Instantiated_Constant
6458 (Entity (Original_Node (Expr1)), Entity (Expr2))
6459 then
6460 null;
6462 elsif Nkind (Expr1) = N_Null then
6463 Check_Mismatch (Nkind (Expr1) /= N_Null);
6465 else
6466 Check_Mismatch (True);
6467 end if;
6469 elsif Ekind (E1) = E_Variable then
6470 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6472 elsif Ekind (E1) = E_Package then
6473 Check_Mismatch
6474 (Ekind (E1) /= Ekind (E2)
6475 or else (Present (Renamed_Object (E2))
6476 and then Renamed_Object (E1) /=
6477 Renamed_Object (E2)));
6479 elsif Is_Overloadable (E1) then
6480 -- Verify that the actual subprograms match. Note that actuals
6481 -- that are attributes are rewritten as subprograms. If the
6482 -- subprogram in the formal package is defaulted, no check is
6483 -- needed. Note that this can only happen in Ada 2005 when the
6484 -- formal package can be partially parameterized.
6486 if Nkind (Unit_Declaration_Node (E1)) =
6487 N_Subprogram_Renaming_Declaration
6488 and then From_Default (Unit_Declaration_Node (E1))
6489 then
6490 null;
6492 -- If the formal package has an "others" box association that
6493 -- covers this formal, there is no need for a check either.
6495 elsif Nkind (Unit_Declaration_Node (E2)) in
6496 N_Formal_Subprogram_Declaration
6497 and then Box_Present (Unit_Declaration_Node (E2))
6498 then
6499 null;
6501 -- No check needed if subprogram is a defaulted null procedure
6503 elsif No (Alias (E2))
6504 and then Ekind (E2) = E_Procedure
6505 and then
6506 Null_Present (Specification (Unit_Declaration_Node (E2)))
6507 then
6508 null;
6510 -- Otherwise the actual in the formal and the actual in the
6511 -- instantiation of the formal must match, up to renamings.
6513 else
6514 Check_Mismatch
6515 (Ekind (E2) /= Ekind (E1)
6516 or else not Same_Instantiated_Function (E1, E2));
6517 end if;
6519 else
6520 raise Program_Error;
6521 end if;
6523 <<Next_E>>
6524 Prev_E1 := E1;
6525 Next_Entity (E1);
6526 Next_Entity (E2);
6527 end loop;
6528 end Check_Formal_Package_Instance;
6530 ---------------------------
6531 -- Check_Formal_Packages --
6532 ---------------------------
6534 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6535 E : Entity_Id;
6536 Formal_P : Entity_Id;
6537 Formal_Decl : Node_Id;
6539 begin
6540 -- Iterate through the declarations in the instance, looking for package
6541 -- renaming declarations that denote instances of formal packages. Stop
6542 -- when we find the renaming of the current package itself. The
6543 -- declaration for a formal package without a box is followed by an
6544 -- internal entity that repeats the instantiation.
6546 E := First_Entity (P_Id);
6547 while Present (E) loop
6548 if Ekind (E) = E_Package then
6549 if Renamed_Object (E) = P_Id then
6550 exit;
6552 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6553 null;
6555 else
6556 Formal_Decl := Parent (Associated_Formal_Package (E));
6558 -- Nothing to check if the formal has a box or an others_clause
6559 -- (necessarily with a box).
6561 if Box_Present (Formal_Decl) then
6562 null;
6564 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6565 N_Others_Choice
6566 then
6567 -- The internal validating package was generated but formal
6568 -- and instance are known to be compatible.
6570 Formal_P := Next_Entity (E);
6571 Remove (Unit_Declaration_Node (Formal_P));
6573 else
6574 Formal_P := Next_Entity (E);
6576 -- If the instance is within an enclosing instance body
6577 -- there is no need to verify the legality of current formal
6578 -- packages because they were legal in the generic body.
6579 -- This optimization may be applicable elsewhere, and it
6580 -- also removes spurious errors that may arise with
6581 -- on-the-fly inlining and confusion between private and
6582 -- full views.
6584 if not In_Instance_Body then
6585 Check_Formal_Package_Instance (Formal_P, E);
6586 end if;
6588 -- After checking, remove the internal validating package.
6589 -- It is only needed for semantic checks, and as it may
6590 -- contain generic formal declarations it should not reach
6591 -- gigi.
6593 Remove (Unit_Declaration_Node (Formal_P));
6594 end if;
6595 end if;
6596 end if;
6598 Next_Entity (E);
6599 end loop;
6600 end Check_Formal_Packages;
6602 ---------------------------------
6603 -- Check_Forward_Instantiation --
6604 ---------------------------------
6606 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6607 S : Entity_Id;
6608 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6610 begin
6611 -- The instantiation appears before the generic body if we are in the
6612 -- scope of the unit containing the generic, either in its spec or in
6613 -- the package body, and before the generic body.
6615 if Ekind (Gen_Comp) = E_Package_Body then
6616 Gen_Comp := Spec_Entity (Gen_Comp);
6617 end if;
6619 if In_Open_Scopes (Gen_Comp)
6620 and then No (Corresponding_Body (Decl))
6621 then
6622 S := Current_Scope;
6624 while Present (S)
6625 and then not Is_Compilation_Unit (S)
6626 and then not Is_Child_Unit (S)
6627 loop
6628 if Ekind (S) = E_Package then
6629 Set_Has_Forward_Instantiation (S);
6630 end if;
6632 S := Scope (S);
6633 end loop;
6634 end if;
6635 end Check_Forward_Instantiation;
6637 ---------------------------
6638 -- Check_Generic_Actuals --
6639 ---------------------------
6641 -- The visibility of the actuals may be different between the point of
6642 -- generic instantiation and the instantiation of the body.
6644 procedure Check_Generic_Actuals
6645 (Instance : Entity_Id;
6646 Is_Formal_Box : Boolean)
6648 E : Entity_Id;
6649 Astype : Entity_Id;
6651 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6652 -- For a formal that is an array type, the component type is often a
6653 -- previous formal in the same unit. The privacy status of the component
6654 -- type will have been examined earlier in the traversal of the
6655 -- corresponding actuals, and this status should not be modified for
6656 -- the array (sub)type itself. However, if the base type of the array
6657 -- (sub)type is private, its full view must be restored in the body to
6658 -- be consistent with subsequent index subtypes, etc.
6660 -- To detect this case we have to rescan the list of formals, which is
6661 -- usually short enough to ignore the resulting inefficiency.
6663 -----------------------------
6664 -- Denotes_Previous_Actual --
6665 -----------------------------
6667 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6668 Prev : Entity_Id;
6670 begin
6671 Prev := First_Entity (Instance);
6672 while Present (Prev) loop
6673 if Is_Type (Prev)
6674 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6675 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6676 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6677 then
6678 return True;
6680 elsif Prev = E then
6681 return False;
6683 else
6684 Next_Entity (Prev);
6685 end if;
6686 end loop;
6688 return False;
6689 end Denotes_Previous_Actual;
6691 -- Start of processing for Check_Generic_Actuals
6693 begin
6694 E := First_Entity (Instance);
6695 while Present (E) loop
6696 if Is_Type (E)
6697 and then Nkind (Parent (E)) = N_Subtype_Declaration
6698 and then Scope (Etype (E)) /= Instance
6699 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6700 then
6701 if Is_Array_Type (E)
6702 and then not Is_Private_Type (Etype (E))
6703 and then Denotes_Previous_Actual (Component_Type (E))
6704 then
6705 null;
6706 else
6707 Check_Private_View (Subtype_Indication (Parent (E)));
6708 end if;
6710 Set_Is_Generic_Actual_Type (E, True);
6711 Set_Is_Hidden (E, False);
6712 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6714 -- We constructed the generic actual type as a subtype of the
6715 -- supplied type. This means that it normally would not inherit
6716 -- subtype specific attributes of the actual, which is wrong for
6717 -- the generic case.
6719 Astype := Ancestor_Subtype (E);
6721 if No (Astype) then
6723 -- This can happen when E is an itype that is the full view of
6724 -- a private type completed, e.g. with a constrained array. In
6725 -- that case, use the first subtype, which will carry size
6726 -- information. The base type itself is unconstrained and will
6727 -- not carry it.
6729 Astype := First_Subtype (E);
6730 end if;
6732 Set_Size_Info (E, (Astype));
6733 Set_RM_Size (E, RM_Size (Astype));
6734 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6736 if Is_Discrete_Or_Fixed_Point_Type (E) then
6737 Set_RM_Size (E, RM_Size (Astype));
6739 -- In nested instances, the base type of an access actual may
6740 -- itself be private, and need to be exchanged.
6742 elsif Is_Access_Type (E)
6743 and then Is_Private_Type (Etype (E))
6744 then
6745 Check_Private_View
6746 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6747 end if;
6749 elsif Ekind (E) = E_Package then
6751 -- If this is the renaming for the current instance, we're done.
6752 -- Otherwise it is a formal package. If the corresponding formal
6753 -- was declared with a box, the (instantiations of the) generic
6754 -- formal part are also visible. Otherwise, ignore the entity
6755 -- created to validate the actuals.
6757 if Renamed_Object (E) = Instance then
6758 exit;
6760 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6761 null;
6763 -- The visibility of a formal of an enclosing generic is already
6764 -- correct.
6766 elsif Denotes_Formal_Package (E) then
6767 null;
6769 elsif Present (Associated_Formal_Package (E))
6770 and then not Is_Generic_Formal (E)
6771 then
6772 if Box_Present (Parent (Associated_Formal_Package (E))) then
6773 Check_Generic_Actuals (Renamed_Object (E), True);
6775 else
6776 Check_Generic_Actuals (Renamed_Object (E), False);
6777 end if;
6779 Set_Is_Hidden (E, False);
6780 end if;
6782 -- If this is a subprogram instance (in a wrapper package) the
6783 -- actual is fully visible.
6785 elsif Is_Wrapper_Package (Instance) then
6786 Set_Is_Hidden (E, False);
6788 -- If the formal package is declared with a box, or if the formal
6789 -- parameter is defaulted, it is visible in the body.
6791 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6792 Set_Is_Hidden (E, False);
6793 end if;
6795 if Ekind (E) = E_Constant then
6797 -- If the type of the actual is a private type declared in the
6798 -- enclosing scope of the generic unit, the body of the generic
6799 -- sees the full view of the type (because it has to appear in
6800 -- the corresponding package body). If the type is private now,
6801 -- exchange views to restore the proper visiblity in the instance.
6803 declare
6804 Typ : constant Entity_Id := Base_Type (Etype (E));
6805 -- The type of the actual
6807 Gen_Id : Entity_Id;
6808 -- The generic unit
6810 Parent_Scope : Entity_Id;
6811 -- The enclosing scope of the generic unit
6813 begin
6814 if Is_Wrapper_Package (Instance) then
6815 Gen_Id :=
6816 Generic_Parent
6817 (Specification
6818 (Unit_Declaration_Node
6819 (Related_Instance (Instance))));
6820 else
6821 Gen_Id :=
6822 Generic_Parent (Package_Specification (Instance));
6823 end if;
6825 Parent_Scope := Scope (Gen_Id);
6827 -- The exchange is only needed if the generic is defined
6828 -- within a package which is not a common ancestor of the
6829 -- scope of the instance, and is not already in scope.
6831 if Is_Private_Type (Typ)
6832 and then Scope (Typ) = Parent_Scope
6833 and then Scope (Instance) /= Parent_Scope
6834 and then Ekind (Parent_Scope) = E_Package
6835 and then not Is_Child_Unit (Gen_Id)
6836 then
6837 Switch_View (Typ);
6839 -- If the type of the entity is a subtype, it may also have
6840 -- to be made visible, together with the base type of its
6841 -- full view, after exchange.
6843 if Is_Private_Type (Etype (E)) then
6844 Switch_View (Etype (E));
6845 Switch_View (Base_Type (Etype (E)));
6846 end if;
6847 end if;
6848 end;
6849 end if;
6851 Next_Entity (E);
6852 end loop;
6853 end Check_Generic_Actuals;
6855 ------------------------------
6856 -- Check_Generic_Child_Unit --
6857 ------------------------------
6859 procedure Check_Generic_Child_Unit
6860 (Gen_Id : Node_Id;
6861 Parent_Installed : in out Boolean)
6863 Loc : constant Source_Ptr := Sloc (Gen_Id);
6864 Gen_Par : Entity_Id := Empty;
6865 E : Entity_Id;
6866 Inst_Par : Entity_Id;
6867 S : Node_Id;
6869 function Find_Generic_Child
6870 (Scop : Entity_Id;
6871 Id : Node_Id) return Entity_Id;
6872 -- Search generic parent for possible child unit with the given name
6874 function In_Enclosing_Instance return Boolean;
6875 -- Within an instance of the parent, the child unit may be denoted by
6876 -- a simple name, or an abbreviated expanded name. Examine enclosing
6877 -- scopes to locate a possible parent instantiation.
6879 ------------------------
6880 -- Find_Generic_Child --
6881 ------------------------
6883 function Find_Generic_Child
6884 (Scop : Entity_Id;
6885 Id : Node_Id) return Entity_Id
6887 E : Entity_Id;
6889 begin
6890 -- If entity of name is already set, instance has already been
6891 -- resolved, e.g. in an enclosing instantiation.
6893 if Present (Entity (Id)) then
6894 if Scope (Entity (Id)) = Scop then
6895 return Entity (Id);
6896 else
6897 return Empty;
6898 end if;
6900 else
6901 E := First_Entity (Scop);
6902 while Present (E) loop
6903 if Chars (E) = Chars (Id)
6904 and then Is_Child_Unit (E)
6905 then
6906 if Is_Child_Unit (E)
6907 and then not Is_Visible_Lib_Unit (E)
6908 then
6909 Error_Msg_NE
6910 ("generic child unit& is not visible", Gen_Id, E);
6911 end if;
6913 Set_Entity (Id, E);
6914 return E;
6915 end if;
6917 Next_Entity (E);
6918 end loop;
6920 return Empty;
6921 end if;
6922 end Find_Generic_Child;
6924 ---------------------------
6925 -- In_Enclosing_Instance --
6926 ---------------------------
6928 function In_Enclosing_Instance return Boolean is
6929 Enclosing_Instance : Node_Id;
6930 Instance_Decl : Node_Id;
6932 begin
6933 -- We do not inline any call that contains instantiations, except
6934 -- for instantiations of Unchecked_Conversion, so if we are within
6935 -- an inlined body the current instance does not require parents.
6937 if In_Inlined_Body then
6938 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6939 return False;
6940 end if;
6942 -- Loop to check enclosing scopes
6944 Enclosing_Instance := Current_Scope;
6945 while Present (Enclosing_Instance) loop
6946 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6948 if Ekind (Enclosing_Instance) = E_Package
6949 and then Is_Generic_Instance (Enclosing_Instance)
6950 and then Present
6951 (Generic_Parent (Specification (Instance_Decl)))
6952 then
6953 -- Check whether the generic we are looking for is a child of
6954 -- this instance.
6956 E := Find_Generic_Child
6957 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6958 exit when Present (E);
6960 else
6961 E := Empty;
6962 end if;
6964 Enclosing_Instance := Scope (Enclosing_Instance);
6965 end loop;
6967 if No (E) then
6969 -- Not a child unit
6971 Analyze (Gen_Id);
6972 return False;
6974 else
6975 Rewrite (Gen_Id,
6976 Make_Expanded_Name (Loc,
6977 Chars => Chars (E),
6978 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6979 Selector_Name => New_Occurrence_Of (E, Loc)));
6981 Set_Entity (Gen_Id, E);
6982 Set_Etype (Gen_Id, Etype (E));
6983 Parent_Installed := False; -- Already in scope.
6984 return True;
6985 end if;
6986 end In_Enclosing_Instance;
6988 -- Start of processing for Check_Generic_Child_Unit
6990 begin
6991 -- If the name of the generic is given by a selected component, it may
6992 -- be the name of a generic child unit, and the prefix is the name of an
6993 -- instance of the parent, in which case the child unit must be visible.
6994 -- If this instance is not in scope, it must be placed there and removed
6995 -- after instantiation, because what is being instantiated is not the
6996 -- original child, but the corresponding child present in the instance
6997 -- of the parent.
6999 -- If the child is instantiated within the parent, it can be given by
7000 -- a simple name. In this case the instance is already in scope, but
7001 -- the child generic must be recovered from the generic parent as well.
7003 if Nkind (Gen_Id) = N_Selected_Component then
7004 S := Selector_Name (Gen_Id);
7005 Analyze (Prefix (Gen_Id));
7006 Inst_Par := Entity (Prefix (Gen_Id));
7008 if Ekind (Inst_Par) = E_Package
7009 and then Present (Renamed_Object (Inst_Par))
7010 then
7011 Inst_Par := Renamed_Object (Inst_Par);
7012 end if;
7014 if Ekind (Inst_Par) = E_Package then
7015 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7016 Gen_Par := Generic_Parent (Parent (Inst_Par));
7018 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7019 and then
7020 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7021 then
7022 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7023 end if;
7025 elsif Ekind (Inst_Par) = E_Generic_Package
7026 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7027 then
7028 -- A formal package may be a real child package, and not the
7029 -- implicit instance within a parent. In this case the child is
7030 -- not visible and has to be retrieved explicitly as well.
7032 Gen_Par := Inst_Par;
7033 end if;
7035 if Present (Gen_Par) then
7037 -- The prefix denotes an instantiation. The entity itself may be a
7038 -- nested generic, or a child unit.
7040 E := Find_Generic_Child (Gen_Par, S);
7042 if Present (E) then
7043 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7044 Set_Entity (Gen_Id, E);
7045 Set_Etype (Gen_Id, Etype (E));
7046 Set_Entity (S, E);
7047 Set_Etype (S, Etype (E));
7049 -- Indicate that this is a reference to the parent
7051 if In_Extended_Main_Source_Unit (Gen_Id) then
7052 Set_Is_Instantiated (Inst_Par);
7053 end if;
7055 -- A common mistake is to replicate the naming scheme of a
7056 -- hierarchy by instantiating a generic child directly, rather
7057 -- than the implicit child in a parent instance:
7059 -- generic .. package Gpar is ..
7060 -- generic .. package Gpar.Child is ..
7061 -- package Par is new Gpar ();
7063 -- with Gpar.Child;
7064 -- package Par.Child is new Gpar.Child ();
7065 -- rather than Par.Child
7067 -- In this case the instantiation is within Par, which is an
7068 -- instance, but Gpar does not denote Par because we are not IN
7069 -- the instance of Gpar, so this is illegal. The test below
7070 -- recognizes this particular case.
7072 if Is_Child_Unit (E)
7073 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7074 and then (not In_Instance
7075 or else Nkind (Parent (Parent (Gen_Id))) =
7076 N_Compilation_Unit)
7077 then
7078 Error_Msg_N
7079 ("prefix of generic child unit must be instance of parent",
7080 Gen_Id);
7081 end if;
7083 if not In_Open_Scopes (Inst_Par)
7084 and then Nkind (Parent (Gen_Id)) not in
7085 N_Generic_Renaming_Declaration
7086 then
7087 Install_Parent (Inst_Par);
7088 Parent_Installed := True;
7090 elsif In_Open_Scopes (Inst_Par) then
7092 -- If the parent is already installed, install the actuals
7093 -- for its formal packages. This is necessary when the child
7094 -- instance is a child of the parent instance: in this case,
7095 -- the parent is placed on the scope stack but the formal
7096 -- packages are not made visible.
7098 Install_Formal_Packages (Inst_Par);
7099 end if;
7101 else
7102 -- If the generic parent does not contain an entity that
7103 -- corresponds to the selector, the instance doesn't either.
7104 -- Analyzing the node will yield the appropriate error message.
7105 -- If the entity is not a child unit, then it is an inner
7106 -- generic in the parent.
7108 Analyze (Gen_Id);
7109 end if;
7111 else
7112 Analyze (Gen_Id);
7114 if Is_Child_Unit (Entity (Gen_Id))
7115 and then
7116 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7117 and then not In_Open_Scopes (Inst_Par)
7118 then
7119 Install_Parent (Inst_Par);
7120 Parent_Installed := True;
7122 -- The generic unit may be the renaming of the implicit child
7123 -- present in an instance. In that case the parent instance is
7124 -- obtained from the name of the renamed entity.
7126 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7127 and then Present (Renamed_Entity (Entity (Gen_Id)))
7128 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7129 then
7130 declare
7131 Renamed_Package : constant Node_Id :=
7132 Name (Parent (Entity (Gen_Id)));
7133 begin
7134 if Nkind (Renamed_Package) = N_Expanded_Name then
7135 Inst_Par := Entity (Prefix (Renamed_Package));
7136 Install_Parent (Inst_Par);
7137 Parent_Installed := True;
7138 end if;
7139 end;
7140 end if;
7141 end if;
7143 elsif Nkind (Gen_Id) = N_Expanded_Name then
7145 -- Entity already present, analyze prefix, whose meaning may be an
7146 -- instance in the current context. If it is an instance of a
7147 -- relative within another, the proper parent may still have to be
7148 -- installed, if they are not of the same generation.
7150 Analyze (Prefix (Gen_Id));
7152 -- Prevent cascaded errors
7154 if Etype (Prefix (Gen_Id)) = Any_Type then
7155 return;
7156 end if;
7158 -- In the unlikely case that a local declaration hides the name of
7159 -- the parent package, locate it on the homonym chain. If the context
7160 -- is an instance of the parent, the renaming entity is flagged as
7161 -- such.
7163 Inst_Par := Entity (Prefix (Gen_Id));
7164 while Present (Inst_Par)
7165 and then not Is_Package_Or_Generic_Package (Inst_Par)
7166 loop
7167 Inst_Par := Homonym (Inst_Par);
7168 end loop;
7170 pragma Assert (Present (Inst_Par));
7171 Set_Entity (Prefix (Gen_Id), Inst_Par);
7173 if In_Enclosing_Instance then
7174 null;
7176 elsif Present (Entity (Gen_Id))
7177 and then Is_Child_Unit (Entity (Gen_Id))
7178 and then not In_Open_Scopes (Inst_Par)
7179 then
7180 Install_Parent (Inst_Par);
7181 Parent_Installed := True;
7182 end if;
7184 elsif In_Enclosing_Instance then
7186 -- The child unit is found in some enclosing scope
7188 null;
7190 else
7191 Analyze (Gen_Id);
7193 -- If this is the renaming of the implicit child in a parent
7194 -- instance, recover the parent name and install it.
7196 if Is_Entity_Name (Gen_Id) then
7197 E := Entity (Gen_Id);
7199 if Is_Generic_Unit (E)
7200 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7201 and then Is_Child_Unit (Renamed_Object (E))
7202 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7203 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7204 then
7205 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7206 Inst_Par := Entity (Prefix (Gen_Id));
7208 if not In_Open_Scopes (Inst_Par) then
7209 Install_Parent (Inst_Par);
7210 Parent_Installed := True;
7211 end if;
7213 -- If it is a child unit of a non-generic parent, it may be
7214 -- use-visible and given by a direct name. Install parent as
7215 -- for other cases.
7217 elsif Is_Generic_Unit (E)
7218 and then Is_Child_Unit (E)
7219 and then
7220 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7221 and then not Is_Generic_Unit (Scope (E))
7222 then
7223 if not In_Open_Scopes (Scope (E)) then
7224 Install_Parent (Scope (E));
7225 Parent_Installed := True;
7226 end if;
7227 end if;
7228 end if;
7229 end if;
7230 end Check_Generic_Child_Unit;
7232 -----------------------------
7233 -- Check_Hidden_Child_Unit --
7234 -----------------------------
7236 procedure Check_Hidden_Child_Unit
7237 (N : Node_Id;
7238 Gen_Unit : Entity_Id;
7239 Act_Decl_Id : Entity_Id)
7241 Gen_Id : constant Node_Id := Name (N);
7243 begin
7244 if Is_Child_Unit (Gen_Unit)
7245 and then Is_Child_Unit (Act_Decl_Id)
7246 and then Nkind (Gen_Id) = N_Expanded_Name
7247 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7248 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7249 then
7250 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7251 Error_Msg_NE
7252 ("generic unit & is implicitly declared in &",
7253 Defining_Unit_Name (N), Gen_Unit);
7254 Error_Msg_N ("\instance must have different name",
7255 Defining_Unit_Name (N));
7256 end if;
7257 end Check_Hidden_Child_Unit;
7259 ------------------------
7260 -- Check_Private_View --
7261 ------------------------
7263 procedure Check_Private_View (N : Node_Id) is
7264 T : constant Entity_Id := Etype (N);
7265 BT : Entity_Id;
7267 begin
7268 -- Exchange views if the type was not private in the generic but is
7269 -- private at the point of instantiation. Do not exchange views if
7270 -- the scope of the type is in scope. This can happen if both generic
7271 -- and instance are sibling units, or if type is defined in a parent.
7272 -- In this case the visibility of the type will be correct for all
7273 -- semantic checks.
7275 if Present (T) then
7276 BT := Base_Type (T);
7278 if Is_Private_Type (T)
7279 and then not Has_Private_View (N)
7280 and then Present (Full_View (T))
7281 and then not In_Open_Scopes (Scope (T))
7282 then
7283 -- In the generic, the full type was visible. Save the private
7284 -- entity, for subsequent exchange.
7286 Switch_View (T);
7288 elsif Has_Private_View (N)
7289 and then not Is_Private_Type (T)
7290 and then not Has_Been_Exchanged (T)
7291 and then Etype (Get_Associated_Node (N)) /= T
7292 then
7293 -- Only the private declaration was visible in the generic. If
7294 -- the type appears in a subtype declaration, the subtype in the
7295 -- instance must have a view compatible with that of its parent,
7296 -- which must be exchanged (see corresponding code in Restore_
7297 -- Private_Views). Otherwise, if the type is defined in a parent
7298 -- unit, leave full visibility within instance, which is safe.
7300 if In_Open_Scopes (Scope (Base_Type (T)))
7301 and then not Is_Private_Type (Base_Type (T))
7302 and then Comes_From_Source (Base_Type (T))
7303 then
7304 null;
7306 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7307 or else not In_Private_Part (Scope (Base_Type (T)))
7308 then
7309 Prepend_Elmt (T, Exchanged_Views);
7310 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7311 end if;
7313 -- For composite types with inconsistent representation exchange
7314 -- component types accordingly.
7316 elsif Is_Access_Type (T)
7317 and then Is_Private_Type (Designated_Type (T))
7318 and then not Has_Private_View (N)
7319 and then Present (Full_View (Designated_Type (T)))
7320 then
7321 Switch_View (Designated_Type (T));
7323 elsif Is_Array_Type (T) then
7324 if Is_Private_Type (Component_Type (T))
7325 and then not Has_Private_View (N)
7326 and then Present (Full_View (Component_Type (T)))
7327 then
7328 Switch_View (Component_Type (T));
7329 end if;
7331 -- The normal exchange mechanism relies on the setting of a
7332 -- flag on the reference in the generic. However, an additional
7333 -- mechanism is needed for types that are not explicitly
7334 -- mentioned in the generic, but may be needed in expanded code
7335 -- in the instance. This includes component types of arrays and
7336 -- designated types of access types. This processing must also
7337 -- include the index types of arrays which we take care of here.
7339 declare
7340 Indx : Node_Id;
7341 Typ : Entity_Id;
7343 begin
7344 Indx := First_Index (T);
7345 while Present (Indx) loop
7346 Typ := Base_Type (Etype (Indx));
7348 if Is_Private_Type (Typ)
7349 and then Present (Full_View (Typ))
7350 then
7351 Switch_View (Typ);
7352 end if;
7354 Next_Index (Indx);
7355 end loop;
7356 end;
7358 elsif Is_Private_Type (T)
7359 and then Present (Full_View (T))
7360 and then Is_Array_Type (Full_View (T))
7361 and then Is_Private_Type (Component_Type (Full_View (T)))
7362 then
7363 Switch_View (T);
7365 -- Finally, a non-private subtype may have a private base type, which
7366 -- must be exchanged for consistency. This can happen when a package
7367 -- body is instantiated, when the scope stack is empty but in fact
7368 -- the subtype and the base type are declared in an enclosing scope.
7370 -- Note that in this case we introduce an inconsistency in the view
7371 -- set, because we switch the base type BT, but there could be some
7372 -- private dependent subtypes of BT which remain unswitched. Such
7373 -- subtypes might need to be switched at a later point (see specific
7374 -- provision for that case in Switch_View).
7376 elsif not Is_Private_Type (T)
7377 and then not Has_Private_View (N)
7378 and then Is_Private_Type (BT)
7379 and then Present (Full_View (BT))
7380 and then not Is_Generic_Type (BT)
7381 and then not In_Open_Scopes (BT)
7382 then
7383 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7384 Exchange_Declarations (BT);
7385 end if;
7386 end if;
7387 end Check_Private_View;
7389 -----------------------------
7390 -- Check_Hidden_Primitives --
7391 -----------------------------
7393 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7394 Actual : Node_Id;
7395 Gen_T : Entity_Id;
7396 Result : Elist_Id := No_Elist;
7398 begin
7399 if No (Assoc_List) then
7400 return No_Elist;
7401 end if;
7403 -- Traverse the list of associations between formals and actuals
7404 -- searching for renamings of tagged types
7406 Actual := First (Assoc_List);
7407 while Present (Actual) loop
7408 if Nkind (Actual) = N_Subtype_Declaration then
7409 Gen_T := Generic_Parent_Type (Actual);
7411 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7413 -- Traverse the list of primitives of the actual types
7414 -- searching for hidden primitives that are visible in the
7415 -- corresponding generic formal; leave them visible and
7416 -- append them to Result to restore their decoration later.
7418 Install_Hidden_Primitives
7419 (Prims_List => Result,
7420 Gen_T => Gen_T,
7421 Act_T => Entity (Subtype_Indication (Actual)));
7422 end if;
7423 end if;
7425 Next (Actual);
7426 end loop;
7428 return Result;
7429 end Check_Hidden_Primitives;
7431 --------------------------
7432 -- Contains_Instance_Of --
7433 --------------------------
7435 function Contains_Instance_Of
7436 (Inner : Entity_Id;
7437 Outer : Entity_Id;
7438 N : Node_Id) return Boolean
7440 Elmt : Elmt_Id;
7441 Scop : Entity_Id;
7443 begin
7444 Scop := Outer;
7446 -- Verify that there are no circular instantiations. We check whether
7447 -- the unit contains an instance of the current scope or some enclosing
7448 -- scope (in case one of the instances appears in a subunit). Longer
7449 -- circularities involving subunits might seem too pathological to
7450 -- consider, but they were not too pathological for the authors of
7451 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7452 -- enclosing generic scopes as containing an instance.
7454 loop
7455 -- Within a generic subprogram body, the scope is not generic, to
7456 -- allow for recursive subprograms. Use the declaration to determine
7457 -- whether this is a generic unit.
7459 if Ekind (Scop) = E_Generic_Package
7460 or else (Is_Subprogram (Scop)
7461 and then Nkind (Unit_Declaration_Node (Scop)) =
7462 N_Generic_Subprogram_Declaration)
7463 then
7464 Elmt := First_Elmt (Inner_Instances (Inner));
7466 while Present (Elmt) loop
7467 if Node (Elmt) = Scop then
7468 Error_Msg_Node_2 := Inner;
7469 Error_Msg_NE
7470 ("circular Instantiation: & instantiated within &!",
7471 N, Scop);
7472 return True;
7474 elsif Node (Elmt) = Inner then
7475 return True;
7477 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7478 Error_Msg_Node_2 := Inner;
7479 Error_Msg_NE
7480 ("circular Instantiation: & instantiated within &!",
7481 N, Node (Elmt));
7482 return True;
7483 end if;
7485 Next_Elmt (Elmt);
7486 end loop;
7488 -- Indicate that Inner is being instantiated within Scop
7490 Append_Elmt (Inner, Inner_Instances (Scop));
7491 end if;
7493 if Scop = Standard_Standard then
7494 exit;
7495 else
7496 Scop := Scope (Scop);
7497 end if;
7498 end loop;
7500 return False;
7501 end Contains_Instance_Of;
7503 -----------------------
7504 -- Copy_Generic_Node --
7505 -----------------------
7507 function Copy_Generic_Node
7508 (N : Node_Id;
7509 Parent_Id : Node_Id;
7510 Instantiating : Boolean) return Node_Id
7512 Ent : Entity_Id;
7513 New_N : Node_Id;
7515 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7516 -- Check the given value of one of the Fields referenced by the current
7517 -- node to determine whether to copy it recursively. The field may hold
7518 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7519 -- Char) in which case it need not be copied.
7521 procedure Copy_Descendants;
7522 -- Common utility for various nodes
7524 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7525 -- Make copy of element list
7527 function Copy_Generic_List
7528 (L : List_Id;
7529 Parent_Id : Node_Id) return List_Id;
7530 -- Apply Copy_Node recursively to the members of a node list
7532 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7533 -- True if an identifier is part of the defining program unit name of
7534 -- a child unit. The entity of such an identifier must be kept (for
7535 -- ASIS use) even though as the name of an enclosing generic it would
7536 -- otherwise not be preserved in the generic tree.
7538 ----------------------
7539 -- Copy_Descendants --
7540 ----------------------
7542 procedure Copy_Descendants is
7543 use Atree.Unchecked_Access;
7544 -- This code section is part of the implementation of an untyped
7545 -- tree traversal, so it needs direct access to node fields.
7547 begin
7548 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7549 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7550 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7551 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7552 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7553 end Copy_Descendants;
7555 -----------------------------
7556 -- Copy_Generic_Descendant --
7557 -----------------------------
7559 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7560 begin
7561 if D = Union_Id (Empty) then
7562 return D;
7564 elsif D in Node_Range then
7565 return Union_Id
7566 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7568 elsif D in List_Range then
7569 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7571 elsif D in Elist_Range then
7572 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7574 -- Nothing else is copyable (e.g. Uint values), return as is
7576 else
7577 return D;
7578 end if;
7579 end Copy_Generic_Descendant;
7581 ------------------------
7582 -- Copy_Generic_Elist --
7583 ------------------------
7585 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7586 M : Elmt_Id;
7587 L : Elist_Id;
7589 begin
7590 if Present (E) then
7591 L := New_Elmt_List;
7592 M := First_Elmt (E);
7593 while Present (M) loop
7594 Append_Elmt
7595 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7596 Next_Elmt (M);
7597 end loop;
7599 return L;
7601 else
7602 return No_Elist;
7603 end if;
7604 end Copy_Generic_Elist;
7606 -----------------------
7607 -- Copy_Generic_List --
7608 -----------------------
7610 function Copy_Generic_List
7611 (L : List_Id;
7612 Parent_Id : Node_Id) return List_Id
7614 N : Node_Id;
7615 New_L : List_Id;
7617 begin
7618 if Present (L) then
7619 New_L := New_List;
7620 Set_Parent (New_L, Parent_Id);
7622 N := First (L);
7623 while Present (N) loop
7624 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7625 Next (N);
7626 end loop;
7628 return New_L;
7630 else
7631 return No_List;
7632 end if;
7633 end Copy_Generic_List;
7635 ---------------------------
7636 -- In_Defining_Unit_Name --
7637 ---------------------------
7639 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7640 begin
7641 return
7642 Present (Parent (Nam))
7643 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7644 or else
7645 (Nkind (Parent (Nam)) = N_Expanded_Name
7646 and then In_Defining_Unit_Name (Parent (Nam))));
7647 end In_Defining_Unit_Name;
7649 -- Start of processing for Copy_Generic_Node
7651 begin
7652 if N = Empty then
7653 return N;
7654 end if;
7656 New_N := New_Copy (N);
7658 -- Copy aspects if present
7660 if Has_Aspects (N) then
7661 Set_Has_Aspects (New_N, False);
7662 Set_Aspect_Specifications
7663 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7664 end if;
7666 -- If we are instantiating, we want to adjust the sloc based on the
7667 -- current S_Adjustment. However, if this is the root node of a subunit,
7668 -- we need to defer that adjustment to below (see "elsif Instantiating
7669 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7670 -- computed the adjustment.
7672 if Instantiating
7673 and then not (Nkind (N) in N_Proper_Body
7674 and then Was_Originally_Stub (N))
7675 then
7676 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7677 end if;
7679 if not Is_List_Member (N) then
7680 Set_Parent (New_N, Parent_Id);
7681 end if;
7683 -- Special casing for identifiers and other entity names and operators
7685 if Nkind_In (New_N, N_Character_Literal,
7686 N_Expanded_Name,
7687 N_Identifier,
7688 N_Operator_Symbol)
7689 or else Nkind (New_N) in N_Op
7690 then
7691 if not Instantiating then
7693 -- Link both nodes in order to assign subsequently the entity of
7694 -- the copy to the original node, in case this is a global
7695 -- reference.
7697 Set_Associated_Node (N, New_N);
7699 -- If we are within an instantiation, this is a nested generic
7700 -- that has already been analyzed at the point of definition.
7701 -- We must preserve references that were global to the enclosing
7702 -- parent at that point. Other occurrences, whether global or
7703 -- local to the current generic, must be resolved anew, so we
7704 -- reset the entity in the generic copy. A global reference has a
7705 -- smaller depth than the parent, or else the same depth in case
7706 -- both are distinct compilation units.
7708 -- A child unit is implicitly declared within the enclosing parent
7709 -- but is in fact global to it, and must be preserved.
7711 -- It is also possible for Current_Instantiated_Parent to be
7712 -- defined, and for this not to be a nested generic, namely if
7713 -- the unit is loaded through Rtsfind. In that case, the entity of
7714 -- New_N is only a link to the associated node, and not a defining
7715 -- occurrence.
7717 -- The entities for parent units in the defining_program_unit of a
7718 -- generic child unit are established when the context of the unit
7719 -- is first analyzed, before the generic copy is made. They are
7720 -- preserved in the copy for use in ASIS queries.
7722 Ent := Entity (New_N);
7724 if No (Current_Instantiated_Parent.Gen_Id) then
7725 if No (Ent)
7726 or else Nkind (Ent) /= N_Defining_Identifier
7727 or else not In_Defining_Unit_Name (N)
7728 then
7729 Set_Associated_Node (New_N, Empty);
7730 end if;
7732 elsif No (Ent)
7733 or else
7734 not Nkind_In (Ent, N_Defining_Identifier,
7735 N_Defining_Character_Literal,
7736 N_Defining_Operator_Symbol)
7737 or else No (Scope (Ent))
7738 or else
7739 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7740 and then not Is_Child_Unit (Ent))
7741 or else
7742 (Scope_Depth (Scope (Ent)) >
7743 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7744 and then
7745 Get_Source_Unit (Ent) =
7746 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7747 then
7748 Set_Associated_Node (New_N, Empty);
7749 end if;
7751 -- Case of instantiating identifier or some other name or operator
7753 else
7754 -- If the associated node is still defined, the entity in it
7755 -- is global, and must be copied to the instance. If this copy
7756 -- is being made for a body to inline, it is applied to an
7757 -- instantiated tree, and the entity is already present and
7758 -- must be also preserved.
7760 declare
7761 Assoc : constant Node_Id := Get_Associated_Node (N);
7763 begin
7764 if Present (Assoc) then
7765 if Nkind (Assoc) = Nkind (N) then
7766 Set_Entity (New_N, Entity (Assoc));
7767 Check_Private_View (N);
7769 -- The node is a reference to a global type and acts as the
7770 -- subtype mark of a qualified expression created in order
7771 -- to aid resolution of accidental overloading in instances.
7772 -- Since N is a reference to a type, the Associated_Node of
7773 -- N denotes an entity rather than another identifier. See
7774 -- Qualify_Universal_Operands for details.
7776 elsif Nkind (N) = N_Identifier
7777 and then Nkind (Parent (N)) = N_Qualified_Expression
7778 and then Subtype_Mark (Parent (N)) = N
7779 and then Is_Qualified_Universal_Literal (Parent (N))
7780 then
7781 Set_Entity (New_N, Assoc);
7783 -- The name in the call may be a selected component if the
7784 -- call has not been analyzed yet, as may be the case for
7785 -- pre/post conditions in a generic unit.
7787 elsif Nkind (Assoc) = N_Function_Call
7788 and then Is_Entity_Name (Name (Assoc))
7789 then
7790 Set_Entity (New_N, Entity (Name (Assoc)));
7792 elsif Nkind_In (Assoc, N_Defining_Identifier,
7793 N_Defining_Character_Literal,
7794 N_Defining_Operator_Symbol)
7795 and then Expander_Active
7796 then
7797 -- Inlining case: we are copying a tree that contains
7798 -- global entities, which are preserved in the copy to be
7799 -- used for subsequent inlining.
7801 null;
7803 else
7804 Set_Entity (New_N, Empty);
7805 end if;
7806 end if;
7807 end;
7808 end if;
7810 -- For expanded name, we must copy the Prefix and Selector_Name
7812 if Nkind (N) = N_Expanded_Name then
7813 Set_Prefix
7814 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7816 Set_Selector_Name (New_N,
7817 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7819 -- For operators, copy the operands
7821 elsif Nkind (N) in N_Op then
7822 if Nkind (N) in N_Binary_Op then
7823 Set_Left_Opnd (New_N,
7824 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7825 end if;
7827 Set_Right_Opnd (New_N,
7828 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7829 end if;
7831 -- Establish a link between an entity from the generic template and the
7832 -- corresponding entity in the generic copy to be analyzed.
7834 elsif Nkind (N) in N_Entity then
7835 if not Instantiating then
7836 Set_Associated_Entity (N, New_N);
7837 end if;
7839 -- Clear any existing link the copy may inherit from the replicated
7840 -- generic template entity.
7842 Set_Associated_Entity (New_N, Empty);
7844 -- Special casing for stubs
7846 elsif Nkind (N) in N_Body_Stub then
7848 -- In any case, we must copy the specification or defining
7849 -- identifier as appropriate.
7851 if Nkind (N) = N_Subprogram_Body_Stub then
7852 Set_Specification (New_N,
7853 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7855 else
7856 Set_Defining_Identifier (New_N,
7857 Copy_Generic_Node
7858 (Defining_Identifier (N), New_N, Instantiating));
7859 end if;
7861 -- If we are not instantiating, then this is where we load and
7862 -- analyze subunits, i.e. at the point where the stub occurs. A
7863 -- more permissive system might defer this analysis to the point
7864 -- of instantiation, but this seems too complicated for now.
7866 if not Instantiating then
7867 declare
7868 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7869 Subunit : Node_Id;
7870 Unum : Unit_Number_Type;
7871 New_Body : Node_Id;
7873 begin
7874 -- Make sure that, if it is a subunit of the main unit that is
7875 -- preprocessed and if -gnateG is specified, the preprocessed
7876 -- file will be written.
7878 Lib.Analysing_Subunit_Of_Main :=
7879 Lib.In_Extended_Main_Source_Unit (N);
7880 Unum :=
7881 Load_Unit
7882 (Load_Name => Subunit_Name,
7883 Required => False,
7884 Subunit => True,
7885 Error_Node => N);
7886 Lib.Analysing_Subunit_Of_Main := False;
7888 -- If the proper body is not found, a warning message will be
7889 -- emitted when analyzing the stub, or later at the point of
7890 -- instantiation. Here we just leave the stub as is.
7892 if Unum = No_Unit then
7893 Subunits_Missing := True;
7894 goto Subunit_Not_Found;
7895 end if;
7897 Subunit := Cunit (Unum);
7899 if Nkind (Unit (Subunit)) /= N_Subunit then
7900 Error_Msg_N
7901 ("found child unit instead of expected SEPARATE subunit",
7902 Subunit);
7903 Error_Msg_Sloc := Sloc (N);
7904 Error_Msg_N ("\to complete stub #", Subunit);
7905 goto Subunit_Not_Found;
7906 end if;
7908 -- We must create a generic copy of the subunit, in order to
7909 -- perform semantic analysis on it, and we must replace the
7910 -- stub in the original generic unit with the subunit, in order
7911 -- to preserve non-local references within.
7913 -- Only the proper body needs to be copied. Library_Unit and
7914 -- context clause are simply inherited by the generic copy.
7915 -- Note that the copy (which may be recursive if there are
7916 -- nested subunits) must be done first, before attaching it to
7917 -- the enclosing generic.
7919 New_Body :=
7920 Copy_Generic_Node
7921 (Proper_Body (Unit (Subunit)),
7922 Empty, Instantiating => False);
7924 -- Now place the original proper body in the original generic
7925 -- unit. This is a body, not a compilation unit.
7927 Rewrite (N, Proper_Body (Unit (Subunit)));
7928 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7929 Set_Was_Originally_Stub (N);
7931 -- Finally replace the body of the subunit with its copy, and
7932 -- make this new subunit into the library unit of the generic
7933 -- copy, which does not have stubs any longer.
7935 Set_Proper_Body (Unit (Subunit), New_Body);
7936 Set_Library_Unit (New_N, Subunit);
7937 Inherit_Context (Unit (Subunit), N);
7938 end;
7940 -- If we are instantiating, this must be an error case, since
7941 -- otherwise we would have replaced the stub node by the proper body
7942 -- that corresponds. So just ignore it in the copy (i.e. we have
7943 -- copied it, and that is good enough).
7945 else
7946 null;
7947 end if;
7949 <<Subunit_Not_Found>> null;
7951 -- If the node is a compilation unit, it is the subunit of a stub, which
7952 -- has been loaded already (see code below). In this case, the library
7953 -- unit field of N points to the parent unit (which is a compilation
7954 -- unit) and need not (and cannot) be copied.
7956 -- When the proper body of the stub is analyzed, the library_unit link
7957 -- is used to establish the proper context (see sem_ch10).
7959 -- The other fields of a compilation unit are copied as usual
7961 elsif Nkind (N) = N_Compilation_Unit then
7963 -- This code can only be executed when not instantiating, because in
7964 -- the copy made for an instantiation, the compilation unit node has
7965 -- disappeared at the point that a stub is replaced by its proper
7966 -- body.
7968 pragma Assert (not Instantiating);
7970 Set_Context_Items (New_N,
7971 Copy_Generic_List (Context_Items (N), New_N));
7973 Set_Unit (New_N,
7974 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
7976 Set_First_Inlined_Subprogram (New_N,
7977 Copy_Generic_Node
7978 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
7980 Set_Aux_Decls_Node
7981 (New_N,
7982 Copy_Generic_Node
7983 (Aux_Decls_Node (N), New_N, Instantiating => False));
7985 -- For an assignment node, the assignment is known to be semantically
7986 -- legal if we are instantiating the template. This avoids incorrect
7987 -- diagnostics in generated code.
7989 elsif Nkind (N) = N_Assignment_Statement then
7991 -- Copy name and expression fields in usual manner
7993 Set_Name (New_N,
7994 Copy_Generic_Node (Name (N), New_N, Instantiating));
7996 Set_Expression (New_N,
7997 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7999 if Instantiating then
8000 Set_Assignment_OK (Name (New_N), True);
8001 end if;
8003 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8004 if not Instantiating then
8005 Set_Associated_Node (N, New_N);
8007 else
8008 if Present (Get_Associated_Node (N))
8009 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8010 then
8011 -- In the generic the aggregate has some composite type. If at
8012 -- the point of instantiation the type has a private view,
8013 -- install the full view (and that of its ancestors, if any).
8015 declare
8016 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8017 Rt : Entity_Id;
8019 begin
8020 if Present (T) and then Is_Private_Type (T) then
8021 Switch_View (T);
8022 end if;
8024 if Present (T)
8025 and then Is_Tagged_Type (T)
8026 and then Is_Derived_Type (T)
8027 then
8028 Rt := Root_Type (T);
8030 loop
8031 T := Etype (T);
8033 if Is_Private_Type (T) then
8034 Switch_View (T);
8035 end if;
8037 exit when T = Rt;
8038 end loop;
8039 end if;
8040 end;
8041 end if;
8042 end if;
8044 -- Do not copy the associated node, which points to the generic copy
8045 -- of the aggregate.
8047 declare
8048 use Atree.Unchecked_Access;
8049 -- This code section is part of the implementation of an untyped
8050 -- tree traversal, so it needs direct access to node fields.
8052 begin
8053 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8054 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8055 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8056 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8057 end;
8059 -- Allocators do not have an identifier denoting the access type, so we
8060 -- must locate it through the expression to check whether the views are
8061 -- consistent.
8063 elsif Nkind (N) = N_Allocator
8064 and then Nkind (Expression (N)) = N_Qualified_Expression
8065 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8066 and then Instantiating
8067 then
8068 declare
8069 T : constant Node_Id :=
8070 Get_Associated_Node (Subtype_Mark (Expression (N)));
8071 Acc_T : Entity_Id;
8073 begin
8074 if Present (T) then
8076 -- Retrieve the allocator node in the generic copy
8078 Acc_T := Etype (Parent (Parent (T)));
8080 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8081 Switch_View (Acc_T);
8082 end if;
8083 end if;
8085 Copy_Descendants;
8086 end;
8088 -- For a proper body, we must catch the case of a proper body that
8089 -- replaces a stub. This represents the point at which a separate
8090 -- compilation unit, and hence template file, may be referenced, so we
8091 -- must make a new source instantiation entry for the template of the
8092 -- subunit, and ensure that all nodes in the subunit are adjusted using
8093 -- this new source instantiation entry.
8095 elsif Nkind (N) in N_Proper_Body then
8096 declare
8097 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8098 begin
8099 if Instantiating and then Was_Originally_Stub (N) then
8100 Create_Instantiation_Source
8101 (Instantiation_Node,
8102 Defining_Entity (N),
8103 S_Adjustment);
8105 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8106 end if;
8108 -- Now copy the fields of the proper body, using the new
8109 -- adjustment factor if one was needed as per test above.
8111 Copy_Descendants;
8113 -- Restore the original adjustment factor
8115 S_Adjustment := Save_Adjustment;
8116 end;
8118 elsif Nkind (N) = N_Pragma and then Instantiating then
8120 -- Do not copy Comment or Ident pragmas their content is relevant to
8121 -- the generic unit, not to the instantiating unit.
8123 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8124 New_N := Make_Null_Statement (Sloc (N));
8126 -- Do not copy pragmas generated from aspects because the pragmas do
8127 -- not carry any semantic information, plus they will be regenerated
8128 -- in the instance.
8130 -- However, generating C we need to copy them since postconditions
8131 -- are inlined by the front end, and the front-end inlining machinery
8132 -- relies on this routine to perform inlining.
8134 elsif From_Aspect_Specification (N)
8135 and then not Modify_Tree_For_C
8136 then
8137 New_N := Make_Null_Statement (Sloc (N));
8139 else
8140 Copy_Descendants;
8141 end if;
8143 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8145 -- No descendant fields need traversing
8147 null;
8149 elsif Nkind (N) = N_String_Literal
8150 and then Present (Etype (N))
8151 and then Instantiating
8152 then
8153 -- If the string is declared in an outer scope, the string_literal
8154 -- subtype created for it may have the wrong scope. Force reanalysis
8155 -- of the constant to generate a new itype in the proper context.
8157 Set_Etype (New_N, Empty);
8158 Set_Analyzed (New_N, False);
8160 -- For the remaining nodes, copy their descendants recursively
8162 else
8163 Copy_Descendants;
8165 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8166 Set_Generic_Parent (Specification (New_N), N);
8168 -- Should preserve Corresponding_Spec??? (12.3(14))
8169 end if;
8170 end if;
8172 -- Propagate dimensions if present, so that they are reflected in the
8173 -- instance.
8175 if Nkind (N) in N_Has_Etype
8176 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8177 and then Present (Etype (N))
8178 and then Is_Floating_Point_Type (Etype (N))
8179 and then Has_Dimension_System (Etype (N))
8180 then
8181 Copy_Dimensions (N, New_N);
8182 end if;
8184 return New_N;
8185 end Copy_Generic_Node;
8187 ----------------------------
8188 -- Denotes_Formal_Package --
8189 ----------------------------
8191 function Denotes_Formal_Package
8192 (Pack : Entity_Id;
8193 On_Exit : Boolean := False;
8194 Instance : Entity_Id := Empty) return Boolean
8196 Par : Entity_Id;
8197 Scop : constant Entity_Id := Scope (Pack);
8198 E : Entity_Id;
8200 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8201 -- The package in question may be an actual for a previous formal
8202 -- package P of the current instance, so examine its actuals as well.
8203 -- This must be recursive over other formal packages.
8205 ----------------------------------
8206 -- Is_Actual_Of_Previous_Formal --
8207 ----------------------------------
8209 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8210 E1 : Entity_Id;
8212 begin
8213 E1 := First_Entity (P);
8214 while Present (E1) and then E1 /= Instance loop
8215 if Ekind (E1) = E_Package
8216 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8217 then
8218 if Renamed_Object (E1) = Pack then
8219 return True;
8221 elsif E1 = P or else Renamed_Object (E1) = P then
8222 return False;
8224 elsif Is_Actual_Of_Previous_Formal (E1) then
8225 return True;
8226 end if;
8227 end if;
8229 Next_Entity (E1);
8230 end loop;
8232 return False;
8233 end Is_Actual_Of_Previous_Formal;
8235 -- Start of processing for Denotes_Formal_Package
8237 begin
8238 if On_Exit then
8239 Par :=
8240 Instance_Envs.Table
8241 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8242 else
8243 Par := Current_Instantiated_Parent.Act_Id;
8244 end if;
8246 if Ekind (Scop) = E_Generic_Package
8247 or else Nkind (Unit_Declaration_Node (Scop)) =
8248 N_Generic_Subprogram_Declaration
8249 then
8250 return True;
8252 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8253 N_Formal_Package_Declaration
8254 then
8255 return True;
8257 elsif No (Par) then
8258 return False;
8260 else
8261 -- Check whether this package is associated with a formal package of
8262 -- the enclosing instantiation. Iterate over the list of renamings.
8264 E := First_Entity (Par);
8265 while Present (E) loop
8266 if Ekind (E) /= E_Package
8267 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8268 then
8269 null;
8271 elsif Renamed_Object (E) = Par then
8272 return False;
8274 elsif Renamed_Object (E) = Pack then
8275 return True;
8277 elsif Is_Actual_Of_Previous_Formal (E) then
8278 return True;
8280 end if;
8282 Next_Entity (E);
8283 end loop;
8285 return False;
8286 end if;
8287 end Denotes_Formal_Package;
8289 -----------------
8290 -- End_Generic --
8291 -----------------
8293 procedure End_Generic is
8294 begin
8295 -- ??? More things could be factored out in this routine. Should
8296 -- probably be done at a later stage.
8298 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8299 Generic_Flags.Decrement_Last;
8301 Expander_Mode_Restore;
8302 end End_Generic;
8304 -------------
8305 -- Earlier --
8306 -------------
8308 function Earlier (N1, N2 : Node_Id) return Boolean is
8309 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8310 -- Find distance from given node to enclosing compilation unit
8312 ----------------
8313 -- Find_Depth --
8314 ----------------
8316 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8317 begin
8318 while Present (P)
8319 and then Nkind (P) /= N_Compilation_Unit
8320 loop
8321 P := True_Parent (P);
8322 D := D + 1;
8323 end loop;
8324 end Find_Depth;
8326 -- Local declarations
8328 D1 : Integer := 0;
8329 D2 : Integer := 0;
8330 P1 : Node_Id := N1;
8331 P2 : Node_Id := N2;
8332 T1 : Source_Ptr;
8333 T2 : Source_Ptr;
8335 -- Start of processing for Earlier
8337 begin
8338 Find_Depth (P1, D1);
8339 Find_Depth (P2, D2);
8341 if P1 /= P2 then
8342 return False;
8343 else
8344 P1 := N1;
8345 P2 := N2;
8346 end if;
8348 while D1 > D2 loop
8349 P1 := True_Parent (P1);
8350 D1 := D1 - 1;
8351 end loop;
8353 while D2 > D1 loop
8354 P2 := True_Parent (P2);
8355 D2 := D2 - 1;
8356 end loop;
8358 -- At this point P1 and P2 are at the same distance from the root.
8359 -- We examine their parents until we find a common declarative list.
8360 -- If we reach the root, N1 and N2 do not descend from the same
8361 -- declarative list (e.g. one is nested in the declarative part and
8362 -- the other is in a block in the statement part) and the earlier
8363 -- one is already frozen.
8365 while not Is_List_Member (P1)
8366 or else not Is_List_Member (P2)
8367 or else List_Containing (P1) /= List_Containing (P2)
8368 loop
8369 P1 := True_Parent (P1);
8370 P2 := True_Parent (P2);
8372 if Nkind (Parent (P1)) = N_Subunit then
8373 P1 := Corresponding_Stub (Parent (P1));
8374 end if;
8376 if Nkind (Parent (P2)) = N_Subunit then
8377 P2 := Corresponding_Stub (Parent (P2));
8378 end if;
8380 if P1 = P2 then
8381 return False;
8382 end if;
8383 end loop;
8385 -- Expanded code usually shares the source location of the original
8386 -- construct it was generated for. This however may not necessarily
8387 -- reflect the true location of the code within the tree.
8389 -- Before comparing the slocs of the two nodes, make sure that we are
8390 -- working with correct source locations. Assume that P1 is to the left
8391 -- of P2. If either one does not come from source, traverse the common
8392 -- list heading towards the other node and locate the first source
8393 -- statement.
8395 -- P1 P2
8396 -- ----+===+===+--------------+===+===+----
8397 -- expanded code expanded code
8399 if not Comes_From_Source (P1) then
8400 while Present (P1) loop
8402 -- Neither P2 nor a source statement were located during the
8403 -- search. If we reach the end of the list, then P1 does not
8404 -- occur earlier than P2.
8406 -- ---->
8407 -- start --- P2 ----- P1 --- end
8409 if No (Next (P1)) then
8410 return False;
8412 -- We encounter P2 while going to the right of the list. This
8413 -- means that P1 does indeed appear earlier.
8415 -- ---->
8416 -- start --- P1 ===== P2 --- end
8417 -- expanded code in between
8419 elsif P1 = P2 then
8420 return True;
8422 -- No need to look any further since we have located a source
8423 -- statement.
8425 elsif Comes_From_Source (P1) then
8426 exit;
8427 end if;
8429 -- Keep going right
8431 Next (P1);
8432 end loop;
8433 end if;
8435 if not Comes_From_Source (P2) then
8436 while Present (P2) loop
8438 -- Neither P1 nor a source statement were located during the
8439 -- search. If we reach the start of the list, then P1 does not
8440 -- occur earlier than P2.
8442 -- <----
8443 -- start --- P2 --- P1 --- end
8445 if No (Prev (P2)) then
8446 return False;
8448 -- We encounter P1 while going to the left of the list. This
8449 -- means that P1 does indeed appear earlier.
8451 -- <----
8452 -- start --- P1 ===== P2 --- end
8453 -- expanded code in between
8455 elsif P2 = P1 then
8456 return True;
8458 -- No need to look any further since we have located a source
8459 -- statement.
8461 elsif Comes_From_Source (P2) then
8462 exit;
8463 end if;
8465 -- Keep going left
8467 Prev (P2);
8468 end loop;
8469 end if;
8471 -- At this point either both nodes came from source or we approximated
8472 -- their source locations through neighboring source statements.
8474 T1 := Top_Level_Location (Sloc (P1));
8475 T2 := Top_Level_Location (Sloc (P2));
8477 -- When two nodes come from the same instance, they have identical top
8478 -- level locations. To determine proper relation within the tree, check
8479 -- their locations within the template.
8481 if T1 = T2 then
8482 return Sloc (P1) < Sloc (P2);
8484 -- The two nodes either come from unrelated instances or do not come
8485 -- from instantiated code at all.
8487 else
8488 return T1 < T2;
8489 end if;
8490 end Earlier;
8492 ----------------------
8493 -- Find_Actual_Type --
8494 ----------------------
8496 function Find_Actual_Type
8497 (Typ : Entity_Id;
8498 Gen_Type : Entity_Id) return Entity_Id
8500 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8501 T : Entity_Id;
8503 begin
8504 -- Special processing only applies to child units
8506 if not Is_Child_Unit (Gen_Scope) then
8507 return Get_Instance_Of (Typ);
8509 -- If designated or component type is itself a formal of the child unit,
8510 -- its instance is available.
8512 elsif Scope (Typ) = Gen_Scope then
8513 return Get_Instance_Of (Typ);
8515 -- If the array or access type is not declared in the parent unit,
8516 -- no special processing needed.
8518 elsif not Is_Generic_Type (Typ)
8519 and then Scope (Gen_Scope) /= Scope (Typ)
8520 then
8521 return Get_Instance_Of (Typ);
8523 -- Otherwise, retrieve designated or component type by visibility
8525 else
8526 T := Current_Entity (Typ);
8527 while Present (T) loop
8528 if In_Open_Scopes (Scope (T)) then
8529 return T;
8530 elsif Is_Generic_Actual_Type (T) then
8531 return T;
8532 end if;
8534 T := Homonym (T);
8535 end loop;
8537 return Typ;
8538 end if;
8539 end Find_Actual_Type;
8541 ----------------------------
8542 -- Freeze_Subprogram_Body --
8543 ----------------------------
8545 procedure Freeze_Subprogram_Body
8546 (Inst_Node : Node_Id;
8547 Gen_Body : Node_Id;
8548 Pack_Id : Entity_Id)
8550 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8551 Par : constant Entity_Id := Scope (Gen_Unit);
8552 E_G_Id : Entity_Id;
8553 Enc_G : Entity_Id;
8554 Enc_I : Node_Id;
8555 F_Node : Node_Id;
8557 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8558 -- Find innermost package body that encloses the given node, and which
8559 -- is not a compilation unit. Freeze nodes for the instance, or for its
8560 -- enclosing body, may be inserted after the enclosing_body of the
8561 -- generic unit. Used to determine proper placement of freeze node for
8562 -- both package and subprogram instances.
8564 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8565 -- Find entity for given package body, and locate or create a freeze
8566 -- node for it.
8568 ----------------------------
8569 -- Enclosing_Package_Body --
8570 ----------------------------
8572 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8573 P : Node_Id;
8575 begin
8576 P := Parent (N);
8577 while Present (P)
8578 and then Nkind (Parent (P)) /= N_Compilation_Unit
8579 loop
8580 if Nkind (P) = N_Package_Body then
8581 if Nkind (Parent (P)) = N_Subunit then
8582 return Corresponding_Stub (Parent (P));
8583 else
8584 return P;
8585 end if;
8586 end if;
8588 P := True_Parent (P);
8589 end loop;
8591 return Empty;
8592 end Enclosing_Package_Body;
8594 -------------------------
8595 -- Package_Freeze_Node --
8596 -------------------------
8598 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8599 Id : Entity_Id;
8601 begin
8602 if Nkind (B) = N_Package_Body then
8603 Id := Corresponding_Spec (B);
8604 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8605 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8606 end if;
8608 Ensure_Freeze_Node (Id);
8609 return Freeze_Node (Id);
8610 end Package_Freeze_Node;
8612 -- Start of processing for Freeze_Subprogram_Body
8614 begin
8615 -- If the instance and the generic body appear within the same unit, and
8616 -- the instance precedes the generic, the freeze node for the instance
8617 -- must appear after that of the generic. If the generic is nested
8618 -- within another instance I2, then current instance must be frozen
8619 -- after I2. In both cases, the freeze nodes are those of enclosing
8620 -- packages. Otherwise, the freeze node is placed at the end of the
8621 -- current declarative part.
8623 Enc_G := Enclosing_Package_Body (Gen_Body);
8624 Enc_I := Enclosing_Package_Body (Inst_Node);
8625 Ensure_Freeze_Node (Pack_Id);
8626 F_Node := Freeze_Node (Pack_Id);
8628 if Is_Generic_Instance (Par)
8629 and then Present (Freeze_Node (Par))
8630 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8631 then
8632 -- The parent was a premature instantiation. Insert freeze node at
8633 -- the end the current declarative part.
8635 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8636 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8638 -- Handle the following case:
8640 -- package Parent_Inst is new ...
8641 -- Parent_Inst []
8643 -- procedure P ... -- this body freezes Parent_Inst
8645 -- package Inst is new ...
8647 -- In this particular scenario, the freeze node for Inst must be
8648 -- inserted in the same manner as that of Parent_Inst - before the
8649 -- next source body or at the end of the declarative list (body not
8650 -- available). If body P did not exist and Parent_Inst was frozen
8651 -- after Inst, either by a body following Inst or at the end of the
8652 -- declarative region, the freeze node for Inst must be inserted
8653 -- after that of Parent_Inst. This relation is established by
8654 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8656 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8657 List_Containing (Inst_Node)
8658 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8659 then
8660 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8662 else
8663 Insert_After (Freeze_Node (Par), F_Node);
8664 end if;
8666 -- The body enclosing the instance should be frozen after the body that
8667 -- includes the generic, because the body of the instance may make
8668 -- references to entities therein. If the two are not in the same
8669 -- declarative part, or if the one enclosing the instance is frozen
8670 -- already, freeze the instance at the end of the current declarative
8671 -- part.
8673 elsif Is_Generic_Instance (Par)
8674 and then Present (Freeze_Node (Par))
8675 and then Present (Enc_I)
8676 then
8677 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8678 or else
8679 (Nkind (Enc_I) = N_Package_Body
8680 and then
8681 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8682 then
8683 -- The enclosing package may contain several instances. Rather
8684 -- than computing the earliest point at which to insert its freeze
8685 -- node, we place it at the end of the declarative part of the
8686 -- parent of the generic.
8688 Insert_Freeze_Node_For_Instance
8689 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8690 end if;
8692 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8694 elsif Present (Enc_G)
8695 and then Present (Enc_I)
8696 and then Enc_G /= Enc_I
8697 and then Earlier (Inst_Node, Gen_Body)
8698 then
8699 if Nkind (Enc_G) = N_Package_Body then
8700 E_G_Id :=
8701 Corresponding_Spec (Enc_G);
8702 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8703 E_G_Id :=
8704 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8705 end if;
8707 -- Freeze package that encloses instance, and place node after the
8708 -- package that encloses generic. If enclosing package is already
8709 -- frozen we have to assume it is at the proper place. This may be a
8710 -- potential ABE that requires dynamic checking. Do not add a freeze
8711 -- node if the package that encloses the generic is inside the body
8712 -- that encloses the instance, because the freeze node would be in
8713 -- the wrong scope. Additional contortions needed if the bodies are
8714 -- within a subunit.
8716 declare
8717 Enclosing_Body : Node_Id;
8719 begin
8720 if Nkind (Enc_I) = N_Package_Body_Stub then
8721 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8722 else
8723 Enclosing_Body := Enc_I;
8724 end if;
8726 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8727 Insert_Freeze_Node_For_Instance
8728 (Enc_G, Package_Freeze_Node (Enc_I));
8729 end if;
8730 end;
8732 -- Freeze enclosing subunit before instance
8734 Ensure_Freeze_Node (E_G_Id);
8736 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8737 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8738 end if;
8740 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8742 else
8743 -- If none of the above, insert freeze node at the end of the current
8744 -- declarative part.
8746 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8747 end if;
8748 end Freeze_Subprogram_Body;
8750 ----------------
8751 -- Get_Gen_Id --
8752 ----------------
8754 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8755 begin
8756 return Generic_Renamings.Table (E).Gen_Id;
8757 end Get_Gen_Id;
8759 ---------------------
8760 -- Get_Instance_Of --
8761 ---------------------
8763 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8764 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8766 begin
8767 if Res /= Assoc_Null then
8768 return Generic_Renamings.Table (Res).Act_Id;
8770 else
8771 -- On exit, entity is not instantiated: not a generic parameter, or
8772 -- else parameter of an inner generic unit.
8774 return A;
8775 end if;
8776 end Get_Instance_Of;
8778 ---------------------------------
8779 -- Get_Unit_Instantiation_Node --
8780 ---------------------------------
8782 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8783 Decl : Node_Id := Unit_Declaration_Node (A);
8784 Inst : Node_Id;
8786 begin
8787 -- If the Package_Instantiation attribute has been set on the package
8788 -- entity, then use it directly when it (or its Original_Node) refers
8789 -- to an N_Package_Instantiation node. In principle it should be
8790 -- possible to have this field set in all cases, which should be
8791 -- investigated, and would allow this function to be significantly
8792 -- simplified. ???
8794 Inst := Package_Instantiation (A);
8796 if Present (Inst) then
8797 if Nkind (Inst) = N_Package_Instantiation then
8798 return Inst;
8800 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8801 return Original_Node (Inst);
8802 end if;
8803 end if;
8805 -- If the instantiation is a compilation unit that does not need body
8806 -- then the instantiation node has been rewritten as a package
8807 -- declaration for the instance, and we return the original node.
8809 -- If it is a compilation unit and the instance node has not been
8810 -- rewritten, then it is still the unit of the compilation. Finally, if
8811 -- a body is present, this is a parent of the main unit whose body has
8812 -- been compiled for inlining purposes, and the instantiation node has
8813 -- been rewritten with the instance body.
8815 -- Otherwise the instantiation node appears after the declaration. If
8816 -- the entity is a formal package, the declaration may have been
8817 -- rewritten as a generic declaration (in the case of a formal with box)
8818 -- or left as a formal package declaration if it has actuals, and is
8819 -- found with a forward search.
8821 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8822 if Nkind (Decl) = N_Package_Declaration
8823 and then Present (Corresponding_Body (Decl))
8824 then
8825 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8826 end if;
8828 if Nkind_In (Original_Node (Decl), N_Function_Instantiation,
8829 N_Package_Instantiation,
8830 N_Procedure_Instantiation)
8831 then
8832 return Original_Node (Decl);
8833 else
8834 return Unit (Parent (Decl));
8835 end if;
8837 elsif Nkind (Decl) = N_Package_Declaration
8838 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8839 then
8840 return Original_Node (Decl);
8842 else
8843 Inst := Next (Decl);
8844 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8845 N_Function_Instantiation,
8846 N_Package_Instantiation,
8847 N_Procedure_Instantiation)
8848 loop
8849 Next (Inst);
8850 end loop;
8852 return Inst;
8853 end if;
8854 end Get_Unit_Instantiation_Node;
8856 ------------------------
8857 -- Has_Been_Exchanged --
8858 ------------------------
8860 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8861 Next : Elmt_Id;
8863 begin
8864 Next := First_Elmt (Exchanged_Views);
8865 while Present (Next) loop
8866 if Full_View (Node (Next)) = E then
8867 return True;
8868 end if;
8870 Next_Elmt (Next);
8871 end loop;
8873 return False;
8874 end Has_Been_Exchanged;
8876 ----------
8877 -- Hash --
8878 ----------
8880 function Hash (F : Entity_Id) return HTable_Range is
8881 begin
8882 return HTable_Range (F mod HTable_Size);
8883 end Hash;
8885 ------------------------
8886 -- Hide_Current_Scope --
8887 ------------------------
8889 procedure Hide_Current_Scope is
8890 C : constant Entity_Id := Current_Scope;
8891 E : Entity_Id;
8893 begin
8894 Set_Is_Hidden_Open_Scope (C);
8896 E := First_Entity (C);
8897 while Present (E) loop
8898 if Is_Immediately_Visible (E) then
8899 Set_Is_Immediately_Visible (E, False);
8900 Append_Elmt (E, Hidden_Entities);
8901 end if;
8903 Next_Entity (E);
8904 end loop;
8906 -- Make the scope name invisible as well. This is necessary, but might
8907 -- conflict with calls to Rtsfind later on, in case the scope is a
8908 -- predefined one. There is no clean solution to this problem, so for
8909 -- now we depend on the user not redefining Standard itself in one of
8910 -- the parent units.
8912 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8913 Set_Is_Immediately_Visible (C, False);
8914 Append_Elmt (C, Hidden_Entities);
8915 end if;
8917 end Hide_Current_Scope;
8919 --------------
8920 -- Init_Env --
8921 --------------
8923 procedure Init_Env is
8924 Saved : Instance_Env;
8926 begin
8927 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8928 Saved.Exchanged_Views := Exchanged_Views;
8929 Saved.Hidden_Entities := Hidden_Entities;
8930 Saved.Current_Sem_Unit := Current_Sem_Unit;
8931 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8932 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8934 -- Save configuration switches. These may be reset if the unit is a
8935 -- predefined unit, and the current mode is not Ada 2005.
8937 Save_Opt_Config_Switches (Saved.Switches);
8939 Instance_Envs.Append (Saved);
8941 Exchanged_Views := New_Elmt_List;
8942 Hidden_Entities := New_Elmt_List;
8944 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8945 -- this is set properly in Set_Instance_Env.
8947 Current_Instantiated_Parent :=
8948 (Current_Scope, Current_Scope, Assoc_Null);
8949 end Init_Env;
8951 ------------------------------
8952 -- In_Same_Declarative_Part --
8953 ------------------------------
8955 function In_Same_Declarative_Part
8956 (F_Node : Node_Id;
8957 Inst : Node_Id) return Boolean
8959 Decls : constant Node_Id := Parent (F_Node);
8960 Nod : Node_Id;
8962 begin
8963 Nod := Parent (Inst);
8964 while Present (Nod) loop
8965 if Nod = Decls then
8966 return True;
8968 elsif Nkind_In (Nod, N_Subprogram_Body,
8969 N_Package_Body,
8970 N_Package_Declaration,
8971 N_Task_Body,
8972 N_Protected_Body,
8973 N_Block_Statement)
8974 then
8975 return False;
8977 elsif Nkind (Nod) = N_Subunit then
8978 Nod := Corresponding_Stub (Nod);
8980 elsif Nkind (Nod) = N_Compilation_Unit then
8981 return False;
8983 else
8984 Nod := Parent (Nod);
8985 end if;
8986 end loop;
8988 return False;
8989 end In_Same_Declarative_Part;
8991 ---------------------
8992 -- In_Main_Context --
8993 ---------------------
8995 function In_Main_Context (E : Entity_Id) return Boolean is
8996 Context : List_Id;
8997 Clause : Node_Id;
8998 Nam : Node_Id;
9000 begin
9001 if not Is_Compilation_Unit (E)
9002 or else Ekind (E) /= E_Package
9003 or else In_Private_Part (E)
9004 then
9005 return False;
9006 end if;
9008 Context := Context_Items (Cunit (Main_Unit));
9010 Clause := First (Context);
9011 while Present (Clause) loop
9012 if Nkind (Clause) = N_With_Clause then
9013 Nam := Name (Clause);
9015 -- If the current scope is part of the context of the main unit,
9016 -- analysis of the corresponding with_clause is not complete, and
9017 -- the entity is not set. We use the Chars field directly, which
9018 -- might produce false positives in rare cases, but guarantees
9019 -- that we produce all the instance bodies we will need.
9021 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9022 or else (Nkind (Nam) = N_Selected_Component
9023 and then Chars (Selector_Name (Nam)) = Chars (E))
9024 then
9025 return True;
9026 end if;
9027 end if;
9029 Next (Clause);
9030 end loop;
9032 return False;
9033 end In_Main_Context;
9035 ---------------------
9036 -- Inherit_Context --
9037 ---------------------
9039 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9040 Current_Context : List_Id;
9041 Current_Unit : Node_Id;
9042 Item : Node_Id;
9043 New_I : Node_Id;
9045 Clause : Node_Id;
9046 OK : Boolean;
9047 Lib_Unit : Node_Id;
9049 begin
9050 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9052 -- The inherited context is attached to the enclosing compilation
9053 -- unit. This is either the main unit, or the declaration for the
9054 -- main unit (in case the instantiation appears within the package
9055 -- declaration and the main unit is its body).
9057 Current_Unit := Parent (Inst);
9058 while Present (Current_Unit)
9059 and then Nkind (Current_Unit) /= N_Compilation_Unit
9060 loop
9061 Current_Unit := Parent (Current_Unit);
9062 end loop;
9064 Current_Context := Context_Items (Current_Unit);
9066 Item := First (Context_Items (Parent (Gen_Decl)));
9067 while Present (Item) loop
9068 if Nkind (Item) = N_With_Clause then
9069 Lib_Unit := Library_Unit (Item);
9071 -- Take care to prevent direct cyclic with's
9073 if Lib_Unit /= Current_Unit then
9075 -- Do not add a unit if it is already in the context
9077 Clause := First (Current_Context);
9078 OK := True;
9079 while Present (Clause) loop
9080 if Nkind (Clause) = N_With_Clause and then
9081 Library_Unit (Clause) = Lib_Unit
9082 then
9083 OK := False;
9084 exit;
9085 end if;
9087 Next (Clause);
9088 end loop;
9090 if OK then
9091 New_I := New_Copy (Item);
9092 Set_Implicit_With (New_I, True);
9093 Set_Implicit_With_From_Instantiation (New_I, True);
9094 Append (New_I, Current_Context);
9095 end if;
9096 end if;
9097 end if;
9099 Next (Item);
9100 end loop;
9101 end if;
9102 end Inherit_Context;
9104 ----------------
9105 -- Initialize --
9106 ----------------
9108 procedure Initialize is
9109 begin
9110 Generic_Renamings.Init;
9111 Instance_Envs.Init;
9112 Generic_Flags.Init;
9113 Generic_Renamings_HTable.Reset;
9114 Circularity_Detected := False;
9115 Exchanged_Views := No_Elist;
9116 Hidden_Entities := No_Elist;
9117 end Initialize;
9119 -------------------------------------
9120 -- Insert_Freeze_Node_For_Instance --
9121 -------------------------------------
9123 procedure Insert_Freeze_Node_For_Instance
9124 (N : Node_Id;
9125 F_Node : Node_Id)
9127 Decl : Node_Id;
9128 Decls : List_Id;
9129 Inst : Entity_Id;
9130 Par_N : Node_Id;
9132 function Enclosing_Body (N : Node_Id) return Node_Id;
9133 -- Find enclosing package or subprogram body, if any. Freeze node may
9134 -- be placed at end of current declarative list if previous instance
9135 -- and current one have different enclosing bodies.
9137 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9138 -- Find the local instance, if any, that declares the generic that is
9139 -- being instantiated. If present, the freeze node for this instance
9140 -- must follow the freeze node for the previous instance.
9142 --------------------
9143 -- Enclosing_Body --
9144 --------------------
9146 function Enclosing_Body (N : Node_Id) return Node_Id is
9147 P : Node_Id;
9149 begin
9150 P := Parent (N);
9151 while Present (P)
9152 and then Nkind (Parent (P)) /= N_Compilation_Unit
9153 loop
9154 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9155 if Nkind (Parent (P)) = N_Subunit then
9156 return Corresponding_Stub (Parent (P));
9157 else
9158 return P;
9159 end if;
9160 end if;
9162 P := True_Parent (P);
9163 end loop;
9165 return Empty;
9166 end Enclosing_Body;
9168 -----------------------
9169 -- Previous_Instance --
9170 -----------------------
9172 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9173 S : Entity_Id;
9175 begin
9176 S := Scope (Gen);
9177 while Present (S) and then S /= Standard_Standard loop
9178 if Is_Generic_Instance (S)
9179 and then In_Same_Source_Unit (S, N)
9180 then
9181 return S;
9182 end if;
9184 S := Scope (S);
9185 end loop;
9187 return Empty;
9188 end Previous_Instance;
9190 -- Start of processing for Insert_Freeze_Node_For_Instance
9192 begin
9193 if not Is_List_Member (F_Node) then
9194 Decl := N;
9195 Decls := List_Containing (N);
9196 Inst := Entity (F_Node);
9197 Par_N := Parent (Decls);
9199 -- When processing a subprogram instantiation, utilize the actual
9200 -- subprogram instantiation rather than its package wrapper as it
9201 -- carries all the context information.
9203 if Is_Wrapper_Package (Inst) then
9204 Inst := Related_Instance (Inst);
9205 end if;
9207 -- If this is a package instance, check whether the generic is
9208 -- declared in a previous instance and the current instance is
9209 -- not within the previous one.
9211 if Present (Generic_Parent (Parent (Inst)))
9212 and then Is_In_Main_Unit (N)
9213 then
9214 declare
9215 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9216 Par_I : constant Entity_Id :=
9217 Previous_Instance
9218 (Generic_Parent (Parent (Inst)));
9219 Scop : Entity_Id;
9221 begin
9222 if Present (Par_I)
9223 and then Earlier (N, Freeze_Node (Par_I))
9224 then
9225 Scop := Scope (Inst);
9227 -- If the current instance is within the one that contains
9228 -- the generic, the freeze node for the current one must
9229 -- appear in the current declarative part. Ditto, if the
9230 -- current instance is within another package instance or
9231 -- within a body that does not enclose the current instance.
9232 -- In these three cases the freeze node of the previous
9233 -- instance is not relevant.
9235 while Present (Scop) and then Scop /= Standard_Standard loop
9236 exit when Scop = Par_I
9237 or else
9238 (Is_Generic_Instance (Scop)
9239 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9240 Scop := Scope (Scop);
9241 end loop;
9243 -- Previous instance encloses current instance
9245 if Scop = Par_I then
9246 null;
9248 -- If the next node is a source body we must freeze in
9249 -- the current scope as well.
9251 elsif Present (Next (N))
9252 and then Nkind_In (Next (N), N_Subprogram_Body,
9253 N_Package_Body)
9254 and then Comes_From_Source (Next (N))
9255 then
9256 null;
9258 -- Current instance is within an unrelated instance
9260 elsif Is_Generic_Instance (Scop) then
9261 null;
9263 -- Current instance is within an unrelated body
9265 elsif Present (Enclosing_N)
9266 and then Enclosing_N /= Enclosing_Body (Par_I)
9267 then
9268 null;
9270 else
9271 Insert_After (Freeze_Node (Par_I), F_Node);
9272 return;
9273 end if;
9274 end if;
9275 end;
9276 end if;
9278 -- When the instantiation occurs in a package declaration, append the
9279 -- freeze node to the private declarations (if any).
9281 if Nkind (Par_N) = N_Package_Specification
9282 and then Decls = Visible_Declarations (Par_N)
9283 and then Present (Private_Declarations (Par_N))
9284 and then not Is_Empty_List (Private_Declarations (Par_N))
9285 then
9286 Decls := Private_Declarations (Par_N);
9287 Decl := First (Decls);
9288 end if;
9290 -- Determine the proper freeze point of a package instantiation. We
9291 -- adhere to the general rule of a package or subprogram body causing
9292 -- freezing of anything before it in the same declarative region. In
9293 -- this case, the proper freeze point of a package instantiation is
9294 -- before the first source body which follows, or before a stub. This
9295 -- ensures that entities coming from the instance are already frozen
9296 -- and usable in source bodies.
9298 if Nkind (Par_N) /= N_Package_Declaration
9299 and then Ekind (Inst) = E_Package
9300 and then Is_Generic_Instance (Inst)
9301 and then
9302 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9303 then
9304 while Present (Decl) loop
9305 if (Nkind (Decl) in N_Unit_Body
9306 or else
9307 Nkind (Decl) in N_Body_Stub)
9308 and then Comes_From_Source (Decl)
9309 then
9310 Insert_Before (Decl, F_Node);
9311 return;
9312 end if;
9314 Next (Decl);
9315 end loop;
9316 end if;
9318 -- In a package declaration, or if no previous body, insert at end
9319 -- of list.
9321 Set_Sloc (F_Node, Sloc (Last (Decls)));
9322 Insert_After (Last (Decls), F_Node);
9323 end if;
9324 end Insert_Freeze_Node_For_Instance;
9326 ------------------
9327 -- Install_Body --
9328 ------------------
9330 procedure Install_Body
9331 (Act_Body : Node_Id;
9332 N : Node_Id;
9333 Gen_Body : Node_Id;
9334 Gen_Decl : Node_Id)
9336 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9337 -- Check if the generic definition and the instantiation come from
9338 -- a common scope, in which case the instance must be frozen after
9339 -- the generic body.
9341 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9342 -- If the instance is nested inside a generic unit, the Sloc of the
9343 -- instance indicates the place of the original definition, not the
9344 -- point of the current enclosing instance. Pending a better usage of
9345 -- Slocs to indicate instantiation places, we determine the place of
9346 -- origin of a node by finding the maximum sloc of any ancestor node.
9347 -- Why is this not equivalent to Top_Level_Location ???
9349 -------------------
9350 -- In_Same_Scope --
9351 -------------------
9353 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9354 Act_Scop : Entity_Id := Scope (Act_Id);
9355 Gen_Scop : Entity_Id := Scope (Gen_Id);
9357 begin
9358 while Act_Scop /= Standard_Standard
9359 and then Gen_Scop /= Standard_Standard
9360 loop
9361 if Act_Scop = Gen_Scop then
9362 return True;
9363 end if;
9365 Act_Scop := Scope (Act_Scop);
9366 Gen_Scop := Scope (Gen_Scop);
9367 end loop;
9369 return False;
9370 end In_Same_Scope;
9372 ---------------
9373 -- True_Sloc --
9374 ---------------
9376 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9377 N1 : Node_Id;
9378 Res : Source_Ptr;
9380 begin
9381 Res := Sloc (N);
9382 N1 := N;
9383 while Present (N1) and then N1 /= Act_Unit loop
9384 if Sloc (N1) > Res then
9385 Res := Sloc (N1);
9386 end if;
9388 N1 := Parent (N1);
9389 end loop;
9391 return Res;
9392 end True_Sloc;
9394 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9395 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9396 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9397 Par : constant Entity_Id := Scope (Gen_Id);
9398 Gen_Unit : constant Node_Id :=
9399 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9401 Body_Unit : Node_Id;
9402 F_Node : Node_Id;
9403 Must_Delay : Boolean;
9404 Orig_Body : Node_Id := Gen_Body;
9406 -- Start of processing for Install_Body
9408 begin
9409 -- Handle first the case of an instance with incomplete actual types.
9410 -- The instance body cannot be placed after the declaration because
9411 -- full views have not been seen yet. Any use of the non-limited views
9412 -- in the instance body requires the presence of a regular with_clause
9413 -- in the enclosing unit, and will fail if this with_clause is missing.
9414 -- We place the instance body at the beginning of the enclosing body,
9415 -- which is the unit being compiled. The freeze node for the instance
9416 -- is then placed after the instance body.
9418 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9419 and then Expander_Active
9420 and then Ekind (Scope (Act_Id)) = E_Package
9421 then
9422 declare
9423 Scop : constant Entity_Id := Scope (Act_Id);
9424 Body_Id : constant Node_Id :=
9425 Corresponding_Body (Unit_Declaration_Node (Scop));
9427 begin
9428 Ensure_Freeze_Node (Act_Id);
9429 F_Node := Freeze_Node (Act_Id);
9430 if Present (Body_Id) then
9431 Set_Is_Frozen (Act_Id, False);
9432 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9433 if Is_List_Member (F_Node) then
9434 Remove (F_Node);
9435 end if;
9437 Insert_After (Act_Body, F_Node);
9438 end if;
9439 end;
9440 return;
9441 end if;
9443 -- If the body is a subunit, the freeze point is the corresponding stub
9444 -- in the current compilation, not the subunit itself.
9446 if Nkind (Parent (Gen_Body)) = N_Subunit then
9447 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9448 else
9449 Orig_Body := Gen_Body;
9450 end if;
9452 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9454 -- If the instantiation and the generic definition appear in the same
9455 -- package declaration, this is an early instantiation. If they appear
9456 -- in the same declarative part, it is an early instantiation only if
9457 -- the generic body appears textually later, and the generic body is
9458 -- also in the main unit.
9460 -- If instance is nested within a subprogram, and the generic body
9461 -- is not, the instance is delayed because the enclosing body is. If
9462 -- instance and body are within the same scope, or the same subprogram
9463 -- body, indicate explicitly that the instance is delayed.
9465 Must_Delay :=
9466 (Gen_Unit = Act_Unit
9467 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9468 N_Package_Declaration)
9469 or else (Gen_Unit = Body_Unit
9470 and then True_Sloc (N, Act_Unit) <
9471 Sloc (Orig_Body)))
9472 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9473 and then In_Same_Scope (Gen_Id, Act_Id));
9475 -- If this is an early instantiation, the freeze node is placed after
9476 -- the generic body. Otherwise, if the generic appears in an instance,
9477 -- we cannot freeze the current instance until the outer one is frozen.
9478 -- This is only relevant if the current instance is nested within some
9479 -- inner scope not itself within the outer instance. If this scope is
9480 -- a package body in the same declarative part as the outer instance,
9481 -- then that body needs to be frozen after the outer instance. Finally,
9482 -- if no delay is needed, we place the freeze node at the end of the
9483 -- current declarative part.
9485 if Expander_Active
9486 and then (No (Freeze_Node (Act_Id))
9487 or else not Is_List_Member (Freeze_Node (Act_Id)))
9488 then
9489 Ensure_Freeze_Node (Act_Id);
9490 F_Node := Freeze_Node (Act_Id);
9492 if Must_Delay then
9493 Insert_After (Orig_Body, F_Node);
9495 elsif Is_Generic_Instance (Par)
9496 and then Present (Freeze_Node (Par))
9497 and then Scope (Act_Id) /= Par
9498 then
9499 -- Freeze instance of inner generic after instance of enclosing
9500 -- generic.
9502 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9504 -- Handle the following case:
9506 -- package Parent_Inst is new ...
9507 -- Parent_Inst []
9509 -- procedure P ... -- this body freezes Parent_Inst
9511 -- package Inst is new ...
9513 -- In this particular scenario, the freeze node for Inst must
9514 -- be inserted in the same manner as that of Parent_Inst,
9515 -- before the next source body or at the end of the declarative
9516 -- list (body not available). If body P did not exist and
9517 -- Parent_Inst was frozen after Inst, either by a body
9518 -- following Inst or at the end of the declarative region,
9519 -- the freeze node for Inst must be inserted after that of
9520 -- Parent_Inst. This relation is established by comparing
9521 -- the Slocs of Parent_Inst freeze node and Inst.
9523 if List_Containing (Get_Unit_Instantiation_Node (Par)) =
9524 List_Containing (N)
9525 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9526 then
9527 Insert_Freeze_Node_For_Instance (N, F_Node);
9528 else
9529 Insert_After (Freeze_Node (Par), F_Node);
9530 end if;
9532 -- Freeze package enclosing instance of inner generic after
9533 -- instance of enclosing generic.
9535 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9536 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9537 then
9538 declare
9539 Enclosing : Entity_Id;
9541 begin
9542 Enclosing := Corresponding_Spec (Parent (N));
9544 if No (Enclosing) then
9545 Enclosing := Defining_Entity (Parent (N));
9546 end if;
9548 Insert_Freeze_Node_For_Instance (N, F_Node);
9549 Ensure_Freeze_Node (Enclosing);
9551 if not Is_List_Member (Freeze_Node (Enclosing)) then
9553 -- The enclosing context is a subunit, insert the freeze
9554 -- node after the stub.
9556 if Nkind (Parent (Parent (N))) = N_Subunit then
9557 Insert_Freeze_Node_For_Instance
9558 (Corresponding_Stub (Parent (Parent (N))),
9559 Freeze_Node (Enclosing));
9561 -- The enclosing context is a package with a stub body
9562 -- which has already been replaced by the real body.
9563 -- Insert the freeze node after the actual body.
9565 elsif Ekind (Enclosing) = E_Package
9566 and then Present (Body_Entity (Enclosing))
9567 and then Was_Originally_Stub
9568 (Parent (Body_Entity (Enclosing)))
9569 then
9570 Insert_Freeze_Node_For_Instance
9571 (Parent (Body_Entity (Enclosing)),
9572 Freeze_Node (Enclosing));
9574 -- The parent instance has been frozen before the body of
9575 -- the enclosing package, insert the freeze node after
9576 -- the body.
9578 elsif List_Containing (Freeze_Node (Par)) =
9579 List_Containing (Parent (N))
9580 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9581 then
9582 Insert_Freeze_Node_For_Instance
9583 (Parent (N), Freeze_Node (Enclosing));
9585 else
9586 Insert_After
9587 (Freeze_Node (Par), Freeze_Node (Enclosing));
9588 end if;
9589 end if;
9590 end;
9592 else
9593 Insert_Freeze_Node_For_Instance (N, F_Node);
9594 end if;
9596 else
9597 Insert_Freeze_Node_For_Instance (N, F_Node);
9598 end if;
9599 end if;
9601 Set_Is_Frozen (Act_Id);
9602 Insert_Before (N, Act_Body);
9603 Mark_Rewrite_Insertion (Act_Body);
9604 end Install_Body;
9606 -----------------------------
9607 -- Install_Formal_Packages --
9608 -----------------------------
9610 procedure Install_Formal_Packages (Par : Entity_Id) is
9611 E : Entity_Id;
9612 Gen : Entity_Id;
9613 Gen_E : Entity_Id := Empty;
9615 begin
9616 E := First_Entity (Par);
9618 -- If we are installing an instance parent, locate the formal packages
9619 -- of its generic parent.
9621 if Is_Generic_Instance (Par) then
9622 Gen := Generic_Parent (Package_Specification (Par));
9623 Gen_E := First_Entity (Gen);
9624 end if;
9626 while Present (E) loop
9627 if Ekind (E) = E_Package
9628 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9629 then
9630 -- If this is the renaming for the parent instance, done
9632 if Renamed_Object (E) = Par then
9633 exit;
9635 -- The visibility of a formal of an enclosing generic is already
9636 -- correct.
9638 elsif Denotes_Formal_Package (E) then
9639 null;
9641 elsif Present (Associated_Formal_Package (E)) then
9642 Check_Generic_Actuals (Renamed_Object (E), True);
9643 Set_Is_Hidden (E, False);
9645 -- Find formal package in generic unit that corresponds to
9646 -- (instance of) formal package in instance.
9648 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9649 Next_Entity (Gen_E);
9650 end loop;
9652 if Present (Gen_E) then
9653 Map_Formal_Package_Entities (Gen_E, E);
9654 end if;
9655 end if;
9656 end if;
9658 Next_Entity (E);
9660 if Present (Gen_E) then
9661 Next_Entity (Gen_E);
9662 end if;
9663 end loop;
9664 end Install_Formal_Packages;
9666 --------------------
9667 -- Install_Parent --
9668 --------------------
9670 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9671 Ancestors : constant Elist_Id := New_Elmt_List;
9672 S : constant Entity_Id := Current_Scope;
9673 Inst_Par : Entity_Id;
9674 First_Par : Entity_Id;
9675 Inst_Node : Node_Id;
9676 Gen_Par : Entity_Id;
9677 First_Gen : Entity_Id;
9678 Elmt : Elmt_Id;
9680 procedure Install_Noninstance_Specs (Par : Entity_Id);
9681 -- Install the scopes of noninstance parent units ending with Par
9683 procedure Install_Spec (Par : Entity_Id);
9684 -- The child unit is within the declarative part of the parent, so the
9685 -- declarations within the parent are immediately visible.
9687 -------------------------------
9688 -- Install_Noninstance_Specs --
9689 -------------------------------
9691 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9692 begin
9693 if Present (Par)
9694 and then Par /= Standard_Standard
9695 and then not In_Open_Scopes (Par)
9696 then
9697 Install_Noninstance_Specs (Scope (Par));
9698 Install_Spec (Par);
9699 end if;
9700 end Install_Noninstance_Specs;
9702 ------------------
9703 -- Install_Spec --
9704 ------------------
9706 procedure Install_Spec (Par : Entity_Id) is
9707 Spec : constant Node_Id := Package_Specification (Par);
9709 begin
9710 -- If this parent of the child instance is a top-level unit,
9711 -- then record the unit and its visibility for later resetting in
9712 -- Remove_Parent. We exclude units that are generic instances, as we
9713 -- only want to record this information for the ultimate top-level
9714 -- noninstance parent (is that always correct???).
9716 if Scope (Par) = Standard_Standard
9717 and then not Is_Generic_Instance (Par)
9718 then
9719 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9720 Instance_Parent_Unit := Par;
9721 end if;
9723 -- Open the parent scope and make it and its declarations visible.
9724 -- If this point is not within a body, then only the visible
9725 -- declarations should be made visible, and installation of the
9726 -- private declarations is deferred until the appropriate point
9727 -- within analysis of the spec being instantiated (see the handling
9728 -- of parent visibility in Analyze_Package_Specification). This is
9729 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9730 -- private view problems that occur when compiling instantiations of
9731 -- a generic child of that package (Generic_Dispatching_Constructor).
9732 -- If the instance freezes a tagged type, inlinings of operations
9733 -- from Ada.Tags may need the full view of type Tag. If inlining took
9734 -- proper account of establishing visibility of inlined subprograms'
9735 -- parents then it should be possible to remove this
9736 -- special check. ???
9738 Push_Scope (Par);
9739 Set_Is_Immediately_Visible (Par);
9740 Install_Visible_Declarations (Par);
9741 Set_Use (Visible_Declarations (Spec));
9743 if In_Body or else Is_RTU (Par, Ada_Tags) then
9744 Install_Private_Declarations (Par);
9745 Set_Use (Private_Declarations (Spec));
9746 end if;
9747 end Install_Spec;
9749 -- Start of processing for Install_Parent
9751 begin
9752 -- We need to install the parent instance to compile the instantiation
9753 -- of the child, but the child instance must appear in the current
9754 -- scope. Given that we cannot place the parent above the current scope
9755 -- in the scope stack, we duplicate the current scope and unstack both
9756 -- after the instantiation is complete.
9758 -- If the parent is itself the instantiation of a child unit, we must
9759 -- also stack the instantiation of its parent, and so on. Each such
9760 -- ancestor is the prefix of the name in a prior instantiation.
9762 -- If this is a nested instance, the parent unit itself resolves to
9763 -- a renaming of the parent instance, whose declaration we need.
9765 -- Finally, the parent may be a generic (not an instance) when the
9766 -- child unit appears as a formal package.
9768 Inst_Par := P;
9770 if Present (Renamed_Entity (Inst_Par)) then
9771 Inst_Par := Renamed_Entity (Inst_Par);
9772 end if;
9774 First_Par := Inst_Par;
9776 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9778 First_Gen := Gen_Par;
9780 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9782 -- Load grandparent instance as well
9784 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9786 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9787 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9789 if Present (Renamed_Entity (Inst_Par)) then
9790 Inst_Par := Renamed_Entity (Inst_Par);
9791 end if;
9793 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9795 if Present (Gen_Par) then
9796 Prepend_Elmt (Inst_Par, Ancestors);
9798 else
9799 -- Parent is not the name of an instantiation
9801 Install_Noninstance_Specs (Inst_Par);
9802 exit;
9803 end if;
9805 else
9806 -- Previous error
9808 exit;
9809 end if;
9810 end loop;
9812 if Present (First_Gen) then
9813 Append_Elmt (First_Par, Ancestors);
9814 else
9815 Install_Noninstance_Specs (First_Par);
9816 end if;
9818 if not Is_Empty_Elmt_List (Ancestors) then
9819 Elmt := First_Elmt (Ancestors);
9820 while Present (Elmt) loop
9821 Install_Spec (Node (Elmt));
9822 Install_Formal_Packages (Node (Elmt));
9823 Next_Elmt (Elmt);
9824 end loop;
9825 end if;
9827 if not In_Body then
9828 Push_Scope (S);
9829 end if;
9830 end Install_Parent;
9832 -------------------------------
9833 -- Install_Hidden_Primitives --
9834 -------------------------------
9836 procedure Install_Hidden_Primitives
9837 (Prims_List : in out Elist_Id;
9838 Gen_T : Entity_Id;
9839 Act_T : Entity_Id)
9841 Elmt : Elmt_Id;
9842 List : Elist_Id := No_Elist;
9843 Prim_G_Elmt : Elmt_Id;
9844 Prim_A_Elmt : Elmt_Id;
9845 Prim_G : Node_Id;
9846 Prim_A : Node_Id;
9848 begin
9849 -- No action needed in case of serious errors because we cannot trust
9850 -- in the order of primitives
9852 if Serious_Errors_Detected > 0 then
9853 return;
9855 -- No action possible if we don't have available the list of primitive
9856 -- operations
9858 elsif No (Gen_T)
9859 or else not Is_Record_Type (Gen_T)
9860 or else not Is_Tagged_Type (Gen_T)
9861 or else not Is_Record_Type (Act_T)
9862 or else not Is_Tagged_Type (Act_T)
9863 then
9864 return;
9866 -- There is no need to handle interface types since their primitives
9867 -- cannot be hidden
9869 elsif Is_Interface (Gen_T) then
9870 return;
9871 end if;
9873 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9875 if not Is_Class_Wide_Type (Act_T) then
9876 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9877 else
9878 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9879 end if;
9881 loop
9882 -- Skip predefined primitives in the generic formal
9884 while Present (Prim_G_Elmt)
9885 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9886 loop
9887 Next_Elmt (Prim_G_Elmt);
9888 end loop;
9890 -- Skip predefined primitives in the generic actual
9892 while Present (Prim_A_Elmt)
9893 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9894 loop
9895 Next_Elmt (Prim_A_Elmt);
9896 end loop;
9898 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9900 Prim_G := Node (Prim_G_Elmt);
9901 Prim_A := Node (Prim_A_Elmt);
9903 -- There is no need to handle interface primitives because their
9904 -- primitives are not hidden
9906 exit when Present (Interface_Alias (Prim_G));
9908 -- Here we install one hidden primitive
9910 if Chars (Prim_G) /= Chars (Prim_A)
9911 and then Has_Suffix (Prim_A, 'P')
9912 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9913 then
9914 Set_Chars (Prim_A, Chars (Prim_G));
9915 Append_New_Elmt (Prim_A, To => List);
9916 end if;
9918 Next_Elmt (Prim_A_Elmt);
9919 Next_Elmt (Prim_G_Elmt);
9920 end loop;
9922 -- Append the elements to the list of temporarily visible primitives
9923 -- avoiding duplicates.
9925 if Present (List) then
9926 if No (Prims_List) then
9927 Prims_List := New_Elmt_List;
9928 end if;
9930 Elmt := First_Elmt (List);
9931 while Present (Elmt) loop
9932 Append_Unique_Elmt (Node (Elmt), Prims_List);
9933 Next_Elmt (Elmt);
9934 end loop;
9935 end if;
9936 end Install_Hidden_Primitives;
9938 -------------------------------
9939 -- Restore_Hidden_Primitives --
9940 -------------------------------
9942 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9943 Prim_Elmt : Elmt_Id;
9944 Prim : Node_Id;
9946 begin
9947 if Prims_List /= No_Elist then
9948 Prim_Elmt := First_Elmt (Prims_List);
9949 while Present (Prim_Elmt) loop
9950 Prim := Node (Prim_Elmt);
9951 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9952 Next_Elmt (Prim_Elmt);
9953 end loop;
9955 Prims_List := No_Elist;
9956 end if;
9957 end Restore_Hidden_Primitives;
9959 --------------------------------
9960 -- Instantiate_Formal_Package --
9961 --------------------------------
9963 function Instantiate_Formal_Package
9964 (Formal : Node_Id;
9965 Actual : Node_Id;
9966 Analyzed_Formal : Node_Id) return List_Id
9968 Loc : constant Source_Ptr := Sloc (Actual);
9969 Actual_Pack : Entity_Id;
9970 Formal_Pack : Entity_Id;
9971 Gen_Parent : Entity_Id;
9972 Decls : List_Id;
9973 Nod : Node_Id;
9974 Parent_Spec : Node_Id;
9976 procedure Find_Matching_Actual
9977 (F : Node_Id;
9978 Act : in out Entity_Id);
9979 -- We need to associate each formal entity in the formal package with
9980 -- the corresponding entity in the actual package. The actual package
9981 -- has been analyzed and possibly expanded, and as a result there is
9982 -- no one-to-one correspondence between the two lists (for example,
9983 -- the actual may include subtypes, itypes, and inherited primitive
9984 -- operations, interspersed among the renaming declarations for the
9985 -- actuals). We retrieve the corresponding actual by name because each
9986 -- actual has the same name as the formal, and they do appear in the
9987 -- same order.
9989 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9990 -- Retrieve entity of defining entity of generic formal parameter.
9991 -- Only the declarations of formals need to be considered when
9992 -- linking them to actuals, but the declarative list may include
9993 -- internal entities generated during analysis, and those are ignored.
9995 procedure Match_Formal_Entity
9996 (Formal_Node : Node_Id;
9997 Formal_Ent : Entity_Id;
9998 Actual_Ent : Entity_Id);
9999 -- Associates the formal entity with the actual. In the case where
10000 -- Formal_Ent is a formal package, this procedure iterates through all
10001 -- of its formals and enters associations between the actuals occurring
10002 -- in the formal package's corresponding actual package (given by
10003 -- Actual_Ent) and the formal package's formal parameters. This
10004 -- procedure recurses if any of the parameters is itself a package.
10006 function Is_Instance_Of
10007 (Act_Spec : Entity_Id;
10008 Gen_Anc : Entity_Id) return Boolean;
10009 -- The actual can be an instantiation of a generic within another
10010 -- instance, in which case there is no direct link from it to the
10011 -- original generic ancestor. In that case, we recognize that the
10012 -- ultimate ancestor is the same by examining names and scopes.
10014 procedure Process_Nested_Formal (Formal : Entity_Id);
10015 -- If the current formal is declared with a box, its own formals are
10016 -- visible in the instance, as they were in the generic, and their
10017 -- Hidden flag must be reset. If some of these formals are themselves
10018 -- packages declared with a box, the processing must be recursive.
10020 --------------------------
10021 -- Find_Matching_Actual --
10022 --------------------------
10024 procedure Find_Matching_Actual
10025 (F : Node_Id;
10026 Act : in out Entity_Id)
10028 Formal_Ent : Entity_Id;
10030 begin
10031 case Nkind (Original_Node (F)) is
10032 when N_Formal_Object_Declaration
10033 | N_Formal_Type_Declaration
10035 Formal_Ent := Defining_Identifier (F);
10037 while Chars (Act) /= Chars (Formal_Ent) loop
10038 Next_Entity (Act);
10039 end loop;
10041 when N_Formal_Package_Declaration
10042 | N_Formal_Subprogram_Declaration
10043 | N_Generic_Package_Declaration
10044 | N_Package_Declaration
10046 Formal_Ent := Defining_Entity (F);
10048 while Chars (Act) /= Chars (Formal_Ent) loop
10049 Next_Entity (Act);
10050 end loop;
10052 when others =>
10053 raise Program_Error;
10054 end case;
10055 end Find_Matching_Actual;
10057 -------------------------
10058 -- Match_Formal_Entity --
10059 -------------------------
10061 procedure Match_Formal_Entity
10062 (Formal_Node : Node_Id;
10063 Formal_Ent : Entity_Id;
10064 Actual_Ent : Entity_Id)
10066 Act_Pkg : Entity_Id;
10068 begin
10069 Set_Instance_Of (Formal_Ent, Actual_Ent);
10071 if Ekind (Actual_Ent) = E_Package then
10073 -- Record associations for each parameter
10075 Act_Pkg := Actual_Ent;
10077 declare
10078 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10079 F_Ent : Entity_Id;
10080 F_Node : Node_Id;
10082 Gen_Decl : Node_Id;
10083 Formals : List_Id;
10084 Actual : Entity_Id;
10086 begin
10087 -- Retrieve the actual given in the formal package declaration
10089 Actual := Entity (Name (Original_Node (Formal_Node)));
10091 -- The actual in the formal package declaration may be a
10092 -- renamed generic package, in which case we want to retrieve
10093 -- the original generic in order to traverse its formal part.
10095 if Present (Renamed_Entity (Actual)) then
10096 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10097 else
10098 Gen_Decl := Unit_Declaration_Node (Actual);
10099 end if;
10101 Formals := Generic_Formal_Declarations (Gen_Decl);
10103 if Present (Formals) then
10104 F_Node := First_Non_Pragma (Formals);
10105 else
10106 F_Node := Empty;
10107 end if;
10109 while Present (A_Ent)
10110 and then Present (F_Node)
10111 and then A_Ent /= First_Private_Entity (Act_Pkg)
10112 loop
10113 F_Ent := Get_Formal_Entity (F_Node);
10115 if Present (F_Ent) then
10117 -- This is a formal of the original package. Record
10118 -- association and recurse.
10120 Find_Matching_Actual (F_Node, A_Ent);
10121 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10122 Next_Entity (A_Ent);
10123 end if;
10125 Next_Non_Pragma (F_Node);
10126 end loop;
10127 end;
10128 end if;
10129 end Match_Formal_Entity;
10131 -----------------------
10132 -- Get_Formal_Entity --
10133 -----------------------
10135 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10136 Kind : constant Node_Kind := Nkind (Original_Node (N));
10137 begin
10138 case Kind is
10139 when N_Formal_Object_Declaration =>
10140 return Defining_Identifier (N);
10142 when N_Formal_Type_Declaration =>
10143 return Defining_Identifier (N);
10145 when N_Formal_Subprogram_Declaration =>
10146 return Defining_Unit_Name (Specification (N));
10148 when N_Formal_Package_Declaration =>
10149 return Defining_Identifier (Original_Node (N));
10151 when N_Generic_Package_Declaration =>
10152 return Defining_Identifier (Original_Node (N));
10154 -- All other declarations are introduced by semantic analysis and
10155 -- have no match in the actual.
10157 when others =>
10158 return Empty;
10159 end case;
10160 end Get_Formal_Entity;
10162 --------------------
10163 -- Is_Instance_Of --
10164 --------------------
10166 function Is_Instance_Of
10167 (Act_Spec : Entity_Id;
10168 Gen_Anc : Entity_Id) return Boolean
10170 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10172 begin
10173 if No (Gen_Par) then
10174 return False;
10176 -- Simplest case: the generic parent of the actual is the formal
10178 elsif Gen_Par = Gen_Anc then
10179 return True;
10181 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10182 return False;
10184 -- The actual may be obtained through several instantiations. Its
10185 -- scope must itself be an instance of a generic declared in the
10186 -- same scope as the formal. Any other case is detected above.
10188 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10189 return False;
10191 else
10192 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10193 end if;
10194 end Is_Instance_Of;
10196 ---------------------------
10197 -- Process_Nested_Formal --
10198 ---------------------------
10200 procedure Process_Nested_Formal (Formal : Entity_Id) is
10201 Ent : Entity_Id;
10203 begin
10204 if Present (Associated_Formal_Package (Formal))
10205 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10206 then
10207 Ent := First_Entity (Formal);
10208 while Present (Ent) loop
10209 Set_Is_Hidden (Ent, False);
10210 Set_Is_Visible_Formal (Ent);
10211 Set_Is_Potentially_Use_Visible
10212 (Ent, Is_Potentially_Use_Visible (Formal));
10214 if Ekind (Ent) = E_Package then
10215 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10216 Process_Nested_Formal (Ent);
10217 end if;
10219 Next_Entity (Ent);
10220 end loop;
10221 end if;
10222 end Process_Nested_Formal;
10224 -- Start of processing for Instantiate_Formal_Package
10226 begin
10227 Analyze (Actual);
10229 if not Is_Entity_Name (Actual)
10230 or else Ekind (Entity (Actual)) /= E_Package
10231 then
10232 Error_Msg_N
10233 ("expect package instance to instantiate formal", Actual);
10234 Abandon_Instantiation (Actual);
10235 raise Program_Error;
10237 else
10238 Actual_Pack := Entity (Actual);
10239 Set_Is_Instantiated (Actual_Pack);
10241 -- The actual may be a renamed package, or an outer generic formal
10242 -- package whose instantiation is converted into a renaming.
10244 if Present (Renamed_Object (Actual_Pack)) then
10245 Actual_Pack := Renamed_Object (Actual_Pack);
10246 end if;
10248 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10249 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10250 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10251 else
10252 Gen_Parent :=
10253 Generic_Parent (Specification (Analyzed_Formal));
10254 Formal_Pack :=
10255 Defining_Unit_Name (Specification (Analyzed_Formal));
10256 end if;
10258 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10259 Parent_Spec := Package_Specification (Actual_Pack);
10260 else
10261 Parent_Spec := Parent (Actual_Pack);
10262 end if;
10264 if Gen_Parent = Any_Id then
10265 Error_Msg_N
10266 ("previous error in declaration of formal package", Actual);
10267 Abandon_Instantiation (Actual);
10269 elsif
10270 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
10271 then
10272 null;
10274 else
10275 Error_Msg_NE
10276 ("actual parameter must be instance of&", Actual, Gen_Parent);
10277 Abandon_Instantiation (Actual);
10278 end if;
10280 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10281 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10283 Nod :=
10284 Make_Package_Renaming_Declaration (Loc,
10285 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10286 Name => New_Occurrence_Of (Actual_Pack, Loc));
10288 Set_Associated_Formal_Package
10289 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10290 Decls := New_List (Nod);
10292 -- If the formal F has a box, then the generic declarations are
10293 -- visible in the generic G. In an instance of G, the corresponding
10294 -- entities in the actual for F (which are the actuals for the
10295 -- instantiation of the generic that F denotes) must also be made
10296 -- visible for analysis of the current instance. On exit from the
10297 -- current instance, those entities are made private again. If the
10298 -- actual is currently in use, these entities are also use-visible.
10300 -- The loop through the actual entities also steps through the formal
10301 -- entities and enters associations from formals to actuals into the
10302 -- renaming map. This is necessary to properly handle checking of
10303 -- actual parameter associations for later formals that depend on
10304 -- actuals declared in the formal package.
10306 -- In Ada 2005, partial parameterization requires that we make
10307 -- visible the actuals corresponding to formals that were defaulted
10308 -- in the formal package. There formals are identified because they
10309 -- remain formal generics within the formal package, rather than
10310 -- being renamings of the actuals supplied.
10312 declare
10313 Gen_Decl : constant Node_Id :=
10314 Unit_Declaration_Node (Gen_Parent);
10315 Formals : constant List_Id :=
10316 Generic_Formal_Declarations (Gen_Decl);
10318 Actual_Ent : Entity_Id;
10319 Actual_Of_Formal : Node_Id;
10320 Formal_Node : Node_Id;
10321 Formal_Ent : Entity_Id;
10323 begin
10324 if Present (Formals) then
10325 Formal_Node := First_Non_Pragma (Formals);
10326 else
10327 Formal_Node := Empty;
10328 end if;
10330 Actual_Ent := First_Entity (Actual_Pack);
10331 Actual_Of_Formal :=
10332 First (Visible_Declarations (Specification (Analyzed_Formal)));
10333 while Present (Actual_Ent)
10334 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10335 loop
10336 if Present (Formal_Node) then
10337 Formal_Ent := Get_Formal_Entity (Formal_Node);
10339 if Present (Formal_Ent) then
10340 Find_Matching_Actual (Formal_Node, Actual_Ent);
10341 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10343 -- We iterate at the same time over the actuals of the
10344 -- local package created for the formal, to determine
10345 -- which one of the formals of the original generic were
10346 -- defaulted in the formal. The corresponding actual
10347 -- entities are visible in the enclosing instance.
10349 if Box_Present (Formal)
10350 or else
10351 (Present (Actual_Of_Formal)
10352 and then
10353 Is_Generic_Formal
10354 (Get_Formal_Entity (Actual_Of_Formal)))
10355 then
10356 Set_Is_Hidden (Actual_Ent, False);
10357 Set_Is_Visible_Formal (Actual_Ent);
10358 Set_Is_Potentially_Use_Visible
10359 (Actual_Ent, In_Use (Actual_Pack));
10361 if Ekind (Actual_Ent) = E_Package then
10362 Process_Nested_Formal (Actual_Ent);
10363 end if;
10365 else
10366 Set_Is_Hidden (Actual_Ent);
10367 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10368 end if;
10369 end if;
10371 Next_Non_Pragma (Formal_Node);
10372 Next (Actual_Of_Formal);
10374 else
10375 -- No further formals to match, but the generic part may
10376 -- contain inherited operation that are not hidden in the
10377 -- enclosing instance.
10379 Next_Entity (Actual_Ent);
10380 end if;
10381 end loop;
10383 -- Inherited subprograms generated by formal derived types are
10384 -- also visible if the types are.
10386 Actual_Ent := First_Entity (Actual_Pack);
10387 while Present (Actual_Ent)
10388 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10389 loop
10390 if Is_Overloadable (Actual_Ent)
10391 and then
10392 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10393 and then
10394 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10395 then
10396 Set_Is_Hidden (Actual_Ent, False);
10397 Set_Is_Potentially_Use_Visible
10398 (Actual_Ent, In_Use (Actual_Pack));
10399 end if;
10401 Next_Entity (Actual_Ent);
10402 end loop;
10403 end;
10405 -- If the formal is not declared with a box, reanalyze it as an
10406 -- abbreviated instantiation, to verify the matching rules of 12.7.
10407 -- The actual checks are performed after the generic associations
10408 -- have been analyzed, to guarantee the same visibility for this
10409 -- instantiation and for the actuals.
10411 -- In Ada 2005, the generic associations for the formal can include
10412 -- defaulted parameters. These are ignored during check. This
10413 -- internal instantiation is removed from the tree after conformance
10414 -- checking, because it contains formal declarations for those
10415 -- defaulted parameters, and those should not reach the back-end.
10417 if not Box_Present (Formal) then
10418 declare
10419 I_Pack : constant Entity_Id :=
10420 Make_Temporary (Sloc (Actual), 'P');
10422 begin
10423 Set_Is_Internal (I_Pack);
10425 Append_To (Decls,
10426 Make_Package_Instantiation (Sloc (Actual),
10427 Defining_Unit_Name => I_Pack,
10428 Name =>
10429 New_Occurrence_Of
10430 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10431 Generic_Associations => Generic_Associations (Formal)));
10432 end;
10433 end if;
10435 return Decls;
10436 end if;
10437 end Instantiate_Formal_Package;
10439 -----------------------------------
10440 -- Instantiate_Formal_Subprogram --
10441 -----------------------------------
10443 function Instantiate_Formal_Subprogram
10444 (Formal : Node_Id;
10445 Actual : Node_Id;
10446 Analyzed_Formal : Node_Id) return Node_Id
10448 Analyzed_S : constant Entity_Id :=
10449 Defining_Unit_Name (Specification (Analyzed_Formal));
10450 Formal_Sub : constant Entity_Id :=
10451 Defining_Unit_Name (Specification (Formal));
10453 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10454 -- If the generic is a child unit, the parent has been installed on the
10455 -- scope stack, but a default subprogram cannot resolve to something
10456 -- on the parent because that parent is not really part of the visible
10457 -- context (it is there to resolve explicit local entities). If the
10458 -- default has resolved in this way, we remove the entity from immediate
10459 -- visibility and analyze the node again to emit an error message or
10460 -- find another visible candidate.
10462 procedure Valid_Actual_Subprogram (Act : Node_Id);
10463 -- Perform legality check and raise exception on failure
10465 -----------------------
10466 -- From_Parent_Scope --
10467 -----------------------
10469 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10470 Gen_Scope : Node_Id;
10472 begin
10473 Gen_Scope := Scope (Analyzed_S);
10474 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10475 if Scope (Subp) = Scope (Gen_Scope) then
10476 return True;
10477 end if;
10479 Gen_Scope := Scope (Gen_Scope);
10480 end loop;
10482 return False;
10483 end From_Parent_Scope;
10485 -----------------------------
10486 -- Valid_Actual_Subprogram --
10487 -----------------------------
10489 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10490 Act_E : Entity_Id;
10492 begin
10493 if Is_Entity_Name (Act) then
10494 Act_E := Entity (Act);
10496 elsif Nkind (Act) = N_Selected_Component
10497 and then Is_Entity_Name (Selector_Name (Act))
10498 then
10499 Act_E := Entity (Selector_Name (Act));
10501 else
10502 Act_E := Empty;
10503 end if;
10505 if (Present (Act_E) and then Is_Overloadable (Act_E))
10506 or else Nkind_In (Act, N_Attribute_Reference,
10507 N_Indexed_Component,
10508 N_Character_Literal,
10509 N_Explicit_Dereference)
10510 then
10511 return;
10512 end if;
10514 Error_Msg_NE
10515 ("expect subprogram or entry name in instantiation of &",
10516 Instantiation_Node, Formal_Sub);
10517 Abandon_Instantiation (Instantiation_Node);
10518 end Valid_Actual_Subprogram;
10520 -- Local variables
10522 Decl_Node : Node_Id;
10523 Loc : Source_Ptr;
10524 Nam : Node_Id;
10525 New_Spec : Node_Id;
10526 New_Subp : Entity_Id;
10528 -- Start of processing for Instantiate_Formal_Subprogram
10530 begin
10531 New_Spec := New_Copy_Tree (Specification (Formal));
10533 -- The tree copy has created the proper instantiation sloc for the
10534 -- new specification. Use this location for all other constructed
10535 -- declarations.
10537 Loc := Sloc (Defining_Unit_Name (New_Spec));
10539 -- Create new entity for the actual (New_Copy_Tree does not), and
10540 -- indicate that it is an actual.
10542 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10543 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10544 Set_Is_Generic_Actual_Subprogram (New_Subp);
10545 Set_Defining_Unit_Name (New_Spec, New_Subp);
10547 -- Create new entities for the each of the formals in the specification
10548 -- of the renaming declaration built for the actual.
10550 if Present (Parameter_Specifications (New_Spec)) then
10551 declare
10552 F : Node_Id;
10553 F_Id : Entity_Id;
10555 begin
10556 F := First (Parameter_Specifications (New_Spec));
10557 while Present (F) loop
10558 F_Id := Defining_Identifier (F);
10560 Set_Defining_Identifier (F,
10561 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10562 Next (F);
10563 end loop;
10564 end;
10565 end if;
10567 -- Find entity of actual. If the actual is an attribute reference, it
10568 -- cannot be resolved here (its formal is missing) but is handled
10569 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10570 -- fully resolved subsequently, when the renaming declaration for the
10571 -- formal is analyzed. If it is an explicit dereference, resolve the
10572 -- prefix but not the actual itself, to prevent interpretation as call.
10574 if Present (Actual) then
10575 Loc := Sloc (Actual);
10576 Set_Sloc (New_Spec, Loc);
10578 if Nkind (Actual) = N_Operator_Symbol then
10579 Find_Direct_Name (Actual);
10581 elsif Nkind (Actual) = N_Explicit_Dereference then
10582 Analyze (Prefix (Actual));
10584 elsif Nkind (Actual) /= N_Attribute_Reference then
10585 Analyze (Actual);
10586 end if;
10588 Valid_Actual_Subprogram (Actual);
10589 Nam := Actual;
10591 elsif Present (Default_Name (Formal)) then
10592 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10593 N_Selected_Component,
10594 N_Indexed_Component,
10595 N_Character_Literal)
10596 and then Present (Entity (Default_Name (Formal)))
10597 then
10598 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10599 else
10600 Nam := New_Copy (Default_Name (Formal));
10601 Set_Sloc (Nam, Loc);
10602 end if;
10604 elsif Box_Present (Formal) then
10606 -- Actual is resolved at the point of instantiation. Create an
10607 -- identifier or operator with the same name as the formal.
10609 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10610 Nam :=
10611 Make_Operator_Symbol (Loc,
10612 Chars => Chars (Formal_Sub),
10613 Strval => No_String);
10614 else
10615 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10616 end if;
10618 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10619 and then Null_Present (Specification (Formal))
10620 then
10621 -- Generate null body for procedure, for use in the instance
10623 Decl_Node :=
10624 Make_Subprogram_Body (Loc,
10625 Specification => New_Spec,
10626 Declarations => New_List,
10627 Handled_Statement_Sequence =>
10628 Make_Handled_Sequence_Of_Statements (Loc,
10629 Statements => New_List (Make_Null_Statement (Loc))));
10631 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10632 return Decl_Node;
10634 else
10635 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10636 Error_Msg_NE
10637 ("missing actual&", Instantiation_Node, Formal_Sub);
10638 Error_Msg_NE
10639 ("\in instantiation of & declared#",
10640 Instantiation_Node, Scope (Analyzed_S));
10641 Abandon_Instantiation (Instantiation_Node);
10642 end if;
10644 Decl_Node :=
10645 Make_Subprogram_Renaming_Declaration (Loc,
10646 Specification => New_Spec,
10647 Name => Nam);
10649 -- If we do not have an actual and the formal specified <> then set to
10650 -- get proper default.
10652 if No (Actual) and then Box_Present (Formal) then
10653 Set_From_Default (Decl_Node);
10654 end if;
10656 -- Gather possible interpretations for the actual before analyzing the
10657 -- instance. If overloaded, it will be resolved when analyzing the
10658 -- renaming declaration.
10660 if Box_Present (Formal) and then No (Actual) then
10661 Analyze (Nam);
10663 if Is_Child_Unit (Scope (Analyzed_S))
10664 and then Present (Entity (Nam))
10665 then
10666 if not Is_Overloaded (Nam) then
10667 if From_Parent_Scope (Entity (Nam)) then
10668 Set_Is_Immediately_Visible (Entity (Nam), False);
10669 Set_Entity (Nam, Empty);
10670 Set_Etype (Nam, Empty);
10672 Analyze (Nam);
10673 Set_Is_Immediately_Visible (Entity (Nam));
10674 end if;
10676 else
10677 declare
10678 I : Interp_Index;
10679 It : Interp;
10681 begin
10682 Get_First_Interp (Nam, I, It);
10683 while Present (It.Nam) loop
10684 if From_Parent_Scope (It.Nam) then
10685 Remove_Interp (I);
10686 end if;
10688 Get_Next_Interp (I, It);
10689 end loop;
10690 end;
10691 end if;
10692 end if;
10693 end if;
10695 -- The generic instantiation freezes the actual. This can only be done
10696 -- once the actual is resolved, in the analysis of the renaming
10697 -- declaration. To make the formal subprogram entity available, we set
10698 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10699 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10700 -- of formal abstract subprograms.
10702 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10704 -- We cannot analyze the renaming declaration, and thus find the actual,
10705 -- until all the actuals are assembled in the instance. For subsequent
10706 -- checks of other actuals, indicate the node that will hold the
10707 -- instance of this formal.
10709 Set_Instance_Of (Analyzed_S, Nam);
10711 if Nkind (Actual) = N_Selected_Component
10712 and then Is_Task_Type (Etype (Prefix (Actual)))
10713 and then not Is_Frozen (Etype (Prefix (Actual)))
10714 then
10715 -- The renaming declaration will create a body, which must appear
10716 -- outside of the instantiation, We move the renaming declaration
10717 -- out of the instance, and create an additional renaming inside,
10718 -- to prevent freezing anomalies.
10720 declare
10721 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10723 begin
10724 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10725 Insert_Before (Instantiation_Node, Decl_Node);
10726 Analyze (Decl_Node);
10728 -- Now create renaming within the instance
10730 Decl_Node :=
10731 Make_Subprogram_Renaming_Declaration (Loc,
10732 Specification => New_Copy_Tree (New_Spec),
10733 Name => New_Occurrence_Of (Anon_Id, Loc));
10735 Set_Defining_Unit_Name (Specification (Decl_Node),
10736 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10737 end;
10738 end if;
10740 return Decl_Node;
10741 end Instantiate_Formal_Subprogram;
10743 ------------------------
10744 -- Instantiate_Object --
10745 ------------------------
10747 function Instantiate_Object
10748 (Formal : Node_Id;
10749 Actual : Node_Id;
10750 Analyzed_Formal : Node_Id) return List_Id
10752 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10753 A_Gen_Obj : constant Entity_Id :=
10754 Defining_Identifier (Analyzed_Formal);
10755 Acc_Def : Node_Id := Empty;
10756 Act_Assoc : constant Node_Id := Parent (Actual);
10757 Actual_Decl : Node_Id := Empty;
10758 Decl_Node : Node_Id;
10759 Def : Node_Id;
10760 Ftyp : Entity_Id;
10761 List : constant List_Id := New_List;
10762 Loc : constant Source_Ptr := Sloc (Actual);
10763 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10764 Subt_Decl : Node_Id := Empty;
10765 Subt_Mark : Node_Id := Empty;
10767 function Copy_Access_Def return Node_Id;
10768 -- If formal is an anonymous access, copy access definition of formal
10769 -- for generated object declaration.
10771 ---------------------
10772 -- Copy_Access_Def --
10773 ---------------------
10775 function Copy_Access_Def return Node_Id is
10776 begin
10777 Def := New_Copy_Tree (Acc_Def);
10779 -- In addition, if formal is an access to subprogram we need to
10780 -- generate new formals for the signature of the default, so that
10781 -- the tree is properly formatted for ASIS use.
10783 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10784 declare
10785 Par_Spec : Node_Id;
10786 begin
10787 Par_Spec :=
10788 First (Parameter_Specifications
10789 (Access_To_Subprogram_Definition (Def)));
10790 while Present (Par_Spec) loop
10791 Set_Defining_Identifier (Par_Spec,
10792 Make_Defining_Identifier (Sloc (Acc_Def),
10793 Chars => Chars (Defining_Identifier (Par_Spec))));
10794 Next (Par_Spec);
10795 end loop;
10796 end;
10797 end if;
10799 return Def;
10800 end Copy_Access_Def;
10802 -- Start of processing for Instantiate_Object
10804 begin
10805 -- Formal may be an anonymous access
10807 if Present (Subtype_Mark (Formal)) then
10808 Subt_Mark := Subtype_Mark (Formal);
10809 else
10810 Check_Access_Definition (Formal);
10811 Acc_Def := Access_Definition (Formal);
10812 end if;
10814 -- Sloc for error message on missing actual
10816 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10818 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10819 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10820 end if;
10822 Set_Parent (List, Parent (Actual));
10824 -- OUT present
10826 if Out_Present (Formal) then
10828 -- An IN OUT generic actual must be a name. The instantiation is a
10829 -- renaming declaration. The actual is the name being renamed. We
10830 -- use the actual directly, rather than a copy, because it is not
10831 -- used further in the list of actuals, and because a copy or a use
10832 -- of relocate_node is incorrect if the instance is nested within a
10833 -- generic. In order to simplify ASIS searches, the Generic_Parent
10834 -- field links the declaration to the generic association.
10836 if No (Actual) then
10837 Error_Msg_NE
10838 ("missing actual &",
10839 Instantiation_Node, Gen_Obj);
10840 Error_Msg_NE
10841 ("\in instantiation of & declared#",
10842 Instantiation_Node, Scope (A_Gen_Obj));
10843 Abandon_Instantiation (Instantiation_Node);
10844 end if;
10846 if Present (Subt_Mark) then
10847 Decl_Node :=
10848 Make_Object_Renaming_Declaration (Loc,
10849 Defining_Identifier => New_Copy (Gen_Obj),
10850 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10851 Name => Actual);
10853 else pragma Assert (Present (Acc_Def));
10854 Decl_Node :=
10855 Make_Object_Renaming_Declaration (Loc,
10856 Defining_Identifier => New_Copy (Gen_Obj),
10857 Access_Definition => New_Copy_Tree (Acc_Def),
10858 Name => Actual);
10859 end if;
10861 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10863 -- The analysis of the actual may produce Insert_Action nodes, so
10864 -- the declaration must have a context in which to attach them.
10866 Append (Decl_Node, List);
10867 Analyze (Actual);
10869 -- Return if the analysis of the actual reported some error
10871 if Etype (Actual) = Any_Type then
10872 return List;
10873 end if;
10875 -- This check is performed here because Analyze_Object_Renaming will
10876 -- not check it when Comes_From_Source is False. Note though that the
10877 -- check for the actual being the name of an object will be performed
10878 -- in Analyze_Object_Renaming.
10880 if Is_Object_Reference (Actual)
10881 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10882 then
10883 Error_Msg_N
10884 ("illegal discriminant-dependent component for in out parameter",
10885 Actual);
10886 end if;
10888 -- The actual has to be resolved in order to check that it is a
10889 -- variable (due to cases such as F (1), where F returns access to
10890 -- an array, and for overloaded prefixes).
10892 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10894 -- If the type of the formal is not itself a formal, and the current
10895 -- unit is a child unit, the formal type must be declared in a
10896 -- parent, and must be retrieved by visibility.
10898 if Ftyp = Orig_Ftyp
10899 and then Is_Generic_Unit (Scope (Ftyp))
10900 and then Is_Child_Unit (Scope (A_Gen_Obj))
10901 then
10902 declare
10903 Temp : constant Node_Id :=
10904 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10905 begin
10906 Set_Entity (Temp, Empty);
10907 Find_Type (Temp);
10908 Ftyp := Entity (Temp);
10909 end;
10910 end if;
10912 if Is_Private_Type (Ftyp)
10913 and then not Is_Private_Type (Etype (Actual))
10914 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10915 or else Base_Type (Etype (Actual)) = Ftyp)
10916 then
10917 -- If the actual has the type of the full view of the formal, or
10918 -- else a non-private subtype of the formal, then the visibility
10919 -- of the formal type has changed. Add to the actuals a subtype
10920 -- declaration that will force the exchange of views in the body
10921 -- of the instance as well.
10923 Subt_Decl :=
10924 Make_Subtype_Declaration (Loc,
10925 Defining_Identifier => Make_Temporary (Loc, 'P'),
10926 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10928 Prepend (Subt_Decl, List);
10930 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10931 Exchange_Declarations (Ftyp);
10932 end if;
10934 Resolve (Actual, Ftyp);
10936 if not Denotes_Variable (Actual) then
10937 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10939 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10941 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10942 -- the type of the actual shall resolve to a specific anonymous
10943 -- access type.
10945 if Ada_Version < Ada_2005
10946 or else Ekind (Base_Type (Ftyp)) /=
10947 E_Anonymous_Access_Type
10948 or else Ekind (Base_Type (Etype (Actual))) /=
10949 E_Anonymous_Access_Type
10950 then
10951 Error_Msg_NE
10952 ("type of actual does not match type of&", Actual, Gen_Obj);
10953 end if;
10954 end if;
10956 Note_Possible_Modification (Actual, Sure => True);
10958 -- Check for instantiation of atomic/volatile actual for
10959 -- non-atomic/volatile formal (RM C.6 (12)).
10961 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10962 Error_Msg_N
10963 ("cannot instantiate non-atomic formal object "
10964 & "with atomic actual", Actual);
10966 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10967 then
10968 Error_Msg_N
10969 ("cannot instantiate non-volatile formal object "
10970 & "with volatile actual", Actual);
10971 end if;
10973 -- Formal in-parameter
10975 else
10976 -- The instantiation of a generic formal in-parameter is constant
10977 -- declaration. The actual is the expression for that declaration.
10978 -- Its type is a full copy of the type of the formal. This may be
10979 -- an access to subprogram, for which we need to generate entities
10980 -- for the formals in the new signature.
10982 if Present (Actual) then
10983 if Present (Subt_Mark) then
10984 Def := New_Copy_Tree (Subt_Mark);
10985 else pragma Assert (Present (Acc_Def));
10986 Def := Copy_Access_Def;
10987 end if;
10989 Decl_Node :=
10990 Make_Object_Declaration (Loc,
10991 Defining_Identifier => New_Copy (Gen_Obj),
10992 Constant_Present => True,
10993 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10994 Object_Definition => Def,
10995 Expression => Actual);
10997 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10999 -- A generic formal object of a tagged type is defined to be
11000 -- aliased so the new constant must also be treated as aliased.
11002 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11003 Set_Aliased_Present (Decl_Node);
11004 end if;
11006 Append (Decl_Node, List);
11008 -- No need to repeat (pre-)analysis of some expression nodes
11009 -- already handled in Preanalyze_Actuals.
11011 if Nkind (Actual) /= N_Allocator then
11012 Analyze (Actual);
11014 -- Return if the analysis of the actual reported some error
11016 if Etype (Actual) = Any_Type then
11017 return List;
11018 end if;
11019 end if;
11021 declare
11022 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11023 Typ : Entity_Id;
11025 begin
11026 Typ := Get_Instance_Of (Formal_Type);
11028 -- If the actual appears in the current or an enclosing scope,
11029 -- use its type directly. This is relevant if it has an actual
11030 -- subtype that is distinct from its nominal one. This cannot
11031 -- be done in general because the type of the actual may
11032 -- depend on other actuals, and only be fully determined when
11033 -- the enclosing instance is analyzed.
11035 if Present (Etype (Actual))
11036 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11037 then
11038 Freeze_Before (Instantiation_Node, Etype (Actual));
11039 else
11040 Freeze_Before (Instantiation_Node, Typ);
11041 end if;
11043 -- If the actual is an aggregate, perform name resolution on
11044 -- its components (the analysis of an aggregate does not do it)
11045 -- to capture local names that may be hidden if the generic is
11046 -- a child unit.
11048 if Nkind (Actual) = N_Aggregate then
11049 Preanalyze_And_Resolve (Actual, Typ);
11050 end if;
11052 if Is_Limited_Type (Typ)
11053 and then not OK_For_Limited_Init (Typ, Actual)
11054 then
11055 Error_Msg_N
11056 ("initialization not allowed for limited types", Actual);
11057 Explain_Limited_Type (Typ, Actual);
11058 end if;
11059 end;
11061 elsif Present (Default_Expression (Formal)) then
11063 -- Use default to construct declaration
11065 if Present (Subt_Mark) then
11066 Def := New_Copy (Subt_Mark);
11067 else pragma Assert (Present (Acc_Def));
11068 Def := Copy_Access_Def;
11069 end if;
11071 Decl_Node :=
11072 Make_Object_Declaration (Sloc (Formal),
11073 Defining_Identifier => New_Copy (Gen_Obj),
11074 Constant_Present => True,
11075 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11076 Object_Definition => Def,
11077 Expression => New_Copy_Tree
11078 (Default_Expression (Formal)));
11080 Append (Decl_Node, List);
11081 Set_Analyzed (Expression (Decl_Node), False);
11083 else
11084 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11085 Error_Msg_NE ("\in instantiation of & declared#",
11086 Instantiation_Node, Scope (A_Gen_Obj));
11088 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11090 -- Create dummy constant declaration so that instance can be
11091 -- analyzed, to minimize cascaded visibility errors.
11093 if Present (Subt_Mark) then
11094 Def := Subt_Mark;
11095 else pragma Assert (Present (Acc_Def));
11096 Def := Acc_Def;
11097 end if;
11099 Decl_Node :=
11100 Make_Object_Declaration (Loc,
11101 Defining_Identifier => New_Copy (Gen_Obj),
11102 Constant_Present => True,
11103 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11104 Object_Definition => New_Copy (Def),
11105 Expression =>
11106 Make_Attribute_Reference (Sloc (Gen_Obj),
11107 Attribute_Name => Name_First,
11108 Prefix => New_Copy (Def)));
11110 Append (Decl_Node, List);
11112 else
11113 Abandon_Instantiation (Instantiation_Node);
11114 end if;
11115 end if;
11116 end if;
11118 if Nkind (Actual) in N_Has_Entity then
11119 Actual_Decl := Parent (Entity (Actual));
11120 end if;
11122 -- Ada 2005 (AI-423): For a formal object declaration with a null
11123 -- exclusion or an access definition that has a null exclusion: If the
11124 -- actual matching the formal object declaration denotes a generic
11125 -- formal object of another generic unit G, and the instantiation
11126 -- containing the actual occurs within the body of G or within the body
11127 -- of a generic unit declared within the declarative region of G, then
11128 -- the declaration of the formal object of G must have a null exclusion.
11129 -- Otherwise, the subtype of the actual matching the formal object
11130 -- declaration shall exclude null.
11132 if Ada_Version >= Ada_2005
11133 and then Present (Actual_Decl)
11134 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11135 N_Object_Declaration)
11136 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11137 and then not Has_Null_Exclusion (Actual_Decl)
11138 and then Has_Null_Exclusion (Analyzed_Formal)
11139 then
11140 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11141 Error_Msg_N
11142 ("actual must exclude null to match generic formal#", Actual);
11143 end if;
11145 -- An effectively volatile object cannot be used as an actual in a
11146 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11147 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11148 -- legality rule, and also verifies that the actual is an object.
11150 if SPARK_Mode = On
11151 and then Present (Actual)
11152 and then Is_Object_Reference (Actual)
11153 and then Is_Effectively_Volatile_Object (Actual)
11154 then
11155 Error_Msg_N
11156 ("volatile object cannot act as actual in generic instantiation",
11157 Actual);
11158 end if;
11160 return List;
11161 end Instantiate_Object;
11163 ------------------------------
11164 -- Instantiate_Package_Body --
11165 ------------------------------
11167 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11168 -- must be replaced by gotos which jump to the end of the routine in order
11169 -- to restore the Ghost and SPARK modes.
11171 procedure Instantiate_Package_Body
11172 (Body_Info : Pending_Body_Info;
11173 Inlined_Body : Boolean := False;
11174 Body_Optional : Boolean := False)
11176 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11177 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11178 Act_Spec : constant Node_Id := Specification (Act_Decl);
11179 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11180 Gen_Id : constant Node_Id := Name (Inst_Node);
11181 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11182 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11183 Loc : constant Source_Ptr := Sloc (Inst_Node);
11185 Saved_ISMP : constant Boolean :=
11186 Ignore_SPARK_Mode_Pragmas_In_Instance;
11187 Saved_Style_Check : constant Boolean := Style_Check;
11189 procedure Check_Initialized_Types;
11190 -- In a generic package body, an entity of a generic private type may
11191 -- appear uninitialized. This is suspicious, unless the actual is a
11192 -- fully initialized type.
11194 -----------------------------
11195 -- Check_Initialized_Types --
11196 -----------------------------
11198 procedure Check_Initialized_Types is
11199 Decl : Node_Id;
11200 Formal : Entity_Id;
11201 Actual : Entity_Id;
11202 Uninit_Var : Entity_Id;
11204 begin
11205 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11206 while Present (Decl) loop
11207 Uninit_Var := Empty;
11209 if Nkind (Decl) = N_Private_Extension_Declaration then
11210 Uninit_Var := Uninitialized_Variable (Decl);
11212 elsif Nkind (Decl) = N_Formal_Type_Declaration
11213 and then Nkind (Formal_Type_Definition (Decl)) =
11214 N_Formal_Private_Type_Definition
11215 then
11216 Uninit_Var :=
11217 Uninitialized_Variable (Formal_Type_Definition (Decl));
11218 end if;
11220 if Present (Uninit_Var) then
11221 Formal := Defining_Identifier (Decl);
11222 Actual := First_Entity (Act_Decl_Id);
11224 -- For each formal there is a subtype declaration that renames
11225 -- the actual and has the same name as the formal. Locate the
11226 -- formal for warning message about uninitialized variables
11227 -- in the generic, for which the actual type should be a fully
11228 -- initialized type.
11230 while Present (Actual) loop
11231 exit when Ekind (Actual) = E_Package
11232 and then Present (Renamed_Object (Actual));
11234 if Chars (Actual) = Chars (Formal)
11235 and then not Is_Scalar_Type (Actual)
11236 and then not Is_Fully_Initialized_Type (Actual)
11237 and then Warn_On_No_Value_Assigned
11238 then
11239 Error_Msg_Node_2 := Formal;
11240 Error_Msg_NE
11241 ("generic unit has uninitialized variable& of "
11242 & "formal private type &?v?", Actual, Uninit_Var);
11243 Error_Msg_NE
11244 ("actual type for& should be fully initialized type?v?",
11245 Actual, Formal);
11246 exit;
11247 end if;
11249 Next_Entity (Actual);
11250 end loop;
11251 end if;
11253 Next (Decl);
11254 end loop;
11255 end Check_Initialized_Types;
11257 -- Local variables
11259 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11260 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11261 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11262 -- Save the Ghost and SPARK mode-related data to restore on exit
11264 Act_Body : Node_Id;
11265 Act_Body_Id : Entity_Id;
11266 Act_Body_Name : Node_Id;
11267 Gen_Body : Node_Id;
11268 Gen_Body_Id : Node_Id;
11269 Par_Ent : Entity_Id := Empty;
11270 Par_Vis : Boolean := False;
11271 Parent_Installed : Boolean := False;
11273 Vis_Prims_List : Elist_Id := No_Elist;
11274 -- List of primitives made temporarily visible in the instantiation
11275 -- to match the visibility of the formal type.
11277 -- Start of processing for Instantiate_Package_Body
11279 begin
11280 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11282 -- The instance body may already have been processed, as the parent of
11283 -- another instance that is inlined (Load_Parent_Of_Generic).
11285 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11286 return;
11287 end if;
11289 -- The package being instantiated may be subject to pragma Ghost. Set
11290 -- the mode now to ensure that any nodes generated during instantiation
11291 -- are properly marked as Ghost.
11293 Set_Ghost_Mode (Act_Decl_Id);
11295 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11297 -- Re-establish the state of information on which checks are suppressed.
11298 -- This information was set in Body_Info at the point of instantiation,
11299 -- and now we restore it so that the instance is compiled using the
11300 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11302 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11303 Scope_Suppress := Body_Info.Scope_Suppress;
11304 Opt.Ada_Version := Body_Info.Version;
11305 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11306 Restore_Warnings (Body_Info.Warnings);
11308 -- Install the SPARK mode which applies to the package body
11310 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11312 if No (Gen_Body_Id) then
11314 -- Do not look for parent of generic body if none is required.
11315 -- This may happen when the routine is called as part of the
11316 -- Pending_Instantiations processing, when nested instances
11317 -- may precede the one generated from the main unit.
11319 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11320 and then Body_Optional
11321 then
11322 goto Leave;
11323 else
11324 Load_Parent_Of_Generic
11325 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11326 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11327 end if;
11328 end if;
11330 -- Establish global variable for sloc adjustment and for error recovery
11331 -- In the case of an instance body for an instantiation with actuals
11332 -- from a limited view, the instance body is placed at the beginning
11333 -- of the enclosing package body: use the body entity as the source
11334 -- location for nodes of the instance body.
11336 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11337 declare
11338 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11339 Body_Id : constant Node_Id :=
11340 Corresponding_Body (Unit_Declaration_Node (Scop));
11342 begin
11343 Instantiation_Node := Body_Id;
11344 end;
11345 else
11346 Instantiation_Node := Inst_Node;
11347 end if;
11349 if Present (Gen_Body_Id) then
11350 Save_Env (Gen_Unit, Act_Decl_Id);
11351 Style_Check := False;
11353 -- If the context of the instance is subject to SPARK_Mode "off", the
11354 -- annotation is missing, or the body is instantiated at a later pass
11355 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11356 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11357 -- instance.
11359 if SPARK_Mode /= On
11360 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11361 then
11362 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11363 end if;
11365 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11366 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11368 Create_Instantiation_Source
11369 (Inst_Node, Gen_Body_Id, S_Adjustment);
11371 Act_Body :=
11372 Copy_Generic_Node
11373 (Original_Node (Gen_Body), Empty, Instantiating => True);
11375 -- Create proper (possibly qualified) defining name for the body, to
11376 -- correspond to the one in the spec.
11378 Act_Body_Id :=
11379 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11380 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11382 -- Some attributes of spec entity are not inherited by body entity
11384 Set_Handler_Records (Act_Body_Id, No_List);
11386 if Nkind (Defining_Unit_Name (Act_Spec)) =
11387 N_Defining_Program_Unit_Name
11388 then
11389 Act_Body_Name :=
11390 Make_Defining_Program_Unit_Name (Loc,
11391 Name =>
11392 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11393 Defining_Identifier => Act_Body_Id);
11394 else
11395 Act_Body_Name := Act_Body_Id;
11396 end if;
11398 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11400 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11401 Check_Generic_Actuals (Act_Decl_Id, False);
11402 Check_Initialized_Types;
11404 -- Install primitives hidden at the point of the instantiation but
11405 -- visible when processing the generic formals
11407 declare
11408 E : Entity_Id;
11410 begin
11411 E := First_Entity (Act_Decl_Id);
11412 while Present (E) loop
11413 if Is_Type (E)
11414 and then not Is_Itype (E)
11415 and then Is_Generic_Actual_Type (E)
11416 and then Is_Tagged_Type (E)
11417 then
11418 Install_Hidden_Primitives
11419 (Prims_List => Vis_Prims_List,
11420 Gen_T => Generic_Parent_Type (Parent (E)),
11421 Act_T => E);
11422 end if;
11424 Next_Entity (E);
11425 end loop;
11426 end;
11428 -- If it is a child unit, make the parent instance (which is an
11429 -- instance of the parent of the generic) visible. The parent
11430 -- instance is the prefix of the name of the generic unit.
11432 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11433 and then Nkind (Gen_Id) = N_Expanded_Name
11434 then
11435 Par_Ent := Entity (Prefix (Gen_Id));
11436 Par_Vis := Is_Immediately_Visible (Par_Ent);
11437 Install_Parent (Par_Ent, In_Body => True);
11438 Parent_Installed := True;
11440 elsif Is_Child_Unit (Gen_Unit) then
11441 Par_Ent := Scope (Gen_Unit);
11442 Par_Vis := Is_Immediately_Visible (Par_Ent);
11443 Install_Parent (Par_Ent, In_Body => True);
11444 Parent_Installed := True;
11445 end if;
11447 -- If the instantiation is a library unit, and this is the main unit,
11448 -- then build the resulting compilation unit nodes for the instance.
11449 -- If this is a compilation unit but it is not the main unit, then it
11450 -- is the body of a unit in the context, that is being compiled
11451 -- because it is encloses some inlined unit or another generic unit
11452 -- being instantiated. In that case, this body is not part of the
11453 -- current compilation, and is not attached to the tree, but its
11454 -- parent must be set for analysis.
11456 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11458 -- Replace instance node with body of instance, and create new
11459 -- node for corresponding instance declaration.
11461 Build_Instance_Compilation_Unit_Nodes
11462 (Inst_Node, Act_Body, Act_Decl);
11463 Analyze (Inst_Node);
11465 if Parent (Inst_Node) = Cunit (Main_Unit) then
11467 -- If the instance is a child unit itself, then set the scope
11468 -- of the expanded body to be the parent of the instantiation
11469 -- (ensuring that the fully qualified name will be generated
11470 -- for the elaboration subprogram).
11472 if Nkind (Defining_Unit_Name (Act_Spec)) =
11473 N_Defining_Program_Unit_Name
11474 then
11475 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11476 end if;
11477 end if;
11479 -- Case where instantiation is not a library unit
11481 else
11482 -- If this is an early instantiation, i.e. appears textually
11483 -- before the corresponding body and must be elaborated first,
11484 -- indicate that the body instance is to be delayed.
11486 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11488 -- Now analyze the body. We turn off all checks if this is an
11489 -- internal unit, since there is no reason to have checks on for
11490 -- any predefined run-time library code. All such code is designed
11491 -- to be compiled with checks off.
11493 -- Note that we do NOT apply this criterion to children of GNAT
11494 -- The latter units must suppress checks explicitly if needed.
11496 -- We also do not suppress checks in CodePeer mode where we are
11497 -- interested in finding possible runtime errors.
11499 if not CodePeer_Mode
11500 and then In_Predefined_Unit (Gen_Decl)
11501 then
11502 Analyze (Act_Body, Suppress => All_Checks);
11503 else
11504 Analyze (Act_Body);
11505 end if;
11506 end if;
11508 Inherit_Context (Gen_Body, Inst_Node);
11510 -- Remove the parent instances if they have been placed on the scope
11511 -- stack to compile the body.
11513 if Parent_Installed then
11514 Remove_Parent (In_Body => True);
11516 -- Restore the previous visibility of the parent
11518 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11519 end if;
11521 Restore_Hidden_Primitives (Vis_Prims_List);
11522 Restore_Private_Views (Act_Decl_Id);
11524 -- Remove the current unit from visibility if this is an instance
11525 -- that is not elaborated on the fly for inlining purposes.
11527 if not Inlined_Body then
11528 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11529 end if;
11531 Restore_Env;
11533 -- If we have no body, and the unit requires a body, then complain. This
11534 -- complaint is suppressed if we have detected other errors (since a
11535 -- common reason for missing the body is that it had errors).
11536 -- In CodePeer mode, a warning has been emitted already, no need for
11537 -- further messages.
11539 elsif Unit_Requires_Body (Gen_Unit)
11540 and then not Body_Optional
11541 then
11542 if CodePeer_Mode then
11543 null;
11545 elsif Serious_Errors_Detected = 0 then
11546 Error_Msg_NE
11547 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11549 -- Don't attempt to perform any cleanup actions if some other error
11550 -- was already detected, since this can cause blowups.
11552 else
11553 goto Leave;
11554 end if;
11556 -- Case of package that does not need a body
11558 else
11559 -- If the instantiation of the declaration is a library unit, rewrite
11560 -- the original package instantiation as a package declaration in the
11561 -- compilation unit node.
11563 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11564 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11565 Rewrite (Inst_Node, Act_Decl);
11567 -- Generate elaboration entity, in case spec has elaboration code.
11568 -- This cannot be done when the instance is analyzed, because it
11569 -- is not known yet whether the body exists.
11571 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11572 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11574 -- If the instantiation is not a library unit, then append the
11575 -- declaration to the list of implicitly generated entities, unless
11576 -- it is already a list member which means that it was already
11577 -- processed
11579 elsif not Is_List_Member (Act_Decl) then
11580 Mark_Rewrite_Insertion (Act_Decl);
11581 Insert_Before (Inst_Node, Act_Decl);
11582 end if;
11583 end if;
11585 Expander_Mode_Restore;
11587 <<Leave>>
11588 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11589 Restore_Ghost_Mode (Saved_GM);
11590 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11591 Style_Check := Saved_Style_Check;
11592 end Instantiate_Package_Body;
11594 ---------------------------------
11595 -- Instantiate_Subprogram_Body --
11596 ---------------------------------
11598 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11599 -- must be replaced by gotos which jump to the end of the routine in order
11600 -- to restore the Ghost and SPARK modes.
11602 procedure Instantiate_Subprogram_Body
11603 (Body_Info : Pending_Body_Info;
11604 Body_Optional : Boolean := False)
11606 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11607 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11608 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11609 Gen_Id : constant Node_Id := Name (Inst_Node);
11610 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11611 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11612 Loc : constant Source_Ptr := Sloc (Inst_Node);
11613 Pack_Id : constant Entity_Id :=
11614 Defining_Unit_Name (Parent (Act_Decl));
11616 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11617 Saved_ISMP : constant Boolean :=
11618 Ignore_SPARK_Mode_Pragmas_In_Instance;
11619 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11620 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11621 -- Save the Ghost and SPARK mode-related data to restore on exit
11623 Saved_Style_Check : constant Boolean := Style_Check;
11624 Saved_Warnings : constant Warning_Record := Save_Warnings;
11626 Act_Body : Node_Id;
11627 Act_Body_Id : Entity_Id;
11628 Gen_Body : Node_Id;
11629 Gen_Body_Id : Node_Id;
11630 Pack_Body : Node_Id;
11631 Par_Ent : Entity_Id := Empty;
11632 Par_Vis : Boolean := False;
11633 Ret_Expr : Node_Id;
11635 Parent_Installed : Boolean := False;
11637 begin
11638 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11640 -- Subprogram body may have been created already because of an inline
11641 -- pragma, or because of multiple elaborations of the enclosing package
11642 -- when several instances of the subprogram appear in the main unit.
11644 if Present (Corresponding_Body (Act_Decl)) then
11645 return;
11646 end if;
11648 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11649 -- the mode now to ensure that any nodes generated during instantiation
11650 -- are properly marked as Ghost.
11652 Set_Ghost_Mode (Act_Decl_Id);
11654 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11656 -- Re-establish the state of information on which checks are suppressed.
11657 -- This information was set in Body_Info at the point of instantiation,
11658 -- and now we restore it so that the instance is compiled using the
11659 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11661 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11662 Scope_Suppress := Body_Info.Scope_Suppress;
11663 Opt.Ada_Version := Body_Info.Version;
11664 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11665 Restore_Warnings (Body_Info.Warnings);
11667 -- Install the SPARK mode which applies to the subprogram body from the
11668 -- instantiation context. This may be refined further if an explicit
11669 -- SPARK_Mode pragma applies to the generic body.
11671 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11673 if No (Gen_Body_Id) then
11675 -- For imported generic subprogram, no body to compile, complete
11676 -- the spec entity appropriately.
11678 if Is_Imported (Gen_Unit) then
11679 Set_Is_Imported (Act_Decl_Id);
11680 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11681 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11682 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11683 Set_Has_Completion (Act_Decl_Id);
11684 goto Leave;
11686 -- For other cases, compile the body
11688 else
11689 Load_Parent_Of_Generic
11690 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11691 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11692 end if;
11693 end if;
11695 Instantiation_Node := Inst_Node;
11697 if Present (Gen_Body_Id) then
11698 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11700 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11702 -- Either body is not present, or context is non-expanding, as
11703 -- when compiling a subunit. Mark the instance as completed, and
11704 -- diagnose a missing body when needed.
11706 if Expander_Active
11707 and then Operating_Mode = Generate_Code
11708 then
11709 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11710 end if;
11712 Set_Has_Completion (Act_Decl_Id);
11713 goto Leave;
11714 end if;
11716 Save_Env (Gen_Unit, Act_Decl_Id);
11717 Style_Check := False;
11719 -- If the context of the instance is subject to SPARK_Mode "off", the
11720 -- annotation is missing, or the body is instantiated at a later pass
11721 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11722 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11723 -- instance.
11725 if SPARK_Mode /= On
11726 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11727 then
11728 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11729 end if;
11731 -- If the context of an instance is not subject to SPARK_Mode "off",
11732 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11733 -- the latter should be the one applicable to the instance.
11735 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11736 and then SPARK_Mode /= Off
11737 and then Present (SPARK_Pragma (Gen_Body_Id))
11738 then
11739 Set_SPARK_Mode (Gen_Body_Id);
11740 end if;
11742 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11743 Create_Instantiation_Source
11744 (Inst_Node,
11745 Gen_Body_Id,
11746 S_Adjustment);
11748 Act_Body :=
11749 Copy_Generic_Node
11750 (Original_Node (Gen_Body), Empty, Instantiating => True);
11752 -- Create proper defining name for the body, to correspond to the one
11753 -- in the spec.
11755 Act_Body_Id :=
11756 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11758 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11759 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11761 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11762 Set_Has_Completion (Act_Decl_Id);
11763 Check_Generic_Actuals (Pack_Id, False);
11765 -- Generate a reference to link the visible subprogram instance to
11766 -- the generic body, which for navigation purposes is the only
11767 -- available source for the instance.
11769 Generate_Reference
11770 (Related_Instance (Pack_Id),
11771 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11773 -- If it is a child unit, make the parent instance (which is an
11774 -- instance of the parent of the generic) visible. The parent
11775 -- instance is the prefix of the name of the generic unit.
11777 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11778 and then Nkind (Gen_Id) = N_Expanded_Name
11779 then
11780 Par_Ent := Entity (Prefix (Gen_Id));
11781 Par_Vis := Is_Immediately_Visible (Par_Ent);
11782 Install_Parent (Par_Ent, In_Body => True);
11783 Parent_Installed := True;
11785 elsif Is_Child_Unit (Gen_Unit) then
11786 Par_Ent := Scope (Gen_Unit);
11787 Par_Vis := Is_Immediately_Visible (Par_Ent);
11788 Install_Parent (Par_Ent, In_Body => True);
11789 Parent_Installed := True;
11790 end if;
11792 -- Subprogram body is placed in the body of wrapper package,
11793 -- whose spec contains the subprogram declaration as well as
11794 -- the renaming declarations for the generic parameters.
11796 Pack_Body :=
11797 Make_Package_Body (Loc,
11798 Defining_Unit_Name => New_Copy (Pack_Id),
11799 Declarations => New_List (Act_Body));
11801 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11803 -- If the instantiation is a library unit, then build resulting
11804 -- compilation unit nodes for the instance. The declaration of
11805 -- the enclosing package is the grandparent of the subprogram
11806 -- declaration. First replace the instantiation node as the unit
11807 -- of the corresponding compilation.
11809 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11810 if Parent (Inst_Node) = Cunit (Main_Unit) then
11811 Set_Unit (Parent (Inst_Node), Inst_Node);
11812 Build_Instance_Compilation_Unit_Nodes
11813 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11814 Analyze (Inst_Node);
11815 else
11816 Set_Parent (Pack_Body, Parent (Inst_Node));
11817 Analyze (Pack_Body);
11818 end if;
11820 else
11821 Insert_Before (Inst_Node, Pack_Body);
11822 Mark_Rewrite_Insertion (Pack_Body);
11823 Analyze (Pack_Body);
11825 if Expander_Active then
11826 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11827 end if;
11828 end if;
11830 Inherit_Context (Gen_Body, Inst_Node);
11832 Restore_Private_Views (Pack_Id, False);
11834 if Parent_Installed then
11835 Remove_Parent (In_Body => True);
11837 -- Restore the previous visibility of the parent
11839 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11840 end if;
11842 Restore_Env;
11843 Restore_Warnings (Saved_Warnings);
11845 -- Body not found. Error was emitted already. If there were no previous
11846 -- errors, this may be an instance whose scope is a premature instance.
11847 -- In that case we must insure that the (legal) program does raise
11848 -- program error if executed. We generate a subprogram body for this
11849 -- purpose. See DEC ac30vso.
11851 -- Should not reference proprietary DEC tests in comments ???
11853 elsif Serious_Errors_Detected = 0
11854 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11855 then
11856 if Body_Optional then
11857 goto Leave;
11859 elsif Ekind (Act_Decl_Id) = E_Procedure then
11860 Act_Body :=
11861 Make_Subprogram_Body (Loc,
11862 Specification =>
11863 Make_Procedure_Specification (Loc,
11864 Defining_Unit_Name =>
11865 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11866 Parameter_Specifications =>
11867 New_Copy_List
11868 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11870 Declarations => Empty_List,
11871 Handled_Statement_Sequence =>
11872 Make_Handled_Sequence_Of_Statements (Loc,
11873 Statements => New_List (
11874 Make_Raise_Program_Error (Loc,
11875 Reason => PE_Access_Before_Elaboration))));
11877 else
11878 Ret_Expr :=
11879 Make_Raise_Program_Error (Loc,
11880 Reason => PE_Access_Before_Elaboration);
11882 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11883 Set_Analyzed (Ret_Expr);
11885 Act_Body :=
11886 Make_Subprogram_Body (Loc,
11887 Specification =>
11888 Make_Function_Specification (Loc,
11889 Defining_Unit_Name =>
11890 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11891 Parameter_Specifications =>
11892 New_Copy_List
11893 (Parameter_Specifications (Parent (Act_Decl_Id))),
11894 Result_Definition =>
11895 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11897 Declarations => Empty_List,
11898 Handled_Statement_Sequence =>
11899 Make_Handled_Sequence_Of_Statements (Loc,
11900 Statements => New_List (
11901 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11902 end if;
11904 Pack_Body :=
11905 Make_Package_Body (Loc,
11906 Defining_Unit_Name => New_Copy (Pack_Id),
11907 Declarations => New_List (Act_Body));
11909 Insert_After (Inst_Node, Pack_Body);
11910 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11911 Analyze (Pack_Body);
11912 end if;
11914 Expander_Mode_Restore;
11916 <<Leave>>
11917 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11918 Restore_Ghost_Mode (Saved_GM);
11919 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11920 Style_Check := Saved_Style_Check;
11921 end Instantiate_Subprogram_Body;
11923 ----------------------
11924 -- Instantiate_Type --
11925 ----------------------
11927 function Instantiate_Type
11928 (Formal : Node_Id;
11929 Actual : Node_Id;
11930 Analyzed_Formal : Node_Id;
11931 Actual_Decls : List_Id) return List_Id
11933 A_Gen_T : constant Entity_Id :=
11934 Defining_Identifier (Analyzed_Formal);
11935 Def : constant Node_Id := Formal_Type_Definition (Formal);
11936 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11937 Act_T : Entity_Id;
11938 Ancestor : Entity_Id := Empty;
11939 Decl_Node : Node_Id;
11940 Decl_Nodes : List_Id;
11941 Loc : Source_Ptr;
11942 Subt : Entity_Id;
11944 procedure Diagnose_Predicated_Actual;
11945 -- There are a number of constructs in which a discrete type with
11946 -- predicates is illegal, e.g. as an index in an array type declaration.
11947 -- If a generic type is used is such a construct in a generic package
11948 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11949 -- of the generic contract that the actual cannot have predicates.
11951 procedure Validate_Array_Type_Instance;
11952 procedure Validate_Access_Subprogram_Instance;
11953 procedure Validate_Access_Type_Instance;
11954 procedure Validate_Derived_Type_Instance;
11955 procedure Validate_Derived_Interface_Type_Instance;
11956 procedure Validate_Discriminated_Formal_Type;
11957 procedure Validate_Interface_Type_Instance;
11958 procedure Validate_Private_Type_Instance;
11959 procedure Validate_Incomplete_Type_Instance;
11960 -- These procedures perform validation tests for the named case.
11961 -- Validate_Discriminated_Formal_Type is shared by formal private
11962 -- types and Ada 2012 formal incomplete types.
11964 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11965 -- Check that base types are the same and that the subtypes match
11966 -- statically. Used in several of the above.
11968 ---------------------------------
11969 -- Diagnose_Predicated_Actual --
11970 ---------------------------------
11972 procedure Diagnose_Predicated_Actual is
11973 begin
11974 if No_Predicate_On_Actual (A_Gen_T)
11975 and then Has_Predicates (Act_T)
11976 then
11977 Error_Msg_NE
11978 ("actual for& cannot be a type with predicate",
11979 Instantiation_Node, A_Gen_T);
11981 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11982 and then Has_Predicates (Act_T)
11983 and then not Has_Static_Predicate_Aspect (Act_T)
11984 then
11985 Error_Msg_NE
11986 ("actual for& cannot be a type with a dynamic predicate",
11987 Instantiation_Node, A_Gen_T);
11988 end if;
11989 end Diagnose_Predicated_Actual;
11991 --------------------
11992 -- Subtypes_Match --
11993 --------------------
11995 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11996 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11998 begin
11999 -- Some detailed comments would be useful here ???
12001 return ((Base_Type (T) = Act_T
12002 or else Base_Type (T) = Base_Type (Act_T))
12003 and then Subtypes_Statically_Match (T, Act_T))
12005 or else (Is_Class_Wide_Type (Gen_T)
12006 and then Is_Class_Wide_Type (Act_T)
12007 and then Subtypes_Match
12008 (Get_Instance_Of (Root_Type (Gen_T)),
12009 Root_Type (Act_T)))
12011 or else
12012 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12013 E_Anonymous_Access_Type)
12014 and then Ekind (Act_T) = Ekind (Gen_T)
12015 and then Subtypes_Statically_Match
12016 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12017 end Subtypes_Match;
12019 -----------------------------------------
12020 -- Validate_Access_Subprogram_Instance --
12021 -----------------------------------------
12023 procedure Validate_Access_Subprogram_Instance is
12024 begin
12025 if not Is_Access_Type (Act_T)
12026 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12027 then
12028 Error_Msg_NE
12029 ("expect access type in instantiation of &", Actual, Gen_T);
12030 Abandon_Instantiation (Actual);
12031 end if;
12033 -- According to AI05-288, actuals for access_to_subprograms must be
12034 -- subtype conformant with the generic formal. Previous to AI05-288
12035 -- only mode conformance was required.
12037 -- This is a binding interpretation that applies to previous versions
12038 -- of the language, no need to maintain previous weaker checks.
12040 Check_Subtype_Conformant
12041 (Designated_Type (Act_T),
12042 Designated_Type (A_Gen_T),
12043 Actual,
12044 Get_Inst => True);
12046 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12047 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12048 Error_Msg_NE
12049 ("protected access type not allowed for formal &",
12050 Actual, Gen_T);
12051 end if;
12053 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12054 Error_Msg_NE
12055 ("expect protected access type for formal &",
12056 Actual, Gen_T);
12057 end if;
12059 -- If the formal has a specified convention (which in most cases
12060 -- will be StdCall) verify that the actual has the same convention.
12062 if Has_Convention_Pragma (A_Gen_T)
12063 and then Convention (A_Gen_T) /= Convention (Act_T)
12064 then
12065 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12066 Error_Msg_NE
12067 ("actual for formal & must have convention %", Actual, Gen_T);
12068 end if;
12069 end Validate_Access_Subprogram_Instance;
12071 -----------------------------------
12072 -- Validate_Access_Type_Instance --
12073 -----------------------------------
12075 procedure Validate_Access_Type_Instance is
12076 Desig_Type : constant Entity_Id :=
12077 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12078 Desig_Act : Entity_Id;
12080 begin
12081 if not Is_Access_Type (Act_T) then
12082 Error_Msg_NE
12083 ("expect access type in instantiation of &", Actual, Gen_T);
12084 Abandon_Instantiation (Actual);
12085 end if;
12087 if Is_Access_Constant (A_Gen_T) then
12088 if not Is_Access_Constant (Act_T) then
12089 Error_Msg_N
12090 ("actual type must be access-to-constant type", Actual);
12091 Abandon_Instantiation (Actual);
12092 end if;
12093 else
12094 if Is_Access_Constant (Act_T) then
12095 Error_Msg_N
12096 ("actual type must be access-to-variable type", Actual);
12097 Abandon_Instantiation (Actual);
12099 elsif Ekind (A_Gen_T) = E_General_Access_Type
12100 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12101 then
12102 Error_Msg_N -- CODEFIX
12103 ("actual must be general access type!", Actual);
12104 Error_Msg_NE -- CODEFIX
12105 ("add ALL to }!", Actual, Act_T);
12106 Abandon_Instantiation (Actual);
12107 end if;
12108 end if;
12110 -- The designated subtypes, that is to say the subtypes introduced
12111 -- by an access type declaration (and not by a subtype declaration)
12112 -- must match.
12114 Desig_Act := Designated_Type (Base_Type (Act_T));
12116 -- The designated type may have been introduced through a limited_
12117 -- with clause, in which case retrieve the non-limited view. This
12118 -- applies to incomplete types as well as to class-wide types.
12120 if From_Limited_With (Desig_Act) then
12121 Desig_Act := Available_View (Desig_Act);
12122 end if;
12124 if not Subtypes_Match (Desig_Type, Desig_Act) then
12125 Error_Msg_NE
12126 ("designated type of actual does not match that of formal &",
12127 Actual, Gen_T);
12129 if not Predicates_Match (Desig_Type, Desig_Act) then
12130 Error_Msg_N ("\predicates do not match", Actual);
12131 end if;
12133 Abandon_Instantiation (Actual);
12135 elsif Is_Access_Type (Designated_Type (Act_T))
12136 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12138 Is_Constrained (Designated_Type (Desig_Type))
12139 then
12140 Error_Msg_NE
12141 ("designated type of actual does not match that of formal &",
12142 Actual, Gen_T);
12144 if not Predicates_Match (Desig_Type, Desig_Act) then
12145 Error_Msg_N ("\predicates do not match", Actual);
12146 end if;
12148 Abandon_Instantiation (Actual);
12149 end if;
12151 -- Ada 2005: null-exclusion indicators of the two types must agree
12153 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12154 Error_Msg_NE
12155 ("non null exclusion of actual and formal & do not match",
12156 Actual, Gen_T);
12157 end if;
12158 end Validate_Access_Type_Instance;
12160 ----------------------------------
12161 -- Validate_Array_Type_Instance --
12162 ----------------------------------
12164 procedure Validate_Array_Type_Instance is
12165 I1 : Node_Id;
12166 I2 : Node_Id;
12167 T2 : Entity_Id;
12169 function Formal_Dimensions return Nat;
12170 -- Count number of dimensions in array type formal
12172 -----------------------
12173 -- Formal_Dimensions --
12174 -----------------------
12176 function Formal_Dimensions return Nat is
12177 Num : Nat := 0;
12178 Index : Node_Id;
12180 begin
12181 if Nkind (Def) = N_Constrained_Array_Definition then
12182 Index := First (Discrete_Subtype_Definitions (Def));
12183 else
12184 Index := First (Subtype_Marks (Def));
12185 end if;
12187 while Present (Index) loop
12188 Num := Num + 1;
12189 Next_Index (Index);
12190 end loop;
12192 return Num;
12193 end Formal_Dimensions;
12195 -- Start of processing for Validate_Array_Type_Instance
12197 begin
12198 if not Is_Array_Type (Act_T) then
12199 Error_Msg_NE
12200 ("expect array type in instantiation of &", Actual, Gen_T);
12201 Abandon_Instantiation (Actual);
12203 elsif Nkind (Def) = N_Constrained_Array_Definition then
12204 if not (Is_Constrained (Act_T)) then
12205 Error_Msg_NE
12206 ("expect constrained array in instantiation of &",
12207 Actual, Gen_T);
12208 Abandon_Instantiation (Actual);
12209 end if;
12211 else
12212 if Is_Constrained (Act_T) then
12213 Error_Msg_NE
12214 ("expect unconstrained array in instantiation of &",
12215 Actual, Gen_T);
12216 Abandon_Instantiation (Actual);
12217 end if;
12218 end if;
12220 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12221 Error_Msg_NE
12222 ("dimensions of actual do not match formal &", Actual, Gen_T);
12223 Abandon_Instantiation (Actual);
12224 end if;
12226 I1 := First_Index (A_Gen_T);
12227 I2 := First_Index (Act_T);
12228 for J in 1 .. Formal_Dimensions loop
12230 -- If the indexes of the actual were given by a subtype_mark,
12231 -- the index was transformed into a range attribute. Retrieve
12232 -- the original type mark for checking.
12234 if Is_Entity_Name (Original_Node (I2)) then
12235 T2 := Entity (Original_Node (I2));
12236 else
12237 T2 := Etype (I2);
12238 end if;
12240 if not Subtypes_Match
12241 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12242 then
12243 Error_Msg_NE
12244 ("index types of actual do not match those of formal &",
12245 Actual, Gen_T);
12246 Abandon_Instantiation (Actual);
12247 end if;
12249 Next_Index (I1);
12250 Next_Index (I2);
12251 end loop;
12253 -- Check matching subtypes. Note that there are complex visibility
12254 -- issues when the generic is a child unit and some aspect of the
12255 -- generic type is declared in a parent unit of the generic. We do
12256 -- the test to handle this special case only after a direct check
12257 -- for static matching has failed. The case where both the component
12258 -- type and the array type are separate formals, and the component
12259 -- type is a private view may also require special checking in
12260 -- Subtypes_Match. Finally, we assume that a child instance where
12261 -- the component type comes from a formal of a parent instance is
12262 -- correct because the generic was correct. A more precise check
12263 -- seems too complex to install???
12265 if Subtypes_Match
12266 (Component_Type (A_Gen_T), Component_Type (Act_T))
12267 or else
12268 Subtypes_Match
12269 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12270 Component_Type (Act_T))
12271 or else
12272 (not Inside_A_Generic
12273 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12274 then
12275 null;
12276 else
12277 Error_Msg_NE
12278 ("component subtype of actual does not match that of formal &",
12279 Actual, Gen_T);
12280 Abandon_Instantiation (Actual);
12281 end if;
12283 if Has_Aliased_Components (A_Gen_T)
12284 and then not Has_Aliased_Components (Act_T)
12285 then
12286 Error_Msg_NE
12287 ("actual must have aliased components to match formal type &",
12288 Actual, Gen_T);
12289 end if;
12290 end Validate_Array_Type_Instance;
12292 -----------------------------------------------
12293 -- Validate_Derived_Interface_Type_Instance --
12294 -----------------------------------------------
12296 procedure Validate_Derived_Interface_Type_Instance is
12297 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12298 Elmt : Elmt_Id;
12300 begin
12301 -- First apply interface instance checks
12303 Validate_Interface_Type_Instance;
12305 -- Verify that immediate parent interface is an ancestor of
12306 -- the actual.
12308 if Present (Par)
12309 and then not Interface_Present_In_Ancestor (Act_T, Par)
12310 then
12311 Error_Msg_NE
12312 ("interface actual must include progenitor&", Actual, Par);
12313 end if;
12315 -- Now verify that the actual includes all other ancestors of
12316 -- the formal.
12318 Elmt := First_Elmt (Interfaces (A_Gen_T));
12319 while Present (Elmt) loop
12320 if not Interface_Present_In_Ancestor
12321 (Act_T, Get_Instance_Of (Node (Elmt)))
12322 then
12323 Error_Msg_NE
12324 ("interface actual must include progenitor&",
12325 Actual, Node (Elmt));
12326 end if;
12328 Next_Elmt (Elmt);
12329 end loop;
12330 end Validate_Derived_Interface_Type_Instance;
12332 ------------------------------------
12333 -- Validate_Derived_Type_Instance --
12334 ------------------------------------
12336 procedure Validate_Derived_Type_Instance is
12337 Actual_Discr : Entity_Id;
12338 Ancestor_Discr : Entity_Id;
12340 begin
12341 -- If the parent type in the generic declaration is itself a previous
12342 -- formal type, then it is local to the generic and absent from the
12343 -- analyzed generic definition. In that case the ancestor is the
12344 -- instance of the formal (which must have been instantiated
12345 -- previously), unless the ancestor is itself a formal derived type.
12346 -- In this latter case (which is the subject of Corrigendum 8652/0038
12347 -- (AI-202) the ancestor of the formals is the ancestor of its
12348 -- parent. Otherwise, the analyzed generic carries the parent type.
12349 -- If the parent type is defined in a previous formal package, then
12350 -- the scope of that formal package is that of the generic type
12351 -- itself, and it has already been mapped into the corresponding type
12352 -- in the actual package.
12354 -- Common case: parent type defined outside of the generic
12356 if Is_Entity_Name (Subtype_Mark (Def))
12357 and then Present (Entity (Subtype_Mark (Def)))
12358 then
12359 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12361 -- Check whether parent is defined in a previous formal package
12363 elsif
12364 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12365 then
12366 Ancestor :=
12367 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12369 -- The type may be a local derivation, or a type extension of a
12370 -- previous formal, or of a formal of a parent package.
12372 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12373 or else
12374 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12375 then
12376 -- Check whether the parent is another derived formal type in the
12377 -- same generic unit.
12379 if Etype (A_Gen_T) /= A_Gen_T
12380 and then Is_Generic_Type (Etype (A_Gen_T))
12381 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12382 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12383 then
12384 -- Locate ancestor of parent from the subtype declaration
12385 -- created for the actual.
12387 declare
12388 Decl : Node_Id;
12390 begin
12391 Decl := First (Actual_Decls);
12392 while Present (Decl) loop
12393 if Nkind (Decl) = N_Subtype_Declaration
12394 and then Chars (Defining_Identifier (Decl)) =
12395 Chars (Etype (A_Gen_T))
12396 then
12397 Ancestor := Generic_Parent_Type (Decl);
12398 exit;
12399 else
12400 Next (Decl);
12401 end if;
12402 end loop;
12403 end;
12405 pragma Assert (Present (Ancestor));
12407 -- The ancestor itself may be a previous formal that has been
12408 -- instantiated.
12410 Ancestor := Get_Instance_Of (Ancestor);
12412 else
12413 Ancestor :=
12414 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12415 end if;
12417 -- Check whether parent is a previous formal of the current generic
12419 elsif Is_Derived_Type (A_Gen_T)
12420 and then Is_Generic_Type (Etype (A_Gen_T))
12421 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12422 then
12423 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12425 -- An unusual case: the actual is a type declared in a parent unit,
12426 -- but is not a formal type so there is no instance_of for it.
12427 -- Retrieve it by analyzing the record extension.
12429 elsif Is_Child_Unit (Scope (A_Gen_T))
12430 and then In_Open_Scopes (Scope (Act_T))
12431 and then Is_Generic_Instance (Scope (Act_T))
12432 then
12433 Analyze (Subtype_Mark (Def));
12434 Ancestor := Entity (Subtype_Mark (Def));
12436 else
12437 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12438 end if;
12440 -- If the formal derived type has pragma Preelaborable_Initialization
12441 -- then the actual type must have preelaborable initialization.
12443 if Known_To_Have_Preelab_Init (A_Gen_T)
12444 and then not Has_Preelaborable_Initialization (Act_T)
12445 then
12446 Error_Msg_NE
12447 ("actual for & must have preelaborable initialization",
12448 Actual, Gen_T);
12449 end if;
12451 -- Ada 2005 (AI-251)
12453 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12454 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12455 Error_Msg_NE
12456 ("(Ada 2005) expected type implementing & in instantiation",
12457 Actual, Ancestor);
12458 end if;
12460 -- Finally verify that the (instance of) the ancestor is an ancestor
12461 -- of the actual.
12463 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12464 Error_Msg_NE
12465 ("expect type derived from & in instantiation",
12466 Actual, First_Subtype (Ancestor));
12467 Abandon_Instantiation (Actual);
12468 end if;
12470 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12471 -- that the formal type declaration has been rewritten as a private
12472 -- extension.
12474 if Ada_Version >= Ada_2005
12475 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12476 and then Synchronized_Present (Parent (A_Gen_T))
12477 then
12478 -- The actual must be a synchronized tagged type
12480 if not Is_Tagged_Type (Act_T) then
12481 Error_Msg_N
12482 ("actual of synchronized type must be tagged", Actual);
12483 Abandon_Instantiation (Actual);
12485 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12486 and then Nkind (Type_Definition (Parent (Act_T))) =
12487 N_Derived_Type_Definition
12488 and then not Synchronized_Present
12489 (Type_Definition (Parent (Act_T)))
12490 then
12491 Error_Msg_N
12492 ("actual of synchronized type must be synchronized", Actual);
12493 Abandon_Instantiation (Actual);
12494 end if;
12495 end if;
12497 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12498 -- removes the second instance of the phrase "or allow pass by copy".
12500 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12501 Error_Msg_N
12502 ("cannot have atomic actual type for non-atomic formal type",
12503 Actual);
12505 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12506 Error_Msg_N
12507 ("cannot have volatile actual type for non-volatile formal type",
12508 Actual);
12509 end if;
12511 -- It should not be necessary to check for unknown discriminants on
12512 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12513 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12514 -- needs fixing. ???
12516 if Is_Definite_Subtype (A_Gen_T)
12517 and then not Unknown_Discriminants_Present (Formal)
12518 and then not Is_Definite_Subtype (Act_T)
12519 then
12520 Error_Msg_N ("actual subtype must be constrained", Actual);
12521 Abandon_Instantiation (Actual);
12522 end if;
12524 if not Unknown_Discriminants_Present (Formal) then
12525 if Is_Constrained (Ancestor) then
12526 if not Is_Constrained (Act_T) then
12527 Error_Msg_N ("actual subtype must be constrained", Actual);
12528 Abandon_Instantiation (Actual);
12529 end if;
12531 -- Ancestor is unconstrained, Check if generic formal and actual
12532 -- agree on constrainedness. The check only applies to array types
12533 -- and discriminated types.
12535 elsif Is_Constrained (Act_T) then
12536 if Ekind (Ancestor) = E_Access_Type
12537 or else (not Is_Constrained (A_Gen_T)
12538 and then Is_Composite_Type (A_Gen_T))
12539 then
12540 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12541 Abandon_Instantiation (Actual);
12542 end if;
12544 -- A class-wide type is only allowed if the formal has unknown
12545 -- discriminants.
12547 elsif Is_Class_Wide_Type (Act_T)
12548 and then not Has_Unknown_Discriminants (Ancestor)
12549 then
12550 Error_Msg_NE
12551 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12552 Abandon_Instantiation (Actual);
12554 -- Otherwise, the formal and actual must have the same number
12555 -- of discriminants and each discriminant of the actual must
12556 -- correspond to a discriminant of the formal.
12558 elsif Has_Discriminants (Act_T)
12559 and then not Has_Unknown_Discriminants (Act_T)
12560 and then Has_Discriminants (Ancestor)
12561 then
12562 Actual_Discr := First_Discriminant (Act_T);
12563 Ancestor_Discr := First_Discriminant (Ancestor);
12564 while Present (Actual_Discr)
12565 and then Present (Ancestor_Discr)
12566 loop
12567 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12568 No (Corresponding_Discriminant (Actual_Discr))
12569 then
12570 Error_Msg_NE
12571 ("discriminant & does not correspond "
12572 & "to ancestor discriminant", Actual, Actual_Discr);
12573 Abandon_Instantiation (Actual);
12574 end if;
12576 Next_Discriminant (Actual_Discr);
12577 Next_Discriminant (Ancestor_Discr);
12578 end loop;
12580 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12581 Error_Msg_NE
12582 ("actual for & must have same number of discriminants",
12583 Actual, Gen_T);
12584 Abandon_Instantiation (Actual);
12585 end if;
12587 -- This case should be caught by the earlier check for
12588 -- constrainedness, but the check here is added for completeness.
12590 elsif Has_Discriminants (Act_T)
12591 and then not Has_Unknown_Discriminants (Act_T)
12592 then
12593 Error_Msg_NE
12594 ("actual for & must not have discriminants", Actual, Gen_T);
12595 Abandon_Instantiation (Actual);
12597 elsif Has_Discriminants (Ancestor) then
12598 Error_Msg_NE
12599 ("actual for & must have known discriminants", Actual, Gen_T);
12600 Abandon_Instantiation (Actual);
12601 end if;
12603 if not Subtypes_Statically_Compatible
12604 (Act_T, Ancestor, Formal_Derived_Matching => True)
12605 then
12606 Error_Msg_N
12607 ("constraint on actual is incompatible with formal", Actual);
12608 Abandon_Instantiation (Actual);
12609 end if;
12610 end if;
12612 -- If the formal and actual types are abstract, check that there
12613 -- are no abstract primitives of the actual type that correspond to
12614 -- nonabstract primitives of the formal type (second sentence of
12615 -- RM95 3.9.3(9)).
12617 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12618 Check_Abstract_Primitives : declare
12619 Gen_Prims : constant Elist_Id :=
12620 Primitive_Operations (A_Gen_T);
12621 Gen_Elmt : Elmt_Id;
12622 Gen_Subp : Entity_Id;
12623 Anc_Subp : Entity_Id;
12624 Anc_Formal : Entity_Id;
12625 Anc_F_Type : Entity_Id;
12627 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12628 Act_Elmt : Elmt_Id;
12629 Act_Subp : Entity_Id;
12630 Act_Formal : Entity_Id;
12631 Act_F_Type : Entity_Id;
12633 Subprograms_Correspond : Boolean;
12635 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12636 -- Returns true if T2 is derived directly or indirectly from
12637 -- T1, including derivations from interfaces. T1 and T2 are
12638 -- required to be specific tagged base types.
12640 ------------------------
12641 -- Is_Tagged_Ancestor --
12642 ------------------------
12644 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12646 Intfc_Elmt : Elmt_Id;
12648 begin
12649 -- The predicate is satisfied if the types are the same
12651 if T1 = T2 then
12652 return True;
12654 -- If we've reached the top of the derivation chain then
12655 -- we know that T1 is not an ancestor of T2.
12657 elsif Etype (T2) = T2 then
12658 return False;
12660 -- Proceed to check T2's immediate parent
12662 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12663 return True;
12665 -- Finally, check to see if T1 is an ancestor of any of T2's
12666 -- progenitors.
12668 else
12669 Intfc_Elmt := First_Elmt (Interfaces (T2));
12670 while Present (Intfc_Elmt) loop
12671 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12672 return True;
12673 end if;
12675 Next_Elmt (Intfc_Elmt);
12676 end loop;
12677 end if;
12679 return False;
12680 end Is_Tagged_Ancestor;
12682 -- Start of processing for Check_Abstract_Primitives
12684 begin
12685 -- Loop over all of the formal derived type's primitives
12687 Gen_Elmt := First_Elmt (Gen_Prims);
12688 while Present (Gen_Elmt) loop
12689 Gen_Subp := Node (Gen_Elmt);
12691 -- If the primitive of the formal is not abstract, then
12692 -- determine whether there is a corresponding primitive of
12693 -- the actual type that's abstract.
12695 if not Is_Abstract_Subprogram (Gen_Subp) then
12696 Act_Elmt := First_Elmt (Act_Prims);
12697 while Present (Act_Elmt) loop
12698 Act_Subp := Node (Act_Elmt);
12700 -- If we find an abstract primitive of the actual,
12701 -- then we need to test whether it corresponds to the
12702 -- subprogram from which the generic formal primitive
12703 -- is inherited.
12705 if Is_Abstract_Subprogram (Act_Subp) then
12706 Anc_Subp := Alias (Gen_Subp);
12708 -- Test whether we have a corresponding primitive
12709 -- by comparing names, kinds, formal types, and
12710 -- result types.
12712 if Chars (Anc_Subp) = Chars (Act_Subp)
12713 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12714 then
12715 Anc_Formal := First_Formal (Anc_Subp);
12716 Act_Formal := First_Formal (Act_Subp);
12717 while Present (Anc_Formal)
12718 and then Present (Act_Formal)
12719 loop
12720 Anc_F_Type := Etype (Anc_Formal);
12721 Act_F_Type := Etype (Act_Formal);
12723 if Ekind (Anc_F_Type) =
12724 E_Anonymous_Access_Type
12725 then
12726 Anc_F_Type := Designated_Type (Anc_F_Type);
12728 if Ekind (Act_F_Type) =
12729 E_Anonymous_Access_Type
12730 then
12731 Act_F_Type :=
12732 Designated_Type (Act_F_Type);
12733 else
12734 exit;
12735 end if;
12737 elsif
12738 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12739 then
12740 exit;
12741 end if;
12743 Anc_F_Type := Base_Type (Anc_F_Type);
12744 Act_F_Type := Base_Type (Act_F_Type);
12746 -- If the formal is controlling, then the
12747 -- the type of the actual primitive's formal
12748 -- must be derived directly or indirectly
12749 -- from the type of the ancestor primitive's
12750 -- formal.
12752 if Is_Controlling_Formal (Anc_Formal) then
12753 if not Is_Tagged_Ancestor
12754 (Anc_F_Type, Act_F_Type)
12755 then
12756 exit;
12757 end if;
12759 -- Otherwise the types of the formals must
12760 -- be the same.
12762 elsif Anc_F_Type /= Act_F_Type then
12763 exit;
12764 end if;
12766 Next_Entity (Anc_Formal);
12767 Next_Entity (Act_Formal);
12768 end loop;
12770 -- If we traversed through all of the formals
12771 -- then so far the subprograms correspond, so
12772 -- now check that any result types correspond.
12774 if No (Anc_Formal) and then No (Act_Formal) then
12775 Subprograms_Correspond := True;
12777 if Ekind (Act_Subp) = E_Function then
12778 Anc_F_Type := Etype (Anc_Subp);
12779 Act_F_Type := Etype (Act_Subp);
12781 if Ekind (Anc_F_Type) =
12782 E_Anonymous_Access_Type
12783 then
12784 Anc_F_Type :=
12785 Designated_Type (Anc_F_Type);
12787 if Ekind (Act_F_Type) =
12788 E_Anonymous_Access_Type
12789 then
12790 Act_F_Type :=
12791 Designated_Type (Act_F_Type);
12792 else
12793 Subprograms_Correspond := False;
12794 end if;
12796 elsif
12797 Ekind (Act_F_Type)
12798 = E_Anonymous_Access_Type
12799 then
12800 Subprograms_Correspond := False;
12801 end if;
12803 Anc_F_Type := Base_Type (Anc_F_Type);
12804 Act_F_Type := Base_Type (Act_F_Type);
12806 -- Now either the result types must be
12807 -- the same or, if the result type is
12808 -- controlling, the result type of the
12809 -- actual primitive must descend from the
12810 -- result type of the ancestor primitive.
12812 if Subprograms_Correspond
12813 and then Anc_F_Type /= Act_F_Type
12814 and then
12815 Has_Controlling_Result (Anc_Subp)
12816 and then not Is_Tagged_Ancestor
12817 (Anc_F_Type, Act_F_Type)
12818 then
12819 Subprograms_Correspond := False;
12820 end if;
12821 end if;
12823 -- Found a matching subprogram belonging to
12824 -- formal ancestor type, so actual subprogram
12825 -- corresponds and this violates 3.9.3(9).
12827 if Subprograms_Correspond then
12828 Error_Msg_NE
12829 ("abstract subprogram & overrides "
12830 & "nonabstract subprogram of ancestor",
12831 Actual, Act_Subp);
12832 end if;
12833 end if;
12834 end if;
12835 end if;
12837 Next_Elmt (Act_Elmt);
12838 end loop;
12839 end if;
12841 Next_Elmt (Gen_Elmt);
12842 end loop;
12843 end Check_Abstract_Primitives;
12844 end if;
12846 -- Verify that limitedness matches. If parent is a limited
12847 -- interface then the generic formal is not unless declared
12848 -- explicitly so. If not declared limited, the actual cannot be
12849 -- limited (see AI05-0087).
12851 -- Even though this AI is a binding interpretation, we enable the
12852 -- check only in Ada 2012 mode, because this improper construct
12853 -- shows up in user code and in existing B-tests.
12855 if Is_Limited_Type (Act_T)
12856 and then not Is_Limited_Type (A_Gen_T)
12857 and then Ada_Version >= Ada_2012
12858 then
12859 if In_Instance then
12860 null;
12861 else
12862 Error_Msg_NE
12863 ("actual for non-limited & cannot be a limited type",
12864 Actual, Gen_T);
12865 Explain_Limited_Type (Act_T, Actual);
12866 Abandon_Instantiation (Actual);
12867 end if;
12868 end if;
12869 end Validate_Derived_Type_Instance;
12871 ----------------------------------------
12872 -- Validate_Discriminated_Formal_Type --
12873 ----------------------------------------
12875 procedure Validate_Discriminated_Formal_Type is
12876 Formal_Discr : Entity_Id;
12877 Actual_Discr : Entity_Id;
12878 Formal_Subt : Entity_Id;
12880 begin
12881 if Has_Discriminants (A_Gen_T) then
12882 if not Has_Discriminants (Act_T) then
12883 Error_Msg_NE
12884 ("actual for & must have discriminants", Actual, Gen_T);
12885 Abandon_Instantiation (Actual);
12887 elsif Is_Constrained (Act_T) then
12888 Error_Msg_NE
12889 ("actual for & must be unconstrained", Actual, Gen_T);
12890 Abandon_Instantiation (Actual);
12892 else
12893 Formal_Discr := First_Discriminant (A_Gen_T);
12894 Actual_Discr := First_Discriminant (Act_T);
12895 while Formal_Discr /= Empty loop
12896 if Actual_Discr = Empty then
12897 Error_Msg_NE
12898 ("discriminants on actual do not match formal",
12899 Actual, Gen_T);
12900 Abandon_Instantiation (Actual);
12901 end if;
12903 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12905 -- Access discriminants match if designated types do
12907 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12908 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12909 E_Anonymous_Access_Type
12910 and then
12911 Get_Instance_Of
12912 (Designated_Type (Base_Type (Formal_Subt))) =
12913 Designated_Type (Base_Type (Etype (Actual_Discr)))
12914 then
12915 null;
12917 elsif Base_Type (Formal_Subt) /=
12918 Base_Type (Etype (Actual_Discr))
12919 then
12920 Error_Msg_NE
12921 ("types of actual discriminants must match formal",
12922 Actual, Gen_T);
12923 Abandon_Instantiation (Actual);
12925 elsif not Subtypes_Statically_Match
12926 (Formal_Subt, Etype (Actual_Discr))
12927 and then Ada_Version >= Ada_95
12928 then
12929 Error_Msg_NE
12930 ("subtypes of actual discriminants must match formal",
12931 Actual, Gen_T);
12932 Abandon_Instantiation (Actual);
12933 end if;
12935 Next_Discriminant (Formal_Discr);
12936 Next_Discriminant (Actual_Discr);
12937 end loop;
12939 if Actual_Discr /= Empty then
12940 Error_Msg_NE
12941 ("discriminants on actual do not match formal",
12942 Actual, Gen_T);
12943 Abandon_Instantiation (Actual);
12944 end if;
12945 end if;
12946 end if;
12947 end Validate_Discriminated_Formal_Type;
12949 ---------------------------------------
12950 -- Validate_Incomplete_Type_Instance --
12951 ---------------------------------------
12953 procedure Validate_Incomplete_Type_Instance is
12954 begin
12955 if not Is_Tagged_Type (Act_T)
12956 and then Is_Tagged_Type (A_Gen_T)
12957 then
12958 Error_Msg_NE
12959 ("actual for & must be a tagged type", Actual, Gen_T);
12960 end if;
12962 Validate_Discriminated_Formal_Type;
12963 end Validate_Incomplete_Type_Instance;
12965 --------------------------------------
12966 -- Validate_Interface_Type_Instance --
12967 --------------------------------------
12969 procedure Validate_Interface_Type_Instance is
12970 begin
12971 if not Is_Interface (Act_T) then
12972 Error_Msg_NE
12973 ("actual for formal interface type must be an interface",
12974 Actual, Gen_T);
12976 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12977 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12978 or else Is_Protected_Interface (A_Gen_T) /=
12979 Is_Protected_Interface (Act_T)
12980 or else Is_Synchronized_Interface (A_Gen_T) /=
12981 Is_Synchronized_Interface (Act_T)
12982 then
12983 Error_Msg_NE
12984 ("actual for interface& does not match (RM 12.5.5(4))",
12985 Actual, Gen_T);
12986 end if;
12987 end Validate_Interface_Type_Instance;
12989 ------------------------------------
12990 -- Validate_Private_Type_Instance --
12991 ------------------------------------
12993 procedure Validate_Private_Type_Instance is
12994 begin
12995 if Is_Limited_Type (Act_T)
12996 and then not Is_Limited_Type (A_Gen_T)
12997 then
12998 if In_Instance then
12999 null;
13000 else
13001 Error_Msg_NE
13002 ("actual for non-limited & cannot be a limited type", Actual,
13003 Gen_T);
13004 Explain_Limited_Type (Act_T, Actual);
13005 Abandon_Instantiation (Actual);
13006 end if;
13008 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13009 and then not Has_Preelaborable_Initialization (Act_T)
13010 then
13011 Error_Msg_NE
13012 ("actual for & must have preelaborable initialization", Actual,
13013 Gen_T);
13015 elsif not Is_Definite_Subtype (Act_T)
13016 and then Is_Definite_Subtype (A_Gen_T)
13017 and then Ada_Version >= Ada_95
13018 then
13019 Error_Msg_NE
13020 ("actual for & must be a definite subtype", Actual, Gen_T);
13022 elsif not Is_Tagged_Type (Act_T)
13023 and then Is_Tagged_Type (A_Gen_T)
13024 then
13025 Error_Msg_NE
13026 ("actual for & must be a tagged type", Actual, Gen_T);
13027 end if;
13029 Validate_Discriminated_Formal_Type;
13030 Ancestor := Gen_T;
13031 end Validate_Private_Type_Instance;
13033 -- Start of processing for Instantiate_Type
13035 begin
13036 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13037 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13038 return New_List (Error);
13040 elsif not Is_Entity_Name (Actual)
13041 or else not Is_Type (Entity (Actual))
13042 then
13043 Error_Msg_NE
13044 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13045 Abandon_Instantiation (Actual);
13047 else
13048 Act_T := Entity (Actual);
13050 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13051 -- as a generic actual parameter if the corresponding formal type
13052 -- does not have a known_discriminant_part, or is a formal derived
13053 -- type that is an Unchecked_Union type.
13055 if Is_Unchecked_Union (Base_Type (Act_T)) then
13056 if not Has_Discriminants (A_Gen_T)
13057 or else (Is_Derived_Type (A_Gen_T)
13058 and then Is_Unchecked_Union (A_Gen_T))
13059 then
13060 null;
13061 else
13062 Error_Msg_N ("unchecked union cannot be the actual for a "
13063 & "discriminated formal type", Act_T);
13065 end if;
13066 end if;
13068 -- Deal with fixed/floating restrictions
13070 if Is_Floating_Point_Type (Act_T) then
13071 Check_Restriction (No_Floating_Point, Actual);
13072 elsif Is_Fixed_Point_Type (Act_T) then
13073 Check_Restriction (No_Fixed_Point, Actual);
13074 end if;
13076 -- Deal with error of using incomplete type as generic actual.
13077 -- This includes limited views of a type, even if the non-limited
13078 -- view may be available.
13080 if Ekind (Act_T) = E_Incomplete_Type
13081 or else (Is_Class_Wide_Type (Act_T)
13082 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13083 then
13084 -- If the formal is an incomplete type, the actual can be
13085 -- incomplete as well.
13087 if Ekind (A_Gen_T) = E_Incomplete_Type then
13088 null;
13090 elsif Is_Class_Wide_Type (Act_T)
13091 or else No (Full_View (Act_T))
13092 then
13093 Error_Msg_N ("premature use of incomplete type", Actual);
13094 Abandon_Instantiation (Actual);
13095 else
13096 Act_T := Full_View (Act_T);
13097 Set_Entity (Actual, Act_T);
13099 if Has_Private_Component (Act_T) then
13100 Error_Msg_N
13101 ("premature use of type with private component", Actual);
13102 end if;
13103 end if;
13105 -- Deal with error of premature use of private type as generic actual
13107 elsif Is_Private_Type (Act_T)
13108 and then Is_Private_Type (Base_Type (Act_T))
13109 and then not Is_Generic_Type (Act_T)
13110 and then not Is_Derived_Type (Act_T)
13111 and then No (Full_View (Root_Type (Act_T)))
13112 then
13113 -- If the formal is an incomplete type, the actual can be
13114 -- private or incomplete as well.
13116 if Ekind (A_Gen_T) = E_Incomplete_Type then
13117 null;
13118 else
13119 Error_Msg_N ("premature use of private type", Actual);
13120 end if;
13122 elsif Has_Private_Component (Act_T) then
13123 Error_Msg_N
13124 ("premature use of type with private component", Actual);
13125 end if;
13127 Set_Instance_Of (A_Gen_T, Act_T);
13129 -- If the type is generic, the class-wide type may also be used
13131 if Is_Tagged_Type (A_Gen_T)
13132 and then Is_Tagged_Type (Act_T)
13133 and then not Is_Class_Wide_Type (A_Gen_T)
13134 then
13135 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13136 Class_Wide_Type (Act_T));
13137 end if;
13139 if not Is_Abstract_Type (A_Gen_T)
13140 and then Is_Abstract_Type (Act_T)
13141 then
13142 Error_Msg_N
13143 ("actual of non-abstract formal cannot be abstract", Actual);
13144 end if;
13146 -- A generic scalar type is a first subtype for which we generate
13147 -- an anonymous base type. Indicate that the instance of this base
13148 -- is the base type of the actual.
13150 if Is_Scalar_Type (A_Gen_T) then
13151 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13152 end if;
13153 end if;
13155 if Error_Posted (Act_T) then
13156 null;
13157 else
13158 case Nkind (Def) is
13159 when N_Formal_Private_Type_Definition =>
13160 Validate_Private_Type_Instance;
13162 when N_Formal_Incomplete_Type_Definition =>
13163 Validate_Incomplete_Type_Instance;
13165 when N_Formal_Derived_Type_Definition =>
13166 Validate_Derived_Type_Instance;
13168 when N_Formal_Discrete_Type_Definition =>
13169 if not Is_Discrete_Type (Act_T) then
13170 Error_Msg_NE
13171 ("expect discrete type in instantiation of&",
13172 Actual, Gen_T);
13173 Abandon_Instantiation (Actual);
13174 end if;
13176 Diagnose_Predicated_Actual;
13178 when N_Formal_Signed_Integer_Type_Definition =>
13179 if not Is_Signed_Integer_Type (Act_T) then
13180 Error_Msg_NE
13181 ("expect signed integer type in instantiation of&",
13182 Actual, Gen_T);
13183 Abandon_Instantiation (Actual);
13184 end if;
13186 Diagnose_Predicated_Actual;
13188 when N_Formal_Modular_Type_Definition =>
13189 if not Is_Modular_Integer_Type (Act_T) then
13190 Error_Msg_NE
13191 ("expect modular type in instantiation of &",
13192 Actual, Gen_T);
13193 Abandon_Instantiation (Actual);
13194 end if;
13196 Diagnose_Predicated_Actual;
13198 when N_Formal_Floating_Point_Definition =>
13199 if not Is_Floating_Point_Type (Act_T) then
13200 Error_Msg_NE
13201 ("expect float type in instantiation of &", Actual, Gen_T);
13202 Abandon_Instantiation (Actual);
13203 end if;
13205 when N_Formal_Ordinary_Fixed_Point_Definition =>
13206 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13207 Error_Msg_NE
13208 ("expect ordinary fixed point type in instantiation of &",
13209 Actual, Gen_T);
13210 Abandon_Instantiation (Actual);
13211 end if;
13213 when N_Formal_Decimal_Fixed_Point_Definition =>
13214 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13215 Error_Msg_NE
13216 ("expect decimal type in instantiation of &",
13217 Actual, Gen_T);
13218 Abandon_Instantiation (Actual);
13219 end if;
13221 when N_Array_Type_Definition =>
13222 Validate_Array_Type_Instance;
13224 when N_Access_To_Object_Definition =>
13225 Validate_Access_Type_Instance;
13227 when N_Access_Function_Definition
13228 | N_Access_Procedure_Definition
13230 Validate_Access_Subprogram_Instance;
13232 when N_Record_Definition =>
13233 Validate_Interface_Type_Instance;
13235 when N_Derived_Type_Definition =>
13236 Validate_Derived_Interface_Type_Instance;
13238 when others =>
13239 raise Program_Error;
13240 end case;
13241 end if;
13243 Subt := New_Copy (Gen_T);
13245 -- Use adjusted sloc of subtype name as the location for other nodes in
13246 -- the subtype declaration.
13248 Loc := Sloc (Subt);
13250 Decl_Node :=
13251 Make_Subtype_Declaration (Loc,
13252 Defining_Identifier => Subt,
13253 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13255 if Is_Private_Type (Act_T) then
13256 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13258 elsif Is_Access_Type (Act_T)
13259 and then Is_Private_Type (Designated_Type (Act_T))
13260 then
13261 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13262 end if;
13264 -- In Ada 2012 the actual may be a limited view. Indicate that
13265 -- the local subtype must be treated as such.
13267 if From_Limited_With (Act_T) then
13268 Set_Ekind (Subt, E_Incomplete_Subtype);
13269 Set_From_Limited_With (Subt);
13270 end if;
13272 Decl_Nodes := New_List (Decl_Node);
13274 -- Flag actual derived types so their elaboration produces the
13275 -- appropriate renamings for the primitive operations of the ancestor.
13276 -- Flag actual for formal private types as well, to determine whether
13277 -- operations in the private part may override inherited operations.
13278 -- If the formal has an interface list, the ancestor is not the
13279 -- parent, but the analyzed formal that includes the interface
13280 -- operations of all its progenitors.
13282 -- Same treatment for formal private types, so we can check whether the
13283 -- type is tagged limited when validating derivations in the private
13284 -- part. (See AI05-096).
13286 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13287 if Present (Interface_List (Def)) then
13288 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13289 else
13290 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13291 end if;
13293 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13294 N_Formal_Incomplete_Type_Definition)
13295 then
13296 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13297 end if;
13299 -- If the actual is a synchronized type that implements an interface,
13300 -- the primitive operations are attached to the corresponding record,
13301 -- and we have to treat it as an additional generic actual, so that its
13302 -- primitive operations become visible in the instance. The task or
13303 -- protected type itself does not carry primitive operations.
13305 if Is_Concurrent_Type (Act_T)
13306 and then Is_Tagged_Type (Act_T)
13307 and then Present (Corresponding_Record_Type (Act_T))
13308 and then Present (Ancestor)
13309 and then Is_Interface (Ancestor)
13310 then
13311 declare
13312 Corr_Rec : constant Entity_Id :=
13313 Corresponding_Record_Type (Act_T);
13314 New_Corr : Entity_Id;
13315 Corr_Decl : Node_Id;
13317 begin
13318 New_Corr := Make_Temporary (Loc, 'S');
13319 Corr_Decl :=
13320 Make_Subtype_Declaration (Loc,
13321 Defining_Identifier => New_Corr,
13322 Subtype_Indication =>
13323 New_Occurrence_Of (Corr_Rec, Loc));
13324 Append_To (Decl_Nodes, Corr_Decl);
13326 if Ekind (Act_T) = E_Task_Type then
13327 Set_Ekind (Subt, E_Task_Subtype);
13328 else
13329 Set_Ekind (Subt, E_Protected_Subtype);
13330 end if;
13332 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13333 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13334 Set_Generic_Parent_Type (Decl_Node, Empty);
13335 end;
13336 end if;
13338 -- For a floating-point type, capture dimension info if any, because
13339 -- the generated subtype declaration does not come from source and
13340 -- will not process dimensions.
13342 if Is_Floating_Point_Type (Act_T) then
13343 Copy_Dimensions (Act_T, Subt);
13344 end if;
13346 return Decl_Nodes;
13347 end Instantiate_Type;
13349 ---------------------
13350 -- Is_In_Main_Unit --
13351 ---------------------
13353 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13354 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13355 Current_Unit : Node_Id;
13357 begin
13358 if Unum = Main_Unit then
13359 return True;
13361 -- If the current unit is a subunit then it is either the main unit or
13362 -- is being compiled as part of the main unit.
13364 elsif Nkind (N) = N_Compilation_Unit then
13365 return Nkind (Unit (N)) = N_Subunit;
13366 end if;
13368 Current_Unit := Parent (N);
13369 while Present (Current_Unit)
13370 and then Nkind (Current_Unit) /= N_Compilation_Unit
13371 loop
13372 Current_Unit := Parent (Current_Unit);
13373 end loop;
13375 -- The instantiation node is in the main unit, or else the current node
13376 -- (perhaps as the result of nested instantiations) is in the main unit,
13377 -- or in the declaration of the main unit, which in this last case must
13378 -- be a body.
13380 return
13381 Current_Unit = Cunit (Main_Unit)
13382 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13383 or else (Present (Current_Unit)
13384 and then Present (Library_Unit (Current_Unit))
13385 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13386 end Is_In_Main_Unit;
13388 ----------------------------
13389 -- Load_Parent_Of_Generic --
13390 ----------------------------
13392 procedure Load_Parent_Of_Generic
13393 (N : Node_Id;
13394 Spec : Node_Id;
13395 Body_Optional : Boolean := False)
13397 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13398 Saved_Style_Check : constant Boolean := Style_Check;
13399 Saved_Warnings : constant Warning_Record := Save_Warnings;
13400 True_Parent : Node_Id;
13401 Inst_Node : Node_Id;
13402 OK : Boolean;
13403 Previous_Instances : constant Elist_Id := New_Elmt_List;
13405 procedure Collect_Previous_Instances (Decls : List_Id);
13406 -- Collect all instantiations in the given list of declarations, that
13407 -- precede the generic that we need to load. If the bodies of these
13408 -- instantiations are available, we must analyze them, to ensure that
13409 -- the public symbols generated are the same when the unit is compiled
13410 -- to generate code, and when it is compiled in the context of a unit
13411 -- that needs a particular nested instance. This process is applied to
13412 -- both package and subprogram instances.
13414 --------------------------------
13415 -- Collect_Previous_Instances --
13416 --------------------------------
13418 procedure Collect_Previous_Instances (Decls : List_Id) is
13419 Decl : Node_Id;
13421 begin
13422 Decl := First (Decls);
13423 while Present (Decl) loop
13424 if Sloc (Decl) >= Sloc (Inst_Node) then
13425 return;
13427 -- If Decl is an instantiation, then record it as requiring
13428 -- instantiation of the corresponding body, except if it is an
13429 -- abbreviated instantiation generated internally for conformance
13430 -- checking purposes only for the case of a formal package
13431 -- declared without a box (see Instantiate_Formal_Package). Such
13432 -- an instantiation does not generate any code (the actual code
13433 -- comes from actual) and thus does not need to be analyzed here.
13434 -- If the instantiation appears with a generic package body it is
13435 -- not analyzed here either.
13437 elsif Nkind (Decl) = N_Package_Instantiation
13438 and then not Is_Internal (Defining_Entity (Decl))
13439 then
13440 Append_Elmt (Decl, Previous_Instances);
13442 -- For a subprogram instantiation, omit instantiations intrinsic
13443 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13445 elsif Nkind_In (Decl, N_Function_Instantiation,
13446 N_Procedure_Instantiation)
13447 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13448 then
13449 Append_Elmt (Decl, Previous_Instances);
13451 elsif Nkind (Decl) = N_Package_Declaration then
13452 Collect_Previous_Instances
13453 (Visible_Declarations (Specification (Decl)));
13454 Collect_Previous_Instances
13455 (Private_Declarations (Specification (Decl)));
13457 -- Previous non-generic bodies may contain instances as well
13459 elsif Nkind (Decl) = N_Package_Body
13460 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13461 then
13462 Collect_Previous_Instances (Declarations (Decl));
13464 elsif Nkind (Decl) = N_Subprogram_Body
13465 and then not Acts_As_Spec (Decl)
13466 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13467 then
13468 Collect_Previous_Instances (Declarations (Decl));
13469 end if;
13471 Next (Decl);
13472 end loop;
13473 end Collect_Previous_Instances;
13475 -- Start of processing for Load_Parent_Of_Generic
13477 begin
13478 if not In_Same_Source_Unit (N, Spec)
13479 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13480 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13481 and then not Is_In_Main_Unit (Spec))
13482 then
13483 -- Find body of parent of spec, and analyze it. A special case arises
13484 -- when the parent is an instantiation, that is to say when we are
13485 -- currently instantiating a nested generic. In that case, there is
13486 -- no separate file for the body of the enclosing instance. Instead,
13487 -- the enclosing body must be instantiated as if it were a pending
13488 -- instantiation, in order to produce the body for the nested generic
13489 -- we require now. Note that in that case the generic may be defined
13490 -- in a package body, the instance defined in the same package body,
13491 -- and the original enclosing body may not be in the main unit.
13493 Inst_Node := Empty;
13495 True_Parent := Parent (Spec);
13496 while Present (True_Parent)
13497 and then Nkind (True_Parent) /= N_Compilation_Unit
13498 loop
13499 if Nkind (True_Parent) = N_Package_Declaration
13500 and then
13501 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13502 then
13503 -- Parent is a compilation unit that is an instantiation.
13504 -- Instantiation node has been replaced with package decl.
13506 Inst_Node := Original_Node (True_Parent);
13507 exit;
13509 elsif Nkind (True_Parent) = N_Package_Declaration
13510 and then Present (Generic_Parent (Specification (True_Parent)))
13511 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13512 then
13513 -- Parent is an instantiation within another specification.
13514 -- Declaration for instance has been inserted before original
13515 -- instantiation node. A direct link would be preferable?
13517 Inst_Node := Next (True_Parent);
13518 while Present (Inst_Node)
13519 and then Nkind (Inst_Node) /= N_Package_Instantiation
13520 loop
13521 Next (Inst_Node);
13522 end loop;
13524 -- If the instance appears within a generic, and the generic
13525 -- unit is defined within a formal package of the enclosing
13526 -- generic, there is no generic body available, and none
13527 -- needed. A more precise test should be used ???
13529 if No (Inst_Node) then
13530 return;
13531 end if;
13533 exit;
13535 else
13536 True_Parent := Parent (True_Parent);
13537 end if;
13538 end loop;
13540 -- Case where we are currently instantiating a nested generic
13542 if Present (Inst_Node) then
13543 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13545 -- Instantiation node and declaration of instantiated package
13546 -- were exchanged when only the declaration was needed.
13547 -- Restore instantiation node before proceeding with body.
13549 Set_Unit (Parent (True_Parent), Inst_Node);
13550 end if;
13552 -- Now complete instantiation of enclosing body, if it appears in
13553 -- some other unit. If it appears in the current unit, the body
13554 -- will have been instantiated already.
13556 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13558 -- We need to determine the expander mode to instantiate the
13559 -- enclosing body. Because the generic body we need may use
13560 -- global entities declared in the enclosing package (including
13561 -- aggregates) it is in general necessary to compile this body
13562 -- with expansion enabled, except if we are within a generic
13563 -- package, in which case the usual generic rule applies.
13565 declare
13566 Exp_Status : Boolean := True;
13567 Scop : Entity_Id;
13569 begin
13570 -- Loop through scopes looking for generic package
13572 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13573 while Present (Scop)
13574 and then Scop /= Standard_Standard
13575 loop
13576 if Ekind (Scop) = E_Generic_Package then
13577 Exp_Status := False;
13578 exit;
13579 end if;
13581 Scop := Scope (Scop);
13582 end loop;
13584 -- Collect previous instantiations in the unit that contains
13585 -- the desired generic.
13587 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13588 and then not Body_Optional
13589 then
13590 declare
13591 Decl : Elmt_Id;
13592 Info : Pending_Body_Info;
13593 Par : Node_Id;
13595 begin
13596 Par := Parent (Inst_Node);
13597 while Present (Par) loop
13598 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13599 Par := Parent (Par);
13600 end loop;
13602 pragma Assert (Present (Par));
13604 if Nkind (Par) = N_Package_Body then
13605 Collect_Previous_Instances (Declarations (Par));
13607 elsif Nkind (Par) = N_Package_Declaration then
13608 Collect_Previous_Instances
13609 (Visible_Declarations (Specification (Par)));
13610 Collect_Previous_Instances
13611 (Private_Declarations (Specification (Par)));
13613 else
13614 -- Enclosing unit is a subprogram body. In this
13615 -- case all instance bodies are processed in order
13616 -- and there is no need to collect them separately.
13618 null;
13619 end if;
13621 Decl := First_Elmt (Previous_Instances);
13622 while Present (Decl) loop
13623 Info :=
13624 (Inst_Node => Node (Decl),
13625 Act_Decl =>
13626 Instance_Spec (Node (Decl)),
13627 Expander_Status => Exp_Status,
13628 Current_Sem_Unit =>
13629 Get_Code_Unit (Sloc (Node (Decl))),
13630 Scope_Suppress => Scope_Suppress,
13631 Local_Suppress_Stack_Top =>
13632 Local_Suppress_Stack_Top,
13633 Version => Ada_Version,
13634 Version_Pragma => Ada_Version_Pragma,
13635 Warnings => Save_Warnings,
13636 SPARK_Mode => SPARK_Mode,
13637 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13639 -- Package instance
13641 if Nkind (Node (Decl)) = N_Package_Instantiation
13642 then
13643 Instantiate_Package_Body
13644 (Info, Body_Optional => True);
13646 -- Subprogram instance
13648 else
13649 -- The instance_spec is in the wrapper package,
13650 -- usually followed by its local renaming
13651 -- declaration. See Build_Subprogram_Renaming
13652 -- for details. If the instance carries aspects,
13653 -- these result in the corresponding pragmas,
13654 -- inserted after the subprogram declaration.
13655 -- They must be skipped as well when retrieving
13656 -- the desired spec. Some of them may have been
13657 -- rewritten as null statements.
13658 -- A direct link would be more robust ???
13660 declare
13661 Decl : Node_Id :=
13662 (Last (Visible_Declarations
13663 (Specification (Info.Act_Decl))));
13664 begin
13665 while Nkind_In (Decl,
13666 N_Null_Statement,
13667 N_Pragma,
13668 N_Subprogram_Renaming_Declaration)
13669 loop
13670 Decl := Prev (Decl);
13671 end loop;
13673 Info.Act_Decl := Decl;
13674 end;
13676 Instantiate_Subprogram_Body
13677 (Info, Body_Optional => True);
13678 end if;
13680 Next_Elmt (Decl);
13681 end loop;
13682 end;
13683 end if;
13685 Instantiate_Package_Body
13686 (Body_Info =>
13687 ((Inst_Node => Inst_Node,
13688 Act_Decl => True_Parent,
13689 Expander_Status => Exp_Status,
13690 Current_Sem_Unit => Get_Code_Unit
13691 (Sloc (Inst_Node)),
13692 Scope_Suppress => Scope_Suppress,
13693 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13694 Version => Ada_Version,
13695 Version_Pragma => Ada_Version_Pragma,
13696 Warnings => Save_Warnings,
13697 SPARK_Mode => SPARK_Mode,
13698 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13699 Body_Optional => Body_Optional);
13700 end;
13701 end if;
13703 -- Case where we are not instantiating a nested generic
13705 else
13706 Opt.Style_Check := False;
13707 Expander_Mode_Save_And_Set (True);
13708 Load_Needed_Body (Comp_Unit, OK);
13709 Opt.Style_Check := Saved_Style_Check;
13710 Restore_Warnings (Saved_Warnings);
13711 Expander_Mode_Restore;
13713 if not OK
13714 and then Unit_Requires_Body (Defining_Entity (Spec))
13715 and then not Body_Optional
13716 then
13717 declare
13718 Bname : constant Unit_Name_Type :=
13719 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13721 begin
13722 -- In CodePeer mode, the missing body may make the analysis
13723 -- incomplete, but we do not treat it as fatal.
13725 if CodePeer_Mode then
13726 return;
13728 else
13729 Error_Msg_Unit_1 := Bname;
13730 Error_Msg_N ("this instantiation requires$!", N);
13731 Error_Msg_File_1 :=
13732 Get_File_Name (Bname, Subunit => False);
13733 Error_Msg_N ("\but file{ was not found!", N);
13734 raise Unrecoverable_Error;
13735 end if;
13736 end;
13737 end if;
13738 end if;
13739 end if;
13741 -- If loading parent of the generic caused an instantiation circularity,
13742 -- we abandon compilation at this point, because otherwise in some cases
13743 -- we get into trouble with infinite recursions after this point.
13745 if Circularity_Detected then
13746 raise Unrecoverable_Error;
13747 end if;
13748 end Load_Parent_Of_Generic;
13750 ---------------------------------
13751 -- Map_Formal_Package_Entities --
13752 ---------------------------------
13754 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13755 E1 : Entity_Id;
13756 E2 : Entity_Id;
13758 begin
13759 Set_Instance_Of (Form, Act);
13761 -- Traverse formal and actual package to map the corresponding entities.
13762 -- We skip over internal entities that may be generated during semantic
13763 -- analysis, and find the matching entities by name, given that they
13764 -- must appear in the same order.
13766 E1 := First_Entity (Form);
13767 E2 := First_Entity (Act);
13768 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13769 -- Could this test be a single condition??? Seems like it could, and
13770 -- isn't FPE (Form) a constant anyway???
13772 if not Is_Internal (E1)
13773 and then Present (Parent (E1))
13774 and then not Is_Class_Wide_Type (E1)
13775 and then not Is_Internal_Name (Chars (E1))
13776 then
13777 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13778 Next_Entity (E2);
13779 end loop;
13781 if No (E2) then
13782 exit;
13783 else
13784 Set_Instance_Of (E1, E2);
13786 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13787 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13788 end if;
13790 if Is_Constrained (E1) then
13791 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13792 end if;
13794 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13795 Map_Formal_Package_Entities (E1, E2);
13796 end if;
13797 end if;
13798 end if;
13800 Next_Entity (E1);
13801 end loop;
13802 end Map_Formal_Package_Entities;
13804 -----------------------
13805 -- Move_Freeze_Nodes --
13806 -----------------------
13808 procedure Move_Freeze_Nodes
13809 (Out_Of : Entity_Id;
13810 After : Node_Id;
13811 L : List_Id)
13813 Decl : Node_Id;
13814 Next_Decl : Node_Id;
13815 Next_Node : Node_Id := After;
13816 Spec : Node_Id;
13818 function Is_Outer_Type (T : Entity_Id) return Boolean;
13819 -- Check whether entity is declared in a scope external to that of the
13820 -- generic unit.
13822 -------------------
13823 -- Is_Outer_Type --
13824 -------------------
13826 function Is_Outer_Type (T : Entity_Id) return Boolean is
13827 Scop : Entity_Id := Scope (T);
13829 begin
13830 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13831 return True;
13833 else
13834 while Scop /= Standard_Standard loop
13835 if Scop = Out_Of then
13836 return False;
13837 else
13838 Scop := Scope (Scop);
13839 end if;
13840 end loop;
13842 return True;
13843 end if;
13844 end Is_Outer_Type;
13846 -- Start of processing for Move_Freeze_Nodes
13848 begin
13849 if No (L) then
13850 return;
13851 end if;
13853 -- First remove the freeze nodes that may appear before all other
13854 -- declarations.
13856 Decl := First (L);
13857 while Present (Decl)
13858 and then Nkind (Decl) = N_Freeze_Entity
13859 and then Is_Outer_Type (Entity (Decl))
13860 loop
13861 Decl := Remove_Head (L);
13862 Insert_After (Next_Node, Decl);
13863 Set_Analyzed (Decl, False);
13864 Next_Node := Decl;
13865 Decl := First (L);
13866 end loop;
13868 -- Next scan the list of declarations and remove each freeze node that
13869 -- appears ahead of the current node.
13871 while Present (Decl) loop
13872 while Present (Next (Decl))
13873 and then Nkind (Next (Decl)) = N_Freeze_Entity
13874 and then Is_Outer_Type (Entity (Next (Decl)))
13875 loop
13876 Next_Decl := Remove_Next (Decl);
13877 Insert_After (Next_Node, Next_Decl);
13878 Set_Analyzed (Next_Decl, False);
13879 Next_Node := Next_Decl;
13880 end loop;
13882 -- If the declaration is a nested package or concurrent type, then
13883 -- recurse. Nested generic packages will have been processed from the
13884 -- inside out.
13886 case Nkind (Decl) is
13887 when N_Package_Declaration =>
13888 Spec := Specification (Decl);
13890 when N_Task_Type_Declaration =>
13891 Spec := Task_Definition (Decl);
13893 when N_Protected_Type_Declaration =>
13894 Spec := Protected_Definition (Decl);
13896 when others =>
13897 Spec := Empty;
13898 end case;
13900 if Present (Spec) then
13901 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13902 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13903 end if;
13905 Next (Decl);
13906 end loop;
13907 end Move_Freeze_Nodes;
13909 ----------------
13910 -- Next_Assoc --
13911 ----------------
13913 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13914 begin
13915 return Generic_Renamings.Table (E).Next_In_HTable;
13916 end Next_Assoc;
13918 ------------------------
13919 -- Preanalyze_Actuals --
13920 ------------------------
13922 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13923 Assoc : Node_Id;
13924 Act : Node_Id;
13925 Errs : constant Nat := Serious_Errors_Detected;
13927 Cur : Entity_Id := Empty;
13928 -- Current homograph of the instance name
13930 Vis : Boolean := False;
13931 -- Saved visibility status of the current homograph
13933 begin
13934 Assoc := First (Generic_Associations (N));
13936 -- If the instance is a child unit, its name may hide an outer homonym,
13937 -- so make it invisible to perform name resolution on the actuals.
13939 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13940 and then Present
13941 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13942 then
13943 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13945 if Is_Compilation_Unit (Cur) then
13946 Vis := Is_Immediately_Visible (Cur);
13947 Set_Is_Immediately_Visible (Cur, False);
13948 else
13949 Cur := Empty;
13950 end if;
13951 end if;
13953 while Present (Assoc) loop
13954 if Nkind (Assoc) /= N_Others_Choice then
13955 Act := Explicit_Generic_Actual_Parameter (Assoc);
13957 -- Within a nested instantiation, a defaulted actual is an empty
13958 -- association, so nothing to analyze. If the subprogram actual
13959 -- is an attribute, analyze prefix only, because actual is not a
13960 -- complete attribute reference.
13962 -- If actual is an allocator, analyze expression only. The full
13963 -- analysis can generate code, and if instance is a compilation
13964 -- unit we have to wait until the package instance is installed
13965 -- to have a proper place to insert this code.
13967 -- String literals may be operators, but at this point we do not
13968 -- know whether the actual is a formal subprogram or a string.
13970 if No (Act) then
13971 null;
13973 elsif Nkind (Act) = N_Attribute_Reference then
13974 Analyze (Prefix (Act));
13976 elsif Nkind (Act) = N_Explicit_Dereference then
13977 Analyze (Prefix (Act));
13979 elsif Nkind (Act) = N_Allocator then
13980 declare
13981 Expr : constant Node_Id := Expression (Act);
13983 begin
13984 if Nkind (Expr) = N_Subtype_Indication then
13985 Analyze (Subtype_Mark (Expr));
13987 -- Analyze separately each discriminant constraint, when
13988 -- given with a named association.
13990 declare
13991 Constr : Node_Id;
13993 begin
13994 Constr := First (Constraints (Constraint (Expr)));
13995 while Present (Constr) loop
13996 if Nkind (Constr) = N_Discriminant_Association then
13997 Analyze (Expression (Constr));
13998 else
13999 Analyze (Constr);
14000 end if;
14002 Next (Constr);
14003 end loop;
14004 end;
14006 else
14007 Analyze (Expr);
14008 end if;
14009 end;
14011 elsif Nkind (Act) /= N_Operator_Symbol then
14012 Analyze (Act);
14014 -- Within a package instance, mark actuals that are limited
14015 -- views, so their use can be moved to the body of the
14016 -- enclosing unit.
14018 if Is_Entity_Name (Act)
14019 and then Is_Type (Entity (Act))
14020 and then From_Limited_With (Entity (Act))
14021 and then Present (Inst)
14022 then
14023 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14024 end if;
14025 end if;
14027 if Errs /= Serious_Errors_Detected then
14029 -- Do a minimal analysis of the generic, to prevent spurious
14030 -- warnings complaining about the generic being unreferenced,
14031 -- before abandoning the instantiation.
14033 Analyze (Name (N));
14035 if Is_Entity_Name (Name (N))
14036 and then Etype (Name (N)) /= Any_Type
14037 then
14038 Generate_Reference (Entity (Name (N)), Name (N));
14039 Set_Is_Instantiated (Entity (Name (N)));
14040 end if;
14042 if Present (Cur) then
14044 -- For the case of a child instance hiding an outer homonym,
14045 -- provide additional warning which might explain the error.
14047 Set_Is_Immediately_Visible (Cur, Vis);
14048 Error_Msg_NE
14049 ("& hides outer unit with the same name??",
14050 N, Defining_Unit_Name (N));
14051 end if;
14053 Abandon_Instantiation (Act);
14054 end if;
14055 end if;
14057 Next (Assoc);
14058 end loop;
14060 if Present (Cur) then
14061 Set_Is_Immediately_Visible (Cur, Vis);
14062 end if;
14063 end Preanalyze_Actuals;
14065 -------------------------------
14066 -- Provide_Completing_Bodies --
14067 -------------------------------
14069 procedure Provide_Completing_Bodies (N : Node_Id) is
14070 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14071 -- Generate the completing body for subprogram declaration Subp_Decl
14073 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14074 -- Generating completing bodies for all subprograms found in declarative
14075 -- list Decls.
14077 ---------------------------
14078 -- Build_Completing_Body --
14079 ---------------------------
14081 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14082 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14083 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14084 Spec : Node_Id;
14086 begin
14087 -- Nothing to do if the subprogram already has a completing body
14089 if Present (Corresponding_Body (Subp_Decl)) then
14090 return;
14092 -- Mark the function as having a valid return statement even though
14093 -- the body contains a single raise statement.
14095 elsif Ekind (Subp_Id) = E_Function then
14096 Set_Return_Present (Subp_Id);
14097 end if;
14099 -- Clone the specification to obtain new entities and reset the only
14100 -- semantic field.
14102 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14103 Set_Generic_Parent (Spec, Empty);
14105 -- Generate:
14106 -- function Func ... return ... is
14107 -- <or>
14108 -- procedure Proc ... is
14109 -- begin
14110 -- raise Program_Error with "access before elaboration";
14111 -- edn Proc;
14113 Insert_After_And_Analyze (Subp_Decl,
14114 Make_Subprogram_Body (Loc,
14115 Specification => Spec,
14116 Declarations => New_List,
14117 Handled_Statement_Sequence =>
14118 Make_Handled_Sequence_Of_Statements (Loc,
14119 Statements => New_List (
14120 Make_Raise_Program_Error (Loc,
14121 Reason => PE_Access_Before_Elaboration)))));
14122 end Build_Completing_Body;
14124 ----------------------------------
14125 -- Provide_Completing_Bodies_In --
14126 ----------------------------------
14128 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14129 Decl : Node_Id;
14131 begin
14132 if Present (Decls) then
14133 Decl := First (Decls);
14134 while Present (Decl) loop
14135 Provide_Completing_Bodies (Decl);
14136 Next (Decl);
14137 end loop;
14138 end if;
14139 end Provide_Completing_Bodies_In;
14141 -- Local variables
14143 Spec : Node_Id;
14145 -- Start of processing for Provide_Completing_Bodies
14147 begin
14148 if Nkind (N) = N_Package_Declaration then
14149 Spec := Specification (N);
14151 Push_Scope (Defining_Entity (N));
14152 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14153 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14154 Pop_Scope;
14156 elsif Nkind (N) = N_Subprogram_Declaration then
14157 Build_Completing_Body (N);
14158 end if;
14159 end Provide_Completing_Bodies;
14161 -------------------
14162 -- Remove_Parent --
14163 -------------------
14165 procedure Remove_Parent (In_Body : Boolean := False) is
14166 S : Entity_Id := Current_Scope;
14167 -- S is the scope containing the instantiation just completed. The scope
14168 -- stack contains the parent instances of the instantiation, followed by
14169 -- the original S.
14171 Cur_P : Entity_Id;
14172 E : Entity_Id;
14173 P : Entity_Id;
14174 Hidden : Elmt_Id;
14176 begin
14177 -- After child instantiation is complete, remove from scope stack the
14178 -- extra copy of the current scope, and then remove parent instances.
14180 if not In_Body then
14181 Pop_Scope;
14183 while Current_Scope /= S loop
14184 P := Current_Scope;
14185 End_Package_Scope (Current_Scope);
14187 if In_Open_Scopes (P) then
14188 E := First_Entity (P);
14189 while Present (E) loop
14190 Set_Is_Immediately_Visible (E, True);
14191 Next_Entity (E);
14192 end loop;
14194 -- If instantiation is declared in a block, it is the enclosing
14195 -- scope that might be a parent instance. Note that only one
14196 -- block can be involved, because the parent instances have
14197 -- been installed within it.
14199 if Ekind (P) = E_Block then
14200 Cur_P := Scope (P);
14201 else
14202 Cur_P := P;
14203 end if;
14205 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14206 -- We are within an instance of some sibling. Retain
14207 -- visibility of parent, for proper subsequent cleanup, and
14208 -- reinstall private declarations as well.
14210 Set_In_Private_Part (P);
14211 Install_Private_Declarations (P);
14212 end if;
14214 -- If the ultimate parent is a top-level unit recorded in
14215 -- Instance_Parent_Unit, then reset its visibility to what it was
14216 -- before instantiation. (It's not clear what the purpose is of
14217 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14218 -- present before the ultimate parent test was added.???)
14220 elsif not In_Open_Scopes (Scope (P))
14221 or else (P = Instance_Parent_Unit
14222 and then not Parent_Unit_Visible)
14223 then
14224 Set_Is_Immediately_Visible (P, False);
14226 -- If the current scope is itself an instantiation of a generic
14227 -- nested within P, and we are in the private part of body of this
14228 -- instantiation, restore the full views of P, that were removed
14229 -- in End_Package_Scope above. This obscure case can occur when a
14230 -- subunit of a generic contains an instance of a child unit of
14231 -- its generic parent unit.
14233 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14234 declare
14235 Par : constant Entity_Id :=
14236 Generic_Parent (Package_Specification (S));
14237 begin
14238 if Present (Par)
14239 and then P = Scope (Par)
14240 and then (In_Package_Body (S) or else In_Private_Part (S))
14241 then
14242 Set_In_Private_Part (P);
14243 Install_Private_Declarations (P);
14244 end if;
14245 end;
14246 end if;
14247 end loop;
14249 -- Reset visibility of entities in the enclosing scope
14251 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14253 Hidden := First_Elmt (Hidden_Entities);
14254 while Present (Hidden) loop
14255 Set_Is_Immediately_Visible (Node (Hidden), True);
14256 Next_Elmt (Hidden);
14257 end loop;
14259 else
14260 -- Each body is analyzed separately, and there is no context that
14261 -- needs preserving from one body instance to the next, so remove all
14262 -- parent scopes that have been installed.
14264 while Present (S) loop
14265 End_Package_Scope (S);
14266 Set_Is_Immediately_Visible (S, False);
14267 S := Current_Scope;
14268 exit when S = Standard_Standard;
14269 end loop;
14270 end if;
14271 end Remove_Parent;
14273 -----------------
14274 -- Restore_Env --
14275 -----------------
14277 procedure Restore_Env is
14278 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14280 begin
14281 if No (Current_Instantiated_Parent.Act_Id) then
14282 -- Restore environment after subprogram inlining
14284 Restore_Private_Views (Empty);
14285 end if;
14287 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14288 Exchanged_Views := Saved.Exchanged_Views;
14289 Hidden_Entities := Saved.Hidden_Entities;
14290 Current_Sem_Unit := Saved.Current_Sem_Unit;
14291 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14292 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14294 Restore_Opt_Config_Switches (Saved.Switches);
14296 Instance_Envs.Decrement_Last;
14297 end Restore_Env;
14299 ---------------------------
14300 -- Restore_Private_Views --
14301 ---------------------------
14303 procedure Restore_Private_Views
14304 (Pack_Id : Entity_Id;
14305 Is_Package : Boolean := True)
14307 M : Elmt_Id;
14308 E : Entity_Id;
14309 Typ : Entity_Id;
14310 Dep_Elmt : Elmt_Id;
14311 Dep_Typ : Node_Id;
14313 procedure Restore_Nested_Formal (Formal : Entity_Id);
14314 -- Hide the generic formals of formal packages declared with box which
14315 -- were reachable in the current instantiation.
14317 ---------------------------
14318 -- Restore_Nested_Formal --
14319 ---------------------------
14321 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14322 Ent : Entity_Id;
14324 begin
14325 if Present (Renamed_Object (Formal))
14326 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14327 then
14328 return;
14330 elsif Present (Associated_Formal_Package (Formal)) then
14331 Ent := First_Entity (Formal);
14332 while Present (Ent) loop
14333 exit when Ekind (Ent) = E_Package
14334 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14336 Set_Is_Hidden (Ent);
14337 Set_Is_Potentially_Use_Visible (Ent, False);
14339 -- If package, then recurse
14341 if Ekind (Ent) = E_Package then
14342 Restore_Nested_Formal (Ent);
14343 end if;
14345 Next_Entity (Ent);
14346 end loop;
14347 end if;
14348 end Restore_Nested_Formal;
14350 -- Start of processing for Restore_Private_Views
14352 begin
14353 M := First_Elmt (Exchanged_Views);
14354 while Present (M) loop
14355 Typ := Node (M);
14357 -- Subtypes of types whose views have been exchanged, and that are
14358 -- defined within the instance, were not on the Private_Dependents
14359 -- list on entry to the instance, so they have to be exchanged
14360 -- explicitly now, in order to remain consistent with the view of the
14361 -- parent type.
14363 if Ekind_In (Typ, E_Private_Type,
14364 E_Limited_Private_Type,
14365 E_Record_Type_With_Private)
14366 then
14367 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14368 while Present (Dep_Elmt) loop
14369 Dep_Typ := Node (Dep_Elmt);
14371 if Scope (Dep_Typ) = Pack_Id
14372 and then Present (Full_View (Dep_Typ))
14373 then
14374 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14375 Exchange_Declarations (Dep_Typ);
14376 end if;
14378 Next_Elmt (Dep_Elmt);
14379 end loop;
14380 end if;
14382 Exchange_Declarations (Node (M));
14383 Next_Elmt (M);
14384 end loop;
14386 if No (Pack_Id) then
14387 return;
14388 end if;
14390 -- Make the generic formal parameters private, and make the formal types
14391 -- into subtypes of the actuals again.
14393 E := First_Entity (Pack_Id);
14394 while Present (E) loop
14395 Set_Is_Hidden (E, True);
14397 if Is_Type (E)
14398 and then Nkind (Parent (E)) = N_Subtype_Declaration
14399 then
14400 -- If the actual for E is itself a generic actual type from
14401 -- an enclosing instance, E is still a generic actual type
14402 -- outside of the current instance. This matter when resolving
14403 -- an overloaded call that may be ambiguous in the enclosing
14404 -- instance, when two of its actuals coincide.
14406 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14407 and then Is_Generic_Actual_Type
14408 (Entity (Subtype_Indication (Parent (E))))
14409 then
14410 null;
14411 else
14412 Set_Is_Generic_Actual_Type (E, False);
14413 end if;
14415 -- An unusual case of aliasing: the actual may also be directly
14416 -- visible in the generic, and be private there, while it is fully
14417 -- visible in the context of the instance. The internal subtype
14418 -- is private in the instance but has full visibility like its
14419 -- parent in the enclosing scope. This enforces the invariant that
14420 -- the privacy status of all private dependents of a type coincide
14421 -- with that of the parent type. This can only happen when a
14422 -- generic child unit is instantiated within a sibling.
14424 if Is_Private_Type (E)
14425 and then not Is_Private_Type (Etype (E))
14426 then
14427 Exchange_Declarations (E);
14428 end if;
14430 elsif Ekind (E) = E_Package then
14432 -- The end of the renaming list is the renaming of the generic
14433 -- package itself. If the instance is a subprogram, all entities
14434 -- in the corresponding package are renamings. If this entity is
14435 -- a formal package, make its own formals private as well. The
14436 -- actual in this case is itself the renaming of an instantiation.
14437 -- If the entity is not a package renaming, it is the entity
14438 -- created to validate formal package actuals: ignore it.
14440 -- If the actual is itself a formal package for the enclosing
14441 -- generic, or the actual for such a formal package, it remains
14442 -- visible on exit from the instance, and therefore nothing needs
14443 -- to be done either, except to keep it accessible.
14445 if Is_Package and then Renamed_Object (E) = Pack_Id then
14446 exit;
14448 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14449 null;
14451 elsif
14452 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14453 then
14454 Set_Is_Hidden (E, False);
14456 else
14457 declare
14458 Act_P : constant Entity_Id := Renamed_Object (E);
14459 Id : Entity_Id;
14461 begin
14462 Id := First_Entity (Act_P);
14463 while Present (Id)
14464 and then Id /= First_Private_Entity (Act_P)
14465 loop
14466 exit when Ekind (Id) = E_Package
14467 and then Renamed_Object (Id) = Act_P;
14469 Set_Is_Hidden (Id, True);
14470 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14472 if Ekind (Id) = E_Package then
14473 Restore_Nested_Formal (Id);
14474 end if;
14476 Next_Entity (Id);
14477 end loop;
14478 end;
14479 end if;
14480 end if;
14482 Next_Entity (E);
14483 end loop;
14484 end Restore_Private_Views;
14486 --------------
14487 -- Save_Env --
14488 --------------
14490 procedure Save_Env
14491 (Gen_Unit : Entity_Id;
14492 Act_Unit : Entity_Id)
14494 begin
14495 Init_Env;
14496 Set_Instance_Env (Gen_Unit, Act_Unit);
14497 end Save_Env;
14499 ----------------------------
14500 -- Save_Global_References --
14501 ----------------------------
14503 procedure Save_Global_References (Templ : Node_Id) is
14505 -- ??? it is horrible to use global variables in highly recursive code
14507 E : Entity_Id;
14508 -- The entity of the current associated node
14510 Gen_Scope : Entity_Id;
14511 -- The scope of the generic for which references are being saved
14513 N2 : Node_Id;
14514 -- The current associated node
14516 function Is_Global (E : Entity_Id) return Boolean;
14517 -- Check whether entity is defined outside of generic unit. Examine the
14518 -- scope of an entity, and the scope of the scope, etc, until we find
14519 -- either Standard, in which case the entity is global, or the generic
14520 -- unit itself, which indicates that the entity is local. If the entity
14521 -- is the generic unit itself, as in the case of a recursive call, or
14522 -- the enclosing generic unit, if different from the current scope, then
14523 -- it is local as well, because it will be replaced at the point of
14524 -- instantiation. On the other hand, if it is a reference to a child
14525 -- unit of a common ancestor, which appears in an instantiation, it is
14526 -- global because it is used to denote a specific compilation unit at
14527 -- the time the instantiations will be analyzed.
14529 procedure Qualify_Universal_Operands
14530 (Op : Node_Id;
14531 Func_Call : Node_Id);
14532 -- Op denotes a binary or unary operator in generic template Templ. Node
14533 -- Func_Call is the function call alternative of the operator within the
14534 -- the analyzed copy of the template. Change each operand which yields a
14535 -- universal type by wrapping it into a qualified expression
14537 -- Actual_Typ'(Operand)
14539 -- where Actual_Typ is the type of corresponding actual parameter of
14540 -- Operand in Func_Call.
14542 procedure Reset_Entity (N : Node_Id);
14543 -- Save semantic information on global entity so that it is not resolved
14544 -- again at instantiation time.
14546 procedure Save_Entity_Descendants (N : Node_Id);
14547 -- Apply Save_Global_References to the two syntactic descendants of
14548 -- non-terminal nodes that carry an Associated_Node and are processed
14549 -- through Reset_Entity. Once the global entity (if any) has been
14550 -- captured together with its type, only two syntactic descendants need
14551 -- to be traversed to complete the processing of the tree rooted at N.
14552 -- This applies to Selected_Components, Expanded_Names, and to Operator
14553 -- nodes. N can also be a character literal, identifier, or operator
14554 -- symbol node, but the call has no effect in these cases.
14556 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14557 -- Default actuals in nested instances must be handled specially
14558 -- because there is no link to them from the original tree. When an
14559 -- actual subprogram is given by a default, we add an explicit generic
14560 -- association for it in the instantiation node. When we save the
14561 -- global references on the name of the instance, we recover the list
14562 -- of generic associations, and add an explicit one to the original
14563 -- generic tree, through which a global actual can be preserved.
14564 -- Similarly, if a child unit is instantiated within a sibling, in the
14565 -- context of the parent, we must preserve the identifier of the parent
14566 -- so that it can be properly resolved in a subsequent instantiation.
14568 procedure Save_Global_Descendant (D : Union_Id);
14569 -- Apply Save_References recursively to the descendants of node D
14571 procedure Save_References (N : Node_Id);
14572 -- This is the recursive procedure that does the work, once the
14573 -- enclosing generic scope has been established.
14575 ---------------
14576 -- Is_Global --
14577 ---------------
14579 function Is_Global (E : Entity_Id) return Boolean is
14580 Se : Entity_Id;
14582 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14583 -- Determine whether the parent node of a reference to a child unit
14584 -- denotes an instantiation or a formal package, in which case the
14585 -- reference to the child unit is global, even if it appears within
14586 -- the current scope (e.g. when the instance appears within the body
14587 -- of an ancestor).
14589 ----------------------
14590 -- Is_Instance_Node --
14591 ----------------------
14593 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14594 begin
14595 return Nkind (Decl) in N_Generic_Instantiation
14596 or else
14597 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14598 end Is_Instance_Node;
14600 -- Start of processing for Is_Global
14602 begin
14603 if E = Gen_Scope then
14604 return False;
14606 elsif E = Standard_Standard then
14607 return True;
14609 elsif Is_Child_Unit (E)
14610 and then (Is_Instance_Node (Parent (N2))
14611 or else (Nkind (Parent (N2)) = N_Expanded_Name
14612 and then N2 = Selector_Name (Parent (N2))
14613 and then
14614 Is_Instance_Node (Parent (Parent (N2)))))
14615 then
14616 return True;
14618 else
14619 Se := Scope (E);
14620 while Se /= Gen_Scope loop
14621 if Se = Standard_Standard then
14622 return True;
14623 else
14624 Se := Scope (Se);
14625 end if;
14626 end loop;
14628 return False;
14629 end if;
14630 end Is_Global;
14632 --------------------------------
14633 -- Qualify_Universal_Operands --
14634 --------------------------------
14636 procedure Qualify_Universal_Operands
14637 (Op : Node_Id;
14638 Func_Call : Node_Id)
14640 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14641 -- Rewrite operand Opnd as a qualified expression of the form
14643 -- Actual_Typ'(Opnd)
14645 -- where Actual is the corresponding actual parameter of Opnd in
14646 -- function call Func_Call.
14648 function Qualify_Type
14649 (Loc : Source_Ptr;
14650 Typ : Entity_Id) return Node_Id;
14651 -- Qualify type Typ by creating a selected component of the form
14653 -- Scope_Of_Typ.Typ
14655 ---------------------
14656 -- Qualify_Operand --
14657 ---------------------
14659 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14660 Loc : constant Source_Ptr := Sloc (Opnd);
14661 Typ : constant Entity_Id := Etype (Actual);
14662 Mark : Node_Id;
14663 Qual : Node_Id;
14665 begin
14666 -- Qualify the operand when it is of a universal type. Note that
14667 -- the template is unanalyzed and it is not possible to directly
14668 -- query the type. This transformation is not done when the type
14669 -- of the actual is internally generated because the type will be
14670 -- regenerated in the instance.
14672 if Yields_Universal_Type (Opnd)
14673 and then Comes_From_Source (Typ)
14674 and then not Is_Hidden (Typ)
14675 then
14676 -- The type of the actual may be a global reference. Save this
14677 -- information by creating a reference to it.
14679 if Is_Global (Typ) then
14680 Mark := New_Occurrence_Of (Typ, Loc);
14682 -- Otherwise rely on resolution to find the proper type within
14683 -- the instance.
14685 else
14686 Mark := Qualify_Type (Loc, Typ);
14687 end if;
14689 Qual :=
14690 Make_Qualified_Expression (Loc,
14691 Subtype_Mark => Mark,
14692 Expression => Relocate_Node (Opnd));
14694 -- Mark the qualification to distinguish it from other source
14695 -- constructs and signal the instantiation mechanism that this
14696 -- node requires special processing. See Copy_Generic_Node for
14697 -- details.
14699 Set_Is_Qualified_Universal_Literal (Qual);
14701 Rewrite (Opnd, Qual);
14702 end if;
14703 end Qualify_Operand;
14705 ------------------
14706 -- Qualify_Type --
14707 ------------------
14709 function Qualify_Type
14710 (Loc : Source_Ptr;
14711 Typ : Entity_Id) return Node_Id
14713 Scop : constant Entity_Id := Scope (Typ);
14714 Result : Node_Id;
14716 begin
14717 Result := Make_Identifier (Loc, Chars (Typ));
14719 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14720 Result :=
14721 Make_Selected_Component (Loc,
14722 Prefix => Make_Identifier (Loc, Chars (Scop)),
14723 Selector_Name => Result);
14724 end if;
14726 return Result;
14727 end Qualify_Type;
14729 -- Local variables
14731 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14733 -- Start of processing for Qualify_Universal_Operands
14735 begin
14736 if Nkind (Op) in N_Binary_Op then
14737 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14738 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14740 elsif Nkind (Op) in N_Unary_Op then
14741 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14742 end if;
14743 end Qualify_Universal_Operands;
14745 ------------------
14746 -- Reset_Entity --
14747 ------------------
14749 procedure Reset_Entity (N : Node_Id) is
14750 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14751 -- If the type of N2 is global to the generic unit, save the type in
14752 -- the generic node. Just as we perform name capture for explicit
14753 -- references within the generic, we must capture the global types
14754 -- of local entities because they may participate in resolution in
14755 -- the instance.
14757 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14758 -- Find the ultimate ancestor of the current unit. If it is not a
14759 -- generic unit, then the name of the current unit in the prefix of
14760 -- an expanded name must be replaced with its generic homonym to
14761 -- ensure that it will be properly resolved in an instance.
14763 ---------------------
14764 -- Set_Global_Type --
14765 ---------------------
14767 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14768 Typ : constant Entity_Id := Etype (N2);
14770 begin
14771 Set_Etype (N, Typ);
14773 -- If the entity of N is not the associated node, this is a
14774 -- nested generic and it has an associated node as well, whose
14775 -- type is already the full view (see below). Indicate that the
14776 -- original node has a private view.
14778 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14779 Set_Has_Private_View (N);
14780 end if;
14782 -- If not a private type, nothing else to do
14784 if not Is_Private_Type (Typ) then
14785 if Is_Array_Type (Typ)
14786 and then Is_Private_Type (Component_Type (Typ))
14787 then
14788 Set_Has_Private_View (N);
14789 end if;
14791 -- If it is a derivation of a private type in a context where no
14792 -- full view is needed, nothing to do either.
14794 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14795 null;
14797 -- Otherwise mark the type for flipping and use the full view when
14798 -- available.
14800 else
14801 Set_Has_Private_View (N);
14803 if Present (Full_View (Typ)) then
14804 Set_Etype (N2, Full_View (Typ));
14805 end if;
14806 end if;
14808 if Is_Floating_Point_Type (Typ)
14809 and then Has_Dimension_System (Typ)
14810 then
14811 Copy_Dimensions (N2, N);
14812 end if;
14813 end Set_Global_Type;
14815 ------------------
14816 -- Top_Ancestor --
14817 ------------------
14819 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14820 Par : Entity_Id;
14822 begin
14823 Par := E;
14824 while Is_Child_Unit (Par) loop
14825 Par := Scope (Par);
14826 end loop;
14828 return Par;
14829 end Top_Ancestor;
14831 -- Start of processing for Reset_Entity
14833 begin
14834 N2 := Get_Associated_Node (N);
14835 E := Entity (N2);
14837 if Present (E) then
14839 -- If the node is an entry call to an entry in an enclosing task,
14840 -- it is rewritten as a selected component. No global entity to
14841 -- preserve in this case, since the expansion will be redone in
14842 -- the instance.
14844 if not Nkind_In (E, N_Defining_Character_Literal,
14845 N_Defining_Identifier,
14846 N_Defining_Operator_Symbol)
14847 then
14848 Set_Associated_Node (N, Empty);
14849 Set_Etype (N, Empty);
14850 return;
14851 end if;
14853 -- If the entity is an itype created as a subtype of an access
14854 -- type with a null exclusion restore source entity for proper
14855 -- visibility. The itype will be created anew in the instance.
14857 if Is_Itype (E)
14858 and then Ekind (E) = E_Access_Subtype
14859 and then Is_Entity_Name (N)
14860 and then Chars (Etype (E)) = Chars (N)
14861 then
14862 E := Etype (E);
14863 Set_Entity (N2, E);
14864 Set_Etype (N2, E);
14865 end if;
14867 if Is_Global (E) then
14869 -- If the entity is a package renaming that is the prefix of
14870 -- an expanded name, it has been rewritten as the renamed
14871 -- package, which is necessary semantically but complicates
14872 -- ASIS tree traversal, so we recover the original entity to
14873 -- expose the renaming. Take into account that the context may
14874 -- be a nested generic, that the original node may itself have
14875 -- an associated node that had better be an entity, and that
14876 -- the current node is still a selected component.
14878 if Ekind (E) = E_Package
14879 and then Nkind (N) = N_Selected_Component
14880 and then Nkind (Parent (N)) = N_Expanded_Name
14881 and then Present (Original_Node (N2))
14882 and then Is_Entity_Name (Original_Node (N2))
14883 and then Present (Entity (Original_Node (N2)))
14884 then
14885 if Is_Global (Entity (Original_Node (N2))) then
14886 N2 := Original_Node (N2);
14887 Set_Associated_Node (N, N2);
14888 Set_Global_Type (N, N2);
14890 -- Renaming is local, and will be resolved in instance
14892 else
14893 Set_Associated_Node (N, Empty);
14894 Set_Etype (N, Empty);
14895 end if;
14897 else
14898 Set_Global_Type (N, N2);
14899 end if;
14901 elsif Nkind (N) = N_Op_Concat
14902 and then Is_Generic_Type (Etype (N2))
14903 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14904 or else
14905 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14906 and then Is_Intrinsic_Subprogram (E)
14907 then
14908 null;
14910 -- Entity is local. Mark generic node as unresolved. Note that now
14911 -- it does not have an entity.
14913 else
14914 Set_Associated_Node (N, Empty);
14915 Set_Etype (N, Empty);
14916 end if;
14918 if Nkind (Parent (N)) in N_Generic_Instantiation
14919 and then N = Name (Parent (N))
14920 then
14921 Save_Global_Defaults (Parent (N), Parent (N2));
14922 end if;
14924 elsif Nkind (Parent (N)) = N_Selected_Component
14925 and then Nkind (Parent (N2)) = N_Expanded_Name
14926 then
14927 if Is_Global (Entity (Parent (N2))) then
14928 Change_Selected_Component_To_Expanded_Name (Parent (N));
14929 Set_Associated_Node (Parent (N), Parent (N2));
14930 Set_Global_Type (Parent (N), Parent (N2));
14931 Save_Entity_Descendants (N);
14933 -- If this is a reference to the current generic entity, replace
14934 -- by the name of the generic homonym of the current package. This
14935 -- is because in an instantiation Par.P.Q will not resolve to the
14936 -- name of the instance, whose enclosing scope is not necessarily
14937 -- Par. We use the generic homonym rather that the name of the
14938 -- generic itself because it may be hidden by a local declaration.
14940 elsif In_Open_Scopes (Entity (Parent (N2)))
14941 and then not
14942 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14943 then
14944 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14945 Rewrite (Parent (N),
14946 Make_Identifier (Sloc (N),
14947 Chars =>
14948 Chars (Generic_Homonym (Entity (Parent (N2))))));
14949 else
14950 Rewrite (Parent (N),
14951 Make_Identifier (Sloc (N),
14952 Chars => Chars (Selector_Name (Parent (N2)))));
14953 end if;
14954 end if;
14956 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14957 and then Parent (N) = Name (Parent (Parent (N)))
14958 then
14959 Save_Global_Defaults
14960 (Parent (Parent (N)), Parent (Parent (N2)));
14961 end if;
14963 -- A selected component may denote a static constant that has been
14964 -- folded. If the static constant is global to the generic, capture
14965 -- its value. Otherwise the folding will happen in any instantiation.
14967 elsif Nkind (Parent (N)) = N_Selected_Component
14968 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14969 then
14970 if Present (Entity (Original_Node (Parent (N2))))
14971 and then Is_Global (Entity (Original_Node (Parent (N2))))
14972 then
14973 Rewrite (Parent (N), New_Copy (Parent (N2)));
14974 Set_Analyzed (Parent (N), False);
14975 end if;
14977 -- A selected component may be transformed into a parameterless
14978 -- function call. If the called entity is global, rewrite the node
14979 -- appropriately, i.e. as an extended name for the global entity.
14981 elsif Nkind (Parent (N)) = N_Selected_Component
14982 and then Nkind (Parent (N2)) = N_Function_Call
14983 and then N = Selector_Name (Parent (N))
14984 then
14985 if No (Parameter_Associations (Parent (N2))) then
14986 if Is_Global (Entity (Name (Parent (N2)))) then
14987 Change_Selected_Component_To_Expanded_Name (Parent (N));
14988 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14989 Set_Global_Type (Parent (N), Name (Parent (N2)));
14990 Save_Entity_Descendants (N);
14992 else
14993 Set_Is_Prefixed_Call (Parent (N));
14994 Set_Associated_Node (N, Empty);
14995 Set_Etype (N, Empty);
14996 end if;
14998 -- In Ada 2005, X.F may be a call to a primitive operation,
14999 -- rewritten as F (X). This rewriting will be done again in an
15000 -- instance, so keep the original node. Global entities will be
15001 -- captured as for other constructs. Indicate that this must
15002 -- resolve as a call, to prevent accidental overloading in the
15003 -- instance, if both a component and a primitive operation appear
15004 -- as candidates.
15006 else
15007 Set_Is_Prefixed_Call (Parent (N));
15008 end if;
15010 -- Entity is local. Reset in generic unit, so that node is resolved
15011 -- anew at the point of instantiation.
15013 else
15014 Set_Associated_Node (N, Empty);
15015 Set_Etype (N, Empty);
15016 end if;
15017 end Reset_Entity;
15019 -----------------------------
15020 -- Save_Entity_Descendants --
15021 -----------------------------
15023 procedure Save_Entity_Descendants (N : Node_Id) is
15024 begin
15025 case Nkind (N) is
15026 when N_Binary_Op =>
15027 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15028 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15030 when N_Unary_Op =>
15031 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15033 when N_Expanded_Name
15034 | N_Selected_Component
15036 Save_Global_Descendant (Union_Id (Prefix (N)));
15037 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15039 when N_Character_Literal
15040 | N_Identifier
15041 | N_Operator_Symbol
15043 null;
15045 when others =>
15046 raise Program_Error;
15047 end case;
15048 end Save_Entity_Descendants;
15050 --------------------------
15051 -- Save_Global_Defaults --
15052 --------------------------
15054 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15055 Loc : constant Source_Ptr := Sloc (N1);
15056 Assoc2 : constant List_Id := Generic_Associations (N2);
15057 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15058 Assoc1 : List_Id;
15059 Act1 : Node_Id;
15060 Act2 : Node_Id;
15061 Def : Node_Id;
15062 Ndec : Node_Id;
15063 Subp : Entity_Id;
15064 Actual : Entity_Id;
15066 begin
15067 Assoc1 := Generic_Associations (N1);
15069 if Present (Assoc1) then
15070 Act1 := First (Assoc1);
15071 else
15072 Act1 := Empty;
15073 Set_Generic_Associations (N1, New_List);
15074 Assoc1 := Generic_Associations (N1);
15075 end if;
15077 if Present (Assoc2) then
15078 Act2 := First (Assoc2);
15079 else
15080 return;
15081 end if;
15083 while Present (Act1) and then Present (Act2) loop
15084 Next (Act1);
15085 Next (Act2);
15086 end loop;
15088 -- Find the associations added for default subprograms
15090 if Present (Act2) then
15091 while Nkind (Act2) /= N_Generic_Association
15092 or else No (Entity (Selector_Name (Act2)))
15093 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15094 loop
15095 Next (Act2);
15096 end loop;
15098 -- Add a similar association if the default is global. The
15099 -- renaming declaration for the actual has been analyzed, and
15100 -- its alias is the program it renames. Link the actual in the
15101 -- original generic tree with the node in the analyzed tree.
15103 while Present (Act2) loop
15104 Subp := Entity (Selector_Name (Act2));
15105 Def := Explicit_Generic_Actual_Parameter (Act2);
15107 -- Following test is defence against rubbish errors
15109 if No (Alias (Subp)) then
15110 return;
15111 end if;
15113 -- Retrieve the resolved actual from the renaming declaration
15114 -- created for the instantiated formal.
15116 Actual := Entity (Name (Parent (Parent (Subp))));
15117 Set_Entity (Def, Actual);
15118 Set_Etype (Def, Etype (Actual));
15120 if Is_Global (Actual) then
15121 Ndec :=
15122 Make_Generic_Association (Loc,
15123 Selector_Name =>
15124 New_Occurrence_Of (Subp, Loc),
15125 Explicit_Generic_Actual_Parameter =>
15126 New_Occurrence_Of (Actual, Loc));
15128 Set_Associated_Node
15129 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15131 Append (Ndec, Assoc1);
15133 -- If there are other defaults, add a dummy association in case
15134 -- there are other defaulted formals with the same name.
15136 elsif Present (Next (Act2)) then
15137 Ndec :=
15138 Make_Generic_Association (Loc,
15139 Selector_Name =>
15140 New_Occurrence_Of (Subp, Loc),
15141 Explicit_Generic_Actual_Parameter => Empty);
15143 Append (Ndec, Assoc1);
15144 end if;
15146 Next (Act2);
15147 end loop;
15148 end if;
15150 if Nkind (Name (N1)) = N_Identifier
15151 and then Is_Child_Unit (Gen_Id)
15152 and then Is_Global (Gen_Id)
15153 and then Is_Generic_Unit (Scope (Gen_Id))
15154 and then In_Open_Scopes (Scope (Gen_Id))
15155 then
15156 -- This is an instantiation of a child unit within a sibling, so
15157 -- that the generic parent is in scope. An eventual instance must
15158 -- occur within the scope of an instance of the parent. Make name
15159 -- in instance into an expanded name, to preserve the identifier
15160 -- of the parent, so it can be resolved subsequently.
15162 Rewrite (Name (N2),
15163 Make_Expanded_Name (Loc,
15164 Chars => Chars (Gen_Id),
15165 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15166 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15167 Set_Entity (Name (N2), Gen_Id);
15169 Rewrite (Name (N1),
15170 Make_Expanded_Name (Loc,
15171 Chars => Chars (Gen_Id),
15172 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15173 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15175 Set_Associated_Node (Name (N1), Name (N2));
15176 Set_Associated_Node (Prefix (Name (N1)), Empty);
15177 Set_Associated_Node
15178 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15179 Set_Etype (Name (N1), Etype (Gen_Id));
15180 end if;
15181 end Save_Global_Defaults;
15183 ----------------------------
15184 -- Save_Global_Descendant --
15185 ----------------------------
15187 procedure Save_Global_Descendant (D : Union_Id) is
15188 N1 : Node_Id;
15190 begin
15191 if D in Node_Range then
15192 if D = Union_Id (Empty) then
15193 null;
15195 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15196 Save_References (Node_Id (D));
15197 end if;
15199 elsif D in List_Range then
15200 pragma Assert (D /= Union_Id (No_List));
15201 -- Because No_List = Empty, which is in Node_Range above
15203 if Is_Empty_List (List_Id (D)) then
15204 null;
15206 else
15207 N1 := First (List_Id (D));
15208 while Present (N1) loop
15209 Save_References (N1);
15210 Next (N1);
15211 end loop;
15212 end if;
15214 -- Element list or other non-node field, nothing to do
15216 else
15217 null;
15218 end if;
15219 end Save_Global_Descendant;
15221 ---------------------
15222 -- Save_References --
15223 ---------------------
15225 -- This is the recursive procedure that does the work once the enclosing
15226 -- generic scope has been established. We have to treat specially a
15227 -- number of node rewritings that are required by semantic processing
15228 -- and which change the kind of nodes in the generic copy: typically
15229 -- constant-folding, replacing an operator node by a string literal, or
15230 -- a selected component by an expanded name. In each of those cases, the
15231 -- transformation is propagated to the generic unit.
15233 procedure Save_References (N : Node_Id) is
15234 Loc : constant Source_Ptr := Sloc (N);
15236 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15237 -- Determine whether arbitrary node Nod requires delayed capture of
15238 -- global references within its aspect specifications.
15240 procedure Save_References_In_Aggregate (N : Node_Id);
15241 -- Save all global references in [extension] aggregate node N
15243 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15244 -- Save all global references in a character literal or operator
15245 -- symbol denoted by N.
15247 procedure Save_References_In_Descendants (N : Node_Id);
15248 -- Save all global references in all descendants of node N
15250 procedure Save_References_In_Identifier (N : Node_Id);
15251 -- Save all global references in identifier node N
15253 procedure Save_References_In_Operator (N : Node_Id);
15254 -- Save all global references in operator node N
15256 procedure Save_References_In_Pragma (Prag : Node_Id);
15257 -- Save all global references found within the expression of pragma
15258 -- Prag.
15260 ---------------------------
15261 -- Requires_Delayed_Save --
15262 ---------------------------
15264 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15265 begin
15266 -- Generic packages and subprograms require delayed capture of
15267 -- global references within their aspects due to the timing of
15268 -- annotation analysis.
15270 if Nkind_In (Nod, N_Generic_Package_Declaration,
15271 N_Generic_Subprogram_Declaration,
15272 N_Package_Body,
15273 N_Package_Body_Stub,
15274 N_Subprogram_Body,
15275 N_Subprogram_Body_Stub)
15276 then
15277 -- Since the capture of global references is done on the
15278 -- unanalyzed generic template, there is no information around
15279 -- to infer the context. Use the Associated_Entity linkages to
15280 -- peek into the analyzed generic copy and determine what the
15281 -- template corresponds to.
15283 if Nod = Templ then
15284 return
15285 Is_Generic_Declaration_Or_Body
15286 (Unit_Declaration_Node
15287 (Associated_Entity (Defining_Entity (Nod))));
15289 -- Otherwise the generic unit being processed is not the top
15290 -- level template. It is safe to capture of global references
15291 -- within the generic unit because at this point the top level
15292 -- copy is fully analyzed.
15294 else
15295 return False;
15296 end if;
15298 -- Otherwise capture the global references without interference
15300 else
15301 return False;
15302 end if;
15303 end Requires_Delayed_Save;
15305 ----------------------------------
15306 -- Save_References_In_Aggregate --
15307 ----------------------------------
15309 procedure Save_References_In_Aggregate (N : Node_Id) is
15310 Nam : Node_Id;
15311 Qual : Node_Id := Empty;
15312 Typ : Entity_Id := Empty;
15314 use Atree.Unchecked_Access;
15315 -- This code section is part of implementing an untyped tree
15316 -- traversal, so it needs direct access to node fields.
15318 begin
15319 N2 := Get_Associated_Node (N);
15321 if Present (N2) then
15322 Typ := Etype (N2);
15324 -- In an instance within a generic, use the name of the actual
15325 -- and not the original generic parameter. If the actual is
15326 -- global in the current generic it must be preserved for its
15327 -- instantiation.
15329 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15330 and then Present (Generic_Parent_Type (Parent (Typ)))
15331 then
15332 Typ := Base_Type (Typ);
15333 Set_Etype (N2, Typ);
15334 end if;
15335 end if;
15337 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15338 Set_Associated_Node (N, Empty);
15340 -- If the aggregate is an actual in a call, it has been
15341 -- resolved in the current context, to some local type. The
15342 -- enclosing call may have been disambiguated by the aggregate,
15343 -- and this disambiguation might fail at instantiation time
15344 -- because the type to which the aggregate did resolve is not
15345 -- preserved. In order to preserve some of this information,
15346 -- wrap the aggregate in a qualified expression, using the id
15347 -- of its type. For further disambiguation we qualify the type
15348 -- name with its scope (if visible and not hidden by a local
15349 -- homograph) because both id's will have corresponding
15350 -- entities in an instance. This resolves most of the problems
15351 -- with missing type information on aggregates in instances.
15353 if Present (N2)
15354 and then Nkind (N2) = Nkind (N)
15355 and then Nkind (Parent (N2)) in N_Subprogram_Call
15356 and then Present (Typ)
15357 and then Comes_From_Source (Typ)
15358 then
15359 Nam := Make_Identifier (Loc, Chars (Typ));
15361 if Is_Immediately_Visible (Scope (Typ))
15362 and then
15363 (not In_Open_Scopes (Scope (Typ))
15364 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15365 then
15366 Nam :=
15367 Make_Selected_Component (Loc,
15368 Prefix =>
15369 Make_Identifier (Loc, Chars (Scope (Typ))),
15370 Selector_Name => Nam);
15371 end if;
15373 Qual :=
15374 Make_Qualified_Expression (Loc,
15375 Subtype_Mark => Nam,
15376 Expression => Relocate_Node (N));
15377 end if;
15378 end if;
15380 Save_Global_Descendant (Field1 (N));
15381 Save_Global_Descendant (Field2 (N));
15382 Save_Global_Descendant (Field3 (N));
15383 Save_Global_Descendant (Field5 (N));
15385 if Present (Qual) then
15386 Rewrite (N, Qual);
15387 end if;
15388 end Save_References_In_Aggregate;
15390 ----------------------------------------------
15391 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15392 ----------------------------------------------
15394 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15395 begin
15396 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15397 Reset_Entity (N);
15399 elsif Nkind (N) = N_Operator_Symbol
15400 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15401 then
15402 Change_Operator_Symbol_To_String_Literal (N);
15403 end if;
15404 end Save_References_In_Char_Lit_Or_Op_Symbol;
15406 ------------------------------------
15407 -- Save_References_In_Descendants --
15408 ------------------------------------
15410 procedure Save_References_In_Descendants (N : Node_Id) is
15411 use Atree.Unchecked_Access;
15412 -- This code section is part of implementing an untyped tree
15413 -- traversal, so it needs direct access to node fields.
15415 begin
15416 Save_Global_Descendant (Field1 (N));
15417 Save_Global_Descendant (Field2 (N));
15418 Save_Global_Descendant (Field3 (N));
15419 Save_Global_Descendant (Field4 (N));
15420 Save_Global_Descendant (Field5 (N));
15421 end Save_References_In_Descendants;
15423 -----------------------------------
15424 -- Save_References_In_Identifier --
15425 -----------------------------------
15427 procedure Save_References_In_Identifier (N : Node_Id) is
15428 begin
15429 -- The node did not undergo a transformation
15431 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15432 declare
15433 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15434 Orig_N2_Parent : constant Node_Id :=
15435 Original_Node (Parent (Aux_N2));
15436 begin
15437 -- The parent of this identifier is a selected component
15438 -- which denotes a named number that was constant folded.
15439 -- Preserve the original name for ASIS and link the parent
15440 -- with its expanded name. The constant folding will be
15441 -- repeated in the instance.
15443 if Nkind (Parent (N)) = N_Selected_Component
15444 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15445 N_Real_Literal)
15446 and then Is_Entity_Name (Orig_N2_Parent)
15447 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15448 and then Is_Global (Entity (Orig_N2_Parent))
15449 then
15450 N2 := Aux_N2;
15451 Set_Associated_Node
15452 (Parent (N), Original_Node (Parent (N2)));
15454 -- Common case
15456 else
15457 -- If this is a discriminant reference, always save it.
15458 -- It is used in the instance to find the corresponding
15459 -- discriminant positionally rather than by name.
15461 Set_Original_Discriminant
15462 (N, Original_Discriminant (Get_Associated_Node (N)));
15463 end if;
15465 Reset_Entity (N);
15466 end;
15468 -- The analysis of the generic copy transformed the identifier
15469 -- into another construct. Propagate the changes to the template.
15471 else
15472 N2 := Get_Associated_Node (N);
15474 -- The identifier denotes a call to a parameterless function.
15475 -- Mark the node as resolved when the function is external.
15477 if Nkind (N2) = N_Function_Call then
15478 E := Entity (Name (N2));
15480 if Present (E) and then Is_Global (E) then
15481 Set_Etype (N, Etype (N2));
15482 else
15483 Set_Associated_Node (N, Empty);
15484 Set_Etype (N, Empty);
15485 end if;
15487 -- The identifier denotes a named number that was constant
15488 -- folded. Preserve the original name for ASIS and undo the
15489 -- constant folding which will be repeated in the instance.
15491 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15492 and then Is_Entity_Name (Original_Node (N2))
15493 then
15494 Set_Associated_Node (N, Original_Node (N2));
15495 Reset_Entity (N);
15497 -- The identifier resolved to a string literal. Propagate this
15498 -- information to the generic template.
15500 elsif Nkind (N2) = N_String_Literal then
15501 Rewrite (N, New_Copy (N2));
15503 -- The identifier is rewritten as a dereference if it is the
15504 -- prefix of an implicit dereference. Preserve the original
15505 -- tree as the analysis of the instance will expand the node
15506 -- again, but preserve the resolved entity if it is global.
15508 elsif Nkind (N2) = N_Explicit_Dereference then
15509 if Is_Entity_Name (Prefix (N2))
15510 and then Present (Entity (Prefix (N2)))
15511 and then Is_Global (Entity (Prefix (N2)))
15512 then
15513 Set_Associated_Node (N, Prefix (N2));
15515 elsif Nkind (Prefix (N2)) = N_Function_Call
15516 and then Present (Entity (Name (Prefix (N2))))
15517 and then Is_Global (Entity (Name (Prefix (N2))))
15518 then
15519 Rewrite (N,
15520 Make_Explicit_Dereference (Loc,
15521 Prefix =>
15522 Make_Function_Call (Loc,
15523 Name =>
15524 New_Occurrence_Of
15525 (Entity (Name (Prefix (N2))), Loc))));
15527 else
15528 Set_Associated_Node (N, Empty);
15529 Set_Etype (N, Empty);
15530 end if;
15532 -- The subtype mark of a nominally unconstrained object is
15533 -- rewritten as a subtype indication using the bounds of the
15534 -- expression. Recover the original subtype mark.
15536 elsif Nkind (N2) = N_Subtype_Indication
15537 and then Is_Entity_Name (Original_Node (N2))
15538 then
15539 Set_Associated_Node (N, Original_Node (N2));
15540 Reset_Entity (N);
15541 end if;
15542 end if;
15543 end Save_References_In_Identifier;
15545 ---------------------------------
15546 -- Save_References_In_Operator --
15547 ---------------------------------
15549 procedure Save_References_In_Operator (N : Node_Id) is
15550 begin
15551 -- The node did not undergo a transformation
15553 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15554 if Nkind (N) = N_Op_Concat then
15555 Set_Is_Component_Left_Opnd (N,
15556 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15558 Set_Is_Component_Right_Opnd (N,
15559 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15560 end if;
15562 Reset_Entity (N);
15564 -- The analysis of the generic copy transformed the operator into
15565 -- some other construct. Propagate the changes to the template if
15566 -- applicable.
15568 else
15569 N2 := Get_Associated_Node (N);
15571 -- The operator resoved to a function call
15573 if Nkind (N2) = N_Function_Call then
15575 -- Add explicit qualifications in the generic template for
15576 -- all operands of universal type. This aids resolution by
15577 -- preserving the actual type of a literal or an attribute
15578 -- that yields a universal result.
15580 Qualify_Universal_Operands (N, N2);
15582 E := Entity (Name (N2));
15584 if Present (E) and then Is_Global (E) then
15585 Set_Etype (N, Etype (N2));
15586 else
15587 Set_Associated_Node (N, Empty);
15588 Set_Etype (N, Empty);
15589 end if;
15591 -- The operator was folded into a literal
15593 elsif Nkind_In (N2, N_Integer_Literal,
15594 N_Real_Literal,
15595 N_String_Literal)
15596 then
15597 if Present (Original_Node (N2))
15598 and then Nkind (Original_Node (N2)) = Nkind (N)
15599 then
15600 -- Operation was constant-folded. Whenever possible,
15601 -- recover semantic information from unfolded node,
15602 -- for ASIS use.
15604 Set_Associated_Node (N, Original_Node (N2));
15606 if Nkind (N) = N_Op_Concat then
15607 Set_Is_Component_Left_Opnd (N,
15608 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15609 Set_Is_Component_Right_Opnd (N,
15610 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15611 end if;
15613 Reset_Entity (N);
15615 -- Propagate the constant folding back to the template
15617 else
15618 Rewrite (N, New_Copy (N2));
15619 Set_Analyzed (N, False);
15620 end if;
15622 -- The operator was folded into an enumeration literal. Retain
15623 -- the entity to avoid spurious ambiguities if it is overloaded
15624 -- at the point of instantiation or inlining.
15626 elsif Nkind (N2) = N_Identifier
15627 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15628 then
15629 Rewrite (N, New_Copy (N2));
15630 Set_Analyzed (N, False);
15631 end if;
15632 end if;
15634 -- Complete the operands check if node has not been constant
15635 -- folded.
15637 if Nkind (N) in N_Op then
15638 Save_Entity_Descendants (N);
15639 end if;
15640 end Save_References_In_Operator;
15642 -------------------------------
15643 -- Save_References_In_Pragma --
15644 -------------------------------
15646 procedure Save_References_In_Pragma (Prag : Node_Id) is
15647 Context : Node_Id;
15648 Do_Save : Boolean := True;
15650 use Atree.Unchecked_Access;
15651 -- This code section is part of implementing an untyped tree
15652 -- traversal, so it needs direct access to node fields.
15654 begin
15655 -- Do not save global references in pragmas generated from aspects
15656 -- because the pragmas will be regenerated at instantiation time.
15658 if From_Aspect_Specification (Prag) then
15659 Do_Save := False;
15661 -- The capture of global references within contract-related source
15662 -- pragmas associated with generic packages, subprograms or their
15663 -- respective bodies must be delayed due to timing of annotation
15664 -- analysis. Global references are still captured in routine
15665 -- Save_Global_References_In_Contract.
15667 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15668 if Is_Package_Contract_Annotation (Prag) then
15669 Context := Find_Related_Package_Or_Body (Prag);
15670 else
15671 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15672 Context := Find_Related_Declaration_Or_Body (Prag);
15673 end if;
15675 -- The use of Original_Node accounts for the case when the
15676 -- related context is generic template.
15678 if Requires_Delayed_Save (Original_Node (Context)) then
15679 Do_Save := False;
15680 end if;
15681 end if;
15683 -- For all other cases, save all global references within the
15684 -- descendants, but skip the following semantic fields:
15686 -- Field1 - Next_Pragma
15687 -- Field3 - Corresponding_Aspect
15688 -- Field5 - Next_Rep_Item
15690 if Do_Save then
15691 Save_Global_Descendant (Field2 (Prag));
15692 Save_Global_Descendant (Field4 (Prag));
15693 end if;
15694 end Save_References_In_Pragma;
15696 -- Start of processing for Save_References
15698 begin
15699 if N = Empty then
15700 null;
15702 -- Aggregates
15704 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15705 Save_References_In_Aggregate (N);
15707 -- Character literals, operator symbols
15709 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15710 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15712 -- Defining identifiers
15714 elsif Nkind (N) in N_Entity then
15715 null;
15717 -- Identifiers
15719 elsif Nkind (N) = N_Identifier then
15720 Save_References_In_Identifier (N);
15722 -- Operators
15724 elsif Nkind (N) in N_Op then
15725 Save_References_In_Operator (N);
15727 -- Pragmas
15729 elsif Nkind (N) = N_Pragma then
15730 Save_References_In_Pragma (N);
15732 else
15733 Save_References_In_Descendants (N);
15734 end if;
15736 -- Save all global references found within the aspect specifications
15737 -- of the related node.
15739 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15741 -- The capture of global references within aspects associated with
15742 -- generic packages, subprograms or their bodies must be delayed
15743 -- due to timing of annotation analysis. Global references are
15744 -- still captured in routine Save_Global_References_In_Contract.
15746 if Requires_Delayed_Save (N) then
15747 null;
15749 -- Otherwise save all global references within the aspects
15751 else
15752 Save_Global_References_In_Aspects (N);
15753 end if;
15754 end if;
15755 end Save_References;
15757 -- Start of processing for Save_Global_References
15759 begin
15760 Gen_Scope := Current_Scope;
15762 -- If the generic unit is a child unit, references to entities in the
15763 -- parent are treated as local, because they will be resolved anew in
15764 -- the context of the instance of the parent.
15766 while Is_Child_Unit (Gen_Scope)
15767 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15768 loop
15769 Gen_Scope := Scope (Gen_Scope);
15770 end loop;
15772 Save_References (Templ);
15773 end Save_Global_References;
15775 ---------------------------------------
15776 -- Save_Global_References_In_Aspects --
15777 ---------------------------------------
15779 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15780 Asp : Node_Id;
15781 Expr : Node_Id;
15783 begin
15784 Asp := First (Aspect_Specifications (N));
15785 while Present (Asp) loop
15786 Expr := Expression (Asp);
15788 if Present (Expr) then
15789 Save_Global_References (Expr);
15790 end if;
15792 Next (Asp);
15793 end loop;
15794 end Save_Global_References_In_Aspects;
15796 ------------------------------------------
15797 -- Set_Copied_Sloc_For_Inherited_Pragma --
15798 ------------------------------------------
15800 procedure Set_Copied_Sloc_For_Inherited_Pragma
15801 (N : Node_Id;
15802 E : Entity_Id)
15804 begin
15805 Create_Instantiation_Source (N, E,
15806 Inlined_Body => False,
15807 Inherited_Pragma => True,
15808 Factor => S_Adjustment);
15809 end Set_Copied_Sloc_For_Inherited_Pragma;
15811 --------------------------------------
15812 -- Set_Copied_Sloc_For_Inlined_Body --
15813 --------------------------------------
15815 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15816 begin
15817 Create_Instantiation_Source (N, E,
15818 Inlined_Body => True,
15819 Inherited_Pragma => False,
15820 Factor => S_Adjustment);
15821 end Set_Copied_Sloc_For_Inlined_Body;
15823 ---------------------
15824 -- Set_Instance_Of --
15825 ---------------------
15827 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15828 begin
15829 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15830 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15831 Generic_Renamings.Increment_Last;
15832 end Set_Instance_Of;
15834 --------------------
15835 -- Set_Next_Assoc --
15836 --------------------
15838 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15839 begin
15840 Generic_Renamings.Table (E).Next_In_HTable := Next;
15841 end Set_Next_Assoc;
15843 -------------------
15844 -- Start_Generic --
15845 -------------------
15847 procedure Start_Generic is
15848 begin
15849 -- ??? More things could be factored out in this routine.
15850 -- Should probably be done at a later stage.
15852 Generic_Flags.Append (Inside_A_Generic);
15853 Inside_A_Generic := True;
15855 Expander_Mode_Save_And_Set (False);
15856 end Start_Generic;
15858 ----------------------
15859 -- Set_Instance_Env --
15860 ----------------------
15862 -- WARNING: This routine manages SPARK regions
15864 procedure Set_Instance_Env
15865 (Gen_Unit : Entity_Id;
15866 Act_Unit : Entity_Id)
15868 Saved_AE : constant Boolean := Assertions_Enabled;
15869 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15870 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15871 -- Save the SPARK mode-related data because utilizing the configuration
15872 -- values of pragmas and switches will eliminate any previously set
15873 -- SPARK_Mode.
15875 begin
15876 -- Regardless of the current mode, predefined units are analyzed in the
15877 -- most current Ada mode, and earlier version Ada checks do not apply
15878 -- to predefined units. Nothing needs to be done for non-internal units.
15879 -- These are always analyzed in the current mode.
15881 if In_Internal_Unit (Gen_Unit) then
15882 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15884 -- In Ada2012 we may want to enable assertions in an instance of a
15885 -- predefined unit, in which case we need to preserve the current
15886 -- setting for the Assertions_Enabled flag. This will become more
15887 -- critical when pre/postconditions are added to predefined units,
15888 -- as is already the case for some numeric libraries.
15890 if Ada_Version >= Ada_2012 then
15891 Assertions_Enabled := Saved_AE;
15892 end if;
15894 -- Reinstall the SPARK_Mode which was in effect at the point of
15895 -- instantiation.
15897 Install_SPARK_Mode (Saved_SM, Saved_SMP);
15898 end if;
15900 Current_Instantiated_Parent :=
15901 (Gen_Id => Gen_Unit,
15902 Act_Id => Act_Unit,
15903 Next_In_HTable => Assoc_Null);
15904 end Set_Instance_Env;
15906 -----------------
15907 -- Switch_View --
15908 -----------------
15910 procedure Switch_View (T : Entity_Id) is
15911 BT : constant Entity_Id := Base_Type (T);
15912 Priv_Elmt : Elmt_Id := No_Elmt;
15913 Priv_Sub : Entity_Id;
15915 begin
15916 -- T may be private but its base type may have been exchanged through
15917 -- some other occurrence, in which case there is nothing to switch
15918 -- besides T itself. Note that a private dependent subtype of a private
15919 -- type might not have been switched even if the base type has been,
15920 -- because of the last branch of Check_Private_View (see comment there).
15922 if not Is_Private_Type (BT) then
15923 Prepend_Elmt (Full_View (T), Exchanged_Views);
15924 Exchange_Declarations (T);
15925 return;
15926 end if;
15928 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15930 if Present (Full_View (BT)) then
15931 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15932 Exchange_Declarations (BT);
15933 end if;
15935 while Present (Priv_Elmt) loop
15936 Priv_Sub := (Node (Priv_Elmt));
15938 -- We avoid flipping the subtype if the Etype of its full view is
15939 -- private because this would result in a malformed subtype. This
15940 -- occurs when the Etype of the subtype full view is the full view of
15941 -- the base type (and since the base types were just switched, the
15942 -- subtype is pointing to the wrong view). This is currently the case
15943 -- for tagged record types, access types (maybe more?) and needs to
15944 -- be resolved. ???
15946 if Present (Full_View (Priv_Sub))
15947 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15948 then
15949 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15950 Exchange_Declarations (Priv_Sub);
15951 end if;
15953 Next_Elmt (Priv_Elmt);
15954 end loop;
15955 end Switch_View;
15957 -----------------
15958 -- True_Parent --
15959 -----------------
15961 function True_Parent (N : Node_Id) return Node_Id is
15962 begin
15963 if Nkind (Parent (N)) = N_Subunit then
15964 return Parent (Corresponding_Stub (Parent (N)));
15965 else
15966 return Parent (N);
15967 end if;
15968 end True_Parent;
15970 -----------------------------
15971 -- Valid_Default_Attribute --
15972 -----------------------------
15974 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15975 Attr_Id : constant Attribute_Id :=
15976 Get_Attribute_Id (Attribute_Name (Def));
15977 T : constant Entity_Id := Entity (Prefix (Def));
15978 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15979 F : Entity_Id;
15980 Num_F : Nat;
15981 OK : Boolean;
15983 begin
15984 if No (T) or else T = Any_Id then
15985 return;
15986 end if;
15988 Num_F := 0;
15989 F := First_Formal (Nam);
15990 while Present (F) loop
15991 Num_F := Num_F + 1;
15992 Next_Formal (F);
15993 end loop;
15995 case Attr_Id is
15996 when Attribute_Adjacent
15997 | Attribute_Ceiling
15998 | Attribute_Copy_Sign
15999 | Attribute_Floor
16000 | Attribute_Fraction
16001 | Attribute_Machine
16002 | Attribute_Model
16003 | Attribute_Remainder
16004 | Attribute_Rounding
16005 | Attribute_Unbiased_Rounding
16007 OK := Is_Fun
16008 and then Num_F = 1
16009 and then Is_Floating_Point_Type (T);
16011 when Attribute_Image
16012 | Attribute_Pred
16013 | Attribute_Succ
16014 | Attribute_Value
16015 | Attribute_Wide_Image
16016 | Attribute_Wide_Value
16018 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16020 when Attribute_Max
16021 | Attribute_Min
16023 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16025 when Attribute_Input =>
16026 OK := (Is_Fun and then Num_F = 1);
16028 when Attribute_Output
16029 | Attribute_Read
16030 | Attribute_Write
16032 OK := not Is_Fun and then Num_F = 2;
16034 when others =>
16035 OK := False;
16036 end case;
16038 if not OK then
16039 Error_Msg_N
16040 ("attribute reference has wrong profile for subprogram", Def);
16041 end if;
16042 end Valid_Default_Attribute;
16044 end Sem_Ch12;