* config/sparc/driver-sparc.c (cpu_names): Add SPARC-T5 entry.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob2b1e1ba5583bff20a60e4625bf2d1d4c86546a9a
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 Remove_Parent (In_Body : Boolean := False);
843 -- Reverse effect after instantiation of child is complete
845 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
846 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
847 -- set to No_Elist.
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
865 -- parent of node.
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
880 type Assoc_Ptr is new Int;
882 Assoc_Null : constant Assoc_Ptr := -1;
884 type Assoc is record
885 Gen_Id : Entity_Id;
886 Act_Id : Entity_Id;
887 Next_In_HTable : Assoc_Ptr;
888 end record;
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
894 Table_Initial => 10,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
903 -- Hash table for associations
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
915 Element => Assoc,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
919 Next => Next_Assoc,
920 Key => Entity_Id,
921 Get_Key => Get_Gen_Id,
922 Hash => Hash,
923 Equal => "=");
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
964 end record;
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
970 Table_Initial => 32,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
980 -- renamings.
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1027 procedure Abandon_Instantiation (N : Node_Id) is
1028 begin
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1033 --------------------------------
1034 -- Add_Pending_Instantiation --
1035 --------------------------------
1037 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1038 begin
1040 -- Add to the instantiation node and the corresponding unit declaration
1041 -- the current values of global flags to be used when analyzing the
1042 -- instance body.
1044 Pending_Instantiations.Append
1045 ((Inst_Node => Inst,
1046 Act_Decl => Act_Decl,
1047 Expander_Status => Expander_Active,
1048 Current_Sem_Unit => Current_Sem_Unit,
1049 Scope_Suppress => Scope_Suppress,
1050 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1051 Version => Ada_Version,
1052 Version_Pragma => Ada_Version_Pragma,
1053 Warnings => Save_Warnings,
1054 SPARK_Mode => SPARK_Mode,
1055 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1056 end Add_Pending_Instantiation;
1058 ----------------------------------
1059 -- Adjust_Inherited_Pragma_Sloc --
1060 ----------------------------------
1062 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1063 begin
1064 Adjust_Instantiation_Sloc (N, S_Adjustment);
1065 end Adjust_Inherited_Pragma_Sloc;
1067 --------------------------
1068 -- Analyze_Associations --
1069 --------------------------
1071 function Analyze_Associations
1072 (I_Node : Node_Id;
1073 Formals : List_Id;
1074 F_Copy : List_Id) return List_Id
1076 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1077 Assoc_List : constant List_Id := New_List;
1078 Default_Actuals : constant List_Id := New_List;
1079 Gen_Unit : constant Entity_Id :=
1080 Defining_Entity (Parent (F_Copy));
1082 Actuals : List_Id;
1083 Actual : Node_Id;
1084 Analyzed_Formal : Node_Id;
1085 First_Named : Node_Id := Empty;
1086 Formal : Node_Id;
1087 Match : Node_Id;
1088 Named : Node_Id;
1089 Saved_Formal : Node_Id;
1091 Default_Formals : constant List_Id := New_List;
1092 -- If an Others_Choice is present, some of the formals may be defaulted.
1093 -- To simplify the treatment of visibility in an instance, we introduce
1094 -- individual defaults for each such formal. These defaults are
1095 -- appended to the list of associations and replace the Others_Choice.
1097 Found_Assoc : Node_Id;
1098 -- Association for the current formal being match. Empty if there are
1099 -- no remaining actuals, or if there is no named association with the
1100 -- name of the formal.
1102 Is_Named_Assoc : Boolean;
1103 Num_Matched : Nat := 0;
1104 Num_Actuals : Nat := 0;
1106 Others_Present : Boolean := False;
1107 Others_Choice : Node_Id := Empty;
1108 -- In Ada 2005, indicates partial parameterization of a formal
1109 -- package. As usual an other association must be last in the list.
1111 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1112 -- Warn if an actual fixed-point type has user-defined arithmetic
1113 -- operations, but there is no corresponding formal in the generic,
1114 -- in which case the predefined operations will be used. This merits
1115 -- a warning because of the special semantics of fixed point ops.
1117 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1118 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1119 -- cannot have a named association for it. AI05-0025 extends this rule
1120 -- to formals of formal packages by AI05-0025, and it also applies to
1121 -- box-initialized formals.
1123 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1124 -- Determine whether the parameter types and the return type of Subp
1125 -- are fully defined at the point of instantiation.
1127 function Matching_Actual
1128 (F : Entity_Id;
1129 A_F : Entity_Id) return Node_Id;
1130 -- Find actual that corresponds to a given a formal parameter. If the
1131 -- actuals are positional, return the next one, if any. If the actuals
1132 -- are named, scan the parameter associations to find the right one.
1133 -- A_F is the corresponding entity in the analyzed generic, which is
1134 -- placed on the selector name for ASIS use.
1136 -- In Ada 2005, a named association may be given with a box, in which
1137 -- case Matching_Actual sets Found_Assoc to the generic association,
1138 -- but return Empty for the actual itself. In this case the code below
1139 -- creates a corresponding declaration for the formal.
1141 function Partial_Parameterization return Boolean;
1142 -- Ada 2005: if no match is found for a given formal, check if the
1143 -- association for it includes a box, or whether the associations
1144 -- include an Others clause.
1146 procedure Process_Default (F : Entity_Id);
1147 -- Add a copy of the declaration of generic formal F to the list of
1148 -- associations, and add an explicit box association for F if there
1149 -- is none yet, and the default comes from an Others_Choice.
1151 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1152 -- Determine whether Subp renames one of the subprograms defined in the
1153 -- generated package Standard.
1155 procedure Set_Analyzed_Formal;
1156 -- Find the node in the generic copy that corresponds to a given formal.
1157 -- The semantic information on this node is used to perform legality
1158 -- checks on the actuals. Because semantic analysis can introduce some
1159 -- anonymous entities or modify the declaration node itself, the
1160 -- correspondence between the two lists is not one-one. In addition to
1161 -- anonymous types, the presence a formal equality will introduce an
1162 -- implicit declaration for the corresponding inequality.
1164 ----------------------------------------
1165 -- Check_Overloaded_Formal_Subprogram --
1166 ----------------------------------------
1168 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1169 Temp_Formal : Entity_Id;
1171 begin
1172 Temp_Formal := First (Formals);
1173 while Present (Temp_Formal) loop
1174 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1175 and then Temp_Formal /= Formal
1176 and then
1177 Chars (Defining_Unit_Name (Specification (Formal))) =
1178 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1179 then
1180 if Present (Found_Assoc) then
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Found_Assoc);
1185 else
1186 Error_Msg_N
1187 ("named association not allowed for overloaded formal",
1188 Others_Choice);
1189 end if;
1191 Abandon_Instantiation (Instantiation_Node);
1192 end if;
1194 Next (Temp_Formal);
1195 end loop;
1196 end Check_Overloaded_Formal_Subprogram;
1198 -------------------------------
1199 -- Check_Fixed_Point_Actual --
1200 -------------------------------
1202 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1203 Typ : constant Entity_Id := Entity (Actual);
1204 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1205 Elem : Elmt_Id;
1206 Formal : Node_Id;
1207 Op : Entity_Id;
1209 begin
1210 -- Locate primitive operations of the type that are arithmetic
1211 -- operations.
1213 Elem := First_Elmt (Prims);
1214 while Present (Elem) loop
1215 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1217 -- Check whether the generic unit has a formal subprogram of
1218 -- the same name. This does not check types but is good enough
1219 -- to justify a warning.
1221 Formal := First_Non_Pragma (Formals);
1222 Op := Alias (Node (Elem));
1224 while Present (Formal) loop
1225 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1226 and then Chars (Defining_Entity (Formal)) =
1227 Chars (Node (Elem))
1228 then
1229 exit;
1231 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1232 declare
1233 Assoc : Node_Id;
1234 Ent : Entity_Id;
1236 begin
1237 -- Locate corresponding actual, and check whether it
1238 -- includes a fixed-point type.
1240 Assoc := First (Assoc_List);
1241 while Present (Assoc) loop
1242 exit when
1243 Nkind (Assoc) = N_Package_Renaming_Declaration
1244 and then Chars (Defining_Unit_Name (Assoc)) =
1245 Chars (Defining_Identifier (Formal));
1247 Next (Assoc);
1248 end loop;
1250 if Present (Assoc) then
1252 -- If formal package declares a fixed-point type,
1253 -- and the user-defined operator is derived from
1254 -- a generic instance package, the fixed-point type
1255 -- does not use the corresponding predefined op.
1257 Ent := First_Entity (Entity (Name (Assoc)));
1258 while Present (Ent) loop
1259 if Is_Fixed_Point_Type (Ent)
1260 and then Present (Op)
1261 and then Is_Generic_Instance (Scope (Op))
1262 then
1263 return;
1264 end if;
1266 Next_Entity (Ent);
1267 end loop;
1268 end if;
1269 end;
1270 end if;
1272 Next (Formal);
1273 end loop;
1275 if No (Formal) then
1276 Error_Msg_Sloc := Sloc (Node (Elem));
1277 Error_Msg_NE
1278 ("?instance does not use primitive operation&#",
1279 Actual, Node (Elem));
1280 end if;
1281 end if;
1283 Next_Elmt (Elem);
1284 end loop;
1285 end Check_Fixed_Point_Actual;
1287 -------------------------------
1288 -- Has_Fully_Defined_Profile --
1289 -------------------------------
1291 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1292 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1293 -- Determine whethet type Typ is fully defined
1295 ---------------------------
1296 -- Is_Fully_Defined_Type --
1297 ---------------------------
1299 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1300 begin
1301 -- A private type without a full view is not fully defined
1303 if Is_Private_Type (Typ)
1304 and then No (Full_View (Typ))
1305 then
1306 return False;
1308 -- An incomplete type is never fully defined
1310 elsif Is_Incomplete_Type (Typ) then
1311 return False;
1313 -- All other types are fully defined
1315 else
1316 return True;
1317 end if;
1318 end Is_Fully_Defined_Type;
1320 -- Local declarations
1322 Param : Entity_Id;
1324 -- Start of processing for Has_Fully_Defined_Profile
1326 begin
1327 -- Check the parameters
1329 Param := First_Formal (Subp);
1330 while Present (Param) loop
1331 if not Is_Fully_Defined_Type (Etype (Param)) then
1332 return False;
1333 end if;
1335 Next_Formal (Param);
1336 end loop;
1338 -- Check the return type
1340 return Is_Fully_Defined_Type (Etype (Subp));
1341 end Has_Fully_Defined_Profile;
1343 ---------------------
1344 -- Matching_Actual --
1345 ---------------------
1347 function Matching_Actual
1348 (F : Entity_Id;
1349 A_F : Entity_Id) return Node_Id
1351 Prev : Node_Id;
1352 Act : Node_Id;
1354 begin
1355 Is_Named_Assoc := False;
1357 -- End of list of purely positional parameters
1359 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1360 Found_Assoc := Empty;
1361 Act := Empty;
1363 -- Case of positional parameter corresponding to current formal
1365 elsif No (Selector_Name (Actual)) then
1366 Found_Assoc := Actual;
1367 Act := Explicit_Generic_Actual_Parameter (Actual);
1368 Num_Matched := Num_Matched + 1;
1369 Next (Actual);
1371 -- Otherwise scan list of named actuals to find the one with the
1372 -- desired name. All remaining actuals have explicit names.
1374 else
1375 Is_Named_Assoc := True;
1376 Found_Assoc := Empty;
1377 Act := Empty;
1378 Prev := Empty;
1380 while Present (Actual) loop
1381 if Nkind (Actual) = N_Others_Choice then
1382 Found_Assoc := Empty;
1383 Act := Empty;
1385 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1386 Set_Entity (Selector_Name (Actual), A_F);
1387 Set_Etype (Selector_Name (Actual), Etype (A_F));
1388 Generate_Reference (A_F, Selector_Name (Actual));
1390 Found_Assoc := Actual;
1391 Act := Explicit_Generic_Actual_Parameter (Actual);
1392 Num_Matched := Num_Matched + 1;
1393 exit;
1394 end if;
1396 Prev := Actual;
1397 Next (Actual);
1398 end loop;
1400 -- Reset for subsequent searches. In most cases the named
1401 -- associations are in order. If they are not, we reorder them
1402 -- to avoid scanning twice the same actual. This is not just a
1403 -- question of efficiency: there may be multiple defaults with
1404 -- boxes that have the same name. In a nested instantiation we
1405 -- insert actuals for those defaults, and cannot rely on their
1406 -- names to disambiguate them.
1408 if Actual = First_Named then
1409 Next (First_Named);
1411 elsif Present (Actual) then
1412 Insert_Before (First_Named, Remove_Next (Prev));
1413 end if;
1415 Actual := First_Named;
1416 end if;
1418 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1419 Set_Used_As_Generic_Actual (Entity (Act));
1420 end if;
1422 return Act;
1423 end Matching_Actual;
1425 ------------------------------
1426 -- Partial_Parameterization --
1427 ------------------------------
1429 function Partial_Parameterization return Boolean is
1430 begin
1431 return Others_Present
1432 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1433 end Partial_Parameterization;
1435 ---------------------
1436 -- Process_Default --
1437 ---------------------
1439 procedure Process_Default (F : Entity_Id) is
1440 Loc : constant Source_Ptr := Sloc (I_Node);
1441 F_Id : constant Entity_Id := Defining_Entity (F);
1442 Decl : Node_Id;
1443 Default : Node_Id;
1444 Id : Entity_Id;
1446 begin
1447 -- Append copy of formal declaration to associations, and create new
1448 -- defining identifier for it.
1450 Decl := New_Copy_Tree (F);
1451 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1453 if Nkind (F) in N_Formal_Subprogram_Declaration then
1454 Set_Defining_Unit_Name (Specification (Decl), Id);
1456 else
1457 Set_Defining_Identifier (Decl, Id);
1458 end if;
1460 Append (Decl, Assoc_List);
1462 if No (Found_Assoc) then
1463 Default :=
1464 Make_Generic_Association (Loc,
1465 Selector_Name =>
1466 New_Occurrence_Of (Id, Loc),
1467 Explicit_Generic_Actual_Parameter => Empty);
1468 Set_Box_Present (Default);
1469 Append (Default, Default_Formals);
1470 end if;
1471 end Process_Default;
1473 ---------------------------------
1474 -- Renames_Standard_Subprogram --
1475 ---------------------------------
1477 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1478 Id : Entity_Id;
1480 begin
1481 Id := Alias (Subp);
1482 while Present (Id) loop
1483 if Scope (Id) = Standard_Standard then
1484 return True;
1485 end if;
1487 Id := Alias (Id);
1488 end loop;
1490 return False;
1491 end Renames_Standard_Subprogram;
1493 -------------------------
1494 -- Set_Analyzed_Formal --
1495 -------------------------
1497 procedure Set_Analyzed_Formal is
1498 Kind : Node_Kind;
1500 begin
1501 while Present (Analyzed_Formal) loop
1502 Kind := Nkind (Analyzed_Formal);
1504 case Nkind (Formal) is
1505 when N_Formal_Subprogram_Declaration =>
1506 exit when Kind in N_Formal_Subprogram_Declaration
1507 and then
1508 Chars
1509 (Defining_Unit_Name (Specification (Formal))) =
1510 Chars
1511 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1513 when N_Formal_Package_Declaration =>
1514 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1515 N_Generic_Package_Declaration,
1516 N_Package_Declaration);
1518 when N_Use_Package_Clause
1519 | N_Use_Type_Clause
1521 exit;
1523 when others =>
1525 -- Skip freeze nodes, and nodes inserted to replace
1526 -- unrecognized pragmas.
1528 exit when
1529 Kind not in N_Formal_Subprogram_Declaration
1530 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1531 N_Freeze_Entity,
1532 N_Null_Statement,
1533 N_Itype_Reference)
1534 and then Chars (Defining_Identifier (Formal)) =
1535 Chars (Defining_Identifier (Analyzed_Formal));
1536 end case;
1538 Next (Analyzed_Formal);
1539 end loop;
1540 end Set_Analyzed_Formal;
1542 -- Start of processing for Analyze_Associations
1544 begin
1545 Actuals := Generic_Associations (I_Node);
1547 if Present (Actuals) then
1549 -- Check for an Others choice, indicating a partial parameterization
1550 -- for a formal package.
1552 Actual := First (Actuals);
1553 while Present (Actual) loop
1554 if Nkind (Actual) = N_Others_Choice then
1555 Others_Present := True;
1556 Others_Choice := Actual;
1558 if Present (Next (Actual)) then
1559 Error_Msg_N ("others must be last association", Actual);
1560 end if;
1562 -- This subprogram is used both for formal packages and for
1563 -- instantiations. For the latter, associations must all be
1564 -- explicit.
1566 if Nkind (I_Node) /= N_Formal_Package_Declaration
1567 and then Comes_From_Source (I_Node)
1568 then
1569 Error_Msg_N
1570 ("others association not allowed in an instance",
1571 Actual);
1572 end if;
1574 -- In any case, nothing to do after the others association
1576 exit;
1578 elsif Box_Present (Actual)
1579 and then Comes_From_Source (I_Node)
1580 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1581 then
1582 Error_Msg_N
1583 ("box association not allowed in an instance", Actual);
1584 end if;
1586 Next (Actual);
1587 end loop;
1589 -- If named associations are present, save first named association
1590 -- (it may of course be Empty) to facilitate subsequent name search.
1592 First_Named := First (Actuals);
1593 while Present (First_Named)
1594 and then Nkind (First_Named) /= N_Others_Choice
1595 and then No (Selector_Name (First_Named))
1596 loop
1597 Num_Actuals := Num_Actuals + 1;
1598 Next (First_Named);
1599 end loop;
1600 end if;
1602 Named := First_Named;
1603 while Present (Named) loop
1604 if Nkind (Named) /= N_Others_Choice
1605 and then No (Selector_Name (Named))
1606 then
1607 Error_Msg_N ("invalid positional actual after named one", Named);
1608 Abandon_Instantiation (Named);
1609 end if;
1611 -- A named association may lack an actual parameter, if it was
1612 -- introduced for a default subprogram that turns out to be local
1613 -- to the outer instantiation. If it has a box association it must
1614 -- correspond to some formal in the generic.
1616 if Nkind (Named) /= N_Others_Choice
1617 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1618 or else Box_Present (Named))
1619 then
1620 Num_Actuals := Num_Actuals + 1;
1621 end if;
1623 Next (Named);
1624 end loop;
1626 if Present (Formals) then
1627 Formal := First_Non_Pragma (Formals);
1628 Analyzed_Formal := First_Non_Pragma (F_Copy);
1630 if Present (Actuals) then
1631 Actual := First (Actuals);
1633 -- All formals should have default values
1635 else
1636 Actual := Empty;
1637 end if;
1639 while Present (Formal) loop
1640 Set_Analyzed_Formal;
1641 Saved_Formal := Next_Non_Pragma (Formal);
1643 case Nkind (Formal) is
1644 when N_Formal_Object_Declaration =>
1645 Match :=
1646 Matching_Actual
1647 (Defining_Identifier (Formal),
1648 Defining_Identifier (Analyzed_Formal));
1650 if No (Match) and then Partial_Parameterization then
1651 Process_Default (Formal);
1653 else
1654 Append_List
1655 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1656 Assoc_List);
1658 -- For a defaulted in_parameter, create an entry in the
1659 -- the list of defaulted actuals, for GNATProve use. Do
1660 -- not included these defaults for an instance nested
1661 -- within a generic, because the defaults are also used
1662 -- in the analysis of the enclosing generic, and only
1663 -- defaulted subprograms are relevant there.
1665 if No (Match) and then not Inside_A_Generic then
1666 Append_To (Default_Actuals,
1667 Make_Generic_Association (Sloc (I_Node),
1668 Selector_Name =>
1669 New_Occurrence_Of
1670 (Defining_Identifier (Formal), Sloc (I_Node)),
1671 Explicit_Generic_Actual_Parameter =>
1672 New_Copy_Tree (Default_Expression (Formal))));
1673 end if;
1674 end if;
1676 -- If the object is a call to an expression function, this
1677 -- is a freezing point for it.
1679 if Is_Entity_Name (Match)
1680 and then Present (Entity (Match))
1681 and then Nkind
1682 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1683 = N_Expression_Function
1684 then
1685 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1686 end if;
1688 when N_Formal_Type_Declaration =>
1689 Match :=
1690 Matching_Actual
1691 (Defining_Identifier (Formal),
1692 Defining_Identifier (Analyzed_Formal));
1694 if No (Match) then
1695 if Partial_Parameterization then
1696 Process_Default (Formal);
1698 else
1699 Error_Msg_Sloc := Sloc (Gen_Unit);
1700 Error_Msg_NE
1701 ("missing actual&",
1702 Instantiation_Node, Defining_Identifier (Formal));
1703 Error_Msg_NE
1704 ("\in instantiation of & declared#",
1705 Instantiation_Node, Gen_Unit);
1706 Abandon_Instantiation (Instantiation_Node);
1707 end if;
1709 else
1710 Analyze (Match);
1711 Append_List
1712 (Instantiate_Type
1713 (Formal, Match, Analyzed_Formal, Assoc_List),
1714 Assoc_List);
1716 if Is_Fixed_Point_Type (Entity (Match)) then
1717 Check_Fixed_Point_Actual (Match);
1718 end if;
1720 -- An instantiation is a freeze point for the actuals,
1721 -- unless this is a rewritten formal package, or the
1722 -- formal is an Ada 2012 formal incomplete type.
1724 if Nkind (I_Node) = N_Formal_Package_Declaration
1725 or else
1726 (Ada_Version >= Ada_2012
1727 and then
1728 Ekind (Defining_Identifier (Analyzed_Formal)) =
1729 E_Incomplete_Type)
1730 then
1731 null;
1733 else
1734 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1735 end if;
1736 end if;
1738 -- A remote access-to-class-wide type is not a legal actual
1739 -- for a generic formal of an access type (E.2.2(17/2)).
1740 -- In GNAT an exception to this rule is introduced when
1741 -- the formal is marked as remote using implementation
1742 -- defined aspect/pragma Remote_Access_Type. In that case
1743 -- the actual must be remote as well.
1745 -- If the current instantiation is the construction of a
1746 -- local copy for a formal package the actuals may be
1747 -- defaulted, and there is no matching actual to check.
1749 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1750 and then
1751 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1752 N_Access_To_Object_Definition
1753 and then Present (Match)
1754 then
1755 declare
1756 Formal_Ent : constant Entity_Id :=
1757 Defining_Identifier (Analyzed_Formal);
1758 begin
1759 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1760 = Is_Remote_Types (Formal_Ent)
1761 then
1762 -- Remoteness of formal and actual match
1764 null;
1766 elsif Is_Remote_Types (Formal_Ent) then
1768 -- Remote formal, non-remote actual
1770 Error_Msg_NE
1771 ("actual for& must be remote", Match, Formal_Ent);
1773 else
1774 -- Non-remote formal, remote actual
1776 Error_Msg_NE
1777 ("actual for& may not be remote",
1778 Match, Formal_Ent);
1779 end if;
1780 end;
1781 end if;
1783 when N_Formal_Subprogram_Declaration =>
1784 Match :=
1785 Matching_Actual
1786 (Defining_Unit_Name (Specification (Formal)),
1787 Defining_Unit_Name (Specification (Analyzed_Formal)));
1789 -- If the formal subprogram has the same name as another
1790 -- formal subprogram of the generic, then a named
1791 -- association is illegal (12.3(9)). Exclude named
1792 -- associations that are generated for a nested instance.
1794 if Present (Match)
1795 and then Is_Named_Assoc
1796 and then Comes_From_Source (Found_Assoc)
1797 then
1798 Check_Overloaded_Formal_Subprogram (Formal);
1799 end if;
1801 -- If there is no corresponding actual, this may be case
1802 -- of partial parameterization, or else the formal has a
1803 -- default or a box.
1805 if No (Match) and then Partial_Parameterization then
1806 Process_Default (Formal);
1808 if Nkind (I_Node) = N_Formal_Package_Declaration then
1809 Check_Overloaded_Formal_Subprogram (Formal);
1810 end if;
1812 else
1813 Append_To (Assoc_List,
1814 Instantiate_Formal_Subprogram
1815 (Formal, Match, Analyzed_Formal));
1817 -- An instantiation is a freeze point for the actuals,
1818 -- unless this is a rewritten formal package.
1820 if Nkind (I_Node) /= N_Formal_Package_Declaration
1821 and then Nkind (Match) = N_Identifier
1822 and then Is_Subprogram (Entity (Match))
1824 -- The actual subprogram may rename a routine defined
1825 -- in Standard. Avoid freezing such renamings because
1826 -- subprograms coming from Standard cannot be frozen.
1828 and then
1829 not Renames_Standard_Subprogram (Entity (Match))
1831 -- If the actual subprogram comes from a different
1832 -- unit, it is already frozen, either by a body in
1833 -- that unit or by the end of the declarative part
1834 -- of the unit. This check avoids the freezing of
1835 -- subprograms defined in Standard which are used
1836 -- as generic actuals.
1838 and then In_Same_Code_Unit (Entity (Match), I_Node)
1839 and then Has_Fully_Defined_Profile (Entity (Match))
1840 then
1841 -- Mark the subprogram as having a delayed freeze
1842 -- since this may be an out-of-order action.
1844 Set_Has_Delayed_Freeze (Entity (Match));
1845 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1846 end if;
1847 end if;
1849 -- If this is a nested generic, preserve default for later
1850 -- instantiations. We do this as well for GNATProve use,
1851 -- so that the list of generic associations is complete.
1853 if No (Match) and then Box_Present (Formal) then
1854 declare
1855 Subp : constant Entity_Id :=
1856 Defining_Unit_Name
1857 (Specification (Last (Assoc_List)));
1859 begin
1860 Append_To (Default_Actuals,
1861 Make_Generic_Association (Sloc (I_Node),
1862 Selector_Name =>
1863 New_Occurrence_Of (Subp, Sloc (I_Node)),
1864 Explicit_Generic_Actual_Parameter =>
1865 New_Occurrence_Of (Subp, Sloc (I_Node))));
1866 end;
1867 end if;
1869 when N_Formal_Package_Declaration =>
1870 Match :=
1871 Matching_Actual
1872 (Defining_Identifier (Formal),
1873 Defining_Identifier (Original_Node (Analyzed_Formal)));
1875 if No (Match) then
1876 if Partial_Parameterization then
1877 Process_Default (Formal);
1879 else
1880 Error_Msg_Sloc := Sloc (Gen_Unit);
1881 Error_Msg_NE
1882 ("missing actual&",
1883 Instantiation_Node, Defining_Identifier (Formal));
1884 Error_Msg_NE
1885 ("\in instantiation of & declared#",
1886 Instantiation_Node, Gen_Unit);
1888 Abandon_Instantiation (Instantiation_Node);
1889 end if;
1891 else
1892 Analyze (Match);
1893 Append_List
1894 (Instantiate_Formal_Package
1895 (Formal, Match, Analyzed_Formal),
1896 Assoc_List);
1897 end if;
1899 -- For use type and use package appearing in the generic part,
1900 -- we have already copied them, so we can just move them where
1901 -- they belong (we mustn't recopy them since this would mess up
1902 -- the Sloc values).
1904 when N_Use_Package_Clause
1905 | N_Use_Type_Clause
1907 if Nkind (Original_Node (I_Node)) =
1908 N_Formal_Package_Declaration
1909 then
1910 Append (New_Copy_Tree (Formal), Assoc_List);
1911 else
1912 Remove (Formal);
1913 Append (Formal, Assoc_List);
1914 end if;
1916 when others =>
1917 raise Program_Error;
1918 end case;
1920 Formal := Saved_Formal;
1921 Next_Non_Pragma (Analyzed_Formal);
1922 end loop;
1924 if Num_Actuals > Num_Matched then
1925 Error_Msg_Sloc := Sloc (Gen_Unit);
1927 if Present (Selector_Name (Actual)) then
1928 Error_Msg_NE
1929 ("unmatched actual &", Actual, Selector_Name (Actual));
1930 Error_Msg_NE
1931 ("\in instantiation of & declared#", Actual, Gen_Unit);
1932 else
1933 Error_Msg_NE
1934 ("unmatched actual in instantiation of & declared#",
1935 Actual, Gen_Unit);
1936 end if;
1937 end if;
1939 elsif Present (Actuals) then
1940 Error_Msg_N
1941 ("too many actuals in generic instantiation", Instantiation_Node);
1942 end if;
1944 -- An instantiation freezes all generic actuals. The only exceptions
1945 -- to this are incomplete types and subprograms which are not fully
1946 -- defined at the point of instantiation.
1948 declare
1949 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1950 begin
1951 while Present (Elmt) loop
1952 Freeze_Before (I_Node, Node (Elmt));
1953 Next_Elmt (Elmt);
1954 end loop;
1955 end;
1957 -- If there are default subprograms, normalize the tree by adding
1958 -- explicit associations for them. This is required if the instance
1959 -- appears within a generic.
1961 if not Is_Empty_List (Default_Actuals) then
1962 declare
1963 Default : Node_Id;
1965 begin
1966 Default := First (Default_Actuals);
1967 while Present (Default) loop
1968 Mark_Rewrite_Insertion (Default);
1969 Next (Default);
1970 end loop;
1972 if No (Actuals) then
1973 Set_Generic_Associations (I_Node, Default_Actuals);
1974 else
1975 Append_List_To (Actuals, Default_Actuals);
1976 end if;
1977 end;
1978 end if;
1980 -- If this is a formal package, normalize the parameter list by adding
1981 -- explicit box associations for the formals that are covered by an
1982 -- Others_Choice.
1984 if not Is_Empty_List (Default_Formals) then
1985 Append_List (Default_Formals, Formals);
1986 end if;
1988 return Assoc_List;
1989 end Analyze_Associations;
1991 -------------------------------
1992 -- Analyze_Formal_Array_Type --
1993 -------------------------------
1995 procedure Analyze_Formal_Array_Type
1996 (T : in out Entity_Id;
1997 Def : Node_Id)
1999 DSS : Node_Id;
2001 begin
2002 -- Treated like a non-generic array declaration, with additional
2003 -- semantic checks.
2005 Enter_Name (T);
2007 if Nkind (Def) = N_Constrained_Array_Definition then
2008 DSS := First (Discrete_Subtype_Definitions (Def));
2009 while Present (DSS) loop
2010 if Nkind_In (DSS, N_Subtype_Indication,
2011 N_Range,
2012 N_Attribute_Reference)
2013 then
2014 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2015 end if;
2017 Next (DSS);
2018 end loop;
2019 end if;
2021 Array_Type_Declaration (T, Def);
2022 Set_Is_Generic_Type (Base_Type (T));
2024 if Ekind (Component_Type (T)) = E_Incomplete_Type
2025 and then No (Full_View (Component_Type (T)))
2026 then
2027 Error_Msg_N ("premature usage of incomplete type", Def);
2029 -- Check that range constraint is not allowed on the component type
2030 -- of a generic formal array type (AARM 12.5.3(3))
2032 elsif Is_Internal (Component_Type (T))
2033 and then Present (Subtype_Indication (Component_Definition (Def)))
2034 and then Nkind (Original_Node
2035 (Subtype_Indication (Component_Definition (Def)))) =
2036 N_Subtype_Indication
2037 then
2038 Error_Msg_N
2039 ("in a formal, a subtype indication can only be "
2040 & "a subtype mark (RM 12.5.3(3))",
2041 Subtype_Indication (Component_Definition (Def)));
2042 end if;
2044 end Analyze_Formal_Array_Type;
2046 ---------------------------------------------
2047 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2048 ---------------------------------------------
2050 -- As for other generic types, we create a valid type representation with
2051 -- legal but arbitrary attributes, whose values are never considered
2052 -- static. For all scalar types we introduce an anonymous base type, with
2053 -- the same attributes. We choose the corresponding integer type to be
2054 -- Standard_Integer.
2055 -- Here and in other similar routines, the Sloc of the generated internal
2056 -- type must be the same as the sloc of the defining identifier of the
2057 -- formal type declaration, to provide proper source navigation.
2059 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2060 (T : Entity_Id;
2061 Def : Node_Id)
2063 Loc : constant Source_Ptr := Sloc (Def);
2065 Base : constant Entity_Id :=
2066 New_Internal_Entity
2067 (E_Decimal_Fixed_Point_Type,
2068 Current_Scope,
2069 Sloc (Defining_Identifier (Parent (Def))), 'G');
2071 Int_Base : constant Entity_Id := Standard_Integer;
2072 Delta_Val : constant Ureal := Ureal_1;
2073 Digs_Val : constant Uint := Uint_6;
2075 function Make_Dummy_Bound return Node_Id;
2076 -- Return a properly typed universal real literal to use as a bound
2078 ----------------------
2079 -- Make_Dummy_Bound --
2080 ----------------------
2082 function Make_Dummy_Bound return Node_Id is
2083 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2084 begin
2085 Set_Etype (Bound, Universal_Real);
2086 return Bound;
2087 end Make_Dummy_Bound;
2089 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2091 begin
2092 Enter_Name (T);
2094 Set_Etype (Base, Base);
2095 Set_Size_Info (Base, Int_Base);
2096 Set_RM_Size (Base, RM_Size (Int_Base));
2097 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2098 Set_Digits_Value (Base, Digs_Val);
2099 Set_Delta_Value (Base, Delta_Val);
2100 Set_Small_Value (Base, Delta_Val);
2101 Set_Scalar_Range (Base,
2102 Make_Range (Loc,
2103 Low_Bound => Make_Dummy_Bound,
2104 High_Bound => Make_Dummy_Bound));
2106 Set_Is_Generic_Type (Base);
2107 Set_Parent (Base, Parent (Def));
2109 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2110 Set_Etype (T, Base);
2111 Set_Size_Info (T, Int_Base);
2112 Set_RM_Size (T, RM_Size (Int_Base));
2113 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2114 Set_Digits_Value (T, Digs_Val);
2115 Set_Delta_Value (T, Delta_Val);
2116 Set_Small_Value (T, Delta_Val);
2117 Set_Scalar_Range (T, Scalar_Range (Base));
2118 Set_Is_Constrained (T);
2120 Check_Restriction (No_Fixed_Point, Def);
2121 end Analyze_Formal_Decimal_Fixed_Point_Type;
2123 -------------------------------------------
2124 -- Analyze_Formal_Derived_Interface_Type --
2125 -------------------------------------------
2127 procedure Analyze_Formal_Derived_Interface_Type
2128 (N : Node_Id;
2129 T : Entity_Id;
2130 Def : Node_Id)
2132 Loc : constant Source_Ptr := Sloc (Def);
2134 begin
2135 -- Rewrite as a type declaration of a derived type. This ensures that
2136 -- the interface list and primitive operations are properly captured.
2138 Rewrite (N,
2139 Make_Full_Type_Declaration (Loc,
2140 Defining_Identifier => T,
2141 Type_Definition => Def));
2142 Analyze (N);
2143 Set_Is_Generic_Type (T);
2144 end Analyze_Formal_Derived_Interface_Type;
2146 ---------------------------------
2147 -- Analyze_Formal_Derived_Type --
2148 ---------------------------------
2150 procedure Analyze_Formal_Derived_Type
2151 (N : Node_Id;
2152 T : Entity_Id;
2153 Def : Node_Id)
2155 Loc : constant Source_Ptr := Sloc (Def);
2156 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2157 New_N : Node_Id;
2159 begin
2160 Set_Is_Generic_Type (T);
2162 if Private_Present (Def) then
2163 New_N :=
2164 Make_Private_Extension_Declaration (Loc,
2165 Defining_Identifier => T,
2166 Discriminant_Specifications => Discriminant_Specifications (N),
2167 Unknown_Discriminants_Present => Unk_Disc,
2168 Subtype_Indication => Subtype_Mark (Def),
2169 Interface_List => Interface_List (Def));
2171 Set_Abstract_Present (New_N, Abstract_Present (Def));
2172 Set_Limited_Present (New_N, Limited_Present (Def));
2173 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2175 else
2176 New_N :=
2177 Make_Full_Type_Declaration (Loc,
2178 Defining_Identifier => T,
2179 Discriminant_Specifications =>
2180 Discriminant_Specifications (Parent (T)),
2181 Type_Definition =>
2182 Make_Derived_Type_Definition (Loc,
2183 Subtype_Indication => Subtype_Mark (Def)));
2185 Set_Abstract_Present
2186 (Type_Definition (New_N), Abstract_Present (Def));
2187 Set_Limited_Present
2188 (Type_Definition (New_N), Limited_Present (Def));
2189 end if;
2191 Rewrite (N, New_N);
2192 Analyze (N);
2194 if Unk_Disc then
2195 if not Is_Composite_Type (T) then
2196 Error_Msg_N
2197 ("unknown discriminants not allowed for elementary types", N);
2198 else
2199 Set_Has_Unknown_Discriminants (T);
2200 Set_Is_Constrained (T, False);
2201 end if;
2202 end if;
2204 -- If the parent type has a known size, so does the formal, which makes
2205 -- legal representation clauses that involve the formal.
2207 Set_Size_Known_At_Compile_Time
2208 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2209 end Analyze_Formal_Derived_Type;
2211 ----------------------------------
2212 -- Analyze_Formal_Discrete_Type --
2213 ----------------------------------
2215 -- The operations defined for a discrete types are those of an enumeration
2216 -- type. The size is set to an arbitrary value, for use in analyzing the
2217 -- generic unit.
2219 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2220 Loc : constant Source_Ptr := Sloc (Def);
2221 Lo : Node_Id;
2222 Hi : Node_Id;
2224 Base : constant Entity_Id :=
2225 New_Internal_Entity
2226 (E_Floating_Point_Type, Current_Scope,
2227 Sloc (Defining_Identifier (Parent (Def))), 'G');
2229 begin
2230 Enter_Name (T);
2231 Set_Ekind (T, E_Enumeration_Subtype);
2232 Set_Etype (T, Base);
2233 Init_Size (T, 8);
2234 Init_Alignment (T);
2235 Set_Is_Generic_Type (T);
2236 Set_Is_Constrained (T);
2238 -- For semantic analysis, the bounds of the type must be set to some
2239 -- non-static value. The simplest is to create attribute nodes for those
2240 -- bounds, that refer to the type itself. These bounds are never
2241 -- analyzed but serve as place-holders.
2243 Lo :=
2244 Make_Attribute_Reference (Loc,
2245 Attribute_Name => Name_First,
2246 Prefix => New_Occurrence_Of (T, Loc));
2247 Set_Etype (Lo, T);
2249 Hi :=
2250 Make_Attribute_Reference (Loc,
2251 Attribute_Name => Name_Last,
2252 Prefix => New_Occurrence_Of (T, Loc));
2253 Set_Etype (Hi, T);
2255 Set_Scalar_Range (T,
2256 Make_Range (Loc,
2257 Low_Bound => Lo,
2258 High_Bound => Hi));
2260 Set_Ekind (Base, E_Enumeration_Type);
2261 Set_Etype (Base, Base);
2262 Init_Size (Base, 8);
2263 Init_Alignment (Base);
2264 Set_Is_Generic_Type (Base);
2265 Set_Scalar_Range (Base, Scalar_Range (T));
2266 Set_Parent (Base, Parent (Def));
2267 end Analyze_Formal_Discrete_Type;
2269 ----------------------------------
2270 -- Analyze_Formal_Floating_Type --
2271 ---------------------------------
2273 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2274 Base : constant Entity_Id :=
2275 New_Internal_Entity
2276 (E_Floating_Point_Type, Current_Scope,
2277 Sloc (Defining_Identifier (Parent (Def))), 'G');
2279 begin
2280 -- The various semantic attributes are taken from the predefined type
2281 -- Float, just so that all of them are initialized. Their values are
2282 -- never used because no constant folding or expansion takes place in
2283 -- the generic itself.
2285 Enter_Name (T);
2286 Set_Ekind (T, E_Floating_Point_Subtype);
2287 Set_Etype (T, Base);
2288 Set_Size_Info (T, (Standard_Float));
2289 Set_RM_Size (T, RM_Size (Standard_Float));
2290 Set_Digits_Value (T, Digits_Value (Standard_Float));
2291 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2292 Set_Is_Constrained (T);
2294 Set_Is_Generic_Type (Base);
2295 Set_Etype (Base, Base);
2296 Set_Size_Info (Base, (Standard_Float));
2297 Set_RM_Size (Base, RM_Size (Standard_Float));
2298 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2299 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2300 Set_Parent (Base, Parent (Def));
2302 Check_Restriction (No_Floating_Point, Def);
2303 end Analyze_Formal_Floating_Type;
2305 -----------------------------------
2306 -- Analyze_Formal_Interface_Type;--
2307 -----------------------------------
2309 procedure Analyze_Formal_Interface_Type
2310 (N : Node_Id;
2311 T : Entity_Id;
2312 Def : Node_Id)
2314 Loc : constant Source_Ptr := Sloc (N);
2315 New_N : Node_Id;
2317 begin
2318 New_N :=
2319 Make_Full_Type_Declaration (Loc,
2320 Defining_Identifier => T,
2321 Type_Definition => Def);
2323 Rewrite (N, New_N);
2324 Analyze (N);
2325 Set_Is_Generic_Type (T);
2326 end Analyze_Formal_Interface_Type;
2328 ---------------------------------
2329 -- Analyze_Formal_Modular_Type --
2330 ---------------------------------
2332 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2333 begin
2334 -- Apart from their entity kind, generic modular types are treated like
2335 -- signed integer types, and have the same attributes.
2337 Analyze_Formal_Signed_Integer_Type (T, Def);
2338 Set_Ekind (T, E_Modular_Integer_Subtype);
2339 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2341 end Analyze_Formal_Modular_Type;
2343 ---------------------------------------
2344 -- Analyze_Formal_Object_Declaration --
2345 ---------------------------------------
2347 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2348 E : constant Node_Id := Default_Expression (N);
2349 Id : constant Node_Id := Defining_Identifier (N);
2350 K : Entity_Kind;
2351 T : Node_Id;
2353 begin
2354 Enter_Name (Id);
2356 -- Determine the mode of the formal object
2358 if Out_Present (N) then
2359 K := E_Generic_In_Out_Parameter;
2361 if not In_Present (N) then
2362 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2363 end if;
2365 else
2366 K := E_Generic_In_Parameter;
2367 end if;
2369 if Present (Subtype_Mark (N)) then
2370 Find_Type (Subtype_Mark (N));
2371 T := Entity (Subtype_Mark (N));
2373 -- Verify that there is no redundant null exclusion
2375 if Null_Exclusion_Present (N) then
2376 if not Is_Access_Type (T) then
2377 Error_Msg_N
2378 ("null exclusion can only apply to an access type", N);
2380 elsif Can_Never_Be_Null (T) then
2381 Error_Msg_NE
2382 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2383 end if;
2384 end if;
2386 -- Ada 2005 (AI-423): Formal object with an access definition
2388 else
2389 Check_Access_Definition (N);
2390 T := Access_Definition
2391 (Related_Nod => N,
2392 N => Access_Definition (N));
2393 end if;
2395 if Ekind (T) = E_Incomplete_Type then
2396 declare
2397 Error_Node : Node_Id;
2399 begin
2400 if Present (Subtype_Mark (N)) then
2401 Error_Node := Subtype_Mark (N);
2402 else
2403 Check_Access_Definition (N);
2404 Error_Node := Access_Definition (N);
2405 end if;
2407 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2408 end;
2409 end if;
2411 if K = E_Generic_In_Parameter then
2413 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2415 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2416 Error_Msg_N
2417 ("generic formal of mode IN must not be of limited type", N);
2418 Explain_Limited_Type (T, N);
2419 end if;
2421 if Is_Abstract_Type (T) then
2422 Error_Msg_N
2423 ("generic formal of mode IN must not be of abstract type", N);
2424 end if;
2426 if Present (E) then
2427 Preanalyze_Spec_Expression (E, T);
2429 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2430 Error_Msg_N
2431 ("initialization not allowed for limited types", E);
2432 Explain_Limited_Type (T, E);
2433 end if;
2434 end if;
2436 Set_Ekind (Id, K);
2437 Set_Etype (Id, T);
2439 -- Case of generic IN OUT parameter
2441 else
2442 -- If the formal has an unconstrained type, construct its actual
2443 -- subtype, as is done for subprogram formals. In this fashion, all
2444 -- its uses can refer to specific bounds.
2446 Set_Ekind (Id, K);
2447 Set_Etype (Id, T);
2449 if (Is_Array_Type (T) and then not Is_Constrained (T))
2450 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2451 then
2452 declare
2453 Non_Freezing_Ref : constant Node_Id :=
2454 New_Occurrence_Of (Id, Sloc (Id));
2455 Decl : Node_Id;
2457 begin
2458 -- Make sure the actual subtype doesn't generate bogus freezing
2460 Set_Must_Not_Freeze (Non_Freezing_Ref);
2461 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2462 Insert_Before_And_Analyze (N, Decl);
2463 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2464 end;
2465 else
2466 Set_Actual_Subtype (Id, T);
2467 end if;
2469 if Present (E) then
2470 Error_Msg_N
2471 ("initialization not allowed for `IN OUT` formals", N);
2472 end if;
2473 end if;
2475 if Has_Aspects (N) then
2476 Analyze_Aspect_Specifications (N, Id);
2477 end if;
2478 end Analyze_Formal_Object_Declaration;
2480 ----------------------------------------------
2481 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2482 ----------------------------------------------
2484 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2485 (T : Entity_Id;
2486 Def : Node_Id)
2488 Loc : constant Source_Ptr := Sloc (Def);
2489 Base : constant Entity_Id :=
2490 New_Internal_Entity
2491 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2492 Sloc (Defining_Identifier (Parent (Def))), 'G');
2494 begin
2495 -- The semantic attributes are set for completeness only, their values
2496 -- will never be used, since all properties of the type are non-static.
2498 Enter_Name (T);
2499 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2500 Set_Etype (T, Base);
2501 Set_Size_Info (T, Standard_Integer);
2502 Set_RM_Size (T, RM_Size (Standard_Integer));
2503 Set_Small_Value (T, Ureal_1);
2504 Set_Delta_Value (T, Ureal_1);
2505 Set_Scalar_Range (T,
2506 Make_Range (Loc,
2507 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2508 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2509 Set_Is_Constrained (T);
2511 Set_Is_Generic_Type (Base);
2512 Set_Etype (Base, Base);
2513 Set_Size_Info (Base, Standard_Integer);
2514 Set_RM_Size (Base, RM_Size (Standard_Integer));
2515 Set_Small_Value (Base, Ureal_1);
2516 Set_Delta_Value (Base, Ureal_1);
2517 Set_Scalar_Range (Base, Scalar_Range (T));
2518 Set_Parent (Base, Parent (Def));
2520 Check_Restriction (No_Fixed_Point, Def);
2521 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2523 ----------------------------------------
2524 -- Analyze_Formal_Package_Declaration --
2525 ----------------------------------------
2527 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2528 Gen_Id : constant Node_Id := Name (N);
2529 Loc : constant Source_Ptr := Sloc (N);
2530 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2531 Formal : Entity_Id;
2532 Gen_Decl : Node_Id;
2533 Gen_Unit : Entity_Id;
2534 Renaming : Node_Id;
2536 Vis_Prims_List : Elist_Id := No_Elist;
2537 -- List of primitives made temporarily visible in the instantiation
2538 -- to match the visibility of the formal type.
2540 function Build_Local_Package return Node_Id;
2541 -- The formal package is rewritten so that its parameters are replaced
2542 -- with corresponding declarations. For parameters with bona fide
2543 -- associations these declarations are created by Analyze_Associations
2544 -- as for a regular instantiation. For boxed parameters, we preserve
2545 -- the formal declarations and analyze them, in order to introduce
2546 -- entities of the right kind in the environment of the formal.
2548 -------------------------
2549 -- Build_Local_Package --
2550 -------------------------
2552 function Build_Local_Package return Node_Id is
2553 Decls : List_Id;
2554 Pack_Decl : Node_Id;
2556 begin
2557 -- Within the formal, the name of the generic package is a renaming
2558 -- of the formal (as for a regular instantiation).
2560 Pack_Decl :=
2561 Make_Package_Declaration (Loc,
2562 Specification =>
2563 Copy_Generic_Node
2564 (Specification (Original_Node (Gen_Decl)),
2565 Empty, Instantiating => True));
2567 Renaming :=
2568 Make_Package_Renaming_Declaration (Loc,
2569 Defining_Unit_Name =>
2570 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2571 Name => New_Occurrence_Of (Formal, Loc));
2573 if Nkind (Gen_Id) = N_Identifier
2574 and then Chars (Gen_Id) = Chars (Pack_Id)
2575 then
2576 Error_Msg_NE
2577 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2578 end if;
2580 -- If the formal is declared with a box, or with an others choice,
2581 -- create corresponding declarations for all entities in the formal
2582 -- part, so that names with the proper types are available in the
2583 -- specification of the formal package.
2585 -- On the other hand, if there are no associations, then all the
2586 -- formals must have defaults, and this will be checked by the
2587 -- call to Analyze_Associations.
2589 if Box_Present (N)
2590 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2591 then
2592 declare
2593 Formal_Decl : Node_Id;
2595 begin
2596 -- TBA : for a formal package, need to recurse ???
2598 Decls := New_List;
2599 Formal_Decl :=
2600 First
2601 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2602 while Present (Formal_Decl) loop
2603 Append_To
2604 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2605 Next (Formal_Decl);
2606 end loop;
2607 end;
2609 -- If generic associations are present, use Analyze_Associations to
2610 -- create the proper renaming declarations.
2612 else
2613 declare
2614 Act_Tree : constant Node_Id :=
2615 Copy_Generic_Node
2616 (Original_Node (Gen_Decl), Empty,
2617 Instantiating => True);
2619 begin
2620 Generic_Renamings.Set_Last (0);
2621 Generic_Renamings_HTable.Reset;
2622 Instantiation_Node := N;
2624 Decls :=
2625 Analyze_Associations
2626 (I_Node => Original_Node (N),
2627 Formals => Generic_Formal_Declarations (Act_Tree),
2628 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2630 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2631 end;
2632 end if;
2634 Append (Renaming, To => Decls);
2636 -- Add generated declarations ahead of local declarations in
2637 -- the package.
2639 if No (Visible_Declarations (Specification (Pack_Decl))) then
2640 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2641 else
2642 Insert_List_Before
2643 (First (Visible_Declarations (Specification (Pack_Decl))),
2644 Decls);
2645 end if;
2647 return Pack_Decl;
2648 end Build_Local_Package;
2650 -- Local variables
2652 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2653 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2655 Associations : Boolean := True;
2656 New_N : Node_Id;
2657 Parent_Installed : Boolean := False;
2658 Parent_Instance : Entity_Id;
2659 Renaming_In_Par : Entity_Id;
2661 -- Start of processing for Analyze_Formal_Package_Declaration
2663 begin
2664 Check_Text_IO_Special_Unit (Gen_Id);
2666 Init_Env;
2667 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2668 Gen_Unit := Entity (Gen_Id);
2670 -- Check for a formal package that is a package renaming
2672 if Present (Renamed_Object (Gen_Unit)) then
2674 -- Indicate that unit is used, before replacing it with renamed
2675 -- entity for use below.
2677 if In_Extended_Main_Source_Unit (N) then
2678 Set_Is_Instantiated (Gen_Unit);
2679 Generate_Reference (Gen_Unit, N);
2680 end if;
2682 Gen_Unit := Renamed_Object (Gen_Unit);
2683 end if;
2685 if Ekind (Gen_Unit) /= E_Generic_Package then
2686 Error_Msg_N ("expect generic package name", Gen_Id);
2687 Restore_Env;
2688 goto Leave;
2690 elsif Gen_Unit = Current_Scope then
2691 Error_Msg_N
2692 ("generic package cannot be used as a formal package of itself",
2693 Gen_Id);
2694 Restore_Env;
2695 goto Leave;
2697 elsif In_Open_Scopes (Gen_Unit) then
2698 if Is_Compilation_Unit (Gen_Unit)
2699 and then Is_Child_Unit (Current_Scope)
2700 then
2701 -- Special-case the error when the formal is a parent, and
2702 -- continue analysis to minimize cascaded errors.
2704 Error_Msg_N
2705 ("generic parent cannot be used as formal package of a child "
2706 & "unit", Gen_Id);
2708 else
2709 Error_Msg_N
2710 ("generic package cannot be used as a formal package within "
2711 & "itself", Gen_Id);
2712 Restore_Env;
2713 goto Leave;
2714 end if;
2715 end if;
2717 -- Check that name of formal package does not hide name of generic,
2718 -- or its leading prefix. This check must be done separately because
2719 -- the name of the generic has already been analyzed.
2721 declare
2722 Gen_Name : Entity_Id;
2724 begin
2725 Gen_Name := Gen_Id;
2726 while Nkind (Gen_Name) = N_Expanded_Name loop
2727 Gen_Name := Prefix (Gen_Name);
2728 end loop;
2730 if Chars (Gen_Name) = Chars (Pack_Id) then
2731 Error_Msg_NE
2732 ("& is hidden within declaration of formal package",
2733 Gen_Id, Gen_Name);
2734 end if;
2735 end;
2737 if Box_Present (N)
2738 or else No (Generic_Associations (N))
2739 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2740 then
2741 Associations := False;
2742 end if;
2744 -- If there are no generic associations, the generic parameters appear
2745 -- as local entities and are instantiated like them. We copy the generic
2746 -- package declaration as if it were an instantiation, and analyze it
2747 -- like a regular package, except that we treat the formals as
2748 -- additional visible components.
2750 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2752 if In_Extended_Main_Source_Unit (N) then
2753 Set_Is_Instantiated (Gen_Unit);
2754 Generate_Reference (Gen_Unit, N);
2755 end if;
2757 Formal := New_Copy (Pack_Id);
2758 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2760 -- Make local generic without formals. The formals will be replaced with
2761 -- internal declarations.
2763 begin
2764 New_N := Build_Local_Package;
2766 -- If there are errors in the parameter list, Analyze_Associations
2767 -- raises Instantiation_Error. Patch the declaration to prevent further
2768 -- exception propagation.
2770 exception
2771 when Instantiation_Error =>
2772 Enter_Name (Formal);
2773 Set_Ekind (Formal, E_Variable);
2774 Set_Etype (Formal, Any_Type);
2775 Restore_Hidden_Primitives (Vis_Prims_List);
2777 if Parent_Installed then
2778 Remove_Parent;
2779 end if;
2781 goto Leave;
2782 end;
2784 Rewrite (N, New_N);
2785 Set_Defining_Unit_Name (Specification (New_N), Formal);
2786 Set_Generic_Parent (Specification (N), Gen_Unit);
2787 Set_Instance_Env (Gen_Unit, Formal);
2788 Set_Is_Generic_Instance (Formal);
2790 Enter_Name (Formal);
2791 Set_Ekind (Formal, E_Package);
2792 Set_Etype (Formal, Standard_Void_Type);
2793 Set_Inner_Instances (Formal, New_Elmt_List);
2794 Push_Scope (Formal);
2796 -- Manually set the SPARK_Mode from the context because the package
2797 -- declaration is never analyzed.
2799 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2800 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2801 Set_SPARK_Pragma_Inherited (Formal);
2802 Set_SPARK_Aux_Pragma_Inherited (Formal);
2804 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2806 -- Similarly, we have to make the name of the formal visible in the
2807 -- parent instance, to resolve properly fully qualified names that
2808 -- may appear in the generic unit. The parent instance has been
2809 -- placed on the scope stack ahead of the current scope.
2811 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2813 Renaming_In_Par :=
2814 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2815 Set_Ekind (Renaming_In_Par, E_Package);
2816 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2817 Set_Scope (Renaming_In_Par, Parent_Instance);
2818 Set_Parent (Renaming_In_Par, Parent (Formal));
2819 Set_Renamed_Object (Renaming_In_Par, Formal);
2820 Append_Entity (Renaming_In_Par, Parent_Instance);
2821 end if;
2823 -- A formal package declaration behaves as a package instantiation with
2824 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2825 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2826 -- all SPARK_Mode pragmas within the generic_package_name.
2828 if SPARK_Mode /= On then
2829 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2831 -- Mark the formal spec in case the body is instantiated at a later
2832 -- pass. This preserves the original context in effect for the body.
2834 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2835 end if;
2837 Analyze (Specification (N));
2839 -- The formals for which associations are provided are not visible
2840 -- outside of the formal package. The others are still declared by a
2841 -- formal parameter declaration.
2843 -- If there are no associations, the only local entity to hide is the
2844 -- generated package renaming itself.
2846 declare
2847 E : Entity_Id;
2849 begin
2850 E := First_Entity (Formal);
2851 while Present (E) loop
2852 if Associations and then not Is_Generic_Formal (E) then
2853 Set_Is_Hidden (E);
2854 end if;
2856 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2857 Set_Is_Hidden (E);
2858 exit;
2859 end if;
2861 Next_Entity (E);
2862 end loop;
2863 end;
2865 End_Package_Scope (Formal);
2866 Restore_Hidden_Primitives (Vis_Prims_List);
2868 if Parent_Installed then
2869 Remove_Parent;
2870 end if;
2872 Restore_Env;
2874 -- Inside the generic unit, the formal package is a regular package, but
2875 -- no body is needed for it. Note that after instantiation, the defining
2876 -- unit name we need is in the new tree and not in the original (see
2877 -- Package_Instantiation). A generic formal package is an instance, and
2878 -- can be used as an actual for an inner instance.
2880 Set_Has_Completion (Formal, True);
2882 -- Add semantic information to the original defining identifier for ASIS
2883 -- use.
2885 Set_Ekind (Pack_Id, E_Package);
2886 Set_Etype (Pack_Id, Standard_Void_Type);
2887 Set_Scope (Pack_Id, Scope (Formal));
2888 Set_Has_Completion (Pack_Id, True);
2890 <<Leave>>
2891 if Has_Aspects (N) then
2892 Analyze_Aspect_Specifications (N, Pack_Id);
2893 end if;
2895 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
2896 end Analyze_Formal_Package_Declaration;
2898 ---------------------------------
2899 -- Analyze_Formal_Private_Type --
2900 ---------------------------------
2902 procedure Analyze_Formal_Private_Type
2903 (N : Node_Id;
2904 T : Entity_Id;
2905 Def : Node_Id)
2907 begin
2908 New_Private_Type (N, T, Def);
2910 -- Set the size to an arbitrary but legal value
2912 Set_Size_Info (T, Standard_Integer);
2913 Set_RM_Size (T, RM_Size (Standard_Integer));
2914 end Analyze_Formal_Private_Type;
2916 ------------------------------------
2917 -- Analyze_Formal_Incomplete_Type --
2918 ------------------------------------
2920 procedure Analyze_Formal_Incomplete_Type
2921 (T : Entity_Id;
2922 Def : Node_Id)
2924 begin
2925 Enter_Name (T);
2926 Set_Ekind (T, E_Incomplete_Type);
2927 Set_Etype (T, T);
2928 Set_Private_Dependents (T, New_Elmt_List);
2930 if Tagged_Present (Def) then
2931 Set_Is_Tagged_Type (T);
2932 Make_Class_Wide_Type (T);
2933 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2934 end if;
2935 end Analyze_Formal_Incomplete_Type;
2937 ----------------------------------------
2938 -- Analyze_Formal_Signed_Integer_Type --
2939 ----------------------------------------
2941 procedure Analyze_Formal_Signed_Integer_Type
2942 (T : Entity_Id;
2943 Def : Node_Id)
2945 Base : constant Entity_Id :=
2946 New_Internal_Entity
2947 (E_Signed_Integer_Type,
2948 Current_Scope,
2949 Sloc (Defining_Identifier (Parent (Def))), 'G');
2951 begin
2952 Enter_Name (T);
2954 Set_Ekind (T, E_Signed_Integer_Subtype);
2955 Set_Etype (T, Base);
2956 Set_Size_Info (T, Standard_Integer);
2957 Set_RM_Size (T, RM_Size (Standard_Integer));
2958 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2959 Set_Is_Constrained (T);
2961 Set_Is_Generic_Type (Base);
2962 Set_Size_Info (Base, Standard_Integer);
2963 Set_RM_Size (Base, RM_Size (Standard_Integer));
2964 Set_Etype (Base, Base);
2965 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2966 Set_Parent (Base, Parent (Def));
2967 end Analyze_Formal_Signed_Integer_Type;
2969 -------------------------------------------
2970 -- Analyze_Formal_Subprogram_Declaration --
2971 -------------------------------------------
2973 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2974 Spec : constant Node_Id := Specification (N);
2975 Def : constant Node_Id := Default_Name (N);
2976 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2977 Subp : Entity_Id;
2979 begin
2980 if Nam = Error then
2981 return;
2982 end if;
2984 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2985 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2986 goto Leave;
2987 end if;
2989 Analyze_Subprogram_Declaration (N);
2990 Set_Is_Formal_Subprogram (Nam);
2991 Set_Has_Completion (Nam);
2993 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2994 Set_Is_Abstract_Subprogram (Nam);
2996 Set_Is_Dispatching_Operation (Nam);
2998 -- A formal abstract procedure cannot have a null default
2999 -- (RM 12.6(4.1/2)).
3001 if Nkind (Spec) = N_Procedure_Specification
3002 and then Null_Present (Spec)
3003 then
3004 Error_Msg_N
3005 ("a formal abstract subprogram cannot default to null", Spec);
3006 end if;
3008 declare
3009 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3010 begin
3011 if No (Ctrl_Type) then
3012 Error_Msg_N
3013 ("abstract formal subprogram must have a controlling type",
3016 elsif Ada_Version >= Ada_2012
3017 and then Is_Incomplete_Type (Ctrl_Type)
3018 then
3019 Error_Msg_NE
3020 ("controlling type of abstract formal subprogram cannot "
3021 & "be incomplete type", N, Ctrl_Type);
3023 else
3024 Check_Controlling_Formals (Ctrl_Type, Nam);
3025 end if;
3026 end;
3027 end if;
3029 -- Default name is resolved at the point of instantiation
3031 if Box_Present (N) then
3032 null;
3034 -- Else default is bound at the point of generic declaration
3036 elsif Present (Def) then
3037 if Nkind (Def) = N_Operator_Symbol then
3038 Find_Direct_Name (Def);
3040 elsif Nkind (Def) /= N_Attribute_Reference then
3041 Analyze (Def);
3043 else
3044 -- For an attribute reference, analyze the prefix and verify
3045 -- that it has the proper profile for the subprogram.
3047 Analyze (Prefix (Def));
3048 Valid_Default_Attribute (Nam, Def);
3049 goto Leave;
3050 end if;
3052 -- Default name may be overloaded, in which case the interpretation
3053 -- with the correct profile must be selected, as for a renaming.
3054 -- If the definition is an indexed component, it must denote a
3055 -- member of an entry family. If it is a selected component, it
3056 -- can be a protected operation.
3058 if Etype (Def) = Any_Type then
3059 goto Leave;
3061 elsif Nkind (Def) = N_Selected_Component then
3062 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3063 Error_Msg_N ("expect valid subprogram name as default", Def);
3064 end if;
3066 elsif Nkind (Def) = N_Indexed_Component then
3067 if Is_Entity_Name (Prefix (Def)) then
3068 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3069 Error_Msg_N ("expect valid subprogram name as default", Def);
3070 end if;
3072 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3073 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3074 E_Entry_Family
3075 then
3076 Error_Msg_N ("expect valid subprogram name as default", Def);
3077 end if;
3079 else
3080 Error_Msg_N ("expect valid subprogram name as default", Def);
3081 goto Leave;
3082 end if;
3084 elsif Nkind (Def) = N_Character_Literal then
3086 -- Needs some type checks: subprogram should be parameterless???
3088 Resolve (Def, (Etype (Nam)));
3090 elsif not Is_Entity_Name (Def)
3091 or else not Is_Overloadable (Entity (Def))
3092 then
3093 Error_Msg_N ("expect valid subprogram name as default", Def);
3094 goto Leave;
3096 elsif not Is_Overloaded (Def) then
3097 Subp := Entity (Def);
3099 if Subp = Nam then
3100 Error_Msg_N ("premature usage of formal subprogram", Def);
3102 elsif not Entity_Matches_Spec (Subp, Nam) then
3103 Error_Msg_N ("no visible entity matches specification", Def);
3104 end if;
3106 -- More than one interpretation, so disambiguate as for a renaming
3108 else
3109 declare
3110 I : Interp_Index;
3111 I1 : Interp_Index := 0;
3112 It : Interp;
3113 It1 : Interp;
3115 begin
3116 Subp := Any_Id;
3117 Get_First_Interp (Def, I, It);
3118 while Present (It.Nam) loop
3119 if Entity_Matches_Spec (It.Nam, Nam) then
3120 if Subp /= Any_Id then
3121 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3123 if It1 = No_Interp then
3124 Error_Msg_N ("ambiguous default subprogram", Def);
3125 else
3126 Subp := It1.Nam;
3127 end if;
3129 exit;
3131 else
3132 I1 := I;
3133 Subp := It.Nam;
3134 end if;
3135 end if;
3137 Get_Next_Interp (I, It);
3138 end loop;
3139 end;
3141 if Subp /= Any_Id then
3143 -- Subprogram found, generate reference to it
3145 Set_Entity (Def, Subp);
3146 Generate_Reference (Subp, Def);
3148 if Subp = Nam then
3149 Error_Msg_N ("premature usage of formal subprogram", Def);
3151 elsif Ekind (Subp) /= E_Operator then
3152 Check_Mode_Conformant (Subp, Nam);
3153 end if;
3155 else
3156 Error_Msg_N ("no visible subprogram matches specification", N);
3157 end if;
3158 end if;
3159 end if;
3161 <<Leave>>
3162 if Has_Aspects (N) then
3163 Analyze_Aspect_Specifications (N, Nam);
3164 end if;
3166 end Analyze_Formal_Subprogram_Declaration;
3168 -------------------------------------
3169 -- Analyze_Formal_Type_Declaration --
3170 -------------------------------------
3172 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3173 Def : constant Node_Id := Formal_Type_Definition (N);
3174 T : Entity_Id;
3176 begin
3177 T := Defining_Identifier (N);
3179 if Present (Discriminant_Specifications (N))
3180 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3181 then
3182 Error_Msg_N
3183 ("discriminants not allowed for this formal type", T);
3184 end if;
3186 -- Enter the new name, and branch to specific routine
3188 case Nkind (Def) is
3189 when N_Formal_Private_Type_Definition =>
3190 Analyze_Formal_Private_Type (N, T, Def);
3192 when N_Formal_Derived_Type_Definition =>
3193 Analyze_Formal_Derived_Type (N, T, Def);
3195 when N_Formal_Incomplete_Type_Definition =>
3196 Analyze_Formal_Incomplete_Type (T, Def);
3198 when N_Formal_Discrete_Type_Definition =>
3199 Analyze_Formal_Discrete_Type (T, Def);
3201 when N_Formal_Signed_Integer_Type_Definition =>
3202 Analyze_Formal_Signed_Integer_Type (T, Def);
3204 when N_Formal_Modular_Type_Definition =>
3205 Analyze_Formal_Modular_Type (T, Def);
3207 when N_Formal_Floating_Point_Definition =>
3208 Analyze_Formal_Floating_Type (T, Def);
3210 when N_Formal_Ordinary_Fixed_Point_Definition =>
3211 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3213 when N_Formal_Decimal_Fixed_Point_Definition =>
3214 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3216 when N_Array_Type_Definition =>
3217 Analyze_Formal_Array_Type (T, Def);
3219 when N_Access_Function_Definition
3220 | N_Access_Procedure_Definition
3221 | N_Access_To_Object_Definition
3223 Analyze_Generic_Access_Type (T, Def);
3225 -- Ada 2005: a interface declaration is encoded as an abstract
3226 -- record declaration or a abstract type derivation.
3228 when N_Record_Definition =>
3229 Analyze_Formal_Interface_Type (N, T, Def);
3231 when N_Derived_Type_Definition =>
3232 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3234 when N_Error =>
3235 null;
3237 when others =>
3238 raise Program_Error;
3239 end case;
3241 Set_Is_Generic_Type (T);
3243 if Has_Aspects (N) then
3244 Analyze_Aspect_Specifications (N, T);
3245 end if;
3246 end Analyze_Formal_Type_Declaration;
3248 ------------------------------------
3249 -- Analyze_Function_Instantiation --
3250 ------------------------------------
3252 procedure Analyze_Function_Instantiation (N : Node_Id) is
3253 begin
3254 Analyze_Subprogram_Instantiation (N, E_Function);
3255 end Analyze_Function_Instantiation;
3257 ---------------------------------
3258 -- Analyze_Generic_Access_Type --
3259 ---------------------------------
3261 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3262 begin
3263 Enter_Name (T);
3265 if Nkind (Def) = N_Access_To_Object_Definition then
3266 Access_Type_Declaration (T, Def);
3268 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3269 and then No (Full_View (Designated_Type (T)))
3270 and then not Is_Generic_Type (Designated_Type (T))
3271 then
3272 Error_Msg_N ("premature usage of incomplete type", Def);
3274 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3275 Error_Msg_N
3276 ("only a subtype mark is allowed in a formal", Def);
3277 end if;
3279 else
3280 Access_Subprogram_Declaration (T, Def);
3281 end if;
3282 end Analyze_Generic_Access_Type;
3284 ---------------------------------
3285 -- Analyze_Generic_Formal_Part --
3286 ---------------------------------
3288 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3289 Gen_Parm_Decl : Node_Id;
3291 begin
3292 -- The generic formals are processed in the scope of the generic unit,
3293 -- where they are immediately visible. The scope is installed by the
3294 -- caller.
3296 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3297 while Present (Gen_Parm_Decl) loop
3298 Analyze (Gen_Parm_Decl);
3299 Next (Gen_Parm_Decl);
3300 end loop;
3302 Generate_Reference_To_Generic_Formals (Current_Scope);
3303 end Analyze_Generic_Formal_Part;
3305 ------------------------------------------
3306 -- Analyze_Generic_Package_Declaration --
3307 ------------------------------------------
3309 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3310 Loc : constant Source_Ptr := Sloc (N);
3311 Decls : constant List_Id :=
3312 Visible_Declarations (Specification (N));
3313 Decl : Node_Id;
3314 Id : Entity_Id;
3315 New_N : Node_Id;
3316 Renaming : Node_Id;
3317 Save_Parent : Node_Id;
3319 begin
3320 Check_SPARK_05_Restriction ("generic is not allowed", N);
3322 -- We introduce a renaming of the enclosing package, to have a usable
3323 -- entity as the prefix of an expanded name for a local entity of the
3324 -- form Par.P.Q, where P is the generic package. This is because a local
3325 -- entity named P may hide it, so that the usual visibility rules in
3326 -- the instance will not resolve properly.
3328 Renaming :=
3329 Make_Package_Renaming_Declaration (Loc,
3330 Defining_Unit_Name =>
3331 Make_Defining_Identifier (Loc,
3332 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3333 Name =>
3334 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3336 if Present (Decls) then
3337 Decl := First (Decls);
3338 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3339 Next (Decl);
3340 end loop;
3342 if Present (Decl) then
3343 Insert_Before (Decl, Renaming);
3344 else
3345 Append (Renaming, Visible_Declarations (Specification (N)));
3346 end if;
3348 else
3349 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3350 end if;
3352 -- Create copy of generic unit, and save for instantiation. If the unit
3353 -- is a child unit, do not copy the specifications for the parent, which
3354 -- are not part of the generic tree.
3356 Save_Parent := Parent_Spec (N);
3357 Set_Parent_Spec (N, Empty);
3359 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3360 Set_Parent_Spec (New_N, Save_Parent);
3361 Rewrite (N, New_N);
3363 -- Once the contents of the generic copy and the template are swapped,
3364 -- do the same for their respective aspect specifications.
3366 Exchange_Aspects (N, New_N);
3368 -- Collect all contract-related source pragmas found within the template
3369 -- and attach them to the contract of the package spec. This contract is
3370 -- used in the capture of global references within annotations.
3372 Create_Generic_Contract (N);
3374 Id := Defining_Entity (N);
3375 Generate_Definition (Id);
3377 -- Expansion is not applied to generic units
3379 Start_Generic;
3381 Enter_Name (Id);
3382 Set_Ekind (Id, E_Generic_Package);
3383 Set_Etype (Id, Standard_Void_Type);
3385 -- Set SPARK_Mode from context
3387 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3388 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3389 Set_SPARK_Pragma_Inherited (Id);
3390 Set_SPARK_Aux_Pragma_Inherited (Id);
3392 -- Analyze aspects now, so that generated pragmas appear in the
3393 -- declarations before building and analyzing the generic copy.
3395 if Has_Aspects (N) then
3396 Analyze_Aspect_Specifications (N, Id);
3397 end if;
3399 Push_Scope (Id);
3400 Enter_Generic_Scope (Id);
3401 Set_Inner_Instances (Id, New_Elmt_List);
3403 Set_Categorization_From_Pragmas (N);
3404 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3406 -- Link the declaration of the generic homonym in the generic copy to
3407 -- the package it renames, so that it is always resolved properly.
3409 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3410 Set_Entity (Associated_Node (Name (Renaming)), Id);
3412 -- For a library unit, we have reconstructed the entity for the unit,
3413 -- and must reset it in the library tables.
3415 if Nkind (Parent (N)) = N_Compilation_Unit then
3416 Set_Cunit_Entity (Current_Sem_Unit, Id);
3417 end if;
3419 Analyze_Generic_Formal_Part (N);
3421 -- After processing the generic formals, analysis proceeds as for a
3422 -- non-generic package.
3424 Analyze (Specification (N));
3426 Validate_Categorization_Dependency (N, Id);
3428 End_Generic;
3430 End_Package_Scope (Id);
3431 Exit_Generic_Scope (Id);
3433 -- If the generic appears within a package unit, the body of that unit
3434 -- has to be present for instantiation and inlining.
3436 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3437 Set_Body_Needed_For_Inlining
3438 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3439 end if;
3441 if Nkind (Parent (N)) /= N_Compilation_Unit then
3442 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3443 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3444 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3446 else
3447 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3448 Validate_RT_RAT_Component (N);
3450 -- If this is a spec without a body, check that generic parameters
3451 -- are referenced.
3453 if not Body_Required (Parent (N)) then
3454 Check_References (Id);
3455 end if;
3456 end if;
3458 -- If there is a specified storage pool in the context, create an
3459 -- aspect on the package declaration, so that it is used in any
3460 -- instance that does not override it.
3462 if Present (Default_Pool) then
3463 declare
3464 ASN : Node_Id;
3466 begin
3467 ASN :=
3468 Make_Aspect_Specification (Loc,
3469 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3470 Expression => New_Copy (Default_Pool));
3472 if No (Aspect_Specifications (Specification (N))) then
3473 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3474 else
3475 Append (ASN, Aspect_Specifications (Specification (N)));
3476 end if;
3477 end;
3478 end if;
3479 end Analyze_Generic_Package_Declaration;
3481 --------------------------------------------
3482 -- Analyze_Generic_Subprogram_Declaration --
3483 --------------------------------------------
3485 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3486 Formals : List_Id;
3487 Id : Entity_Id;
3488 New_N : Node_Id;
3489 Result_Type : Entity_Id;
3490 Save_Parent : Node_Id;
3491 Spec : Node_Id;
3492 Typ : Entity_Id;
3494 begin
3495 Check_SPARK_05_Restriction ("generic is not allowed", N);
3497 -- Create copy of generic unit, and save for instantiation. If the unit
3498 -- is a child unit, do not copy the specifications for the parent, which
3499 -- are not part of the generic tree.
3501 Save_Parent := Parent_Spec (N);
3502 Set_Parent_Spec (N, Empty);
3504 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3505 Set_Parent_Spec (New_N, Save_Parent);
3506 Rewrite (N, New_N);
3508 -- Once the contents of the generic copy and the template are swapped,
3509 -- do the same for their respective aspect specifications.
3511 Exchange_Aspects (N, New_N);
3513 -- Collect all contract-related source pragmas found within the template
3514 -- and attach them to the contract of the subprogram spec. This contract
3515 -- is used in the capture of global references within annotations.
3517 Create_Generic_Contract (N);
3519 Spec := Specification (N);
3520 Id := Defining_Entity (Spec);
3521 Generate_Definition (Id);
3523 if Nkind (Id) = N_Defining_Operator_Symbol then
3524 Error_Msg_N
3525 ("operator symbol not allowed for generic subprogram", Id);
3526 end if;
3528 Start_Generic;
3530 Enter_Name (Id);
3531 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3533 -- Analyze the aspects of the generic copy to ensure that all generated
3534 -- pragmas (if any) perform their semantic effects.
3536 if Has_Aspects (N) then
3537 Analyze_Aspect_Specifications (N, Id);
3538 end if;
3540 Push_Scope (Id);
3541 Enter_Generic_Scope (Id);
3542 Set_Inner_Instances (Id, New_Elmt_List);
3543 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3545 Analyze_Generic_Formal_Part (N);
3547 Formals := Parameter_Specifications (Spec);
3549 if Nkind (Spec) = N_Function_Specification then
3550 Set_Ekind (Id, E_Generic_Function);
3551 else
3552 Set_Ekind (Id, E_Generic_Procedure);
3553 end if;
3555 if Present (Formals) then
3556 Process_Formals (Formals, Spec);
3557 end if;
3559 if Nkind (Spec) = N_Function_Specification then
3560 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3561 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3562 Set_Etype (Id, Result_Type);
3564 -- Check restriction imposed by AI05-073: a generic function
3565 -- cannot return an abstract type or an access to such.
3567 -- This is a binding interpretation should it apply to earlier
3568 -- versions of Ada as well as Ada 2012???
3570 if Is_Abstract_Type (Designated_Type (Result_Type))
3571 and then Ada_Version >= Ada_2012
3572 then
3573 Error_Msg_N
3574 ("generic function cannot have an access result "
3575 & "that designates an abstract type", Spec);
3576 end if;
3578 else
3579 Find_Type (Result_Definition (Spec));
3580 Typ := Entity (Result_Definition (Spec));
3582 if Is_Abstract_Type (Typ)
3583 and then Ada_Version >= Ada_2012
3584 then
3585 Error_Msg_N
3586 ("generic function cannot have abstract result type", Spec);
3587 end if;
3589 -- If a null exclusion is imposed on the result type, then create
3590 -- a null-excluding itype (an access subtype) and use it as the
3591 -- function's Etype.
3593 if Is_Access_Type (Typ)
3594 and then Null_Exclusion_Present (Spec)
3595 then
3596 Set_Etype (Id,
3597 Create_Null_Excluding_Itype
3598 (T => Typ,
3599 Related_Nod => Spec,
3600 Scope_Id => Defining_Unit_Name (Spec)));
3601 else
3602 Set_Etype (Id, Typ);
3603 end if;
3604 end if;
3606 else
3607 Set_Etype (Id, Standard_Void_Type);
3608 end if;
3610 -- For a library unit, we have reconstructed the entity for the unit,
3611 -- and must reset it in the library tables. We also make sure that
3612 -- Body_Required is set properly in the original compilation unit node.
3614 if Nkind (Parent (N)) = N_Compilation_Unit then
3615 Set_Cunit_Entity (Current_Sem_Unit, Id);
3616 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3617 end if;
3619 -- If the generic appears within a package unit, the body of that unit
3620 -- has to be present for instantiation and inlining.
3622 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3623 and then Unit_Requires_Body (Id)
3624 then
3625 Set_Body_Needed_For_Inlining
3626 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3627 end if;
3629 Set_Categorization_From_Pragmas (N);
3630 Validate_Categorization_Dependency (N, Id);
3632 -- Capture all global references that occur within the profile of the
3633 -- generic subprogram. Aspects are not part of this processing because
3634 -- they must be delayed. If processed now, Save_Global_References will
3635 -- destroy the Associated_Node links and prevent the capture of global
3636 -- references when the contract of the generic subprogram is analyzed.
3638 Save_Global_References (Original_Node (N));
3640 End_Generic;
3641 End_Scope;
3642 Exit_Generic_Scope (Id);
3643 Generate_Reference_To_Formals (Id);
3645 List_Inherited_Pre_Post_Aspects (Id);
3646 end Analyze_Generic_Subprogram_Declaration;
3648 -----------------------------------
3649 -- Analyze_Package_Instantiation --
3650 -----------------------------------
3652 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3653 -- must be replaced by gotos which jump to the end of the routine in order
3654 -- to restore the Ghost and SPARK modes.
3656 procedure Analyze_Package_Instantiation (N : Node_Id) is
3657 Has_Inline_Always : Boolean := False;
3659 procedure Delay_Descriptors (E : Entity_Id);
3660 -- Delay generation of subprogram descriptors for given entity
3662 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3663 -- If inlining is active and the generic contains inlined subprograms,
3664 -- we instantiate the body. This may cause superfluous instantiations,
3665 -- but it is simpler than detecting the need for the body at the point
3666 -- of inlining, when the context of the instance is not available.
3668 -----------------------
3669 -- Delay_Descriptors --
3670 -----------------------
3672 procedure Delay_Descriptors (E : Entity_Id) is
3673 begin
3674 if not Delay_Subprogram_Descriptors (E) then
3675 Set_Delay_Subprogram_Descriptors (E);
3676 Pending_Descriptor.Append (E);
3677 end if;
3678 end Delay_Descriptors;
3680 -----------------------
3681 -- Might_Inline_Subp --
3682 -----------------------
3684 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3685 E : Entity_Id;
3687 begin
3688 if not Inline_Processing_Required then
3689 return False;
3691 else
3692 E := First_Entity (Gen_Unit);
3693 while Present (E) loop
3694 if Is_Subprogram (E) and then Is_Inlined (E) then
3695 -- Remember if there are any subprograms with Inline_Always
3697 if Has_Pragma_Inline_Always (E) then
3698 Has_Inline_Always := True;
3699 end if;
3701 return True;
3702 end if;
3704 Next_Entity (E);
3705 end loop;
3706 end if;
3708 return False;
3709 end Might_Inline_Subp;
3711 -- Local declarations
3713 Gen_Id : constant Node_Id := Name (N);
3714 Is_Actual_Pack : constant Boolean :=
3715 Is_Internal (Defining_Entity (N));
3716 Loc : constant Source_Ptr := Sloc (N);
3718 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3719 Saved_ISMP : constant Boolean :=
3720 Ignore_SPARK_Mode_Pragmas_In_Instance;
3721 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3722 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3723 -- Save the Ghost and SPARK mode-related data to restore on exit
3725 Saved_Style_Check : constant Boolean := Style_Check;
3726 -- Save style check mode for restore on exit
3728 Act_Decl : Node_Id;
3729 Act_Decl_Name : Node_Id;
3730 Act_Decl_Id : Entity_Id;
3731 Act_Spec : Node_Id;
3732 Act_Tree : Node_Id;
3733 Env_Installed : Boolean := False;
3734 Gen_Decl : Node_Id;
3735 Gen_Spec : Node_Id;
3736 Gen_Unit : Entity_Id;
3737 Inline_Now : Boolean := False;
3738 Needs_Body : Boolean;
3739 Parent_Installed : Boolean := False;
3740 Renaming_List : List_Id;
3741 Unit_Renaming : Node_Id;
3743 Vis_Prims_List : Elist_Id := No_Elist;
3744 -- List of primitives made temporarily visible in the instantiation
3745 -- to match the visibility of the formal type
3747 -- Start of processing for Analyze_Package_Instantiation
3749 begin
3750 Check_SPARK_05_Restriction ("generic is not allowed", N);
3752 -- Very first thing: check for Text_IO special unit in case we are
3753 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3755 Check_Text_IO_Special_Unit (Name (N));
3757 -- Make node global for error reporting
3759 Instantiation_Node := N;
3761 -- Case of instantiation of a generic package
3763 if Nkind (N) = N_Package_Instantiation then
3764 Act_Decl_Id := New_Copy (Defining_Entity (N));
3765 Set_Comes_From_Source (Act_Decl_Id, True);
3767 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3768 Act_Decl_Name :=
3769 Make_Defining_Program_Unit_Name (Loc,
3770 Name =>
3771 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3772 Defining_Identifier => Act_Decl_Id);
3773 else
3774 Act_Decl_Name := Act_Decl_Id;
3775 end if;
3777 -- Case of instantiation of a formal package
3779 else
3780 Act_Decl_Id := Defining_Identifier (N);
3781 Act_Decl_Name := Act_Decl_Id;
3782 end if;
3784 Generate_Definition (Act_Decl_Id);
3785 Set_Ekind (Act_Decl_Id, E_Package);
3787 -- Initialize list of incomplete actuals before analysis
3789 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3791 Preanalyze_Actuals (N, Act_Decl_Id);
3793 -- Turn off style checking in instances. If the check is enabled on the
3794 -- generic unit, a warning in an instance would just be noise. If not
3795 -- enabled on the generic, then a warning in an instance is just wrong.
3796 -- This must be done after analyzing the actuals, which do come from
3797 -- source and are subject to style checking.
3799 Style_Check := False;
3801 Init_Env;
3802 Env_Installed := True;
3804 -- Reset renaming map for formal types. The mapping is established
3805 -- when analyzing the generic associations, but some mappings are
3806 -- inherited from formal packages of parent units, and these are
3807 -- constructed when the parents are installed.
3809 Generic_Renamings.Set_Last (0);
3810 Generic_Renamings_HTable.Reset;
3812 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3813 Gen_Unit := Entity (Gen_Id);
3815 -- A package instantiation is Ghost when it is subject to pragma Ghost
3816 -- or the generic template is Ghost. Set the mode now to ensure that
3817 -- any nodes generated during analysis and expansion are marked as
3818 -- Ghost.
3820 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
3822 -- Verify that it is the name of a generic package
3824 -- A visibility glitch: if the instance is a child unit and the generic
3825 -- is the generic unit of a parent instance (i.e. both the parent and
3826 -- the child units are instances of the same package) the name now
3827 -- denotes the renaming within the parent, not the intended generic
3828 -- unit. See if there is a homonym that is the desired generic. The
3829 -- renaming declaration must be visible inside the instance of the
3830 -- child, but not when analyzing the name in the instantiation itself.
3832 if Ekind (Gen_Unit) = E_Package
3833 and then Present (Renamed_Entity (Gen_Unit))
3834 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3835 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3836 and then Present (Homonym (Gen_Unit))
3837 then
3838 Gen_Unit := Homonym (Gen_Unit);
3839 end if;
3841 if Etype (Gen_Unit) = Any_Type then
3842 Restore_Env;
3843 goto Leave;
3845 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3847 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3849 if From_Limited_With (Gen_Unit) then
3850 Error_Msg_N
3851 ("cannot instantiate a limited withed package", Gen_Id);
3852 else
3853 Error_Msg_NE
3854 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3855 end if;
3857 Restore_Env;
3858 goto Leave;
3859 end if;
3861 if In_Extended_Main_Source_Unit (N) then
3862 Set_Is_Instantiated (Gen_Unit);
3863 Generate_Reference (Gen_Unit, N);
3865 if Present (Renamed_Object (Gen_Unit)) then
3866 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3867 Generate_Reference (Renamed_Object (Gen_Unit), N);
3868 end if;
3869 end if;
3871 if Nkind (Gen_Id) = N_Identifier
3872 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3873 then
3874 Error_Msg_NE
3875 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3877 elsif Nkind (Gen_Id) = N_Expanded_Name
3878 and then Is_Child_Unit (Gen_Unit)
3879 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3880 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3881 then
3882 Error_Msg_N
3883 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3884 end if;
3886 Set_Entity (Gen_Id, Gen_Unit);
3888 -- If generic is a renaming, get original generic unit
3890 if Present (Renamed_Object (Gen_Unit))
3891 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3892 then
3893 Gen_Unit := Renamed_Object (Gen_Unit);
3894 end if;
3896 -- Verify that there are no circular instantiations
3898 if In_Open_Scopes (Gen_Unit) then
3899 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3900 Restore_Env;
3901 goto Leave;
3903 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3904 Error_Msg_Node_2 := Current_Scope;
3905 Error_Msg_NE
3906 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3907 Circularity_Detected := True;
3908 Restore_Env;
3909 goto Leave;
3911 else
3912 -- If the context of the instance is subject to SPARK_Mode "off" or
3913 -- the annotation is altogether missing, set the global flag which
3914 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3915 -- the instance.
3917 if SPARK_Mode /= On then
3918 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3920 -- Mark the instance spec in case the body is instantiated at a
3921 -- later pass. This preserves the original context in effect for
3922 -- the body.
3924 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
3925 end if;
3927 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3928 Gen_Spec := Specification (Gen_Decl);
3930 -- Initialize renamings map, for error checking, and the list that
3931 -- holds private entities whose views have changed between generic
3932 -- definition and instantiation. If this is the instance created to
3933 -- validate an actual package, the instantiation environment is that
3934 -- of the enclosing instance.
3936 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3938 -- Copy original generic tree, to produce text for instantiation
3940 Act_Tree :=
3941 Copy_Generic_Node
3942 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3944 Act_Spec := Specification (Act_Tree);
3946 -- If this is the instance created to validate an actual package,
3947 -- only the formals matter, do not examine the package spec itself.
3949 if Is_Actual_Pack then
3950 Set_Visible_Declarations (Act_Spec, New_List);
3951 Set_Private_Declarations (Act_Spec, New_List);
3952 end if;
3954 Renaming_List :=
3955 Analyze_Associations
3956 (I_Node => N,
3957 Formals => Generic_Formal_Declarations (Act_Tree),
3958 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3960 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3962 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3963 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3964 Set_Is_Generic_Instance (Act_Decl_Id);
3965 Set_Generic_Parent (Act_Spec, Gen_Unit);
3967 -- References to the generic in its own declaration or its body are
3968 -- references to the instance. Add a renaming declaration for the
3969 -- generic unit itself. This declaration, as well as the renaming
3970 -- declarations for the generic formals, must remain private to the
3971 -- unit: the formals, because this is the language semantics, and
3972 -- the unit because its use is an artifact of the implementation.
3974 Unit_Renaming :=
3975 Make_Package_Renaming_Declaration (Loc,
3976 Defining_Unit_Name =>
3977 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3978 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3980 Append (Unit_Renaming, Renaming_List);
3982 -- The renaming declarations are the first local declarations of the
3983 -- new unit.
3985 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3986 Insert_List_Before
3987 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3988 else
3989 Set_Visible_Declarations (Act_Spec, Renaming_List);
3990 end if;
3992 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3994 -- Propagate the aspect specifications from the package declaration
3995 -- template to the instantiated version of the package declaration.
3997 if Has_Aspects (Act_Tree) then
3998 Set_Aspect_Specifications (Act_Decl,
3999 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4000 end if;
4002 -- The generic may have a generated Default_Storage_Pool aspect,
4003 -- set at the point of generic declaration. If the instance has
4004 -- that aspect, it overrides the one inherited from the generic.
4006 if Has_Aspects (Gen_Spec) then
4007 if No (Aspect_Specifications (N)) then
4008 Set_Aspect_Specifications (N,
4009 (New_Copy_List_Tree
4010 (Aspect_Specifications (Gen_Spec))));
4012 else
4013 declare
4014 ASN1, ASN2 : Node_Id;
4016 begin
4017 ASN1 := First (Aspect_Specifications (N));
4018 while Present (ASN1) loop
4019 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
4020 then
4021 -- If generic carries a default storage pool, remove
4022 -- it in favor of the instance one.
4024 ASN2 := First (Aspect_Specifications (Gen_Spec));
4025 while Present (ASN2) loop
4026 if Chars (Identifier (ASN2)) =
4027 Name_Default_Storage_Pool
4028 then
4029 Remove (ASN2);
4030 exit;
4031 end if;
4033 Next (ASN2);
4034 end loop;
4035 end if;
4037 Next (ASN1);
4038 end loop;
4040 Prepend_List_To (Aspect_Specifications (N),
4041 (New_Copy_List_Tree
4042 (Aspect_Specifications (Gen_Spec))));
4043 end;
4044 end if;
4045 end if;
4047 -- Save the instantiation node, for subsequent instantiation of the
4048 -- body, if there is one and we are generating code for the current
4049 -- unit. Mark unit as having a body (avoids premature error message).
4051 -- We instantiate the body if we are generating code, if we are
4052 -- generating cross-reference information, or if we are building
4053 -- trees for ASIS use or GNATprove use.
4055 declare
4056 Enclosing_Body_Present : Boolean := False;
4057 -- If the generic unit is not a compilation unit, then a body may
4058 -- be present in its parent even if none is required. We create a
4059 -- tentative pending instantiation for the body, which will be
4060 -- discarded if none is actually present.
4062 Scop : Entity_Id;
4064 begin
4065 if Scope (Gen_Unit) /= Standard_Standard
4066 and then not Is_Child_Unit (Gen_Unit)
4067 then
4068 Scop := Scope (Gen_Unit);
4069 while Present (Scop) and then Scop /= Standard_Standard loop
4070 if Unit_Requires_Body (Scop) then
4071 Enclosing_Body_Present := True;
4072 exit;
4074 elsif In_Open_Scopes (Scop)
4075 and then In_Package_Body (Scop)
4076 then
4077 Enclosing_Body_Present := True;
4078 exit;
4079 end if;
4081 exit when Is_Compilation_Unit (Scop);
4082 Scop := Scope (Scop);
4083 end loop;
4084 end if;
4086 -- If front-end inlining is enabled or there are any subprograms
4087 -- marked with Inline_Always, and this is a unit for which code
4088 -- will be generated, we instantiate the body at once.
4090 -- This is done if the instance is not the main unit, and if the
4091 -- generic is not a child unit of another generic, to avoid scope
4092 -- problems and the reinstallation of parent instances.
4094 if Expander_Active
4095 and then (not Is_Child_Unit (Gen_Unit)
4096 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4097 and then Might_Inline_Subp (Gen_Unit)
4098 and then not Is_Actual_Pack
4099 then
4100 if not Back_End_Inlining
4101 and then (Front_End_Inlining or else Has_Inline_Always)
4102 and then (Is_In_Main_Unit (N)
4103 or else In_Main_Context (Current_Scope))
4104 and then Nkind (Parent (N)) /= N_Compilation_Unit
4105 then
4106 Inline_Now := True;
4108 -- In configurable_run_time mode we force the inlining of
4109 -- predefined subprograms marked Inline_Always, to minimize
4110 -- the use of the run-time library.
4112 elsif In_Predefined_Unit (Gen_Decl)
4113 and then Configurable_Run_Time_Mode
4114 and then Nkind (Parent (N)) /= N_Compilation_Unit
4115 then
4116 Inline_Now := True;
4117 end if;
4119 -- If the current scope is itself an instance within a child
4120 -- unit, there will be duplications in the scope stack, and the
4121 -- unstacking mechanism in Inline_Instance_Body will fail.
4122 -- This loses some rare cases of optimization, and might be
4123 -- improved some day, if we can find a proper abstraction for
4124 -- "the complete compilation context" that can be saved and
4125 -- restored. ???
4127 if Is_Generic_Instance (Current_Scope) then
4128 declare
4129 Curr_Unit : constant Entity_Id :=
4130 Cunit_Entity (Current_Sem_Unit);
4131 begin
4132 if Curr_Unit /= Current_Scope
4133 and then Is_Child_Unit (Curr_Unit)
4134 then
4135 Inline_Now := False;
4136 end if;
4137 end;
4138 end if;
4139 end if;
4141 Needs_Body :=
4142 (Unit_Requires_Body (Gen_Unit)
4143 or else Enclosing_Body_Present
4144 or else Present (Corresponding_Body (Gen_Decl)))
4145 and then (Is_In_Main_Unit (N)
4146 or else Might_Inline_Subp (Gen_Unit))
4147 and then not Is_Actual_Pack
4148 and then not Inline_Now
4149 and then (Operating_Mode = Generate_Code
4151 -- Need comment for this check ???
4153 or else (Operating_Mode = Check_Semantics
4154 and then (ASIS_Mode or GNATprove_Mode)));
4156 -- If front-end inlining is enabled or there are any subprograms
4157 -- marked with Inline_Always, do not instantiate body when within
4158 -- a generic context.
4160 if ((Front_End_Inlining or else Has_Inline_Always)
4161 and then not Expander_Active)
4162 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4163 then
4164 Needs_Body := False;
4165 end if;
4167 -- If the current context is generic, and the package being
4168 -- instantiated is declared within a formal package, there is no
4169 -- body to instantiate until the enclosing generic is instantiated
4170 -- and there is an actual for the formal package. If the formal
4171 -- package has parameters, we build a regular package instance for
4172 -- it, that precedes the original formal package declaration.
4174 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4175 declare
4176 Decl : constant Node_Id :=
4177 Original_Node
4178 (Unit_Declaration_Node (Scope (Gen_Unit)));
4179 begin
4180 if Nkind (Decl) = N_Formal_Package_Declaration
4181 or else (Nkind (Decl) = N_Package_Declaration
4182 and then Is_List_Member (Decl)
4183 and then Present (Next (Decl))
4184 and then
4185 Nkind (Next (Decl)) =
4186 N_Formal_Package_Declaration)
4187 then
4188 Needs_Body := False;
4189 end if;
4190 end;
4191 end if;
4192 end;
4194 -- For RCI unit calling stubs, we omit the instance body if the
4195 -- instance is the RCI library unit itself.
4197 -- However there is a special case for nested instances: in this case
4198 -- we do generate the instance body, as it might be required, e.g.
4199 -- because it provides stream attributes for some type used in the
4200 -- profile of a remote subprogram. This is consistent with 12.3(12),
4201 -- which indicates that the instance body occurs at the place of the
4202 -- instantiation, and thus is part of the RCI declaration, which is
4203 -- present on all client partitions (this is E.2.3(18)).
4205 -- Note that AI12-0002 may make it illegal at some point to have
4206 -- stream attributes defined in an RCI unit, in which case this
4207 -- special case will become unnecessary. In the meantime, there
4208 -- is known application code in production that depends on this
4209 -- being possible, so we definitely cannot eliminate the body in
4210 -- the case of nested instances for the time being.
4212 -- When we generate a nested instance body, calling stubs for any
4213 -- relevant subprogram will be be inserted immediately after the
4214 -- subprogram declarations, and will take precedence over the
4215 -- subsequent (original) body. (The stub and original body will be
4216 -- complete homographs, but this is permitted in an instance).
4217 -- (Could we do better and remove the original body???)
4219 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4220 and then Comes_From_Source (N)
4221 and then Nkind (Parent (N)) = N_Compilation_Unit
4222 then
4223 Needs_Body := False;
4224 end if;
4226 if Needs_Body then
4228 -- Here is a defence against a ludicrous number of instantiations
4229 -- caused by a circular set of instantiation attempts.
4231 if Pending_Instantiations.Last > Maximum_Instantiations then
4232 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4233 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4234 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4235 raise Unrecoverable_Error;
4236 end if;
4238 -- Indicate that the enclosing scopes contain an instantiation,
4239 -- and that cleanup actions should be delayed until after the
4240 -- instance body is expanded.
4242 Check_Forward_Instantiation (Gen_Decl);
4243 if Nkind (N) = N_Package_Instantiation then
4244 declare
4245 Enclosing_Master : Entity_Id;
4247 begin
4248 -- Loop to search enclosing masters
4250 Enclosing_Master := Current_Scope;
4251 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4252 if Ekind (Enclosing_Master) = E_Package then
4253 if Is_Compilation_Unit (Enclosing_Master) then
4254 if In_Package_Body (Enclosing_Master) then
4255 Delay_Descriptors
4256 (Body_Entity (Enclosing_Master));
4257 else
4258 Delay_Descriptors
4259 (Enclosing_Master);
4260 end if;
4262 exit Scope_Loop;
4264 else
4265 Enclosing_Master := Scope (Enclosing_Master);
4266 end if;
4268 elsif Is_Generic_Unit (Enclosing_Master)
4269 or else Ekind (Enclosing_Master) = E_Void
4270 then
4271 -- Cleanup actions will eventually be performed on the
4272 -- enclosing subprogram or package instance, if any.
4273 -- Enclosing scope is void in the formal part of a
4274 -- generic subprogram.
4276 exit Scope_Loop;
4278 else
4279 if Ekind (Enclosing_Master) = E_Entry
4280 and then
4281 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4282 then
4283 if not Expander_Active then
4284 exit Scope_Loop;
4285 else
4286 Enclosing_Master :=
4287 Protected_Body_Subprogram (Enclosing_Master);
4288 end if;
4289 end if;
4291 Set_Delay_Cleanups (Enclosing_Master);
4293 while Ekind (Enclosing_Master) = E_Block loop
4294 Enclosing_Master := Scope (Enclosing_Master);
4295 end loop;
4297 if Is_Subprogram (Enclosing_Master) then
4298 Delay_Descriptors (Enclosing_Master);
4300 elsif Is_Task_Type (Enclosing_Master) then
4301 declare
4302 TBP : constant Node_Id :=
4303 Get_Task_Body_Procedure
4304 (Enclosing_Master);
4305 begin
4306 if Present (TBP) then
4307 Delay_Descriptors (TBP);
4308 Set_Delay_Cleanups (TBP);
4309 end if;
4310 end;
4311 end if;
4313 exit Scope_Loop;
4314 end if;
4315 end loop Scope_Loop;
4316 end;
4318 -- Make entry in table
4320 Add_Pending_Instantiation (N, Act_Decl);
4321 end if;
4322 end if;
4324 Set_Categorization_From_Pragmas (Act_Decl);
4326 if Parent_Installed then
4327 Hide_Current_Scope;
4328 end if;
4330 Set_Instance_Spec (N, Act_Decl);
4332 -- If not a compilation unit, insert the package declaration before
4333 -- the original instantiation node.
4335 if Nkind (Parent (N)) /= N_Compilation_Unit then
4336 Mark_Rewrite_Insertion (Act_Decl);
4337 Insert_Before (N, Act_Decl);
4339 if Has_Aspects (N) then
4340 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4342 -- The pragma created for a Default_Storage_Pool aspect must
4343 -- appear ahead of the declarations in the instance spec.
4344 -- Analysis has placed it after the instance node, so remove
4345 -- it and reinsert it properly now.
4347 declare
4348 ASN : constant Node_Id := First (Aspect_Specifications (N));
4349 A_Name : constant Name_Id := Chars (Identifier (ASN));
4350 Decl : Node_Id;
4352 begin
4353 if A_Name = Name_Default_Storage_Pool then
4354 if No (Visible_Declarations (Act_Spec)) then
4355 Set_Visible_Declarations (Act_Spec, New_List);
4356 end if;
4358 Decl := Next (N);
4359 while Present (Decl) loop
4360 if Nkind (Decl) = N_Pragma then
4361 Remove (Decl);
4362 Prepend (Decl, Visible_Declarations (Act_Spec));
4363 exit;
4364 end if;
4366 Next (Decl);
4367 end loop;
4368 end if;
4369 end;
4370 end if;
4372 Analyze (Act_Decl);
4374 -- For an instantiation that is a compilation unit, place
4375 -- declaration on current node so context is complete for analysis
4376 -- (including nested instantiations). If this is the main unit,
4377 -- the declaration eventually replaces the instantiation node.
4378 -- If the instance body is created later, it replaces the
4379 -- instance node, and the declaration is attached to it
4380 -- (see Build_Instance_Compilation_Unit_Nodes).
4382 else
4383 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4385 -- The entity for the current unit is the newly created one,
4386 -- and all semantic information is attached to it.
4388 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4390 -- If this is the main unit, replace the main entity as well
4392 if Current_Sem_Unit = Main_Unit then
4393 Main_Unit_Entity := Act_Decl_Id;
4394 end if;
4395 end if;
4397 Set_Unit (Parent (N), Act_Decl);
4398 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4399 Set_Package_Instantiation (Act_Decl_Id, N);
4401 -- Process aspect specifications of the instance node, if any, to
4402 -- take into account categorization pragmas before analyzing the
4403 -- instance.
4405 if Has_Aspects (N) then
4406 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4407 end if;
4409 Analyze (Act_Decl);
4410 Set_Unit (Parent (N), N);
4411 Set_Body_Required (Parent (N), False);
4413 -- We never need elaboration checks on instantiations, since by
4414 -- definition, the body instantiation is elaborated at the same
4415 -- time as the spec instantiation.
4417 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4418 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4419 end if;
4421 Check_Elab_Instantiation (N);
4423 if ABE_Is_Certain (N) and then Needs_Body then
4424 Pending_Instantiations.Decrement_Last;
4425 end if;
4427 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4429 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4430 First_Private_Entity (Act_Decl_Id));
4432 -- If the instantiation will receive a body, the unit will be
4433 -- transformed into a package body, and receive its own elaboration
4434 -- entity. Otherwise, the nature of the unit is now a package
4435 -- declaration.
4437 if Nkind (Parent (N)) = N_Compilation_Unit
4438 and then not Needs_Body
4439 then
4440 Rewrite (N, Act_Decl);
4441 end if;
4443 if Present (Corresponding_Body (Gen_Decl))
4444 or else Unit_Requires_Body (Gen_Unit)
4445 then
4446 Set_Has_Completion (Act_Decl_Id);
4447 end if;
4449 Check_Formal_Packages (Act_Decl_Id);
4451 Restore_Hidden_Primitives (Vis_Prims_List);
4452 Restore_Private_Views (Act_Decl_Id);
4454 Inherit_Context (Gen_Decl, N);
4456 if Parent_Installed then
4457 Remove_Parent;
4458 end if;
4460 Restore_Env;
4461 Env_Installed := False;
4462 end if;
4464 Validate_Categorization_Dependency (N, Act_Decl_Id);
4466 -- There used to be a check here to prevent instantiations in local
4467 -- contexts if the No_Local_Allocators restriction was active. This
4468 -- check was removed by a binding interpretation in AI-95-00130/07,
4469 -- but we retain the code for documentation purposes.
4471 -- if Ekind (Act_Decl_Id) /= E_Void
4472 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4473 -- then
4474 -- Check_Restriction (No_Local_Allocators, N);
4475 -- end if;
4477 if Inline_Now then
4478 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4479 end if;
4481 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4482 -- be used as defining identifiers for a formal package and for the
4483 -- corresponding expanded package.
4485 if Nkind (N) = N_Formal_Package_Declaration then
4486 Act_Decl_Id := New_Copy (Defining_Entity (N));
4487 Set_Comes_From_Source (Act_Decl_Id, True);
4488 Set_Is_Generic_Instance (Act_Decl_Id, False);
4489 Set_Defining_Identifier (N, Act_Decl_Id);
4490 end if;
4492 -- Check that if N is an instantiation of System.Dim_Float_IO or
4493 -- System.Dim_Integer_IO, the formal type has a dimension system.
4495 if Nkind (N) = N_Package_Instantiation
4496 and then Is_Dim_IO_Package_Instantiation (N)
4497 then
4498 declare
4499 Assoc : constant Node_Id := First (Generic_Associations (N));
4500 begin
4501 if not Has_Dimension_System
4502 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4503 then
4504 Error_Msg_N ("type with a dimension system expected", Assoc);
4505 end if;
4506 end;
4507 end if;
4509 <<Leave>>
4510 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4511 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4512 end if;
4514 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4515 Restore_Ghost_Mode (Saved_GM);
4516 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4517 Style_Check := Saved_Style_Check;
4519 exception
4520 when Instantiation_Error =>
4521 if Parent_Installed then
4522 Remove_Parent;
4523 end if;
4525 if Env_Installed then
4526 Restore_Env;
4527 end if;
4529 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4530 Restore_Ghost_Mode (Saved_GM);
4531 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4532 Style_Check := Saved_Style_Check;
4533 end Analyze_Package_Instantiation;
4535 --------------------------
4536 -- Inline_Instance_Body --
4537 --------------------------
4539 -- WARNING: This routine manages SPARK regions. Return statements must be
4540 -- replaced by gotos which jump to the end of the routine and restore the
4541 -- SPARK mode.
4543 procedure Inline_Instance_Body
4544 (N : Node_Id;
4545 Gen_Unit : Entity_Id;
4546 Act_Decl : Node_Id)
4548 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4549 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4550 Gen_Comp : constant Entity_Id :=
4551 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4553 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4554 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4555 -- Save the SPARK mode-related data to restore on exit. Removing
4556 -- enclosing scopes to provide a clean environment for analysis of
4557 -- the inlined body will eliminate any previously set SPARK_Mode.
4559 Scope_Stack_Depth : constant Pos :=
4560 Scope_Stack.Last - Scope_Stack.First + 1;
4562 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4563 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4564 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4566 Curr_Scope : Entity_Id := Empty;
4567 List : Elist_Id;
4568 N_Instances : Nat := 0;
4569 Num_Inner : Nat := 0;
4570 Num_Scopes : Nat := 0;
4571 Removed : Boolean := False;
4572 S : Entity_Id;
4573 Vis : Boolean;
4575 begin
4576 -- Case of generic unit defined in another unit. We must remove the
4577 -- complete context of the current unit to install that of the generic.
4579 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4581 -- Add some comments for the following two loops ???
4583 S := Current_Scope;
4584 while Present (S) and then S /= Standard_Standard loop
4585 loop
4586 Num_Scopes := Num_Scopes + 1;
4588 Use_Clauses (Num_Scopes) :=
4589 (Scope_Stack.Table
4590 (Scope_Stack.Last - Num_Scopes + 1).
4591 First_Use_Clause);
4592 End_Use_Clauses (Use_Clauses (Num_Scopes));
4594 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4595 or else Scope_Stack.Table
4596 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4597 end loop;
4599 exit when Is_Generic_Instance (S)
4600 and then (In_Package_Body (S)
4601 or else Ekind (S) = E_Procedure
4602 or else Ekind (S) = E_Function);
4603 S := Scope (S);
4604 end loop;
4606 Vis := Is_Immediately_Visible (Gen_Comp);
4608 -- Find and save all enclosing instances
4610 S := Current_Scope;
4612 while Present (S)
4613 and then S /= Standard_Standard
4614 loop
4615 if Is_Generic_Instance (S) then
4616 N_Instances := N_Instances + 1;
4617 Instances (N_Instances) := S;
4619 exit when In_Package_Body (S);
4620 end if;
4622 S := Scope (S);
4623 end loop;
4625 -- Remove context of current compilation unit, unless we are within a
4626 -- nested package instantiation, in which case the context has been
4627 -- removed previously.
4629 -- If current scope is the body of a child unit, remove context of
4630 -- spec as well. If an enclosing scope is an instance body, the
4631 -- context has already been removed, but the entities in the body
4632 -- must be made invisible as well.
4634 S := Current_Scope;
4635 while Present (S) and then S /= Standard_Standard loop
4636 if Is_Generic_Instance (S)
4637 and then (In_Package_Body (S)
4638 or else Ekind_In (S, E_Procedure, E_Function))
4639 then
4640 -- We still have to remove the entities of the enclosing
4641 -- instance from direct visibility.
4643 declare
4644 E : Entity_Id;
4645 begin
4646 E := First_Entity (S);
4647 while Present (E) loop
4648 Set_Is_Immediately_Visible (E, False);
4649 Next_Entity (E);
4650 end loop;
4651 end;
4653 exit;
4654 end if;
4656 if S = Curr_Unit
4657 or else (Ekind (Curr_Unit) = E_Package_Body
4658 and then S = Spec_Entity (Curr_Unit))
4659 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4660 and then S = Corresponding_Spec
4661 (Unit_Declaration_Node (Curr_Unit)))
4662 then
4663 Removed := True;
4665 -- Remove entities in current scopes from visibility, so that
4666 -- instance body is compiled in a clean environment.
4668 List := Save_Scope_Stack (Handle_Use => False);
4670 if Is_Child_Unit (S) then
4672 -- Remove child unit from stack, as well as inner scopes.
4673 -- Removing the context of a child unit removes parent units
4674 -- as well.
4676 while Current_Scope /= S loop
4677 Num_Inner := Num_Inner + 1;
4678 Inner_Scopes (Num_Inner) := Current_Scope;
4679 Pop_Scope;
4680 end loop;
4682 Pop_Scope;
4683 Remove_Context (Curr_Comp);
4684 Curr_Scope := S;
4686 else
4687 Remove_Context (Curr_Comp);
4688 end if;
4690 if Ekind (Curr_Unit) = E_Package_Body then
4691 Remove_Context (Library_Unit (Curr_Comp));
4692 end if;
4693 end if;
4695 S := Scope (S);
4696 end loop;
4698 pragma Assert (Num_Inner < Num_Scopes);
4700 -- The inlined package body must be analyzed with the SPARK_Mode of
4701 -- the enclosing context, otherwise the body may cause bogus errors
4702 -- if a configuration SPARK_Mode pragma in in effect.
4704 Push_Scope (Standard_Standard);
4705 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4706 Instantiate_Package_Body
4707 (Body_Info =>
4708 ((Inst_Node => N,
4709 Act_Decl => Act_Decl,
4710 Expander_Status => Expander_Active,
4711 Current_Sem_Unit => Current_Sem_Unit,
4712 Scope_Suppress => Scope_Suppress,
4713 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4714 Version => Ada_Version,
4715 Version_Pragma => Ada_Version_Pragma,
4716 Warnings => Save_Warnings,
4717 SPARK_Mode => Saved_SM,
4718 SPARK_Mode_Pragma => Saved_SMP)),
4719 Inlined_Body => True);
4721 Pop_Scope;
4723 -- Restore context
4725 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4727 -- Reset Generic_Instance flag so that use clauses can be installed
4728 -- in the proper order. (See Use_One_Package for effect of enclosing
4729 -- instances on processing of use clauses).
4731 for J in 1 .. N_Instances loop
4732 Set_Is_Generic_Instance (Instances (J), False);
4733 end loop;
4735 if Removed then
4736 Install_Context (Curr_Comp);
4738 if Present (Curr_Scope)
4739 and then Is_Child_Unit (Curr_Scope)
4740 then
4741 Push_Scope (Curr_Scope);
4742 Set_Is_Immediately_Visible (Curr_Scope);
4744 -- Finally, restore inner scopes as well
4746 for J in reverse 1 .. Num_Inner loop
4747 Push_Scope (Inner_Scopes (J));
4748 end loop;
4749 end if;
4751 Restore_Scope_Stack (List, Handle_Use => False);
4753 if Present (Curr_Scope)
4754 and then
4755 (In_Private_Part (Curr_Scope)
4756 or else In_Package_Body (Curr_Scope))
4757 then
4758 -- Install private declaration of ancestor units, which are
4759 -- currently available. Restore_Scope_Stack and Install_Context
4760 -- only install the visible part of parents.
4762 declare
4763 Par : Entity_Id;
4764 begin
4765 Par := Scope (Curr_Scope);
4766 while (Present (Par)) and then Par /= Standard_Standard loop
4767 Install_Private_Declarations (Par);
4768 Par := Scope (Par);
4769 end loop;
4770 end;
4771 end if;
4772 end if;
4774 -- Restore use clauses. For a child unit, use clauses in the parents
4775 -- are restored when installing the context, so only those in inner
4776 -- scopes (and those local to the child unit itself) need to be
4777 -- installed explicitly.
4779 if Is_Child_Unit (Curr_Unit) and then Removed then
4780 for J in reverse 1 .. Num_Inner + 1 loop
4781 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4782 Use_Clauses (J);
4783 Install_Use_Clauses (Use_Clauses (J));
4784 end loop;
4786 else
4787 for J in reverse 1 .. Num_Scopes loop
4788 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4789 Use_Clauses (J);
4790 Install_Use_Clauses (Use_Clauses (J));
4791 end loop;
4792 end if;
4794 -- Restore status of instances. If one of them is a body, make its
4795 -- local entities visible again.
4797 declare
4798 E : Entity_Id;
4799 Inst : Entity_Id;
4801 begin
4802 for J in 1 .. N_Instances loop
4803 Inst := Instances (J);
4804 Set_Is_Generic_Instance (Inst, True);
4806 if In_Package_Body (Inst)
4807 or else Ekind_In (S, E_Procedure, E_Function)
4808 then
4809 E := First_Entity (Instances (J));
4810 while Present (E) loop
4811 Set_Is_Immediately_Visible (E);
4812 Next_Entity (E);
4813 end loop;
4814 end if;
4815 end loop;
4816 end;
4818 -- If generic unit is in current unit, current context is correct. Note
4819 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4820 -- enclosing scopes were removed.
4822 else
4823 Instantiate_Package_Body
4824 (Body_Info =>
4825 ((Inst_Node => N,
4826 Act_Decl => Act_Decl,
4827 Expander_Status => Expander_Active,
4828 Current_Sem_Unit => Current_Sem_Unit,
4829 Scope_Suppress => Scope_Suppress,
4830 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4831 Version => Ada_Version,
4832 Version_Pragma => Ada_Version_Pragma,
4833 Warnings => Save_Warnings,
4834 SPARK_Mode => SPARK_Mode,
4835 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4836 Inlined_Body => True);
4837 end if;
4838 end Inline_Instance_Body;
4840 -------------------------------------
4841 -- Analyze_Procedure_Instantiation --
4842 -------------------------------------
4844 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4845 begin
4846 Analyze_Subprogram_Instantiation (N, E_Procedure);
4847 end Analyze_Procedure_Instantiation;
4849 -----------------------------------
4850 -- Need_Subprogram_Instance_Body --
4851 -----------------------------------
4853 function Need_Subprogram_Instance_Body
4854 (N : Node_Id;
4855 Subp : Entity_Id) return Boolean
4857 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4858 -- Return True if E is an inlined subprogram, an inlined renaming or a
4859 -- subprogram nested in an inlined subprogram. The inlining machinery
4860 -- totally disregards nested subprograms since it considers that they
4861 -- will always be compiled if the parent is (see Inline.Is_Nested).
4863 ------------------------------------
4864 -- Is_Inlined_Or_Child_Of_Inlined --
4865 ------------------------------------
4867 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4868 Scop : Entity_Id;
4870 begin
4871 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4872 return True;
4873 end if;
4875 Scop := Scope (E);
4876 while Scop /= Standard_Standard loop
4877 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4878 return True;
4879 end if;
4881 Scop := Scope (Scop);
4882 end loop;
4884 return False;
4885 end Is_Inlined_Or_Child_Of_Inlined;
4887 begin
4888 -- Must be in the main unit or inlined (or child of inlined)
4890 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4892 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4894 and then (Operating_Mode = Generate_Code
4895 or else (Operating_Mode = Check_Semantics
4896 and then (ASIS_Mode or GNATprove_Mode)))
4898 -- The body is needed when generating code (full expansion), in ASIS
4899 -- mode for other tools, and in GNATprove mode (special expansion) for
4900 -- formal verification of the body itself.
4902 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4904 -- No point in inlining if ABE is inevitable
4906 and then not ABE_Is_Certain (N)
4908 -- Or if subprogram is eliminated
4910 and then not Is_Eliminated (Subp)
4911 then
4912 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4913 return True;
4915 -- Here if not inlined, or we ignore the inlining
4917 else
4918 return False;
4919 end if;
4920 end Need_Subprogram_Instance_Body;
4922 --------------------------------------
4923 -- Analyze_Subprogram_Instantiation --
4924 --------------------------------------
4926 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4927 -- must be replaced by gotos which jump to the end of the routine in order
4928 -- to restore the Ghost and SPARK modes.
4930 procedure Analyze_Subprogram_Instantiation
4931 (N : Node_Id;
4932 K : Entity_Kind)
4934 Loc : constant Source_Ptr := Sloc (N);
4935 Gen_Id : constant Node_Id := Name (N);
4937 Anon_Id : constant Entity_Id :=
4938 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4939 Chars => New_External_Name
4940 (Chars (Defining_Entity (N)), 'R'));
4942 Act_Decl_Id : Entity_Id;
4943 Act_Decl : Node_Id;
4944 Act_Spec : Node_Id;
4945 Act_Tree : Node_Id;
4947 Env_Installed : Boolean := False;
4948 Gen_Unit : Entity_Id;
4949 Gen_Decl : Node_Id;
4950 Pack_Id : Entity_Id;
4951 Parent_Installed : Boolean := False;
4953 Renaming_List : List_Id;
4954 -- The list of declarations that link formals and actuals of the
4955 -- instance. These are subtype declarations for formal types, and
4956 -- renaming declarations for other formals. The subprogram declaration
4957 -- for the instance is then appended to the list, and the last item on
4958 -- the list is the renaming declaration for the instance.
4960 procedure Analyze_Instance_And_Renamings;
4961 -- The instance must be analyzed in a context that includes the mappings
4962 -- of generic parameters into actuals. We create a package declaration
4963 -- for this purpose, and a subprogram with an internal name within the
4964 -- package. The subprogram instance is simply an alias for the internal
4965 -- subprogram, declared in the current scope.
4967 procedure Build_Subprogram_Renaming;
4968 -- If the subprogram is recursive, there are occurrences of the name of
4969 -- the generic within the body, which must resolve to the current
4970 -- instance. We add a renaming declaration after the declaration, which
4971 -- is available in the instance body, as well as in the analysis of
4972 -- aspects that appear in the generic. This renaming declaration is
4973 -- inserted after the instance declaration which it renames.
4975 ------------------------------------
4976 -- Analyze_Instance_And_Renamings --
4977 ------------------------------------
4979 procedure Analyze_Instance_And_Renamings is
4980 Def_Ent : constant Entity_Id := Defining_Entity (N);
4981 Pack_Decl : Node_Id;
4983 begin
4984 if Nkind (Parent (N)) = N_Compilation_Unit then
4986 -- For the case of a compilation unit, the container package has
4987 -- the same name as the instantiation, to insure that the binder
4988 -- calls the elaboration procedure with the right name. Copy the
4989 -- entity of the instance, which may have compilation level flags
4990 -- (e.g. Is_Child_Unit) set.
4992 Pack_Id := New_Copy (Def_Ent);
4994 else
4995 -- Otherwise we use the name of the instantiation concatenated
4996 -- with its source position to ensure uniqueness if there are
4997 -- several instantiations with the same name.
4999 Pack_Id :=
5000 Make_Defining_Identifier (Loc,
5001 Chars => New_External_Name
5002 (Related_Id => Chars (Def_Ent),
5003 Suffix => "GP",
5004 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5005 end if;
5007 Pack_Decl :=
5008 Make_Package_Declaration (Loc,
5009 Specification => Make_Package_Specification (Loc,
5010 Defining_Unit_Name => Pack_Id,
5011 Visible_Declarations => Renaming_List,
5012 End_Label => Empty));
5014 Set_Instance_Spec (N, Pack_Decl);
5015 Set_Is_Generic_Instance (Pack_Id);
5016 Set_Debug_Info_Needed (Pack_Id);
5018 -- Case of not a compilation unit
5020 if Nkind (Parent (N)) /= N_Compilation_Unit then
5021 Mark_Rewrite_Insertion (Pack_Decl);
5022 Insert_Before (N, Pack_Decl);
5023 Set_Has_Completion (Pack_Id);
5025 -- Case of an instantiation that is a compilation unit
5027 -- Place declaration on current node so context is complete for
5028 -- analysis (including nested instantiations), and for use in a
5029 -- context_clause (see Analyze_With_Clause).
5031 else
5032 Set_Unit (Parent (N), Pack_Decl);
5033 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5034 end if;
5036 Analyze (Pack_Decl);
5037 Check_Formal_Packages (Pack_Id);
5038 Set_Is_Generic_Instance (Pack_Id, False);
5040 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5041 -- above???
5043 -- Body of the enclosing package is supplied when instantiating the
5044 -- subprogram body, after semantic analysis is completed.
5046 if Nkind (Parent (N)) = N_Compilation_Unit then
5048 -- Remove package itself from visibility, so it does not
5049 -- conflict with subprogram.
5051 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5053 -- Set name and scope of internal subprogram so that the proper
5054 -- external name will be generated. The proper scope is the scope
5055 -- of the wrapper package. We need to generate debugging info for
5056 -- the internal subprogram, so set flag accordingly.
5058 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5059 Set_Scope (Anon_Id, Scope (Pack_Id));
5061 -- Mark wrapper package as referenced, to avoid spurious warnings
5062 -- if the instantiation appears in various with_ clauses of
5063 -- subunits of the main unit.
5065 Set_Referenced (Pack_Id);
5066 end if;
5068 Set_Is_Generic_Instance (Anon_Id);
5069 Set_Debug_Info_Needed (Anon_Id);
5070 Act_Decl_Id := New_Copy (Anon_Id);
5072 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5073 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5074 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5076 -- Subprogram instance comes from source only if generic does
5078 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5080 -- If the instance is a child unit, mark the Id accordingly. Mark
5081 -- the anonymous entity as well, which is the real subprogram and
5082 -- which is used when the instance appears in a context clause.
5083 -- Similarly, propagate the Is_Eliminated flag to handle properly
5084 -- nested eliminated subprograms.
5086 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5087 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5088 New_Overloaded_Entity (Act_Decl_Id);
5089 Check_Eliminated (Act_Decl_Id);
5090 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5092 -- In compilation unit case, kill elaboration checks on the
5093 -- instantiation, since they are never needed -- the body is
5094 -- instantiated at the same point as the spec.
5096 if Nkind (Parent (N)) = N_Compilation_Unit then
5097 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5098 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5099 Set_Is_Compilation_Unit (Anon_Id);
5101 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5102 end if;
5104 -- The instance is not a freezing point for the new subprogram.
5105 -- The anonymous subprogram may have a freeze node, created for
5106 -- some delayed aspects. This freeze node must not be inherited
5107 -- by the visible subprogram entity.
5109 Set_Is_Frozen (Act_Decl_Id, False);
5110 Set_Freeze_Node (Act_Decl_Id, Empty);
5112 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5113 Valid_Operator_Definition (Act_Decl_Id);
5114 end if;
5116 Set_Alias (Act_Decl_Id, Anon_Id);
5117 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5118 Set_Has_Completion (Act_Decl_Id);
5119 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5121 if Nkind (Parent (N)) = N_Compilation_Unit then
5122 Set_Body_Required (Parent (N), False);
5123 end if;
5124 end Analyze_Instance_And_Renamings;
5126 -------------------------------
5127 -- Build_Subprogram_Renaming --
5128 -------------------------------
5130 procedure Build_Subprogram_Renaming is
5131 Renaming_Decl : Node_Id;
5132 Unit_Renaming : Node_Id;
5134 begin
5135 Unit_Renaming :=
5136 Make_Subprogram_Renaming_Declaration (Loc,
5137 Specification =>
5138 Copy_Generic_Node
5139 (Specification (Original_Node (Gen_Decl)),
5140 Empty,
5141 Instantiating => True),
5142 Name => New_Occurrence_Of (Anon_Id, Loc));
5144 -- The generic may be a a child unit. The renaming needs an
5145 -- identifier with the proper name.
5147 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5148 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5150 -- If there is a formal subprogram with the same name as the unit
5151 -- itself, do not add this renaming declaration, to prevent
5152 -- ambiguities when there is a call with that name in the body.
5153 -- This is a partial and ugly fix for one ACATS test. ???
5155 Renaming_Decl := First (Renaming_List);
5156 while Present (Renaming_Decl) loop
5157 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5158 and then
5159 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5160 then
5161 exit;
5162 end if;
5164 Next (Renaming_Decl);
5165 end loop;
5167 if No (Renaming_Decl) then
5168 Append (Unit_Renaming, Renaming_List);
5169 end if;
5170 end Build_Subprogram_Renaming;
5172 -- Local variables
5174 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5175 Saved_ISMP : constant Boolean :=
5176 Ignore_SPARK_Mode_Pragmas_In_Instance;
5177 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5178 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5179 -- Save the Ghost and SPARK mode-related data to restore on exit
5181 Vis_Prims_List : Elist_Id := No_Elist;
5182 -- List of primitives made temporarily visible in the instantiation
5183 -- to match the visibility of the formal type
5185 -- Start of processing for Analyze_Subprogram_Instantiation
5187 begin
5188 Check_SPARK_05_Restriction ("generic is not allowed", N);
5190 -- Very first thing: check for special Text_IO unit in case we are
5191 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5192 -- such an instantiation is bogus (these are packages, not subprograms),
5193 -- but we get a better error message if we do this.
5195 Check_Text_IO_Special_Unit (Gen_Id);
5197 -- Make node global for error reporting
5199 Instantiation_Node := N;
5201 -- For package instantiations we turn off style checks, because they
5202 -- will have been emitted in the generic. For subprogram instantiations
5203 -- we want to apply at least the check on overriding indicators so we
5204 -- do not modify the style check status.
5206 -- The renaming declarations for the actuals do not come from source and
5207 -- will not generate spurious warnings.
5209 Preanalyze_Actuals (N);
5211 Init_Env;
5212 Env_Installed := True;
5213 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5214 Gen_Unit := Entity (Gen_Id);
5216 -- A subprogram instantiation is Ghost when it is subject to pragma
5217 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5218 -- that any nodes generated during analysis and expansion are marked as
5219 -- Ghost.
5221 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5223 Generate_Reference (Gen_Unit, Gen_Id);
5225 if Nkind (Gen_Id) = N_Identifier
5226 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5227 then
5228 Error_Msg_NE
5229 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5230 end if;
5232 if Etype (Gen_Unit) = Any_Type then
5233 Restore_Env;
5234 goto Leave;
5235 end if;
5237 -- Verify that it is a generic subprogram of the right kind, and that
5238 -- it does not lead to a circular instantiation.
5240 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5241 Error_Msg_NE
5242 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5244 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5245 Error_Msg_NE
5246 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5248 elsif In_Open_Scopes (Gen_Unit) then
5249 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5251 else
5252 Set_Entity (Gen_Id, Gen_Unit);
5253 Set_Is_Instantiated (Gen_Unit);
5255 if In_Extended_Main_Source_Unit (N) then
5256 Generate_Reference (Gen_Unit, N);
5257 end if;
5259 -- If renaming, get original unit
5261 if Present (Renamed_Object (Gen_Unit))
5262 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5263 E_Generic_Function)
5264 then
5265 Gen_Unit := Renamed_Object (Gen_Unit);
5266 Set_Is_Instantiated (Gen_Unit);
5267 Generate_Reference (Gen_Unit, N);
5268 end if;
5270 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5271 Error_Msg_Node_2 := Current_Scope;
5272 Error_Msg_NE
5273 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5274 Circularity_Detected := True;
5275 Restore_Hidden_Primitives (Vis_Prims_List);
5276 goto Leave;
5277 end if;
5279 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5281 -- Initialize renamings map, for error checking
5283 Generic_Renamings.Set_Last (0);
5284 Generic_Renamings_HTable.Reset;
5286 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5288 -- Copy original generic tree, to produce text for instantiation
5290 Act_Tree :=
5291 Copy_Generic_Node
5292 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5294 -- Inherit overriding indicator from instance node
5296 Act_Spec := Specification (Act_Tree);
5297 Set_Must_Override (Act_Spec, Must_Override (N));
5298 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5300 Renaming_List :=
5301 Analyze_Associations
5302 (I_Node => N,
5303 Formals => Generic_Formal_Declarations (Act_Tree),
5304 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5306 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5308 -- The subprogram itself cannot contain a nested instance, so the
5309 -- current parent is left empty.
5311 Set_Instance_Env (Gen_Unit, Empty);
5313 -- Build the subprogram declaration, which does not appear in the
5314 -- generic template, and give it a sloc consistent with that of the
5315 -- template.
5317 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5318 Set_Generic_Parent (Act_Spec, Gen_Unit);
5319 Act_Decl :=
5320 Make_Subprogram_Declaration (Sloc (Act_Spec),
5321 Specification => Act_Spec);
5323 -- The aspects have been copied previously, but they have to be
5324 -- linked explicitly to the new subprogram declaration. Explicit
5325 -- pre/postconditions on the instance are analyzed below, in a
5326 -- separate step.
5328 Move_Aspects (Act_Tree, To => Act_Decl);
5329 Set_Categorization_From_Pragmas (Act_Decl);
5331 if Parent_Installed then
5332 Hide_Current_Scope;
5333 end if;
5335 Append (Act_Decl, Renaming_List);
5337 -- Contract-related source pragmas that follow a generic subprogram
5338 -- must be instantiated explicitly because they are not part of the
5339 -- subprogram template.
5341 Instantiate_Subprogram_Contract
5342 (Original_Node (Gen_Decl), Renaming_List);
5344 Build_Subprogram_Renaming;
5345 Analyze_Instance_And_Renamings;
5347 -- If the generic is marked Import (Intrinsic), then so is the
5348 -- instance. This indicates that there is no body to instantiate. If
5349 -- generic is marked inline, so it the instance, and the anonymous
5350 -- subprogram it renames. If inlined, or else if inlining is enabled
5351 -- for the compilation, we generate the instance body even if it is
5352 -- not within the main unit.
5354 if Is_Intrinsic_Subprogram (Gen_Unit) then
5355 Set_Is_Intrinsic_Subprogram (Anon_Id);
5356 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5358 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5359 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5360 end if;
5361 end if;
5363 -- Inherit convention from generic unit. Intrinsic convention, as for
5364 -- an instance of unchecked conversion, is not inherited because an
5365 -- explicit Ada instance has been created.
5367 if Has_Convention_Pragma (Gen_Unit)
5368 and then Convention (Gen_Unit) /= Convention_Intrinsic
5369 then
5370 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5371 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5372 end if;
5374 Generate_Definition (Act_Decl_Id);
5376 -- Inherit all inlining-related flags which apply to the generic in
5377 -- the subprogram and its declaration.
5379 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5380 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5382 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5383 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5385 Set_Has_Pragma_Inline_Always
5386 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5387 Set_Has_Pragma_Inline_Always
5388 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5390 -- If the context of the instance is subject to SPARK_Mode "off" or
5391 -- the annotation is altogether missing, set the global flag which
5392 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5393 -- the instance.
5395 if SPARK_Mode /= On then
5396 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5398 -- Mark both the instance spec and the anonymous package in case
5399 -- the body is instantiated at a later pass. This preserves the
5400 -- original context in effect for the body.
5402 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5403 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5404 end if;
5406 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5407 Check_Elab_Instantiation (N);
5408 end if;
5410 if Is_Dispatching_Operation (Act_Decl_Id)
5411 and then Ada_Version >= Ada_2005
5412 then
5413 declare
5414 Formal : Entity_Id;
5416 begin
5417 Formal := First_Formal (Act_Decl_Id);
5418 while Present (Formal) loop
5419 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5420 and then Is_Controlling_Formal (Formal)
5421 and then not Can_Never_Be_Null (Formal)
5422 then
5423 Error_Msg_NE
5424 ("access parameter& is controlling,", N, Formal);
5425 Error_Msg_NE
5426 ("\corresponding parameter of & must be explicitly "
5427 & "null-excluding", N, Gen_Id);
5428 end if;
5430 Next_Formal (Formal);
5431 end loop;
5432 end;
5433 end if;
5435 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5437 Validate_Categorization_Dependency (N, Act_Decl_Id);
5439 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5440 Inherit_Context (Gen_Decl, N);
5442 Restore_Private_Views (Pack_Id, False);
5444 -- If the context requires a full instantiation, mark node for
5445 -- subsequent construction of the body.
5447 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5448 Check_Forward_Instantiation (Gen_Decl);
5450 -- The wrapper package is always delayed, because it does not
5451 -- constitute a freeze point, but to insure that the freeze node
5452 -- is placed properly, it is created directly when instantiating
5453 -- the body (otherwise the freeze node might appear to early for
5454 -- nested instantiations). For ASIS purposes, indicate that the
5455 -- wrapper package has replaced the instantiation node.
5457 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5458 Rewrite (N, Unit (Parent (N)));
5459 Set_Unit (Parent (N), N);
5460 end if;
5462 -- Replace instance node for library-level instantiations of
5463 -- intrinsic subprograms, for ASIS use.
5465 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5466 Rewrite (N, Unit (Parent (N)));
5467 Set_Unit (Parent (N), N);
5468 end if;
5470 if Parent_Installed then
5471 Remove_Parent;
5472 end if;
5474 Restore_Hidden_Primitives (Vis_Prims_List);
5475 Restore_Env;
5476 Env_Installed := False;
5477 Generic_Renamings.Set_Last (0);
5478 Generic_Renamings_HTable.Reset;
5479 end if;
5481 <<Leave>>
5482 if Has_Aspects (N) then
5483 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5484 end if;
5486 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5487 Restore_Ghost_Mode (Saved_GM);
5488 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5490 exception
5491 when Instantiation_Error =>
5492 if Parent_Installed then
5493 Remove_Parent;
5494 end if;
5496 if Env_Installed then
5497 Restore_Env;
5498 end if;
5500 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5501 Restore_Ghost_Mode (Saved_GM);
5502 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5503 end Analyze_Subprogram_Instantiation;
5505 -------------------------
5506 -- Get_Associated_Node --
5507 -------------------------
5509 function Get_Associated_Node (N : Node_Id) return Node_Id is
5510 Assoc : Node_Id;
5512 begin
5513 Assoc := Associated_Node (N);
5515 if Nkind (Assoc) /= Nkind (N) then
5516 return Assoc;
5518 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5519 return Assoc;
5521 else
5522 -- If the node is part of an inner generic, it may itself have been
5523 -- remapped into a further generic copy. Associated_Node is otherwise
5524 -- used for the entity of the node, and will be of a different node
5525 -- kind, or else N has been rewritten as a literal or function call.
5527 while Present (Associated_Node (Assoc))
5528 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5529 loop
5530 Assoc := Associated_Node (Assoc);
5531 end loop;
5533 -- Follow and additional link in case the final node was rewritten.
5534 -- This can only happen with nested generic units.
5536 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5537 and then Present (Associated_Node (Assoc))
5538 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5539 N_Explicit_Dereference,
5540 N_Integer_Literal,
5541 N_Real_Literal,
5542 N_String_Literal))
5543 then
5544 Assoc := Associated_Node (Assoc);
5545 end if;
5547 -- An additional special case: an unconstrained type in an object
5548 -- declaration may have been rewritten as a local subtype constrained
5549 -- by the expression in the declaration. We need to recover the
5550 -- original entity which may be global.
5552 if Present (Original_Node (Assoc))
5553 and then Nkind (Parent (N)) = N_Object_Declaration
5554 then
5555 Assoc := Original_Node (Assoc);
5556 end if;
5558 return Assoc;
5559 end if;
5560 end Get_Associated_Node;
5562 ----------------------------
5563 -- Build_Function_Wrapper --
5564 ----------------------------
5566 function Build_Function_Wrapper
5567 (Formal_Subp : Entity_Id;
5568 Actual_Subp : Entity_Id) return Node_Id
5570 Loc : constant Source_Ptr := Sloc (Current_Scope);
5571 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5572 Actuals : List_Id;
5573 Decl : Node_Id;
5574 Func_Name : Node_Id;
5575 Func : Entity_Id;
5576 Parm_Type : Node_Id;
5577 Profile : List_Id := New_List;
5578 Spec : Node_Id;
5579 Act_F : Entity_Id;
5580 Form_F : Entity_Id;
5581 New_F : Entity_Id;
5583 begin
5584 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5586 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5587 Set_Ekind (Func, E_Function);
5588 Set_Is_Generic_Actual_Subprogram (Func);
5590 Actuals := New_List;
5591 Profile := New_List;
5593 Act_F := First_Formal (Actual_Subp);
5594 Form_F := First_Formal (Formal_Subp);
5595 while Present (Form_F) loop
5597 -- Create new formal for profile of wrapper, and add a reference
5598 -- to it in the list of actuals for the enclosing call. The name
5599 -- must be that of the formal in the formal subprogram, because
5600 -- calls to it in the generic body may use named associations.
5602 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5604 Parm_Type :=
5605 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5607 Append_To (Profile,
5608 Make_Parameter_Specification (Loc,
5609 Defining_Identifier => New_F,
5610 Parameter_Type => Parm_Type));
5612 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5613 Next_Formal (Form_F);
5615 if Present (Act_F) then
5616 Next_Formal (Act_F);
5617 end if;
5618 end loop;
5620 Spec :=
5621 Make_Function_Specification (Loc,
5622 Defining_Unit_Name => Func,
5623 Parameter_Specifications => Profile,
5624 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5626 Decl :=
5627 Make_Expression_Function (Loc,
5628 Specification => Spec,
5629 Expression =>
5630 Make_Function_Call (Loc,
5631 Name => Func_Name,
5632 Parameter_Associations => Actuals));
5634 return Decl;
5635 end Build_Function_Wrapper;
5637 ----------------------------
5638 -- Build_Operator_Wrapper --
5639 ----------------------------
5641 function Build_Operator_Wrapper
5642 (Formal_Subp : Entity_Id;
5643 Actual_Subp : Entity_Id) return Node_Id
5645 Loc : constant Source_Ptr := Sloc (Current_Scope);
5646 Ret_Type : constant Entity_Id :=
5647 Get_Instance_Of (Etype (Formal_Subp));
5648 Op_Type : constant Entity_Id :=
5649 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5650 Is_Binary : constant Boolean :=
5651 Present (Next_Formal (First_Formal (Formal_Subp)));
5653 Decl : Node_Id;
5654 Expr : Node_Id;
5655 pragma Warnings (Off, Expr);
5656 F1, F2 : Entity_Id;
5657 Func : Entity_Id;
5658 Op_Name : Name_Id;
5659 Spec : Node_Id;
5660 L, R : Node_Id;
5662 begin
5663 Op_Name := Chars (Actual_Subp);
5665 -- Create entities for wrapper function and its formals
5667 F1 := Make_Temporary (Loc, 'A');
5668 F2 := Make_Temporary (Loc, 'B');
5669 L := New_Occurrence_Of (F1, Loc);
5670 R := New_Occurrence_Of (F2, Loc);
5672 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5673 Set_Ekind (Func, E_Function);
5674 Set_Is_Generic_Actual_Subprogram (Func);
5676 Spec :=
5677 Make_Function_Specification (Loc,
5678 Defining_Unit_Name => Func,
5679 Parameter_Specifications => New_List (
5680 Make_Parameter_Specification (Loc,
5681 Defining_Identifier => F1,
5682 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5683 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5685 if Is_Binary then
5686 Append_To (Parameter_Specifications (Spec),
5687 Make_Parameter_Specification (Loc,
5688 Defining_Identifier => F2,
5689 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5690 end if;
5692 -- Build expression as a function call, or as an operator node
5693 -- that corresponds to the name of the actual, starting with
5694 -- binary operators.
5696 if Op_Name not in Any_Operator_Name then
5697 Expr :=
5698 Make_Function_Call (Loc,
5699 Name =>
5700 New_Occurrence_Of (Actual_Subp, Loc),
5701 Parameter_Associations => New_List (L));
5703 if Is_Binary then
5704 Append_To (Parameter_Associations (Expr), R);
5705 end if;
5707 -- Binary operators
5709 elsif Is_Binary then
5710 if Op_Name = Name_Op_And then
5711 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5712 elsif Op_Name = Name_Op_Or then
5713 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5714 elsif Op_Name = Name_Op_Xor then
5715 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5716 elsif Op_Name = Name_Op_Eq then
5717 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5718 elsif Op_Name = Name_Op_Ne then
5719 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5720 elsif Op_Name = Name_Op_Le then
5721 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5722 elsif Op_Name = Name_Op_Gt then
5723 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5724 elsif Op_Name = Name_Op_Ge then
5725 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5726 elsif Op_Name = Name_Op_Lt then
5727 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5728 elsif Op_Name = Name_Op_Add then
5729 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5730 elsif Op_Name = Name_Op_Subtract then
5731 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5732 elsif Op_Name = Name_Op_Concat then
5733 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5734 elsif Op_Name = Name_Op_Multiply then
5735 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5736 elsif Op_Name = Name_Op_Divide then
5737 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5738 elsif Op_Name = Name_Op_Mod then
5739 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5740 elsif Op_Name = Name_Op_Rem then
5741 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5742 elsif Op_Name = Name_Op_Expon then
5743 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5744 end if;
5746 -- Unary operators
5748 else
5749 if Op_Name = Name_Op_Add then
5750 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5751 elsif Op_Name = Name_Op_Subtract then
5752 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5753 elsif Op_Name = Name_Op_Abs then
5754 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5755 elsif Op_Name = Name_Op_Not then
5756 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5757 end if;
5758 end if;
5760 Decl :=
5761 Make_Expression_Function (Loc,
5762 Specification => Spec,
5763 Expression => Expr);
5765 return Decl;
5766 end Build_Operator_Wrapper;
5768 -------------------------------------------
5769 -- Build_Instance_Compilation_Unit_Nodes --
5770 -------------------------------------------
5772 procedure Build_Instance_Compilation_Unit_Nodes
5773 (N : Node_Id;
5774 Act_Body : Node_Id;
5775 Act_Decl : Node_Id)
5777 Decl_Cunit : Node_Id;
5778 Body_Cunit : Node_Id;
5779 Citem : Node_Id;
5780 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5781 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5783 begin
5784 -- A new compilation unit node is built for the instance declaration
5786 Decl_Cunit :=
5787 Make_Compilation_Unit (Sloc (N),
5788 Context_Items => Empty_List,
5789 Unit => Act_Decl,
5790 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5792 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5794 -- The new compilation unit is linked to its body, but both share the
5795 -- same file, so we do not set Body_Required on the new unit so as not
5796 -- to create a spurious dependency on a non-existent body in the ali.
5797 -- This simplifies CodePeer unit traversal.
5799 -- We use the original instantiation compilation unit as the resulting
5800 -- compilation unit of the instance, since this is the main unit.
5802 Rewrite (N, Act_Body);
5804 -- Propagate the aspect specifications from the package body template to
5805 -- the instantiated version of the package body.
5807 if Has_Aspects (Act_Body) then
5808 Set_Aspect_Specifications
5809 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5810 end if;
5812 Body_Cunit := Parent (N);
5814 -- The two compilation unit nodes are linked by the Library_Unit field
5816 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5817 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5819 -- Preserve the private nature of the package if needed
5821 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5823 -- If the instance is not the main unit, its context, categorization
5824 -- and elaboration entity are not relevant to the compilation.
5826 if Body_Cunit /= Cunit (Main_Unit) then
5827 Make_Instance_Unit (Body_Cunit, In_Main => False);
5828 return;
5829 end if;
5831 -- The context clause items on the instantiation, which are now attached
5832 -- to the body compilation unit (since the body overwrote the original
5833 -- instantiation node), semantically belong on the spec, so copy them
5834 -- there. It's harmless to leave them on the body as well. In fact one
5835 -- could argue that they belong in both places.
5837 Citem := First (Context_Items (Body_Cunit));
5838 while Present (Citem) loop
5839 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5840 Next (Citem);
5841 end loop;
5843 -- Propagate categorization flags on packages, so that they appear in
5844 -- the ali file for the spec of the unit.
5846 if Ekind (New_Main) = E_Package then
5847 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5848 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5849 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5850 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5851 Set_Is_Remote_Call_Interface
5852 (Old_Main, Is_Remote_Call_Interface (New_Main));
5853 end if;
5855 -- Make entry in Units table, so that binder can generate call to
5856 -- elaboration procedure for body, if any.
5858 Make_Instance_Unit (Body_Cunit, In_Main => True);
5859 Main_Unit_Entity := New_Main;
5860 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5862 -- Build elaboration entity, since the instance may certainly generate
5863 -- elaboration code requiring a flag for protection.
5865 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5866 end Build_Instance_Compilation_Unit_Nodes;
5868 -----------------------------
5869 -- Check_Access_Definition --
5870 -----------------------------
5872 procedure Check_Access_Definition (N : Node_Id) is
5873 begin
5874 pragma Assert
5875 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5876 null;
5877 end Check_Access_Definition;
5879 -----------------------------------
5880 -- Check_Formal_Package_Instance --
5881 -----------------------------------
5883 -- If the formal has specific parameters, they must match those of the
5884 -- actual. Both of them are instances, and the renaming declarations for
5885 -- their formal parameters appear in the same order in both. The analyzed
5886 -- formal has been analyzed in the context of the current instance.
5888 procedure Check_Formal_Package_Instance
5889 (Formal_Pack : Entity_Id;
5890 Actual_Pack : Entity_Id)
5892 E1 : Entity_Id := First_Entity (Actual_Pack);
5893 E2 : Entity_Id := First_Entity (Formal_Pack);
5894 Prev_E1 : Entity_Id;
5896 Expr1 : Node_Id;
5897 Expr2 : Node_Id;
5899 procedure Check_Mismatch (B : Boolean);
5900 -- Common error routine for mismatch between the parameters of the
5901 -- actual instance and those of the formal package.
5903 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5904 -- The formal may come from a nested formal package, and the actual may
5905 -- have been constant-folded. To determine whether the two denote the
5906 -- same entity we may have to traverse several definitions to recover
5907 -- the ultimate entity that they refer to.
5909 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5910 -- The formal and the actual must be identical, but if both are
5911 -- given by attributes they end up renaming different generated bodies,
5912 -- and we must verify that the attributes themselves match.
5914 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5915 -- Similarly, if the formal comes from a nested formal package, the
5916 -- actual may designate the formal through multiple renamings, which
5917 -- have to be followed to determine the original variable in question.
5919 --------------------
5920 -- Check_Mismatch --
5921 --------------------
5923 procedure Check_Mismatch (B : Boolean) is
5924 -- A Formal_Type_Declaration for a derived private type is rewritten
5925 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5926 -- which is why we examine the original node.
5928 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5930 begin
5931 if Kind = N_Formal_Type_Declaration then
5932 return;
5934 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5935 N_Formal_Package_Declaration)
5936 or else Kind in N_Formal_Subprogram_Declaration
5937 then
5938 null;
5940 -- Ada 2012: If both formal and actual are incomplete types they
5941 -- are conformant.
5943 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5944 null;
5946 elsif B then
5947 Error_Msg_NE
5948 ("actual for & in actual instance does not match formal",
5949 Parent (Actual_Pack), E1);
5950 end if;
5951 end Check_Mismatch;
5953 --------------------------------
5954 -- Same_Instantiated_Constant --
5955 --------------------------------
5957 function Same_Instantiated_Constant
5958 (E1, E2 : Entity_Id) return Boolean
5960 Ent : Entity_Id;
5962 begin
5963 Ent := E2;
5964 while Present (Ent) loop
5965 if E1 = Ent then
5966 return True;
5968 elsif Ekind (Ent) /= E_Constant then
5969 return False;
5971 elsif Is_Entity_Name (Constant_Value (Ent)) then
5972 if Entity (Constant_Value (Ent)) = E1 then
5973 return True;
5974 else
5975 Ent := Entity (Constant_Value (Ent));
5976 end if;
5978 -- The actual may be a constant that has been folded. Recover
5979 -- original name.
5981 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5982 Ent := Entity (Original_Node (Constant_Value (Ent)));
5984 else
5985 return False;
5986 end if;
5987 end loop;
5989 return False;
5990 end Same_Instantiated_Constant;
5992 --------------------------------
5993 -- Same_Instantiated_Function --
5994 --------------------------------
5996 function Same_Instantiated_Function
5997 (E1, E2 : Entity_Id) return Boolean
5999 U1, U2 : Node_Id;
6000 begin
6001 if Alias (E1) = Alias (E2) then
6002 return True;
6004 elsif Present (Alias (E2)) then
6005 U1 := Original_Node (Unit_Declaration_Node (E1));
6006 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6008 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6009 and then Nkind (Name (U1)) = N_Attribute_Reference
6011 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6012 and then Nkind (Name (U2)) = N_Attribute_Reference
6014 and then
6015 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6016 else
6017 return False;
6018 end if;
6019 end Same_Instantiated_Function;
6021 --------------------------------
6022 -- Same_Instantiated_Variable --
6023 --------------------------------
6025 function Same_Instantiated_Variable
6026 (E1, E2 : Entity_Id) return Boolean
6028 function Original_Entity (E : Entity_Id) return Entity_Id;
6029 -- Follow chain of renamings to the ultimate ancestor
6031 ---------------------
6032 -- Original_Entity --
6033 ---------------------
6035 function Original_Entity (E : Entity_Id) return Entity_Id is
6036 Orig : Entity_Id;
6038 begin
6039 Orig := E;
6040 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6041 and then Present (Renamed_Object (Orig))
6042 and then Is_Entity_Name (Renamed_Object (Orig))
6043 loop
6044 Orig := Entity (Renamed_Object (Orig));
6045 end loop;
6047 return Orig;
6048 end Original_Entity;
6050 -- Start of processing for Same_Instantiated_Variable
6052 begin
6053 return Ekind (E1) = Ekind (E2)
6054 and then Original_Entity (E1) = Original_Entity (E2);
6055 end Same_Instantiated_Variable;
6057 -- Start of processing for Check_Formal_Package_Instance
6059 begin
6060 Prev_E1 := E1;
6061 while Present (E1) and then Present (E2) loop
6062 exit when Ekind (E1) = E_Package
6063 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6065 -- If the formal is the renaming of the formal package, this
6066 -- is the end of its formal part, which may occur before the
6067 -- end of the formal part in the actual in the presence of
6068 -- defaulted parameters in the formal package.
6070 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6071 and then Renamed_Entity (E2) = Scope (E2);
6073 -- The analysis of the actual may generate additional internal
6074 -- entities. If the formal is defaulted, there is no corresponding
6075 -- analysis and the internal entities must be skipped, until we
6076 -- find corresponding entities again.
6078 if Comes_From_Source (E2)
6079 and then not Comes_From_Source (E1)
6080 and then Chars (E1) /= Chars (E2)
6081 then
6082 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6083 Next_Entity (E1);
6084 end loop;
6085 end if;
6087 if No (E1) then
6088 return;
6090 -- Entities may be declared without full declaration, such as
6091 -- itypes and predefined operators (concatenation for arrays, eg).
6092 -- Skip it and keep the formal entity to find a later match for it.
6094 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6095 E1 := Prev_E1;
6096 goto Next_E;
6098 -- If the formal entity comes from a formal declaration, it was
6099 -- defaulted in the formal package, and no check is needed on it.
6101 elsif Nkind_In (Original_Node (Parent (E2)),
6102 N_Formal_Object_Declaration,
6103 N_Formal_Type_Declaration)
6104 then
6105 -- If the formal is a tagged type the corresponding class-wide
6106 -- type has been generated as well, and it must be skipped.
6108 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6109 Next_Entity (E2);
6110 end if;
6112 goto Next_E;
6114 -- Ditto for defaulted formal subprograms.
6116 elsif Is_Overloadable (E1)
6117 and then Nkind (Unit_Declaration_Node (E2)) in
6118 N_Formal_Subprogram_Declaration
6119 then
6120 goto Next_E;
6122 elsif Is_Type (E1) then
6124 -- Subtypes must statically match. E1, E2 are the local entities
6125 -- that are subtypes of the actuals. Itypes generated for other
6126 -- parameters need not be checked, the check will be performed
6127 -- on the parameters themselves.
6129 -- If E2 is a formal type declaration, it is a defaulted parameter
6130 -- and needs no checking.
6132 if not Is_Itype (E1) and then not Is_Itype (E2) then
6133 Check_Mismatch
6134 (not Is_Type (E2)
6135 or else Etype (E1) /= Etype (E2)
6136 or else not Subtypes_Statically_Match (E1, E2));
6137 end if;
6139 elsif Ekind (E1) = E_Constant then
6141 -- IN parameters must denote the same static value, or the same
6142 -- constant, or the literal null.
6144 Expr1 := Expression (Parent (E1));
6146 if Ekind (E2) /= E_Constant then
6147 Check_Mismatch (True);
6148 goto Next_E;
6149 else
6150 Expr2 := Expression (Parent (E2));
6151 end if;
6153 if Is_OK_Static_Expression (Expr1) then
6154 if not Is_OK_Static_Expression (Expr2) then
6155 Check_Mismatch (True);
6157 elsif Is_Discrete_Type (Etype (E1)) then
6158 declare
6159 V1 : constant Uint := Expr_Value (Expr1);
6160 V2 : constant Uint := Expr_Value (Expr2);
6161 begin
6162 Check_Mismatch (V1 /= V2);
6163 end;
6165 elsif Is_Real_Type (Etype (E1)) then
6166 declare
6167 V1 : constant Ureal := Expr_Value_R (Expr1);
6168 V2 : constant Ureal := Expr_Value_R (Expr2);
6169 begin
6170 Check_Mismatch (V1 /= V2);
6171 end;
6173 elsif Is_String_Type (Etype (E1))
6174 and then Nkind (Expr1) = N_String_Literal
6175 then
6176 if Nkind (Expr2) /= N_String_Literal then
6177 Check_Mismatch (True);
6178 else
6179 Check_Mismatch
6180 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6181 end if;
6182 end if;
6184 elsif Is_Entity_Name (Expr1) then
6185 if Is_Entity_Name (Expr2) then
6186 if Entity (Expr1) = Entity (Expr2) then
6187 null;
6188 else
6189 Check_Mismatch
6190 (not Same_Instantiated_Constant
6191 (Entity (Expr1), Entity (Expr2)));
6192 end if;
6194 else
6195 Check_Mismatch (True);
6196 end if;
6198 elsif Is_Entity_Name (Original_Node (Expr1))
6199 and then Is_Entity_Name (Expr2)
6200 and then Same_Instantiated_Constant
6201 (Entity (Original_Node (Expr1)), Entity (Expr2))
6202 then
6203 null;
6205 elsif Nkind (Expr1) = N_Null then
6206 Check_Mismatch (Nkind (Expr1) /= N_Null);
6208 else
6209 Check_Mismatch (True);
6210 end if;
6212 elsif Ekind (E1) = E_Variable then
6213 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6215 elsif Ekind (E1) = E_Package then
6216 Check_Mismatch
6217 (Ekind (E1) /= Ekind (E2)
6218 or else Renamed_Object (E1) /= Renamed_Object (E2));
6220 elsif Is_Overloadable (E1) then
6222 -- Verify that the actual subprograms match. Note that actuals
6223 -- that are attributes are rewritten as subprograms. If the
6224 -- subprogram in the formal package is defaulted, no check is
6225 -- needed. Note that this can only happen in Ada 2005 when the
6226 -- formal package can be partially parameterized.
6228 if Nkind (Unit_Declaration_Node (E1)) =
6229 N_Subprogram_Renaming_Declaration
6230 and then From_Default (Unit_Declaration_Node (E1))
6231 then
6232 null;
6234 -- If the formal package has an "others" box association that
6235 -- covers this formal, there is no need for a check either.
6237 elsif Nkind (Unit_Declaration_Node (E2)) in
6238 N_Formal_Subprogram_Declaration
6239 and then Box_Present (Unit_Declaration_Node (E2))
6240 then
6241 null;
6243 -- No check needed if subprogram is a defaulted null procedure
6245 elsif No (Alias (E2))
6246 and then Ekind (E2) = E_Procedure
6247 and then
6248 Null_Present (Specification (Unit_Declaration_Node (E2)))
6249 then
6250 null;
6252 -- Otherwise the actual in the formal and the actual in the
6253 -- instantiation of the formal must match, up to renamings.
6255 else
6256 Check_Mismatch
6257 (Ekind (E2) /= Ekind (E1)
6258 or else not Same_Instantiated_Function (E1, E2));
6259 end if;
6261 else
6262 raise Program_Error;
6263 end if;
6265 <<Next_E>>
6266 Prev_E1 := E1;
6267 Next_Entity (E1);
6268 Next_Entity (E2);
6269 end loop;
6270 end Check_Formal_Package_Instance;
6272 ---------------------------
6273 -- Check_Formal_Packages --
6274 ---------------------------
6276 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6277 E : Entity_Id;
6278 Formal_P : Entity_Id;
6279 Formal_Decl : Node_Id;
6281 begin
6282 -- Iterate through the declarations in the instance, looking for package
6283 -- renaming declarations that denote instances of formal packages. Stop
6284 -- when we find the renaming of the current package itself. The
6285 -- declaration for a formal package without a box is followed by an
6286 -- internal entity that repeats the instantiation.
6288 E := First_Entity (P_Id);
6289 while Present (E) loop
6290 if Ekind (E) = E_Package then
6291 if Renamed_Object (E) = P_Id then
6292 exit;
6294 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6295 null;
6297 else
6298 Formal_Decl := Parent (Associated_Formal_Package (E));
6300 -- Nothing to check if the formal has a box or an others_clause
6301 -- (necessarily with a box).
6303 if Box_Present (Formal_Decl) then
6304 null;
6306 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6307 N_Others_Choice
6308 then
6309 -- The internal validating package was generated but formal
6310 -- and instance are known to be compatible.
6312 Formal_P := Next_Entity (E);
6313 Remove (Unit_Declaration_Node (Formal_P));
6315 else
6316 Formal_P := Next_Entity (E);
6317 Check_Formal_Package_Instance (Formal_P, E);
6319 -- After checking, remove the internal validating package.
6320 -- It is only needed for semantic checks, and as it may
6321 -- contain generic formal declarations it should not reach
6322 -- gigi.
6324 Remove (Unit_Declaration_Node (Formal_P));
6325 end if;
6326 end if;
6327 end if;
6329 Next_Entity (E);
6330 end loop;
6331 end Check_Formal_Packages;
6333 ---------------------------------
6334 -- Check_Forward_Instantiation --
6335 ---------------------------------
6337 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6338 S : Entity_Id;
6339 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6341 begin
6342 -- The instantiation appears before the generic body if we are in the
6343 -- scope of the unit containing the generic, either in its spec or in
6344 -- the package body, and before the generic body.
6346 if Ekind (Gen_Comp) = E_Package_Body then
6347 Gen_Comp := Spec_Entity (Gen_Comp);
6348 end if;
6350 if In_Open_Scopes (Gen_Comp)
6351 and then No (Corresponding_Body (Decl))
6352 then
6353 S := Current_Scope;
6355 while Present (S)
6356 and then not Is_Compilation_Unit (S)
6357 and then not Is_Child_Unit (S)
6358 loop
6359 if Ekind (S) = E_Package then
6360 Set_Has_Forward_Instantiation (S);
6361 end if;
6363 S := Scope (S);
6364 end loop;
6365 end if;
6366 end Check_Forward_Instantiation;
6368 ---------------------------
6369 -- Check_Generic_Actuals --
6370 ---------------------------
6372 -- The visibility of the actuals may be different between the point of
6373 -- generic instantiation and the instantiation of the body.
6375 procedure Check_Generic_Actuals
6376 (Instance : Entity_Id;
6377 Is_Formal_Box : Boolean)
6379 E : Entity_Id;
6380 Astype : Entity_Id;
6382 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6383 -- For a formal that is an array type, the component type is often a
6384 -- previous formal in the same unit. The privacy status of the component
6385 -- type will have been examined earlier in the traversal of the
6386 -- corresponding actuals, and this status should not be modified for
6387 -- the array (sub)type itself. However, if the base type of the array
6388 -- (sub)type is private, its full view must be restored in the body to
6389 -- be consistent with subsequent index subtypes, etc.
6391 -- To detect this case we have to rescan the list of formals, which is
6392 -- usually short enough to ignore the resulting inefficiency.
6394 -----------------------------
6395 -- Denotes_Previous_Actual --
6396 -----------------------------
6398 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6399 Prev : Entity_Id;
6401 begin
6402 Prev := First_Entity (Instance);
6403 while Present (Prev) loop
6404 if Is_Type (Prev)
6405 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6406 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6407 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6408 then
6409 return True;
6411 elsif Prev = E then
6412 return False;
6414 else
6415 Next_Entity (Prev);
6416 end if;
6417 end loop;
6419 return False;
6420 end Denotes_Previous_Actual;
6422 -- Start of processing for Check_Generic_Actuals
6424 begin
6425 E := First_Entity (Instance);
6426 while Present (E) loop
6427 if Is_Type (E)
6428 and then Nkind (Parent (E)) = N_Subtype_Declaration
6429 and then Scope (Etype (E)) /= Instance
6430 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6431 then
6432 if Is_Array_Type (E)
6433 and then not Is_Private_Type (Etype (E))
6434 and then Denotes_Previous_Actual (Component_Type (E))
6435 then
6436 null;
6437 else
6438 Check_Private_View (Subtype_Indication (Parent (E)));
6439 end if;
6441 Set_Is_Generic_Actual_Type (E, True);
6442 Set_Is_Hidden (E, False);
6443 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6445 -- We constructed the generic actual type as a subtype of the
6446 -- supplied type. This means that it normally would not inherit
6447 -- subtype specific attributes of the actual, which is wrong for
6448 -- the generic case.
6450 Astype := Ancestor_Subtype (E);
6452 if No (Astype) then
6454 -- This can happen when E is an itype that is the full view of
6455 -- a private type completed, e.g. with a constrained array. In
6456 -- that case, use the first subtype, which will carry size
6457 -- information. The base type itself is unconstrained and will
6458 -- not carry it.
6460 Astype := First_Subtype (E);
6461 end if;
6463 Set_Size_Info (E, (Astype));
6464 Set_RM_Size (E, RM_Size (Astype));
6465 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6467 if Is_Discrete_Or_Fixed_Point_Type (E) then
6468 Set_RM_Size (E, RM_Size (Astype));
6470 -- In nested instances, the base type of an access actual may
6471 -- itself be private, and need to be exchanged.
6473 elsif Is_Access_Type (E)
6474 and then Is_Private_Type (Etype (E))
6475 then
6476 Check_Private_View
6477 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6478 end if;
6480 elsif Ekind (E) = E_Package then
6482 -- If this is the renaming for the current instance, we're done.
6483 -- Otherwise it is a formal package. If the corresponding formal
6484 -- was declared with a box, the (instantiations of the) generic
6485 -- formal part are also visible. Otherwise, ignore the entity
6486 -- created to validate the actuals.
6488 if Renamed_Object (E) = Instance then
6489 exit;
6491 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6492 null;
6494 -- The visibility of a formal of an enclosing generic is already
6495 -- correct.
6497 elsif Denotes_Formal_Package (E) then
6498 null;
6500 elsif Present (Associated_Formal_Package (E))
6501 and then not Is_Generic_Formal (E)
6502 then
6503 if Box_Present (Parent (Associated_Formal_Package (E))) then
6504 Check_Generic_Actuals (Renamed_Object (E), True);
6506 else
6507 Check_Generic_Actuals (Renamed_Object (E), False);
6508 end if;
6510 Set_Is_Hidden (E, False);
6511 end if;
6513 -- If this is a subprogram instance (in a wrapper package) the
6514 -- actual is fully visible.
6516 elsif Is_Wrapper_Package (Instance) then
6517 Set_Is_Hidden (E, False);
6519 -- If the formal package is declared with a box, or if the formal
6520 -- parameter is defaulted, it is visible in the body.
6522 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6523 Set_Is_Hidden (E, False);
6524 end if;
6526 if Ekind (E) = E_Constant then
6528 -- If the type of the actual is a private type declared in the
6529 -- enclosing scope of the generic unit, the body of the generic
6530 -- sees the full view of the type (because it has to appear in
6531 -- the corresponding package body). If the type is private now,
6532 -- exchange views to restore the proper visiblity in the instance.
6534 declare
6535 Typ : constant Entity_Id := Base_Type (Etype (E));
6536 -- The type of the actual
6538 Gen_Id : Entity_Id;
6539 -- The generic unit
6541 Parent_Scope : Entity_Id;
6542 -- The enclosing scope of the generic unit
6544 begin
6545 if Is_Wrapper_Package (Instance) then
6546 Gen_Id :=
6547 Generic_Parent
6548 (Specification
6549 (Unit_Declaration_Node
6550 (Related_Instance (Instance))));
6551 else
6552 Gen_Id :=
6553 Generic_Parent (Package_Specification (Instance));
6554 end if;
6556 Parent_Scope := Scope (Gen_Id);
6558 -- The exchange is only needed if the generic is defined
6559 -- within a package which is not a common ancestor of the
6560 -- scope of the instance, and is not already in scope.
6562 if Is_Private_Type (Typ)
6563 and then Scope (Typ) = Parent_Scope
6564 and then Scope (Instance) /= Parent_Scope
6565 and then Ekind (Parent_Scope) = E_Package
6566 and then not Is_Child_Unit (Gen_Id)
6567 then
6568 Switch_View (Typ);
6570 -- If the type of the entity is a subtype, it may also have
6571 -- to be made visible, together with the base type of its
6572 -- full view, after exchange.
6574 if Is_Private_Type (Etype (E)) then
6575 Switch_View (Etype (E));
6576 Switch_View (Base_Type (Etype (E)));
6577 end if;
6578 end if;
6579 end;
6580 end if;
6582 Next_Entity (E);
6583 end loop;
6584 end Check_Generic_Actuals;
6586 ------------------------------
6587 -- Check_Generic_Child_Unit --
6588 ------------------------------
6590 procedure Check_Generic_Child_Unit
6591 (Gen_Id : Node_Id;
6592 Parent_Installed : in out Boolean)
6594 Loc : constant Source_Ptr := Sloc (Gen_Id);
6595 Gen_Par : Entity_Id := Empty;
6596 E : Entity_Id;
6597 Inst_Par : Entity_Id;
6598 S : Node_Id;
6600 function Find_Generic_Child
6601 (Scop : Entity_Id;
6602 Id : Node_Id) return Entity_Id;
6603 -- Search generic parent for possible child unit with the given name
6605 function In_Enclosing_Instance return Boolean;
6606 -- Within an instance of the parent, the child unit may be denoted by
6607 -- a simple name, or an abbreviated expanded name. Examine enclosing
6608 -- scopes to locate a possible parent instantiation.
6610 ------------------------
6611 -- Find_Generic_Child --
6612 ------------------------
6614 function Find_Generic_Child
6615 (Scop : Entity_Id;
6616 Id : Node_Id) return Entity_Id
6618 E : Entity_Id;
6620 begin
6621 -- If entity of name is already set, instance has already been
6622 -- resolved, e.g. in an enclosing instantiation.
6624 if Present (Entity (Id)) then
6625 if Scope (Entity (Id)) = Scop then
6626 return Entity (Id);
6627 else
6628 return Empty;
6629 end if;
6631 else
6632 E := First_Entity (Scop);
6633 while Present (E) loop
6634 if Chars (E) = Chars (Id)
6635 and then Is_Child_Unit (E)
6636 then
6637 if Is_Child_Unit (E)
6638 and then not Is_Visible_Lib_Unit (E)
6639 then
6640 Error_Msg_NE
6641 ("generic child unit& is not visible", Gen_Id, E);
6642 end if;
6644 Set_Entity (Id, E);
6645 return E;
6646 end if;
6648 Next_Entity (E);
6649 end loop;
6651 return Empty;
6652 end if;
6653 end Find_Generic_Child;
6655 ---------------------------
6656 -- In_Enclosing_Instance --
6657 ---------------------------
6659 function In_Enclosing_Instance return Boolean is
6660 Enclosing_Instance : Node_Id;
6661 Instance_Decl : Node_Id;
6663 begin
6664 -- We do not inline any call that contains instantiations, except
6665 -- for instantiations of Unchecked_Conversion, so if we are within
6666 -- an inlined body the current instance does not require parents.
6668 if In_Inlined_Body then
6669 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6670 return False;
6671 end if;
6673 -- Loop to check enclosing scopes
6675 Enclosing_Instance := Current_Scope;
6676 while Present (Enclosing_Instance) loop
6677 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6679 if Ekind (Enclosing_Instance) = E_Package
6680 and then Is_Generic_Instance (Enclosing_Instance)
6681 and then Present
6682 (Generic_Parent (Specification (Instance_Decl)))
6683 then
6684 -- Check whether the generic we are looking for is a child of
6685 -- this instance.
6687 E := Find_Generic_Child
6688 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6689 exit when Present (E);
6691 else
6692 E := Empty;
6693 end if;
6695 Enclosing_Instance := Scope (Enclosing_Instance);
6696 end loop;
6698 if No (E) then
6700 -- Not a child unit
6702 Analyze (Gen_Id);
6703 return False;
6705 else
6706 Rewrite (Gen_Id,
6707 Make_Expanded_Name (Loc,
6708 Chars => Chars (E),
6709 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6710 Selector_Name => New_Occurrence_Of (E, Loc)));
6712 Set_Entity (Gen_Id, E);
6713 Set_Etype (Gen_Id, Etype (E));
6714 Parent_Installed := False; -- Already in scope.
6715 return True;
6716 end if;
6717 end In_Enclosing_Instance;
6719 -- Start of processing for Check_Generic_Child_Unit
6721 begin
6722 -- If the name of the generic is given by a selected component, it may
6723 -- be the name of a generic child unit, and the prefix is the name of an
6724 -- instance of the parent, in which case the child unit must be visible.
6725 -- If this instance is not in scope, it must be placed there and removed
6726 -- after instantiation, because what is being instantiated is not the
6727 -- original child, but the corresponding child present in the instance
6728 -- of the parent.
6730 -- If the child is instantiated within the parent, it can be given by
6731 -- a simple name. In this case the instance is already in scope, but
6732 -- the child generic must be recovered from the generic parent as well.
6734 if Nkind (Gen_Id) = N_Selected_Component then
6735 S := Selector_Name (Gen_Id);
6736 Analyze (Prefix (Gen_Id));
6737 Inst_Par := Entity (Prefix (Gen_Id));
6739 if Ekind (Inst_Par) = E_Package
6740 and then Present (Renamed_Object (Inst_Par))
6741 then
6742 Inst_Par := Renamed_Object (Inst_Par);
6743 end if;
6745 if Ekind (Inst_Par) = E_Package then
6746 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6747 Gen_Par := Generic_Parent (Parent (Inst_Par));
6749 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6750 and then
6751 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6752 then
6753 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6754 end if;
6756 elsif Ekind (Inst_Par) = E_Generic_Package
6757 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6758 then
6759 -- A formal package may be a real child package, and not the
6760 -- implicit instance within a parent. In this case the child is
6761 -- not visible and has to be retrieved explicitly as well.
6763 Gen_Par := Inst_Par;
6764 end if;
6766 if Present (Gen_Par) then
6768 -- The prefix denotes an instantiation. The entity itself may be a
6769 -- nested generic, or a child unit.
6771 E := Find_Generic_Child (Gen_Par, S);
6773 if Present (E) then
6774 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6775 Set_Entity (Gen_Id, E);
6776 Set_Etype (Gen_Id, Etype (E));
6777 Set_Entity (S, E);
6778 Set_Etype (S, Etype (E));
6780 -- Indicate that this is a reference to the parent
6782 if In_Extended_Main_Source_Unit (Gen_Id) then
6783 Set_Is_Instantiated (Inst_Par);
6784 end if;
6786 -- A common mistake is to replicate the naming scheme of a
6787 -- hierarchy by instantiating a generic child directly, rather
6788 -- than the implicit child in a parent instance:
6790 -- generic .. package Gpar is ..
6791 -- generic .. package Gpar.Child is ..
6792 -- package Par is new Gpar ();
6794 -- with Gpar.Child;
6795 -- package Par.Child is new Gpar.Child ();
6796 -- rather than Par.Child
6798 -- In this case the instantiation is within Par, which is an
6799 -- instance, but Gpar does not denote Par because we are not IN
6800 -- the instance of Gpar, so this is illegal. The test below
6801 -- recognizes this particular case.
6803 if Is_Child_Unit (E)
6804 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6805 and then (not In_Instance
6806 or else Nkind (Parent (Parent (Gen_Id))) =
6807 N_Compilation_Unit)
6808 then
6809 Error_Msg_N
6810 ("prefix of generic child unit must be instance of parent",
6811 Gen_Id);
6812 end if;
6814 if not In_Open_Scopes (Inst_Par)
6815 and then Nkind (Parent (Gen_Id)) not in
6816 N_Generic_Renaming_Declaration
6817 then
6818 Install_Parent (Inst_Par);
6819 Parent_Installed := True;
6821 elsif In_Open_Scopes (Inst_Par) then
6823 -- If the parent is already installed, install the actuals
6824 -- for its formal packages. This is necessary when the child
6825 -- instance is a child of the parent instance: in this case,
6826 -- the parent is placed on the scope stack but the formal
6827 -- packages are not made visible.
6829 Install_Formal_Packages (Inst_Par);
6830 end if;
6832 else
6833 -- If the generic parent does not contain an entity that
6834 -- corresponds to the selector, the instance doesn't either.
6835 -- Analyzing the node will yield the appropriate error message.
6836 -- If the entity is not a child unit, then it is an inner
6837 -- generic in the parent.
6839 Analyze (Gen_Id);
6840 end if;
6842 else
6843 Analyze (Gen_Id);
6845 if Is_Child_Unit (Entity (Gen_Id))
6846 and then
6847 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6848 and then not In_Open_Scopes (Inst_Par)
6849 then
6850 Install_Parent (Inst_Par);
6851 Parent_Installed := True;
6853 -- The generic unit may be the renaming of the implicit child
6854 -- present in an instance. In that case the parent instance is
6855 -- obtained from the name of the renamed entity.
6857 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6858 and then Present (Renamed_Entity (Entity (Gen_Id)))
6859 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6860 then
6861 declare
6862 Renamed_Package : constant Node_Id :=
6863 Name (Parent (Entity (Gen_Id)));
6864 begin
6865 if Nkind (Renamed_Package) = N_Expanded_Name then
6866 Inst_Par := Entity (Prefix (Renamed_Package));
6867 Install_Parent (Inst_Par);
6868 Parent_Installed := True;
6869 end if;
6870 end;
6871 end if;
6872 end if;
6874 elsif Nkind (Gen_Id) = N_Expanded_Name then
6876 -- Entity already present, analyze prefix, whose meaning may be an
6877 -- instance in the current context. If it is an instance of a
6878 -- relative within another, the proper parent may still have to be
6879 -- installed, if they are not of the same generation.
6881 Analyze (Prefix (Gen_Id));
6883 -- Prevent cascaded errors
6885 if Etype (Prefix (Gen_Id)) = Any_Type then
6886 return;
6887 end if;
6889 -- In the unlikely case that a local declaration hides the name of
6890 -- the parent package, locate it on the homonym chain. If the context
6891 -- is an instance of the parent, the renaming entity is flagged as
6892 -- such.
6894 Inst_Par := Entity (Prefix (Gen_Id));
6895 while Present (Inst_Par)
6896 and then not Is_Package_Or_Generic_Package (Inst_Par)
6897 loop
6898 Inst_Par := Homonym (Inst_Par);
6899 end loop;
6901 pragma Assert (Present (Inst_Par));
6902 Set_Entity (Prefix (Gen_Id), Inst_Par);
6904 if In_Enclosing_Instance then
6905 null;
6907 elsif Present (Entity (Gen_Id))
6908 and then Is_Child_Unit (Entity (Gen_Id))
6909 and then not In_Open_Scopes (Inst_Par)
6910 then
6911 Install_Parent (Inst_Par);
6912 Parent_Installed := True;
6913 end if;
6915 elsif In_Enclosing_Instance then
6917 -- The child unit is found in some enclosing scope
6919 null;
6921 else
6922 Analyze (Gen_Id);
6924 -- If this is the renaming of the implicit child in a parent
6925 -- instance, recover the parent name and install it.
6927 if Is_Entity_Name (Gen_Id) then
6928 E := Entity (Gen_Id);
6930 if Is_Generic_Unit (E)
6931 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6932 and then Is_Child_Unit (Renamed_Object (E))
6933 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6934 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6935 then
6936 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6937 Inst_Par := Entity (Prefix (Gen_Id));
6939 if not In_Open_Scopes (Inst_Par) then
6940 Install_Parent (Inst_Par);
6941 Parent_Installed := True;
6942 end if;
6944 -- If it is a child unit of a non-generic parent, it may be
6945 -- use-visible and given by a direct name. Install parent as
6946 -- for other cases.
6948 elsif Is_Generic_Unit (E)
6949 and then Is_Child_Unit (E)
6950 and then
6951 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6952 and then not Is_Generic_Unit (Scope (E))
6953 then
6954 if not In_Open_Scopes (Scope (E)) then
6955 Install_Parent (Scope (E));
6956 Parent_Installed := True;
6957 end if;
6958 end if;
6959 end if;
6960 end if;
6961 end Check_Generic_Child_Unit;
6963 -----------------------------
6964 -- Check_Hidden_Child_Unit --
6965 -----------------------------
6967 procedure Check_Hidden_Child_Unit
6968 (N : Node_Id;
6969 Gen_Unit : Entity_Id;
6970 Act_Decl_Id : Entity_Id)
6972 Gen_Id : constant Node_Id := Name (N);
6974 begin
6975 if Is_Child_Unit (Gen_Unit)
6976 and then Is_Child_Unit (Act_Decl_Id)
6977 and then Nkind (Gen_Id) = N_Expanded_Name
6978 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6979 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6980 then
6981 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6982 Error_Msg_NE
6983 ("generic unit & is implicitly declared in &",
6984 Defining_Unit_Name (N), Gen_Unit);
6985 Error_Msg_N ("\instance must have different name",
6986 Defining_Unit_Name (N));
6987 end if;
6988 end Check_Hidden_Child_Unit;
6990 ------------------------
6991 -- Check_Private_View --
6992 ------------------------
6994 procedure Check_Private_View (N : Node_Id) is
6995 T : constant Entity_Id := Etype (N);
6996 BT : Entity_Id;
6998 begin
6999 -- Exchange views if the type was not private in the generic but is
7000 -- private at the point of instantiation. Do not exchange views if
7001 -- the scope of the type is in scope. This can happen if both generic
7002 -- and instance are sibling units, or if type is defined in a parent.
7003 -- In this case the visibility of the type will be correct for all
7004 -- semantic checks.
7006 if Present (T) then
7007 BT := Base_Type (T);
7009 if Is_Private_Type (T)
7010 and then not Has_Private_View (N)
7011 and then Present (Full_View (T))
7012 and then not In_Open_Scopes (Scope (T))
7013 then
7014 -- In the generic, the full type was visible. Save the private
7015 -- entity, for subsequent exchange.
7017 Switch_View (T);
7019 elsif Has_Private_View (N)
7020 and then not Is_Private_Type (T)
7021 and then not Has_Been_Exchanged (T)
7022 and then Etype (Get_Associated_Node (N)) /= T
7023 then
7024 -- Only the private declaration was visible in the generic. If
7025 -- the type appears in a subtype declaration, the subtype in the
7026 -- instance must have a view compatible with that of its parent,
7027 -- which must be exchanged (see corresponding code in Restore_
7028 -- Private_Views). Otherwise, if the type is defined in a parent
7029 -- unit, leave full visibility within instance, which is safe.
7031 if In_Open_Scopes (Scope (Base_Type (T)))
7032 and then not Is_Private_Type (Base_Type (T))
7033 and then Comes_From_Source (Base_Type (T))
7034 then
7035 null;
7037 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7038 or else not In_Private_Part (Scope (Base_Type (T)))
7039 then
7040 Prepend_Elmt (T, Exchanged_Views);
7041 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7042 end if;
7044 -- For composite types with inconsistent representation exchange
7045 -- component types accordingly.
7047 elsif Is_Access_Type (T)
7048 and then Is_Private_Type (Designated_Type (T))
7049 and then not Has_Private_View (N)
7050 and then Present (Full_View (Designated_Type (T)))
7051 then
7052 Switch_View (Designated_Type (T));
7054 elsif Is_Array_Type (T) then
7055 if Is_Private_Type (Component_Type (T))
7056 and then not Has_Private_View (N)
7057 and then Present (Full_View (Component_Type (T)))
7058 then
7059 Switch_View (Component_Type (T));
7060 end if;
7062 -- The normal exchange mechanism relies on the setting of a
7063 -- flag on the reference in the generic. However, an additional
7064 -- mechanism is needed for types that are not explicitly
7065 -- mentioned in the generic, but may be needed in expanded code
7066 -- in the instance. This includes component types of arrays and
7067 -- designated types of access types. This processing must also
7068 -- include the index types of arrays which we take care of here.
7070 declare
7071 Indx : Node_Id;
7072 Typ : Entity_Id;
7074 begin
7075 Indx := First_Index (T);
7076 while Present (Indx) loop
7077 Typ := Base_Type (Etype (Indx));
7079 if Is_Private_Type (Typ)
7080 and then Present (Full_View (Typ))
7081 then
7082 Switch_View (Typ);
7083 end if;
7085 Next_Index (Indx);
7086 end loop;
7087 end;
7089 elsif Is_Private_Type (T)
7090 and then Present (Full_View (T))
7091 and then Is_Array_Type (Full_View (T))
7092 and then Is_Private_Type (Component_Type (Full_View (T)))
7093 then
7094 Switch_View (T);
7096 -- Finally, a non-private subtype may have a private base type, which
7097 -- must be exchanged for consistency. This can happen when a package
7098 -- body is instantiated, when the scope stack is empty but in fact
7099 -- the subtype and the base type are declared in an enclosing scope.
7101 -- Note that in this case we introduce an inconsistency in the view
7102 -- set, because we switch the base type BT, but there could be some
7103 -- private dependent subtypes of BT which remain unswitched. Such
7104 -- subtypes might need to be switched at a later point (see specific
7105 -- provision for that case in Switch_View).
7107 elsif not Is_Private_Type (T)
7108 and then not Has_Private_View (N)
7109 and then Is_Private_Type (BT)
7110 and then Present (Full_View (BT))
7111 and then not Is_Generic_Type (BT)
7112 and then not In_Open_Scopes (BT)
7113 then
7114 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7115 Exchange_Declarations (BT);
7116 end if;
7117 end if;
7118 end Check_Private_View;
7120 -----------------------------
7121 -- Check_Hidden_Primitives --
7122 -----------------------------
7124 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7125 Actual : Node_Id;
7126 Gen_T : Entity_Id;
7127 Result : Elist_Id := No_Elist;
7129 begin
7130 if No (Assoc_List) then
7131 return No_Elist;
7132 end if;
7134 -- Traverse the list of associations between formals and actuals
7135 -- searching for renamings of tagged types
7137 Actual := First (Assoc_List);
7138 while Present (Actual) loop
7139 if Nkind (Actual) = N_Subtype_Declaration then
7140 Gen_T := Generic_Parent_Type (Actual);
7142 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7144 -- Traverse the list of primitives of the actual types
7145 -- searching for hidden primitives that are visible in the
7146 -- corresponding generic formal; leave them visible and
7147 -- append them to Result to restore their decoration later.
7149 Install_Hidden_Primitives
7150 (Prims_List => Result,
7151 Gen_T => Gen_T,
7152 Act_T => Entity (Subtype_Indication (Actual)));
7153 end if;
7154 end if;
7156 Next (Actual);
7157 end loop;
7159 return Result;
7160 end Check_Hidden_Primitives;
7162 --------------------------
7163 -- Contains_Instance_Of --
7164 --------------------------
7166 function Contains_Instance_Of
7167 (Inner : Entity_Id;
7168 Outer : Entity_Id;
7169 N : Node_Id) return Boolean
7171 Elmt : Elmt_Id;
7172 Scop : Entity_Id;
7174 begin
7175 Scop := Outer;
7177 -- Verify that there are no circular instantiations. We check whether
7178 -- the unit contains an instance of the current scope or some enclosing
7179 -- scope (in case one of the instances appears in a subunit). Longer
7180 -- circularities involving subunits might seem too pathological to
7181 -- consider, but they were not too pathological for the authors of
7182 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7183 -- enclosing generic scopes as containing an instance.
7185 loop
7186 -- Within a generic subprogram body, the scope is not generic, to
7187 -- allow for recursive subprograms. Use the declaration to determine
7188 -- whether this is a generic unit.
7190 if Ekind (Scop) = E_Generic_Package
7191 or else (Is_Subprogram (Scop)
7192 and then Nkind (Unit_Declaration_Node (Scop)) =
7193 N_Generic_Subprogram_Declaration)
7194 then
7195 Elmt := First_Elmt (Inner_Instances (Inner));
7197 while Present (Elmt) loop
7198 if Node (Elmt) = Scop then
7199 Error_Msg_Node_2 := Inner;
7200 Error_Msg_NE
7201 ("circular Instantiation: & instantiated within &!",
7202 N, Scop);
7203 return True;
7205 elsif Node (Elmt) = Inner then
7206 return True;
7208 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7209 Error_Msg_Node_2 := Inner;
7210 Error_Msg_NE
7211 ("circular Instantiation: & instantiated within &!",
7212 N, Node (Elmt));
7213 return True;
7214 end if;
7216 Next_Elmt (Elmt);
7217 end loop;
7219 -- Indicate that Inner is being instantiated within Scop
7221 Append_Elmt (Inner, Inner_Instances (Scop));
7222 end if;
7224 if Scop = Standard_Standard then
7225 exit;
7226 else
7227 Scop := Scope (Scop);
7228 end if;
7229 end loop;
7231 return False;
7232 end Contains_Instance_Of;
7234 -----------------------
7235 -- Copy_Generic_Node --
7236 -----------------------
7238 function Copy_Generic_Node
7239 (N : Node_Id;
7240 Parent_Id : Node_Id;
7241 Instantiating : Boolean) return Node_Id
7243 Ent : Entity_Id;
7244 New_N : Node_Id;
7246 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7247 -- Check the given value of one of the Fields referenced by the current
7248 -- node to determine whether to copy it recursively. The field may hold
7249 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7250 -- Char) in which case it need not be copied.
7252 procedure Copy_Descendants;
7253 -- Common utility for various nodes
7255 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7256 -- Make copy of element list
7258 function Copy_Generic_List
7259 (L : List_Id;
7260 Parent_Id : Node_Id) return List_Id;
7261 -- Apply Copy_Node recursively to the members of a node list
7263 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7264 -- True if an identifier is part of the defining program unit name of
7265 -- a child unit. The entity of such an identifier must be kept (for
7266 -- ASIS use) even though as the name of an enclosing generic it would
7267 -- otherwise not be preserved in the generic tree.
7269 ----------------------
7270 -- Copy_Descendants --
7271 ----------------------
7273 procedure Copy_Descendants is
7274 use Atree.Unchecked_Access;
7275 -- This code section is part of the implementation of an untyped
7276 -- tree traversal, so it needs direct access to node fields.
7278 begin
7279 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7280 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7281 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7282 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7283 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7284 end Copy_Descendants;
7286 -----------------------------
7287 -- Copy_Generic_Descendant --
7288 -----------------------------
7290 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7291 begin
7292 if D = Union_Id (Empty) then
7293 return D;
7295 elsif D in Node_Range then
7296 return Union_Id
7297 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7299 elsif D in List_Range then
7300 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7302 elsif D in Elist_Range then
7303 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7305 -- Nothing else is copyable (e.g. Uint values), return as is
7307 else
7308 return D;
7309 end if;
7310 end Copy_Generic_Descendant;
7312 ------------------------
7313 -- Copy_Generic_Elist --
7314 ------------------------
7316 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7317 M : Elmt_Id;
7318 L : Elist_Id;
7320 begin
7321 if Present (E) then
7322 L := New_Elmt_List;
7323 M := First_Elmt (E);
7324 while Present (M) loop
7325 Append_Elmt
7326 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7327 Next_Elmt (M);
7328 end loop;
7330 return L;
7332 else
7333 return No_Elist;
7334 end if;
7335 end Copy_Generic_Elist;
7337 -----------------------
7338 -- Copy_Generic_List --
7339 -----------------------
7341 function Copy_Generic_List
7342 (L : List_Id;
7343 Parent_Id : Node_Id) return List_Id
7345 N : Node_Id;
7346 New_L : List_Id;
7348 begin
7349 if Present (L) then
7350 New_L := New_List;
7351 Set_Parent (New_L, Parent_Id);
7353 N := First (L);
7354 while Present (N) loop
7355 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7356 Next (N);
7357 end loop;
7359 return New_L;
7361 else
7362 return No_List;
7363 end if;
7364 end Copy_Generic_List;
7366 ---------------------------
7367 -- In_Defining_Unit_Name --
7368 ---------------------------
7370 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7371 begin
7372 return
7373 Present (Parent (Nam))
7374 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7375 or else
7376 (Nkind (Parent (Nam)) = N_Expanded_Name
7377 and then In_Defining_Unit_Name (Parent (Nam))));
7378 end In_Defining_Unit_Name;
7380 -- Start of processing for Copy_Generic_Node
7382 begin
7383 if N = Empty then
7384 return N;
7385 end if;
7387 New_N := New_Copy (N);
7389 -- Copy aspects if present
7391 if Has_Aspects (N) then
7392 Set_Has_Aspects (New_N, False);
7393 Set_Aspect_Specifications
7394 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7395 end if;
7397 if Instantiating then
7398 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7399 end if;
7401 if not Is_List_Member (N) then
7402 Set_Parent (New_N, Parent_Id);
7403 end if;
7405 -- Special casing for identifiers and other entity names and operators
7407 if Nkind_In (New_N, N_Character_Literal,
7408 N_Expanded_Name,
7409 N_Identifier,
7410 N_Operator_Symbol)
7411 or else Nkind (New_N) in N_Op
7412 then
7413 if not Instantiating then
7415 -- Link both nodes in order to assign subsequently the entity of
7416 -- the copy to the original node, in case this is a global
7417 -- reference.
7419 Set_Associated_Node (N, New_N);
7421 -- If we are within an instantiation, this is a nested generic
7422 -- that has already been analyzed at the point of definition.
7423 -- We must preserve references that were global to the enclosing
7424 -- parent at that point. Other occurrences, whether global or
7425 -- local to the current generic, must be resolved anew, so we
7426 -- reset the entity in the generic copy. A global reference has a
7427 -- smaller depth than the parent, or else the same depth in case
7428 -- both are distinct compilation units.
7430 -- A child unit is implicitly declared within the enclosing parent
7431 -- but is in fact global to it, and must be preserved.
7433 -- It is also possible for Current_Instantiated_Parent to be
7434 -- defined, and for this not to be a nested generic, namely if
7435 -- the unit is loaded through Rtsfind. In that case, the entity of
7436 -- New_N is only a link to the associated node, and not a defining
7437 -- occurrence.
7439 -- The entities for parent units in the defining_program_unit of a
7440 -- generic child unit are established when the context of the unit
7441 -- is first analyzed, before the generic copy is made. They are
7442 -- preserved in the copy for use in ASIS queries.
7444 Ent := Entity (New_N);
7446 if No (Current_Instantiated_Parent.Gen_Id) then
7447 if No (Ent)
7448 or else Nkind (Ent) /= N_Defining_Identifier
7449 or else not In_Defining_Unit_Name (N)
7450 then
7451 Set_Associated_Node (New_N, Empty);
7452 end if;
7454 elsif No (Ent)
7455 or else
7456 not Nkind_In (Ent, N_Defining_Identifier,
7457 N_Defining_Character_Literal,
7458 N_Defining_Operator_Symbol)
7459 or else No (Scope (Ent))
7460 or else
7461 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7462 and then not Is_Child_Unit (Ent))
7463 or else
7464 (Scope_Depth (Scope (Ent)) >
7465 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7466 and then
7467 Get_Source_Unit (Ent) =
7468 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7469 then
7470 Set_Associated_Node (New_N, Empty);
7471 end if;
7473 -- Case of instantiating identifier or some other name or operator
7475 else
7476 -- If the associated node is still defined, the entity in it
7477 -- is global, and must be copied to the instance. If this copy
7478 -- is being made for a body to inline, it is applied to an
7479 -- instantiated tree, and the entity is already present and
7480 -- must be also preserved.
7482 declare
7483 Assoc : constant Node_Id := Get_Associated_Node (N);
7485 begin
7486 if Present (Assoc) then
7487 if Nkind (Assoc) = Nkind (N) then
7488 Set_Entity (New_N, Entity (Assoc));
7489 Check_Private_View (N);
7491 -- The node is a reference to a global type and acts as the
7492 -- subtype mark of a qualified expression created in order
7493 -- to aid resolution of accidental overloading in instances.
7494 -- Since N is a reference to a type, the Associated_Node of
7495 -- N denotes an entity rather than another identifier. See
7496 -- Qualify_Universal_Operands for details.
7498 elsif Nkind (N) = N_Identifier
7499 and then Nkind (Parent (N)) = N_Qualified_Expression
7500 and then Subtype_Mark (Parent (N)) = N
7501 and then Is_Qualified_Universal_Literal (Parent (N))
7502 then
7503 Set_Entity (New_N, Assoc);
7505 -- The name in the call may be a selected component if the
7506 -- call has not been analyzed yet, as may be the case for
7507 -- pre/post conditions in a generic unit.
7509 elsif Nkind (Assoc) = N_Function_Call
7510 and then Is_Entity_Name (Name (Assoc))
7511 then
7512 Set_Entity (New_N, Entity (Name (Assoc)));
7514 elsif Nkind_In (Assoc, N_Defining_Identifier,
7515 N_Defining_Character_Literal,
7516 N_Defining_Operator_Symbol)
7517 and then Expander_Active
7518 then
7519 -- Inlining case: we are copying a tree that contains
7520 -- global entities, which are preserved in the copy to be
7521 -- used for subsequent inlining.
7523 null;
7525 else
7526 Set_Entity (New_N, Empty);
7527 end if;
7528 end if;
7529 end;
7530 end if;
7532 -- For expanded name, we must copy the Prefix and Selector_Name
7534 if Nkind (N) = N_Expanded_Name then
7535 Set_Prefix
7536 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7538 Set_Selector_Name (New_N,
7539 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7541 -- For operators, we must copy the right operand
7543 elsif Nkind (N) in N_Op then
7544 Set_Right_Opnd (New_N,
7545 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7547 -- And for binary operators, the left operand as well
7549 if Nkind (N) in N_Binary_Op then
7550 Set_Left_Opnd (New_N,
7551 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7552 end if;
7553 end if;
7555 -- Establish a link between an entity from the generic template and the
7556 -- corresponding entity in the generic copy to be analyzed.
7558 elsif Nkind (N) in N_Entity then
7559 if not Instantiating then
7560 Set_Associated_Entity (N, New_N);
7561 end if;
7563 -- Clear any existing link the copy may inherit from the replicated
7564 -- generic template entity.
7566 Set_Associated_Entity (New_N, Empty);
7568 -- Special casing for stubs
7570 elsif Nkind (N) in N_Body_Stub then
7572 -- In any case, we must copy the specification or defining
7573 -- identifier as appropriate.
7575 if Nkind (N) = N_Subprogram_Body_Stub then
7576 Set_Specification (New_N,
7577 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7579 else
7580 Set_Defining_Identifier (New_N,
7581 Copy_Generic_Node
7582 (Defining_Identifier (N), New_N, Instantiating));
7583 end if;
7585 -- If we are not instantiating, then this is where we load and
7586 -- analyze subunits, i.e. at the point where the stub occurs. A
7587 -- more permissive system might defer this analysis to the point
7588 -- of instantiation, but this seems too complicated for now.
7590 if not Instantiating then
7591 declare
7592 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7593 Subunit : Node_Id;
7594 Unum : Unit_Number_Type;
7595 New_Body : Node_Id;
7597 begin
7598 -- Make sure that, if it is a subunit of the main unit that is
7599 -- preprocessed and if -gnateG is specified, the preprocessed
7600 -- file will be written.
7602 Lib.Analysing_Subunit_Of_Main :=
7603 Lib.In_Extended_Main_Source_Unit (N);
7604 Unum :=
7605 Load_Unit
7606 (Load_Name => Subunit_Name,
7607 Required => False,
7608 Subunit => True,
7609 Error_Node => N);
7610 Lib.Analysing_Subunit_Of_Main := False;
7612 -- If the proper body is not found, a warning message will be
7613 -- emitted when analyzing the stub, or later at the point of
7614 -- instantiation. Here we just leave the stub as is.
7616 if Unum = No_Unit then
7617 Subunits_Missing := True;
7618 goto Subunit_Not_Found;
7619 end if;
7621 Subunit := Cunit (Unum);
7623 if Nkind (Unit (Subunit)) /= N_Subunit then
7624 Error_Msg_N
7625 ("found child unit instead of expected SEPARATE subunit",
7626 Subunit);
7627 Error_Msg_Sloc := Sloc (N);
7628 Error_Msg_N ("\to complete stub #", Subunit);
7629 goto Subunit_Not_Found;
7630 end if;
7632 -- We must create a generic copy of the subunit, in order to
7633 -- perform semantic analysis on it, and we must replace the
7634 -- stub in the original generic unit with the subunit, in order
7635 -- to preserve non-local references within.
7637 -- Only the proper body needs to be copied. Library_Unit and
7638 -- context clause are simply inherited by the generic copy.
7639 -- Note that the copy (which may be recursive if there are
7640 -- nested subunits) must be done first, before attaching it to
7641 -- the enclosing generic.
7643 New_Body :=
7644 Copy_Generic_Node
7645 (Proper_Body (Unit (Subunit)),
7646 Empty, Instantiating => False);
7648 -- Now place the original proper body in the original generic
7649 -- unit. This is a body, not a compilation unit.
7651 Rewrite (N, Proper_Body (Unit (Subunit)));
7652 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7653 Set_Was_Originally_Stub (N);
7655 -- Finally replace the body of the subunit with its copy, and
7656 -- make this new subunit into the library unit of the generic
7657 -- copy, which does not have stubs any longer.
7659 Set_Proper_Body (Unit (Subunit), New_Body);
7660 Set_Library_Unit (New_N, Subunit);
7661 Inherit_Context (Unit (Subunit), N);
7662 end;
7664 -- If we are instantiating, this must be an error case, since
7665 -- otherwise we would have replaced the stub node by the proper body
7666 -- that corresponds. So just ignore it in the copy (i.e. we have
7667 -- copied it, and that is good enough).
7669 else
7670 null;
7671 end if;
7673 <<Subunit_Not_Found>> null;
7675 -- If the node is a compilation unit, it is the subunit of a stub, which
7676 -- has been loaded already (see code below). In this case, the library
7677 -- unit field of N points to the parent unit (which is a compilation
7678 -- unit) and need not (and cannot) be copied.
7680 -- When the proper body of the stub is analyzed, the library_unit link
7681 -- is used to establish the proper context (see sem_ch10).
7683 -- The other fields of a compilation unit are copied as usual
7685 elsif Nkind (N) = N_Compilation_Unit then
7687 -- This code can only be executed when not instantiating, because in
7688 -- the copy made for an instantiation, the compilation unit node has
7689 -- disappeared at the point that a stub is replaced by its proper
7690 -- body.
7692 pragma Assert (not Instantiating);
7694 Set_Context_Items (New_N,
7695 Copy_Generic_List (Context_Items (N), New_N));
7697 Set_Unit (New_N,
7698 Copy_Generic_Node (Unit (N), New_N, False));
7700 Set_First_Inlined_Subprogram (New_N,
7701 Copy_Generic_Node
7702 (First_Inlined_Subprogram (N), New_N, False));
7704 Set_Aux_Decls_Node (New_N,
7705 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7707 -- For an assignment node, the assignment is known to be semantically
7708 -- legal if we are instantiating the template. This avoids incorrect
7709 -- diagnostics in generated code.
7711 elsif Nkind (N) = N_Assignment_Statement then
7713 -- Copy name and expression fields in usual manner
7715 Set_Name (New_N,
7716 Copy_Generic_Node (Name (N), New_N, Instantiating));
7718 Set_Expression (New_N,
7719 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7721 if Instantiating then
7722 Set_Assignment_OK (Name (New_N), True);
7723 end if;
7725 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7726 if not Instantiating then
7727 Set_Associated_Node (N, New_N);
7729 else
7730 if Present (Get_Associated_Node (N))
7731 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7732 then
7733 -- In the generic the aggregate has some composite type. If at
7734 -- the point of instantiation the type has a private view,
7735 -- install the full view (and that of its ancestors, if any).
7737 declare
7738 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7739 Rt : Entity_Id;
7741 begin
7742 if Present (T) and then Is_Private_Type (T) then
7743 Switch_View (T);
7744 end if;
7746 if Present (T)
7747 and then Is_Tagged_Type (T)
7748 and then Is_Derived_Type (T)
7749 then
7750 Rt := Root_Type (T);
7752 loop
7753 T := Etype (T);
7755 if Is_Private_Type (T) then
7756 Switch_View (T);
7757 end if;
7759 exit when T = Rt;
7760 end loop;
7761 end if;
7762 end;
7763 end if;
7764 end if;
7766 -- Do not copy the associated node, which points to the generic copy
7767 -- of the aggregate.
7769 declare
7770 use Atree.Unchecked_Access;
7771 -- This code section is part of the implementation of an untyped
7772 -- tree traversal, so it needs direct access to node fields.
7774 begin
7775 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7776 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7777 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7778 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7779 end;
7781 -- Allocators do not have an identifier denoting the access type, so we
7782 -- must locate it through the expression to check whether the views are
7783 -- consistent.
7785 elsif Nkind (N) = N_Allocator
7786 and then Nkind (Expression (N)) = N_Qualified_Expression
7787 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7788 and then Instantiating
7789 then
7790 declare
7791 T : constant Node_Id :=
7792 Get_Associated_Node (Subtype_Mark (Expression (N)));
7793 Acc_T : Entity_Id;
7795 begin
7796 if Present (T) then
7798 -- Retrieve the allocator node in the generic copy
7800 Acc_T := Etype (Parent (Parent (T)));
7802 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7803 Switch_View (Acc_T);
7804 end if;
7805 end if;
7807 Copy_Descendants;
7808 end;
7810 -- For a proper body, we must catch the case of a proper body that
7811 -- replaces a stub. This represents the point at which a separate
7812 -- compilation unit, and hence template file, may be referenced, so we
7813 -- must make a new source instantiation entry for the template of the
7814 -- subunit, and ensure that all nodes in the subunit are adjusted using
7815 -- this new source instantiation entry.
7817 elsif Nkind (N) in N_Proper_Body then
7818 declare
7819 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7821 begin
7822 if Instantiating and then Was_Originally_Stub (N) then
7823 Create_Instantiation_Source
7824 (Instantiation_Node,
7825 Defining_Entity (N),
7826 S_Adjustment);
7827 end if;
7829 -- Now copy the fields of the proper body, using the new
7830 -- adjustment factor if one was needed as per test above.
7832 Copy_Descendants;
7834 -- Restore the original adjustment factor in case changed
7836 S_Adjustment := Save_Adjustment;
7837 end;
7839 elsif Nkind (N) = N_Pragma and then Instantiating then
7841 -- Do not copy Comment or Ident pragmas their content is relevant to
7842 -- the generic unit, not to the instantiating unit.
7844 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
7845 New_N := Make_Null_Statement (Sloc (N));
7847 -- Do not copy pragmas generated from aspects because the pragmas do
7848 -- not carry any semantic information, plus they will be regenerated
7849 -- in the instance.
7851 -- However, generating C we need to copy them since postconditions
7852 -- are inlined by the front end, and the front-end inlining machinery
7853 -- relies on this routine to perform inlining.
7855 elsif From_Aspect_Specification (N)
7856 and then not Modify_Tree_For_C
7857 then
7858 New_N := Make_Null_Statement (Sloc (N));
7860 else
7861 Copy_Descendants;
7862 end if;
7864 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7866 -- No descendant fields need traversing
7868 null;
7870 elsif Nkind (N) = N_String_Literal
7871 and then Present (Etype (N))
7872 and then Instantiating
7873 then
7874 -- If the string is declared in an outer scope, the string_literal
7875 -- subtype created for it may have the wrong scope. Force reanalysis
7876 -- of the constant to generate a new itype in the proper context.
7878 Set_Etype (New_N, Empty);
7879 Set_Analyzed (New_N, False);
7881 -- For the remaining nodes, copy their descendants recursively
7883 else
7884 Copy_Descendants;
7886 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7887 Set_Generic_Parent (Specification (New_N), N);
7889 -- Should preserve Corresponding_Spec??? (12.3(14))
7890 end if;
7891 end if;
7893 -- Propagate dimensions if present, so that they are reflected in the
7894 -- instance.
7896 if Nkind (N) in N_Has_Etype
7897 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7898 and then Present (Etype (N))
7899 and then Is_Floating_Point_Type (Etype (N))
7900 and then Has_Dimension_System (Etype (N))
7901 then
7902 Copy_Dimensions (N, New_N);
7903 end if;
7905 return New_N;
7906 end Copy_Generic_Node;
7908 ----------------------------
7909 -- Denotes_Formal_Package --
7910 ----------------------------
7912 function Denotes_Formal_Package
7913 (Pack : Entity_Id;
7914 On_Exit : Boolean := False;
7915 Instance : Entity_Id := Empty) return Boolean
7917 Par : Entity_Id;
7918 Scop : constant Entity_Id := Scope (Pack);
7919 E : Entity_Id;
7921 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7922 -- The package in question may be an actual for a previous formal
7923 -- package P of the current instance, so examine its actuals as well.
7924 -- This must be recursive over other formal packages.
7926 ----------------------------------
7927 -- Is_Actual_Of_Previous_Formal --
7928 ----------------------------------
7930 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7931 E1 : Entity_Id;
7933 begin
7934 E1 := First_Entity (P);
7935 while Present (E1) and then E1 /= Instance loop
7936 if Ekind (E1) = E_Package
7937 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7938 then
7939 if Renamed_Object (E1) = Pack then
7940 return True;
7942 elsif E1 = P or else Renamed_Object (E1) = P then
7943 return False;
7945 elsif Is_Actual_Of_Previous_Formal (E1) then
7946 return True;
7947 end if;
7948 end if;
7950 Next_Entity (E1);
7951 end loop;
7953 return False;
7954 end Is_Actual_Of_Previous_Formal;
7956 -- Start of processing for Denotes_Formal_Package
7958 begin
7959 if On_Exit then
7960 Par :=
7961 Instance_Envs.Table
7962 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7963 else
7964 Par := Current_Instantiated_Parent.Act_Id;
7965 end if;
7967 if Ekind (Scop) = E_Generic_Package
7968 or else Nkind (Unit_Declaration_Node (Scop)) =
7969 N_Generic_Subprogram_Declaration
7970 then
7971 return True;
7973 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7974 N_Formal_Package_Declaration
7975 then
7976 return True;
7978 elsif No (Par) then
7979 return False;
7981 else
7982 -- Check whether this package is associated with a formal package of
7983 -- the enclosing instantiation. Iterate over the list of renamings.
7985 E := First_Entity (Par);
7986 while Present (E) loop
7987 if Ekind (E) /= E_Package
7988 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7989 then
7990 null;
7992 elsif Renamed_Object (E) = Par then
7993 return False;
7995 elsif Renamed_Object (E) = Pack then
7996 return True;
7998 elsif Is_Actual_Of_Previous_Formal (E) then
7999 return True;
8001 end if;
8003 Next_Entity (E);
8004 end loop;
8006 return False;
8007 end if;
8008 end Denotes_Formal_Package;
8010 -----------------
8011 -- End_Generic --
8012 -----------------
8014 procedure End_Generic is
8015 begin
8016 -- ??? More things could be factored out in this routine. Should
8017 -- probably be done at a later stage.
8019 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8020 Generic_Flags.Decrement_Last;
8022 Expander_Mode_Restore;
8023 end End_Generic;
8025 -------------
8026 -- Earlier --
8027 -------------
8029 function Earlier (N1, N2 : Node_Id) return Boolean is
8030 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8031 -- Find distance from given node to enclosing compilation unit
8033 ----------------
8034 -- Find_Depth --
8035 ----------------
8037 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8038 begin
8039 while Present (P)
8040 and then Nkind (P) /= N_Compilation_Unit
8041 loop
8042 P := True_Parent (P);
8043 D := D + 1;
8044 end loop;
8045 end Find_Depth;
8047 -- Local declarations
8049 D1 : Integer := 0;
8050 D2 : Integer := 0;
8051 P1 : Node_Id := N1;
8052 P2 : Node_Id := N2;
8053 T1 : Source_Ptr;
8054 T2 : Source_Ptr;
8056 -- Start of processing for Earlier
8058 begin
8059 Find_Depth (P1, D1);
8060 Find_Depth (P2, D2);
8062 if P1 /= P2 then
8063 return False;
8064 else
8065 P1 := N1;
8066 P2 := N2;
8067 end if;
8069 while D1 > D2 loop
8070 P1 := True_Parent (P1);
8071 D1 := D1 - 1;
8072 end loop;
8074 while D2 > D1 loop
8075 P2 := True_Parent (P2);
8076 D2 := D2 - 1;
8077 end loop;
8079 -- At this point P1 and P2 are at the same distance from the root.
8080 -- We examine their parents until we find a common declarative list.
8081 -- If we reach the root, N1 and N2 do not descend from the same
8082 -- declarative list (e.g. one is nested in the declarative part and
8083 -- the other is in a block in the statement part) and the earlier
8084 -- one is already frozen.
8086 while not Is_List_Member (P1)
8087 or else not Is_List_Member (P2)
8088 or else List_Containing (P1) /= List_Containing (P2)
8089 loop
8090 P1 := True_Parent (P1);
8091 P2 := True_Parent (P2);
8093 if Nkind (Parent (P1)) = N_Subunit then
8094 P1 := Corresponding_Stub (Parent (P1));
8095 end if;
8097 if Nkind (Parent (P2)) = N_Subunit then
8098 P2 := Corresponding_Stub (Parent (P2));
8099 end if;
8101 if P1 = P2 then
8102 return False;
8103 end if;
8104 end loop;
8106 -- Expanded code usually shares the source location of the original
8107 -- construct it was generated for. This however may not necessarily
8108 -- reflect the true location of the code within the tree.
8110 -- Before comparing the slocs of the two nodes, make sure that we are
8111 -- working with correct source locations. Assume that P1 is to the left
8112 -- of P2. If either one does not come from source, traverse the common
8113 -- list heading towards the other node and locate the first source
8114 -- statement.
8116 -- P1 P2
8117 -- ----+===+===+--------------+===+===+----
8118 -- expanded code expanded code
8120 if not Comes_From_Source (P1) then
8121 while Present (P1) loop
8123 -- Neither P2 nor a source statement were located during the
8124 -- search. If we reach the end of the list, then P1 does not
8125 -- occur earlier than P2.
8127 -- ---->
8128 -- start --- P2 ----- P1 --- end
8130 if No (Next (P1)) then
8131 return False;
8133 -- We encounter P2 while going to the right of the list. This
8134 -- means that P1 does indeed appear earlier.
8136 -- ---->
8137 -- start --- P1 ===== P2 --- end
8138 -- expanded code in between
8140 elsif P1 = P2 then
8141 return True;
8143 -- No need to look any further since we have located a source
8144 -- statement.
8146 elsif Comes_From_Source (P1) then
8147 exit;
8148 end if;
8150 -- Keep going right
8152 Next (P1);
8153 end loop;
8154 end if;
8156 if not Comes_From_Source (P2) then
8157 while Present (P2) loop
8159 -- Neither P1 nor a source statement were located during the
8160 -- search. If we reach the start of the list, then P1 does not
8161 -- occur earlier than P2.
8163 -- <----
8164 -- start --- P2 --- P1 --- end
8166 if No (Prev (P2)) then
8167 return False;
8169 -- We encounter P1 while going to the left of the list. This
8170 -- means that P1 does indeed appear earlier.
8172 -- <----
8173 -- start --- P1 ===== P2 --- end
8174 -- expanded code in between
8176 elsif P2 = P1 then
8177 return True;
8179 -- No need to look any further since we have located a source
8180 -- statement.
8182 elsif Comes_From_Source (P2) then
8183 exit;
8184 end if;
8186 -- Keep going left
8188 Prev (P2);
8189 end loop;
8190 end if;
8192 -- At this point either both nodes came from source or we approximated
8193 -- their source locations through neighboring source statements.
8195 T1 := Top_Level_Location (Sloc (P1));
8196 T2 := Top_Level_Location (Sloc (P2));
8198 -- When two nodes come from the same instance, they have identical top
8199 -- level locations. To determine proper relation within the tree, check
8200 -- their locations within the template.
8202 if T1 = T2 then
8203 return Sloc (P1) < Sloc (P2);
8205 -- The two nodes either come from unrelated instances or do not come
8206 -- from instantiated code at all.
8208 else
8209 return T1 < T2;
8210 end if;
8211 end Earlier;
8213 ----------------------
8214 -- Find_Actual_Type --
8215 ----------------------
8217 function Find_Actual_Type
8218 (Typ : Entity_Id;
8219 Gen_Type : Entity_Id) return Entity_Id
8221 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8222 T : Entity_Id;
8224 begin
8225 -- Special processing only applies to child units
8227 if not Is_Child_Unit (Gen_Scope) then
8228 return Get_Instance_Of (Typ);
8230 -- If designated or component type is itself a formal of the child unit,
8231 -- its instance is available.
8233 elsif Scope (Typ) = Gen_Scope then
8234 return Get_Instance_Of (Typ);
8236 -- If the array or access type is not declared in the parent unit,
8237 -- no special processing needed.
8239 elsif not Is_Generic_Type (Typ)
8240 and then Scope (Gen_Scope) /= Scope (Typ)
8241 then
8242 return Get_Instance_Of (Typ);
8244 -- Otherwise, retrieve designated or component type by visibility
8246 else
8247 T := Current_Entity (Typ);
8248 while Present (T) loop
8249 if In_Open_Scopes (Scope (T)) then
8250 return T;
8251 elsif Is_Generic_Actual_Type (T) then
8252 return T;
8253 end if;
8255 T := Homonym (T);
8256 end loop;
8258 return Typ;
8259 end if;
8260 end Find_Actual_Type;
8262 ----------------------------
8263 -- Freeze_Subprogram_Body --
8264 ----------------------------
8266 procedure Freeze_Subprogram_Body
8267 (Inst_Node : Node_Id;
8268 Gen_Body : Node_Id;
8269 Pack_Id : Entity_Id)
8271 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8272 Par : constant Entity_Id := Scope (Gen_Unit);
8273 E_G_Id : Entity_Id;
8274 Enc_G : Entity_Id;
8275 Enc_I : Node_Id;
8276 F_Node : Node_Id;
8278 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8279 -- Find innermost package body that encloses the given node, and which
8280 -- is not a compilation unit. Freeze nodes for the instance, or for its
8281 -- enclosing body, may be inserted after the enclosing_body of the
8282 -- generic unit. Used to determine proper placement of freeze node for
8283 -- both package and subprogram instances.
8285 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8286 -- Find entity for given package body, and locate or create a freeze
8287 -- node for it.
8289 ----------------------------
8290 -- Enclosing_Package_Body --
8291 ----------------------------
8293 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8294 P : Node_Id;
8296 begin
8297 P := Parent (N);
8298 while Present (P)
8299 and then Nkind (Parent (P)) /= N_Compilation_Unit
8300 loop
8301 if Nkind (P) = N_Package_Body then
8302 if Nkind (Parent (P)) = N_Subunit then
8303 return Corresponding_Stub (Parent (P));
8304 else
8305 return P;
8306 end if;
8307 end if;
8309 P := True_Parent (P);
8310 end loop;
8312 return Empty;
8313 end Enclosing_Package_Body;
8315 -------------------------
8316 -- Package_Freeze_Node --
8317 -------------------------
8319 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8320 Id : Entity_Id;
8322 begin
8323 if Nkind (B) = N_Package_Body then
8324 Id := Corresponding_Spec (B);
8325 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8326 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8327 end if;
8329 Ensure_Freeze_Node (Id);
8330 return Freeze_Node (Id);
8331 end Package_Freeze_Node;
8333 -- Start of processing for Freeze_Subprogram_Body
8335 begin
8336 -- If the instance and the generic body appear within the same unit, and
8337 -- the instance precedes the generic, the freeze node for the instance
8338 -- must appear after that of the generic. If the generic is nested
8339 -- within another instance I2, then current instance must be frozen
8340 -- after I2. In both cases, the freeze nodes are those of enclosing
8341 -- packages. Otherwise, the freeze node is placed at the end of the
8342 -- current declarative part.
8344 Enc_G := Enclosing_Package_Body (Gen_Body);
8345 Enc_I := Enclosing_Package_Body (Inst_Node);
8346 Ensure_Freeze_Node (Pack_Id);
8347 F_Node := Freeze_Node (Pack_Id);
8349 if Is_Generic_Instance (Par)
8350 and then Present (Freeze_Node (Par))
8351 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8352 then
8353 -- The parent was a premature instantiation. Insert freeze node at
8354 -- the end the current declarative part.
8356 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8357 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8359 -- Handle the following case:
8361 -- package Parent_Inst is new ...
8362 -- Parent_Inst []
8364 -- procedure P ... -- this body freezes Parent_Inst
8366 -- package Inst is new ...
8368 -- In this particular scenario, the freeze node for Inst must be
8369 -- inserted in the same manner as that of Parent_Inst - before the
8370 -- next source body or at the end of the declarative list (body not
8371 -- available). If body P did not exist and Parent_Inst was frozen
8372 -- after Inst, either by a body following Inst or at the end of the
8373 -- declarative region, the freeze node for Inst must be inserted
8374 -- after that of Parent_Inst. This relation is established by
8375 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8377 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8378 List_Containing (Inst_Node)
8379 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8380 then
8381 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8383 else
8384 Insert_After (Freeze_Node (Par), F_Node);
8385 end if;
8387 -- The body enclosing the instance should be frozen after the body that
8388 -- includes the generic, because the body of the instance may make
8389 -- references to entities therein. If the two are not in the same
8390 -- declarative part, or if the one enclosing the instance is frozen
8391 -- already, freeze the instance at the end of the current declarative
8392 -- part.
8394 elsif Is_Generic_Instance (Par)
8395 and then Present (Freeze_Node (Par))
8396 and then Present (Enc_I)
8397 then
8398 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8399 or else
8400 (Nkind (Enc_I) = N_Package_Body
8401 and then
8402 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8403 then
8404 -- The enclosing package may contain several instances. Rather
8405 -- than computing the earliest point at which to insert its freeze
8406 -- node, we place it at the end of the declarative part of the
8407 -- parent of the generic.
8409 Insert_Freeze_Node_For_Instance
8410 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8411 end if;
8413 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8415 elsif Present (Enc_G)
8416 and then Present (Enc_I)
8417 and then Enc_G /= Enc_I
8418 and then Earlier (Inst_Node, Gen_Body)
8419 then
8420 if Nkind (Enc_G) = N_Package_Body then
8421 E_G_Id :=
8422 Corresponding_Spec (Enc_G);
8423 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8424 E_G_Id :=
8425 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8426 end if;
8428 -- Freeze package that encloses instance, and place node after the
8429 -- package that encloses generic. If enclosing package is already
8430 -- frozen we have to assume it is at the proper place. This may be a
8431 -- potential ABE that requires dynamic checking. Do not add a freeze
8432 -- node if the package that encloses the generic is inside the body
8433 -- that encloses the instance, because the freeze node would be in
8434 -- the wrong scope. Additional contortions needed if the bodies are
8435 -- within a subunit.
8437 declare
8438 Enclosing_Body : Node_Id;
8440 begin
8441 if Nkind (Enc_I) = N_Package_Body_Stub then
8442 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8443 else
8444 Enclosing_Body := Enc_I;
8445 end if;
8447 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8448 Insert_Freeze_Node_For_Instance
8449 (Enc_G, Package_Freeze_Node (Enc_I));
8450 end if;
8451 end;
8453 -- Freeze enclosing subunit before instance
8455 Ensure_Freeze_Node (E_G_Id);
8457 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8458 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8459 end if;
8461 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8463 else
8464 -- If none of the above, insert freeze node at the end of the current
8465 -- declarative part.
8467 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8468 end if;
8469 end Freeze_Subprogram_Body;
8471 ----------------
8472 -- Get_Gen_Id --
8473 ----------------
8475 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8476 begin
8477 return Generic_Renamings.Table (E).Gen_Id;
8478 end Get_Gen_Id;
8480 ---------------------
8481 -- Get_Instance_Of --
8482 ---------------------
8484 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8485 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8487 begin
8488 if Res /= Assoc_Null then
8489 return Generic_Renamings.Table (Res).Act_Id;
8491 else
8492 -- On exit, entity is not instantiated: not a generic parameter, or
8493 -- else parameter of an inner generic unit.
8495 return A;
8496 end if;
8497 end Get_Instance_Of;
8499 ------------------------------------
8500 -- Get_Package_Instantiation_Node --
8501 ------------------------------------
8503 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8504 Decl : Node_Id := Unit_Declaration_Node (A);
8505 Inst : Node_Id;
8507 begin
8508 -- If the Package_Instantiation attribute has been set on the package
8509 -- entity, then use it directly when it (or its Original_Node) refers
8510 -- to an N_Package_Instantiation node. In principle it should be
8511 -- possible to have this field set in all cases, which should be
8512 -- investigated, and would allow this function to be significantly
8513 -- simplified. ???
8515 Inst := Package_Instantiation (A);
8517 if Present (Inst) then
8518 if Nkind (Inst) = N_Package_Instantiation then
8519 return Inst;
8521 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8522 return Original_Node (Inst);
8523 end if;
8524 end if;
8526 -- If the instantiation is a compilation unit that does not need body
8527 -- then the instantiation node has been rewritten as a package
8528 -- declaration for the instance, and we return the original node.
8530 -- If it is a compilation unit and the instance node has not been
8531 -- rewritten, then it is still the unit of the compilation. Finally, if
8532 -- a body is present, this is a parent of the main unit whose body has
8533 -- been compiled for inlining purposes, and the instantiation node has
8534 -- been rewritten with the instance body.
8536 -- Otherwise the instantiation node appears after the declaration. If
8537 -- the entity is a formal package, the declaration may have been
8538 -- rewritten as a generic declaration (in the case of a formal with box)
8539 -- or left as a formal package declaration if it has actuals, and is
8540 -- found with a forward search.
8542 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8543 if Nkind (Decl) = N_Package_Declaration
8544 and then Present (Corresponding_Body (Decl))
8545 then
8546 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8547 end if;
8549 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8550 return Original_Node (Decl);
8551 else
8552 return Unit (Parent (Decl));
8553 end if;
8555 elsif Nkind (Decl) = N_Package_Declaration
8556 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8557 then
8558 return Original_Node (Decl);
8560 else
8561 Inst := Next (Decl);
8562 while not Nkind_In (Inst, N_Package_Instantiation,
8563 N_Formal_Package_Declaration)
8564 loop
8565 Next (Inst);
8566 end loop;
8568 return Inst;
8569 end if;
8570 end Get_Package_Instantiation_Node;
8572 ------------------------
8573 -- Has_Been_Exchanged --
8574 ------------------------
8576 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8577 Next : Elmt_Id;
8579 begin
8580 Next := First_Elmt (Exchanged_Views);
8581 while Present (Next) loop
8582 if Full_View (Node (Next)) = E then
8583 return True;
8584 end if;
8586 Next_Elmt (Next);
8587 end loop;
8589 return False;
8590 end Has_Been_Exchanged;
8592 ----------
8593 -- Hash --
8594 ----------
8596 function Hash (F : Entity_Id) return HTable_Range is
8597 begin
8598 return HTable_Range (F mod HTable_Size);
8599 end Hash;
8601 ------------------------
8602 -- Hide_Current_Scope --
8603 ------------------------
8605 procedure Hide_Current_Scope is
8606 C : constant Entity_Id := Current_Scope;
8607 E : Entity_Id;
8609 begin
8610 Set_Is_Hidden_Open_Scope (C);
8612 E := First_Entity (C);
8613 while Present (E) loop
8614 if Is_Immediately_Visible (E) then
8615 Set_Is_Immediately_Visible (E, False);
8616 Append_Elmt (E, Hidden_Entities);
8617 end if;
8619 Next_Entity (E);
8620 end loop;
8622 -- Make the scope name invisible as well. This is necessary, but might
8623 -- conflict with calls to Rtsfind later on, in case the scope is a
8624 -- predefined one. There is no clean solution to this problem, so for
8625 -- now we depend on the user not redefining Standard itself in one of
8626 -- the parent units.
8628 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8629 Set_Is_Immediately_Visible (C, False);
8630 Append_Elmt (C, Hidden_Entities);
8631 end if;
8633 end Hide_Current_Scope;
8635 --------------
8636 -- Init_Env --
8637 --------------
8639 procedure Init_Env is
8640 Saved : Instance_Env;
8642 begin
8643 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8644 Saved.Exchanged_Views := Exchanged_Views;
8645 Saved.Hidden_Entities := Hidden_Entities;
8646 Saved.Current_Sem_Unit := Current_Sem_Unit;
8647 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8648 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8650 -- Save configuration switches. These may be reset if the unit is a
8651 -- predefined unit, and the current mode is not Ada 2005.
8653 Save_Opt_Config_Switches (Saved.Switches);
8655 Instance_Envs.Append (Saved);
8657 Exchanged_Views := New_Elmt_List;
8658 Hidden_Entities := New_Elmt_List;
8660 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8661 -- this is set properly in Set_Instance_Env.
8663 Current_Instantiated_Parent :=
8664 (Current_Scope, Current_Scope, Assoc_Null);
8665 end Init_Env;
8667 ------------------------------
8668 -- In_Same_Declarative_Part --
8669 ------------------------------
8671 function In_Same_Declarative_Part
8672 (F_Node : Node_Id;
8673 Inst : Node_Id) return Boolean
8675 Decls : constant Node_Id := Parent (F_Node);
8676 Nod : Node_Id;
8678 begin
8679 Nod := Parent (Inst);
8680 while Present (Nod) loop
8681 if Nod = Decls then
8682 return True;
8684 elsif Nkind_In (Nod, N_Subprogram_Body,
8685 N_Package_Body,
8686 N_Package_Declaration,
8687 N_Task_Body,
8688 N_Protected_Body,
8689 N_Block_Statement)
8690 then
8691 return False;
8693 elsif Nkind (Nod) = N_Subunit then
8694 Nod := Corresponding_Stub (Nod);
8696 elsif Nkind (Nod) = N_Compilation_Unit then
8697 return False;
8699 else
8700 Nod := Parent (Nod);
8701 end if;
8702 end loop;
8704 return False;
8705 end In_Same_Declarative_Part;
8707 ---------------------
8708 -- In_Main_Context --
8709 ---------------------
8711 function In_Main_Context (E : Entity_Id) return Boolean is
8712 Context : List_Id;
8713 Clause : Node_Id;
8714 Nam : Node_Id;
8716 begin
8717 if not Is_Compilation_Unit (E)
8718 or else Ekind (E) /= E_Package
8719 or else In_Private_Part (E)
8720 then
8721 return False;
8722 end if;
8724 Context := Context_Items (Cunit (Main_Unit));
8726 Clause := First (Context);
8727 while Present (Clause) loop
8728 if Nkind (Clause) = N_With_Clause then
8729 Nam := Name (Clause);
8731 -- If the current scope is part of the context of the main unit,
8732 -- analysis of the corresponding with_clause is not complete, and
8733 -- the entity is not set. We use the Chars field directly, which
8734 -- might produce false positives in rare cases, but guarantees
8735 -- that we produce all the instance bodies we will need.
8737 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8738 or else (Nkind (Nam) = N_Selected_Component
8739 and then Chars (Selector_Name (Nam)) = Chars (E))
8740 then
8741 return True;
8742 end if;
8743 end if;
8745 Next (Clause);
8746 end loop;
8748 return False;
8749 end In_Main_Context;
8751 ---------------------
8752 -- Inherit_Context --
8753 ---------------------
8755 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8756 Current_Context : List_Id;
8757 Current_Unit : Node_Id;
8758 Item : Node_Id;
8759 New_I : Node_Id;
8761 Clause : Node_Id;
8762 OK : Boolean;
8763 Lib_Unit : Node_Id;
8765 begin
8766 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8768 -- The inherited context is attached to the enclosing compilation
8769 -- unit. This is either the main unit, or the declaration for the
8770 -- main unit (in case the instantiation appears within the package
8771 -- declaration and the main unit is its body).
8773 Current_Unit := Parent (Inst);
8774 while Present (Current_Unit)
8775 and then Nkind (Current_Unit) /= N_Compilation_Unit
8776 loop
8777 Current_Unit := Parent (Current_Unit);
8778 end loop;
8780 Current_Context := Context_Items (Current_Unit);
8782 Item := First (Context_Items (Parent (Gen_Decl)));
8783 while Present (Item) loop
8784 if Nkind (Item) = N_With_Clause then
8785 Lib_Unit := Library_Unit (Item);
8787 -- Take care to prevent direct cyclic with's
8789 if Lib_Unit /= Current_Unit then
8791 -- Do not add a unit if it is already in the context
8793 Clause := First (Current_Context);
8794 OK := True;
8795 while Present (Clause) loop
8796 if Nkind (Clause) = N_With_Clause and then
8797 Library_Unit (Clause) = Lib_Unit
8798 then
8799 OK := False;
8800 exit;
8801 end if;
8803 Next (Clause);
8804 end loop;
8806 if OK then
8807 New_I := New_Copy (Item);
8808 Set_Implicit_With (New_I, True);
8809 Set_Implicit_With_From_Instantiation (New_I, True);
8810 Append (New_I, Current_Context);
8811 end if;
8812 end if;
8813 end if;
8815 Next (Item);
8816 end loop;
8817 end if;
8818 end Inherit_Context;
8820 ----------------
8821 -- Initialize --
8822 ----------------
8824 procedure Initialize is
8825 begin
8826 Generic_Renamings.Init;
8827 Instance_Envs.Init;
8828 Generic_Flags.Init;
8829 Generic_Renamings_HTable.Reset;
8830 Circularity_Detected := False;
8831 Exchanged_Views := No_Elist;
8832 Hidden_Entities := No_Elist;
8833 end Initialize;
8835 -------------------------------------
8836 -- Insert_Freeze_Node_For_Instance --
8837 -------------------------------------
8839 procedure Insert_Freeze_Node_For_Instance
8840 (N : Node_Id;
8841 F_Node : Node_Id)
8843 Decl : Node_Id;
8844 Decls : List_Id;
8845 Inst : Entity_Id;
8846 Par_N : Node_Id;
8848 function Enclosing_Body (N : Node_Id) return Node_Id;
8849 -- Find enclosing package or subprogram body, if any. Freeze node may
8850 -- be placed at end of current declarative list if previous instance
8851 -- and current one have different enclosing bodies.
8853 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8854 -- Find the local instance, if any, that declares the generic that is
8855 -- being instantiated. If present, the freeze node for this instance
8856 -- must follow the freeze node for the previous instance.
8858 --------------------
8859 -- Enclosing_Body --
8860 --------------------
8862 function Enclosing_Body (N : Node_Id) return Node_Id is
8863 P : Node_Id;
8865 begin
8866 P := Parent (N);
8867 while Present (P)
8868 and then Nkind (Parent (P)) /= N_Compilation_Unit
8869 loop
8870 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8871 if Nkind (Parent (P)) = N_Subunit then
8872 return Corresponding_Stub (Parent (P));
8873 else
8874 return P;
8875 end if;
8876 end if;
8878 P := True_Parent (P);
8879 end loop;
8881 return Empty;
8882 end Enclosing_Body;
8884 -----------------------
8885 -- Previous_Instance --
8886 -----------------------
8888 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8889 S : Entity_Id;
8891 begin
8892 S := Scope (Gen);
8893 while Present (S) and then S /= Standard_Standard loop
8894 if Is_Generic_Instance (S)
8895 and then In_Same_Source_Unit (S, N)
8896 then
8897 return S;
8898 end if;
8900 S := Scope (S);
8901 end loop;
8903 return Empty;
8904 end Previous_Instance;
8906 -- Start of processing for Insert_Freeze_Node_For_Instance
8908 begin
8909 if not Is_List_Member (F_Node) then
8910 Decl := N;
8911 Decls := List_Containing (N);
8912 Inst := Entity (F_Node);
8913 Par_N := Parent (Decls);
8915 -- When processing a subprogram instantiation, utilize the actual
8916 -- subprogram instantiation rather than its package wrapper as it
8917 -- carries all the context information.
8919 if Is_Wrapper_Package (Inst) then
8920 Inst := Related_Instance (Inst);
8921 end if;
8923 -- If this is a package instance, check whether the generic is
8924 -- declared in a previous instance and the current instance is
8925 -- not within the previous one.
8927 if Present (Generic_Parent (Parent (Inst)))
8928 and then Is_In_Main_Unit (N)
8929 then
8930 declare
8931 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8932 Par_I : constant Entity_Id :=
8933 Previous_Instance
8934 (Generic_Parent (Parent (Inst)));
8935 Scop : Entity_Id;
8937 begin
8938 if Present (Par_I)
8939 and then Earlier (N, Freeze_Node (Par_I))
8940 then
8941 Scop := Scope (Inst);
8943 -- If the current instance is within the one that contains
8944 -- the generic, the freeze node for the current one must
8945 -- appear in the current declarative part. Ditto, if the
8946 -- current instance is within another package instance or
8947 -- within a body that does not enclose the current instance.
8948 -- In these three cases the freeze node of the previous
8949 -- instance is not relevant.
8951 while Present (Scop) and then Scop /= Standard_Standard loop
8952 exit when Scop = Par_I
8953 or else
8954 (Is_Generic_Instance (Scop)
8955 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8956 Scop := Scope (Scop);
8957 end loop;
8959 -- Previous instance encloses current instance
8961 if Scop = Par_I then
8962 null;
8964 -- If the next node is a source body we must freeze in
8965 -- the current scope as well.
8967 elsif Present (Next (N))
8968 and then Nkind_In (Next (N), N_Subprogram_Body,
8969 N_Package_Body)
8970 and then Comes_From_Source (Next (N))
8971 then
8972 null;
8974 -- Current instance is within an unrelated instance
8976 elsif Is_Generic_Instance (Scop) then
8977 null;
8979 -- Current instance is within an unrelated body
8981 elsif Present (Enclosing_N)
8982 and then Enclosing_N /= Enclosing_Body (Par_I)
8983 then
8984 null;
8986 else
8987 Insert_After (Freeze_Node (Par_I), F_Node);
8988 return;
8989 end if;
8990 end if;
8991 end;
8992 end if;
8994 -- When the instantiation occurs in a package declaration, append the
8995 -- freeze node to the private declarations (if any).
8997 if Nkind (Par_N) = N_Package_Specification
8998 and then Decls = Visible_Declarations (Par_N)
8999 and then Present (Private_Declarations (Par_N))
9000 and then not Is_Empty_List (Private_Declarations (Par_N))
9001 then
9002 Decls := Private_Declarations (Par_N);
9003 Decl := First (Decls);
9004 end if;
9006 -- Determine the proper freeze point of a package instantiation. We
9007 -- adhere to the general rule of a package or subprogram body causing
9008 -- freezing of anything before it in the same declarative region. In
9009 -- this case, the proper freeze point of a package instantiation is
9010 -- before the first source body which follows, or before a stub. This
9011 -- ensures that entities coming from the instance are already frozen
9012 -- and usable in source bodies.
9014 if Nkind (Par_N) /= N_Package_Declaration
9015 and then Ekind (Inst) = E_Package
9016 and then Is_Generic_Instance (Inst)
9017 and then
9018 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9019 then
9020 while Present (Decl) loop
9021 if (Nkind (Decl) in N_Unit_Body
9022 or else
9023 Nkind (Decl) in N_Body_Stub)
9024 and then Comes_From_Source (Decl)
9025 then
9026 Insert_Before (Decl, F_Node);
9027 return;
9028 end if;
9030 Next (Decl);
9031 end loop;
9032 end if;
9034 -- In a package declaration, or if no previous body, insert at end
9035 -- of list.
9037 Set_Sloc (F_Node, Sloc (Last (Decls)));
9038 Insert_After (Last (Decls), F_Node);
9039 end if;
9040 end Insert_Freeze_Node_For_Instance;
9042 ------------------
9043 -- Install_Body --
9044 ------------------
9046 procedure Install_Body
9047 (Act_Body : Node_Id;
9048 N : Node_Id;
9049 Gen_Body : Node_Id;
9050 Gen_Decl : Node_Id)
9052 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9053 -- Check if the generic definition and the instantiation come from
9054 -- a common scope, in which case the instance must be frozen after
9055 -- the generic body.
9057 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9058 -- If the instance is nested inside a generic unit, the Sloc of the
9059 -- instance indicates the place of the original definition, not the
9060 -- point of the current enclosing instance. Pending a better usage of
9061 -- Slocs to indicate instantiation places, we determine the place of
9062 -- origin of a node by finding the maximum sloc of any ancestor node.
9063 -- Why is this not equivalent to Top_Level_Location ???
9065 -------------------
9066 -- In_Same_Scope --
9067 -------------------
9069 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9070 Act_Scop : Entity_Id := Scope (Act_Id);
9071 Gen_Scop : Entity_Id := Scope (Gen_Id);
9073 begin
9074 while Act_Scop /= Standard_Standard
9075 and then Gen_Scop /= Standard_Standard
9076 loop
9077 if Act_Scop = Gen_Scop then
9078 return True;
9079 end if;
9081 Act_Scop := Scope (Act_Scop);
9082 Gen_Scop := Scope (Gen_Scop);
9083 end loop;
9085 return False;
9086 end In_Same_Scope;
9088 ---------------
9089 -- True_Sloc --
9090 ---------------
9092 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9093 N1 : Node_Id;
9094 Res : Source_Ptr;
9096 begin
9097 Res := Sloc (N);
9098 N1 := N;
9099 while Present (N1) and then N1 /= Act_Unit loop
9100 if Sloc (N1) > Res then
9101 Res := Sloc (N1);
9102 end if;
9104 N1 := Parent (N1);
9105 end loop;
9107 return Res;
9108 end True_Sloc;
9110 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9111 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9112 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9113 Par : constant Entity_Id := Scope (Gen_Id);
9114 Gen_Unit : constant Node_Id :=
9115 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9117 Body_Unit : Node_Id;
9118 F_Node : Node_Id;
9119 Must_Delay : Boolean;
9120 Orig_Body : Node_Id := Gen_Body;
9122 -- Start of processing for Install_Body
9124 begin
9125 -- Handle first the case of an instance with incomplete actual types.
9126 -- The instance body cannot be placed after the declaration because
9127 -- full views have not been seen yet. Any use of the non-limited views
9128 -- in the instance body requires the presence of a regular with_clause
9129 -- in the enclosing unit, and will fail if this with_clause is missing.
9130 -- We place the instance body at the beginning of the enclosing body,
9131 -- which is the unit being compiled. The freeze node for the instance
9132 -- is then placed after the instance body.
9134 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9135 and then Expander_Active
9136 and then Ekind (Scope (Act_Id)) = E_Package
9137 then
9138 declare
9139 Scop : constant Entity_Id := Scope (Act_Id);
9140 Body_Id : constant Node_Id :=
9141 Corresponding_Body (Unit_Declaration_Node (Scop));
9143 begin
9144 Ensure_Freeze_Node (Act_Id);
9145 F_Node := Freeze_Node (Act_Id);
9146 if Present (Body_Id) then
9147 Set_Is_Frozen (Act_Id, False);
9148 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9149 if Is_List_Member (F_Node) then
9150 Remove (F_Node);
9151 end if;
9153 Insert_After (Act_Body, F_Node);
9154 end if;
9155 end;
9156 return;
9157 end if;
9159 -- If the body is a subunit, the freeze point is the corresponding stub
9160 -- in the current compilation, not the subunit itself.
9162 if Nkind (Parent (Gen_Body)) = N_Subunit then
9163 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9164 else
9165 Orig_Body := Gen_Body;
9166 end if;
9168 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9170 -- If the instantiation and the generic definition appear in the same
9171 -- package declaration, this is an early instantiation. If they appear
9172 -- in the same declarative part, it is an early instantiation only if
9173 -- the generic body appears textually later, and the generic body is
9174 -- also in the main unit.
9176 -- If instance is nested within a subprogram, and the generic body
9177 -- is not, the instance is delayed because the enclosing body is. If
9178 -- instance and body are within the same scope, or the same subprogram
9179 -- body, indicate explicitly that the instance is delayed.
9181 Must_Delay :=
9182 (Gen_Unit = Act_Unit
9183 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9184 N_Package_Declaration)
9185 or else (Gen_Unit = Body_Unit
9186 and then True_Sloc (N, Act_Unit)
9187 < Sloc (Orig_Body)))
9188 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9189 and then In_Same_Scope (Gen_Id, Act_Id));
9191 -- If this is an early instantiation, the freeze node is placed after
9192 -- the generic body. Otherwise, if the generic appears in an instance,
9193 -- we cannot freeze the current instance until the outer one is frozen.
9194 -- This is only relevant if the current instance is nested within some
9195 -- inner scope not itself within the outer instance. If this scope is
9196 -- a package body in the same declarative part as the outer instance,
9197 -- then that body needs to be frozen after the outer instance. Finally,
9198 -- if no delay is needed, we place the freeze node at the end of the
9199 -- current declarative part.
9201 if Expander_Active then
9202 Ensure_Freeze_Node (Act_Id);
9203 F_Node := Freeze_Node (Act_Id);
9205 if Must_Delay then
9206 Insert_After (Orig_Body, F_Node);
9208 elsif Is_Generic_Instance (Par)
9209 and then Present (Freeze_Node (Par))
9210 and then Scope (Act_Id) /= Par
9211 then
9212 -- Freeze instance of inner generic after instance of enclosing
9213 -- generic.
9215 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9217 -- Handle the following case:
9219 -- package Parent_Inst is new ...
9220 -- Parent_Inst []
9222 -- procedure P ... -- this body freezes Parent_Inst
9224 -- package Inst is new ...
9226 -- In this particular scenario, the freeze node for Inst must
9227 -- be inserted in the same manner as that of Parent_Inst,
9228 -- before the next source body or at the end of the declarative
9229 -- list (body not available). If body P did not exist and
9230 -- Parent_Inst was frozen after Inst, either by a body
9231 -- following Inst or at the end of the declarative region,
9232 -- the freeze node for Inst must be inserted after that of
9233 -- Parent_Inst. This relation is established by comparing
9234 -- the Slocs of Parent_Inst freeze node and Inst.
9236 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9237 List_Containing (N)
9238 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9239 then
9240 Insert_Freeze_Node_For_Instance (N, F_Node);
9241 else
9242 Insert_After (Freeze_Node (Par), F_Node);
9243 end if;
9245 -- Freeze package enclosing instance of inner generic after
9246 -- instance of enclosing generic.
9248 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9249 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9250 then
9251 declare
9252 Enclosing : Entity_Id;
9254 begin
9255 Enclosing := Corresponding_Spec (Parent (N));
9257 if No (Enclosing) then
9258 Enclosing := Defining_Entity (Parent (N));
9259 end if;
9261 Insert_Freeze_Node_For_Instance (N, F_Node);
9262 Ensure_Freeze_Node (Enclosing);
9264 if not Is_List_Member (Freeze_Node (Enclosing)) then
9266 -- The enclosing context is a subunit, insert the freeze
9267 -- node after the stub.
9269 if Nkind (Parent (Parent (N))) = N_Subunit then
9270 Insert_Freeze_Node_For_Instance
9271 (Corresponding_Stub (Parent (Parent (N))),
9272 Freeze_Node (Enclosing));
9274 -- The enclosing context is a package with a stub body
9275 -- which has already been replaced by the real body.
9276 -- Insert the freeze node after the actual body.
9278 elsif Ekind (Enclosing) = E_Package
9279 and then Present (Body_Entity (Enclosing))
9280 and then Was_Originally_Stub
9281 (Parent (Body_Entity (Enclosing)))
9282 then
9283 Insert_Freeze_Node_For_Instance
9284 (Parent (Body_Entity (Enclosing)),
9285 Freeze_Node (Enclosing));
9287 -- The parent instance has been frozen before the body of
9288 -- the enclosing package, insert the freeze node after
9289 -- the body.
9291 elsif List_Containing (Freeze_Node (Par)) =
9292 List_Containing (Parent (N))
9293 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9294 then
9295 Insert_Freeze_Node_For_Instance
9296 (Parent (N), Freeze_Node (Enclosing));
9298 else
9299 Insert_After
9300 (Freeze_Node (Par), Freeze_Node (Enclosing));
9301 end if;
9302 end if;
9303 end;
9305 else
9306 Insert_Freeze_Node_For_Instance (N, F_Node);
9307 end if;
9309 else
9310 Insert_Freeze_Node_For_Instance (N, F_Node);
9311 end if;
9312 end if;
9314 Set_Is_Frozen (Act_Id);
9315 Insert_Before (N, Act_Body);
9316 Mark_Rewrite_Insertion (Act_Body);
9317 end Install_Body;
9319 -----------------------------
9320 -- Install_Formal_Packages --
9321 -----------------------------
9323 procedure Install_Formal_Packages (Par : Entity_Id) is
9324 E : Entity_Id;
9325 Gen : Entity_Id;
9326 Gen_E : Entity_Id := Empty;
9328 begin
9329 E := First_Entity (Par);
9331 -- If we are installing an instance parent, locate the formal packages
9332 -- of its generic parent.
9334 if Is_Generic_Instance (Par) then
9335 Gen := Generic_Parent (Package_Specification (Par));
9336 Gen_E := First_Entity (Gen);
9337 end if;
9339 while Present (E) loop
9340 if Ekind (E) = E_Package
9341 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9342 then
9343 -- If this is the renaming for the parent instance, done
9345 if Renamed_Object (E) = Par then
9346 exit;
9348 -- The visibility of a formal of an enclosing generic is already
9349 -- correct.
9351 elsif Denotes_Formal_Package (E) then
9352 null;
9354 elsif Present (Associated_Formal_Package (E)) then
9355 Check_Generic_Actuals (Renamed_Object (E), True);
9356 Set_Is_Hidden (E, False);
9358 -- Find formal package in generic unit that corresponds to
9359 -- (instance of) formal package in instance.
9361 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9362 Next_Entity (Gen_E);
9363 end loop;
9365 if Present (Gen_E) then
9366 Map_Formal_Package_Entities (Gen_E, E);
9367 end if;
9368 end if;
9369 end if;
9371 Next_Entity (E);
9373 if Present (Gen_E) then
9374 Next_Entity (Gen_E);
9375 end if;
9376 end loop;
9377 end Install_Formal_Packages;
9379 --------------------
9380 -- Install_Parent --
9381 --------------------
9383 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9384 Ancestors : constant Elist_Id := New_Elmt_List;
9385 S : constant Entity_Id := Current_Scope;
9386 Inst_Par : Entity_Id;
9387 First_Par : Entity_Id;
9388 Inst_Node : Node_Id;
9389 Gen_Par : Entity_Id;
9390 First_Gen : Entity_Id;
9391 Elmt : Elmt_Id;
9393 procedure Install_Noninstance_Specs (Par : Entity_Id);
9394 -- Install the scopes of noninstance parent units ending with Par
9396 procedure Install_Spec (Par : Entity_Id);
9397 -- The child unit is within the declarative part of the parent, so the
9398 -- declarations within the parent are immediately visible.
9400 -------------------------------
9401 -- Install_Noninstance_Specs --
9402 -------------------------------
9404 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9405 begin
9406 if Present (Par)
9407 and then Par /= Standard_Standard
9408 and then not In_Open_Scopes (Par)
9409 then
9410 Install_Noninstance_Specs (Scope (Par));
9411 Install_Spec (Par);
9412 end if;
9413 end Install_Noninstance_Specs;
9415 ------------------
9416 -- Install_Spec --
9417 ------------------
9419 procedure Install_Spec (Par : Entity_Id) is
9420 Spec : constant Node_Id := Package_Specification (Par);
9422 begin
9423 -- If this parent of the child instance is a top-level unit,
9424 -- then record the unit and its visibility for later resetting in
9425 -- Remove_Parent. We exclude units that are generic instances, as we
9426 -- only want to record this information for the ultimate top-level
9427 -- noninstance parent (is that always correct???).
9429 if Scope (Par) = Standard_Standard
9430 and then not Is_Generic_Instance (Par)
9431 then
9432 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9433 Instance_Parent_Unit := Par;
9434 end if;
9436 -- Open the parent scope and make it and its declarations visible.
9437 -- If this point is not within a body, then only the visible
9438 -- declarations should be made visible, and installation of the
9439 -- private declarations is deferred until the appropriate point
9440 -- within analysis of the spec being instantiated (see the handling
9441 -- of parent visibility in Analyze_Package_Specification). This is
9442 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9443 -- private view problems that occur when compiling instantiations of
9444 -- a generic child of that package (Generic_Dispatching_Constructor).
9445 -- If the instance freezes a tagged type, inlinings of operations
9446 -- from Ada.Tags may need the full view of type Tag. If inlining took
9447 -- proper account of establishing visibility of inlined subprograms'
9448 -- parents then it should be possible to remove this
9449 -- special check. ???
9451 Push_Scope (Par);
9452 Set_Is_Immediately_Visible (Par);
9453 Install_Visible_Declarations (Par);
9454 Set_Use (Visible_Declarations (Spec));
9456 if In_Body or else Is_RTU (Par, Ada_Tags) then
9457 Install_Private_Declarations (Par);
9458 Set_Use (Private_Declarations (Spec));
9459 end if;
9460 end Install_Spec;
9462 -- Start of processing for Install_Parent
9464 begin
9465 -- We need to install the parent instance to compile the instantiation
9466 -- of the child, but the child instance must appear in the current
9467 -- scope. Given that we cannot place the parent above the current scope
9468 -- in the scope stack, we duplicate the current scope and unstack both
9469 -- after the instantiation is complete.
9471 -- If the parent is itself the instantiation of a child unit, we must
9472 -- also stack the instantiation of its parent, and so on. Each such
9473 -- ancestor is the prefix of the name in a prior instantiation.
9475 -- If this is a nested instance, the parent unit itself resolves to
9476 -- a renaming of the parent instance, whose declaration we need.
9478 -- Finally, the parent may be a generic (not an instance) when the
9479 -- child unit appears as a formal package.
9481 Inst_Par := P;
9483 if Present (Renamed_Entity (Inst_Par)) then
9484 Inst_Par := Renamed_Entity (Inst_Par);
9485 end if;
9487 First_Par := Inst_Par;
9489 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9491 First_Gen := Gen_Par;
9493 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9495 -- Load grandparent instance as well
9497 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9499 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9500 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9502 if Present (Renamed_Entity (Inst_Par)) then
9503 Inst_Par := Renamed_Entity (Inst_Par);
9504 end if;
9506 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9508 if Present (Gen_Par) then
9509 Prepend_Elmt (Inst_Par, Ancestors);
9511 else
9512 -- Parent is not the name of an instantiation
9514 Install_Noninstance_Specs (Inst_Par);
9515 exit;
9516 end if;
9518 else
9519 -- Previous error
9521 exit;
9522 end if;
9523 end loop;
9525 if Present (First_Gen) then
9526 Append_Elmt (First_Par, Ancestors);
9527 else
9528 Install_Noninstance_Specs (First_Par);
9529 end if;
9531 if not Is_Empty_Elmt_List (Ancestors) then
9532 Elmt := First_Elmt (Ancestors);
9533 while Present (Elmt) loop
9534 Install_Spec (Node (Elmt));
9535 Install_Formal_Packages (Node (Elmt));
9536 Next_Elmt (Elmt);
9537 end loop;
9538 end if;
9540 if not In_Body then
9541 Push_Scope (S);
9542 end if;
9543 end Install_Parent;
9545 -------------------------------
9546 -- Install_Hidden_Primitives --
9547 -------------------------------
9549 procedure Install_Hidden_Primitives
9550 (Prims_List : in out Elist_Id;
9551 Gen_T : Entity_Id;
9552 Act_T : Entity_Id)
9554 Elmt : Elmt_Id;
9555 List : Elist_Id := No_Elist;
9556 Prim_G_Elmt : Elmt_Id;
9557 Prim_A_Elmt : Elmt_Id;
9558 Prim_G : Node_Id;
9559 Prim_A : Node_Id;
9561 begin
9562 -- No action needed in case of serious errors because we cannot trust
9563 -- in the order of primitives
9565 if Serious_Errors_Detected > 0 then
9566 return;
9568 -- No action possible if we don't have available the list of primitive
9569 -- operations
9571 elsif No (Gen_T)
9572 or else not Is_Record_Type (Gen_T)
9573 or else not Is_Tagged_Type (Gen_T)
9574 or else not Is_Record_Type (Act_T)
9575 or else not Is_Tagged_Type (Act_T)
9576 then
9577 return;
9579 -- There is no need to handle interface types since their primitives
9580 -- cannot be hidden
9582 elsif Is_Interface (Gen_T) then
9583 return;
9584 end if;
9586 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9588 if not Is_Class_Wide_Type (Act_T) then
9589 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9590 else
9591 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9592 end if;
9594 loop
9595 -- Skip predefined primitives in the generic formal
9597 while Present (Prim_G_Elmt)
9598 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9599 loop
9600 Next_Elmt (Prim_G_Elmt);
9601 end loop;
9603 -- Skip predefined primitives in the generic actual
9605 while Present (Prim_A_Elmt)
9606 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9607 loop
9608 Next_Elmt (Prim_A_Elmt);
9609 end loop;
9611 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9613 Prim_G := Node (Prim_G_Elmt);
9614 Prim_A := Node (Prim_A_Elmt);
9616 -- There is no need to handle interface primitives because their
9617 -- primitives are not hidden
9619 exit when Present (Interface_Alias (Prim_G));
9621 -- Here we install one hidden primitive
9623 if Chars (Prim_G) /= Chars (Prim_A)
9624 and then Has_Suffix (Prim_A, 'P')
9625 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9626 then
9627 Set_Chars (Prim_A, Chars (Prim_G));
9628 Append_New_Elmt (Prim_A, To => List);
9629 end if;
9631 Next_Elmt (Prim_A_Elmt);
9632 Next_Elmt (Prim_G_Elmt);
9633 end loop;
9635 -- Append the elements to the list of temporarily visible primitives
9636 -- avoiding duplicates.
9638 if Present (List) then
9639 if No (Prims_List) then
9640 Prims_List := New_Elmt_List;
9641 end if;
9643 Elmt := First_Elmt (List);
9644 while Present (Elmt) loop
9645 Append_Unique_Elmt (Node (Elmt), Prims_List);
9646 Next_Elmt (Elmt);
9647 end loop;
9648 end if;
9649 end Install_Hidden_Primitives;
9651 -------------------------------
9652 -- Restore_Hidden_Primitives --
9653 -------------------------------
9655 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9656 Prim_Elmt : Elmt_Id;
9657 Prim : Node_Id;
9659 begin
9660 if Prims_List /= No_Elist then
9661 Prim_Elmt := First_Elmt (Prims_List);
9662 while Present (Prim_Elmt) loop
9663 Prim := Node (Prim_Elmt);
9664 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9665 Next_Elmt (Prim_Elmt);
9666 end loop;
9668 Prims_List := No_Elist;
9669 end if;
9670 end Restore_Hidden_Primitives;
9672 --------------------------------
9673 -- Instantiate_Formal_Package --
9674 --------------------------------
9676 function Instantiate_Formal_Package
9677 (Formal : Node_Id;
9678 Actual : Node_Id;
9679 Analyzed_Formal : Node_Id) return List_Id
9681 Loc : constant Source_Ptr := Sloc (Actual);
9682 Actual_Pack : Entity_Id;
9683 Formal_Pack : Entity_Id;
9684 Gen_Parent : Entity_Id;
9685 Decls : List_Id;
9686 Nod : Node_Id;
9687 Parent_Spec : Node_Id;
9689 procedure Find_Matching_Actual
9690 (F : Node_Id;
9691 Act : in out Entity_Id);
9692 -- We need to associate each formal entity in the formal package with
9693 -- the corresponding entity in the actual package. The actual package
9694 -- has been analyzed and possibly expanded, and as a result there is
9695 -- no one-to-one correspondence between the two lists (for example,
9696 -- the actual may include subtypes, itypes, and inherited primitive
9697 -- operations, interspersed among the renaming declarations for the
9698 -- actuals). We retrieve the corresponding actual by name because each
9699 -- actual has the same name as the formal, and they do appear in the
9700 -- same order.
9702 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9703 -- Retrieve entity of defining entity of generic formal parameter.
9704 -- Only the declarations of formals need to be considered when
9705 -- linking them to actuals, but the declarative list may include
9706 -- internal entities generated during analysis, and those are ignored.
9708 procedure Match_Formal_Entity
9709 (Formal_Node : Node_Id;
9710 Formal_Ent : Entity_Id;
9711 Actual_Ent : Entity_Id);
9712 -- Associates the formal entity with the actual. In the case where
9713 -- Formal_Ent is a formal package, this procedure iterates through all
9714 -- of its formals and enters associations between the actuals occurring
9715 -- in the formal package's corresponding actual package (given by
9716 -- Actual_Ent) and the formal package's formal parameters. This
9717 -- procedure recurses if any of the parameters is itself a package.
9719 function Is_Instance_Of
9720 (Act_Spec : Entity_Id;
9721 Gen_Anc : Entity_Id) return Boolean;
9722 -- The actual can be an instantiation of a generic within another
9723 -- instance, in which case there is no direct link from it to the
9724 -- original generic ancestor. In that case, we recognize that the
9725 -- ultimate ancestor is the same by examining names and scopes.
9727 procedure Process_Nested_Formal (Formal : Entity_Id);
9728 -- If the current formal is declared with a box, its own formals are
9729 -- visible in the instance, as they were in the generic, and their
9730 -- Hidden flag must be reset. If some of these formals are themselves
9731 -- packages declared with a box, the processing must be recursive.
9733 --------------------------
9734 -- Find_Matching_Actual --
9735 --------------------------
9737 procedure Find_Matching_Actual
9738 (F : Node_Id;
9739 Act : in out Entity_Id)
9741 Formal_Ent : Entity_Id;
9743 begin
9744 case Nkind (Original_Node (F)) is
9745 when N_Formal_Object_Declaration
9746 | N_Formal_Type_Declaration
9748 Formal_Ent := Defining_Identifier (F);
9750 while Chars (Act) /= Chars (Formal_Ent) loop
9751 Next_Entity (Act);
9752 end loop;
9754 when N_Formal_Package_Declaration
9755 | N_Formal_Subprogram_Declaration
9756 | N_Generic_Package_Declaration
9757 | N_Package_Declaration
9759 Formal_Ent := Defining_Entity (F);
9761 while Chars (Act) /= Chars (Formal_Ent) loop
9762 Next_Entity (Act);
9763 end loop;
9765 when others =>
9766 raise Program_Error;
9767 end case;
9768 end Find_Matching_Actual;
9770 -------------------------
9771 -- Match_Formal_Entity --
9772 -------------------------
9774 procedure Match_Formal_Entity
9775 (Formal_Node : Node_Id;
9776 Formal_Ent : Entity_Id;
9777 Actual_Ent : Entity_Id)
9779 Act_Pkg : Entity_Id;
9781 begin
9782 Set_Instance_Of (Formal_Ent, Actual_Ent);
9784 if Ekind (Actual_Ent) = E_Package then
9786 -- Record associations for each parameter
9788 Act_Pkg := Actual_Ent;
9790 declare
9791 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9792 F_Ent : Entity_Id;
9793 F_Node : Node_Id;
9795 Gen_Decl : Node_Id;
9796 Formals : List_Id;
9797 Actual : Entity_Id;
9799 begin
9800 -- Retrieve the actual given in the formal package declaration
9802 Actual := Entity (Name (Original_Node (Formal_Node)));
9804 -- The actual in the formal package declaration may be a
9805 -- renamed generic package, in which case we want to retrieve
9806 -- the original generic in order to traverse its formal part.
9808 if Present (Renamed_Entity (Actual)) then
9809 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9810 else
9811 Gen_Decl := Unit_Declaration_Node (Actual);
9812 end if;
9814 Formals := Generic_Formal_Declarations (Gen_Decl);
9816 if Present (Formals) then
9817 F_Node := First_Non_Pragma (Formals);
9818 else
9819 F_Node := Empty;
9820 end if;
9822 while Present (A_Ent)
9823 and then Present (F_Node)
9824 and then A_Ent /= First_Private_Entity (Act_Pkg)
9825 loop
9826 F_Ent := Get_Formal_Entity (F_Node);
9828 if Present (F_Ent) then
9830 -- This is a formal of the original package. Record
9831 -- association and recurse.
9833 Find_Matching_Actual (F_Node, A_Ent);
9834 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9835 Next_Entity (A_Ent);
9836 end if;
9838 Next_Non_Pragma (F_Node);
9839 end loop;
9840 end;
9841 end if;
9842 end Match_Formal_Entity;
9844 -----------------------
9845 -- Get_Formal_Entity --
9846 -----------------------
9848 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9849 Kind : constant Node_Kind := Nkind (Original_Node (N));
9850 begin
9851 case Kind is
9852 when N_Formal_Object_Declaration =>
9853 return Defining_Identifier (N);
9855 when N_Formal_Type_Declaration =>
9856 return Defining_Identifier (N);
9858 when N_Formal_Subprogram_Declaration =>
9859 return Defining_Unit_Name (Specification (N));
9861 when N_Formal_Package_Declaration =>
9862 return Defining_Identifier (Original_Node (N));
9864 when N_Generic_Package_Declaration =>
9865 return Defining_Identifier (Original_Node (N));
9867 -- All other declarations are introduced by semantic analysis and
9868 -- have no match in the actual.
9870 when others =>
9871 return Empty;
9872 end case;
9873 end Get_Formal_Entity;
9875 --------------------
9876 -- Is_Instance_Of --
9877 --------------------
9879 function Is_Instance_Of
9880 (Act_Spec : Entity_Id;
9881 Gen_Anc : Entity_Id) return Boolean
9883 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9885 begin
9886 if No (Gen_Par) then
9887 return False;
9889 -- Simplest case: the generic parent of the actual is the formal
9891 elsif Gen_Par = Gen_Anc then
9892 return True;
9894 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9895 return False;
9897 -- The actual may be obtained through several instantiations. Its
9898 -- scope must itself be an instance of a generic declared in the
9899 -- same scope as the formal. Any other case is detected above.
9901 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9902 return False;
9904 else
9905 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9906 end if;
9907 end Is_Instance_Of;
9909 ---------------------------
9910 -- Process_Nested_Formal --
9911 ---------------------------
9913 procedure Process_Nested_Formal (Formal : Entity_Id) is
9914 Ent : Entity_Id;
9916 begin
9917 if Present (Associated_Formal_Package (Formal))
9918 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9919 then
9920 Ent := First_Entity (Formal);
9921 while Present (Ent) loop
9922 Set_Is_Hidden (Ent, False);
9923 Set_Is_Visible_Formal (Ent);
9924 Set_Is_Potentially_Use_Visible
9925 (Ent, Is_Potentially_Use_Visible (Formal));
9927 if Ekind (Ent) = E_Package then
9928 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9929 Process_Nested_Formal (Ent);
9930 end if;
9932 Next_Entity (Ent);
9933 end loop;
9934 end if;
9935 end Process_Nested_Formal;
9937 -- Start of processing for Instantiate_Formal_Package
9939 begin
9940 Analyze (Actual);
9942 if not Is_Entity_Name (Actual)
9943 or else Ekind (Entity (Actual)) /= E_Package
9944 then
9945 Error_Msg_N
9946 ("expect package instance to instantiate formal", Actual);
9947 Abandon_Instantiation (Actual);
9948 raise Program_Error;
9950 else
9951 Actual_Pack := Entity (Actual);
9952 Set_Is_Instantiated (Actual_Pack);
9954 -- The actual may be a renamed package, or an outer generic formal
9955 -- package whose instantiation is converted into a renaming.
9957 if Present (Renamed_Object (Actual_Pack)) then
9958 Actual_Pack := Renamed_Object (Actual_Pack);
9959 end if;
9961 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9962 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9963 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9964 else
9965 Gen_Parent :=
9966 Generic_Parent (Specification (Analyzed_Formal));
9967 Formal_Pack :=
9968 Defining_Unit_Name (Specification (Analyzed_Formal));
9969 end if;
9971 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9972 Parent_Spec := Package_Specification (Actual_Pack);
9973 else
9974 Parent_Spec := Parent (Actual_Pack);
9975 end if;
9977 if Gen_Parent = Any_Id then
9978 Error_Msg_N
9979 ("previous error in declaration of formal package", Actual);
9980 Abandon_Instantiation (Actual);
9982 elsif
9983 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9984 then
9985 null;
9987 else
9988 Error_Msg_NE
9989 ("actual parameter must be instance of&", Actual, Gen_Parent);
9990 Abandon_Instantiation (Actual);
9991 end if;
9993 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9994 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9996 Nod :=
9997 Make_Package_Renaming_Declaration (Loc,
9998 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9999 Name => New_Occurrence_Of (Actual_Pack, Loc));
10001 Set_Associated_Formal_Package
10002 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10003 Decls := New_List (Nod);
10005 -- If the formal F has a box, then the generic declarations are
10006 -- visible in the generic G. In an instance of G, the corresponding
10007 -- entities in the actual for F (which are the actuals for the
10008 -- instantiation of the generic that F denotes) must also be made
10009 -- visible for analysis of the current instance. On exit from the
10010 -- current instance, those entities are made private again. If the
10011 -- actual is currently in use, these entities are also use-visible.
10013 -- The loop through the actual entities also steps through the formal
10014 -- entities and enters associations from formals to actuals into the
10015 -- renaming map. This is necessary to properly handle checking of
10016 -- actual parameter associations for later formals that depend on
10017 -- actuals declared in the formal package.
10019 -- In Ada 2005, partial parameterization requires that we make
10020 -- visible the actuals corresponding to formals that were defaulted
10021 -- in the formal package. There formals are identified because they
10022 -- remain formal generics within the formal package, rather than
10023 -- being renamings of the actuals supplied.
10025 declare
10026 Gen_Decl : constant Node_Id :=
10027 Unit_Declaration_Node (Gen_Parent);
10028 Formals : constant List_Id :=
10029 Generic_Formal_Declarations (Gen_Decl);
10031 Actual_Ent : Entity_Id;
10032 Actual_Of_Formal : Node_Id;
10033 Formal_Node : Node_Id;
10034 Formal_Ent : Entity_Id;
10036 begin
10037 if Present (Formals) then
10038 Formal_Node := First_Non_Pragma (Formals);
10039 else
10040 Formal_Node := Empty;
10041 end if;
10043 Actual_Ent := First_Entity (Actual_Pack);
10044 Actual_Of_Formal :=
10045 First (Visible_Declarations (Specification (Analyzed_Formal)));
10046 while Present (Actual_Ent)
10047 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10048 loop
10049 if Present (Formal_Node) then
10050 Formal_Ent := Get_Formal_Entity (Formal_Node);
10052 if Present (Formal_Ent) then
10053 Find_Matching_Actual (Formal_Node, Actual_Ent);
10054 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10056 -- We iterate at the same time over the actuals of the
10057 -- local package created for the formal, to determine
10058 -- which one of the formals of the original generic were
10059 -- defaulted in the formal. The corresponding actual
10060 -- entities are visible in the enclosing instance.
10062 if Box_Present (Formal)
10063 or else
10064 (Present (Actual_Of_Formal)
10065 and then
10066 Is_Generic_Formal
10067 (Get_Formal_Entity (Actual_Of_Formal)))
10068 then
10069 Set_Is_Hidden (Actual_Ent, False);
10070 Set_Is_Visible_Formal (Actual_Ent);
10071 Set_Is_Potentially_Use_Visible
10072 (Actual_Ent, In_Use (Actual_Pack));
10074 if Ekind (Actual_Ent) = E_Package then
10075 Process_Nested_Formal (Actual_Ent);
10076 end if;
10078 else
10079 Set_Is_Hidden (Actual_Ent);
10080 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10081 end if;
10082 end if;
10084 Next_Non_Pragma (Formal_Node);
10085 Next (Actual_Of_Formal);
10087 else
10088 -- No further formals to match, but the generic part may
10089 -- contain inherited operation that are not hidden in the
10090 -- enclosing instance.
10092 Next_Entity (Actual_Ent);
10093 end if;
10094 end loop;
10096 -- Inherited subprograms generated by formal derived types are
10097 -- also visible if the types are.
10099 Actual_Ent := First_Entity (Actual_Pack);
10100 while Present (Actual_Ent)
10101 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10102 loop
10103 if Is_Overloadable (Actual_Ent)
10104 and then
10105 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10106 and then
10107 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10108 then
10109 Set_Is_Hidden (Actual_Ent, False);
10110 Set_Is_Potentially_Use_Visible
10111 (Actual_Ent, In_Use (Actual_Pack));
10112 end if;
10114 Next_Entity (Actual_Ent);
10115 end loop;
10116 end;
10118 -- If the formal is not declared with a box, reanalyze it as an
10119 -- abbreviated instantiation, to verify the matching rules of 12.7.
10120 -- The actual checks are performed after the generic associations
10121 -- have been analyzed, to guarantee the same visibility for this
10122 -- instantiation and for the actuals.
10124 -- In Ada 2005, the generic associations for the formal can include
10125 -- defaulted parameters. These are ignored during check. This
10126 -- internal instantiation is removed from the tree after conformance
10127 -- checking, because it contains formal declarations for those
10128 -- defaulted parameters, and those should not reach the back-end.
10130 if not Box_Present (Formal) then
10131 declare
10132 I_Pack : constant Entity_Id :=
10133 Make_Temporary (Sloc (Actual), 'P');
10135 begin
10136 Set_Is_Internal (I_Pack);
10138 Append_To (Decls,
10139 Make_Package_Instantiation (Sloc (Actual),
10140 Defining_Unit_Name => I_Pack,
10141 Name =>
10142 New_Occurrence_Of
10143 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10144 Generic_Associations => Generic_Associations (Formal)));
10145 end;
10146 end if;
10148 return Decls;
10149 end if;
10150 end Instantiate_Formal_Package;
10152 -----------------------------------
10153 -- Instantiate_Formal_Subprogram --
10154 -----------------------------------
10156 function Instantiate_Formal_Subprogram
10157 (Formal : Node_Id;
10158 Actual : Node_Id;
10159 Analyzed_Formal : Node_Id) return Node_Id
10161 Analyzed_S : constant Entity_Id :=
10162 Defining_Unit_Name (Specification (Analyzed_Formal));
10163 Formal_Sub : constant Entity_Id :=
10164 Defining_Unit_Name (Specification (Formal));
10166 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10167 -- If the generic is a child unit, the parent has been installed on the
10168 -- scope stack, but a default subprogram cannot resolve to something
10169 -- on the parent because that parent is not really part of the visible
10170 -- context (it is there to resolve explicit local entities). If the
10171 -- default has resolved in this way, we remove the entity from immediate
10172 -- visibility and analyze the node again to emit an error message or
10173 -- find another visible candidate.
10175 procedure Valid_Actual_Subprogram (Act : Node_Id);
10176 -- Perform legality check and raise exception on failure
10178 -----------------------
10179 -- From_Parent_Scope --
10180 -----------------------
10182 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10183 Gen_Scope : Node_Id;
10185 begin
10186 Gen_Scope := Scope (Analyzed_S);
10187 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10188 if Scope (Subp) = Scope (Gen_Scope) then
10189 return True;
10190 end if;
10192 Gen_Scope := Scope (Gen_Scope);
10193 end loop;
10195 return False;
10196 end From_Parent_Scope;
10198 -----------------------------
10199 -- Valid_Actual_Subprogram --
10200 -----------------------------
10202 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10203 Act_E : Entity_Id;
10205 begin
10206 if Is_Entity_Name (Act) then
10207 Act_E := Entity (Act);
10209 elsif Nkind (Act) = N_Selected_Component
10210 and then Is_Entity_Name (Selector_Name (Act))
10211 then
10212 Act_E := Entity (Selector_Name (Act));
10214 else
10215 Act_E := Empty;
10216 end if;
10218 if (Present (Act_E) and then Is_Overloadable (Act_E))
10219 or else Nkind_In (Act, N_Attribute_Reference,
10220 N_Indexed_Component,
10221 N_Character_Literal,
10222 N_Explicit_Dereference)
10223 then
10224 return;
10225 end if;
10227 Error_Msg_NE
10228 ("expect subprogram or entry name in instantiation of &",
10229 Instantiation_Node, Formal_Sub);
10230 Abandon_Instantiation (Instantiation_Node);
10231 end Valid_Actual_Subprogram;
10233 -- Local variables
10235 Decl_Node : Node_Id;
10236 Loc : Source_Ptr;
10237 Nam : Node_Id;
10238 New_Spec : Node_Id;
10239 New_Subp : Entity_Id;
10241 -- Start of processing for Instantiate_Formal_Subprogram
10243 begin
10244 New_Spec := New_Copy_Tree (Specification (Formal));
10246 -- The tree copy has created the proper instantiation sloc for the
10247 -- new specification. Use this location for all other constructed
10248 -- declarations.
10250 Loc := Sloc (Defining_Unit_Name (New_Spec));
10252 -- Create new entity for the actual (New_Copy_Tree does not), and
10253 -- indicate that it is an actual.
10255 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10256 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10257 Set_Is_Generic_Actual_Subprogram (New_Subp);
10258 Set_Defining_Unit_Name (New_Spec, New_Subp);
10260 -- Create new entities for the each of the formals in the specification
10261 -- of the renaming declaration built for the actual.
10263 if Present (Parameter_Specifications (New_Spec)) then
10264 declare
10265 F : Node_Id;
10266 F_Id : Entity_Id;
10268 begin
10269 F := First (Parameter_Specifications (New_Spec));
10270 while Present (F) loop
10271 F_Id := Defining_Identifier (F);
10273 Set_Defining_Identifier (F,
10274 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10275 Next (F);
10276 end loop;
10277 end;
10278 end if;
10280 -- Find entity of actual. If the actual is an attribute reference, it
10281 -- cannot be resolved here (its formal is missing) but is handled
10282 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10283 -- fully resolved subsequently, when the renaming declaration for the
10284 -- formal is analyzed. If it is an explicit dereference, resolve the
10285 -- prefix but not the actual itself, to prevent interpretation as call.
10287 if Present (Actual) then
10288 Loc := Sloc (Actual);
10289 Set_Sloc (New_Spec, Loc);
10291 if Nkind (Actual) = N_Operator_Symbol then
10292 Find_Direct_Name (Actual);
10294 elsif Nkind (Actual) = N_Explicit_Dereference then
10295 Analyze (Prefix (Actual));
10297 elsif Nkind (Actual) /= N_Attribute_Reference then
10298 Analyze (Actual);
10299 end if;
10301 Valid_Actual_Subprogram (Actual);
10302 Nam := Actual;
10304 elsif Present (Default_Name (Formal)) then
10305 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10306 N_Selected_Component,
10307 N_Indexed_Component,
10308 N_Character_Literal)
10309 and then Present (Entity (Default_Name (Formal)))
10310 then
10311 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10312 else
10313 Nam := New_Copy (Default_Name (Formal));
10314 Set_Sloc (Nam, Loc);
10315 end if;
10317 elsif Box_Present (Formal) then
10319 -- Actual is resolved at the point of instantiation. Create an
10320 -- identifier or operator with the same name as the formal.
10322 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10323 Nam :=
10324 Make_Operator_Symbol (Loc,
10325 Chars => Chars (Formal_Sub),
10326 Strval => No_String);
10327 else
10328 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10329 end if;
10331 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10332 and then Null_Present (Specification (Formal))
10333 then
10334 -- Generate null body for procedure, for use in the instance
10336 Decl_Node :=
10337 Make_Subprogram_Body (Loc,
10338 Specification => New_Spec,
10339 Declarations => New_List,
10340 Handled_Statement_Sequence =>
10341 Make_Handled_Sequence_Of_Statements (Loc,
10342 Statements => New_List (Make_Null_Statement (Loc))));
10344 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10345 return Decl_Node;
10347 else
10348 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10349 Error_Msg_NE
10350 ("missing actual&", Instantiation_Node, Formal_Sub);
10351 Error_Msg_NE
10352 ("\in instantiation of & declared#",
10353 Instantiation_Node, Scope (Analyzed_S));
10354 Abandon_Instantiation (Instantiation_Node);
10355 end if;
10357 Decl_Node :=
10358 Make_Subprogram_Renaming_Declaration (Loc,
10359 Specification => New_Spec,
10360 Name => Nam);
10362 -- If we do not have an actual and the formal specified <> then set to
10363 -- get proper default.
10365 if No (Actual) and then Box_Present (Formal) then
10366 Set_From_Default (Decl_Node);
10367 end if;
10369 -- Gather possible interpretations for the actual before analyzing the
10370 -- instance. If overloaded, it will be resolved when analyzing the
10371 -- renaming declaration.
10373 if Box_Present (Formal) and then No (Actual) then
10374 Analyze (Nam);
10376 if Is_Child_Unit (Scope (Analyzed_S))
10377 and then Present (Entity (Nam))
10378 then
10379 if not Is_Overloaded (Nam) then
10380 if From_Parent_Scope (Entity (Nam)) then
10381 Set_Is_Immediately_Visible (Entity (Nam), False);
10382 Set_Entity (Nam, Empty);
10383 Set_Etype (Nam, Empty);
10385 Analyze (Nam);
10386 Set_Is_Immediately_Visible (Entity (Nam));
10387 end if;
10389 else
10390 declare
10391 I : Interp_Index;
10392 It : Interp;
10394 begin
10395 Get_First_Interp (Nam, I, It);
10396 while Present (It.Nam) loop
10397 if From_Parent_Scope (It.Nam) then
10398 Remove_Interp (I);
10399 end if;
10401 Get_Next_Interp (I, It);
10402 end loop;
10403 end;
10404 end if;
10405 end if;
10406 end if;
10408 -- The generic instantiation freezes the actual. This can only be done
10409 -- once the actual is resolved, in the analysis of the renaming
10410 -- declaration. To make the formal subprogram entity available, we set
10411 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10412 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10413 -- of formal abstract subprograms.
10415 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10417 -- We cannot analyze the renaming declaration, and thus find the actual,
10418 -- until all the actuals are assembled in the instance. For subsequent
10419 -- checks of other actuals, indicate the node that will hold the
10420 -- instance of this formal.
10422 Set_Instance_Of (Analyzed_S, Nam);
10424 if Nkind (Actual) = N_Selected_Component
10425 and then Is_Task_Type (Etype (Prefix (Actual)))
10426 and then not Is_Frozen (Etype (Prefix (Actual)))
10427 then
10428 -- The renaming declaration will create a body, which must appear
10429 -- outside of the instantiation, We move the renaming declaration
10430 -- out of the instance, and create an additional renaming inside,
10431 -- to prevent freezing anomalies.
10433 declare
10434 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10436 begin
10437 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10438 Insert_Before (Instantiation_Node, Decl_Node);
10439 Analyze (Decl_Node);
10441 -- Now create renaming within the instance
10443 Decl_Node :=
10444 Make_Subprogram_Renaming_Declaration (Loc,
10445 Specification => New_Copy_Tree (New_Spec),
10446 Name => New_Occurrence_Of (Anon_Id, Loc));
10448 Set_Defining_Unit_Name (Specification (Decl_Node),
10449 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10450 end;
10451 end if;
10453 return Decl_Node;
10454 end Instantiate_Formal_Subprogram;
10456 ------------------------
10457 -- Instantiate_Object --
10458 ------------------------
10460 function Instantiate_Object
10461 (Formal : Node_Id;
10462 Actual : Node_Id;
10463 Analyzed_Formal : Node_Id) return List_Id
10465 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10466 A_Gen_Obj : constant Entity_Id :=
10467 Defining_Identifier (Analyzed_Formal);
10468 Acc_Def : Node_Id := Empty;
10469 Act_Assoc : constant Node_Id := Parent (Actual);
10470 Actual_Decl : Node_Id := Empty;
10471 Decl_Node : Node_Id;
10472 Def : Node_Id;
10473 Ftyp : Entity_Id;
10474 List : constant List_Id := New_List;
10475 Loc : constant Source_Ptr := Sloc (Actual);
10476 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10477 Subt_Decl : Node_Id := Empty;
10478 Subt_Mark : Node_Id := Empty;
10480 function Copy_Access_Def return Node_Id;
10481 -- If formal is an anonymous access, copy access definition of formal
10482 -- for generated object declaration.
10484 ---------------------
10485 -- Copy_Access_Def --
10486 ---------------------
10488 function Copy_Access_Def return Node_Id is
10489 begin
10490 Def := New_Copy_Tree (Acc_Def);
10492 -- In addition, if formal is an access to subprogram we need to
10493 -- generate new formals for the signature of the default, so that
10494 -- the tree is properly formatted for ASIS use.
10496 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10497 declare
10498 Par_Spec : Node_Id;
10499 begin
10500 Par_Spec :=
10501 First (Parameter_Specifications
10502 (Access_To_Subprogram_Definition (Def)));
10503 while Present (Par_Spec) loop
10504 Set_Defining_Identifier (Par_Spec,
10505 Make_Defining_Identifier (Sloc (Acc_Def),
10506 Chars => Chars (Defining_Identifier (Par_Spec))));
10507 Next (Par_Spec);
10508 end loop;
10509 end;
10510 end if;
10512 return Def;
10513 end Copy_Access_Def;
10515 -- Start of processing for Instantiate_Object
10517 begin
10518 -- Formal may be an anonymous access
10520 if Present (Subtype_Mark (Formal)) then
10521 Subt_Mark := Subtype_Mark (Formal);
10522 else
10523 Check_Access_Definition (Formal);
10524 Acc_Def := Access_Definition (Formal);
10525 end if;
10527 -- Sloc for error message on missing actual
10529 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10531 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10532 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10533 end if;
10535 Set_Parent (List, Parent (Actual));
10537 -- OUT present
10539 if Out_Present (Formal) then
10541 -- An IN OUT generic actual must be a name. The instantiation is a
10542 -- renaming declaration. The actual is the name being renamed. We
10543 -- use the actual directly, rather than a copy, because it is not
10544 -- used further in the list of actuals, and because a copy or a use
10545 -- of relocate_node is incorrect if the instance is nested within a
10546 -- generic. In order to simplify ASIS searches, the Generic_Parent
10547 -- field links the declaration to the generic association.
10549 if No (Actual) then
10550 Error_Msg_NE
10551 ("missing actual &",
10552 Instantiation_Node, Gen_Obj);
10553 Error_Msg_NE
10554 ("\in instantiation of & declared#",
10555 Instantiation_Node, Scope (A_Gen_Obj));
10556 Abandon_Instantiation (Instantiation_Node);
10557 end if;
10559 if Present (Subt_Mark) then
10560 Decl_Node :=
10561 Make_Object_Renaming_Declaration (Loc,
10562 Defining_Identifier => New_Copy (Gen_Obj),
10563 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10564 Name => Actual);
10566 else pragma Assert (Present (Acc_Def));
10567 Decl_Node :=
10568 Make_Object_Renaming_Declaration (Loc,
10569 Defining_Identifier => New_Copy (Gen_Obj),
10570 Access_Definition => New_Copy_Tree (Acc_Def),
10571 Name => Actual);
10572 end if;
10574 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10576 -- The analysis of the actual may produce Insert_Action nodes, so
10577 -- the declaration must have a context in which to attach them.
10579 Append (Decl_Node, List);
10580 Analyze (Actual);
10582 -- Return if the analysis of the actual reported some error
10584 if Etype (Actual) = Any_Type then
10585 return List;
10586 end if;
10588 -- This check is performed here because Analyze_Object_Renaming will
10589 -- not check it when Comes_From_Source is False. Note though that the
10590 -- check for the actual being the name of an object will be performed
10591 -- in Analyze_Object_Renaming.
10593 if Is_Object_Reference (Actual)
10594 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10595 then
10596 Error_Msg_N
10597 ("illegal discriminant-dependent component for in out parameter",
10598 Actual);
10599 end if;
10601 -- The actual has to be resolved in order to check that it is a
10602 -- variable (due to cases such as F (1), where F returns access to
10603 -- an array, and for overloaded prefixes).
10605 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10607 -- If the type of the formal is not itself a formal, and the current
10608 -- unit is a child unit, the formal type must be declared in a
10609 -- parent, and must be retrieved by visibility.
10611 if Ftyp = Orig_Ftyp
10612 and then Is_Generic_Unit (Scope (Ftyp))
10613 and then Is_Child_Unit (Scope (A_Gen_Obj))
10614 then
10615 declare
10616 Temp : constant Node_Id :=
10617 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10618 begin
10619 Set_Entity (Temp, Empty);
10620 Find_Type (Temp);
10621 Ftyp := Entity (Temp);
10622 end;
10623 end if;
10625 if Is_Private_Type (Ftyp)
10626 and then not Is_Private_Type (Etype (Actual))
10627 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10628 or else Base_Type (Etype (Actual)) = Ftyp)
10629 then
10630 -- If the actual has the type of the full view of the formal, or
10631 -- else a non-private subtype of the formal, then the visibility
10632 -- of the formal type has changed. Add to the actuals a subtype
10633 -- declaration that will force the exchange of views in the body
10634 -- of the instance as well.
10636 Subt_Decl :=
10637 Make_Subtype_Declaration (Loc,
10638 Defining_Identifier => Make_Temporary (Loc, 'P'),
10639 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10641 Prepend (Subt_Decl, List);
10643 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10644 Exchange_Declarations (Ftyp);
10645 end if;
10647 Resolve (Actual, Ftyp);
10649 if not Denotes_Variable (Actual) then
10650 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10652 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10654 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10655 -- the type of the actual shall resolve to a specific anonymous
10656 -- access type.
10658 if Ada_Version < Ada_2005
10659 or else Ekind (Base_Type (Ftyp)) /=
10660 E_Anonymous_Access_Type
10661 or else Ekind (Base_Type (Etype (Actual))) /=
10662 E_Anonymous_Access_Type
10663 then
10664 Error_Msg_NE
10665 ("type of actual does not match type of&", Actual, Gen_Obj);
10666 end if;
10667 end if;
10669 Note_Possible_Modification (Actual, Sure => True);
10671 -- Check for instantiation of atomic/volatile actual for
10672 -- non-atomic/volatile formal (RM C.6 (12)).
10674 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10675 Error_Msg_N
10676 ("cannot instantiate non-atomic formal object "
10677 & "with atomic actual", Actual);
10679 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10680 then
10681 Error_Msg_N
10682 ("cannot instantiate non-volatile formal object "
10683 & "with volatile actual", Actual);
10684 end if;
10686 -- Formal in-parameter
10688 else
10689 -- The instantiation of a generic formal in-parameter is constant
10690 -- declaration. The actual is the expression for that declaration.
10691 -- Its type is a full copy of the type of the formal. This may be
10692 -- an access to subprogram, for which we need to generate entities
10693 -- for the formals in the new signature.
10695 if Present (Actual) then
10696 if Present (Subt_Mark) then
10697 Def := New_Copy_Tree (Subt_Mark);
10698 else pragma Assert (Present (Acc_Def));
10699 Def := Copy_Access_Def;
10700 end if;
10702 Decl_Node :=
10703 Make_Object_Declaration (Loc,
10704 Defining_Identifier => New_Copy (Gen_Obj),
10705 Constant_Present => True,
10706 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10707 Object_Definition => Def,
10708 Expression => Actual);
10710 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10712 -- A generic formal object of a tagged type is defined to be
10713 -- aliased so the new constant must also be treated as aliased.
10715 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10716 Set_Aliased_Present (Decl_Node);
10717 end if;
10719 Append (Decl_Node, List);
10721 -- No need to repeat (pre-)analysis of some expression nodes
10722 -- already handled in Preanalyze_Actuals.
10724 if Nkind (Actual) /= N_Allocator then
10725 Analyze (Actual);
10727 -- Return if the analysis of the actual reported some error
10729 if Etype (Actual) = Any_Type then
10730 return List;
10731 end if;
10732 end if;
10734 declare
10735 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10736 Typ : Entity_Id;
10738 begin
10739 Typ := Get_Instance_Of (Formal_Type);
10741 -- If the actual appears in the current or an enclosing scope,
10742 -- use its type directly. This is relevant if it has an actual
10743 -- subtype that is distinct from its nominal one. This cannot
10744 -- be done in general because the type of the actual may
10745 -- depend on other actuals, and only be fully determined when
10746 -- the enclosing instance is analyzed.
10748 if Present (Etype (Actual))
10749 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10750 then
10751 Freeze_Before (Instantiation_Node, Etype (Actual));
10752 else
10753 Freeze_Before (Instantiation_Node, Typ);
10754 end if;
10756 -- If the actual is an aggregate, perform name resolution on
10757 -- its components (the analysis of an aggregate does not do it)
10758 -- to capture local names that may be hidden if the generic is
10759 -- a child unit.
10761 if Nkind (Actual) = N_Aggregate then
10762 Preanalyze_And_Resolve (Actual, Typ);
10763 end if;
10765 if Is_Limited_Type (Typ)
10766 and then not OK_For_Limited_Init (Typ, Actual)
10767 then
10768 Error_Msg_N
10769 ("initialization not allowed for limited types", Actual);
10770 Explain_Limited_Type (Typ, Actual);
10771 end if;
10772 end;
10774 elsif Present (Default_Expression (Formal)) then
10776 -- Use default to construct declaration
10778 if Present (Subt_Mark) then
10779 Def := New_Copy (Subt_Mark);
10780 else pragma Assert (Present (Acc_Def));
10781 Def := Copy_Access_Def;
10782 end if;
10784 Decl_Node :=
10785 Make_Object_Declaration (Sloc (Formal),
10786 Defining_Identifier => New_Copy (Gen_Obj),
10787 Constant_Present => True,
10788 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10789 Object_Definition => Def,
10790 Expression => New_Copy_Tree
10791 (Default_Expression (Formal)));
10793 Append (Decl_Node, List);
10794 Set_Analyzed (Expression (Decl_Node), False);
10796 else
10797 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10798 Error_Msg_NE ("\in instantiation of & declared#",
10799 Instantiation_Node, Scope (A_Gen_Obj));
10801 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10803 -- Create dummy constant declaration so that instance can be
10804 -- analyzed, to minimize cascaded visibility errors.
10806 if Present (Subt_Mark) then
10807 Def := Subt_Mark;
10808 else pragma Assert (Present (Acc_Def));
10809 Def := Acc_Def;
10810 end if;
10812 Decl_Node :=
10813 Make_Object_Declaration (Loc,
10814 Defining_Identifier => New_Copy (Gen_Obj),
10815 Constant_Present => True,
10816 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10817 Object_Definition => New_Copy (Def),
10818 Expression =>
10819 Make_Attribute_Reference (Sloc (Gen_Obj),
10820 Attribute_Name => Name_First,
10821 Prefix => New_Copy (Def)));
10823 Append (Decl_Node, List);
10825 else
10826 Abandon_Instantiation (Instantiation_Node);
10827 end if;
10828 end if;
10829 end if;
10831 if Nkind (Actual) in N_Has_Entity then
10832 Actual_Decl := Parent (Entity (Actual));
10833 end if;
10835 -- Ada 2005 (AI-423): For a formal object declaration with a null
10836 -- exclusion or an access definition that has a null exclusion: If the
10837 -- actual matching the formal object declaration denotes a generic
10838 -- formal object of another generic unit G, and the instantiation
10839 -- containing the actual occurs within the body of G or within the body
10840 -- of a generic unit declared within the declarative region of G, then
10841 -- the declaration of the formal object of G must have a null exclusion.
10842 -- Otherwise, the subtype of the actual matching the formal object
10843 -- declaration shall exclude null.
10845 if Ada_Version >= Ada_2005
10846 and then Present (Actual_Decl)
10847 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10848 N_Object_Declaration)
10849 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10850 and then not Has_Null_Exclusion (Actual_Decl)
10851 and then Has_Null_Exclusion (Analyzed_Formal)
10852 then
10853 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10854 Error_Msg_N
10855 ("actual must exclude null to match generic formal#", Actual);
10856 end if;
10858 -- An effectively volatile object cannot be used as an actual in a
10859 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10860 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10861 -- legality rule, and also verifies that the actual is an object.
10863 if SPARK_Mode = On
10864 and then Present (Actual)
10865 and then Is_Object_Reference (Actual)
10866 and then Is_Effectively_Volatile_Object (Actual)
10867 then
10868 Error_Msg_N
10869 ("volatile object cannot act as actual in generic instantiation",
10870 Actual);
10871 end if;
10873 return List;
10874 end Instantiate_Object;
10876 ------------------------------
10877 -- Instantiate_Package_Body --
10878 ------------------------------
10880 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
10881 -- must be replaced by gotos which jump to the end of the routine in order
10882 -- to restore the Ghost and SPARK modes.
10884 procedure Instantiate_Package_Body
10885 (Body_Info : Pending_Body_Info;
10886 Inlined_Body : Boolean := False;
10887 Body_Optional : Boolean := False)
10889 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10890 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
10891 Act_Spec : constant Node_Id := Specification (Act_Decl);
10892 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10893 Gen_Id : constant Node_Id := Name (Inst_Node);
10894 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10895 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10896 Loc : constant Source_Ptr := Sloc (Inst_Node);
10898 Saved_ISMP : constant Boolean :=
10899 Ignore_SPARK_Mode_Pragmas_In_Instance;
10900 Saved_Style_Check : constant Boolean := Style_Check;
10902 procedure Check_Initialized_Types;
10903 -- In a generic package body, an entity of a generic private type may
10904 -- appear uninitialized. This is suspicious, unless the actual is a
10905 -- fully initialized type.
10907 -----------------------------
10908 -- Check_Initialized_Types --
10909 -----------------------------
10911 procedure Check_Initialized_Types is
10912 Decl : Node_Id;
10913 Formal : Entity_Id;
10914 Actual : Entity_Id;
10915 Uninit_Var : Entity_Id;
10917 begin
10918 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10919 while Present (Decl) loop
10920 Uninit_Var := Empty;
10922 if Nkind (Decl) = N_Private_Extension_Declaration then
10923 Uninit_Var := Uninitialized_Variable (Decl);
10925 elsif Nkind (Decl) = N_Formal_Type_Declaration
10926 and then Nkind (Formal_Type_Definition (Decl)) =
10927 N_Formal_Private_Type_Definition
10928 then
10929 Uninit_Var :=
10930 Uninitialized_Variable (Formal_Type_Definition (Decl));
10931 end if;
10933 if Present (Uninit_Var) then
10934 Formal := Defining_Identifier (Decl);
10935 Actual := First_Entity (Act_Decl_Id);
10937 -- For each formal there is a subtype declaration that renames
10938 -- the actual and has the same name as the formal. Locate the
10939 -- formal for warning message about uninitialized variables
10940 -- in the generic, for which the actual type should be a fully
10941 -- initialized type.
10943 while Present (Actual) loop
10944 exit when Ekind (Actual) = E_Package
10945 and then Present (Renamed_Object (Actual));
10947 if Chars (Actual) = Chars (Formal)
10948 and then not Is_Scalar_Type (Actual)
10949 and then not Is_Fully_Initialized_Type (Actual)
10950 and then Warn_On_No_Value_Assigned
10951 then
10952 Error_Msg_Node_2 := Formal;
10953 Error_Msg_NE
10954 ("generic unit has uninitialized variable& of "
10955 & "formal private type &?v?", Actual, Uninit_Var);
10956 Error_Msg_NE
10957 ("actual type for& should be fully initialized type?v?",
10958 Actual, Formal);
10959 exit;
10960 end if;
10962 Next_Entity (Actual);
10963 end loop;
10964 end if;
10966 Next (Decl);
10967 end loop;
10968 end Check_Initialized_Types;
10970 -- Local variables
10972 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
10973 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
10974 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
10975 -- Save the Ghost and SPARK mode-related data to restore on exit
10977 Act_Body : Node_Id;
10978 Act_Body_Id : Entity_Id;
10979 Act_Body_Name : Node_Id;
10980 Gen_Body : Node_Id;
10981 Gen_Body_Id : Node_Id;
10982 Par_Ent : Entity_Id := Empty;
10983 Par_Vis : Boolean := False;
10984 Parent_Installed : Boolean := False;
10986 Vis_Prims_List : Elist_Id := No_Elist;
10987 -- List of primitives made temporarily visible in the instantiation
10988 -- to match the visibility of the formal type.
10990 -- Start of processing for Instantiate_Package_Body
10992 begin
10993 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10995 -- The instance body may already have been processed, as the parent of
10996 -- another instance that is inlined (Load_Parent_Of_Generic).
10998 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10999 return;
11000 end if;
11002 -- The package being instantiated may be subject to pragma Ghost. Set
11003 -- the mode now to ensure that any nodes generated during instantiation
11004 -- are properly marked as Ghost.
11006 Set_Ghost_Mode (Act_Decl_Id);
11008 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11010 -- Re-establish the state of information on which checks are suppressed.
11011 -- This information was set in Body_Info at the point of instantiation,
11012 -- and now we restore it so that the instance is compiled using the
11013 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11015 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11016 Scope_Suppress := Body_Info.Scope_Suppress;
11017 Opt.Ada_Version := Body_Info.Version;
11018 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11019 Restore_Warnings (Body_Info.Warnings);
11021 -- Install the SPARK mode which applies to the package body
11023 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11025 if No (Gen_Body_Id) then
11027 -- Do not look for parent of generic body if none is required.
11028 -- This may happen when the routine is called as part of the
11029 -- Pending_Instantiations processing, when nested instances
11030 -- may precede the one generated from the main unit.
11032 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11033 and then Body_Optional
11034 then
11035 goto Leave;
11036 else
11037 Load_Parent_Of_Generic
11038 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11039 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11040 end if;
11041 end if;
11043 -- Establish global variable for sloc adjustment and for error recovery
11044 -- In the case of an instance body for an instantiation with actuals
11045 -- from a limited view, the instance body is placed at the beginning
11046 -- of the enclosing package body: use the body entity as the source
11047 -- location for nodes of the instance body.
11049 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11050 declare
11051 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11052 Body_Id : constant Node_Id :=
11053 Corresponding_Body (Unit_Declaration_Node (Scop));
11055 begin
11056 Instantiation_Node := Body_Id;
11057 end;
11058 else
11059 Instantiation_Node := Inst_Node;
11060 end if;
11062 if Present (Gen_Body_Id) then
11063 Save_Env (Gen_Unit, Act_Decl_Id);
11064 Style_Check := False;
11066 -- If the context of the instance is subject to SPARK_Mode "off", the
11067 -- annotation is missing, or the body is instantiated at a later pass
11068 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11069 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11070 -- instance.
11072 if SPARK_Mode /= On
11073 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11074 then
11075 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11076 end if;
11078 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11079 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11081 Create_Instantiation_Source
11082 (Inst_Node, Gen_Body_Id, S_Adjustment);
11084 Act_Body :=
11085 Copy_Generic_Node
11086 (Original_Node (Gen_Body), Empty, Instantiating => True);
11088 -- Create proper (possibly qualified) defining name for the body, to
11089 -- correspond to the one in the spec.
11091 Act_Body_Id :=
11092 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11093 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11095 -- Some attributes of spec entity are not inherited by body entity
11097 Set_Handler_Records (Act_Body_Id, No_List);
11099 if Nkind (Defining_Unit_Name (Act_Spec)) =
11100 N_Defining_Program_Unit_Name
11101 then
11102 Act_Body_Name :=
11103 Make_Defining_Program_Unit_Name (Loc,
11104 Name =>
11105 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11106 Defining_Identifier => Act_Body_Id);
11107 else
11108 Act_Body_Name := Act_Body_Id;
11109 end if;
11111 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11113 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11114 Check_Generic_Actuals (Act_Decl_Id, False);
11115 Check_Initialized_Types;
11117 -- Install primitives hidden at the point of the instantiation but
11118 -- visible when processing the generic formals
11120 declare
11121 E : Entity_Id;
11123 begin
11124 E := First_Entity (Act_Decl_Id);
11125 while Present (E) loop
11126 if Is_Type (E)
11127 and then not Is_Itype (E)
11128 and then Is_Generic_Actual_Type (E)
11129 and then Is_Tagged_Type (E)
11130 then
11131 Install_Hidden_Primitives
11132 (Prims_List => Vis_Prims_List,
11133 Gen_T => Generic_Parent_Type (Parent (E)),
11134 Act_T => E);
11135 end if;
11137 Next_Entity (E);
11138 end loop;
11139 end;
11141 -- If it is a child unit, make the parent instance (which is an
11142 -- instance of the parent of the generic) visible. The parent
11143 -- instance is the prefix of the name of the generic unit.
11145 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11146 and then Nkind (Gen_Id) = N_Expanded_Name
11147 then
11148 Par_Ent := Entity (Prefix (Gen_Id));
11149 Par_Vis := Is_Immediately_Visible (Par_Ent);
11150 Install_Parent (Par_Ent, In_Body => True);
11151 Parent_Installed := True;
11153 elsif Is_Child_Unit (Gen_Unit) then
11154 Par_Ent := Scope (Gen_Unit);
11155 Par_Vis := Is_Immediately_Visible (Par_Ent);
11156 Install_Parent (Par_Ent, In_Body => True);
11157 Parent_Installed := True;
11158 end if;
11160 -- If the instantiation is a library unit, and this is the main unit,
11161 -- then build the resulting compilation unit nodes for the instance.
11162 -- If this is a compilation unit but it is not the main unit, then it
11163 -- is the body of a unit in the context, that is being compiled
11164 -- because it is encloses some inlined unit or another generic unit
11165 -- being instantiated. In that case, this body is not part of the
11166 -- current compilation, and is not attached to the tree, but its
11167 -- parent must be set for analysis.
11169 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11171 -- Replace instance node with body of instance, and create new
11172 -- node for corresponding instance declaration.
11174 Build_Instance_Compilation_Unit_Nodes
11175 (Inst_Node, Act_Body, Act_Decl);
11176 Analyze (Inst_Node);
11178 if Parent (Inst_Node) = Cunit (Main_Unit) then
11180 -- If the instance is a child unit itself, then set the scope
11181 -- of the expanded body to be the parent of the instantiation
11182 -- (ensuring that the fully qualified name will be generated
11183 -- for the elaboration subprogram).
11185 if Nkind (Defining_Unit_Name (Act_Spec)) =
11186 N_Defining_Program_Unit_Name
11187 then
11188 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11189 end if;
11190 end if;
11192 -- Case where instantiation is not a library unit
11194 else
11195 -- If this is an early instantiation, i.e. appears textually
11196 -- before the corresponding body and must be elaborated first,
11197 -- indicate that the body instance is to be delayed.
11199 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11201 -- Now analyze the body. We turn off all checks if this is an
11202 -- internal unit, since there is no reason to have checks on for
11203 -- any predefined run-time library code. All such code is designed
11204 -- to be compiled with checks off.
11206 -- Note that we do NOT apply this criterion to children of GNAT
11207 -- The latter units must suppress checks explicitly if needed.
11209 -- We also do not suppress checks in CodePeer mode where we are
11210 -- interested in finding possible runtime errors.
11212 if not CodePeer_Mode
11213 and then In_Predefined_Unit (Gen_Decl)
11214 then
11215 Analyze (Act_Body, Suppress => All_Checks);
11216 else
11217 Analyze (Act_Body);
11218 end if;
11219 end if;
11221 Inherit_Context (Gen_Body, Inst_Node);
11223 -- Remove the parent instances if they have been placed on the scope
11224 -- stack to compile the body.
11226 if Parent_Installed then
11227 Remove_Parent (In_Body => True);
11229 -- Restore the previous visibility of the parent
11231 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11232 end if;
11234 Restore_Hidden_Primitives (Vis_Prims_List);
11235 Restore_Private_Views (Act_Decl_Id);
11237 -- Remove the current unit from visibility if this is an instance
11238 -- that is not elaborated on the fly for inlining purposes.
11240 if not Inlined_Body then
11241 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11242 end if;
11244 Restore_Env;
11246 -- If we have no body, and the unit requires a body, then complain. This
11247 -- complaint is suppressed if we have detected other errors (since a
11248 -- common reason for missing the body is that it had errors).
11249 -- In CodePeer mode, a warning has been emitted already, no need for
11250 -- further messages.
11252 elsif Unit_Requires_Body (Gen_Unit)
11253 and then not Body_Optional
11254 then
11255 if CodePeer_Mode then
11256 null;
11258 elsif Serious_Errors_Detected = 0 then
11259 Error_Msg_NE
11260 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11262 -- Don't attempt to perform any cleanup actions if some other error
11263 -- was already detected, since this can cause blowups.
11265 else
11266 goto Leave;
11267 end if;
11269 -- Case of package that does not need a body
11271 else
11272 -- If the instantiation of the declaration is a library unit, rewrite
11273 -- the original package instantiation as a package declaration in the
11274 -- compilation unit node.
11276 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11277 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11278 Rewrite (Inst_Node, Act_Decl);
11280 -- Generate elaboration entity, in case spec has elaboration code.
11281 -- This cannot be done when the instance is analyzed, because it
11282 -- is not known yet whether the body exists.
11284 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11285 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11287 -- If the instantiation is not a library unit, then append the
11288 -- declaration to the list of implicitly generated entities, unless
11289 -- it is already a list member which means that it was already
11290 -- processed
11292 elsif not Is_List_Member (Act_Decl) then
11293 Mark_Rewrite_Insertion (Act_Decl);
11294 Insert_Before (Inst_Node, Act_Decl);
11295 end if;
11296 end if;
11298 Expander_Mode_Restore;
11300 <<Leave>>
11301 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11302 Restore_Ghost_Mode (Saved_GM);
11303 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11304 Style_Check := Saved_Style_Check;
11305 end Instantiate_Package_Body;
11307 ---------------------------------
11308 -- Instantiate_Subprogram_Body --
11309 ---------------------------------
11311 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11312 -- must be replaced by gotos which jump to the end of the routine in order
11313 -- to restore the Ghost and SPARK modes.
11315 procedure Instantiate_Subprogram_Body
11316 (Body_Info : Pending_Body_Info;
11317 Body_Optional : Boolean := False)
11319 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11320 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11321 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11322 Gen_Id : constant Node_Id := Name (Inst_Node);
11323 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11324 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11325 Loc : constant Source_Ptr := Sloc (Inst_Node);
11326 Pack_Id : constant Entity_Id :=
11327 Defining_Unit_Name (Parent (Act_Decl));
11329 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11330 Saved_ISMP : constant Boolean :=
11331 Ignore_SPARK_Mode_Pragmas_In_Instance;
11332 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11333 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11334 -- Save the Ghost and SPARK mode-related data to restore on exit
11336 Saved_Style_Check : constant Boolean := Style_Check;
11337 Saved_Warnings : constant Warning_Record := Save_Warnings;
11339 Act_Body : Node_Id;
11340 Act_Body_Id : Entity_Id;
11341 Gen_Body : Node_Id;
11342 Gen_Body_Id : Node_Id;
11343 Pack_Body : Node_Id;
11344 Par_Ent : Entity_Id := Empty;
11345 Par_Vis : Boolean := False;
11346 Ret_Expr : Node_Id;
11348 Parent_Installed : Boolean := False;
11350 begin
11351 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11353 -- Subprogram body may have been created already because of an inline
11354 -- pragma, or because of multiple elaborations of the enclosing package
11355 -- when several instances of the subprogram appear in the main unit.
11357 if Present (Corresponding_Body (Act_Decl)) then
11358 return;
11359 end if;
11361 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11362 -- the mode now to ensure that any nodes generated during instantiation
11363 -- are properly marked as Ghost.
11365 Set_Ghost_Mode (Act_Decl_Id);
11367 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11369 -- Re-establish the state of information on which checks are suppressed.
11370 -- This information was set in Body_Info at the point of instantiation,
11371 -- and now we restore it so that the instance is compiled using the
11372 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11374 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11375 Scope_Suppress := Body_Info.Scope_Suppress;
11376 Opt.Ada_Version := Body_Info.Version;
11377 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11378 Restore_Warnings (Body_Info.Warnings);
11380 -- Install the SPARK mode which applies to the subprogram body
11382 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11384 if No (Gen_Body_Id) then
11386 -- For imported generic subprogram, no body to compile, complete
11387 -- the spec entity appropriately.
11389 if Is_Imported (Gen_Unit) then
11390 Set_Is_Imported (Act_Decl_Id);
11391 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11392 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11393 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11394 Set_Has_Completion (Act_Decl_Id);
11395 goto Leave;
11397 -- For other cases, compile the body
11399 else
11400 Load_Parent_Of_Generic
11401 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11402 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11403 end if;
11404 end if;
11406 Instantiation_Node := Inst_Node;
11408 if Present (Gen_Body_Id) then
11409 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11411 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11413 -- Either body is not present, or context is non-expanding, as
11414 -- when compiling a subunit. Mark the instance as completed, and
11415 -- diagnose a missing body when needed.
11417 if Expander_Active
11418 and then Operating_Mode = Generate_Code
11419 then
11420 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11421 end if;
11423 Set_Has_Completion (Act_Decl_Id);
11424 goto Leave;
11425 end if;
11427 Save_Env (Gen_Unit, Act_Decl_Id);
11428 Style_Check := False;
11430 -- If the context of the instance is subject to SPARK_Mode "off", the
11431 -- annotation is missing, or the body is instantiated at a later pass
11432 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11433 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11434 -- instance.
11436 if SPARK_Mode /= On
11437 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11438 then
11439 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11440 end if;
11442 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11443 Create_Instantiation_Source
11444 (Inst_Node,
11445 Gen_Body_Id,
11446 S_Adjustment);
11448 Act_Body :=
11449 Copy_Generic_Node
11450 (Original_Node (Gen_Body), Empty, Instantiating => True);
11452 -- Create proper defining name for the body, to correspond to the one
11453 -- in the spec.
11455 Act_Body_Id :=
11456 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11458 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11459 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11461 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11462 Set_Has_Completion (Act_Decl_Id);
11463 Check_Generic_Actuals (Pack_Id, False);
11465 -- Generate a reference to link the visible subprogram instance to
11466 -- the generic body, which for navigation purposes is the only
11467 -- available source for the instance.
11469 Generate_Reference
11470 (Related_Instance (Pack_Id),
11471 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11473 -- If it is a child unit, make the parent instance (which is an
11474 -- instance of the parent of the generic) visible. The parent
11475 -- instance is the prefix of the name of the generic unit.
11477 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11478 and then Nkind (Gen_Id) = N_Expanded_Name
11479 then
11480 Par_Ent := Entity (Prefix (Gen_Id));
11481 Par_Vis := Is_Immediately_Visible (Par_Ent);
11482 Install_Parent (Par_Ent, In_Body => True);
11483 Parent_Installed := True;
11485 elsif Is_Child_Unit (Gen_Unit) then
11486 Par_Ent := Scope (Gen_Unit);
11487 Par_Vis := Is_Immediately_Visible (Par_Ent);
11488 Install_Parent (Par_Ent, In_Body => True);
11489 Parent_Installed := True;
11490 end if;
11492 -- Subprogram body is placed in the body of wrapper package,
11493 -- whose spec contains the subprogram declaration as well as
11494 -- the renaming declarations for the generic parameters.
11496 Pack_Body :=
11497 Make_Package_Body (Loc,
11498 Defining_Unit_Name => New_Copy (Pack_Id),
11499 Declarations => New_List (Act_Body));
11501 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11503 -- If the instantiation is a library unit, then build resulting
11504 -- compilation unit nodes for the instance. The declaration of
11505 -- the enclosing package is the grandparent of the subprogram
11506 -- declaration. First replace the instantiation node as the unit
11507 -- of the corresponding compilation.
11509 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11510 if Parent (Inst_Node) = Cunit (Main_Unit) then
11511 Set_Unit (Parent (Inst_Node), Inst_Node);
11512 Build_Instance_Compilation_Unit_Nodes
11513 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11514 Analyze (Inst_Node);
11515 else
11516 Set_Parent (Pack_Body, Parent (Inst_Node));
11517 Analyze (Pack_Body);
11518 end if;
11520 else
11521 Insert_Before (Inst_Node, Pack_Body);
11522 Mark_Rewrite_Insertion (Pack_Body);
11523 Analyze (Pack_Body);
11525 if Expander_Active then
11526 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11527 end if;
11528 end if;
11530 Inherit_Context (Gen_Body, Inst_Node);
11532 Restore_Private_Views (Pack_Id, False);
11534 if Parent_Installed then
11535 Remove_Parent (In_Body => True);
11537 -- Restore the previous visibility of the parent
11539 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11540 end if;
11542 Restore_Env;
11543 Restore_Warnings (Saved_Warnings);
11545 -- Body not found. Error was emitted already. If there were no previous
11546 -- errors, this may be an instance whose scope is a premature instance.
11547 -- In that case we must insure that the (legal) program does raise
11548 -- program error if executed. We generate a subprogram body for this
11549 -- purpose. See DEC ac30vso.
11551 -- Should not reference proprietary DEC tests in comments ???
11553 elsif Serious_Errors_Detected = 0
11554 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11555 then
11556 if Body_Optional then
11557 goto Leave;
11559 elsif Ekind (Act_Decl_Id) = E_Procedure then
11560 Act_Body :=
11561 Make_Subprogram_Body (Loc,
11562 Specification =>
11563 Make_Procedure_Specification (Loc,
11564 Defining_Unit_Name =>
11565 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11566 Parameter_Specifications =>
11567 New_Copy_List
11568 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11570 Declarations => Empty_List,
11571 Handled_Statement_Sequence =>
11572 Make_Handled_Sequence_Of_Statements (Loc,
11573 Statements => New_List (
11574 Make_Raise_Program_Error (Loc,
11575 Reason => PE_Access_Before_Elaboration))));
11577 else
11578 Ret_Expr :=
11579 Make_Raise_Program_Error (Loc,
11580 Reason => PE_Access_Before_Elaboration);
11582 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11583 Set_Analyzed (Ret_Expr);
11585 Act_Body :=
11586 Make_Subprogram_Body (Loc,
11587 Specification =>
11588 Make_Function_Specification (Loc,
11589 Defining_Unit_Name =>
11590 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11591 Parameter_Specifications =>
11592 New_Copy_List
11593 (Parameter_Specifications (Parent (Act_Decl_Id))),
11594 Result_Definition =>
11595 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11597 Declarations => Empty_List,
11598 Handled_Statement_Sequence =>
11599 Make_Handled_Sequence_Of_Statements (Loc,
11600 Statements => New_List (
11601 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11602 end if;
11604 Pack_Body :=
11605 Make_Package_Body (Loc,
11606 Defining_Unit_Name => New_Copy (Pack_Id),
11607 Declarations => New_List (Act_Body));
11609 Insert_After (Inst_Node, Pack_Body);
11610 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11611 Analyze (Pack_Body);
11612 end if;
11614 Expander_Mode_Restore;
11616 <<Leave>>
11617 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11618 Restore_Ghost_Mode (Saved_GM);
11619 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11620 Style_Check := Saved_Style_Check;
11621 end Instantiate_Subprogram_Body;
11623 ----------------------
11624 -- Instantiate_Type --
11625 ----------------------
11627 function Instantiate_Type
11628 (Formal : Node_Id;
11629 Actual : Node_Id;
11630 Analyzed_Formal : Node_Id;
11631 Actual_Decls : List_Id) return List_Id
11633 A_Gen_T : constant Entity_Id :=
11634 Defining_Identifier (Analyzed_Formal);
11635 Def : constant Node_Id := Formal_Type_Definition (Formal);
11636 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11637 Act_T : Entity_Id;
11638 Ancestor : Entity_Id := Empty;
11639 Decl_Node : Node_Id;
11640 Decl_Nodes : List_Id;
11641 Loc : Source_Ptr;
11642 Subt : Entity_Id;
11644 procedure Diagnose_Predicated_Actual;
11645 -- There are a number of constructs in which a discrete type with
11646 -- predicates is illegal, e.g. as an index in an array type declaration.
11647 -- If a generic type is used is such a construct in a generic package
11648 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11649 -- of the generic contract that the actual cannot have predicates.
11651 procedure Validate_Array_Type_Instance;
11652 procedure Validate_Access_Subprogram_Instance;
11653 procedure Validate_Access_Type_Instance;
11654 procedure Validate_Derived_Type_Instance;
11655 procedure Validate_Derived_Interface_Type_Instance;
11656 procedure Validate_Discriminated_Formal_Type;
11657 procedure Validate_Interface_Type_Instance;
11658 procedure Validate_Private_Type_Instance;
11659 procedure Validate_Incomplete_Type_Instance;
11660 -- These procedures perform validation tests for the named case.
11661 -- Validate_Discriminated_Formal_Type is shared by formal private
11662 -- types and Ada 2012 formal incomplete types.
11664 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11665 -- Check that base types are the same and that the subtypes match
11666 -- statically. Used in several of the above.
11668 ---------------------------------
11669 -- Diagnose_Predicated_Actual --
11670 ---------------------------------
11672 procedure Diagnose_Predicated_Actual is
11673 begin
11674 if No_Predicate_On_Actual (A_Gen_T)
11675 and then Has_Predicates (Act_T)
11676 then
11677 Error_Msg_NE
11678 ("actual for& cannot be a type with predicate",
11679 Instantiation_Node, A_Gen_T);
11681 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11682 and then Has_Predicates (Act_T)
11683 and then not Has_Static_Predicate_Aspect (Act_T)
11684 then
11685 Error_Msg_NE
11686 ("actual for& cannot be a type with a dynamic predicate",
11687 Instantiation_Node, A_Gen_T);
11688 end if;
11689 end Diagnose_Predicated_Actual;
11691 --------------------
11692 -- Subtypes_Match --
11693 --------------------
11695 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11696 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11698 begin
11699 -- Some detailed comments would be useful here ???
11701 return ((Base_Type (T) = Act_T
11702 or else Base_Type (T) = Base_Type (Act_T))
11703 and then Subtypes_Statically_Match (T, Act_T))
11705 or else (Is_Class_Wide_Type (Gen_T)
11706 and then Is_Class_Wide_Type (Act_T)
11707 and then Subtypes_Match
11708 (Get_Instance_Of (Root_Type (Gen_T)),
11709 Root_Type (Act_T)))
11711 or else
11712 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11713 E_Anonymous_Access_Type)
11714 and then Ekind (Act_T) = Ekind (Gen_T)
11715 and then Subtypes_Statically_Match
11716 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11717 end Subtypes_Match;
11719 -----------------------------------------
11720 -- Validate_Access_Subprogram_Instance --
11721 -----------------------------------------
11723 procedure Validate_Access_Subprogram_Instance is
11724 begin
11725 if not Is_Access_Type (Act_T)
11726 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11727 then
11728 Error_Msg_NE
11729 ("expect access type in instantiation of &", Actual, Gen_T);
11730 Abandon_Instantiation (Actual);
11731 end if;
11733 -- According to AI05-288, actuals for access_to_subprograms must be
11734 -- subtype conformant with the generic formal. Previous to AI05-288
11735 -- only mode conformance was required.
11737 -- This is a binding interpretation that applies to previous versions
11738 -- of the language, no need to maintain previous weaker checks.
11740 Check_Subtype_Conformant
11741 (Designated_Type (Act_T),
11742 Designated_Type (A_Gen_T),
11743 Actual,
11744 Get_Inst => True);
11746 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11747 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11748 Error_Msg_NE
11749 ("protected access type not allowed for formal &",
11750 Actual, Gen_T);
11751 end if;
11753 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11754 Error_Msg_NE
11755 ("expect protected access type for formal &",
11756 Actual, Gen_T);
11757 end if;
11759 -- If the formal has a specified convention (which in most cases
11760 -- will be StdCall) verify that the actual has the same convention.
11762 if Has_Convention_Pragma (A_Gen_T)
11763 and then Convention (A_Gen_T) /= Convention (Act_T)
11764 then
11765 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11766 Error_Msg_NE
11767 ("actual for formal & must have convention %", Actual, Gen_T);
11768 end if;
11769 end Validate_Access_Subprogram_Instance;
11771 -----------------------------------
11772 -- Validate_Access_Type_Instance --
11773 -----------------------------------
11775 procedure Validate_Access_Type_Instance is
11776 Desig_Type : constant Entity_Id :=
11777 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11778 Desig_Act : Entity_Id;
11780 begin
11781 if not Is_Access_Type (Act_T) then
11782 Error_Msg_NE
11783 ("expect access type in instantiation of &", Actual, Gen_T);
11784 Abandon_Instantiation (Actual);
11785 end if;
11787 if Is_Access_Constant (A_Gen_T) then
11788 if not Is_Access_Constant (Act_T) then
11789 Error_Msg_N
11790 ("actual type must be access-to-constant type", Actual);
11791 Abandon_Instantiation (Actual);
11792 end if;
11793 else
11794 if Is_Access_Constant (Act_T) then
11795 Error_Msg_N
11796 ("actual type must be access-to-variable type", Actual);
11797 Abandon_Instantiation (Actual);
11799 elsif Ekind (A_Gen_T) = E_General_Access_Type
11800 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11801 then
11802 Error_Msg_N -- CODEFIX
11803 ("actual must be general access type!", Actual);
11804 Error_Msg_NE -- CODEFIX
11805 ("add ALL to }!", Actual, Act_T);
11806 Abandon_Instantiation (Actual);
11807 end if;
11808 end if;
11810 -- The designated subtypes, that is to say the subtypes introduced
11811 -- by an access type declaration (and not by a subtype declaration)
11812 -- must match.
11814 Desig_Act := Designated_Type (Base_Type (Act_T));
11816 -- The designated type may have been introduced through a limited_
11817 -- with clause, in which case retrieve the non-limited view. This
11818 -- applies to incomplete types as well as to class-wide types.
11820 if From_Limited_With (Desig_Act) then
11821 Desig_Act := Available_View (Desig_Act);
11822 end if;
11824 if not Subtypes_Match (Desig_Type, Desig_Act) then
11825 Error_Msg_NE
11826 ("designated type of actual does not match that of formal &",
11827 Actual, Gen_T);
11829 if not Predicates_Match (Desig_Type, Desig_Act) then
11830 Error_Msg_N ("\predicates do not match", Actual);
11831 end if;
11833 Abandon_Instantiation (Actual);
11835 elsif Is_Access_Type (Designated_Type (Act_T))
11836 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11838 Is_Constrained (Designated_Type (Desig_Type))
11839 then
11840 Error_Msg_NE
11841 ("designated type of actual does not match that of formal &",
11842 Actual, Gen_T);
11844 if not Predicates_Match (Desig_Type, Desig_Act) then
11845 Error_Msg_N ("\predicates do not match", Actual);
11846 end if;
11848 Abandon_Instantiation (Actual);
11849 end if;
11851 -- Ada 2005: null-exclusion indicators of the two types must agree
11853 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11854 Error_Msg_NE
11855 ("non null exclusion of actual and formal & do not match",
11856 Actual, Gen_T);
11857 end if;
11858 end Validate_Access_Type_Instance;
11860 ----------------------------------
11861 -- Validate_Array_Type_Instance --
11862 ----------------------------------
11864 procedure Validate_Array_Type_Instance is
11865 I1 : Node_Id;
11866 I2 : Node_Id;
11867 T2 : Entity_Id;
11869 function Formal_Dimensions return Nat;
11870 -- Count number of dimensions in array type formal
11872 -----------------------
11873 -- Formal_Dimensions --
11874 -----------------------
11876 function Formal_Dimensions return Nat is
11877 Num : Nat := 0;
11878 Index : Node_Id;
11880 begin
11881 if Nkind (Def) = N_Constrained_Array_Definition then
11882 Index := First (Discrete_Subtype_Definitions (Def));
11883 else
11884 Index := First (Subtype_Marks (Def));
11885 end if;
11887 while Present (Index) loop
11888 Num := Num + 1;
11889 Next_Index (Index);
11890 end loop;
11892 return Num;
11893 end Formal_Dimensions;
11895 -- Start of processing for Validate_Array_Type_Instance
11897 begin
11898 if not Is_Array_Type (Act_T) then
11899 Error_Msg_NE
11900 ("expect array type in instantiation of &", Actual, Gen_T);
11901 Abandon_Instantiation (Actual);
11903 elsif Nkind (Def) = N_Constrained_Array_Definition then
11904 if not (Is_Constrained (Act_T)) then
11905 Error_Msg_NE
11906 ("expect constrained array in instantiation of &",
11907 Actual, Gen_T);
11908 Abandon_Instantiation (Actual);
11909 end if;
11911 else
11912 if Is_Constrained (Act_T) then
11913 Error_Msg_NE
11914 ("expect unconstrained array in instantiation of &",
11915 Actual, Gen_T);
11916 Abandon_Instantiation (Actual);
11917 end if;
11918 end if;
11920 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11921 Error_Msg_NE
11922 ("dimensions of actual do not match formal &", Actual, Gen_T);
11923 Abandon_Instantiation (Actual);
11924 end if;
11926 I1 := First_Index (A_Gen_T);
11927 I2 := First_Index (Act_T);
11928 for J in 1 .. Formal_Dimensions loop
11930 -- If the indexes of the actual were given by a subtype_mark,
11931 -- the index was transformed into a range attribute. Retrieve
11932 -- the original type mark for checking.
11934 if Is_Entity_Name (Original_Node (I2)) then
11935 T2 := Entity (Original_Node (I2));
11936 else
11937 T2 := Etype (I2);
11938 end if;
11940 if not Subtypes_Match
11941 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11942 then
11943 Error_Msg_NE
11944 ("index types of actual do not match those of formal &",
11945 Actual, Gen_T);
11946 Abandon_Instantiation (Actual);
11947 end if;
11949 Next_Index (I1);
11950 Next_Index (I2);
11951 end loop;
11953 -- Check matching subtypes. Note that there are complex visibility
11954 -- issues when the generic is a child unit and some aspect of the
11955 -- generic type is declared in a parent unit of the generic. We do
11956 -- the test to handle this special case only after a direct check
11957 -- for static matching has failed. The case where both the component
11958 -- type and the array type are separate formals, and the component
11959 -- type is a private view may also require special checking in
11960 -- Subtypes_Match.
11962 if Subtypes_Match
11963 (Component_Type (A_Gen_T), Component_Type (Act_T))
11964 or else
11965 Subtypes_Match
11966 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11967 Component_Type (Act_T))
11968 then
11969 null;
11970 else
11971 Error_Msg_NE
11972 ("component subtype of actual does not match that of formal &",
11973 Actual, Gen_T);
11974 Abandon_Instantiation (Actual);
11975 end if;
11977 if Has_Aliased_Components (A_Gen_T)
11978 and then not Has_Aliased_Components (Act_T)
11979 then
11980 Error_Msg_NE
11981 ("actual must have aliased components to match formal type &",
11982 Actual, Gen_T);
11983 end if;
11984 end Validate_Array_Type_Instance;
11986 -----------------------------------------------
11987 -- Validate_Derived_Interface_Type_Instance --
11988 -----------------------------------------------
11990 procedure Validate_Derived_Interface_Type_Instance is
11991 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11992 Elmt : Elmt_Id;
11994 begin
11995 -- First apply interface instance checks
11997 Validate_Interface_Type_Instance;
11999 -- Verify that immediate parent interface is an ancestor of
12000 -- the actual.
12002 if Present (Par)
12003 and then not Interface_Present_In_Ancestor (Act_T, Par)
12004 then
12005 Error_Msg_NE
12006 ("interface actual must include progenitor&", Actual, Par);
12007 end if;
12009 -- Now verify that the actual includes all other ancestors of
12010 -- the formal.
12012 Elmt := First_Elmt (Interfaces (A_Gen_T));
12013 while Present (Elmt) loop
12014 if not Interface_Present_In_Ancestor
12015 (Act_T, Get_Instance_Of (Node (Elmt)))
12016 then
12017 Error_Msg_NE
12018 ("interface actual must include progenitor&",
12019 Actual, Node (Elmt));
12020 end if;
12022 Next_Elmt (Elmt);
12023 end loop;
12024 end Validate_Derived_Interface_Type_Instance;
12026 ------------------------------------
12027 -- Validate_Derived_Type_Instance --
12028 ------------------------------------
12030 procedure Validate_Derived_Type_Instance is
12031 Actual_Discr : Entity_Id;
12032 Ancestor_Discr : Entity_Id;
12034 begin
12035 -- If the parent type in the generic declaration is itself a previous
12036 -- formal type, then it is local to the generic and absent from the
12037 -- analyzed generic definition. In that case the ancestor is the
12038 -- instance of the formal (which must have been instantiated
12039 -- previously), unless the ancestor is itself a formal derived type.
12040 -- In this latter case (which is the subject of Corrigendum 8652/0038
12041 -- (AI-202) the ancestor of the formals is the ancestor of its
12042 -- parent. Otherwise, the analyzed generic carries the parent type.
12043 -- If the parent type is defined in a previous formal package, then
12044 -- the scope of that formal package is that of the generic type
12045 -- itself, and it has already been mapped into the corresponding type
12046 -- in the actual package.
12048 -- Common case: parent type defined outside of the generic
12050 if Is_Entity_Name (Subtype_Mark (Def))
12051 and then Present (Entity (Subtype_Mark (Def)))
12052 then
12053 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12055 -- Check whether parent is defined in a previous formal package
12057 elsif
12058 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12059 then
12060 Ancestor :=
12061 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12063 -- The type may be a local derivation, or a type extension of a
12064 -- previous formal, or of a formal of a parent package.
12066 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12067 or else
12068 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12069 then
12070 -- Check whether the parent is another derived formal type in the
12071 -- same generic unit.
12073 if Etype (A_Gen_T) /= A_Gen_T
12074 and then Is_Generic_Type (Etype (A_Gen_T))
12075 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12076 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12077 then
12078 -- Locate ancestor of parent from the subtype declaration
12079 -- created for the actual.
12081 declare
12082 Decl : Node_Id;
12084 begin
12085 Decl := First (Actual_Decls);
12086 while Present (Decl) loop
12087 if Nkind (Decl) = N_Subtype_Declaration
12088 and then Chars (Defining_Identifier (Decl)) =
12089 Chars (Etype (A_Gen_T))
12090 then
12091 Ancestor := Generic_Parent_Type (Decl);
12092 exit;
12093 else
12094 Next (Decl);
12095 end if;
12096 end loop;
12097 end;
12099 pragma Assert (Present (Ancestor));
12101 -- The ancestor itself may be a previous formal that has been
12102 -- instantiated.
12104 Ancestor := Get_Instance_Of (Ancestor);
12106 else
12107 Ancestor :=
12108 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12109 end if;
12111 -- Check whether parent is a previous formal of the current generic
12113 elsif Is_Derived_Type (A_Gen_T)
12114 and then Is_Generic_Type (Etype (A_Gen_T))
12115 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12116 then
12117 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12119 -- An unusual case: the actual is a type declared in a parent unit,
12120 -- but is not a formal type so there is no instance_of for it.
12121 -- Retrieve it by analyzing the record extension.
12123 elsif Is_Child_Unit (Scope (A_Gen_T))
12124 and then In_Open_Scopes (Scope (Act_T))
12125 and then Is_Generic_Instance (Scope (Act_T))
12126 then
12127 Analyze (Subtype_Mark (Def));
12128 Ancestor := Entity (Subtype_Mark (Def));
12130 else
12131 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12132 end if;
12134 -- If the formal derived type has pragma Preelaborable_Initialization
12135 -- then the actual type must have preelaborable initialization.
12137 if Known_To_Have_Preelab_Init (A_Gen_T)
12138 and then not Has_Preelaborable_Initialization (Act_T)
12139 then
12140 Error_Msg_NE
12141 ("actual for & must have preelaborable initialization",
12142 Actual, Gen_T);
12143 end if;
12145 -- Ada 2005 (AI-251)
12147 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12148 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12149 Error_Msg_NE
12150 ("(Ada 2005) expected type implementing & in instantiation",
12151 Actual, Ancestor);
12152 end if;
12154 -- Finally verify that the (instance of) the ancestor is an ancestor
12155 -- of the actual.
12157 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12158 Error_Msg_NE
12159 ("expect type derived from & in instantiation",
12160 Actual, First_Subtype (Ancestor));
12161 Abandon_Instantiation (Actual);
12162 end if;
12164 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12165 -- that the formal type declaration has been rewritten as a private
12166 -- extension.
12168 if Ada_Version >= Ada_2005
12169 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12170 and then Synchronized_Present (Parent (A_Gen_T))
12171 then
12172 -- The actual must be a synchronized tagged type
12174 if not Is_Tagged_Type (Act_T) then
12175 Error_Msg_N
12176 ("actual of synchronized type must be tagged", Actual);
12177 Abandon_Instantiation (Actual);
12179 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12180 and then Nkind (Type_Definition (Parent (Act_T))) =
12181 N_Derived_Type_Definition
12182 and then not Synchronized_Present
12183 (Type_Definition (Parent (Act_T)))
12184 then
12185 Error_Msg_N
12186 ("actual of synchronized type must be synchronized", Actual);
12187 Abandon_Instantiation (Actual);
12188 end if;
12189 end if;
12191 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12192 -- removes the second instance of the phrase "or allow pass by copy".
12194 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12195 Error_Msg_N
12196 ("cannot have atomic actual type for non-atomic formal type",
12197 Actual);
12199 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12200 Error_Msg_N
12201 ("cannot have volatile actual type for non-volatile formal type",
12202 Actual);
12203 end if;
12205 -- It should not be necessary to check for unknown discriminants on
12206 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12207 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12208 -- needs fixing. ???
12210 if Is_Definite_Subtype (A_Gen_T)
12211 and then not Unknown_Discriminants_Present (Formal)
12212 and then not Is_Definite_Subtype (Act_T)
12213 then
12214 Error_Msg_N ("actual subtype must be constrained", Actual);
12215 Abandon_Instantiation (Actual);
12216 end if;
12218 if not Unknown_Discriminants_Present (Formal) then
12219 if Is_Constrained (Ancestor) then
12220 if not Is_Constrained (Act_T) then
12221 Error_Msg_N ("actual subtype must be constrained", Actual);
12222 Abandon_Instantiation (Actual);
12223 end if;
12225 -- Ancestor is unconstrained, Check if generic formal and actual
12226 -- agree on constrainedness. The check only applies to array types
12227 -- and discriminated types.
12229 elsif Is_Constrained (Act_T) then
12230 if Ekind (Ancestor) = E_Access_Type
12231 or else (not Is_Constrained (A_Gen_T)
12232 and then Is_Composite_Type (A_Gen_T))
12233 then
12234 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12235 Abandon_Instantiation (Actual);
12236 end if;
12238 -- A class-wide type is only allowed if the formal has unknown
12239 -- discriminants.
12241 elsif Is_Class_Wide_Type (Act_T)
12242 and then not Has_Unknown_Discriminants (Ancestor)
12243 then
12244 Error_Msg_NE
12245 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12246 Abandon_Instantiation (Actual);
12248 -- Otherwise, the formal and actual must have the same number
12249 -- of discriminants and each discriminant of the actual must
12250 -- correspond to a discriminant of the formal.
12252 elsif Has_Discriminants (Act_T)
12253 and then not Has_Unknown_Discriminants (Act_T)
12254 and then Has_Discriminants (Ancestor)
12255 then
12256 Actual_Discr := First_Discriminant (Act_T);
12257 Ancestor_Discr := First_Discriminant (Ancestor);
12258 while Present (Actual_Discr)
12259 and then Present (Ancestor_Discr)
12260 loop
12261 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12262 No (Corresponding_Discriminant (Actual_Discr))
12263 then
12264 Error_Msg_NE
12265 ("discriminant & does not correspond "
12266 & "to ancestor discriminant", Actual, Actual_Discr);
12267 Abandon_Instantiation (Actual);
12268 end if;
12270 Next_Discriminant (Actual_Discr);
12271 Next_Discriminant (Ancestor_Discr);
12272 end loop;
12274 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12275 Error_Msg_NE
12276 ("actual for & must have same number of discriminants",
12277 Actual, Gen_T);
12278 Abandon_Instantiation (Actual);
12279 end if;
12281 -- This case should be caught by the earlier check for
12282 -- constrainedness, but the check here is added for completeness.
12284 elsif Has_Discriminants (Act_T)
12285 and then not Has_Unknown_Discriminants (Act_T)
12286 then
12287 Error_Msg_NE
12288 ("actual for & must not have discriminants", Actual, Gen_T);
12289 Abandon_Instantiation (Actual);
12291 elsif Has_Discriminants (Ancestor) then
12292 Error_Msg_NE
12293 ("actual for & must have known discriminants", Actual, Gen_T);
12294 Abandon_Instantiation (Actual);
12295 end if;
12297 if not Subtypes_Statically_Compatible
12298 (Act_T, Ancestor, Formal_Derived_Matching => True)
12299 then
12300 Error_Msg_N
12301 ("constraint on actual is incompatible with formal", Actual);
12302 Abandon_Instantiation (Actual);
12303 end if;
12304 end if;
12306 -- If the formal and actual types are abstract, check that there
12307 -- are no abstract primitives of the actual type that correspond to
12308 -- nonabstract primitives of the formal type (second sentence of
12309 -- RM95 3.9.3(9)).
12311 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12312 Check_Abstract_Primitives : declare
12313 Gen_Prims : constant Elist_Id :=
12314 Primitive_Operations (A_Gen_T);
12315 Gen_Elmt : Elmt_Id;
12316 Gen_Subp : Entity_Id;
12317 Anc_Subp : Entity_Id;
12318 Anc_Formal : Entity_Id;
12319 Anc_F_Type : Entity_Id;
12321 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12322 Act_Elmt : Elmt_Id;
12323 Act_Subp : Entity_Id;
12324 Act_Formal : Entity_Id;
12325 Act_F_Type : Entity_Id;
12327 Subprograms_Correspond : Boolean;
12329 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12330 -- Returns true if T2 is derived directly or indirectly from
12331 -- T1, including derivations from interfaces. T1 and T2 are
12332 -- required to be specific tagged base types.
12334 ------------------------
12335 -- Is_Tagged_Ancestor --
12336 ------------------------
12338 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12340 Intfc_Elmt : Elmt_Id;
12342 begin
12343 -- The predicate is satisfied if the types are the same
12345 if T1 = T2 then
12346 return True;
12348 -- If we've reached the top of the derivation chain then
12349 -- we know that T1 is not an ancestor of T2.
12351 elsif Etype (T2) = T2 then
12352 return False;
12354 -- Proceed to check T2's immediate parent
12356 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12357 return True;
12359 -- Finally, check to see if T1 is an ancestor of any of T2's
12360 -- progenitors.
12362 else
12363 Intfc_Elmt := First_Elmt (Interfaces (T2));
12364 while Present (Intfc_Elmt) loop
12365 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12366 return True;
12367 end if;
12369 Next_Elmt (Intfc_Elmt);
12370 end loop;
12371 end if;
12373 return False;
12374 end Is_Tagged_Ancestor;
12376 -- Start of processing for Check_Abstract_Primitives
12378 begin
12379 -- Loop over all of the formal derived type's primitives
12381 Gen_Elmt := First_Elmt (Gen_Prims);
12382 while Present (Gen_Elmt) loop
12383 Gen_Subp := Node (Gen_Elmt);
12385 -- If the primitive of the formal is not abstract, then
12386 -- determine whether there is a corresponding primitive of
12387 -- the actual type that's abstract.
12389 if not Is_Abstract_Subprogram (Gen_Subp) then
12390 Act_Elmt := First_Elmt (Act_Prims);
12391 while Present (Act_Elmt) loop
12392 Act_Subp := Node (Act_Elmt);
12394 -- If we find an abstract primitive of the actual,
12395 -- then we need to test whether it corresponds to the
12396 -- subprogram from which the generic formal primitive
12397 -- is inherited.
12399 if Is_Abstract_Subprogram (Act_Subp) then
12400 Anc_Subp := Alias (Gen_Subp);
12402 -- Test whether we have a corresponding primitive
12403 -- by comparing names, kinds, formal types, and
12404 -- result types.
12406 if Chars (Anc_Subp) = Chars (Act_Subp)
12407 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12408 then
12409 Anc_Formal := First_Formal (Anc_Subp);
12410 Act_Formal := First_Formal (Act_Subp);
12411 while Present (Anc_Formal)
12412 and then Present (Act_Formal)
12413 loop
12414 Anc_F_Type := Etype (Anc_Formal);
12415 Act_F_Type := Etype (Act_Formal);
12417 if Ekind (Anc_F_Type) =
12418 E_Anonymous_Access_Type
12419 then
12420 Anc_F_Type := Designated_Type (Anc_F_Type);
12422 if Ekind (Act_F_Type) =
12423 E_Anonymous_Access_Type
12424 then
12425 Act_F_Type :=
12426 Designated_Type (Act_F_Type);
12427 else
12428 exit;
12429 end if;
12431 elsif
12432 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12433 then
12434 exit;
12435 end if;
12437 Anc_F_Type := Base_Type (Anc_F_Type);
12438 Act_F_Type := Base_Type (Act_F_Type);
12440 -- If the formal is controlling, then the
12441 -- the type of the actual primitive's formal
12442 -- must be derived directly or indirectly
12443 -- from the type of the ancestor primitive's
12444 -- formal.
12446 if Is_Controlling_Formal (Anc_Formal) then
12447 if not Is_Tagged_Ancestor
12448 (Anc_F_Type, Act_F_Type)
12449 then
12450 exit;
12451 end if;
12453 -- Otherwise the types of the formals must
12454 -- be the same.
12456 elsif Anc_F_Type /= Act_F_Type then
12457 exit;
12458 end if;
12460 Next_Entity (Anc_Formal);
12461 Next_Entity (Act_Formal);
12462 end loop;
12464 -- If we traversed through all of the formals
12465 -- then so far the subprograms correspond, so
12466 -- now check that any result types correspond.
12468 if No (Anc_Formal) and then No (Act_Formal) then
12469 Subprograms_Correspond := True;
12471 if Ekind (Act_Subp) = E_Function then
12472 Anc_F_Type := Etype (Anc_Subp);
12473 Act_F_Type := Etype (Act_Subp);
12475 if Ekind (Anc_F_Type) =
12476 E_Anonymous_Access_Type
12477 then
12478 Anc_F_Type :=
12479 Designated_Type (Anc_F_Type);
12481 if Ekind (Act_F_Type) =
12482 E_Anonymous_Access_Type
12483 then
12484 Act_F_Type :=
12485 Designated_Type (Act_F_Type);
12486 else
12487 Subprograms_Correspond := False;
12488 end if;
12490 elsif
12491 Ekind (Act_F_Type)
12492 = E_Anonymous_Access_Type
12493 then
12494 Subprograms_Correspond := False;
12495 end if;
12497 Anc_F_Type := Base_Type (Anc_F_Type);
12498 Act_F_Type := Base_Type (Act_F_Type);
12500 -- Now either the result types must be
12501 -- the same or, if the result type is
12502 -- controlling, the result type of the
12503 -- actual primitive must descend from the
12504 -- result type of the ancestor primitive.
12506 if Subprograms_Correspond
12507 and then Anc_F_Type /= Act_F_Type
12508 and then
12509 Has_Controlling_Result (Anc_Subp)
12510 and then not Is_Tagged_Ancestor
12511 (Anc_F_Type, Act_F_Type)
12512 then
12513 Subprograms_Correspond := False;
12514 end if;
12515 end if;
12517 -- Found a matching subprogram belonging to
12518 -- formal ancestor type, so actual subprogram
12519 -- corresponds and this violates 3.9.3(9).
12521 if Subprograms_Correspond then
12522 Error_Msg_NE
12523 ("abstract subprogram & overrides "
12524 & "nonabstract subprogram of ancestor",
12525 Actual, Act_Subp);
12526 end if;
12527 end if;
12528 end if;
12529 end if;
12531 Next_Elmt (Act_Elmt);
12532 end loop;
12533 end if;
12535 Next_Elmt (Gen_Elmt);
12536 end loop;
12537 end Check_Abstract_Primitives;
12538 end if;
12540 -- Verify that limitedness matches. If parent is a limited
12541 -- interface then the generic formal is not unless declared
12542 -- explicitly so. If not declared limited, the actual cannot be
12543 -- limited (see AI05-0087).
12545 -- Even though this AI is a binding interpretation, we enable the
12546 -- check only in Ada 2012 mode, because this improper construct
12547 -- shows up in user code and in existing B-tests.
12549 if Is_Limited_Type (Act_T)
12550 and then not Is_Limited_Type (A_Gen_T)
12551 and then Ada_Version >= Ada_2012
12552 then
12553 if In_Instance then
12554 null;
12555 else
12556 Error_Msg_NE
12557 ("actual for non-limited & cannot be a limited type",
12558 Actual, Gen_T);
12559 Explain_Limited_Type (Act_T, Actual);
12560 Abandon_Instantiation (Actual);
12561 end if;
12562 end if;
12563 end Validate_Derived_Type_Instance;
12565 ----------------------------------------
12566 -- Validate_Discriminated_Formal_Type --
12567 ----------------------------------------
12569 procedure Validate_Discriminated_Formal_Type is
12570 Formal_Discr : Entity_Id;
12571 Actual_Discr : Entity_Id;
12572 Formal_Subt : Entity_Id;
12574 begin
12575 if Has_Discriminants (A_Gen_T) then
12576 if not Has_Discriminants (Act_T) then
12577 Error_Msg_NE
12578 ("actual for & must have discriminants", Actual, Gen_T);
12579 Abandon_Instantiation (Actual);
12581 elsif Is_Constrained (Act_T) then
12582 Error_Msg_NE
12583 ("actual for & must be unconstrained", Actual, Gen_T);
12584 Abandon_Instantiation (Actual);
12586 else
12587 Formal_Discr := First_Discriminant (A_Gen_T);
12588 Actual_Discr := First_Discriminant (Act_T);
12589 while Formal_Discr /= Empty loop
12590 if Actual_Discr = Empty then
12591 Error_Msg_NE
12592 ("discriminants on actual do not match formal",
12593 Actual, Gen_T);
12594 Abandon_Instantiation (Actual);
12595 end if;
12597 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12599 -- Access discriminants match if designated types do
12601 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12602 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12603 E_Anonymous_Access_Type
12604 and then
12605 Get_Instance_Of
12606 (Designated_Type (Base_Type (Formal_Subt))) =
12607 Designated_Type (Base_Type (Etype (Actual_Discr)))
12608 then
12609 null;
12611 elsif Base_Type (Formal_Subt) /=
12612 Base_Type (Etype (Actual_Discr))
12613 then
12614 Error_Msg_NE
12615 ("types of actual discriminants must match formal",
12616 Actual, Gen_T);
12617 Abandon_Instantiation (Actual);
12619 elsif not Subtypes_Statically_Match
12620 (Formal_Subt, Etype (Actual_Discr))
12621 and then Ada_Version >= Ada_95
12622 then
12623 Error_Msg_NE
12624 ("subtypes of actual discriminants must match formal",
12625 Actual, Gen_T);
12626 Abandon_Instantiation (Actual);
12627 end if;
12629 Next_Discriminant (Formal_Discr);
12630 Next_Discriminant (Actual_Discr);
12631 end loop;
12633 if Actual_Discr /= Empty then
12634 Error_Msg_NE
12635 ("discriminants on actual do not match formal",
12636 Actual, Gen_T);
12637 Abandon_Instantiation (Actual);
12638 end if;
12639 end if;
12640 end if;
12641 end Validate_Discriminated_Formal_Type;
12643 ---------------------------------------
12644 -- Validate_Incomplete_Type_Instance --
12645 ---------------------------------------
12647 procedure Validate_Incomplete_Type_Instance is
12648 begin
12649 if not Is_Tagged_Type (Act_T)
12650 and then Is_Tagged_Type (A_Gen_T)
12651 then
12652 Error_Msg_NE
12653 ("actual for & must be a tagged type", Actual, Gen_T);
12654 end if;
12656 Validate_Discriminated_Formal_Type;
12657 end Validate_Incomplete_Type_Instance;
12659 --------------------------------------
12660 -- Validate_Interface_Type_Instance --
12661 --------------------------------------
12663 procedure Validate_Interface_Type_Instance is
12664 begin
12665 if not Is_Interface (Act_T) then
12666 Error_Msg_NE
12667 ("actual for formal interface type must be an interface",
12668 Actual, Gen_T);
12670 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12671 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12672 or else Is_Protected_Interface (A_Gen_T) /=
12673 Is_Protected_Interface (Act_T)
12674 or else Is_Synchronized_Interface (A_Gen_T) /=
12675 Is_Synchronized_Interface (Act_T)
12676 then
12677 Error_Msg_NE
12678 ("actual for interface& does not match (RM 12.5.5(4))",
12679 Actual, Gen_T);
12680 end if;
12681 end Validate_Interface_Type_Instance;
12683 ------------------------------------
12684 -- Validate_Private_Type_Instance --
12685 ------------------------------------
12687 procedure Validate_Private_Type_Instance is
12688 begin
12689 if Is_Limited_Type (Act_T)
12690 and then not Is_Limited_Type (A_Gen_T)
12691 then
12692 if In_Instance then
12693 null;
12694 else
12695 Error_Msg_NE
12696 ("actual for non-limited & cannot be a limited type", Actual,
12697 Gen_T);
12698 Explain_Limited_Type (Act_T, Actual);
12699 Abandon_Instantiation (Actual);
12700 end if;
12702 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12703 and then not Has_Preelaborable_Initialization (Act_T)
12704 then
12705 Error_Msg_NE
12706 ("actual for & must have preelaborable initialization", Actual,
12707 Gen_T);
12709 elsif not Is_Definite_Subtype (Act_T)
12710 and then Is_Definite_Subtype (A_Gen_T)
12711 and then Ada_Version >= Ada_95
12712 then
12713 Error_Msg_NE
12714 ("actual for & must be a definite subtype", Actual, Gen_T);
12716 elsif not Is_Tagged_Type (Act_T)
12717 and then Is_Tagged_Type (A_Gen_T)
12718 then
12719 Error_Msg_NE
12720 ("actual for & must be a tagged type", Actual, Gen_T);
12721 end if;
12723 Validate_Discriminated_Formal_Type;
12724 Ancestor := Gen_T;
12725 end Validate_Private_Type_Instance;
12727 -- Start of processing for Instantiate_Type
12729 begin
12730 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12731 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12732 return New_List (Error);
12734 elsif not Is_Entity_Name (Actual)
12735 or else not Is_Type (Entity (Actual))
12736 then
12737 Error_Msg_NE
12738 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12739 Abandon_Instantiation (Actual);
12741 else
12742 Act_T := Entity (Actual);
12744 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12745 -- as a generic actual parameter if the corresponding formal type
12746 -- does not have a known_discriminant_part, or is a formal derived
12747 -- type that is an Unchecked_Union type.
12749 if Is_Unchecked_Union (Base_Type (Act_T)) then
12750 if not Has_Discriminants (A_Gen_T)
12751 or else (Is_Derived_Type (A_Gen_T)
12752 and then Is_Unchecked_Union (A_Gen_T))
12753 then
12754 null;
12755 else
12756 Error_Msg_N ("unchecked union cannot be the actual for a "
12757 & "discriminated formal type", Act_T);
12759 end if;
12760 end if;
12762 -- Deal with fixed/floating restrictions
12764 if Is_Floating_Point_Type (Act_T) then
12765 Check_Restriction (No_Floating_Point, Actual);
12766 elsif Is_Fixed_Point_Type (Act_T) then
12767 Check_Restriction (No_Fixed_Point, Actual);
12768 end if;
12770 -- Deal with error of using incomplete type as generic actual.
12771 -- This includes limited views of a type, even if the non-limited
12772 -- view may be available.
12774 if Ekind (Act_T) = E_Incomplete_Type
12775 or else (Is_Class_Wide_Type (Act_T)
12776 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12777 then
12778 -- If the formal is an incomplete type, the actual can be
12779 -- incomplete as well.
12781 if Ekind (A_Gen_T) = E_Incomplete_Type then
12782 null;
12784 elsif Is_Class_Wide_Type (Act_T)
12785 or else No (Full_View (Act_T))
12786 then
12787 Error_Msg_N ("premature use of incomplete type", Actual);
12788 Abandon_Instantiation (Actual);
12789 else
12790 Act_T := Full_View (Act_T);
12791 Set_Entity (Actual, Act_T);
12793 if Has_Private_Component (Act_T) then
12794 Error_Msg_N
12795 ("premature use of type with private component", Actual);
12796 end if;
12797 end if;
12799 -- Deal with error of premature use of private type as generic actual
12801 elsif Is_Private_Type (Act_T)
12802 and then Is_Private_Type (Base_Type (Act_T))
12803 and then not Is_Generic_Type (Act_T)
12804 and then not Is_Derived_Type (Act_T)
12805 and then No (Full_View (Root_Type (Act_T)))
12806 then
12807 -- If the formal is an incomplete type, the actual can be
12808 -- private or incomplete as well.
12810 if Ekind (A_Gen_T) = E_Incomplete_Type then
12811 null;
12812 else
12813 Error_Msg_N ("premature use of private type", Actual);
12814 end if;
12816 elsif Has_Private_Component (Act_T) then
12817 Error_Msg_N
12818 ("premature use of type with private component", Actual);
12819 end if;
12821 Set_Instance_Of (A_Gen_T, Act_T);
12823 -- If the type is generic, the class-wide type may also be used
12825 if Is_Tagged_Type (A_Gen_T)
12826 and then Is_Tagged_Type (Act_T)
12827 and then not Is_Class_Wide_Type (A_Gen_T)
12828 then
12829 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12830 Class_Wide_Type (Act_T));
12831 end if;
12833 if not Is_Abstract_Type (A_Gen_T)
12834 and then Is_Abstract_Type (Act_T)
12835 then
12836 Error_Msg_N
12837 ("actual of non-abstract formal cannot be abstract", Actual);
12838 end if;
12840 -- A generic scalar type is a first subtype for which we generate
12841 -- an anonymous base type. Indicate that the instance of this base
12842 -- is the base type of the actual.
12844 if Is_Scalar_Type (A_Gen_T) then
12845 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12846 end if;
12847 end if;
12849 if Error_Posted (Act_T) then
12850 null;
12851 else
12852 case Nkind (Def) is
12853 when N_Formal_Private_Type_Definition =>
12854 Validate_Private_Type_Instance;
12856 when N_Formal_Incomplete_Type_Definition =>
12857 Validate_Incomplete_Type_Instance;
12859 when N_Formal_Derived_Type_Definition =>
12860 Validate_Derived_Type_Instance;
12862 when N_Formal_Discrete_Type_Definition =>
12863 if not Is_Discrete_Type (Act_T) then
12864 Error_Msg_NE
12865 ("expect discrete type in instantiation of&",
12866 Actual, Gen_T);
12867 Abandon_Instantiation (Actual);
12868 end if;
12870 Diagnose_Predicated_Actual;
12872 when N_Formal_Signed_Integer_Type_Definition =>
12873 if not Is_Signed_Integer_Type (Act_T) then
12874 Error_Msg_NE
12875 ("expect signed integer type in instantiation of&",
12876 Actual, Gen_T);
12877 Abandon_Instantiation (Actual);
12878 end if;
12880 Diagnose_Predicated_Actual;
12882 when N_Formal_Modular_Type_Definition =>
12883 if not Is_Modular_Integer_Type (Act_T) then
12884 Error_Msg_NE
12885 ("expect modular type in instantiation of &",
12886 Actual, Gen_T);
12887 Abandon_Instantiation (Actual);
12888 end if;
12890 Diagnose_Predicated_Actual;
12892 when N_Formal_Floating_Point_Definition =>
12893 if not Is_Floating_Point_Type (Act_T) then
12894 Error_Msg_NE
12895 ("expect float type in instantiation of &", Actual, Gen_T);
12896 Abandon_Instantiation (Actual);
12897 end if;
12899 when N_Formal_Ordinary_Fixed_Point_Definition =>
12900 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12901 Error_Msg_NE
12902 ("expect ordinary fixed point type in instantiation of &",
12903 Actual, Gen_T);
12904 Abandon_Instantiation (Actual);
12905 end if;
12907 when N_Formal_Decimal_Fixed_Point_Definition =>
12908 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12909 Error_Msg_NE
12910 ("expect decimal type in instantiation of &",
12911 Actual, Gen_T);
12912 Abandon_Instantiation (Actual);
12913 end if;
12915 when N_Array_Type_Definition =>
12916 Validate_Array_Type_Instance;
12918 when N_Access_To_Object_Definition =>
12919 Validate_Access_Type_Instance;
12921 when N_Access_Function_Definition
12922 | N_Access_Procedure_Definition
12924 Validate_Access_Subprogram_Instance;
12926 when N_Record_Definition =>
12927 Validate_Interface_Type_Instance;
12929 when N_Derived_Type_Definition =>
12930 Validate_Derived_Interface_Type_Instance;
12932 when others =>
12933 raise Program_Error;
12934 end case;
12935 end if;
12937 Subt := New_Copy (Gen_T);
12939 -- Use adjusted sloc of subtype name as the location for other nodes in
12940 -- the subtype declaration.
12942 Loc := Sloc (Subt);
12944 Decl_Node :=
12945 Make_Subtype_Declaration (Loc,
12946 Defining_Identifier => Subt,
12947 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12949 if Is_Private_Type (Act_T) then
12950 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12952 elsif Is_Access_Type (Act_T)
12953 and then Is_Private_Type (Designated_Type (Act_T))
12954 then
12955 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12956 end if;
12958 -- In Ada 2012 the actual may be a limited view. Indicate that
12959 -- the local subtype must be treated as such.
12961 if From_Limited_With (Act_T) then
12962 Set_Ekind (Subt, E_Incomplete_Subtype);
12963 Set_From_Limited_With (Subt);
12964 end if;
12966 Decl_Nodes := New_List (Decl_Node);
12968 -- Flag actual derived types so their elaboration produces the
12969 -- appropriate renamings for the primitive operations of the ancestor.
12970 -- Flag actual for formal private types as well, to determine whether
12971 -- operations in the private part may override inherited operations.
12972 -- If the formal has an interface list, the ancestor is not the
12973 -- parent, but the analyzed formal that includes the interface
12974 -- operations of all its progenitors.
12976 -- Same treatment for formal private types, so we can check whether the
12977 -- type is tagged limited when validating derivations in the private
12978 -- part. (See AI05-096).
12980 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12981 if Present (Interface_List (Def)) then
12982 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12983 else
12984 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12985 end if;
12987 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12988 N_Formal_Incomplete_Type_Definition)
12989 then
12990 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12991 end if;
12993 -- If the actual is a synchronized type that implements an interface,
12994 -- the primitive operations are attached to the corresponding record,
12995 -- and we have to treat it as an additional generic actual, so that its
12996 -- primitive operations become visible in the instance. The task or
12997 -- protected type itself does not carry primitive operations.
12999 if Is_Concurrent_Type (Act_T)
13000 and then Is_Tagged_Type (Act_T)
13001 and then Present (Corresponding_Record_Type (Act_T))
13002 and then Present (Ancestor)
13003 and then Is_Interface (Ancestor)
13004 then
13005 declare
13006 Corr_Rec : constant Entity_Id :=
13007 Corresponding_Record_Type (Act_T);
13008 New_Corr : Entity_Id;
13009 Corr_Decl : Node_Id;
13011 begin
13012 New_Corr := Make_Temporary (Loc, 'S');
13013 Corr_Decl :=
13014 Make_Subtype_Declaration (Loc,
13015 Defining_Identifier => New_Corr,
13016 Subtype_Indication =>
13017 New_Occurrence_Of (Corr_Rec, Loc));
13018 Append_To (Decl_Nodes, Corr_Decl);
13020 if Ekind (Act_T) = E_Task_Type then
13021 Set_Ekind (Subt, E_Task_Subtype);
13022 else
13023 Set_Ekind (Subt, E_Protected_Subtype);
13024 end if;
13026 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13027 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13028 Set_Generic_Parent_Type (Decl_Node, Empty);
13029 end;
13030 end if;
13032 -- For a floating-point type, capture dimension info if any, because
13033 -- the generated subtype declaration does not come from source and
13034 -- will not process dimensions.
13036 if Is_Floating_Point_Type (Act_T) then
13037 Copy_Dimensions (Act_T, Subt);
13038 end if;
13040 return Decl_Nodes;
13041 end Instantiate_Type;
13043 ---------------------
13044 -- Is_In_Main_Unit --
13045 ---------------------
13047 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13048 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13049 Current_Unit : Node_Id;
13051 begin
13052 if Unum = Main_Unit then
13053 return True;
13055 -- If the current unit is a subunit then it is either the main unit or
13056 -- is being compiled as part of the main unit.
13058 elsif Nkind (N) = N_Compilation_Unit then
13059 return Nkind (Unit (N)) = N_Subunit;
13060 end if;
13062 Current_Unit := Parent (N);
13063 while Present (Current_Unit)
13064 and then Nkind (Current_Unit) /= N_Compilation_Unit
13065 loop
13066 Current_Unit := Parent (Current_Unit);
13067 end loop;
13069 -- The instantiation node is in the main unit, or else the current node
13070 -- (perhaps as the result of nested instantiations) is in the main unit,
13071 -- or in the declaration of the main unit, which in this last case must
13072 -- be a body.
13074 return
13075 Current_Unit = Cunit (Main_Unit)
13076 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13077 or else (Present (Current_Unit)
13078 and then Present (Library_Unit (Current_Unit))
13079 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13080 end Is_In_Main_Unit;
13082 ----------------------------
13083 -- Load_Parent_Of_Generic --
13084 ----------------------------
13086 procedure Load_Parent_Of_Generic
13087 (N : Node_Id;
13088 Spec : Node_Id;
13089 Body_Optional : Boolean := False)
13091 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13092 Saved_Style_Check : constant Boolean := Style_Check;
13093 Saved_Warnings : constant Warning_Record := Save_Warnings;
13094 True_Parent : Node_Id;
13095 Inst_Node : Node_Id;
13096 OK : Boolean;
13097 Previous_Instances : constant Elist_Id := New_Elmt_List;
13099 procedure Collect_Previous_Instances (Decls : List_Id);
13100 -- Collect all instantiations in the given list of declarations, that
13101 -- precede the generic that we need to load. If the bodies of these
13102 -- instantiations are available, we must analyze them, to ensure that
13103 -- the public symbols generated are the same when the unit is compiled
13104 -- to generate code, and when it is compiled in the context of a unit
13105 -- that needs a particular nested instance. This process is applied to
13106 -- both package and subprogram instances.
13108 --------------------------------
13109 -- Collect_Previous_Instances --
13110 --------------------------------
13112 procedure Collect_Previous_Instances (Decls : List_Id) is
13113 Decl : Node_Id;
13115 begin
13116 Decl := First (Decls);
13117 while Present (Decl) loop
13118 if Sloc (Decl) >= Sloc (Inst_Node) then
13119 return;
13121 -- If Decl is an instantiation, then record it as requiring
13122 -- instantiation of the corresponding body, except if it is an
13123 -- abbreviated instantiation generated internally for conformance
13124 -- checking purposes only for the case of a formal package
13125 -- declared without a box (see Instantiate_Formal_Package). Such
13126 -- an instantiation does not generate any code (the actual code
13127 -- comes from actual) and thus does not need to be analyzed here.
13128 -- If the instantiation appears with a generic package body it is
13129 -- not analyzed here either.
13131 elsif Nkind (Decl) = N_Package_Instantiation
13132 and then not Is_Internal (Defining_Entity (Decl))
13133 then
13134 Append_Elmt (Decl, Previous_Instances);
13136 -- For a subprogram instantiation, omit instantiations intrinsic
13137 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13139 elsif Nkind_In (Decl, N_Function_Instantiation,
13140 N_Procedure_Instantiation)
13141 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13142 then
13143 Append_Elmt (Decl, Previous_Instances);
13145 elsif Nkind (Decl) = N_Package_Declaration then
13146 Collect_Previous_Instances
13147 (Visible_Declarations (Specification (Decl)));
13148 Collect_Previous_Instances
13149 (Private_Declarations (Specification (Decl)));
13151 -- Previous non-generic bodies may contain instances as well
13153 elsif Nkind (Decl) = N_Package_Body
13154 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13155 then
13156 Collect_Previous_Instances (Declarations (Decl));
13158 elsif Nkind (Decl) = N_Subprogram_Body
13159 and then not Acts_As_Spec (Decl)
13160 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13161 then
13162 Collect_Previous_Instances (Declarations (Decl));
13163 end if;
13165 Next (Decl);
13166 end loop;
13167 end Collect_Previous_Instances;
13169 -- Start of processing for Load_Parent_Of_Generic
13171 begin
13172 if not In_Same_Source_Unit (N, Spec)
13173 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13174 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13175 and then not Is_In_Main_Unit (Spec))
13176 then
13177 -- Find body of parent of spec, and analyze it. A special case arises
13178 -- when the parent is an instantiation, that is to say when we are
13179 -- currently instantiating a nested generic. In that case, there is
13180 -- no separate file for the body of the enclosing instance. Instead,
13181 -- the enclosing body must be instantiated as if it were a pending
13182 -- instantiation, in order to produce the body for the nested generic
13183 -- we require now. Note that in that case the generic may be defined
13184 -- in a package body, the instance defined in the same package body,
13185 -- and the original enclosing body may not be in the main unit.
13187 Inst_Node := Empty;
13189 True_Parent := Parent (Spec);
13190 while Present (True_Parent)
13191 and then Nkind (True_Parent) /= N_Compilation_Unit
13192 loop
13193 if Nkind (True_Parent) = N_Package_Declaration
13194 and then
13195 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13196 then
13197 -- Parent is a compilation unit that is an instantiation.
13198 -- Instantiation node has been replaced with package decl.
13200 Inst_Node := Original_Node (True_Parent);
13201 exit;
13203 elsif Nkind (True_Parent) = N_Package_Declaration
13204 and then Present (Generic_Parent (Specification (True_Parent)))
13205 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13206 then
13207 -- Parent is an instantiation within another specification.
13208 -- Declaration for instance has been inserted before original
13209 -- instantiation node. A direct link would be preferable?
13211 Inst_Node := Next (True_Parent);
13212 while Present (Inst_Node)
13213 and then Nkind (Inst_Node) /= N_Package_Instantiation
13214 loop
13215 Next (Inst_Node);
13216 end loop;
13218 -- If the instance appears within a generic, and the generic
13219 -- unit is defined within a formal package of the enclosing
13220 -- generic, there is no generic body available, and none
13221 -- needed. A more precise test should be used ???
13223 if No (Inst_Node) then
13224 return;
13225 end if;
13227 exit;
13229 else
13230 True_Parent := Parent (True_Parent);
13231 end if;
13232 end loop;
13234 -- Case where we are currently instantiating a nested generic
13236 if Present (Inst_Node) then
13237 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13239 -- Instantiation node and declaration of instantiated package
13240 -- were exchanged when only the declaration was needed.
13241 -- Restore instantiation node before proceeding with body.
13243 Set_Unit (Parent (True_Parent), Inst_Node);
13244 end if;
13246 -- Now complete instantiation of enclosing body, if it appears in
13247 -- some other unit. If it appears in the current unit, the body
13248 -- will have been instantiated already.
13250 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13252 -- We need to determine the expander mode to instantiate the
13253 -- enclosing body. Because the generic body we need may use
13254 -- global entities declared in the enclosing package (including
13255 -- aggregates) it is in general necessary to compile this body
13256 -- with expansion enabled, except if we are within a generic
13257 -- package, in which case the usual generic rule applies.
13259 declare
13260 Exp_Status : Boolean := True;
13261 Scop : Entity_Id;
13263 begin
13264 -- Loop through scopes looking for generic package
13266 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13267 while Present (Scop)
13268 and then Scop /= Standard_Standard
13269 loop
13270 if Ekind (Scop) = E_Generic_Package then
13271 Exp_Status := False;
13272 exit;
13273 end if;
13275 Scop := Scope (Scop);
13276 end loop;
13278 -- Collect previous instantiations in the unit that contains
13279 -- the desired generic.
13281 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13282 and then not Body_Optional
13283 then
13284 declare
13285 Decl : Elmt_Id;
13286 Info : Pending_Body_Info;
13287 Par : Node_Id;
13289 begin
13290 Par := Parent (Inst_Node);
13291 while Present (Par) loop
13292 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13293 Par := Parent (Par);
13294 end loop;
13296 pragma Assert (Present (Par));
13298 if Nkind (Par) = N_Package_Body then
13299 Collect_Previous_Instances (Declarations (Par));
13301 elsif Nkind (Par) = N_Package_Declaration then
13302 Collect_Previous_Instances
13303 (Visible_Declarations (Specification (Par)));
13304 Collect_Previous_Instances
13305 (Private_Declarations (Specification (Par)));
13307 else
13308 -- Enclosing unit is a subprogram body. In this
13309 -- case all instance bodies are processed in order
13310 -- and there is no need to collect them separately.
13312 null;
13313 end if;
13315 Decl := First_Elmt (Previous_Instances);
13316 while Present (Decl) loop
13317 Info :=
13318 (Inst_Node => Node (Decl),
13319 Act_Decl =>
13320 Instance_Spec (Node (Decl)),
13321 Expander_Status => Exp_Status,
13322 Current_Sem_Unit =>
13323 Get_Code_Unit (Sloc (Node (Decl))),
13324 Scope_Suppress => Scope_Suppress,
13325 Local_Suppress_Stack_Top =>
13326 Local_Suppress_Stack_Top,
13327 Version => Ada_Version,
13328 Version_Pragma => Ada_Version_Pragma,
13329 Warnings => Save_Warnings,
13330 SPARK_Mode => SPARK_Mode,
13331 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13333 -- Package instance
13335 if Nkind (Node (Decl)) = N_Package_Instantiation
13336 then
13337 Instantiate_Package_Body
13338 (Info, Body_Optional => True);
13340 -- Subprogram instance
13342 else
13343 -- The instance_spec is in the wrapper package,
13344 -- usually followed by its local renaming
13345 -- declaration. See Build_Subprogram_Renaming
13346 -- for details. If the instance carries aspects,
13347 -- these result in the corresponding pragmas,
13348 -- inserted after the subprogram declaration.
13349 -- They must be skipped as well when retrieving
13350 -- the desired spec. Some of them may have been
13351 -- rewritten as null statements.
13352 -- A direct link would be more robust ???
13354 declare
13355 Decl : Node_Id :=
13356 (Last (Visible_Declarations
13357 (Specification (Info.Act_Decl))));
13358 begin
13359 while Nkind_In (Decl,
13360 N_Null_Statement,
13361 N_Pragma,
13362 N_Subprogram_Renaming_Declaration)
13363 loop
13364 Decl := Prev (Decl);
13365 end loop;
13367 Info.Act_Decl := Decl;
13368 end;
13370 Instantiate_Subprogram_Body
13371 (Info, Body_Optional => True);
13372 end if;
13374 Next_Elmt (Decl);
13375 end loop;
13376 end;
13377 end if;
13379 Instantiate_Package_Body
13380 (Body_Info =>
13381 ((Inst_Node => Inst_Node,
13382 Act_Decl => True_Parent,
13383 Expander_Status => Exp_Status,
13384 Current_Sem_Unit => Get_Code_Unit
13385 (Sloc (Inst_Node)),
13386 Scope_Suppress => Scope_Suppress,
13387 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13388 Version => Ada_Version,
13389 Version_Pragma => Ada_Version_Pragma,
13390 Warnings => Save_Warnings,
13391 SPARK_Mode => SPARK_Mode,
13392 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13393 Body_Optional => Body_Optional);
13394 end;
13395 end if;
13397 -- Case where we are not instantiating a nested generic
13399 else
13400 Opt.Style_Check := False;
13401 Expander_Mode_Save_And_Set (True);
13402 Load_Needed_Body (Comp_Unit, OK);
13403 Opt.Style_Check := Saved_Style_Check;
13404 Restore_Warnings (Saved_Warnings);
13405 Expander_Mode_Restore;
13407 if not OK
13408 and then Unit_Requires_Body (Defining_Entity (Spec))
13409 and then not Body_Optional
13410 then
13411 declare
13412 Bname : constant Unit_Name_Type :=
13413 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13415 begin
13416 -- In CodePeer mode, the missing body may make the analysis
13417 -- incomplete, but we do not treat it as fatal.
13419 if CodePeer_Mode then
13420 return;
13422 else
13423 Error_Msg_Unit_1 := Bname;
13424 Error_Msg_N ("this instantiation requires$!", N);
13425 Error_Msg_File_1 :=
13426 Get_File_Name (Bname, Subunit => False);
13427 Error_Msg_N ("\but file{ was not found!", N);
13428 raise Unrecoverable_Error;
13429 end if;
13430 end;
13431 end if;
13432 end if;
13433 end if;
13435 -- If loading parent of the generic caused an instantiation circularity,
13436 -- we abandon compilation at this point, because otherwise in some cases
13437 -- we get into trouble with infinite recursions after this point.
13439 if Circularity_Detected then
13440 raise Unrecoverable_Error;
13441 end if;
13442 end Load_Parent_Of_Generic;
13444 ---------------------------------
13445 -- Map_Formal_Package_Entities --
13446 ---------------------------------
13448 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13449 E1 : Entity_Id;
13450 E2 : Entity_Id;
13452 begin
13453 Set_Instance_Of (Form, Act);
13455 -- Traverse formal and actual package to map the corresponding entities.
13456 -- We skip over internal entities that may be generated during semantic
13457 -- analysis, and find the matching entities by name, given that they
13458 -- must appear in the same order.
13460 E1 := First_Entity (Form);
13461 E2 := First_Entity (Act);
13462 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13463 -- Could this test be a single condition??? Seems like it could, and
13464 -- isn't FPE (Form) a constant anyway???
13466 if not Is_Internal (E1)
13467 and then Present (Parent (E1))
13468 and then not Is_Class_Wide_Type (E1)
13469 and then not Is_Internal_Name (Chars (E1))
13470 then
13471 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13472 Next_Entity (E2);
13473 end loop;
13475 if No (E2) then
13476 exit;
13477 else
13478 Set_Instance_Of (E1, E2);
13480 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13481 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13482 end if;
13484 if Is_Constrained (E1) then
13485 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13486 end if;
13488 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13489 Map_Formal_Package_Entities (E1, E2);
13490 end if;
13491 end if;
13492 end if;
13494 Next_Entity (E1);
13495 end loop;
13496 end Map_Formal_Package_Entities;
13498 -----------------------
13499 -- Move_Freeze_Nodes --
13500 -----------------------
13502 procedure Move_Freeze_Nodes
13503 (Out_Of : Entity_Id;
13504 After : Node_Id;
13505 L : List_Id)
13507 Decl : Node_Id;
13508 Next_Decl : Node_Id;
13509 Next_Node : Node_Id := After;
13510 Spec : Node_Id;
13512 function Is_Outer_Type (T : Entity_Id) return Boolean;
13513 -- Check whether entity is declared in a scope external to that of the
13514 -- generic unit.
13516 -------------------
13517 -- Is_Outer_Type --
13518 -------------------
13520 function Is_Outer_Type (T : Entity_Id) return Boolean is
13521 Scop : Entity_Id := Scope (T);
13523 begin
13524 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13525 return True;
13527 else
13528 while Scop /= Standard_Standard loop
13529 if Scop = Out_Of then
13530 return False;
13531 else
13532 Scop := Scope (Scop);
13533 end if;
13534 end loop;
13536 return True;
13537 end if;
13538 end Is_Outer_Type;
13540 -- Start of processing for Move_Freeze_Nodes
13542 begin
13543 if No (L) then
13544 return;
13545 end if;
13547 -- First remove the freeze nodes that may appear before all other
13548 -- declarations.
13550 Decl := First (L);
13551 while Present (Decl)
13552 and then Nkind (Decl) = N_Freeze_Entity
13553 and then Is_Outer_Type (Entity (Decl))
13554 loop
13555 Decl := Remove_Head (L);
13556 Insert_After (Next_Node, Decl);
13557 Set_Analyzed (Decl, False);
13558 Next_Node := Decl;
13559 Decl := First (L);
13560 end loop;
13562 -- Next scan the list of declarations and remove each freeze node that
13563 -- appears ahead of the current node.
13565 while Present (Decl) loop
13566 while Present (Next (Decl))
13567 and then Nkind (Next (Decl)) = N_Freeze_Entity
13568 and then Is_Outer_Type (Entity (Next (Decl)))
13569 loop
13570 Next_Decl := Remove_Next (Decl);
13571 Insert_After (Next_Node, Next_Decl);
13572 Set_Analyzed (Next_Decl, False);
13573 Next_Node := Next_Decl;
13574 end loop;
13576 -- If the declaration is a nested package or concurrent type, then
13577 -- recurse. Nested generic packages will have been processed from the
13578 -- inside out.
13580 case Nkind (Decl) is
13581 when N_Package_Declaration =>
13582 Spec := Specification (Decl);
13584 when N_Task_Type_Declaration =>
13585 Spec := Task_Definition (Decl);
13587 when N_Protected_Type_Declaration =>
13588 Spec := Protected_Definition (Decl);
13590 when others =>
13591 Spec := Empty;
13592 end case;
13594 if Present (Spec) then
13595 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13596 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13597 end if;
13599 Next (Decl);
13600 end loop;
13601 end Move_Freeze_Nodes;
13603 ----------------
13604 -- Next_Assoc --
13605 ----------------
13607 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13608 begin
13609 return Generic_Renamings.Table (E).Next_In_HTable;
13610 end Next_Assoc;
13612 ------------------------
13613 -- Preanalyze_Actuals --
13614 ------------------------
13616 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13617 Assoc : Node_Id;
13618 Act : Node_Id;
13619 Errs : constant Nat := Serious_Errors_Detected;
13621 Cur : Entity_Id := Empty;
13622 -- Current homograph of the instance name
13624 Vis : Boolean := False;
13625 -- Saved visibility status of the current homograph
13627 begin
13628 Assoc := First (Generic_Associations (N));
13630 -- If the instance is a child unit, its name may hide an outer homonym,
13631 -- so make it invisible to perform name resolution on the actuals.
13633 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13634 and then Present
13635 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13636 then
13637 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13639 if Is_Compilation_Unit (Cur) then
13640 Vis := Is_Immediately_Visible (Cur);
13641 Set_Is_Immediately_Visible (Cur, False);
13642 else
13643 Cur := Empty;
13644 end if;
13645 end if;
13647 while Present (Assoc) loop
13648 if Nkind (Assoc) /= N_Others_Choice then
13649 Act := Explicit_Generic_Actual_Parameter (Assoc);
13651 -- Within a nested instantiation, a defaulted actual is an empty
13652 -- association, so nothing to analyze. If the subprogram actual
13653 -- is an attribute, analyze prefix only, because actual is not a
13654 -- complete attribute reference.
13656 -- If actual is an allocator, analyze expression only. The full
13657 -- analysis can generate code, and if instance is a compilation
13658 -- unit we have to wait until the package instance is installed
13659 -- to have a proper place to insert this code.
13661 -- String literals may be operators, but at this point we do not
13662 -- know whether the actual is a formal subprogram or a string.
13664 if No (Act) then
13665 null;
13667 elsif Nkind (Act) = N_Attribute_Reference then
13668 Analyze (Prefix (Act));
13670 elsif Nkind (Act) = N_Explicit_Dereference then
13671 Analyze (Prefix (Act));
13673 elsif Nkind (Act) = N_Allocator then
13674 declare
13675 Expr : constant Node_Id := Expression (Act);
13677 begin
13678 if Nkind (Expr) = N_Subtype_Indication then
13679 Analyze (Subtype_Mark (Expr));
13681 -- Analyze separately each discriminant constraint, when
13682 -- given with a named association.
13684 declare
13685 Constr : Node_Id;
13687 begin
13688 Constr := First (Constraints (Constraint (Expr)));
13689 while Present (Constr) loop
13690 if Nkind (Constr) = N_Discriminant_Association then
13691 Analyze (Expression (Constr));
13692 else
13693 Analyze (Constr);
13694 end if;
13696 Next (Constr);
13697 end loop;
13698 end;
13700 else
13701 Analyze (Expr);
13702 end if;
13703 end;
13705 elsif Nkind (Act) /= N_Operator_Symbol then
13706 Analyze (Act);
13708 -- Within a package instance, mark actuals that are limited
13709 -- views, so their use can be moved to the body of the
13710 -- enclosing unit.
13712 if Is_Entity_Name (Act)
13713 and then Is_Type (Entity (Act))
13714 and then From_Limited_With (Entity (Act))
13715 and then Present (Inst)
13716 then
13717 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13718 end if;
13719 end if;
13721 if Errs /= Serious_Errors_Detected then
13723 -- Do a minimal analysis of the generic, to prevent spurious
13724 -- warnings complaining about the generic being unreferenced,
13725 -- before abandoning the instantiation.
13727 Analyze (Name (N));
13729 if Is_Entity_Name (Name (N))
13730 and then Etype (Name (N)) /= Any_Type
13731 then
13732 Generate_Reference (Entity (Name (N)), Name (N));
13733 Set_Is_Instantiated (Entity (Name (N)));
13734 end if;
13736 if Present (Cur) then
13738 -- For the case of a child instance hiding an outer homonym,
13739 -- provide additional warning which might explain the error.
13741 Set_Is_Immediately_Visible (Cur, Vis);
13742 Error_Msg_NE
13743 ("& hides outer unit with the same name??",
13744 N, Defining_Unit_Name (N));
13745 end if;
13747 Abandon_Instantiation (Act);
13748 end if;
13749 end if;
13751 Next (Assoc);
13752 end loop;
13754 if Present (Cur) then
13755 Set_Is_Immediately_Visible (Cur, Vis);
13756 end if;
13757 end Preanalyze_Actuals;
13759 -------------------
13760 -- Remove_Parent --
13761 -------------------
13763 procedure Remove_Parent (In_Body : Boolean := False) is
13764 S : Entity_Id := Current_Scope;
13765 -- S is the scope containing the instantiation just completed. The scope
13766 -- stack contains the parent instances of the instantiation, followed by
13767 -- the original S.
13769 Cur_P : Entity_Id;
13770 E : Entity_Id;
13771 P : Entity_Id;
13772 Hidden : Elmt_Id;
13774 begin
13775 -- After child instantiation is complete, remove from scope stack the
13776 -- extra copy of the current scope, and then remove parent instances.
13778 if not In_Body then
13779 Pop_Scope;
13781 while Current_Scope /= S loop
13782 P := Current_Scope;
13783 End_Package_Scope (Current_Scope);
13785 if In_Open_Scopes (P) then
13786 E := First_Entity (P);
13787 while Present (E) loop
13788 Set_Is_Immediately_Visible (E, True);
13789 Next_Entity (E);
13790 end loop;
13792 -- If instantiation is declared in a block, it is the enclosing
13793 -- scope that might be a parent instance. Note that only one
13794 -- block can be involved, because the parent instances have
13795 -- been installed within it.
13797 if Ekind (P) = E_Block then
13798 Cur_P := Scope (P);
13799 else
13800 Cur_P := P;
13801 end if;
13803 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13804 -- We are within an instance of some sibling. Retain
13805 -- visibility of parent, for proper subsequent cleanup, and
13806 -- reinstall private declarations as well.
13808 Set_In_Private_Part (P);
13809 Install_Private_Declarations (P);
13810 end if;
13812 -- If the ultimate parent is a top-level unit recorded in
13813 -- Instance_Parent_Unit, then reset its visibility to what it was
13814 -- before instantiation. (It's not clear what the purpose is of
13815 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13816 -- present before the ultimate parent test was added.???)
13818 elsif not In_Open_Scopes (Scope (P))
13819 or else (P = Instance_Parent_Unit
13820 and then not Parent_Unit_Visible)
13821 then
13822 Set_Is_Immediately_Visible (P, False);
13824 -- If the current scope is itself an instantiation of a generic
13825 -- nested within P, and we are in the private part of body of this
13826 -- instantiation, restore the full views of P, that were removed
13827 -- in End_Package_Scope above. This obscure case can occur when a
13828 -- subunit of a generic contains an instance of a child unit of
13829 -- its generic parent unit.
13831 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13832 declare
13833 Par : constant Entity_Id :=
13834 Generic_Parent (Package_Specification (S));
13835 begin
13836 if Present (Par)
13837 and then P = Scope (Par)
13838 and then (In_Package_Body (S) or else In_Private_Part (S))
13839 then
13840 Set_In_Private_Part (P);
13841 Install_Private_Declarations (P);
13842 end if;
13843 end;
13844 end if;
13845 end loop;
13847 -- Reset visibility of entities in the enclosing scope
13849 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13851 Hidden := First_Elmt (Hidden_Entities);
13852 while Present (Hidden) loop
13853 Set_Is_Immediately_Visible (Node (Hidden), True);
13854 Next_Elmt (Hidden);
13855 end loop;
13857 else
13858 -- Each body is analyzed separately, and there is no context that
13859 -- needs preserving from one body instance to the next, so remove all
13860 -- parent scopes that have been installed.
13862 while Present (S) loop
13863 End_Package_Scope (S);
13864 Set_Is_Immediately_Visible (S, False);
13865 S := Current_Scope;
13866 exit when S = Standard_Standard;
13867 end loop;
13868 end if;
13869 end Remove_Parent;
13871 -----------------
13872 -- Restore_Env --
13873 -----------------
13875 procedure Restore_Env is
13876 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13878 begin
13879 if No (Current_Instantiated_Parent.Act_Id) then
13880 -- Restore environment after subprogram inlining
13882 Restore_Private_Views (Empty);
13883 end if;
13885 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13886 Exchanged_Views := Saved.Exchanged_Views;
13887 Hidden_Entities := Saved.Hidden_Entities;
13888 Current_Sem_Unit := Saved.Current_Sem_Unit;
13889 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13890 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13892 Restore_Opt_Config_Switches (Saved.Switches);
13894 Instance_Envs.Decrement_Last;
13895 end Restore_Env;
13897 ---------------------------
13898 -- Restore_Private_Views --
13899 ---------------------------
13901 procedure Restore_Private_Views
13902 (Pack_Id : Entity_Id;
13903 Is_Package : Boolean := True)
13905 M : Elmt_Id;
13906 E : Entity_Id;
13907 Typ : Entity_Id;
13908 Dep_Elmt : Elmt_Id;
13909 Dep_Typ : Node_Id;
13911 procedure Restore_Nested_Formal (Formal : Entity_Id);
13912 -- Hide the generic formals of formal packages declared with box which
13913 -- were reachable in the current instantiation.
13915 ---------------------------
13916 -- Restore_Nested_Formal --
13917 ---------------------------
13919 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13920 Ent : Entity_Id;
13922 begin
13923 if Present (Renamed_Object (Formal))
13924 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13925 then
13926 return;
13928 elsif Present (Associated_Formal_Package (Formal)) then
13929 Ent := First_Entity (Formal);
13930 while Present (Ent) loop
13931 exit when Ekind (Ent) = E_Package
13932 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13934 Set_Is_Hidden (Ent);
13935 Set_Is_Potentially_Use_Visible (Ent, False);
13937 -- If package, then recurse
13939 if Ekind (Ent) = E_Package then
13940 Restore_Nested_Formal (Ent);
13941 end if;
13943 Next_Entity (Ent);
13944 end loop;
13945 end if;
13946 end Restore_Nested_Formal;
13948 -- Start of processing for Restore_Private_Views
13950 begin
13951 M := First_Elmt (Exchanged_Views);
13952 while Present (M) loop
13953 Typ := Node (M);
13955 -- Subtypes of types whose views have been exchanged, and that are
13956 -- defined within the instance, were not on the Private_Dependents
13957 -- list on entry to the instance, so they have to be exchanged
13958 -- explicitly now, in order to remain consistent with the view of the
13959 -- parent type.
13961 if Ekind_In (Typ, E_Private_Type,
13962 E_Limited_Private_Type,
13963 E_Record_Type_With_Private)
13964 then
13965 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13966 while Present (Dep_Elmt) loop
13967 Dep_Typ := Node (Dep_Elmt);
13969 if Scope (Dep_Typ) = Pack_Id
13970 and then Present (Full_View (Dep_Typ))
13971 then
13972 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13973 Exchange_Declarations (Dep_Typ);
13974 end if;
13976 Next_Elmt (Dep_Elmt);
13977 end loop;
13978 end if;
13980 Exchange_Declarations (Node (M));
13981 Next_Elmt (M);
13982 end loop;
13984 if No (Pack_Id) then
13985 return;
13986 end if;
13988 -- Make the generic formal parameters private, and make the formal types
13989 -- into subtypes of the actuals again.
13991 E := First_Entity (Pack_Id);
13992 while Present (E) loop
13993 Set_Is_Hidden (E, True);
13995 if Is_Type (E)
13996 and then Nkind (Parent (E)) = N_Subtype_Declaration
13997 then
13998 -- If the actual for E is itself a generic actual type from
13999 -- an enclosing instance, E is still a generic actual type
14000 -- outside of the current instance. This matter when resolving
14001 -- an overloaded call that may be ambiguous in the enclosing
14002 -- instance, when two of its actuals coincide.
14004 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14005 and then Is_Generic_Actual_Type
14006 (Entity (Subtype_Indication (Parent (E))))
14007 then
14008 null;
14009 else
14010 Set_Is_Generic_Actual_Type (E, False);
14011 end if;
14013 -- An unusual case of aliasing: the actual may also be directly
14014 -- visible in the generic, and be private there, while it is fully
14015 -- visible in the context of the instance. The internal subtype
14016 -- is private in the instance but has full visibility like its
14017 -- parent in the enclosing scope. This enforces the invariant that
14018 -- the privacy status of all private dependents of a type coincide
14019 -- with that of the parent type. This can only happen when a
14020 -- generic child unit is instantiated within a sibling.
14022 if Is_Private_Type (E)
14023 and then not Is_Private_Type (Etype (E))
14024 then
14025 Exchange_Declarations (E);
14026 end if;
14028 elsif Ekind (E) = E_Package then
14030 -- The end of the renaming list is the renaming of the generic
14031 -- package itself. If the instance is a subprogram, all entities
14032 -- in the corresponding package are renamings. If this entity is
14033 -- a formal package, make its own formals private as well. The
14034 -- actual in this case is itself the renaming of an instantiation.
14035 -- If the entity is not a package renaming, it is the entity
14036 -- created to validate formal package actuals: ignore it.
14038 -- If the actual is itself a formal package for the enclosing
14039 -- generic, or the actual for such a formal package, it remains
14040 -- visible on exit from the instance, and therefore nothing needs
14041 -- to be done either, except to keep it accessible.
14043 if Is_Package and then Renamed_Object (E) = Pack_Id then
14044 exit;
14046 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14047 null;
14049 elsif
14050 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14051 then
14052 Set_Is_Hidden (E, False);
14054 else
14055 declare
14056 Act_P : constant Entity_Id := Renamed_Object (E);
14057 Id : Entity_Id;
14059 begin
14060 Id := First_Entity (Act_P);
14061 while Present (Id)
14062 and then Id /= First_Private_Entity (Act_P)
14063 loop
14064 exit when Ekind (Id) = E_Package
14065 and then Renamed_Object (Id) = Act_P;
14067 Set_Is_Hidden (Id, True);
14068 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14070 if Ekind (Id) = E_Package then
14071 Restore_Nested_Formal (Id);
14072 end if;
14074 Next_Entity (Id);
14075 end loop;
14076 end;
14077 end if;
14078 end if;
14080 Next_Entity (E);
14081 end loop;
14082 end Restore_Private_Views;
14084 --------------
14085 -- Save_Env --
14086 --------------
14088 procedure Save_Env
14089 (Gen_Unit : Entity_Id;
14090 Act_Unit : Entity_Id)
14092 begin
14093 Init_Env;
14094 Set_Instance_Env (Gen_Unit, Act_Unit);
14095 end Save_Env;
14097 ----------------------------
14098 -- Save_Global_References --
14099 ----------------------------
14101 procedure Save_Global_References (Templ : Node_Id) is
14103 -- ??? it is horrible to use global variables in highly recursive code
14105 E : Entity_Id;
14106 -- The entity of the current associated node
14108 Gen_Scope : Entity_Id;
14109 -- The scope of the generic for which references are being saved
14111 N2 : Node_Id;
14112 -- The current associated node
14114 function Is_Global (E : Entity_Id) return Boolean;
14115 -- Check whether entity is defined outside of generic unit. Examine the
14116 -- scope of an entity, and the scope of the scope, etc, until we find
14117 -- either Standard, in which case the entity is global, or the generic
14118 -- unit itself, which indicates that the entity is local. If the entity
14119 -- is the generic unit itself, as in the case of a recursive call, or
14120 -- the enclosing generic unit, if different from the current scope, then
14121 -- it is local as well, because it will be replaced at the point of
14122 -- instantiation. On the other hand, if it is a reference to a child
14123 -- unit of a common ancestor, which appears in an instantiation, it is
14124 -- global because it is used to denote a specific compilation unit at
14125 -- the time the instantiations will be analyzed.
14127 procedure Qualify_Universal_Operands
14128 (Op : Node_Id;
14129 Func_Call : Node_Id);
14130 -- Op denotes a binary or unary operator in generic template Templ. Node
14131 -- Func_Call is the function call alternative of the operator within the
14132 -- the analyzed copy of the template. Change each operand which yields a
14133 -- universal type by wrapping it into a qualified expression
14135 -- Actual_Typ'(Operand)
14137 -- where Actual_Typ is the type of corresponding actual parameter of
14138 -- Operand in Func_Call.
14140 procedure Reset_Entity (N : Node_Id);
14141 -- Save semantic information on global entity so that it is not resolved
14142 -- again at instantiation time.
14144 procedure Save_Entity_Descendants (N : Node_Id);
14145 -- Apply Save_Global_References to the two syntactic descendants of
14146 -- non-terminal nodes that carry an Associated_Node and are processed
14147 -- through Reset_Entity. Once the global entity (if any) has been
14148 -- captured together with its type, only two syntactic descendants need
14149 -- to be traversed to complete the processing of the tree rooted at N.
14150 -- This applies to Selected_Components, Expanded_Names, and to Operator
14151 -- nodes. N can also be a character literal, identifier, or operator
14152 -- symbol node, but the call has no effect in these cases.
14154 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14155 -- Default actuals in nested instances must be handled specially
14156 -- because there is no link to them from the original tree. When an
14157 -- actual subprogram is given by a default, we add an explicit generic
14158 -- association for it in the instantiation node. When we save the
14159 -- global references on the name of the instance, we recover the list
14160 -- of generic associations, and add an explicit one to the original
14161 -- generic tree, through which a global actual can be preserved.
14162 -- Similarly, if a child unit is instantiated within a sibling, in the
14163 -- context of the parent, we must preserve the identifier of the parent
14164 -- so that it can be properly resolved in a subsequent instantiation.
14166 procedure Save_Global_Descendant (D : Union_Id);
14167 -- Apply Save_References recursively to the descendants of node D
14169 procedure Save_References (N : Node_Id);
14170 -- This is the recursive procedure that does the work, once the
14171 -- enclosing generic scope has been established.
14173 ---------------
14174 -- Is_Global --
14175 ---------------
14177 function Is_Global (E : Entity_Id) return Boolean is
14178 Se : Entity_Id;
14180 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14181 -- Determine whether the parent node of a reference to a child unit
14182 -- denotes an instantiation or a formal package, in which case the
14183 -- reference to the child unit is global, even if it appears within
14184 -- the current scope (e.g. when the instance appears within the body
14185 -- of an ancestor).
14187 ----------------------
14188 -- Is_Instance_Node --
14189 ----------------------
14191 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14192 begin
14193 return Nkind (Decl) in N_Generic_Instantiation
14194 or else
14195 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14196 end Is_Instance_Node;
14198 -- Start of processing for Is_Global
14200 begin
14201 if E = Gen_Scope then
14202 return False;
14204 elsif E = Standard_Standard then
14205 return True;
14207 elsif Is_Child_Unit (E)
14208 and then (Is_Instance_Node (Parent (N2))
14209 or else (Nkind (Parent (N2)) = N_Expanded_Name
14210 and then N2 = Selector_Name (Parent (N2))
14211 and then
14212 Is_Instance_Node (Parent (Parent (N2)))))
14213 then
14214 return True;
14216 else
14217 Se := Scope (E);
14218 while Se /= Gen_Scope loop
14219 if Se = Standard_Standard then
14220 return True;
14221 else
14222 Se := Scope (Se);
14223 end if;
14224 end loop;
14226 return False;
14227 end if;
14228 end Is_Global;
14230 --------------------------------
14231 -- Qualify_Universal_Operands --
14232 --------------------------------
14234 procedure Qualify_Universal_Operands
14235 (Op : Node_Id;
14236 Func_Call : Node_Id)
14238 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14239 -- Rewrite operand Opnd as a qualified expression of the form
14241 -- Actual_Typ'(Opnd)
14243 -- where Actual is the corresponding actual parameter of Opnd in
14244 -- function call Func_Call.
14246 function Qualify_Type
14247 (Loc : Source_Ptr;
14248 Typ : Entity_Id) return Node_Id;
14249 -- Qualify type Typ by creating a selected component of the form
14251 -- Scope_Of_Typ.Typ
14253 ---------------------
14254 -- Qualify_Operand --
14255 ---------------------
14257 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14258 Loc : constant Source_Ptr := Sloc (Opnd);
14259 Typ : constant Entity_Id := Etype (Actual);
14260 Mark : Node_Id;
14261 Qual : Node_Id;
14263 begin
14264 -- Qualify the operand when it is of a universal type. Note that
14265 -- the template is unanalyzed and it is not possible to directly
14266 -- query the type. This transformation is not done when the type
14267 -- of the actual is internally generated because the type will be
14268 -- regenerated in the instance.
14270 if Yields_Universal_Type (Opnd)
14271 and then Comes_From_Source (Typ)
14272 and then not Is_Hidden (Typ)
14273 then
14274 -- The type of the actual may be a global reference. Save this
14275 -- information by creating a reference to it.
14277 if Is_Global (Typ) then
14278 Mark := New_Occurrence_Of (Typ, Loc);
14280 -- Otherwise rely on resolution to find the proper type within
14281 -- the instance.
14283 else
14284 Mark := Qualify_Type (Loc, Typ);
14285 end if;
14287 Qual :=
14288 Make_Qualified_Expression (Loc,
14289 Subtype_Mark => Mark,
14290 Expression => Relocate_Node (Opnd));
14292 -- Mark the qualification to distinguish it from other source
14293 -- constructs and signal the instantiation mechanism that this
14294 -- node requires special processing. See Copy_Generic_Node for
14295 -- details.
14297 Set_Is_Qualified_Universal_Literal (Qual);
14299 Rewrite (Opnd, Qual);
14300 end if;
14301 end Qualify_Operand;
14303 ------------------
14304 -- Qualify_Type --
14305 ------------------
14307 function Qualify_Type
14308 (Loc : Source_Ptr;
14309 Typ : Entity_Id) return Node_Id
14311 Scop : constant Entity_Id := Scope (Typ);
14312 Result : Node_Id;
14314 begin
14315 Result := Make_Identifier (Loc, Chars (Typ));
14317 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14318 Result :=
14319 Make_Selected_Component (Loc,
14320 Prefix => Make_Identifier (Loc, Chars (Scop)),
14321 Selector_Name => Result);
14322 end if;
14324 return Result;
14325 end Qualify_Type;
14327 -- Local variables
14329 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14331 -- Start of processing for Qualify_Universal_Operands
14333 begin
14334 if Nkind (Op) in N_Binary_Op then
14335 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14336 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14338 elsif Nkind (Op) in N_Unary_Op then
14339 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14340 end if;
14341 end Qualify_Universal_Operands;
14343 ------------------
14344 -- Reset_Entity --
14345 ------------------
14347 procedure Reset_Entity (N : Node_Id) is
14348 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14349 -- If the type of N2 is global to the generic unit, save the type in
14350 -- the generic node. Just as we perform name capture for explicit
14351 -- references within the generic, we must capture the global types
14352 -- of local entities because they may participate in resolution in
14353 -- the instance.
14355 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14356 -- Find the ultimate ancestor of the current unit. If it is not a
14357 -- generic unit, then the name of the current unit in the prefix of
14358 -- an expanded name must be replaced with its generic homonym to
14359 -- ensure that it will be properly resolved in an instance.
14361 ---------------------
14362 -- Set_Global_Type --
14363 ---------------------
14365 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14366 Typ : constant Entity_Id := Etype (N2);
14368 begin
14369 Set_Etype (N, Typ);
14371 -- If the entity of N is not the associated node, this is a
14372 -- nested generic and it has an associated node as well, whose
14373 -- type is already the full view (see below). Indicate that the
14374 -- original node has a private view.
14376 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14377 Set_Has_Private_View (N);
14378 end if;
14380 -- If not a private type, nothing else to do
14382 if not Is_Private_Type (Typ) then
14383 if Is_Array_Type (Typ)
14384 and then Is_Private_Type (Component_Type (Typ))
14385 then
14386 Set_Has_Private_View (N);
14387 end if;
14389 -- If it is a derivation of a private type in a context where no
14390 -- full view is needed, nothing to do either.
14392 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14393 null;
14395 -- Otherwise mark the type for flipping and use the full view when
14396 -- available.
14398 else
14399 Set_Has_Private_View (N);
14401 if Present (Full_View (Typ)) then
14402 Set_Etype (N2, Full_View (Typ));
14403 end if;
14404 end if;
14406 if Is_Floating_Point_Type (Typ)
14407 and then Has_Dimension_System (Typ)
14408 then
14409 Copy_Dimensions (N2, N);
14410 end if;
14411 end Set_Global_Type;
14413 ------------------
14414 -- Top_Ancestor --
14415 ------------------
14417 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14418 Par : Entity_Id;
14420 begin
14421 Par := E;
14422 while Is_Child_Unit (Par) loop
14423 Par := Scope (Par);
14424 end loop;
14426 return Par;
14427 end Top_Ancestor;
14429 -- Start of processing for Reset_Entity
14431 begin
14432 N2 := Get_Associated_Node (N);
14433 E := Entity (N2);
14435 if Present (E) then
14437 -- If the node is an entry call to an entry in an enclosing task,
14438 -- it is rewritten as a selected component. No global entity to
14439 -- preserve in this case, since the expansion will be redone in
14440 -- the instance.
14442 if not Nkind_In (E, N_Defining_Character_Literal,
14443 N_Defining_Identifier,
14444 N_Defining_Operator_Symbol)
14445 then
14446 Set_Associated_Node (N, Empty);
14447 Set_Etype (N, Empty);
14448 return;
14449 end if;
14451 -- If the entity is an itype created as a subtype of an access
14452 -- type with a null exclusion restore source entity for proper
14453 -- visibility. The itype will be created anew in the instance.
14455 if Is_Itype (E)
14456 and then Ekind (E) = E_Access_Subtype
14457 and then Is_Entity_Name (N)
14458 and then Chars (Etype (E)) = Chars (N)
14459 then
14460 E := Etype (E);
14461 Set_Entity (N2, E);
14462 Set_Etype (N2, E);
14463 end if;
14465 if Is_Global (E) then
14467 -- If the entity is a package renaming that is the prefix of
14468 -- an expanded name, it has been rewritten as the renamed
14469 -- package, which is necessary semantically but complicates
14470 -- ASIS tree traversal, so we recover the original entity to
14471 -- expose the renaming. Take into account that the context may
14472 -- be a nested generic, that the original node may itself have
14473 -- an associated node that had better be an entity, and that
14474 -- the current node is still a selected component.
14476 if Ekind (E) = E_Package
14477 and then Nkind (N) = N_Selected_Component
14478 and then Nkind (Parent (N)) = N_Expanded_Name
14479 and then Present (Original_Node (N2))
14480 and then Is_Entity_Name (Original_Node (N2))
14481 and then Present (Entity (Original_Node (N2)))
14482 then
14483 if Is_Global (Entity (Original_Node (N2))) then
14484 N2 := Original_Node (N2);
14485 Set_Associated_Node (N, N2);
14486 Set_Global_Type (N, N2);
14488 -- Renaming is local, and will be resolved in instance
14490 else
14491 Set_Associated_Node (N, Empty);
14492 Set_Etype (N, Empty);
14493 end if;
14495 else
14496 Set_Global_Type (N, N2);
14497 end if;
14499 elsif Nkind (N) = N_Op_Concat
14500 and then Is_Generic_Type (Etype (N2))
14501 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14502 or else
14503 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14504 and then Is_Intrinsic_Subprogram (E)
14505 then
14506 null;
14508 -- Entity is local. Mark generic node as unresolved. Note that now
14509 -- it does not have an entity.
14511 else
14512 Set_Associated_Node (N, Empty);
14513 Set_Etype (N, Empty);
14514 end if;
14516 if Nkind (Parent (N)) in N_Generic_Instantiation
14517 and then N = Name (Parent (N))
14518 then
14519 Save_Global_Defaults (Parent (N), Parent (N2));
14520 end if;
14522 elsif Nkind (Parent (N)) = N_Selected_Component
14523 and then Nkind (Parent (N2)) = N_Expanded_Name
14524 then
14525 if Is_Global (Entity (Parent (N2))) then
14526 Change_Selected_Component_To_Expanded_Name (Parent (N));
14527 Set_Associated_Node (Parent (N), Parent (N2));
14528 Set_Global_Type (Parent (N), Parent (N2));
14529 Save_Entity_Descendants (N);
14531 -- If this is a reference to the current generic entity, replace
14532 -- by the name of the generic homonym of the current package. This
14533 -- is because in an instantiation Par.P.Q will not resolve to the
14534 -- name of the instance, whose enclosing scope is not necessarily
14535 -- Par. We use the generic homonym rather that the name of the
14536 -- generic itself because it may be hidden by a local declaration.
14538 elsif In_Open_Scopes (Entity (Parent (N2)))
14539 and then not
14540 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14541 then
14542 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14543 Rewrite (Parent (N),
14544 Make_Identifier (Sloc (N),
14545 Chars =>
14546 Chars (Generic_Homonym (Entity (Parent (N2))))));
14547 else
14548 Rewrite (Parent (N),
14549 Make_Identifier (Sloc (N),
14550 Chars => Chars (Selector_Name (Parent (N2)))));
14551 end if;
14552 end if;
14554 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14555 and then Parent (N) = Name (Parent (Parent (N)))
14556 then
14557 Save_Global_Defaults
14558 (Parent (Parent (N)), Parent (Parent (N2)));
14559 end if;
14561 -- A selected component may denote a static constant that has been
14562 -- folded. If the static constant is global to the generic, capture
14563 -- its value. Otherwise the folding will happen in any instantiation.
14565 elsif Nkind (Parent (N)) = N_Selected_Component
14566 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14567 then
14568 if Present (Entity (Original_Node (Parent (N2))))
14569 and then Is_Global (Entity (Original_Node (Parent (N2))))
14570 then
14571 Rewrite (Parent (N), New_Copy (Parent (N2)));
14572 Set_Analyzed (Parent (N), False);
14573 end if;
14575 -- A selected component may be transformed into a parameterless
14576 -- function call. If the called entity is global, rewrite the node
14577 -- appropriately, i.e. as an extended name for the global entity.
14579 elsif Nkind (Parent (N)) = N_Selected_Component
14580 and then Nkind (Parent (N2)) = N_Function_Call
14581 and then N = Selector_Name (Parent (N))
14582 then
14583 if No (Parameter_Associations (Parent (N2))) then
14584 if Is_Global (Entity (Name (Parent (N2)))) then
14585 Change_Selected_Component_To_Expanded_Name (Parent (N));
14586 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14587 Set_Global_Type (Parent (N), Name (Parent (N2)));
14588 Save_Entity_Descendants (N);
14590 else
14591 Set_Is_Prefixed_Call (Parent (N));
14592 Set_Associated_Node (N, Empty);
14593 Set_Etype (N, Empty);
14594 end if;
14596 -- In Ada 2005, X.F may be a call to a primitive operation,
14597 -- rewritten as F (X). This rewriting will be done again in an
14598 -- instance, so keep the original node. Global entities will be
14599 -- captured as for other constructs. Indicate that this must
14600 -- resolve as a call, to prevent accidental overloading in the
14601 -- instance, if both a component and a primitive operation appear
14602 -- as candidates.
14604 else
14605 Set_Is_Prefixed_Call (Parent (N));
14606 end if;
14608 -- Entity is local. Reset in generic unit, so that node is resolved
14609 -- anew at the point of instantiation.
14611 else
14612 Set_Associated_Node (N, Empty);
14613 Set_Etype (N, Empty);
14614 end if;
14615 end Reset_Entity;
14617 -----------------------------
14618 -- Save_Entity_Descendants --
14619 -----------------------------
14621 procedure Save_Entity_Descendants (N : Node_Id) is
14622 begin
14623 case Nkind (N) is
14624 when N_Binary_Op =>
14625 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14626 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14628 when N_Unary_Op =>
14629 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14631 when N_Expanded_Name
14632 | N_Selected_Component
14634 Save_Global_Descendant (Union_Id (Prefix (N)));
14635 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14637 when N_Character_Literal
14638 | N_Identifier
14639 | N_Operator_Symbol
14641 null;
14643 when others =>
14644 raise Program_Error;
14645 end case;
14646 end Save_Entity_Descendants;
14648 --------------------------
14649 -- Save_Global_Defaults --
14650 --------------------------
14652 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14653 Loc : constant Source_Ptr := Sloc (N1);
14654 Assoc2 : constant List_Id := Generic_Associations (N2);
14655 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14656 Assoc1 : List_Id;
14657 Act1 : Node_Id;
14658 Act2 : Node_Id;
14659 Def : Node_Id;
14660 Ndec : Node_Id;
14661 Subp : Entity_Id;
14662 Actual : Entity_Id;
14664 begin
14665 Assoc1 := Generic_Associations (N1);
14667 if Present (Assoc1) then
14668 Act1 := First (Assoc1);
14669 else
14670 Act1 := Empty;
14671 Set_Generic_Associations (N1, New_List);
14672 Assoc1 := Generic_Associations (N1);
14673 end if;
14675 if Present (Assoc2) then
14676 Act2 := First (Assoc2);
14677 else
14678 return;
14679 end if;
14681 while Present (Act1) and then Present (Act2) loop
14682 Next (Act1);
14683 Next (Act2);
14684 end loop;
14686 -- Find the associations added for default subprograms
14688 if Present (Act2) then
14689 while Nkind (Act2) /= N_Generic_Association
14690 or else No (Entity (Selector_Name (Act2)))
14691 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14692 loop
14693 Next (Act2);
14694 end loop;
14696 -- Add a similar association if the default is global. The
14697 -- renaming declaration for the actual has been analyzed, and
14698 -- its alias is the program it renames. Link the actual in the
14699 -- original generic tree with the node in the analyzed tree.
14701 while Present (Act2) loop
14702 Subp := Entity (Selector_Name (Act2));
14703 Def := Explicit_Generic_Actual_Parameter (Act2);
14705 -- Following test is defence against rubbish errors
14707 if No (Alias (Subp)) then
14708 return;
14709 end if;
14711 -- Retrieve the resolved actual from the renaming declaration
14712 -- created for the instantiated formal.
14714 Actual := Entity (Name (Parent (Parent (Subp))));
14715 Set_Entity (Def, Actual);
14716 Set_Etype (Def, Etype (Actual));
14718 if Is_Global (Actual) then
14719 Ndec :=
14720 Make_Generic_Association (Loc,
14721 Selector_Name =>
14722 New_Occurrence_Of (Subp, Loc),
14723 Explicit_Generic_Actual_Parameter =>
14724 New_Occurrence_Of (Actual, Loc));
14726 Set_Associated_Node
14727 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14729 Append (Ndec, Assoc1);
14731 -- If there are other defaults, add a dummy association in case
14732 -- there are other defaulted formals with the same name.
14734 elsif Present (Next (Act2)) then
14735 Ndec :=
14736 Make_Generic_Association (Loc,
14737 Selector_Name =>
14738 New_Occurrence_Of (Subp, Loc),
14739 Explicit_Generic_Actual_Parameter => Empty);
14741 Append (Ndec, Assoc1);
14742 end if;
14744 Next (Act2);
14745 end loop;
14746 end if;
14748 if Nkind (Name (N1)) = N_Identifier
14749 and then Is_Child_Unit (Gen_Id)
14750 and then Is_Global (Gen_Id)
14751 and then Is_Generic_Unit (Scope (Gen_Id))
14752 and then In_Open_Scopes (Scope (Gen_Id))
14753 then
14754 -- This is an instantiation of a child unit within a sibling, so
14755 -- that the generic parent is in scope. An eventual instance must
14756 -- occur within the scope of an instance of the parent. Make name
14757 -- in instance into an expanded name, to preserve the identifier
14758 -- of the parent, so it can be resolved subsequently.
14760 Rewrite (Name (N2),
14761 Make_Expanded_Name (Loc,
14762 Chars => Chars (Gen_Id),
14763 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14764 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14765 Set_Entity (Name (N2), Gen_Id);
14767 Rewrite (Name (N1),
14768 Make_Expanded_Name (Loc,
14769 Chars => Chars (Gen_Id),
14770 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14771 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14773 Set_Associated_Node (Name (N1), Name (N2));
14774 Set_Associated_Node (Prefix (Name (N1)), Empty);
14775 Set_Associated_Node
14776 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14777 Set_Etype (Name (N1), Etype (Gen_Id));
14778 end if;
14779 end Save_Global_Defaults;
14781 ----------------------------
14782 -- Save_Global_Descendant --
14783 ----------------------------
14785 procedure Save_Global_Descendant (D : Union_Id) is
14786 N1 : Node_Id;
14788 begin
14789 if D in Node_Range then
14790 if D = Union_Id (Empty) then
14791 null;
14793 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14794 Save_References (Node_Id (D));
14795 end if;
14797 elsif D in List_Range then
14798 pragma Assert (D /= Union_Id (No_List));
14799 -- Because No_List = Empty, which is in Node_Range above
14801 if Is_Empty_List (List_Id (D)) then
14802 null;
14804 else
14805 N1 := First (List_Id (D));
14806 while Present (N1) loop
14807 Save_References (N1);
14808 Next (N1);
14809 end loop;
14810 end if;
14812 -- Element list or other non-node field, nothing to do
14814 else
14815 null;
14816 end if;
14817 end Save_Global_Descendant;
14819 ---------------------
14820 -- Save_References --
14821 ---------------------
14823 -- This is the recursive procedure that does the work once the enclosing
14824 -- generic scope has been established. We have to treat specially a
14825 -- number of node rewritings that are required by semantic processing
14826 -- and which change the kind of nodes in the generic copy: typically
14827 -- constant-folding, replacing an operator node by a string literal, or
14828 -- a selected component by an expanded name. In each of those cases, the
14829 -- transformation is propagated to the generic unit.
14831 procedure Save_References (N : Node_Id) is
14832 Loc : constant Source_Ptr := Sloc (N);
14834 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14835 -- Determine whether arbitrary node Nod requires delayed capture of
14836 -- global references within its aspect specifications.
14838 procedure Save_References_In_Aggregate (N : Node_Id);
14839 -- Save all global references in [extension] aggregate node N
14841 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14842 -- Save all global references in a character literal or operator
14843 -- symbol denoted by N.
14845 procedure Save_References_In_Descendants (N : Node_Id);
14846 -- Save all global references in all descendants of node N
14848 procedure Save_References_In_Identifier (N : Node_Id);
14849 -- Save all global references in identifier node N
14851 procedure Save_References_In_Operator (N : Node_Id);
14852 -- Save all global references in operator node N
14854 procedure Save_References_In_Pragma (Prag : Node_Id);
14855 -- Save all global references found within the expression of pragma
14856 -- Prag.
14858 ---------------------------
14859 -- Requires_Delayed_Save --
14860 ---------------------------
14862 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14863 begin
14864 -- Generic packages and subprograms require delayed capture of
14865 -- global references within their aspects due to the timing of
14866 -- annotation analysis.
14868 if Nkind_In (Nod, N_Generic_Package_Declaration,
14869 N_Generic_Subprogram_Declaration,
14870 N_Package_Body,
14871 N_Package_Body_Stub,
14872 N_Subprogram_Body,
14873 N_Subprogram_Body_Stub)
14874 then
14875 -- Since the capture of global references is done on the
14876 -- unanalyzed generic template, there is no information around
14877 -- to infer the context. Use the Associated_Entity linkages to
14878 -- peek into the analyzed generic copy and determine what the
14879 -- template corresponds to.
14881 if Nod = Templ then
14882 return
14883 Is_Generic_Declaration_Or_Body
14884 (Unit_Declaration_Node
14885 (Associated_Entity (Defining_Entity (Nod))));
14887 -- Otherwise the generic unit being processed is not the top
14888 -- level template. It is safe to capture of global references
14889 -- within the generic unit because at this point the top level
14890 -- copy is fully analyzed.
14892 else
14893 return False;
14894 end if;
14896 -- Otherwise capture the global references without interference
14898 else
14899 return False;
14900 end if;
14901 end Requires_Delayed_Save;
14903 ----------------------------------
14904 -- Save_References_In_Aggregate --
14905 ----------------------------------
14907 procedure Save_References_In_Aggregate (N : Node_Id) is
14908 Nam : Node_Id;
14909 Qual : Node_Id := Empty;
14910 Typ : Entity_Id := Empty;
14912 use Atree.Unchecked_Access;
14913 -- This code section is part of implementing an untyped tree
14914 -- traversal, so it needs direct access to node fields.
14916 begin
14917 N2 := Get_Associated_Node (N);
14919 if Present (N2) then
14920 Typ := Etype (N2);
14922 -- In an instance within a generic, use the name of the actual
14923 -- and not the original generic parameter. If the actual is
14924 -- global in the current generic it must be preserved for its
14925 -- instantiation.
14927 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14928 and then Present (Generic_Parent_Type (Parent (Typ)))
14929 then
14930 Typ := Base_Type (Typ);
14931 Set_Etype (N2, Typ);
14932 end if;
14933 end if;
14935 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14936 Set_Associated_Node (N, Empty);
14938 -- If the aggregate is an actual in a call, it has been
14939 -- resolved in the current context, to some local type. The
14940 -- enclosing call may have been disambiguated by the aggregate,
14941 -- and this disambiguation might fail at instantiation time
14942 -- because the type to which the aggregate did resolve is not
14943 -- preserved. In order to preserve some of this information,
14944 -- wrap the aggregate in a qualified expression, using the id
14945 -- of its type. For further disambiguation we qualify the type
14946 -- name with its scope (if visible) because both id's will have
14947 -- corresponding entities in an instance. This resolves most of
14948 -- the problems with missing type information on aggregates in
14949 -- instances.
14951 if Present (N2)
14952 and then Nkind (N2) = Nkind (N)
14953 and then Nkind (Parent (N2)) in N_Subprogram_Call
14954 and then Present (Typ)
14955 and then Comes_From_Source (Typ)
14956 then
14957 Nam := Make_Identifier (Loc, Chars (Typ));
14959 if Is_Immediately_Visible (Scope (Typ)) then
14960 Nam :=
14961 Make_Selected_Component (Loc,
14962 Prefix =>
14963 Make_Identifier (Loc, Chars (Scope (Typ))),
14964 Selector_Name => Nam);
14965 end if;
14967 Qual :=
14968 Make_Qualified_Expression (Loc,
14969 Subtype_Mark => Nam,
14970 Expression => Relocate_Node (N));
14971 end if;
14972 end if;
14974 Save_Global_Descendant (Field1 (N));
14975 Save_Global_Descendant (Field2 (N));
14976 Save_Global_Descendant (Field3 (N));
14977 Save_Global_Descendant (Field5 (N));
14979 if Present (Qual) then
14980 Rewrite (N, Qual);
14981 end if;
14982 end Save_References_In_Aggregate;
14984 ----------------------------------------------
14985 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14986 ----------------------------------------------
14988 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14989 begin
14990 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14991 Reset_Entity (N);
14993 elsif Nkind (N) = N_Operator_Symbol
14994 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14995 then
14996 Change_Operator_Symbol_To_String_Literal (N);
14997 end if;
14998 end Save_References_In_Char_Lit_Or_Op_Symbol;
15000 ------------------------------------
15001 -- Save_References_In_Descendants --
15002 ------------------------------------
15004 procedure Save_References_In_Descendants (N : Node_Id) is
15005 use Atree.Unchecked_Access;
15006 -- This code section is part of implementing an untyped tree
15007 -- traversal, so it needs direct access to node fields.
15009 begin
15010 Save_Global_Descendant (Field1 (N));
15011 Save_Global_Descendant (Field2 (N));
15012 Save_Global_Descendant (Field3 (N));
15013 Save_Global_Descendant (Field4 (N));
15014 Save_Global_Descendant (Field5 (N));
15015 end Save_References_In_Descendants;
15017 -----------------------------------
15018 -- Save_References_In_Identifier --
15019 -----------------------------------
15021 procedure Save_References_In_Identifier (N : Node_Id) is
15022 begin
15023 -- The node did not undergo a transformation
15025 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15026 declare
15027 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15028 Orig_N2_Parent : constant Node_Id :=
15029 Original_Node (Parent (Aux_N2));
15030 begin
15031 -- The parent of this identifier is a selected component
15032 -- which denotes a named number that was constant folded.
15033 -- Preserve the original name for ASIS and link the parent
15034 -- with its expanded name. The constant folding will be
15035 -- repeated in the instance.
15037 if Nkind (Parent (N)) = N_Selected_Component
15038 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15039 N_Real_Literal)
15040 and then Is_Entity_Name (Orig_N2_Parent)
15041 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15042 and then Is_Global (Entity (Orig_N2_Parent))
15043 then
15044 N2 := Aux_N2;
15045 Set_Associated_Node
15046 (Parent (N), Original_Node (Parent (N2)));
15048 -- Common case
15050 else
15051 -- If this is a discriminant reference, always save it.
15052 -- It is used in the instance to find the corresponding
15053 -- discriminant positionally rather than by name.
15055 Set_Original_Discriminant
15056 (N, Original_Discriminant (Get_Associated_Node (N)));
15057 end if;
15059 Reset_Entity (N);
15060 end;
15062 -- The analysis of the generic copy transformed the identifier
15063 -- into another construct. Propagate the changes to the template.
15065 else
15066 N2 := Get_Associated_Node (N);
15068 -- The identifier denotes a call to a parameterless function.
15069 -- Mark the node as resolved when the function is external.
15071 if Nkind (N2) = N_Function_Call then
15072 E := Entity (Name (N2));
15074 if Present (E) and then Is_Global (E) then
15075 Set_Etype (N, Etype (N2));
15076 else
15077 Set_Associated_Node (N, Empty);
15078 Set_Etype (N, Empty);
15079 end if;
15081 -- The identifier denotes a named number that was constant
15082 -- folded. Preserve the original name for ASIS and undo the
15083 -- constant folding which will be repeated in the instance.
15085 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15086 and then Is_Entity_Name (Original_Node (N2))
15087 then
15088 Set_Associated_Node (N, Original_Node (N2));
15089 Reset_Entity (N);
15091 -- The identifier resolved to a string literal. Propagate this
15092 -- information to the generic template.
15094 elsif Nkind (N2) = N_String_Literal then
15095 Rewrite (N, New_Copy (N2));
15097 -- The identifier is rewritten as a dereference if it is the
15098 -- prefix of an implicit dereference. Preserve the original
15099 -- tree as the analysis of the instance will expand the node
15100 -- again, but preserve the resolved entity if it is global.
15102 elsif Nkind (N2) = N_Explicit_Dereference then
15103 if Is_Entity_Name (Prefix (N2))
15104 and then Present (Entity (Prefix (N2)))
15105 and then Is_Global (Entity (Prefix (N2)))
15106 then
15107 Set_Associated_Node (N, Prefix (N2));
15109 elsif Nkind (Prefix (N2)) = N_Function_Call
15110 and then Present (Entity (Name (Prefix (N2))))
15111 and then Is_Global (Entity (Name (Prefix (N2))))
15112 then
15113 Rewrite (N,
15114 Make_Explicit_Dereference (Loc,
15115 Prefix =>
15116 Make_Function_Call (Loc,
15117 Name =>
15118 New_Occurrence_Of
15119 (Entity (Name (Prefix (N2))), Loc))));
15121 else
15122 Set_Associated_Node (N, Empty);
15123 Set_Etype (N, Empty);
15124 end if;
15126 -- The subtype mark of a nominally unconstrained object is
15127 -- rewritten as a subtype indication using the bounds of the
15128 -- expression. Recover the original subtype mark.
15130 elsif Nkind (N2) = N_Subtype_Indication
15131 and then Is_Entity_Name (Original_Node (N2))
15132 then
15133 Set_Associated_Node (N, Original_Node (N2));
15134 Reset_Entity (N);
15135 end if;
15136 end if;
15137 end Save_References_In_Identifier;
15139 ---------------------------------
15140 -- Save_References_In_Operator --
15141 ---------------------------------
15143 procedure Save_References_In_Operator (N : Node_Id) is
15144 begin
15145 -- The node did not undergo a transformation
15147 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15148 if Nkind (N) = N_Op_Concat then
15149 Set_Is_Component_Left_Opnd (N,
15150 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15152 Set_Is_Component_Right_Opnd (N,
15153 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15154 end if;
15156 Reset_Entity (N);
15158 -- The analysis of the generic copy transformed the operator into
15159 -- some other construct. Propagate the changes to the template if
15160 -- applicable.
15162 else
15163 N2 := Get_Associated_Node (N);
15165 -- The operator resoved to a function call
15167 if Nkind (N2) = N_Function_Call then
15169 -- Add explicit qualifications in the generic template for
15170 -- all operands of universal type. This aids resolution by
15171 -- preserving the actual type of a literal or an attribute
15172 -- that yields a universal result.
15174 Qualify_Universal_Operands (N, N2);
15176 E := Entity (Name (N2));
15178 if Present (E) and then Is_Global (E) then
15179 Set_Etype (N, Etype (N2));
15180 else
15181 Set_Associated_Node (N, Empty);
15182 Set_Etype (N, Empty);
15183 end if;
15185 -- The operator was folded into a literal
15187 elsif Nkind_In (N2, N_Integer_Literal,
15188 N_Real_Literal,
15189 N_String_Literal)
15190 then
15191 if Present (Original_Node (N2))
15192 and then Nkind (Original_Node (N2)) = Nkind (N)
15193 then
15194 -- Operation was constant-folded. Whenever possible,
15195 -- recover semantic information from unfolded node,
15196 -- for ASIS use.
15198 Set_Associated_Node (N, Original_Node (N2));
15200 if Nkind (N) = N_Op_Concat then
15201 Set_Is_Component_Left_Opnd (N,
15202 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15203 Set_Is_Component_Right_Opnd (N,
15204 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15205 end if;
15207 Reset_Entity (N);
15209 -- Propagate the constant folding back to the template
15211 else
15212 Rewrite (N, New_Copy (N2));
15213 Set_Analyzed (N, False);
15214 end if;
15216 -- The operator was folded into an enumeration literal. Retain
15217 -- the entity to avoid spurious ambiguities if it is overloaded
15218 -- at the point of instantiation or inlining.
15220 elsif Nkind (N2) = N_Identifier
15221 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15222 then
15223 Rewrite (N, New_Copy (N2));
15224 Set_Analyzed (N, False);
15225 end if;
15226 end if;
15228 -- Complete the operands check if node has not been constant
15229 -- folded.
15231 if Nkind (N) in N_Op then
15232 Save_Entity_Descendants (N);
15233 end if;
15234 end Save_References_In_Operator;
15236 -------------------------------
15237 -- Save_References_In_Pragma --
15238 -------------------------------
15240 procedure Save_References_In_Pragma (Prag : Node_Id) is
15241 Context : Node_Id;
15242 Do_Save : Boolean := True;
15244 use Atree.Unchecked_Access;
15245 -- This code section is part of implementing an untyped tree
15246 -- traversal, so it needs direct access to node fields.
15248 begin
15249 -- Do not save global references in pragmas generated from aspects
15250 -- because the pragmas will be regenerated at instantiation time.
15252 if From_Aspect_Specification (Prag) then
15253 Do_Save := False;
15255 -- The capture of global references within contract-related source
15256 -- pragmas associated with generic packages, subprograms or their
15257 -- respective bodies must be delayed due to timing of annotation
15258 -- analysis. Global references are still captured in routine
15259 -- Save_Global_References_In_Contract.
15261 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15262 if Is_Package_Contract_Annotation (Prag) then
15263 Context := Find_Related_Package_Or_Body (Prag);
15264 else
15265 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15266 Context := Find_Related_Declaration_Or_Body (Prag);
15267 end if;
15269 -- The use of Original_Node accounts for the case when the
15270 -- related context is generic template.
15272 if Requires_Delayed_Save (Original_Node (Context)) then
15273 Do_Save := False;
15274 end if;
15275 end if;
15277 -- For all other cases, save all global references within the
15278 -- descendants, but skip the following semantic fields:
15280 -- Field1 - Next_Pragma
15281 -- Field3 - Corresponding_Aspect
15282 -- Field5 - Next_Rep_Item
15284 if Do_Save then
15285 Save_Global_Descendant (Field2 (Prag));
15286 Save_Global_Descendant (Field4 (Prag));
15287 end if;
15288 end Save_References_In_Pragma;
15290 -- Start of processing for Save_References
15292 begin
15293 if N = Empty then
15294 null;
15296 -- Aggregates
15298 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15299 Save_References_In_Aggregate (N);
15301 -- Character literals, operator symbols
15303 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15304 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15306 -- Defining identifiers
15308 elsif Nkind (N) in N_Entity then
15309 null;
15311 -- Identifiers
15313 elsif Nkind (N) = N_Identifier then
15314 Save_References_In_Identifier (N);
15316 -- Operators
15318 elsif Nkind (N) in N_Op then
15319 Save_References_In_Operator (N);
15321 -- Pragmas
15323 elsif Nkind (N) = N_Pragma then
15324 Save_References_In_Pragma (N);
15326 else
15327 Save_References_In_Descendants (N);
15328 end if;
15330 -- Save all global references found within the aspect specifications
15331 -- of the related node.
15333 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15335 -- The capture of global references within aspects associated with
15336 -- generic packages, subprograms or their bodies must be delayed
15337 -- due to timing of annotation analysis. Global references are
15338 -- still captured in routine Save_Global_References_In_Contract.
15340 if Requires_Delayed_Save (N) then
15341 null;
15343 -- Otherwise save all global references within the aspects
15345 else
15346 Save_Global_References_In_Aspects (N);
15347 end if;
15348 end if;
15349 end Save_References;
15351 -- Start of processing for Save_Global_References
15353 begin
15354 Gen_Scope := Current_Scope;
15356 -- If the generic unit is a child unit, references to entities in the
15357 -- parent are treated as local, because they will be resolved anew in
15358 -- the context of the instance of the parent.
15360 while Is_Child_Unit (Gen_Scope)
15361 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15362 loop
15363 Gen_Scope := Scope (Gen_Scope);
15364 end loop;
15366 Save_References (Templ);
15367 end Save_Global_References;
15369 ---------------------------------------
15370 -- Save_Global_References_In_Aspects --
15371 ---------------------------------------
15373 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15374 Asp : Node_Id;
15375 Expr : Node_Id;
15377 begin
15378 Asp := First (Aspect_Specifications (N));
15379 while Present (Asp) loop
15380 Expr := Expression (Asp);
15382 if Present (Expr) then
15383 Save_Global_References (Expr);
15384 end if;
15386 Next (Asp);
15387 end loop;
15388 end Save_Global_References_In_Aspects;
15390 ------------------------------------------
15391 -- Set_Copied_Sloc_For_Inherited_Pragma --
15392 ------------------------------------------
15394 procedure Set_Copied_Sloc_For_Inherited_Pragma
15395 (N : Node_Id;
15396 E : Entity_Id)
15398 begin
15399 Create_Instantiation_Source (N, E,
15400 Inlined_Body => False,
15401 Inherited_Pragma => True,
15402 Factor => S_Adjustment);
15403 end Set_Copied_Sloc_For_Inherited_Pragma;
15405 --------------------------------------
15406 -- Set_Copied_Sloc_For_Inlined_Body --
15407 --------------------------------------
15409 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15410 begin
15411 Create_Instantiation_Source (N, E,
15412 Inlined_Body => True,
15413 Inherited_Pragma => False,
15414 Factor => S_Adjustment);
15415 end Set_Copied_Sloc_For_Inlined_Body;
15417 ---------------------
15418 -- Set_Instance_Of --
15419 ---------------------
15421 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15422 begin
15423 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15424 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15425 Generic_Renamings.Increment_Last;
15426 end Set_Instance_Of;
15428 --------------------
15429 -- Set_Next_Assoc --
15430 --------------------
15432 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15433 begin
15434 Generic_Renamings.Table (E).Next_In_HTable := Next;
15435 end Set_Next_Assoc;
15437 -------------------
15438 -- Start_Generic --
15439 -------------------
15441 procedure Start_Generic is
15442 begin
15443 -- ??? More things could be factored out in this routine.
15444 -- Should probably be done at a later stage.
15446 Generic_Flags.Append (Inside_A_Generic);
15447 Inside_A_Generic := True;
15449 Expander_Mode_Save_And_Set (False);
15450 end Start_Generic;
15452 ----------------------
15453 -- Set_Instance_Env --
15454 ----------------------
15456 -- WARNING: This routine manages SPARK regions
15458 procedure Set_Instance_Env
15459 (Gen_Unit : Entity_Id;
15460 Act_Unit : Entity_Id)
15462 Saved_AE : constant Boolean := Assertions_Enabled;
15463 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15464 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15465 -- Save the SPARK mode-related data because utilizing the configuration
15466 -- values of pragmas and switches will eliminate any previously set
15467 -- SPARK_Mode.
15469 begin
15470 -- Regardless of the current mode, predefined units are analyzed in the
15471 -- most current Ada mode, and earlier version Ada checks do not apply
15472 -- to predefined units. Nothing needs to be done for non-internal units.
15473 -- These are always analyzed in the current mode.
15475 if In_Internal_Unit (Gen_Unit) then
15476 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15478 -- In Ada2012 we may want to enable assertions in an instance of a
15479 -- predefined unit, in which case we need to preserve the current
15480 -- setting for the Assertions_Enabled flag. This will become more
15481 -- critical when pre/postconditions are added to predefined units,
15482 -- as is already the case for some numeric libraries.
15484 if Ada_Version >= Ada_2012 then
15485 Assertions_Enabled := Saved_AE;
15486 end if;
15488 -- Reinstall the SPARK_Mode which was in effect at the point of
15489 -- instantiation.
15491 Install_SPARK_Mode (Saved_SM, Saved_SMP);
15492 end if;
15494 Current_Instantiated_Parent :=
15495 (Gen_Id => Gen_Unit,
15496 Act_Id => Act_Unit,
15497 Next_In_HTable => Assoc_Null);
15498 end Set_Instance_Env;
15500 -----------------
15501 -- Switch_View --
15502 -----------------
15504 procedure Switch_View (T : Entity_Id) is
15505 BT : constant Entity_Id := Base_Type (T);
15506 Priv_Elmt : Elmt_Id := No_Elmt;
15507 Priv_Sub : Entity_Id;
15509 begin
15510 -- T may be private but its base type may have been exchanged through
15511 -- some other occurrence, in which case there is nothing to switch
15512 -- besides T itself. Note that a private dependent subtype of a private
15513 -- type might not have been switched even if the base type has been,
15514 -- because of the last branch of Check_Private_View (see comment there).
15516 if not Is_Private_Type (BT) then
15517 Prepend_Elmt (Full_View (T), Exchanged_Views);
15518 Exchange_Declarations (T);
15519 return;
15520 end if;
15522 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15524 if Present (Full_View (BT)) then
15525 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15526 Exchange_Declarations (BT);
15527 end if;
15529 while Present (Priv_Elmt) loop
15530 Priv_Sub := (Node (Priv_Elmt));
15532 -- We avoid flipping the subtype if the Etype of its full view is
15533 -- private because this would result in a malformed subtype. This
15534 -- occurs when the Etype of the subtype full view is the full view of
15535 -- the base type (and since the base types were just switched, the
15536 -- subtype is pointing to the wrong view). This is currently the case
15537 -- for tagged record types, access types (maybe more?) and needs to
15538 -- be resolved. ???
15540 if Present (Full_View (Priv_Sub))
15541 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15542 then
15543 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15544 Exchange_Declarations (Priv_Sub);
15545 end if;
15547 Next_Elmt (Priv_Elmt);
15548 end loop;
15549 end Switch_View;
15551 -----------------
15552 -- True_Parent --
15553 -----------------
15555 function True_Parent (N : Node_Id) return Node_Id is
15556 begin
15557 if Nkind (Parent (N)) = N_Subunit then
15558 return Parent (Corresponding_Stub (Parent (N)));
15559 else
15560 return Parent (N);
15561 end if;
15562 end True_Parent;
15564 -----------------------------
15565 -- Valid_Default_Attribute --
15566 -----------------------------
15568 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15569 Attr_Id : constant Attribute_Id :=
15570 Get_Attribute_Id (Attribute_Name (Def));
15571 T : constant Entity_Id := Entity (Prefix (Def));
15572 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15573 F : Entity_Id;
15574 Num_F : Nat;
15575 OK : Boolean;
15577 begin
15578 if No (T) or else T = Any_Id then
15579 return;
15580 end if;
15582 Num_F := 0;
15583 F := First_Formal (Nam);
15584 while Present (F) loop
15585 Num_F := Num_F + 1;
15586 Next_Formal (F);
15587 end loop;
15589 case Attr_Id is
15590 when Attribute_Adjacent
15591 | Attribute_Ceiling
15592 | Attribute_Copy_Sign
15593 | Attribute_Floor
15594 | Attribute_Fraction
15595 | Attribute_Machine
15596 | Attribute_Model
15597 | Attribute_Remainder
15598 | Attribute_Rounding
15599 | Attribute_Unbiased_Rounding
15601 OK := Is_Fun
15602 and then Num_F = 1
15603 and then Is_Floating_Point_Type (T);
15605 when Attribute_Image
15606 | Attribute_Pred
15607 | Attribute_Succ
15608 | Attribute_Value
15609 | Attribute_Wide_Image
15610 | Attribute_Wide_Value
15612 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
15614 when Attribute_Max
15615 | Attribute_Min
15617 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
15619 when Attribute_Input =>
15620 OK := (Is_Fun and then Num_F = 1);
15622 when Attribute_Output
15623 | Attribute_Read
15624 | Attribute_Write
15626 OK := not Is_Fun and then Num_F = 2;
15628 when others =>
15629 OK := False;
15630 end case;
15632 if not OK then
15633 Error_Msg_N
15634 ("attribute reference has wrong profile for subprogram", Def);
15635 end if;
15636 end Valid_Default_Attribute;
15638 end Sem_Ch12;