[NDS32] new attribute no_prologue and new option -mret-in-naked-func.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob93a1c12053d721d858c8b3c4a27b7f1b355df75c
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-2018, 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 Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
75 with Table;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
81 with GNAT.HTable;
83 package body Sem_Ch12 is
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
105 -- | copy | | unit |
106 -- | |==============>| |
107 -- |___________| global |__________|
108 -- references | | |
109 -- | | |
110 -- .-----|--|.
111 -- | .-----|---.
112 -- | | .----------.
113 -- | | | generic |
114 -- |__| | |
115 -- |__| instance |
116 -- |__________|
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
131 -- actuals.
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
138 -- package.
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
164 -- type Global is ... -- outside of generic unit.
165 -- generic ...
166 -- package Outer is
167 -- ...
168 -- type Semi_Global is ... -- global to inner.
170 -- generic ... -- 1
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
173 -- procedure in2 is new inner (...); -- 4
174 -- end Outer;
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
216 -- with B; with A;
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
243 -----------------------------------------
244 -- Implementation of Generic Contracts --
245 -----------------------------------------
247 -- A "contract" is a collection of aspects and pragmas that either verify a
248 -- property of a construct at runtime or classify the data flow to and from
249 -- the construct in some fashion.
251 -- Generic packages, subprograms and their respective bodies may be subject
252 -- to the following contract-related aspects or pragmas collectively known
253 -- as annotations:
255 -- package subprogram [body]
256 -- Abstract_State Contract_Cases
257 -- Initial_Condition Depends
258 -- Initializes Extensions_Visible
259 -- Global
260 -- package body Post
261 -- Refined_State Post_Class
262 -- Postcondition
263 -- Pre
264 -- Pre_Class
265 -- Precondition
266 -- Refined_Depends
267 -- Refined_Global
268 -- Refined_Post
269 -- Test_Case
271 -- Most package contract annotations utilize forward references to classify
272 -- data declared within the package [body]. Subprogram annotations then use
273 -- the classifications to further refine them. These inter dependencies are
274 -- problematic with respect to the implementation of generics because their
275 -- analysis, capture of global references and instantiation does not mesh
276 -- well with the existing mechanism.
278 -- 1) Analysis of generic contracts is carried out the same way non-generic
279 -- contracts are analyzed:
281 -- 1.1) General rule - a contract is analyzed after all related aspects
282 -- and pragmas are analyzed. This is done by routines
284 -- Analyze_Package_Body_Contract
285 -- Analyze_Package_Contract
286 -- Analyze_Subprogram_Body_Contract
287 -- Analyze_Subprogram_Contract
289 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
290 -- are processed.
292 -- 1.3) Compilation unit body - the contract is analyzed at the end of
293 -- the body declaration list.
295 -- 1.4) Package - the contract is analyzed at the end of the private or
296 -- visible declarations, prior to analyzing the contracts of any nested
297 -- packages or subprograms.
299 -- 1.5) Package body - the contract is analyzed at the end of the body
300 -- declaration list, prior to analyzing the contracts of any nested
301 -- packages or subprograms.
303 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
304 -- package or a subprogram, then its contract is analyzed at the end of
305 -- the enclosing declarations, otherwise the subprogram is a compilation
306 -- unit 1.2).
308 -- 1.7) Subprogram body - if the subprogram body is declared inside a
309 -- block, a package body or a subprogram body, then its contract is
310 -- analyzed at the end of the enclosing declarations, otherwise the
311 -- subprogram is a compilation unit 1.3).
313 -- 2) Capture of global references within contracts is done after capturing
314 -- global references within the generic template. There are two reasons for
315 -- this delay - pragma annotations are not part of the generic template in
316 -- the case of a generic subprogram declaration, and analysis of contracts
317 -- is delayed.
319 -- Contract-related source pragmas within generic templates are prepared
320 -- for delayed capture of global references by routine
322 -- Create_Generic_Contract
324 -- The routine associates these pragmas with the contract of the template.
325 -- In the case of a generic subprogram declaration, the routine creates
326 -- generic templates for the pragmas declared after the subprogram because
327 -- they are not part of the template.
329 -- generic -- template starts
330 -- procedure Gen_Proc (Input : Integer); -- template ends
331 -- pragma Precondition (Input > 0); -- requires own template
333 -- 2.1) The capture of global references with aspect specifications and
334 -- source pragmas that apply to a generic unit must be suppressed when
335 -- the generic template is being processed because the contracts have not
336 -- been analyzed yet. Any attempts to capture global references at that
337 -- point will destroy the Associated_Node linkages and leave the template
338 -- undecorated. This delay is controlled by routine
340 -- Requires_Delayed_Save
342 -- 2.2) The real capture of global references within a contract is done
343 -- after the contract has been analyzed, by routine
345 -- Save_Global_References_In_Contract
347 -- 3) The instantiation of a generic contract occurs as part of the
348 -- instantiation of the contract owner. Generic subprogram declarations
349 -- require additional processing when the contract is specified by pragmas
350 -- because the pragmas are not part of the generic template. This is done
351 -- by routine
353 -- Instantiate_Subprogram_Contract
355 Circularity_Detected : Boolean := False;
356 -- This should really be reset on encountering a new main unit, but in
357 -- practice we are not using multiple main units so it is not critical.
359 --------------------------------------------------
360 -- Formal packages and partial parameterization --
361 --------------------------------------------------
363 -- When compiling a generic, a formal package is a local instantiation. If
364 -- declared with a box, its generic formals are visible in the enclosing
365 -- generic. If declared with a partial list of actuals, those actuals that
366 -- are defaulted (covered by an Others clause, or given an explicit box
367 -- initialization) are also visible in the enclosing generic, while those
368 -- that have a corresponding actual are not.
370 -- In our source model of instantiation, the same visibility must be
371 -- present in the spec and body of an instance: the names of the formals
372 -- that are defaulted must be made visible within the instance, and made
373 -- invisible (hidden) after the instantiation is complete, so that they
374 -- are not accessible outside of the instance.
376 -- In a generic, a formal package is treated like a special instantiation.
377 -- Our Ada 95 compiler handled formals with and without box in different
378 -- ways. With partial parameterization, we use a single model for both.
379 -- We create a package declaration that consists of the specification of
380 -- the generic package, and a set of declarations that map the actuals
381 -- into local renamings, just as we do for bona fide instantiations. For
382 -- defaulted parameters and formals with a box, we copy directly the
383 -- declarations of the formal into this local package. The result is a
384 -- a package whose visible declarations may include generic formals. This
385 -- package is only used for type checking and visibility analysis, and
386 -- never reaches the back-end, so it can freely violate the placement
387 -- rules for generic formal declarations.
389 -- The list of declarations (renamings and copies of formals) is built
390 -- by Analyze_Associations, just as for regular instantiations.
392 -- At the point of instantiation, conformance checking must be applied only
393 -- to those parameters that were specified in the formal. We perform this
394 -- checking by creating another internal instantiation, this one including
395 -- only the renamings and the formals (the rest of the package spec is not
396 -- relevant to conformance checking). We can then traverse two lists: the
397 -- list of actuals in the instance that corresponds to the formal package,
398 -- and the list of actuals produced for this bogus instantiation. We apply
399 -- the conformance rules to those actuals that are not defaulted (i.e.
400 -- which still appear as generic formals.
402 -- When we compile an instance body we must make the right parameters
403 -- visible again. The predicate Is_Generic_Formal indicates which of the
404 -- formals should have its Is_Hidden flag reset.
406 -----------------------
407 -- Local subprograms --
408 -----------------------
410 procedure Abandon_Instantiation (N : Node_Id);
411 pragma No_Return (Abandon_Instantiation);
412 -- Posts an error message "instantiation abandoned" at the indicated node
413 -- and then raises the exception Instantiation_Error to do it.
415 procedure Analyze_Formal_Array_Type
416 (T : in out Entity_Id;
417 Def : Node_Id);
418 -- A formal array type is treated like an array type declaration, and
419 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
420 -- in-out, because in the case of an anonymous type the entity is
421 -- actually created in the procedure.
423 -- The following procedures treat other kinds of formal parameters
425 procedure Analyze_Formal_Derived_Interface_Type
426 (N : Node_Id;
427 T : Entity_Id;
428 Def : Node_Id);
430 procedure Analyze_Formal_Derived_Type
431 (N : Node_Id;
432 T : Entity_Id;
433 Def : Node_Id);
435 procedure Analyze_Formal_Interface_Type
436 (N : Node_Id;
437 T : Entity_Id;
438 Def : Node_Id);
440 -- The following subprograms create abbreviated declarations for formal
441 -- scalar types. We introduce an anonymous base of the proper class for
442 -- each of them, and define the formals as constrained first subtypes of
443 -- their bases. The bounds are expressions that are non-static in the
444 -- generic.
446 procedure Analyze_Formal_Decimal_Fixed_Point_Type
447 (T : Entity_Id; Def : Node_Id);
448 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
453 (T : Entity_Id; Def : Node_Id);
455 procedure Analyze_Formal_Private_Type
456 (N : Node_Id;
457 T : Entity_Id;
458 Def : Node_Id);
459 -- Creates a new private type, which does not require completion
461 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
462 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
464 procedure Analyze_Generic_Formal_Part (N : Node_Id);
465 -- Analyze generic formal part
467 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
468 -- Create a new access type with the given designated type
470 function Analyze_Associations
471 (I_Node : Node_Id;
472 Formals : List_Id;
473 F_Copy : List_Id) return List_Id;
474 -- At instantiation time, build the list of associations between formals
475 -- and actuals. Each association becomes a renaming declaration for the
476 -- formal entity. F_Copy is the analyzed list of formals in the generic
477 -- copy. It is used to apply legality checks to the actuals. I_Node is the
478 -- instantiation node itself.
480 procedure Analyze_Subprogram_Instantiation
481 (N : Node_Id;
482 K : Entity_Kind);
484 procedure Build_Instance_Compilation_Unit_Nodes
485 (N : Node_Id;
486 Act_Body : Node_Id;
487 Act_Decl : Node_Id);
488 -- This procedure is used in the case where the generic instance of a
489 -- subprogram body or package body is a library unit. In this case, the
490 -- original library unit node for the generic instantiation must be
491 -- replaced by the resulting generic body, and a link made to a new
492 -- compilation unit node for the generic declaration. The argument N is
493 -- the original generic instantiation. Act_Body and Act_Decl are the body
494 -- and declaration of the instance (either package body and declaration
495 -- nodes or subprogram body and declaration nodes depending on the case).
496 -- On return, the node N has been rewritten with the actual body.
498 procedure Check_Access_Definition (N : Node_Id);
499 -- Subsidiary routine to null exclusion processing. Perform an assertion
500 -- check on Ada version and the presence of an access definition in N.
502 procedure Check_Formal_Packages (P_Id : Entity_Id);
503 -- Apply the following to all formal packages in generic associations.
504 -- Restore the visibility of the formals of the instance that are not
505 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
506 -- created for formal instances that are not defaulted.
508 procedure Check_Formal_Package_Instance
509 (Formal_Pack : Entity_Id;
510 Actual_Pack : Entity_Id);
511 -- Verify that the actuals of the actual instance match the actuals of
512 -- the template for a formal package that is not declared with a box.
514 procedure Check_Forward_Instantiation (Decl : Node_Id);
515 -- If the generic is a local entity and the corresponding body has not
516 -- been seen yet, flag enclosing packages to indicate that it will be
517 -- elaborated after the generic body. Subprograms declared in the same
518 -- package cannot be inlined by the front end because front-end inlining
519 -- requires a strict linear order of elaboration.
521 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
522 -- Check if some association between formals and actuals requires to make
523 -- visible primitives of a tagged type, and make those primitives visible.
524 -- Return the list of primitives whose visibility is modified (to restore
525 -- their visibility later through Restore_Hidden_Primitives). If no
526 -- candidate is found then return No_Elist.
528 procedure Check_Hidden_Child_Unit
529 (N : Node_Id;
530 Gen_Unit : Entity_Id;
531 Act_Decl_Id : Entity_Id);
532 -- If the generic unit is an implicit child instance within a parent
533 -- instance, we need to make an explicit test that it is not hidden by
534 -- a child instance of the same name and parent.
536 procedure Check_Generic_Actuals
537 (Instance : Entity_Id;
538 Is_Formal_Box : Boolean);
539 -- Similar to previous one. Check the actuals in the instantiation,
540 -- whose views can change between the point of instantiation and the point
541 -- of instantiation of the body. In addition, mark the generic renamings
542 -- as generic actuals, so that they are not compatible with other actuals.
543 -- Recurse on an actual that is a formal package whose declaration has
544 -- a box.
546 function Contains_Instance_Of
547 (Inner : Entity_Id;
548 Outer : Entity_Id;
549 N : Node_Id) return Boolean;
550 -- Inner is instantiated within the generic Outer. Check whether Inner
551 -- directly or indirectly contains an instance of Outer or of one of its
552 -- parents, in the case of a subunit. Each generic unit holds a list of
553 -- the entities instantiated within (at any depth). This procedure
554 -- determines whether the set of such lists contains a cycle, i.e. an
555 -- illegal circular instantiation.
557 function Denotes_Formal_Package
558 (Pack : Entity_Id;
559 On_Exit : Boolean := False;
560 Instance : Entity_Id := Empty) return Boolean;
561 -- Returns True if E is a formal package of an enclosing generic, or
562 -- the actual for such a formal in an enclosing instantiation. If such
563 -- a package is used as a formal in an nested generic, or as an actual
564 -- in a nested instantiation, the visibility of ITS formals should not
565 -- be modified. When called from within Restore_Private_Views, the flag
566 -- On_Exit is true, to indicate that the search for a possible enclosing
567 -- instance should ignore the current one. In that case Instance denotes
568 -- the declaration for which this is an actual. This declaration may be
569 -- an instantiation in the source, or the internal instantiation that
570 -- corresponds to the actual for a formal package.
572 function Earlier (N1, N2 : Node_Id) return Boolean;
573 -- Yields True if N1 and N2 appear in the same compilation unit,
574 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
575 -- traversal of the tree for the unit. Used to determine the placement
576 -- of freeze nodes for instance bodies that may depend on other instances.
578 function Find_Actual_Type
579 (Typ : Entity_Id;
580 Gen_Type : Entity_Id) return Entity_Id;
581 -- When validating the actual types of a child instance, check whether
582 -- the formal is a formal type of the parent unit, and retrieve the current
583 -- actual for it. Typ is the entity in the analyzed formal type declaration
584 -- (component or index type of an array type, or designated type of an
585 -- access formal) and Gen_Type is the enclosing analyzed formal array
586 -- or access type. The desired actual may be a formal of a parent, or may
587 -- be declared in a formal package of a parent. In both cases it is a
588 -- generic actual type because it appears within a visible instance.
589 -- Finally, it may be declared in a parent unit without being a formal
590 -- of that unit, in which case it must be retrieved by visibility.
591 -- Ambiguities may still arise if two homonyms are declared in two formal
592 -- packages, and the prefix of the formal type may be needed to resolve
593 -- the ambiguity in the instance ???
595 procedure Freeze_Subprogram_Body
596 (Inst_Node : Node_Id;
597 Gen_Body : Node_Id;
598 Pack_Id : Entity_Id);
599 -- The generic body may appear textually after the instance, including
600 -- in the proper body of a stub, or within a different package instance.
601 -- Given that the instance can only be elaborated after the generic, we
602 -- place freeze_nodes for the instance and/or for packages that may enclose
603 -- the instance and the generic, so that the back-end can establish the
604 -- proper order of elaboration.
606 function Get_Associated_Node (N : Node_Id) return Node_Id;
607 -- In order to propagate semantic information back from the analyzed copy
608 -- to the original generic, we maintain links between selected nodes in the
609 -- generic and their corresponding copies. At the end of generic analysis,
610 -- the routine Save_Global_References traverses the generic tree, examines
611 -- the semantic information, and preserves the links to those nodes that
612 -- contain global information. At instantiation, the information from the
613 -- associated node is placed on the new copy, so that name resolution is
614 -- not repeated.
616 -- Three kinds of source nodes have associated nodes:
618 -- a) those that can reference (denote) entities, that is identifiers,
619 -- character literals, expanded_names, operator symbols, operators,
620 -- and attribute reference nodes. These nodes have an Entity field
621 -- and are the set of nodes that are in N_Has_Entity.
623 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
625 -- c) selected components (N_Selected_Component)
627 -- For the first class, the associated node preserves the entity if it is
628 -- global. If the generic contains nested instantiations, the associated
629 -- node itself has been recopied, and a chain of them must be followed.
631 -- For aggregates, the associated node allows retrieval of the type, which
632 -- may otherwise not appear in the generic. The view of this type may be
633 -- different between generic and instantiation, and the full view can be
634 -- installed before the instantiation is analyzed. For aggregates of type
635 -- extensions, the same view exchange may have to be performed for some of
636 -- the ancestor types, if their view is private at the point of
637 -- instantiation.
639 -- Nodes that are selected components in the parse tree may be rewritten
640 -- as expanded names after resolution, and must be treated as potential
641 -- entity holders, which is why they also have an Associated_Node.
643 -- Nodes that do not come from source, such as freeze nodes, do not appear
644 -- in the generic tree, and need not have an associated node.
646 -- The associated node is stored in the Associated_Node field. Note that
647 -- this field overlaps Entity, which is fine, because the whole point is
648 -- that we don't need or want the normal Entity field in this situation.
650 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
651 -- Traverse the Exchanged_Views list to see if a type was private
652 -- and has already been flipped during this phase of instantiation.
654 procedure Hide_Current_Scope;
655 -- When instantiating a generic child unit, the parent context must be
656 -- present, but the instance and all entities that may be generated
657 -- must be inserted in the current scope. We leave the current scope
658 -- on the stack, but make its entities invisible to avoid visibility
659 -- problems. This is reversed at the end of the instantiation. This is
660 -- not done for the instantiation of the bodies, which only require the
661 -- instances of the generic parents to be in scope.
663 function In_Main_Context (E : Entity_Id) return Boolean;
664 -- Check whether an instantiation is in the context of the main unit.
665 -- Used to determine whether its body should be elaborated to allow
666 -- front-end inlining.
668 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
669 -- Add the context clause of the unit containing a generic unit to a
670 -- compilation unit that is, or contains, an instantiation.
672 procedure Init_Env;
673 -- Establish environment for subsequent instantiation. Separated from
674 -- Save_Env because data-structures for visibility handling must be
675 -- initialized before call to Check_Generic_Child_Unit.
677 procedure Inline_Instance_Body
678 (N : Node_Id;
679 Gen_Unit : Entity_Id;
680 Act_Decl : Node_Id);
681 -- If front-end inlining is requested, instantiate the package body,
682 -- and preserve the visibility of its compilation unit, to insure
683 -- that successive instantiations succeed.
685 procedure Insert_Freeze_Node_For_Instance
686 (N : Node_Id;
687 F_Node : Node_Id);
688 -- N denotes a package or a subprogram instantiation and F_Node is the
689 -- associated freeze node. Insert the freeze node before the first source
690 -- body which follows immediately after N. If no such body is found, the
691 -- freeze node is inserted at the end of the declarative region which
692 -- contains N.
694 procedure Install_Body
695 (Act_Body : Node_Id;
696 N : Node_Id;
697 Gen_Body : Node_Id;
698 Gen_Decl : Node_Id);
699 -- If the instantiation happens textually before the body of the generic,
700 -- the instantiation of the body must be analyzed after the generic body,
701 -- and not at the point of instantiation. Such early instantiations can
702 -- happen if the generic and the instance appear in a package declaration
703 -- because the generic body can only appear in the corresponding package
704 -- body. Early instantiations can also appear if generic, instance and
705 -- body are all in the declarative part of a subprogram or entry. Entities
706 -- of packages that are early instantiations are delayed, and their freeze
707 -- node appears after the generic body. This rather complex machinery is
708 -- needed when nested instantiations are present, because the source does
709 -- not carry any indication of where the corresponding instance bodies must
710 -- be installed and frozen.
712 procedure Install_Formal_Packages (Par : Entity_Id);
713 -- Install the visible part of any formal of the parent that is a formal
714 -- package. Note that for the case of a formal package with a box, this
715 -- includes the formal part of the formal package (12.7(10/2)).
717 procedure Install_Hidden_Primitives
718 (Prims_List : in out Elist_Id;
719 Gen_T : Entity_Id;
720 Act_T : Entity_Id);
721 -- Remove suffix 'P' from hidden primitives of Act_T to match the
722 -- visibility of primitives of Gen_T. The list of primitives to which
723 -- the suffix is removed is added to Prims_List to restore them later.
725 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
726 -- When compiling an instance of a child unit the parent (which is
727 -- itself an instance) is an enclosing scope that must be made
728 -- immediately visible. This procedure is also used to install the non-
729 -- generic parent of a generic child unit when compiling its body, so
730 -- that full views of types in the parent are made visible.
732 -- The functions Instantiate_XXX perform various legality checks and build
733 -- the declarations for instantiated generic parameters. In all of these
734 -- Formal is the entity in the generic unit, Actual is the entity of
735 -- expression in the generic associations, and Analyzed_Formal is the
736 -- formal in the generic copy, which contains the semantic information to
737 -- be used to validate the actual.
739 function Instantiate_Object
740 (Formal : Node_Id;
741 Actual : Node_Id;
742 Analyzed_Formal : Node_Id) return List_Id;
744 function Instantiate_Type
745 (Formal : Node_Id;
746 Actual : Node_Id;
747 Analyzed_Formal : Node_Id;
748 Actual_Decls : List_Id) return List_Id;
750 function Instantiate_Formal_Subprogram
751 (Formal : Node_Id;
752 Actual : Node_Id;
753 Analyzed_Formal : Node_Id) return Node_Id;
755 function Instantiate_Formal_Package
756 (Formal : Node_Id;
757 Actual : Node_Id;
758 Analyzed_Formal : Node_Id) return List_Id;
759 -- If the formal package is declared with a box, special visibility rules
760 -- apply to its formals: they are in the visible part of the package. This
761 -- is true in the declarative region of the formal package, that is to say
762 -- in the enclosing generic or instantiation. For an instantiation, the
763 -- parameters of the formal package are made visible in an explicit step.
764 -- Furthermore, if the actual has a visible USE clause, these formals must
765 -- be made potentially use-visible as well. On exit from the enclosing
766 -- instantiation, the reverse must be done.
768 -- For a formal package declared without a box, there are conformance rules
769 -- that apply to the actuals in the generic declaration and the actuals of
770 -- the actual package in the enclosing instantiation. The simplest way to
771 -- apply these rules is to repeat the instantiation of the formal package
772 -- in the context of the enclosing instance, and compare the generic
773 -- associations of this instantiation with those of the actual package.
774 -- This internal instantiation only needs to contain the renamings of the
775 -- formals: the visible and private declarations themselves need not be
776 -- created.
778 -- In Ada 2005, the formal package may be only partially parameterized.
779 -- In that case the visibility step must make visible those actuals whose
780 -- corresponding formals were given with a box. A final complication
781 -- involves inherited operations from formal derived types, which must
782 -- be visible if the type is.
784 function Is_In_Main_Unit (N : Node_Id) return Boolean;
785 -- Test if given node is in the main unit
787 procedure Load_Parent_Of_Generic
788 (N : Node_Id;
789 Spec : Node_Id;
790 Body_Optional : Boolean := False);
791 -- If the generic appears in a separate non-generic library unit, load the
792 -- corresponding body to retrieve the body of the generic. N is the node
793 -- for the generic instantiation, Spec is the generic package declaration.
795 -- Body_Optional is a flag that indicates that the body is being loaded to
796 -- ensure that temporaries are generated consistently when there are other
797 -- instances in the current declarative part that precede the one being
798 -- loaded. In that case a missing body is acceptable.
800 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
801 -- Within the generic part, entities in the formal package are
802 -- visible. To validate subsequent type declarations, indicate
803 -- the correspondence between the entities in the analyzed formal,
804 -- and the entities in the actual package. There are three packages
805 -- involved in the instantiation of a formal package: the parent
806 -- generic P1 which appears in the generic declaration, the fake
807 -- instantiation P2 which appears in the analyzed generic, and whose
808 -- visible entities may be used in subsequent formals, and the actual
809 -- P3 in the instance. To validate subsequent formals, me indicate
810 -- that the entities in P2 are mapped into those of P3. The mapping of
811 -- entities has to be done recursively for nested packages.
813 procedure Move_Freeze_Nodes
814 (Out_Of : Entity_Id;
815 After : Node_Id;
816 L : List_Id);
817 -- Freeze nodes can be generated in the analysis of a generic unit, but
818 -- will not be seen by the back-end. It is necessary to move those nodes
819 -- to the enclosing scope if they freeze an outer entity. We place them
820 -- at the end of the enclosing generic package, which is semantically
821 -- neutral.
823 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
824 -- Analyze actuals to perform name resolution. Full resolution is done
825 -- later, when the expected types are known, but names have to be captured
826 -- before installing parents of generics, that are not visible for the
827 -- actuals themselves.
829 -- If Inst is present, it is the entity of the package instance. This
830 -- entity is marked as having a limited_view actual when some actual is
831 -- a limited view. This is used to place the instance body properly.
833 procedure Provide_Completing_Bodies (N : Node_Id);
834 -- Generate completing bodies for all subprograms found within package or
835 -- subprogram declaration N.
837 procedure Remove_Parent (In_Body : Boolean := False);
838 -- Reverse effect after instantiation of child is complete
840 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
841 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
842 -- set to No_Elist.
844 procedure Set_Instance_Env
845 (Gen_Unit : Entity_Id;
846 Act_Unit : Entity_Id);
847 -- Save current instance on saved environment, to be used to determine
848 -- the global status of entities in nested instances. Part of Save_Env.
849 -- called after verifying that the generic unit is legal for the instance,
850 -- The procedure also examines whether the generic unit is a predefined
851 -- unit, in order to set configuration switches accordingly. As a result
852 -- the procedure must be called after analyzing and freezing the actuals.
854 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
855 -- Associate analyzed generic parameter with corresponding instance. Used
856 -- for semantic checks at instantiation time.
858 function True_Parent (N : Node_Id) return Node_Id;
859 -- For a subunit, return parent of corresponding stub, else return
860 -- parent of node.
862 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
863 -- Verify that an attribute that appears as the default for a formal
864 -- subprogram is a function or procedure with the correct profile.
866 -------------------------------------------
867 -- Data Structures for Generic Renamings --
868 -------------------------------------------
870 -- The map Generic_Renamings associates generic entities with their
871 -- corresponding actuals. Currently used to validate type instances. It
872 -- will eventually be used for all generic parameters to eliminate the
873 -- need for overload resolution in the instance.
875 type Assoc_Ptr is new Int;
877 Assoc_Null : constant Assoc_Ptr := -1;
879 type Assoc is record
880 Gen_Id : Entity_Id;
881 Act_Id : Entity_Id;
882 Next_In_HTable : Assoc_Ptr;
883 end record;
885 package Generic_Renamings is new Table.Table
886 (Table_Component_Type => Assoc,
887 Table_Index_Type => Assoc_Ptr,
888 Table_Low_Bound => 0,
889 Table_Initial => 10,
890 Table_Increment => 100,
891 Table_Name => "Generic_Renamings");
893 -- Variable to hold enclosing instantiation. When the environment is
894 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
896 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
898 -- Hash table for associations
900 HTable_Size : constant := 37;
901 type HTable_Range is range 0 .. HTable_Size - 1;
903 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
904 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
905 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
906 function Hash (F : Entity_Id) return HTable_Range;
908 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
909 Header_Num => HTable_Range,
910 Element => Assoc,
911 Elmt_Ptr => Assoc_Ptr,
912 Null_Ptr => Assoc_Null,
913 Set_Next => Set_Next_Assoc,
914 Next => Next_Assoc,
915 Key => Entity_Id,
916 Get_Key => Get_Gen_Id,
917 Hash => Hash,
918 Equal => "=");
920 Exchanged_Views : Elist_Id;
921 -- This list holds the private views that have been exchanged during
922 -- instantiation to restore the visibility of the generic declaration.
923 -- (see comments above). After instantiation, the current visibility is
924 -- reestablished by means of a traversal of this list.
926 Hidden_Entities : Elist_Id;
927 -- This list holds the entities of the current scope that are removed
928 -- from immediate visibility when instantiating a child unit. Their
929 -- visibility is restored in Remove_Parent.
931 -- Because instantiations can be recursive, the following must be saved
932 -- on entry and restored on exit from an instantiation (spec or body).
933 -- This is done by the two procedures Save_Env and Restore_Env. For
934 -- package and subprogram instantiations (but not for the body instances)
935 -- the action of Save_Env is done in two steps: Init_Env is called before
936 -- Check_Generic_Child_Unit, because setting the parent instances requires
937 -- that the visibility data structures be properly initialized. Once the
938 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
940 Parent_Unit_Visible : Boolean := False;
941 -- Parent_Unit_Visible is used when the generic is a child unit, and
942 -- indicates whether the ultimate parent of the generic is visible in the
943 -- instantiation environment. It is used to reset the visibility of the
944 -- parent at the end of the instantiation (see Remove_Parent).
946 Instance_Parent_Unit : Entity_Id := Empty;
947 -- This records the ultimate parent unit of an instance of a generic
948 -- child unit and is used in conjunction with Parent_Unit_Visible to
949 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
951 type Instance_Env is record
952 Instantiated_Parent : Assoc;
953 Exchanged_Views : Elist_Id;
954 Hidden_Entities : Elist_Id;
955 Current_Sem_Unit : Unit_Number_Type;
956 Parent_Unit_Visible : Boolean := False;
957 Instance_Parent_Unit : Entity_Id := Empty;
958 Switches : Config_Switches_Type;
959 end record;
961 package Instance_Envs is new Table.Table (
962 Table_Component_Type => Instance_Env,
963 Table_Index_Type => Int,
964 Table_Low_Bound => 0,
965 Table_Initial => 32,
966 Table_Increment => 100,
967 Table_Name => "Instance_Envs");
969 procedure Restore_Private_Views
970 (Pack_Id : Entity_Id;
971 Is_Package : Boolean := True);
972 -- Restore the private views of external types, and unmark the generic
973 -- renamings of actuals, so that they become compatible subtypes again.
974 -- For subprograms, Pack_Id is the package constructed to hold the
975 -- renamings.
977 procedure Switch_View (T : Entity_Id);
978 -- Switch the partial and full views of a type and its private
979 -- dependents (i.e. its subtypes and derived types).
981 ------------------------------------
982 -- Structures for Error Reporting --
983 ------------------------------------
985 Instantiation_Node : Node_Id;
986 -- Used by subprograms that validate instantiation of formal parameters
987 -- where there might be no actual on which to place the error message.
988 -- Also used to locate the instantiation node for generic subunits.
990 Instantiation_Error : exception;
991 -- When there is a semantic error in the generic parameter matching,
992 -- there is no point in continuing the instantiation, because the
993 -- number of cascaded errors is unpredictable. This exception aborts
994 -- the instantiation process altogether.
996 S_Adjustment : Sloc_Adjustment;
997 -- Offset created for each node in an instantiation, in order to keep
998 -- track of the source position of the instantiation in each of its nodes.
999 -- A subsequent semantic error or warning on a construct of the instance
1000 -- points to both places: the original generic node, and the point of
1001 -- instantiation. See Sinput and Sinput.L for additional details.
1003 ------------------------------------------------------------
1004 -- Data structure for keeping track when inside a Generic --
1005 ------------------------------------------------------------
1007 -- The following table is used to save values of the Inside_A_Generic
1008 -- flag (see spec of Sem) when they are saved by Start_Generic.
1010 package Generic_Flags is new Table.Table (
1011 Table_Component_Type => Boolean,
1012 Table_Index_Type => Int,
1013 Table_Low_Bound => 0,
1014 Table_Initial => 32,
1015 Table_Increment => 200,
1016 Table_Name => "Generic_Flags");
1018 ---------------------------
1019 -- Abandon_Instantiation --
1020 ---------------------------
1022 procedure Abandon_Instantiation (N : Node_Id) is
1023 begin
1024 Error_Msg_N ("\instantiation abandoned!", N);
1025 raise Instantiation_Error;
1026 end Abandon_Instantiation;
1028 --------------------------------
1029 -- Add_Pending_Instantiation --
1030 --------------------------------
1032 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1033 begin
1035 -- Add to the instantiation node and the corresponding unit declaration
1036 -- the current values of global flags to be used when analyzing the
1037 -- instance body.
1039 Pending_Instantiations.Append
1040 ((Inst_Node => Inst,
1041 Act_Decl => Act_Decl,
1042 Expander_Status => Expander_Active,
1043 Current_Sem_Unit => Current_Sem_Unit,
1044 Scope_Suppress => Scope_Suppress,
1045 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1046 Version => Ada_Version,
1047 Version_Pragma => Ada_Version_Pragma,
1048 Warnings => Save_Warnings,
1049 SPARK_Mode => SPARK_Mode,
1050 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1051 end Add_Pending_Instantiation;
1053 ----------------------------------
1054 -- Adjust_Inherited_Pragma_Sloc --
1055 ----------------------------------
1057 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1058 begin
1059 Adjust_Instantiation_Sloc (N, S_Adjustment);
1060 end Adjust_Inherited_Pragma_Sloc;
1062 --------------------------
1063 -- Analyze_Associations --
1064 --------------------------
1066 function Analyze_Associations
1067 (I_Node : Node_Id;
1068 Formals : List_Id;
1069 F_Copy : List_Id) return List_Id
1071 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1072 Assoc_List : constant List_Id := New_List;
1073 Default_Actuals : constant List_Id := New_List;
1074 Gen_Unit : constant Entity_Id :=
1075 Defining_Entity (Parent (F_Copy));
1077 Actuals : List_Id;
1078 Actual : Node_Id;
1079 Analyzed_Formal : Node_Id;
1080 First_Named : Node_Id := Empty;
1081 Formal : Node_Id;
1082 Match : Node_Id;
1083 Named : Node_Id;
1084 Saved_Formal : Node_Id;
1086 Default_Formals : constant List_Id := New_List;
1087 -- If an Others_Choice is present, some of the formals may be defaulted.
1088 -- To simplify the treatment of visibility in an instance, we introduce
1089 -- individual defaults for each such formal. These defaults are
1090 -- appended to the list of associations and replace the Others_Choice.
1092 Found_Assoc : Node_Id;
1093 -- Association for the current formal being match. Empty if there are
1094 -- no remaining actuals, or if there is no named association with the
1095 -- name of the formal.
1097 Is_Named_Assoc : Boolean;
1098 Num_Matched : Nat := 0;
1099 Num_Actuals : Nat := 0;
1101 Others_Present : Boolean := False;
1102 Others_Choice : Node_Id := Empty;
1103 -- In Ada 2005, indicates partial parameterization of a formal
1104 -- package. As usual an other association must be last in the list.
1106 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1107 -- Warn if an actual fixed-point type has user-defined arithmetic
1108 -- operations, but there is no corresponding formal in the generic,
1109 -- in which case the predefined operations will be used. This merits
1110 -- a warning because of the special semantics of fixed point ops.
1112 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1113 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1114 -- cannot have a named association for it. AI05-0025 extends this rule
1115 -- to formals of formal packages by AI05-0025, and it also applies to
1116 -- box-initialized formals.
1118 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1119 -- Determine whether the parameter types and the return type of Subp
1120 -- are fully defined at the point of instantiation.
1122 function Matching_Actual
1123 (F : Entity_Id;
1124 A_F : Entity_Id) return Node_Id;
1125 -- Find actual that corresponds to a given a formal parameter. If the
1126 -- actuals are positional, return the next one, if any. If the actuals
1127 -- are named, scan the parameter associations to find the right one.
1128 -- A_F is the corresponding entity in the analyzed generic, which is
1129 -- placed on the selector name for ASIS use.
1131 -- In Ada 2005, a named association may be given with a box, in which
1132 -- case Matching_Actual sets Found_Assoc to the generic association,
1133 -- but return Empty for the actual itself. In this case the code below
1134 -- creates a corresponding declaration for the formal.
1136 function Partial_Parameterization return Boolean;
1137 -- Ada 2005: if no match is found for a given formal, check if the
1138 -- association for it includes a box, or whether the associations
1139 -- include an Others clause.
1141 procedure Process_Default (F : Entity_Id);
1142 -- Add a copy of the declaration of generic formal F to the list of
1143 -- associations, and add an explicit box association for F if there
1144 -- is none yet, and the default comes from an Others_Choice.
1146 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1147 -- Determine whether Subp renames one of the subprograms defined in the
1148 -- generated package Standard.
1150 procedure Set_Analyzed_Formal;
1151 -- Find the node in the generic copy that corresponds to a given formal.
1152 -- The semantic information on this node is used to perform legality
1153 -- checks on the actuals. Because semantic analysis can introduce some
1154 -- anonymous entities or modify the declaration node itself, the
1155 -- correspondence between the two lists is not one-one. In addition to
1156 -- anonymous types, the presence a formal equality will introduce an
1157 -- implicit declaration for the corresponding inequality.
1159 ----------------------------------------
1160 -- Check_Overloaded_Formal_Subprogram --
1161 ----------------------------------------
1163 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1164 Temp_Formal : Entity_Id;
1166 begin
1167 Temp_Formal := First (Formals);
1168 while Present (Temp_Formal) loop
1169 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1170 and then Temp_Formal /= Formal
1171 and then
1172 Chars (Defining_Unit_Name (Specification (Formal))) =
1173 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1174 then
1175 if Present (Found_Assoc) then
1176 Error_Msg_N
1177 ("named association not allowed for overloaded formal",
1178 Found_Assoc);
1180 else
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Others_Choice);
1184 end if;
1186 Abandon_Instantiation (Instantiation_Node);
1187 end if;
1189 Next (Temp_Formal);
1190 end loop;
1191 end Check_Overloaded_Formal_Subprogram;
1193 -------------------------------
1194 -- Check_Fixed_Point_Actual --
1195 -------------------------------
1197 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1198 Typ : constant Entity_Id := Entity (Actual);
1199 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1200 Elem : Elmt_Id;
1201 Formal : Node_Id;
1202 Op : Entity_Id;
1204 begin
1205 -- Locate primitive operations of the type that are arithmetic
1206 -- operations.
1208 Elem := First_Elmt (Prims);
1209 while Present (Elem) loop
1210 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1212 -- Check whether the generic unit has a formal subprogram of
1213 -- the same name. This does not check types but is good enough
1214 -- to justify a warning.
1216 Formal := First_Non_Pragma (Formals);
1217 Op := Alias (Node (Elem));
1219 while Present (Formal) loop
1220 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1221 and then Chars (Defining_Entity (Formal)) =
1222 Chars (Node (Elem))
1223 then
1224 exit;
1226 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1227 declare
1228 Assoc : Node_Id;
1229 Ent : Entity_Id;
1231 begin
1232 -- Locate corresponding actual, and check whether it
1233 -- includes a fixed-point type.
1235 Assoc := First (Assoc_List);
1236 while Present (Assoc) loop
1237 exit when
1238 Nkind (Assoc) = N_Package_Renaming_Declaration
1239 and then Chars (Defining_Unit_Name (Assoc)) =
1240 Chars (Defining_Identifier (Formal));
1242 Next (Assoc);
1243 end loop;
1245 if Present (Assoc) then
1247 -- If formal package declares a fixed-point type,
1248 -- and the user-defined operator is derived from
1249 -- a generic instance package, the fixed-point type
1250 -- does not use the corresponding predefined op.
1252 Ent := First_Entity (Entity (Name (Assoc)));
1253 while Present (Ent) loop
1254 if Is_Fixed_Point_Type (Ent)
1255 and then Present (Op)
1256 and then Is_Generic_Instance (Scope (Op))
1257 then
1258 return;
1259 end if;
1261 Next_Entity (Ent);
1262 end loop;
1263 end if;
1264 end;
1265 end if;
1267 Next (Formal);
1268 end loop;
1270 if No (Formal) then
1271 Error_Msg_Sloc := Sloc (Node (Elem));
1272 Error_Msg_NE
1273 ("?instance uses predefined operation, not primitive "
1274 & "operation&#", Actual, Node (Elem));
1275 end if;
1276 end if;
1278 Next_Elmt (Elem);
1279 end loop;
1280 end Check_Fixed_Point_Actual;
1282 -------------------------------
1283 -- Has_Fully_Defined_Profile --
1284 -------------------------------
1286 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1287 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1288 -- Determine whethet type Typ is fully defined
1290 ---------------------------
1291 -- Is_Fully_Defined_Type --
1292 ---------------------------
1294 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1295 begin
1296 -- A private type without a full view is not fully defined
1298 if Is_Private_Type (Typ)
1299 and then No (Full_View (Typ))
1300 then
1301 return False;
1303 -- An incomplete type is never fully defined
1305 elsif Is_Incomplete_Type (Typ) then
1306 return False;
1308 -- All other types are fully defined
1310 else
1311 return True;
1312 end if;
1313 end Is_Fully_Defined_Type;
1315 -- Local declarations
1317 Param : Entity_Id;
1319 -- Start of processing for Has_Fully_Defined_Profile
1321 begin
1322 -- Check the parameters
1324 Param := First_Formal (Subp);
1325 while Present (Param) loop
1326 if not Is_Fully_Defined_Type (Etype (Param)) then
1327 return False;
1328 end if;
1330 Next_Formal (Param);
1331 end loop;
1333 -- Check the return type
1335 return Is_Fully_Defined_Type (Etype (Subp));
1336 end Has_Fully_Defined_Profile;
1338 ---------------------
1339 -- Matching_Actual --
1340 ---------------------
1342 function Matching_Actual
1343 (F : Entity_Id;
1344 A_F : Entity_Id) return Node_Id
1346 Prev : Node_Id;
1347 Act : Node_Id;
1349 begin
1350 Is_Named_Assoc := False;
1352 -- End of list of purely positional parameters
1354 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1355 Found_Assoc := Empty;
1356 Act := Empty;
1358 -- Case of positional parameter corresponding to current formal
1360 elsif No (Selector_Name (Actual)) then
1361 Found_Assoc := Actual;
1362 Act := Explicit_Generic_Actual_Parameter (Actual);
1363 Num_Matched := Num_Matched + 1;
1364 Next (Actual);
1366 -- Otherwise scan list of named actuals to find the one with the
1367 -- desired name. All remaining actuals have explicit names.
1369 else
1370 Is_Named_Assoc := True;
1371 Found_Assoc := Empty;
1372 Act := Empty;
1373 Prev := Empty;
1375 while Present (Actual) loop
1376 if Nkind (Actual) = N_Others_Choice then
1377 Found_Assoc := Empty;
1378 Act := Empty;
1380 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1381 Set_Entity (Selector_Name (Actual), A_F);
1382 Set_Etype (Selector_Name (Actual), Etype (A_F));
1383 Generate_Reference (A_F, Selector_Name (Actual));
1385 Found_Assoc := Actual;
1386 Act := Explicit_Generic_Actual_Parameter (Actual);
1387 Num_Matched := Num_Matched + 1;
1388 exit;
1389 end if;
1391 Prev := Actual;
1392 Next (Actual);
1393 end loop;
1395 -- Reset for subsequent searches. In most cases the named
1396 -- associations are in order. If they are not, we reorder them
1397 -- to avoid scanning twice the same actual. This is not just a
1398 -- question of efficiency: there may be multiple defaults with
1399 -- boxes that have the same name. In a nested instantiation we
1400 -- insert actuals for those defaults, and cannot rely on their
1401 -- names to disambiguate them.
1403 if Actual = First_Named then
1404 Next (First_Named);
1406 elsif Present (Actual) then
1407 Insert_Before (First_Named, Remove_Next (Prev));
1408 end if;
1410 Actual := First_Named;
1411 end if;
1413 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1414 Set_Used_As_Generic_Actual (Entity (Act));
1415 end if;
1417 return Act;
1418 end Matching_Actual;
1420 ------------------------------
1421 -- Partial_Parameterization --
1422 ------------------------------
1424 function Partial_Parameterization return Boolean is
1425 begin
1426 return Others_Present
1427 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1428 end Partial_Parameterization;
1430 ---------------------
1431 -- Process_Default --
1432 ---------------------
1434 procedure Process_Default (F : Entity_Id) is
1435 Loc : constant Source_Ptr := Sloc (I_Node);
1436 F_Id : constant Entity_Id := Defining_Entity (F);
1437 Decl : Node_Id;
1438 Default : Node_Id;
1439 Id : Entity_Id;
1441 begin
1442 -- Append copy of formal declaration to associations, and create new
1443 -- defining identifier for it.
1445 Decl := New_Copy_Tree (F);
1446 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1448 if Nkind (F) in N_Formal_Subprogram_Declaration then
1449 Set_Defining_Unit_Name (Specification (Decl), Id);
1451 else
1452 Set_Defining_Identifier (Decl, Id);
1453 end if;
1455 Append (Decl, Assoc_List);
1457 if No (Found_Assoc) then
1458 Default :=
1459 Make_Generic_Association (Loc,
1460 Selector_Name =>
1461 New_Occurrence_Of (Id, Loc),
1462 Explicit_Generic_Actual_Parameter => Empty);
1463 Set_Box_Present (Default);
1464 Append (Default, Default_Formals);
1465 end if;
1466 end Process_Default;
1468 ---------------------------------
1469 -- Renames_Standard_Subprogram --
1470 ---------------------------------
1472 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1473 Id : Entity_Id;
1475 begin
1476 Id := Alias (Subp);
1477 while Present (Id) loop
1478 if Scope (Id) = Standard_Standard then
1479 return True;
1480 end if;
1482 Id := Alias (Id);
1483 end loop;
1485 return False;
1486 end Renames_Standard_Subprogram;
1488 -------------------------
1489 -- Set_Analyzed_Formal --
1490 -------------------------
1492 procedure Set_Analyzed_Formal is
1493 Kind : Node_Kind;
1495 begin
1496 while Present (Analyzed_Formal) loop
1497 Kind := Nkind (Analyzed_Formal);
1499 case Nkind (Formal) is
1500 when N_Formal_Subprogram_Declaration =>
1501 exit when Kind in N_Formal_Subprogram_Declaration
1502 and then
1503 Chars
1504 (Defining_Unit_Name (Specification (Formal))) =
1505 Chars
1506 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1508 when N_Formal_Package_Declaration =>
1509 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1510 N_Generic_Package_Declaration,
1511 N_Package_Declaration);
1513 when N_Use_Package_Clause
1514 | N_Use_Type_Clause
1516 exit;
1518 when others =>
1520 -- Skip freeze nodes, and nodes inserted to replace
1521 -- unrecognized pragmas.
1523 exit when
1524 Kind not in N_Formal_Subprogram_Declaration
1525 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1526 N_Freeze_Entity,
1527 N_Null_Statement,
1528 N_Itype_Reference)
1529 and then Chars (Defining_Identifier (Formal)) =
1530 Chars (Defining_Identifier (Analyzed_Formal));
1531 end case;
1533 Next (Analyzed_Formal);
1534 end loop;
1535 end Set_Analyzed_Formal;
1537 -- Start of processing for Analyze_Associations
1539 begin
1540 Actuals := Generic_Associations (I_Node);
1542 if Present (Actuals) then
1544 -- Check for an Others choice, indicating a partial parameterization
1545 -- for a formal package.
1547 Actual := First (Actuals);
1548 while Present (Actual) loop
1549 if Nkind (Actual) = N_Others_Choice then
1550 Others_Present := True;
1551 Others_Choice := Actual;
1553 if Present (Next (Actual)) then
1554 Error_Msg_N ("others must be last association", Actual);
1555 end if;
1557 -- This subprogram is used both for formal packages and for
1558 -- instantiations. For the latter, associations must all be
1559 -- explicit.
1561 if Nkind (I_Node) /= N_Formal_Package_Declaration
1562 and then Comes_From_Source (I_Node)
1563 then
1564 Error_Msg_N
1565 ("others association not allowed in an instance",
1566 Actual);
1567 end if;
1569 -- In any case, nothing to do after the others association
1571 exit;
1573 elsif Box_Present (Actual)
1574 and then Comes_From_Source (I_Node)
1575 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1576 then
1577 Error_Msg_N
1578 ("box association not allowed in an instance", Actual);
1579 end if;
1581 Next (Actual);
1582 end loop;
1584 -- If named associations are present, save first named association
1585 -- (it may of course be Empty) to facilitate subsequent name search.
1587 First_Named := First (Actuals);
1588 while Present (First_Named)
1589 and then Nkind (First_Named) /= N_Others_Choice
1590 and then No (Selector_Name (First_Named))
1591 loop
1592 Num_Actuals := Num_Actuals + 1;
1593 Next (First_Named);
1594 end loop;
1595 end if;
1597 Named := First_Named;
1598 while Present (Named) loop
1599 if Nkind (Named) /= N_Others_Choice
1600 and then No (Selector_Name (Named))
1601 then
1602 Error_Msg_N ("invalid positional actual after named one", Named);
1603 Abandon_Instantiation (Named);
1604 end if;
1606 -- A named association may lack an actual parameter, if it was
1607 -- introduced for a default subprogram that turns out to be local
1608 -- to the outer instantiation. If it has a box association it must
1609 -- correspond to some formal in the generic.
1611 if Nkind (Named) /= N_Others_Choice
1612 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1613 or else Box_Present (Named))
1614 then
1615 Num_Actuals := Num_Actuals + 1;
1616 end if;
1618 Next (Named);
1619 end loop;
1621 if Present (Formals) then
1622 Formal := First_Non_Pragma (Formals);
1623 Analyzed_Formal := First_Non_Pragma (F_Copy);
1625 if Present (Actuals) then
1626 Actual := First (Actuals);
1628 -- All formals should have default values
1630 else
1631 Actual := Empty;
1632 end if;
1634 while Present (Formal) loop
1635 Set_Analyzed_Formal;
1636 Saved_Formal := Next_Non_Pragma (Formal);
1638 case Nkind (Formal) is
1639 when N_Formal_Object_Declaration =>
1640 Match :=
1641 Matching_Actual
1642 (Defining_Identifier (Formal),
1643 Defining_Identifier (Analyzed_Formal));
1645 if No (Match) and then Partial_Parameterization then
1646 Process_Default (Formal);
1648 else
1649 Append_List
1650 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1651 Assoc_List);
1653 -- For a defaulted in_parameter, create an entry in the
1654 -- the list of defaulted actuals, for GNATProve use. Do
1655 -- not included these defaults for an instance nested
1656 -- within a generic, because the defaults are also used
1657 -- in the analysis of the enclosing generic, and only
1658 -- defaulted subprograms are relevant there.
1660 if No (Match) and then not Inside_A_Generic then
1661 Append_To (Default_Actuals,
1662 Make_Generic_Association (Sloc (I_Node),
1663 Selector_Name =>
1664 New_Occurrence_Of
1665 (Defining_Identifier (Formal), Sloc (I_Node)),
1666 Explicit_Generic_Actual_Parameter =>
1667 New_Copy_Tree (Default_Expression (Formal))));
1668 end if;
1669 end if;
1671 -- If the object is a call to an expression function, this
1672 -- is a freezing point for it.
1674 if Is_Entity_Name (Match)
1675 and then Present (Entity (Match))
1676 and then Nkind
1677 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1678 = N_Expression_Function
1679 then
1680 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1681 end if;
1683 when N_Formal_Type_Declaration =>
1684 Match :=
1685 Matching_Actual
1686 (Defining_Identifier (Formal),
1687 Defining_Identifier (Analyzed_Formal));
1689 if No (Match) then
1690 if Partial_Parameterization then
1691 Process_Default (Formal);
1693 else
1694 Error_Msg_Sloc := Sloc (Gen_Unit);
1695 Error_Msg_NE
1696 ("missing actual&",
1697 Instantiation_Node, Defining_Identifier (Formal));
1698 Error_Msg_NE
1699 ("\in instantiation of & declared#",
1700 Instantiation_Node, Gen_Unit);
1701 Abandon_Instantiation (Instantiation_Node);
1702 end if;
1704 else
1705 Analyze (Match);
1706 Append_List
1707 (Instantiate_Type
1708 (Formal, Match, Analyzed_Formal, Assoc_List),
1709 Assoc_List);
1711 -- Warn when an actual is a fixed-point with user-
1712 -- defined promitives. The warning is superfluous
1713 -- if the fornal is private, because there can be
1714 -- no arithmetic operations in the generic so there
1715 -- no danger of confusion.
1717 if Is_Fixed_Point_Type (Entity (Match))
1718 and then not Is_Private_Type
1719 (Defining_Identifier (Analyzed_Formal))
1720 then
1721 Check_Fixed_Point_Actual (Match);
1722 end if;
1724 -- An instantiation is a freeze point for the actuals,
1725 -- unless this is a rewritten formal package, or the
1726 -- formal is an Ada 2012 formal incomplete type.
1728 if Nkind (I_Node) = N_Formal_Package_Declaration
1729 or else
1730 (Ada_Version >= Ada_2012
1731 and then
1732 Ekind (Defining_Identifier (Analyzed_Formal)) =
1733 E_Incomplete_Type)
1734 then
1735 null;
1737 else
1738 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1739 end if;
1740 end if;
1742 -- A remote access-to-class-wide type is not a legal actual
1743 -- for a generic formal of an access type (E.2.2(17/2)).
1744 -- In GNAT an exception to this rule is introduced when
1745 -- the formal is marked as remote using implementation
1746 -- defined aspect/pragma Remote_Access_Type. In that case
1747 -- the actual must be remote as well.
1749 -- If the current instantiation is the construction of a
1750 -- local copy for a formal package the actuals may be
1751 -- defaulted, and there is no matching actual to check.
1753 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1754 and then
1755 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1756 N_Access_To_Object_Definition
1757 and then Present (Match)
1758 then
1759 declare
1760 Formal_Ent : constant Entity_Id :=
1761 Defining_Identifier (Analyzed_Formal);
1762 begin
1763 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1764 = Is_Remote_Types (Formal_Ent)
1765 then
1766 -- Remoteness of formal and actual match
1768 null;
1770 elsif Is_Remote_Types (Formal_Ent) then
1772 -- Remote formal, non-remote actual
1774 Error_Msg_NE
1775 ("actual for& must be remote", Match, Formal_Ent);
1777 else
1778 -- Non-remote formal, remote actual
1780 Error_Msg_NE
1781 ("actual for& may not be remote",
1782 Match, Formal_Ent);
1783 end if;
1784 end;
1785 end if;
1787 when N_Formal_Subprogram_Declaration =>
1788 Match :=
1789 Matching_Actual
1790 (Defining_Unit_Name (Specification (Formal)),
1791 Defining_Unit_Name (Specification (Analyzed_Formal)));
1793 -- If the formal subprogram has the same name as another
1794 -- formal subprogram of the generic, then a named
1795 -- association is illegal (12.3(9)). Exclude named
1796 -- associations that are generated for a nested instance.
1798 if Present (Match)
1799 and then Is_Named_Assoc
1800 and then Comes_From_Source (Found_Assoc)
1801 then
1802 Check_Overloaded_Formal_Subprogram (Formal);
1803 end if;
1805 -- If there is no corresponding actual, this may be case
1806 -- of partial parameterization, or else the formal has a
1807 -- default or a box.
1809 if No (Match) and then Partial_Parameterization then
1810 Process_Default (Formal);
1812 if Nkind (I_Node) = N_Formal_Package_Declaration then
1813 Check_Overloaded_Formal_Subprogram (Formal);
1814 end if;
1816 else
1817 Append_To (Assoc_List,
1818 Instantiate_Formal_Subprogram
1819 (Formal, Match, Analyzed_Formal));
1821 -- An instantiation is a freeze point for the actuals,
1822 -- unless this is a rewritten formal package.
1824 if Nkind (I_Node) /= N_Formal_Package_Declaration
1825 and then Nkind (Match) = N_Identifier
1826 and then Is_Subprogram (Entity (Match))
1828 -- The actual subprogram may rename a routine defined
1829 -- in Standard. Avoid freezing such renamings because
1830 -- subprograms coming from Standard cannot be frozen.
1832 and then
1833 not Renames_Standard_Subprogram (Entity (Match))
1835 -- If the actual subprogram comes from a different
1836 -- unit, it is already frozen, either by a body in
1837 -- that unit or by the end of the declarative part
1838 -- of the unit. This check avoids the freezing of
1839 -- subprograms defined in Standard which are used
1840 -- as generic actuals.
1842 and then In_Same_Code_Unit (Entity (Match), I_Node)
1843 and then Has_Fully_Defined_Profile (Entity (Match))
1844 then
1845 -- Mark the subprogram as having a delayed freeze
1846 -- since this may be an out-of-order action.
1848 Set_Has_Delayed_Freeze (Entity (Match));
1849 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1850 end if;
1851 end if;
1853 -- If this is a nested generic, preserve default for later
1854 -- instantiations. We do this as well for GNATProve use,
1855 -- so that the list of generic associations is complete.
1857 if No (Match) and then Box_Present (Formal) then
1858 declare
1859 Subp : constant Entity_Id :=
1860 Defining_Unit_Name
1861 (Specification (Last (Assoc_List)));
1863 begin
1864 Append_To (Default_Actuals,
1865 Make_Generic_Association (Sloc (I_Node),
1866 Selector_Name =>
1867 New_Occurrence_Of (Subp, Sloc (I_Node)),
1868 Explicit_Generic_Actual_Parameter =>
1869 New_Occurrence_Of (Subp, Sloc (I_Node))));
1870 end;
1871 end if;
1873 when N_Formal_Package_Declaration =>
1874 Match :=
1875 Matching_Actual
1876 (Defining_Identifier (Formal),
1877 Defining_Identifier (Original_Node (Analyzed_Formal)));
1879 if No (Match) then
1880 if Partial_Parameterization then
1881 Process_Default (Formal);
1883 else
1884 Error_Msg_Sloc := Sloc (Gen_Unit);
1885 Error_Msg_NE
1886 ("missing actual&",
1887 Instantiation_Node, Defining_Identifier (Formal));
1888 Error_Msg_NE
1889 ("\in instantiation of & declared#",
1890 Instantiation_Node, Gen_Unit);
1892 Abandon_Instantiation (Instantiation_Node);
1893 end if;
1895 else
1896 Analyze (Match);
1897 Append_List
1898 (Instantiate_Formal_Package
1899 (Formal, Match, Analyzed_Formal),
1900 Assoc_List);
1902 -- Determine whether the actual package needs an explicit
1903 -- freeze node. This is only the case if the actual is
1904 -- declared in the same unit and has a body. Normally
1905 -- packages do not have explicit freeze nodes, and gigi
1906 -- only uses them to elaborate entities in a package
1907 -- body.
1909 Explicit_Freeze_Check : declare
1910 Actual : constant Entity_Id := Entity (Match);
1911 Gen_Par : Entity_Id;
1913 Needs_Freezing : Boolean;
1914 S : Entity_Id;
1916 procedure Check_Generic_Parent;
1917 -- The actual may be an instantiation of a unit
1918 -- declared in a previous instantiation. If that
1919 -- one is also in the current compilation, it must
1920 -- itself be frozen before the actual. The actual
1921 -- may be an instantiation of a generic child unit,
1922 -- in which case the same applies to the instance
1923 -- of the parent which must be frozen before the
1924 -- actual.
1925 -- Should this itself be recursive ???
1927 --------------------------
1928 -- Check_Generic_Parent --
1929 --------------------------
1931 procedure Check_Generic_Parent is
1932 Inst : constant Node_Id :=
1933 Next (Unit_Declaration_Node (Actual));
1934 Par : Entity_Id;
1936 begin
1937 Par := Empty;
1939 if Nkind (Parent (Actual)) = N_Package_Specification
1940 then
1941 Par := Scope (Generic_Parent (Parent (Actual)));
1943 if Is_Generic_Instance (Par) then
1944 null;
1946 -- If the actual is a child generic unit, check
1947 -- whether the instantiation of the parent is
1948 -- also local and must also be frozen now. We
1949 -- must retrieve the instance node to locate the
1950 -- parent instance if any.
1952 elsif Ekind (Par) = E_Generic_Package
1953 and then Is_Child_Unit (Gen_Par)
1954 and then Ekind (Scope (Gen_Par)) =
1955 E_Generic_Package
1956 then
1957 if Nkind (Inst) = N_Package_Instantiation
1958 and then Nkind (Name (Inst)) =
1959 N_Expanded_Name
1960 then
1961 -- Retrieve entity of parent instance
1963 Par := Entity (Prefix (Name (Inst)));
1964 end if;
1966 else
1967 Par := Empty;
1968 end if;
1969 end if;
1971 if Present (Par)
1972 and then Is_Generic_Instance (Par)
1973 and then Scope (Par) = Current_Scope
1974 and then
1975 (No (Freeze_Node (Par))
1976 or else
1977 not Is_List_Member (Freeze_Node (Par)))
1978 then
1979 Set_Has_Delayed_Freeze (Par);
1980 Append_Elmt (Par, Actuals_To_Freeze);
1981 end if;
1982 end Check_Generic_Parent;
1984 -- Start of processing for Explicit_Freeze_Check
1986 begin
1987 if Present (Renamed_Entity (Actual)) then
1988 Gen_Par :=
1989 Generic_Parent (Specification
1990 (Unit_Declaration_Node
1991 (Renamed_Entity (Actual))));
1992 else
1993 Gen_Par :=
1994 Generic_Parent (Specification
1995 (Unit_Declaration_Node (Actual)));
1996 end if;
1998 if not Expander_Active
1999 or else not Has_Completion (Actual)
2000 or else not In_Same_Source_Unit (I_Node, Actual)
2001 or else Is_Frozen (Actual)
2002 or else
2003 (Present (Renamed_Entity (Actual))
2004 and then
2005 not In_Same_Source_Unit
2006 (I_Node, (Renamed_Entity (Actual))))
2007 then
2008 null;
2010 else
2011 -- Finally we want to exclude such freeze nodes
2012 -- from statement sequences, which freeze
2013 -- everything before them.
2014 -- Is this strictly necessary ???
2016 Needs_Freezing := True;
2018 S := Current_Scope;
2019 while Present (S) loop
2020 if Ekind_In (S, E_Block,
2021 E_Function,
2022 E_Loop,
2023 E_Procedure)
2024 then
2025 Needs_Freezing := False;
2026 exit;
2027 end if;
2029 S := Scope (S);
2030 end loop;
2032 if Needs_Freezing then
2033 Check_Generic_Parent;
2035 -- If the actual is a renaming of a proper
2036 -- instance of the formal package, indicate
2037 -- that it is the instance that must be frozen.
2039 if Nkind (Parent (Actual)) =
2040 N_Package_Renaming_Declaration
2041 then
2042 Set_Has_Delayed_Freeze
2043 (Renamed_Entity (Actual));
2044 Append_Elmt
2045 (Renamed_Entity (Actual),
2046 Actuals_To_Freeze);
2047 else
2048 Set_Has_Delayed_Freeze (Actual);
2049 Append_Elmt (Actual, Actuals_To_Freeze);
2050 end if;
2051 end if;
2052 end if;
2053 end Explicit_Freeze_Check;
2054 end if;
2056 -- For use type and use package appearing in the generic part,
2057 -- we have already copied them, so we can just move them where
2058 -- they belong (we mustn't recopy them since this would mess up
2059 -- the Sloc values).
2061 when N_Use_Package_Clause
2062 | N_Use_Type_Clause
2064 if Nkind (Original_Node (I_Node)) =
2065 N_Formal_Package_Declaration
2066 then
2067 Append (New_Copy_Tree (Formal), Assoc_List);
2068 else
2069 Remove (Formal);
2070 Append (Formal, Assoc_List);
2071 end if;
2073 when others =>
2074 raise Program_Error;
2075 end case;
2077 Formal := Saved_Formal;
2078 Next_Non_Pragma (Analyzed_Formal);
2079 end loop;
2081 if Num_Actuals > Num_Matched then
2082 Error_Msg_Sloc := Sloc (Gen_Unit);
2084 if Present (Selector_Name (Actual)) then
2085 Error_Msg_NE
2086 ("unmatched actual &", Actual, Selector_Name (Actual));
2087 Error_Msg_NE
2088 ("\in instantiation of & declared#", Actual, Gen_Unit);
2089 else
2090 Error_Msg_NE
2091 ("unmatched actual in instantiation of & declared#",
2092 Actual, Gen_Unit);
2093 end if;
2094 end if;
2096 elsif Present (Actuals) then
2097 Error_Msg_N
2098 ("too many actuals in generic instantiation", Instantiation_Node);
2099 end if;
2101 -- An instantiation freezes all generic actuals. The only exceptions
2102 -- to this are incomplete types and subprograms which are not fully
2103 -- defined at the point of instantiation.
2105 declare
2106 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2107 begin
2108 while Present (Elmt) loop
2109 Freeze_Before (I_Node, Node (Elmt));
2110 Next_Elmt (Elmt);
2111 end loop;
2112 end;
2114 -- If there are default subprograms, normalize the tree by adding
2115 -- explicit associations for them. This is required if the instance
2116 -- appears within a generic.
2118 if not Is_Empty_List (Default_Actuals) then
2119 declare
2120 Default : Node_Id;
2122 begin
2123 Default := First (Default_Actuals);
2124 while Present (Default) loop
2125 Mark_Rewrite_Insertion (Default);
2126 Next (Default);
2127 end loop;
2129 if No (Actuals) then
2130 Set_Generic_Associations (I_Node, Default_Actuals);
2131 else
2132 Append_List_To (Actuals, Default_Actuals);
2133 end if;
2134 end;
2135 end if;
2137 -- If this is a formal package, normalize the parameter list by adding
2138 -- explicit box associations for the formals that are covered by an
2139 -- Others_Choice.
2141 if not Is_Empty_List (Default_Formals) then
2142 Append_List (Default_Formals, Formals);
2143 end if;
2145 return Assoc_List;
2146 end Analyze_Associations;
2148 -------------------------------
2149 -- Analyze_Formal_Array_Type --
2150 -------------------------------
2152 procedure Analyze_Formal_Array_Type
2153 (T : in out Entity_Id;
2154 Def : Node_Id)
2156 DSS : Node_Id;
2158 begin
2159 -- Treated like a non-generic array declaration, with additional
2160 -- semantic checks.
2162 Enter_Name (T);
2164 if Nkind (Def) = N_Constrained_Array_Definition then
2165 DSS := First (Discrete_Subtype_Definitions (Def));
2166 while Present (DSS) loop
2167 if Nkind_In (DSS, N_Subtype_Indication,
2168 N_Range,
2169 N_Attribute_Reference)
2170 then
2171 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2172 end if;
2174 Next (DSS);
2175 end loop;
2176 end if;
2178 Array_Type_Declaration (T, Def);
2179 Set_Is_Generic_Type (Base_Type (T));
2181 if Ekind (Component_Type (T)) = E_Incomplete_Type
2182 and then No (Full_View (Component_Type (T)))
2183 then
2184 Error_Msg_N ("premature usage of incomplete type", Def);
2186 -- Check that range constraint is not allowed on the component type
2187 -- of a generic formal array type (AARM 12.5.3(3))
2189 elsif Is_Internal (Component_Type (T))
2190 and then Present (Subtype_Indication (Component_Definition (Def)))
2191 and then Nkind (Original_Node
2192 (Subtype_Indication (Component_Definition (Def)))) =
2193 N_Subtype_Indication
2194 then
2195 Error_Msg_N
2196 ("in a formal, a subtype indication can only be "
2197 & "a subtype mark (RM 12.5.3(3))",
2198 Subtype_Indication (Component_Definition (Def)));
2199 end if;
2201 end Analyze_Formal_Array_Type;
2203 ---------------------------------------------
2204 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2205 ---------------------------------------------
2207 -- As for other generic types, we create a valid type representation with
2208 -- legal but arbitrary attributes, whose values are never considered
2209 -- static. For all scalar types we introduce an anonymous base type, with
2210 -- the same attributes. We choose the corresponding integer type to be
2211 -- Standard_Integer.
2212 -- Here and in other similar routines, the Sloc of the generated internal
2213 -- type must be the same as the sloc of the defining identifier of the
2214 -- formal type declaration, to provide proper source navigation.
2216 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2217 (T : Entity_Id;
2218 Def : Node_Id)
2220 Loc : constant Source_Ptr := Sloc (Def);
2222 Base : constant Entity_Id :=
2223 New_Internal_Entity
2224 (E_Decimal_Fixed_Point_Type,
2225 Current_Scope,
2226 Sloc (Defining_Identifier (Parent (Def))), 'G');
2228 Int_Base : constant Entity_Id := Standard_Integer;
2229 Delta_Val : constant Ureal := Ureal_1;
2230 Digs_Val : constant Uint := Uint_6;
2232 function Make_Dummy_Bound return Node_Id;
2233 -- Return a properly typed universal real literal to use as a bound
2235 ----------------------
2236 -- Make_Dummy_Bound --
2237 ----------------------
2239 function Make_Dummy_Bound return Node_Id is
2240 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2241 begin
2242 Set_Etype (Bound, Universal_Real);
2243 return Bound;
2244 end Make_Dummy_Bound;
2246 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2248 begin
2249 Enter_Name (T);
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, Int_Base);
2253 Set_RM_Size (Base, RM_Size (Int_Base));
2254 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2255 Set_Digits_Value (Base, Digs_Val);
2256 Set_Delta_Value (Base, Delta_Val);
2257 Set_Small_Value (Base, Delta_Val);
2258 Set_Scalar_Range (Base,
2259 Make_Range (Loc,
2260 Low_Bound => Make_Dummy_Bound,
2261 High_Bound => Make_Dummy_Bound));
2263 Set_Is_Generic_Type (Base);
2264 Set_Parent (Base, Parent (Def));
2266 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2267 Set_Etype (T, Base);
2268 Set_Size_Info (T, Int_Base);
2269 Set_RM_Size (T, RM_Size (Int_Base));
2270 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2271 Set_Digits_Value (T, Digs_Val);
2272 Set_Delta_Value (T, Delta_Val);
2273 Set_Small_Value (T, Delta_Val);
2274 Set_Scalar_Range (T, Scalar_Range (Base));
2275 Set_Is_Constrained (T);
2277 Check_Restriction (No_Fixed_Point, Def);
2278 end Analyze_Formal_Decimal_Fixed_Point_Type;
2280 -------------------------------------------
2281 -- Analyze_Formal_Derived_Interface_Type --
2282 -------------------------------------------
2284 procedure Analyze_Formal_Derived_Interface_Type
2285 (N : Node_Id;
2286 T : Entity_Id;
2287 Def : Node_Id)
2289 Loc : constant Source_Ptr := Sloc (Def);
2291 begin
2292 -- Rewrite as a type declaration of a derived type. This ensures that
2293 -- the interface list and primitive operations are properly captured.
2295 Rewrite (N,
2296 Make_Full_Type_Declaration (Loc,
2297 Defining_Identifier => T,
2298 Type_Definition => Def));
2299 Analyze (N);
2300 Set_Is_Generic_Type (T);
2301 end Analyze_Formal_Derived_Interface_Type;
2303 ---------------------------------
2304 -- Analyze_Formal_Derived_Type --
2305 ---------------------------------
2307 procedure Analyze_Formal_Derived_Type
2308 (N : Node_Id;
2309 T : Entity_Id;
2310 Def : Node_Id)
2312 Loc : constant Source_Ptr := Sloc (Def);
2313 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2314 New_N : Node_Id;
2316 begin
2317 Set_Is_Generic_Type (T);
2319 if Private_Present (Def) then
2320 New_N :=
2321 Make_Private_Extension_Declaration (Loc,
2322 Defining_Identifier => T,
2323 Discriminant_Specifications => Discriminant_Specifications (N),
2324 Unknown_Discriminants_Present => Unk_Disc,
2325 Subtype_Indication => Subtype_Mark (Def),
2326 Interface_List => Interface_List (Def));
2328 Set_Abstract_Present (New_N, Abstract_Present (Def));
2329 Set_Limited_Present (New_N, Limited_Present (Def));
2330 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2332 else
2333 New_N :=
2334 Make_Full_Type_Declaration (Loc,
2335 Defining_Identifier => T,
2336 Discriminant_Specifications =>
2337 Discriminant_Specifications (Parent (T)),
2338 Type_Definition =>
2339 Make_Derived_Type_Definition (Loc,
2340 Subtype_Indication => Subtype_Mark (Def)));
2342 Set_Abstract_Present
2343 (Type_Definition (New_N), Abstract_Present (Def));
2344 Set_Limited_Present
2345 (Type_Definition (New_N), Limited_Present (Def));
2346 end if;
2348 Rewrite (N, New_N);
2349 Analyze (N);
2351 if Unk_Disc then
2352 if not Is_Composite_Type (T) then
2353 Error_Msg_N
2354 ("unknown discriminants not allowed for elementary types", N);
2355 else
2356 Set_Has_Unknown_Discriminants (T);
2357 Set_Is_Constrained (T, False);
2358 end if;
2359 end if;
2361 -- If the parent type has a known size, so does the formal, which makes
2362 -- legal representation clauses that involve the formal.
2364 Set_Size_Known_At_Compile_Time
2365 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2366 end Analyze_Formal_Derived_Type;
2368 ----------------------------------
2369 -- Analyze_Formal_Discrete_Type --
2370 ----------------------------------
2372 -- The operations defined for a discrete types are those of an enumeration
2373 -- type. The size is set to an arbitrary value, for use in analyzing the
2374 -- generic unit.
2376 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2377 Loc : constant Source_Ptr := Sloc (Def);
2378 Lo : Node_Id;
2379 Hi : Node_Id;
2381 Base : constant Entity_Id :=
2382 New_Internal_Entity
2383 (E_Floating_Point_Type, Current_Scope,
2384 Sloc (Defining_Identifier (Parent (Def))), 'G');
2386 begin
2387 Enter_Name (T);
2388 Set_Ekind (T, E_Enumeration_Subtype);
2389 Set_Etype (T, Base);
2390 Init_Size (T, 8);
2391 Init_Alignment (T);
2392 Set_Is_Generic_Type (T);
2393 Set_Is_Constrained (T);
2395 -- For semantic analysis, the bounds of the type must be set to some
2396 -- non-static value. The simplest is to create attribute nodes for those
2397 -- bounds, that refer to the type itself. These bounds are never
2398 -- analyzed but serve as place-holders.
2400 Lo :=
2401 Make_Attribute_Reference (Loc,
2402 Attribute_Name => Name_First,
2403 Prefix => New_Occurrence_Of (T, Loc));
2404 Set_Etype (Lo, T);
2406 Hi :=
2407 Make_Attribute_Reference (Loc,
2408 Attribute_Name => Name_Last,
2409 Prefix => New_Occurrence_Of (T, Loc));
2410 Set_Etype (Hi, T);
2412 Set_Scalar_Range (T,
2413 Make_Range (Loc,
2414 Low_Bound => Lo,
2415 High_Bound => Hi));
2417 Set_Ekind (Base, E_Enumeration_Type);
2418 Set_Etype (Base, Base);
2419 Init_Size (Base, 8);
2420 Init_Alignment (Base);
2421 Set_Is_Generic_Type (Base);
2422 Set_Scalar_Range (Base, Scalar_Range (T));
2423 Set_Parent (Base, Parent (Def));
2424 end Analyze_Formal_Discrete_Type;
2426 ----------------------------------
2427 -- Analyze_Formal_Floating_Type --
2428 ---------------------------------
2430 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2431 Base : constant Entity_Id :=
2432 New_Internal_Entity
2433 (E_Floating_Point_Type, Current_Scope,
2434 Sloc (Defining_Identifier (Parent (Def))), 'G');
2436 begin
2437 -- The various semantic attributes are taken from the predefined type
2438 -- Float, just so that all of them are initialized. Their values are
2439 -- never used because no constant folding or expansion takes place in
2440 -- the generic itself.
2442 Enter_Name (T);
2443 Set_Ekind (T, E_Floating_Point_Subtype);
2444 Set_Etype (T, Base);
2445 Set_Size_Info (T, (Standard_Float));
2446 Set_RM_Size (T, RM_Size (Standard_Float));
2447 Set_Digits_Value (T, Digits_Value (Standard_Float));
2448 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2449 Set_Is_Constrained (T);
2451 Set_Is_Generic_Type (Base);
2452 Set_Etype (Base, Base);
2453 Set_Size_Info (Base, (Standard_Float));
2454 Set_RM_Size (Base, RM_Size (Standard_Float));
2455 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2456 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2457 Set_Parent (Base, Parent (Def));
2459 Check_Restriction (No_Floating_Point, Def);
2460 end Analyze_Formal_Floating_Type;
2462 -----------------------------------
2463 -- Analyze_Formal_Interface_Type;--
2464 -----------------------------------
2466 procedure Analyze_Formal_Interface_Type
2467 (N : Node_Id;
2468 T : Entity_Id;
2469 Def : Node_Id)
2471 Loc : constant Source_Ptr := Sloc (N);
2472 New_N : Node_Id;
2474 begin
2475 New_N :=
2476 Make_Full_Type_Declaration (Loc,
2477 Defining_Identifier => T,
2478 Type_Definition => Def);
2480 Rewrite (N, New_N);
2481 Analyze (N);
2482 Set_Is_Generic_Type (T);
2483 end Analyze_Formal_Interface_Type;
2485 ---------------------------------
2486 -- Analyze_Formal_Modular_Type --
2487 ---------------------------------
2489 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2490 begin
2491 -- Apart from their entity kind, generic modular types are treated like
2492 -- signed integer types, and have the same attributes.
2494 Analyze_Formal_Signed_Integer_Type (T, Def);
2495 Set_Ekind (T, E_Modular_Integer_Subtype);
2496 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2498 end Analyze_Formal_Modular_Type;
2500 ---------------------------------------
2501 -- Analyze_Formal_Object_Declaration --
2502 ---------------------------------------
2504 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2505 E : constant Node_Id := Default_Expression (N);
2506 Id : constant Node_Id := Defining_Identifier (N);
2507 K : Entity_Kind;
2508 T : Node_Id;
2510 begin
2511 Enter_Name (Id);
2513 -- Determine the mode of the formal object
2515 if Out_Present (N) then
2516 K := E_Generic_In_Out_Parameter;
2518 if not In_Present (N) then
2519 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2520 end if;
2522 else
2523 K := E_Generic_In_Parameter;
2524 end if;
2526 if Present (Subtype_Mark (N)) then
2527 Find_Type (Subtype_Mark (N));
2528 T := Entity (Subtype_Mark (N));
2530 -- Verify that there is no redundant null exclusion
2532 if Null_Exclusion_Present (N) then
2533 if not Is_Access_Type (T) then
2534 Error_Msg_N
2535 ("null exclusion can only apply to an access type", N);
2537 elsif Can_Never_Be_Null (T) then
2538 Error_Msg_NE
2539 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2540 end if;
2541 end if;
2543 -- Ada 2005 (AI-423): Formal object with an access definition
2545 else
2546 Check_Access_Definition (N);
2547 T := Access_Definition
2548 (Related_Nod => N,
2549 N => Access_Definition (N));
2550 end if;
2552 if Ekind (T) = E_Incomplete_Type then
2553 declare
2554 Error_Node : Node_Id;
2556 begin
2557 if Present (Subtype_Mark (N)) then
2558 Error_Node := Subtype_Mark (N);
2559 else
2560 Check_Access_Definition (N);
2561 Error_Node := Access_Definition (N);
2562 end if;
2564 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2565 end;
2566 end if;
2568 if K = E_Generic_In_Parameter then
2570 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2572 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2573 Error_Msg_N
2574 ("generic formal of mode IN must not be of limited type", N);
2575 Explain_Limited_Type (T, N);
2576 end if;
2578 if Is_Abstract_Type (T) then
2579 Error_Msg_N
2580 ("generic formal of mode IN must not be of abstract type", N);
2581 end if;
2583 if Present (E) then
2584 Preanalyze_Spec_Expression (E, T);
2586 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2587 Error_Msg_N
2588 ("initialization not allowed for limited types", E);
2589 Explain_Limited_Type (T, E);
2590 end if;
2591 end if;
2593 Set_Ekind (Id, K);
2594 Set_Etype (Id, T);
2596 -- Case of generic IN OUT parameter
2598 else
2599 -- If the formal has an unconstrained type, construct its actual
2600 -- subtype, as is done for subprogram formals. In this fashion, all
2601 -- its uses can refer to specific bounds.
2603 Set_Ekind (Id, K);
2604 Set_Etype (Id, T);
2606 if (Is_Array_Type (T) and then not Is_Constrained (T))
2607 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2608 then
2609 declare
2610 Non_Freezing_Ref : constant Node_Id :=
2611 New_Occurrence_Of (Id, Sloc (Id));
2612 Decl : Node_Id;
2614 begin
2615 -- Make sure the actual subtype doesn't generate bogus freezing
2617 Set_Must_Not_Freeze (Non_Freezing_Ref);
2618 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2619 Insert_Before_And_Analyze (N, Decl);
2620 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2621 end;
2622 else
2623 Set_Actual_Subtype (Id, T);
2624 end if;
2626 if Present (E) then
2627 Error_Msg_N
2628 ("initialization not allowed for `IN OUT` formals", N);
2629 end if;
2630 end if;
2632 if Has_Aspects (N) then
2633 Analyze_Aspect_Specifications (N, Id);
2634 end if;
2635 end Analyze_Formal_Object_Declaration;
2637 ----------------------------------------------
2638 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2639 ----------------------------------------------
2641 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2642 (T : Entity_Id;
2643 Def : Node_Id)
2645 Loc : constant Source_Ptr := Sloc (Def);
2646 Base : constant Entity_Id :=
2647 New_Internal_Entity
2648 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2649 Sloc (Defining_Identifier (Parent (Def))), 'G');
2651 begin
2652 -- The semantic attributes are set for completeness only, their values
2653 -- will never be used, since all properties of the type are non-static.
2655 Enter_Name (T);
2656 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2657 Set_Etype (T, Base);
2658 Set_Size_Info (T, Standard_Integer);
2659 Set_RM_Size (T, RM_Size (Standard_Integer));
2660 Set_Small_Value (T, Ureal_1);
2661 Set_Delta_Value (T, Ureal_1);
2662 Set_Scalar_Range (T,
2663 Make_Range (Loc,
2664 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2665 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2666 Set_Is_Constrained (T);
2668 Set_Is_Generic_Type (Base);
2669 Set_Etype (Base, Base);
2670 Set_Size_Info (Base, Standard_Integer);
2671 Set_RM_Size (Base, RM_Size (Standard_Integer));
2672 Set_Small_Value (Base, Ureal_1);
2673 Set_Delta_Value (Base, Ureal_1);
2674 Set_Scalar_Range (Base, Scalar_Range (T));
2675 Set_Parent (Base, Parent (Def));
2677 Check_Restriction (No_Fixed_Point, Def);
2678 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2680 ----------------------------------------
2681 -- Analyze_Formal_Package_Declaration --
2682 ----------------------------------------
2684 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2685 Gen_Id : constant Node_Id := Name (N);
2686 Loc : constant Source_Ptr := Sloc (N);
2687 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2688 Formal : Entity_Id;
2689 Gen_Decl : Node_Id;
2690 Gen_Unit : Entity_Id;
2691 Renaming : Node_Id;
2693 Vis_Prims_List : Elist_Id := No_Elist;
2694 -- List of primitives made temporarily visible in the instantiation
2695 -- to match the visibility of the formal type.
2697 function Build_Local_Package return Node_Id;
2698 -- The formal package is rewritten so that its parameters are replaced
2699 -- with corresponding declarations. For parameters with bona fide
2700 -- associations these declarations are created by Analyze_Associations
2701 -- as for a regular instantiation. For boxed parameters, we preserve
2702 -- the formal declarations and analyze them, in order to introduce
2703 -- entities of the right kind in the environment of the formal.
2705 -------------------------
2706 -- Build_Local_Package --
2707 -------------------------
2709 function Build_Local_Package return Node_Id is
2710 Decls : List_Id;
2711 Pack_Decl : Node_Id;
2713 begin
2714 -- Within the formal, the name of the generic package is a renaming
2715 -- of the formal (as for a regular instantiation).
2717 Pack_Decl :=
2718 Make_Package_Declaration (Loc,
2719 Specification =>
2720 Copy_Generic_Node
2721 (Specification (Original_Node (Gen_Decl)),
2722 Empty, Instantiating => True));
2724 Renaming :=
2725 Make_Package_Renaming_Declaration (Loc,
2726 Defining_Unit_Name =>
2727 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2728 Name => New_Occurrence_Of (Formal, Loc));
2730 if Nkind (Gen_Id) = N_Identifier
2731 and then Chars (Gen_Id) = Chars (Pack_Id)
2732 then
2733 Error_Msg_NE
2734 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2735 end if;
2737 -- If the formal is declared with a box, or with an others choice,
2738 -- create corresponding declarations for all entities in the formal
2739 -- part, so that names with the proper types are available in the
2740 -- specification of the formal package.
2742 -- On the other hand, if there are no associations, then all the
2743 -- formals must have defaults, and this will be checked by the
2744 -- call to Analyze_Associations.
2746 if Box_Present (N)
2747 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2748 then
2749 declare
2750 Formal_Decl : Node_Id;
2752 begin
2753 -- TBA : for a formal package, need to recurse ???
2755 Decls := New_List;
2756 Formal_Decl :=
2757 First
2758 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2759 while Present (Formal_Decl) loop
2760 Append_To
2761 (Decls,
2762 Copy_Generic_Node
2763 (Formal_Decl, Empty, Instantiating => True));
2764 Next (Formal_Decl);
2765 end loop;
2766 end;
2768 -- If generic associations are present, use Analyze_Associations to
2769 -- create the proper renaming declarations.
2771 else
2772 declare
2773 Act_Tree : constant Node_Id :=
2774 Copy_Generic_Node
2775 (Original_Node (Gen_Decl), Empty,
2776 Instantiating => True);
2778 begin
2779 Generic_Renamings.Set_Last (0);
2780 Generic_Renamings_HTable.Reset;
2781 Instantiation_Node := N;
2783 Decls :=
2784 Analyze_Associations
2785 (I_Node => Original_Node (N),
2786 Formals => Generic_Formal_Declarations (Act_Tree),
2787 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2789 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2790 end;
2791 end if;
2793 Append (Renaming, To => Decls);
2795 -- Add generated declarations ahead of local declarations in
2796 -- the package.
2798 if No (Visible_Declarations (Specification (Pack_Decl))) then
2799 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2800 else
2801 Insert_List_Before
2802 (First (Visible_Declarations (Specification (Pack_Decl))),
2803 Decls);
2804 end if;
2806 return Pack_Decl;
2807 end Build_Local_Package;
2809 -- Local variables
2811 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2812 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2814 Associations : Boolean := True;
2815 New_N : Node_Id;
2816 Parent_Installed : Boolean := False;
2817 Parent_Instance : Entity_Id;
2818 Renaming_In_Par : Entity_Id;
2820 -- Start of processing for Analyze_Formal_Package_Declaration
2822 begin
2823 Check_Text_IO_Special_Unit (Gen_Id);
2825 Init_Env;
2826 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2827 Gen_Unit := Entity (Gen_Id);
2829 -- Check for a formal package that is a package renaming
2831 if Present (Renamed_Object (Gen_Unit)) then
2833 -- Indicate that unit is used, before replacing it with renamed
2834 -- entity for use below.
2836 if In_Extended_Main_Source_Unit (N) then
2837 Set_Is_Instantiated (Gen_Unit);
2838 Generate_Reference (Gen_Unit, N);
2839 end if;
2841 Gen_Unit := Renamed_Object (Gen_Unit);
2842 end if;
2844 if Ekind (Gen_Unit) /= E_Generic_Package then
2845 Error_Msg_N ("expect generic package name", Gen_Id);
2846 Restore_Env;
2847 goto Leave;
2849 elsif Gen_Unit = Current_Scope then
2850 Error_Msg_N
2851 ("generic package cannot be used as a formal package of itself",
2852 Gen_Id);
2853 Restore_Env;
2854 goto Leave;
2856 elsif In_Open_Scopes (Gen_Unit) then
2857 if Is_Compilation_Unit (Gen_Unit)
2858 and then Is_Child_Unit (Current_Scope)
2859 then
2860 -- Special-case the error when the formal is a parent, and
2861 -- continue analysis to minimize cascaded errors.
2863 Error_Msg_N
2864 ("generic parent cannot be used as formal package of a child "
2865 & "unit", Gen_Id);
2867 else
2868 Error_Msg_N
2869 ("generic package cannot be used as a formal package within "
2870 & "itself", Gen_Id);
2871 Restore_Env;
2872 goto Leave;
2873 end if;
2874 end if;
2876 -- Check that name of formal package does not hide name of generic,
2877 -- or its leading prefix. This check must be done separately because
2878 -- the name of the generic has already been analyzed.
2880 declare
2881 Gen_Name : Entity_Id;
2883 begin
2884 Gen_Name := Gen_Id;
2885 while Nkind (Gen_Name) = N_Expanded_Name loop
2886 Gen_Name := Prefix (Gen_Name);
2887 end loop;
2889 if Chars (Gen_Name) = Chars (Pack_Id) then
2890 Error_Msg_NE
2891 ("& is hidden within declaration of formal package",
2892 Gen_Id, Gen_Name);
2893 end if;
2894 end;
2896 if Box_Present (N)
2897 or else No (Generic_Associations (N))
2898 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2899 then
2900 Associations := False;
2901 end if;
2903 -- If there are no generic associations, the generic parameters appear
2904 -- as local entities and are instantiated like them. We copy the generic
2905 -- package declaration as if it were an instantiation, and analyze it
2906 -- like a regular package, except that we treat the formals as
2907 -- additional visible components.
2909 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2911 if In_Extended_Main_Source_Unit (N) then
2912 Set_Is_Instantiated (Gen_Unit);
2913 Generate_Reference (Gen_Unit, N);
2914 end if;
2916 Formal := New_Copy (Pack_Id);
2917 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2919 -- Make local generic without formals. The formals will be replaced with
2920 -- internal declarations.
2922 begin
2923 New_N := Build_Local_Package;
2925 -- If there are errors in the parameter list, Analyze_Associations
2926 -- raises Instantiation_Error. Patch the declaration to prevent further
2927 -- exception propagation.
2929 exception
2930 when Instantiation_Error =>
2931 Enter_Name (Formal);
2932 Set_Ekind (Formal, E_Variable);
2933 Set_Etype (Formal, Any_Type);
2934 Restore_Hidden_Primitives (Vis_Prims_List);
2936 if Parent_Installed then
2937 Remove_Parent;
2938 end if;
2940 goto Leave;
2941 end;
2943 Rewrite (N, New_N);
2944 Set_Defining_Unit_Name (Specification (New_N), Formal);
2945 Set_Generic_Parent (Specification (N), Gen_Unit);
2946 Set_Instance_Env (Gen_Unit, Formal);
2947 Set_Is_Generic_Instance (Formal);
2949 Enter_Name (Formal);
2950 Set_Ekind (Formal, E_Package);
2951 Set_Etype (Formal, Standard_Void_Type);
2952 Set_Inner_Instances (Formal, New_Elmt_List);
2953 Push_Scope (Formal);
2955 -- Manually set the SPARK_Mode from the context because the package
2956 -- declaration is never analyzed.
2958 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2959 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2960 Set_SPARK_Pragma_Inherited (Formal);
2961 Set_SPARK_Aux_Pragma_Inherited (Formal);
2963 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2965 -- Similarly, we have to make the name of the formal visible in the
2966 -- parent instance, to resolve properly fully qualified names that
2967 -- may appear in the generic unit. The parent instance has been
2968 -- placed on the scope stack ahead of the current scope.
2970 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2972 Renaming_In_Par :=
2973 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2974 Set_Ekind (Renaming_In_Par, E_Package);
2975 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2976 Set_Scope (Renaming_In_Par, Parent_Instance);
2977 Set_Parent (Renaming_In_Par, Parent (Formal));
2978 Set_Renamed_Object (Renaming_In_Par, Formal);
2979 Append_Entity (Renaming_In_Par, Parent_Instance);
2980 end if;
2982 -- A formal package declaration behaves as a package instantiation with
2983 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2984 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2985 -- all SPARK_Mode pragmas within the generic_package_name.
2987 if SPARK_Mode /= On then
2988 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2990 -- Mark the formal spec in case the body is instantiated at a later
2991 -- pass. This preserves the original context in effect for the body.
2993 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2994 end if;
2996 Analyze (Specification (N));
2998 -- The formals for which associations are provided are not visible
2999 -- outside of the formal package. The others are still declared by a
3000 -- formal parameter declaration.
3002 -- If there are no associations, the only local entity to hide is the
3003 -- generated package renaming itself.
3005 declare
3006 E : Entity_Id;
3008 begin
3009 E := First_Entity (Formal);
3010 while Present (E) loop
3011 if Associations and then not Is_Generic_Formal (E) then
3012 Set_Is_Hidden (E);
3013 end if;
3015 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3016 Set_Is_Hidden (E);
3017 exit;
3018 end if;
3020 Next_Entity (E);
3021 end loop;
3022 end;
3024 End_Package_Scope (Formal);
3025 Restore_Hidden_Primitives (Vis_Prims_List);
3027 if Parent_Installed then
3028 Remove_Parent;
3029 end if;
3031 Restore_Env;
3033 -- Inside the generic unit, the formal package is a regular package, but
3034 -- no body is needed for it. Note that after instantiation, the defining
3035 -- unit name we need is in the new tree and not in the original (see
3036 -- Package_Instantiation). A generic formal package is an instance, and
3037 -- can be used as an actual for an inner instance.
3039 Set_Has_Completion (Formal, True);
3041 -- Add semantic information to the original defining identifier for ASIS
3042 -- use.
3044 Set_Ekind (Pack_Id, E_Package);
3045 Set_Etype (Pack_Id, Standard_Void_Type);
3046 Set_Scope (Pack_Id, Scope (Formal));
3047 Set_Has_Completion (Pack_Id, True);
3049 <<Leave>>
3050 if Has_Aspects (N) then
3051 Analyze_Aspect_Specifications (N, Pack_Id);
3052 end if;
3054 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3055 end Analyze_Formal_Package_Declaration;
3057 ---------------------------------
3058 -- Analyze_Formal_Private_Type --
3059 ---------------------------------
3061 procedure Analyze_Formal_Private_Type
3062 (N : Node_Id;
3063 T : Entity_Id;
3064 Def : Node_Id)
3066 begin
3067 New_Private_Type (N, T, Def);
3069 -- Set the size to an arbitrary but legal value
3071 Set_Size_Info (T, Standard_Integer);
3072 Set_RM_Size (T, RM_Size (Standard_Integer));
3073 end Analyze_Formal_Private_Type;
3075 ------------------------------------
3076 -- Analyze_Formal_Incomplete_Type --
3077 ------------------------------------
3079 procedure Analyze_Formal_Incomplete_Type
3080 (T : Entity_Id;
3081 Def : Node_Id)
3083 begin
3084 Enter_Name (T);
3085 Set_Ekind (T, E_Incomplete_Type);
3086 Set_Etype (T, T);
3087 Set_Private_Dependents (T, New_Elmt_List);
3089 if Tagged_Present (Def) then
3090 Set_Is_Tagged_Type (T);
3091 Make_Class_Wide_Type (T);
3092 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3093 end if;
3094 end Analyze_Formal_Incomplete_Type;
3096 ----------------------------------------
3097 -- Analyze_Formal_Signed_Integer_Type --
3098 ----------------------------------------
3100 procedure Analyze_Formal_Signed_Integer_Type
3101 (T : Entity_Id;
3102 Def : Node_Id)
3104 Base : constant Entity_Id :=
3105 New_Internal_Entity
3106 (E_Signed_Integer_Type,
3107 Current_Scope,
3108 Sloc (Defining_Identifier (Parent (Def))), 'G');
3110 begin
3111 Enter_Name (T);
3113 Set_Ekind (T, E_Signed_Integer_Subtype);
3114 Set_Etype (T, Base);
3115 Set_Size_Info (T, Standard_Integer);
3116 Set_RM_Size (T, RM_Size (Standard_Integer));
3117 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3118 Set_Is_Constrained (T);
3120 Set_Is_Generic_Type (Base);
3121 Set_Size_Info (Base, Standard_Integer);
3122 Set_RM_Size (Base, RM_Size (Standard_Integer));
3123 Set_Etype (Base, Base);
3124 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3125 Set_Parent (Base, Parent (Def));
3126 end Analyze_Formal_Signed_Integer_Type;
3128 -------------------------------------------
3129 -- Analyze_Formal_Subprogram_Declaration --
3130 -------------------------------------------
3132 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3133 Spec : constant Node_Id := Specification (N);
3134 Def : constant Node_Id := Default_Name (N);
3135 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3136 Subp : Entity_Id;
3138 begin
3139 if Nam = Error then
3140 return;
3141 end if;
3143 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3144 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3145 goto Leave;
3146 end if;
3148 Analyze_Subprogram_Declaration (N);
3149 Set_Is_Formal_Subprogram (Nam);
3150 Set_Has_Completion (Nam);
3152 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3153 Set_Is_Abstract_Subprogram (Nam);
3155 Set_Is_Dispatching_Operation (Nam);
3157 -- A formal abstract procedure cannot have a null default
3158 -- (RM 12.6(4.1/2)).
3160 if Nkind (Spec) = N_Procedure_Specification
3161 and then Null_Present (Spec)
3162 then
3163 Error_Msg_N
3164 ("a formal abstract subprogram cannot default to null", Spec);
3165 end if;
3167 declare
3168 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3169 begin
3170 if No (Ctrl_Type) then
3171 Error_Msg_N
3172 ("abstract formal subprogram must have a controlling type",
3175 elsif Ada_Version >= Ada_2012
3176 and then Is_Incomplete_Type (Ctrl_Type)
3177 then
3178 Error_Msg_NE
3179 ("controlling type of abstract formal subprogram cannot "
3180 & "be incomplete type", N, Ctrl_Type);
3182 else
3183 Check_Controlling_Formals (Ctrl_Type, Nam);
3184 end if;
3185 end;
3186 end if;
3188 -- Default name is resolved at the point of instantiation
3190 if Box_Present (N) then
3191 null;
3193 -- Else default is bound at the point of generic declaration
3195 elsif Present (Def) then
3196 if Nkind (Def) = N_Operator_Symbol then
3197 Find_Direct_Name (Def);
3199 elsif Nkind (Def) /= N_Attribute_Reference then
3200 Analyze (Def);
3202 else
3203 -- For an attribute reference, analyze the prefix and verify
3204 -- that it has the proper profile for the subprogram.
3206 Analyze (Prefix (Def));
3207 Valid_Default_Attribute (Nam, Def);
3208 goto Leave;
3209 end if;
3211 -- Default name may be overloaded, in which case the interpretation
3212 -- with the correct profile must be selected, as for a renaming.
3213 -- If the definition is an indexed component, it must denote a
3214 -- member of an entry family. If it is a selected component, it
3215 -- can be a protected operation.
3217 if Etype (Def) = Any_Type then
3218 goto Leave;
3220 elsif Nkind (Def) = N_Selected_Component then
3221 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3222 Error_Msg_N ("expect valid subprogram name as default", Def);
3223 end if;
3225 elsif Nkind (Def) = N_Indexed_Component then
3226 if Is_Entity_Name (Prefix (Def)) then
3227 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3228 Error_Msg_N ("expect valid subprogram name as default", Def);
3229 end if;
3231 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3232 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3233 E_Entry_Family
3234 then
3235 Error_Msg_N ("expect valid subprogram name as default", Def);
3236 end if;
3238 else
3239 Error_Msg_N ("expect valid subprogram name as default", Def);
3240 goto Leave;
3241 end if;
3243 elsif Nkind (Def) = N_Character_Literal then
3245 -- Needs some type checks: subprogram should be parameterless???
3247 Resolve (Def, (Etype (Nam)));
3249 elsif not Is_Entity_Name (Def)
3250 or else not Is_Overloadable (Entity (Def))
3251 then
3252 Error_Msg_N ("expect valid subprogram name as default", Def);
3253 goto Leave;
3255 elsif not Is_Overloaded (Def) then
3256 Subp := Entity (Def);
3258 if Subp = Nam then
3259 Error_Msg_N ("premature usage of formal subprogram", Def);
3261 elsif not Entity_Matches_Spec (Subp, Nam) then
3262 Error_Msg_N ("no visible entity matches specification", Def);
3263 end if;
3265 -- More than one interpretation, so disambiguate as for a renaming
3267 else
3268 declare
3269 I : Interp_Index;
3270 I1 : Interp_Index := 0;
3271 It : Interp;
3272 It1 : Interp;
3274 begin
3275 Subp := Any_Id;
3276 Get_First_Interp (Def, I, It);
3277 while Present (It.Nam) loop
3278 if Entity_Matches_Spec (It.Nam, Nam) then
3279 if Subp /= Any_Id then
3280 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3282 if It1 = No_Interp then
3283 Error_Msg_N ("ambiguous default subprogram", Def);
3284 else
3285 Subp := It1.Nam;
3286 end if;
3288 exit;
3290 else
3291 I1 := I;
3292 Subp := It.Nam;
3293 end if;
3294 end if;
3296 Get_Next_Interp (I, It);
3297 end loop;
3298 end;
3300 if Subp /= Any_Id then
3302 -- Subprogram found, generate reference to it
3304 Set_Entity (Def, Subp);
3305 Generate_Reference (Subp, Def);
3307 if Subp = Nam then
3308 Error_Msg_N ("premature usage of formal subprogram", Def);
3310 elsif Ekind (Subp) /= E_Operator then
3311 Check_Mode_Conformant (Subp, Nam);
3312 end if;
3314 else
3315 Error_Msg_N ("no visible subprogram matches specification", N);
3316 end if;
3317 end if;
3318 end if;
3320 <<Leave>>
3321 if Has_Aspects (N) then
3322 Analyze_Aspect_Specifications (N, Nam);
3323 end if;
3325 end Analyze_Formal_Subprogram_Declaration;
3327 -------------------------------------
3328 -- Analyze_Formal_Type_Declaration --
3329 -------------------------------------
3331 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3332 Def : constant Node_Id := Formal_Type_Definition (N);
3333 T : Entity_Id;
3335 begin
3336 T := Defining_Identifier (N);
3338 if Present (Discriminant_Specifications (N))
3339 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3340 then
3341 Error_Msg_N
3342 ("discriminants not allowed for this formal type", T);
3343 end if;
3345 -- Enter the new name, and branch to specific routine
3347 case Nkind (Def) is
3348 when N_Formal_Private_Type_Definition =>
3349 Analyze_Formal_Private_Type (N, T, Def);
3351 when N_Formal_Derived_Type_Definition =>
3352 Analyze_Formal_Derived_Type (N, T, Def);
3354 when N_Formal_Incomplete_Type_Definition =>
3355 Analyze_Formal_Incomplete_Type (T, Def);
3357 when N_Formal_Discrete_Type_Definition =>
3358 Analyze_Formal_Discrete_Type (T, Def);
3360 when N_Formal_Signed_Integer_Type_Definition =>
3361 Analyze_Formal_Signed_Integer_Type (T, Def);
3363 when N_Formal_Modular_Type_Definition =>
3364 Analyze_Formal_Modular_Type (T, Def);
3366 when N_Formal_Floating_Point_Definition =>
3367 Analyze_Formal_Floating_Type (T, Def);
3369 when N_Formal_Ordinary_Fixed_Point_Definition =>
3370 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3372 when N_Formal_Decimal_Fixed_Point_Definition =>
3373 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3375 when N_Array_Type_Definition =>
3376 Analyze_Formal_Array_Type (T, Def);
3378 when N_Access_Function_Definition
3379 | N_Access_Procedure_Definition
3380 | N_Access_To_Object_Definition
3382 Analyze_Generic_Access_Type (T, Def);
3384 -- Ada 2005: a interface declaration is encoded as an abstract
3385 -- record declaration or a abstract type derivation.
3387 when N_Record_Definition =>
3388 Analyze_Formal_Interface_Type (N, T, Def);
3390 when N_Derived_Type_Definition =>
3391 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3393 when N_Error =>
3394 null;
3396 when others =>
3397 raise Program_Error;
3398 end case;
3400 Set_Is_Generic_Type (T);
3402 if Has_Aspects (N) then
3403 Analyze_Aspect_Specifications (N, T);
3404 end if;
3405 end Analyze_Formal_Type_Declaration;
3407 ------------------------------------
3408 -- Analyze_Function_Instantiation --
3409 ------------------------------------
3411 procedure Analyze_Function_Instantiation (N : Node_Id) is
3412 begin
3413 Analyze_Subprogram_Instantiation (N, E_Function);
3414 end Analyze_Function_Instantiation;
3416 ---------------------------------
3417 -- Analyze_Generic_Access_Type --
3418 ---------------------------------
3420 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3421 begin
3422 Enter_Name (T);
3424 if Nkind (Def) = N_Access_To_Object_Definition then
3425 Access_Type_Declaration (T, Def);
3427 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3428 and then No (Full_View (Designated_Type (T)))
3429 and then not Is_Generic_Type (Designated_Type (T))
3430 then
3431 Error_Msg_N ("premature usage of incomplete type", Def);
3433 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3434 Error_Msg_N
3435 ("only a subtype mark is allowed in a formal", Def);
3436 end if;
3438 else
3439 Access_Subprogram_Declaration (T, Def);
3440 end if;
3441 end Analyze_Generic_Access_Type;
3443 ---------------------------------
3444 -- Analyze_Generic_Formal_Part --
3445 ---------------------------------
3447 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3448 Gen_Parm_Decl : Node_Id;
3450 begin
3451 -- The generic formals are processed in the scope of the generic unit,
3452 -- where they are immediately visible. The scope is installed by the
3453 -- caller.
3455 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3456 while Present (Gen_Parm_Decl) loop
3457 Analyze (Gen_Parm_Decl);
3458 Next (Gen_Parm_Decl);
3459 end loop;
3461 Generate_Reference_To_Generic_Formals (Current_Scope);
3462 end Analyze_Generic_Formal_Part;
3464 ------------------------------------------
3465 -- Analyze_Generic_Package_Declaration --
3466 ------------------------------------------
3468 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3469 Decls : constant List_Id := Visible_Declarations (Specification (N));
3470 Loc : constant Source_Ptr := Sloc (N);
3472 Decl : Node_Id;
3473 Id : Entity_Id;
3474 New_N : Node_Id;
3475 Renaming : Node_Id;
3476 Save_Parent : Node_Id;
3478 begin
3479 Check_SPARK_05_Restriction ("generic is not allowed", N);
3481 -- We introduce a renaming of the enclosing package, to have a usable
3482 -- entity as the prefix of an expanded name for a local entity of the
3483 -- form Par.P.Q, where P is the generic package. This is because a local
3484 -- entity named P may hide it, so that the usual visibility rules in
3485 -- the instance will not resolve properly.
3487 Renaming :=
3488 Make_Package_Renaming_Declaration (Loc,
3489 Defining_Unit_Name =>
3490 Make_Defining_Identifier (Loc,
3491 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3492 Name =>
3493 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3495 -- The declaration is inserted before other declarations, but before
3496 -- pragmas that may be library-unit pragmas and must appear before other
3497 -- declarations. The pragma Compile_Time_Error is not in this class, and
3498 -- may contain an expression that includes such a qualified name, so the
3499 -- renaming declaration must appear before it.
3501 -- Are there other pragmas that require this special handling ???
3503 if Present (Decls) then
3504 Decl := First (Decls);
3505 while Present (Decl)
3506 and then Nkind (Decl) = N_Pragma
3507 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3508 loop
3509 Next (Decl);
3510 end loop;
3512 if Present (Decl) then
3513 Insert_Before (Decl, Renaming);
3514 else
3515 Append (Renaming, Visible_Declarations (Specification (N)));
3516 end if;
3518 else
3519 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3520 end if;
3522 -- Create copy of generic unit, and save for instantiation. If the unit
3523 -- is a child unit, do not copy the specifications for the parent, which
3524 -- are not part of the generic tree.
3526 Save_Parent := Parent_Spec (N);
3527 Set_Parent_Spec (N, Empty);
3529 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3530 Set_Parent_Spec (New_N, Save_Parent);
3531 Rewrite (N, New_N);
3533 -- Once the contents of the generic copy and the template are swapped,
3534 -- do the same for their respective aspect specifications.
3536 Exchange_Aspects (N, New_N);
3538 -- Collect all contract-related source pragmas found within the template
3539 -- and attach them to the contract of the package spec. This contract is
3540 -- used in the capture of global references within annotations.
3542 Create_Generic_Contract (N);
3544 Id := Defining_Entity (N);
3545 Generate_Definition (Id);
3547 -- Expansion is not applied to generic units
3549 Start_Generic;
3551 Enter_Name (Id);
3552 Set_Ekind (Id, E_Generic_Package);
3553 Set_Etype (Id, Standard_Void_Type);
3555 -- Set SPARK_Mode from context
3557 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3558 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3559 Set_SPARK_Pragma_Inherited (Id);
3560 Set_SPARK_Aux_Pragma_Inherited (Id);
3562 -- Preserve relevant elaboration-related attributes of the context which
3563 -- are no longer available or very expensive to recompute once analysis,
3564 -- resolution, and expansion are over.
3566 Mark_Elaboration_Attributes
3567 (N_Id => Id,
3568 Checks => True,
3569 Warnings => True);
3571 -- Analyze aspects now, so that generated pragmas appear in the
3572 -- declarations before building and analyzing the generic copy.
3574 if Has_Aspects (N) then
3575 Analyze_Aspect_Specifications (N, Id);
3576 end if;
3578 Push_Scope (Id);
3579 Enter_Generic_Scope (Id);
3580 Set_Inner_Instances (Id, New_Elmt_List);
3582 Set_Categorization_From_Pragmas (N);
3583 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3585 -- Link the declaration of the generic homonym in the generic copy to
3586 -- the package it renames, so that it is always resolved properly.
3588 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3589 Set_Entity (Associated_Node (Name (Renaming)), Id);
3591 -- For a library unit, we have reconstructed the entity for the unit,
3592 -- and must reset it in the library tables.
3594 if Nkind (Parent (N)) = N_Compilation_Unit then
3595 Set_Cunit_Entity (Current_Sem_Unit, Id);
3596 end if;
3598 Analyze_Generic_Formal_Part (N);
3600 -- After processing the generic formals, analysis proceeds as for a
3601 -- non-generic package.
3603 Analyze (Specification (N));
3605 Validate_Categorization_Dependency (N, Id);
3607 End_Generic;
3609 End_Package_Scope (Id);
3610 Exit_Generic_Scope (Id);
3612 -- If the generic appears within a package unit, the body of that unit
3613 -- has to be present for instantiation and inlining.
3615 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3616 Set_Body_Needed_For_Inlining
3617 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3618 end if;
3620 if Nkind (Parent (N)) /= N_Compilation_Unit then
3621 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3622 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3623 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3625 else
3626 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3627 Validate_RT_RAT_Component (N);
3629 -- If this is a spec without a body, check that generic parameters
3630 -- are referenced.
3632 if not Body_Required (Parent (N)) then
3633 Check_References (Id);
3634 end if;
3635 end if;
3637 -- If there is a specified storage pool in the context, create an
3638 -- aspect on the package declaration, so that it is used in any
3639 -- instance that does not override it.
3641 if Present (Default_Pool) then
3642 declare
3643 ASN : Node_Id;
3645 begin
3646 ASN :=
3647 Make_Aspect_Specification (Loc,
3648 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3649 Expression => New_Copy (Default_Pool));
3651 if No (Aspect_Specifications (Specification (N))) then
3652 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3653 else
3654 Append (ASN, Aspect_Specifications (Specification (N)));
3655 end if;
3656 end;
3657 end if;
3658 end Analyze_Generic_Package_Declaration;
3660 --------------------------------------------
3661 -- Analyze_Generic_Subprogram_Declaration --
3662 --------------------------------------------
3664 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3665 Formals : List_Id;
3666 Id : Entity_Id;
3667 New_N : Node_Id;
3668 Result_Type : Entity_Id;
3669 Save_Parent : Node_Id;
3670 Spec : Node_Id;
3671 Typ : Entity_Id;
3673 begin
3674 Check_SPARK_05_Restriction ("generic is not allowed", N);
3676 -- Create copy of generic unit, and save for instantiation. If the unit
3677 -- is a child unit, do not copy the specifications for the parent, which
3678 -- are not part of the generic tree.
3680 Save_Parent := Parent_Spec (N);
3681 Set_Parent_Spec (N, Empty);
3683 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3684 Set_Parent_Spec (New_N, Save_Parent);
3685 Rewrite (N, New_N);
3687 -- Once the contents of the generic copy and the template are swapped,
3688 -- do the same for their respective aspect specifications.
3690 Exchange_Aspects (N, New_N);
3692 -- Collect all contract-related source pragmas found within the template
3693 -- and attach them to the contract of the subprogram spec. This contract
3694 -- is used in the capture of global references within annotations.
3696 Create_Generic_Contract (N);
3698 Spec := Specification (N);
3699 Id := Defining_Entity (Spec);
3700 Generate_Definition (Id);
3702 if Nkind (Id) = N_Defining_Operator_Symbol then
3703 Error_Msg_N
3704 ("operator symbol not allowed for generic subprogram", Id);
3705 end if;
3707 Start_Generic;
3709 Enter_Name (Id);
3710 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3712 -- Analyze the aspects of the generic copy to ensure that all generated
3713 -- pragmas (if any) perform their semantic effects.
3715 if Has_Aspects (N) then
3716 Analyze_Aspect_Specifications (N, Id);
3717 end if;
3719 Push_Scope (Id);
3720 Enter_Generic_Scope (Id);
3721 Set_Inner_Instances (Id, New_Elmt_List);
3722 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3724 Analyze_Generic_Formal_Part (N);
3726 if Nkind (Spec) = N_Function_Specification then
3727 Set_Ekind (Id, E_Generic_Function);
3728 else
3729 Set_Ekind (Id, E_Generic_Procedure);
3730 end if;
3732 -- Set SPARK_Mode from context
3734 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3735 Set_SPARK_Pragma_Inherited (Id);
3737 -- Preserve relevant elaboration-related attributes of the context which
3738 -- are no longer available or very expensive to recompute once analysis,
3739 -- resolution, and expansion are over.
3741 Mark_Elaboration_Attributes
3742 (N_Id => Id,
3743 Checks => True,
3744 Warnings => True);
3746 Formals := Parameter_Specifications (Spec);
3748 if Present (Formals) then
3749 Process_Formals (Formals, Spec);
3750 end if;
3752 if Nkind (Spec) = N_Function_Specification then
3753 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3754 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3755 Set_Etype (Id, Result_Type);
3757 -- Check restriction imposed by AI05-073: a generic function
3758 -- cannot return an abstract type or an access to such.
3760 -- This is a binding interpretation should it apply to earlier
3761 -- versions of Ada as well as Ada 2012???
3763 if Is_Abstract_Type (Designated_Type (Result_Type))
3764 and then Ada_Version >= Ada_2012
3765 then
3766 Error_Msg_N
3767 ("generic function cannot have an access result "
3768 & "that designates an abstract type", Spec);
3769 end if;
3771 else
3772 Find_Type (Result_Definition (Spec));
3773 Typ := Entity (Result_Definition (Spec));
3775 if Is_Abstract_Type (Typ)
3776 and then Ada_Version >= Ada_2012
3777 then
3778 Error_Msg_N
3779 ("generic function cannot have abstract result type", Spec);
3780 end if;
3782 -- If a null exclusion is imposed on the result type, then create
3783 -- a null-excluding itype (an access subtype) and use it as the
3784 -- function's Etype.
3786 if Is_Access_Type (Typ)
3787 and then Null_Exclusion_Present (Spec)
3788 then
3789 Set_Etype (Id,
3790 Create_Null_Excluding_Itype
3791 (T => Typ,
3792 Related_Nod => Spec,
3793 Scope_Id => Defining_Unit_Name (Spec)));
3794 else
3795 Set_Etype (Id, Typ);
3796 end if;
3797 end if;
3799 else
3800 Set_Etype (Id, Standard_Void_Type);
3801 end if;
3803 -- For a library unit, we have reconstructed the entity for the unit,
3804 -- and must reset it in the library tables. We also make sure that
3805 -- Body_Required is set properly in the original compilation unit node.
3807 if Nkind (Parent (N)) = N_Compilation_Unit then
3808 Set_Cunit_Entity (Current_Sem_Unit, Id);
3809 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3810 end if;
3812 -- If the generic appears within a package unit, the body of that unit
3813 -- has to be present for instantiation and inlining.
3815 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3816 and then Unit_Requires_Body (Id)
3817 then
3818 Set_Body_Needed_For_Inlining
3819 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3820 end if;
3822 Set_Categorization_From_Pragmas (N);
3823 Validate_Categorization_Dependency (N, Id);
3825 -- Capture all global references that occur within the profile of the
3826 -- generic subprogram. Aspects are not part of this processing because
3827 -- they must be delayed. If processed now, Save_Global_References will
3828 -- destroy the Associated_Node links and prevent the capture of global
3829 -- references when the contract of the generic subprogram is analyzed.
3831 Save_Global_References (Original_Node (N));
3833 End_Generic;
3834 End_Scope;
3835 Exit_Generic_Scope (Id);
3836 Generate_Reference_To_Formals (Id);
3838 List_Inherited_Pre_Post_Aspects (Id);
3839 end Analyze_Generic_Subprogram_Declaration;
3841 -----------------------------------
3842 -- Analyze_Package_Instantiation --
3843 -----------------------------------
3845 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3846 -- must be replaced by gotos which jump to the end of the routine in order
3847 -- to restore the Ghost and SPARK modes.
3849 procedure Analyze_Package_Instantiation (N : Node_Id) is
3850 Has_Inline_Always : Boolean := False;
3852 procedure Delay_Descriptors (E : Entity_Id);
3853 -- Delay generation of subprogram descriptors for given entity
3855 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3856 -- If inlining is active and the generic contains inlined subprograms,
3857 -- we instantiate the body. This may cause superfluous instantiations,
3858 -- but it is simpler than detecting the need for the body at the point
3859 -- of inlining, when the context of the instance is not available.
3861 -----------------------
3862 -- Delay_Descriptors --
3863 -----------------------
3865 procedure Delay_Descriptors (E : Entity_Id) is
3866 begin
3867 if not Delay_Subprogram_Descriptors (E) then
3868 Set_Delay_Subprogram_Descriptors (E);
3869 Pending_Descriptor.Append (E);
3870 end if;
3871 end Delay_Descriptors;
3873 -----------------------
3874 -- Might_Inline_Subp --
3875 -----------------------
3877 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3878 E : Entity_Id;
3880 begin
3881 if not Inline_Processing_Required then
3882 return False;
3884 else
3885 E := First_Entity (Gen_Unit);
3886 while Present (E) loop
3887 if Is_Subprogram (E) and then Is_Inlined (E) then
3888 -- Remember if there are any subprograms with Inline_Always
3890 if Has_Pragma_Inline_Always (E) then
3891 Has_Inline_Always := True;
3892 end if;
3894 return True;
3895 end if;
3897 Next_Entity (E);
3898 end loop;
3899 end if;
3901 return False;
3902 end Might_Inline_Subp;
3904 -- Local declarations
3906 Gen_Id : constant Node_Id := Name (N);
3907 Is_Actual_Pack : constant Boolean :=
3908 Is_Internal (Defining_Entity (N));
3909 Loc : constant Source_Ptr := Sloc (N);
3911 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3912 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3913 Saved_ISMP : constant Boolean :=
3914 Ignore_SPARK_Mode_Pragmas_In_Instance;
3915 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3916 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3917 -- Save the Ghost and SPARK mode-related data to restore on exit
3919 Saved_Style_Check : constant Boolean := Style_Check;
3920 -- Save style check mode for restore on exit
3922 Act_Decl : Node_Id;
3923 Act_Decl_Name : Node_Id;
3924 Act_Decl_Id : Entity_Id;
3925 Act_Spec : Node_Id;
3926 Act_Tree : Node_Id;
3927 Env_Installed : Boolean := False;
3928 Gen_Decl : Node_Id;
3929 Gen_Spec : Node_Id;
3930 Gen_Unit : Entity_Id;
3931 Inline_Now : Boolean := False;
3932 Needs_Body : Boolean;
3933 Parent_Installed : Boolean := False;
3934 Renaming_List : List_Id;
3935 Unit_Renaming : Node_Id;
3937 Vis_Prims_List : Elist_Id := No_Elist;
3938 -- List of primitives made temporarily visible in the instantiation
3939 -- to match the visibility of the formal type
3941 -- Start of processing for Analyze_Package_Instantiation
3943 begin
3944 -- Preserve relevant elaboration-related attributes of the context which
3945 -- are no longer available or very expensive to recompute once analysis,
3946 -- resolution, and expansion are over.
3948 Mark_Elaboration_Attributes
3949 (N_Id => N,
3950 Checks => True,
3951 Level => True,
3952 Modes => True,
3953 Warnings => True);
3955 Check_SPARK_05_Restriction ("generic is not allowed", N);
3957 -- Very first thing: check for Text_IO special unit in case we are
3958 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3960 Check_Text_IO_Special_Unit (Name (N));
3962 -- Make node global for error reporting
3964 Instantiation_Node := N;
3966 -- Case of instantiation of a generic package
3968 if Nkind (N) = N_Package_Instantiation then
3969 Act_Decl_Id := New_Copy (Defining_Entity (N));
3970 Set_Comes_From_Source (Act_Decl_Id, True);
3972 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3973 Act_Decl_Name :=
3974 Make_Defining_Program_Unit_Name (Loc,
3975 Name =>
3976 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3977 Defining_Identifier => Act_Decl_Id);
3978 else
3979 Act_Decl_Name := Act_Decl_Id;
3980 end if;
3982 -- Case of instantiation of a formal package
3984 else
3985 Act_Decl_Id := Defining_Identifier (N);
3986 Act_Decl_Name := Act_Decl_Id;
3987 end if;
3989 Generate_Definition (Act_Decl_Id);
3990 Set_Ekind (Act_Decl_Id, E_Package);
3992 -- Initialize list of incomplete actuals before analysis
3994 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3996 Preanalyze_Actuals (N, Act_Decl_Id);
3998 -- Turn off style checking in instances. If the check is enabled on the
3999 -- generic unit, a warning in an instance would just be noise. If not
4000 -- enabled on the generic, then a warning in an instance is just wrong.
4001 -- This must be done after analyzing the actuals, which do come from
4002 -- source and are subject to style checking.
4004 Style_Check := False;
4006 Init_Env;
4007 Env_Installed := True;
4009 -- Reset renaming map for formal types. The mapping is established
4010 -- when analyzing the generic associations, but some mappings are
4011 -- inherited from formal packages of parent units, and these are
4012 -- constructed when the parents are installed.
4014 Generic_Renamings.Set_Last (0);
4015 Generic_Renamings_HTable.Reset;
4017 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4018 Gen_Unit := Entity (Gen_Id);
4020 -- A package instantiation is Ghost when it is subject to pragma Ghost
4021 -- or the generic template is Ghost. Set the mode now to ensure that
4022 -- any nodes generated during analysis and expansion are marked as
4023 -- Ghost.
4025 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4027 -- Verify that it is the name of a generic package
4029 -- A visibility glitch: if the instance is a child unit and the generic
4030 -- is the generic unit of a parent instance (i.e. both the parent and
4031 -- the child units are instances of the same package) the name now
4032 -- denotes the renaming within the parent, not the intended generic
4033 -- unit. See if there is a homonym that is the desired generic. The
4034 -- renaming declaration must be visible inside the instance of the
4035 -- child, but not when analyzing the name in the instantiation itself.
4037 if Ekind (Gen_Unit) = E_Package
4038 and then Present (Renamed_Entity (Gen_Unit))
4039 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4040 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4041 and then Present (Homonym (Gen_Unit))
4042 then
4043 Gen_Unit := Homonym (Gen_Unit);
4044 end if;
4046 if Etype (Gen_Unit) = Any_Type then
4047 Restore_Env;
4048 goto Leave;
4050 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4052 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4054 if From_Limited_With (Gen_Unit) then
4055 Error_Msg_N
4056 ("cannot instantiate a limited withed package", Gen_Id);
4057 else
4058 Error_Msg_NE
4059 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4060 end if;
4062 Restore_Env;
4063 goto Leave;
4064 end if;
4066 if In_Extended_Main_Source_Unit (N) then
4067 Set_Is_Instantiated (Gen_Unit);
4068 Generate_Reference (Gen_Unit, N);
4070 if Present (Renamed_Object (Gen_Unit)) then
4071 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4072 Generate_Reference (Renamed_Object (Gen_Unit), N);
4073 end if;
4074 end if;
4076 if Nkind (Gen_Id) = N_Identifier
4077 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4078 then
4079 Error_Msg_NE
4080 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4082 elsif Nkind (Gen_Id) = N_Expanded_Name
4083 and then Is_Child_Unit (Gen_Unit)
4084 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4085 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4086 then
4087 Error_Msg_N
4088 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4089 end if;
4091 Set_Entity (Gen_Id, Gen_Unit);
4093 -- If generic is a renaming, get original generic unit
4095 if Present (Renamed_Object (Gen_Unit))
4096 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4097 then
4098 Gen_Unit := Renamed_Object (Gen_Unit);
4099 end if;
4101 -- Verify that there are no circular instantiations
4103 if In_Open_Scopes (Gen_Unit) then
4104 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4105 Restore_Env;
4106 goto Leave;
4108 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4109 Error_Msg_Node_2 := Current_Scope;
4110 Error_Msg_NE
4111 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4112 Circularity_Detected := True;
4113 Restore_Env;
4114 goto Leave;
4116 else
4117 -- If the context of the instance is subject to SPARK_Mode "off" or
4118 -- the annotation is altogether missing, set the global flag which
4119 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4120 -- the instance.
4122 if SPARK_Mode /= On then
4123 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4125 -- Mark the instance spec in case the body is instantiated at a
4126 -- later pass. This preserves the original context in effect for
4127 -- the body.
4129 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4130 end if;
4132 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4133 Gen_Spec := Specification (Gen_Decl);
4135 -- Initialize renamings map, for error checking, and the list that
4136 -- holds private entities whose views have changed between generic
4137 -- definition and instantiation. If this is the instance created to
4138 -- validate an actual package, the instantiation environment is that
4139 -- of the enclosing instance.
4141 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4143 -- Copy original generic tree, to produce text for instantiation
4145 Act_Tree :=
4146 Copy_Generic_Node
4147 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4149 Act_Spec := Specification (Act_Tree);
4151 -- If this is the instance created to validate an actual package,
4152 -- only the formals matter, do not examine the package spec itself.
4154 if Is_Actual_Pack then
4155 Set_Visible_Declarations (Act_Spec, New_List);
4156 Set_Private_Declarations (Act_Spec, New_List);
4157 end if;
4159 Renaming_List :=
4160 Analyze_Associations
4161 (I_Node => N,
4162 Formals => Generic_Formal_Declarations (Act_Tree),
4163 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4165 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4167 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4168 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4169 Set_Is_Generic_Instance (Act_Decl_Id);
4170 Set_Generic_Parent (Act_Spec, Gen_Unit);
4172 -- References to the generic in its own declaration or its body are
4173 -- references to the instance. Add a renaming declaration for the
4174 -- generic unit itself. This declaration, as well as the renaming
4175 -- declarations for the generic formals, must remain private to the
4176 -- unit: the formals, because this is the language semantics, and
4177 -- the unit because its use is an artifact of the implementation.
4179 Unit_Renaming :=
4180 Make_Package_Renaming_Declaration (Loc,
4181 Defining_Unit_Name =>
4182 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4183 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4185 Append (Unit_Renaming, Renaming_List);
4187 -- The renaming declarations are the first local declarations of the
4188 -- new unit.
4190 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4191 Insert_List_Before
4192 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4193 else
4194 Set_Visible_Declarations (Act_Spec, Renaming_List);
4195 end if;
4197 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4199 -- Propagate the aspect specifications from the package declaration
4200 -- template to the instantiated version of the package declaration.
4202 if Has_Aspects (Act_Tree) then
4203 Set_Aspect_Specifications (Act_Decl,
4204 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4205 end if;
4207 -- The generic may have a generated Default_Storage_Pool aspect,
4208 -- set at the point of generic declaration. If the instance has
4209 -- that aspect, it overrides the one inherited from the generic.
4211 if Has_Aspects (Gen_Spec) then
4212 if No (Aspect_Specifications (N)) then
4213 Set_Aspect_Specifications (N,
4214 (New_Copy_List_Tree
4215 (Aspect_Specifications (Gen_Spec))));
4217 else
4218 declare
4219 ASN1, ASN2 : Node_Id;
4221 begin
4222 ASN1 := First (Aspect_Specifications (N));
4223 while Present (ASN1) loop
4224 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
4225 then
4226 -- If generic carries a default storage pool, remove
4227 -- it in favor of the instance one.
4229 ASN2 := First (Aspect_Specifications (Gen_Spec));
4230 while Present (ASN2) loop
4231 if Chars (Identifier (ASN2)) =
4232 Name_Default_Storage_Pool
4233 then
4234 Remove (ASN2);
4235 exit;
4236 end if;
4238 Next (ASN2);
4239 end loop;
4240 end if;
4242 Next (ASN1);
4243 end loop;
4245 Prepend_List_To (Aspect_Specifications (N),
4246 (New_Copy_List_Tree
4247 (Aspect_Specifications (Gen_Spec))));
4248 end;
4249 end if;
4250 end if;
4252 -- Save the instantiation node, for subsequent instantiation of the
4253 -- body, if there is one and we are generating code for the current
4254 -- unit. Mark unit as having a body (avoids premature error message).
4256 -- We instantiate the body if we are generating code, if we are
4257 -- generating cross-reference information, or if we are building
4258 -- trees for ASIS use or GNATprove use.
4260 declare
4261 Enclosing_Body_Present : Boolean := False;
4262 -- If the generic unit is not a compilation unit, then a body may
4263 -- be present in its parent even if none is required. We create a
4264 -- tentative pending instantiation for the body, which will be
4265 -- discarded if none is actually present.
4267 Scop : Entity_Id;
4269 begin
4270 if Scope (Gen_Unit) /= Standard_Standard
4271 and then not Is_Child_Unit (Gen_Unit)
4272 then
4273 Scop := Scope (Gen_Unit);
4274 while Present (Scop) and then Scop /= Standard_Standard loop
4275 if Unit_Requires_Body (Scop) then
4276 Enclosing_Body_Present := True;
4277 exit;
4279 elsif In_Open_Scopes (Scop)
4280 and then In_Package_Body (Scop)
4281 then
4282 Enclosing_Body_Present := True;
4283 exit;
4284 end if;
4286 exit when Is_Compilation_Unit (Scop);
4287 Scop := Scope (Scop);
4288 end loop;
4289 end if;
4291 -- If front-end inlining is enabled or there are any subprograms
4292 -- marked with Inline_Always, and this is a unit for which code
4293 -- will be generated, we instantiate the body at once.
4295 -- This is done if the instance is not the main unit, and if the
4296 -- generic is not a child unit of another generic, to avoid scope
4297 -- problems and the reinstallation of parent instances.
4299 if Expander_Active
4300 and then (not Is_Child_Unit (Gen_Unit)
4301 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4302 and then Might_Inline_Subp (Gen_Unit)
4303 and then not Is_Actual_Pack
4304 then
4305 if not Back_End_Inlining
4306 and then (Front_End_Inlining or else Has_Inline_Always)
4307 and then (Is_In_Main_Unit (N)
4308 or else In_Main_Context (Current_Scope))
4309 and then Nkind (Parent (N)) /= N_Compilation_Unit
4310 then
4311 Inline_Now := True;
4313 -- In configurable_run_time mode we force the inlining of
4314 -- predefined subprograms marked Inline_Always, to minimize
4315 -- the use of the run-time library.
4317 elsif In_Predefined_Unit (Gen_Decl)
4318 and then Configurable_Run_Time_Mode
4319 and then Nkind (Parent (N)) /= N_Compilation_Unit
4320 then
4321 Inline_Now := True;
4322 end if;
4324 -- If the current scope is itself an instance within a child
4325 -- unit, there will be duplications in the scope stack, and the
4326 -- unstacking mechanism in Inline_Instance_Body will fail.
4327 -- This loses some rare cases of optimization, and might be
4328 -- improved some day, if we can find a proper abstraction for
4329 -- "the complete compilation context" that can be saved and
4330 -- restored. ???
4332 if Is_Generic_Instance (Current_Scope) then
4333 declare
4334 Curr_Unit : constant Entity_Id :=
4335 Cunit_Entity (Current_Sem_Unit);
4336 begin
4337 if Curr_Unit /= Current_Scope
4338 and then Is_Child_Unit (Curr_Unit)
4339 then
4340 Inline_Now := False;
4341 end if;
4342 end;
4343 end if;
4344 end if;
4346 Needs_Body :=
4347 (Unit_Requires_Body (Gen_Unit)
4348 or else Enclosing_Body_Present
4349 or else Present (Corresponding_Body (Gen_Decl)))
4350 and then (Is_In_Main_Unit (N)
4351 or else Might_Inline_Subp (Gen_Unit))
4352 and then not Is_Actual_Pack
4353 and then not Inline_Now
4354 and then (Operating_Mode = Generate_Code
4356 -- Need comment for this check ???
4358 or else (Operating_Mode = Check_Semantics
4359 and then (ASIS_Mode or GNATprove_Mode)));
4361 -- If front-end inlining is enabled or there are any subprograms
4362 -- marked with Inline_Always, do not instantiate body when within
4363 -- a generic context.
4365 if ((Front_End_Inlining or else Has_Inline_Always)
4366 and then not Expander_Active)
4367 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4368 then
4369 Needs_Body := False;
4370 end if;
4372 -- If the current context is generic, and the package being
4373 -- instantiated is declared within a formal package, there is no
4374 -- body to instantiate until the enclosing generic is instantiated
4375 -- and there is an actual for the formal package. If the formal
4376 -- package has parameters, we build a regular package instance for
4377 -- it, that precedes the original formal package declaration.
4379 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4380 declare
4381 Decl : constant Node_Id :=
4382 Original_Node
4383 (Unit_Declaration_Node (Scope (Gen_Unit)));
4384 begin
4385 if Nkind (Decl) = N_Formal_Package_Declaration
4386 or else (Nkind (Decl) = N_Package_Declaration
4387 and then Is_List_Member (Decl)
4388 and then Present (Next (Decl))
4389 and then
4390 Nkind (Next (Decl)) =
4391 N_Formal_Package_Declaration)
4392 then
4393 Needs_Body := False;
4394 end if;
4395 end;
4396 end if;
4397 end;
4399 -- For RCI unit calling stubs, we omit the instance body if the
4400 -- instance is the RCI library unit itself.
4402 -- However there is a special case for nested instances: in this case
4403 -- we do generate the instance body, as it might be required, e.g.
4404 -- because it provides stream attributes for some type used in the
4405 -- profile of a remote subprogram. This is consistent with 12.3(12),
4406 -- which indicates that the instance body occurs at the place of the
4407 -- instantiation, and thus is part of the RCI declaration, which is
4408 -- present on all client partitions (this is E.2.3(18)).
4410 -- Note that AI12-0002 may make it illegal at some point to have
4411 -- stream attributes defined in an RCI unit, in which case this
4412 -- special case will become unnecessary. In the meantime, there
4413 -- is known application code in production that depends on this
4414 -- being possible, so we definitely cannot eliminate the body in
4415 -- the case of nested instances for the time being.
4417 -- When we generate a nested instance body, calling stubs for any
4418 -- relevant subprogram will be be inserted immediately after the
4419 -- subprogram declarations, and will take precedence over the
4420 -- subsequent (original) body. (The stub and original body will be
4421 -- complete homographs, but this is permitted in an instance).
4422 -- (Could we do better and remove the original body???)
4424 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4425 and then Comes_From_Source (N)
4426 and then Nkind (Parent (N)) = N_Compilation_Unit
4427 then
4428 Needs_Body := False;
4429 end if;
4431 if Needs_Body then
4433 -- Here is a defence against a ludicrous number of instantiations
4434 -- caused by a circular set of instantiation attempts.
4436 if Pending_Instantiations.Last > Maximum_Instantiations then
4437 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4438 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4439 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4440 raise Unrecoverable_Error;
4441 end if;
4443 -- Indicate that the enclosing scopes contain an instantiation,
4444 -- and that cleanup actions should be delayed until after the
4445 -- instance body is expanded.
4447 Check_Forward_Instantiation (Gen_Decl);
4448 if Nkind (N) = N_Package_Instantiation then
4449 declare
4450 Enclosing_Master : Entity_Id;
4452 begin
4453 -- Loop to search enclosing masters
4455 Enclosing_Master := Current_Scope;
4456 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4457 if Ekind (Enclosing_Master) = E_Package then
4458 if Is_Compilation_Unit (Enclosing_Master) then
4459 if In_Package_Body (Enclosing_Master) then
4460 Delay_Descriptors
4461 (Body_Entity (Enclosing_Master));
4462 else
4463 Delay_Descriptors
4464 (Enclosing_Master);
4465 end if;
4467 exit Scope_Loop;
4469 else
4470 Enclosing_Master := Scope (Enclosing_Master);
4471 end if;
4473 elsif Is_Generic_Unit (Enclosing_Master)
4474 or else Ekind (Enclosing_Master) = E_Void
4475 then
4476 -- Cleanup actions will eventually be performed on the
4477 -- enclosing subprogram or package instance, if any.
4478 -- Enclosing scope is void in the formal part of a
4479 -- generic subprogram.
4481 exit Scope_Loop;
4483 else
4484 if Ekind (Enclosing_Master) = E_Entry
4485 and then
4486 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4487 then
4488 if not Expander_Active then
4489 exit Scope_Loop;
4490 else
4491 Enclosing_Master :=
4492 Protected_Body_Subprogram (Enclosing_Master);
4493 end if;
4494 end if;
4496 Set_Delay_Cleanups (Enclosing_Master);
4498 while Ekind (Enclosing_Master) = E_Block loop
4499 Enclosing_Master := Scope (Enclosing_Master);
4500 end loop;
4502 if Is_Subprogram (Enclosing_Master) then
4503 Delay_Descriptors (Enclosing_Master);
4505 elsif Is_Task_Type (Enclosing_Master) then
4506 declare
4507 TBP : constant Node_Id :=
4508 Get_Task_Body_Procedure
4509 (Enclosing_Master);
4510 begin
4511 if Present (TBP) then
4512 Delay_Descriptors (TBP);
4513 Set_Delay_Cleanups (TBP);
4514 end if;
4515 end;
4516 end if;
4518 exit Scope_Loop;
4519 end if;
4520 end loop Scope_Loop;
4521 end;
4523 -- Make entry in table
4525 Add_Pending_Instantiation (N, Act_Decl);
4526 end if;
4527 end if;
4529 Set_Categorization_From_Pragmas (Act_Decl);
4531 if Parent_Installed then
4532 Hide_Current_Scope;
4533 end if;
4535 Set_Instance_Spec (N, Act_Decl);
4537 -- If not a compilation unit, insert the package declaration before
4538 -- the original instantiation node.
4540 if Nkind (Parent (N)) /= N_Compilation_Unit then
4541 Mark_Rewrite_Insertion (Act_Decl);
4542 Insert_Before (N, Act_Decl);
4544 if Has_Aspects (N) then
4545 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4547 -- The pragma created for a Default_Storage_Pool aspect must
4548 -- appear ahead of the declarations in the instance spec.
4549 -- Analysis has placed it after the instance node, so remove
4550 -- it and reinsert it properly now.
4552 declare
4553 ASN : constant Node_Id := First (Aspect_Specifications (N));
4554 A_Name : constant Name_Id := Chars (Identifier (ASN));
4555 Decl : Node_Id;
4557 begin
4558 if A_Name = Name_Default_Storage_Pool then
4559 if No (Visible_Declarations (Act_Spec)) then
4560 Set_Visible_Declarations (Act_Spec, New_List);
4561 end if;
4563 Decl := Next (N);
4564 while Present (Decl) loop
4565 if Nkind (Decl) = N_Pragma then
4566 Remove (Decl);
4567 Prepend (Decl, Visible_Declarations (Act_Spec));
4568 exit;
4569 end if;
4571 Next (Decl);
4572 end loop;
4573 end if;
4574 end;
4575 end if;
4577 Analyze (Act_Decl);
4579 -- For an instantiation that is a compilation unit, place
4580 -- declaration on current node so context is complete for analysis
4581 -- (including nested instantiations). If this is the main unit,
4582 -- the declaration eventually replaces the instantiation node.
4583 -- If the instance body is created later, it replaces the
4584 -- instance node, and the declaration is attached to it
4585 -- (see Build_Instance_Compilation_Unit_Nodes).
4587 else
4588 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4590 -- The entity for the current unit is the newly created one,
4591 -- and all semantic information is attached to it.
4593 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4595 -- If this is the main unit, replace the main entity as well
4597 if Current_Sem_Unit = Main_Unit then
4598 Main_Unit_Entity := Act_Decl_Id;
4599 end if;
4600 end if;
4602 Set_Unit (Parent (N), Act_Decl);
4603 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4604 Set_Package_Instantiation (Act_Decl_Id, N);
4606 -- Process aspect specifications of the instance node, if any, to
4607 -- take into account categorization pragmas before analyzing the
4608 -- instance.
4610 if Has_Aspects (N) then
4611 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4612 end if;
4614 Analyze (Act_Decl);
4615 Set_Unit (Parent (N), N);
4616 Set_Body_Required (Parent (N), False);
4618 -- We never need elaboration checks on instantiations, since by
4619 -- definition, the body instantiation is elaborated at the same
4620 -- time as the spec instantiation.
4622 if Legacy_Elaboration_Checks then
4623 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4624 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4625 end if;
4626 end if;
4628 if Legacy_Elaboration_Checks then
4629 Check_Elab_Instantiation (N);
4630 end if;
4632 -- Save the scenario for later examination by the ABE Processing
4633 -- phase.
4635 Record_Elaboration_Scenario (N);
4637 -- The instantiation results in a guaranteed ABE
4639 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4641 -- Do not instantiate the corresponding body because gigi cannot
4642 -- handle certain types of premature instantiations.
4644 Pending_Instantiations.Decrement_Last;
4646 -- Create completing bodies for all subprogram declarations since
4647 -- their real bodies will not be instantiated.
4649 Provide_Completing_Bodies (Instance_Spec (N));
4650 end if;
4652 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4654 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4655 First_Private_Entity (Act_Decl_Id));
4657 -- If the instantiation will receive a body, the unit will be
4658 -- transformed into a package body, and receive its own elaboration
4659 -- entity. Otherwise, the nature of the unit is now a package
4660 -- declaration.
4662 if Nkind (Parent (N)) = N_Compilation_Unit
4663 and then not Needs_Body
4664 then
4665 Rewrite (N, Act_Decl);
4666 end if;
4668 if Present (Corresponding_Body (Gen_Decl))
4669 or else Unit_Requires_Body (Gen_Unit)
4670 then
4671 Set_Has_Completion (Act_Decl_Id);
4672 end if;
4674 Check_Formal_Packages (Act_Decl_Id);
4676 Restore_Hidden_Primitives (Vis_Prims_List);
4677 Restore_Private_Views (Act_Decl_Id);
4679 Inherit_Context (Gen_Decl, N);
4681 if Parent_Installed then
4682 Remove_Parent;
4683 end if;
4685 Restore_Env;
4686 Env_Installed := False;
4687 end if;
4689 Validate_Categorization_Dependency (N, Act_Decl_Id);
4691 -- There used to be a check here to prevent instantiations in local
4692 -- contexts if the No_Local_Allocators restriction was active. This
4693 -- check was removed by a binding interpretation in AI-95-00130/07,
4694 -- but we retain the code for documentation purposes.
4696 -- if Ekind (Act_Decl_Id) /= E_Void
4697 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4698 -- then
4699 -- Check_Restriction (No_Local_Allocators, N);
4700 -- end if;
4702 if Inline_Now then
4703 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4704 end if;
4706 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4707 -- be used as defining identifiers for a formal package and for the
4708 -- corresponding expanded package.
4710 if Nkind (N) = N_Formal_Package_Declaration then
4711 Act_Decl_Id := New_Copy (Defining_Entity (N));
4712 Set_Comes_From_Source (Act_Decl_Id, True);
4713 Set_Is_Generic_Instance (Act_Decl_Id, False);
4714 Set_Defining_Identifier (N, Act_Decl_Id);
4715 end if;
4717 -- Check that if N is an instantiation of System.Dim_Float_IO or
4718 -- System.Dim_Integer_IO, the formal type has a dimension system.
4720 if Nkind (N) = N_Package_Instantiation
4721 and then Is_Dim_IO_Package_Instantiation (N)
4722 then
4723 declare
4724 Assoc : constant Node_Id := First (Generic_Associations (N));
4725 begin
4726 if not Has_Dimension_System
4727 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4728 then
4729 Error_Msg_N ("type with a dimension system expected", Assoc);
4730 end if;
4731 end;
4732 end if;
4734 <<Leave>>
4735 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4736 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4737 end if;
4739 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4740 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4741 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4742 Style_Check := Saved_Style_Check;
4744 exception
4745 when Instantiation_Error =>
4746 if Parent_Installed then
4747 Remove_Parent;
4748 end if;
4750 if Env_Installed then
4751 Restore_Env;
4752 end if;
4754 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4755 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4756 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4757 Style_Check := Saved_Style_Check;
4758 end Analyze_Package_Instantiation;
4760 --------------------------
4761 -- Inline_Instance_Body --
4762 --------------------------
4764 -- WARNING: This routine manages SPARK regions. Return statements must be
4765 -- replaced by gotos which jump to the end of the routine and restore the
4766 -- SPARK mode.
4768 procedure Inline_Instance_Body
4769 (N : Node_Id;
4770 Gen_Unit : Entity_Id;
4771 Act_Decl : Node_Id)
4773 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4774 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4775 Gen_Comp : constant Entity_Id :=
4776 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4778 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4779 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4780 -- Save the SPARK mode-related data to restore on exit. Removing
4781 -- enclosing scopes to provide a clean environment for analysis of
4782 -- the inlined body will eliminate any previously set SPARK_Mode.
4784 Scope_Stack_Depth : constant Pos :=
4785 Scope_Stack.Last - Scope_Stack.First + 1;
4787 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4788 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4789 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4791 Curr_Scope : Entity_Id := Empty;
4792 List : Elist_Id := No_Elist; -- init to avoid warning
4793 N_Instances : Nat := 0;
4794 Num_Inner : Nat := 0;
4795 Num_Scopes : Nat := 0;
4796 Removed : Boolean := False;
4797 S : Entity_Id;
4798 Vis : Boolean;
4800 begin
4801 -- Case of generic unit defined in another unit. We must remove the
4802 -- complete context of the current unit to install that of the generic.
4804 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4806 -- Add some comments for the following two loops ???
4808 S := Current_Scope;
4809 while Present (S) and then S /= Standard_Standard loop
4810 loop
4811 Num_Scopes := Num_Scopes + 1;
4813 Use_Clauses (Num_Scopes) :=
4814 (Scope_Stack.Table
4815 (Scope_Stack.Last - Num_Scopes + 1).
4816 First_Use_Clause);
4817 End_Use_Clauses (Use_Clauses (Num_Scopes));
4819 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4820 or else Scope_Stack.Table
4821 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4822 end loop;
4824 exit when Is_Generic_Instance (S)
4825 and then (In_Package_Body (S)
4826 or else Ekind (S) = E_Procedure
4827 or else Ekind (S) = E_Function);
4828 S := Scope (S);
4829 end loop;
4831 Vis := Is_Immediately_Visible (Gen_Comp);
4833 -- Find and save all enclosing instances
4835 S := Current_Scope;
4837 while Present (S)
4838 and then S /= Standard_Standard
4839 loop
4840 if Is_Generic_Instance (S) then
4841 N_Instances := N_Instances + 1;
4842 Instances (N_Instances) := S;
4844 exit when In_Package_Body (S);
4845 end if;
4847 S := Scope (S);
4848 end loop;
4850 -- Remove context of current compilation unit, unless we are within a
4851 -- nested package instantiation, in which case the context has been
4852 -- removed previously.
4854 -- If current scope is the body of a child unit, remove context of
4855 -- spec as well. If an enclosing scope is an instance body, the
4856 -- context has already been removed, but the entities in the body
4857 -- must be made invisible as well.
4859 S := Current_Scope;
4860 while Present (S) and then S /= Standard_Standard loop
4861 if Is_Generic_Instance (S)
4862 and then (In_Package_Body (S)
4863 or else Ekind_In (S, E_Procedure, E_Function))
4864 then
4865 -- We still have to remove the entities of the enclosing
4866 -- instance from direct visibility.
4868 declare
4869 E : Entity_Id;
4870 begin
4871 E := First_Entity (S);
4872 while Present (E) loop
4873 Set_Is_Immediately_Visible (E, False);
4874 Next_Entity (E);
4875 end loop;
4876 end;
4878 exit;
4879 end if;
4881 if S = Curr_Unit
4882 or else (Ekind (Curr_Unit) = E_Package_Body
4883 and then S = Spec_Entity (Curr_Unit))
4884 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4885 and then S = Corresponding_Spec
4886 (Unit_Declaration_Node (Curr_Unit)))
4887 then
4888 Removed := True;
4890 -- Remove entities in current scopes from visibility, so that
4891 -- instance body is compiled in a clean environment.
4893 List := Save_Scope_Stack (Handle_Use => False);
4895 if Is_Child_Unit (S) then
4897 -- Remove child unit from stack, as well as inner scopes.
4898 -- Removing the context of a child unit removes parent units
4899 -- as well.
4901 while Current_Scope /= S loop
4902 Num_Inner := Num_Inner + 1;
4903 Inner_Scopes (Num_Inner) := Current_Scope;
4904 Pop_Scope;
4905 end loop;
4907 Pop_Scope;
4908 Remove_Context (Curr_Comp);
4909 Curr_Scope := S;
4911 else
4912 Remove_Context (Curr_Comp);
4913 end if;
4915 if Ekind (Curr_Unit) = E_Package_Body then
4916 Remove_Context (Library_Unit (Curr_Comp));
4917 end if;
4918 end if;
4920 S := Scope (S);
4921 end loop;
4923 pragma Assert (Num_Inner < Num_Scopes);
4925 -- The inlined package body must be analyzed with the SPARK_Mode of
4926 -- the enclosing context, otherwise the body may cause bogus errors
4927 -- if a configuration SPARK_Mode pragma in in effect.
4929 Push_Scope (Standard_Standard);
4930 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4931 Instantiate_Package_Body
4932 (Body_Info =>
4933 ((Inst_Node => N,
4934 Act_Decl => Act_Decl,
4935 Expander_Status => Expander_Active,
4936 Current_Sem_Unit => Current_Sem_Unit,
4937 Scope_Suppress => Scope_Suppress,
4938 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4939 Version => Ada_Version,
4940 Version_Pragma => Ada_Version_Pragma,
4941 Warnings => Save_Warnings,
4942 SPARK_Mode => Saved_SM,
4943 SPARK_Mode_Pragma => Saved_SMP)),
4944 Inlined_Body => True);
4946 Pop_Scope;
4948 -- Restore context
4950 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4952 -- Reset Generic_Instance flag so that use clauses can be installed
4953 -- in the proper order. (See Use_One_Package for effect of enclosing
4954 -- instances on processing of use clauses).
4956 for J in 1 .. N_Instances loop
4957 Set_Is_Generic_Instance (Instances (J), False);
4958 end loop;
4960 if Removed then
4961 Install_Context (Curr_Comp, Chain => False);
4963 if Present (Curr_Scope)
4964 and then Is_Child_Unit (Curr_Scope)
4965 then
4966 Push_Scope (Curr_Scope);
4967 Set_Is_Immediately_Visible (Curr_Scope);
4969 -- Finally, restore inner scopes as well
4971 for J in reverse 1 .. Num_Inner loop
4972 Push_Scope (Inner_Scopes (J));
4973 end loop;
4974 end if;
4976 Restore_Scope_Stack (List, Handle_Use => False);
4978 if Present (Curr_Scope)
4979 and then
4980 (In_Private_Part (Curr_Scope)
4981 or else In_Package_Body (Curr_Scope))
4982 then
4983 -- Install private declaration of ancestor units, which are
4984 -- currently available. Restore_Scope_Stack and Install_Context
4985 -- only install the visible part of parents.
4987 declare
4988 Par : Entity_Id;
4989 begin
4990 Par := Scope (Curr_Scope);
4991 while (Present (Par)) and then Par /= Standard_Standard loop
4992 Install_Private_Declarations (Par);
4993 Par := Scope (Par);
4994 end loop;
4995 end;
4996 end if;
4997 end if;
4999 -- Restore use clauses. For a child unit, use clauses in the parents
5000 -- are restored when installing the context, so only those in inner
5001 -- scopes (and those local to the child unit itself) need to be
5002 -- installed explicitly.
5004 if Is_Child_Unit (Curr_Unit) and then Removed then
5005 for J in reverse 1 .. Num_Inner + 1 loop
5006 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5007 Use_Clauses (J);
5008 Install_Use_Clauses (Use_Clauses (J));
5009 end loop;
5011 else
5012 for J in reverse 1 .. Num_Scopes loop
5013 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5014 Use_Clauses (J);
5015 Install_Use_Clauses (Use_Clauses (J));
5016 end loop;
5017 end if;
5019 -- Restore status of instances. If one of them is a body, make its
5020 -- local entities visible again.
5022 declare
5023 E : Entity_Id;
5024 Inst : Entity_Id;
5026 begin
5027 for J in 1 .. N_Instances loop
5028 Inst := Instances (J);
5029 Set_Is_Generic_Instance (Inst, True);
5031 if In_Package_Body (Inst)
5032 or else Ekind_In (S, E_Procedure, E_Function)
5033 then
5034 E := First_Entity (Instances (J));
5035 while Present (E) loop
5036 Set_Is_Immediately_Visible (E);
5037 Next_Entity (E);
5038 end loop;
5039 end if;
5040 end loop;
5041 end;
5043 -- If generic unit is in current unit, current context is correct. Note
5044 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5045 -- enclosing scopes were removed.
5047 else
5048 Instantiate_Package_Body
5049 (Body_Info =>
5050 ((Inst_Node => N,
5051 Act_Decl => Act_Decl,
5052 Expander_Status => Expander_Active,
5053 Current_Sem_Unit => Current_Sem_Unit,
5054 Scope_Suppress => Scope_Suppress,
5055 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5056 Version => Ada_Version,
5057 Version_Pragma => Ada_Version_Pragma,
5058 Warnings => Save_Warnings,
5059 SPARK_Mode => SPARK_Mode,
5060 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
5061 Inlined_Body => True);
5062 end if;
5063 end Inline_Instance_Body;
5065 -------------------------------------
5066 -- Analyze_Procedure_Instantiation --
5067 -------------------------------------
5069 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5070 begin
5071 Analyze_Subprogram_Instantiation (N, E_Procedure);
5072 end Analyze_Procedure_Instantiation;
5074 -----------------------------------
5075 -- Need_Subprogram_Instance_Body --
5076 -----------------------------------
5078 function Need_Subprogram_Instance_Body
5079 (N : Node_Id;
5080 Subp : Entity_Id) return Boolean
5082 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5083 -- Return True if E is an inlined subprogram, an inlined renaming or a
5084 -- subprogram nested in an inlined subprogram. The inlining machinery
5085 -- totally disregards nested subprograms since it considers that they
5086 -- will always be compiled if the parent is (see Inline.Is_Nested).
5088 ------------------------------------
5089 -- Is_Inlined_Or_Child_Of_Inlined --
5090 ------------------------------------
5092 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5093 Scop : Entity_Id;
5095 begin
5096 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5097 return True;
5098 end if;
5100 Scop := Scope (E);
5101 while Scop /= Standard_Standard loop
5102 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5103 return True;
5104 end if;
5106 Scop := Scope (Scop);
5107 end loop;
5109 return False;
5110 end Is_Inlined_Or_Child_Of_Inlined;
5112 begin
5113 -- Must be in the main unit or inlined (or child of inlined)
5115 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5117 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5119 and then (Operating_Mode = Generate_Code
5120 or else (Operating_Mode = Check_Semantics
5121 and then (ASIS_Mode or GNATprove_Mode)))
5123 -- The body is needed when generating code (full expansion), in ASIS
5124 -- mode for other tools, and in GNATprove mode (special expansion) for
5125 -- formal verification of the body itself.
5127 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
5129 -- No point in inlining if ABE is inevitable
5131 and then not Is_Known_Guaranteed_ABE (N)
5133 -- Or if subprogram is eliminated
5135 and then not Is_Eliminated (Subp)
5136 then
5137 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5138 return True;
5140 -- Here if not inlined, or we ignore the inlining
5142 else
5143 return False;
5144 end if;
5145 end Need_Subprogram_Instance_Body;
5147 --------------------------------------
5148 -- Analyze_Subprogram_Instantiation --
5149 --------------------------------------
5151 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5152 -- must be replaced by gotos which jump to the end of the routine in order
5153 -- to restore the Ghost and SPARK modes.
5155 procedure Analyze_Subprogram_Instantiation
5156 (N : Node_Id;
5157 K : Entity_Kind)
5159 Loc : constant Source_Ptr := Sloc (N);
5160 Gen_Id : constant Node_Id := Name (N);
5161 Errs : constant Nat := Serious_Errors_Detected;
5163 Anon_Id : constant Entity_Id :=
5164 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
5165 Chars => New_External_Name
5166 (Chars (Defining_Entity (N)), 'R'));
5168 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5169 Act_Decl : Node_Id;
5170 Act_Spec : Node_Id;
5171 Act_Tree : Node_Id;
5173 Env_Installed : Boolean := False;
5174 Gen_Unit : Entity_Id;
5175 Gen_Decl : Node_Id;
5176 Pack_Id : Entity_Id;
5177 Parent_Installed : Boolean := False;
5179 Renaming_List : List_Id;
5180 -- The list of declarations that link formals and actuals of the
5181 -- instance. These are subtype declarations for formal types, and
5182 -- renaming declarations for other formals. The subprogram declaration
5183 -- for the instance is then appended to the list, and the last item on
5184 -- the list is the renaming declaration for the instance.
5186 procedure Analyze_Instance_And_Renamings;
5187 -- The instance must be analyzed in a context that includes the mappings
5188 -- of generic parameters into actuals. We create a package declaration
5189 -- for this purpose, and a subprogram with an internal name within the
5190 -- package. The subprogram instance is simply an alias for the internal
5191 -- subprogram, declared in the current scope.
5193 procedure Build_Subprogram_Renaming;
5194 -- If the subprogram is recursive, there are occurrences of the name of
5195 -- the generic within the body, which must resolve to the current
5196 -- instance. We add a renaming declaration after the declaration, which
5197 -- is available in the instance body, as well as in the analysis of
5198 -- aspects that appear in the generic. This renaming declaration is
5199 -- inserted after the instance declaration which it renames.
5201 ------------------------------------
5202 -- Analyze_Instance_And_Renamings --
5203 ------------------------------------
5205 procedure Analyze_Instance_And_Renamings is
5206 Def_Ent : constant Entity_Id := Defining_Entity (N);
5207 Pack_Decl : Node_Id;
5209 begin
5210 if Nkind (Parent (N)) = N_Compilation_Unit then
5212 -- For the case of a compilation unit, the container package has
5213 -- the same name as the instantiation, to insure that the binder
5214 -- calls the elaboration procedure with the right name. Copy the
5215 -- entity of the instance, which may have compilation level flags
5216 -- (e.g. Is_Child_Unit) set.
5218 Pack_Id := New_Copy (Def_Ent);
5220 else
5221 -- Otherwise we use the name of the instantiation concatenated
5222 -- with its source position to ensure uniqueness if there are
5223 -- several instantiations with the same name.
5225 Pack_Id :=
5226 Make_Defining_Identifier (Loc,
5227 Chars => New_External_Name
5228 (Related_Id => Chars (Def_Ent),
5229 Suffix => "GP",
5230 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5231 end if;
5233 Pack_Decl :=
5234 Make_Package_Declaration (Loc,
5235 Specification => Make_Package_Specification (Loc,
5236 Defining_Unit_Name => Pack_Id,
5237 Visible_Declarations => Renaming_List,
5238 End_Label => Empty));
5240 Set_Instance_Spec (N, Pack_Decl);
5241 Set_Is_Generic_Instance (Pack_Id);
5242 Set_Debug_Info_Needed (Pack_Id);
5244 -- Case of not a compilation unit
5246 if Nkind (Parent (N)) /= N_Compilation_Unit then
5247 Mark_Rewrite_Insertion (Pack_Decl);
5248 Insert_Before (N, Pack_Decl);
5249 Set_Has_Completion (Pack_Id);
5251 -- Case of an instantiation that is a compilation unit
5253 -- Place declaration on current node so context is complete for
5254 -- analysis (including nested instantiations), and for use in a
5255 -- context_clause (see Analyze_With_Clause).
5257 else
5258 Set_Unit (Parent (N), Pack_Decl);
5259 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5260 end if;
5262 Analyze (Pack_Decl);
5263 Check_Formal_Packages (Pack_Id);
5264 Set_Is_Generic_Instance (Pack_Id, False);
5266 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5267 -- above???
5269 -- Body of the enclosing package is supplied when instantiating the
5270 -- subprogram body, after semantic analysis is completed.
5272 if Nkind (Parent (N)) = N_Compilation_Unit then
5274 -- Remove package itself from visibility, so it does not
5275 -- conflict with subprogram.
5277 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5279 -- Set name and scope of internal subprogram so that the proper
5280 -- external name will be generated. The proper scope is the scope
5281 -- of the wrapper package. We need to generate debugging info for
5282 -- the internal subprogram, so set flag accordingly.
5284 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5285 Set_Scope (Anon_Id, Scope (Pack_Id));
5287 -- Mark wrapper package as referenced, to avoid spurious warnings
5288 -- if the instantiation appears in various with_ clauses of
5289 -- subunits of the main unit.
5291 Set_Referenced (Pack_Id);
5292 end if;
5294 Set_Is_Generic_Instance (Anon_Id);
5295 Set_Debug_Info_Needed (Anon_Id);
5296 Act_Decl_Id := New_Copy (Anon_Id);
5298 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5299 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5300 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5302 -- Subprogram instance comes from source only if generic does
5304 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5306 -- If the instance is a child unit, mark the Id accordingly. Mark
5307 -- the anonymous entity as well, which is the real subprogram and
5308 -- which is used when the instance appears in a context clause.
5309 -- Similarly, propagate the Is_Eliminated flag to handle properly
5310 -- nested eliminated subprograms.
5312 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5313 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5314 New_Overloaded_Entity (Act_Decl_Id);
5315 Check_Eliminated (Act_Decl_Id);
5316 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5318 if Nkind (Parent (N)) = N_Compilation_Unit then
5320 -- In compilation unit case, kill elaboration checks on the
5321 -- instantiation, since they are never needed - the body is
5322 -- instantiated at the same point as the spec.
5324 if Legacy_Elaboration_Checks then
5325 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5326 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5327 end if;
5329 Set_Is_Compilation_Unit (Anon_Id);
5330 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5331 end if;
5333 -- The instance is not a freezing point for the new subprogram.
5334 -- The anonymous subprogram may have a freeze node, created for
5335 -- some delayed aspects. This freeze node must not be inherited
5336 -- by the visible subprogram entity.
5338 Set_Is_Frozen (Act_Decl_Id, False);
5339 Set_Freeze_Node (Act_Decl_Id, Empty);
5341 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5342 Valid_Operator_Definition (Act_Decl_Id);
5343 end if;
5345 Set_Alias (Act_Decl_Id, Anon_Id);
5346 Set_Has_Completion (Act_Decl_Id);
5347 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5349 if Nkind (Parent (N)) = N_Compilation_Unit then
5350 Set_Body_Required (Parent (N), False);
5351 end if;
5352 end Analyze_Instance_And_Renamings;
5354 -------------------------------
5355 -- Build_Subprogram_Renaming --
5356 -------------------------------
5358 procedure Build_Subprogram_Renaming is
5359 Renaming_Decl : Node_Id;
5360 Unit_Renaming : Node_Id;
5362 begin
5363 Unit_Renaming :=
5364 Make_Subprogram_Renaming_Declaration (Loc,
5365 Specification =>
5366 Copy_Generic_Node
5367 (Specification (Original_Node (Gen_Decl)),
5368 Empty,
5369 Instantiating => True),
5370 Name => New_Occurrence_Of (Anon_Id, Loc));
5372 -- The generic may be a a child unit. The renaming needs an
5373 -- identifier with the proper name.
5375 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5376 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5378 -- If there is a formal subprogram with the same name as the unit
5379 -- itself, do not add this renaming declaration, to prevent
5380 -- ambiguities when there is a call with that name in the body.
5381 -- This is a partial and ugly fix for one ACATS test. ???
5383 Renaming_Decl := First (Renaming_List);
5384 while Present (Renaming_Decl) loop
5385 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5386 and then
5387 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5388 then
5389 exit;
5390 end if;
5392 Next (Renaming_Decl);
5393 end loop;
5395 if No (Renaming_Decl) then
5396 Append (Unit_Renaming, Renaming_List);
5397 end if;
5398 end Build_Subprogram_Renaming;
5400 -- Local variables
5402 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5403 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5404 Saved_ISMP : constant Boolean :=
5405 Ignore_SPARK_Mode_Pragmas_In_Instance;
5406 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5407 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5408 -- Save the Ghost and SPARK mode-related data to restore on exit
5410 Vis_Prims_List : Elist_Id := No_Elist;
5411 -- List of primitives made temporarily visible in the instantiation
5412 -- to match the visibility of the formal type
5414 -- Start of processing for Analyze_Subprogram_Instantiation
5416 begin
5417 -- Preserve relevant elaboration-related attributes of the context which
5418 -- are no longer available or very expensive to recompute once analysis,
5419 -- resolution, and expansion are over.
5421 Mark_Elaboration_Attributes
5422 (N_Id => N,
5423 Checks => True,
5424 Level => True,
5425 Modes => True,
5426 Warnings => True);
5428 Check_SPARK_05_Restriction ("generic is not allowed", N);
5430 -- Very first thing: check for special Text_IO unit in case we are
5431 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5432 -- such an instantiation is bogus (these are packages, not subprograms),
5433 -- but we get a better error message if we do this.
5435 Check_Text_IO_Special_Unit (Gen_Id);
5437 -- Make node global for error reporting
5439 Instantiation_Node := N;
5441 -- For package instantiations we turn off style checks, because they
5442 -- will have been emitted in the generic. For subprogram instantiations
5443 -- we want to apply at least the check on overriding indicators so we
5444 -- do not modify the style check status.
5446 -- The renaming declarations for the actuals do not come from source and
5447 -- will not generate spurious warnings.
5449 Preanalyze_Actuals (N);
5451 Init_Env;
5452 Env_Installed := True;
5453 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5454 Gen_Unit := Entity (Gen_Id);
5456 -- A subprogram instantiation is Ghost when it is subject to pragma
5457 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5458 -- that any nodes generated during analysis and expansion are marked as
5459 -- Ghost.
5461 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5463 Generate_Reference (Gen_Unit, Gen_Id);
5465 if Nkind (Gen_Id) = N_Identifier
5466 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5467 then
5468 Error_Msg_NE
5469 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5470 end if;
5472 if Etype (Gen_Unit) = Any_Type then
5473 Restore_Env;
5474 goto Leave;
5475 end if;
5477 -- Verify that it is a generic subprogram of the right kind, and that
5478 -- it does not lead to a circular instantiation.
5480 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5481 Error_Msg_NE
5482 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5484 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5485 Error_Msg_NE
5486 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5488 elsif In_Open_Scopes (Gen_Unit) then
5489 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5491 else
5492 Set_Entity (Gen_Id, Gen_Unit);
5493 Set_Is_Instantiated (Gen_Unit);
5495 if In_Extended_Main_Source_Unit (N) then
5496 Generate_Reference (Gen_Unit, N);
5497 end if;
5499 -- If renaming, get original unit
5501 if Present (Renamed_Object (Gen_Unit))
5502 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5503 E_Generic_Function)
5504 then
5505 Gen_Unit := Renamed_Object (Gen_Unit);
5506 Set_Is_Instantiated (Gen_Unit);
5507 Generate_Reference (Gen_Unit, N);
5508 end if;
5510 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5511 Error_Msg_Node_2 := Current_Scope;
5512 Error_Msg_NE
5513 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5514 Circularity_Detected := True;
5515 Restore_Hidden_Primitives (Vis_Prims_List);
5516 goto Leave;
5517 end if;
5519 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5521 -- Initialize renamings map, for error checking
5523 Generic_Renamings.Set_Last (0);
5524 Generic_Renamings_HTable.Reset;
5526 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5528 -- Copy original generic tree, to produce text for instantiation
5530 Act_Tree :=
5531 Copy_Generic_Node
5532 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5534 -- Inherit overriding indicator from instance node
5536 Act_Spec := Specification (Act_Tree);
5537 Set_Must_Override (Act_Spec, Must_Override (N));
5538 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5540 Renaming_List :=
5541 Analyze_Associations
5542 (I_Node => N,
5543 Formals => Generic_Formal_Declarations (Act_Tree),
5544 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5546 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5548 -- The subprogram itself cannot contain a nested instance, so the
5549 -- current parent is left empty.
5551 Set_Instance_Env (Gen_Unit, Empty);
5553 -- Build the subprogram declaration, which does not appear in the
5554 -- generic template, and give it a sloc consistent with that of the
5555 -- template.
5557 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5558 Set_Generic_Parent (Act_Spec, Gen_Unit);
5559 Act_Decl :=
5560 Make_Subprogram_Declaration (Sloc (Act_Spec),
5561 Specification => Act_Spec);
5563 -- The aspects have been copied previously, but they have to be
5564 -- linked explicitly to the new subprogram declaration. Explicit
5565 -- pre/postconditions on the instance are analyzed below, in a
5566 -- separate step.
5568 Move_Aspects (Act_Tree, To => Act_Decl);
5569 Set_Categorization_From_Pragmas (Act_Decl);
5571 if Parent_Installed then
5572 Hide_Current_Scope;
5573 end if;
5575 Append (Act_Decl, Renaming_List);
5577 -- Contract-related source pragmas that follow a generic subprogram
5578 -- must be instantiated explicitly because they are not part of the
5579 -- subprogram template.
5581 Instantiate_Subprogram_Contract
5582 (Original_Node (Gen_Decl), Renaming_List);
5584 Build_Subprogram_Renaming;
5586 -- If the context of the instance is subject to SPARK_Mode "off" or
5587 -- the annotation is altogether missing, set the global flag which
5588 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5589 -- the instance. This should be done prior to analyzing the instance.
5591 if SPARK_Mode /= On then
5592 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5593 end if;
5595 -- If the context of an instance is not subject to SPARK_Mode "off",
5596 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5597 -- the latter should be the one applicable to the instance.
5599 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5600 and then Saved_SM /= Off
5601 and then Present (SPARK_Pragma (Gen_Unit))
5602 then
5603 Set_SPARK_Mode (Gen_Unit);
5604 end if;
5606 Analyze_Instance_And_Renamings;
5608 -- Restore SPARK_Mode from the context after analysis of the package
5609 -- declaration, so that the SPARK_Mode on the generic spec does not
5610 -- apply to the pending instance for the instance body.
5612 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5613 and then Saved_SM /= Off
5614 and then Present (SPARK_Pragma (Gen_Unit))
5615 then
5616 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5617 end if;
5619 -- If the generic is marked Import (Intrinsic), then so is the
5620 -- instance. This indicates that there is no body to instantiate. If
5621 -- generic is marked inline, so it the instance, and the anonymous
5622 -- subprogram it renames. If inlined, or else if inlining is enabled
5623 -- for the compilation, we generate the instance body even if it is
5624 -- not within the main unit.
5626 if Is_Intrinsic_Subprogram (Gen_Unit) then
5627 Set_Is_Intrinsic_Subprogram (Anon_Id);
5628 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5630 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5631 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5632 end if;
5633 end if;
5635 -- Inherit convention from generic unit. Intrinsic convention, as for
5636 -- an instance of unchecked conversion, is not inherited because an
5637 -- explicit Ada instance has been created.
5639 if Has_Convention_Pragma (Gen_Unit)
5640 and then Convention (Gen_Unit) /= Convention_Intrinsic
5641 then
5642 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5643 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5644 end if;
5646 Generate_Definition (Act_Decl_Id);
5648 -- Inherit all inlining-related flags which apply to the generic in
5649 -- the subprogram and its declaration.
5651 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5652 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5654 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5655 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5657 -- Propagate No_Return if pragma applied to generic unit. This must
5658 -- be done explicitly because pragma does not appear in generic
5659 -- declaration (unlike the aspect case).
5661 if No_Return (Gen_Unit) then
5662 Set_No_Return (Act_Decl_Id);
5663 Set_No_Return (Anon_Id);
5664 end if;
5666 Set_Has_Pragma_Inline_Always
5667 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5668 Set_Has_Pragma_Inline_Always
5669 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5671 -- Mark both the instance spec and the anonymous package in case the
5672 -- body is instantiated at a later pass. This preserves the original
5673 -- context in effect for the body.
5675 if SPARK_Mode /= On then
5676 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5677 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5678 end if;
5680 if Legacy_Elaboration_Checks
5681 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5682 then
5683 Check_Elab_Instantiation (N);
5684 end if;
5686 -- Save the scenario for later examination by the ABE Processing
5687 -- phase.
5689 Record_Elaboration_Scenario (N);
5691 -- The instantiation results in a guaranteed ABE. Create a completing
5692 -- body for the subprogram declaration because the real body will not
5693 -- be instantiated.
5695 if Is_Known_Guaranteed_ABE (N) then
5696 Provide_Completing_Bodies (Instance_Spec (N));
5697 end if;
5699 if Is_Dispatching_Operation (Act_Decl_Id)
5700 and then Ada_Version >= Ada_2005
5701 then
5702 declare
5703 Formal : Entity_Id;
5705 begin
5706 Formal := First_Formal (Act_Decl_Id);
5707 while Present (Formal) loop
5708 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5709 and then Is_Controlling_Formal (Formal)
5710 and then not Can_Never_Be_Null (Formal)
5711 then
5712 Error_Msg_NE
5713 ("access parameter& is controlling,", N, Formal);
5714 Error_Msg_NE
5715 ("\corresponding parameter of & must be explicitly "
5716 & "null-excluding", N, Gen_Id);
5717 end if;
5719 Next_Formal (Formal);
5720 end loop;
5721 end;
5722 end if;
5724 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5726 Validate_Categorization_Dependency (N, Act_Decl_Id);
5728 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5729 Inherit_Context (Gen_Decl, N);
5731 Restore_Private_Views (Pack_Id, False);
5733 -- If the context requires a full instantiation, mark node for
5734 -- subsequent construction of the body.
5736 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5737 Check_Forward_Instantiation (Gen_Decl);
5739 -- The wrapper package is always delayed, because it does not
5740 -- constitute a freeze point, but to insure that the freeze node
5741 -- is placed properly, it is created directly when instantiating
5742 -- the body (otherwise the freeze node might appear to early for
5743 -- nested instantiations). For ASIS purposes, indicate that the
5744 -- wrapper package has replaced the instantiation node.
5746 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5747 Rewrite (N, Unit (Parent (N)));
5748 Set_Unit (Parent (N), N);
5749 end if;
5751 -- Replace instance node for library-level instantiations of
5752 -- intrinsic subprograms, for ASIS use.
5754 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5755 Rewrite (N, Unit (Parent (N)));
5756 Set_Unit (Parent (N), N);
5757 end if;
5759 if Parent_Installed then
5760 Remove_Parent;
5761 end if;
5763 Restore_Hidden_Primitives (Vis_Prims_List);
5764 Restore_Env;
5765 Env_Installed := False;
5766 Generic_Renamings.Set_Last (0);
5767 Generic_Renamings_HTable.Reset;
5768 end if;
5770 <<Leave>>
5771 -- Analyze aspects in declaration if no errors appear in the instance.
5773 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5774 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5775 end if;
5777 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5778 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5779 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5781 exception
5782 when Instantiation_Error =>
5783 if Parent_Installed then
5784 Remove_Parent;
5785 end if;
5787 if Env_Installed then
5788 Restore_Env;
5789 end if;
5791 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5792 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5793 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5794 end Analyze_Subprogram_Instantiation;
5796 -------------------------
5797 -- Get_Associated_Node --
5798 -------------------------
5800 function Get_Associated_Node (N : Node_Id) return Node_Id is
5801 Assoc : Node_Id;
5803 begin
5804 Assoc := Associated_Node (N);
5806 if Nkind (Assoc) /= Nkind (N) then
5807 return Assoc;
5809 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5810 return Assoc;
5812 else
5813 -- If the node is part of an inner generic, it may itself have been
5814 -- remapped into a further generic copy. Associated_Node is otherwise
5815 -- used for the entity of the node, and will be of a different node
5816 -- kind, or else N has been rewritten as a literal or function call.
5818 while Present (Associated_Node (Assoc))
5819 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5820 loop
5821 Assoc := Associated_Node (Assoc);
5822 end loop;
5824 -- Follow an additional link in case the final node was rewritten.
5825 -- This can only happen with nested generic units.
5827 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5828 and then Present (Associated_Node (Assoc))
5829 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5830 N_Explicit_Dereference,
5831 N_Integer_Literal,
5832 N_Real_Literal,
5833 N_String_Literal))
5834 then
5835 Assoc := Associated_Node (Assoc);
5836 end if;
5838 -- An additional special case: an unconstrained type in an object
5839 -- declaration may have been rewritten as a local subtype constrained
5840 -- by the expression in the declaration. We need to recover the
5841 -- original entity, which may be global.
5843 if Present (Original_Node (Assoc))
5844 and then Nkind (Parent (N)) = N_Object_Declaration
5845 then
5846 Assoc := Original_Node (Assoc);
5847 end if;
5849 return Assoc;
5850 end if;
5851 end Get_Associated_Node;
5853 ----------------------------
5854 -- Build_Function_Wrapper --
5855 ----------------------------
5857 function Build_Function_Wrapper
5858 (Formal_Subp : Entity_Id;
5859 Actual_Subp : Entity_Id) return Node_Id
5861 Loc : constant Source_Ptr := Sloc (Current_Scope);
5862 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5863 Actuals : List_Id;
5864 Decl : Node_Id;
5865 Func_Name : Node_Id;
5866 Func : Entity_Id;
5867 Parm_Type : Node_Id;
5868 Profile : List_Id := New_List;
5869 Spec : Node_Id;
5870 Act_F : Entity_Id;
5871 Form_F : Entity_Id;
5872 New_F : Entity_Id;
5874 begin
5875 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5877 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5878 Set_Ekind (Func, E_Function);
5879 Set_Is_Generic_Actual_Subprogram (Func);
5881 Actuals := New_List;
5882 Profile := New_List;
5884 Act_F := First_Formal (Actual_Subp);
5885 Form_F := First_Formal (Formal_Subp);
5886 while Present (Form_F) loop
5888 -- Create new formal for profile of wrapper, and add a reference
5889 -- to it in the list of actuals for the enclosing call. The name
5890 -- must be that of the formal in the formal subprogram, because
5891 -- calls to it in the generic body may use named associations.
5893 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5895 Parm_Type :=
5896 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5898 Append_To (Profile,
5899 Make_Parameter_Specification (Loc,
5900 Defining_Identifier => New_F,
5901 Parameter_Type => Parm_Type));
5903 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5904 Next_Formal (Form_F);
5906 if Present (Act_F) then
5907 Next_Formal (Act_F);
5908 end if;
5909 end loop;
5911 Spec :=
5912 Make_Function_Specification (Loc,
5913 Defining_Unit_Name => Func,
5914 Parameter_Specifications => Profile,
5915 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5917 Decl :=
5918 Make_Expression_Function (Loc,
5919 Specification => Spec,
5920 Expression =>
5921 Make_Function_Call (Loc,
5922 Name => Func_Name,
5923 Parameter_Associations => Actuals));
5925 return Decl;
5926 end Build_Function_Wrapper;
5928 ----------------------------
5929 -- Build_Operator_Wrapper --
5930 ----------------------------
5932 function Build_Operator_Wrapper
5933 (Formal_Subp : Entity_Id;
5934 Actual_Subp : Entity_Id) return Node_Id
5936 Loc : constant Source_Ptr := Sloc (Current_Scope);
5937 Ret_Type : constant Entity_Id :=
5938 Get_Instance_Of (Etype (Formal_Subp));
5939 Op_Type : constant Entity_Id :=
5940 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5941 Is_Binary : constant Boolean :=
5942 Present (Next_Formal (First_Formal (Formal_Subp)));
5944 Decl : Node_Id;
5945 Expr : Node_Id := Empty;
5946 F1, F2 : Entity_Id;
5947 Func : Entity_Id;
5948 Op_Name : Name_Id;
5949 Spec : Node_Id;
5950 L, R : Node_Id;
5952 begin
5953 Op_Name := Chars (Actual_Subp);
5955 -- Create entities for wrapper function and its formals
5957 F1 := Make_Temporary (Loc, 'A');
5958 F2 := Make_Temporary (Loc, 'B');
5959 L := New_Occurrence_Of (F1, Loc);
5960 R := New_Occurrence_Of (F2, Loc);
5962 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5963 Set_Ekind (Func, E_Function);
5964 Set_Is_Generic_Actual_Subprogram (Func);
5966 Spec :=
5967 Make_Function_Specification (Loc,
5968 Defining_Unit_Name => Func,
5969 Parameter_Specifications => New_List (
5970 Make_Parameter_Specification (Loc,
5971 Defining_Identifier => F1,
5972 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5973 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5975 if Is_Binary then
5976 Append_To (Parameter_Specifications (Spec),
5977 Make_Parameter_Specification (Loc,
5978 Defining_Identifier => F2,
5979 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5980 end if;
5982 -- Build expression as a function call, or as an operator node
5983 -- that corresponds to the name of the actual, starting with
5984 -- binary operators.
5986 if Op_Name not in Any_Operator_Name then
5987 Expr :=
5988 Make_Function_Call (Loc,
5989 Name =>
5990 New_Occurrence_Of (Actual_Subp, Loc),
5991 Parameter_Associations => New_List (L));
5993 if Is_Binary then
5994 Append_To (Parameter_Associations (Expr), R);
5995 end if;
5997 -- Binary operators
5999 elsif Is_Binary then
6000 if Op_Name = Name_Op_And then
6001 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6002 elsif Op_Name = Name_Op_Or then
6003 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6004 elsif Op_Name = Name_Op_Xor then
6005 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6006 elsif Op_Name = Name_Op_Eq then
6007 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6008 elsif Op_Name = Name_Op_Ne then
6009 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6010 elsif Op_Name = Name_Op_Le then
6011 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6012 elsif Op_Name = Name_Op_Gt then
6013 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6014 elsif Op_Name = Name_Op_Ge then
6015 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6016 elsif Op_Name = Name_Op_Lt then
6017 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6018 elsif Op_Name = Name_Op_Add then
6019 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6020 elsif Op_Name = Name_Op_Subtract then
6021 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6022 elsif Op_Name = Name_Op_Concat then
6023 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6024 elsif Op_Name = Name_Op_Multiply then
6025 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6026 elsif Op_Name = Name_Op_Divide then
6027 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6028 elsif Op_Name = Name_Op_Mod then
6029 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6030 elsif Op_Name = Name_Op_Rem then
6031 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6032 elsif Op_Name = Name_Op_Expon then
6033 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6034 end if;
6036 -- Unary operators
6038 else
6039 if Op_Name = Name_Op_Add then
6040 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6041 elsif Op_Name = Name_Op_Subtract then
6042 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6043 elsif Op_Name = Name_Op_Abs then
6044 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6045 elsif Op_Name = Name_Op_Not then
6046 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6047 end if;
6048 end if;
6050 Decl :=
6051 Make_Expression_Function (Loc,
6052 Specification => Spec,
6053 Expression => Expr);
6055 return Decl;
6056 end Build_Operator_Wrapper;
6058 -------------------------------------------
6059 -- Build_Instance_Compilation_Unit_Nodes --
6060 -------------------------------------------
6062 procedure Build_Instance_Compilation_Unit_Nodes
6063 (N : Node_Id;
6064 Act_Body : Node_Id;
6065 Act_Decl : Node_Id)
6067 Decl_Cunit : Node_Id;
6068 Body_Cunit : Node_Id;
6069 Citem : Node_Id;
6070 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6071 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6073 begin
6074 -- A new compilation unit node is built for the instance declaration
6076 Decl_Cunit :=
6077 Make_Compilation_Unit (Sloc (N),
6078 Context_Items => Empty_List,
6079 Unit => Act_Decl,
6080 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6082 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6084 -- The new compilation unit is linked to its body, but both share the
6085 -- same file, so we do not set Body_Required on the new unit so as not
6086 -- to create a spurious dependency on a non-existent body in the ali.
6087 -- This simplifies CodePeer unit traversal.
6089 -- We use the original instantiation compilation unit as the resulting
6090 -- compilation unit of the instance, since this is the main unit.
6092 Rewrite (N, Act_Body);
6094 -- Propagate the aspect specifications from the package body template to
6095 -- the instantiated version of the package body.
6097 if Has_Aspects (Act_Body) then
6098 Set_Aspect_Specifications
6099 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6100 end if;
6102 Body_Cunit := Parent (N);
6104 -- The two compilation unit nodes are linked by the Library_Unit field
6106 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6107 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6109 -- Preserve the private nature of the package if needed
6111 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6113 -- If the instance is not the main unit, its context, categorization
6114 -- and elaboration entity are not relevant to the compilation.
6116 if Body_Cunit /= Cunit (Main_Unit) then
6117 Make_Instance_Unit (Body_Cunit, In_Main => False);
6118 return;
6119 end if;
6121 -- The context clause items on the instantiation, which are now attached
6122 -- to the body compilation unit (since the body overwrote the original
6123 -- instantiation node), semantically belong on the spec, so copy them
6124 -- there. It's harmless to leave them on the body as well. In fact one
6125 -- could argue that they belong in both places.
6127 Citem := First (Context_Items (Body_Cunit));
6128 while Present (Citem) loop
6129 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6130 Next (Citem);
6131 end loop;
6133 -- Propagate categorization flags on packages, so that they appear in
6134 -- the ali file for the spec of the unit.
6136 if Ekind (New_Main) = E_Package then
6137 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6138 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6139 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6140 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6141 Set_Is_Remote_Call_Interface
6142 (Old_Main, Is_Remote_Call_Interface (New_Main));
6143 end if;
6145 -- Make entry in Units table, so that binder can generate call to
6146 -- elaboration procedure for body, if any.
6148 Make_Instance_Unit (Body_Cunit, In_Main => True);
6149 Main_Unit_Entity := New_Main;
6150 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6152 -- Build elaboration entity, since the instance may certainly generate
6153 -- elaboration code requiring a flag for protection.
6155 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6156 end Build_Instance_Compilation_Unit_Nodes;
6158 -----------------------------
6159 -- Check_Access_Definition --
6160 -----------------------------
6162 procedure Check_Access_Definition (N : Node_Id) is
6163 begin
6164 pragma Assert
6165 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6166 null;
6167 end Check_Access_Definition;
6169 -----------------------------------
6170 -- Check_Formal_Package_Instance --
6171 -----------------------------------
6173 -- If the formal has specific parameters, they must match those of the
6174 -- actual. Both of them are instances, and the renaming declarations for
6175 -- their formal parameters appear in the same order in both. The analyzed
6176 -- formal has been analyzed in the context of the current instance.
6178 procedure Check_Formal_Package_Instance
6179 (Formal_Pack : Entity_Id;
6180 Actual_Pack : Entity_Id)
6182 E1 : Entity_Id := First_Entity (Actual_Pack);
6183 E2 : Entity_Id := First_Entity (Formal_Pack);
6184 Prev_E1 : Entity_Id;
6186 Expr1 : Node_Id;
6187 Expr2 : Node_Id;
6189 procedure Check_Mismatch (B : Boolean);
6190 -- Common error routine for mismatch between the parameters of the
6191 -- actual instance and those of the formal package.
6193 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6194 -- The formal may come from a nested formal package, and the actual may
6195 -- have been constant-folded. To determine whether the two denote the
6196 -- same entity we may have to traverse several definitions to recover
6197 -- the ultimate entity that they refer to.
6199 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6200 -- The formal and the actual must be identical, but if both are
6201 -- given by attributes they end up renaming different generated bodies,
6202 -- and we must verify that the attributes themselves match.
6204 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6205 -- Similarly, if the formal comes from a nested formal package, the
6206 -- actual may designate the formal through multiple renamings, which
6207 -- have to be followed to determine the original variable in question.
6209 --------------------
6210 -- Check_Mismatch --
6211 --------------------
6213 procedure Check_Mismatch (B : Boolean) is
6214 -- A Formal_Type_Declaration for a derived private type is rewritten
6215 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6216 -- which is why we examine the original node.
6218 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6220 begin
6221 if Kind = N_Formal_Type_Declaration then
6222 return;
6224 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6225 N_Formal_Package_Declaration)
6226 or else Kind in N_Formal_Subprogram_Declaration
6227 then
6228 null;
6230 -- Ada 2012: If both formal and actual are incomplete types they
6231 -- are conformant.
6233 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6234 null;
6236 elsif B then
6237 Error_Msg_NE
6238 ("actual for & in actual instance does not match formal",
6239 Parent (Actual_Pack), E1);
6240 end if;
6241 end Check_Mismatch;
6243 --------------------------------
6244 -- Same_Instantiated_Constant --
6245 --------------------------------
6247 function Same_Instantiated_Constant
6248 (E1, E2 : Entity_Id) return Boolean
6250 Ent : Entity_Id;
6252 begin
6253 Ent := E2;
6254 while Present (Ent) loop
6255 if E1 = Ent then
6256 return True;
6258 elsif Ekind (Ent) /= E_Constant then
6259 return False;
6261 elsif Is_Entity_Name (Constant_Value (Ent)) then
6262 if Entity (Constant_Value (Ent)) = E1 then
6263 return True;
6264 else
6265 Ent := Entity (Constant_Value (Ent));
6266 end if;
6268 -- The actual may be a constant that has been folded. Recover
6269 -- original name.
6271 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6272 Ent := Entity (Original_Node (Constant_Value (Ent)));
6274 else
6275 return False;
6276 end if;
6277 end loop;
6279 return False;
6280 end Same_Instantiated_Constant;
6282 --------------------------------
6283 -- Same_Instantiated_Function --
6284 --------------------------------
6286 function Same_Instantiated_Function
6287 (E1, E2 : Entity_Id) return Boolean
6289 U1, U2 : Node_Id;
6290 begin
6291 if Alias (E1) = Alias (E2) then
6292 return True;
6294 elsif Present (Alias (E2)) then
6295 U1 := Original_Node (Unit_Declaration_Node (E1));
6296 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6298 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6299 and then Nkind (Name (U1)) = N_Attribute_Reference
6301 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6302 and then Nkind (Name (U2)) = N_Attribute_Reference
6304 and then
6305 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6306 else
6307 return False;
6308 end if;
6309 end Same_Instantiated_Function;
6311 --------------------------------
6312 -- Same_Instantiated_Variable --
6313 --------------------------------
6315 function Same_Instantiated_Variable
6316 (E1, E2 : Entity_Id) return Boolean
6318 function Original_Entity (E : Entity_Id) return Entity_Id;
6319 -- Follow chain of renamings to the ultimate ancestor
6321 ---------------------
6322 -- Original_Entity --
6323 ---------------------
6325 function Original_Entity (E : Entity_Id) return Entity_Id is
6326 Orig : Entity_Id;
6328 begin
6329 Orig := E;
6330 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6331 and then Present (Renamed_Object (Orig))
6332 and then Is_Entity_Name (Renamed_Object (Orig))
6333 loop
6334 Orig := Entity (Renamed_Object (Orig));
6335 end loop;
6337 return Orig;
6338 end Original_Entity;
6340 -- Start of processing for Same_Instantiated_Variable
6342 begin
6343 return Ekind (E1) = Ekind (E2)
6344 and then Original_Entity (E1) = Original_Entity (E2);
6345 end Same_Instantiated_Variable;
6347 -- Start of processing for Check_Formal_Package_Instance
6349 begin
6350 Prev_E1 := E1;
6351 while Present (E1) and then Present (E2) loop
6352 exit when Ekind (E1) = E_Package
6353 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6355 -- If the formal is the renaming of the formal package, this
6356 -- is the end of its formal part, which may occur before the
6357 -- end of the formal part in the actual in the presence of
6358 -- defaulted parameters in the formal package.
6360 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6361 and then Renamed_Entity (E2) = Scope (E2);
6363 -- The analysis of the actual may generate additional internal
6364 -- entities. If the formal is defaulted, there is no corresponding
6365 -- analysis and the internal entities must be skipped, until we
6366 -- find corresponding entities again.
6368 if Comes_From_Source (E2)
6369 and then not Comes_From_Source (E1)
6370 and then Chars (E1) /= Chars (E2)
6371 then
6372 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6373 Next_Entity (E1);
6374 end loop;
6375 end if;
6377 if No (E1) then
6378 return;
6380 -- Entities may be declared without full declaration, such as
6381 -- itypes and predefined operators (concatenation for arrays, eg).
6382 -- Skip it and keep the formal entity to find a later match for it.
6384 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6385 E1 := Prev_E1;
6386 goto Next_E;
6388 -- If the formal entity comes from a formal declaration, it was
6389 -- defaulted in the formal package, and no check is needed on it.
6391 elsif Nkind_In (Original_Node (Parent (E2)),
6392 N_Formal_Object_Declaration,
6393 N_Formal_Type_Declaration)
6394 then
6395 -- If the formal is a tagged type the corresponding class-wide
6396 -- type has been generated as well, and it must be skipped.
6398 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6399 Next_Entity (E2);
6400 end if;
6402 goto Next_E;
6404 -- Ditto for defaulted formal subprograms.
6406 elsif Is_Overloadable (E1)
6407 and then Nkind (Unit_Declaration_Node (E2)) in
6408 N_Formal_Subprogram_Declaration
6409 then
6410 goto Next_E;
6412 elsif Is_Type (E1) then
6414 -- Subtypes must statically match. E1, E2 are the local entities
6415 -- that are subtypes of the actuals. Itypes generated for other
6416 -- parameters need not be checked, the check will be performed
6417 -- on the parameters themselves.
6419 -- If E2 is a formal type declaration, it is a defaulted parameter
6420 -- and needs no checking.
6422 if not Is_Itype (E1) and then not Is_Itype (E2) then
6423 Check_Mismatch
6424 (not Is_Type (E2)
6425 or else Etype (E1) /= Etype (E2)
6426 or else not Subtypes_Statically_Match (E1, E2));
6427 end if;
6429 elsif Ekind (E1) = E_Constant then
6431 -- IN parameters must denote the same static value, or the same
6432 -- constant, or the literal null.
6434 Expr1 := Expression (Parent (E1));
6436 if Ekind (E2) /= E_Constant then
6437 Check_Mismatch (True);
6438 goto Next_E;
6439 else
6440 Expr2 := Expression (Parent (E2));
6441 end if;
6443 if Is_OK_Static_Expression (Expr1) then
6444 if not Is_OK_Static_Expression (Expr2) then
6445 Check_Mismatch (True);
6447 elsif Is_Discrete_Type (Etype (E1)) then
6448 declare
6449 V1 : constant Uint := Expr_Value (Expr1);
6450 V2 : constant Uint := Expr_Value (Expr2);
6451 begin
6452 Check_Mismatch (V1 /= V2);
6453 end;
6455 elsif Is_Real_Type (Etype (E1)) then
6456 declare
6457 V1 : constant Ureal := Expr_Value_R (Expr1);
6458 V2 : constant Ureal := Expr_Value_R (Expr2);
6459 begin
6460 Check_Mismatch (V1 /= V2);
6461 end;
6463 elsif Is_String_Type (Etype (E1))
6464 and then Nkind (Expr1) = N_String_Literal
6465 then
6466 if Nkind (Expr2) /= N_String_Literal then
6467 Check_Mismatch (True);
6468 else
6469 Check_Mismatch
6470 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6471 end if;
6472 end if;
6474 elsif Is_Entity_Name (Expr1) then
6475 if Is_Entity_Name (Expr2) then
6476 if Entity (Expr1) = Entity (Expr2) then
6477 null;
6478 else
6479 Check_Mismatch
6480 (not Same_Instantiated_Constant
6481 (Entity (Expr1), Entity (Expr2)));
6482 end if;
6484 else
6485 Check_Mismatch (True);
6486 end if;
6488 elsif Is_Entity_Name (Original_Node (Expr1))
6489 and then Is_Entity_Name (Expr2)
6490 and then Same_Instantiated_Constant
6491 (Entity (Original_Node (Expr1)), Entity (Expr2))
6492 then
6493 null;
6495 elsif Nkind (Expr1) = N_Null then
6496 Check_Mismatch (Nkind (Expr1) /= N_Null);
6498 else
6499 Check_Mismatch (True);
6500 end if;
6502 elsif Ekind (E1) = E_Variable then
6503 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6505 elsif Ekind (E1) = E_Package then
6506 Check_Mismatch
6507 (Ekind (E1) /= Ekind (E2)
6508 or else (Present (Renamed_Object (E2))
6509 and then Renamed_Object (E1) /=
6510 Renamed_Object (E2)));
6512 elsif Is_Overloadable (E1) then
6513 -- Verify that the actual subprograms match. Note that actuals
6514 -- that are attributes are rewritten as subprograms. If the
6515 -- subprogram in the formal package is defaulted, no check is
6516 -- needed. Note that this can only happen in Ada 2005 when the
6517 -- formal package can be partially parameterized.
6519 if Nkind (Unit_Declaration_Node (E1)) =
6520 N_Subprogram_Renaming_Declaration
6521 and then From_Default (Unit_Declaration_Node (E1))
6522 then
6523 null;
6525 -- If the formal package has an "others" box association that
6526 -- covers this formal, there is no need for a check either.
6528 elsif Nkind (Unit_Declaration_Node (E2)) in
6529 N_Formal_Subprogram_Declaration
6530 and then Box_Present (Unit_Declaration_Node (E2))
6531 then
6532 null;
6534 -- No check needed if subprogram is a defaulted null procedure
6536 elsif No (Alias (E2))
6537 and then Ekind (E2) = E_Procedure
6538 and then
6539 Null_Present (Specification (Unit_Declaration_Node (E2)))
6540 then
6541 null;
6543 -- Otherwise the actual in the formal and the actual in the
6544 -- instantiation of the formal must match, up to renamings.
6546 else
6547 Check_Mismatch
6548 (Ekind (E2) /= Ekind (E1)
6549 or else not Same_Instantiated_Function (E1, E2));
6550 end if;
6552 else
6553 raise Program_Error;
6554 end if;
6556 <<Next_E>>
6557 Prev_E1 := E1;
6558 Next_Entity (E1);
6559 Next_Entity (E2);
6560 end loop;
6561 end Check_Formal_Package_Instance;
6563 ---------------------------
6564 -- Check_Formal_Packages --
6565 ---------------------------
6567 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6568 E : Entity_Id;
6569 Formal_P : Entity_Id;
6570 Formal_Decl : Node_Id;
6571 begin
6572 -- Iterate through the declarations in the instance, looking for package
6573 -- renaming declarations that denote instances of formal packages. Stop
6574 -- when we find the renaming of the current package itself. The
6575 -- declaration for a formal package without a box is followed by an
6576 -- internal entity that repeats the instantiation.
6578 E := First_Entity (P_Id);
6579 while Present (E) loop
6580 if Ekind (E) = E_Package then
6581 if Renamed_Object (E) = P_Id then
6582 exit;
6584 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6585 null;
6587 else
6588 Formal_Decl := Parent (Associated_Formal_Package (E));
6590 -- Nothing to check if the formal has a box or an others_clause
6591 -- (necessarily with a box).
6593 if Box_Present (Formal_Decl) then
6594 null;
6596 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6597 N_Others_Choice
6598 then
6599 -- The internal validating package was generated but formal
6600 -- and instance are known to be compatible.
6602 Formal_P := Next_Entity (E);
6603 Remove (Unit_Declaration_Node (Formal_P));
6605 else
6606 Formal_P := Next_Entity (E);
6608 -- If the instance is within an enclosing instance body
6609 -- there is no need to verify the legality of current formal
6610 -- packages because they were legal in the generic body.
6611 -- This optimization may be applicable elsewhere, and it
6612 -- also removes spurious errors that may arise with
6613 -- on-the-fly inlining and confusion between private and
6614 -- full views.
6616 if not In_Instance_Body then
6617 Check_Formal_Package_Instance (Formal_P, E);
6618 end if;
6620 -- Restore the visibility of formals of the formal instance
6621 -- that are not defaulted, and are hidden within the current
6622 -- generic. These formals may be visible within an enclosing
6623 -- generic.
6625 declare
6626 Elmt : Elmt_Id;
6627 begin
6628 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6629 while Present (Elmt) loop
6630 Set_Is_Hidden (Node (Elmt), False);
6631 Next_Elmt (Elmt);
6632 end loop;
6633 end;
6635 -- After checking, remove the internal validating package.
6636 -- It is only needed for semantic checks, and as it may
6637 -- contain generic formal declarations it should not reach
6638 -- gigi.
6640 Remove (Unit_Declaration_Node (Formal_P));
6641 end if;
6642 end if;
6643 end if;
6645 Next_Entity (E);
6646 end loop;
6647 end Check_Formal_Packages;
6649 ---------------------------------
6650 -- Check_Forward_Instantiation --
6651 ---------------------------------
6653 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6654 S : Entity_Id;
6655 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6657 begin
6658 -- The instantiation appears before the generic body if we are in the
6659 -- scope of the unit containing the generic, either in its spec or in
6660 -- the package body, and before the generic body.
6662 if Ekind (Gen_Comp) = E_Package_Body then
6663 Gen_Comp := Spec_Entity (Gen_Comp);
6664 end if;
6666 if In_Open_Scopes (Gen_Comp)
6667 and then No (Corresponding_Body (Decl))
6668 then
6669 S := Current_Scope;
6671 while Present (S)
6672 and then not Is_Compilation_Unit (S)
6673 and then not Is_Child_Unit (S)
6674 loop
6675 if Ekind (S) = E_Package then
6676 Set_Has_Forward_Instantiation (S);
6677 end if;
6679 S := Scope (S);
6680 end loop;
6681 end if;
6682 end Check_Forward_Instantiation;
6684 ---------------------------
6685 -- Check_Generic_Actuals --
6686 ---------------------------
6688 -- The visibility of the actuals may be different between the point of
6689 -- generic instantiation and the instantiation of the body.
6691 procedure Check_Generic_Actuals
6692 (Instance : Entity_Id;
6693 Is_Formal_Box : Boolean)
6695 E : Entity_Id;
6696 Astype : Entity_Id;
6698 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6699 -- For a formal that is an array type, the component type is often a
6700 -- previous formal in the same unit. The privacy status of the component
6701 -- type will have been examined earlier in the traversal of the
6702 -- corresponding actuals, and this status should not be modified for
6703 -- the array (sub)type itself. However, if the base type of the array
6704 -- (sub)type is private, its full view must be restored in the body to
6705 -- be consistent with subsequent index subtypes, etc.
6707 -- To detect this case we have to rescan the list of formals, which is
6708 -- usually short enough to ignore the resulting inefficiency.
6710 -----------------------------
6711 -- Denotes_Previous_Actual --
6712 -----------------------------
6714 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6715 Prev : Entity_Id;
6717 begin
6718 Prev := First_Entity (Instance);
6719 while Present (Prev) loop
6720 if Is_Type (Prev)
6721 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6722 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6723 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6724 then
6725 return True;
6727 elsif Prev = E then
6728 return False;
6730 else
6731 Next_Entity (Prev);
6732 end if;
6733 end loop;
6735 return False;
6736 end Denotes_Previous_Actual;
6738 -- Start of processing for Check_Generic_Actuals
6740 begin
6741 E := First_Entity (Instance);
6742 while Present (E) loop
6743 if Is_Type (E)
6744 and then Nkind (Parent (E)) = N_Subtype_Declaration
6745 and then Scope (Etype (E)) /= Instance
6746 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6747 then
6748 if Is_Array_Type (E)
6749 and then not Is_Private_Type (Etype (E))
6750 and then Denotes_Previous_Actual (Component_Type (E))
6751 then
6752 null;
6753 else
6754 Check_Private_View (Subtype_Indication (Parent (E)));
6755 end if;
6757 Set_Is_Generic_Actual_Type (E, True);
6758 Set_Is_Hidden (E, False);
6759 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6761 -- We constructed the generic actual type as a subtype of the
6762 -- supplied type. This means that it normally would not inherit
6763 -- subtype specific attributes of the actual, which is wrong for
6764 -- the generic case.
6766 Astype := Ancestor_Subtype (E);
6768 if No (Astype) then
6770 -- This can happen when E is an itype that is the full view of
6771 -- a private type completed, e.g. with a constrained array. In
6772 -- that case, use the first subtype, which will carry size
6773 -- information. The base type itself is unconstrained and will
6774 -- not carry it.
6776 Astype := First_Subtype (E);
6777 end if;
6779 Set_Size_Info (E, (Astype));
6780 Set_RM_Size (E, RM_Size (Astype));
6781 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6783 if Is_Discrete_Or_Fixed_Point_Type (E) then
6784 Set_RM_Size (E, RM_Size (Astype));
6786 -- In nested instances, the base type of an access actual may
6787 -- itself be private, and need to be exchanged.
6789 elsif Is_Access_Type (E)
6790 and then Is_Private_Type (Etype (E))
6791 then
6792 Check_Private_View
6793 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6794 end if;
6796 elsif Ekind (E) = E_Package then
6798 -- If this is the renaming for the current instance, we're done.
6799 -- Otherwise it is a formal package. If the corresponding formal
6800 -- was declared with a box, the (instantiations of the) generic
6801 -- formal part are also visible. Otherwise, ignore the entity
6802 -- created to validate the actuals.
6804 if Renamed_Object (E) = Instance then
6805 exit;
6807 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6808 null;
6810 -- The visibility of a formal of an enclosing generic is already
6811 -- correct.
6813 elsif Denotes_Formal_Package (E) then
6814 null;
6816 elsif Present (Associated_Formal_Package (E))
6817 and then not Is_Generic_Formal (E)
6818 then
6819 if Box_Present (Parent (Associated_Formal_Package (E))) then
6820 Check_Generic_Actuals (Renamed_Object (E), True);
6822 else
6823 Check_Generic_Actuals (Renamed_Object (E), False);
6824 end if;
6826 Set_Is_Hidden (E, False);
6827 end if;
6829 -- If this is a subprogram instance (in a wrapper package) the
6830 -- actual is fully visible.
6832 elsif Is_Wrapper_Package (Instance) then
6833 Set_Is_Hidden (E, False);
6835 -- If the formal package is declared with a box, or if the formal
6836 -- parameter is defaulted, it is visible in the body.
6838 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6839 Set_Is_Hidden (E, False);
6840 end if;
6842 if Ekind (E) = E_Constant then
6844 -- If the type of the actual is a private type declared in the
6845 -- enclosing scope of the generic unit, the body of the generic
6846 -- sees the full view of the type (because it has to appear in
6847 -- the corresponding package body). If the type is private now,
6848 -- exchange views to restore the proper visiblity in the instance.
6850 declare
6851 Typ : constant Entity_Id := Base_Type (Etype (E));
6852 -- The type of the actual
6854 Gen_Id : Entity_Id;
6855 -- The generic unit
6857 Parent_Scope : Entity_Id;
6858 -- The enclosing scope of the generic unit
6860 begin
6861 if Is_Wrapper_Package (Instance) then
6862 Gen_Id :=
6863 Generic_Parent
6864 (Specification
6865 (Unit_Declaration_Node
6866 (Related_Instance (Instance))));
6867 else
6868 Gen_Id :=
6869 Generic_Parent (Package_Specification (Instance));
6870 end if;
6872 Parent_Scope := Scope (Gen_Id);
6874 -- The exchange is only needed if the generic is defined
6875 -- within a package which is not a common ancestor of the
6876 -- scope of the instance, and is not already in scope.
6878 if Is_Private_Type (Typ)
6879 and then Scope (Typ) = Parent_Scope
6880 and then Scope (Instance) /= Parent_Scope
6881 and then Ekind (Parent_Scope) = E_Package
6882 and then not Is_Child_Unit (Gen_Id)
6883 then
6884 Switch_View (Typ);
6886 -- If the type of the entity is a subtype, it may also have
6887 -- to be made visible, together with the base type of its
6888 -- full view, after exchange.
6890 if Is_Private_Type (Etype (E)) then
6891 Switch_View (Etype (E));
6892 Switch_View (Base_Type (Etype (E)));
6893 end if;
6894 end if;
6895 end;
6896 end if;
6898 Next_Entity (E);
6899 end loop;
6900 end Check_Generic_Actuals;
6902 ------------------------------
6903 -- Check_Generic_Child_Unit --
6904 ------------------------------
6906 procedure Check_Generic_Child_Unit
6907 (Gen_Id : Node_Id;
6908 Parent_Installed : in out Boolean)
6910 Loc : constant Source_Ptr := Sloc (Gen_Id);
6911 Gen_Par : Entity_Id := Empty;
6912 E : Entity_Id;
6913 Inst_Par : Entity_Id;
6914 S : Node_Id;
6916 function Find_Generic_Child
6917 (Scop : Entity_Id;
6918 Id : Node_Id) return Entity_Id;
6919 -- Search generic parent for possible child unit with the given name
6921 function In_Enclosing_Instance return Boolean;
6922 -- Within an instance of the parent, the child unit may be denoted by
6923 -- a simple name, or an abbreviated expanded name. Examine enclosing
6924 -- scopes to locate a possible parent instantiation.
6926 ------------------------
6927 -- Find_Generic_Child --
6928 ------------------------
6930 function Find_Generic_Child
6931 (Scop : Entity_Id;
6932 Id : Node_Id) return Entity_Id
6934 E : Entity_Id;
6936 begin
6937 -- If entity of name is already set, instance has already been
6938 -- resolved, e.g. in an enclosing instantiation.
6940 if Present (Entity (Id)) then
6941 if Scope (Entity (Id)) = Scop then
6942 return Entity (Id);
6943 else
6944 return Empty;
6945 end if;
6947 else
6948 E := First_Entity (Scop);
6949 while Present (E) loop
6950 if Chars (E) = Chars (Id)
6951 and then Is_Child_Unit (E)
6952 then
6953 if Is_Child_Unit (E)
6954 and then not Is_Visible_Lib_Unit (E)
6955 then
6956 Error_Msg_NE
6957 ("generic child unit& is not visible", Gen_Id, E);
6958 end if;
6960 Set_Entity (Id, E);
6961 return E;
6962 end if;
6964 Next_Entity (E);
6965 end loop;
6967 return Empty;
6968 end if;
6969 end Find_Generic_Child;
6971 ---------------------------
6972 -- In_Enclosing_Instance --
6973 ---------------------------
6975 function In_Enclosing_Instance return Boolean is
6976 Enclosing_Instance : Node_Id;
6977 Instance_Decl : Node_Id;
6979 begin
6980 -- We do not inline any call that contains instantiations, except
6981 -- for instantiations of Unchecked_Conversion, so if we are within
6982 -- an inlined body the current instance does not require parents.
6984 if In_Inlined_Body then
6985 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6986 return False;
6987 end if;
6989 -- Loop to check enclosing scopes
6991 Enclosing_Instance := Current_Scope;
6992 while Present (Enclosing_Instance) loop
6993 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6995 if Ekind (Enclosing_Instance) = E_Package
6996 and then Is_Generic_Instance (Enclosing_Instance)
6997 and then Present
6998 (Generic_Parent (Specification (Instance_Decl)))
6999 then
7000 -- Check whether the generic we are looking for is a child of
7001 -- this instance.
7003 E := Find_Generic_Child
7004 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7005 exit when Present (E);
7007 else
7008 E := Empty;
7009 end if;
7011 Enclosing_Instance := Scope (Enclosing_Instance);
7012 end loop;
7014 if No (E) then
7016 -- Not a child unit
7018 Analyze (Gen_Id);
7019 return False;
7021 else
7022 Rewrite (Gen_Id,
7023 Make_Expanded_Name (Loc,
7024 Chars => Chars (E),
7025 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7026 Selector_Name => New_Occurrence_Of (E, Loc)));
7028 Set_Entity (Gen_Id, E);
7029 Set_Etype (Gen_Id, Etype (E));
7030 Parent_Installed := False; -- Already in scope.
7031 return True;
7032 end if;
7033 end In_Enclosing_Instance;
7035 -- Start of processing for Check_Generic_Child_Unit
7037 begin
7038 -- If the name of the generic is given by a selected component, it may
7039 -- be the name of a generic child unit, and the prefix is the name of an
7040 -- instance of the parent, in which case the child unit must be visible.
7041 -- If this instance is not in scope, it must be placed there and removed
7042 -- after instantiation, because what is being instantiated is not the
7043 -- original child, but the corresponding child present in the instance
7044 -- of the parent.
7046 -- If the child is instantiated within the parent, it can be given by
7047 -- a simple name. In this case the instance is already in scope, but
7048 -- the child generic must be recovered from the generic parent as well.
7050 if Nkind (Gen_Id) = N_Selected_Component then
7051 S := Selector_Name (Gen_Id);
7052 Analyze (Prefix (Gen_Id));
7053 Inst_Par := Entity (Prefix (Gen_Id));
7055 if Ekind (Inst_Par) = E_Package
7056 and then Present (Renamed_Object (Inst_Par))
7057 then
7058 Inst_Par := Renamed_Object (Inst_Par);
7059 end if;
7061 if Ekind (Inst_Par) = E_Package then
7062 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7063 Gen_Par := Generic_Parent (Parent (Inst_Par));
7065 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7066 and then
7067 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7068 then
7069 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7070 end if;
7072 elsif Ekind (Inst_Par) = E_Generic_Package
7073 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7074 then
7075 -- A formal package may be a real child package, and not the
7076 -- implicit instance within a parent. In this case the child is
7077 -- not visible and has to be retrieved explicitly as well.
7079 Gen_Par := Inst_Par;
7080 end if;
7082 if Present (Gen_Par) then
7084 -- The prefix denotes an instantiation. The entity itself may be a
7085 -- nested generic, or a child unit.
7087 E := Find_Generic_Child (Gen_Par, S);
7089 if Present (E) then
7090 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7091 Set_Entity (Gen_Id, E);
7092 Set_Etype (Gen_Id, Etype (E));
7093 Set_Entity (S, E);
7094 Set_Etype (S, Etype (E));
7096 -- Indicate that this is a reference to the parent
7098 if In_Extended_Main_Source_Unit (Gen_Id) then
7099 Set_Is_Instantiated (Inst_Par);
7100 end if;
7102 -- A common mistake is to replicate the naming scheme of a
7103 -- hierarchy by instantiating a generic child directly, rather
7104 -- than the implicit child in a parent instance:
7106 -- generic .. package Gpar is ..
7107 -- generic .. package Gpar.Child is ..
7108 -- package Par is new Gpar ();
7110 -- with Gpar.Child;
7111 -- package Par.Child is new Gpar.Child ();
7112 -- rather than Par.Child
7114 -- In this case the instantiation is within Par, which is an
7115 -- instance, but Gpar does not denote Par because we are not IN
7116 -- the instance of Gpar, so this is illegal. The test below
7117 -- recognizes this particular case.
7119 if Is_Child_Unit (E)
7120 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7121 and then (not In_Instance
7122 or else Nkind (Parent (Parent (Gen_Id))) =
7123 N_Compilation_Unit)
7124 then
7125 Error_Msg_N
7126 ("prefix of generic child unit must be instance of parent",
7127 Gen_Id);
7128 end if;
7130 if not In_Open_Scopes (Inst_Par)
7131 and then Nkind (Parent (Gen_Id)) not in
7132 N_Generic_Renaming_Declaration
7133 then
7134 Install_Parent (Inst_Par);
7135 Parent_Installed := True;
7137 elsif In_Open_Scopes (Inst_Par) then
7139 -- If the parent is already installed, install the actuals
7140 -- for its formal packages. This is necessary when the child
7141 -- instance is a child of the parent instance: in this case,
7142 -- the parent is placed on the scope stack but the formal
7143 -- packages are not made visible.
7145 Install_Formal_Packages (Inst_Par);
7146 end if;
7148 else
7149 -- If the generic parent does not contain an entity that
7150 -- corresponds to the selector, the instance doesn't either.
7151 -- Analyzing the node will yield the appropriate error message.
7152 -- If the entity is not a child unit, then it is an inner
7153 -- generic in the parent.
7155 Analyze (Gen_Id);
7156 end if;
7158 else
7159 Analyze (Gen_Id);
7161 if Is_Child_Unit (Entity (Gen_Id))
7162 and then
7163 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7164 and then not In_Open_Scopes (Inst_Par)
7165 then
7166 Install_Parent (Inst_Par);
7167 Parent_Installed := True;
7169 -- The generic unit may be the renaming of the implicit child
7170 -- present in an instance. In that case the parent instance is
7171 -- obtained from the name of the renamed entity.
7173 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7174 and then Present (Renamed_Entity (Entity (Gen_Id)))
7175 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7176 then
7177 declare
7178 Renamed_Package : constant Node_Id :=
7179 Name (Parent (Entity (Gen_Id)));
7180 begin
7181 if Nkind (Renamed_Package) = N_Expanded_Name then
7182 Inst_Par := Entity (Prefix (Renamed_Package));
7183 Install_Parent (Inst_Par);
7184 Parent_Installed := True;
7185 end if;
7186 end;
7187 end if;
7188 end if;
7190 elsif Nkind (Gen_Id) = N_Expanded_Name then
7192 -- Entity already present, analyze prefix, whose meaning may be an
7193 -- instance in the current context. If it is an instance of a
7194 -- relative within another, the proper parent may still have to be
7195 -- installed, if they are not of the same generation.
7197 Analyze (Prefix (Gen_Id));
7199 -- Prevent cascaded errors
7201 if Etype (Prefix (Gen_Id)) = Any_Type then
7202 return;
7203 end if;
7205 -- In the unlikely case that a local declaration hides the name of
7206 -- the parent package, locate it on the homonym chain. If the context
7207 -- is an instance of the parent, the renaming entity is flagged as
7208 -- such.
7210 Inst_Par := Entity (Prefix (Gen_Id));
7211 while Present (Inst_Par)
7212 and then not Is_Package_Or_Generic_Package (Inst_Par)
7213 loop
7214 Inst_Par := Homonym (Inst_Par);
7215 end loop;
7217 pragma Assert (Present (Inst_Par));
7218 Set_Entity (Prefix (Gen_Id), Inst_Par);
7220 if In_Enclosing_Instance then
7221 null;
7223 elsif Present (Entity (Gen_Id))
7224 and then Is_Child_Unit (Entity (Gen_Id))
7225 and then not In_Open_Scopes (Inst_Par)
7226 then
7227 Install_Parent (Inst_Par);
7228 Parent_Installed := True;
7229 end if;
7231 elsif In_Enclosing_Instance then
7233 -- The child unit is found in some enclosing scope
7235 null;
7237 else
7238 Analyze (Gen_Id);
7240 -- If this is the renaming of the implicit child in a parent
7241 -- instance, recover the parent name and install it.
7243 if Is_Entity_Name (Gen_Id) then
7244 E := Entity (Gen_Id);
7246 if Is_Generic_Unit (E)
7247 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7248 and then Is_Child_Unit (Renamed_Object (E))
7249 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7250 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7251 then
7252 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7253 Inst_Par := Entity (Prefix (Gen_Id));
7255 if not In_Open_Scopes (Inst_Par) then
7256 Install_Parent (Inst_Par);
7257 Parent_Installed := True;
7258 end if;
7260 -- If it is a child unit of a non-generic parent, it may be
7261 -- use-visible and given by a direct name. Install parent as
7262 -- for other cases.
7264 elsif Is_Generic_Unit (E)
7265 and then Is_Child_Unit (E)
7266 and then
7267 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7268 and then not Is_Generic_Unit (Scope (E))
7269 then
7270 if not In_Open_Scopes (Scope (E)) then
7271 Install_Parent (Scope (E));
7272 Parent_Installed := True;
7273 end if;
7274 end if;
7275 end if;
7276 end if;
7277 end Check_Generic_Child_Unit;
7279 -----------------------------
7280 -- Check_Hidden_Child_Unit --
7281 -----------------------------
7283 procedure Check_Hidden_Child_Unit
7284 (N : Node_Id;
7285 Gen_Unit : Entity_Id;
7286 Act_Decl_Id : Entity_Id)
7288 Gen_Id : constant Node_Id := Name (N);
7290 begin
7291 if Is_Child_Unit (Gen_Unit)
7292 and then Is_Child_Unit (Act_Decl_Id)
7293 and then Nkind (Gen_Id) = N_Expanded_Name
7294 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7295 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7296 then
7297 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7298 Error_Msg_NE
7299 ("generic unit & is implicitly declared in &",
7300 Defining_Unit_Name (N), Gen_Unit);
7301 Error_Msg_N ("\instance must have different name",
7302 Defining_Unit_Name (N));
7303 end if;
7304 end Check_Hidden_Child_Unit;
7306 ------------------------
7307 -- Check_Private_View --
7308 ------------------------
7310 procedure Check_Private_View (N : Node_Id) is
7311 T : constant Entity_Id := Etype (N);
7312 BT : Entity_Id;
7314 begin
7315 -- Exchange views if the type was not private in the generic but is
7316 -- private at the point of instantiation. Do not exchange views if
7317 -- the scope of the type is in scope. This can happen if both generic
7318 -- and instance are sibling units, or if type is defined in a parent.
7319 -- In this case the visibility of the type will be correct for all
7320 -- semantic checks.
7322 if Present (T) then
7323 BT := Base_Type (T);
7325 if Is_Private_Type (T)
7326 and then not Has_Private_View (N)
7327 and then Present (Full_View (T))
7328 and then not In_Open_Scopes (Scope (T))
7329 then
7330 -- In the generic, the full type was visible. Save the private
7331 -- entity, for subsequent exchange.
7333 Switch_View (T);
7335 elsif Has_Private_View (N)
7336 and then not Is_Private_Type (T)
7337 and then not Has_Been_Exchanged (T)
7338 and then Etype (Get_Associated_Node (N)) /= T
7339 then
7340 -- Only the private declaration was visible in the generic. If
7341 -- the type appears in a subtype declaration, the subtype in the
7342 -- instance must have a view compatible with that of its parent,
7343 -- which must be exchanged (see corresponding code in Restore_
7344 -- Private_Views). Otherwise, if the type is defined in a parent
7345 -- unit, leave full visibility within instance, which is safe.
7347 if In_Open_Scopes (Scope (Base_Type (T)))
7348 and then not Is_Private_Type (Base_Type (T))
7349 and then Comes_From_Source (Base_Type (T))
7350 then
7351 null;
7353 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7354 or else not In_Private_Part (Scope (Base_Type (T)))
7355 then
7356 Prepend_Elmt (T, Exchanged_Views);
7357 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7358 end if;
7360 -- For composite types with inconsistent representation exchange
7361 -- component types accordingly.
7363 elsif Is_Access_Type (T)
7364 and then Is_Private_Type (Designated_Type (T))
7365 and then not Has_Private_View (N)
7366 and then Present (Full_View (Designated_Type (T)))
7367 then
7368 Switch_View (Designated_Type (T));
7370 elsif Is_Array_Type (T) then
7371 if Is_Private_Type (Component_Type (T))
7372 and then not Has_Private_View (N)
7373 and then Present (Full_View (Component_Type (T)))
7374 then
7375 Switch_View (Component_Type (T));
7376 end if;
7378 -- The normal exchange mechanism relies on the setting of a
7379 -- flag on the reference in the generic. However, an additional
7380 -- mechanism is needed for types that are not explicitly
7381 -- mentioned in the generic, but may be needed in expanded code
7382 -- in the instance. This includes component types of arrays and
7383 -- designated types of access types. This processing must also
7384 -- include the index types of arrays which we take care of here.
7386 declare
7387 Indx : Node_Id;
7388 Typ : Entity_Id;
7390 begin
7391 Indx := First_Index (T);
7392 while Present (Indx) loop
7393 Typ := Base_Type (Etype (Indx));
7395 if Is_Private_Type (Typ)
7396 and then Present (Full_View (Typ))
7397 then
7398 Switch_View (Typ);
7399 end if;
7401 Next_Index (Indx);
7402 end loop;
7403 end;
7405 elsif Is_Private_Type (T)
7406 and then Present (Full_View (T))
7407 and then Is_Array_Type (Full_View (T))
7408 and then Is_Private_Type (Component_Type (Full_View (T)))
7409 then
7410 Switch_View (T);
7412 -- Finally, a non-private subtype may have a private base type, which
7413 -- must be exchanged for consistency. This can happen when a package
7414 -- body is instantiated, when the scope stack is empty but in fact
7415 -- the subtype and the base type are declared in an enclosing scope.
7417 -- Note that in this case we introduce an inconsistency in the view
7418 -- set, because we switch the base type BT, but there could be some
7419 -- private dependent subtypes of BT which remain unswitched. Such
7420 -- subtypes might need to be switched at a later point (see specific
7421 -- provision for that case in Switch_View).
7423 elsif not Is_Private_Type (T)
7424 and then not Has_Private_View (N)
7425 and then Is_Private_Type (BT)
7426 and then Present (Full_View (BT))
7427 and then not Is_Generic_Type (BT)
7428 and then not In_Open_Scopes (BT)
7429 then
7430 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7431 Exchange_Declarations (BT);
7432 end if;
7433 end if;
7434 end Check_Private_View;
7436 -----------------------------
7437 -- Check_Hidden_Primitives --
7438 -----------------------------
7440 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7441 Actual : Node_Id;
7442 Gen_T : Entity_Id;
7443 Result : Elist_Id := No_Elist;
7445 begin
7446 if No (Assoc_List) then
7447 return No_Elist;
7448 end if;
7450 -- Traverse the list of associations between formals and actuals
7451 -- searching for renamings of tagged types
7453 Actual := First (Assoc_List);
7454 while Present (Actual) loop
7455 if Nkind (Actual) = N_Subtype_Declaration then
7456 Gen_T := Generic_Parent_Type (Actual);
7458 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7460 -- Traverse the list of primitives of the actual types
7461 -- searching for hidden primitives that are visible in the
7462 -- corresponding generic formal; leave them visible and
7463 -- append them to Result to restore their decoration later.
7465 Install_Hidden_Primitives
7466 (Prims_List => Result,
7467 Gen_T => Gen_T,
7468 Act_T => Entity (Subtype_Indication (Actual)));
7469 end if;
7470 end if;
7472 Next (Actual);
7473 end loop;
7475 return Result;
7476 end Check_Hidden_Primitives;
7478 --------------------------
7479 -- Contains_Instance_Of --
7480 --------------------------
7482 function Contains_Instance_Of
7483 (Inner : Entity_Id;
7484 Outer : Entity_Id;
7485 N : Node_Id) return Boolean
7487 Elmt : Elmt_Id;
7488 Scop : Entity_Id;
7490 begin
7491 Scop := Outer;
7493 -- Verify that there are no circular instantiations. We check whether
7494 -- the unit contains an instance of the current scope or some enclosing
7495 -- scope (in case one of the instances appears in a subunit). Longer
7496 -- circularities involving subunits might seem too pathological to
7497 -- consider, but they were not too pathological for the authors of
7498 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7499 -- enclosing generic scopes as containing an instance.
7501 loop
7502 -- Within a generic subprogram body, the scope is not generic, to
7503 -- allow for recursive subprograms. Use the declaration to determine
7504 -- whether this is a generic unit.
7506 if Ekind (Scop) = E_Generic_Package
7507 or else (Is_Subprogram (Scop)
7508 and then Nkind (Unit_Declaration_Node (Scop)) =
7509 N_Generic_Subprogram_Declaration)
7510 then
7511 Elmt := First_Elmt (Inner_Instances (Inner));
7513 while Present (Elmt) loop
7514 if Node (Elmt) = Scop then
7515 Error_Msg_Node_2 := Inner;
7516 Error_Msg_NE
7517 ("circular Instantiation: & instantiated within &!",
7518 N, Scop);
7519 return True;
7521 elsif Node (Elmt) = Inner then
7522 return True;
7524 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7525 Error_Msg_Node_2 := Inner;
7526 Error_Msg_NE
7527 ("circular Instantiation: & instantiated within &!",
7528 N, Node (Elmt));
7529 return True;
7530 end if;
7532 Next_Elmt (Elmt);
7533 end loop;
7535 -- Indicate that Inner is being instantiated within Scop
7537 Append_Elmt (Inner, Inner_Instances (Scop));
7538 end if;
7540 if Scop = Standard_Standard then
7541 exit;
7542 else
7543 Scop := Scope (Scop);
7544 end if;
7545 end loop;
7547 return False;
7548 end Contains_Instance_Of;
7550 -----------------------
7551 -- Copy_Generic_Node --
7552 -----------------------
7554 function Copy_Generic_Node
7555 (N : Node_Id;
7556 Parent_Id : Node_Id;
7557 Instantiating : Boolean) return Node_Id
7559 Ent : Entity_Id;
7560 New_N : Node_Id;
7562 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7563 -- Check the given value of one of the Fields referenced by the current
7564 -- node to determine whether to copy it recursively. The field may hold
7565 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7566 -- Char) in which case it need not be copied.
7568 procedure Copy_Descendants;
7569 -- Common utility for various nodes
7571 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7572 -- Make copy of element list
7574 function Copy_Generic_List
7575 (L : List_Id;
7576 Parent_Id : Node_Id) return List_Id;
7577 -- Apply Copy_Node recursively to the members of a node list
7579 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7580 -- True if an identifier is part of the defining program unit name of
7581 -- a child unit. The entity of such an identifier must be kept (for
7582 -- ASIS use) even though as the name of an enclosing generic it would
7583 -- otherwise not be preserved in the generic tree.
7585 ----------------------
7586 -- Copy_Descendants --
7587 ----------------------
7589 procedure Copy_Descendants is
7590 use Atree.Unchecked_Access;
7591 -- This code section is part of the implementation of an untyped
7592 -- tree traversal, so it needs direct access to node fields.
7594 begin
7595 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7596 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7597 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7598 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7599 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7600 end Copy_Descendants;
7602 -----------------------------
7603 -- Copy_Generic_Descendant --
7604 -----------------------------
7606 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7607 begin
7608 if D = Union_Id (Empty) then
7609 return D;
7611 elsif D in Node_Range then
7612 return Union_Id
7613 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7615 elsif D in List_Range then
7616 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7618 elsif D in Elist_Range then
7619 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7621 -- Nothing else is copyable (e.g. Uint values), return as is
7623 else
7624 return D;
7625 end if;
7626 end Copy_Generic_Descendant;
7628 ------------------------
7629 -- Copy_Generic_Elist --
7630 ------------------------
7632 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7633 M : Elmt_Id;
7634 L : Elist_Id;
7636 begin
7637 if Present (E) then
7638 L := New_Elmt_List;
7639 M := First_Elmt (E);
7640 while Present (M) loop
7641 Append_Elmt
7642 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7643 Next_Elmt (M);
7644 end loop;
7646 return L;
7648 else
7649 return No_Elist;
7650 end if;
7651 end Copy_Generic_Elist;
7653 -----------------------
7654 -- Copy_Generic_List --
7655 -----------------------
7657 function Copy_Generic_List
7658 (L : List_Id;
7659 Parent_Id : Node_Id) return List_Id
7661 N : Node_Id;
7662 New_L : List_Id;
7664 begin
7665 if Present (L) then
7666 New_L := New_List;
7667 Set_Parent (New_L, Parent_Id);
7669 N := First (L);
7670 while Present (N) loop
7671 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7672 Next (N);
7673 end loop;
7675 return New_L;
7677 else
7678 return No_List;
7679 end if;
7680 end Copy_Generic_List;
7682 ---------------------------
7683 -- In_Defining_Unit_Name --
7684 ---------------------------
7686 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7687 begin
7688 return
7689 Present (Parent (Nam))
7690 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7691 or else
7692 (Nkind (Parent (Nam)) = N_Expanded_Name
7693 and then In_Defining_Unit_Name (Parent (Nam))));
7694 end In_Defining_Unit_Name;
7696 -- Start of processing for Copy_Generic_Node
7698 begin
7699 if N = Empty then
7700 return N;
7701 end if;
7703 New_N := New_Copy (N);
7705 -- Copy aspects if present
7707 if Has_Aspects (N) then
7708 Set_Has_Aspects (New_N, False);
7709 Set_Aspect_Specifications
7710 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7711 end if;
7713 -- If we are instantiating, we want to adjust the sloc based on the
7714 -- current S_Adjustment. However, if this is the root node of a subunit,
7715 -- we need to defer that adjustment to below (see "elsif Instantiating
7716 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7717 -- computed the adjustment.
7719 if Instantiating
7720 and then not (Nkind (N) in N_Proper_Body
7721 and then Was_Originally_Stub (N))
7722 then
7723 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7724 end if;
7726 if not Is_List_Member (N) then
7727 Set_Parent (New_N, Parent_Id);
7728 end if;
7730 -- Special casing for identifiers and other entity names and operators
7732 if Nkind_In (New_N, N_Character_Literal,
7733 N_Expanded_Name,
7734 N_Identifier,
7735 N_Operator_Symbol)
7736 or else Nkind (New_N) in N_Op
7737 then
7738 if not Instantiating then
7740 -- Link both nodes in order to assign subsequently the entity of
7741 -- the copy to the original node, in case this is a global
7742 -- reference.
7744 Set_Associated_Node (N, New_N);
7746 -- If we are within an instantiation, this is a nested generic
7747 -- that has already been analyzed at the point of definition.
7748 -- We must preserve references that were global to the enclosing
7749 -- parent at that point. Other occurrences, whether global or
7750 -- local to the current generic, must be resolved anew, so we
7751 -- reset the entity in the generic copy. A global reference has a
7752 -- smaller depth than the parent, or else the same depth in case
7753 -- both are distinct compilation units.
7755 -- A child unit is implicitly declared within the enclosing parent
7756 -- but is in fact global to it, and must be preserved.
7758 -- It is also possible for Current_Instantiated_Parent to be
7759 -- defined, and for this not to be a nested generic, namely if
7760 -- the unit is loaded through Rtsfind. In that case, the entity of
7761 -- New_N is only a link to the associated node, and not a defining
7762 -- occurrence.
7764 -- The entities for parent units in the defining_program_unit of a
7765 -- generic child unit are established when the context of the unit
7766 -- is first analyzed, before the generic copy is made. They are
7767 -- preserved in the copy for use in ASIS queries.
7769 Ent := Entity (New_N);
7771 if No (Current_Instantiated_Parent.Gen_Id) then
7772 if No (Ent)
7773 or else Nkind (Ent) /= N_Defining_Identifier
7774 or else not In_Defining_Unit_Name (N)
7775 then
7776 Set_Associated_Node (New_N, Empty);
7777 end if;
7779 elsif No (Ent)
7780 or else
7781 not Nkind_In (Ent, N_Defining_Identifier,
7782 N_Defining_Character_Literal,
7783 N_Defining_Operator_Symbol)
7784 or else No (Scope (Ent))
7785 or else
7786 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7787 and then not Is_Child_Unit (Ent))
7788 or else
7789 (Scope_Depth (Scope (Ent)) >
7790 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7791 and then
7792 Get_Source_Unit (Ent) =
7793 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7794 then
7795 Set_Associated_Node (New_N, Empty);
7796 end if;
7798 -- Case of instantiating identifier or some other name or operator
7800 else
7801 -- If the associated node is still defined, the entity in it
7802 -- is global, and must be copied to the instance. If this copy
7803 -- is being made for a body to inline, it is applied to an
7804 -- instantiated tree, and the entity is already present and
7805 -- must be also preserved.
7807 declare
7808 Assoc : constant Node_Id := Get_Associated_Node (N);
7810 begin
7811 if Present (Assoc) then
7812 if Nkind (Assoc) = Nkind (N) then
7813 Set_Entity (New_N, Entity (Assoc));
7814 Check_Private_View (N);
7816 -- The node is a reference to a global type and acts as the
7817 -- subtype mark of a qualified expression created in order
7818 -- to aid resolution of accidental overloading in instances.
7819 -- Since N is a reference to a type, the Associated_Node of
7820 -- N denotes an entity rather than another identifier. See
7821 -- Qualify_Universal_Operands for details.
7823 elsif Nkind (N) = N_Identifier
7824 and then Nkind (Parent (N)) = N_Qualified_Expression
7825 and then Subtype_Mark (Parent (N)) = N
7826 and then Is_Qualified_Universal_Literal (Parent (N))
7827 then
7828 Set_Entity (New_N, Assoc);
7830 -- The name in the call may be a selected component if the
7831 -- call has not been analyzed yet, as may be the case for
7832 -- pre/post conditions in a generic unit.
7834 elsif Nkind (Assoc) = N_Function_Call
7835 and then Is_Entity_Name (Name (Assoc))
7836 then
7837 Set_Entity (New_N, Entity (Name (Assoc)));
7839 elsif Nkind_In (Assoc, N_Defining_Identifier,
7840 N_Defining_Character_Literal,
7841 N_Defining_Operator_Symbol)
7842 and then Expander_Active
7843 then
7844 -- Inlining case: we are copying a tree that contains
7845 -- global entities, which are preserved in the copy to be
7846 -- used for subsequent inlining.
7848 null;
7850 else
7851 Set_Entity (New_N, Empty);
7852 end if;
7853 end if;
7854 end;
7855 end if;
7857 -- For expanded name, we must copy the Prefix and Selector_Name
7859 if Nkind (N) = N_Expanded_Name then
7860 Set_Prefix
7861 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7863 Set_Selector_Name (New_N,
7864 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7866 -- For operators, copy the operands
7868 elsif Nkind (N) in N_Op then
7869 if Nkind (N) in N_Binary_Op then
7870 Set_Left_Opnd (New_N,
7871 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7872 end if;
7874 Set_Right_Opnd (New_N,
7875 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7876 end if;
7878 -- Establish a link between an entity from the generic template and the
7879 -- corresponding entity in the generic copy to be analyzed.
7881 elsif Nkind (N) in N_Entity then
7882 if not Instantiating then
7883 Set_Associated_Entity (N, New_N);
7884 end if;
7886 -- Clear any existing link the copy may inherit from the replicated
7887 -- generic template entity.
7889 Set_Associated_Entity (New_N, Empty);
7891 -- Special casing for stubs
7893 elsif Nkind (N) in N_Body_Stub then
7895 -- In any case, we must copy the specification or defining
7896 -- identifier as appropriate.
7898 if Nkind (N) = N_Subprogram_Body_Stub then
7899 Set_Specification (New_N,
7900 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7902 else
7903 Set_Defining_Identifier (New_N,
7904 Copy_Generic_Node
7905 (Defining_Identifier (N), New_N, Instantiating));
7906 end if;
7908 -- If we are not instantiating, then this is where we load and
7909 -- analyze subunits, i.e. at the point where the stub occurs. A
7910 -- more permissive system might defer this analysis to the point
7911 -- of instantiation, but this seems too complicated for now.
7913 if not Instantiating then
7914 declare
7915 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7916 Subunit : Node_Id;
7917 Unum : Unit_Number_Type;
7918 New_Body : Node_Id;
7920 begin
7921 -- Make sure that, if it is a subunit of the main unit that is
7922 -- preprocessed and if -gnateG is specified, the preprocessed
7923 -- file will be written.
7925 Lib.Analysing_Subunit_Of_Main :=
7926 Lib.In_Extended_Main_Source_Unit (N);
7927 Unum :=
7928 Load_Unit
7929 (Load_Name => Subunit_Name,
7930 Required => False,
7931 Subunit => True,
7932 Error_Node => N);
7933 Lib.Analysing_Subunit_Of_Main := False;
7935 -- If the proper body is not found, a warning message will be
7936 -- emitted when analyzing the stub, or later at the point of
7937 -- instantiation. Here we just leave the stub as is.
7939 if Unum = No_Unit then
7940 Subunits_Missing := True;
7941 goto Subunit_Not_Found;
7942 end if;
7944 Subunit := Cunit (Unum);
7946 if Nkind (Unit (Subunit)) /= N_Subunit then
7947 Error_Msg_N
7948 ("found child unit instead of expected SEPARATE subunit",
7949 Subunit);
7950 Error_Msg_Sloc := Sloc (N);
7951 Error_Msg_N ("\to complete stub #", Subunit);
7952 goto Subunit_Not_Found;
7953 end if;
7955 -- We must create a generic copy of the subunit, in order to
7956 -- perform semantic analysis on it, and we must replace the
7957 -- stub in the original generic unit with the subunit, in order
7958 -- to preserve non-local references within.
7960 -- Only the proper body needs to be copied. Library_Unit and
7961 -- context clause are simply inherited by the generic copy.
7962 -- Note that the copy (which may be recursive if there are
7963 -- nested subunits) must be done first, before attaching it to
7964 -- the enclosing generic.
7966 New_Body :=
7967 Copy_Generic_Node
7968 (Proper_Body (Unit (Subunit)),
7969 Empty, Instantiating => False);
7971 -- Now place the original proper body in the original generic
7972 -- unit. This is a body, not a compilation unit.
7974 Rewrite (N, Proper_Body (Unit (Subunit)));
7975 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7976 Set_Was_Originally_Stub (N);
7978 -- Finally replace the body of the subunit with its copy, and
7979 -- make this new subunit into the library unit of the generic
7980 -- copy, which does not have stubs any longer.
7982 Set_Proper_Body (Unit (Subunit), New_Body);
7983 Set_Library_Unit (New_N, Subunit);
7984 Inherit_Context (Unit (Subunit), N);
7985 end;
7987 -- If we are instantiating, this must be an error case, since
7988 -- otherwise we would have replaced the stub node by the proper body
7989 -- that corresponds. So just ignore it in the copy (i.e. we have
7990 -- copied it, and that is good enough).
7992 else
7993 null;
7994 end if;
7996 <<Subunit_Not_Found>> null;
7998 -- If the node is a compilation unit, it is the subunit of a stub, which
7999 -- has been loaded already (see code below). In this case, the library
8000 -- unit field of N points to the parent unit (which is a compilation
8001 -- unit) and need not (and cannot) be copied.
8003 -- When the proper body of the stub is analyzed, the library_unit link
8004 -- is used to establish the proper context (see sem_ch10).
8006 -- The other fields of a compilation unit are copied as usual
8008 elsif Nkind (N) = N_Compilation_Unit then
8010 -- This code can only be executed when not instantiating, because in
8011 -- the copy made for an instantiation, the compilation unit node has
8012 -- disappeared at the point that a stub is replaced by its proper
8013 -- body.
8015 pragma Assert (not Instantiating);
8017 Set_Context_Items (New_N,
8018 Copy_Generic_List (Context_Items (N), New_N));
8020 Set_Unit (New_N,
8021 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8023 Set_First_Inlined_Subprogram (New_N,
8024 Copy_Generic_Node
8025 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8027 Set_Aux_Decls_Node
8028 (New_N,
8029 Copy_Generic_Node
8030 (Aux_Decls_Node (N), New_N, Instantiating => False));
8032 -- For an assignment node, the assignment is known to be semantically
8033 -- legal if we are instantiating the template. This avoids incorrect
8034 -- diagnostics in generated code.
8036 elsif Nkind (N) = N_Assignment_Statement then
8038 -- Copy name and expression fields in usual manner
8040 Set_Name (New_N,
8041 Copy_Generic_Node (Name (N), New_N, Instantiating));
8043 Set_Expression (New_N,
8044 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8046 if Instantiating then
8047 Set_Assignment_OK (Name (New_N), True);
8048 end if;
8050 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8051 if not Instantiating then
8052 Set_Associated_Node (N, New_N);
8054 else
8055 if Present (Get_Associated_Node (N))
8056 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8057 then
8058 -- In the generic the aggregate has some composite type. If at
8059 -- the point of instantiation the type has a private view,
8060 -- install the full view (and that of its ancestors, if any).
8062 declare
8063 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8064 Rt : Entity_Id;
8066 begin
8067 if Present (T) and then Is_Private_Type (T) then
8068 Switch_View (T);
8069 end if;
8071 if Present (T)
8072 and then Is_Tagged_Type (T)
8073 and then Is_Derived_Type (T)
8074 then
8075 Rt := Root_Type (T);
8077 loop
8078 T := Etype (T);
8080 if Is_Private_Type (T) then
8081 Switch_View (T);
8082 end if;
8084 exit when T = Rt;
8085 end loop;
8086 end if;
8087 end;
8088 end if;
8089 end if;
8091 -- Do not copy the associated node, which points to the generic copy
8092 -- of the aggregate.
8094 declare
8095 use Atree.Unchecked_Access;
8096 -- This code section is part of the implementation of an untyped
8097 -- tree traversal, so it needs direct access to node fields.
8099 begin
8100 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8101 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8102 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8103 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8104 end;
8106 -- Allocators do not have an identifier denoting the access type, so we
8107 -- must locate it through the expression to check whether the views are
8108 -- consistent.
8110 elsif Nkind (N) = N_Allocator
8111 and then Nkind (Expression (N)) = N_Qualified_Expression
8112 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8113 and then Instantiating
8114 then
8115 declare
8116 T : constant Node_Id :=
8117 Get_Associated_Node (Subtype_Mark (Expression (N)));
8118 Acc_T : Entity_Id;
8120 begin
8121 if Present (T) then
8123 -- Retrieve the allocator node in the generic copy
8125 Acc_T := Etype (Parent (Parent (T)));
8127 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8128 Switch_View (Acc_T);
8129 end if;
8130 end if;
8132 Copy_Descendants;
8133 end;
8135 -- For a proper body, we must catch the case of a proper body that
8136 -- replaces a stub. This represents the point at which a separate
8137 -- compilation unit, and hence template file, may be referenced, so we
8138 -- must make a new source instantiation entry for the template of the
8139 -- subunit, and ensure that all nodes in the subunit are adjusted using
8140 -- this new source instantiation entry.
8142 elsif Nkind (N) in N_Proper_Body then
8143 declare
8144 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8145 begin
8146 if Instantiating and then Was_Originally_Stub (N) then
8147 Create_Instantiation_Source
8148 (Instantiation_Node,
8149 Defining_Entity (N),
8150 S_Adjustment);
8152 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8153 end if;
8155 -- Now copy the fields of the proper body, using the new
8156 -- adjustment factor if one was needed as per test above.
8158 Copy_Descendants;
8160 -- Restore the original adjustment factor
8162 S_Adjustment := Save_Adjustment;
8163 end;
8165 elsif Nkind (N) = N_Pragma and then Instantiating then
8167 -- Do not copy Comment or Ident pragmas their content is relevant to
8168 -- the generic unit, not to the instantiating unit.
8170 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8171 New_N := Make_Null_Statement (Sloc (N));
8173 -- Do not copy pragmas generated from aspects because the pragmas do
8174 -- not carry any semantic information, plus they will be regenerated
8175 -- in the instance.
8177 -- However, generating C we need to copy them since postconditions
8178 -- are inlined by the front end, and the front-end inlining machinery
8179 -- relies on this routine to perform inlining.
8181 elsif From_Aspect_Specification (N)
8182 and then not Modify_Tree_For_C
8183 then
8184 New_N := Make_Null_Statement (Sloc (N));
8186 else
8187 Copy_Descendants;
8188 end if;
8190 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8192 -- No descendant fields need traversing
8194 null;
8196 elsif Nkind (N) = N_String_Literal
8197 and then Present (Etype (N))
8198 and then Instantiating
8199 then
8200 -- If the string is declared in an outer scope, the string_literal
8201 -- subtype created for it may have the wrong scope. Force reanalysis
8202 -- of the constant to generate a new itype in the proper context.
8204 Set_Etype (New_N, Empty);
8205 Set_Analyzed (New_N, False);
8207 -- For the remaining nodes, copy their descendants recursively
8209 else
8210 Copy_Descendants;
8212 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8213 Set_Generic_Parent (Specification (New_N), N);
8215 -- Should preserve Corresponding_Spec??? (12.3(14))
8216 end if;
8217 end if;
8219 -- Propagate dimensions if present, so that they are reflected in the
8220 -- instance.
8222 if Nkind (N) in N_Has_Etype
8223 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8224 and then Present (Etype (N))
8225 and then Is_Floating_Point_Type (Etype (N))
8226 and then Has_Dimension_System (Etype (N))
8227 then
8228 Copy_Dimensions (N, New_N);
8229 end if;
8231 return New_N;
8232 end Copy_Generic_Node;
8234 ----------------------------
8235 -- Denotes_Formal_Package --
8236 ----------------------------
8238 function Denotes_Formal_Package
8239 (Pack : Entity_Id;
8240 On_Exit : Boolean := False;
8241 Instance : Entity_Id := Empty) return Boolean
8243 Par : Entity_Id;
8244 Scop : constant Entity_Id := Scope (Pack);
8245 E : Entity_Id;
8247 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8248 -- The package in question may be an actual for a previous formal
8249 -- package P of the current instance, so examine its actuals as well.
8250 -- This must be recursive over other formal packages.
8252 ----------------------------------
8253 -- Is_Actual_Of_Previous_Formal --
8254 ----------------------------------
8256 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8257 E1 : Entity_Id;
8259 begin
8260 E1 := First_Entity (P);
8261 while Present (E1) and then E1 /= Instance loop
8262 if Ekind (E1) = E_Package
8263 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8264 then
8265 if Renamed_Object (E1) = Pack then
8266 return True;
8268 elsif E1 = P or else Renamed_Object (E1) = P then
8269 return False;
8271 elsif Is_Actual_Of_Previous_Formal (E1) then
8272 return True;
8273 end if;
8274 end if;
8276 Next_Entity (E1);
8277 end loop;
8279 return False;
8280 end Is_Actual_Of_Previous_Formal;
8282 -- Start of processing for Denotes_Formal_Package
8284 begin
8285 if On_Exit then
8286 Par :=
8287 Instance_Envs.Table
8288 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8289 else
8290 Par := Current_Instantiated_Parent.Act_Id;
8291 end if;
8293 if Ekind (Scop) = E_Generic_Package
8294 or else Nkind (Unit_Declaration_Node (Scop)) =
8295 N_Generic_Subprogram_Declaration
8296 then
8297 return True;
8299 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8300 N_Formal_Package_Declaration
8301 then
8302 return True;
8304 elsif No (Par) then
8305 return False;
8307 else
8308 -- Check whether this package is associated with a formal package of
8309 -- the enclosing instantiation. Iterate over the list of renamings.
8311 E := First_Entity (Par);
8312 while Present (E) loop
8313 if Ekind (E) /= E_Package
8314 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8315 then
8316 null;
8318 elsif Renamed_Object (E) = Par then
8319 return False;
8321 elsif Renamed_Object (E) = Pack then
8322 return True;
8324 elsif Is_Actual_Of_Previous_Formal (E) then
8325 return True;
8327 end if;
8329 Next_Entity (E);
8330 end loop;
8332 return False;
8333 end if;
8334 end Denotes_Formal_Package;
8336 -----------------
8337 -- End_Generic --
8338 -----------------
8340 procedure End_Generic is
8341 begin
8342 -- ??? More things could be factored out in this routine. Should
8343 -- probably be done at a later stage.
8345 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8346 Generic_Flags.Decrement_Last;
8348 Expander_Mode_Restore;
8349 end End_Generic;
8351 -------------
8352 -- Earlier --
8353 -------------
8355 function Earlier (N1, N2 : Node_Id) return Boolean is
8356 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8357 -- Find distance from given node to enclosing compilation unit
8359 ----------------
8360 -- Find_Depth --
8361 ----------------
8363 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8364 begin
8365 while Present (P)
8366 and then Nkind (P) /= N_Compilation_Unit
8367 loop
8368 P := True_Parent (P);
8369 D := D + 1;
8370 end loop;
8371 end Find_Depth;
8373 -- Local declarations
8375 D1 : Integer := 0;
8376 D2 : Integer := 0;
8377 P1 : Node_Id := N1;
8378 P2 : Node_Id := N2;
8379 T1 : Source_Ptr;
8380 T2 : Source_Ptr;
8382 -- Start of processing for Earlier
8384 begin
8385 Find_Depth (P1, D1);
8386 Find_Depth (P2, D2);
8388 if P1 /= P2 then
8389 return False;
8390 else
8391 P1 := N1;
8392 P2 := N2;
8393 end if;
8395 while D1 > D2 loop
8396 P1 := True_Parent (P1);
8397 D1 := D1 - 1;
8398 end loop;
8400 while D2 > D1 loop
8401 P2 := True_Parent (P2);
8402 D2 := D2 - 1;
8403 end loop;
8405 -- At this point P1 and P2 are at the same distance from the root.
8406 -- We examine their parents until we find a common declarative list.
8407 -- If we reach the root, N1 and N2 do not descend from the same
8408 -- declarative list (e.g. one is nested in the declarative part and
8409 -- the other is in a block in the statement part) and the earlier
8410 -- one is already frozen.
8412 while not Is_List_Member (P1)
8413 or else not Is_List_Member (P2)
8414 or else List_Containing (P1) /= List_Containing (P2)
8415 loop
8416 P1 := True_Parent (P1);
8417 P2 := True_Parent (P2);
8419 if Nkind (Parent (P1)) = N_Subunit then
8420 P1 := Corresponding_Stub (Parent (P1));
8421 end if;
8423 if Nkind (Parent (P2)) = N_Subunit then
8424 P2 := Corresponding_Stub (Parent (P2));
8425 end if;
8427 if P1 = P2 then
8428 return False;
8429 end if;
8430 end loop;
8432 -- Expanded code usually shares the source location of the original
8433 -- construct it was generated for. This however may not necessarily
8434 -- reflect the true location of the code within the tree.
8436 -- Before comparing the slocs of the two nodes, make sure that we are
8437 -- working with correct source locations. Assume that P1 is to the left
8438 -- of P2. If either one does not come from source, traverse the common
8439 -- list heading towards the other node and locate the first source
8440 -- statement.
8442 -- P1 P2
8443 -- ----+===+===+--------------+===+===+----
8444 -- expanded code expanded code
8446 if not Comes_From_Source (P1) then
8447 while Present (P1) loop
8449 -- Neither P2 nor a source statement were located during the
8450 -- search. If we reach the end of the list, then P1 does not
8451 -- occur earlier than P2.
8453 -- ---->
8454 -- start --- P2 ----- P1 --- end
8456 if No (Next (P1)) then
8457 return False;
8459 -- We encounter P2 while going to the right of the list. This
8460 -- means that P1 does indeed appear earlier.
8462 -- ---->
8463 -- start --- P1 ===== P2 --- end
8464 -- expanded code in between
8466 elsif P1 = P2 then
8467 return True;
8469 -- No need to look any further since we have located a source
8470 -- statement.
8472 elsif Comes_From_Source (P1) then
8473 exit;
8474 end if;
8476 -- Keep going right
8478 Next (P1);
8479 end loop;
8480 end if;
8482 if not Comes_From_Source (P2) then
8483 while Present (P2) loop
8485 -- Neither P1 nor a source statement were located during the
8486 -- search. If we reach the start of the list, then P1 does not
8487 -- occur earlier than P2.
8489 -- <----
8490 -- start --- P2 --- P1 --- end
8492 if No (Prev (P2)) then
8493 return False;
8495 -- We encounter P1 while going to the left of the list. This
8496 -- means that P1 does indeed appear earlier.
8498 -- <----
8499 -- start --- P1 ===== P2 --- end
8500 -- expanded code in between
8502 elsif P2 = P1 then
8503 return True;
8505 -- No need to look any further since we have located a source
8506 -- statement.
8508 elsif Comes_From_Source (P2) then
8509 exit;
8510 end if;
8512 -- Keep going left
8514 Prev (P2);
8515 end loop;
8516 end if;
8518 -- At this point either both nodes came from source or we approximated
8519 -- their source locations through neighboring source statements.
8521 T1 := Top_Level_Location (Sloc (P1));
8522 T2 := Top_Level_Location (Sloc (P2));
8524 -- When two nodes come from the same instance, they have identical top
8525 -- level locations. To determine proper relation within the tree, check
8526 -- their locations within the template.
8528 if T1 = T2 then
8529 return Sloc (P1) < Sloc (P2);
8531 -- The two nodes either come from unrelated instances or do not come
8532 -- from instantiated code at all.
8534 else
8535 return T1 < T2;
8536 end if;
8537 end Earlier;
8539 ----------------------
8540 -- Find_Actual_Type --
8541 ----------------------
8543 function Find_Actual_Type
8544 (Typ : Entity_Id;
8545 Gen_Type : Entity_Id) return Entity_Id
8547 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8548 T : Entity_Id;
8550 begin
8551 -- Special processing only applies to child units
8553 if not Is_Child_Unit (Gen_Scope) then
8554 return Get_Instance_Of (Typ);
8556 -- If designated or component type is itself a formal of the child unit,
8557 -- its instance is available.
8559 elsif Scope (Typ) = Gen_Scope then
8560 return Get_Instance_Of (Typ);
8562 -- If the array or access type is not declared in the parent unit,
8563 -- no special processing needed.
8565 elsif not Is_Generic_Type (Typ)
8566 and then Scope (Gen_Scope) /= Scope (Typ)
8567 then
8568 return Get_Instance_Of (Typ);
8570 -- Otherwise, retrieve designated or component type by visibility
8572 else
8573 T := Current_Entity (Typ);
8574 while Present (T) loop
8575 if In_Open_Scopes (Scope (T)) then
8576 return T;
8577 elsif Is_Generic_Actual_Type (T) then
8578 return T;
8579 end if;
8581 T := Homonym (T);
8582 end loop;
8584 return Typ;
8585 end if;
8586 end Find_Actual_Type;
8588 ----------------------------
8589 -- Freeze_Subprogram_Body --
8590 ----------------------------
8592 procedure Freeze_Subprogram_Body
8593 (Inst_Node : Node_Id;
8594 Gen_Body : Node_Id;
8595 Pack_Id : Entity_Id)
8597 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8598 Par : constant Entity_Id := Scope (Gen_Unit);
8599 E_G_Id : Entity_Id;
8600 Enc_G : Entity_Id;
8601 Enc_I : Node_Id;
8602 F_Node : Node_Id;
8604 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8605 -- Find innermost package body that encloses the given node, and which
8606 -- is not a compilation unit. Freeze nodes for the instance, or for its
8607 -- enclosing body, may be inserted after the enclosing_body of the
8608 -- generic unit. Used to determine proper placement of freeze node for
8609 -- both package and subprogram instances.
8611 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8612 -- Find entity for given package body, and locate or create a freeze
8613 -- node for it.
8615 ----------------------------
8616 -- Enclosing_Package_Body --
8617 ----------------------------
8619 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8620 P : Node_Id;
8622 begin
8623 P := Parent (N);
8624 while Present (P)
8625 and then Nkind (Parent (P)) /= N_Compilation_Unit
8626 loop
8627 if Nkind (P) = N_Package_Body then
8628 if Nkind (Parent (P)) = N_Subunit then
8629 return Corresponding_Stub (Parent (P));
8630 else
8631 return P;
8632 end if;
8633 end if;
8635 P := True_Parent (P);
8636 end loop;
8638 return Empty;
8639 end Enclosing_Package_Body;
8641 -------------------------
8642 -- Package_Freeze_Node --
8643 -------------------------
8645 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8646 Id : Entity_Id;
8648 begin
8649 if Nkind (B) = N_Package_Body then
8650 Id := Corresponding_Spec (B);
8651 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8652 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8653 end if;
8655 Ensure_Freeze_Node (Id);
8656 return Freeze_Node (Id);
8657 end Package_Freeze_Node;
8659 -- Start of processing for Freeze_Subprogram_Body
8661 begin
8662 -- If the instance and the generic body appear within the same unit, and
8663 -- the instance precedes the generic, the freeze node for the instance
8664 -- must appear after that of the generic. If the generic is nested
8665 -- within another instance I2, then current instance must be frozen
8666 -- after I2. In both cases, the freeze nodes are those of enclosing
8667 -- packages. Otherwise, the freeze node is placed at the end of the
8668 -- current declarative part.
8670 Enc_G := Enclosing_Package_Body (Gen_Body);
8671 Enc_I := Enclosing_Package_Body (Inst_Node);
8672 Ensure_Freeze_Node (Pack_Id);
8673 F_Node := Freeze_Node (Pack_Id);
8675 if Is_Generic_Instance (Par)
8676 and then Present (Freeze_Node (Par))
8677 and then In_Same_Declarative_Part
8678 (Parent (Freeze_Node (Par)), Inst_Node)
8679 then
8680 -- The parent was a premature instantiation. Insert freeze node at
8681 -- the end the current declarative part.
8683 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8684 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8686 -- Handle the following case:
8688 -- package Parent_Inst is new ...
8689 -- Parent_Inst []
8691 -- procedure P ... -- this body freezes Parent_Inst
8693 -- package Inst is new ...
8695 -- In this particular scenario, the freeze node for Inst must be
8696 -- inserted in the same manner as that of Parent_Inst - before the
8697 -- next source body or at the end of the declarative list (body not
8698 -- available). If body P did not exist and Parent_Inst was frozen
8699 -- after Inst, either by a body following Inst or at the end of the
8700 -- declarative region, the freeze node for Inst must be inserted
8701 -- after that of Parent_Inst. This relation is established by
8702 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8704 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8705 List_Containing (Inst_Node)
8706 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8707 then
8708 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8710 else
8711 Insert_After (Freeze_Node (Par), F_Node);
8712 end if;
8714 -- The body enclosing the instance should be frozen after the body that
8715 -- includes the generic, because the body of the instance may make
8716 -- references to entities therein. If the two are not in the same
8717 -- declarative part, or if the one enclosing the instance is frozen
8718 -- already, freeze the instance at the end of the current declarative
8719 -- part.
8721 elsif Is_Generic_Instance (Par)
8722 and then Present (Freeze_Node (Par))
8723 and then Present (Enc_I)
8724 then
8725 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
8726 or else
8727 (Nkind (Enc_I) = N_Package_Body
8728 and then In_Same_Declarative_Part
8729 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
8730 then
8731 -- The enclosing package may contain several instances. Rather
8732 -- than computing the earliest point at which to insert its freeze
8733 -- node, we place it at the end of the declarative part of the
8734 -- parent of the generic.
8736 Insert_Freeze_Node_For_Instance
8737 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8738 end if;
8740 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8742 elsif Present (Enc_G)
8743 and then Present (Enc_I)
8744 and then Enc_G /= Enc_I
8745 and then Earlier (Inst_Node, Gen_Body)
8746 then
8747 if Nkind (Enc_G) = N_Package_Body then
8748 E_G_Id :=
8749 Corresponding_Spec (Enc_G);
8750 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8751 E_G_Id :=
8752 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8753 end if;
8755 -- Freeze package that encloses instance, and place node after the
8756 -- package that encloses generic. If enclosing package is already
8757 -- frozen we have to assume it is at the proper place. This may be a
8758 -- potential ABE that requires dynamic checking. Do not add a freeze
8759 -- node if the package that encloses the generic is inside the body
8760 -- that encloses the instance, because the freeze node would be in
8761 -- the wrong scope. Additional contortions needed if the bodies are
8762 -- within a subunit.
8764 declare
8765 Enclosing_Body : Node_Id;
8767 begin
8768 if Nkind (Enc_I) = N_Package_Body_Stub then
8769 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8770 else
8771 Enclosing_Body := Enc_I;
8772 end if;
8774 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8775 Insert_Freeze_Node_For_Instance
8776 (Enc_G, Package_Freeze_Node (Enc_I));
8777 end if;
8778 end;
8780 -- Freeze enclosing subunit before instance
8782 Ensure_Freeze_Node (E_G_Id);
8784 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8785 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8786 end if;
8788 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8790 else
8791 -- If none of the above, insert freeze node at the end of the current
8792 -- declarative part.
8794 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8795 end if;
8796 end Freeze_Subprogram_Body;
8798 ----------------
8799 -- Get_Gen_Id --
8800 ----------------
8802 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8803 begin
8804 return Generic_Renamings.Table (E).Gen_Id;
8805 end Get_Gen_Id;
8807 ---------------------
8808 -- Get_Instance_Of --
8809 ---------------------
8811 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8812 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8814 begin
8815 if Res /= Assoc_Null then
8816 return Generic_Renamings.Table (Res).Act_Id;
8818 else
8819 -- On exit, entity is not instantiated: not a generic parameter, or
8820 -- else parameter of an inner generic unit.
8822 return A;
8823 end if;
8824 end Get_Instance_Of;
8826 ---------------------------------
8827 -- Get_Unit_Instantiation_Node --
8828 ---------------------------------
8830 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8831 Decl : Node_Id := Unit_Declaration_Node (A);
8832 Inst : Node_Id;
8834 begin
8835 -- If the Package_Instantiation attribute has been set on the package
8836 -- entity, then use it directly when it (or its Original_Node) refers
8837 -- to an N_Package_Instantiation node. In principle it should be
8838 -- possible to have this field set in all cases, which should be
8839 -- investigated, and would allow this function to be significantly
8840 -- simplified. ???
8842 Inst := Package_Instantiation (A);
8844 if Present (Inst) then
8845 if Nkind (Inst) = N_Package_Instantiation then
8846 return Inst;
8848 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8849 return Original_Node (Inst);
8850 end if;
8851 end if;
8853 -- If the instantiation is a compilation unit that does not need body
8854 -- then the instantiation node has been rewritten as a package
8855 -- declaration for the instance, and we return the original node.
8857 -- If it is a compilation unit and the instance node has not been
8858 -- rewritten, then it is still the unit of the compilation. Finally, if
8859 -- a body is present, this is a parent of the main unit whose body has
8860 -- been compiled for inlining purposes, and the instantiation node has
8861 -- been rewritten with the instance body.
8863 -- Otherwise the instantiation node appears after the declaration. If
8864 -- the entity is a formal package, the declaration may have been
8865 -- rewritten as a generic declaration (in the case of a formal with box)
8866 -- or left as a formal package declaration if it has actuals, and is
8867 -- found with a forward search.
8869 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8870 if Nkind (Decl) = N_Package_Declaration
8871 and then Present (Corresponding_Body (Decl))
8872 then
8873 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8874 end if;
8876 if Nkind_In (Original_Node (Decl), N_Function_Instantiation,
8877 N_Package_Instantiation,
8878 N_Procedure_Instantiation)
8879 then
8880 return Original_Node (Decl);
8881 else
8882 return Unit (Parent (Decl));
8883 end if;
8885 elsif Nkind (Decl) = N_Package_Declaration
8886 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8887 then
8888 return Original_Node (Decl);
8890 else
8891 Inst := Next (Decl);
8892 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8893 N_Function_Instantiation,
8894 N_Package_Instantiation,
8895 N_Procedure_Instantiation)
8896 loop
8897 Next (Inst);
8898 end loop;
8900 return Inst;
8901 end if;
8902 end Get_Unit_Instantiation_Node;
8904 ------------------------
8905 -- Has_Been_Exchanged --
8906 ------------------------
8908 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8909 Next : Elmt_Id;
8911 begin
8912 Next := First_Elmt (Exchanged_Views);
8913 while Present (Next) loop
8914 if Full_View (Node (Next)) = E then
8915 return True;
8916 end if;
8918 Next_Elmt (Next);
8919 end loop;
8921 return False;
8922 end Has_Been_Exchanged;
8924 ----------
8925 -- Hash --
8926 ----------
8928 function Hash (F : Entity_Id) return HTable_Range is
8929 begin
8930 return HTable_Range (F mod HTable_Size);
8931 end Hash;
8933 ------------------------
8934 -- Hide_Current_Scope --
8935 ------------------------
8937 procedure Hide_Current_Scope is
8938 C : constant Entity_Id := Current_Scope;
8939 E : Entity_Id;
8941 begin
8942 Set_Is_Hidden_Open_Scope (C);
8944 E := First_Entity (C);
8945 while Present (E) loop
8946 if Is_Immediately_Visible (E) then
8947 Set_Is_Immediately_Visible (E, False);
8948 Append_Elmt (E, Hidden_Entities);
8949 end if;
8951 Next_Entity (E);
8952 end loop;
8954 -- Make the scope name invisible as well. This is necessary, but might
8955 -- conflict with calls to Rtsfind later on, in case the scope is a
8956 -- predefined one. There is no clean solution to this problem, so for
8957 -- now we depend on the user not redefining Standard itself in one of
8958 -- the parent units.
8960 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8961 Set_Is_Immediately_Visible (C, False);
8962 Append_Elmt (C, Hidden_Entities);
8963 end if;
8965 end Hide_Current_Scope;
8967 --------------
8968 -- Init_Env --
8969 --------------
8971 procedure Init_Env is
8972 Saved : Instance_Env;
8974 begin
8975 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8976 Saved.Exchanged_Views := Exchanged_Views;
8977 Saved.Hidden_Entities := Hidden_Entities;
8978 Saved.Current_Sem_Unit := Current_Sem_Unit;
8979 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8980 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8982 -- Save configuration switches. These may be reset if the unit is a
8983 -- predefined unit, and the current mode is not Ada 2005.
8985 Save_Opt_Config_Switches (Saved.Switches);
8987 Instance_Envs.Append (Saved);
8989 Exchanged_Views := New_Elmt_List;
8990 Hidden_Entities := New_Elmt_List;
8992 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8993 -- this is set properly in Set_Instance_Env.
8995 Current_Instantiated_Parent :=
8996 (Current_Scope, Current_Scope, Assoc_Null);
8997 end Init_Env;
8999 ---------------------
9000 -- In_Main_Context --
9001 ---------------------
9003 function In_Main_Context (E : Entity_Id) return Boolean is
9004 Context : List_Id;
9005 Clause : Node_Id;
9006 Nam : Node_Id;
9008 begin
9009 if not Is_Compilation_Unit (E)
9010 or else Ekind (E) /= E_Package
9011 or else In_Private_Part (E)
9012 then
9013 return False;
9014 end if;
9016 Context := Context_Items (Cunit (Main_Unit));
9018 Clause := First (Context);
9019 while Present (Clause) loop
9020 if Nkind (Clause) = N_With_Clause then
9021 Nam := Name (Clause);
9023 -- If the current scope is part of the context of the main unit,
9024 -- analysis of the corresponding with_clause is not complete, and
9025 -- the entity is not set. We use the Chars field directly, which
9026 -- might produce false positives in rare cases, but guarantees
9027 -- that we produce all the instance bodies we will need.
9029 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9030 or else (Nkind (Nam) = N_Selected_Component
9031 and then Chars (Selector_Name (Nam)) = Chars (E))
9032 then
9033 return True;
9034 end if;
9035 end if;
9037 Next (Clause);
9038 end loop;
9040 return False;
9041 end In_Main_Context;
9043 ---------------------
9044 -- Inherit_Context --
9045 ---------------------
9047 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9048 Current_Context : List_Id;
9049 Current_Unit : Node_Id;
9050 Item : Node_Id;
9051 New_I : Node_Id;
9053 Clause : Node_Id;
9054 OK : Boolean;
9055 Lib_Unit : Node_Id;
9057 begin
9058 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9060 -- The inherited context is attached to the enclosing compilation
9061 -- unit. This is either the main unit, or the declaration for the
9062 -- main unit (in case the instantiation appears within the package
9063 -- declaration and the main unit is its body).
9065 Current_Unit := Parent (Inst);
9066 while Present (Current_Unit)
9067 and then Nkind (Current_Unit) /= N_Compilation_Unit
9068 loop
9069 Current_Unit := Parent (Current_Unit);
9070 end loop;
9072 Current_Context := Context_Items (Current_Unit);
9074 Item := First (Context_Items (Parent (Gen_Decl)));
9075 while Present (Item) loop
9076 if Nkind (Item) = N_With_Clause then
9077 Lib_Unit := Library_Unit (Item);
9079 -- Take care to prevent direct cyclic with's
9081 if Lib_Unit /= Current_Unit then
9083 -- Do not add a unit if it is already in the context
9085 Clause := First (Current_Context);
9086 OK := True;
9087 while Present (Clause) loop
9088 if Nkind (Clause) = N_With_Clause
9089 and then Library_Unit (Clause) = Lib_Unit
9090 then
9091 OK := False;
9092 exit;
9093 end if;
9095 Next (Clause);
9096 end loop;
9098 if OK then
9099 New_I := New_Copy (Item);
9100 Set_Implicit_With (New_I);
9102 Append (New_I, Current_Context);
9103 end if;
9104 end if;
9105 end if;
9107 Next (Item);
9108 end loop;
9109 end if;
9110 end Inherit_Context;
9112 ----------------
9113 -- Initialize --
9114 ----------------
9116 procedure Initialize is
9117 begin
9118 Generic_Renamings.Init;
9119 Instance_Envs.Init;
9120 Generic_Flags.Init;
9121 Generic_Renamings_HTable.Reset;
9122 Circularity_Detected := False;
9123 Exchanged_Views := No_Elist;
9124 Hidden_Entities := No_Elist;
9125 end Initialize;
9127 -------------------------------------
9128 -- Insert_Freeze_Node_For_Instance --
9129 -------------------------------------
9131 procedure Insert_Freeze_Node_For_Instance
9132 (N : Node_Id;
9133 F_Node : Node_Id)
9135 Decl : Node_Id;
9136 Decls : List_Id;
9137 Inst : Entity_Id;
9138 Par_N : Node_Id;
9140 function Enclosing_Body (N : Node_Id) return Node_Id;
9141 -- Find enclosing package or subprogram body, if any. Freeze node may
9142 -- be placed at end of current declarative list if previous instance
9143 -- and current one have different enclosing bodies.
9145 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9146 -- Find the local instance, if any, that declares the generic that is
9147 -- being instantiated. If present, the freeze node for this instance
9148 -- must follow the freeze node for the previous instance.
9150 --------------------
9151 -- Enclosing_Body --
9152 --------------------
9154 function Enclosing_Body (N : Node_Id) return Node_Id is
9155 P : Node_Id;
9157 begin
9158 P := Parent (N);
9159 while Present (P)
9160 and then Nkind (Parent (P)) /= N_Compilation_Unit
9161 loop
9162 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9163 if Nkind (Parent (P)) = N_Subunit then
9164 return Corresponding_Stub (Parent (P));
9165 else
9166 return P;
9167 end if;
9168 end if;
9170 P := True_Parent (P);
9171 end loop;
9173 return Empty;
9174 end Enclosing_Body;
9176 -----------------------
9177 -- Previous_Instance --
9178 -----------------------
9180 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9181 S : Entity_Id;
9183 begin
9184 S := Scope (Gen);
9185 while Present (S) and then S /= Standard_Standard loop
9186 if Is_Generic_Instance (S)
9187 and then In_Same_Source_Unit (S, N)
9188 then
9189 return S;
9190 end if;
9192 S := Scope (S);
9193 end loop;
9195 return Empty;
9196 end Previous_Instance;
9198 -- Start of processing for Insert_Freeze_Node_For_Instance
9200 begin
9201 if not Is_List_Member (F_Node) then
9202 Decl := N;
9203 Decls := List_Containing (N);
9204 Inst := Entity (F_Node);
9205 Par_N := Parent (Decls);
9207 -- When processing a subprogram instantiation, utilize the actual
9208 -- subprogram instantiation rather than its package wrapper as it
9209 -- carries all the context information.
9211 if Is_Wrapper_Package (Inst) then
9212 Inst := Related_Instance (Inst);
9213 end if;
9215 -- If this is a package instance, check whether the generic is
9216 -- declared in a previous instance and the current instance is
9217 -- not within the previous one.
9219 if Present (Generic_Parent (Parent (Inst)))
9220 and then Is_In_Main_Unit (N)
9221 then
9222 declare
9223 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9224 Par_I : constant Entity_Id :=
9225 Previous_Instance
9226 (Generic_Parent (Parent (Inst)));
9227 Scop : Entity_Id;
9229 begin
9230 if Present (Par_I)
9231 and then Earlier (N, Freeze_Node (Par_I))
9232 then
9233 Scop := Scope (Inst);
9235 -- If the current instance is within the one that contains
9236 -- the generic, the freeze node for the current one must
9237 -- appear in the current declarative part. Ditto, if the
9238 -- current instance is within another package instance or
9239 -- within a body that does not enclose the current instance.
9240 -- In these three cases the freeze node of the previous
9241 -- instance is not relevant.
9243 while Present (Scop) and then Scop /= Standard_Standard loop
9244 exit when Scop = Par_I
9245 or else
9246 (Is_Generic_Instance (Scop)
9247 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9248 Scop := Scope (Scop);
9249 end loop;
9251 -- Previous instance encloses current instance
9253 if Scop = Par_I then
9254 null;
9256 -- If the next node is a source body we must freeze in
9257 -- the current scope as well.
9259 elsif Present (Next (N))
9260 and then Nkind_In (Next (N), N_Subprogram_Body,
9261 N_Package_Body)
9262 and then Comes_From_Source (Next (N))
9263 then
9264 null;
9266 -- Current instance is within an unrelated instance
9268 elsif Is_Generic_Instance (Scop) then
9269 null;
9271 -- Current instance is within an unrelated body
9273 elsif Present (Enclosing_N)
9274 and then Enclosing_N /= Enclosing_Body (Par_I)
9275 then
9276 null;
9278 else
9279 Insert_After (Freeze_Node (Par_I), F_Node);
9280 return;
9281 end if;
9282 end if;
9283 end;
9284 end if;
9286 -- When the instantiation occurs in a package declaration, append the
9287 -- freeze node to the private declarations (if any).
9289 if Nkind (Par_N) = N_Package_Specification
9290 and then Decls = Visible_Declarations (Par_N)
9291 and then Present (Private_Declarations (Par_N))
9292 and then not Is_Empty_List (Private_Declarations (Par_N))
9293 then
9294 Decls := Private_Declarations (Par_N);
9295 Decl := First (Decls);
9296 end if;
9298 -- Determine the proper freeze point of a package instantiation. We
9299 -- adhere to the general rule of a package or subprogram body causing
9300 -- freezing of anything before it in the same declarative region. In
9301 -- this case, the proper freeze point of a package instantiation is
9302 -- before the first source body which follows, or before a stub. This
9303 -- ensures that entities coming from the instance are already frozen
9304 -- and usable in source bodies.
9306 if Nkind (Par_N) /= N_Package_Declaration
9307 and then Ekind (Inst) = E_Package
9308 and then Is_Generic_Instance (Inst)
9309 and then
9310 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9311 then
9312 while Present (Decl) loop
9313 if (Nkind (Decl) in N_Unit_Body
9314 or else
9315 Nkind (Decl) in N_Body_Stub)
9316 and then Comes_From_Source (Decl)
9317 then
9318 Insert_Before (Decl, F_Node);
9319 return;
9320 end if;
9322 Next (Decl);
9323 end loop;
9324 end if;
9326 -- In a package declaration, or if no previous body, insert at end
9327 -- of list.
9329 Set_Sloc (F_Node, Sloc (Last (Decls)));
9330 Insert_After (Last (Decls), F_Node);
9331 end if;
9332 end Insert_Freeze_Node_For_Instance;
9334 ------------------
9335 -- Install_Body --
9336 ------------------
9338 procedure Install_Body
9339 (Act_Body : Node_Id;
9340 N : Node_Id;
9341 Gen_Body : Node_Id;
9342 Gen_Decl : Node_Id)
9344 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9345 -- Check if the generic definition and the instantiation come from
9346 -- a common scope, in which case the instance must be frozen after
9347 -- the generic body.
9349 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9350 -- If the instance is nested inside a generic unit, the Sloc of the
9351 -- instance indicates the place of the original definition, not the
9352 -- point of the current enclosing instance. Pending a better usage of
9353 -- Slocs to indicate instantiation places, we determine the place of
9354 -- origin of a node by finding the maximum sloc of any ancestor node.
9355 -- Why is this not equivalent to Top_Level_Location ???
9357 -------------------
9358 -- In_Same_Scope --
9359 -------------------
9361 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9362 Act_Scop : Entity_Id := Scope (Act_Id);
9363 Gen_Scop : Entity_Id := Scope (Gen_Id);
9365 begin
9366 while Act_Scop /= Standard_Standard
9367 and then Gen_Scop /= Standard_Standard
9368 loop
9369 if Act_Scop = Gen_Scop then
9370 return True;
9371 end if;
9373 Act_Scop := Scope (Act_Scop);
9374 Gen_Scop := Scope (Gen_Scop);
9375 end loop;
9377 return False;
9378 end In_Same_Scope;
9380 ---------------
9381 -- True_Sloc --
9382 ---------------
9384 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9385 N1 : Node_Id;
9386 Res : Source_Ptr;
9388 begin
9389 Res := Sloc (N);
9390 N1 := N;
9391 while Present (N1) and then N1 /= Act_Unit loop
9392 if Sloc (N1) > Res then
9393 Res := Sloc (N1);
9394 end if;
9396 N1 := Parent (N1);
9397 end loop;
9399 return Res;
9400 end True_Sloc;
9402 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9403 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9404 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9405 Par : constant Entity_Id := Scope (Gen_Id);
9406 Gen_Unit : constant Node_Id :=
9407 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9409 Body_Unit : Node_Id;
9410 F_Node : Node_Id;
9411 Must_Delay : Boolean;
9412 Orig_Body : Node_Id := Gen_Body;
9414 -- Start of processing for Install_Body
9416 begin
9417 -- Handle first the case of an instance with incomplete actual types.
9418 -- The instance body cannot be placed after the declaration because
9419 -- full views have not been seen yet. Any use of the non-limited views
9420 -- in the instance body requires the presence of a regular with_clause
9421 -- in the enclosing unit, and will fail if this with_clause is missing.
9422 -- We place the instance body at the beginning of the enclosing body,
9423 -- which is the unit being compiled. The freeze node for the instance
9424 -- is then placed after the instance body.
9426 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9427 and then Expander_Active
9428 and then Ekind (Scope (Act_Id)) = E_Package
9429 then
9430 declare
9431 Scop : constant Entity_Id := Scope (Act_Id);
9432 Body_Id : constant Node_Id :=
9433 Corresponding_Body (Unit_Declaration_Node (Scop));
9435 begin
9436 Ensure_Freeze_Node (Act_Id);
9437 F_Node := Freeze_Node (Act_Id);
9438 if Present (Body_Id) then
9439 Set_Is_Frozen (Act_Id, False);
9440 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9441 if Is_List_Member (F_Node) then
9442 Remove (F_Node);
9443 end if;
9445 Insert_After (Act_Body, F_Node);
9446 end if;
9447 end;
9448 return;
9449 end if;
9451 -- If the body is a subunit, the freeze point is the corresponding stub
9452 -- in the current compilation, not the subunit itself.
9454 if Nkind (Parent (Gen_Body)) = N_Subunit then
9455 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9456 else
9457 Orig_Body := Gen_Body;
9458 end if;
9460 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9462 -- If the instantiation and the generic definition appear in the same
9463 -- package declaration, this is an early instantiation. If they appear
9464 -- in the same declarative part, it is an early instantiation only if
9465 -- the generic body appears textually later, and the generic body is
9466 -- also in the main unit.
9468 -- If instance is nested within a subprogram, and the generic body
9469 -- is not, the instance is delayed because the enclosing body is. If
9470 -- instance and body are within the same scope, or the same subprogram
9471 -- body, indicate explicitly that the instance is delayed.
9473 Must_Delay :=
9474 (Gen_Unit = Act_Unit
9475 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9476 N_Package_Declaration)
9477 or else (Gen_Unit = Body_Unit
9478 and then True_Sloc (N, Act_Unit) <
9479 Sloc (Orig_Body)))
9480 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9481 and then In_Same_Scope (Gen_Id, Act_Id));
9483 -- If this is an early instantiation, the freeze node is placed after
9484 -- the generic body. Otherwise, if the generic appears in an instance,
9485 -- we cannot freeze the current instance until the outer one is frozen.
9486 -- This is only relevant if the current instance is nested within some
9487 -- inner scope not itself within the outer instance. If this scope is
9488 -- a package body in the same declarative part as the outer instance,
9489 -- then that body needs to be frozen after the outer instance. Finally,
9490 -- if no delay is needed, we place the freeze node at the end of the
9491 -- current declarative part.
9493 if Expander_Active
9494 and then (No (Freeze_Node (Act_Id))
9495 or else not Is_List_Member (Freeze_Node (Act_Id)))
9496 then
9497 Ensure_Freeze_Node (Act_Id);
9498 F_Node := Freeze_Node (Act_Id);
9500 if Must_Delay then
9501 Insert_After (Orig_Body, F_Node);
9503 elsif Is_Generic_Instance (Par)
9504 and then Present (Freeze_Node (Par))
9505 and then Scope (Act_Id) /= Par
9506 then
9507 -- Freeze instance of inner generic after instance of enclosing
9508 -- generic.
9510 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9512 -- Handle the following case:
9514 -- package Parent_Inst is new ...
9515 -- Parent_Inst []
9517 -- procedure P ... -- this body freezes Parent_Inst
9519 -- package Inst is new ...
9521 -- In this particular scenario, the freeze node for Inst must
9522 -- be inserted in the same manner as that of Parent_Inst,
9523 -- before the next source body or at the end of the declarative
9524 -- list (body not available). If body P did not exist and
9525 -- Parent_Inst was frozen after Inst, either by a body
9526 -- following Inst or at the end of the declarative region,
9527 -- the freeze node for Inst must be inserted after that of
9528 -- Parent_Inst. This relation is established by comparing
9529 -- the Slocs of Parent_Inst freeze node and Inst.
9531 if List_Containing (Get_Unit_Instantiation_Node (Par)) =
9532 List_Containing (N)
9533 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9534 then
9535 Insert_Freeze_Node_For_Instance (N, F_Node);
9536 else
9537 Insert_After (Freeze_Node (Par), F_Node);
9538 end if;
9540 -- Freeze package enclosing instance of inner generic after
9541 -- instance of enclosing generic.
9543 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9544 and then In_Same_Declarative_Part
9545 (Parent (Freeze_Node (Par)), Parent (N))
9546 then
9547 declare
9548 Enclosing : Entity_Id;
9550 begin
9551 Enclosing := Corresponding_Spec (Parent (N));
9553 if No (Enclosing) then
9554 Enclosing := Defining_Entity (Parent (N));
9555 end if;
9557 Insert_Freeze_Node_For_Instance (N, F_Node);
9558 Ensure_Freeze_Node (Enclosing);
9560 if not Is_List_Member (Freeze_Node (Enclosing)) then
9562 -- The enclosing context is a subunit, insert the freeze
9563 -- node after the stub.
9565 if Nkind (Parent (Parent (N))) = N_Subunit then
9566 Insert_Freeze_Node_For_Instance
9567 (Corresponding_Stub (Parent (Parent (N))),
9568 Freeze_Node (Enclosing));
9570 -- The enclosing context is a package with a stub body
9571 -- which has already been replaced by the real body.
9572 -- Insert the freeze node after the actual body.
9574 elsif Ekind (Enclosing) = E_Package
9575 and then Present (Body_Entity (Enclosing))
9576 and then Was_Originally_Stub
9577 (Parent (Body_Entity (Enclosing)))
9578 then
9579 Insert_Freeze_Node_For_Instance
9580 (Parent (Body_Entity (Enclosing)),
9581 Freeze_Node (Enclosing));
9583 -- The parent instance has been frozen before the body of
9584 -- the enclosing package, insert the freeze node after
9585 -- the body.
9587 elsif List_Containing (Freeze_Node (Par)) =
9588 List_Containing (Parent (N))
9589 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9590 then
9591 Insert_Freeze_Node_For_Instance
9592 (Parent (N), Freeze_Node (Enclosing));
9594 else
9595 Insert_After
9596 (Freeze_Node (Par), Freeze_Node (Enclosing));
9597 end if;
9598 end if;
9599 end;
9601 else
9602 Insert_Freeze_Node_For_Instance (N, F_Node);
9603 end if;
9605 else
9606 Insert_Freeze_Node_For_Instance (N, F_Node);
9607 end if;
9608 end if;
9610 Set_Is_Frozen (Act_Id);
9611 Insert_Before (N, Act_Body);
9612 Mark_Rewrite_Insertion (Act_Body);
9613 end Install_Body;
9615 -----------------------------
9616 -- Install_Formal_Packages --
9617 -----------------------------
9619 procedure Install_Formal_Packages (Par : Entity_Id) is
9620 E : Entity_Id;
9621 Gen : Entity_Id;
9622 Gen_E : Entity_Id := Empty;
9624 begin
9625 E := First_Entity (Par);
9627 -- If we are installing an instance parent, locate the formal packages
9628 -- of its generic parent.
9630 if Is_Generic_Instance (Par) then
9631 Gen := Generic_Parent (Package_Specification (Par));
9632 Gen_E := First_Entity (Gen);
9633 end if;
9635 while Present (E) loop
9636 if Ekind (E) = E_Package
9637 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9638 then
9639 -- If this is the renaming for the parent instance, done
9641 if Renamed_Object (E) = Par then
9642 exit;
9644 -- The visibility of a formal of an enclosing generic is already
9645 -- correct.
9647 elsif Denotes_Formal_Package (E) then
9648 null;
9650 elsif Present (Associated_Formal_Package (E)) then
9651 Check_Generic_Actuals (Renamed_Object (E), True);
9652 Set_Is_Hidden (E, False);
9654 -- Find formal package in generic unit that corresponds to
9655 -- (instance of) formal package in instance.
9657 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9658 Next_Entity (Gen_E);
9659 end loop;
9661 if Present (Gen_E) then
9662 Map_Formal_Package_Entities (Gen_E, E);
9663 end if;
9664 end if;
9665 end if;
9667 Next_Entity (E);
9669 if Present (Gen_E) then
9670 Next_Entity (Gen_E);
9671 end if;
9672 end loop;
9673 end Install_Formal_Packages;
9675 --------------------
9676 -- Install_Parent --
9677 --------------------
9679 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9680 Ancestors : constant Elist_Id := New_Elmt_List;
9681 S : constant Entity_Id := Current_Scope;
9682 Inst_Par : Entity_Id;
9683 First_Par : Entity_Id;
9684 Inst_Node : Node_Id;
9685 Gen_Par : Entity_Id;
9686 First_Gen : Entity_Id;
9687 Elmt : Elmt_Id;
9689 procedure Install_Noninstance_Specs (Par : Entity_Id);
9690 -- Install the scopes of noninstance parent units ending with Par
9692 procedure Install_Spec (Par : Entity_Id);
9693 -- The child unit is within the declarative part of the parent, so the
9694 -- declarations within the parent are immediately visible.
9696 -------------------------------
9697 -- Install_Noninstance_Specs --
9698 -------------------------------
9700 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9701 begin
9702 if Present (Par)
9703 and then Par /= Standard_Standard
9704 and then not In_Open_Scopes (Par)
9705 then
9706 Install_Noninstance_Specs (Scope (Par));
9707 Install_Spec (Par);
9708 end if;
9709 end Install_Noninstance_Specs;
9711 ------------------
9712 -- Install_Spec --
9713 ------------------
9715 procedure Install_Spec (Par : Entity_Id) is
9716 Spec : constant Node_Id := Package_Specification (Par);
9718 begin
9719 -- If this parent of the child instance is a top-level unit,
9720 -- then record the unit and its visibility for later resetting in
9721 -- Remove_Parent. We exclude units that are generic instances, as we
9722 -- only want to record this information for the ultimate top-level
9723 -- noninstance parent (is that always correct???).
9725 if Scope (Par) = Standard_Standard
9726 and then not Is_Generic_Instance (Par)
9727 then
9728 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9729 Instance_Parent_Unit := Par;
9730 end if;
9732 -- Open the parent scope and make it and its declarations visible.
9733 -- If this point is not within a body, then only the visible
9734 -- declarations should be made visible, and installation of the
9735 -- private declarations is deferred until the appropriate point
9736 -- within analysis of the spec being instantiated (see the handling
9737 -- of parent visibility in Analyze_Package_Specification). This is
9738 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9739 -- private view problems that occur when compiling instantiations of
9740 -- a generic child of that package (Generic_Dispatching_Constructor).
9741 -- If the instance freezes a tagged type, inlinings of operations
9742 -- from Ada.Tags may need the full view of type Tag. If inlining took
9743 -- proper account of establishing visibility of inlined subprograms'
9744 -- parents then it should be possible to remove this
9745 -- special check. ???
9747 Push_Scope (Par);
9748 Set_Is_Immediately_Visible (Par);
9749 Install_Visible_Declarations (Par);
9750 Set_Use (Visible_Declarations (Spec));
9752 if In_Body or else Is_RTU (Par, Ada_Tags) then
9753 Install_Private_Declarations (Par);
9754 Set_Use (Private_Declarations (Spec));
9755 end if;
9756 end Install_Spec;
9758 -- Start of processing for Install_Parent
9760 begin
9761 -- We need to install the parent instance to compile the instantiation
9762 -- of the child, but the child instance must appear in the current
9763 -- scope. Given that we cannot place the parent above the current scope
9764 -- in the scope stack, we duplicate the current scope and unstack both
9765 -- after the instantiation is complete.
9767 -- If the parent is itself the instantiation of a child unit, we must
9768 -- also stack the instantiation of its parent, and so on. Each such
9769 -- ancestor is the prefix of the name in a prior instantiation.
9771 -- If this is a nested instance, the parent unit itself resolves to
9772 -- a renaming of the parent instance, whose declaration we need.
9774 -- Finally, the parent may be a generic (not an instance) when the
9775 -- child unit appears as a formal package.
9777 Inst_Par := P;
9779 if Present (Renamed_Entity (Inst_Par)) then
9780 Inst_Par := Renamed_Entity (Inst_Par);
9781 end if;
9783 First_Par := Inst_Par;
9785 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9787 First_Gen := Gen_Par;
9789 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9791 -- Load grandparent instance as well
9793 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9795 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9796 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9798 if Present (Renamed_Entity (Inst_Par)) then
9799 Inst_Par := Renamed_Entity (Inst_Par);
9800 end if;
9802 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9804 if Present (Gen_Par) then
9805 Prepend_Elmt (Inst_Par, Ancestors);
9807 else
9808 -- Parent is not the name of an instantiation
9810 Install_Noninstance_Specs (Inst_Par);
9811 exit;
9812 end if;
9814 else
9815 -- Previous error
9817 exit;
9818 end if;
9819 end loop;
9821 if Present (First_Gen) then
9822 Append_Elmt (First_Par, Ancestors);
9823 else
9824 Install_Noninstance_Specs (First_Par);
9825 end if;
9827 if not Is_Empty_Elmt_List (Ancestors) then
9828 Elmt := First_Elmt (Ancestors);
9829 while Present (Elmt) loop
9830 Install_Spec (Node (Elmt));
9831 Install_Formal_Packages (Node (Elmt));
9832 Next_Elmt (Elmt);
9833 end loop;
9834 end if;
9836 if not In_Body then
9837 Push_Scope (S);
9838 end if;
9839 end Install_Parent;
9841 -------------------------------
9842 -- Install_Hidden_Primitives --
9843 -------------------------------
9845 procedure Install_Hidden_Primitives
9846 (Prims_List : in out Elist_Id;
9847 Gen_T : Entity_Id;
9848 Act_T : Entity_Id)
9850 Elmt : Elmt_Id;
9851 List : Elist_Id := No_Elist;
9852 Prim_G_Elmt : Elmt_Id;
9853 Prim_A_Elmt : Elmt_Id;
9854 Prim_G : Node_Id;
9855 Prim_A : Node_Id;
9857 begin
9858 -- No action needed in case of serious errors because we cannot trust
9859 -- in the order of primitives
9861 if Serious_Errors_Detected > 0 then
9862 return;
9864 -- No action possible if we don't have available the list of primitive
9865 -- operations
9867 elsif No (Gen_T)
9868 or else not Is_Record_Type (Gen_T)
9869 or else not Is_Tagged_Type (Gen_T)
9870 or else not Is_Record_Type (Act_T)
9871 or else not Is_Tagged_Type (Act_T)
9872 then
9873 return;
9875 -- There is no need to handle interface types since their primitives
9876 -- cannot be hidden
9878 elsif Is_Interface (Gen_T) then
9879 return;
9880 end if;
9882 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9884 if not Is_Class_Wide_Type (Act_T) then
9885 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9886 else
9887 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9888 end if;
9890 loop
9891 -- Skip predefined primitives in the generic formal
9893 while Present (Prim_G_Elmt)
9894 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9895 loop
9896 Next_Elmt (Prim_G_Elmt);
9897 end loop;
9899 -- Skip predefined primitives in the generic actual
9901 while Present (Prim_A_Elmt)
9902 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9903 loop
9904 Next_Elmt (Prim_A_Elmt);
9905 end loop;
9907 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9909 Prim_G := Node (Prim_G_Elmt);
9910 Prim_A := Node (Prim_A_Elmt);
9912 -- There is no need to handle interface primitives because their
9913 -- primitives are not hidden
9915 exit when Present (Interface_Alias (Prim_G));
9917 -- Here we install one hidden primitive
9919 if Chars (Prim_G) /= Chars (Prim_A)
9920 and then Has_Suffix (Prim_A, 'P')
9921 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9922 then
9923 Set_Chars (Prim_A, Chars (Prim_G));
9924 Append_New_Elmt (Prim_A, To => List);
9925 end if;
9927 Next_Elmt (Prim_A_Elmt);
9928 Next_Elmt (Prim_G_Elmt);
9929 end loop;
9931 -- Append the elements to the list of temporarily visible primitives
9932 -- avoiding duplicates.
9934 if Present (List) then
9935 if No (Prims_List) then
9936 Prims_List := New_Elmt_List;
9937 end if;
9939 Elmt := First_Elmt (List);
9940 while Present (Elmt) loop
9941 Append_Unique_Elmt (Node (Elmt), Prims_List);
9942 Next_Elmt (Elmt);
9943 end loop;
9944 end if;
9945 end Install_Hidden_Primitives;
9947 -------------------------------
9948 -- Restore_Hidden_Primitives --
9949 -------------------------------
9951 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9952 Prim_Elmt : Elmt_Id;
9953 Prim : Node_Id;
9955 begin
9956 if Prims_List /= No_Elist then
9957 Prim_Elmt := First_Elmt (Prims_List);
9958 while Present (Prim_Elmt) loop
9959 Prim := Node (Prim_Elmt);
9960 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9961 Next_Elmt (Prim_Elmt);
9962 end loop;
9964 Prims_List := No_Elist;
9965 end if;
9966 end Restore_Hidden_Primitives;
9968 --------------------------------
9969 -- Instantiate_Formal_Package --
9970 --------------------------------
9972 function Instantiate_Formal_Package
9973 (Formal : Node_Id;
9974 Actual : Node_Id;
9975 Analyzed_Formal : Node_Id) return List_Id
9977 Loc : constant Source_Ptr := Sloc (Actual);
9978 Hidden_Formals : constant Elist_Id := New_Elmt_List;
9979 Actual_Pack : Entity_Id;
9980 Formal_Pack : Entity_Id;
9981 Gen_Parent : Entity_Id;
9982 Decls : List_Id;
9983 Nod : Node_Id;
9984 Parent_Spec : Node_Id;
9986 procedure Find_Matching_Actual
9987 (F : Node_Id;
9988 Act : in out Entity_Id);
9989 -- We need to associate each formal entity in the formal package with
9990 -- the corresponding entity in the actual package. The actual package
9991 -- has been analyzed and possibly expanded, and as a result there is
9992 -- no one-to-one correspondence between the two lists (for example,
9993 -- the actual may include subtypes, itypes, and inherited primitive
9994 -- operations, interspersed among the renaming declarations for the
9995 -- actuals). We retrieve the corresponding actual by name because each
9996 -- actual has the same name as the formal, and they do appear in the
9997 -- same order.
9999 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10000 -- Retrieve entity of defining entity of generic formal parameter.
10001 -- Only the declarations of formals need to be considered when
10002 -- linking them to actuals, but the declarative list may include
10003 -- internal entities generated during analysis, and those are ignored.
10005 procedure Match_Formal_Entity
10006 (Formal_Node : Node_Id;
10007 Formal_Ent : Entity_Id;
10008 Actual_Ent : Entity_Id);
10009 -- Associates the formal entity with the actual. In the case where
10010 -- Formal_Ent is a formal package, this procedure iterates through all
10011 -- of its formals and enters associations between the actuals occurring
10012 -- in the formal package's corresponding actual package (given by
10013 -- Actual_Ent) and the formal package's formal parameters. This
10014 -- procedure recurses if any of the parameters is itself a package.
10016 function Is_Instance_Of
10017 (Act_Spec : Entity_Id;
10018 Gen_Anc : Entity_Id) return Boolean;
10019 -- The actual can be an instantiation of a generic within another
10020 -- instance, in which case there is no direct link from it to the
10021 -- original generic ancestor. In that case, we recognize that the
10022 -- ultimate ancestor is the same by examining names and scopes.
10024 procedure Process_Nested_Formal (Formal : Entity_Id);
10025 -- If the current formal is declared with a box, its own formals are
10026 -- visible in the instance, as they were in the generic, and their
10027 -- Hidden flag must be reset. If some of these formals are themselves
10028 -- packages declared with a box, the processing must be recursive.
10030 --------------------------
10031 -- Find_Matching_Actual --
10032 --------------------------
10034 procedure Find_Matching_Actual
10035 (F : Node_Id;
10036 Act : in out Entity_Id)
10038 Formal_Ent : Entity_Id;
10040 begin
10041 case Nkind (Original_Node (F)) is
10042 when N_Formal_Object_Declaration
10043 | N_Formal_Type_Declaration
10045 Formal_Ent := Defining_Identifier (F);
10047 while Chars (Act) /= Chars (Formal_Ent) loop
10048 Next_Entity (Act);
10049 end loop;
10051 when N_Formal_Package_Declaration
10052 | N_Formal_Subprogram_Declaration
10053 | N_Generic_Package_Declaration
10054 | N_Package_Declaration
10056 Formal_Ent := Defining_Entity (F);
10058 while Chars (Act) /= Chars (Formal_Ent) loop
10059 Next_Entity (Act);
10060 end loop;
10062 when others =>
10063 raise Program_Error;
10064 end case;
10065 end Find_Matching_Actual;
10067 -------------------------
10068 -- Match_Formal_Entity --
10069 -------------------------
10071 procedure Match_Formal_Entity
10072 (Formal_Node : Node_Id;
10073 Formal_Ent : Entity_Id;
10074 Actual_Ent : Entity_Id)
10076 Act_Pkg : Entity_Id;
10078 begin
10079 Set_Instance_Of (Formal_Ent, Actual_Ent);
10081 if Ekind (Actual_Ent) = E_Package then
10083 -- Record associations for each parameter
10085 Act_Pkg := Actual_Ent;
10087 declare
10088 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10089 F_Ent : Entity_Id;
10090 F_Node : Node_Id;
10092 Gen_Decl : Node_Id;
10093 Formals : List_Id;
10094 Actual : Entity_Id;
10096 begin
10097 -- Retrieve the actual given in the formal package declaration
10099 Actual := Entity (Name (Original_Node (Formal_Node)));
10101 -- The actual in the formal package declaration may be a
10102 -- renamed generic package, in which case we want to retrieve
10103 -- the original generic in order to traverse its formal part.
10105 if Present (Renamed_Entity (Actual)) then
10106 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10107 else
10108 Gen_Decl := Unit_Declaration_Node (Actual);
10109 end if;
10111 Formals := Generic_Formal_Declarations (Gen_Decl);
10113 if Present (Formals) then
10114 F_Node := First_Non_Pragma (Formals);
10115 else
10116 F_Node := Empty;
10117 end if;
10119 while Present (A_Ent)
10120 and then Present (F_Node)
10121 and then A_Ent /= First_Private_Entity (Act_Pkg)
10122 loop
10123 F_Ent := Get_Formal_Entity (F_Node);
10125 if Present (F_Ent) then
10127 -- This is a formal of the original package. Record
10128 -- association and recurse.
10130 Find_Matching_Actual (F_Node, A_Ent);
10131 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10132 Next_Entity (A_Ent);
10133 end if;
10135 Next_Non_Pragma (F_Node);
10136 end loop;
10137 end;
10138 end if;
10139 end Match_Formal_Entity;
10141 -----------------------
10142 -- Get_Formal_Entity --
10143 -----------------------
10145 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10146 Kind : constant Node_Kind := Nkind (Original_Node (N));
10147 begin
10148 case Kind is
10149 when N_Formal_Object_Declaration =>
10150 return Defining_Identifier (N);
10152 when N_Formal_Type_Declaration =>
10153 return Defining_Identifier (N);
10155 when N_Formal_Subprogram_Declaration =>
10156 return Defining_Unit_Name (Specification (N));
10158 when N_Formal_Package_Declaration =>
10159 return Defining_Identifier (Original_Node (N));
10161 when N_Generic_Package_Declaration =>
10162 return Defining_Identifier (Original_Node (N));
10164 -- All other declarations are introduced by semantic analysis and
10165 -- have no match in the actual.
10167 when others =>
10168 return Empty;
10169 end case;
10170 end Get_Formal_Entity;
10172 --------------------
10173 -- Is_Instance_Of --
10174 --------------------
10176 function Is_Instance_Of
10177 (Act_Spec : Entity_Id;
10178 Gen_Anc : Entity_Id) return Boolean
10180 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10182 begin
10183 if No (Gen_Par) then
10184 return False;
10186 -- Simplest case: the generic parent of the actual is the formal
10188 elsif Gen_Par = Gen_Anc then
10189 return True;
10191 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10192 return False;
10194 -- The actual may be obtained through several instantiations. Its
10195 -- scope must itself be an instance of a generic declared in the
10196 -- same scope as the formal. Any other case is detected above.
10198 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10199 return False;
10201 else
10202 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10203 end if;
10204 end Is_Instance_Of;
10206 ---------------------------
10207 -- Process_Nested_Formal --
10208 ---------------------------
10210 procedure Process_Nested_Formal (Formal : Entity_Id) is
10211 Ent : Entity_Id;
10213 begin
10214 if Present (Associated_Formal_Package (Formal))
10215 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10216 then
10217 Ent := First_Entity (Formal);
10218 while Present (Ent) loop
10219 Set_Is_Hidden (Ent, False);
10220 Set_Is_Visible_Formal (Ent);
10221 Set_Is_Potentially_Use_Visible
10222 (Ent, Is_Potentially_Use_Visible (Formal));
10224 if Ekind (Ent) = E_Package then
10225 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10226 Process_Nested_Formal (Ent);
10227 end if;
10229 Next_Entity (Ent);
10230 end loop;
10231 end if;
10232 end Process_Nested_Formal;
10234 -- Start of processing for Instantiate_Formal_Package
10236 begin
10237 Analyze (Actual);
10239 if not Is_Entity_Name (Actual)
10240 or else Ekind (Entity (Actual)) /= E_Package
10241 then
10242 Error_Msg_N
10243 ("expect package instance to instantiate formal", Actual);
10244 Abandon_Instantiation (Actual);
10245 raise Program_Error;
10247 else
10248 Actual_Pack := Entity (Actual);
10249 Set_Is_Instantiated (Actual_Pack);
10251 -- The actual may be a renamed package, or an outer generic formal
10252 -- package whose instantiation is converted into a renaming.
10254 if Present (Renamed_Object (Actual_Pack)) then
10255 Actual_Pack := Renamed_Object (Actual_Pack);
10256 end if;
10258 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10259 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10260 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10261 else
10262 Gen_Parent :=
10263 Generic_Parent (Specification (Analyzed_Formal));
10264 Formal_Pack :=
10265 Defining_Unit_Name (Specification (Analyzed_Formal));
10266 end if;
10268 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10269 Parent_Spec := Package_Specification (Actual_Pack);
10270 else
10271 Parent_Spec := Parent (Actual_Pack);
10272 end if;
10274 if Gen_Parent = Any_Id then
10275 Error_Msg_N
10276 ("previous error in declaration of formal package", Actual);
10277 Abandon_Instantiation (Actual);
10279 elsif
10280 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
10281 then
10282 null;
10284 else
10285 Error_Msg_NE
10286 ("actual parameter must be instance of&", Actual, Gen_Parent);
10287 Abandon_Instantiation (Actual);
10288 end if;
10290 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10291 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10293 Nod :=
10294 Make_Package_Renaming_Declaration (Loc,
10295 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10296 Name => New_Occurrence_Of (Actual_Pack, Loc));
10298 Set_Associated_Formal_Package
10299 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10300 Decls := New_List (Nod);
10302 -- If the formal F has a box, then the generic declarations are
10303 -- visible in the generic G. In an instance of G, the corresponding
10304 -- entities in the actual for F (which are the actuals for the
10305 -- instantiation of the generic that F denotes) must also be made
10306 -- visible for analysis of the current instance. On exit from the
10307 -- current instance, those entities are made private again. If the
10308 -- actual is currently in use, these entities are also use-visible.
10310 -- The loop through the actual entities also steps through the formal
10311 -- entities and enters associations from formals to actuals into the
10312 -- renaming map. This is necessary to properly handle checking of
10313 -- actual parameter associations for later formals that depend on
10314 -- actuals declared in the formal package.
10316 -- In Ada 2005, partial parameterization requires that we make
10317 -- visible the actuals corresponding to formals that were defaulted
10318 -- in the formal package. There formals are identified because they
10319 -- remain formal generics within the formal package, rather than
10320 -- being renamings of the actuals supplied.
10322 declare
10323 Gen_Decl : constant Node_Id :=
10324 Unit_Declaration_Node (Gen_Parent);
10325 Formals : constant List_Id :=
10326 Generic_Formal_Declarations (Gen_Decl);
10328 Actual_Ent : Entity_Id;
10329 Actual_Of_Formal : Node_Id;
10330 Formal_Node : Node_Id;
10331 Formal_Ent : Entity_Id;
10333 begin
10334 if Present (Formals) then
10335 Formal_Node := First_Non_Pragma (Formals);
10336 else
10337 Formal_Node := Empty;
10338 end if;
10340 Actual_Ent := First_Entity (Actual_Pack);
10341 Actual_Of_Formal :=
10342 First (Visible_Declarations (Specification (Analyzed_Formal)));
10343 while Present (Actual_Ent)
10344 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10345 loop
10346 if Present (Formal_Node) then
10347 Formal_Ent := Get_Formal_Entity (Formal_Node);
10349 if Present (Formal_Ent) then
10350 Find_Matching_Actual (Formal_Node, Actual_Ent);
10351 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10353 -- We iterate at the same time over the actuals of the
10354 -- local package created for the formal, to determine
10355 -- which one of the formals of the original generic were
10356 -- defaulted in the formal. The corresponding actual
10357 -- entities are visible in the enclosing instance.
10359 if Box_Present (Formal)
10360 or else
10361 (Present (Actual_Of_Formal)
10362 and then
10363 Is_Generic_Formal
10364 (Get_Formal_Entity (Actual_Of_Formal)))
10365 then
10366 Set_Is_Hidden (Actual_Ent, False);
10367 Set_Is_Visible_Formal (Actual_Ent);
10368 Set_Is_Potentially_Use_Visible
10369 (Actual_Ent, In_Use (Actual_Pack));
10371 if Ekind (Actual_Ent) = E_Package then
10372 Process_Nested_Formal (Actual_Ent);
10373 end if;
10375 else
10376 if not Is_Hidden (Actual_Ent) then
10377 Append_Elmt (Actual_Ent, Hidden_Formals);
10378 end if;
10380 Set_Is_Hidden (Actual_Ent);
10381 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10382 end if;
10383 end if;
10385 Next_Non_Pragma (Formal_Node);
10386 Next (Actual_Of_Formal);
10388 else
10389 -- No further formals to match, but the generic part may
10390 -- contain inherited operation that are not hidden in the
10391 -- enclosing instance.
10393 Next_Entity (Actual_Ent);
10394 end if;
10395 end loop;
10397 -- Inherited subprograms generated by formal derived types are
10398 -- also visible if the types are.
10400 Actual_Ent := First_Entity (Actual_Pack);
10401 while Present (Actual_Ent)
10402 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10403 loop
10404 if Is_Overloadable (Actual_Ent)
10405 and then
10406 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10407 and then
10408 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10409 then
10410 Set_Is_Hidden (Actual_Ent, False);
10411 Set_Is_Potentially_Use_Visible
10412 (Actual_Ent, In_Use (Actual_Pack));
10413 end if;
10415 Next_Entity (Actual_Ent);
10416 end loop;
10417 end;
10419 -- If the formal is not declared with a box, reanalyze it as an
10420 -- abbreviated instantiation, to verify the matching rules of 12.7.
10421 -- The actual checks are performed after the generic associations
10422 -- have been analyzed, to guarantee the same visibility for this
10423 -- instantiation and for the actuals.
10425 -- In Ada 2005, the generic associations for the formal can include
10426 -- defaulted parameters. These are ignored during check. This
10427 -- internal instantiation is removed from the tree after conformance
10428 -- checking, because it contains formal declarations for those
10429 -- defaulted parameters, and those should not reach the back-end.
10431 if not Box_Present (Formal) then
10432 declare
10433 I_Pack : constant Entity_Id :=
10434 Make_Temporary (Sloc (Actual), 'P');
10436 begin
10437 Set_Is_Internal (I_Pack);
10438 Set_Ekind (I_Pack, E_Package);
10439 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10441 Append_To (Decls,
10442 Make_Package_Instantiation (Sloc (Actual),
10443 Defining_Unit_Name => I_Pack,
10444 Name =>
10445 New_Occurrence_Of
10446 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10447 Generic_Associations => Generic_Associations (Formal)));
10448 end;
10449 end if;
10451 return Decls;
10452 end if;
10453 end Instantiate_Formal_Package;
10455 -----------------------------------
10456 -- Instantiate_Formal_Subprogram --
10457 -----------------------------------
10459 function Instantiate_Formal_Subprogram
10460 (Formal : Node_Id;
10461 Actual : Node_Id;
10462 Analyzed_Formal : Node_Id) return Node_Id
10464 Analyzed_S : constant Entity_Id :=
10465 Defining_Unit_Name (Specification (Analyzed_Formal));
10466 Formal_Sub : constant Entity_Id :=
10467 Defining_Unit_Name (Specification (Formal));
10469 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10470 -- If the generic is a child unit, the parent has been installed on the
10471 -- scope stack, but a default subprogram cannot resolve to something
10472 -- on the parent because that parent is not really part of the visible
10473 -- context (it is there to resolve explicit local entities). If the
10474 -- default has resolved in this way, we remove the entity from immediate
10475 -- visibility and analyze the node again to emit an error message or
10476 -- find another visible candidate.
10478 procedure Valid_Actual_Subprogram (Act : Node_Id);
10479 -- Perform legality check and raise exception on failure
10481 -----------------------
10482 -- From_Parent_Scope --
10483 -----------------------
10485 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10486 Gen_Scope : Node_Id;
10488 begin
10489 Gen_Scope := Scope (Analyzed_S);
10490 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10491 if Scope (Subp) = Scope (Gen_Scope) then
10492 return True;
10493 end if;
10495 Gen_Scope := Scope (Gen_Scope);
10496 end loop;
10498 return False;
10499 end From_Parent_Scope;
10501 -----------------------------
10502 -- Valid_Actual_Subprogram --
10503 -----------------------------
10505 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10506 Act_E : Entity_Id;
10508 begin
10509 if Is_Entity_Name (Act) then
10510 Act_E := Entity (Act);
10512 elsif Nkind (Act) = N_Selected_Component
10513 and then Is_Entity_Name (Selector_Name (Act))
10514 then
10515 Act_E := Entity (Selector_Name (Act));
10517 else
10518 Act_E := Empty;
10519 end if;
10521 if (Present (Act_E) and then Is_Overloadable (Act_E))
10522 or else Nkind_In (Act, N_Attribute_Reference,
10523 N_Indexed_Component,
10524 N_Character_Literal,
10525 N_Explicit_Dereference)
10526 then
10527 return;
10528 end if;
10530 Error_Msg_NE
10531 ("expect subprogram or entry name in instantiation of &",
10532 Instantiation_Node, Formal_Sub);
10533 Abandon_Instantiation (Instantiation_Node);
10534 end Valid_Actual_Subprogram;
10536 -- Local variables
10538 Decl_Node : Node_Id;
10539 Loc : Source_Ptr;
10540 Nam : Node_Id;
10541 New_Spec : Node_Id;
10542 New_Subp : Entity_Id;
10544 -- Start of processing for Instantiate_Formal_Subprogram
10546 begin
10547 New_Spec := New_Copy_Tree (Specification (Formal));
10549 -- The tree copy has created the proper instantiation sloc for the
10550 -- new specification. Use this location for all other constructed
10551 -- declarations.
10553 Loc := Sloc (Defining_Unit_Name (New_Spec));
10555 -- Create new entity for the actual (New_Copy_Tree does not), and
10556 -- indicate that it is an actual.
10558 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10559 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10560 Set_Is_Generic_Actual_Subprogram (New_Subp);
10561 Set_Defining_Unit_Name (New_Spec, New_Subp);
10563 -- Create new entities for the each of the formals in the specification
10564 -- of the renaming declaration built for the actual.
10566 if Present (Parameter_Specifications (New_Spec)) then
10567 declare
10568 F : Node_Id;
10569 F_Id : Entity_Id;
10571 begin
10572 F := First (Parameter_Specifications (New_Spec));
10573 while Present (F) loop
10574 F_Id := Defining_Identifier (F);
10576 Set_Defining_Identifier (F,
10577 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10578 Next (F);
10579 end loop;
10580 end;
10581 end if;
10583 -- Find entity of actual. If the actual is an attribute reference, it
10584 -- cannot be resolved here (its formal is missing) but is handled
10585 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10586 -- fully resolved subsequently, when the renaming declaration for the
10587 -- formal is analyzed. If it is an explicit dereference, resolve the
10588 -- prefix but not the actual itself, to prevent interpretation as call.
10590 if Present (Actual) then
10591 Loc := Sloc (Actual);
10592 Set_Sloc (New_Spec, Loc);
10594 if Nkind (Actual) = N_Operator_Symbol then
10595 Find_Direct_Name (Actual);
10597 elsif Nkind (Actual) = N_Explicit_Dereference then
10598 Analyze (Prefix (Actual));
10600 elsif Nkind (Actual) /= N_Attribute_Reference then
10601 Analyze (Actual);
10602 end if;
10604 Valid_Actual_Subprogram (Actual);
10605 Nam := Actual;
10607 elsif Present (Default_Name (Formal)) then
10608 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10609 N_Selected_Component,
10610 N_Indexed_Component,
10611 N_Character_Literal)
10612 and then Present (Entity (Default_Name (Formal)))
10613 then
10614 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10615 else
10616 Nam := New_Copy (Default_Name (Formal));
10617 Set_Sloc (Nam, Loc);
10618 end if;
10620 elsif Box_Present (Formal) then
10622 -- Actual is resolved at the point of instantiation. Create an
10623 -- identifier or operator with the same name as the formal.
10625 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10626 Nam :=
10627 Make_Operator_Symbol (Loc,
10628 Chars => Chars (Formal_Sub),
10629 Strval => No_String);
10630 else
10631 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10632 end if;
10634 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10635 and then Null_Present (Specification (Formal))
10636 then
10637 -- Generate null body for procedure, for use in the instance
10639 Decl_Node :=
10640 Make_Subprogram_Body (Loc,
10641 Specification => New_Spec,
10642 Declarations => New_List,
10643 Handled_Statement_Sequence =>
10644 Make_Handled_Sequence_Of_Statements (Loc,
10645 Statements => New_List (Make_Null_Statement (Loc))));
10647 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10648 return Decl_Node;
10650 else
10651 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10652 Error_Msg_NE
10653 ("missing actual&", Instantiation_Node, Formal_Sub);
10654 Error_Msg_NE
10655 ("\in instantiation of & declared#",
10656 Instantiation_Node, Scope (Analyzed_S));
10657 Abandon_Instantiation (Instantiation_Node);
10658 end if;
10660 Decl_Node :=
10661 Make_Subprogram_Renaming_Declaration (Loc,
10662 Specification => New_Spec,
10663 Name => Nam);
10665 -- If we do not have an actual and the formal specified <> then set to
10666 -- get proper default.
10668 if No (Actual) and then Box_Present (Formal) then
10669 Set_From_Default (Decl_Node);
10670 end if;
10672 -- Gather possible interpretations for the actual before analyzing the
10673 -- instance. If overloaded, it will be resolved when analyzing the
10674 -- renaming declaration.
10676 if Box_Present (Formal) and then No (Actual) then
10677 Analyze (Nam);
10679 if Is_Child_Unit (Scope (Analyzed_S))
10680 and then Present (Entity (Nam))
10681 then
10682 if not Is_Overloaded (Nam) then
10683 if From_Parent_Scope (Entity (Nam)) then
10684 Set_Is_Immediately_Visible (Entity (Nam), False);
10685 Set_Entity (Nam, Empty);
10686 Set_Etype (Nam, Empty);
10688 Analyze (Nam);
10689 Set_Is_Immediately_Visible (Entity (Nam));
10690 end if;
10692 else
10693 declare
10694 I : Interp_Index;
10695 It : Interp;
10697 begin
10698 Get_First_Interp (Nam, I, It);
10699 while Present (It.Nam) loop
10700 if From_Parent_Scope (It.Nam) then
10701 Remove_Interp (I);
10702 end if;
10704 Get_Next_Interp (I, It);
10705 end loop;
10706 end;
10707 end if;
10708 end if;
10709 end if;
10711 -- The generic instantiation freezes the actual. This can only be done
10712 -- once the actual is resolved, in the analysis of the renaming
10713 -- declaration. To make the formal subprogram entity available, we set
10714 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10715 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10716 -- of formal abstract subprograms.
10718 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10720 -- We cannot analyze the renaming declaration, and thus find the actual,
10721 -- until all the actuals are assembled in the instance. For subsequent
10722 -- checks of other actuals, indicate the node that will hold the
10723 -- instance of this formal.
10725 Set_Instance_Of (Analyzed_S, Nam);
10727 if Nkind (Actual) = N_Selected_Component
10728 and then Is_Task_Type (Etype (Prefix (Actual)))
10729 and then not Is_Frozen (Etype (Prefix (Actual)))
10730 then
10731 -- The renaming declaration will create a body, which must appear
10732 -- outside of the instantiation, We move the renaming declaration
10733 -- out of the instance, and create an additional renaming inside,
10734 -- to prevent freezing anomalies.
10736 declare
10737 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10739 begin
10740 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10741 Insert_Before (Instantiation_Node, Decl_Node);
10742 Analyze (Decl_Node);
10744 -- Now create renaming within the instance
10746 Decl_Node :=
10747 Make_Subprogram_Renaming_Declaration (Loc,
10748 Specification => New_Copy_Tree (New_Spec),
10749 Name => New_Occurrence_Of (Anon_Id, Loc));
10751 Set_Defining_Unit_Name (Specification (Decl_Node),
10752 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10753 end;
10754 end if;
10756 return Decl_Node;
10757 end Instantiate_Formal_Subprogram;
10759 ------------------------
10760 -- Instantiate_Object --
10761 ------------------------
10763 function Instantiate_Object
10764 (Formal : Node_Id;
10765 Actual : Node_Id;
10766 Analyzed_Formal : Node_Id) return List_Id
10768 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10769 A_Gen_Obj : constant Entity_Id :=
10770 Defining_Identifier (Analyzed_Formal);
10771 Acc_Def : Node_Id := Empty;
10772 Act_Assoc : constant Node_Id := Parent (Actual);
10773 Actual_Decl : Node_Id := Empty;
10774 Decl_Node : Node_Id;
10775 Def : Node_Id;
10776 Ftyp : Entity_Id;
10777 List : constant List_Id := New_List;
10778 Loc : constant Source_Ptr := Sloc (Actual);
10779 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10780 Subt_Decl : Node_Id := Empty;
10781 Subt_Mark : Node_Id := Empty;
10783 function Copy_Access_Def return Node_Id;
10784 -- If formal is an anonymous access, copy access definition of formal
10785 -- for generated object declaration.
10787 ---------------------
10788 -- Copy_Access_Def --
10789 ---------------------
10791 function Copy_Access_Def return Node_Id is
10792 begin
10793 Def := New_Copy_Tree (Acc_Def);
10795 -- In addition, if formal is an access to subprogram we need to
10796 -- generate new formals for the signature of the default, so that
10797 -- the tree is properly formatted for ASIS use.
10799 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10800 declare
10801 Par_Spec : Node_Id;
10802 begin
10803 Par_Spec :=
10804 First (Parameter_Specifications
10805 (Access_To_Subprogram_Definition (Def)));
10806 while Present (Par_Spec) loop
10807 Set_Defining_Identifier (Par_Spec,
10808 Make_Defining_Identifier (Sloc (Acc_Def),
10809 Chars => Chars (Defining_Identifier (Par_Spec))));
10810 Next (Par_Spec);
10811 end loop;
10812 end;
10813 end if;
10815 return Def;
10816 end Copy_Access_Def;
10818 -- Start of processing for Instantiate_Object
10820 begin
10821 -- Formal may be an anonymous access
10823 if Present (Subtype_Mark (Formal)) then
10824 Subt_Mark := Subtype_Mark (Formal);
10825 else
10826 Check_Access_Definition (Formal);
10827 Acc_Def := Access_Definition (Formal);
10828 end if;
10830 -- Sloc for error message on missing actual
10832 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10834 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10835 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10836 end if;
10838 Set_Parent (List, Parent (Actual));
10840 -- OUT present
10842 if Out_Present (Formal) then
10844 -- An IN OUT generic actual must be a name. The instantiation is a
10845 -- renaming declaration. The actual is the name being renamed. We
10846 -- use the actual directly, rather than a copy, because it is not
10847 -- used further in the list of actuals, and because a copy or a use
10848 -- of relocate_node is incorrect if the instance is nested within a
10849 -- generic. In order to simplify ASIS searches, the Generic_Parent
10850 -- field links the declaration to the generic association.
10852 if No (Actual) then
10853 Error_Msg_NE
10854 ("missing actual &",
10855 Instantiation_Node, Gen_Obj);
10856 Error_Msg_NE
10857 ("\in instantiation of & declared#",
10858 Instantiation_Node, Scope (A_Gen_Obj));
10859 Abandon_Instantiation (Instantiation_Node);
10860 end if;
10862 if Present (Subt_Mark) then
10863 Decl_Node :=
10864 Make_Object_Renaming_Declaration (Loc,
10865 Defining_Identifier => New_Copy (Gen_Obj),
10866 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10867 Name => Actual);
10869 else pragma Assert (Present (Acc_Def));
10870 Decl_Node :=
10871 Make_Object_Renaming_Declaration (Loc,
10872 Defining_Identifier => New_Copy (Gen_Obj),
10873 Access_Definition => New_Copy_Tree (Acc_Def),
10874 Name => Actual);
10875 end if;
10877 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10879 -- The analysis of the actual may produce Insert_Action nodes, so
10880 -- the declaration must have a context in which to attach them.
10882 Append (Decl_Node, List);
10883 Analyze (Actual);
10885 -- Return if the analysis of the actual reported some error
10887 if Etype (Actual) = Any_Type then
10888 return List;
10889 end if;
10891 -- This check is performed here because Analyze_Object_Renaming will
10892 -- not check it when Comes_From_Source is False. Note though that the
10893 -- check for the actual being the name of an object will be performed
10894 -- in Analyze_Object_Renaming.
10896 if Is_Object_Reference (Actual)
10897 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10898 then
10899 Error_Msg_N
10900 ("illegal discriminant-dependent component for in out parameter",
10901 Actual);
10902 end if;
10904 -- The actual has to be resolved in order to check that it is a
10905 -- variable (due to cases such as F (1), where F returns access to
10906 -- an array, and for overloaded prefixes).
10908 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10910 -- If the type of the formal is not itself a formal, and the current
10911 -- unit is a child unit, the formal type must be declared in a
10912 -- parent, and must be retrieved by visibility.
10914 if Ftyp = Orig_Ftyp
10915 and then Is_Generic_Unit (Scope (Ftyp))
10916 and then Is_Child_Unit (Scope (A_Gen_Obj))
10917 then
10918 declare
10919 Temp : constant Node_Id :=
10920 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10921 begin
10922 Set_Entity (Temp, Empty);
10923 Find_Type (Temp);
10924 Ftyp := Entity (Temp);
10925 end;
10926 end if;
10928 if Is_Private_Type (Ftyp)
10929 and then not Is_Private_Type (Etype (Actual))
10930 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10931 or else Base_Type (Etype (Actual)) = Ftyp)
10932 then
10933 -- If the actual has the type of the full view of the formal, or
10934 -- else a non-private subtype of the formal, then the visibility
10935 -- of the formal type has changed. Add to the actuals a subtype
10936 -- declaration that will force the exchange of views in the body
10937 -- of the instance as well.
10939 Subt_Decl :=
10940 Make_Subtype_Declaration (Loc,
10941 Defining_Identifier => Make_Temporary (Loc, 'P'),
10942 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10944 Prepend (Subt_Decl, List);
10946 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10947 Exchange_Declarations (Ftyp);
10948 end if;
10950 Resolve (Actual, Ftyp);
10952 if not Denotes_Variable (Actual) then
10953 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10955 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10957 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10958 -- the type of the actual shall resolve to a specific anonymous
10959 -- access type.
10961 if Ada_Version < Ada_2005
10962 or else Ekind (Base_Type (Ftyp)) /=
10963 E_Anonymous_Access_Type
10964 or else Ekind (Base_Type (Etype (Actual))) /=
10965 E_Anonymous_Access_Type
10966 then
10967 Error_Msg_NE
10968 ("type of actual does not match type of&", Actual, Gen_Obj);
10969 end if;
10970 end if;
10972 Note_Possible_Modification (Actual, Sure => True);
10974 -- Check for instantiation of atomic/volatile actual for
10975 -- non-atomic/volatile formal (RM C.6 (12)).
10977 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10978 Error_Msg_N
10979 ("cannot instantiate non-atomic formal object "
10980 & "with atomic actual", Actual);
10982 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10983 then
10984 Error_Msg_N
10985 ("cannot instantiate non-volatile formal object "
10986 & "with volatile actual", Actual);
10987 end if;
10989 -- Formal in-parameter
10991 else
10992 -- The instantiation of a generic formal in-parameter is constant
10993 -- declaration. The actual is the expression for that declaration.
10994 -- Its type is a full copy of the type of the formal. This may be
10995 -- an access to subprogram, for which we need to generate entities
10996 -- for the formals in the new signature.
10998 if Present (Actual) then
10999 if Present (Subt_Mark) then
11000 Def := New_Copy_Tree (Subt_Mark);
11001 else pragma Assert (Present (Acc_Def));
11002 Def := Copy_Access_Def;
11003 end if;
11005 Decl_Node :=
11006 Make_Object_Declaration (Loc,
11007 Defining_Identifier => New_Copy (Gen_Obj),
11008 Constant_Present => True,
11009 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11010 Object_Definition => Def,
11011 Expression => Actual);
11013 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11015 -- A generic formal object of a tagged type is defined to be
11016 -- aliased so the new constant must also be treated as aliased.
11018 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11019 Set_Aliased_Present (Decl_Node);
11020 end if;
11022 Append (Decl_Node, List);
11024 -- No need to repeat (pre-)analysis of some expression nodes
11025 -- already handled in Preanalyze_Actuals.
11027 if Nkind (Actual) /= N_Allocator then
11028 Analyze (Actual);
11030 -- Return if the analysis of the actual reported some error
11032 if Etype (Actual) = Any_Type then
11033 return List;
11034 end if;
11035 end if;
11037 declare
11038 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11039 Typ : Entity_Id;
11041 begin
11042 Typ := Get_Instance_Of (Formal_Type);
11044 -- If the actual appears in the current or an enclosing scope,
11045 -- use its type directly. This is relevant if it has an actual
11046 -- subtype that is distinct from its nominal one. This cannot
11047 -- be done in general because the type of the actual may
11048 -- depend on other actuals, and only be fully determined when
11049 -- the enclosing instance is analyzed.
11051 if Present (Etype (Actual))
11052 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11053 then
11054 Freeze_Before (Instantiation_Node, Etype (Actual));
11055 else
11056 Freeze_Before (Instantiation_Node, Typ);
11057 end if;
11059 -- If the actual is an aggregate, perform name resolution on
11060 -- its components (the analysis of an aggregate does not do it)
11061 -- to capture local names that may be hidden if the generic is
11062 -- a child unit.
11064 if Nkind (Actual) = N_Aggregate then
11065 Preanalyze_And_Resolve (Actual, Typ);
11066 end if;
11068 if Is_Limited_Type (Typ)
11069 and then not OK_For_Limited_Init (Typ, Actual)
11070 then
11071 Error_Msg_N
11072 ("initialization not allowed for limited types", Actual);
11073 Explain_Limited_Type (Typ, Actual);
11074 end if;
11075 end;
11077 elsif Present (Default_Expression (Formal)) then
11079 -- Use default to construct declaration
11081 if Present (Subt_Mark) then
11082 Def := New_Copy (Subt_Mark);
11083 else pragma Assert (Present (Acc_Def));
11084 Def := Copy_Access_Def;
11085 end if;
11087 Decl_Node :=
11088 Make_Object_Declaration (Sloc (Formal),
11089 Defining_Identifier => New_Copy (Gen_Obj),
11090 Constant_Present => True,
11091 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11092 Object_Definition => Def,
11093 Expression => New_Copy_Tree
11094 (Default_Expression (Formal)));
11096 Append (Decl_Node, List);
11097 Set_Analyzed (Expression (Decl_Node), False);
11099 else
11100 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11101 Error_Msg_NE ("\in instantiation of & declared#",
11102 Instantiation_Node, Scope (A_Gen_Obj));
11104 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11106 -- Create dummy constant declaration so that instance can be
11107 -- analyzed, to minimize cascaded visibility errors.
11109 if Present (Subt_Mark) then
11110 Def := Subt_Mark;
11111 else pragma Assert (Present (Acc_Def));
11112 Def := Acc_Def;
11113 end if;
11115 Decl_Node :=
11116 Make_Object_Declaration (Loc,
11117 Defining_Identifier => New_Copy (Gen_Obj),
11118 Constant_Present => True,
11119 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11120 Object_Definition => New_Copy (Def),
11121 Expression =>
11122 Make_Attribute_Reference (Sloc (Gen_Obj),
11123 Attribute_Name => Name_First,
11124 Prefix => New_Copy (Def)));
11126 Append (Decl_Node, List);
11128 else
11129 Abandon_Instantiation (Instantiation_Node);
11130 end if;
11131 end if;
11132 end if;
11134 if Nkind (Actual) in N_Has_Entity then
11135 Actual_Decl := Parent (Entity (Actual));
11136 end if;
11138 -- Ada 2005 (AI-423): For a formal object declaration with a null
11139 -- exclusion or an access definition that has a null exclusion: If the
11140 -- actual matching the formal object declaration denotes a generic
11141 -- formal object of another generic unit G, and the instantiation
11142 -- containing the actual occurs within the body of G or within the body
11143 -- of a generic unit declared within the declarative region of G, then
11144 -- the declaration of the formal object of G must have a null exclusion.
11145 -- Otherwise, the subtype of the actual matching the formal object
11146 -- declaration shall exclude null.
11148 if Ada_Version >= Ada_2005
11149 and then Present (Actual_Decl)
11150 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11151 N_Object_Declaration)
11152 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11153 and then not Has_Null_Exclusion (Actual_Decl)
11154 and then Has_Null_Exclusion (Analyzed_Formal)
11155 then
11156 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11157 Error_Msg_N
11158 ("actual must exclude null to match generic formal#", Actual);
11159 end if;
11161 -- An effectively volatile object cannot be used as an actual in a
11162 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11163 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11164 -- legality rule, and also verifies that the actual is an object.
11166 if SPARK_Mode = On
11167 and then Present (Actual)
11168 and then Is_Object_Reference (Actual)
11169 and then Is_Effectively_Volatile_Object (Actual)
11170 then
11171 Error_Msg_N
11172 ("volatile object cannot act as actual in generic instantiation",
11173 Actual);
11174 end if;
11176 return List;
11177 end Instantiate_Object;
11179 ------------------------------
11180 -- Instantiate_Package_Body --
11181 ------------------------------
11183 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11184 -- must be replaced by gotos which jump to the end of the routine in order
11185 -- to restore the Ghost and SPARK modes.
11187 procedure Instantiate_Package_Body
11188 (Body_Info : Pending_Body_Info;
11189 Inlined_Body : Boolean := False;
11190 Body_Optional : Boolean := False)
11192 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11193 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11194 Act_Spec : constant Node_Id := Specification (Act_Decl);
11195 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11196 Gen_Id : constant Node_Id := Name (Inst_Node);
11197 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11198 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11199 Loc : constant Source_Ptr := Sloc (Inst_Node);
11201 Saved_ISMP : constant Boolean :=
11202 Ignore_SPARK_Mode_Pragmas_In_Instance;
11203 Saved_Style_Check : constant Boolean := Style_Check;
11205 procedure Check_Initialized_Types;
11206 -- In a generic package body, an entity of a generic private type may
11207 -- appear uninitialized. This is suspicious, unless the actual is a
11208 -- fully initialized type.
11210 -----------------------------
11211 -- Check_Initialized_Types --
11212 -----------------------------
11214 procedure Check_Initialized_Types is
11215 Decl : Node_Id;
11216 Formal : Entity_Id;
11217 Actual : Entity_Id;
11218 Uninit_Var : Entity_Id;
11220 begin
11221 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11222 while Present (Decl) loop
11223 Uninit_Var := Empty;
11225 if Nkind (Decl) = N_Private_Extension_Declaration then
11226 Uninit_Var := Uninitialized_Variable (Decl);
11228 elsif Nkind (Decl) = N_Formal_Type_Declaration
11229 and then Nkind (Formal_Type_Definition (Decl)) =
11230 N_Formal_Private_Type_Definition
11231 then
11232 Uninit_Var :=
11233 Uninitialized_Variable (Formal_Type_Definition (Decl));
11234 end if;
11236 if Present (Uninit_Var) then
11237 Formal := Defining_Identifier (Decl);
11238 Actual := First_Entity (Act_Decl_Id);
11240 -- For each formal there is a subtype declaration that renames
11241 -- the actual and has the same name as the formal. Locate the
11242 -- formal for warning message about uninitialized variables
11243 -- in the generic, for which the actual type should be a fully
11244 -- initialized type.
11246 while Present (Actual) loop
11247 exit when Ekind (Actual) = E_Package
11248 and then Present (Renamed_Object (Actual));
11250 if Chars (Actual) = Chars (Formal)
11251 and then not Is_Scalar_Type (Actual)
11252 and then not Is_Fully_Initialized_Type (Actual)
11253 and then Warn_On_No_Value_Assigned
11254 then
11255 Error_Msg_Node_2 := Formal;
11256 Error_Msg_NE
11257 ("generic unit has uninitialized variable& of "
11258 & "formal private type &?v?", Actual, Uninit_Var);
11259 Error_Msg_NE
11260 ("actual type for& should be fully initialized type?v?",
11261 Actual, Formal);
11262 exit;
11263 end if;
11265 Next_Entity (Actual);
11266 end loop;
11267 end if;
11269 Next (Decl);
11270 end loop;
11271 end Check_Initialized_Types;
11273 -- Local variables
11275 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11276 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11277 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11278 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11279 -- Save the Ghost and SPARK mode-related data to restore on exit
11281 Act_Body : Node_Id;
11282 Act_Body_Id : Entity_Id;
11283 Act_Body_Name : Node_Id;
11284 Gen_Body : Node_Id;
11285 Gen_Body_Id : Node_Id;
11286 Par_Ent : Entity_Id := Empty;
11287 Par_Vis : Boolean := False;
11288 Parent_Installed : Boolean := False;
11290 Vis_Prims_List : Elist_Id := No_Elist;
11291 -- List of primitives made temporarily visible in the instantiation
11292 -- to match the visibility of the formal type.
11294 -- Start of processing for Instantiate_Package_Body
11296 begin
11297 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11299 -- The instance body may already have been processed, as the parent of
11300 -- another instance that is inlined (Load_Parent_Of_Generic).
11302 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11303 return;
11304 end if;
11306 -- The package being instantiated may be subject to pragma Ghost. Set
11307 -- the mode now to ensure that any nodes generated during instantiation
11308 -- are properly marked as Ghost.
11310 Set_Ghost_Mode (Act_Decl_Id);
11312 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11314 -- Re-establish the state of information on which checks are suppressed.
11315 -- This information was set in Body_Info at the point of instantiation,
11316 -- and now we restore it so that the instance is compiled using the
11317 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11319 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11320 Scope_Suppress := Body_Info.Scope_Suppress;
11321 Opt.Ada_Version := Body_Info.Version;
11322 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11323 Restore_Warnings (Body_Info.Warnings);
11325 -- Install the SPARK mode which applies to the package body
11327 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11329 if No (Gen_Body_Id) then
11331 -- Do not look for parent of generic body if none is required.
11332 -- This may happen when the routine is called as part of the
11333 -- Pending_Instantiations processing, when nested instances
11334 -- may precede the one generated from the main unit.
11336 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11337 and then Body_Optional
11338 then
11339 goto Leave;
11340 else
11341 Load_Parent_Of_Generic
11342 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11343 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11344 end if;
11345 end if;
11347 -- Establish global variable for sloc adjustment and for error recovery
11348 -- In the case of an instance body for an instantiation with actuals
11349 -- from a limited view, the instance body is placed at the beginning
11350 -- of the enclosing package body: use the body entity as the source
11351 -- location for nodes of the instance body.
11353 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11354 declare
11355 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11356 Body_Id : constant Node_Id :=
11357 Corresponding_Body (Unit_Declaration_Node (Scop));
11359 begin
11360 Instantiation_Node := Body_Id;
11361 end;
11362 else
11363 Instantiation_Node := Inst_Node;
11364 end if;
11366 if Present (Gen_Body_Id) then
11367 Save_Env (Gen_Unit, Act_Decl_Id);
11368 Style_Check := False;
11370 -- If the context of the instance is subject to SPARK_Mode "off", the
11371 -- annotation is missing, or the body is instantiated at a later pass
11372 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11373 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11374 -- instance.
11376 if SPARK_Mode /= On
11377 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11378 then
11379 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11380 end if;
11382 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11383 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11385 Create_Instantiation_Source
11386 (Inst_Node, Gen_Body_Id, S_Adjustment);
11388 Act_Body :=
11389 Copy_Generic_Node
11390 (Original_Node (Gen_Body), Empty, Instantiating => True);
11392 -- Create proper (possibly qualified) defining name for the body, to
11393 -- correspond to the one in the spec.
11395 Act_Body_Id :=
11396 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11397 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11399 -- Some attributes of spec entity are not inherited by body entity
11401 Set_Handler_Records (Act_Body_Id, No_List);
11403 if Nkind (Defining_Unit_Name (Act_Spec)) =
11404 N_Defining_Program_Unit_Name
11405 then
11406 Act_Body_Name :=
11407 Make_Defining_Program_Unit_Name (Loc,
11408 Name =>
11409 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11410 Defining_Identifier => Act_Body_Id);
11411 else
11412 Act_Body_Name := Act_Body_Id;
11413 end if;
11415 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11417 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11418 Check_Generic_Actuals (Act_Decl_Id, False);
11419 Check_Initialized_Types;
11421 -- Install primitives hidden at the point of the instantiation but
11422 -- visible when processing the generic formals
11424 declare
11425 E : Entity_Id;
11427 begin
11428 E := First_Entity (Act_Decl_Id);
11429 while Present (E) loop
11430 if Is_Type (E)
11431 and then not Is_Itype (E)
11432 and then Is_Generic_Actual_Type (E)
11433 and then Is_Tagged_Type (E)
11434 then
11435 Install_Hidden_Primitives
11436 (Prims_List => Vis_Prims_List,
11437 Gen_T => Generic_Parent_Type (Parent (E)),
11438 Act_T => E);
11439 end if;
11441 Next_Entity (E);
11442 end loop;
11443 end;
11445 -- If it is a child unit, make the parent instance (which is an
11446 -- instance of the parent of the generic) visible. The parent
11447 -- instance is the prefix of the name of the generic unit.
11449 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11450 and then Nkind (Gen_Id) = N_Expanded_Name
11451 then
11452 Par_Ent := Entity (Prefix (Gen_Id));
11453 Par_Vis := Is_Immediately_Visible (Par_Ent);
11454 Install_Parent (Par_Ent, In_Body => True);
11455 Parent_Installed := True;
11457 elsif Is_Child_Unit (Gen_Unit) then
11458 Par_Ent := Scope (Gen_Unit);
11459 Par_Vis := Is_Immediately_Visible (Par_Ent);
11460 Install_Parent (Par_Ent, In_Body => True);
11461 Parent_Installed := True;
11462 end if;
11464 -- If the instantiation is a library unit, and this is the main unit,
11465 -- then build the resulting compilation unit nodes for the instance.
11466 -- If this is a compilation unit but it is not the main unit, then it
11467 -- is the body of a unit in the context, that is being compiled
11468 -- because it is encloses some inlined unit or another generic unit
11469 -- being instantiated. In that case, this body is not part of the
11470 -- current compilation, and is not attached to the tree, but its
11471 -- parent must be set for analysis.
11473 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11475 -- Replace instance node with body of instance, and create new
11476 -- node for corresponding instance declaration.
11478 Build_Instance_Compilation_Unit_Nodes
11479 (Inst_Node, Act_Body, Act_Decl);
11480 Analyze (Inst_Node);
11482 if Parent (Inst_Node) = Cunit (Main_Unit) then
11484 -- If the instance is a child unit itself, then set the scope
11485 -- of the expanded body to be the parent of the instantiation
11486 -- (ensuring that the fully qualified name will be generated
11487 -- for the elaboration subprogram).
11489 if Nkind (Defining_Unit_Name (Act_Spec)) =
11490 N_Defining_Program_Unit_Name
11491 then
11492 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11493 end if;
11494 end if;
11496 -- Case where instantiation is not a library unit
11498 else
11499 -- If this is an early instantiation, i.e. appears textually
11500 -- before the corresponding body and must be elaborated first,
11501 -- indicate that the body instance is to be delayed.
11503 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11505 -- Now analyze the body. We turn off all checks if this is an
11506 -- internal unit, since there is no reason to have checks on for
11507 -- any predefined run-time library code. All such code is designed
11508 -- to be compiled with checks off.
11510 -- Note that we do NOT apply this criterion to children of GNAT
11511 -- The latter units must suppress checks explicitly if needed.
11513 -- We also do not suppress checks in CodePeer mode where we are
11514 -- interested in finding possible runtime errors.
11516 if not CodePeer_Mode
11517 and then In_Predefined_Unit (Gen_Decl)
11518 then
11519 Analyze (Act_Body, Suppress => All_Checks);
11520 else
11521 Analyze (Act_Body);
11522 end if;
11523 end if;
11525 Inherit_Context (Gen_Body, Inst_Node);
11527 -- Remove the parent instances if they have been placed on the scope
11528 -- stack to compile the body.
11530 if Parent_Installed then
11531 Remove_Parent (In_Body => True);
11533 -- Restore the previous visibility of the parent
11535 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11536 end if;
11538 Restore_Hidden_Primitives (Vis_Prims_List);
11539 Restore_Private_Views (Act_Decl_Id);
11541 -- Remove the current unit from visibility if this is an instance
11542 -- that is not elaborated on the fly for inlining purposes.
11544 if not Inlined_Body then
11545 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11546 end if;
11548 Restore_Env;
11550 -- If we have no body, and the unit requires a body, then complain. This
11551 -- complaint is suppressed if we have detected other errors (since a
11552 -- common reason for missing the body is that it had errors).
11553 -- In CodePeer mode, a warning has been emitted already, no need for
11554 -- further messages.
11556 elsif Unit_Requires_Body (Gen_Unit)
11557 and then not Body_Optional
11558 then
11559 if CodePeer_Mode then
11560 null;
11562 elsif Serious_Errors_Detected = 0 then
11563 Error_Msg_NE
11564 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11566 -- Don't attempt to perform any cleanup actions if some other error
11567 -- was already detected, since this can cause blowups.
11569 else
11570 goto Leave;
11571 end if;
11573 -- Case of package that does not need a body
11575 else
11576 -- If the instantiation of the declaration is a library unit, rewrite
11577 -- the original package instantiation as a package declaration in the
11578 -- compilation unit node.
11580 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11581 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11582 Rewrite (Inst_Node, Act_Decl);
11584 -- Generate elaboration entity, in case spec has elaboration code.
11585 -- This cannot be done when the instance is analyzed, because it
11586 -- is not known yet whether the body exists.
11588 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11589 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11591 -- If the instantiation is not a library unit, then append the
11592 -- declaration to the list of implicitly generated entities, unless
11593 -- it is already a list member which means that it was already
11594 -- processed
11596 elsif not Is_List_Member (Act_Decl) then
11597 Mark_Rewrite_Insertion (Act_Decl);
11598 Insert_Before (Inst_Node, Act_Decl);
11599 end if;
11600 end if;
11602 Expander_Mode_Restore;
11604 <<Leave>>
11605 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11606 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11607 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11608 Style_Check := Saved_Style_Check;
11609 end Instantiate_Package_Body;
11611 ---------------------------------
11612 -- Instantiate_Subprogram_Body --
11613 ---------------------------------
11615 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11616 -- must be replaced by gotos which jump to the end of the routine in order
11617 -- to restore the Ghost and SPARK modes.
11619 procedure Instantiate_Subprogram_Body
11620 (Body_Info : Pending_Body_Info;
11621 Body_Optional : Boolean := False)
11623 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11624 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11625 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11626 Gen_Id : constant Node_Id := Name (Inst_Node);
11627 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11628 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11629 Loc : constant Source_Ptr := Sloc (Inst_Node);
11630 Pack_Id : constant Entity_Id :=
11631 Defining_Unit_Name (Parent (Act_Decl));
11633 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11634 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11635 Saved_ISMP : constant Boolean :=
11636 Ignore_SPARK_Mode_Pragmas_In_Instance;
11637 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11638 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11639 -- Save the Ghost and SPARK mode-related data to restore on exit
11641 Saved_Style_Check : constant Boolean := Style_Check;
11642 Saved_Warnings : constant Warning_Record := Save_Warnings;
11644 Act_Body : Node_Id;
11645 Act_Body_Id : Entity_Id;
11646 Gen_Body : Node_Id;
11647 Gen_Body_Id : Node_Id;
11648 Pack_Body : Node_Id;
11649 Par_Ent : Entity_Id := Empty;
11650 Par_Vis : Boolean := False;
11651 Ret_Expr : Node_Id;
11653 Parent_Installed : Boolean := False;
11655 begin
11656 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11658 -- Subprogram body may have been created already because of an inline
11659 -- pragma, or because of multiple elaborations of the enclosing package
11660 -- when several instances of the subprogram appear in the main unit.
11662 if Present (Corresponding_Body (Act_Decl)) then
11663 return;
11664 end if;
11666 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11667 -- the mode now to ensure that any nodes generated during instantiation
11668 -- are properly marked as Ghost.
11670 Set_Ghost_Mode (Act_Decl_Id);
11672 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11674 -- Re-establish the state of information on which checks are suppressed.
11675 -- This information was set in Body_Info at the point of instantiation,
11676 -- and now we restore it so that the instance is compiled using the
11677 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11679 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11680 Scope_Suppress := Body_Info.Scope_Suppress;
11681 Opt.Ada_Version := Body_Info.Version;
11682 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11683 Restore_Warnings (Body_Info.Warnings);
11685 -- Install the SPARK mode which applies to the subprogram body from the
11686 -- instantiation context. This may be refined further if an explicit
11687 -- SPARK_Mode pragma applies to the generic body.
11689 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11691 if No (Gen_Body_Id) then
11693 -- For imported generic subprogram, no body to compile, complete
11694 -- the spec entity appropriately.
11696 if Is_Imported (Gen_Unit) then
11697 Set_Is_Imported (Act_Decl_Id);
11698 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11699 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11700 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11701 Set_Has_Completion (Act_Decl_Id);
11702 goto Leave;
11704 -- For other cases, compile the body
11706 else
11707 Load_Parent_Of_Generic
11708 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11709 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11710 end if;
11711 end if;
11713 Instantiation_Node := Inst_Node;
11715 if Present (Gen_Body_Id) then
11716 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11718 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11720 -- Either body is not present, or context is non-expanding, as
11721 -- when compiling a subunit. Mark the instance as completed, and
11722 -- diagnose a missing body when needed.
11724 if Expander_Active
11725 and then Operating_Mode = Generate_Code
11726 then
11727 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11728 end if;
11730 Set_Has_Completion (Act_Decl_Id);
11731 goto Leave;
11732 end if;
11734 Save_Env (Gen_Unit, Act_Decl_Id);
11735 Style_Check := False;
11737 -- If the context of the instance is subject to SPARK_Mode "off", the
11738 -- annotation is missing, or the body is instantiated at a later pass
11739 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11740 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11741 -- instance.
11743 if SPARK_Mode /= On
11744 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11745 then
11746 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11747 end if;
11749 -- If the context of an instance is not subject to SPARK_Mode "off",
11750 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11751 -- the latter should be the one applicable to the instance.
11753 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11754 and then SPARK_Mode /= Off
11755 and then Present (SPARK_Pragma (Gen_Body_Id))
11756 then
11757 Set_SPARK_Mode (Gen_Body_Id);
11758 end if;
11760 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11761 Create_Instantiation_Source
11762 (Inst_Node,
11763 Gen_Body_Id,
11764 S_Adjustment);
11766 Act_Body :=
11767 Copy_Generic_Node
11768 (Original_Node (Gen_Body), Empty, Instantiating => True);
11770 -- Create proper defining name for the body, to correspond to the one
11771 -- in the spec.
11773 Act_Body_Id :=
11774 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11776 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11777 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11779 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11780 Set_Has_Completion (Act_Decl_Id);
11781 Check_Generic_Actuals (Pack_Id, False);
11783 -- Generate a reference to link the visible subprogram instance to
11784 -- the generic body, which for navigation purposes is the only
11785 -- available source for the instance.
11787 Generate_Reference
11788 (Related_Instance (Pack_Id),
11789 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11791 -- If it is a child unit, make the parent instance (which is an
11792 -- instance of the parent of the generic) visible. The parent
11793 -- instance is the prefix of the name of the generic unit.
11795 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11796 and then Nkind (Gen_Id) = N_Expanded_Name
11797 then
11798 Par_Ent := Entity (Prefix (Gen_Id));
11799 Par_Vis := Is_Immediately_Visible (Par_Ent);
11800 Install_Parent (Par_Ent, In_Body => True);
11801 Parent_Installed := True;
11803 elsif Is_Child_Unit (Gen_Unit) then
11804 Par_Ent := Scope (Gen_Unit);
11805 Par_Vis := Is_Immediately_Visible (Par_Ent);
11806 Install_Parent (Par_Ent, In_Body => True);
11807 Parent_Installed := True;
11808 end if;
11810 -- Subprogram body is placed in the body of wrapper package,
11811 -- whose spec contains the subprogram declaration as well as
11812 -- the renaming declarations for the generic parameters.
11814 Pack_Body :=
11815 Make_Package_Body (Loc,
11816 Defining_Unit_Name => New_Copy (Pack_Id),
11817 Declarations => New_List (Act_Body));
11819 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11821 -- If the instantiation is a library unit, then build resulting
11822 -- compilation unit nodes for the instance. The declaration of
11823 -- the enclosing package is the grandparent of the subprogram
11824 -- declaration. First replace the instantiation node as the unit
11825 -- of the corresponding compilation.
11827 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11828 if Parent (Inst_Node) = Cunit (Main_Unit) then
11829 Set_Unit (Parent (Inst_Node), Inst_Node);
11830 Build_Instance_Compilation_Unit_Nodes
11831 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11832 Analyze (Inst_Node);
11833 else
11834 Set_Parent (Pack_Body, Parent (Inst_Node));
11835 Analyze (Pack_Body);
11836 end if;
11838 else
11839 Insert_Before (Inst_Node, Pack_Body);
11840 Mark_Rewrite_Insertion (Pack_Body);
11841 Analyze (Pack_Body);
11843 if Expander_Active then
11844 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11845 end if;
11846 end if;
11848 Inherit_Context (Gen_Body, Inst_Node);
11850 Restore_Private_Views (Pack_Id, False);
11852 if Parent_Installed then
11853 Remove_Parent (In_Body => True);
11855 -- Restore the previous visibility of the parent
11857 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11858 end if;
11860 Restore_Env;
11861 Restore_Warnings (Saved_Warnings);
11863 -- Body not found. Error was emitted already. If there were no previous
11864 -- errors, this may be an instance whose scope is a premature instance.
11865 -- In that case we must insure that the (legal) program does raise
11866 -- program error if executed. We generate a subprogram body for this
11867 -- purpose. See DEC ac30vso.
11869 -- Should not reference proprietary DEC tests in comments ???
11871 elsif Serious_Errors_Detected = 0
11872 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11873 then
11874 if Body_Optional then
11875 goto Leave;
11877 elsif Ekind (Act_Decl_Id) = E_Procedure then
11878 Act_Body :=
11879 Make_Subprogram_Body (Loc,
11880 Specification =>
11881 Make_Procedure_Specification (Loc,
11882 Defining_Unit_Name =>
11883 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11884 Parameter_Specifications =>
11885 New_Copy_List
11886 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11888 Declarations => Empty_List,
11889 Handled_Statement_Sequence =>
11890 Make_Handled_Sequence_Of_Statements (Loc,
11891 Statements => New_List (
11892 Make_Raise_Program_Error (Loc,
11893 Reason => PE_Access_Before_Elaboration))));
11895 else
11896 Ret_Expr :=
11897 Make_Raise_Program_Error (Loc,
11898 Reason => PE_Access_Before_Elaboration);
11900 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11901 Set_Analyzed (Ret_Expr);
11903 Act_Body :=
11904 Make_Subprogram_Body (Loc,
11905 Specification =>
11906 Make_Function_Specification (Loc,
11907 Defining_Unit_Name =>
11908 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11909 Parameter_Specifications =>
11910 New_Copy_List
11911 (Parameter_Specifications (Parent (Act_Decl_Id))),
11912 Result_Definition =>
11913 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11915 Declarations => Empty_List,
11916 Handled_Statement_Sequence =>
11917 Make_Handled_Sequence_Of_Statements (Loc,
11918 Statements => New_List (
11919 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11920 end if;
11922 Pack_Body :=
11923 Make_Package_Body (Loc,
11924 Defining_Unit_Name => New_Copy (Pack_Id),
11925 Declarations => New_List (Act_Body));
11927 Insert_After (Inst_Node, Pack_Body);
11928 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11929 Analyze (Pack_Body);
11930 end if;
11932 Expander_Mode_Restore;
11934 <<Leave>>
11935 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11936 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11937 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11938 Style_Check := Saved_Style_Check;
11939 end Instantiate_Subprogram_Body;
11941 ----------------------
11942 -- Instantiate_Type --
11943 ----------------------
11945 function Instantiate_Type
11946 (Formal : Node_Id;
11947 Actual : Node_Id;
11948 Analyzed_Formal : Node_Id;
11949 Actual_Decls : List_Id) return List_Id
11951 A_Gen_T : constant Entity_Id :=
11952 Defining_Identifier (Analyzed_Formal);
11953 Def : constant Node_Id := Formal_Type_Definition (Formal);
11954 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11955 Act_T : Entity_Id;
11956 Ancestor : Entity_Id := Empty;
11957 Decl_Node : Node_Id;
11958 Decl_Nodes : List_Id;
11959 Loc : Source_Ptr;
11960 Subt : Entity_Id;
11962 procedure Diagnose_Predicated_Actual;
11963 -- There are a number of constructs in which a discrete type with
11964 -- predicates is illegal, e.g. as an index in an array type declaration.
11965 -- If a generic type is used is such a construct in a generic package
11966 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11967 -- of the generic contract that the actual cannot have predicates.
11969 procedure Validate_Array_Type_Instance;
11970 procedure Validate_Access_Subprogram_Instance;
11971 procedure Validate_Access_Type_Instance;
11972 procedure Validate_Derived_Type_Instance;
11973 procedure Validate_Derived_Interface_Type_Instance;
11974 procedure Validate_Discriminated_Formal_Type;
11975 procedure Validate_Interface_Type_Instance;
11976 procedure Validate_Private_Type_Instance;
11977 procedure Validate_Incomplete_Type_Instance;
11978 -- These procedures perform validation tests for the named case.
11979 -- Validate_Discriminated_Formal_Type is shared by formal private
11980 -- types and Ada 2012 formal incomplete types.
11982 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11983 -- Check that base types are the same and that the subtypes match
11984 -- statically. Used in several of the above.
11986 ---------------------------------
11987 -- Diagnose_Predicated_Actual --
11988 ---------------------------------
11990 procedure Diagnose_Predicated_Actual is
11991 begin
11992 if No_Predicate_On_Actual (A_Gen_T)
11993 and then Has_Predicates (Act_T)
11994 then
11995 Error_Msg_NE
11996 ("actual for& cannot be a type with predicate",
11997 Instantiation_Node, A_Gen_T);
11999 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12000 and then Has_Predicates (Act_T)
12001 and then not Has_Static_Predicate_Aspect (Act_T)
12002 then
12003 Error_Msg_NE
12004 ("actual for& cannot be a type with a dynamic predicate",
12005 Instantiation_Node, A_Gen_T);
12006 end if;
12007 end Diagnose_Predicated_Actual;
12009 --------------------
12010 -- Subtypes_Match --
12011 --------------------
12013 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12014 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12016 begin
12017 -- Some detailed comments would be useful here ???
12019 return ((Base_Type (T) = Act_T
12020 or else Base_Type (T) = Base_Type (Act_T))
12021 and then Subtypes_Statically_Match (T, Act_T))
12023 or else (Is_Class_Wide_Type (Gen_T)
12024 and then Is_Class_Wide_Type (Act_T)
12025 and then Subtypes_Match
12026 (Get_Instance_Of (Root_Type (Gen_T)),
12027 Root_Type (Act_T)))
12029 or else
12030 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12031 E_Anonymous_Access_Type)
12032 and then Ekind (Act_T) = Ekind (Gen_T)
12033 and then Subtypes_Statically_Match
12034 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12035 end Subtypes_Match;
12037 -----------------------------------------
12038 -- Validate_Access_Subprogram_Instance --
12039 -----------------------------------------
12041 procedure Validate_Access_Subprogram_Instance is
12042 begin
12043 if not Is_Access_Type (Act_T)
12044 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12045 then
12046 Error_Msg_NE
12047 ("expect access type in instantiation of &", Actual, Gen_T);
12048 Abandon_Instantiation (Actual);
12049 end if;
12051 -- According to AI05-288, actuals for access_to_subprograms must be
12052 -- subtype conformant with the generic formal. Previous to AI05-288
12053 -- only mode conformance was required.
12055 -- This is a binding interpretation that applies to previous versions
12056 -- of the language, no need to maintain previous weaker checks.
12058 Check_Subtype_Conformant
12059 (Designated_Type (Act_T),
12060 Designated_Type (A_Gen_T),
12061 Actual,
12062 Get_Inst => True);
12064 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12065 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12066 Error_Msg_NE
12067 ("protected access type not allowed for formal &",
12068 Actual, Gen_T);
12069 end if;
12071 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12072 Error_Msg_NE
12073 ("expect protected access type for formal &",
12074 Actual, Gen_T);
12075 end if;
12077 -- If the formal has a specified convention (which in most cases
12078 -- will be StdCall) verify that the actual has the same convention.
12080 if Has_Convention_Pragma (A_Gen_T)
12081 and then Convention (A_Gen_T) /= Convention (Act_T)
12082 then
12083 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12084 Error_Msg_NE
12085 ("actual for formal & must have convention %", Actual, Gen_T);
12086 end if;
12087 end Validate_Access_Subprogram_Instance;
12089 -----------------------------------
12090 -- Validate_Access_Type_Instance --
12091 -----------------------------------
12093 procedure Validate_Access_Type_Instance is
12094 Desig_Type : constant Entity_Id :=
12095 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12096 Desig_Act : Entity_Id;
12098 begin
12099 if not Is_Access_Type (Act_T) then
12100 Error_Msg_NE
12101 ("expect access type in instantiation of &", Actual, Gen_T);
12102 Abandon_Instantiation (Actual);
12103 end if;
12105 if Is_Access_Constant (A_Gen_T) then
12106 if not Is_Access_Constant (Act_T) then
12107 Error_Msg_N
12108 ("actual type must be access-to-constant type", Actual);
12109 Abandon_Instantiation (Actual);
12110 end if;
12111 else
12112 if Is_Access_Constant (Act_T) then
12113 Error_Msg_N
12114 ("actual type must be access-to-variable type", Actual);
12115 Abandon_Instantiation (Actual);
12117 elsif Ekind (A_Gen_T) = E_General_Access_Type
12118 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12119 then
12120 Error_Msg_N -- CODEFIX
12121 ("actual must be general access type!", Actual);
12122 Error_Msg_NE -- CODEFIX
12123 ("add ALL to }!", Actual, Act_T);
12124 Abandon_Instantiation (Actual);
12125 end if;
12126 end if;
12128 -- The designated subtypes, that is to say the subtypes introduced
12129 -- by an access type declaration (and not by a subtype declaration)
12130 -- must match.
12132 Desig_Act := Designated_Type (Base_Type (Act_T));
12134 -- The designated type may have been introduced through a limited_
12135 -- with clause, in which case retrieve the non-limited view. This
12136 -- applies to incomplete types as well as to class-wide types.
12138 if From_Limited_With (Desig_Act) then
12139 Desig_Act := Available_View (Desig_Act);
12140 end if;
12142 if not Subtypes_Match (Desig_Type, Desig_Act) then
12143 Error_Msg_NE
12144 ("designated type of actual does not match that of formal &",
12145 Actual, Gen_T);
12147 if not Predicates_Match (Desig_Type, Desig_Act) then
12148 Error_Msg_N ("\predicates do not match", Actual);
12149 end if;
12151 Abandon_Instantiation (Actual);
12153 elsif Is_Access_Type (Designated_Type (Act_T))
12154 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12156 Is_Constrained (Designated_Type (Desig_Type))
12157 then
12158 Error_Msg_NE
12159 ("designated type of actual does not match that of formal &",
12160 Actual, Gen_T);
12162 if not Predicates_Match (Desig_Type, Desig_Act) then
12163 Error_Msg_N ("\predicates do not match", Actual);
12164 end if;
12166 Abandon_Instantiation (Actual);
12167 end if;
12169 -- Ada 2005: null-exclusion indicators of the two types must agree
12171 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12172 Error_Msg_NE
12173 ("non null exclusion of actual and formal & do not match",
12174 Actual, Gen_T);
12175 end if;
12176 end Validate_Access_Type_Instance;
12178 ----------------------------------
12179 -- Validate_Array_Type_Instance --
12180 ----------------------------------
12182 procedure Validate_Array_Type_Instance is
12183 I1 : Node_Id;
12184 I2 : Node_Id;
12185 T2 : Entity_Id;
12187 function Formal_Dimensions return Nat;
12188 -- Count number of dimensions in array type formal
12190 -----------------------
12191 -- Formal_Dimensions --
12192 -----------------------
12194 function Formal_Dimensions return Nat is
12195 Num : Nat := 0;
12196 Index : Node_Id;
12198 begin
12199 if Nkind (Def) = N_Constrained_Array_Definition then
12200 Index := First (Discrete_Subtype_Definitions (Def));
12201 else
12202 Index := First (Subtype_Marks (Def));
12203 end if;
12205 while Present (Index) loop
12206 Num := Num + 1;
12207 Next_Index (Index);
12208 end loop;
12210 return Num;
12211 end Formal_Dimensions;
12213 -- Start of processing for Validate_Array_Type_Instance
12215 begin
12216 if not Is_Array_Type (Act_T) then
12217 Error_Msg_NE
12218 ("expect array type in instantiation of &", Actual, Gen_T);
12219 Abandon_Instantiation (Actual);
12221 elsif Nkind (Def) = N_Constrained_Array_Definition then
12222 if not (Is_Constrained (Act_T)) then
12223 Error_Msg_NE
12224 ("expect constrained array in instantiation of &",
12225 Actual, Gen_T);
12226 Abandon_Instantiation (Actual);
12227 end if;
12229 else
12230 if Is_Constrained (Act_T) then
12231 Error_Msg_NE
12232 ("expect unconstrained array in instantiation of &",
12233 Actual, Gen_T);
12234 Abandon_Instantiation (Actual);
12235 end if;
12236 end if;
12238 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12239 Error_Msg_NE
12240 ("dimensions of actual do not match formal &", Actual, Gen_T);
12241 Abandon_Instantiation (Actual);
12242 end if;
12244 I1 := First_Index (A_Gen_T);
12245 I2 := First_Index (Act_T);
12246 for J in 1 .. Formal_Dimensions loop
12248 -- If the indexes of the actual were given by a subtype_mark,
12249 -- the index was transformed into a range attribute. Retrieve
12250 -- the original type mark for checking.
12252 if Is_Entity_Name (Original_Node (I2)) then
12253 T2 := Entity (Original_Node (I2));
12254 else
12255 T2 := Etype (I2);
12256 end if;
12258 if not Subtypes_Match
12259 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12260 then
12261 Error_Msg_NE
12262 ("index types of actual do not match those of formal &",
12263 Actual, Gen_T);
12264 Abandon_Instantiation (Actual);
12265 end if;
12267 Next_Index (I1);
12268 Next_Index (I2);
12269 end loop;
12271 -- Check matching subtypes. Note that there are complex visibility
12272 -- issues when the generic is a child unit and some aspect of the
12273 -- generic type is declared in a parent unit of the generic. We do
12274 -- the test to handle this special case only after a direct check
12275 -- for static matching has failed. The case where both the component
12276 -- type and the array type are separate formals, and the component
12277 -- type is a private view may also require special checking in
12278 -- Subtypes_Match. Finally, we assume that a child instance where
12279 -- the component type comes from a formal of a parent instance is
12280 -- correct because the generic was correct. A more precise check
12281 -- seems too complex to install???
12283 if Subtypes_Match
12284 (Component_Type (A_Gen_T), Component_Type (Act_T))
12285 or else
12286 Subtypes_Match
12287 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12288 Component_Type (Act_T))
12289 or else
12290 (not Inside_A_Generic
12291 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12292 then
12293 null;
12294 else
12295 Error_Msg_NE
12296 ("component subtype of actual does not match that of formal &",
12297 Actual, Gen_T);
12298 Abandon_Instantiation (Actual);
12299 end if;
12301 if Has_Aliased_Components (A_Gen_T)
12302 and then not Has_Aliased_Components (Act_T)
12303 then
12304 Error_Msg_NE
12305 ("actual must have aliased components to match formal type &",
12306 Actual, Gen_T);
12307 end if;
12308 end Validate_Array_Type_Instance;
12310 -----------------------------------------------
12311 -- Validate_Derived_Interface_Type_Instance --
12312 -----------------------------------------------
12314 procedure Validate_Derived_Interface_Type_Instance is
12315 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12316 Elmt : Elmt_Id;
12318 begin
12319 -- First apply interface instance checks
12321 Validate_Interface_Type_Instance;
12323 -- Verify that immediate parent interface is an ancestor of
12324 -- the actual.
12326 if Present (Par)
12327 and then not Interface_Present_In_Ancestor (Act_T, Par)
12328 then
12329 Error_Msg_NE
12330 ("interface actual must include progenitor&", Actual, Par);
12331 end if;
12333 -- Now verify that the actual includes all other ancestors of
12334 -- the formal.
12336 Elmt := First_Elmt (Interfaces (A_Gen_T));
12337 while Present (Elmt) loop
12338 if not Interface_Present_In_Ancestor
12339 (Act_T, Get_Instance_Of (Node (Elmt)))
12340 then
12341 Error_Msg_NE
12342 ("interface actual must include progenitor&",
12343 Actual, Node (Elmt));
12344 end if;
12346 Next_Elmt (Elmt);
12347 end loop;
12348 end Validate_Derived_Interface_Type_Instance;
12350 ------------------------------------
12351 -- Validate_Derived_Type_Instance --
12352 ------------------------------------
12354 procedure Validate_Derived_Type_Instance is
12355 Actual_Discr : Entity_Id;
12356 Ancestor_Discr : Entity_Id;
12358 begin
12359 -- Verify that the actual includes the progenitors of the formal,
12360 -- if any. The formal may depend on previous formals and their
12361 -- instance, so we must examine instance of interfaces if present.
12362 -- The actual may be an extension of an interface, in which case
12363 -- it does not appear in the interface list, so this must be
12364 -- checked separately.
12366 if Present (Interface_List (Def)) then
12367 if not Has_Interfaces (Act_T) then
12368 Error_Msg_NE
12369 ("actual must implement all interfaces of formal&",
12370 Actual, A_Gen_T);
12372 else
12373 declare
12374 Act_Iface_List : Elist_Id;
12375 Iface : Node_Id;
12376 Iface_Ent : Entity_Id;
12378 function Instance_Exists (I : Entity_Id) return Boolean;
12379 -- If the interface entity is declared in a generic unit,
12380 -- this can only be legal if we are within an instantiation
12381 -- of a child of that generic. There is currently no
12382 -- mechanism to relate an interface declared within a
12383 -- generic to the corresponding interface in an instance,
12384 -- so we traverse the list of interfaces of the actual,
12385 -- looking for a name match.
12387 ---------------------
12388 -- Instance_Exists --
12389 ---------------------
12391 function Instance_Exists (I : Entity_Id) return Boolean is
12392 Iface_Elmt : Elmt_Id;
12394 begin
12395 Iface_Elmt := First_Elmt (Act_Iface_List);
12396 while Present (Iface_Elmt) loop
12397 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
12398 and then Chars (Node (Iface_Elmt)) = Chars (I)
12399 then
12400 return True;
12401 end if;
12403 Next_Elmt (Iface_Elmt);
12404 end loop;
12406 return False;
12407 end Instance_Exists;
12409 begin
12410 Iface := First (Abstract_Interface_List (A_Gen_T));
12411 Collect_Interfaces (Act_T, Act_Iface_List);
12413 while Present (Iface) loop
12414 Iface_Ent := Get_Instance_Of (Entity (Iface));
12416 if Is_Ancestor (Iface_Ent, Act_T)
12417 or else Is_Progenitor (Iface_Ent, Act_T)
12418 then
12419 null;
12421 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
12422 and then Instance_Exists (Iface_Ent)
12423 then
12424 null;
12426 else
12427 Error_Msg_Name_1 := Chars (Act_T);
12428 Error_Msg_NE
12429 ("Actual% must implement interface&",
12430 Actual, Etype (Iface));
12431 end if;
12433 Next (Iface);
12434 end loop;
12435 end;
12436 end if;
12437 end if;
12439 -- If the parent type in the generic declaration is itself a previous
12440 -- formal type, then it is local to the generic and absent from the
12441 -- analyzed generic definition. In that case the ancestor is the
12442 -- instance of the formal (which must have been instantiated
12443 -- previously), unless the ancestor is itself a formal derived type.
12444 -- In this latter case (which is the subject of Corrigendum 8652/0038
12445 -- (AI-202) the ancestor of the formals is the ancestor of its
12446 -- parent. Otherwise, the analyzed generic carries the parent type.
12447 -- If the parent type is defined in a previous formal package, then
12448 -- the scope of that formal package is that of the generic type
12449 -- itself, and it has already been mapped into the corresponding type
12450 -- in the actual package.
12452 -- Common case: parent type defined outside of the generic
12454 if Is_Entity_Name (Subtype_Mark (Def))
12455 and then Present (Entity (Subtype_Mark (Def)))
12456 then
12457 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12459 -- Check whether parent is defined in a previous formal package
12461 elsif
12462 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12463 then
12464 Ancestor :=
12465 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12467 -- The type may be a local derivation, or a type extension of a
12468 -- previous formal, or of a formal of a parent package.
12470 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12471 or else
12472 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12473 then
12474 -- Check whether the parent is another derived formal type in the
12475 -- same generic unit.
12477 if Etype (A_Gen_T) /= A_Gen_T
12478 and then Is_Generic_Type (Etype (A_Gen_T))
12479 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12480 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12481 then
12482 -- Locate ancestor of parent from the subtype declaration
12483 -- created for the actual.
12485 declare
12486 Decl : Node_Id;
12488 begin
12489 Decl := First (Actual_Decls);
12490 while Present (Decl) loop
12491 if Nkind (Decl) = N_Subtype_Declaration
12492 and then Chars (Defining_Identifier (Decl)) =
12493 Chars (Etype (A_Gen_T))
12494 then
12495 Ancestor := Generic_Parent_Type (Decl);
12496 exit;
12497 else
12498 Next (Decl);
12499 end if;
12500 end loop;
12501 end;
12503 pragma Assert (Present (Ancestor));
12505 -- The ancestor itself may be a previous formal that has been
12506 -- instantiated.
12508 Ancestor := Get_Instance_Of (Ancestor);
12510 else
12511 Ancestor :=
12512 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12513 end if;
12515 -- Check whether parent is a previous formal of the current generic
12517 elsif Is_Derived_Type (A_Gen_T)
12518 and then Is_Generic_Type (Etype (A_Gen_T))
12519 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12520 then
12521 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12523 -- An unusual case: the actual is a type declared in a parent unit,
12524 -- but is not a formal type so there is no instance_of for it.
12525 -- Retrieve it by analyzing the record extension.
12527 elsif Is_Child_Unit (Scope (A_Gen_T))
12528 and then In_Open_Scopes (Scope (Act_T))
12529 and then Is_Generic_Instance (Scope (Act_T))
12530 then
12531 Analyze (Subtype_Mark (Def));
12532 Ancestor := Entity (Subtype_Mark (Def));
12534 else
12535 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12536 end if;
12538 -- If the formal derived type has pragma Preelaborable_Initialization
12539 -- then the actual type must have preelaborable initialization.
12541 if Known_To_Have_Preelab_Init (A_Gen_T)
12542 and then not Has_Preelaborable_Initialization (Act_T)
12543 then
12544 Error_Msg_NE
12545 ("actual for & must have preelaborable initialization",
12546 Actual, Gen_T);
12547 end if;
12549 -- Ada 2005 (AI-251)
12551 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12552 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12553 Error_Msg_NE
12554 ("(Ada 2005) expected type implementing & in instantiation",
12555 Actual, Ancestor);
12556 end if;
12558 -- Finally verify that the (instance of) the ancestor is an ancestor
12559 -- of the actual.
12561 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12562 Error_Msg_NE
12563 ("expect type derived from & in instantiation",
12564 Actual, First_Subtype (Ancestor));
12565 Abandon_Instantiation (Actual);
12566 end if;
12568 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12569 -- that the formal type declaration has been rewritten as a private
12570 -- extension.
12572 if Ada_Version >= Ada_2005
12573 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12574 and then Synchronized_Present (Parent (A_Gen_T))
12575 then
12576 -- The actual must be a synchronized tagged type
12578 if not Is_Tagged_Type (Act_T) then
12579 Error_Msg_N
12580 ("actual of synchronized type must be tagged", Actual);
12581 Abandon_Instantiation (Actual);
12583 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12584 and then Nkind (Type_Definition (Parent (Act_T))) =
12585 N_Derived_Type_Definition
12586 and then not Synchronized_Present
12587 (Type_Definition (Parent (Act_T)))
12588 then
12589 Error_Msg_N
12590 ("actual of synchronized type must be synchronized", Actual);
12591 Abandon_Instantiation (Actual);
12592 end if;
12593 end if;
12595 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12596 -- removes the second instance of the phrase "or allow pass by copy".
12598 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12599 Error_Msg_N
12600 ("cannot have atomic actual type for non-atomic formal type",
12601 Actual);
12603 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12604 Error_Msg_N
12605 ("cannot have volatile actual type for non-volatile formal type",
12606 Actual);
12607 end if;
12609 -- It should not be necessary to check for unknown discriminants on
12610 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12611 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12612 -- needs fixing. ???
12614 if Is_Definite_Subtype (A_Gen_T)
12615 and then not Unknown_Discriminants_Present (Formal)
12616 and then not Is_Definite_Subtype (Act_T)
12617 then
12618 Error_Msg_N ("actual subtype must be constrained", Actual);
12619 Abandon_Instantiation (Actual);
12620 end if;
12622 if not Unknown_Discriminants_Present (Formal) then
12623 if Is_Constrained (Ancestor) then
12624 if not Is_Constrained (Act_T) then
12625 Error_Msg_N ("actual subtype must be constrained", Actual);
12626 Abandon_Instantiation (Actual);
12627 end if;
12629 -- Ancestor is unconstrained, Check if generic formal and actual
12630 -- agree on constrainedness. The check only applies to array types
12631 -- and discriminated types.
12633 elsif Is_Constrained (Act_T) then
12634 if Ekind (Ancestor) = E_Access_Type
12635 or else (not Is_Constrained (A_Gen_T)
12636 and then Is_Composite_Type (A_Gen_T))
12637 then
12638 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12639 Abandon_Instantiation (Actual);
12640 end if;
12642 -- A class-wide type is only allowed if the formal has unknown
12643 -- discriminants.
12645 elsif Is_Class_Wide_Type (Act_T)
12646 and then not Has_Unknown_Discriminants (Ancestor)
12647 then
12648 Error_Msg_NE
12649 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12650 Abandon_Instantiation (Actual);
12652 -- Otherwise, the formal and actual must have the same number
12653 -- of discriminants and each discriminant of the actual must
12654 -- correspond to a discriminant of the formal.
12656 elsif Has_Discriminants (Act_T)
12657 and then not Has_Unknown_Discriminants (Act_T)
12658 and then Has_Discriminants (Ancestor)
12659 then
12660 Actual_Discr := First_Discriminant (Act_T);
12661 Ancestor_Discr := First_Discriminant (Ancestor);
12662 while Present (Actual_Discr)
12663 and then Present (Ancestor_Discr)
12664 loop
12665 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12666 No (Corresponding_Discriminant (Actual_Discr))
12667 then
12668 Error_Msg_NE
12669 ("discriminant & does not correspond "
12670 & "to ancestor discriminant", Actual, Actual_Discr);
12671 Abandon_Instantiation (Actual);
12672 end if;
12674 Next_Discriminant (Actual_Discr);
12675 Next_Discriminant (Ancestor_Discr);
12676 end loop;
12678 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12679 Error_Msg_NE
12680 ("actual for & must have same number of discriminants",
12681 Actual, Gen_T);
12682 Abandon_Instantiation (Actual);
12683 end if;
12685 -- This case should be caught by the earlier check for
12686 -- constrainedness, but the check here is added for completeness.
12688 elsif Has_Discriminants (Act_T)
12689 and then not Has_Unknown_Discriminants (Act_T)
12690 then
12691 Error_Msg_NE
12692 ("actual for & must not have discriminants", Actual, Gen_T);
12693 Abandon_Instantiation (Actual);
12695 elsif Has_Discriminants (Ancestor) then
12696 Error_Msg_NE
12697 ("actual for & must have known discriminants", Actual, Gen_T);
12698 Abandon_Instantiation (Actual);
12699 end if;
12701 if not Subtypes_Statically_Compatible
12702 (Act_T, Ancestor, Formal_Derived_Matching => True)
12703 then
12704 Error_Msg_N
12705 ("constraint on actual is incompatible with formal", Actual);
12706 Abandon_Instantiation (Actual);
12707 end if;
12708 end if;
12710 -- If the formal and actual types are abstract, check that there
12711 -- are no abstract primitives of the actual type that correspond to
12712 -- nonabstract primitives of the formal type (second sentence of
12713 -- RM95 3.9.3(9)).
12715 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12716 Check_Abstract_Primitives : declare
12717 Gen_Prims : constant Elist_Id :=
12718 Primitive_Operations (A_Gen_T);
12719 Gen_Elmt : Elmt_Id;
12720 Gen_Subp : Entity_Id;
12721 Anc_Subp : Entity_Id;
12722 Anc_Formal : Entity_Id;
12723 Anc_F_Type : Entity_Id;
12725 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12726 Act_Elmt : Elmt_Id;
12727 Act_Subp : Entity_Id;
12728 Act_Formal : Entity_Id;
12729 Act_F_Type : Entity_Id;
12731 Subprograms_Correspond : Boolean;
12733 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12734 -- Returns true if T2 is derived directly or indirectly from
12735 -- T1, including derivations from interfaces. T1 and T2 are
12736 -- required to be specific tagged base types.
12738 ------------------------
12739 -- Is_Tagged_Ancestor --
12740 ------------------------
12742 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12744 Intfc_Elmt : Elmt_Id;
12746 begin
12747 -- The predicate is satisfied if the types are the same
12749 if T1 = T2 then
12750 return True;
12752 -- If we've reached the top of the derivation chain then
12753 -- we know that T1 is not an ancestor of T2.
12755 elsif Etype (T2) = T2 then
12756 return False;
12758 -- Proceed to check T2's immediate parent
12760 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12761 return True;
12763 -- Finally, check to see if T1 is an ancestor of any of T2's
12764 -- progenitors.
12766 else
12767 Intfc_Elmt := First_Elmt (Interfaces (T2));
12768 while Present (Intfc_Elmt) loop
12769 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12770 return True;
12771 end if;
12773 Next_Elmt (Intfc_Elmt);
12774 end loop;
12775 end if;
12777 return False;
12778 end Is_Tagged_Ancestor;
12780 -- Start of processing for Check_Abstract_Primitives
12782 begin
12783 -- Loop over all of the formal derived type's primitives
12785 Gen_Elmt := First_Elmt (Gen_Prims);
12786 while Present (Gen_Elmt) loop
12787 Gen_Subp := Node (Gen_Elmt);
12789 -- If the primitive of the formal is not abstract, then
12790 -- determine whether there is a corresponding primitive of
12791 -- the actual type that's abstract.
12793 if not Is_Abstract_Subprogram (Gen_Subp) then
12794 Act_Elmt := First_Elmt (Act_Prims);
12795 while Present (Act_Elmt) loop
12796 Act_Subp := Node (Act_Elmt);
12798 -- If we find an abstract primitive of the actual,
12799 -- then we need to test whether it corresponds to the
12800 -- subprogram from which the generic formal primitive
12801 -- is inherited.
12803 if Is_Abstract_Subprogram (Act_Subp) then
12804 Anc_Subp := Alias (Gen_Subp);
12806 -- Test whether we have a corresponding primitive
12807 -- by comparing names, kinds, formal types, and
12808 -- result types.
12810 if Chars (Anc_Subp) = Chars (Act_Subp)
12811 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12812 then
12813 Anc_Formal := First_Formal (Anc_Subp);
12814 Act_Formal := First_Formal (Act_Subp);
12815 while Present (Anc_Formal)
12816 and then Present (Act_Formal)
12817 loop
12818 Anc_F_Type := Etype (Anc_Formal);
12819 Act_F_Type := Etype (Act_Formal);
12821 if Ekind (Anc_F_Type) =
12822 E_Anonymous_Access_Type
12823 then
12824 Anc_F_Type := Designated_Type (Anc_F_Type);
12826 if Ekind (Act_F_Type) =
12827 E_Anonymous_Access_Type
12828 then
12829 Act_F_Type :=
12830 Designated_Type (Act_F_Type);
12831 else
12832 exit;
12833 end if;
12835 elsif
12836 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12837 then
12838 exit;
12839 end if;
12841 Anc_F_Type := Base_Type (Anc_F_Type);
12842 Act_F_Type := Base_Type (Act_F_Type);
12844 -- If the formal is controlling, then the
12845 -- the type of the actual primitive's formal
12846 -- must be derived directly or indirectly
12847 -- from the type of the ancestor primitive's
12848 -- formal.
12850 if Is_Controlling_Formal (Anc_Formal) then
12851 if not Is_Tagged_Ancestor
12852 (Anc_F_Type, Act_F_Type)
12853 then
12854 exit;
12855 end if;
12857 -- Otherwise the types of the formals must
12858 -- be the same.
12860 elsif Anc_F_Type /= Act_F_Type then
12861 exit;
12862 end if;
12864 Next_Entity (Anc_Formal);
12865 Next_Entity (Act_Formal);
12866 end loop;
12868 -- If we traversed through all of the formals
12869 -- then so far the subprograms correspond, so
12870 -- now check that any result types correspond.
12872 if No (Anc_Formal) and then No (Act_Formal) then
12873 Subprograms_Correspond := True;
12875 if Ekind (Act_Subp) = E_Function then
12876 Anc_F_Type := Etype (Anc_Subp);
12877 Act_F_Type := Etype (Act_Subp);
12879 if Ekind (Anc_F_Type) =
12880 E_Anonymous_Access_Type
12881 then
12882 Anc_F_Type :=
12883 Designated_Type (Anc_F_Type);
12885 if Ekind (Act_F_Type) =
12886 E_Anonymous_Access_Type
12887 then
12888 Act_F_Type :=
12889 Designated_Type (Act_F_Type);
12890 else
12891 Subprograms_Correspond := False;
12892 end if;
12894 elsif
12895 Ekind (Act_F_Type)
12896 = E_Anonymous_Access_Type
12897 then
12898 Subprograms_Correspond := False;
12899 end if;
12901 Anc_F_Type := Base_Type (Anc_F_Type);
12902 Act_F_Type := Base_Type (Act_F_Type);
12904 -- Now either the result types must be
12905 -- the same or, if the result type is
12906 -- controlling, the result type of the
12907 -- actual primitive must descend from the
12908 -- result type of the ancestor primitive.
12910 if Subprograms_Correspond
12911 and then Anc_F_Type /= Act_F_Type
12912 and then
12913 Has_Controlling_Result (Anc_Subp)
12914 and then not Is_Tagged_Ancestor
12915 (Anc_F_Type, Act_F_Type)
12916 then
12917 Subprograms_Correspond := False;
12918 end if;
12919 end if;
12921 -- Found a matching subprogram belonging to
12922 -- formal ancestor type, so actual subprogram
12923 -- corresponds and this violates 3.9.3(9).
12925 if Subprograms_Correspond then
12926 Error_Msg_NE
12927 ("abstract subprogram & overrides "
12928 & "nonabstract subprogram of ancestor",
12929 Actual, Act_Subp);
12930 end if;
12931 end if;
12932 end if;
12933 end if;
12935 Next_Elmt (Act_Elmt);
12936 end loop;
12937 end if;
12939 Next_Elmt (Gen_Elmt);
12940 end loop;
12941 end Check_Abstract_Primitives;
12942 end if;
12944 -- Verify that limitedness matches. If parent is a limited
12945 -- interface then the generic formal is not unless declared
12946 -- explicitly so. If not declared limited, the actual cannot be
12947 -- limited (see AI05-0087).
12949 -- Even though this AI is a binding interpretation, we enable the
12950 -- check only in Ada 2012 mode, because this improper construct
12951 -- shows up in user code and in existing B-tests.
12953 if Is_Limited_Type (Act_T)
12954 and then not Is_Limited_Type (A_Gen_T)
12955 and then Ada_Version >= Ada_2012
12956 then
12957 if In_Instance then
12958 null;
12959 else
12960 Error_Msg_NE
12961 ("actual for non-limited & cannot be a limited type",
12962 Actual, Gen_T);
12963 Explain_Limited_Type (Act_T, Actual);
12964 Abandon_Instantiation (Actual);
12965 end if;
12966 end if;
12967 end Validate_Derived_Type_Instance;
12969 ----------------------------------------
12970 -- Validate_Discriminated_Formal_Type --
12971 ----------------------------------------
12973 procedure Validate_Discriminated_Formal_Type is
12974 Formal_Discr : Entity_Id;
12975 Actual_Discr : Entity_Id;
12976 Formal_Subt : Entity_Id;
12978 begin
12979 if Has_Discriminants (A_Gen_T) then
12980 if not Has_Discriminants (Act_T) then
12981 Error_Msg_NE
12982 ("actual for & must have discriminants", Actual, Gen_T);
12983 Abandon_Instantiation (Actual);
12985 elsif Is_Constrained (Act_T) then
12986 Error_Msg_NE
12987 ("actual for & must be unconstrained", Actual, Gen_T);
12988 Abandon_Instantiation (Actual);
12990 else
12991 Formal_Discr := First_Discriminant (A_Gen_T);
12992 Actual_Discr := First_Discriminant (Act_T);
12993 while Formal_Discr /= Empty loop
12994 if Actual_Discr = Empty then
12995 Error_Msg_NE
12996 ("discriminants on actual do not match formal",
12997 Actual, Gen_T);
12998 Abandon_Instantiation (Actual);
12999 end if;
13001 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13003 -- Access discriminants match if designated types do
13005 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13006 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13007 E_Anonymous_Access_Type
13008 and then
13009 Get_Instance_Of
13010 (Designated_Type (Base_Type (Formal_Subt))) =
13011 Designated_Type (Base_Type (Etype (Actual_Discr)))
13012 then
13013 null;
13015 elsif Base_Type (Formal_Subt) /=
13016 Base_Type (Etype (Actual_Discr))
13017 then
13018 Error_Msg_NE
13019 ("types of actual discriminants must match formal",
13020 Actual, Gen_T);
13021 Abandon_Instantiation (Actual);
13023 elsif not Subtypes_Statically_Match
13024 (Formal_Subt, Etype (Actual_Discr))
13025 and then Ada_Version >= Ada_95
13026 then
13027 Error_Msg_NE
13028 ("subtypes of actual discriminants must match formal",
13029 Actual, Gen_T);
13030 Abandon_Instantiation (Actual);
13031 end if;
13033 Next_Discriminant (Formal_Discr);
13034 Next_Discriminant (Actual_Discr);
13035 end loop;
13037 if Actual_Discr /= Empty then
13038 Error_Msg_NE
13039 ("discriminants on actual do not match formal",
13040 Actual, Gen_T);
13041 Abandon_Instantiation (Actual);
13042 end if;
13043 end if;
13044 end if;
13045 end Validate_Discriminated_Formal_Type;
13047 ---------------------------------------
13048 -- Validate_Incomplete_Type_Instance --
13049 ---------------------------------------
13051 procedure Validate_Incomplete_Type_Instance is
13052 begin
13053 if not Is_Tagged_Type (Act_T)
13054 and then Is_Tagged_Type (A_Gen_T)
13055 then
13056 Error_Msg_NE
13057 ("actual for & must be a tagged type", Actual, Gen_T);
13058 end if;
13060 Validate_Discriminated_Formal_Type;
13061 end Validate_Incomplete_Type_Instance;
13063 --------------------------------------
13064 -- Validate_Interface_Type_Instance --
13065 --------------------------------------
13067 procedure Validate_Interface_Type_Instance is
13068 begin
13069 if not Is_Interface (Act_T) then
13070 Error_Msg_NE
13071 ("actual for formal interface type must be an interface",
13072 Actual, Gen_T);
13074 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13075 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13076 or else Is_Protected_Interface (A_Gen_T) /=
13077 Is_Protected_Interface (Act_T)
13078 or else Is_Synchronized_Interface (A_Gen_T) /=
13079 Is_Synchronized_Interface (Act_T)
13080 then
13081 Error_Msg_NE
13082 ("actual for interface& does not match (RM 12.5.5(4))",
13083 Actual, Gen_T);
13084 end if;
13085 end Validate_Interface_Type_Instance;
13087 ------------------------------------
13088 -- Validate_Private_Type_Instance --
13089 ------------------------------------
13091 procedure Validate_Private_Type_Instance is
13092 begin
13093 if Is_Limited_Type (Act_T)
13094 and then not Is_Limited_Type (A_Gen_T)
13095 then
13096 if In_Instance then
13097 null;
13098 else
13099 Error_Msg_NE
13100 ("actual for non-limited & cannot be a limited type", Actual,
13101 Gen_T);
13102 Explain_Limited_Type (Act_T, Actual);
13103 Abandon_Instantiation (Actual);
13104 end if;
13106 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13107 and then not Has_Preelaborable_Initialization (Act_T)
13108 then
13109 Error_Msg_NE
13110 ("actual for & must have preelaborable initialization", Actual,
13111 Gen_T);
13113 elsif not Is_Definite_Subtype (Act_T)
13114 and then Is_Definite_Subtype (A_Gen_T)
13115 and then Ada_Version >= Ada_95
13116 then
13117 Error_Msg_NE
13118 ("actual for & must be a definite subtype", Actual, Gen_T);
13120 elsif not Is_Tagged_Type (Act_T)
13121 and then Is_Tagged_Type (A_Gen_T)
13122 then
13123 Error_Msg_NE
13124 ("actual for & must be a tagged type", Actual, Gen_T);
13125 end if;
13127 Validate_Discriminated_Formal_Type;
13128 Ancestor := Gen_T;
13129 end Validate_Private_Type_Instance;
13131 -- Start of processing for Instantiate_Type
13133 begin
13134 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13135 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13136 return New_List (Error);
13138 elsif not Is_Entity_Name (Actual)
13139 or else not Is_Type (Entity (Actual))
13140 then
13141 Error_Msg_NE
13142 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13143 Abandon_Instantiation (Actual);
13145 else
13146 Act_T := Entity (Actual);
13148 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13149 -- as a generic actual parameter if the corresponding formal type
13150 -- does not have a known_discriminant_part, or is a formal derived
13151 -- type that is an Unchecked_Union type.
13153 if Is_Unchecked_Union (Base_Type (Act_T)) then
13154 if not Has_Discriminants (A_Gen_T)
13155 or else (Is_Derived_Type (A_Gen_T)
13156 and then Is_Unchecked_Union (A_Gen_T))
13157 then
13158 null;
13159 else
13160 Error_Msg_N ("unchecked union cannot be the actual for a "
13161 & "discriminated formal type", Act_T);
13163 end if;
13164 end if;
13166 -- Deal with fixed/floating restrictions
13168 if Is_Floating_Point_Type (Act_T) then
13169 Check_Restriction (No_Floating_Point, Actual);
13170 elsif Is_Fixed_Point_Type (Act_T) then
13171 Check_Restriction (No_Fixed_Point, Actual);
13172 end if;
13174 -- Deal with error of using incomplete type as generic actual.
13175 -- This includes limited views of a type, even if the non-limited
13176 -- view may be available.
13178 if Ekind (Act_T) = E_Incomplete_Type
13179 or else (Is_Class_Wide_Type (Act_T)
13180 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13181 then
13182 -- If the formal is an incomplete type, the actual can be
13183 -- incomplete as well.
13185 if Ekind (A_Gen_T) = E_Incomplete_Type then
13186 null;
13188 elsif Is_Class_Wide_Type (Act_T)
13189 or else No (Full_View (Act_T))
13190 then
13191 Error_Msg_N ("premature use of incomplete type", Actual);
13192 Abandon_Instantiation (Actual);
13193 else
13194 Act_T := Full_View (Act_T);
13195 Set_Entity (Actual, Act_T);
13197 if Has_Private_Component (Act_T) then
13198 Error_Msg_N
13199 ("premature use of type with private component", Actual);
13200 end if;
13201 end if;
13203 -- Deal with error of premature use of private type as generic actual
13205 elsif Is_Private_Type (Act_T)
13206 and then Is_Private_Type (Base_Type (Act_T))
13207 and then not Is_Generic_Type (Act_T)
13208 and then not Is_Derived_Type (Act_T)
13209 and then No (Full_View (Root_Type (Act_T)))
13210 then
13211 -- If the formal is an incomplete type, the actual can be
13212 -- private or incomplete as well.
13214 if Ekind (A_Gen_T) = E_Incomplete_Type then
13215 null;
13216 else
13217 Error_Msg_N ("premature use of private type", Actual);
13218 end if;
13220 elsif Has_Private_Component (Act_T) then
13221 Error_Msg_N
13222 ("premature use of type with private component", Actual);
13223 end if;
13225 Set_Instance_Of (A_Gen_T, Act_T);
13227 -- If the type is generic, the class-wide type may also be used
13229 if Is_Tagged_Type (A_Gen_T)
13230 and then Is_Tagged_Type (Act_T)
13231 and then not Is_Class_Wide_Type (A_Gen_T)
13232 then
13233 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13234 Class_Wide_Type (Act_T));
13235 end if;
13237 if not Is_Abstract_Type (A_Gen_T)
13238 and then Is_Abstract_Type (Act_T)
13239 then
13240 Error_Msg_N
13241 ("actual of non-abstract formal cannot be abstract", Actual);
13242 end if;
13244 -- A generic scalar type is a first subtype for which we generate
13245 -- an anonymous base type. Indicate that the instance of this base
13246 -- is the base type of the actual.
13248 if Is_Scalar_Type (A_Gen_T) then
13249 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13250 end if;
13251 end if;
13253 if Error_Posted (Act_T) then
13254 null;
13255 else
13256 case Nkind (Def) is
13257 when N_Formal_Private_Type_Definition =>
13258 Validate_Private_Type_Instance;
13260 when N_Formal_Incomplete_Type_Definition =>
13261 Validate_Incomplete_Type_Instance;
13263 when N_Formal_Derived_Type_Definition =>
13264 Validate_Derived_Type_Instance;
13266 when N_Formal_Discrete_Type_Definition =>
13267 if not Is_Discrete_Type (Act_T) then
13268 Error_Msg_NE
13269 ("expect discrete type in instantiation of&",
13270 Actual, Gen_T);
13271 Abandon_Instantiation (Actual);
13272 end if;
13274 Diagnose_Predicated_Actual;
13276 when N_Formal_Signed_Integer_Type_Definition =>
13277 if not Is_Signed_Integer_Type (Act_T) then
13278 Error_Msg_NE
13279 ("expect signed integer type in instantiation of&",
13280 Actual, Gen_T);
13281 Abandon_Instantiation (Actual);
13282 end if;
13284 Diagnose_Predicated_Actual;
13286 when N_Formal_Modular_Type_Definition =>
13287 if not Is_Modular_Integer_Type (Act_T) then
13288 Error_Msg_NE
13289 ("expect modular type in instantiation of &",
13290 Actual, Gen_T);
13291 Abandon_Instantiation (Actual);
13292 end if;
13294 Diagnose_Predicated_Actual;
13296 when N_Formal_Floating_Point_Definition =>
13297 if not Is_Floating_Point_Type (Act_T) then
13298 Error_Msg_NE
13299 ("expect float type in instantiation of &", Actual, Gen_T);
13300 Abandon_Instantiation (Actual);
13301 end if;
13303 when N_Formal_Ordinary_Fixed_Point_Definition =>
13304 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13305 Error_Msg_NE
13306 ("expect ordinary fixed point type in instantiation of &",
13307 Actual, Gen_T);
13308 Abandon_Instantiation (Actual);
13309 end if;
13311 when N_Formal_Decimal_Fixed_Point_Definition =>
13312 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13313 Error_Msg_NE
13314 ("expect decimal type in instantiation of &",
13315 Actual, Gen_T);
13316 Abandon_Instantiation (Actual);
13317 end if;
13319 when N_Array_Type_Definition =>
13320 Validate_Array_Type_Instance;
13322 when N_Access_To_Object_Definition =>
13323 Validate_Access_Type_Instance;
13325 when N_Access_Function_Definition
13326 | N_Access_Procedure_Definition
13328 Validate_Access_Subprogram_Instance;
13330 when N_Record_Definition =>
13331 Validate_Interface_Type_Instance;
13333 when N_Derived_Type_Definition =>
13334 Validate_Derived_Interface_Type_Instance;
13336 when others =>
13337 raise Program_Error;
13338 end case;
13339 end if;
13341 Subt := New_Copy (Gen_T);
13343 -- Use adjusted sloc of subtype name as the location for other nodes in
13344 -- the subtype declaration.
13346 Loc := Sloc (Subt);
13348 Decl_Node :=
13349 Make_Subtype_Declaration (Loc,
13350 Defining_Identifier => Subt,
13351 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13353 if Is_Private_Type (Act_T) then
13354 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13356 elsif Is_Access_Type (Act_T)
13357 and then Is_Private_Type (Designated_Type (Act_T))
13358 then
13359 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13360 end if;
13362 -- In Ada 2012 the actual may be a limited view. Indicate that
13363 -- the local subtype must be treated as such.
13365 if From_Limited_With (Act_T) then
13366 Set_Ekind (Subt, E_Incomplete_Subtype);
13367 Set_From_Limited_With (Subt);
13368 end if;
13370 Decl_Nodes := New_List (Decl_Node);
13372 -- Flag actual derived types so their elaboration produces the
13373 -- appropriate renamings for the primitive operations of the ancestor.
13374 -- Flag actual for formal private types as well, to determine whether
13375 -- operations in the private part may override inherited operations.
13376 -- If the formal has an interface list, the ancestor is not the
13377 -- parent, but the analyzed formal that includes the interface
13378 -- operations of all its progenitors.
13380 -- Same treatment for formal private types, so we can check whether the
13381 -- type is tagged limited when validating derivations in the private
13382 -- part. (See AI05-096).
13384 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13385 if Present (Interface_List (Def)) then
13386 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13387 else
13388 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13389 end if;
13391 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13392 N_Formal_Incomplete_Type_Definition)
13393 then
13394 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13395 end if;
13397 -- If the actual is a synchronized type that implements an interface,
13398 -- the primitive operations are attached to the corresponding record,
13399 -- and we have to treat it as an additional generic actual, so that its
13400 -- primitive operations become visible in the instance. The task or
13401 -- protected type itself does not carry primitive operations.
13403 if Is_Concurrent_Type (Act_T)
13404 and then Is_Tagged_Type (Act_T)
13405 and then Present (Corresponding_Record_Type (Act_T))
13406 and then Present (Ancestor)
13407 and then Is_Interface (Ancestor)
13408 then
13409 declare
13410 Corr_Rec : constant Entity_Id :=
13411 Corresponding_Record_Type (Act_T);
13412 New_Corr : Entity_Id;
13413 Corr_Decl : Node_Id;
13415 begin
13416 New_Corr := Make_Temporary (Loc, 'S');
13417 Corr_Decl :=
13418 Make_Subtype_Declaration (Loc,
13419 Defining_Identifier => New_Corr,
13420 Subtype_Indication =>
13421 New_Occurrence_Of (Corr_Rec, Loc));
13422 Append_To (Decl_Nodes, Corr_Decl);
13424 if Ekind (Act_T) = E_Task_Type then
13425 Set_Ekind (Subt, E_Task_Subtype);
13426 else
13427 Set_Ekind (Subt, E_Protected_Subtype);
13428 end if;
13430 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13431 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13432 Set_Generic_Parent_Type (Decl_Node, Empty);
13433 end;
13434 end if;
13436 -- For a floating-point type, capture dimension info if any, because
13437 -- the generated subtype declaration does not come from source and
13438 -- will not process dimensions.
13440 if Is_Floating_Point_Type (Act_T) then
13441 Copy_Dimensions (Act_T, Subt);
13442 end if;
13444 return Decl_Nodes;
13445 end Instantiate_Type;
13447 ---------------------
13448 -- Is_In_Main_Unit --
13449 ---------------------
13451 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13452 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13453 Current_Unit : Node_Id;
13455 begin
13456 if Unum = Main_Unit then
13457 return True;
13459 -- If the current unit is a subunit then it is either the main unit or
13460 -- is being compiled as part of the main unit.
13462 elsif Nkind (N) = N_Compilation_Unit then
13463 return Nkind (Unit (N)) = N_Subunit;
13464 end if;
13466 Current_Unit := Parent (N);
13467 while Present (Current_Unit)
13468 and then Nkind (Current_Unit) /= N_Compilation_Unit
13469 loop
13470 Current_Unit := Parent (Current_Unit);
13471 end loop;
13473 -- The instantiation node is in the main unit, or else the current node
13474 -- (perhaps as the result of nested instantiations) is in the main unit,
13475 -- or in the declaration of the main unit, which in this last case must
13476 -- be a body.
13478 return
13479 Current_Unit = Cunit (Main_Unit)
13480 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13481 or else (Present (Current_Unit)
13482 and then Present (Library_Unit (Current_Unit))
13483 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13484 end Is_In_Main_Unit;
13486 ----------------------------
13487 -- Load_Parent_Of_Generic --
13488 ----------------------------
13490 procedure Load_Parent_Of_Generic
13491 (N : Node_Id;
13492 Spec : Node_Id;
13493 Body_Optional : Boolean := False)
13495 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13496 Saved_Style_Check : constant Boolean := Style_Check;
13497 Saved_Warnings : constant Warning_Record := Save_Warnings;
13498 True_Parent : Node_Id;
13499 Inst_Node : Node_Id;
13500 OK : Boolean;
13501 Previous_Instances : constant Elist_Id := New_Elmt_List;
13503 procedure Collect_Previous_Instances (Decls : List_Id);
13504 -- Collect all instantiations in the given list of declarations, that
13505 -- precede the generic that we need to load. If the bodies of these
13506 -- instantiations are available, we must analyze them, to ensure that
13507 -- the public symbols generated are the same when the unit is compiled
13508 -- to generate code, and when it is compiled in the context of a unit
13509 -- that needs a particular nested instance. This process is applied to
13510 -- both package and subprogram instances.
13512 --------------------------------
13513 -- Collect_Previous_Instances --
13514 --------------------------------
13516 procedure Collect_Previous_Instances (Decls : List_Id) is
13517 Decl : Node_Id;
13519 begin
13520 Decl := First (Decls);
13521 while Present (Decl) loop
13522 if Sloc (Decl) >= Sloc (Inst_Node) then
13523 return;
13525 -- If Decl is an instantiation, then record it as requiring
13526 -- instantiation of the corresponding body, except if it is an
13527 -- abbreviated instantiation generated internally for conformance
13528 -- checking purposes only for the case of a formal package
13529 -- declared without a box (see Instantiate_Formal_Package). Such
13530 -- an instantiation does not generate any code (the actual code
13531 -- comes from actual) and thus does not need to be analyzed here.
13532 -- If the instantiation appears with a generic package body it is
13533 -- not analyzed here either.
13535 elsif Nkind (Decl) = N_Package_Instantiation
13536 and then not Is_Internal (Defining_Entity (Decl))
13537 then
13538 Append_Elmt (Decl, Previous_Instances);
13540 -- For a subprogram instantiation, omit instantiations intrinsic
13541 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13543 elsif Nkind_In (Decl, N_Function_Instantiation,
13544 N_Procedure_Instantiation)
13545 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13546 then
13547 Append_Elmt (Decl, Previous_Instances);
13549 elsif Nkind (Decl) = N_Package_Declaration then
13550 Collect_Previous_Instances
13551 (Visible_Declarations (Specification (Decl)));
13552 Collect_Previous_Instances
13553 (Private_Declarations (Specification (Decl)));
13555 -- Previous non-generic bodies may contain instances as well
13557 elsif Nkind (Decl) = N_Package_Body
13558 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13559 then
13560 Collect_Previous_Instances (Declarations (Decl));
13562 elsif Nkind (Decl) = N_Subprogram_Body
13563 and then not Acts_As_Spec (Decl)
13564 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13565 then
13566 Collect_Previous_Instances (Declarations (Decl));
13567 end if;
13569 Next (Decl);
13570 end loop;
13571 end Collect_Previous_Instances;
13573 -- Start of processing for Load_Parent_Of_Generic
13575 begin
13576 if not In_Same_Source_Unit (N, Spec)
13577 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13578 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13579 and then not Is_In_Main_Unit (Spec))
13580 then
13581 -- Find body of parent of spec, and analyze it. A special case arises
13582 -- when the parent is an instantiation, that is to say when we are
13583 -- currently instantiating a nested generic. In that case, there is
13584 -- no separate file for the body of the enclosing instance. Instead,
13585 -- the enclosing body must be instantiated as if it were a pending
13586 -- instantiation, in order to produce the body for the nested generic
13587 -- we require now. Note that in that case the generic may be defined
13588 -- in a package body, the instance defined in the same package body,
13589 -- and the original enclosing body may not be in the main unit.
13591 Inst_Node := Empty;
13593 True_Parent := Parent (Spec);
13594 while Present (True_Parent)
13595 and then Nkind (True_Parent) /= N_Compilation_Unit
13596 loop
13597 if Nkind (True_Parent) = N_Package_Declaration
13598 and then
13599 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13600 then
13601 -- Parent is a compilation unit that is an instantiation.
13602 -- Instantiation node has been replaced with package decl.
13604 Inst_Node := Original_Node (True_Parent);
13605 exit;
13607 elsif Nkind (True_Parent) = N_Package_Declaration
13608 and then Present (Generic_Parent (Specification (True_Parent)))
13609 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13610 then
13611 -- Parent is an instantiation within another specification.
13612 -- Declaration for instance has been inserted before original
13613 -- instantiation node. A direct link would be preferable?
13615 Inst_Node := Next (True_Parent);
13616 while Present (Inst_Node)
13617 and then Nkind (Inst_Node) /= N_Package_Instantiation
13618 loop
13619 Next (Inst_Node);
13620 end loop;
13622 -- If the instance appears within a generic, and the generic
13623 -- unit is defined within a formal package of the enclosing
13624 -- generic, there is no generic body available, and none
13625 -- needed. A more precise test should be used ???
13627 if No (Inst_Node) then
13628 return;
13629 end if;
13631 exit;
13633 else
13634 True_Parent := Parent (True_Parent);
13635 end if;
13636 end loop;
13638 -- Case where we are currently instantiating a nested generic
13640 if Present (Inst_Node) then
13641 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13643 -- Instantiation node and declaration of instantiated package
13644 -- were exchanged when only the declaration was needed.
13645 -- Restore instantiation node before proceeding with body.
13647 Set_Unit (Parent (True_Parent), Inst_Node);
13648 end if;
13650 -- Now complete instantiation of enclosing body, if it appears in
13651 -- some other unit. If it appears in the current unit, the body
13652 -- will have been instantiated already.
13654 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13656 -- We need to determine the expander mode to instantiate the
13657 -- enclosing body. Because the generic body we need may use
13658 -- global entities declared in the enclosing package (including
13659 -- aggregates) it is in general necessary to compile this body
13660 -- with expansion enabled, except if we are within a generic
13661 -- package, in which case the usual generic rule applies.
13663 declare
13664 Exp_Status : Boolean := True;
13665 Scop : Entity_Id;
13667 begin
13668 -- Loop through scopes looking for generic package
13670 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13671 while Present (Scop)
13672 and then Scop /= Standard_Standard
13673 loop
13674 if Ekind (Scop) = E_Generic_Package then
13675 Exp_Status := False;
13676 exit;
13677 end if;
13679 Scop := Scope (Scop);
13680 end loop;
13682 -- Collect previous instantiations in the unit that contains
13683 -- the desired generic.
13685 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13686 and then not Body_Optional
13687 then
13688 declare
13689 Decl : Elmt_Id;
13690 Info : Pending_Body_Info;
13691 Par : Node_Id;
13693 begin
13694 Par := Parent (Inst_Node);
13695 while Present (Par) loop
13696 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13697 Par := Parent (Par);
13698 end loop;
13700 pragma Assert (Present (Par));
13702 if Nkind (Par) = N_Package_Body then
13703 Collect_Previous_Instances (Declarations (Par));
13705 elsif Nkind (Par) = N_Package_Declaration then
13706 Collect_Previous_Instances
13707 (Visible_Declarations (Specification (Par)));
13708 Collect_Previous_Instances
13709 (Private_Declarations (Specification (Par)));
13711 else
13712 -- Enclosing unit is a subprogram body. In this
13713 -- case all instance bodies are processed in order
13714 -- and there is no need to collect them separately.
13716 null;
13717 end if;
13719 Decl := First_Elmt (Previous_Instances);
13720 while Present (Decl) loop
13721 Info :=
13722 (Inst_Node => Node (Decl),
13723 Act_Decl =>
13724 Instance_Spec (Node (Decl)),
13725 Expander_Status => Exp_Status,
13726 Current_Sem_Unit =>
13727 Get_Code_Unit (Sloc (Node (Decl))),
13728 Scope_Suppress => Scope_Suppress,
13729 Local_Suppress_Stack_Top =>
13730 Local_Suppress_Stack_Top,
13731 Version => Ada_Version,
13732 Version_Pragma => Ada_Version_Pragma,
13733 Warnings => Save_Warnings,
13734 SPARK_Mode => SPARK_Mode,
13735 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13737 -- Package instance
13739 if Nkind (Node (Decl)) = N_Package_Instantiation
13740 then
13741 Instantiate_Package_Body
13742 (Info, Body_Optional => True);
13744 -- Subprogram instance
13746 else
13747 -- The instance_spec is in the wrapper package,
13748 -- usually followed by its local renaming
13749 -- declaration. See Build_Subprogram_Renaming
13750 -- for details. If the instance carries aspects,
13751 -- these result in the corresponding pragmas,
13752 -- inserted after the subprogram declaration.
13753 -- They must be skipped as well when retrieving
13754 -- the desired spec. Some of them may have been
13755 -- rewritten as null statements.
13756 -- A direct link would be more robust ???
13758 declare
13759 Decl : Node_Id :=
13760 (Last (Visible_Declarations
13761 (Specification (Info.Act_Decl))));
13762 begin
13763 while Nkind_In (Decl,
13764 N_Null_Statement,
13765 N_Pragma,
13766 N_Subprogram_Renaming_Declaration)
13767 loop
13768 Decl := Prev (Decl);
13769 end loop;
13771 Info.Act_Decl := Decl;
13772 end;
13774 Instantiate_Subprogram_Body
13775 (Info, Body_Optional => True);
13776 end if;
13778 Next_Elmt (Decl);
13779 end loop;
13780 end;
13781 end if;
13783 Instantiate_Package_Body
13784 (Body_Info =>
13785 ((Inst_Node => Inst_Node,
13786 Act_Decl => True_Parent,
13787 Expander_Status => Exp_Status,
13788 Current_Sem_Unit => Get_Code_Unit
13789 (Sloc (Inst_Node)),
13790 Scope_Suppress => Scope_Suppress,
13791 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13792 Version => Ada_Version,
13793 Version_Pragma => Ada_Version_Pragma,
13794 Warnings => Save_Warnings,
13795 SPARK_Mode => SPARK_Mode,
13796 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13797 Body_Optional => Body_Optional);
13798 end;
13799 end if;
13801 -- Case where we are not instantiating a nested generic
13803 else
13804 Opt.Style_Check := False;
13805 Expander_Mode_Save_And_Set (True);
13806 Load_Needed_Body (Comp_Unit, OK);
13807 Opt.Style_Check := Saved_Style_Check;
13808 Restore_Warnings (Saved_Warnings);
13809 Expander_Mode_Restore;
13811 if not OK
13812 and then Unit_Requires_Body (Defining_Entity (Spec))
13813 and then not Body_Optional
13814 then
13815 declare
13816 Bname : constant Unit_Name_Type :=
13817 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13819 begin
13820 -- In CodePeer mode, the missing body may make the analysis
13821 -- incomplete, but we do not treat it as fatal.
13823 if CodePeer_Mode then
13824 return;
13826 else
13827 Error_Msg_Unit_1 := Bname;
13828 Error_Msg_N ("this instantiation requires$!", N);
13829 Error_Msg_File_1 :=
13830 Get_File_Name (Bname, Subunit => False);
13831 Error_Msg_N ("\but file{ was not found!", N);
13832 raise Unrecoverable_Error;
13833 end if;
13834 end;
13835 end if;
13836 end if;
13837 end if;
13839 -- If loading parent of the generic caused an instantiation circularity,
13840 -- we abandon compilation at this point, because otherwise in some cases
13841 -- we get into trouble with infinite recursions after this point.
13843 if Circularity_Detected then
13844 raise Unrecoverable_Error;
13845 end if;
13846 end Load_Parent_Of_Generic;
13848 ---------------------------------
13849 -- Map_Formal_Package_Entities --
13850 ---------------------------------
13852 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13853 E1 : Entity_Id;
13854 E2 : Entity_Id;
13856 begin
13857 Set_Instance_Of (Form, Act);
13859 -- Traverse formal and actual package to map the corresponding entities.
13860 -- We skip over internal entities that may be generated during semantic
13861 -- analysis, and find the matching entities by name, given that they
13862 -- must appear in the same order.
13864 E1 := First_Entity (Form);
13865 E2 := First_Entity (Act);
13866 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13867 -- Could this test be a single condition??? Seems like it could, and
13868 -- isn't FPE (Form) a constant anyway???
13870 if not Is_Internal (E1)
13871 and then Present (Parent (E1))
13872 and then not Is_Class_Wide_Type (E1)
13873 and then not Is_Internal_Name (Chars (E1))
13874 then
13875 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13876 Next_Entity (E2);
13877 end loop;
13879 if No (E2) then
13880 exit;
13881 else
13882 Set_Instance_Of (E1, E2);
13884 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13885 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13886 end if;
13888 if Is_Constrained (E1) then
13889 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13890 end if;
13892 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13893 Map_Formal_Package_Entities (E1, E2);
13894 end if;
13895 end if;
13896 end if;
13898 Next_Entity (E1);
13899 end loop;
13900 end Map_Formal_Package_Entities;
13902 -----------------------
13903 -- Move_Freeze_Nodes --
13904 -----------------------
13906 procedure Move_Freeze_Nodes
13907 (Out_Of : Entity_Id;
13908 After : Node_Id;
13909 L : List_Id)
13911 Decl : Node_Id;
13912 Next_Decl : Node_Id;
13913 Next_Node : Node_Id := After;
13914 Spec : Node_Id;
13916 function Is_Outer_Type (T : Entity_Id) return Boolean;
13917 -- Check whether entity is declared in a scope external to that of the
13918 -- generic unit.
13920 -------------------
13921 -- Is_Outer_Type --
13922 -------------------
13924 function Is_Outer_Type (T : Entity_Id) return Boolean is
13925 Scop : Entity_Id := Scope (T);
13927 begin
13928 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13929 return True;
13931 else
13932 while Scop /= Standard_Standard loop
13933 if Scop = Out_Of then
13934 return False;
13935 else
13936 Scop := Scope (Scop);
13937 end if;
13938 end loop;
13940 return True;
13941 end if;
13942 end Is_Outer_Type;
13944 -- Start of processing for Move_Freeze_Nodes
13946 begin
13947 if No (L) then
13948 return;
13949 end if;
13951 -- First remove the freeze nodes that may appear before all other
13952 -- declarations.
13954 Decl := First (L);
13955 while Present (Decl)
13956 and then Nkind (Decl) = N_Freeze_Entity
13957 and then Is_Outer_Type (Entity (Decl))
13958 loop
13959 Decl := Remove_Head (L);
13960 Insert_After (Next_Node, Decl);
13961 Set_Analyzed (Decl, False);
13962 Next_Node := Decl;
13963 Decl := First (L);
13964 end loop;
13966 -- Next scan the list of declarations and remove each freeze node that
13967 -- appears ahead of the current node.
13969 while Present (Decl) loop
13970 while Present (Next (Decl))
13971 and then Nkind (Next (Decl)) = N_Freeze_Entity
13972 and then Is_Outer_Type (Entity (Next (Decl)))
13973 loop
13974 Next_Decl := Remove_Next (Decl);
13975 Insert_After (Next_Node, Next_Decl);
13976 Set_Analyzed (Next_Decl, False);
13977 Next_Node := Next_Decl;
13978 end loop;
13980 -- If the declaration is a nested package or concurrent type, then
13981 -- recurse. Nested generic packages will have been processed from the
13982 -- inside out.
13984 case Nkind (Decl) is
13985 when N_Package_Declaration =>
13986 Spec := Specification (Decl);
13988 when N_Task_Type_Declaration =>
13989 Spec := Task_Definition (Decl);
13991 when N_Protected_Type_Declaration =>
13992 Spec := Protected_Definition (Decl);
13994 when others =>
13995 Spec := Empty;
13996 end case;
13998 if Present (Spec) then
13999 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14000 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14001 end if;
14003 Next (Decl);
14004 end loop;
14005 end Move_Freeze_Nodes;
14007 ----------------
14008 -- Next_Assoc --
14009 ----------------
14011 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14012 begin
14013 return Generic_Renamings.Table (E).Next_In_HTable;
14014 end Next_Assoc;
14016 ------------------------
14017 -- Preanalyze_Actuals --
14018 ------------------------
14020 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14021 Assoc : Node_Id;
14022 Act : Node_Id;
14023 Errs : constant Nat := Serious_Errors_Detected;
14025 Cur : Entity_Id := Empty;
14026 -- Current homograph of the instance name
14028 Vis : Boolean := False;
14029 -- Saved visibility status of the current homograph
14031 begin
14032 Assoc := First (Generic_Associations (N));
14034 -- If the instance is a child unit, its name may hide an outer homonym,
14035 -- so make it invisible to perform name resolution on the actuals.
14037 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14038 and then Present
14039 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14040 then
14041 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14043 if Is_Compilation_Unit (Cur) then
14044 Vis := Is_Immediately_Visible (Cur);
14045 Set_Is_Immediately_Visible (Cur, False);
14046 else
14047 Cur := Empty;
14048 end if;
14049 end if;
14051 while Present (Assoc) loop
14052 if Nkind (Assoc) /= N_Others_Choice then
14053 Act := Explicit_Generic_Actual_Parameter (Assoc);
14055 -- Within a nested instantiation, a defaulted actual is an empty
14056 -- association, so nothing to analyze. If the subprogram actual
14057 -- is an attribute, analyze prefix only, because actual is not a
14058 -- complete attribute reference.
14060 -- If actual is an allocator, analyze expression only. The full
14061 -- analysis can generate code, and if instance is a compilation
14062 -- unit we have to wait until the package instance is installed
14063 -- to have a proper place to insert this code.
14065 -- String literals may be operators, but at this point we do not
14066 -- know whether the actual is a formal subprogram or a string.
14068 if No (Act) then
14069 null;
14071 elsif Nkind (Act) = N_Attribute_Reference then
14072 Analyze (Prefix (Act));
14074 elsif Nkind (Act) = N_Explicit_Dereference then
14075 Analyze (Prefix (Act));
14077 elsif Nkind (Act) = N_Allocator then
14078 declare
14079 Expr : constant Node_Id := Expression (Act);
14081 begin
14082 if Nkind (Expr) = N_Subtype_Indication then
14083 Analyze (Subtype_Mark (Expr));
14085 -- Analyze separately each discriminant constraint, when
14086 -- given with a named association.
14088 declare
14089 Constr : Node_Id;
14091 begin
14092 Constr := First (Constraints (Constraint (Expr)));
14093 while Present (Constr) loop
14094 if Nkind (Constr) = N_Discriminant_Association then
14095 Analyze (Expression (Constr));
14096 else
14097 Analyze (Constr);
14098 end if;
14100 Next (Constr);
14101 end loop;
14102 end;
14104 else
14105 Analyze (Expr);
14106 end if;
14107 end;
14109 elsif Nkind (Act) /= N_Operator_Symbol then
14110 Analyze (Act);
14112 -- Within a package instance, mark actuals that are limited
14113 -- views, so their use can be moved to the body of the
14114 -- enclosing unit.
14116 if Is_Entity_Name (Act)
14117 and then Is_Type (Entity (Act))
14118 and then From_Limited_With (Entity (Act))
14119 and then Present (Inst)
14120 then
14121 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14122 end if;
14123 end if;
14125 if Errs /= Serious_Errors_Detected then
14127 -- Do a minimal analysis of the generic, to prevent spurious
14128 -- warnings complaining about the generic being unreferenced,
14129 -- before abandoning the instantiation.
14131 Analyze (Name (N));
14133 if Is_Entity_Name (Name (N))
14134 and then Etype (Name (N)) /= Any_Type
14135 then
14136 Generate_Reference (Entity (Name (N)), Name (N));
14137 Set_Is_Instantiated (Entity (Name (N)));
14138 end if;
14140 if Present (Cur) then
14142 -- For the case of a child instance hiding an outer homonym,
14143 -- provide additional warning which might explain the error.
14145 Set_Is_Immediately_Visible (Cur, Vis);
14146 Error_Msg_NE
14147 ("& hides outer unit with the same name??",
14148 N, Defining_Unit_Name (N));
14149 end if;
14151 Abandon_Instantiation (Act);
14152 end if;
14153 end if;
14155 Next (Assoc);
14156 end loop;
14158 if Present (Cur) then
14159 Set_Is_Immediately_Visible (Cur, Vis);
14160 end if;
14161 end Preanalyze_Actuals;
14163 -------------------------------
14164 -- Provide_Completing_Bodies --
14165 -------------------------------
14167 procedure Provide_Completing_Bodies (N : Node_Id) is
14168 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14169 -- Generate the completing body for subprogram declaration Subp_Decl
14171 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14172 -- Generating completing bodies for all subprograms found in declarative
14173 -- list Decls.
14175 ---------------------------
14176 -- Build_Completing_Body --
14177 ---------------------------
14179 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14180 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14181 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14182 Spec : Node_Id;
14184 begin
14185 -- Nothing to do if the subprogram already has a completing body
14187 if Present (Corresponding_Body (Subp_Decl)) then
14188 return;
14190 -- Mark the function as having a valid return statement even though
14191 -- the body contains a single raise statement.
14193 elsif Ekind (Subp_Id) = E_Function then
14194 Set_Return_Present (Subp_Id);
14195 end if;
14197 -- Clone the specification to obtain new entities and reset the only
14198 -- semantic field.
14200 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14201 Set_Generic_Parent (Spec, Empty);
14203 -- Generate:
14204 -- function Func ... return ... is
14205 -- <or>
14206 -- procedure Proc ... is
14207 -- begin
14208 -- raise Program_Error with "access before elaboration";
14209 -- edn Proc;
14211 Insert_After_And_Analyze (Subp_Decl,
14212 Make_Subprogram_Body (Loc,
14213 Specification => Spec,
14214 Declarations => New_List,
14215 Handled_Statement_Sequence =>
14216 Make_Handled_Sequence_Of_Statements (Loc,
14217 Statements => New_List (
14218 Make_Raise_Program_Error (Loc,
14219 Reason => PE_Access_Before_Elaboration)))));
14220 end Build_Completing_Body;
14222 ----------------------------------
14223 -- Provide_Completing_Bodies_In --
14224 ----------------------------------
14226 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14227 Decl : Node_Id;
14229 begin
14230 if Present (Decls) then
14231 Decl := First (Decls);
14232 while Present (Decl) loop
14233 Provide_Completing_Bodies (Decl);
14234 Next (Decl);
14235 end loop;
14236 end if;
14237 end Provide_Completing_Bodies_In;
14239 -- Local variables
14241 Spec : Node_Id;
14243 -- Start of processing for Provide_Completing_Bodies
14245 begin
14246 if Nkind (N) = N_Package_Declaration then
14247 Spec := Specification (N);
14249 Push_Scope (Defining_Entity (N));
14250 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14251 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14252 Pop_Scope;
14254 elsif Nkind (N) = N_Subprogram_Declaration then
14255 Build_Completing_Body (N);
14256 end if;
14257 end Provide_Completing_Bodies;
14259 -------------------
14260 -- Remove_Parent --
14261 -------------------
14263 procedure Remove_Parent (In_Body : Boolean := False) is
14264 S : Entity_Id := Current_Scope;
14265 -- S is the scope containing the instantiation just completed. The scope
14266 -- stack contains the parent instances of the instantiation, followed by
14267 -- the original S.
14269 Cur_P : Entity_Id;
14270 E : Entity_Id;
14271 P : Entity_Id;
14272 Hidden : Elmt_Id;
14274 begin
14275 -- After child instantiation is complete, remove from scope stack the
14276 -- extra copy of the current scope, and then remove parent instances.
14278 if not In_Body then
14279 Pop_Scope;
14281 while Current_Scope /= S loop
14282 P := Current_Scope;
14283 End_Package_Scope (Current_Scope);
14285 if In_Open_Scopes (P) then
14286 E := First_Entity (P);
14287 while Present (E) loop
14288 Set_Is_Immediately_Visible (E, True);
14289 Next_Entity (E);
14290 end loop;
14292 -- If instantiation is declared in a block, it is the enclosing
14293 -- scope that might be a parent instance. Note that only one
14294 -- block can be involved, because the parent instances have
14295 -- been installed within it.
14297 if Ekind (P) = E_Block then
14298 Cur_P := Scope (P);
14299 else
14300 Cur_P := P;
14301 end if;
14303 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14304 -- We are within an instance of some sibling. Retain
14305 -- visibility of parent, for proper subsequent cleanup, and
14306 -- reinstall private declarations as well.
14308 Set_In_Private_Part (P);
14309 Install_Private_Declarations (P);
14310 end if;
14312 -- If the ultimate parent is a top-level unit recorded in
14313 -- Instance_Parent_Unit, then reset its visibility to what it was
14314 -- before instantiation. (It's not clear what the purpose is of
14315 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14316 -- present before the ultimate parent test was added.???)
14318 elsif not In_Open_Scopes (Scope (P))
14319 or else (P = Instance_Parent_Unit
14320 and then not Parent_Unit_Visible)
14321 then
14322 Set_Is_Immediately_Visible (P, False);
14324 -- If the current scope is itself an instantiation of a generic
14325 -- nested within P, and we are in the private part of body of this
14326 -- instantiation, restore the full views of P, that were removed
14327 -- in End_Package_Scope above. This obscure case can occur when a
14328 -- subunit of a generic contains an instance of a child unit of
14329 -- its generic parent unit.
14331 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14332 declare
14333 Par : constant Entity_Id :=
14334 Generic_Parent (Package_Specification (S));
14335 begin
14336 if Present (Par)
14337 and then P = Scope (Par)
14338 and then (In_Package_Body (S) or else In_Private_Part (S))
14339 then
14340 Set_In_Private_Part (P);
14341 Install_Private_Declarations (P);
14342 end if;
14343 end;
14344 end if;
14345 end loop;
14347 -- Reset visibility of entities in the enclosing scope
14349 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14351 Hidden := First_Elmt (Hidden_Entities);
14352 while Present (Hidden) loop
14353 Set_Is_Immediately_Visible (Node (Hidden), True);
14354 Next_Elmt (Hidden);
14355 end loop;
14357 else
14358 -- Each body is analyzed separately, and there is no context that
14359 -- needs preserving from one body instance to the next, so remove all
14360 -- parent scopes that have been installed.
14362 while Present (S) loop
14363 End_Package_Scope (S);
14364 Set_Is_Immediately_Visible (S, False);
14365 S := Current_Scope;
14366 exit when S = Standard_Standard;
14367 end loop;
14368 end if;
14369 end Remove_Parent;
14371 -----------------
14372 -- Restore_Env --
14373 -----------------
14375 procedure Restore_Env is
14376 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14378 begin
14379 if No (Current_Instantiated_Parent.Act_Id) then
14380 -- Restore environment after subprogram inlining
14382 Restore_Private_Views (Empty);
14383 end if;
14385 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14386 Exchanged_Views := Saved.Exchanged_Views;
14387 Hidden_Entities := Saved.Hidden_Entities;
14388 Current_Sem_Unit := Saved.Current_Sem_Unit;
14389 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14390 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14392 Restore_Opt_Config_Switches (Saved.Switches);
14394 Instance_Envs.Decrement_Last;
14395 end Restore_Env;
14397 ---------------------------
14398 -- Restore_Private_Views --
14399 ---------------------------
14401 procedure Restore_Private_Views
14402 (Pack_Id : Entity_Id;
14403 Is_Package : Boolean := True)
14405 M : Elmt_Id;
14406 E : Entity_Id;
14407 Typ : Entity_Id;
14408 Dep_Elmt : Elmt_Id;
14409 Dep_Typ : Node_Id;
14411 procedure Restore_Nested_Formal (Formal : Entity_Id);
14412 -- Hide the generic formals of formal packages declared with box which
14413 -- were reachable in the current instantiation.
14415 ---------------------------
14416 -- Restore_Nested_Formal --
14417 ---------------------------
14419 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14420 Ent : Entity_Id;
14422 begin
14423 if Present (Renamed_Object (Formal))
14424 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14425 then
14426 return;
14428 elsif Present (Associated_Formal_Package (Formal)) then
14429 Ent := First_Entity (Formal);
14430 while Present (Ent) loop
14431 exit when Ekind (Ent) = E_Package
14432 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14434 Set_Is_Hidden (Ent);
14435 Set_Is_Potentially_Use_Visible (Ent, False);
14437 -- If package, then recurse
14439 if Ekind (Ent) = E_Package then
14440 Restore_Nested_Formal (Ent);
14441 end if;
14443 Next_Entity (Ent);
14444 end loop;
14445 end if;
14446 end Restore_Nested_Formal;
14448 -- Start of processing for Restore_Private_Views
14450 begin
14451 M := First_Elmt (Exchanged_Views);
14452 while Present (M) loop
14453 Typ := Node (M);
14455 -- Subtypes of types whose views have been exchanged, and that are
14456 -- defined within the instance, were not on the Private_Dependents
14457 -- list on entry to the instance, so they have to be exchanged
14458 -- explicitly now, in order to remain consistent with the view of the
14459 -- parent type.
14461 if Ekind_In (Typ, E_Private_Type,
14462 E_Limited_Private_Type,
14463 E_Record_Type_With_Private)
14464 then
14465 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14466 while Present (Dep_Elmt) loop
14467 Dep_Typ := Node (Dep_Elmt);
14469 if Scope (Dep_Typ) = Pack_Id
14470 and then Present (Full_View (Dep_Typ))
14471 then
14472 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14473 Exchange_Declarations (Dep_Typ);
14474 end if;
14476 Next_Elmt (Dep_Elmt);
14477 end loop;
14478 end if;
14480 Exchange_Declarations (Node (M));
14481 Next_Elmt (M);
14482 end loop;
14484 if No (Pack_Id) then
14485 return;
14486 end if;
14488 -- Make the generic formal parameters private, and make the formal types
14489 -- into subtypes of the actuals again.
14491 E := First_Entity (Pack_Id);
14492 while Present (E) loop
14493 Set_Is_Hidden (E, True);
14495 if Is_Type (E)
14496 and then Nkind (Parent (E)) = N_Subtype_Declaration
14497 then
14498 -- If the actual for E is itself a generic actual type from
14499 -- an enclosing instance, E is still a generic actual type
14500 -- outside of the current instance. This matter when resolving
14501 -- an overloaded call that may be ambiguous in the enclosing
14502 -- instance, when two of its actuals coincide.
14504 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14505 and then Is_Generic_Actual_Type
14506 (Entity (Subtype_Indication (Parent (E))))
14507 then
14508 null;
14509 else
14510 Set_Is_Generic_Actual_Type (E, False);
14511 end if;
14513 -- An unusual case of aliasing: the actual may also be directly
14514 -- visible in the generic, and be private there, while it is fully
14515 -- visible in the context of the instance. The internal subtype
14516 -- is private in the instance but has full visibility like its
14517 -- parent in the enclosing scope. This enforces the invariant that
14518 -- the privacy status of all private dependents of a type coincide
14519 -- with that of the parent type. This can only happen when a
14520 -- generic child unit is instantiated within a sibling.
14522 if Is_Private_Type (E)
14523 and then not Is_Private_Type (Etype (E))
14524 then
14525 Exchange_Declarations (E);
14526 end if;
14528 elsif Ekind (E) = E_Package then
14530 -- The end of the renaming list is the renaming of the generic
14531 -- package itself. If the instance is a subprogram, all entities
14532 -- in the corresponding package are renamings. If this entity is
14533 -- a formal package, make its own formals private as well. The
14534 -- actual in this case is itself the renaming of an instantiation.
14535 -- If the entity is not a package renaming, it is the entity
14536 -- created to validate formal package actuals: ignore it.
14538 -- If the actual is itself a formal package for the enclosing
14539 -- generic, or the actual for such a formal package, it remains
14540 -- visible on exit from the instance, and therefore nothing needs
14541 -- to be done either, except to keep it accessible.
14543 if Is_Package and then Renamed_Object (E) = Pack_Id then
14544 exit;
14546 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14547 null;
14549 elsif
14550 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14551 then
14552 Set_Is_Hidden (E, False);
14554 else
14555 declare
14556 Act_P : constant Entity_Id := Renamed_Object (E);
14557 Id : Entity_Id;
14559 begin
14560 Id := First_Entity (Act_P);
14561 while Present (Id)
14562 and then Id /= First_Private_Entity (Act_P)
14563 loop
14564 exit when Ekind (Id) = E_Package
14565 and then Renamed_Object (Id) = Act_P;
14567 Set_Is_Hidden (Id, True);
14568 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14570 if Ekind (Id) = E_Package then
14571 Restore_Nested_Formal (Id);
14572 end if;
14574 Next_Entity (Id);
14575 end loop;
14576 end;
14577 end if;
14578 end if;
14580 Next_Entity (E);
14581 end loop;
14582 end Restore_Private_Views;
14584 --------------
14585 -- Save_Env --
14586 --------------
14588 procedure Save_Env
14589 (Gen_Unit : Entity_Id;
14590 Act_Unit : Entity_Id)
14592 begin
14593 Init_Env;
14594 Set_Instance_Env (Gen_Unit, Act_Unit);
14595 end Save_Env;
14597 ----------------------------
14598 -- Save_Global_References --
14599 ----------------------------
14601 procedure Save_Global_References (Templ : Node_Id) is
14603 -- ??? it is horrible to use global variables in highly recursive code
14605 E : Entity_Id;
14606 -- The entity of the current associated node
14608 Gen_Scope : Entity_Id;
14609 -- The scope of the generic for which references are being saved
14611 N2 : Node_Id;
14612 -- The current associated node
14614 function Is_Global (E : Entity_Id) return Boolean;
14615 -- Check whether entity is defined outside of generic unit. Examine the
14616 -- scope of an entity, and the scope of the scope, etc, until we find
14617 -- either Standard, in which case the entity is global, or the generic
14618 -- unit itself, which indicates that the entity is local. If the entity
14619 -- is the generic unit itself, as in the case of a recursive call, or
14620 -- the enclosing generic unit, if different from the current scope, then
14621 -- it is local as well, because it will be replaced at the point of
14622 -- instantiation. On the other hand, if it is a reference to a child
14623 -- unit of a common ancestor, which appears in an instantiation, it is
14624 -- global because it is used to denote a specific compilation unit at
14625 -- the time the instantiations will be analyzed.
14627 procedure Qualify_Universal_Operands
14628 (Op : Node_Id;
14629 Func_Call : Node_Id);
14630 -- Op denotes a binary or unary operator in generic template Templ. Node
14631 -- Func_Call is the function call alternative of the operator within the
14632 -- the analyzed copy of the template. Change each operand which yields a
14633 -- universal type by wrapping it into a qualified expression
14635 -- Actual_Typ'(Operand)
14637 -- where Actual_Typ is the type of corresponding actual parameter of
14638 -- Operand in Func_Call.
14640 procedure Reset_Entity (N : Node_Id);
14641 -- Save semantic information on global entity so that it is not resolved
14642 -- again at instantiation time.
14644 procedure Save_Entity_Descendants (N : Node_Id);
14645 -- Apply Save_Global_References to the two syntactic descendants of
14646 -- non-terminal nodes that carry an Associated_Node and are processed
14647 -- through Reset_Entity. Once the global entity (if any) has been
14648 -- captured together with its type, only two syntactic descendants need
14649 -- to be traversed to complete the processing of the tree rooted at N.
14650 -- This applies to Selected_Components, Expanded_Names, and to Operator
14651 -- nodes. N can also be a character literal, identifier, or operator
14652 -- symbol node, but the call has no effect in these cases.
14654 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14655 -- Default actuals in nested instances must be handled specially
14656 -- because there is no link to them from the original tree. When an
14657 -- actual subprogram is given by a default, we add an explicit generic
14658 -- association for it in the instantiation node. When we save the
14659 -- global references on the name of the instance, we recover the list
14660 -- of generic associations, and add an explicit one to the original
14661 -- generic tree, through which a global actual can be preserved.
14662 -- Similarly, if a child unit is instantiated within a sibling, in the
14663 -- context of the parent, we must preserve the identifier of the parent
14664 -- so that it can be properly resolved in a subsequent instantiation.
14666 procedure Save_Global_Descendant (D : Union_Id);
14667 -- Apply Save_References recursively to the descendants of node D
14669 procedure Save_References (N : Node_Id);
14670 -- This is the recursive procedure that does the work, once the
14671 -- enclosing generic scope has been established.
14673 ---------------
14674 -- Is_Global --
14675 ---------------
14677 function Is_Global (E : Entity_Id) return Boolean is
14678 Se : Entity_Id;
14680 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14681 -- Determine whether the parent node of a reference to a child unit
14682 -- denotes an instantiation or a formal package, in which case the
14683 -- reference to the child unit is global, even if it appears within
14684 -- the current scope (e.g. when the instance appears within the body
14685 -- of an ancestor).
14687 ----------------------
14688 -- Is_Instance_Node --
14689 ----------------------
14691 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14692 begin
14693 return Nkind (Decl) in N_Generic_Instantiation
14694 or else
14695 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14696 end Is_Instance_Node;
14698 -- Start of processing for Is_Global
14700 begin
14701 if E = Gen_Scope then
14702 return False;
14704 elsif E = Standard_Standard then
14705 return True;
14707 elsif Is_Child_Unit (E)
14708 and then (Is_Instance_Node (Parent (N2))
14709 or else (Nkind (Parent (N2)) = N_Expanded_Name
14710 and then N2 = Selector_Name (Parent (N2))
14711 and then
14712 Is_Instance_Node (Parent (Parent (N2)))))
14713 then
14714 return True;
14716 else
14717 Se := Scope (E);
14718 while Se /= Gen_Scope loop
14719 if Se = Standard_Standard then
14720 return True;
14721 else
14722 Se := Scope (Se);
14723 end if;
14724 end loop;
14726 return False;
14727 end if;
14728 end Is_Global;
14730 --------------------------------
14731 -- Qualify_Universal_Operands --
14732 --------------------------------
14734 procedure Qualify_Universal_Operands
14735 (Op : Node_Id;
14736 Func_Call : Node_Id)
14738 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14739 -- Rewrite operand Opnd as a qualified expression of the form
14741 -- Actual_Typ'(Opnd)
14743 -- where Actual is the corresponding actual parameter of Opnd in
14744 -- function call Func_Call.
14746 function Qualify_Type
14747 (Loc : Source_Ptr;
14748 Typ : Entity_Id) return Node_Id;
14749 -- Qualify type Typ by creating a selected component of the form
14751 -- Scope_Of_Typ.Typ
14753 ---------------------
14754 -- Qualify_Operand --
14755 ---------------------
14757 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14758 Loc : constant Source_Ptr := Sloc (Opnd);
14759 Typ : constant Entity_Id := Etype (Actual);
14760 Mark : Node_Id;
14761 Qual : Node_Id;
14763 begin
14764 -- Qualify the operand when it is of a universal type. Note that
14765 -- the template is unanalyzed and it is not possible to directly
14766 -- query the type. This transformation is not done when the type
14767 -- of the actual is internally generated because the type will be
14768 -- regenerated in the instance.
14770 if Yields_Universal_Type (Opnd)
14771 and then Comes_From_Source (Typ)
14772 and then not Is_Hidden (Typ)
14773 then
14774 -- The type of the actual may be a global reference. Save this
14775 -- information by creating a reference to it.
14777 if Is_Global (Typ) then
14778 Mark := New_Occurrence_Of (Typ, Loc);
14780 -- Otherwise rely on resolution to find the proper type within
14781 -- the instance.
14783 else
14784 Mark := Qualify_Type (Loc, Typ);
14785 end if;
14787 Qual :=
14788 Make_Qualified_Expression (Loc,
14789 Subtype_Mark => Mark,
14790 Expression => Relocate_Node (Opnd));
14792 -- Mark the qualification to distinguish it from other source
14793 -- constructs and signal the instantiation mechanism that this
14794 -- node requires special processing. See Copy_Generic_Node for
14795 -- details.
14797 Set_Is_Qualified_Universal_Literal (Qual);
14799 Rewrite (Opnd, Qual);
14800 end if;
14801 end Qualify_Operand;
14803 ------------------
14804 -- Qualify_Type --
14805 ------------------
14807 function Qualify_Type
14808 (Loc : Source_Ptr;
14809 Typ : Entity_Id) return Node_Id
14811 Scop : constant Entity_Id := Scope (Typ);
14812 Result : Node_Id;
14814 begin
14815 Result := Make_Identifier (Loc, Chars (Typ));
14817 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14818 Result :=
14819 Make_Selected_Component (Loc,
14820 Prefix => Make_Identifier (Loc, Chars (Scop)),
14821 Selector_Name => Result);
14822 end if;
14824 return Result;
14825 end Qualify_Type;
14827 -- Local variables
14829 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14831 -- Start of processing for Qualify_Universal_Operands
14833 begin
14834 if Nkind (Op) in N_Binary_Op then
14835 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14836 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14838 elsif Nkind (Op) in N_Unary_Op then
14839 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14840 end if;
14841 end Qualify_Universal_Operands;
14843 ------------------
14844 -- Reset_Entity --
14845 ------------------
14847 procedure Reset_Entity (N : Node_Id) is
14848 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14849 -- If the type of N2 is global to the generic unit, save the type in
14850 -- the generic node. Just as we perform name capture for explicit
14851 -- references within the generic, we must capture the global types
14852 -- of local entities because they may participate in resolution in
14853 -- the instance.
14855 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14856 -- Find the ultimate ancestor of the current unit. If it is not a
14857 -- generic unit, then the name of the current unit in the prefix of
14858 -- an expanded name must be replaced with its generic homonym to
14859 -- ensure that it will be properly resolved in an instance.
14861 ---------------------
14862 -- Set_Global_Type --
14863 ---------------------
14865 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14866 Typ : constant Entity_Id := Etype (N2);
14868 begin
14869 Set_Etype (N, Typ);
14871 -- If the entity of N is not the associated node, this is a
14872 -- nested generic and it has an associated node as well, whose
14873 -- type is already the full view (see below). Indicate that the
14874 -- original node has a private view.
14876 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14877 Set_Has_Private_View (N);
14878 end if;
14880 -- If not a private type, nothing else to do
14882 if not Is_Private_Type (Typ) then
14883 if Is_Array_Type (Typ)
14884 and then Is_Private_Type (Component_Type (Typ))
14885 then
14886 Set_Has_Private_View (N);
14887 end if;
14889 -- If it is a derivation of a private type in a context where no
14890 -- full view is needed, nothing to do either.
14892 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14893 null;
14895 -- Otherwise mark the type for flipping and use the full view when
14896 -- available.
14898 else
14899 Set_Has_Private_View (N);
14901 if Present (Full_View (Typ)) then
14902 Set_Etype (N2, Full_View (Typ));
14903 end if;
14904 end if;
14906 if Is_Floating_Point_Type (Typ)
14907 and then Has_Dimension_System (Typ)
14908 then
14909 Copy_Dimensions (N2, N);
14910 end if;
14911 end Set_Global_Type;
14913 ------------------
14914 -- Top_Ancestor --
14915 ------------------
14917 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14918 Par : Entity_Id;
14920 begin
14921 Par := E;
14922 while Is_Child_Unit (Par) loop
14923 Par := Scope (Par);
14924 end loop;
14926 return Par;
14927 end Top_Ancestor;
14929 -- Start of processing for Reset_Entity
14931 begin
14932 N2 := Get_Associated_Node (N);
14933 E := Entity (N2);
14935 if Present (E) then
14937 -- If the node is an entry call to an entry in an enclosing task,
14938 -- it is rewritten as a selected component. No global entity to
14939 -- preserve in this case, since the expansion will be redone in
14940 -- the instance.
14942 if not Nkind_In (E, N_Defining_Character_Literal,
14943 N_Defining_Identifier,
14944 N_Defining_Operator_Symbol)
14945 then
14946 Set_Associated_Node (N, Empty);
14947 Set_Etype (N, Empty);
14948 return;
14949 end if;
14951 -- If the entity is an itype created as a subtype of an access
14952 -- type with a null exclusion restore source entity for proper
14953 -- visibility. The itype will be created anew in the instance.
14955 if Is_Itype (E)
14956 and then Ekind (E) = E_Access_Subtype
14957 and then Is_Entity_Name (N)
14958 and then Chars (Etype (E)) = Chars (N)
14959 then
14960 E := Etype (E);
14961 Set_Entity (N2, E);
14962 Set_Etype (N2, E);
14963 end if;
14965 if Is_Global (E) then
14967 -- If the entity is a package renaming that is the prefix of
14968 -- an expanded name, it has been rewritten as the renamed
14969 -- package, which is necessary semantically but complicates
14970 -- ASIS tree traversal, so we recover the original entity to
14971 -- expose the renaming. Take into account that the context may
14972 -- be a nested generic, that the original node may itself have
14973 -- an associated node that had better be an entity, and that
14974 -- the current node is still a selected component.
14976 if Ekind (E) = E_Package
14977 and then Nkind (N) = N_Selected_Component
14978 and then Nkind (Parent (N)) = N_Expanded_Name
14979 and then Present (Original_Node (N2))
14980 and then Is_Entity_Name (Original_Node (N2))
14981 and then Present (Entity (Original_Node (N2)))
14982 then
14983 if Is_Global (Entity (Original_Node (N2))) then
14984 N2 := Original_Node (N2);
14985 Set_Associated_Node (N, N2);
14986 Set_Global_Type (N, N2);
14988 -- Renaming is local, and will be resolved in instance
14990 else
14991 Set_Associated_Node (N, Empty);
14992 Set_Etype (N, Empty);
14993 end if;
14995 else
14996 Set_Global_Type (N, N2);
14997 end if;
14999 elsif Nkind (N) = N_Op_Concat
15000 and then Is_Generic_Type (Etype (N2))
15001 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15002 or else
15003 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15004 and then Is_Intrinsic_Subprogram (E)
15005 then
15006 null;
15008 -- Entity is local. Mark generic node as unresolved. Note that now
15009 -- it does not have an entity.
15011 else
15012 Set_Associated_Node (N, Empty);
15013 Set_Etype (N, Empty);
15014 end if;
15016 if Nkind (Parent (N)) in N_Generic_Instantiation
15017 and then N = Name (Parent (N))
15018 then
15019 Save_Global_Defaults (Parent (N), Parent (N2));
15020 end if;
15022 elsif Nkind (Parent (N)) = N_Selected_Component
15023 and then Nkind (Parent (N2)) = N_Expanded_Name
15024 then
15025 if Is_Global (Entity (Parent (N2))) then
15026 Change_Selected_Component_To_Expanded_Name (Parent (N));
15027 Set_Associated_Node (Parent (N), Parent (N2));
15028 Set_Global_Type (Parent (N), Parent (N2));
15029 Save_Entity_Descendants (N);
15031 -- If this is a reference to the current generic entity, replace
15032 -- by the name of the generic homonym of the current package. This
15033 -- is because in an instantiation Par.P.Q will not resolve to the
15034 -- name of the instance, whose enclosing scope is not necessarily
15035 -- Par. We use the generic homonym rather that the name of the
15036 -- generic itself because it may be hidden by a local declaration.
15038 elsif In_Open_Scopes (Entity (Parent (N2)))
15039 and then not
15040 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15041 then
15042 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15043 Rewrite (Parent (N),
15044 Make_Identifier (Sloc (N),
15045 Chars =>
15046 Chars (Generic_Homonym (Entity (Parent (N2))))));
15047 else
15048 Rewrite (Parent (N),
15049 Make_Identifier (Sloc (N),
15050 Chars => Chars (Selector_Name (Parent (N2)))));
15051 end if;
15052 end if;
15054 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15055 and then Parent (N) = Name (Parent (Parent (N)))
15056 then
15057 Save_Global_Defaults
15058 (Parent (Parent (N)), Parent (Parent (N2)));
15059 end if;
15061 -- A selected component may denote a static constant that has been
15062 -- folded. If the static constant is global to the generic, capture
15063 -- its value. Otherwise the folding will happen in any instantiation.
15065 elsif Nkind (Parent (N)) = N_Selected_Component
15066 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15067 then
15068 if Present (Entity (Original_Node (Parent (N2))))
15069 and then Is_Global (Entity (Original_Node (Parent (N2))))
15070 then
15071 Rewrite (Parent (N), New_Copy (Parent (N2)));
15072 Set_Analyzed (Parent (N), False);
15073 end if;
15075 -- A selected component may be transformed into a parameterless
15076 -- function call. If the called entity is global, rewrite the node
15077 -- appropriately, i.e. as an extended name for the global entity.
15079 elsif Nkind (Parent (N)) = N_Selected_Component
15080 and then Nkind (Parent (N2)) = N_Function_Call
15081 and then N = Selector_Name (Parent (N))
15082 then
15083 if No (Parameter_Associations (Parent (N2))) then
15084 if Is_Global (Entity (Name (Parent (N2)))) then
15085 Change_Selected_Component_To_Expanded_Name (Parent (N));
15086 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15087 Set_Global_Type (Parent (N), Name (Parent (N2)));
15088 Save_Entity_Descendants (N);
15090 else
15091 Set_Is_Prefixed_Call (Parent (N));
15092 Set_Associated_Node (N, Empty);
15093 Set_Etype (N, Empty);
15094 end if;
15096 -- In Ada 2005, X.F may be a call to a primitive operation,
15097 -- rewritten as F (X). This rewriting will be done again in an
15098 -- instance, so keep the original node. Global entities will be
15099 -- captured as for other constructs. Indicate that this must
15100 -- resolve as a call, to prevent accidental overloading in the
15101 -- instance, if both a component and a primitive operation appear
15102 -- as candidates.
15104 else
15105 Set_Is_Prefixed_Call (Parent (N));
15106 end if;
15108 -- Entity is local. Reset in generic unit, so that node is resolved
15109 -- anew at the point of instantiation.
15111 else
15112 Set_Associated_Node (N, Empty);
15113 Set_Etype (N, Empty);
15114 end if;
15115 end Reset_Entity;
15117 -----------------------------
15118 -- Save_Entity_Descendants --
15119 -----------------------------
15121 procedure Save_Entity_Descendants (N : Node_Id) is
15122 begin
15123 case Nkind (N) is
15124 when N_Binary_Op =>
15125 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15126 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15128 when N_Unary_Op =>
15129 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15131 when N_Expanded_Name
15132 | N_Selected_Component
15134 Save_Global_Descendant (Union_Id (Prefix (N)));
15135 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15137 when N_Character_Literal
15138 | N_Identifier
15139 | N_Operator_Symbol
15141 null;
15143 when others =>
15144 raise Program_Error;
15145 end case;
15146 end Save_Entity_Descendants;
15148 --------------------------
15149 -- Save_Global_Defaults --
15150 --------------------------
15152 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15153 Loc : constant Source_Ptr := Sloc (N1);
15154 Assoc2 : constant List_Id := Generic_Associations (N2);
15155 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15156 Assoc1 : List_Id;
15157 Act1 : Node_Id;
15158 Act2 : Node_Id;
15159 Def : Node_Id;
15160 Ndec : Node_Id;
15161 Subp : Entity_Id;
15162 Actual : Entity_Id;
15164 begin
15165 Assoc1 := Generic_Associations (N1);
15167 if Present (Assoc1) then
15168 Act1 := First (Assoc1);
15169 else
15170 Act1 := Empty;
15171 Set_Generic_Associations (N1, New_List);
15172 Assoc1 := Generic_Associations (N1);
15173 end if;
15175 if Present (Assoc2) then
15176 Act2 := First (Assoc2);
15177 else
15178 return;
15179 end if;
15181 while Present (Act1) and then Present (Act2) loop
15182 Next (Act1);
15183 Next (Act2);
15184 end loop;
15186 -- Find the associations added for default subprograms
15188 if Present (Act2) then
15189 while Nkind (Act2) /= N_Generic_Association
15190 or else No (Entity (Selector_Name (Act2)))
15191 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15192 loop
15193 Next (Act2);
15194 end loop;
15196 -- Add a similar association if the default is global. The
15197 -- renaming declaration for the actual has been analyzed, and
15198 -- its alias is the program it renames. Link the actual in the
15199 -- original generic tree with the node in the analyzed tree.
15201 while Present (Act2) loop
15202 Subp := Entity (Selector_Name (Act2));
15203 Def := Explicit_Generic_Actual_Parameter (Act2);
15205 -- Following test is defence against rubbish errors
15207 if No (Alias (Subp)) then
15208 return;
15209 end if;
15211 -- Retrieve the resolved actual from the renaming declaration
15212 -- created for the instantiated formal.
15214 Actual := Entity (Name (Parent (Parent (Subp))));
15215 Set_Entity (Def, Actual);
15216 Set_Etype (Def, Etype (Actual));
15218 if Is_Global (Actual) then
15219 Ndec :=
15220 Make_Generic_Association (Loc,
15221 Selector_Name =>
15222 New_Occurrence_Of (Subp, Loc),
15223 Explicit_Generic_Actual_Parameter =>
15224 New_Occurrence_Of (Actual, Loc));
15226 Set_Associated_Node
15227 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15229 Append (Ndec, Assoc1);
15231 -- If there are other defaults, add a dummy association in case
15232 -- there are other defaulted formals with the same name.
15234 elsif Present (Next (Act2)) then
15235 Ndec :=
15236 Make_Generic_Association (Loc,
15237 Selector_Name =>
15238 New_Occurrence_Of (Subp, Loc),
15239 Explicit_Generic_Actual_Parameter => Empty);
15241 Append (Ndec, Assoc1);
15242 end if;
15244 Next (Act2);
15245 end loop;
15246 end if;
15248 if Nkind (Name (N1)) = N_Identifier
15249 and then Is_Child_Unit (Gen_Id)
15250 and then Is_Global (Gen_Id)
15251 and then Is_Generic_Unit (Scope (Gen_Id))
15252 and then In_Open_Scopes (Scope (Gen_Id))
15253 then
15254 -- This is an instantiation of a child unit within a sibling, so
15255 -- that the generic parent is in scope. An eventual instance must
15256 -- occur within the scope of an instance of the parent. Make name
15257 -- in instance into an expanded name, to preserve the identifier
15258 -- of the parent, so it can be resolved subsequently.
15260 Rewrite (Name (N2),
15261 Make_Expanded_Name (Loc,
15262 Chars => Chars (Gen_Id),
15263 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15264 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15265 Set_Entity (Name (N2), Gen_Id);
15267 Rewrite (Name (N1),
15268 Make_Expanded_Name (Loc,
15269 Chars => Chars (Gen_Id),
15270 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15271 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15273 Set_Associated_Node (Name (N1), Name (N2));
15274 Set_Associated_Node (Prefix (Name (N1)), Empty);
15275 Set_Associated_Node
15276 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15277 Set_Etype (Name (N1), Etype (Gen_Id));
15278 end if;
15279 end Save_Global_Defaults;
15281 ----------------------------
15282 -- Save_Global_Descendant --
15283 ----------------------------
15285 procedure Save_Global_Descendant (D : Union_Id) is
15286 N1 : Node_Id;
15288 begin
15289 if D in Node_Range then
15290 if D = Union_Id (Empty) then
15291 null;
15293 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15294 Save_References (Node_Id (D));
15295 end if;
15297 elsif D in List_Range then
15298 pragma Assert (D /= Union_Id (No_List));
15299 -- Because No_List = Empty, which is in Node_Range above
15301 if Is_Empty_List (List_Id (D)) then
15302 null;
15304 else
15305 N1 := First (List_Id (D));
15306 while Present (N1) loop
15307 Save_References (N1);
15308 Next (N1);
15309 end loop;
15310 end if;
15312 -- Element list or other non-node field, nothing to do
15314 else
15315 null;
15316 end if;
15317 end Save_Global_Descendant;
15319 ---------------------
15320 -- Save_References --
15321 ---------------------
15323 -- This is the recursive procedure that does the work once the enclosing
15324 -- generic scope has been established. We have to treat specially a
15325 -- number of node rewritings that are required by semantic processing
15326 -- and which change the kind of nodes in the generic copy: typically
15327 -- constant-folding, replacing an operator node by a string literal, or
15328 -- a selected component by an expanded name. In each of those cases, the
15329 -- transformation is propagated to the generic unit.
15331 procedure Save_References (N : Node_Id) is
15332 Loc : constant Source_Ptr := Sloc (N);
15334 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15335 -- Determine whether arbitrary node Nod requires delayed capture of
15336 -- global references within its aspect specifications.
15338 procedure Save_References_In_Aggregate (N : Node_Id);
15339 -- Save all global references in [extension] aggregate node N
15341 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15342 -- Save all global references in a character literal or operator
15343 -- symbol denoted by N.
15345 procedure Save_References_In_Descendants (N : Node_Id);
15346 -- Save all global references in all descendants of node N
15348 procedure Save_References_In_Identifier (N : Node_Id);
15349 -- Save all global references in identifier node N
15351 procedure Save_References_In_Operator (N : Node_Id);
15352 -- Save all global references in operator node N
15354 procedure Save_References_In_Pragma (Prag : Node_Id);
15355 -- Save all global references found within the expression of pragma
15356 -- Prag.
15358 ---------------------------
15359 -- Requires_Delayed_Save --
15360 ---------------------------
15362 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15363 begin
15364 -- Generic packages and subprograms require delayed capture of
15365 -- global references within their aspects due to the timing of
15366 -- annotation analysis.
15368 if Nkind_In (Nod, N_Generic_Package_Declaration,
15369 N_Generic_Subprogram_Declaration,
15370 N_Package_Body,
15371 N_Package_Body_Stub,
15372 N_Subprogram_Body,
15373 N_Subprogram_Body_Stub)
15374 then
15375 -- Since the capture of global references is done on the
15376 -- unanalyzed generic template, there is no information around
15377 -- to infer the context. Use the Associated_Entity linkages to
15378 -- peek into the analyzed generic copy and determine what the
15379 -- template corresponds to.
15381 if Nod = Templ then
15382 return
15383 Is_Generic_Declaration_Or_Body
15384 (Unit_Declaration_Node
15385 (Associated_Entity (Defining_Entity (Nod))));
15387 -- Otherwise the generic unit being processed is not the top
15388 -- level template. It is safe to capture of global references
15389 -- within the generic unit because at this point the top level
15390 -- copy is fully analyzed.
15392 else
15393 return False;
15394 end if;
15396 -- Otherwise capture the global references without interference
15398 else
15399 return False;
15400 end if;
15401 end Requires_Delayed_Save;
15403 ----------------------------------
15404 -- Save_References_In_Aggregate --
15405 ----------------------------------
15407 procedure Save_References_In_Aggregate (N : Node_Id) is
15408 Nam : Node_Id;
15409 Qual : Node_Id := Empty;
15410 Typ : Entity_Id := Empty;
15412 use Atree.Unchecked_Access;
15413 -- This code section is part of implementing an untyped tree
15414 -- traversal, so it needs direct access to node fields.
15416 begin
15417 N2 := Get_Associated_Node (N);
15419 if Present (N2) then
15420 Typ := Etype (N2);
15422 -- In an instance within a generic, use the name of the actual
15423 -- and not the original generic parameter. If the actual is
15424 -- global in the current generic it must be preserved for its
15425 -- instantiation.
15427 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15428 and then Present (Generic_Parent_Type (Parent (Typ)))
15429 then
15430 Typ := Base_Type (Typ);
15431 Set_Etype (N2, Typ);
15432 end if;
15433 end if;
15435 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15436 Set_Associated_Node (N, Empty);
15438 -- If the aggregate is an actual in a call, it has been
15439 -- resolved in the current context, to some local type. The
15440 -- enclosing call may have been disambiguated by the aggregate,
15441 -- and this disambiguation might fail at instantiation time
15442 -- because the type to which the aggregate did resolve is not
15443 -- preserved. In order to preserve some of this information,
15444 -- wrap the aggregate in a qualified expression, using the id
15445 -- of its type. For further disambiguation we qualify the type
15446 -- name with its scope (if visible and not hidden by a local
15447 -- homograph) because both id's will have corresponding
15448 -- entities in an instance. This resolves most of the problems
15449 -- with missing type information on aggregates in instances.
15451 if Present (N2)
15452 and then Nkind (N2) = Nkind (N)
15453 and then Nkind (Parent (N2)) in N_Subprogram_Call
15454 and then Present (Typ)
15455 and then Comes_From_Source (Typ)
15456 then
15457 Nam := Make_Identifier (Loc, Chars (Typ));
15459 if Is_Immediately_Visible (Scope (Typ))
15460 and then
15461 (not In_Open_Scopes (Scope (Typ))
15462 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15463 then
15464 Nam :=
15465 Make_Selected_Component (Loc,
15466 Prefix =>
15467 Make_Identifier (Loc, Chars (Scope (Typ))),
15468 Selector_Name => Nam);
15469 end if;
15471 Qual :=
15472 Make_Qualified_Expression (Loc,
15473 Subtype_Mark => Nam,
15474 Expression => Relocate_Node (N));
15475 end if;
15476 end if;
15478 Save_Global_Descendant (Field1 (N));
15479 Save_Global_Descendant (Field2 (N));
15480 Save_Global_Descendant (Field3 (N));
15481 Save_Global_Descendant (Field5 (N));
15483 if Present (Qual) then
15484 Rewrite (N, Qual);
15485 end if;
15486 end Save_References_In_Aggregate;
15488 ----------------------------------------------
15489 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15490 ----------------------------------------------
15492 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15493 begin
15494 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15495 Reset_Entity (N);
15497 elsif Nkind (N) = N_Operator_Symbol
15498 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15499 then
15500 Change_Operator_Symbol_To_String_Literal (N);
15501 end if;
15502 end Save_References_In_Char_Lit_Or_Op_Symbol;
15504 ------------------------------------
15505 -- Save_References_In_Descendants --
15506 ------------------------------------
15508 procedure Save_References_In_Descendants (N : Node_Id) is
15509 use Atree.Unchecked_Access;
15510 -- This code section is part of implementing an untyped tree
15511 -- traversal, so it needs direct access to node fields.
15513 begin
15514 Save_Global_Descendant (Field1 (N));
15515 Save_Global_Descendant (Field2 (N));
15516 Save_Global_Descendant (Field3 (N));
15517 Save_Global_Descendant (Field4 (N));
15518 Save_Global_Descendant (Field5 (N));
15519 end Save_References_In_Descendants;
15521 -----------------------------------
15522 -- Save_References_In_Identifier --
15523 -----------------------------------
15525 procedure Save_References_In_Identifier (N : Node_Id) is
15526 begin
15527 -- The node did not undergo a transformation
15529 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15530 declare
15531 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15532 Orig_N2_Parent : constant Node_Id :=
15533 Original_Node (Parent (Aux_N2));
15534 begin
15535 -- The parent of this identifier is a selected component
15536 -- which denotes a named number that was constant folded.
15537 -- Preserve the original name for ASIS and link the parent
15538 -- with its expanded name. The constant folding will be
15539 -- repeated in the instance.
15541 if Nkind (Parent (N)) = N_Selected_Component
15542 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15543 N_Real_Literal)
15544 and then Is_Entity_Name (Orig_N2_Parent)
15545 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15546 and then Is_Global (Entity (Orig_N2_Parent))
15547 then
15548 N2 := Aux_N2;
15549 Set_Associated_Node
15550 (Parent (N), Original_Node (Parent (N2)));
15552 -- Common case
15554 else
15555 -- If this is a discriminant reference, always save it.
15556 -- It is used in the instance to find the corresponding
15557 -- discriminant positionally rather than by name.
15559 Set_Original_Discriminant
15560 (N, Original_Discriminant (Get_Associated_Node (N)));
15561 end if;
15563 Reset_Entity (N);
15564 end;
15566 -- The analysis of the generic copy transformed the identifier
15567 -- into another construct. Propagate the changes to the template.
15569 else
15570 N2 := Get_Associated_Node (N);
15572 -- The identifier denotes a call to a parameterless function.
15573 -- Mark the node as resolved when the function is external.
15575 if Nkind (N2) = N_Function_Call then
15576 E := Entity (Name (N2));
15578 if Present (E) and then Is_Global (E) then
15579 Set_Etype (N, Etype (N2));
15580 else
15581 Set_Associated_Node (N, Empty);
15582 Set_Etype (N, Empty);
15583 end if;
15585 -- The identifier denotes a named number that was constant
15586 -- folded. Preserve the original name for ASIS and undo the
15587 -- constant folding which will be repeated in the instance.
15589 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15590 and then Is_Entity_Name (Original_Node (N2))
15591 then
15592 Set_Associated_Node (N, Original_Node (N2));
15593 Reset_Entity (N);
15595 -- The identifier resolved to a string literal. Propagate this
15596 -- information to the generic template.
15598 elsif Nkind (N2) = N_String_Literal then
15599 Rewrite (N, New_Copy (N2));
15601 -- The identifier is rewritten as a dereference if it is the
15602 -- prefix of an implicit dereference. Preserve the original
15603 -- tree as the analysis of the instance will expand the node
15604 -- again, but preserve the resolved entity if it is global.
15606 elsif Nkind (N2) = N_Explicit_Dereference then
15607 if Is_Entity_Name (Prefix (N2))
15608 and then Present (Entity (Prefix (N2)))
15609 and then Is_Global (Entity (Prefix (N2)))
15610 then
15611 Set_Associated_Node (N, Prefix (N2));
15613 elsif Nkind (Prefix (N2)) = N_Function_Call
15614 and then Present (Entity (Name (Prefix (N2))))
15615 and then Is_Global (Entity (Name (Prefix (N2))))
15616 then
15617 Rewrite (N,
15618 Make_Explicit_Dereference (Loc,
15619 Prefix =>
15620 Make_Function_Call (Loc,
15621 Name =>
15622 New_Occurrence_Of
15623 (Entity (Name (Prefix (N2))), Loc))));
15625 else
15626 Set_Associated_Node (N, Empty);
15627 Set_Etype (N, Empty);
15628 end if;
15630 -- The subtype mark of a nominally unconstrained object is
15631 -- rewritten as a subtype indication using the bounds of the
15632 -- expression. Recover the original subtype mark.
15634 elsif Nkind (N2) = N_Subtype_Indication
15635 and then Is_Entity_Name (Original_Node (N2))
15636 then
15637 Set_Associated_Node (N, Original_Node (N2));
15638 Reset_Entity (N);
15639 end if;
15640 end if;
15641 end Save_References_In_Identifier;
15643 ---------------------------------
15644 -- Save_References_In_Operator --
15645 ---------------------------------
15647 procedure Save_References_In_Operator (N : Node_Id) is
15648 begin
15649 -- The node did not undergo a transformation
15651 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15652 if Nkind (N) = N_Op_Concat then
15653 Set_Is_Component_Left_Opnd (N,
15654 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15656 Set_Is_Component_Right_Opnd (N,
15657 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15658 end if;
15660 Reset_Entity (N);
15662 -- The analysis of the generic copy transformed the operator into
15663 -- some other construct. Propagate the changes to the template if
15664 -- applicable.
15666 else
15667 N2 := Get_Associated_Node (N);
15669 -- The operator resoved to a function call
15671 if Nkind (N2) = N_Function_Call then
15673 -- Add explicit qualifications in the generic template for
15674 -- all operands of universal type. This aids resolution by
15675 -- preserving the actual type of a literal or an attribute
15676 -- that yields a universal result.
15678 Qualify_Universal_Operands (N, N2);
15680 E := Entity (Name (N2));
15682 if Present (E) and then Is_Global (E) then
15683 Set_Etype (N, Etype (N2));
15684 else
15685 Set_Associated_Node (N, Empty);
15686 Set_Etype (N, Empty);
15687 end if;
15689 -- The operator was folded into a literal
15691 elsif Nkind_In (N2, N_Integer_Literal,
15692 N_Real_Literal,
15693 N_String_Literal)
15694 then
15695 if Present (Original_Node (N2))
15696 and then Nkind (Original_Node (N2)) = Nkind (N)
15697 then
15698 -- Operation was constant-folded. Whenever possible,
15699 -- recover semantic information from unfolded node,
15700 -- for ASIS use.
15702 Set_Associated_Node (N, Original_Node (N2));
15704 if Nkind (N) = N_Op_Concat then
15705 Set_Is_Component_Left_Opnd (N,
15706 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15707 Set_Is_Component_Right_Opnd (N,
15708 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15709 end if;
15711 Reset_Entity (N);
15713 -- Propagate the constant folding back to the template
15715 else
15716 Rewrite (N, New_Copy (N2));
15717 Set_Analyzed (N, False);
15718 end if;
15720 -- The operator was folded into an enumeration literal. Retain
15721 -- the entity to avoid spurious ambiguities if it is overloaded
15722 -- at the point of instantiation or inlining.
15724 elsif Nkind (N2) = N_Identifier
15725 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15726 then
15727 Rewrite (N, New_Copy (N2));
15728 Set_Analyzed (N, False);
15729 end if;
15730 end if;
15732 -- Complete the operands check if node has not been constant
15733 -- folded.
15735 if Nkind (N) in N_Op then
15736 Save_Entity_Descendants (N);
15737 end if;
15738 end Save_References_In_Operator;
15740 -------------------------------
15741 -- Save_References_In_Pragma --
15742 -------------------------------
15744 procedure Save_References_In_Pragma (Prag : Node_Id) is
15745 Context : Node_Id;
15746 Do_Save : Boolean := True;
15748 use Atree.Unchecked_Access;
15749 -- This code section is part of implementing an untyped tree
15750 -- traversal, so it needs direct access to node fields.
15752 begin
15753 -- Do not save global references in pragmas generated from aspects
15754 -- because the pragmas will be regenerated at instantiation time.
15756 if From_Aspect_Specification (Prag) then
15757 Do_Save := False;
15759 -- The capture of global references within contract-related source
15760 -- pragmas associated with generic packages, subprograms or their
15761 -- respective bodies must be delayed due to timing of annotation
15762 -- analysis. Global references are still captured in routine
15763 -- Save_Global_References_In_Contract.
15765 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15766 if Is_Package_Contract_Annotation (Prag) then
15767 Context := Find_Related_Package_Or_Body (Prag);
15768 else
15769 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15770 Context := Find_Related_Declaration_Or_Body (Prag);
15771 end if;
15773 -- The use of Original_Node accounts for the case when the
15774 -- related context is generic template.
15776 if Requires_Delayed_Save (Original_Node (Context)) then
15777 Do_Save := False;
15778 end if;
15779 end if;
15781 -- For all other cases, save all global references within the
15782 -- descendants, but skip the following semantic fields:
15784 -- Field1 - Next_Pragma
15785 -- Field3 - Corresponding_Aspect
15786 -- Field5 - Next_Rep_Item
15788 if Do_Save then
15789 Save_Global_Descendant (Field2 (Prag));
15790 Save_Global_Descendant (Field4 (Prag));
15791 end if;
15792 end Save_References_In_Pragma;
15794 -- Start of processing for Save_References
15796 begin
15797 if N = Empty then
15798 null;
15800 -- Aggregates
15802 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15803 Save_References_In_Aggregate (N);
15805 -- Character literals, operator symbols
15807 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15808 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15810 -- Defining identifiers
15812 elsif Nkind (N) in N_Entity then
15813 null;
15815 -- Identifiers
15817 elsif Nkind (N) = N_Identifier then
15818 Save_References_In_Identifier (N);
15820 -- Operators
15822 elsif Nkind (N) in N_Op then
15823 Save_References_In_Operator (N);
15825 -- Pragmas
15827 elsif Nkind (N) = N_Pragma then
15828 Save_References_In_Pragma (N);
15830 else
15831 Save_References_In_Descendants (N);
15832 end if;
15834 -- Save all global references found within the aspect specifications
15835 -- of the related node.
15837 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15839 -- The capture of global references within aspects associated with
15840 -- generic packages, subprograms or their bodies must be delayed
15841 -- due to timing of annotation analysis. Global references are
15842 -- still captured in routine Save_Global_References_In_Contract.
15844 if Requires_Delayed_Save (N) then
15845 null;
15847 -- Otherwise save all global references within the aspects
15849 else
15850 Save_Global_References_In_Aspects (N);
15851 end if;
15852 end if;
15853 end Save_References;
15855 -- Start of processing for Save_Global_References
15857 begin
15858 Gen_Scope := Current_Scope;
15860 -- If the generic unit is a child unit, references to entities in the
15861 -- parent are treated as local, because they will be resolved anew in
15862 -- the context of the instance of the parent.
15864 while Is_Child_Unit (Gen_Scope)
15865 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15866 loop
15867 Gen_Scope := Scope (Gen_Scope);
15868 end loop;
15870 Save_References (Templ);
15871 end Save_Global_References;
15873 ---------------------------------------
15874 -- Save_Global_References_In_Aspects --
15875 ---------------------------------------
15877 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15878 Asp : Node_Id;
15879 Expr : Node_Id;
15881 begin
15882 Asp := First (Aspect_Specifications (N));
15883 while Present (Asp) loop
15884 Expr := Expression (Asp);
15886 if Present (Expr) then
15887 Save_Global_References (Expr);
15888 end if;
15890 Next (Asp);
15891 end loop;
15892 end Save_Global_References_In_Aspects;
15894 ------------------------------------------
15895 -- Set_Copied_Sloc_For_Inherited_Pragma --
15896 ------------------------------------------
15898 procedure Set_Copied_Sloc_For_Inherited_Pragma
15899 (N : Node_Id;
15900 E : Entity_Id)
15902 begin
15903 Create_Instantiation_Source (N, E,
15904 Inlined_Body => False,
15905 Inherited_Pragma => True,
15906 Factor => S_Adjustment);
15907 end Set_Copied_Sloc_For_Inherited_Pragma;
15909 --------------------------------------
15910 -- Set_Copied_Sloc_For_Inlined_Body --
15911 --------------------------------------
15913 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15914 begin
15915 Create_Instantiation_Source (N, E,
15916 Inlined_Body => True,
15917 Inherited_Pragma => False,
15918 Factor => S_Adjustment);
15919 end Set_Copied_Sloc_For_Inlined_Body;
15921 ---------------------
15922 -- Set_Instance_Of --
15923 ---------------------
15925 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15926 begin
15927 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15928 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15929 Generic_Renamings.Increment_Last;
15930 end Set_Instance_Of;
15932 --------------------
15933 -- Set_Next_Assoc --
15934 --------------------
15936 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15937 begin
15938 Generic_Renamings.Table (E).Next_In_HTable := Next;
15939 end Set_Next_Assoc;
15941 -------------------
15942 -- Start_Generic --
15943 -------------------
15945 procedure Start_Generic is
15946 begin
15947 -- ??? More things could be factored out in this routine.
15948 -- Should probably be done at a later stage.
15950 Generic_Flags.Append (Inside_A_Generic);
15951 Inside_A_Generic := True;
15953 Expander_Mode_Save_And_Set (False);
15954 end Start_Generic;
15956 ----------------------
15957 -- Set_Instance_Env --
15958 ----------------------
15960 -- WARNING: This routine manages SPARK regions
15962 procedure Set_Instance_Env
15963 (Gen_Unit : Entity_Id;
15964 Act_Unit : Entity_Id)
15966 Saved_AE : constant Boolean := Assertions_Enabled;
15967 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15968 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15969 -- Save the SPARK mode-related data because utilizing the configuration
15970 -- values of pragmas and switches will eliminate any previously set
15971 -- SPARK_Mode.
15973 begin
15974 -- Regardless of the current mode, predefined units are analyzed in the
15975 -- most current Ada mode, and earlier version Ada checks do not apply
15976 -- to predefined units. Nothing needs to be done for non-internal units.
15977 -- These are always analyzed in the current mode.
15979 if In_Internal_Unit (Gen_Unit) then
15980 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15982 -- In Ada2012 we may want to enable assertions in an instance of a
15983 -- predefined unit, in which case we need to preserve the current
15984 -- setting for the Assertions_Enabled flag. This will become more
15985 -- critical when pre/postconditions are added to predefined units,
15986 -- as is already the case for some numeric libraries.
15988 if Ada_Version >= Ada_2012 then
15989 Assertions_Enabled := Saved_AE;
15990 end if;
15992 -- Reinstall the SPARK_Mode which was in effect at the point of
15993 -- instantiation.
15995 Install_SPARK_Mode (Saved_SM, Saved_SMP);
15996 end if;
15998 Current_Instantiated_Parent :=
15999 (Gen_Id => Gen_Unit,
16000 Act_Id => Act_Unit,
16001 Next_In_HTable => Assoc_Null);
16002 end Set_Instance_Env;
16004 -----------------
16005 -- Switch_View --
16006 -----------------
16008 procedure Switch_View (T : Entity_Id) is
16009 BT : constant Entity_Id := Base_Type (T);
16010 Priv_Elmt : Elmt_Id := No_Elmt;
16011 Priv_Sub : Entity_Id;
16013 begin
16014 -- T may be private but its base type may have been exchanged through
16015 -- some other occurrence, in which case there is nothing to switch
16016 -- besides T itself. Note that a private dependent subtype of a private
16017 -- type might not have been switched even if the base type has been,
16018 -- because of the last branch of Check_Private_View (see comment there).
16020 if not Is_Private_Type (BT) then
16021 Prepend_Elmt (Full_View (T), Exchanged_Views);
16022 Exchange_Declarations (T);
16023 return;
16024 end if;
16026 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16028 if Present (Full_View (BT)) then
16029 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16030 Exchange_Declarations (BT);
16031 end if;
16033 while Present (Priv_Elmt) loop
16034 Priv_Sub := (Node (Priv_Elmt));
16036 -- We avoid flipping the subtype if the Etype of its full view is
16037 -- private because this would result in a malformed subtype. This
16038 -- occurs when the Etype of the subtype full view is the full view of
16039 -- the base type (and since the base types were just switched, the
16040 -- subtype is pointing to the wrong view). This is currently the case
16041 -- for tagged record types, access types (maybe more?) and needs to
16042 -- be resolved. ???
16044 if Present (Full_View (Priv_Sub))
16045 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16046 then
16047 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16048 Exchange_Declarations (Priv_Sub);
16049 end if;
16051 Next_Elmt (Priv_Elmt);
16052 end loop;
16053 end Switch_View;
16055 -----------------
16056 -- True_Parent --
16057 -----------------
16059 function True_Parent (N : Node_Id) return Node_Id is
16060 begin
16061 if Nkind (Parent (N)) = N_Subunit then
16062 return Parent (Corresponding_Stub (Parent (N)));
16063 else
16064 return Parent (N);
16065 end if;
16066 end True_Parent;
16068 -----------------------------
16069 -- Valid_Default_Attribute --
16070 -----------------------------
16072 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16073 Attr_Id : constant Attribute_Id :=
16074 Get_Attribute_Id (Attribute_Name (Def));
16075 T : constant Entity_Id := Entity (Prefix (Def));
16076 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16077 F : Entity_Id;
16078 Num_F : Nat;
16079 OK : Boolean;
16081 begin
16082 if No (T) or else T = Any_Id then
16083 return;
16084 end if;
16086 Num_F := 0;
16087 F := First_Formal (Nam);
16088 while Present (F) loop
16089 Num_F := Num_F + 1;
16090 Next_Formal (F);
16091 end loop;
16093 case Attr_Id is
16094 when Attribute_Adjacent
16095 | Attribute_Ceiling
16096 | Attribute_Copy_Sign
16097 | Attribute_Floor
16098 | Attribute_Fraction
16099 | Attribute_Machine
16100 | Attribute_Model
16101 | Attribute_Remainder
16102 | Attribute_Rounding
16103 | Attribute_Unbiased_Rounding
16105 OK := Is_Fun
16106 and then Num_F = 1
16107 and then Is_Floating_Point_Type (T);
16109 when Attribute_Image
16110 | Attribute_Pred
16111 | Attribute_Succ
16112 | Attribute_Value
16113 | Attribute_Wide_Image
16114 | Attribute_Wide_Value
16116 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16118 when Attribute_Max
16119 | Attribute_Min
16121 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16123 when Attribute_Input =>
16124 OK := (Is_Fun and then Num_F = 1);
16126 when Attribute_Output
16127 | Attribute_Read
16128 | Attribute_Write
16130 OK := not Is_Fun and then Num_F = 2;
16132 when others =>
16133 OK := False;
16134 end case;
16136 if not OK then
16137 Error_Msg_N
16138 ("attribute reference has wrong profile for subprogram", Def);
16139 end if;
16140 end Valid_Default_Attribute;
16142 end Sem_Ch12;