]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch8.adb
[Ada] Consolidate handling of implicit dereferences into semantic analysis
[thirdparty/gcc.git] / gcc / ada / sem_ch8.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 8 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2020, 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 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Disp; use Exp_Disp;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Ghost; use Ghost;
36 with Impunit; use Impunit;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Namet.Sp; use Namet.Sp;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch4; use Sem_Ch4;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch12; use Sem_Ch12;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Dist; use Sem_Dist;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Util; use Sem_Util;
65 with Sem_Type; use Sem_Type;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.CN; use Sinfo.CN;
69 with Snames; use Snames;
70 with Style;
71 with Table;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74
75 package body Sem_Ch8 is
76
77 ------------------------------------
78 -- Visibility and Name Resolution --
79 ------------------------------------
80
81 -- This package handles name resolution and the collection of possible
82 -- interpretations for overloaded names, prior to overload resolution.
83
84 -- Name resolution is the process that establishes a mapping between source
85 -- identifiers and the entities they denote at each point in the program.
86 -- Each entity is represented by a defining occurrence. Each identifier
87 -- that denotes an entity points to the corresponding defining occurrence.
88 -- This is the entity of the applied occurrence. Each occurrence holds
89 -- an index into the names table, where source identifiers are stored.
90
91 -- Each entry in the names table for an identifier or designator uses the
92 -- Info pointer to hold a link to the currently visible entity that has
93 -- this name (see subprograms Get_Name_Entity_Id and Set_Name_Entity_Id
94 -- in package Sem_Util). The visibility is initialized at the beginning of
95 -- semantic processing to make entities in package Standard immediately
96 -- visible. The visibility table is used in a more subtle way when
97 -- compiling subunits (see below).
98
99 -- Entities that have the same name (i.e. homonyms) are chained. In the
100 -- case of overloaded entities, this chain holds all the possible meanings
101 -- of a given identifier. The process of overload resolution uses type
102 -- information to select from this chain the unique meaning of a given
103 -- identifier.
104
105 -- Entities are also chained in their scope, through the Next_Entity link.
106 -- As a consequence, the name space is organized as a sparse matrix, where
107 -- each row corresponds to a scope, and each column to a source identifier.
108 -- Open scopes, that is to say scopes currently being compiled, have their
109 -- corresponding rows of entities in order, innermost scope first.
110
111 -- The scopes of packages that are mentioned in context clauses appear in
112 -- no particular order, interspersed among open scopes. This is because
113 -- in the course of analyzing the context of a compilation, a package
114 -- declaration is first an open scope, and subsequently an element of the
115 -- context. If subunits or child units are present, a parent unit may
116 -- appear under various guises at various times in the compilation.
117
118 -- When the compilation of the innermost scope is complete, the entities
119 -- defined therein are no longer visible. If the scope is not a package
120 -- declaration, these entities are never visible subsequently, and can be
121 -- removed from visibility chains. If the scope is a package declaration,
122 -- its visible declarations may still be accessible. Therefore the entities
123 -- defined in such a scope are left on the visibility chains, and only
124 -- their visibility (immediately visibility or potential use-visibility)
125 -- is affected.
126
127 -- The ordering of homonyms on their chain does not necessarily follow
128 -- the order of their corresponding scopes on the scope stack. For
129 -- example, if package P and the enclosing scope both contain entities
130 -- named E, then when compiling the package body the chain for E will
131 -- hold the global entity first, and the local one (corresponding to
132 -- the current inner scope) next. As a result, name resolution routines
133 -- do not assume any relative ordering of the homonym chains, either
134 -- for scope nesting or to order of appearance of context clauses.
135
136 -- When compiling a child unit, entities in the parent scope are always
137 -- immediately visible. When compiling the body of a child unit, private
138 -- entities in the parent must also be made immediately visible. There
139 -- are separate routines to make the visible and private declarations
140 -- visible at various times (see package Sem_Ch7).
141
142 -- +--------+ +-----+
143 -- | In use |-------->| EU1 |-------------------------->
144 -- +--------+ +-----+
145 -- | |
146 -- +--------+ +-----+ +-----+
147 -- | Stand. |---------------->| ES1 |--------------->| ES2 |--->
148 -- +--------+ +-----+ +-----+
149 -- | |
150 -- +---------+ | +-----+
151 -- | with'ed |------------------------------>| EW2 |--->
152 -- +---------+ | +-----+
153 -- | |
154 -- +--------+ +-----+ +-----+
155 -- | Scope2 |---------------->| E12 |--------------->| E22 |--->
156 -- +--------+ +-----+ +-----+
157 -- | |
158 -- +--------+ +-----+ +-----+
159 -- | Scope1 |---------------->| E11 |--------------->| E12 |--->
160 -- +--------+ +-----+ +-----+
161 -- ^ | |
162 -- | | |
163 -- | +---------+ | |
164 -- | | with'ed |----------------------------------------->
165 -- | +---------+ | |
166 -- | | |
167 -- Scope stack | |
168 -- (innermost first) | |
169 -- +----------------------------+
170 -- Names table => | Id1 | | | | Id2 |
171 -- +----------------------------+
172
173 -- Name resolution must deal with several syntactic forms: simple names,
174 -- qualified names, indexed names, and various forms of calls.
175
176 -- Each identifier points to an entry in the names table. The resolution
177 -- of a simple name consists in traversing the homonym chain, starting
178 -- from the names table. If an entry is immediately visible, it is the one
179 -- designated by the identifier. If only potentially use-visible entities
180 -- are on the chain, we must verify that they do not hide each other. If
181 -- the entity we find is overloadable, we collect all other overloadable
182 -- entities on the chain as long as they are not hidden.
183 --
184 -- To resolve expanded names, we must find the entity at the intersection
185 -- of the entity chain for the scope (the prefix) and the homonym chain
186 -- for the selector. In general, homonym chains will be much shorter than
187 -- entity chains, so it is preferable to start from the names table as
188 -- well. If the entity found is overloadable, we must collect all other
189 -- interpretations that are defined in the scope denoted by the prefix.
190
191 -- For records, protected types, and tasks, their local entities are
192 -- removed from visibility chains on exit from the corresponding scope.
193 -- From the outside, these entities are always accessed by selected
194 -- notation, and the entity chain for the record type, protected type,
195 -- etc. is traversed sequentially in order to find the designated entity.
196
197 -- The discriminants of a type and the operations of a protected type or
198 -- task are unchained on exit from the first view of the type, (such as
199 -- a private or incomplete type declaration, or a protected type speci-
200 -- fication) and re-chained when compiling the second view.
201
202 -- In the case of operators, we do not make operators on derived types
203 -- explicit. As a result, the notation P."+" may denote either a user-
204 -- defined function with name "+", or else an implicit declaration of the
205 -- operator "+" in package P. The resolution of expanded names always
206 -- tries to resolve an operator name as such an implicitly defined entity,
207 -- in addition to looking for explicit declarations.
208
209 -- All forms of names that denote entities (simple names, expanded names,
210 -- character literals in some cases) have a Entity attribute, which
211 -- identifies the entity denoted by the name.
212
213 ---------------------
214 -- The Scope Stack --
215 ---------------------
216
217 -- The Scope stack keeps track of the scopes currently been compiled.
218 -- Every entity that contains declarations (including records) is placed
219 -- on the scope stack while it is being processed, and removed at the end.
220 -- Whenever a non-package scope is exited, the entities defined therein
221 -- are removed from the visibility table, so that entities in outer scopes
222 -- become visible (see previous description). On entry to Sem, the scope
223 -- stack only contains the package Standard. As usual, subunits complicate
224 -- this picture ever so slightly.
225
226 -- The Rtsfind mechanism can force a call to Semantics while another
227 -- compilation is in progress. The unit retrieved by Rtsfind must be
228 -- compiled in its own context, and has no access to the visibility of
229 -- the unit currently being compiled. The procedures Save_Scope_Stack and
230 -- Restore_Scope_Stack make entities in current open scopes invisible
231 -- before compiling the retrieved unit, and restore the compilation
232 -- environment afterwards.
233
234 ------------------------
235 -- Compiling subunits --
236 ------------------------
237
238 -- Subunits must be compiled in the environment of the corresponding stub,
239 -- that is to say with the same visibility into the parent (and its
240 -- context) that is available at the point of the stub declaration, but
241 -- with the additional visibility provided by the context clause of the
242 -- subunit itself. As a result, compilation of a subunit forces compilation
243 -- of the parent (see description in lib-). At the point of the stub
244 -- declaration, Analyze is called recursively to compile the proper body of
245 -- the subunit, but without reinitializing the names table, nor the scope
246 -- stack (i.e. standard is not pushed on the stack). In this fashion the
247 -- context of the subunit is added to the context of the parent, and the
248 -- subunit is compiled in the correct environment. Note that in the course
249 -- of processing the context of a subunit, Standard will appear twice on
250 -- the scope stack: once for the parent of the subunit, and once for the
251 -- unit in the context clause being compiled. However, the two sets of
252 -- entities are not linked by homonym chains, so that the compilation of
253 -- any context unit happens in a fresh visibility environment.
254
255 -------------------------------
256 -- Processing of USE Clauses --
257 -------------------------------
258
259 -- Every defining occurrence has a flag indicating if it is potentially use
260 -- visible. Resolution of simple names examines this flag. The processing
261 -- of use clauses consists in setting this flag on all visible entities
262 -- defined in the corresponding package. On exit from the scope of the use
263 -- clause, the corresponding flag must be reset. However, a package may
264 -- appear in several nested use clauses (pathological but legal, alas)
265 -- which forces us to use a slightly more involved scheme:
266
267 -- a) The defining occurrence for a package holds a flag -In_Use- to
268 -- indicate that it is currently in the scope of a use clause. If a
269 -- redundant use clause is encountered, then the corresponding occurrence
270 -- of the package name is flagged -Redundant_Use-.
271
272 -- b) On exit from a scope, the use clauses in its declarative part are
273 -- scanned. The visibility flag is reset in all entities declared in
274 -- package named in a use clause, as long as the package is not flagged
275 -- as being in a redundant use clause (in which case the outer use
276 -- clause is still in effect, and the direct visibility of its entities
277 -- must be retained).
278
279 -- Note that entities are not removed from their homonym chains on exit
280 -- from the package specification. A subsequent use clause does not need
281 -- to rechain the visible entities, but only to establish their direct
282 -- visibility.
283
284 -----------------------------------
285 -- Handling private declarations --
286 -----------------------------------
287
288 -- The principle that each entity has a single defining occurrence clashes
289 -- with the presence of two separate definitions for private types: the
290 -- first is the private type declaration, and second is the full type
291 -- declaration. It is important that all references to the type point to
292 -- the same defining occurrence, namely the first one. To enforce the two
293 -- separate views of the entity, the corresponding information is swapped
294 -- between the two declarations. Outside of the package, the defining
295 -- occurrence only contains the private declaration information, while in
296 -- the private part and the body of the package the defining occurrence
297 -- contains the full declaration. To simplify the swap, the defining
298 -- occurrence that currently holds the private declaration points to the
299 -- full declaration. During semantic processing the defining occurrence
300 -- also points to a list of private dependents, that is to say access types
301 -- or composite types whose designated types or component types are
302 -- subtypes or derived types of the private type in question. After the
303 -- full declaration has been seen, the private dependents are updated to
304 -- indicate that they have full definitions.
305
306 ------------------------------------
307 -- Handling of Undefined Messages --
308 ------------------------------------
309
310 -- In normal mode, only the first use of an undefined identifier generates
311 -- a message. The table Urefs is used to record error messages that have
312 -- been issued so that second and subsequent ones do not generate further
313 -- messages. However, the second reference causes text to be added to the
314 -- original undefined message noting "(more references follow)". The
315 -- full error list option (-gnatf) forces messages to be generated for
316 -- every reference and disconnects the use of this table.
317
318 type Uref_Entry is record
319 Node : Node_Id;
320 -- Node for identifier for which original message was posted. The
321 -- Chars field of this identifier is used to detect later references
322 -- to the same identifier.
323
324 Err : Error_Msg_Id;
325 -- Records error message Id of original undefined message. Reset to
326 -- No_Error_Msg after the second occurrence, where it is used to add
327 -- text to the original message as described above.
328
329 Nvis : Boolean;
330 -- Set if the message is not visible rather than undefined
331
332 Loc : Source_Ptr;
333 -- Records location of error message. Used to make sure that we do
334 -- not consider a, b : undefined as two separate instances, which
335 -- would otherwise happen, since the parser converts this sequence
336 -- to a : undefined; b : undefined.
337
338 end record;
339
340 package Urefs is new Table.Table (
341 Table_Component_Type => Uref_Entry,
342 Table_Index_Type => Nat,
343 Table_Low_Bound => 1,
344 Table_Initial => 10,
345 Table_Increment => 100,
346 Table_Name => "Urefs");
347
348 Candidate_Renaming : Entity_Id;
349 -- Holds a candidate interpretation that appears in a subprogram renaming
350 -- declaration and does not match the given specification, but matches at
351 -- least on the first formal. Allows better error message when given
352 -- specification omits defaulted parameters, a common error.
353
354 -----------------------
355 -- Local Subprograms --
356 -----------------------
357
358 procedure Analyze_Generic_Renaming
359 (N : Node_Id;
360 K : Entity_Kind);
361 -- Common processing for all three kinds of generic renaming declarations.
362 -- Enter new name and indicate that it renames the generic unit.
363
364 procedure Analyze_Renamed_Character
365 (N : Node_Id;
366 New_S : Entity_Id;
367 Is_Body : Boolean);
368 -- Renamed entity is given by a character literal, which must belong
369 -- to the return type of the new entity. Is_Body indicates whether the
370 -- declaration is a renaming_as_body. If the original declaration has
371 -- already been frozen (because of an intervening body, e.g.) the body of
372 -- the function must be built now. The same applies to the following
373 -- various renaming procedures.
374
375 procedure Analyze_Renamed_Dereference
376 (N : Node_Id;
377 New_S : Entity_Id;
378 Is_Body : Boolean);
379 -- Renamed entity is given by an explicit dereference. Prefix must be a
380 -- conformant access_to_subprogram type.
381
382 procedure Analyze_Renamed_Entry
383 (N : Node_Id;
384 New_S : Entity_Id;
385 Is_Body : Boolean);
386 -- If the renamed entity in a subprogram renaming is an entry or protected
387 -- subprogram, build a body for the new entity whose only statement is a
388 -- call to the renamed entity.
389
390 procedure Analyze_Renamed_Family_Member
391 (N : Node_Id;
392 New_S : Entity_Id;
393 Is_Body : Boolean);
394 -- Used when the renamed entity is an indexed component. The prefix must
395 -- denote an entry family.
396
397 procedure Analyze_Renamed_Primitive_Operation
398 (N : Node_Id;
399 New_S : Entity_Id;
400 Is_Body : Boolean);
401 -- If the renamed entity in a subprogram renaming is a primitive operation
402 -- or a class-wide operation in prefix form, save the target object,
403 -- which must be added to the list of actuals in any subsequent call.
404 -- The renaming operation is intrinsic because the compiler must in
405 -- fact generate a wrapper for it (6.3.1 (10 1/2)).
406
407 procedure Attribute_Renaming (N : Node_Id);
408 -- Analyze renaming of attribute as subprogram. The renaming declaration N
409 -- is rewritten as a subprogram body that returns the attribute reference
410 -- applied to the formals of the function.
411
412 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id);
413 -- Set Entity, with style check if need be. For a discriminant reference,
414 -- replace by the corresponding discriminal, i.e. the parameter of the
415 -- initialization procedure that corresponds to the discriminant.
416
417 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id);
418 -- A renaming_as_body may occur after the entity of the original decla-
419 -- ration has been frozen. In that case, the body of the new entity must
420 -- be built now, because the usual mechanism of building the renamed
421 -- body at the point of freezing will not work. Subp is the subprogram
422 -- for which N provides the Renaming_As_Body.
423
424 procedure Check_In_Previous_With_Clause
425 (N : Node_Id;
426 Nam : Node_Id);
427 -- N is a use_package clause and Nam the package name, or N is a use_type
428 -- clause and Nam is the prefix of the type name. In either case, verify
429 -- that the package is visible at that point in the context: either it
430 -- appears in a previous with_clause, or because it is a fully qualified
431 -- name and the root ancestor appears in a previous with_clause.
432
433 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id);
434 -- Verify that the entity in a renaming declaration that is a library unit
435 -- is itself a library unit and not a nested unit or subunit. Also check
436 -- that if the renaming is a child unit of a generic parent, then the
437 -- renamed unit must also be a child unit of that parent. Finally, verify
438 -- that a renamed generic unit is not an implicit child declared within
439 -- an instance of the parent.
440
441 procedure Chain_Use_Clause (N : Node_Id);
442 -- Chain use clause onto list of uses clauses headed by First_Use_Clause in
443 -- the proper scope table entry. This is usually the current scope, but it
444 -- will be an inner scope when installing the use clauses of the private
445 -- declarations of a parent unit prior to compiling the private part of a
446 -- child unit. This chain is traversed when installing/removing use clauses
447 -- when compiling a subunit or instantiating a generic body on the fly,
448 -- when it is necessary to save and restore full environments.
449
450 function Enclosing_Instance return Entity_Id;
451 -- In an instance nested within another one, several semantic checks are
452 -- unnecessary because the legality of the nested instance has been checked
453 -- in the enclosing generic unit. This applies in particular to legality
454 -- checks on actuals for formal subprograms of the inner instance, which
455 -- are checked as subprogram renamings, and may be complicated by confusion
456 -- in private/full views. This function returns the instance enclosing the
457 -- current one if there is such, else it returns Empty.
458 --
459 -- If the renaming determines the entity for the default of a formal
460 -- subprogram nested within another instance, choose the innermost
461 -- candidate. This is because if the formal has a box, and we are within
462 -- an enclosing instance where some candidate interpretations are local
463 -- to this enclosing instance, we know that the default was properly
464 -- resolved when analyzing the generic, so we prefer the local
465 -- candidates to those that are external. This is not always the case
466 -- but is a reasonable heuristic on the use of nested generics. The
467 -- proper solution requires a full renaming model.
468
469 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
470 -- Return the appropriate entity for determining which unit has a deeper
471 -- scope: the defining entity for U, unless U is a package instance, in
472 -- which case we retrieve the entity of the instance spec.
473
474 procedure Find_Expanded_Name (N : Node_Id);
475 -- The input is a selected component known to be an expanded name. Verify
476 -- legality of selector given the scope denoted by prefix, and change node
477 -- N into a expanded name with a properly set Entity field.
478
479 function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id;
480 -- Find the most previous use clause (that is, the first one to appear in
481 -- the source) by traversing the previous clause chain that exists in both
482 -- N_Use_Package_Clause nodes and N_Use_Type_Clause nodes.
483 -- ??? a better subprogram name is in order
484
485 function Find_Renamed_Entity
486 (N : Node_Id;
487 Nam : Node_Id;
488 New_S : Entity_Id;
489 Is_Actual : Boolean := False) return Entity_Id;
490 -- Find the renamed entity that corresponds to the given parameter profile
491 -- in a subprogram renaming declaration. The renamed entity may be an
492 -- operator, a subprogram, an entry, or a protected operation. Is_Actual
493 -- indicates that the renaming is the one generated for an actual subpro-
494 -- gram in an instance, for which special visibility checks apply.
495
496 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean;
497 -- Find a type derived from Character or Wide_Character in the prefix of N.
498 -- Used to resolved qualified names whose selector is a character literal.
499
500 function Has_Private_With (E : Entity_Id) return Boolean;
501 -- Ada 2005 (AI-262): Determines if the current compilation unit has a
502 -- private with on E.
503
504 function Has_Components (Typ : Entity_Id) return Boolean;
505 -- Determine if given type has components, i.e. is either a record type or
506 -- type or a type that has discriminants.
507
508 function Has_Implicit_Operator (N : Node_Id) return Boolean;
509 -- N is an expanded name whose selector is an operator name (e.g. P."+").
510 -- declarative part contains an implicit declaration of an operator if it
511 -- has a declaration of a type to which one of the predefined operators
512 -- apply. The existence of this routine is an implementation artifact. A
513 -- more straightforward but more space-consuming choice would be to make
514 -- all inherited operators explicit in the symbol table.
515
516 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id);
517 -- A subprogram defined by a renaming declaration inherits the parameter
518 -- profile of the renamed entity. The subtypes given in the subprogram
519 -- specification are discarded and replaced with those of the renamed
520 -- subprogram, which are then used to recheck the default values.
521
522 function Most_Descendant_Use_Clause
523 (Clause1 : Entity_Id;
524 Clause2 : Entity_Id) return Entity_Id;
525 -- Determine which use clause parameter is the most descendant in terms of
526 -- scope.
527 -- ??? a better subprogram name is in order
528
529 procedure Premature_Usage (N : Node_Id);
530 -- Diagnose usage of an entity before it is visible
531
532 procedure Use_One_Package
533 (N : Node_Id;
534 Pack_Name : Entity_Id := Empty;
535 Force : Boolean := False);
536 -- Make visible entities declared in package P potentially use-visible
537 -- in the current context. Also used in the analysis of subunits, when
538 -- re-installing use clauses of parent units. N is the use_clause that
539 -- names P (and possibly other packages).
540
541 procedure Use_One_Type
542 (Id : Node_Id;
543 Installed : Boolean := False;
544 Force : Boolean := False);
545 -- Id is the subtype mark from a use_type_clause. This procedure makes
546 -- the primitive operators of the type potentially use-visible. The
547 -- boolean flag Installed indicates that the clause is being reinstalled
548 -- after previous analysis, and primitive operations are already chained
549 -- on the Used_Operations list of the clause.
550
551 procedure Write_Info;
552 -- Write debugging information on entities declared in current scope
553
554 --------------------------------
555 -- Analyze_Exception_Renaming --
556 --------------------------------
557
558 -- The language only allows a single identifier, but the tree holds an
559 -- identifier list. The parser has already issued an error message if
560 -- there is more than one element in the list.
561
562 procedure Analyze_Exception_Renaming (N : Node_Id) is
563 Id : constant Entity_Id := Defining_Entity (N);
564 Nam : constant Node_Id := Name (N);
565
566 begin
567 Enter_Name (Id);
568 Analyze (Nam);
569
570 Set_Ekind (Id, E_Exception);
571 Set_Etype (Id, Standard_Exception_Type);
572 Set_Is_Pure (Id, Is_Pure (Current_Scope));
573
574 if Is_Entity_Name (Nam)
575 and then Present (Entity (Nam))
576 and then Ekind (Entity (Nam)) = E_Exception
577 then
578 if Present (Renamed_Object (Entity (Nam))) then
579 Set_Renamed_Object (Id, Renamed_Object (Entity (Nam)));
580 else
581 Set_Renamed_Object (Id, Entity (Nam));
582 end if;
583
584 -- The exception renaming declaration may become Ghost if it renames
585 -- a Ghost entity.
586
587 Mark_Ghost_Renaming (N, Entity (Nam));
588 else
589 Error_Msg_N ("invalid exception name in renaming", Nam);
590 end if;
591
592 -- Implementation-defined aspect specifications can appear in a renaming
593 -- declaration, but not language-defined ones. The call to procedure
594 -- Analyze_Aspect_Specifications will take care of this error check.
595
596 if Has_Aspects (N) then
597 Analyze_Aspect_Specifications (N, Id);
598 end if;
599 end Analyze_Exception_Renaming;
600
601 ---------------------------
602 -- Analyze_Expanded_Name --
603 ---------------------------
604
605 procedure Analyze_Expanded_Name (N : Node_Id) is
606 begin
607 -- If the entity pointer is already set, this is an internal node, or a
608 -- node that is analyzed more than once, after a tree modification. In
609 -- such a case there is no resolution to perform, just set the type. In
610 -- either case, start by analyzing the prefix.
611
612 Analyze (Prefix (N));
613
614 if Present (Entity (N)) then
615 if Is_Type (Entity (N)) then
616 Set_Etype (N, Entity (N));
617 else
618 Set_Etype (N, Etype (Entity (N)));
619 end if;
620
621 else
622 Find_Expanded_Name (N);
623 end if;
624
625 -- In either case, propagate dimension of entity to expanded name
626
627 Analyze_Dimension (N);
628 end Analyze_Expanded_Name;
629
630 ---------------------------------------
631 -- Analyze_Generic_Function_Renaming --
632 ---------------------------------------
633
634 procedure Analyze_Generic_Function_Renaming (N : Node_Id) is
635 begin
636 Analyze_Generic_Renaming (N, E_Generic_Function);
637 end Analyze_Generic_Function_Renaming;
638
639 --------------------------------------
640 -- Analyze_Generic_Package_Renaming --
641 --------------------------------------
642
643 procedure Analyze_Generic_Package_Renaming (N : Node_Id) is
644 begin
645 -- Test for the Text_IO special unit case here, since we may be renaming
646 -- one of the subpackages of Text_IO, then join common routine.
647
648 Check_Text_IO_Special_Unit (Name (N));
649
650 Analyze_Generic_Renaming (N, E_Generic_Package);
651 end Analyze_Generic_Package_Renaming;
652
653 ----------------------------------------
654 -- Analyze_Generic_Procedure_Renaming --
655 ----------------------------------------
656
657 procedure Analyze_Generic_Procedure_Renaming (N : Node_Id) is
658 begin
659 Analyze_Generic_Renaming (N, E_Generic_Procedure);
660 end Analyze_Generic_Procedure_Renaming;
661
662 ------------------------------
663 -- Analyze_Generic_Renaming --
664 ------------------------------
665
666 procedure Analyze_Generic_Renaming
667 (N : Node_Id;
668 K : Entity_Kind)
669 is
670 New_P : constant Entity_Id := Defining_Entity (N);
671 Inst : Boolean := False;
672 Old_P : Entity_Id;
673
674 begin
675 if Name (N) = Error then
676 return;
677 end if;
678
679 Generate_Definition (New_P);
680
681 if Current_Scope /= Standard_Standard then
682 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
683 end if;
684
685 if Nkind (Name (N)) = N_Selected_Component then
686 Check_Generic_Child_Unit (Name (N), Inst);
687 else
688 Analyze (Name (N));
689 end if;
690
691 if not Is_Entity_Name (Name (N)) then
692 Error_Msg_N ("expect entity name in renaming declaration", Name (N));
693 Old_P := Any_Id;
694 else
695 Old_P := Entity (Name (N));
696 end if;
697
698 Enter_Name (New_P);
699 Set_Ekind (New_P, K);
700
701 if Etype (Old_P) = Any_Type then
702 null;
703
704 elsif Ekind (Old_P) /= K then
705 Error_Msg_N ("invalid generic unit name", Name (N));
706
707 else
708 if Present (Renamed_Object (Old_P)) then
709 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
710 else
711 Set_Renamed_Object (New_P, Old_P);
712 end if;
713
714 -- The generic renaming declaration may become Ghost if it renames a
715 -- Ghost entity.
716
717 Mark_Ghost_Renaming (N, Old_P);
718
719 Set_Is_Pure (New_P, Is_Pure (Old_P));
720 Set_Is_Preelaborated (New_P, Is_Preelaborated (Old_P));
721
722 Set_Etype (New_P, Etype (Old_P));
723 Set_Has_Completion (New_P);
724
725 if In_Open_Scopes (Old_P) then
726 Error_Msg_N ("within its scope, generic denotes its instance", N);
727 end if;
728
729 -- For subprograms, propagate the Intrinsic flag, to allow, e.g.
730 -- renamings and subsequent instantiations of Unchecked_Conversion.
731
732 if Ekind_In (Old_P, E_Generic_Function, E_Generic_Procedure) then
733 Set_Is_Intrinsic_Subprogram
734 (New_P, Is_Intrinsic_Subprogram (Old_P));
735 end if;
736
737 Check_Library_Unit_Renaming (N, Old_P);
738 end if;
739
740 -- Implementation-defined aspect specifications can appear in a renaming
741 -- declaration, but not language-defined ones. The call to procedure
742 -- Analyze_Aspect_Specifications will take care of this error check.
743
744 if Has_Aspects (N) then
745 Analyze_Aspect_Specifications (N, New_P);
746 end if;
747 end Analyze_Generic_Renaming;
748
749 -----------------------------
750 -- Analyze_Object_Renaming --
751 -----------------------------
752
753 procedure Analyze_Object_Renaming (N : Node_Id) is
754 Id : constant Entity_Id := Defining_Identifier (N);
755 Loc : constant Source_Ptr := Sloc (N);
756 Nam : constant Node_Id := Name (N);
757 Is_Object_Ref : Boolean := False;
758 Dec : Node_Id;
759 T : Entity_Id;
760 T2 : Entity_Id;
761
762 procedure Check_Constrained_Object;
763 -- If the nominal type is unconstrained but the renamed object is
764 -- constrained, as can happen with renaming an explicit dereference or
765 -- a function return, build a constrained subtype from the object. If
766 -- the renaming is for a formal in an accept statement, the analysis
767 -- has already established its actual subtype. This is only relevant
768 -- if the renamed object is an explicit dereference.
769
770 function Get_Object_Name (Nod : Node_Id) return Node_Id;
771 -- Obtain the name of the object from node Nod which is being renamed by
772 -- the object renaming declaration N.
773
774 ------------------------------
775 -- Check_Constrained_Object --
776 ------------------------------
777
778 procedure Check_Constrained_Object is
779 Typ : constant Entity_Id := Etype (Nam);
780 Subt : Entity_Id;
781
782 begin
783 if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference)
784 and then Is_Composite_Type (Typ)
785 and then not Is_Constrained (Typ)
786 and then not Has_Unknown_Discriminants (Typ)
787 and then Expander_Active
788 then
789 -- If Actual_Subtype is already set, nothing to do
790
791 if Ekind_In (Id, E_Variable, E_Constant)
792 and then Present (Actual_Subtype (Id))
793 then
794 null;
795
796 -- A renaming of an unchecked union has no actual subtype
797
798 elsif Is_Unchecked_Union (Typ) then
799 null;
800
801 -- If a record is limited its size is invariant. This is the case
802 -- in particular with record types with an access discriminant
803 -- that are used in iterators. This is an optimization, but it
804 -- also prevents typing anomalies when the prefix is further
805 -- expanded.
806 -- Note that we cannot just use the Is_Limited_Record flag because
807 -- it does not apply to records with limited components, for which
808 -- this syntactic flag is not set, but whose size is also fixed.
809
810 elsif Is_Limited_Type (Typ) then
811 null;
812
813 else
814 Subt := Make_Temporary (Loc, 'T');
815 Remove_Side_Effects (Nam);
816 Insert_Action (N,
817 Make_Subtype_Declaration (Loc,
818 Defining_Identifier => Subt,
819 Subtype_Indication =>
820 Make_Subtype_From_Expr (Nam, Typ)));
821 Rewrite (Subtype_Mark (N), New_Occurrence_Of (Subt, Loc));
822 Set_Etype (Nam, Subt);
823
824 -- Freeze subtype at once, to prevent order of elaboration
825 -- issues in the backend. The renamed object exists, so its
826 -- type is already frozen in any case.
827
828 Freeze_Before (N, Subt);
829 end if;
830 end if;
831 end Check_Constrained_Object;
832
833 ---------------------
834 -- Get_Object_Name --
835 ---------------------
836
837 function Get_Object_Name (Nod : Node_Id) return Node_Id is
838 Obj_Nam : Node_Id;
839
840 begin
841 Obj_Nam := Nod;
842 while Present (Obj_Nam) loop
843 case Nkind (Obj_Nam) is
844 when N_Attribute_Reference
845 | N_Explicit_Dereference
846 | N_Indexed_Component
847 | N_Slice
848 =>
849 Obj_Nam := Prefix (Obj_Nam);
850
851 when N_Selected_Component =>
852 Obj_Nam := Selector_Name (Obj_Nam);
853
854 when N_Qualified_Expression | N_Type_Conversion =>
855 Obj_Nam := Expression (Obj_Nam);
856
857 when others =>
858 exit;
859 end case;
860 end loop;
861
862 return Obj_Nam;
863 end Get_Object_Name;
864
865 -- Start of processing for Analyze_Object_Renaming
866
867 begin
868 if Nam = Error then
869 return;
870 end if;
871
872 Set_Is_Pure (Id, Is_Pure (Current_Scope));
873 Enter_Name (Id);
874
875 -- The renaming of a component that depends on a discriminant requires
876 -- an actual subtype, because in subsequent use of the object Gigi will
877 -- be unable to locate the actual bounds. This explicit step is required
878 -- when the renaming is generated in removing side effects of an
879 -- already-analyzed expression.
880
881 if Nkind (Nam) = N_Selected_Component and then Analyzed (Nam) then
882
883 -- The object renaming declaration may become Ghost if it renames a
884 -- Ghost entity.
885
886 if Is_Entity_Name (Nam) then
887 Mark_Ghost_Renaming (N, Entity (Nam));
888 end if;
889
890 T := Etype (Nam);
891 Dec := Build_Actual_Subtype_Of_Component (Etype (Nam), Nam);
892
893 if Present (Dec) then
894 Insert_Action (N, Dec);
895 T := Defining_Identifier (Dec);
896 Set_Etype (Nam, T);
897 end if;
898 elsif Present (Subtype_Mark (N))
899 or else not Present (Access_Definition (N))
900 then
901 if Present (Subtype_Mark (N)) then
902 Find_Type (Subtype_Mark (N));
903 T := Entity (Subtype_Mark (N));
904 Analyze (Nam);
905
906 -- AI12-0275: Case of object renaming without a subtype_mark
907
908 else
909 Analyze (Nam);
910
911 -- Normal case of no overloading in object name
912
913 if not Is_Overloaded (Nam) then
914
915 -- Catch error cases (such as attempting to rename a procedure
916 -- or package) using the shorthand form.
917
918 if No (Etype (Nam))
919 or else Etype (Nam) = Standard_Void_Type
920 then
921 Error_Msg_N ("object name expected in renaming", Nam);
922
923 Set_Ekind (Id, E_Variable);
924 Set_Etype (Id, Any_Type);
925
926 return;
927
928 else
929 T := Etype (Nam);
930 end if;
931
932 -- Case of overloaded name, which will be illegal if there's more
933 -- than one acceptable interpretation (such as overloaded function
934 -- calls).
935
936 else
937 declare
938 I : Interp_Index;
939 I1 : Interp_Index;
940 It : Interp;
941 It1 : Interp;
942 Nam1 : Entity_Id;
943
944 begin
945 -- More than one candidate interpretation is available
946
947 -- Remove procedure calls, which syntactically cannot appear
948 -- in this context, but which cannot be removed by type
949 -- checking, because the context does not impose a type.
950
951 Get_First_Interp (Nam, I, It);
952 while Present (It.Typ) loop
953 if It.Typ = Standard_Void_Type then
954 Remove_Interp (I);
955 end if;
956
957 Get_Next_Interp (I, It);
958 end loop;
959
960 Get_First_Interp (Nam, I, It);
961 I1 := I;
962 It1 := It;
963
964 -- If there's no type present, we have an error case (such
965 -- as overloaded procedures named in the object renaming).
966
967 if No (It.Typ) then
968 Error_Msg_N ("object name expected in renaming", Nam);
969
970 Set_Ekind (Id, E_Variable);
971 Set_Etype (Id, Any_Type);
972
973 return;
974 end if;
975
976 Get_Next_Interp (I, It);
977
978 if Present (It.Typ) then
979 Nam1 := It1.Nam;
980 It1 := Disambiguate (Nam, I1, I, Any_Type);
981
982 if It1 = No_Interp then
983 Error_Msg_N ("ambiguous name in object renaming", Nam);
984
985 Error_Msg_Sloc := Sloc (It.Nam);
986 Error_Msg_N ("\\possible interpretation#!", Nam);
987
988 Error_Msg_Sloc := Sloc (Nam1);
989 Error_Msg_N ("\\possible interpretation#!", Nam);
990
991 return;
992 end if;
993 end if;
994
995 Set_Etype (Nam, It1.Typ);
996 T := It1.Typ;
997 end;
998 end if;
999 end if;
1000
1001 -- The object renaming declaration may become Ghost if it renames a
1002 -- Ghost entity.
1003
1004 if Is_Entity_Name (Nam) then
1005 Mark_Ghost_Renaming (N, Entity (Nam));
1006 end if;
1007
1008 Resolve (Nam, T);
1009
1010 -- If the renamed object is a function call of a limited type,
1011 -- the expansion of the renaming is complicated by the presence
1012 -- of various temporaries and subtypes that capture constraints
1013 -- of the renamed object. Rewrite node as an object declaration,
1014 -- whose expansion is simpler. Given that the object is limited
1015 -- there is no copy involved and no performance hit.
1016
1017 if Nkind (Nam) = N_Function_Call
1018 and then Is_Limited_View (Etype (Nam))
1019 and then not Is_Constrained (Etype (Nam))
1020 and then Comes_From_Source (N)
1021 then
1022 Set_Etype (Id, T);
1023 Set_Ekind (Id, E_Constant);
1024 Rewrite (N,
1025 Make_Object_Declaration (Loc,
1026 Defining_Identifier => Id,
1027 Constant_Present => True,
1028 Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
1029 Expression => Relocate_Node (Nam)));
1030 return;
1031 end if;
1032
1033 -- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
1034 -- when renaming declaration has a named access type. The Ada 2012
1035 -- coverage rules allow an anonymous access type in the context of
1036 -- an expected named general access type, but the renaming rules
1037 -- require the types to be the same. (An exception is when the type
1038 -- of the renaming is also an anonymous access type, which can only
1039 -- happen due to a renaming created by the expander.)
1040
1041 if Nkind (Nam) = N_Type_Conversion
1042 and then not Comes_From_Source (Nam)
1043 and then Ekind (Etype (Expression (Nam))) in Anonymous_Access_Kind
1044 and then Ekind (T) not in Anonymous_Access_Kind
1045 then
1046 Wrong_Type (Expression (Nam), T); -- Should we give better error???
1047 end if;
1048
1049 -- Check that a class-wide object is not being renamed as an object
1050 -- of a specific type. The test for access types is needed to exclude
1051 -- cases where the renamed object is a dynamically tagged access
1052 -- result, such as occurs in certain expansions.
1053
1054 if Is_Tagged_Type (T) then
1055 Check_Dynamically_Tagged_Expression
1056 (Expr => Nam,
1057 Typ => T,
1058 Related_Nod => N);
1059 end if;
1060
1061 -- Ada 2005 (AI-230/AI-254): Access renaming
1062
1063 else pragma Assert (Present (Access_Definition (N)));
1064 T :=
1065 Access_Definition
1066 (Related_Nod => N,
1067 N => Access_Definition (N));
1068
1069 Analyze (Nam);
1070
1071 -- The object renaming declaration may become Ghost if it renames a
1072 -- Ghost entity.
1073
1074 if Is_Entity_Name (Nam) then
1075 Mark_Ghost_Renaming (N, Entity (Nam));
1076 end if;
1077
1078 -- Ada 2005 AI05-105: if the declaration has an anonymous access
1079 -- type, the renamed object must also have an anonymous type, and
1080 -- this is a name resolution rule. This was implicit in the last part
1081 -- of the first sentence in 8.5.1(3/2), and is made explicit by this
1082 -- recent AI.
1083
1084 if not Is_Overloaded (Nam) then
1085 if Ekind (Etype (Nam)) /= Ekind (T) then
1086 Error_Msg_N
1087 ("expect anonymous access type in object renaming", N);
1088 end if;
1089
1090 else
1091 declare
1092 I : Interp_Index;
1093 It : Interp;
1094 Typ : Entity_Id := Empty;
1095 Seen : Boolean := False;
1096
1097 begin
1098 Get_First_Interp (Nam, I, It);
1099 while Present (It.Typ) loop
1100
1101 -- Renaming is ambiguous if more than one candidate
1102 -- interpretation is type-conformant with the context.
1103
1104 if Ekind (It.Typ) = Ekind (T) then
1105 if Ekind (T) = E_Anonymous_Access_Subprogram_Type
1106 and then
1107 Type_Conformant
1108 (Designated_Type (T), Designated_Type (It.Typ))
1109 then
1110 if not Seen then
1111 Seen := True;
1112 else
1113 Error_Msg_N
1114 ("ambiguous expression in renaming", Nam);
1115 end if;
1116
1117 elsif Ekind (T) = E_Anonymous_Access_Type
1118 and then
1119 Covers (Designated_Type (T), Designated_Type (It.Typ))
1120 then
1121 if not Seen then
1122 Seen := True;
1123 else
1124 Error_Msg_N
1125 ("ambiguous expression in renaming", Nam);
1126 end if;
1127 end if;
1128
1129 if Covers (T, It.Typ) then
1130 Typ := It.Typ;
1131 Set_Etype (Nam, Typ);
1132 Set_Is_Overloaded (Nam, False);
1133 end if;
1134 end if;
1135
1136 Get_Next_Interp (I, It);
1137 end loop;
1138 end;
1139 end if;
1140
1141 Resolve (Nam, T);
1142
1143 -- Do not perform the legality checks below when the resolution of
1144 -- the renaming name failed because the associated type is Any_Type.
1145
1146 if Etype (Nam) = Any_Type then
1147 null;
1148
1149 -- Ada 2005 (AI-231): In the case where the type is defined by an
1150 -- access_definition, the renamed entity shall be of an access-to-
1151 -- constant type if and only if the access_definition defines an
1152 -- access-to-constant type. ARM 8.5.1(4)
1153
1154 elsif Constant_Present (Access_Definition (N))
1155 and then not Is_Access_Constant (Etype (Nam))
1156 then
1157 Error_Msg_N
1158 ("(Ada 2005): the renamed object is not access-to-constant "
1159 & "(RM 8.5.1(6))", N);
1160
1161 elsif not Constant_Present (Access_Definition (N))
1162 and then Is_Access_Constant (Etype (Nam))
1163 then
1164 Error_Msg_N
1165 ("(Ada 2005): the renamed object is not access-to-variable "
1166 & "(RM 8.5.1(6))", N);
1167 end if;
1168
1169 if Is_Access_Subprogram_Type (Etype (Nam)) then
1170 Check_Subtype_Conformant
1171 (Designated_Type (T), Designated_Type (Etype (Nam)));
1172
1173 elsif not Subtypes_Statically_Match
1174 (Designated_Type (T),
1175 Available_View (Designated_Type (Etype (Nam))))
1176 then
1177 Error_Msg_N
1178 ("subtype of renamed object does not statically match", N);
1179 end if;
1180 end if;
1181
1182 -- Special processing for renaming function return object. Some errors
1183 -- and warnings are produced only for calls that come from source.
1184
1185 if Nkind (Nam) = N_Function_Call then
1186 case Ada_Version is
1187
1188 -- Usage is illegal in Ada 83, but renamings are also introduced
1189 -- during expansion, and error does not apply to those.
1190
1191 when Ada_83 =>
1192 if Comes_From_Source (N) then
1193 Error_Msg_N
1194 ("(Ada 83) cannot rename function return object", Nam);
1195 end if;
1196
1197 -- In Ada 95, warn for odd case of renaming parameterless function
1198 -- call if this is not a limited type (where this is useful).
1199
1200 when others =>
1201 if Warn_On_Object_Renames_Function
1202 and then No (Parameter_Associations (Nam))
1203 and then not Is_Limited_Type (Etype (Nam))
1204 and then Comes_From_Source (Nam)
1205 then
1206 Error_Msg_N
1207 ("renaming function result object is suspicious?R?", Nam);
1208 Error_Msg_NE
1209 ("\function & will be called only once?R?", Nam,
1210 Entity (Name (Nam)));
1211 Error_Msg_N -- CODEFIX
1212 ("\suggest using an initialized constant object "
1213 & "instead?R?", Nam);
1214 end if;
1215 end case;
1216 end if;
1217
1218 Check_Constrained_Object;
1219
1220 -- An object renaming requires an exact match of the type. Class-wide
1221 -- matching is not allowed.
1222
1223 if Is_Class_Wide_Type (T)
1224 and then Base_Type (Etype (Nam)) /= Base_Type (T)
1225 then
1226 Wrong_Type (Nam, T);
1227 end if;
1228
1229 -- We must search for an actual subtype here so that the bounds of
1230 -- objects of unconstrained types don't get dropped on the floor - such
1231 -- as with renamings of formal parameters.
1232
1233 T2 := Get_Actual_Subtype_If_Available (Nam);
1234
1235 -- Ada 2005 (AI-326): Handle wrong use of incomplete type
1236
1237 if Nkind (Nam) = N_Explicit_Dereference
1238 and then Ekind (Etype (T2)) = E_Incomplete_Type
1239 then
1240 Error_Msg_NE ("invalid use of incomplete type&", Id, T2);
1241 return;
1242
1243 elsif Ekind (Etype (T)) = E_Incomplete_Type then
1244 Error_Msg_NE ("invalid use of incomplete type&", Id, T);
1245 return;
1246 end if;
1247
1248 if Ada_Version >= Ada_2005 and then Nkind (Nam) in N_Has_Entity then
1249 declare
1250 Nam_Ent : constant Entity_Id := Entity (Get_Object_Name (Nam));
1251 Nam_Decl : constant Node_Id := Declaration_Node (Nam_Ent);
1252
1253 begin
1254 if Has_Null_Exclusion (N)
1255 and then not Has_Null_Exclusion (Nam_Decl)
1256 then
1257 -- Ada 2005 (AI-423): If the object name denotes a generic
1258 -- formal object of a generic unit G, and the object renaming
1259 -- declaration occurs within the body of G or within the body
1260 -- of a generic unit declared within the declarative region
1261 -- of G, then the declaration of the formal object of G must
1262 -- have a null exclusion or a null-excluding subtype.
1263
1264 if Is_Formal_Object (Nam_Ent)
1265 and then In_Generic_Scope (Id)
1266 then
1267 if not Can_Never_Be_Null (Etype (Nam_Ent)) then
1268 Error_Msg_N
1269 ("object does not exclude `NULL` "
1270 & "(RM 8.5.1(4.6/2))", N);
1271
1272 elsif In_Package_Body (Scope (Id)) then
1273 Error_Msg_N
1274 ("formal object does not have a null exclusion"
1275 & "(RM 8.5.1(4.6/2))", N);
1276 end if;
1277
1278 -- Ada 2005 (AI-423): Otherwise, the subtype of the object name
1279 -- shall exclude null.
1280
1281 elsif not Can_Never_Be_Null (Etype (Nam_Ent)) then
1282 Error_Msg_N
1283 ("object does not exclude `NULL` "
1284 & "(RM 8.5.1(4.6/2))", N);
1285
1286 -- An instance is illegal if it contains a renaming that
1287 -- excludes null, and the actual does not. The renaming
1288 -- declaration has already indicated that the declaration
1289 -- of the renamed actual in the instance will raise
1290 -- constraint_error.
1291
1292 elsif Nkind (Nam_Decl) = N_Object_Declaration
1293 and then In_Instance
1294 and then
1295 Present (Corresponding_Generic_Association (Nam_Decl))
1296 and then Nkind (Expression (Nam_Decl)) =
1297 N_Raise_Constraint_Error
1298 then
1299 Error_Msg_N
1300 ("actual does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1301
1302 -- Finally, if there is a null exclusion, the subtype mark
1303 -- must not be null-excluding.
1304
1305 elsif No (Access_Definition (N))
1306 and then Can_Never_Be_Null (T)
1307 then
1308 Error_Msg_NE
1309 ("`NOT NULL` not allowed (& already excludes null)",
1310 N, T);
1311
1312 end if;
1313
1314 elsif Can_Never_Be_Null (T)
1315 and then not Can_Never_Be_Null (Etype (Nam_Ent))
1316 then
1317 Error_Msg_N
1318 ("object does not exclude `NULL` (RM 8.5.1(4.6/2))", N);
1319
1320 elsif Has_Null_Exclusion (N)
1321 and then No (Access_Definition (N))
1322 and then Can_Never_Be_Null (T)
1323 then
1324 Error_Msg_NE
1325 ("`NOT NULL` not allowed (& already excludes null)", N, T);
1326 end if;
1327 end;
1328 end if;
1329
1330 -- Set the Ekind of the entity, unless it has been set already, as is
1331 -- the case for the iteration object over a container with no variable
1332 -- indexing. In that case it's been marked as a constant, and we do not
1333 -- want to change it to a variable.
1334
1335 if Ekind (Id) /= E_Constant then
1336 Set_Ekind (Id, E_Variable);
1337 end if;
1338
1339 -- Initialize the object size and alignment. Note that we used to call
1340 -- Init_Size_Align here, but that's wrong for objects which have only
1341 -- an Esize, not an RM_Size field.
1342
1343 Init_Object_Size_Align (Id);
1344
1345 -- If N comes from source then check that the original node is an
1346 -- object reference since there may have been several rewritting and
1347 -- folding. Do not do this for N_Function_Call or N_Explicit_Dereference
1348 -- which might correspond to rewrites of e.g. N_Selected_Component
1349 -- (for example Object.Method rewriting).
1350 -- If N does not come from source then assume the tree is properly
1351 -- formed and accept any object reference. In such cases we do support
1352 -- more cases of renamings anyway, so the actual check on which renaming
1353 -- is valid is better left to the code generator as a last sanity
1354 -- check.
1355
1356 if Comes_From_Source (N) then
1357 if Nkind_In (Nam, N_Function_Call, N_Explicit_Dereference) then
1358 Is_Object_Ref := Is_Object_Reference (Nam);
1359 else
1360 Is_Object_Ref := Is_Object_Reference (Original_Node (Nam));
1361 end if;
1362 else
1363 Is_Object_Ref := True;
1364 end if;
1365
1366 if T = Any_Type or else Etype (Nam) = Any_Type then
1367 return;
1368
1369 -- Verify that the renamed entity is an object or function call.
1370
1371 elsif Is_Object_Ref then
1372 if Comes_From_Source (N) then
1373 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1374 Error_Msg_N
1375 ("illegal renaming of discriminant-dependent component", Nam);
1376 end if;
1377
1378 -- If the renaming comes from source and the renamed object is a
1379 -- dereference, then mark the prefix as needing debug information,
1380 -- since it might have been rewritten hence internally generated
1381 -- and Debug_Renaming_Declaration will link the renaming to it.
1382
1383 if Nkind (Nam) = N_Explicit_Dereference
1384 and then Is_Entity_Name (Prefix (Nam))
1385 then
1386 Set_Debug_Info_Needed (Entity (Prefix (Nam)));
1387 end if;
1388 end if;
1389
1390 -- Weird but legal, equivalent to renaming a function call. Illegal
1391 -- if the literal is the result of constant-folding an attribute
1392 -- reference that is not a function.
1393
1394 elsif Is_Entity_Name (Nam)
1395 and then Ekind (Entity (Nam)) = E_Enumeration_Literal
1396 and then Nkind (Original_Node (Nam)) /= N_Attribute_Reference
1397 then
1398 null;
1399 else
1400 Error_Msg_N ("expect object name in renaming", Nam);
1401 end if;
1402
1403 Set_Etype (Id, T2);
1404
1405 if not Is_Variable (Nam) then
1406 Set_Ekind (Id, E_Constant);
1407 Set_Never_Set_In_Source (Id, True);
1408 Set_Is_True_Constant (Id, True);
1409 end if;
1410
1411 -- The entity of the renaming declaration needs to reflect whether the
1412 -- renamed object is atomic, independent, volatile or VFA. These flags
1413 -- are set on the renamed object in the RM legality sense.
1414
1415 Set_Is_Atomic (Id, Is_Atomic_Object (Nam));
1416 Set_Is_Independent (Id, Is_Independent_Object (Nam));
1417 Set_Is_Volatile (Id, Is_Volatile_Object (Nam));
1418 Set_Is_Volatile_Full_Access (Id, Is_Volatile_Full_Access_Object (Nam));
1419
1420 -- Treat as volatile if we just set the Volatile flag
1421
1422 if Is_Volatile (Id)
1423
1424 -- Or if we are renaming an entity which was marked this way
1425
1426 -- Are there more cases, e.g. X(J) where X is Treat_As_Volatile ???
1427
1428 or else (Is_Entity_Name (Nam)
1429 and then Treat_As_Volatile (Entity (Nam)))
1430 then
1431 Set_Treat_As_Volatile (Id, True);
1432 end if;
1433
1434 -- Now make the link to the renamed object
1435
1436 Set_Renamed_Object (Id, Nam);
1437
1438 -- Implementation-defined aspect specifications can appear in a renaming
1439 -- declaration, but not language-defined ones. The call to procedure
1440 -- Analyze_Aspect_Specifications will take care of this error check.
1441
1442 if Has_Aspects (N) then
1443 Analyze_Aspect_Specifications (N, Id);
1444 end if;
1445
1446 -- Deal with dimensions
1447
1448 Analyze_Dimension (N);
1449 end Analyze_Object_Renaming;
1450
1451 ------------------------------
1452 -- Analyze_Package_Renaming --
1453 ------------------------------
1454
1455 procedure Analyze_Package_Renaming (N : Node_Id) is
1456 New_P : constant Entity_Id := Defining_Entity (N);
1457 Old_P : Entity_Id;
1458 Spec : Node_Id;
1459
1460 begin
1461 if Name (N) = Error then
1462 return;
1463 end if;
1464
1465 -- Check for Text_IO special unit (we may be renaming a Text_IO child)
1466
1467 Check_Text_IO_Special_Unit (Name (N));
1468
1469 if Current_Scope /= Standard_Standard then
1470 Set_Is_Pure (New_P, Is_Pure (Current_Scope));
1471 end if;
1472
1473 Enter_Name (New_P);
1474 Analyze (Name (N));
1475
1476 if Is_Entity_Name (Name (N)) then
1477 Old_P := Entity (Name (N));
1478 else
1479 Old_P := Any_Id;
1480 end if;
1481
1482 if Etype (Old_P) = Any_Type then
1483 Error_Msg_N ("expect package name in renaming", Name (N));
1484
1485 elsif Ekind (Old_P) /= E_Package
1486 and then not (Ekind (Old_P) = E_Generic_Package
1487 and then In_Open_Scopes (Old_P))
1488 then
1489 if Ekind (Old_P) = E_Generic_Package then
1490 Error_Msg_N
1491 ("generic package cannot be renamed as a package", Name (N));
1492 else
1493 Error_Msg_Sloc := Sloc (Old_P);
1494 Error_Msg_NE
1495 ("expect package name in renaming, found& declared#",
1496 Name (N), Old_P);
1497 end if;
1498
1499 -- Set basic attributes to minimize cascaded errors
1500
1501 Set_Ekind (New_P, E_Package);
1502 Set_Etype (New_P, Standard_Void_Type);
1503
1504 -- Here for OK package renaming
1505
1506 else
1507 -- Entities in the old package are accessible through the renaming
1508 -- entity. The simplest implementation is to have both packages share
1509 -- the entity list.
1510
1511 Set_Ekind (New_P, E_Package);
1512 Set_Etype (New_P, Standard_Void_Type);
1513
1514 if Present (Renamed_Object (Old_P)) then
1515 Set_Renamed_Object (New_P, Renamed_Object (Old_P));
1516 else
1517 Set_Renamed_Object (New_P, Old_P);
1518 end if;
1519
1520 -- The package renaming declaration may become Ghost if it renames a
1521 -- Ghost entity.
1522
1523 Mark_Ghost_Renaming (N, Old_P);
1524
1525 Set_Has_Completion (New_P);
1526 Set_First_Entity (New_P, First_Entity (Old_P));
1527 Set_Last_Entity (New_P, Last_Entity (Old_P));
1528 Set_First_Private_Entity (New_P, First_Private_Entity (Old_P));
1529 Check_Library_Unit_Renaming (N, Old_P);
1530 Generate_Reference (Old_P, Name (N));
1531
1532 -- If the renaming is in the visible part of a package, then we set
1533 -- Renamed_In_Spec for the renamed package, to prevent giving
1534 -- warnings about no entities referenced. Such a warning would be
1535 -- overenthusiastic, since clients can see entities in the renamed
1536 -- package via the visible package renaming.
1537
1538 declare
1539 Ent : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1540 begin
1541 if Ekind (Ent) = E_Package
1542 and then not In_Private_Part (Ent)
1543 and then In_Extended_Main_Source_Unit (N)
1544 and then Ekind (Old_P) = E_Package
1545 then
1546 Set_Renamed_In_Spec (Old_P);
1547 end if;
1548 end;
1549
1550 -- If this is the renaming declaration of a package instantiation
1551 -- within itself, it is the declaration that ends the list of actuals
1552 -- for the instantiation. At this point, the subtypes that rename
1553 -- the actuals are flagged as generic, to avoid spurious ambiguities
1554 -- if the actuals for two distinct formals happen to coincide. If
1555 -- the actual is a private type, the subtype has a private completion
1556 -- that is flagged in the same fashion.
1557
1558 -- Resolution is identical to what is was in the original generic.
1559 -- On exit from the generic instance, these are turned into regular
1560 -- subtypes again, so they are compatible with types in their class.
1561
1562 if not Is_Generic_Instance (Old_P) then
1563 return;
1564 else
1565 Spec := Specification (Unit_Declaration_Node (Old_P));
1566 end if;
1567
1568 if Nkind (Spec) = N_Package_Specification
1569 and then Present (Generic_Parent (Spec))
1570 and then Old_P = Current_Scope
1571 and then Chars (New_P) = Chars (Generic_Parent (Spec))
1572 then
1573 declare
1574 E : Entity_Id;
1575
1576 begin
1577 E := First_Entity (Old_P);
1578 while Present (E) and then E /= New_P loop
1579 if Is_Type (E)
1580 and then Nkind (Parent (E)) = N_Subtype_Declaration
1581 then
1582 Set_Is_Generic_Actual_Type (E);
1583
1584 if Is_Private_Type (E)
1585 and then Present (Full_View (E))
1586 then
1587 Set_Is_Generic_Actual_Type (Full_View (E));
1588 end if;
1589 end if;
1590
1591 Next_Entity (E);
1592 end loop;
1593 end;
1594 end if;
1595 end if;
1596
1597 -- Implementation-defined aspect specifications can appear in a renaming
1598 -- declaration, but not language-defined ones. The call to procedure
1599 -- Analyze_Aspect_Specifications will take care of this error check.
1600
1601 if Has_Aspects (N) then
1602 Analyze_Aspect_Specifications (N, New_P);
1603 end if;
1604 end Analyze_Package_Renaming;
1605
1606 -------------------------------
1607 -- Analyze_Renamed_Character --
1608 -------------------------------
1609
1610 procedure Analyze_Renamed_Character
1611 (N : Node_Id;
1612 New_S : Entity_Id;
1613 Is_Body : Boolean)
1614 is
1615 C : constant Node_Id := Name (N);
1616
1617 begin
1618 if Ekind (New_S) = E_Function then
1619 Resolve (C, Etype (New_S));
1620
1621 if Is_Body then
1622 Check_Frozen_Renaming (N, New_S);
1623 end if;
1624
1625 else
1626 Error_Msg_N ("character literal can only be renamed as function", N);
1627 end if;
1628 end Analyze_Renamed_Character;
1629
1630 ---------------------------------
1631 -- Analyze_Renamed_Dereference --
1632 ---------------------------------
1633
1634 procedure Analyze_Renamed_Dereference
1635 (N : Node_Id;
1636 New_S : Entity_Id;
1637 Is_Body : Boolean)
1638 is
1639 Nam : constant Node_Id := Name (N);
1640 P : constant Node_Id := Prefix (Nam);
1641 Typ : Entity_Id;
1642 Ind : Interp_Index;
1643 It : Interp;
1644
1645 begin
1646 if not Is_Overloaded (P) then
1647 if Ekind (Etype (Nam)) /= E_Subprogram_Type
1648 or else not Type_Conformant (Etype (Nam), New_S)
1649 then
1650 Error_Msg_N ("designated type does not match specification", P);
1651 else
1652 Resolve (P);
1653 end if;
1654
1655 return;
1656
1657 else
1658 Typ := Any_Type;
1659 Get_First_Interp (Nam, Ind, It);
1660
1661 while Present (It.Nam) loop
1662
1663 if Ekind (It.Nam) = E_Subprogram_Type
1664 and then Type_Conformant (It.Nam, New_S)
1665 then
1666 if Typ /= Any_Id then
1667 Error_Msg_N ("ambiguous renaming", P);
1668 return;
1669 else
1670 Typ := It.Nam;
1671 end if;
1672 end if;
1673
1674 Get_Next_Interp (Ind, It);
1675 end loop;
1676
1677 if Typ = Any_Type then
1678 Error_Msg_N ("designated type does not match specification", P);
1679 else
1680 Resolve (N, Typ);
1681
1682 if Is_Body then
1683 Check_Frozen_Renaming (N, New_S);
1684 end if;
1685 end if;
1686 end if;
1687 end Analyze_Renamed_Dereference;
1688
1689 ---------------------------
1690 -- Analyze_Renamed_Entry --
1691 ---------------------------
1692
1693 procedure Analyze_Renamed_Entry
1694 (N : Node_Id;
1695 New_S : Entity_Id;
1696 Is_Body : Boolean)
1697 is
1698 Nam : constant Node_Id := Name (N);
1699 Sel : constant Node_Id := Selector_Name (Nam);
1700 Is_Actual : constant Boolean := Present (Corresponding_Formal_Spec (N));
1701 Old_S : Entity_Id;
1702
1703 begin
1704 if Entity (Sel) = Any_Id then
1705
1706 -- Selector is undefined on prefix. Error emitted already
1707
1708 Set_Has_Completion (New_S);
1709 return;
1710 end if;
1711
1712 -- Otherwise find renamed entity and build body of New_S as a call to it
1713
1714 Old_S := Find_Renamed_Entity (N, Selector_Name (Nam), New_S);
1715
1716 if Old_S = Any_Id then
1717 Error_Msg_N (" no subprogram or entry matches specification", N);
1718 else
1719 if Is_Body then
1720 Check_Subtype_Conformant (New_S, Old_S, N);
1721 Generate_Reference (New_S, Defining_Entity (N), 'b');
1722 Style.Check_Identifier (Defining_Entity (N), New_S);
1723
1724 else
1725 -- Only mode conformance required for a renaming_as_declaration
1726
1727 Check_Mode_Conformant (New_S, Old_S, N);
1728 end if;
1729
1730 Inherit_Renamed_Profile (New_S, Old_S);
1731
1732 -- The prefix can be an arbitrary expression that yields a task or
1733 -- protected object, so it must be resolved.
1734
1735 if Is_Access_Type (Etype (Prefix (Nam))) then
1736 Insert_Explicit_Dereference (Prefix (Nam));
1737 end if;
1738 Resolve (Prefix (Nam), Scope (Old_S));
1739 end if;
1740
1741 Set_Convention (New_S, Convention (Old_S));
1742 Set_Has_Completion (New_S, Inside_A_Generic);
1743
1744 -- AI05-0225: If the renamed entity is a procedure or entry of a
1745 -- protected object, the target object must be a variable.
1746
1747 if Is_Protected_Type (Scope (Old_S))
1748 and then Ekind (New_S) = E_Procedure
1749 and then not Is_Variable (Prefix (Nam))
1750 then
1751 if Is_Actual then
1752 Error_Msg_N
1753 ("target object of protected operation used as actual for "
1754 & "formal procedure must be a variable", Nam);
1755 else
1756 Error_Msg_N
1757 ("target object of protected operation renamed as procedure, "
1758 & "must be a variable", Nam);
1759 end if;
1760 end if;
1761
1762 if Is_Body then
1763 Check_Frozen_Renaming (N, New_S);
1764 end if;
1765 end Analyze_Renamed_Entry;
1766
1767 -----------------------------------
1768 -- Analyze_Renamed_Family_Member --
1769 -----------------------------------
1770
1771 procedure Analyze_Renamed_Family_Member
1772 (N : Node_Id;
1773 New_S : Entity_Id;
1774 Is_Body : Boolean)
1775 is
1776 Nam : constant Node_Id := Name (N);
1777 P : constant Node_Id := Prefix (Nam);
1778 Old_S : Entity_Id;
1779
1780 begin
1781 if (Is_Entity_Name (P) and then Ekind (Entity (P)) = E_Entry_Family)
1782 or else (Nkind (P) = N_Selected_Component
1783 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family)
1784 then
1785 if Is_Entity_Name (P) then
1786 Old_S := Entity (P);
1787 else
1788 Old_S := Entity (Selector_Name (P));
1789 end if;
1790
1791 if not Entity_Matches_Spec (Old_S, New_S) then
1792 Error_Msg_N ("entry family does not match specification", N);
1793
1794 elsif Is_Body then
1795 Check_Subtype_Conformant (New_S, Old_S, N);
1796 Generate_Reference (New_S, Defining_Entity (N), 'b');
1797 Style.Check_Identifier (Defining_Entity (N), New_S);
1798 end if;
1799
1800 else
1801 Error_Msg_N ("no entry family matches specification", N);
1802 end if;
1803
1804 Set_Has_Completion (New_S, Inside_A_Generic);
1805
1806 if Is_Body then
1807 Check_Frozen_Renaming (N, New_S);
1808 end if;
1809 end Analyze_Renamed_Family_Member;
1810
1811 -----------------------------------------
1812 -- Analyze_Renamed_Primitive_Operation --
1813 -----------------------------------------
1814
1815 procedure Analyze_Renamed_Primitive_Operation
1816 (N : Node_Id;
1817 New_S : Entity_Id;
1818 Is_Body : Boolean)
1819 is
1820 Old_S : Entity_Id;
1821 Nam : Entity_Id;
1822
1823 function Conforms
1824 (Subp : Entity_Id;
1825 Ctyp : Conformance_Type) return Boolean;
1826 -- Verify that the signatures of the renamed entity and the new entity
1827 -- match. The first formal of the renamed entity is skipped because it
1828 -- is the target object in any subsequent call.
1829
1830 --------------
1831 -- Conforms --
1832 --------------
1833
1834 function Conforms
1835 (Subp : Entity_Id;
1836 Ctyp : Conformance_Type) return Boolean
1837 is
1838 Old_F : Entity_Id;
1839 New_F : Entity_Id;
1840
1841 begin
1842 if Ekind (Subp) /= Ekind (New_S) then
1843 return False;
1844 end if;
1845
1846 Old_F := Next_Formal (First_Formal (Subp));
1847 New_F := First_Formal (New_S);
1848 while Present (Old_F) and then Present (New_F) loop
1849 if not Conforming_Types (Etype (Old_F), Etype (New_F), Ctyp) then
1850 return False;
1851 end if;
1852
1853 if Ctyp >= Mode_Conformant
1854 and then Ekind (Old_F) /= Ekind (New_F)
1855 then
1856 return False;
1857 end if;
1858
1859 Next_Formal (New_F);
1860 Next_Formal (Old_F);
1861 end loop;
1862
1863 return True;
1864 end Conforms;
1865
1866 -- Start of processing for Analyze_Renamed_Primitive_Operation
1867
1868 begin
1869 if not Is_Overloaded (Selector_Name (Name (N))) then
1870 Old_S := Entity (Selector_Name (Name (N)));
1871
1872 if not Conforms (Old_S, Type_Conformant) then
1873 Old_S := Any_Id;
1874 end if;
1875
1876 else
1877 -- Find the operation that matches the given signature
1878
1879 declare
1880 It : Interp;
1881 Ind : Interp_Index;
1882
1883 begin
1884 Old_S := Any_Id;
1885 Get_First_Interp (Selector_Name (Name (N)), Ind, It);
1886
1887 while Present (It.Nam) loop
1888 if Conforms (It.Nam, Type_Conformant) then
1889 Old_S := It.Nam;
1890 end if;
1891
1892 Get_Next_Interp (Ind, It);
1893 end loop;
1894 end;
1895 end if;
1896
1897 if Old_S = Any_Id then
1898 Error_Msg_N ("no subprogram or entry matches specification", N);
1899
1900 else
1901 if Is_Body then
1902 if not Conforms (Old_S, Subtype_Conformant) then
1903 Error_Msg_N ("subtype conformance error in renaming", N);
1904 end if;
1905
1906 Generate_Reference (New_S, Defining_Entity (N), 'b');
1907 Style.Check_Identifier (Defining_Entity (N), New_S);
1908
1909 else
1910 -- Only mode conformance required for a renaming_as_declaration
1911
1912 if not Conforms (Old_S, Mode_Conformant) then
1913 Error_Msg_N ("mode conformance error in renaming", N);
1914 end if;
1915
1916 -- AI12-0204: The prefix of a prefixed view that is renamed or
1917 -- passed as a formal subprogram must be renamable as an object.
1918
1919 Nam := Prefix (Name (N));
1920
1921 if Is_Object_Reference (Nam) then
1922 if Is_Dependent_Component_Of_Mutable_Object (Nam) then
1923 Error_Msg_N
1924 ("illegal renaming of discriminant-dependent component",
1925 Nam);
1926 end if;
1927 else
1928 Error_Msg_N ("expect object name in renaming", Nam);
1929 end if;
1930
1931 -- Enforce the rule given in (RM 6.3.1 (10.1/2)): a prefixed
1932 -- view of a subprogram is intrinsic, because the compiler has
1933 -- to generate a wrapper for any call to it. If the name in a
1934 -- subprogram renaming is a prefixed view, the entity is thus
1935 -- intrinsic, and 'Access cannot be applied to it.
1936
1937 Set_Convention (New_S, Convention_Intrinsic);
1938 end if;
1939
1940 -- Inherit_Renamed_Profile (New_S, Old_S);
1941
1942 -- The prefix can be an arbitrary expression that yields an
1943 -- object, so it must be resolved.
1944
1945 Resolve (Prefix (Name (N)));
1946 end if;
1947 end Analyze_Renamed_Primitive_Operation;
1948
1949 ---------------------------------
1950 -- Analyze_Subprogram_Renaming --
1951 ---------------------------------
1952
1953 procedure Analyze_Subprogram_Renaming (N : Node_Id) is
1954 Formal_Spec : constant Entity_Id := Corresponding_Formal_Spec (N);
1955 Is_Actual : constant Boolean := Present (Formal_Spec);
1956 Nam : constant Node_Id := Name (N);
1957 Save_AV : constant Ada_Version_Type := Ada_Version;
1958 Save_AVP : constant Node_Id := Ada_Version_Pragma;
1959 Save_AV_Exp : constant Ada_Version_Type := Ada_Version_Explicit;
1960 Spec : constant Node_Id := Specification (N);
1961
1962 Old_S : Entity_Id := Empty;
1963 Rename_Spec : Entity_Id;
1964
1965 procedure Build_Class_Wide_Wrapper
1966 (Ren_Id : out Entity_Id;
1967 Wrap_Id : out Entity_Id);
1968 -- Ada 2012 (AI05-0071): A generic/instance scenario involving a formal
1969 -- type with unknown discriminants and a generic primitive operation of
1970 -- the said type with a box require special processing when the actual
1971 -- is a class-wide type:
1972 --
1973 -- generic
1974 -- type Formal_Typ (<>) is private;
1975 -- with procedure Prim_Op (Param : Formal_Typ) is <>;
1976 -- package Gen is ...
1977 --
1978 -- package Inst is new Gen (Actual_Typ'Class);
1979 --
1980 -- In this case the general renaming mechanism used in the prologue of
1981 -- an instance no longer applies:
1982 --
1983 -- procedure Prim_Op (Param : Formal_Typ) renames Prim_Op;
1984 --
1985 -- The above is replaced the following wrapper/renaming combination:
1986 --
1987 -- procedure Wrapper (Param : Formal_Typ) is -- wrapper
1988 -- begin
1989 -- Prim_Op (Param); -- primitive
1990 -- end Wrapper;
1991 --
1992 -- procedure Prim_Op (Param : Formal_Typ) renames Wrapper;
1993 --
1994 -- This transformation applies only if there is no explicit visible
1995 -- class-wide operation at the point of the instantiation. Ren_Id is
1996 -- the entity of the renaming declaration. When the transformation
1997 -- applies, Wrap_Id is the entity of the generated class-wide wrapper
1998 -- (or Any_Id). Otherwise, Wrap_Id is the entity of the class-wide
1999 -- operation.
2000
2001 procedure Check_Null_Exclusion
2002 (Ren : Entity_Id;
2003 Sub : Entity_Id);
2004 -- Ada 2005 (AI-423): Given renaming Ren of subprogram Sub, check the
2005 -- following AI rules:
2006 --
2007 -- If Ren is a renaming of a formal subprogram and one of its
2008 -- parameters has a null exclusion, then the corresponding formal
2009 -- in Sub must also have one. Otherwise the subtype of the Sub's
2010 -- formal parameter must exclude null.
2011 --
2012 -- If Ren is a renaming of a formal function and its return
2013 -- profile has a null exclusion, then Sub's return profile must
2014 -- have one. Otherwise the subtype of Sub's return profile must
2015 -- exclude null.
2016
2017 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id);
2018 -- Ensure that a SPARK renaming denoted by its entity Subp_Id does not
2019 -- declare a primitive operation of a tagged type (SPARK RM 6.1.1(3)).
2020
2021 procedure Freeze_Actual_Profile;
2022 -- In Ada 2012, enforce the freezing rule concerning formal incomplete
2023 -- types: a callable entity freezes its profile, unless it has an
2024 -- incomplete untagged formal (RM 13.14(10.2/3)).
2025
2026 function Has_Class_Wide_Actual return Boolean;
2027 -- Ada 2012 (AI05-071, AI05-0131): True if N is the renaming for a
2028 -- defaulted formal subprogram where the actual for the controlling
2029 -- formal type is class-wide.
2030
2031 function Original_Subprogram (Subp : Entity_Id) return Entity_Id;
2032 -- Find renamed entity when the declaration is a renaming_as_body and
2033 -- the renamed entity may itself be a renaming_as_body. Used to enforce
2034 -- rule that a renaming_as_body is illegal if the declaration occurs
2035 -- before the subprogram it completes is frozen, and renaming indirectly
2036 -- renames the subprogram itself.(Defect Report 8652/0027).
2037
2038 ------------------------------
2039 -- Build_Class_Wide_Wrapper --
2040 ------------------------------
2041
2042 procedure Build_Class_Wide_Wrapper
2043 (Ren_Id : out Entity_Id;
2044 Wrap_Id : out Entity_Id)
2045 is
2046 Loc : constant Source_Ptr := Sloc (N);
2047
2048 function Build_Call
2049 (Subp_Id : Entity_Id;
2050 Params : List_Id) return Node_Id;
2051 -- Create a dispatching call to invoke routine Subp_Id with actuals
2052 -- built from the parameter specifications of list Params.
2053
2054 function Build_Expr_Fun_Call
2055 (Subp_Id : Entity_Id;
2056 Params : List_Id) return Node_Id;
2057 -- Create a dispatching call to invoke function Subp_Id with actuals
2058 -- built from the parameter specifications of list Params. Return
2059 -- directly the call, so that it can be used inside an expression
2060 -- function. This is a specificity of the GNATprove mode.
2061
2062 function Build_Spec (Subp_Id : Entity_Id) return Node_Id;
2063 -- Create a subprogram specification based on the subprogram profile
2064 -- of Subp_Id.
2065
2066 function Find_Primitive (Typ : Entity_Id) return Entity_Id;
2067 -- Find a primitive subprogram of type Typ which matches the profile
2068 -- of the renaming declaration.
2069
2070 procedure Interpretation_Error (Subp_Id : Entity_Id);
2071 -- Emit a continuation error message suggesting subprogram Subp_Id as
2072 -- a possible interpretation.
2073
2074 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean;
2075 -- Determine whether subprogram Subp_Id denotes the intrinsic "="
2076 -- operator.
2077
2078 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean;
2079 -- Determine whether subprogram Subp_Id is a suitable candidate for
2080 -- the role of a wrapped subprogram.
2081
2082 ----------------
2083 -- Build_Call --
2084 ----------------
2085
2086 function Build_Call
2087 (Subp_Id : Entity_Id;
2088 Params : List_Id) return Node_Id
2089 is
2090 Actuals : constant List_Id := New_List;
2091 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2092 Formal : Node_Id;
2093
2094 begin
2095 -- Build the actual parameters of the call
2096
2097 Formal := First (Params);
2098 while Present (Formal) loop
2099 Append_To (Actuals,
2100 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2101 Next (Formal);
2102 end loop;
2103
2104 -- Generate:
2105 -- return Subp_Id (Actuals);
2106
2107 if Ekind_In (Subp_Id, E_Function, E_Operator) then
2108 return
2109 Make_Simple_Return_Statement (Loc,
2110 Expression =>
2111 Make_Function_Call (Loc,
2112 Name => Call_Ref,
2113 Parameter_Associations => Actuals));
2114
2115 -- Generate:
2116 -- Subp_Id (Actuals);
2117
2118 else
2119 return
2120 Make_Procedure_Call_Statement (Loc,
2121 Name => Call_Ref,
2122 Parameter_Associations => Actuals);
2123 end if;
2124 end Build_Call;
2125
2126 -------------------------
2127 -- Build_Expr_Fun_Call --
2128 -------------------------
2129
2130 function Build_Expr_Fun_Call
2131 (Subp_Id : Entity_Id;
2132 Params : List_Id) return Node_Id
2133 is
2134 Actuals : constant List_Id := New_List;
2135 Call_Ref : constant Node_Id := New_Occurrence_Of (Subp_Id, Loc);
2136 Formal : Node_Id;
2137
2138 begin
2139 pragma Assert (Ekind_In (Subp_Id, E_Function, E_Operator));
2140
2141 -- Build the actual parameters of the call
2142
2143 Formal := First (Params);
2144 while Present (Formal) loop
2145 Append_To (Actuals,
2146 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2147 Next (Formal);
2148 end loop;
2149
2150 -- Generate:
2151 -- Subp_Id (Actuals);
2152
2153 return
2154 Make_Function_Call (Loc,
2155 Name => Call_Ref,
2156 Parameter_Associations => Actuals);
2157 end Build_Expr_Fun_Call;
2158
2159 ----------------
2160 -- Build_Spec --
2161 ----------------
2162
2163 function Build_Spec (Subp_Id : Entity_Id) return Node_Id is
2164 Params : constant List_Id := Copy_Parameter_List (Subp_Id);
2165 Spec_Id : constant Entity_Id :=
2166 Make_Defining_Identifier (Loc,
2167 Chars => New_External_Name (Chars (Subp_Id), 'R'));
2168
2169 begin
2170 if Ekind (Formal_Spec) = E_Procedure then
2171 return
2172 Make_Procedure_Specification (Loc,
2173 Defining_Unit_Name => Spec_Id,
2174 Parameter_Specifications => Params);
2175 else
2176 return
2177 Make_Function_Specification (Loc,
2178 Defining_Unit_Name => Spec_Id,
2179 Parameter_Specifications => Params,
2180 Result_Definition =>
2181 New_Copy_Tree (Result_Definition (Spec)));
2182 end if;
2183 end Build_Spec;
2184
2185 --------------------
2186 -- Find_Primitive --
2187 --------------------
2188
2189 function Find_Primitive (Typ : Entity_Id) return Entity_Id is
2190 procedure Replace_Parameter_Types (Spec : Node_Id);
2191 -- Given a specification Spec, replace all class-wide parameter
2192 -- types with reference to type Typ.
2193
2194 -----------------------------
2195 -- Replace_Parameter_Types --
2196 -----------------------------
2197
2198 procedure Replace_Parameter_Types (Spec : Node_Id) is
2199 Formal : Node_Id;
2200 Formal_Id : Entity_Id;
2201 Formal_Typ : Node_Id;
2202
2203 begin
2204 Formal := First (Parameter_Specifications (Spec));
2205 while Present (Formal) loop
2206 Formal_Id := Defining_Identifier (Formal);
2207 Formal_Typ := Parameter_Type (Formal);
2208
2209 -- Create a new entity for each class-wide formal to prevent
2210 -- aliasing with the original renaming. Replace the type of
2211 -- such a parameter with the candidate type.
2212
2213 if Nkind (Formal_Typ) = N_Identifier
2214 and then Is_Class_Wide_Type (Etype (Formal_Typ))
2215 then
2216 Set_Defining_Identifier (Formal,
2217 Make_Defining_Identifier (Loc, Chars (Formal_Id)));
2218
2219 Set_Parameter_Type (Formal, New_Occurrence_Of (Typ, Loc));
2220 end if;
2221
2222 Next (Formal);
2223 end loop;
2224 end Replace_Parameter_Types;
2225
2226 -- Local variables
2227
2228 Alt_Ren : constant Node_Id := New_Copy_Tree (N);
2229 Alt_Nam : constant Node_Id := Name (Alt_Ren);
2230 Alt_Spec : constant Node_Id := Specification (Alt_Ren);
2231 Subp_Id : Entity_Id;
2232
2233 -- Start of processing for Find_Primitive
2234
2235 begin
2236 -- Each attempt to find a suitable primitive of a particular type
2237 -- operates on its own copy of the original renaming. As a result
2238 -- the original renaming is kept decoration and side-effect free.
2239
2240 -- Inherit the overloaded status of the renamed subprogram name
2241
2242 if Is_Overloaded (Nam) then
2243 Set_Is_Overloaded (Alt_Nam);
2244 Save_Interps (Nam, Alt_Nam);
2245 end if;
2246
2247 -- The copied renaming is hidden from visibility to prevent the
2248 -- pollution of the enclosing context.
2249
2250 Set_Defining_Unit_Name (Alt_Spec, Make_Temporary (Loc, 'R'));
2251
2252 -- The types of all class-wide parameters must be changed to the
2253 -- candidate type.
2254
2255 Replace_Parameter_Types (Alt_Spec);
2256
2257 -- Try to find a suitable primitive which matches the altered
2258 -- profile of the renaming specification.
2259
2260 Subp_Id :=
2261 Find_Renamed_Entity
2262 (N => Alt_Ren,
2263 Nam => Name (Alt_Ren),
2264 New_S => Analyze_Subprogram_Specification (Alt_Spec),
2265 Is_Actual => Is_Actual);
2266
2267 -- Do not return Any_Id if the resolion of the altered profile
2268 -- failed as this complicates further checks on the caller side,
2269 -- return Empty instead.
2270
2271 if Subp_Id = Any_Id then
2272 return Empty;
2273 else
2274 return Subp_Id;
2275 end if;
2276 end Find_Primitive;
2277
2278 --------------------------
2279 -- Interpretation_Error --
2280 --------------------------
2281
2282 procedure Interpretation_Error (Subp_Id : Entity_Id) is
2283 begin
2284 Error_Msg_Sloc := Sloc (Subp_Id);
2285
2286 if Is_Internal (Subp_Id) then
2287 Error_Msg_NE
2288 ("\\possible interpretation: predefined & #",
2289 Spec, Formal_Spec);
2290 else
2291 Error_Msg_NE
2292 ("\\possible interpretation: & defined #", Spec, Formal_Spec);
2293 end if;
2294 end Interpretation_Error;
2295
2296 ---------------------------
2297 -- Is_Intrinsic_Equality --
2298 ---------------------------
2299
2300 function Is_Intrinsic_Equality (Subp_Id : Entity_Id) return Boolean is
2301 begin
2302 return
2303 Ekind (Subp_Id) = E_Operator
2304 and then Chars (Subp_Id) = Name_Op_Eq
2305 and then Is_Intrinsic_Subprogram (Subp_Id);
2306 end Is_Intrinsic_Equality;
2307
2308 ---------------------------
2309 -- Is_Suitable_Candidate --
2310 ---------------------------
2311
2312 function Is_Suitable_Candidate (Subp_Id : Entity_Id) return Boolean is
2313 begin
2314 if No (Subp_Id) then
2315 return False;
2316
2317 -- An intrinsic subprogram is never a good candidate. This is an
2318 -- indication of a missing primitive, either defined directly or
2319 -- inherited from a parent tagged type.
2320
2321 elsif Is_Intrinsic_Subprogram (Subp_Id) then
2322 return False;
2323
2324 else
2325 return True;
2326 end if;
2327 end Is_Suitable_Candidate;
2328
2329 -- Local variables
2330
2331 Actual_Typ : Entity_Id := Empty;
2332 -- The actual class-wide type for Formal_Typ
2333
2334 CW_Prim_OK : Boolean;
2335 CW_Prim_Op : Entity_Id;
2336 -- The class-wide subprogram (if available) which corresponds to the
2337 -- renamed generic formal subprogram.
2338
2339 Formal_Typ : Entity_Id := Empty;
2340 -- The generic formal type with unknown discriminants
2341
2342 Root_Prim_OK : Boolean;
2343 Root_Prim_Op : Entity_Id;
2344 -- The root type primitive (if available) which corresponds to the
2345 -- renamed generic formal subprogram.
2346
2347 Root_Typ : Entity_Id := Empty;
2348 -- The root type of Actual_Typ
2349
2350 Body_Decl : Node_Id;
2351 Formal : Node_Id;
2352 Prim_Op : Entity_Id;
2353 Spec_Decl : Node_Id;
2354 New_Spec : Node_Id;
2355
2356 -- Start of processing for Build_Class_Wide_Wrapper
2357
2358 begin
2359 -- Analyze the specification of the renaming in case the generation
2360 -- of the class-wide wrapper fails.
2361
2362 Ren_Id := Analyze_Subprogram_Specification (Spec);
2363 Wrap_Id := Any_Id;
2364
2365 -- Do not attempt to build a wrapper if the renaming is in error
2366
2367 if Error_Posted (Nam) then
2368 return;
2369 end if;
2370
2371 -- Analyze the renamed name, but do not resolve it. The resolution is
2372 -- completed once a suitable subprogram is found.
2373
2374 Analyze (Nam);
2375
2376 -- When the renamed name denotes the intrinsic operator equals, the
2377 -- name must be treated as overloaded. This allows for a potential
2378 -- match against the root type's predefined equality function.
2379
2380 if Is_Intrinsic_Equality (Entity (Nam)) then
2381 Set_Is_Overloaded (Nam);
2382 Collect_Interps (Nam);
2383 end if;
2384
2385 -- Step 1: Find the generic formal type with unknown discriminants
2386 -- and its corresponding class-wide actual type from the renamed
2387 -- generic formal subprogram.
2388
2389 Formal := First_Formal (Formal_Spec);
2390 while Present (Formal) loop
2391 if Has_Unknown_Discriminants (Etype (Formal))
2392 and then not Is_Class_Wide_Type (Etype (Formal))
2393 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (Formal)))
2394 then
2395 Formal_Typ := Etype (Formal);
2396 Actual_Typ := Get_Instance_Of (Formal_Typ);
2397 Root_Typ := Etype (Actual_Typ);
2398 exit;
2399 end if;
2400
2401 Next_Formal (Formal);
2402 end loop;
2403
2404 -- The specification of the generic formal subprogram should always
2405 -- contain a formal type with unknown discriminants whose actual is
2406 -- a class-wide type, otherwise this indicates a failure in routine
2407 -- Has_Class_Wide_Actual.
2408
2409 pragma Assert (Present (Formal_Typ));
2410
2411 -- Step 2: Find the proper class-wide subprogram or primitive which
2412 -- corresponds to the renamed generic formal subprogram.
2413
2414 CW_Prim_Op := Find_Primitive (Actual_Typ);
2415 CW_Prim_OK := Is_Suitable_Candidate (CW_Prim_Op);
2416 Root_Prim_Op := Find_Primitive (Root_Typ);
2417 Root_Prim_OK := Is_Suitable_Candidate (Root_Prim_Op);
2418
2419 -- The class-wide actual type has two subprograms which correspond to
2420 -- the renamed generic formal subprogram:
2421
2422 -- with procedure Prim_Op (Param : Formal_Typ);
2423
2424 -- procedure Prim_Op (Param : Actual_Typ); -- may be inherited
2425 -- procedure Prim_Op (Param : Actual_Typ'Class);
2426
2427 -- Even though the declaration of the two subprograms is legal, a
2428 -- call to either one is ambiguous and therefore illegal.
2429
2430 if CW_Prim_OK and Root_Prim_OK then
2431
2432 -- A user-defined primitive has precedence over a predefined one
2433
2434 if Is_Internal (CW_Prim_Op)
2435 and then not Is_Internal (Root_Prim_Op)
2436 then
2437 Prim_Op := Root_Prim_Op;
2438
2439 elsif Is_Internal (Root_Prim_Op)
2440 and then not Is_Internal (CW_Prim_Op)
2441 then
2442 Prim_Op := CW_Prim_Op;
2443
2444 elsif CW_Prim_Op = Root_Prim_Op then
2445 Prim_Op := Root_Prim_Op;
2446
2447 -- Otherwise both candidate subprograms are user-defined and
2448 -- ambiguous.
2449
2450 else
2451 Error_Msg_NE
2452 ("ambiguous actual for generic subprogram &",
2453 Spec, Formal_Spec);
2454 Interpretation_Error (Root_Prim_Op);
2455 Interpretation_Error (CW_Prim_Op);
2456 return;
2457 end if;
2458
2459 elsif CW_Prim_OK and not Root_Prim_OK then
2460 Prim_Op := CW_Prim_Op;
2461
2462 elsif not CW_Prim_OK and Root_Prim_OK then
2463 Prim_Op := Root_Prim_Op;
2464
2465 -- An intrinsic equality may act as a suitable candidate in the case
2466 -- of a null type extension where the parent's equality is hidden. A
2467 -- call to an intrinsic equality is expanded as dispatching.
2468
2469 elsif Present (Root_Prim_Op)
2470 and then Is_Intrinsic_Equality (Root_Prim_Op)
2471 then
2472 Prim_Op := Root_Prim_Op;
2473
2474 -- Otherwise there are no candidate subprograms. Let the caller
2475 -- diagnose the error.
2476
2477 else
2478 return;
2479 end if;
2480
2481 -- At this point resolution has taken place and the name is no longer
2482 -- overloaded. Mark the primitive as referenced.
2483
2484 Set_Is_Overloaded (Name (N), False);
2485 Set_Referenced (Prim_Op);
2486
2487 -- Do not generate a wrapper when the only candidate is a class-wide
2488 -- subprogram. Instead modify the renaming to directly map the actual
2489 -- to the generic formal.
2490
2491 if CW_Prim_OK and then Prim_Op = CW_Prim_Op then
2492 Wrap_Id := Prim_Op;
2493 Rewrite (Nam, New_Occurrence_Of (Prim_Op, Loc));
2494 return;
2495 end if;
2496
2497 -- Step 3: Create the declaration and the body of the wrapper, insert
2498 -- all the pieces into the tree.
2499
2500 -- In GNATprove mode, create a function wrapper in the form of an
2501 -- expression function, so that an implicit postcondition relating
2502 -- the result of calling the wrapper function and the result of the
2503 -- dispatching call to the wrapped function is known during proof.
2504
2505 if GNATprove_Mode
2506 and then Ekind_In (Ren_Id, E_Function, E_Operator)
2507 then
2508 New_Spec := Build_Spec (Ren_Id);
2509 Body_Decl :=
2510 Make_Expression_Function (Loc,
2511 Specification => New_Spec,
2512 Expression =>
2513 Build_Expr_Fun_Call
2514 (Subp_Id => Prim_Op,
2515 Params => Parameter_Specifications (New_Spec)));
2516
2517 Wrap_Id := Defining_Entity (Body_Decl);
2518
2519 -- Otherwise, create separate spec and body for the subprogram
2520
2521 else
2522 Spec_Decl :=
2523 Make_Subprogram_Declaration (Loc,
2524 Specification => Build_Spec (Ren_Id));
2525 Insert_Before_And_Analyze (N, Spec_Decl);
2526
2527 Wrap_Id := Defining_Entity (Spec_Decl);
2528
2529 Body_Decl :=
2530 Make_Subprogram_Body (Loc,
2531 Specification => Build_Spec (Ren_Id),
2532 Declarations => New_List,
2533 Handled_Statement_Sequence =>
2534 Make_Handled_Sequence_Of_Statements (Loc,
2535 Statements => New_List (
2536 Build_Call
2537 (Subp_Id => Prim_Op,
2538 Params =>
2539 Parameter_Specifications
2540 (Specification (Spec_Decl))))));
2541
2542 Set_Corresponding_Body (Spec_Decl, Defining_Entity (Body_Decl));
2543 end if;
2544
2545 -- If the operator carries an Eliminated pragma, indicate that the
2546 -- wrapper is also to be eliminated, to prevent spurious error when
2547 -- using gnatelim on programs that include box-initialization of
2548 -- equality operators.
2549
2550 Set_Is_Eliminated (Wrap_Id, Is_Eliminated (Prim_Op));
2551
2552 -- In GNATprove mode, insert the body in the tree for analysis
2553
2554 if GNATprove_Mode then
2555 Insert_Before_And_Analyze (N, Body_Decl);
2556 end if;
2557
2558 -- The generated body does not freeze and must be analyzed when the
2559 -- class-wide wrapper is frozen. The body is only needed if expansion
2560 -- is enabled.
2561
2562 if Expander_Active then
2563 Append_Freeze_Action (Wrap_Id, Body_Decl);
2564 end if;
2565
2566 -- Step 4: The subprogram renaming aliases the wrapper
2567
2568 Rewrite (Nam, New_Occurrence_Of (Wrap_Id, Loc));
2569 end Build_Class_Wide_Wrapper;
2570
2571 --------------------------
2572 -- Check_Null_Exclusion --
2573 --------------------------
2574
2575 procedure Check_Null_Exclusion
2576 (Ren : Entity_Id;
2577 Sub : Entity_Id)
2578 is
2579 Ren_Formal : Entity_Id;
2580 Sub_Formal : Entity_Id;
2581
2582 begin
2583 -- Parameter check
2584
2585 Ren_Formal := First_Formal (Ren);
2586 Sub_Formal := First_Formal (Sub);
2587 while Present (Ren_Formal) and then Present (Sub_Formal) loop
2588 if Has_Null_Exclusion (Parent (Ren_Formal))
2589 and then
2590 not (Has_Null_Exclusion (Parent (Sub_Formal))
2591 or else Can_Never_Be_Null (Etype (Sub_Formal)))
2592 then
2593 Error_Msg_NE
2594 ("`NOT NULL` required for parameter &",
2595 Parent (Sub_Formal), Sub_Formal);
2596 end if;
2597
2598 Next_Formal (Ren_Formal);
2599 Next_Formal (Sub_Formal);
2600 end loop;
2601
2602 -- Return profile check
2603
2604 if Nkind (Parent (Ren)) = N_Function_Specification
2605 and then Nkind (Parent (Sub)) = N_Function_Specification
2606 and then Has_Null_Exclusion (Parent (Ren))
2607 and then not (Has_Null_Exclusion (Parent (Sub))
2608 or else Can_Never_Be_Null (Etype (Sub)))
2609 then
2610 Error_Msg_N
2611 ("return must specify `NOT NULL`",
2612 Result_Definition (Parent (Sub)));
2613 end if;
2614 end Check_Null_Exclusion;
2615
2616 -------------------------------------
2617 -- Check_SPARK_Primitive_Operation --
2618 -------------------------------------
2619
2620 procedure Check_SPARK_Primitive_Operation (Subp_Id : Entity_Id) is
2621 Prag : constant Node_Id := SPARK_Pragma (Subp_Id);
2622 Typ : Entity_Id;
2623
2624 begin
2625 -- Nothing to do when the subprogram is not subject to SPARK_Mode On
2626 -- because this check applies to SPARK code only.
2627
2628 if not (Present (Prag)
2629 and then Get_SPARK_Mode_From_Annotation (Prag) = On)
2630 then
2631 return;
2632
2633 -- Nothing to do when the subprogram is not a primitive operation
2634
2635 elsif not Is_Primitive (Subp_Id) then
2636 return;
2637 end if;
2638
2639 Typ := Find_Dispatching_Type (Subp_Id);
2640
2641 -- Nothing to do when the subprogram is a primitive operation of an
2642 -- untagged type.
2643
2644 if No (Typ) then
2645 return;
2646 end if;
2647
2648 -- At this point a renaming declaration introduces a new primitive
2649 -- operation for a tagged type.
2650
2651 Error_Msg_Node_2 := Typ;
2652 Error_Msg_NE
2653 ("subprogram renaming & cannot declare primitive for type & "
2654 & "(SPARK RM 6.1.1(3))", N, Subp_Id);
2655 end Check_SPARK_Primitive_Operation;
2656
2657 ---------------------------
2658 -- Freeze_Actual_Profile --
2659 ---------------------------
2660
2661 procedure Freeze_Actual_Profile is
2662 F : Entity_Id;
2663 Has_Untagged_Inc : Boolean;
2664 Instantiation_Node : constant Node_Id := Parent (N);
2665
2666 begin
2667 if Ada_Version >= Ada_2012 then
2668 F := First_Formal (Formal_Spec);
2669 Has_Untagged_Inc := False;
2670 while Present (F) loop
2671 if Ekind (Etype (F)) = E_Incomplete_Type
2672 and then not Is_Tagged_Type (Etype (F))
2673 then
2674 Has_Untagged_Inc := True;
2675 exit;
2676 end if;
2677
2678 Next_Formal (F);
2679 end loop;
2680
2681 if Ekind (Formal_Spec) = E_Function
2682 and then not Is_Tagged_Type (Etype (Formal_Spec))
2683 then
2684 Has_Untagged_Inc := True;
2685 end if;
2686
2687 if not Has_Untagged_Inc then
2688 F := First_Formal (Old_S);
2689 while Present (F) loop
2690 Freeze_Before (Instantiation_Node, Etype (F));
2691
2692 if Is_Incomplete_Or_Private_Type (Etype (F))
2693 and then No (Underlying_Type (Etype (F)))
2694 then
2695 -- Exclude generic types, or types derived from them.
2696 -- They will be frozen in the enclosing instance.
2697
2698 if Is_Generic_Type (Etype (F))
2699 or else Is_Generic_Type (Root_Type (Etype (F)))
2700 then
2701 null;
2702
2703 -- A limited view of a type declared elsewhere needs no
2704 -- freezing actions.
2705
2706 elsif From_Limited_With (Etype (F)) then
2707 null;
2708
2709 else
2710 Error_Msg_NE
2711 ("type& must be frozen before this point",
2712 Instantiation_Node, Etype (F));
2713 end if;
2714 end if;
2715
2716 Next_Formal (F);
2717 end loop;
2718 end if;
2719 end if;
2720 end Freeze_Actual_Profile;
2721
2722 ---------------------------
2723 -- Has_Class_Wide_Actual --
2724 ---------------------------
2725
2726 function Has_Class_Wide_Actual return Boolean is
2727 Formal : Entity_Id;
2728 Formal_Typ : Entity_Id;
2729
2730 begin
2731 if Is_Actual then
2732 Formal := First_Formal (Formal_Spec);
2733 while Present (Formal) loop
2734 Formal_Typ := Etype (Formal);
2735
2736 if Has_Unknown_Discriminants (Formal_Typ)
2737 and then not Is_Class_Wide_Type (Formal_Typ)
2738 and then Is_Class_Wide_Type (Get_Instance_Of (Formal_Typ))
2739 then
2740 return True;
2741 end if;
2742
2743 Next_Formal (Formal);
2744 end loop;
2745 end if;
2746
2747 return False;
2748 end Has_Class_Wide_Actual;
2749
2750 -------------------------
2751 -- Original_Subprogram --
2752 -------------------------
2753
2754 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
2755 Orig_Decl : Node_Id;
2756 Orig_Subp : Entity_Id;
2757
2758 begin
2759 -- First case: renamed entity is itself a renaming
2760
2761 if Present (Alias (Subp)) then
2762 return Alias (Subp);
2763
2764 elsif Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
2765 and then Present (Corresponding_Body (Unit_Declaration_Node (Subp)))
2766 then
2767 -- Check if renamed entity is a renaming_as_body
2768
2769 Orig_Decl :=
2770 Unit_Declaration_Node
2771 (Corresponding_Body (Unit_Declaration_Node (Subp)));
2772
2773 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
2774 Orig_Subp := Entity (Name (Orig_Decl));
2775
2776 if Orig_Subp = Rename_Spec then
2777
2778 -- Circularity detected
2779
2780 return Orig_Subp;
2781
2782 else
2783 return (Original_Subprogram (Orig_Subp));
2784 end if;
2785 else
2786 return Subp;
2787 end if;
2788 else
2789 return Subp;
2790 end if;
2791 end Original_Subprogram;
2792
2793 -- Local variables
2794
2795 CW_Actual : constant Boolean := Has_Class_Wide_Actual;
2796 -- Ada 2012 (AI05-071, AI05-0131): True if the renaming is for a
2797 -- defaulted formal subprogram when the actual for a related formal
2798 -- type is class-wide.
2799
2800 Inst_Node : Node_Id := Empty;
2801 New_S : Entity_Id;
2802
2803 -- Start of processing for Analyze_Subprogram_Renaming
2804
2805 begin
2806 -- We must test for the attribute renaming case before the Analyze
2807 -- call because otherwise Sem_Attr will complain that the attribute
2808 -- is missing an argument when it is analyzed.
2809
2810 if Nkind (Nam) = N_Attribute_Reference then
2811
2812 -- In the case of an abstract formal subprogram association, rewrite
2813 -- an actual given by a stream or Put_Image attribute as the name of
2814 -- the corresponding stream or Put_Image primitive of the type.
2815
2816 -- In a generic context the stream and Put_Image operations are not
2817 -- generated, and this must be treated as a normal attribute
2818 -- reference, to be expanded in subsequent instantiations.
2819
2820 if Is_Actual
2821 and then Is_Abstract_Subprogram (Formal_Spec)
2822 and then Expander_Active
2823 then
2824 declare
2825 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
2826 Prim : Entity_Id;
2827
2828 begin
2829 -- The class-wide forms of the stream and Put_Image attributes
2830 -- are not primitive dispatching operations (even though they
2831 -- internally dispatch).
2832
2833 if Is_Class_Wide_Type (Prefix_Type) then
2834 Error_Msg_N
2835 ("attribute must be a primitive dispatching operation",
2836 Nam);
2837 return;
2838 end if;
2839
2840 -- Retrieve the primitive subprogram associated with the
2841 -- attribute. This can only be a stream attribute, since those
2842 -- are the only ones that are dispatching (and the actual for
2843 -- an abstract formal subprogram must be dispatching
2844 -- operation).
2845
2846 case Attribute_Name (Nam) is
2847 when Name_Input =>
2848 Prim :=
2849 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Input);
2850
2851 when Name_Output =>
2852 Prim :=
2853 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Output);
2854
2855 when Name_Read =>
2856 Prim :=
2857 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Read);
2858
2859 when Name_Write =>
2860 Prim :=
2861 Find_Optional_Prim_Op (Prefix_Type, TSS_Stream_Write);
2862
2863 when Name_Put_Image =>
2864 Prim :=
2865 Find_Optional_Prim_Op (Prefix_Type, TSS_Put_Image);
2866
2867 when others =>
2868 Error_Msg_N
2869 ("attribute must be a primitive dispatching operation",
2870 Nam);
2871 return;
2872 end case;
2873
2874 -- If no stream operation was found, and the type is limited,
2875 -- the user should have defined one. This rule does not apply
2876 -- to Put_Image.
2877
2878 if No (Prim)
2879 and then Attribute_Name (Nam) /= Name_Put_Image
2880 then
2881 if Is_Limited_Type (Prefix_Type) then
2882 Error_Msg_NE
2883 ("stream operation not defined for type&",
2884 N, Prefix_Type);
2885 return;
2886
2887 -- Otherwise, compiler should have generated default
2888
2889 else
2890 raise Program_Error;
2891 end if;
2892 end if;
2893
2894 -- Rewrite the attribute into the name of its corresponding
2895 -- primitive dispatching subprogram. We can then proceed with
2896 -- the usual processing for subprogram renamings.
2897
2898 declare
2899 Prim_Name : constant Node_Id :=
2900 Make_Identifier (Sloc (Nam),
2901 Chars => Chars (Prim));
2902 begin
2903 Set_Entity (Prim_Name, Prim);
2904 Rewrite (Nam, Prim_Name);
2905 Analyze (Nam);
2906 end;
2907 end;
2908
2909 -- Normal processing for a renaming of an attribute
2910
2911 else
2912 Attribute_Renaming (N);
2913 return;
2914 end if;
2915 end if;
2916
2917 -- Check whether this declaration corresponds to the instantiation of a
2918 -- formal subprogram.
2919
2920 -- If this is an instantiation, the corresponding actual is frozen and
2921 -- error messages can be made more precise. If this is a default
2922 -- subprogram, the entity is already established in the generic, and is
2923 -- not retrieved by visibility. If it is a default with a box, the
2924 -- candidate interpretations, if any, have been collected when building
2925 -- the renaming declaration. If overloaded, the proper interpretation is
2926 -- determined in Find_Renamed_Entity. If the entity is an operator,
2927 -- Find_Renamed_Entity applies additional visibility checks.
2928
2929 if Is_Actual then
2930 Inst_Node := Unit_Declaration_Node (Formal_Spec);
2931
2932 -- Check whether the renaming is for a defaulted actual subprogram
2933 -- with a class-wide actual.
2934
2935 -- The class-wide wrapper is not needed in GNATprove_Mode and there
2936 -- is an external axiomatization on the package.
2937
2938 if CW_Actual
2939 and then Box_Present (Inst_Node)
2940 and then not
2941 (GNATprove_Mode
2942 and then
2943 Present (Containing_Package_With_Ext_Axioms (Formal_Spec)))
2944 then
2945 Build_Class_Wide_Wrapper (New_S, Old_S);
2946
2947 elsif Is_Entity_Name (Nam)
2948 and then Present (Entity (Nam))
2949 and then not Comes_From_Source (Nam)
2950 and then not Is_Overloaded (Nam)
2951 then
2952 Old_S := Entity (Nam);
2953
2954 -- The subprogram renaming declaration may become Ghost if it
2955 -- renames a Ghost entity.
2956
2957 Mark_Ghost_Renaming (N, Old_S);
2958
2959 New_S := Analyze_Subprogram_Specification (Spec);
2960
2961 -- Operator case
2962
2963 if Ekind (Old_S) = E_Operator then
2964
2965 -- Box present
2966
2967 if Box_Present (Inst_Node) then
2968 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2969
2970 -- If there is an immediately visible homonym of the operator
2971 -- and the declaration has a default, this is worth a warning
2972 -- because the user probably did not intend to get the pre-
2973 -- defined operator, visible in the generic declaration. To
2974 -- find if there is an intended candidate, analyze the renaming
2975 -- again in the current context.
2976
2977 elsif Scope (Old_S) = Standard_Standard
2978 and then Present (Default_Name (Inst_Node))
2979 then
2980 declare
2981 Decl : constant Node_Id := New_Copy_Tree (N);
2982 Hidden : Entity_Id;
2983
2984 begin
2985 Set_Entity (Name (Decl), Empty);
2986 Analyze (Name (Decl));
2987 Hidden :=
2988 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
2989
2990 if Present (Hidden)
2991 and then In_Open_Scopes (Scope (Hidden))
2992 and then Is_Immediately_Visible (Hidden)
2993 and then Comes_From_Source (Hidden)
2994 and then Hidden /= Old_S
2995 then
2996 Error_Msg_Sloc := Sloc (Hidden);
2997 Error_Msg_N
2998 ("default subprogram is resolved in the generic "
2999 & "declaration (RM 12.6(17))??", N);
3000 Error_Msg_NE ("\and will not use & #??", N, Hidden);
3001 end if;
3002 end;
3003 end if;
3004 end if;
3005
3006 else
3007 Analyze (Nam);
3008
3009 -- The subprogram renaming declaration may become Ghost if it
3010 -- renames a Ghost entity.
3011
3012 if Is_Entity_Name (Nam) then
3013 Mark_Ghost_Renaming (N, Entity (Nam));
3014 end if;
3015
3016 New_S := Analyze_Subprogram_Specification (Spec);
3017 end if;
3018
3019 else
3020 -- Renamed entity must be analyzed first, to avoid being hidden by
3021 -- new name (which might be the same in a generic instance).
3022
3023 Analyze (Nam);
3024
3025 -- The subprogram renaming declaration may become Ghost if it renames
3026 -- a Ghost entity.
3027
3028 if Is_Entity_Name (Nam) then
3029 Mark_Ghost_Renaming (N, Entity (Nam));
3030 end if;
3031
3032 -- The renaming defines a new overloaded entity, which is analyzed
3033 -- like a subprogram declaration.
3034
3035 New_S := Analyze_Subprogram_Specification (Spec);
3036 end if;
3037
3038 if Current_Scope /= Standard_Standard then
3039 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
3040 end if;
3041
3042 -- Set SPARK mode from current context
3043
3044 Set_SPARK_Pragma (New_S, SPARK_Mode_Pragma);
3045 Set_SPARK_Pragma_Inherited (New_S);
3046
3047 Rename_Spec := Find_Corresponding_Spec (N);
3048
3049 -- Case of Renaming_As_Body
3050
3051 if Present (Rename_Spec) then
3052 Check_Previous_Null_Procedure (N, Rename_Spec);
3053
3054 -- Renaming declaration is the completion of the declaration of
3055 -- Rename_Spec. We build an actual body for it at the freezing point.
3056
3057 Set_Corresponding_Spec (N, Rename_Spec);
3058
3059 -- Deal with special case of stream functions of abstract types
3060 -- and interfaces.
3061
3062 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
3063 N_Abstract_Subprogram_Declaration
3064 then
3065 -- Input stream functions are abstract if the object type is
3066 -- abstract. Similarly, all default stream functions for an
3067 -- interface type are abstract. However, these subprograms may
3068 -- receive explicit declarations in representation clauses, making
3069 -- the attribute subprograms usable as defaults in subsequent
3070 -- type extensions.
3071 -- In this case we rewrite the declaration to make the subprogram
3072 -- non-abstract. We remove the previous declaration, and insert
3073 -- the new one at the point of the renaming, to prevent premature
3074 -- access to unfrozen types. The new declaration reuses the
3075 -- specification of the previous one, and must not be analyzed.
3076
3077 pragma Assert
3078 (Is_Primitive (Entity (Nam))
3079 and then
3080 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
3081 declare
3082 Old_Decl : constant Node_Id :=
3083 Unit_Declaration_Node (Rename_Spec);
3084 New_Decl : constant Node_Id :=
3085 Make_Subprogram_Declaration (Sloc (N),
3086 Specification =>
3087 Relocate_Node (Specification (Old_Decl)));
3088 begin
3089 Remove (Old_Decl);
3090 Insert_After (N, New_Decl);
3091 Set_Is_Abstract_Subprogram (Rename_Spec, False);
3092 Set_Analyzed (New_Decl);
3093 end;
3094 end if;
3095
3096 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
3097
3098 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3099 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
3100 end if;
3101
3102 Set_Convention (New_S, Convention (Rename_Spec));
3103 Check_Fully_Conformant (New_S, Rename_Spec);
3104 Set_Public_Status (New_S);
3105
3106 if No_Return (Rename_Spec)
3107 and then not No_Return (Entity (Nam))
3108 then
3109 Error_Msg_N ("renaming completes a No_Return procedure", N);
3110 Error_Msg_N
3111 ("\renamed procedure must be nonreturning (RM 6.5.1 (7/2))", N);
3112 end if;
3113
3114 -- The specification does not introduce new formals, but only
3115 -- repeats the formals of the original subprogram declaration.
3116 -- For cross-reference purposes, and for refactoring tools, we
3117 -- treat the formals of the renaming declaration as body formals.
3118
3119 Reference_Body_Formals (Rename_Spec, New_S);
3120
3121 -- Indicate that the entity in the declaration functions like the
3122 -- corresponding body, and is not a new entity. The body will be
3123 -- constructed later at the freeze point, so indicate that the
3124 -- completion has not been seen yet.
3125
3126 Set_Ekind (New_S, E_Subprogram_Body);
3127 New_S := Rename_Spec;
3128 Set_Has_Completion (Rename_Spec, False);
3129
3130 -- Ada 2005: check overriding indicator
3131
3132 if Present (Overridden_Operation (Rename_Spec)) then
3133 if Must_Not_Override (Specification (N)) then
3134 Error_Msg_NE
3135 ("subprogram& overrides inherited operation",
3136 N, Rename_Spec);
3137
3138 elsif Style_Check
3139 and then not Must_Override (Specification (N))
3140 then
3141 Style.Missing_Overriding (N, Rename_Spec);
3142 end if;
3143
3144 elsif Must_Override (Specification (N)) then
3145 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
3146 end if;
3147
3148 -- Normal subprogram renaming (not renaming as body)
3149
3150 else
3151 Generate_Definition (New_S);
3152 New_Overloaded_Entity (New_S);
3153
3154 if not (Is_Entity_Name (Nam)
3155 and then Is_Intrinsic_Subprogram (Entity (Nam)))
3156 then
3157 Check_Delayed_Subprogram (New_S);
3158 end if;
3159
3160 -- Verify that a SPARK renaming does not declare a primitive
3161 -- operation of a tagged type.
3162
3163 Check_SPARK_Primitive_Operation (New_S);
3164 end if;
3165
3166 -- There is no need for elaboration checks on the new entity, which may
3167 -- be called before the next freezing point where the body will appear.
3168 -- Elaboration checks refer to the real entity, not the one created by
3169 -- the renaming declaration.
3170
3171 Set_Kill_Elaboration_Checks (New_S, True);
3172
3173 -- If we had a previous error, indicate a completely is present to stop
3174 -- junk cascaded messages, but don't take any further action.
3175
3176 if Etype (Nam) = Any_Type then
3177 Set_Has_Completion (New_S);
3178 return;
3179
3180 -- Case where name has the form of a selected component
3181
3182 elsif Nkind (Nam) = N_Selected_Component then
3183
3184 -- A name which has the form A.B can designate an entry of task A, a
3185 -- protected operation of protected object A, or finally a primitive
3186 -- operation of object A. In the later case, A is an object of some
3187 -- tagged type, or an access type that denotes one such. To further
3188 -- distinguish these cases, note that the scope of a task entry or
3189 -- protected operation is type of the prefix.
3190
3191 -- The prefix could be an overloaded function call that returns both
3192 -- kinds of operations. This overloading pathology is left to the
3193 -- dedicated reader ???
3194
3195 declare
3196 T : constant Entity_Id := Etype (Prefix (Nam));
3197
3198 begin
3199 if Present (T)
3200 and then
3201 (Is_Tagged_Type (T)
3202 or else
3203 (Is_Access_Type (T)
3204 and then Is_Tagged_Type (Designated_Type (T))))
3205 and then Scope (Entity (Selector_Name (Nam))) /= T
3206 then
3207 Analyze_Renamed_Primitive_Operation
3208 (N, New_S, Present (Rename_Spec));
3209 return;
3210
3211 else
3212 -- Renamed entity is an entry or protected operation. For those
3213 -- cases an explicit body is built (at the point of freezing of
3214 -- this entity) that contains a call to the renamed entity.
3215
3216 -- This is not allowed for renaming as body if the renamed
3217 -- spec is already frozen (see RM 8.5.4(5) for details).
3218
3219 if Present (Rename_Spec) and then Is_Frozen (Rename_Spec) then
3220 Error_Msg_N
3221 ("renaming-as-body cannot rename entry as subprogram", N);
3222 Error_Msg_NE
3223 ("\since & is already frozen (RM 8.5.4(5))",
3224 N, Rename_Spec);
3225 else
3226 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
3227 end if;
3228
3229 return;
3230 end if;
3231 end;
3232
3233 -- Case where name is an explicit dereference X.all
3234
3235 elsif Nkind (Nam) = N_Explicit_Dereference then
3236
3237 -- Renamed entity is designated by access_to_subprogram expression.
3238 -- Must build body to encapsulate call, as in the entry case.
3239
3240 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
3241 return;
3242
3243 -- Indexed component
3244
3245 elsif Nkind (Nam) = N_Indexed_Component then
3246 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
3247 return;
3248
3249 -- Character literal
3250
3251 elsif Nkind (Nam) = N_Character_Literal then
3252 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
3253 return;
3254
3255 -- Only remaining case is where we have a non-entity name, or a renaming
3256 -- of some other non-overloadable entity.
3257
3258 elsif not Is_Entity_Name (Nam)
3259 or else not Is_Overloadable (Entity (Nam))
3260 then
3261 -- Do not mention the renaming if it comes from an instance
3262
3263 if not Is_Actual then
3264 Error_Msg_N ("expect valid subprogram name in renaming", N);
3265 else
3266 Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
3267 end if;
3268
3269 return;
3270 end if;
3271
3272 -- Find the renamed entity that matches the given specification. Disable
3273 -- Ada_83 because there is no requirement of full conformance between
3274 -- renamed entity and new entity, even though the same circuit is used.
3275
3276 -- This is a bit of an odd case, which introduces a really irregular use
3277 -- of Ada_Version[_Explicit]. Would be nice to find cleaner way to do
3278 -- this. ???
3279
3280 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
3281 Ada_Version_Pragma := Empty;
3282 Ada_Version_Explicit := Ada_Version;
3283
3284 if No (Old_S) then
3285 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
3286
3287 -- The visible operation may be an inherited abstract operation that
3288 -- was overridden in the private part, in which case a call will
3289 -- dispatch to the overriding operation. Use the overriding one in
3290 -- the renaming declaration, to prevent spurious errors below.
3291
3292 if Is_Overloadable (Old_S)
3293 and then Is_Abstract_Subprogram (Old_S)
3294 and then No (DTC_Entity (Old_S))
3295 and then Present (Alias (Old_S))
3296 and then not Is_Abstract_Subprogram (Alias (Old_S))
3297 and then Present (Overridden_Operation (Alias (Old_S)))
3298 then
3299 Old_S := Alias (Old_S);
3300 end if;
3301
3302 -- When the renamed subprogram is overloaded and used as an actual
3303 -- of a generic, its entity is set to the first available homonym.
3304 -- We must first disambiguate the name, then set the proper entity.
3305
3306 if Is_Actual and then Is_Overloaded (Nam) then
3307 Set_Entity (Nam, Old_S);
3308 end if;
3309 end if;
3310
3311 -- Most common case: subprogram renames subprogram. No body is generated
3312 -- in this case, so we must indicate the declaration is complete as is.
3313 -- and inherit various attributes of the renamed subprogram.
3314
3315 if No (Rename_Spec) then
3316 Set_Has_Completion (New_S);
3317 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
3318 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
3319 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
3320
3321 -- Ada 2005 (AI-423): Check the consistency of null exclusions
3322 -- between a subprogram and its correct renaming.
3323
3324 -- Note: the Any_Id check is a guard that prevents compiler crashes
3325 -- when performing a null exclusion check between a renaming and a
3326 -- renamed subprogram that has been found to be illegal.
3327
3328 if Ada_Version >= Ada_2005 and then Entity (Nam) /= Any_Id then
3329 Check_Null_Exclusion
3330 (Ren => New_S,
3331 Sub => Entity (Nam));
3332 end if;
3333
3334 -- Enforce the Ada 2005 rule that the renamed entity cannot require
3335 -- overriding. The flag Requires_Overriding is set very selectively
3336 -- and misses some other illegal cases. The additional conditions
3337 -- checked below are sufficient but not necessary ???
3338
3339 -- The rule does not apply to the renaming generated for an actual
3340 -- subprogram in an instance.
3341
3342 if Is_Actual then
3343 null;
3344
3345 -- Guard against previous errors, and omit renamings of predefined
3346 -- operators.
3347
3348 elsif not Ekind_In (Old_S, E_Function, E_Procedure) then
3349 null;
3350
3351 elsif Requires_Overriding (Old_S)
3352 or else
3353 (Is_Abstract_Subprogram (Old_S)
3354 and then Present (Find_Dispatching_Type (Old_S))
3355 and then not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
3356 then
3357 Error_Msg_N
3358 ("renamed entity cannot be subprogram that requires overriding "
3359 & "(RM 8.5.4 (5.1))", N);
3360 end if;
3361
3362 declare
3363 Prev : constant Entity_Id := Overridden_Operation (New_S);
3364 begin
3365 if Present (Prev)
3366 and then
3367 (Has_Non_Trivial_Precondition (Prev)
3368 or else Has_Non_Trivial_Precondition (Old_S))
3369 then
3370 Error_Msg_NE
3371 ("conflicting inherited classwide preconditions in renaming "
3372 & "of& (RM 6.1.1 (17)", N, Old_S);
3373 end if;
3374 end;
3375 end if;
3376
3377 if Old_S /= Any_Id then
3378 if Is_Actual and then From_Default (N) then
3379
3380 -- This is an implicit reference to the default actual
3381
3382 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
3383
3384 else
3385 Generate_Reference (Old_S, Nam);
3386 end if;
3387
3388 Check_Internal_Protected_Use (N, Old_S);
3389
3390 -- For a renaming-as-body, require subtype conformance, but if the
3391 -- declaration being completed has not been frozen, then inherit the
3392 -- convention of the renamed subprogram prior to checking conformance
3393 -- (unless the renaming has an explicit convention established; the
3394 -- rule stated in the RM doesn't seem to address this ???).
3395
3396 if Present (Rename_Spec) then
3397 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
3398 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
3399
3400 if not Is_Frozen (Rename_Spec) then
3401 if not Has_Convention_Pragma (Rename_Spec) then
3402 Set_Convention (New_S, Convention (Old_S));
3403 end if;
3404
3405 if Ekind (Old_S) /= E_Operator then
3406 Check_Mode_Conformant (New_S, Old_S, Spec);
3407 end if;
3408
3409 if Original_Subprogram (Old_S) = Rename_Spec then
3410 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
3411 end if;
3412 else
3413 Check_Subtype_Conformant (New_S, Old_S, Spec);
3414 end if;
3415
3416 Check_Frozen_Renaming (N, Rename_Spec);
3417
3418 -- Check explicitly that renamed entity is not intrinsic, because
3419 -- in a generic the renamed body is not built. In this case,
3420 -- the renaming_as_body is a completion.
3421
3422 if Inside_A_Generic then
3423 if Is_Frozen (Rename_Spec)
3424 and then Is_Intrinsic_Subprogram (Old_S)
3425 then
3426 Error_Msg_N
3427 ("subprogram in renaming_as_body cannot be intrinsic",
3428 Name (N));
3429 end if;
3430
3431 Set_Has_Completion (Rename_Spec);
3432 end if;
3433
3434 elsif Ekind (Old_S) /= E_Operator then
3435
3436 -- If this a defaulted subprogram for a class-wide actual there is
3437 -- no check for mode conformance, given that the signatures don't
3438 -- match (the source mentions T but the actual mentions T'Class).
3439
3440 if CW_Actual then
3441 null;
3442
3443 -- No need for a redundant error message if this is a nested
3444 -- instance, unless the current instantiation (of a child unit)
3445 -- is a compilation unit, which is not analyzed when the parent
3446 -- generic is analyzed.
3447
3448 elsif not Is_Actual
3449 or else No (Enclosing_Instance)
3450 or else Is_Compilation_Unit (Current_Scope)
3451 then
3452 Check_Mode_Conformant (New_S, Old_S);
3453 end if;
3454
3455 if Is_Actual and then Error_Posted (New_S) then
3456 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
3457 end if;
3458 end if;
3459
3460 if No (Rename_Spec) then
3461
3462 -- The parameter profile of the new entity is that of the renamed
3463 -- entity: the subtypes given in the specification are irrelevant.
3464
3465 Inherit_Renamed_Profile (New_S, Old_S);
3466
3467 -- A call to the subprogram is transformed into a call to the
3468 -- renamed entity. This is transitive if the renamed entity is
3469 -- itself a renaming.
3470
3471 if Present (Alias (Old_S)) then
3472 Set_Alias (New_S, Alias (Old_S));
3473 else
3474 Set_Alias (New_S, Old_S);
3475 end if;
3476
3477 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
3478 -- renaming as body, since the entity in this case is not an
3479 -- intrinsic (it calls an intrinsic, but we have a real body for
3480 -- this call, and it is in this body that the required intrinsic
3481 -- processing will take place).
3482
3483 -- Also, if this is a renaming of inequality, the renamed operator
3484 -- is intrinsic, but what matters is the corresponding equality
3485 -- operator, which may be user-defined.
3486
3487 Set_Is_Intrinsic_Subprogram
3488 (New_S,
3489 Is_Intrinsic_Subprogram (Old_S)
3490 and then
3491 (Chars (Old_S) /= Name_Op_Ne
3492 or else Ekind (Old_S) = E_Operator
3493 or else Is_Intrinsic_Subprogram
3494 (Corresponding_Equality (Old_S))));
3495
3496 if Ekind (Alias (New_S)) = E_Operator then
3497 Set_Has_Delayed_Freeze (New_S, False);
3498 end if;
3499
3500 -- If the renaming corresponds to an association for an abstract
3501 -- formal subprogram, then various attributes must be set to
3502 -- indicate that the renaming is an abstract dispatching operation
3503 -- with a controlling type.
3504
3505 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
3506
3507 -- Mark the renaming as abstract here, so Find_Dispatching_Type
3508 -- see it as corresponding to a generic association for a
3509 -- formal abstract subprogram
3510
3511 Set_Is_Abstract_Subprogram (New_S);
3512
3513 declare
3514 New_S_Ctrl_Type : constant Entity_Id :=
3515 Find_Dispatching_Type (New_S);
3516 Old_S_Ctrl_Type : constant Entity_Id :=
3517 Find_Dispatching_Type (Old_S);
3518
3519 begin
3520
3521 -- The actual must match the (instance of the) formal,
3522 -- and must be a controlling type.
3523
3524 if Old_S_Ctrl_Type /= New_S_Ctrl_Type
3525 or else No (New_S_Ctrl_Type)
3526 then
3527 if No (New_S_Ctrl_Type) then
3528 Error_Msg_N
3529 ("actual must be dispatching subprogram", Nam);
3530 else
3531 Error_Msg_NE
3532 ("actual must be dispatching subprogram for type&",
3533 Nam, New_S_Ctrl_Type);
3534 end if;
3535
3536 else
3537 Set_Is_Dispatching_Operation (New_S);
3538 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
3539
3540 -- If the actual in the formal subprogram is itself a
3541 -- formal abstract subprogram association, there's no
3542 -- dispatch table component or position to inherit.
3543
3544 if Present (DTC_Entity (Old_S)) then
3545 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
3546 Set_DT_Position_Value (New_S, DT_Position (Old_S));
3547 end if;
3548 end if;
3549 end;
3550 end if;
3551 end if;
3552
3553 if Is_Actual then
3554 null;
3555
3556 -- The following is illegal, because F hides whatever other F may
3557 -- be around:
3558 -- function F (...) renames F;
3559
3560 elsif Old_S = New_S
3561 or else (Nkind (Nam) /= N_Expanded_Name
3562 and then Chars (Old_S) = Chars (New_S))
3563 then
3564 Error_Msg_N ("subprogram cannot rename itself", N);
3565
3566 -- This is illegal even if we use a selector:
3567 -- function F (...) renames Pkg.F;
3568 -- because F is still hidden.
3569
3570 elsif Nkind (Nam) = N_Expanded_Name
3571 and then Entity (Prefix (Nam)) = Current_Scope
3572 and then Chars (Selector_Name (Nam)) = Chars (New_S)
3573 then
3574 -- This is an error, but we overlook the error and accept the
3575 -- renaming if the special Overriding_Renamings mode is in effect.
3576
3577 if not Overriding_Renamings then
3578 Error_Msg_NE
3579 ("implicit operation& is not visible (RM 8.3 (15))",
3580 Nam, Old_S);
3581 end if;
3582 end if;
3583
3584 Set_Convention (New_S, Convention (Old_S));
3585
3586 if Is_Abstract_Subprogram (Old_S) then
3587 if Present (Rename_Spec) then
3588 Error_Msg_N
3589 ("a renaming-as-body cannot rename an abstract subprogram",
3590 N);
3591 Set_Has_Completion (Rename_Spec);
3592 else
3593 Set_Is_Abstract_Subprogram (New_S);
3594 end if;
3595 end if;
3596
3597 Check_Library_Unit_Renaming (N, Old_S);
3598
3599 -- Pathological case: procedure renames entry in the scope of its
3600 -- task. Entry is given by simple name, but body must be built for
3601 -- procedure. Of course if called it will deadlock.
3602
3603 if Ekind (Old_S) = E_Entry then
3604 Set_Has_Completion (New_S, False);
3605 Set_Alias (New_S, Empty);
3606 end if;
3607
3608 -- Do not freeze the renaming nor the renamed entity when the context
3609 -- is an enclosing generic. Freezing is an expansion activity, and in
3610 -- addition the renamed entity may depend on the generic formals of
3611 -- the enclosing generic.
3612
3613 if Is_Actual and not Inside_A_Generic then
3614 Freeze_Before (N, Old_S);
3615 Freeze_Actual_Profile;
3616 Set_Has_Delayed_Freeze (New_S, False);
3617 Freeze_Before (N, New_S);
3618
3619 -- An abstract subprogram is only allowed as an actual in the case
3620 -- where the formal subprogram is also abstract.
3621
3622 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
3623 and then Is_Abstract_Subprogram (Old_S)
3624 and then not Is_Abstract_Subprogram (Formal_Spec)
3625 then
3626 Error_Msg_N
3627 ("abstract subprogram not allowed as generic actual", Nam);
3628 end if;
3629 end if;
3630
3631 else
3632 -- A common error is to assume that implicit operators for types are
3633 -- defined in Standard, or in the scope of a subtype. In those cases
3634 -- where the renamed entity is given with an expanded name, it is
3635 -- worth mentioning that operators for the type are not declared in
3636 -- the scope given by the prefix.
3637
3638 if Nkind (Nam) = N_Expanded_Name
3639 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
3640 and then Scope (Entity (Nam)) = Standard_Standard
3641 then
3642 declare
3643 T : constant Entity_Id :=
3644 Base_Type (Etype (First_Formal (New_S)));
3645 begin
3646 Error_Msg_Node_2 := Prefix (Nam);
3647 Error_Msg_NE
3648 ("operator for type& is not declared in&", Prefix (Nam), T);
3649 end;
3650
3651 else
3652 Error_Msg_NE
3653 ("no visible subprogram matches the specification for&",
3654 Spec, New_S);
3655 end if;
3656
3657 if Present (Candidate_Renaming) then
3658 declare
3659 F1 : Entity_Id;
3660 F2 : Entity_Id;
3661 T1 : Entity_Id;
3662
3663 begin
3664 F1 := First_Formal (Candidate_Renaming);
3665 F2 := First_Formal (New_S);
3666 T1 := First_Subtype (Etype (F1));
3667 while Present (F1) and then Present (F2) loop
3668 Next_Formal (F1);
3669 Next_Formal (F2);
3670 end loop;
3671
3672 if Present (F1) and then Present (Default_Value (F1)) then
3673 if Present (Next_Formal (F1)) then
3674 Error_Msg_NE
3675 ("\missing specification for & and other formals with "
3676 & "defaults", Spec, F1);
3677 else
3678 Error_Msg_NE ("\missing specification for &", Spec, F1);
3679 end if;
3680 end if;
3681
3682 if Nkind (Nam) = N_Operator_Symbol
3683 and then From_Default (N)
3684 then
3685 Error_Msg_Node_2 := T1;
3686 Error_Msg_NE
3687 ("default & on & is not directly visible", Nam, Nam);
3688 end if;
3689 end;
3690 end if;
3691 end if;
3692
3693 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
3694 -- controlling access parameters are known non-null for the renamed
3695 -- subprogram. Test also applies to a subprogram instantiation that
3696 -- is dispatching. Test is skipped if some previous error was detected
3697 -- that set Old_S to Any_Id.
3698
3699 if Ada_Version >= Ada_2005
3700 and then Old_S /= Any_Id
3701 and then not Is_Dispatching_Operation (Old_S)
3702 and then Is_Dispatching_Operation (New_S)
3703 then
3704 declare
3705 Old_F : Entity_Id;
3706 New_F : Entity_Id;
3707
3708 begin
3709 Old_F := First_Formal (Old_S);
3710 New_F := First_Formal (New_S);
3711 while Present (Old_F) loop
3712 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
3713 and then Is_Controlling_Formal (New_F)
3714 and then not Can_Never_Be_Null (Old_F)
3715 then
3716 Error_Msg_N ("access parameter is controlling,", New_F);
3717 Error_Msg_NE
3718 ("\corresponding parameter of& must be explicitly null "
3719 & "excluding", New_F, Old_S);
3720 end if;
3721
3722 Next_Formal (Old_F);
3723 Next_Formal (New_F);
3724 end loop;
3725 end;
3726 end if;
3727
3728 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
3729 -- is to warn if an operator is being renamed as a different operator.
3730 -- If the operator is predefined, examine the kind of the entity, not
3731 -- the abbreviated declaration in Standard.
3732
3733 if Comes_From_Source (N)
3734 and then Present (Old_S)
3735 and then (Nkind (Old_S) = N_Defining_Operator_Symbol
3736 or else Ekind (Old_S) = E_Operator)
3737 and then Nkind (New_S) = N_Defining_Operator_Symbol
3738 and then Chars (Old_S) /= Chars (New_S)
3739 then
3740 Error_Msg_NE
3741 ("& is being renamed as a different operator??", N, Old_S);
3742 end if;
3743
3744 -- Check for renaming of obsolescent subprogram
3745
3746 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
3747
3748 -- Another warning or some utility: if the new subprogram as the same
3749 -- name as the old one, the old one is not hidden by an outer homograph,
3750 -- the new one is not a public symbol, and the old one is otherwise
3751 -- directly visible, the renaming is superfluous.
3752
3753 if Chars (Old_S) = Chars (New_S)
3754 and then Comes_From_Source (N)
3755 and then Scope (Old_S) /= Standard_Standard
3756 and then Warn_On_Redundant_Constructs
3757 and then (Is_Immediately_Visible (Old_S)
3758 or else Is_Potentially_Use_Visible (Old_S))
3759 and then Is_Overloadable (Current_Scope)
3760 and then Chars (Current_Scope) /= Chars (Old_S)
3761 then
3762 Error_Msg_N
3763 ("redundant renaming, entity is directly visible?r?", Name (N));
3764 end if;
3765
3766 -- Implementation-defined aspect specifications can appear in a renaming
3767 -- declaration, but not language-defined ones. The call to procedure
3768 -- Analyze_Aspect_Specifications will take care of this error check.
3769
3770 if Has_Aspects (N) then
3771 Analyze_Aspect_Specifications (N, New_S);
3772 end if;
3773
3774 Ada_Version := Save_AV;
3775 Ada_Version_Pragma := Save_AVP;
3776 Ada_Version_Explicit := Save_AV_Exp;
3777
3778 -- In GNATprove mode, the renamings of actual subprograms are replaced
3779 -- with wrapper functions that make it easier to propagate axioms to the
3780 -- points of call within an instance. Wrappers are generated if formal
3781 -- subprogram is subject to axiomatization.
3782
3783 -- The types in the wrapper profiles are obtained from (instances of)
3784 -- the types of the formal subprogram.
3785
3786 if Is_Actual
3787 and then GNATprove_Mode
3788 and then Present (Containing_Package_With_Ext_Axioms (Formal_Spec))
3789 and then not Inside_A_Generic
3790 then
3791 if Ekind (Old_S) = E_Function then
3792 Rewrite (N, Build_Function_Wrapper (Formal_Spec, Old_S));
3793 Analyze (N);
3794
3795 elsif Ekind (Old_S) = E_Operator then
3796 Rewrite (N, Build_Operator_Wrapper (Formal_Spec, Old_S));
3797 Analyze (N);
3798 end if;
3799 end if;
3800
3801 -- Check if we are looking at an Ada 2012 defaulted formal subprogram
3802 -- and mark any use_package_clauses that affect the visibility of the
3803 -- implicit generic actual.
3804
3805 -- Also, we may be looking at an internal renaming of a user-defined
3806 -- subprogram created for a generic formal subprogram association,
3807 -- which will also have to be marked here. This can occur when the
3808 -- corresponding formal subprogram contains references to other generic
3809 -- formals.
3810
3811 if Is_Generic_Actual_Subprogram (New_S)
3812 and then (Is_Intrinsic_Subprogram (New_S)
3813 or else From_Default (N)
3814 or else Nkind (N) = N_Subprogram_Renaming_Declaration)
3815 then
3816 Mark_Use_Clauses (New_S);
3817
3818 -- Handle overloaded subprograms
3819
3820 if Present (Alias (New_S)) then
3821 Mark_Use_Clauses (Alias (New_S));
3822 end if;
3823 end if;
3824 end Analyze_Subprogram_Renaming;
3825
3826 -------------------------
3827 -- Analyze_Use_Package --
3828 -------------------------
3829
3830 -- Resolve the package names in the use clause, and make all the visible
3831 -- entities defined in the package potentially use-visible. If the package
3832 -- is already in use from a previous use clause, its visible entities are
3833 -- already use-visible. In that case, mark the occurrence as a redundant
3834 -- use. If the package is an open scope, i.e. if the use clause occurs
3835 -- within the package itself, ignore it.
3836
3837 procedure Analyze_Use_Package (N : Node_Id; Chain : Boolean := True) is
3838 procedure Analyze_Package_Name (Clause : Node_Id);
3839 -- Perform analysis on a package name from a use_package_clause
3840
3841 procedure Analyze_Package_Name_List (Head_Clause : Node_Id);
3842 -- Similar to Analyze_Package_Name but iterates over all the names
3843 -- in a use clause.
3844
3845 --------------------------
3846 -- Analyze_Package_Name --
3847 --------------------------
3848
3849 procedure Analyze_Package_Name (Clause : Node_Id) is
3850 Pack : constant Node_Id := Name (Clause);
3851 Pref : Node_Id;
3852
3853 begin
3854 pragma Assert (Nkind (Clause) = N_Use_Package_Clause);
3855 Analyze (Pack);
3856
3857 -- Verify that the package standard is not directly named in a
3858 -- use_package_clause.
3859
3860 if Nkind (Parent (Clause)) = N_Compilation_Unit
3861 and then Nkind (Pack) = N_Expanded_Name
3862 then
3863 Pref := Prefix (Pack);
3864
3865 while Nkind (Pref) = N_Expanded_Name loop
3866 Pref := Prefix (Pref);
3867 end loop;
3868
3869 if Entity (Pref) = Standard_Standard then
3870 Error_Msg_N
3871 ("predefined package Standard cannot appear in a context "
3872 & "clause", Pref);
3873 end if;
3874 end if;
3875 end Analyze_Package_Name;
3876
3877 -------------------------------
3878 -- Analyze_Package_Name_List --
3879 -------------------------------
3880
3881 procedure Analyze_Package_Name_List (Head_Clause : Node_Id) is
3882 Curr : Node_Id;
3883
3884 begin
3885 -- Due to the way source use clauses are split during parsing we are
3886 -- forced to simply iterate through all entities in scope until the
3887 -- clause representing the last name in the list is found.
3888
3889 Curr := Head_Clause;
3890 while Present (Curr) loop
3891 Analyze_Package_Name (Curr);
3892
3893 -- Stop iterating over the names in the use clause when we are at
3894 -- the last one.
3895
3896 exit when not More_Ids (Curr) and then Prev_Ids (Curr);
3897 Next (Curr);
3898 end loop;
3899 end Analyze_Package_Name_List;
3900
3901 -- Local variables
3902
3903 Pack : Entity_Id;
3904
3905 -- Start of processing for Analyze_Use_Package
3906
3907 begin
3908 Set_Hidden_By_Use_Clause (N, No_Elist);
3909
3910 -- Use clause not allowed in a spec of a predefined package declaration
3911 -- except that packages whose file name starts a-n are OK (these are
3912 -- children of Ada.Numerics, which are never loaded by Rtsfind).
3913
3914 if Is_Predefined_Unit (Current_Sem_Unit)
3915 and then Get_Name_String
3916 (Unit_File_Name (Current_Sem_Unit)) (1 .. 3) /= "a-n"
3917 and then Nkind (Unit (Cunit (Current_Sem_Unit))) =
3918 N_Package_Declaration
3919 then
3920 Error_Msg_N ("use clause not allowed in predefined spec", N);
3921 end if;
3922
3923 -- Loop through all package names from the original use clause in
3924 -- order to analyze referenced packages. A use_package_clause with only
3925 -- one name does not have More_Ids or Prev_Ids set, while a clause with
3926 -- More_Ids only starts the chain produced by the parser.
3927
3928 if not More_Ids (N) and then not Prev_Ids (N) then
3929 Analyze_Package_Name (N);
3930
3931 elsif More_Ids (N) and then not Prev_Ids (N) then
3932 Analyze_Package_Name_List (N);
3933 end if;
3934
3935 if not Is_Entity_Name (Name (N)) then
3936 Error_Msg_N ("& is not a package", Name (N));
3937
3938 return;
3939 end if;
3940
3941 if Chain then
3942 Chain_Use_Clause (N);
3943 end if;
3944
3945 Pack := Entity (Name (N));
3946
3947 -- There are many cases where scopes are manipulated during analysis, so
3948 -- check that Pack's current use clause has not already been chained
3949 -- before setting its previous use clause.
3950
3951 if Ekind (Pack) = E_Package
3952 and then Present (Current_Use_Clause (Pack))
3953 and then Current_Use_Clause (Pack) /= N
3954 and then No (Prev_Use_Clause (N))
3955 and then Prev_Use_Clause (Current_Use_Clause (Pack)) /= N
3956 then
3957 Set_Prev_Use_Clause (N, Current_Use_Clause (Pack));
3958 end if;
3959
3960 -- Mark all entities as potentially use visible.
3961
3962 if Ekind (Pack) /= E_Package and then Etype (Pack) /= Any_Type then
3963 if Ekind (Pack) = E_Generic_Package then
3964 Error_Msg_N -- CODEFIX
3965 ("a generic package is not allowed in a use clause", Name (N));
3966
3967 elsif Ekind_In (Pack, E_Generic_Function, E_Generic_Package)
3968 then
3969 Error_Msg_N -- CODEFIX
3970 ("a generic subprogram is not allowed in a use clause",
3971 Name (N));
3972
3973 elsif Ekind_In (Pack, E_Function, E_Procedure, E_Operator) then
3974 Error_Msg_N -- CODEFIX
3975 ("a subprogram is not allowed in a use clause", Name (N));
3976
3977 else
3978 Error_Msg_N ("& is not allowed in a use clause", Name (N));
3979 end if;
3980
3981 else
3982 if Nkind (Parent (N)) = N_Compilation_Unit then
3983 Check_In_Previous_With_Clause (N, Name (N));
3984 end if;
3985
3986 Use_One_Package (N, Name (N));
3987 end if;
3988
3989 Mark_Ghost_Clause (N);
3990 end Analyze_Use_Package;
3991
3992 ----------------------
3993 -- Analyze_Use_Type --
3994 ----------------------
3995
3996 procedure Analyze_Use_Type (N : Node_Id; Chain : Boolean := True) is
3997 E : Entity_Id;
3998 Id : Node_Id;
3999
4000 begin
4001 Set_Hidden_By_Use_Clause (N, No_Elist);
4002
4003 -- Chain clause to list of use clauses in current scope when flagged
4004
4005 if Chain then
4006 Chain_Use_Clause (N);
4007 end if;
4008
4009 -- Obtain the base type of the type denoted within the use_type_clause's
4010 -- subtype mark.
4011
4012 Id := Subtype_Mark (N);
4013 Find_Type (Id);
4014 E := Base_Type (Entity (Id));
4015
4016 -- There are many cases where a use_type_clause may be reanalyzed due to
4017 -- manipulation of the scope stack so we much guard against those cases
4018 -- here, otherwise, we must add the new use_type_clause to the previous
4019 -- use_type_clause chain in order to mark redundant use_type_clauses as
4020 -- used. When the redundant use-type clauses appear in a parent unit and
4021 -- a child unit we must prevent a circularity in the chain that would
4022 -- otherwise result from the separate steps of analysis and installation
4023 -- of the parent context.
4024
4025 if Present (Current_Use_Clause (E))
4026 and then Current_Use_Clause (E) /= N
4027 and then Prev_Use_Clause (Current_Use_Clause (E)) /= N
4028 and then No (Prev_Use_Clause (N))
4029 then
4030 Set_Prev_Use_Clause (N, Current_Use_Clause (E));
4031 end if;
4032
4033 -- If the Used_Operations list is already initialized, the clause has
4034 -- been analyzed previously, and it is being reinstalled, for example
4035 -- when the clause appears in a package spec and we are compiling the
4036 -- corresponding package body. In that case, make the entities on the
4037 -- existing list use_visible, and mark the corresponding types In_Use.
4038
4039 if Present (Used_Operations (N)) then
4040 declare
4041 Elmt : Elmt_Id;
4042
4043 begin
4044 Use_One_Type (Subtype_Mark (N), Installed => True);
4045
4046 Elmt := First_Elmt (Used_Operations (N));
4047 while Present (Elmt) loop
4048 Set_Is_Potentially_Use_Visible (Node (Elmt));
4049 Next_Elmt (Elmt);
4050 end loop;
4051 end;
4052
4053 return;
4054 end if;
4055
4056 -- Otherwise, create new list and attach to it the operations that are
4057 -- made use-visible by the clause.
4058
4059 Set_Used_Operations (N, New_Elmt_List);
4060 E := Entity (Id);
4061
4062 if E /= Any_Type then
4063 Use_One_Type (Id);
4064
4065 if Nkind (Parent (N)) = N_Compilation_Unit then
4066 if Nkind (Id) = N_Identifier then
4067 Error_Msg_N ("type is not directly visible", Id);
4068
4069 elsif Is_Child_Unit (Scope (E))
4070 and then Scope (E) /= System_Aux_Id
4071 then
4072 Check_In_Previous_With_Clause (N, Prefix (Id));
4073 end if;
4074 end if;
4075
4076 else
4077 -- If the use_type_clause appears in a compilation unit context,
4078 -- check whether it comes from a unit that may appear in a
4079 -- limited_with_clause, for a better error message.
4080
4081 if Nkind (Parent (N)) = N_Compilation_Unit
4082 and then Nkind (Id) /= N_Identifier
4083 then
4084 declare
4085 Item : Node_Id;
4086 Pref : Node_Id;
4087
4088 function Mentioned (Nam : Node_Id) return Boolean;
4089 -- Check whether the prefix of expanded name for the type
4090 -- appears in the prefix of some limited_with_clause.
4091
4092 ---------------
4093 -- Mentioned --
4094 ---------------
4095
4096 function Mentioned (Nam : Node_Id) return Boolean is
4097 begin
4098 return Nkind (Name (Item)) = N_Selected_Component
4099 and then Chars (Prefix (Name (Item))) = Chars (Nam);
4100 end Mentioned;
4101
4102 begin
4103 Pref := Prefix (Id);
4104 Item := First (Context_Items (Parent (N)));
4105 while Present (Item) and then Item /= N loop
4106 if Nkind (Item) = N_With_Clause
4107 and then Limited_Present (Item)
4108 and then Mentioned (Pref)
4109 then
4110 Change_Error_Text
4111 (Get_Msg_Id, "premature usage of incomplete type");
4112 end if;
4113
4114 Next (Item);
4115 end loop;
4116 end;
4117 end if;
4118 end if;
4119
4120 Mark_Ghost_Clause (N);
4121 end Analyze_Use_Type;
4122
4123 ------------------------
4124 -- Attribute_Renaming --
4125 ------------------------
4126
4127 procedure Attribute_Renaming (N : Node_Id) is
4128 Loc : constant Source_Ptr := Sloc (N);
4129 Nam : constant Node_Id := Name (N);
4130 Spec : constant Node_Id := Specification (N);
4131 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
4132 Aname : constant Name_Id := Attribute_Name (Nam);
4133
4134 Form_Num : Nat := 0;
4135 Expr_List : List_Id := No_List;
4136
4137 Attr_Node : Node_Id;
4138 Body_Node : Node_Id;
4139 Param_Spec : Node_Id;
4140
4141 begin
4142 Generate_Definition (New_S);
4143
4144 -- This procedure is called in the context of subprogram renaming, and
4145 -- thus the attribute must be one that is a subprogram. All of those
4146 -- have at least one formal parameter, with the exceptions of the GNAT
4147 -- attribute 'Img, which GNAT treats as renameable.
4148
4149 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
4150 if Aname /= Name_Img then
4151 Error_Msg_N
4152 ("subprogram renaming an attribute must have formals", N);
4153 return;
4154 end if;
4155
4156 else
4157 Param_Spec := First (Parameter_Specifications (Spec));
4158 while Present (Param_Spec) loop
4159 Form_Num := Form_Num + 1;
4160
4161 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4162 Find_Type (Parameter_Type (Param_Spec));
4163
4164 -- The profile of the new entity denotes the base type (s) of
4165 -- the types given in the specification. For access parameters
4166 -- there are no subtypes involved.
4167
4168 Rewrite (Parameter_Type (Param_Spec),
4169 New_Occurrence_Of
4170 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
4171 end if;
4172
4173 if No (Expr_List) then
4174 Expr_List := New_List;
4175 end if;
4176
4177 Append_To (Expr_List,
4178 Make_Identifier (Loc,
4179 Chars => Chars (Defining_Identifier (Param_Spec))));
4180
4181 -- The expressions in the attribute reference are not freeze
4182 -- points. Neither is the attribute as a whole, see below.
4183
4184 Set_Must_Not_Freeze (Last (Expr_List));
4185 Next (Param_Spec);
4186 end loop;
4187 end if;
4188
4189 -- Immediate error if too many formals. Other mismatches in number or
4190 -- types of parameters are detected when we analyze the body of the
4191 -- subprogram that we construct.
4192
4193 if Form_Num > 2 then
4194 Error_Msg_N ("too many formals for attribute", N);
4195
4196 -- Error if the attribute reference has expressions that look like
4197 -- formal parameters.
4198
4199 elsif Present (Expressions (Nam)) then
4200 Error_Msg_N ("illegal expressions in attribute reference", Nam);
4201
4202 elsif
4203 Nam_In (Aname, Name_Compose, Name_Exponent, Name_Leading_Part,
4204 Name_Pos, Name_Round, Name_Scaling,
4205 Name_Val)
4206 then
4207 if Nkind (N) = N_Subprogram_Renaming_Declaration
4208 and then Present (Corresponding_Formal_Spec (N))
4209 then
4210 Error_Msg_N
4211 ("generic actual cannot be attribute involving universal type",
4212 Nam);
4213 else
4214 Error_Msg_N
4215 ("attribute involving a universal type cannot be renamed",
4216 Nam);
4217 end if;
4218 end if;
4219
4220 -- Rewrite attribute node to have a list of expressions corresponding to
4221 -- the subprogram formals. A renaming declaration is not a freeze point,
4222 -- and the analysis of the attribute reference should not freeze the
4223 -- type of the prefix. We use the original node in the renaming so that
4224 -- its source location is preserved, and checks on stream attributes are
4225 -- properly applied.
4226
4227 Attr_Node := Relocate_Node (Nam);
4228 Set_Expressions (Attr_Node, Expr_List);
4229
4230 Set_Must_Not_Freeze (Attr_Node);
4231 Set_Must_Not_Freeze (Prefix (Nam));
4232
4233 -- Case of renaming a function
4234
4235 if Nkind (Spec) = N_Function_Specification then
4236 if Is_Procedure_Attribute_Name (Aname) then
4237 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
4238 return;
4239 end if;
4240
4241 Find_Type (Result_Definition (Spec));
4242 Rewrite (Result_Definition (Spec),
4243 New_Occurrence_Of
4244 (Base_Type (Entity (Result_Definition (Spec))), Loc));
4245
4246 Body_Node :=
4247 Make_Subprogram_Body (Loc,
4248 Specification => Spec,
4249 Declarations => New_List,
4250 Handled_Statement_Sequence =>
4251 Make_Handled_Sequence_Of_Statements (Loc,
4252 Statements => New_List (
4253 Make_Simple_Return_Statement (Loc,
4254 Expression => Attr_Node))));
4255
4256 -- Case of renaming a procedure
4257
4258 else
4259 if not Is_Procedure_Attribute_Name (Aname) then
4260 Error_Msg_N ("attribute can only be renamed as function", Nam);
4261 return;
4262 end if;
4263
4264 Body_Node :=
4265 Make_Subprogram_Body (Loc,
4266 Specification => Spec,
4267 Declarations => New_List,
4268 Handled_Statement_Sequence =>
4269 Make_Handled_Sequence_Of_Statements (Loc,
4270 Statements => New_List (Attr_Node)));
4271 end if;
4272
4273 -- Signal the ABE mechanism that the generated subprogram body has not
4274 -- ABE ramifications.
4275
4276 Set_Was_Attribute_Reference (Body_Node);
4277
4278 -- In case of tagged types we add the body of the generated function to
4279 -- the freezing actions of the type (because in the general case such
4280 -- type is still not frozen). We exclude from this processing generic
4281 -- formal subprograms found in instantiations.
4282
4283 -- We must exclude restricted run-time libraries because
4284 -- entity AST_Handler is defined in package System.Aux_Dec which is not
4285 -- available in those platforms. Note that we cannot use the function
4286 -- Restricted_Profile (instead of Configurable_Run_Time_Mode) because
4287 -- the ZFP run-time library is not defined as a profile, and we do not
4288 -- want to deal with AST_Handler in ZFP mode.
4289
4290 if not Configurable_Run_Time_Mode
4291 and then not Present (Corresponding_Formal_Spec (N))
4292 and then Etype (Nam) /= RTE (RE_AST_Handler)
4293 then
4294 declare
4295 P : constant Node_Id := Prefix (Nam);
4296
4297 begin
4298 -- The prefix of 'Img is an object that is evaluated for each call
4299 -- of the function that renames it.
4300
4301 if Aname = Name_Img then
4302 Preanalyze_And_Resolve (P);
4303
4304 -- For all other attribute renamings, the prefix is a subtype
4305
4306 else
4307 Find_Type (P);
4308 end if;
4309
4310 -- If the target type is not yet frozen, add the body to the
4311 -- actions to be elaborated at freeze time.
4312
4313 if Is_Tagged_Type (Etype (P))
4314 and then In_Open_Scopes (Scope (Etype (P)))
4315 then
4316 Ensure_Freeze_Node (Etype (P));
4317 Append_Freeze_Action (Etype (P), Body_Node);
4318 else
4319 Rewrite (N, Body_Node);
4320 Analyze (N);
4321 Set_Etype (New_S, Base_Type (Etype (New_S)));
4322 end if;
4323 end;
4324
4325 -- Generic formal subprograms or AST_Handler renaming
4326
4327 else
4328 Rewrite (N, Body_Node);
4329 Analyze (N);
4330 Set_Etype (New_S, Base_Type (Etype (New_S)));
4331 end if;
4332
4333 if Is_Compilation_Unit (New_S) then
4334 Error_Msg_N
4335 ("a library unit can only rename another library unit", N);
4336 end if;
4337
4338 -- We suppress elaboration warnings for the resulting entity, since
4339 -- clearly they are not needed, and more particularly, in the case
4340 -- of a generic formal subprogram, the resulting entity can appear
4341 -- after the instantiation itself, and thus look like a bogus case
4342 -- of access before elaboration.
4343
4344 if Legacy_Elaboration_Checks then
4345 Set_Suppress_Elaboration_Warnings (New_S);
4346 end if;
4347 end Attribute_Renaming;
4348
4349 ----------------------
4350 -- Chain_Use_Clause --
4351 ----------------------
4352
4353 procedure Chain_Use_Clause (N : Node_Id) is
4354 Level : Int := Scope_Stack.Last;
4355 Pack : Entity_Id;
4356
4357 begin
4358 -- Common case
4359
4360 if not Is_Compilation_Unit (Current_Scope)
4361 or else not Is_Child_Unit (Current_Scope)
4362 then
4363 null;
4364
4365 -- Common case for compilation unit
4366
4367 elsif Defining_Entity (Parent (N)) = Current_Scope then
4368 null;
4369
4370 else
4371 -- If declaration appears in some other scope, it must be in some
4372 -- parent unit when compiling a child.
4373
4374 Pack := Defining_Entity (Parent (N));
4375
4376 if not In_Open_Scopes (Pack) then
4377 null;
4378
4379 -- If the use clause appears in an ancestor and we are in the
4380 -- private part of the immediate parent, the use clauses are
4381 -- already installed.
4382
4383 elsif Pack /= Scope (Current_Scope)
4384 and then In_Private_Part (Scope (Current_Scope))
4385 then
4386 null;
4387
4388 else
4389 -- Find entry for parent unit in scope stack
4390
4391 while Scope_Stack.Table (Level).Entity /= Pack loop
4392 Level := Level - 1;
4393 end loop;
4394 end if;
4395 end if;
4396
4397 Set_Next_Use_Clause (N,
4398 Scope_Stack.Table (Level).First_Use_Clause);
4399 Scope_Stack.Table (Level).First_Use_Clause := N;
4400 end Chain_Use_Clause;
4401
4402 ---------------------------
4403 -- Check_Frozen_Renaming --
4404 ---------------------------
4405
4406 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
4407 B_Node : Node_Id;
4408 Old_S : Entity_Id;
4409
4410 begin
4411 if Is_Frozen (Subp) and then not Has_Completion (Subp) then
4412 B_Node :=
4413 Build_Renamed_Body
4414 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
4415
4416 if Is_Entity_Name (Name (N)) then
4417 Old_S := Entity (Name (N));
4418
4419 if not Is_Frozen (Old_S)
4420 and then Operating_Mode /= Check_Semantics
4421 then
4422 Append_Freeze_Action (Old_S, B_Node);
4423 else
4424 Insert_After (N, B_Node);
4425 Analyze (B_Node);
4426 end if;
4427
4428 if Is_Intrinsic_Subprogram (Old_S)
4429 and then not In_Instance
4430 and then not Relaxed_RM_Semantics
4431 then
4432 Error_Msg_N
4433 ("subprogram used in renaming_as_body cannot be intrinsic",
4434 Name (N));
4435 end if;
4436
4437 else
4438 Insert_After (N, B_Node);
4439 Analyze (B_Node);
4440 end if;
4441 end if;
4442 end Check_Frozen_Renaming;
4443
4444 -------------------------------
4445 -- Set_Entity_Or_Discriminal --
4446 -------------------------------
4447
4448 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
4449 P : Node_Id;
4450
4451 begin
4452 -- If the entity is not a discriminant, or else expansion is disabled,
4453 -- simply set the entity.
4454
4455 if not In_Spec_Expression
4456 or else Ekind (E) /= E_Discriminant
4457 or else Inside_A_Generic
4458 then
4459 Set_Entity_With_Checks (N, E);
4460
4461 -- The replacement of a discriminant by the corresponding discriminal
4462 -- is not done for a task discriminant that appears in a default
4463 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4464 -- for details on their handling.
4465
4466 elsif Is_Concurrent_Type (Scope (E)) then
4467 P := Parent (N);
4468 while Present (P)
4469 and then not Nkind_In (P, N_Parameter_Specification,
4470 N_Component_Declaration)
4471 loop
4472 P := Parent (P);
4473 end loop;
4474
4475 if Present (P)
4476 and then Nkind (P) = N_Parameter_Specification
4477 then
4478 null;
4479
4480 else
4481 Set_Entity (N, Discriminal (E));
4482 end if;
4483
4484 -- Otherwise, this is a discriminant in a context in which
4485 -- it is a reference to the corresponding parameter of the
4486 -- init proc for the enclosing type.
4487
4488 else
4489 Set_Entity (N, Discriminal (E));
4490 end if;
4491 end Set_Entity_Or_Discriminal;
4492
4493 -----------------------------------
4494 -- Check_In_Previous_With_Clause --
4495 -----------------------------------
4496
4497 procedure Check_In_Previous_With_Clause
4498 (N : Node_Id;
4499 Nam : Entity_Id)
4500 is
4501 Pack : constant Entity_Id := Entity (Original_Node (Nam));
4502 Item : Node_Id;
4503 Par : Node_Id;
4504
4505 begin
4506 Item := First (Context_Items (Parent (N)));
4507 while Present (Item) and then Item /= N loop
4508 if Nkind (Item) = N_With_Clause
4509
4510 -- Protect the frontend against previous critical errors
4511
4512 and then Nkind (Name (Item)) /= N_Selected_Component
4513 and then Entity (Name (Item)) = Pack
4514 then
4515 Par := Nam;
4516
4517 -- Find root library unit in with_clause
4518
4519 while Nkind (Par) = N_Expanded_Name loop
4520 Par := Prefix (Par);
4521 end loop;
4522
4523 if Is_Child_Unit (Entity (Original_Node (Par))) then
4524 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
4525 else
4526 return;
4527 end if;
4528 end if;
4529
4530 Next (Item);
4531 end loop;
4532
4533 -- On exit, package is not mentioned in a previous with_clause.
4534 -- Check if its prefix is.
4535
4536 if Nkind (Nam) = N_Expanded_Name then
4537 Check_In_Previous_With_Clause (N, Prefix (Nam));
4538
4539 elsif Pack /= Any_Id then
4540 Error_Msg_NE ("& is not visible", Nam, Pack);
4541 end if;
4542 end Check_In_Previous_With_Clause;
4543
4544 ---------------------------------
4545 -- Check_Library_Unit_Renaming --
4546 ---------------------------------
4547
4548 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
4549 New_E : Entity_Id;
4550
4551 begin
4552 if Nkind (Parent (N)) /= N_Compilation_Unit then
4553 return;
4554
4555 -- Check for library unit. Note that we used to check for the scope
4556 -- being Standard here, but that was wrong for Standard itself.
4557
4558 elsif not Is_Compilation_Unit (Old_E)
4559 and then not Is_Child_Unit (Old_E)
4560 then
4561 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4562
4563 -- Entities defined in Standard (operators and boolean literals) cannot
4564 -- be renamed as library units.
4565
4566 elsif Scope (Old_E) = Standard_Standard
4567 and then Sloc (Old_E) = Standard_Location
4568 then
4569 Error_Msg_N ("renamed unit must be a library unit", Name (N));
4570
4571 elsif Present (Parent_Spec (N))
4572 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
4573 and then not Is_Child_Unit (Old_E)
4574 then
4575 Error_Msg_N
4576 ("renamed unit must be a child unit of generic parent", Name (N));
4577
4578 elsif Nkind (N) in N_Generic_Renaming_Declaration
4579 and then Nkind (Name (N)) = N_Expanded_Name
4580 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
4581 and then Is_Generic_Unit (Old_E)
4582 then
4583 Error_Msg_N
4584 ("renamed generic unit must be a library unit", Name (N));
4585
4586 elsif Is_Package_Or_Generic_Package (Old_E) then
4587
4588 -- Inherit categorization flags
4589
4590 New_E := Defining_Entity (N);
4591 Set_Is_Pure (New_E, Is_Pure (Old_E));
4592 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
4593 Set_Is_Remote_Call_Interface (New_E,
4594 Is_Remote_Call_Interface (Old_E));
4595 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
4596 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
4597 end if;
4598 end Check_Library_Unit_Renaming;
4599
4600 ------------------------
4601 -- Enclosing_Instance --
4602 ------------------------
4603
4604 function Enclosing_Instance return Entity_Id is
4605 S : Entity_Id;
4606
4607 begin
4608 if not Is_Generic_Instance (Current_Scope) then
4609 return Empty;
4610 end if;
4611
4612 S := Scope (Current_Scope);
4613 while S /= Standard_Standard loop
4614 if Is_Generic_Instance (S) then
4615 return S;
4616 end if;
4617
4618 S := Scope (S);
4619 end loop;
4620
4621 return Empty;
4622 end Enclosing_Instance;
4623
4624 ---------------
4625 -- End_Scope --
4626 ---------------
4627
4628 procedure End_Scope is
4629 Id : Entity_Id;
4630 Prev : Entity_Id;
4631 Outer : Entity_Id;
4632
4633 begin
4634 Id := First_Entity (Current_Scope);
4635 while Present (Id) loop
4636 -- An entity in the current scope is not necessarily the first one
4637 -- on its homonym chain. Find its predecessor if any,
4638 -- If it is an internal entity, it will not be in the visibility
4639 -- chain altogether, and there is nothing to unchain.
4640
4641 if Id /= Current_Entity (Id) then
4642 Prev := Current_Entity (Id);
4643 while Present (Prev)
4644 and then Present (Homonym (Prev))
4645 and then Homonym (Prev) /= Id
4646 loop
4647 Prev := Homonym (Prev);
4648 end loop;
4649
4650 -- Skip to end of loop if Id is not in the visibility chain
4651
4652 if No (Prev) or else Homonym (Prev) /= Id then
4653 goto Next_Ent;
4654 end if;
4655
4656 else
4657 Prev := Empty;
4658 end if;
4659
4660 Set_Is_Immediately_Visible (Id, False);
4661
4662 Outer := Homonym (Id);
4663 while Present (Outer) and then Scope (Outer) = Current_Scope loop
4664 Outer := Homonym (Outer);
4665 end loop;
4666
4667 -- Reset homonym link of other entities, but do not modify link
4668 -- between entities in current scope, so that the back-end can have
4669 -- a proper count of local overloadings.
4670
4671 if No (Prev) then
4672 Set_Name_Entity_Id (Chars (Id), Outer);
4673
4674 elsif Scope (Prev) /= Scope (Id) then
4675 Set_Homonym (Prev, Outer);
4676 end if;
4677
4678 <<Next_Ent>>
4679 Next_Entity (Id);
4680 end loop;
4681
4682 -- If the scope generated freeze actions, place them before the
4683 -- current declaration and analyze them. Type declarations and
4684 -- the bodies of initialization procedures can generate such nodes.
4685 -- We follow the parent chain until we reach a list node, which is
4686 -- the enclosing list of declarations. If the list appears within
4687 -- a protected definition, move freeze nodes outside the protected
4688 -- type altogether.
4689
4690 if Present
4691 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
4692 then
4693 declare
4694 Decl : Node_Id;
4695 L : constant List_Id := Scope_Stack.Table
4696 (Scope_Stack.Last).Pending_Freeze_Actions;
4697
4698 begin
4699 if Is_Itype (Current_Scope) then
4700 Decl := Associated_Node_For_Itype (Current_Scope);
4701 else
4702 Decl := Parent (Current_Scope);
4703 end if;
4704
4705 Pop_Scope;
4706
4707 while not (Is_List_Member (Decl))
4708 or else Nkind_In (Parent (Decl), N_Protected_Definition,
4709 N_Task_Definition)
4710 loop
4711 Decl := Parent (Decl);
4712 end loop;
4713
4714 Insert_List_Before_And_Analyze (Decl, L);
4715 end;
4716
4717 else
4718 Pop_Scope;
4719 end if;
4720 end End_Scope;
4721
4722 ---------------------
4723 -- End_Use_Clauses --
4724 ---------------------
4725
4726 procedure End_Use_Clauses (Clause : Node_Id) is
4727 U : Node_Id;
4728
4729 begin
4730 -- Remove use_type_clauses first, because they affect the visibility of
4731 -- operators in subsequent used packages.
4732
4733 U := Clause;
4734 while Present (U) loop
4735 if Nkind (U) = N_Use_Type_Clause then
4736 End_Use_Type (U);
4737 end if;
4738
4739 Next_Use_Clause (U);
4740 end loop;
4741
4742 U := Clause;
4743 while Present (U) loop
4744 if Nkind (U) = N_Use_Package_Clause then
4745 End_Use_Package (U);
4746 end if;
4747
4748 Next_Use_Clause (U);
4749 end loop;
4750 end End_Use_Clauses;
4751
4752 ---------------------
4753 -- End_Use_Package --
4754 ---------------------
4755
4756 procedure End_Use_Package (N : Node_Id) is
4757 Pack : Entity_Id;
4758 Pack_Name : Node_Id;
4759 Id : Entity_Id;
4760 Elmt : Elmt_Id;
4761
4762 function Is_Primitive_Operator_In_Use
4763 (Op : Entity_Id;
4764 F : Entity_Id) return Boolean;
4765 -- Check whether Op is a primitive operator of a use-visible type
4766
4767 ----------------------------------
4768 -- Is_Primitive_Operator_In_Use --
4769 ----------------------------------
4770
4771 function Is_Primitive_Operator_In_Use
4772 (Op : Entity_Id;
4773 F : Entity_Id) return Boolean
4774 is
4775 T : constant Entity_Id := Base_Type (Etype (F));
4776 begin
4777 return In_Use (T) and then Scope (T) = Scope (Op);
4778 end Is_Primitive_Operator_In_Use;
4779
4780 -- Start of processing for End_Use_Package
4781
4782 begin
4783 Pack_Name := Name (N);
4784
4785 -- Test that Pack_Name actually denotes a package before processing
4786
4787 if Is_Entity_Name (Pack_Name)
4788 and then Ekind (Entity (Pack_Name)) = E_Package
4789 then
4790 Pack := Entity (Pack_Name);
4791
4792 if In_Open_Scopes (Pack) then
4793 null;
4794
4795 elsif not Redundant_Use (Pack_Name) then
4796 Set_In_Use (Pack, False);
4797 Set_Current_Use_Clause (Pack, Empty);
4798
4799 Id := First_Entity (Pack);
4800 while Present (Id) loop
4801
4802 -- Preserve use-visibility of operators that are primitive
4803 -- operators of a type that is use-visible through an active
4804 -- use_type_clause.
4805
4806 if Nkind (Id) = N_Defining_Operator_Symbol
4807 and then
4808 (Is_Primitive_Operator_In_Use (Id, First_Formal (Id))
4809 or else
4810 (Present (Next_Formal (First_Formal (Id)))
4811 and then
4812 Is_Primitive_Operator_In_Use
4813 (Id, Next_Formal (First_Formal (Id)))))
4814 then
4815 null;
4816 else
4817 Set_Is_Potentially_Use_Visible (Id, False);
4818 end if;
4819
4820 if Is_Private_Type (Id)
4821 and then Present (Full_View (Id))
4822 then
4823 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4824 end if;
4825
4826 Next_Entity (Id);
4827 end loop;
4828
4829 if Present (Renamed_Object (Pack)) then
4830 Set_In_Use (Renamed_Object (Pack), False);
4831 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
4832 end if;
4833
4834 if Chars (Pack) = Name_System
4835 and then Scope (Pack) = Standard_Standard
4836 and then Present_System_Aux
4837 then
4838 Id := First_Entity (System_Aux_Id);
4839 while Present (Id) loop
4840 Set_Is_Potentially_Use_Visible (Id, False);
4841
4842 if Is_Private_Type (Id)
4843 and then Present (Full_View (Id))
4844 then
4845 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
4846 end if;
4847
4848 Next_Entity (Id);
4849 end loop;
4850
4851 Set_In_Use (System_Aux_Id, False);
4852 end if;
4853 else
4854 Set_Redundant_Use (Pack_Name, False);
4855 end if;
4856 end if;
4857
4858 if Present (Hidden_By_Use_Clause (N)) then
4859 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
4860 while Present (Elmt) loop
4861 declare
4862 E : constant Entity_Id := Node (Elmt);
4863
4864 begin
4865 -- Reset either Use_Visibility or Direct_Visibility, depending
4866 -- on how the entity was hidden by the use clause.
4867
4868 if In_Use (Scope (E))
4869 and then Used_As_Generic_Actual (Scope (E))
4870 then
4871 Set_Is_Potentially_Use_Visible (Node (Elmt));
4872 else
4873 Set_Is_Immediately_Visible (Node (Elmt));
4874 end if;
4875
4876 Next_Elmt (Elmt);
4877 end;
4878 end loop;
4879
4880 Set_Hidden_By_Use_Clause (N, No_Elist);
4881 end if;
4882 end End_Use_Package;
4883
4884 ------------------
4885 -- End_Use_Type --
4886 ------------------
4887
4888 procedure End_Use_Type (N : Node_Id) is
4889 Elmt : Elmt_Id;
4890 Id : Entity_Id;
4891 T : Entity_Id;
4892
4893 -- Start of processing for End_Use_Type
4894
4895 begin
4896 Id := Subtype_Mark (N);
4897
4898 -- A call to Rtsfind may occur while analyzing a use_type_clause, in
4899 -- which case the type marks are not resolved yet, so guard against that
4900 -- here.
4901
4902 if Is_Entity_Name (Id) and then Present (Entity (Id)) then
4903 T := Entity (Id);
4904
4905 if T = Any_Type or else From_Limited_With (T) then
4906 null;
4907
4908 -- Note that the use_type_clause may mention a subtype of the type
4909 -- whose primitive operations have been made visible. Here as
4910 -- elsewhere, it is the base type that matters for visibility.
4911
4912 elsif In_Open_Scopes (Scope (Base_Type (T))) then
4913 null;
4914
4915 elsif not Redundant_Use (Id) then
4916 Set_In_Use (T, False);
4917 Set_In_Use (Base_Type (T), False);
4918 Set_Current_Use_Clause (T, Empty);
4919 Set_Current_Use_Clause (Base_Type (T), Empty);
4920
4921 -- See Use_One_Type for the rationale. This is a bit on the naive
4922 -- side, but should be good enough in practice.
4923
4924 if Is_Tagged_Type (T) then
4925 Set_In_Use (Class_Wide_Type (T), False);
4926 end if;
4927 end if;
4928 end if;
4929
4930 if Is_Empty_Elmt_List (Used_Operations (N)) then
4931 return;
4932
4933 else
4934 Elmt := First_Elmt (Used_Operations (N));
4935 while Present (Elmt) loop
4936 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
4937 Next_Elmt (Elmt);
4938 end loop;
4939 end if;
4940 end End_Use_Type;
4941
4942 --------------------
4943 -- Entity_Of_Unit --
4944 --------------------
4945
4946 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
4947 begin
4948 if Nkind (U) = N_Package_Instantiation and then Analyzed (U) then
4949 return Defining_Entity (Instance_Spec (U));
4950 else
4951 return Defining_Entity (U);
4952 end if;
4953 end Entity_Of_Unit;
4954
4955 ----------------------
4956 -- Find_Direct_Name --
4957 ----------------------
4958
4959 procedure Find_Direct_Name
4960 (N : Node_Id;
4961 Errors_OK : Boolean := True;
4962 Marker_OK : Boolean := True;
4963 Reference_OK : Boolean := True)
4964 is
4965 E : Entity_Id;
4966 E2 : Entity_Id;
4967 Msg : Boolean;
4968
4969 Homonyms : Entity_Id;
4970 -- Saves start of homonym chain
4971
4972 Inst : Entity_Id := Empty;
4973 -- Enclosing instance, if any
4974
4975 Nvis_Entity : Boolean;
4976 -- Set True to indicate that there is at least one entity on the homonym
4977 -- chain which, while not visible, is visible enough from the user point
4978 -- of view to warrant an error message of "not visible" rather than
4979 -- undefined.
4980
4981 Nvis_Is_Private_Subprg : Boolean := False;
4982 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
4983 -- effect concerning library subprograms has been detected. Used to
4984 -- generate the precise error message.
4985
4986 function From_Actual_Package (E : Entity_Id) return Boolean;
4987 -- Returns true if the entity is an actual for a package that is itself
4988 -- an actual for a formal package of the current instance. Such an
4989 -- entity requires special handling because it may be use-visible but
4990 -- hides directly visible entities defined outside the instance, because
4991 -- the corresponding formal did so in the generic.
4992
4993 function Is_Actual_Parameter return Boolean;
4994 -- This function checks if the node N is an identifier that is an actual
4995 -- parameter of a procedure call. If so it returns True, otherwise it
4996 -- return False. The reason for this check is that at this stage we do
4997 -- not know what procedure is being called if the procedure might be
4998 -- overloaded, so it is premature to go setting referenced flags or
4999 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
5000 -- for that processing
5001
5002 function Known_But_Invisible (E : Entity_Id) return Boolean;
5003 -- This function determines whether a reference to the entity E, which
5004 -- is not visible, can reasonably be considered to be known to the
5005 -- writer of the reference. This is a heuristic test, used only for
5006 -- the purposes of figuring out whether we prefer to complain that an
5007 -- entity is undefined or invisible (and identify the declaration of
5008 -- the invisible entity in the latter case). The point here is that we
5009 -- don't want to complain that something is invisible and then point to
5010 -- something entirely mysterious to the writer.
5011
5012 procedure Nvis_Messages;
5013 -- Called if there are no visible entries for N, but there is at least
5014 -- one non-directly visible, or hidden declaration. This procedure
5015 -- outputs an appropriate set of error messages.
5016
5017 procedure Undefined (Nvis : Boolean);
5018 -- This function is called if the current node has no corresponding
5019 -- visible entity or entities. The value set in Msg indicates whether
5020 -- an error message was generated (multiple error messages for the
5021 -- same variable are generally suppressed, see body for details).
5022 -- Msg is True if an error message was generated, False if not. This
5023 -- value is used by the caller to determine whether or not to output
5024 -- additional messages where appropriate. The parameter is set False
5025 -- to get the message "X is undefined", and True to get the message
5026 -- "X is not visible".
5027
5028 -------------------------
5029 -- From_Actual_Package --
5030 -------------------------
5031
5032 function From_Actual_Package (E : Entity_Id) return Boolean is
5033 Scop : constant Entity_Id := Scope (E);
5034 -- Declared scope of candidate entity
5035
5036 function Declared_In_Actual (Pack : Entity_Id) return Boolean;
5037 -- Recursive function that does the work and examines actuals of
5038 -- actual packages of current instance.
5039
5040 ------------------------
5041 -- Declared_In_Actual --
5042 ------------------------
5043
5044 function Declared_In_Actual (Pack : Entity_Id) return Boolean is
5045 Act : Entity_Id;
5046
5047 begin
5048 if No (Associated_Formal_Package (Pack)) then
5049 return False;
5050
5051 else
5052 Act := First_Entity (Pack);
5053 while Present (Act) loop
5054 if Renamed_Object (Pack) = Scop then
5055 return True;
5056
5057 -- Check for end of list of actuals
5058
5059 elsif Ekind (Act) = E_Package
5060 and then Renamed_Object (Act) = Pack
5061 then
5062 return False;
5063
5064 elsif Ekind (Act) = E_Package
5065 and then Declared_In_Actual (Act)
5066 then
5067 return True;
5068 end if;
5069
5070 Next_Entity (Act);
5071 end loop;
5072
5073 return False;
5074 end if;
5075 end Declared_In_Actual;
5076
5077 -- Local variables
5078
5079 Act : Entity_Id;
5080
5081 -- Start of processing for From_Actual_Package
5082
5083 begin
5084 if not In_Instance then
5085 return False;
5086
5087 else
5088 Inst := Current_Scope;
5089 while Present (Inst)
5090 and then Ekind (Inst) /= E_Package
5091 and then not Is_Generic_Instance (Inst)
5092 loop
5093 Inst := Scope (Inst);
5094 end loop;
5095
5096 if No (Inst) then
5097 return False;
5098 end if;
5099
5100 Act := First_Entity (Inst);
5101 while Present (Act) loop
5102 if Ekind (Act) = E_Package
5103 and then Declared_In_Actual (Act)
5104 then
5105 return True;
5106 end if;
5107
5108 Next_Entity (Act);
5109 end loop;
5110
5111 return False;
5112 end if;
5113 end From_Actual_Package;
5114
5115 -------------------------
5116 -- Is_Actual_Parameter --
5117 -------------------------
5118
5119 function Is_Actual_Parameter return Boolean is
5120 begin
5121 return
5122 Nkind (N) = N_Identifier
5123 and then
5124 (Nkind (Parent (N)) = N_Procedure_Call_Statement
5125 or else
5126 (Nkind (Parent (N)) = N_Parameter_Association
5127 and then N = Explicit_Actual_Parameter (Parent (N))
5128 and then Nkind (Parent (Parent (N))) =
5129 N_Procedure_Call_Statement));
5130 end Is_Actual_Parameter;
5131
5132 -------------------------
5133 -- Known_But_Invisible --
5134 -------------------------
5135
5136 function Known_But_Invisible (E : Entity_Id) return Boolean is
5137 Fname : File_Name_Type;
5138
5139 begin
5140 -- Entities in Standard are always considered to be known
5141
5142 if Sloc (E) <= Standard_Location then
5143 return True;
5144
5145 -- An entity that does not come from source is always considered
5146 -- to be unknown, since it is an artifact of code expansion.
5147
5148 elsif not Comes_From_Source (E) then
5149 return False;
5150
5151 -- In gnat internal mode, we consider all entities known. The
5152 -- historical reason behind this discrepancy is not known??? But the
5153 -- only effect is to modify the error message given, so it is not
5154 -- critical. Since it only affects the exact wording of error
5155 -- messages in illegal programs, we do not mention this as an
5156 -- effect of -gnatg, since it is not a language modification.
5157
5158 elsif GNAT_Mode then
5159 return True;
5160 end if;
5161
5162 -- Here we have an entity that is not from package Standard, and
5163 -- which comes from Source. See if it comes from an internal file.
5164
5165 Fname := Unit_File_Name (Get_Source_Unit (E));
5166
5167 -- Case of from internal file
5168
5169 if In_Internal_Unit (E) then
5170
5171 -- Private part entities in internal files are never considered
5172 -- to be known to the writer of normal application code.
5173
5174 if Is_Hidden (E) then
5175 return False;
5176 end if;
5177
5178 -- Entities from System packages other than System and
5179 -- System.Storage_Elements are not considered to be known.
5180 -- System.Auxxxx files are also considered known to the user.
5181
5182 -- Should refine this at some point to generally distinguish
5183 -- between known and unknown internal files ???
5184
5185 Get_Name_String (Fname);
5186
5187 return
5188 Name_Len < 2
5189 or else
5190 Name_Buffer (1 .. 2) /= "s-"
5191 or else
5192 Name_Buffer (3 .. 8) = "stoele"
5193 or else
5194 Name_Buffer (3 .. 5) = "aux";
5195
5196 -- If not an internal file, then entity is definitely known, even if
5197 -- it is in a private part (the message generated will note that it
5198 -- is in a private part).
5199
5200 else
5201 return True;
5202 end if;
5203 end Known_But_Invisible;
5204
5205 -------------------
5206 -- Nvis_Messages --
5207 -------------------
5208
5209 procedure Nvis_Messages is
5210 Comp_Unit : Node_Id;
5211 Ent : Entity_Id;
5212 Found : Boolean := False;
5213 Hidden : Boolean := False;
5214 Item : Node_Id;
5215
5216 begin
5217 if not Errors_OK then
5218 return;
5219 end if;
5220
5221 -- Ada 2005 (AI-262): Generate a precise error concerning the
5222 -- Beaujolais effect that was previously detected
5223
5224 if Nvis_Is_Private_Subprg then
5225
5226 pragma Assert (Nkind (E2) = N_Defining_Identifier
5227 and then Ekind (E2) = E_Function
5228 and then Scope (E2) = Standard_Standard
5229 and then Has_Private_With (E2));
5230
5231 -- Find the sloc corresponding to the private with'ed unit
5232
5233 Comp_Unit := Cunit (Current_Sem_Unit);
5234 Error_Msg_Sloc := No_Location;
5235
5236 Item := First (Context_Items (Comp_Unit));
5237 while Present (Item) loop
5238 if Nkind (Item) = N_With_Clause
5239 and then Private_Present (Item)
5240 and then Entity (Name (Item)) = E2
5241 then
5242 Error_Msg_Sloc := Sloc (Item);
5243 exit;
5244 end if;
5245
5246 Next (Item);
5247 end loop;
5248
5249 pragma Assert (Error_Msg_Sloc /= No_Location);
5250
5251 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
5252 return;
5253 end if;
5254
5255 Undefined (Nvis => True);
5256
5257 if Msg then
5258
5259 -- First loop does hidden declarations
5260
5261 Ent := Homonyms;
5262 while Present (Ent) loop
5263 if Is_Potentially_Use_Visible (Ent) then
5264 if not Hidden then
5265 Error_Msg_N -- CODEFIX
5266 ("multiple use clauses cause hiding!", N);
5267 Hidden := True;
5268 end if;
5269
5270 Error_Msg_Sloc := Sloc (Ent);
5271 Error_Msg_N -- CODEFIX
5272 ("hidden declaration#!", N);
5273 end if;
5274
5275 Ent := Homonym (Ent);
5276 end loop;
5277
5278 -- If we found hidden declarations, then that's enough, don't
5279 -- bother looking for non-visible declarations as well.
5280
5281 if Hidden then
5282 return;
5283 end if;
5284
5285 -- Second loop does non-directly visible declarations
5286
5287 Ent := Homonyms;
5288 while Present (Ent) loop
5289 if not Is_Potentially_Use_Visible (Ent) then
5290
5291 -- Do not bother the user with unknown entities
5292
5293 if not Known_But_Invisible (Ent) then
5294 goto Continue;
5295 end if;
5296
5297 Error_Msg_Sloc := Sloc (Ent);
5298
5299 -- Output message noting that there is a non-visible
5300 -- declaration, distinguishing the private part case.
5301
5302 if Is_Hidden (Ent) then
5303 Error_Msg_N ("non-visible (private) declaration#!", N);
5304
5305 -- If the entity is declared in a generic package, it
5306 -- cannot be visible, so there is no point in adding it
5307 -- to the list of candidates if another homograph from a
5308 -- non-generic package has been seen.
5309
5310 elsif Ekind (Scope (Ent)) = E_Generic_Package
5311 and then Found
5312 then
5313 null;
5314
5315 else
5316 Error_Msg_N -- CODEFIX
5317 ("non-visible declaration#!", N);
5318
5319 if Ekind (Scope (Ent)) /= E_Generic_Package then
5320 Found := True;
5321 end if;
5322
5323 if Is_Compilation_Unit (Ent)
5324 and then
5325 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
5326 then
5327 Error_Msg_Qual_Level := 99;
5328 Error_Msg_NE -- CODEFIX
5329 ("\\missing `WITH &;`", N, Ent);
5330 Error_Msg_Qual_Level := 0;
5331 end if;
5332
5333 if Ekind (Ent) = E_Discriminant
5334 and then Present (Corresponding_Discriminant (Ent))
5335 and then Scope (Corresponding_Discriminant (Ent)) =
5336 Etype (Scope (Ent))
5337 then
5338 Error_Msg_N
5339 ("inherited discriminant not allowed here" &
5340 " (RM 3.8 (12), 3.8.1 (6))!", N);
5341 end if;
5342 end if;
5343
5344 -- Set entity and its containing package as referenced. We
5345 -- can't be sure of this, but this seems a better choice
5346 -- to avoid unused entity messages.
5347
5348 if Comes_From_Source (Ent) then
5349 Set_Referenced (Ent);
5350 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
5351 end if;
5352 end if;
5353
5354 <<Continue>>
5355 Ent := Homonym (Ent);
5356 end loop;
5357 end if;
5358 end Nvis_Messages;
5359
5360 ---------------
5361 -- Undefined --
5362 ---------------
5363
5364 procedure Undefined (Nvis : Boolean) is
5365 Emsg : Error_Msg_Id;
5366
5367 begin
5368 -- We should never find an undefined internal name. If we do, then
5369 -- see if we have previous errors. If so, ignore on the grounds that
5370 -- it is probably a cascaded message (e.g. a block label from a badly
5371 -- formed block). If no previous errors, then we have a real internal
5372 -- error of some kind so raise an exception.
5373
5374 if Is_Internal_Name (Chars (N)) then
5375 if Total_Errors_Detected /= 0 then
5376 return;
5377 else
5378 raise Program_Error;
5379 end if;
5380 end if;
5381
5382 -- A very specialized error check, if the undefined variable is
5383 -- a case tag, and the case type is an enumeration type, check
5384 -- for a possible misspelling, and if so, modify the identifier
5385
5386 -- Named aggregate should also be handled similarly ???
5387
5388 if Errors_OK
5389 and then Nkind (N) = N_Identifier
5390 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
5391 then
5392 declare
5393 Case_Stm : constant Node_Id := Parent (Parent (N));
5394 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
5395
5396 Lit : Node_Id;
5397
5398 begin
5399 if Is_Enumeration_Type (Case_Typ)
5400 and then not Is_Standard_Character_Type (Case_Typ)
5401 then
5402 Lit := First_Literal (Case_Typ);
5403 Get_Name_String (Chars (Lit));
5404
5405 if Chars (Lit) /= Chars (N)
5406 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit))
5407 then
5408 Error_Msg_Node_2 := Lit;
5409 Error_Msg_N -- CODEFIX
5410 ("& is undefined, assume misspelling of &", N);
5411 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
5412 return;
5413 end if;
5414
5415 Next_Literal (Lit);
5416 end if;
5417 end;
5418 end if;
5419
5420 -- Normal processing
5421
5422 Set_Entity (N, Any_Id);
5423 Set_Etype (N, Any_Type);
5424
5425 if Errors_OK then
5426
5427 -- We use the table Urefs to keep track of entities for which we
5428 -- have issued errors for undefined references. Multiple errors
5429 -- for a single name are normally suppressed, however we modify
5430 -- the error message to alert the programmer to this effect.
5431
5432 for J in Urefs.First .. Urefs.Last loop
5433 if Chars (N) = Chars (Urefs.Table (J).Node) then
5434 if Urefs.Table (J).Err /= No_Error_Msg
5435 and then Sloc (N) /= Urefs.Table (J).Loc
5436 then
5437 Error_Msg_Node_1 := Urefs.Table (J).Node;
5438
5439 if Urefs.Table (J).Nvis then
5440 Change_Error_Text (Urefs.Table (J).Err,
5441 "& is not visible (more references follow)");
5442 else
5443 Change_Error_Text (Urefs.Table (J).Err,
5444 "& is undefined (more references follow)");
5445 end if;
5446
5447 Urefs.Table (J).Err := No_Error_Msg;
5448 end if;
5449
5450 -- Although we will set Msg False, and thus suppress the
5451 -- message, we also set Error_Posted True, to avoid any
5452 -- cascaded messages resulting from the undefined reference.
5453
5454 Msg := False;
5455 Set_Error_Posted (N);
5456 return;
5457 end if;
5458 end loop;
5459
5460 -- If entry not found, this is first undefined occurrence
5461
5462 if Nvis then
5463 Error_Msg_N ("& is not visible!", N);
5464 Emsg := Get_Msg_Id;
5465
5466 else
5467 Error_Msg_N ("& is undefined!", N);
5468 Emsg := Get_Msg_Id;
5469
5470 -- A very bizarre special check, if the undefined identifier
5471 -- is Put or Put_Line, then add a special error message (since
5472 -- this is a very common error for beginners to make).
5473
5474 if Nam_In (Chars (N), Name_Put, Name_Put_Line) then
5475 Error_Msg_N -- CODEFIX
5476 ("\\possible missing `WITH Ada.Text_'I'O; " &
5477 "USE Ada.Text_'I'O`!", N);
5478
5479 -- Another special check if N is the prefix of a selected
5480 -- component which is a known unit: add message complaining
5481 -- about missing with for this unit.
5482
5483 elsif Nkind (Parent (N)) = N_Selected_Component
5484 and then N = Prefix (Parent (N))
5485 and then Is_Known_Unit (Parent (N))
5486 then
5487 Error_Msg_Node_2 := Selector_Name (Parent (N));
5488 Error_Msg_N -- CODEFIX
5489 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
5490 end if;
5491
5492 -- Now check for possible misspellings
5493
5494 declare
5495 E : Entity_Id;
5496 Ematch : Entity_Id := Empty;
5497
5498 Last_Name_Id : constant Name_Id :=
5499 Name_Id (Nat (First_Name_Id) +
5500 Name_Entries_Count - 1);
5501
5502 begin
5503 for Nam in First_Name_Id .. Last_Name_Id loop
5504 E := Get_Name_Entity_Id (Nam);
5505
5506 if Present (E)
5507 and then (Is_Immediately_Visible (E)
5508 or else
5509 Is_Potentially_Use_Visible (E))
5510 then
5511 if Is_Bad_Spelling_Of (Chars (N), Nam) then
5512 Ematch := E;
5513 exit;
5514 end if;
5515 end if;
5516 end loop;
5517
5518 if Present (Ematch) then
5519 Error_Msg_NE -- CODEFIX
5520 ("\possible misspelling of&", N, Ematch);
5521 end if;
5522 end;
5523 end if;
5524
5525 -- Make entry in undefined references table unless the full errors
5526 -- switch is set, in which case by refraining from generating the
5527 -- table entry we guarantee that we get an error message for every
5528 -- undefined reference. The entry is not added if we are ignoring
5529 -- errors.
5530
5531 if not All_Errors_Mode and then Ignore_Errors_Enable = 0 then
5532 Urefs.Append (
5533 (Node => N,
5534 Err => Emsg,
5535 Nvis => Nvis,
5536 Loc => Sloc (N)));
5537 end if;
5538
5539 Msg := True;
5540 end if;
5541 end Undefined;
5542
5543 -- Local variables
5544
5545 Nested_Inst : Entity_Id := Empty;
5546 -- The entity of a nested instance which appears within Inst (if any)
5547
5548 -- Start of processing for Find_Direct_Name
5549
5550 begin
5551 -- If the entity pointer is already set, this is an internal node, or
5552 -- a node that is analyzed more than once, after a tree modification.
5553 -- In such a case there is no resolution to perform, just set the type.
5554
5555 if Present (Entity (N)) then
5556 if Is_Type (Entity (N)) then
5557 Set_Etype (N, Entity (N));
5558
5559 else
5560 declare
5561 Entyp : constant Entity_Id := Etype (Entity (N));
5562
5563 begin
5564 -- One special case here. If the Etype field is already set,
5565 -- and references the packed array type corresponding to the
5566 -- etype of the referenced entity, then leave it alone. This
5567 -- happens for trees generated from Exp_Pakd, where expressions
5568 -- can be deliberately "mis-typed" to the packed array type.
5569
5570 if Is_Array_Type (Entyp)
5571 and then Is_Packed (Entyp)
5572 and then Present (Etype (N))
5573 and then Etype (N) = Packed_Array_Impl_Type (Entyp)
5574 then
5575 null;
5576
5577 -- If not that special case, then just reset the Etype
5578
5579 else
5580 Set_Etype (N, Etype (Entity (N)));
5581 end if;
5582 end;
5583 end if;
5584
5585 -- Although the marking of use clauses happens at the end of
5586 -- Find_Direct_Name, a certain case where a generic actual satisfies
5587 -- a use clause must be checked here due to how the generic machinery
5588 -- handles the analysis of said actuals.
5589
5590 if In_Instance
5591 and then Nkind (Parent (N)) = N_Generic_Association
5592 then
5593 Mark_Use_Clauses (Entity (N));
5594 end if;
5595
5596 return;
5597 end if;
5598
5599 -- Preserve relevant elaboration-related attributes of the context which
5600 -- are no longer available or very expensive to recompute once analysis,
5601 -- resolution, and expansion are over.
5602
5603 if Nkind (N) = N_Identifier then
5604 Mark_Elaboration_Attributes
5605 (N_Id => N,
5606 Checks => True,
5607 Modes => True,
5608 Warnings => True);
5609 end if;
5610
5611 -- Here if Entity pointer was not set, we need full visibility analysis
5612 -- First we generate debugging output if the debug E flag is set.
5613
5614 if Debug_Flag_E then
5615 Write_Str ("Looking for ");
5616 Write_Name (Chars (N));
5617 Write_Eol;
5618 end if;
5619
5620 Homonyms := Current_Entity (N);
5621 Nvis_Entity := False;
5622
5623 E := Homonyms;
5624 while Present (E) loop
5625
5626 -- If entity is immediately visible or potentially use visible, then
5627 -- process the entity and we are done.
5628
5629 if Is_Immediately_Visible (E) then
5630 goto Immediately_Visible_Entity;
5631
5632 elsif Is_Potentially_Use_Visible (E) then
5633 goto Potentially_Use_Visible_Entity;
5634
5635 -- Note if a known but invisible entity encountered
5636
5637 elsif Known_But_Invisible (E) then
5638 Nvis_Entity := True;
5639 end if;
5640
5641 -- Move to next entity in chain and continue search
5642
5643 E := Homonym (E);
5644 end loop;
5645
5646 -- If no entries on homonym chain that were potentially visible,
5647 -- and no entities reasonably considered as non-visible, then
5648 -- we have a plain undefined reference, with no additional
5649 -- explanation required.
5650
5651 if not Nvis_Entity then
5652 Undefined (Nvis => False);
5653
5654 -- Otherwise there is at least one entry on the homonym chain that
5655 -- is reasonably considered as being known and non-visible.
5656
5657 else
5658 Nvis_Messages;
5659 end if;
5660
5661 goto Done;
5662
5663 -- Processing for a potentially use visible entry found. We must search
5664 -- the rest of the homonym chain for two reasons. First, if there is a
5665 -- directly visible entry, then none of the potentially use-visible
5666 -- entities are directly visible (RM 8.4(10)). Second, we need to check
5667 -- for the case of multiple potentially use-visible entries hiding one
5668 -- another and as a result being non-directly visible (RM 8.4(11)).
5669
5670 <<Potentially_Use_Visible_Entity>> declare
5671 Only_One_Visible : Boolean := True;
5672 All_Overloadable : Boolean := Is_Overloadable (E);
5673
5674 begin
5675 E2 := Homonym (E);
5676 while Present (E2) loop
5677 if Is_Immediately_Visible (E2) then
5678
5679 -- If the use-visible entity comes from the actual for a
5680 -- formal package, it hides a directly visible entity from
5681 -- outside the instance.
5682
5683 if From_Actual_Package (E)
5684 and then Scope_Depth (E2) < Scope_Depth (Inst)
5685 then
5686 goto Found;
5687 else
5688 E := E2;
5689 goto Immediately_Visible_Entity;
5690 end if;
5691
5692 elsif Is_Potentially_Use_Visible (E2) then
5693 Only_One_Visible := False;
5694 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
5695
5696 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
5697 -- that can occur in private_with clauses. Example:
5698
5699 -- with A;
5700 -- private with B; package A is
5701 -- package C is function B return Integer;
5702 -- use A; end A;
5703 -- V1 : Integer := B;
5704 -- private function B return Integer;
5705 -- V2 : Integer := B;
5706 -- end C;
5707
5708 -- V1 resolves to A.B, but V2 resolves to library unit B
5709
5710 elsif Ekind (E2) = E_Function
5711 and then Scope (E2) = Standard_Standard
5712 and then Has_Private_With (E2)
5713 then
5714 Only_One_Visible := False;
5715 All_Overloadable := False;
5716 Nvis_Is_Private_Subprg := True;
5717 exit;
5718 end if;
5719
5720 E2 := Homonym (E2);
5721 end loop;
5722
5723 -- On falling through this loop, we have checked that there are no
5724 -- immediately visible entities. Only_One_Visible is set if exactly
5725 -- one potentially use visible entity exists. All_Overloadable is
5726 -- set if all the potentially use visible entities are overloadable.
5727 -- The condition for legality is that either there is one potentially
5728 -- use visible entity, or if there is more than one, then all of them
5729 -- are overloadable.
5730
5731 if Only_One_Visible or All_Overloadable then
5732 goto Found;
5733
5734 -- If there is more than one potentially use-visible entity and at
5735 -- least one of them non-overloadable, we have an error (RM 8.4(11)).
5736 -- Note that E points to the first such entity on the homonym list.
5737
5738 else
5739 -- If one of the entities is declared in an actual package, it
5740 -- was visible in the generic, and takes precedence over other
5741 -- entities that are potentially use-visible. The same applies
5742 -- if the entity is declared in a local instantiation of the
5743 -- current instance.
5744
5745 if In_Instance then
5746
5747 -- Find the current instance
5748
5749 Inst := Current_Scope;
5750 while Present (Inst) and then Inst /= Standard_Standard loop
5751 if Is_Generic_Instance (Inst) then
5752 exit;
5753 end if;
5754
5755 Inst := Scope (Inst);
5756 end loop;
5757
5758 -- Reexamine the candidate entities, giving priority to those
5759 -- that were visible within the generic.
5760
5761 E2 := E;
5762 while Present (E2) loop
5763 Nested_Inst := Nearest_Enclosing_Instance (E2);
5764
5765 -- The entity is declared within an actual package, or in a
5766 -- nested instance. The ">=" accounts for the case where the
5767 -- current instance and the nested instance are the same.
5768
5769 if From_Actual_Package (E2)
5770 or else (Present (Nested_Inst)
5771 and then Scope_Depth (Nested_Inst) >=
5772 Scope_Depth (Inst))
5773 then
5774 E := E2;
5775 goto Found;
5776 end if;
5777
5778 E2 := Homonym (E2);
5779 end loop;
5780
5781 Nvis_Messages;
5782 goto Done;
5783
5784 elsif Is_Predefined_Unit (Current_Sem_Unit) then
5785 -- A use clause in the body of a system file creates conflict
5786 -- with some entity in a user scope, while rtsfind is active.
5787 -- Keep only the entity coming from another predefined unit.
5788
5789 E2 := E;
5790 while Present (E2) loop
5791 if In_Predefined_Unit (E2) then
5792 E := E2;
5793 goto Found;
5794 end if;
5795
5796 E2 := Homonym (E2);
5797 end loop;
5798
5799 -- Entity must exist because predefined unit is correct
5800
5801 raise Program_Error;
5802
5803 else
5804 Nvis_Messages;
5805 goto Done;
5806 end if;
5807 end if;
5808 end;
5809
5810 -- Come here with E set to the first immediately visible entity on
5811 -- the homonym chain. This is the one we want unless there is another
5812 -- immediately visible entity further on in the chain for an inner
5813 -- scope (RM 8.3(8)).
5814
5815 <<Immediately_Visible_Entity>> declare
5816 Level : Int;
5817 Scop : Entity_Id;
5818
5819 begin
5820 -- Find scope level of initial entity. When compiling through
5821 -- Rtsfind, the previous context is not completely invisible, and
5822 -- an outer entity may appear on the chain, whose scope is below
5823 -- the entry for Standard that delimits the current scope stack.
5824 -- Indicate that the level for this spurious entry is outside of
5825 -- the current scope stack.
5826
5827 Level := Scope_Stack.Last;
5828 loop
5829 Scop := Scope_Stack.Table (Level).Entity;
5830 exit when Scop = Scope (E);
5831 Level := Level - 1;
5832 exit when Scop = Standard_Standard;
5833 end loop;
5834
5835 -- Now search remainder of homonym chain for more inner entry
5836 -- If the entity is Standard itself, it has no scope, and we
5837 -- compare it with the stack entry directly.
5838
5839 E2 := Homonym (E);
5840 while Present (E2) loop
5841 if Is_Immediately_Visible (E2) then
5842
5843 -- If a generic package contains a local declaration that
5844 -- has the same name as the generic, there may be a visibility
5845 -- conflict in an instance, where the local declaration must
5846 -- also hide the name of the corresponding package renaming.
5847 -- We check explicitly for a package declared by a renaming,
5848 -- whose renamed entity is an instance that is on the scope
5849 -- stack, and that contains a homonym in the same scope. Once
5850 -- we have found it, we know that the package renaming is not
5851 -- immediately visible, and that the identifier denotes the
5852 -- other entity (and its homonyms if overloaded).
5853
5854 if Scope (E) = Scope (E2)
5855 and then Ekind (E) = E_Package
5856 and then Present (Renamed_Object (E))
5857 and then Is_Generic_Instance (Renamed_Object (E))
5858 and then In_Open_Scopes (Renamed_Object (E))
5859 and then Comes_From_Source (N)
5860 then
5861 Set_Is_Immediately_Visible (E, False);
5862 E := E2;
5863
5864 else
5865 for J in Level + 1 .. Scope_Stack.Last loop
5866 if Scope_Stack.Table (J).Entity = Scope (E2)
5867 or else Scope_Stack.Table (J).Entity = E2
5868 then
5869 Level := J;
5870 E := E2;
5871 exit;
5872 end if;
5873 end loop;
5874 end if;
5875 end if;
5876
5877 E2 := Homonym (E2);
5878 end loop;
5879
5880 -- At the end of that loop, E is the innermost immediately
5881 -- visible entity, so we are all set.
5882 end;
5883
5884 -- Come here with entity found, and stored in E
5885
5886 <<Found>> begin
5887
5888 -- Check violation of No_Wide_Characters restriction
5889
5890 Check_Wide_Character_Restriction (E, N);
5891
5892 -- When distribution features are available (Get_PCS_Name /=
5893 -- Name_No_DSA), a remote access-to-subprogram type is converted
5894 -- into a record type holding whatever information is needed to
5895 -- perform a remote call on an RCI subprogram. In that case we
5896 -- rewrite any occurrence of the RAS type into the equivalent record
5897 -- type here. 'Access attribute references and RAS dereferences are
5898 -- then implemented using specific TSSs. However when distribution is
5899 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
5900 -- generation of these TSSs, and we must keep the RAS type in its
5901 -- original access-to-subprogram form (since all calls through a
5902 -- value of such type will be local anyway in the absence of a PCS).
5903
5904 if Comes_From_Source (N)
5905 and then Is_Remote_Access_To_Subprogram_Type (E)
5906 and then Ekind (E) = E_Access_Subprogram_Type
5907 and then Expander_Active
5908 and then Get_PCS_Name /= Name_No_DSA
5909 then
5910 Rewrite (N, New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
5911 goto Done;
5912 end if;
5913
5914 -- Set the entity. Note that the reason we call Set_Entity for the
5915 -- overloadable case, as opposed to Set_Entity_With_Checks is
5916 -- that in the overloaded case, the initial call can set the wrong
5917 -- homonym. The call that sets the right homonym is in Sem_Res and
5918 -- that call does use Set_Entity_With_Checks, so we don't miss
5919 -- a style check.
5920
5921 if Is_Overloadable (E) then
5922 Set_Entity (N, E);
5923 else
5924 Set_Entity_With_Checks (N, E);
5925 end if;
5926
5927 if Is_Type (E) then
5928 Set_Etype (N, E);
5929 else
5930 Set_Etype (N, Get_Full_View (Etype (E)));
5931 end if;
5932
5933 if Debug_Flag_E then
5934 Write_Str (" found ");
5935 Write_Entity_Info (E, " ");
5936 end if;
5937
5938 -- If the Ekind of the entity is Void, it means that all homonyms
5939 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
5940 -- test is skipped if the current scope is a record and the name is
5941 -- a pragma argument expression (case of Atomic and Volatile pragmas
5942 -- and possibly other similar pragmas added later, which are allowed
5943 -- to reference components in the current record).
5944
5945 if Ekind (E) = E_Void
5946 and then
5947 (not Is_Record_Type (Current_Scope)
5948 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
5949 then
5950 Premature_Usage (N);
5951
5952 -- If the entity is overloadable, collect all interpretations of the
5953 -- name for subsequent overload resolution. We optimize a bit here to
5954 -- do this only if we have an overloadable entity that is not on its
5955 -- own on the homonym chain.
5956
5957 elsif Is_Overloadable (E)
5958 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
5959 then
5960 Collect_Interps (N);
5961
5962 -- If no homonyms were visible, the entity is unambiguous
5963
5964 if not Is_Overloaded (N) then
5965 if Reference_OK and then not Is_Actual_Parameter then
5966 Generate_Reference (E, N);
5967 end if;
5968 end if;
5969
5970 -- Case of non-overloadable entity, set the entity providing that
5971 -- we do not have the case of a discriminant reference within a
5972 -- default expression. Such references are replaced with the
5973 -- corresponding discriminal, which is the formal corresponding to
5974 -- to the discriminant in the initialization procedure.
5975
5976 else
5977 -- Entity is unambiguous, indicate that it is referenced here
5978
5979 -- For a renaming of an object, always generate simple reference,
5980 -- we don't try to keep track of assignments in this case, except
5981 -- in SPARK mode where renamings are traversed for generating
5982 -- local effects of subprograms.
5983
5984 if Reference_OK
5985 and then Is_Object (E)
5986 and then Present (Renamed_Object (E))
5987 and then not GNATprove_Mode
5988 then
5989 Generate_Reference (E, N);
5990
5991 -- If the renamed entity is a private protected component,
5992 -- reference the original component as well. This needs to be
5993 -- done because the private renamings are installed before any
5994 -- analysis has occurred. Reference to a private component will
5995 -- resolve to the renaming and the original component will be
5996 -- left unreferenced, hence the following.
5997
5998 if Is_Prival (E) then
5999 Generate_Reference (Prival_Link (E), N);
6000 end if;
6001
6002 -- One odd case is that we do not want to set the Referenced flag
6003 -- if the entity is a label, and the identifier is the label in
6004 -- the source, since this is not a reference from the point of
6005 -- view of the user.
6006
6007 elsif Nkind (Parent (N)) = N_Label then
6008 declare
6009 R : constant Boolean := Referenced (E);
6010
6011 begin
6012 -- Generate reference unless this is an actual parameter
6013 -- (see comment below)
6014
6015 if Reference_OK and then Is_Actual_Parameter then
6016 Generate_Reference (E, N);
6017 Set_Referenced (E, R);
6018 end if;
6019 end;
6020
6021 -- Normal case, not a label: generate reference
6022
6023 else
6024 if Reference_OK and then not Is_Actual_Parameter then
6025
6026 -- Package or generic package is always a simple reference
6027
6028 if Is_Package_Or_Generic_Package (E) then
6029 Generate_Reference (E, N, 'r');
6030
6031 -- Else see if we have a left hand side
6032
6033 else
6034 case Is_LHS (N) is
6035 when Yes =>
6036 Generate_Reference (E, N, 'm');
6037
6038 when No =>
6039 Generate_Reference (E, N, 'r');
6040
6041 -- If we don't know now, generate reference later
6042
6043 when Unknown =>
6044 Deferred_References.Append ((E, N));
6045 end case;
6046 end if;
6047 end if;
6048 end if;
6049
6050 Set_Entity_Or_Discriminal (N, E);
6051
6052 -- The name may designate a generalized reference, in which case
6053 -- the dereference interpretation will be included. Context is
6054 -- one in which a name is legal.
6055
6056 if Ada_Version >= Ada_2012
6057 and then
6058 (Nkind (Parent (N)) in N_Subexpr
6059 or else Nkind_In (Parent (N), N_Assignment_Statement,
6060 N_Object_Declaration,
6061 N_Parameter_Association))
6062 then
6063 Check_Implicit_Dereference (N, Etype (E));
6064 end if;
6065 end if;
6066 end;
6067
6068 -- Mark relevant use-type and use-package clauses as effective if the
6069 -- node in question is not overloaded and therefore does not require
6070 -- resolution.
6071 --
6072 -- Note: Generic actual subprograms do not follow the normal resolution
6073 -- path, so ignore the fact that they are overloaded and mark them
6074 -- anyway.
6075
6076 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6077 Mark_Use_Clauses (N);
6078 end if;
6079
6080 -- Come here with entity set
6081
6082 <<Done>>
6083 Check_Restriction_No_Use_Of_Entity (N);
6084
6085 -- Annotate the tree by creating a variable reference marker in case the
6086 -- original variable reference is folded or optimized away. The variable
6087 -- reference marker is automatically saved for later examination by the
6088 -- ABE Processing phase. Variable references which act as actuals in a
6089 -- call require special processing and are left to Resolve_Actuals. The
6090 -- reference is a write when it appears on the left hand side of an
6091 -- assignment.
6092
6093 if Marker_OK
6094 and then Needs_Variable_Reference_Marker
6095 (N => N,
6096 Calls_OK => False)
6097 then
6098 declare
6099 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6100
6101 begin
6102 Build_Variable_Reference_Marker
6103 (N => N,
6104 Read => not Is_Assignment_LHS,
6105 Write => Is_Assignment_LHS);
6106 end;
6107 end if;
6108 end Find_Direct_Name;
6109
6110 ------------------------
6111 -- Find_Expanded_Name --
6112 ------------------------
6113
6114 -- This routine searches the homonym chain of the entity until it finds
6115 -- an entity declared in the scope denoted by the prefix. If the entity
6116 -- is private, it may nevertheless be immediately visible, if we are in
6117 -- the scope of its declaration.
6118
6119 procedure Find_Expanded_Name (N : Node_Id) is
6120 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean;
6121 -- Determine whether expanded name Nod appears within a pragma which is
6122 -- a suitable context for an abstract view of a state or variable. The
6123 -- following pragmas fall in this category:
6124 -- Depends
6125 -- Global
6126 -- Initializes
6127 -- Refined_Depends
6128 -- Refined_Global
6129 --
6130 -- In addition, pragma Abstract_State is also considered suitable even
6131 -- though it is an illegal context for an abstract view as this allows
6132 -- for proper resolution of abstract views of variables. This illegal
6133 -- context is later flagged in the analysis of indicator Part_Of.
6134
6135 -----------------------------
6136 -- In_Abstract_View_Pragma --
6137 -----------------------------
6138
6139 function In_Abstract_View_Pragma (Nod : Node_Id) return Boolean is
6140 Par : Node_Id;
6141
6142 begin
6143 -- Climb the parent chain looking for a pragma
6144
6145 Par := Nod;
6146 while Present (Par) loop
6147 if Nkind (Par) = N_Pragma then
6148 if Nam_In (Pragma_Name_Unmapped (Par),
6149 Name_Abstract_State,
6150 Name_Depends,
6151 Name_Global,
6152 Name_Initializes,
6153 Name_Refined_Depends,
6154 Name_Refined_Global)
6155 then
6156 return True;
6157
6158 -- Otherwise the pragma is not a legal context for an abstract
6159 -- view.
6160
6161 else
6162 exit;
6163 end if;
6164
6165 -- Prevent the search from going too far
6166
6167 elsif Is_Body_Or_Package_Declaration (Par) then
6168 exit;
6169 end if;
6170
6171 Par := Parent (Par);
6172 end loop;
6173
6174 return False;
6175 end In_Abstract_View_Pragma;
6176
6177 -- Local variables
6178
6179 Selector : constant Node_Id := Selector_Name (N);
6180
6181 Candidate : Entity_Id := Empty;
6182 P_Name : Entity_Id;
6183 Id : Entity_Id;
6184
6185 -- Start of processing for Find_Expanded_Name
6186
6187 begin
6188 P_Name := Entity (Prefix (N));
6189
6190 -- If the prefix is a renamed package, look for the entity in the
6191 -- original package.
6192
6193 if Ekind (P_Name) = E_Package
6194 and then Present (Renamed_Object (P_Name))
6195 then
6196 P_Name := Renamed_Object (P_Name);
6197
6198 -- Rewrite node with entity field pointing to renamed object
6199
6200 Rewrite (Prefix (N), New_Copy (Prefix (N)));
6201 Set_Entity (Prefix (N), P_Name);
6202
6203 -- If the prefix is an object of a concurrent type, look for
6204 -- the entity in the associated task or protected type.
6205
6206 elsif Is_Concurrent_Type (Etype (P_Name)) then
6207 P_Name := Etype (P_Name);
6208 end if;
6209
6210 Id := Current_Entity (Selector);
6211
6212 declare
6213 Is_New_Candidate : Boolean;
6214
6215 begin
6216 while Present (Id) loop
6217 if Scope (Id) = P_Name then
6218 Candidate := Id;
6219 Is_New_Candidate := True;
6220
6221 -- Handle abstract views of states and variables. These are
6222 -- acceptable candidates only when the reference to the view
6223 -- appears in certain pragmas.
6224
6225 if Ekind (Id) = E_Abstract_State
6226 and then From_Limited_With (Id)
6227 and then Present (Non_Limited_View (Id))
6228 then
6229 if In_Abstract_View_Pragma (N) then
6230 Candidate := Non_Limited_View (Id);
6231 Is_New_Candidate := True;
6232
6233 -- Hide the candidate because it is not used in a proper
6234 -- context.
6235
6236 else
6237 Candidate := Empty;
6238 Is_New_Candidate := False;
6239 end if;
6240 end if;
6241
6242 -- Ada 2005 (AI-217): Handle shadow entities associated with
6243 -- types declared in limited-withed nested packages. We don't need
6244 -- to handle E_Incomplete_Subtype entities because the entities
6245 -- in the limited view are always E_Incomplete_Type and
6246 -- E_Class_Wide_Type entities (see Build_Limited_Views).
6247
6248 -- Regarding the expression used to evaluate the scope, it
6249 -- is important to note that the limited view also has shadow
6250 -- entities associated nested packages. For this reason the
6251 -- correct scope of the entity is the scope of the real entity.
6252 -- The non-limited view may itself be incomplete, in which case
6253 -- get the full view if available.
6254
6255 elsif Ekind_In (Id, E_Incomplete_Type, E_Class_Wide_Type)
6256 and then From_Limited_With (Id)
6257 and then Present (Non_Limited_View (Id))
6258 and then Scope (Non_Limited_View (Id)) = P_Name
6259 then
6260 Candidate := Get_Full_View (Non_Limited_View (Id));
6261 Is_New_Candidate := True;
6262
6263 -- An unusual case arises with a fully qualified name for an
6264 -- entity local to a generic child unit package, within an
6265 -- instantiation of that package. The name of the unit now
6266 -- denotes the renaming created within the instance. This is
6267 -- only relevant in an instance body, see below.
6268
6269 elsif Is_Generic_Instance (Scope (Id))
6270 and then In_Open_Scopes (Scope (Id))
6271 and then In_Instance_Body
6272 and then Ekind (Scope (Id)) = E_Package
6273 and then Ekind (Id) = E_Package
6274 and then Renamed_Entity (Id) = Scope (Id)
6275 and then Is_Immediately_Visible (P_Name)
6276 then
6277 Is_New_Candidate := True;
6278
6279 else
6280 Is_New_Candidate := False;
6281 end if;
6282
6283 if Is_New_Candidate then
6284
6285 -- If entity is a child unit, either it is a visible child of
6286 -- the prefix, or we are in the body of a generic prefix, as
6287 -- will happen when a child unit is instantiated in the body
6288 -- of a generic parent. This is because the instance body does
6289 -- not restore the full compilation context, given that all
6290 -- non-local references have been captured.
6291
6292 if Is_Child_Unit (Id) or else P_Name = Standard_Standard then
6293 exit when Is_Visible_Lib_Unit (Id)
6294 or else (Is_Child_Unit (Id)
6295 and then In_Open_Scopes (Scope (Id))
6296 and then In_Instance_Body);
6297 else
6298 exit when not Is_Hidden (Id);
6299 end if;
6300
6301 exit when Is_Immediately_Visible (Id);
6302 end if;
6303
6304 Id := Homonym (Id);
6305 end loop;
6306 end;
6307
6308 if No (Id)
6309 and then Ekind_In (P_Name, E_Procedure, E_Function)
6310 and then Is_Generic_Instance (P_Name)
6311 then
6312 -- Expanded name denotes entity in (instance of) generic subprogram.
6313 -- The entity may be in the subprogram instance, or may denote one of
6314 -- the formals, which is declared in the enclosing wrapper package.
6315
6316 P_Name := Scope (P_Name);
6317
6318 Id := Current_Entity (Selector);
6319 while Present (Id) loop
6320 exit when Scope (Id) = P_Name;
6321 Id := Homonym (Id);
6322 end loop;
6323 end if;
6324
6325 if No (Id) or else Chars (Id) /= Chars (Selector) then
6326 Set_Etype (N, Any_Type);
6327
6328 -- If we are looking for an entity defined in System, try to find it
6329 -- in the child package that may have been provided as an extension
6330 -- to System. The Extend_System pragma will have supplied the name of
6331 -- the extension, which may have to be loaded.
6332
6333 if Chars (P_Name) = Name_System
6334 and then Scope (P_Name) = Standard_Standard
6335 and then Present (System_Extend_Unit)
6336 and then Present_System_Aux (N)
6337 then
6338 Set_Entity (Prefix (N), System_Aux_Id);
6339 Find_Expanded_Name (N);
6340 return;
6341
6342 -- There is an implicit instance of the predefined operator in
6343 -- the given scope. The operator entity is defined in Standard.
6344 -- Has_Implicit_Operator makes the node into an Expanded_Name.
6345
6346 elsif Nkind (Selector) = N_Operator_Symbol
6347 and then Has_Implicit_Operator (N)
6348 then
6349 return;
6350
6351 -- If there is no literal defined in the scope denoted by the
6352 -- prefix, the literal may belong to (a type derived from)
6353 -- Standard_Character, for which we have no explicit literals.
6354
6355 elsif Nkind (Selector) = N_Character_Literal
6356 and then Has_Implicit_Character_Literal (N)
6357 then
6358 return;
6359
6360 else
6361 -- If the prefix is a single concurrent object, use its name in
6362 -- the error message, rather than that of the anonymous type.
6363
6364 if Is_Concurrent_Type (P_Name)
6365 and then Is_Internal_Name (Chars (P_Name))
6366 then
6367 Error_Msg_Node_2 := Entity (Prefix (N));
6368 else
6369 Error_Msg_Node_2 := P_Name;
6370 end if;
6371
6372 if P_Name = System_Aux_Id then
6373 P_Name := Scope (P_Name);
6374 Set_Entity (Prefix (N), P_Name);
6375 end if;
6376
6377 if Present (Candidate) then
6378
6379 -- If we know that the unit is a child unit we can give a more
6380 -- accurate error message.
6381
6382 if Is_Child_Unit (Candidate) then
6383
6384 -- If the candidate is a private child unit and we are in
6385 -- the visible part of a public unit, specialize the error
6386 -- message. There might be a private with_clause for it,
6387 -- but it is not currently active.
6388
6389 if Is_Private_Descendant (Candidate)
6390 and then Ekind (Current_Scope) = E_Package
6391 and then not In_Private_Part (Current_Scope)
6392 and then not Is_Private_Descendant (Current_Scope)
6393 then
6394 Error_Msg_N
6395 ("private child unit& is not visible here", Selector);
6396
6397 -- Normal case where we have a missing with for a child unit
6398
6399 else
6400 Error_Msg_Qual_Level := 99;
6401 Error_Msg_NE -- CODEFIX
6402 ("missing `WITH &;`", Selector, Candidate);
6403 Error_Msg_Qual_Level := 0;
6404 end if;
6405
6406 -- Here we don't know that this is a child unit
6407
6408 else
6409 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
6410 end if;
6411
6412 else
6413 -- Within the instantiation of a child unit, the prefix may
6414 -- denote the parent instance, but the selector has the name
6415 -- of the original child. That is to say, when A.B appears
6416 -- within an instantiation of generic child unit B, the scope
6417 -- stack includes an instance of A (P_Name) and an instance
6418 -- of B under some other name. We scan the scope to find this
6419 -- child instance, which is the desired entity.
6420 -- Note that the parent may itself be a child instance, if
6421 -- the reference is of the form A.B.C, in which case A.B has
6422 -- already been rewritten with the proper entity.
6423
6424 if In_Open_Scopes (P_Name)
6425 and then Is_Generic_Instance (P_Name)
6426 then
6427 declare
6428 Gen_Par : constant Entity_Id :=
6429 Generic_Parent (Specification
6430 (Unit_Declaration_Node (P_Name)));
6431 S : Entity_Id := Current_Scope;
6432 P : Entity_Id;
6433
6434 begin
6435 for J in reverse 0 .. Scope_Stack.Last loop
6436 S := Scope_Stack.Table (J).Entity;
6437
6438 exit when S = Standard_Standard;
6439
6440 if Ekind_In (S, E_Function,
6441 E_Package,
6442 E_Procedure)
6443 then
6444 P :=
6445 Generic_Parent (Specification
6446 (Unit_Declaration_Node (S)));
6447
6448 -- Check that P is a generic child of the generic
6449 -- parent of the prefix.
6450
6451 if Present (P)
6452 and then Chars (P) = Chars (Selector)
6453 and then Scope (P) = Gen_Par
6454 then
6455 Id := S;
6456 goto Found;
6457 end if;
6458 end if;
6459
6460 end loop;
6461 end;
6462 end if;
6463
6464 -- If this is a selection from Ada, System or Interfaces, then
6465 -- we assume a missing with for the corresponding package.
6466
6467 if Is_Known_Unit (N)
6468 and then not (Present (Entity (Prefix (N)))
6469 and then Scope (Entity (Prefix (N))) /=
6470 Standard_Standard)
6471 then
6472 if not Error_Posted (N) then
6473 Error_Msg_Node_2 := Selector;
6474 Error_Msg_N -- CODEFIX
6475 ("missing `WITH &.&;`", Prefix (N));
6476 end if;
6477
6478 -- If this is a selection from a dummy package, then suppress
6479 -- the error message, of course the entity is missing if the
6480 -- package is missing.
6481
6482 elsif Sloc (Error_Msg_Node_2) = No_Location then
6483 null;
6484
6485 -- Here we have the case of an undefined component
6486
6487 else
6488 -- The prefix may hide a homonym in the context that
6489 -- declares the desired entity. This error can use a
6490 -- specialized message.
6491
6492 if In_Open_Scopes (P_Name) then
6493 declare
6494 H : constant Entity_Id := Homonym (P_Name);
6495
6496 begin
6497 if Present (H)
6498 and then Is_Compilation_Unit (H)
6499 and then
6500 (Is_Immediately_Visible (H)
6501 or else Is_Visible_Lib_Unit (H))
6502 then
6503 Id := First_Entity (H);
6504 while Present (Id) loop
6505 if Chars (Id) = Chars (Selector) then
6506 Error_Msg_Qual_Level := 99;
6507 Error_Msg_Name_1 := Chars (Selector);
6508 Error_Msg_NE
6509 ("% not declared in&", N, P_Name);
6510 Error_Msg_NE
6511 ("\use fully qualified name starting with "
6512 & "Standard to make& visible", N, H);
6513 Error_Msg_Qual_Level := 0;
6514 goto Done;
6515 end if;
6516
6517 Next_Entity (Id);
6518 end loop;
6519 end if;
6520
6521 -- If not found, standard error message
6522
6523 Error_Msg_NE ("& not declared in&", N, Selector);
6524
6525 <<Done>> null;
6526 end;
6527
6528 else
6529 -- Might be worth specializing the case when the prefix
6530 -- is a limited view.
6531 -- ... not declared in limited view of...
6532
6533 Error_Msg_NE ("& not declared in&", N, Selector);
6534 end if;
6535
6536 -- Check for misspelling of some entity in prefix
6537
6538 Id := First_Entity (P_Name);
6539 while Present (Id) loop
6540 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
6541 and then not Is_Internal_Name (Chars (Id))
6542 then
6543 Error_Msg_NE -- CODEFIX
6544 ("possible misspelling of&", Selector, Id);
6545 exit;
6546 end if;
6547
6548 Next_Entity (Id);
6549 end loop;
6550
6551 -- Specialize the message if this may be an instantiation
6552 -- of a child unit that was not mentioned in the context.
6553
6554 if Nkind (Parent (N)) = N_Package_Instantiation
6555 and then Is_Generic_Instance (Entity (Prefix (N)))
6556 and then Is_Compilation_Unit
6557 (Generic_Parent (Parent (Entity (Prefix (N)))))
6558 then
6559 Error_Msg_Node_2 := Selector;
6560 Error_Msg_N -- CODEFIX
6561 ("\missing `WITH &.&;`", Prefix (N));
6562 end if;
6563 end if;
6564 end if;
6565
6566 Id := Any_Id;
6567 end if;
6568 end if;
6569
6570 <<Found>>
6571 if Comes_From_Source (N)
6572 and then Is_Remote_Access_To_Subprogram_Type (Id)
6573 and then Ekind (Id) = E_Access_Subprogram_Type
6574 and then Present (Equivalent_Type (Id))
6575 then
6576 -- If we are not actually generating distribution code (i.e. the
6577 -- current PCS is the dummy non-distributed version), then the
6578 -- Equivalent_Type will be missing, and Id should be treated as
6579 -- a regular access-to-subprogram type.
6580
6581 Id := Equivalent_Type (Id);
6582 Set_Chars (Selector, Chars (Id));
6583 end if;
6584
6585 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
6586
6587 if Ekind (P_Name) = E_Package and then From_Limited_With (P_Name) then
6588 if From_Limited_With (Id)
6589 or else Is_Type (Id)
6590 or else Ekind (Id) = E_Package
6591 then
6592 null;
6593 else
6594 Error_Msg_N
6595 ("limited withed package can only be used to access incomplete "
6596 & "types", N);
6597 end if;
6598 end if;
6599
6600 if Is_Task_Type (P_Name)
6601 and then ((Ekind (Id) = E_Entry
6602 and then Nkind (Parent (N)) /= N_Attribute_Reference)
6603 or else
6604 (Ekind (Id) = E_Entry_Family
6605 and then
6606 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
6607 then
6608 -- If both the task type and the entry are in scope, this may still
6609 -- be the expanded name of an entry formal.
6610
6611 if In_Open_Scopes (Id)
6612 and then Nkind (Parent (N)) = N_Selected_Component
6613 then
6614 null;
6615
6616 else
6617 -- It is an entry call after all, either to the current task
6618 -- (which will deadlock) or to an enclosing task.
6619
6620 Analyze_Selected_Component (N);
6621 return;
6622 end if;
6623 end if;
6624
6625 Change_Selected_Component_To_Expanded_Name (N);
6626
6627 -- Preserve relevant elaboration-related attributes of the context which
6628 -- are no longer available or very expensive to recompute once analysis,
6629 -- resolution, and expansion are over.
6630
6631 Mark_Elaboration_Attributes
6632 (N_Id => N,
6633 Checks => True,
6634 Modes => True,
6635 Warnings => True);
6636
6637 -- Set appropriate type
6638
6639 if Is_Type (Id) then
6640 Set_Etype (N, Id);
6641 else
6642 Set_Etype (N, Get_Full_View (Etype (Id)));
6643 end if;
6644
6645 -- Do style check and generate reference, but skip both steps if this
6646 -- entity has homonyms, since we may not have the right homonym set yet.
6647 -- The proper homonym will be set during the resolve phase.
6648
6649 if Has_Homonym (Id) then
6650 Set_Entity (N, Id);
6651
6652 else
6653 Set_Entity_Or_Discriminal (N, Id);
6654
6655 case Is_LHS (N) is
6656 when Yes =>
6657 Generate_Reference (Id, N, 'm');
6658
6659 when No =>
6660 Generate_Reference (Id, N, 'r');
6661
6662 when Unknown =>
6663 Deferred_References.Append ((Id, N));
6664 end case;
6665 end if;
6666
6667 -- Check for violation of No_Wide_Characters
6668
6669 Check_Wide_Character_Restriction (Id, N);
6670
6671 -- If the Ekind of the entity is Void, it means that all homonyms are
6672 -- hidden from all visibility (RM 8.3(5,14-20)).
6673
6674 if Ekind (Id) = E_Void then
6675 Premature_Usage (N);
6676
6677 elsif Is_Overloadable (Id) and then Present (Homonym (Id)) then
6678 declare
6679 H : Entity_Id := Homonym (Id);
6680
6681 begin
6682 while Present (H) loop
6683 if Scope (H) = Scope (Id)
6684 and then (not Is_Hidden (H)
6685 or else Is_Immediately_Visible (H))
6686 then
6687 Collect_Interps (N);
6688 exit;
6689 end if;
6690
6691 H := Homonym (H);
6692 end loop;
6693
6694 -- If an extension of System is present, collect possible explicit
6695 -- overloadings declared in the extension.
6696
6697 if Chars (P_Name) = Name_System
6698 and then Scope (P_Name) = Standard_Standard
6699 and then Present (System_Extend_Unit)
6700 and then Present_System_Aux (N)
6701 then
6702 H := Current_Entity (Id);
6703
6704 while Present (H) loop
6705 if Scope (H) = System_Aux_Id then
6706 Add_One_Interp (N, H, Etype (H));
6707 end if;
6708
6709 H := Homonym (H);
6710 end loop;
6711 end if;
6712 end;
6713 end if;
6714
6715 if Nkind (Selector_Name (N)) = N_Operator_Symbol
6716 and then Scope (Id) /= Standard_Standard
6717 then
6718 -- In addition to user-defined operators in the given scope, there
6719 -- may be an implicit instance of the predefined operator. The
6720 -- operator (defined in Standard) is found in Has_Implicit_Operator,
6721 -- and added to the interpretations. Procedure Add_One_Interp will
6722 -- determine which hides which.
6723
6724 if Has_Implicit_Operator (N) then
6725 null;
6726 end if;
6727 end if;
6728
6729 -- If there is a single interpretation for N we can generate a
6730 -- reference to the unique entity found.
6731
6732 if Is_Overloadable (Id) and then not Is_Overloaded (N) then
6733 Generate_Reference (Id, N);
6734 end if;
6735
6736 -- Mark relevant use-type and use-package clauses as effective if the
6737 -- node in question is not overloaded and therefore does not require
6738 -- resolution.
6739
6740 if Nkind (N) not in N_Subexpr or else not Is_Overloaded (N) then
6741 Mark_Use_Clauses (N);
6742 end if;
6743
6744 Check_Restriction_No_Use_Of_Entity (N);
6745
6746 -- Annotate the tree by creating a variable reference marker in case the
6747 -- original variable reference is folded or optimized away. The variable
6748 -- reference marker is automatically saved for later examination by the
6749 -- ABE Processing phase. Variable references which act as actuals in a
6750 -- call require special processing and are left to Resolve_Actuals. The
6751 -- reference is a write when it appears on the left hand side of an
6752 -- assignment.
6753
6754 if Needs_Variable_Reference_Marker
6755 (N => N,
6756 Calls_OK => False)
6757 then
6758 declare
6759 Is_Assignment_LHS : constant Boolean := Is_LHS (N) = Yes;
6760
6761 begin
6762 Build_Variable_Reference_Marker
6763 (N => N,
6764 Read => not Is_Assignment_LHS,
6765 Write => Is_Assignment_LHS);
6766 end;
6767 end if;
6768 end Find_Expanded_Name;
6769
6770 --------------------
6771 -- Find_Most_Prev --
6772 --------------------
6773
6774 function Find_Most_Prev (Use_Clause : Node_Id) return Node_Id is
6775 Curr : Node_Id;
6776
6777 begin
6778 -- Loop through the Prev_Use_Clause chain
6779
6780 Curr := Use_Clause;
6781 while Present (Prev_Use_Clause (Curr)) loop
6782 Curr := Prev_Use_Clause (Curr);
6783 end loop;
6784
6785 return Curr;
6786 end Find_Most_Prev;
6787
6788 -------------------------
6789 -- Find_Renamed_Entity --
6790 -------------------------
6791
6792 function Find_Renamed_Entity
6793 (N : Node_Id;
6794 Nam : Node_Id;
6795 New_S : Entity_Id;
6796 Is_Actual : Boolean := False) return Entity_Id
6797 is
6798 Ind : Interp_Index;
6799 I1 : Interp_Index := 0; -- Suppress junk warnings
6800 It : Interp;
6801 It1 : Interp;
6802 Old_S : Entity_Id;
6803 Inst : Entity_Id;
6804
6805 function Find_Nearer_Entity
6806 (New_S : Entity_Id;
6807 Old1_S : Entity_Id;
6808 Old2_S : Entity_Id) return Entity_Id;
6809 -- Determine whether one of Old_S1 and Old_S2 is nearer to New_S than
6810 -- the other, and return it if so. Return Empty otherwise. We use this
6811 -- in conjunction with Inherit_Renamed_Profile to simplify later type
6812 -- disambiguation for actual subprograms in instances.
6813
6814 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
6815 -- If the renamed entity is an implicit operator, check whether it is
6816 -- visible because its operand type is properly visible. This check
6817 -- applies to explicit renamed entities that appear in the source in a
6818 -- renaming declaration or a formal subprogram instance, but not to
6819 -- default generic actuals with a name.
6820
6821 function Report_Overload return Entity_Id;
6822 -- List possible interpretations, and specialize message in the
6823 -- case of a generic actual.
6824
6825 function Within (Inner, Outer : Entity_Id) return Boolean;
6826 -- Determine whether a candidate subprogram is defined within the
6827 -- enclosing instance. If yes, it has precedence over outer candidates.
6828
6829 --------------------------
6830 -- Find_Nearer_Entity --
6831 --------------------------
6832
6833 function Find_Nearer_Entity
6834 (New_S : Entity_Id;
6835 Old1_S : Entity_Id;
6836 Old2_S : Entity_Id) return Entity_Id
6837 is
6838 New_F : Entity_Id;
6839 Old1_F : Entity_Id;
6840 Old2_F : Entity_Id;
6841 Anc_T : Entity_Id;
6842
6843 begin
6844 New_F := First_Formal (New_S);
6845 Old1_F := First_Formal (Old1_S);
6846 Old2_F := First_Formal (Old2_S);
6847
6848 -- The criterion is whether the type of the formals of one of Old1_S
6849 -- and Old2_S is an ancestor subtype of the type of the corresponding
6850 -- formals of New_S while the other is not (we already know that they
6851 -- are all subtypes of the same base type).
6852
6853 -- This makes it possible to find the more correct renamed entity in
6854 -- the case of a generic instantiation nested in an enclosing one for
6855 -- which different formal types get the same actual type, which will
6856 -- in turn make it possible for Inherit_Renamed_Profile to preserve
6857 -- types on formal parameters and ultimately simplify disambiguation.
6858
6859 -- Consider the follow package G:
6860
6861 -- generic
6862 -- type Item_T is private;
6863 -- with function Compare (L, R: Item_T) return Boolean is <>;
6864
6865 -- type Bound_T is private;
6866 -- with function Compare (L, R : Bound_T) return Boolean is <>;
6867 -- package G is
6868 -- ...
6869 -- end G;
6870
6871 -- package body G is
6872 -- package My_Inner is Inner_G (Bound_T);
6873 -- ...
6874 -- end G;
6875
6876 -- with the following package Inner_G:
6877
6878 -- generic
6879 -- type T is private;
6880 -- with function Compare (L, R: T) return Boolean is <>;
6881 -- package Inner_G is
6882 -- function "<" (L, R: T) return Boolean is (Compare (L, R));
6883 -- end Inner_G;
6884
6885 -- If G is instantiated on the same actual type with a single Compare
6886 -- function:
6887
6888 -- type T is ...
6889 -- function Compare (L, R : T) return Boolean;
6890 -- package My_G is new (T, T);
6891
6892 -- then the renaming generated for Compare in the inner instantiation
6893 -- is ambiguous: it can rename either of the renamings generated for
6894 -- the outer instantiation. Now if the first one is picked up, then
6895 -- the subtypes of the formal parameters of the renaming will not be
6896 -- preserved in Inherit_Renamed_Profile because they are subtypes of
6897 -- the Bound_T formal type and not of the Item_T formal type, so we
6898 -- need to arrange for the second one to be picked up instead.
6899
6900 while Present (New_F) loop
6901 if Etype (Old1_F) /= Etype (Old2_F) then
6902 Anc_T := Ancestor_Subtype (Etype (New_F));
6903
6904 if Etype (Old1_F) = Anc_T then
6905 return Old1_S;
6906 elsif Etype (Old2_F) = Anc_T then
6907 return Old2_S;
6908 end if;
6909 end if;
6910
6911 Next_Formal (New_F);
6912 Next_Formal (Old1_F);
6913 Next_Formal (Old2_F);
6914 end loop;
6915
6916 pragma Assert (No (Old1_F));
6917 pragma Assert (No (Old2_F));
6918
6919 return Empty;
6920 end Find_Nearer_Entity;
6921
6922 --------------------------
6923 -- Is_Visible_Operation --
6924 --------------------------
6925
6926 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
6927 Scop : Entity_Id;
6928 Typ : Entity_Id;
6929 Btyp : Entity_Id;
6930
6931 begin
6932 if Ekind (Op) /= E_Operator
6933 or else Scope (Op) /= Standard_Standard
6934 or else (In_Instance
6935 and then (not Is_Actual
6936 or else Present (Enclosing_Instance)))
6937 then
6938 return True;
6939
6940 else
6941 -- For a fixed point type operator, check the resulting type,
6942 -- because it may be a mixed mode integer * fixed operation.
6943
6944 if Present (Next_Formal (First_Formal (New_S)))
6945 and then Is_Fixed_Point_Type (Etype (New_S))
6946 then
6947 Typ := Etype (New_S);
6948 else
6949 Typ := Etype (First_Formal (New_S));
6950 end if;
6951
6952 Btyp := Base_Type (Typ);
6953
6954 if Nkind (Nam) /= N_Expanded_Name then
6955 return (In_Open_Scopes (Scope (Btyp))
6956 or else Is_Potentially_Use_Visible (Btyp)
6957 or else In_Use (Btyp)
6958 or else In_Use (Scope (Btyp)));
6959
6960 else
6961 Scop := Entity (Prefix (Nam));
6962
6963 if Ekind (Scop) = E_Package
6964 and then Present (Renamed_Object (Scop))
6965 then
6966 Scop := Renamed_Object (Scop);
6967 end if;
6968
6969 -- Operator is visible if prefix of expanded name denotes
6970 -- scope of type, or else type is defined in System_Aux
6971 -- and the prefix denotes System.
6972
6973 return Scope (Btyp) = Scop
6974 or else (Scope (Btyp) = System_Aux_Id
6975 and then Scope (Scope (Btyp)) = Scop);
6976 end if;
6977 end if;
6978 end Is_Visible_Operation;
6979
6980 ------------
6981 -- Within --
6982 ------------
6983
6984 function Within (Inner, Outer : Entity_Id) return Boolean is
6985 Sc : Entity_Id;
6986
6987 begin
6988 Sc := Scope (Inner);
6989 while Sc /= Standard_Standard loop
6990 if Sc = Outer then
6991 return True;
6992 else
6993 Sc := Scope (Sc);
6994 end if;
6995 end loop;
6996
6997 return False;
6998 end Within;
6999
7000 ---------------------
7001 -- Report_Overload --
7002 ---------------------
7003
7004 function Report_Overload return Entity_Id is
7005 begin
7006 if Is_Actual then
7007 Error_Msg_NE -- CODEFIX
7008 ("ambiguous actual subprogram&, " &
7009 "possible interpretations:", N, Nam);
7010 else
7011 Error_Msg_N -- CODEFIX
7012 ("ambiguous subprogram, " &
7013 "possible interpretations:", N);
7014 end if;
7015
7016 List_Interps (Nam, N);
7017 return Old_S;
7018 end Report_Overload;
7019
7020 -- Start of processing for Find_Renamed_Entity
7021
7022 begin
7023 Old_S := Any_Id;
7024 Candidate_Renaming := Empty;
7025
7026 if Is_Overloaded (Nam) then
7027 Get_First_Interp (Nam, Ind, It);
7028 while Present (It.Nam) loop
7029 if Entity_Matches_Spec (It.Nam, New_S)
7030 and then Is_Visible_Operation (It.Nam)
7031 then
7032 if Old_S /= Any_Id then
7033
7034 -- Note: The call to Disambiguate only happens if a
7035 -- previous interpretation was found, in which case I1
7036 -- has received a value.
7037
7038 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
7039
7040 if It1 = No_Interp then
7041 Inst := Enclosing_Instance;
7042
7043 if Present (Inst) then
7044 if Within (It.Nam, Inst) then
7045 if Within (Old_S, Inst) then
7046 declare
7047 It_D : constant Uint := Scope_Depth (It.Nam);
7048 Old_D : constant Uint := Scope_Depth (Old_S);
7049 N_Ent : Entity_Id;
7050 begin
7051 -- Choose the innermost subprogram, which
7052 -- would hide the outer one in the generic.
7053
7054 if Old_D > It_D then
7055 return Old_S;
7056 elsif It_D > Old_D then
7057 return It.Nam;
7058 end if;
7059
7060 -- Otherwise, if we can determine that one
7061 -- of the entities is nearer to the renaming
7062 -- than the other, choose it. If not, then
7063 -- return the newer one as done historically.
7064
7065 N_Ent :=
7066 Find_Nearer_Entity (New_S, Old_S, It.Nam);
7067 if Present (N_Ent) then
7068 return N_Ent;
7069 else
7070 return It.Nam;
7071 end if;
7072 end;
7073 end if;
7074
7075 elsif Within (Old_S, Inst) then
7076 return Old_S;
7077
7078 else
7079 return Report_Overload;
7080 end if;
7081
7082 -- If not within an instance, ambiguity is real
7083
7084 else
7085 return Report_Overload;
7086 end if;
7087
7088 else
7089 Old_S := It1.Nam;
7090 exit;
7091 end if;
7092
7093 else
7094 I1 := Ind;
7095 Old_S := It.Nam;
7096 end if;
7097
7098 elsif
7099 Present (First_Formal (It.Nam))
7100 and then Present (First_Formal (New_S))
7101 and then (Base_Type (Etype (First_Formal (It.Nam))) =
7102 Base_Type (Etype (First_Formal (New_S))))
7103 then
7104 Candidate_Renaming := It.Nam;
7105 end if;
7106
7107 Get_Next_Interp (Ind, It);
7108 end loop;
7109
7110 Set_Entity (Nam, Old_S);
7111
7112 if Old_S /= Any_Id then
7113 Set_Is_Overloaded (Nam, False);
7114 end if;
7115
7116 -- Non-overloaded case
7117
7118 else
7119 if Is_Actual
7120 and then Present (Enclosing_Instance)
7121 and then Entity_Matches_Spec (Entity (Nam), New_S)
7122 then
7123 Old_S := Entity (Nam);
7124
7125 elsif Entity_Matches_Spec (Entity (Nam), New_S) then
7126 Candidate_Renaming := New_S;
7127
7128 if Is_Visible_Operation (Entity (Nam)) then
7129 Old_S := Entity (Nam);
7130 end if;
7131
7132 elsif Present (First_Formal (Entity (Nam)))
7133 and then Present (First_Formal (New_S))
7134 and then (Base_Type (Etype (First_Formal (Entity (Nam)))) =
7135 Base_Type (Etype (First_Formal (New_S))))
7136 then
7137 Candidate_Renaming := Entity (Nam);
7138 end if;
7139 end if;
7140
7141 return Old_S;
7142 end Find_Renamed_Entity;
7143
7144 -----------------------------
7145 -- Find_Selected_Component --
7146 -----------------------------
7147
7148 procedure Find_Selected_Component (N : Node_Id) is
7149 P : constant Node_Id := Prefix (N);
7150
7151 P_Name : Entity_Id;
7152 -- Entity denoted by prefix
7153
7154 P_Type : Entity_Id;
7155 -- and its type
7156
7157 Nam : Node_Id;
7158
7159 function Available_Subtype return Boolean;
7160 -- A small optimization: if the prefix is constrained and the component
7161 -- is an array type we may already have a usable subtype for it, so we
7162 -- can use it rather than generating a new one, because the bounds
7163 -- will be the values of the discriminants and not discriminant refs.
7164 -- This simplifies value tracing in GNATProve. For consistency, both
7165 -- the entity name and the subtype come from the constrained component.
7166
7167 -- This is only used in GNATProve mode: when generating code it may be
7168 -- necessary to create an itype in the scope of use of the selected
7169 -- component, e.g. in the context of a expanded record equality.
7170
7171 function Is_Reference_In_Subunit return Boolean;
7172 -- In a subunit, the scope depth is not a proper measure of hiding,
7173 -- because the context of the proper body may itself hide entities in
7174 -- parent units. This rare case requires inspecting the tree directly
7175 -- because the proper body is inserted in the main unit and its context
7176 -- is simply added to that of the parent.
7177
7178 -----------------------
7179 -- Available_Subtype --
7180 -----------------------
7181
7182 function Available_Subtype return Boolean is
7183 Comp : Entity_Id;
7184
7185 begin
7186 if GNATprove_Mode then
7187 Comp := First_Entity (Etype (P));
7188 while Present (Comp) loop
7189 if Chars (Comp) = Chars (Selector_Name (N)) then
7190 Set_Etype (N, Etype (Comp));
7191 Set_Entity (Selector_Name (N), Comp);
7192 Set_Etype (Selector_Name (N), Etype (Comp));
7193 return True;
7194 end if;
7195
7196 Next_Component (Comp);
7197 end loop;
7198 end if;
7199
7200 return False;
7201 end Available_Subtype;
7202
7203 -----------------------------
7204 -- Is_Reference_In_Subunit --
7205 -----------------------------
7206
7207 function Is_Reference_In_Subunit return Boolean is
7208 Clause : Node_Id;
7209 Comp_Unit : Node_Id;
7210
7211 begin
7212 Comp_Unit := N;
7213 while Present (Comp_Unit)
7214 and then Nkind (Comp_Unit) /= N_Compilation_Unit
7215 loop
7216 Comp_Unit := Parent (Comp_Unit);
7217 end loop;
7218
7219 if No (Comp_Unit) or else Nkind (Unit (Comp_Unit)) /= N_Subunit then
7220 return False;
7221 end if;
7222
7223 -- Now check whether the package is in the context of the subunit
7224
7225 Clause := First (Context_Items (Comp_Unit));
7226 while Present (Clause) loop
7227 if Nkind (Clause) = N_With_Clause
7228 and then Entity (Name (Clause)) = P_Name
7229 then
7230 return True;
7231 end if;
7232
7233 Clause := Next (Clause);
7234 end loop;
7235
7236 return False;
7237 end Is_Reference_In_Subunit;
7238
7239 -- Start of processing for Find_Selected_Component
7240
7241 begin
7242 Analyze (P);
7243
7244 if Nkind (P) = N_Error then
7245 return;
7246 end if;
7247
7248 -- If the selector already has an entity, the node has been constructed
7249 -- in the course of expansion, and is known to be valid. Do not verify
7250 -- that it is defined for the type (it may be a private component used
7251 -- in the expansion of record equality).
7252
7253 if Present (Entity (Selector_Name (N))) then
7254 if No (Etype (N)) or else Etype (N) = Any_Type then
7255 declare
7256 Sel_Name : constant Node_Id := Selector_Name (N);
7257 Selector : constant Entity_Id := Entity (Sel_Name);
7258 C_Etype : Node_Id;
7259
7260 begin
7261 Set_Etype (Sel_Name, Etype (Selector));
7262
7263 if not Is_Entity_Name (P) then
7264 Resolve (P);
7265 end if;
7266
7267 -- Build an actual subtype except for the first parameter
7268 -- of an init proc, where this actual subtype is by
7269 -- definition incorrect, since the object is uninitialized
7270 -- (and does not even have defined discriminants etc.)
7271
7272 if Is_Entity_Name (P)
7273 and then Ekind (Entity (P)) = E_Function
7274 then
7275 Nam := New_Copy (P);
7276
7277 if Is_Overloaded (P) then
7278 Save_Interps (P, Nam);
7279 end if;
7280
7281 Rewrite (P, Make_Function_Call (Sloc (P), Name => Nam));
7282 Analyze_Call (P);
7283 Analyze_Selected_Component (N);
7284 return;
7285
7286 elsif Ekind (Selector) = E_Component
7287 and then (not Is_Entity_Name (P)
7288 or else Chars (Entity (P)) /= Name_uInit)
7289 then
7290 -- Check if we already have an available subtype we can use
7291
7292 if Ekind (Etype (P)) = E_Record_Subtype
7293 and then Nkind (Parent (Etype (P))) = N_Subtype_Declaration
7294 and then Is_Array_Type (Etype (Selector))
7295 and then not Is_Packed (Etype (Selector))
7296 and then Available_Subtype
7297 then
7298 return;
7299
7300 -- Do not build the subtype when referencing components of
7301 -- dispatch table wrappers. Required to avoid generating
7302 -- elaboration code with HI runtimes.
7303
7304 elsif RTU_Loaded (Ada_Tags)
7305 and then
7306 ((RTE_Available (RE_Dispatch_Table_Wrapper)
7307 and then Scope (Selector) =
7308 RTE (RE_Dispatch_Table_Wrapper))
7309 or else
7310 (RTE_Available (RE_No_Dispatch_Table_Wrapper)
7311 and then Scope (Selector) =
7312 RTE (RE_No_Dispatch_Table_Wrapper)))
7313 then
7314 C_Etype := Empty;
7315 else
7316 C_Etype :=
7317 Build_Actual_Subtype_Of_Component
7318 (Etype (Selector), N);
7319 end if;
7320
7321 else
7322 C_Etype := Empty;
7323 end if;
7324
7325 if No (C_Etype) then
7326 C_Etype := Etype (Selector);
7327 else
7328 Insert_Action (N, C_Etype);
7329 C_Etype := Defining_Identifier (C_Etype);
7330 end if;
7331
7332 Set_Etype (N, C_Etype);
7333 end;
7334
7335 -- If the selected component appears within a default expression
7336 -- and it has an actual subtype, the preanalysis has not yet
7337 -- completed its analysis, because Insert_Actions is disabled in
7338 -- that context. Within the init proc of the enclosing type we
7339 -- must complete this analysis, if an actual subtype was created.
7340
7341 elsif Inside_Init_Proc then
7342 declare
7343 Typ : constant Entity_Id := Etype (N);
7344 Decl : constant Node_Id := Declaration_Node (Typ);
7345 begin
7346 if Nkind (Decl) = N_Subtype_Declaration
7347 and then not Analyzed (Decl)
7348 and then Is_List_Member (Decl)
7349 and then No (Parent (Decl))
7350 then
7351 Remove (Decl);
7352 Insert_Action (N, Decl);
7353 end if;
7354 end;
7355 end if;
7356
7357 return;
7358
7359 elsif Is_Entity_Name (P) then
7360 P_Name := Entity (P);
7361
7362 -- The prefix may denote an enclosing type which is the completion
7363 -- of an incomplete type declaration.
7364
7365 if Is_Type (P_Name) then
7366 Set_Entity (P, Get_Full_View (P_Name));
7367 Set_Etype (P, Entity (P));
7368 P_Name := Entity (P);
7369 end if;
7370
7371 P_Type := Base_Type (Etype (P));
7372
7373 if Debug_Flag_E then
7374 Write_Str ("Found prefix type to be ");
7375 Write_Entity_Info (P_Type, " "); Write_Eol;
7376 end if;
7377
7378 -- If the prefix's type is an access type, get to the record type
7379
7380 if Is_Access_Type (P_Type) then
7381 P_Type := Implicitly_Designated_Type (P_Type);
7382 end if;
7383
7384 -- First check for components of a record object (not the
7385 -- result of a call, which is handled below).
7386
7387 if Has_Components (P_Type)
7388 and then not Is_Overloadable (P_Name)
7389 and then not Is_Type (P_Name)
7390 then
7391 -- Selected component of record. Type checking will validate
7392 -- name of selector.
7393
7394 -- ??? Could we rewrite an implicit dereference into an explicit
7395 -- one here?
7396
7397 Analyze_Selected_Component (N);
7398
7399 -- Reference to type name in predicate/invariant expression
7400
7401 elsif (Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type))
7402 and then not In_Open_Scopes (P_Name)
7403 and then (not Is_Concurrent_Type (Etype (P_Name))
7404 or else not In_Open_Scopes (Etype (P_Name)))
7405 then
7406 -- Call to protected operation or entry. Type checking is
7407 -- needed on the prefix.
7408
7409 Analyze_Selected_Component (N);
7410
7411 elsif (In_Open_Scopes (P_Name)
7412 and then Ekind (P_Name) /= E_Void
7413 and then not Is_Overloadable (P_Name))
7414 or else (Is_Concurrent_Type (Etype (P_Name))
7415 and then In_Open_Scopes (Etype (P_Name)))
7416 then
7417 -- Prefix denotes an enclosing loop, block, or task, i.e. an
7418 -- enclosing construct that is not a subprogram or accept.
7419
7420 -- A special case: a protected body may call an operation
7421 -- on an external object of the same type, in which case it
7422 -- is not an expanded name. If the prefix is the type itself,
7423 -- or the context is a single synchronized object it can only
7424 -- be interpreted as an expanded name.
7425
7426 if Is_Concurrent_Type (Etype (P_Name)) then
7427 if Is_Type (P_Name)
7428 or else Present (Anonymous_Object (Etype (P_Name)))
7429 then
7430 Find_Expanded_Name (N);
7431
7432 else
7433 Analyze_Selected_Component (N);
7434 return;
7435 end if;
7436
7437 else
7438 Find_Expanded_Name (N);
7439 end if;
7440
7441 elsif Ekind (P_Name) = E_Package then
7442 Find_Expanded_Name (N);
7443
7444 elsif Is_Overloadable (P_Name) then
7445
7446 -- The subprogram may be a renaming (of an enclosing scope) as
7447 -- in the case of the name of the generic within an instantiation.
7448
7449 if Ekind_In (P_Name, E_Procedure, E_Function)
7450 and then Present (Alias (P_Name))
7451 and then Is_Generic_Instance (Alias (P_Name))
7452 then
7453 P_Name := Alias (P_Name);
7454 end if;
7455
7456 if Is_Overloaded (P) then
7457
7458 -- The prefix must resolve to a unique enclosing construct
7459
7460 declare
7461 Found : Boolean := False;
7462 Ind : Interp_Index;
7463 It : Interp;
7464
7465 begin
7466 Get_First_Interp (P, Ind, It);
7467 while Present (It.Nam) loop
7468 if In_Open_Scopes (It.Nam) then
7469 if Found then
7470 Error_Msg_N (
7471 "prefix must be unique enclosing scope", N);
7472 Set_Entity (N, Any_Id);
7473 Set_Etype (N, Any_Type);
7474 return;
7475
7476 else
7477 Found := True;
7478 P_Name := It.Nam;
7479 end if;
7480 end if;
7481
7482 Get_Next_Interp (Ind, It);
7483 end loop;
7484 end;
7485 end if;
7486
7487 if In_Open_Scopes (P_Name) then
7488 Set_Entity (P, P_Name);
7489 Set_Is_Overloaded (P, False);
7490 Find_Expanded_Name (N);
7491
7492 else
7493 -- If no interpretation as an expanded name is possible, it
7494 -- must be a selected component of a record returned by a
7495 -- function call. Reformat prefix as a function call, the rest
7496 -- is done by type resolution.
7497
7498 -- Error if the prefix is procedure or entry, as is P.X
7499
7500 if Ekind (P_Name) /= E_Function
7501 and then
7502 (not Is_Overloaded (P)
7503 or else Nkind (Parent (N)) = N_Procedure_Call_Statement)
7504 then
7505 -- Prefix may mention a package that is hidden by a local
7506 -- declaration: let the user know. Scan the full homonym
7507 -- chain, the candidate package may be anywhere on it.
7508
7509 if Present (Homonym (Current_Entity (P_Name))) then
7510 P_Name := Current_Entity (P_Name);
7511
7512 while Present (P_Name) loop
7513 exit when Ekind (P_Name) = E_Package;
7514 P_Name := Homonym (P_Name);
7515 end loop;
7516
7517 if Present (P_Name) then
7518 if not Is_Reference_In_Subunit then
7519 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
7520 Error_Msg_NE
7521 ("package& is hidden by declaration#", N, P_Name);
7522 end if;
7523
7524 Set_Entity (Prefix (N), P_Name);
7525 Find_Expanded_Name (N);
7526 return;
7527
7528 else
7529 P_Name := Entity (Prefix (N));
7530 end if;
7531 end if;
7532
7533 Error_Msg_NE
7534 ("invalid prefix in selected component&", N, P_Name);
7535 Change_Selected_Component_To_Expanded_Name (N);
7536 Set_Entity (N, Any_Id);
7537 Set_Etype (N, Any_Type);
7538
7539 -- Here we have a function call, so do the reformatting
7540
7541 else
7542 Nam := New_Copy (P);
7543 Save_Interps (P, Nam);
7544
7545 -- We use Replace here because this is one of those cases
7546 -- where the parser has missclassified the node, and we fix
7547 -- things up and then do the semantic analysis on the fixed
7548 -- up node. Normally we do this using one of the Sinfo.CN
7549 -- routines, but this is too tricky for that.
7550
7551 -- Note that using Rewrite would be wrong, because we would
7552 -- have a tree where the original node is unanalyzed.
7553
7554 Replace (P,
7555 Make_Function_Call (Sloc (P), Name => Nam));
7556
7557 -- Now analyze the reformatted node
7558
7559 Analyze_Call (P);
7560
7561 -- If the prefix is illegal after this transformation, there
7562 -- may be visibility errors on the prefix. The safest is to
7563 -- treat the selected component as an error.
7564
7565 if Error_Posted (P) then
7566 Set_Etype (N, Any_Type);
7567 return;
7568
7569 else
7570 Analyze_Selected_Component (N);
7571 end if;
7572 end if;
7573 end if;
7574
7575 -- Remaining cases generate various error messages
7576
7577 else
7578 -- Format node as expanded name, to avoid cascaded errors
7579
7580 Change_Selected_Component_To_Expanded_Name (N);
7581 Set_Entity (N, Any_Id);
7582 Set_Etype (N, Any_Type);
7583
7584 -- Issue error message, but avoid this if error issued already.
7585 -- Use identifier of prefix if one is available.
7586
7587 if P_Name = Any_Id then
7588 null;
7589
7590 -- It is not an error if the prefix is the current instance of
7591 -- type name, e.g. the expression of a type aspect, when it is
7592 -- analyzed within a generic unit. We still have to verify that a
7593 -- component of that name exists, and decorate the node
7594 -- accordingly.
7595
7596 elsif Is_Entity_Name (P) and then Is_Current_Instance (P) then
7597 declare
7598 Comp : Entity_Id;
7599
7600 begin
7601 Comp := First_Entity (Entity (P));
7602 while Present (Comp) loop
7603 if Chars (Comp) = Chars (Selector_Name (N)) then
7604 Set_Entity (N, Comp);
7605 Set_Etype (N, Etype (Comp));
7606 Set_Entity (Selector_Name (N), Comp);
7607 Set_Etype (Selector_Name (N), Etype (Comp));
7608 return;
7609 end if;
7610
7611 Next_Entity (Comp);
7612 end loop;
7613 end;
7614
7615 elsif Ekind (P_Name) = E_Void then
7616 Premature_Usage (P);
7617
7618 elsif Nkind (P) /= N_Attribute_Reference then
7619
7620 -- This may have been meant as a prefixed call to a primitive
7621 -- of an untagged type. If it is a function call check type of
7622 -- its first formal and add explanation.
7623
7624 declare
7625 F : constant Entity_Id :=
7626 Current_Entity (Selector_Name (N));
7627 begin
7628 if Present (F)
7629 and then Is_Overloadable (F)
7630 and then Present (First_Entity (F))
7631 and then not Is_Tagged_Type (Etype (First_Entity (F)))
7632 then
7633 Error_Msg_N
7634 ("prefixed call is only allowed for objects of a "
7635 & "tagged type", N);
7636 end if;
7637 end;
7638
7639 Error_Msg_N ("invalid prefix in selected component&", P);
7640
7641 if Is_Incomplete_Type (P_Type)
7642 and then Is_Access_Type (Etype (P))
7643 then
7644 Error_Msg_N
7645 ("\dereference must not be of an incomplete type "
7646 & "(RM 3.10.1)", P);
7647 end if;
7648
7649 else
7650 Error_Msg_N ("invalid prefix in selected component", P);
7651 end if;
7652 end if;
7653 else
7654 -- If prefix is not the name of an entity, it must be an expression,
7655 -- whose type is appropriate for a record. This is determined by
7656 -- type resolution.
7657
7658 Analyze_Selected_Component (N);
7659 end if;
7660
7661 Analyze_Dimension (N);
7662 end Find_Selected_Component;
7663
7664 ---------------
7665 -- Find_Type --
7666 ---------------
7667
7668 procedure Find_Type (N : Node_Id) is
7669 C : Entity_Id;
7670 Typ : Entity_Id;
7671 T : Entity_Id;
7672 T_Name : Entity_Id;
7673
7674 begin
7675 if N = Error then
7676 return;
7677
7678 elsif Nkind (N) = N_Attribute_Reference then
7679
7680 -- Class attribute. This is not valid in Ada 83 mode, but we do not
7681 -- need to enforce that at this point, since the declaration of the
7682 -- tagged type in the prefix would have been flagged already.
7683
7684 if Attribute_Name (N) = Name_Class then
7685 Check_Restriction (No_Dispatch, N);
7686 Find_Type (Prefix (N));
7687
7688 -- Propagate error from bad prefix
7689
7690 if Etype (Prefix (N)) = Any_Type then
7691 Set_Entity (N, Any_Type);
7692 Set_Etype (N, Any_Type);
7693 return;
7694 end if;
7695
7696 T := Base_Type (Entity (Prefix (N)));
7697
7698 -- Case where type is not known to be tagged. Its appearance in
7699 -- the prefix of the 'Class attribute indicates that the full view
7700 -- will be tagged.
7701
7702 if not Is_Tagged_Type (T) then
7703 if Ekind (T) = E_Incomplete_Type then
7704
7705 -- It is legal to denote the class type of an incomplete
7706 -- type. The full type will have to be tagged, of course.
7707 -- In Ada 2005 this usage is declared obsolescent, so we
7708 -- warn accordingly. This usage is only legal if the type
7709 -- is completed in the current scope, and not for a limited
7710 -- view of a type.
7711
7712 if Ada_Version >= Ada_2005 then
7713
7714 -- Test whether the Available_View of a limited type view
7715 -- is tagged, since the limited view may not be marked as
7716 -- tagged if the type itself has an untagged incomplete
7717 -- type view in its package.
7718
7719 if From_Limited_With (T)
7720 and then not Is_Tagged_Type (Available_View (T))
7721 then
7722 Error_Msg_N
7723 ("prefix of Class attribute must be tagged", N);
7724 Set_Etype (N, Any_Type);
7725 Set_Entity (N, Any_Type);
7726 return;
7727
7728 -- ??? This test is temporarily disabled (always
7729 -- False) because it causes an unwanted warning on
7730 -- GNAT sources (built with -gnatg, which includes
7731 -- Warn_On_Obsolescent_ Feature). Once this issue
7732 -- is cleared in the sources, it can be enabled.
7733
7734 elsif Warn_On_Obsolescent_Feature and then False then
7735 Error_Msg_N
7736 ("applying 'Class to an untagged incomplete type"
7737 & " is an obsolescent feature (RM J.11)?r?", N);
7738 end if;
7739 end if;
7740
7741 Set_Is_Tagged_Type (T);
7742 Set_Direct_Primitive_Operations (T, New_Elmt_List);
7743 Make_Class_Wide_Type (T);
7744 Set_Entity (N, Class_Wide_Type (T));
7745 Set_Etype (N, Class_Wide_Type (T));
7746
7747 elsif Ekind (T) = E_Private_Type
7748 and then not Is_Generic_Type (T)
7749 and then In_Private_Part (Scope (T))
7750 then
7751 -- The Class attribute can be applied to an untagged private
7752 -- type fulfilled by a tagged type prior to the full type
7753 -- declaration (but only within the parent package's private
7754 -- part). Create the class-wide type now and check that the
7755 -- full type is tagged later during its analysis. Note that
7756 -- we do not mark the private type as tagged, unlike the
7757 -- case of incomplete types, because the type must still
7758 -- appear untagged to outside units.
7759
7760 if No (Class_Wide_Type (T)) then
7761 Make_Class_Wide_Type (T);
7762 end if;
7763
7764 Set_Entity (N, Class_Wide_Type (T));
7765 Set_Etype (N, Class_Wide_Type (T));
7766
7767 else
7768 -- Should we introduce a type Any_Tagged and use Wrong_Type
7769 -- here, it would be a bit more consistent???
7770
7771 Error_Msg_NE
7772 ("tagged type required, found}",
7773 Prefix (N), First_Subtype (T));
7774 Set_Entity (N, Any_Type);
7775 return;
7776 end if;
7777
7778 -- Case of tagged type
7779
7780 else
7781 if Is_Concurrent_Type (T) then
7782 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
7783
7784 -- Previous error. Create a class-wide type for the
7785 -- synchronized type itself, with minimal semantic
7786 -- attributes, to catch other errors in some ACATS tests.
7787
7788 pragma Assert (Serious_Errors_Detected /= 0);
7789 Make_Class_Wide_Type (T);
7790 C := Class_Wide_Type (T);
7791 Set_First_Entity (C, First_Entity (T));
7792
7793 else
7794 C := Class_Wide_Type
7795 (Corresponding_Record_Type (Entity (Prefix (N))));
7796 end if;
7797
7798 else
7799 C := Class_Wide_Type (Entity (Prefix (N)));
7800 end if;
7801
7802 Set_Entity_With_Checks (N, C);
7803 Generate_Reference (C, N);
7804 Set_Etype (N, C);
7805 end if;
7806
7807 -- Base attribute, not allowed in Ada 83
7808
7809 elsif Attribute_Name (N) = Name_Base then
7810 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
7811 Error_Msg_N
7812 ("(Ada 83) Base attribute not allowed in subtype mark", N);
7813
7814 else
7815 Find_Type (Prefix (N));
7816 Typ := Entity (Prefix (N));
7817
7818 if Ada_Version >= Ada_95
7819 and then not Is_Scalar_Type (Typ)
7820 and then not Is_Generic_Type (Typ)
7821 then
7822 Error_Msg_N
7823 ("prefix of Base attribute must be scalar type",
7824 Prefix (N));
7825
7826 elsif Warn_On_Redundant_Constructs
7827 and then Base_Type (Typ) = Typ
7828 then
7829 Error_Msg_NE -- CODEFIX
7830 ("redundant attribute, & is its own base type?r?", N, Typ);
7831 end if;
7832
7833 T := Base_Type (Typ);
7834
7835 -- Rewrite attribute reference with type itself (see similar
7836 -- processing in Analyze_Attribute, case Base). Preserve prefix
7837 -- if present, for other legality checks.
7838
7839 if Nkind (Prefix (N)) = N_Expanded_Name then
7840 Rewrite (N,
7841 Make_Expanded_Name (Sloc (N),
7842 Chars => Chars (T),
7843 Prefix => New_Copy (Prefix (Prefix (N))),
7844 Selector_Name => New_Occurrence_Of (T, Sloc (N))));
7845
7846 else
7847 Rewrite (N, New_Occurrence_Of (T, Sloc (N)));
7848 end if;
7849
7850 Set_Entity (N, T);
7851 Set_Etype (N, T);
7852 end if;
7853
7854 elsif Attribute_Name (N) = Name_Stub_Type then
7855
7856 -- This is handled in Analyze_Attribute
7857
7858 Analyze (N);
7859
7860 -- All other attributes are invalid in a subtype mark
7861
7862 else
7863 Error_Msg_N ("invalid attribute in subtype mark", N);
7864 end if;
7865
7866 else
7867 Analyze (N);
7868
7869 if Is_Entity_Name (N) then
7870 T_Name := Entity (N);
7871 else
7872 Error_Msg_N ("subtype mark required in this context", N);
7873 Set_Etype (N, Any_Type);
7874 return;
7875 end if;
7876
7877 if T_Name = Any_Id or else Etype (N) = Any_Type then
7878
7879 -- Undefined id. Make it into a valid type
7880
7881 Set_Entity (N, Any_Type);
7882
7883 elsif not Is_Type (T_Name)
7884 and then T_Name /= Standard_Void_Type
7885 then
7886 Error_Msg_Sloc := Sloc (T_Name);
7887 Error_Msg_N ("subtype mark required in this context", N);
7888 Error_Msg_NE ("\\found & declared#", N, T_Name);
7889 Set_Entity (N, Any_Type);
7890
7891 else
7892 -- If the type is an incomplete type created to handle
7893 -- anonymous access components of a record type, then the
7894 -- incomplete type is the visible entity and subsequent
7895 -- references will point to it. Mark the original full
7896 -- type as referenced, to prevent spurious warnings.
7897
7898 if Is_Incomplete_Type (T_Name)
7899 and then Present (Full_View (T_Name))
7900 and then not Comes_From_Source (T_Name)
7901 then
7902 Set_Referenced (Full_View (T_Name));
7903 end if;
7904
7905 T_Name := Get_Full_View (T_Name);
7906
7907 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
7908 -- limited-with clauses
7909
7910 if From_Limited_With (T_Name)
7911 and then Is_Incomplete_Type (T_Name)
7912 and then Present (Non_Limited_View (T_Name))
7913 and then Is_Interface (Non_Limited_View (T_Name))
7914 then
7915 T_Name := Non_Limited_View (T_Name);
7916 end if;
7917
7918 if In_Open_Scopes (T_Name) then
7919 if Ekind (Base_Type (T_Name)) = E_Task_Type then
7920
7921 -- In Ada 2005, a task name can be used in an access
7922 -- definition within its own body. It cannot be used
7923 -- in the discriminant part of the task declaration,
7924 -- nor anywhere else in the declaration because entries
7925 -- cannot have access parameters.
7926
7927 if Ada_Version >= Ada_2005
7928 and then Nkind (Parent (N)) = N_Access_Definition
7929 then
7930 Set_Entity (N, T_Name);
7931 Set_Etype (N, T_Name);
7932
7933 if Has_Completion (T_Name) then
7934 return;
7935
7936 else
7937 Error_Msg_N
7938 ("task type cannot be used as type mark " &
7939 "within its own declaration", N);
7940 end if;
7941
7942 else
7943 Error_Msg_N
7944 ("task type cannot be used as type mark " &
7945 "within its own spec or body", N);
7946 end if;
7947
7948 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
7949
7950 -- In Ada 2005, a protected name can be used in an access
7951 -- definition within its own body.
7952
7953 if Ada_Version >= Ada_2005
7954 and then Nkind (Parent (N)) = N_Access_Definition
7955 then
7956 Set_Entity (N, T_Name);
7957 Set_Etype (N, T_Name);
7958 return;
7959
7960 else
7961 Error_Msg_N
7962 ("protected type cannot be used as type mark " &
7963 "within its own spec or body", N);
7964 end if;
7965
7966 else
7967 Error_Msg_N ("type declaration cannot refer to itself", N);
7968 end if;
7969
7970 Set_Etype (N, Any_Type);
7971 Set_Entity (N, Any_Type);
7972 Set_Error_Posted (T_Name);
7973 return;
7974 end if;
7975
7976 Set_Entity (N, T_Name);
7977 Set_Etype (N, T_Name);
7978 end if;
7979 end if;
7980
7981 if Present (Etype (N)) and then Comes_From_Source (N) then
7982 if Is_Fixed_Point_Type (Etype (N)) then
7983 Check_Restriction (No_Fixed_Point, N);
7984 elsif Is_Floating_Point_Type (Etype (N)) then
7985 Check_Restriction (No_Floating_Point, N);
7986 end if;
7987
7988 -- A Ghost type must appear in a specific context
7989
7990 if Is_Ghost_Entity (Etype (N)) then
7991 Check_Ghost_Context (Etype (N), N);
7992 end if;
7993 end if;
7994 end Find_Type;
7995
7996 --------------------
7997 -- Has_Components --
7998 --------------------
7999
8000 function Has_Components (Typ : Entity_Id) return Boolean is
8001 begin
8002 return Is_Record_Type (Typ)
8003 or else (Is_Private_Type (Typ) and then Has_Discriminants (Typ))
8004 or else (Is_Task_Type (Typ) and then Has_Discriminants (Typ))
8005 or else (Is_Incomplete_Type (Typ)
8006 and then From_Limited_With (Typ)
8007 and then Is_Record_Type (Available_View (Typ)));
8008 end Has_Components;
8009
8010 ------------------------------------
8011 -- Has_Implicit_Character_Literal --
8012 ------------------------------------
8013
8014 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
8015 Id : Entity_Id;
8016 Found : Boolean := False;
8017 P : constant Entity_Id := Entity (Prefix (N));
8018 Priv_Id : Entity_Id := Empty;
8019
8020 begin
8021 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8022 Priv_Id := First_Private_Entity (P);
8023 end if;
8024
8025 if P = Standard_Standard then
8026 Change_Selected_Component_To_Expanded_Name (N);
8027 Rewrite (N, Selector_Name (N));
8028 Analyze (N);
8029 Set_Etype (Original_Node (N), Standard_Character);
8030 return True;
8031 end if;
8032
8033 Id := First_Entity (P);
8034 while Present (Id) and then Id /= Priv_Id loop
8035 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
8036
8037 -- We replace the node with the literal itself, resolve as a
8038 -- character, and set the type correctly.
8039
8040 if not Found then
8041 Change_Selected_Component_To_Expanded_Name (N);
8042 Rewrite (N, Selector_Name (N));
8043 Analyze (N);
8044 Set_Etype (N, Id);
8045 Set_Etype (Original_Node (N), Id);
8046 Found := True;
8047
8048 else
8049 -- More than one type derived from Character in given scope.
8050 -- Collect all possible interpretations.
8051
8052 Add_One_Interp (N, Id, Id);
8053 end if;
8054 end if;
8055
8056 Next_Entity (Id);
8057 end loop;
8058
8059 return Found;
8060 end Has_Implicit_Character_Literal;
8061
8062 ----------------------
8063 -- Has_Private_With --
8064 ----------------------
8065
8066 function Has_Private_With (E : Entity_Id) return Boolean is
8067 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
8068 Item : Node_Id;
8069
8070 begin
8071 Item := First (Context_Items (Comp_Unit));
8072 while Present (Item) loop
8073 if Nkind (Item) = N_With_Clause
8074 and then Private_Present (Item)
8075 and then Entity (Name (Item)) = E
8076 then
8077 return True;
8078 end if;
8079
8080 Next (Item);
8081 end loop;
8082
8083 return False;
8084 end Has_Private_With;
8085
8086 ---------------------------
8087 -- Has_Implicit_Operator --
8088 ---------------------------
8089
8090 function Has_Implicit_Operator (N : Node_Id) return Boolean is
8091 Op_Id : constant Name_Id := Chars (Selector_Name (N));
8092 P : constant Entity_Id := Entity (Prefix (N));
8093 Id : Entity_Id;
8094 Priv_Id : Entity_Id := Empty;
8095
8096 procedure Add_Implicit_Operator
8097 (T : Entity_Id;
8098 Op_Type : Entity_Id := Empty);
8099 -- Add implicit interpretation to node N, using the type for which a
8100 -- predefined operator exists. If the operator yields a boolean type,
8101 -- the Operand_Type is implicitly referenced by the operator, and a
8102 -- reference to it must be generated.
8103
8104 ---------------------------
8105 -- Add_Implicit_Operator --
8106 ---------------------------
8107
8108 procedure Add_Implicit_Operator
8109 (T : Entity_Id;
8110 Op_Type : Entity_Id := Empty)
8111 is
8112 Predef_Op : Entity_Id;
8113
8114 begin
8115 Predef_Op := Current_Entity (Selector_Name (N));
8116 while Present (Predef_Op)
8117 and then Scope (Predef_Op) /= Standard_Standard
8118 loop
8119 Predef_Op := Homonym (Predef_Op);
8120 end loop;
8121
8122 if Nkind (N) = N_Selected_Component then
8123 Change_Selected_Component_To_Expanded_Name (N);
8124 end if;
8125
8126 -- If the context is an unanalyzed function call, determine whether
8127 -- a binary or unary interpretation is required.
8128
8129 if Nkind (Parent (N)) = N_Indexed_Component then
8130 declare
8131 Is_Binary_Call : constant Boolean :=
8132 Present
8133 (Next (First (Expressions (Parent (N)))));
8134 Is_Binary_Op : constant Boolean :=
8135 First_Entity
8136 (Predef_Op) /= Last_Entity (Predef_Op);
8137 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
8138
8139 begin
8140 if Is_Binary_Call then
8141 if Is_Binary_Op then
8142 Add_One_Interp (N, Predef_Op, T);
8143 else
8144 Add_One_Interp (N, Predef_Op2, T);
8145 end if;
8146
8147 else
8148 if not Is_Binary_Op then
8149 Add_One_Interp (N, Predef_Op, T);
8150 else
8151 Add_One_Interp (N, Predef_Op2, T);
8152 end if;
8153 end if;
8154 end;
8155
8156 else
8157 Add_One_Interp (N, Predef_Op, T);
8158
8159 -- For operators with unary and binary interpretations, if
8160 -- context is not a call, add both
8161
8162 if Present (Homonym (Predef_Op)) then
8163 Add_One_Interp (N, Homonym (Predef_Op), T);
8164 end if;
8165 end if;
8166
8167 -- The node is a reference to a predefined operator, and
8168 -- an implicit reference to the type of its operands.
8169
8170 if Present (Op_Type) then
8171 Generate_Operator_Reference (N, Op_Type);
8172 else
8173 Generate_Operator_Reference (N, T);
8174 end if;
8175 end Add_Implicit_Operator;
8176
8177 -- Start of processing for Has_Implicit_Operator
8178
8179 begin
8180 if Ekind (P) = E_Package and then not In_Open_Scopes (P) then
8181 Priv_Id := First_Private_Entity (P);
8182 end if;
8183
8184 Id := First_Entity (P);
8185
8186 case Op_Id is
8187
8188 -- Boolean operators: an implicit declaration exists if the scope
8189 -- contains a declaration for a derived Boolean type, or for an
8190 -- array of Boolean type.
8191
8192 when Name_Op_And
8193 | Name_Op_Not
8194 | Name_Op_Or
8195 | Name_Op_Xor
8196 =>
8197 while Id /= Priv_Id loop
8198 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
8199 Add_Implicit_Operator (Id);
8200 return True;
8201 end if;
8202
8203 Next_Entity (Id);
8204 end loop;
8205
8206 -- Equality: look for any non-limited type (result is Boolean)
8207
8208 when Name_Op_Eq
8209 | Name_Op_Ne
8210 =>
8211 while Id /= Priv_Id loop
8212 if Is_Type (Id)
8213 and then not Is_Limited_Type (Id)
8214 and then Is_Base_Type (Id)
8215 then
8216 Add_Implicit_Operator (Standard_Boolean, Id);
8217 return True;
8218 end if;
8219
8220 Next_Entity (Id);
8221 end loop;
8222
8223 -- Comparison operators: scalar type, or array of scalar
8224
8225 when Name_Op_Ge
8226 | Name_Op_Gt
8227 | Name_Op_Le
8228 | Name_Op_Lt
8229 =>
8230 while Id /= Priv_Id loop
8231 if (Is_Scalar_Type (Id)
8232 or else (Is_Array_Type (Id)
8233 and then Is_Scalar_Type (Component_Type (Id))))
8234 and then Is_Base_Type (Id)
8235 then
8236 Add_Implicit_Operator (Standard_Boolean, Id);
8237 return True;
8238 end if;
8239
8240 Next_Entity (Id);
8241 end loop;
8242
8243 -- Arithmetic operators: any numeric type
8244
8245 when Name_Op_Abs
8246 | Name_Op_Add
8247 | Name_Op_Divide
8248 | Name_Op_Expon
8249 | Name_Op_Mod
8250 | Name_Op_Multiply
8251 | Name_Op_Rem
8252 | Name_Op_Subtract
8253 =>
8254 while Id /= Priv_Id loop
8255 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
8256 Add_Implicit_Operator (Id);
8257 return True;
8258 end if;
8259
8260 Next_Entity (Id);
8261 end loop;
8262
8263 -- Concatenation: any one-dimensional array type
8264
8265 when Name_Op_Concat =>
8266 while Id /= Priv_Id loop
8267 if Is_Array_Type (Id)
8268 and then Number_Dimensions (Id) = 1
8269 and then Is_Base_Type (Id)
8270 then
8271 Add_Implicit_Operator (Id);
8272 return True;
8273 end if;
8274
8275 Next_Entity (Id);
8276 end loop;
8277
8278 -- What is the others condition here? Should we be using a
8279 -- subtype of Name_Id that would restrict to operators ???
8280
8281 when others =>
8282 null;
8283 end case;
8284
8285 -- If we fall through, then we do not have an implicit operator
8286
8287 return False;
8288 end Has_Implicit_Operator;
8289
8290 -----------------------------------
8291 -- Has_Loop_In_Inner_Open_Scopes --
8292 -----------------------------------
8293
8294 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
8295 begin
8296 -- Several scope stacks are maintained by Scope_Stack. The base of the
8297 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8298 -- flag in the scope stack entry. Note that the scope stacks used to
8299 -- simply be delimited implicitly by the presence of Standard_Standard
8300 -- at their base, but there now are cases where this is not sufficient
8301 -- because Standard_Standard actually may appear in the middle of the
8302 -- active set of scopes.
8303
8304 for J in reverse 0 .. Scope_Stack.Last loop
8305
8306 -- S was reached without seing a loop scope first
8307
8308 if Scope_Stack.Table (J).Entity = S then
8309 return False;
8310
8311 -- S was not yet reached, so it contains at least one inner loop
8312
8313 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
8314 return True;
8315 end if;
8316
8317 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8318 -- cases where Standard_Standard appears in the middle of the active
8319 -- set of scopes. This affects the declaration and overriding of
8320 -- private inherited operations in instantiations of generic child
8321 -- units.
8322
8323 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
8324 end loop;
8325
8326 raise Program_Error; -- unreachable
8327 end Has_Loop_In_Inner_Open_Scopes;
8328
8329 --------------------
8330 -- In_Open_Scopes --
8331 --------------------
8332
8333 function In_Open_Scopes (S : Entity_Id) return Boolean is
8334 begin
8335 -- Several scope stacks are maintained by Scope_Stack. The base of the
8336 -- currently active scope stack is denoted by the Is_Active_Stack_Base
8337 -- flag in the scope stack entry. Note that the scope stacks used to
8338 -- simply be delimited implicitly by the presence of Standard_Standard
8339 -- at their base, but there now are cases where this is not sufficient
8340 -- because Standard_Standard actually may appear in the middle of the
8341 -- active set of scopes.
8342
8343 for J in reverse 0 .. Scope_Stack.Last loop
8344 if Scope_Stack.Table (J).Entity = S then
8345 return True;
8346 end if;
8347
8348 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
8349 -- cases where Standard_Standard appears in the middle of the active
8350 -- set of scopes. This affects the declaration and overriding of
8351 -- private inherited operations in instantiations of generic child
8352 -- units.
8353
8354 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
8355 end loop;
8356
8357 return False;
8358 end In_Open_Scopes;
8359
8360 -----------------------------
8361 -- Inherit_Renamed_Profile --
8362 -----------------------------
8363
8364 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
8365 New_F : Entity_Id;
8366 Old_F : Entity_Id;
8367 Old_T : Entity_Id;
8368 New_T : Entity_Id;
8369
8370 begin
8371 if Ekind (Old_S) = E_Operator then
8372 New_F := First_Formal (New_S);
8373
8374 while Present (New_F) loop
8375 Set_Etype (New_F, Base_Type (Etype (New_F)));
8376 Next_Formal (New_F);
8377 end loop;
8378
8379 Set_Etype (New_S, Base_Type (Etype (New_S)));
8380
8381 else
8382 New_F := First_Formal (New_S);
8383 Old_F := First_Formal (Old_S);
8384
8385 while Present (New_F) loop
8386 New_T := Etype (New_F);
8387 Old_T := Etype (Old_F);
8388
8389 -- If the new type is a renaming of the old one, as is the case
8390 -- for actuals in instances, retain its name, to simplify later
8391 -- disambiguation.
8392
8393 if Nkind (Parent (New_T)) = N_Subtype_Declaration
8394 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
8395 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
8396 then
8397 null;
8398 else
8399 Set_Etype (New_F, Old_T);
8400 end if;
8401
8402 Next_Formal (New_F);
8403 Next_Formal (Old_F);
8404 end loop;
8405
8406 pragma Assert (No (Old_F));
8407
8408 if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then
8409 Set_Etype (New_S, Etype (Old_S));
8410 end if;
8411 end if;
8412 end Inherit_Renamed_Profile;
8413
8414 ----------------
8415 -- Initialize --
8416 ----------------
8417
8418 procedure Initialize is
8419 begin
8420 Urefs.Init;
8421 end Initialize;
8422
8423 -------------------------
8424 -- Install_Use_Clauses --
8425 -------------------------
8426
8427 procedure Install_Use_Clauses
8428 (Clause : Node_Id;
8429 Force_Installation : Boolean := False)
8430 is
8431 U : Node_Id;
8432
8433 begin
8434 U := Clause;
8435 while Present (U) loop
8436
8437 -- Case of USE package
8438
8439 if Nkind (U) = N_Use_Package_Clause then
8440 Use_One_Package (U, Name (U), True);
8441
8442 -- Case of USE TYPE
8443
8444 else
8445 Use_One_Type (Subtype_Mark (U), Force => Force_Installation);
8446
8447 end if;
8448
8449 Next_Use_Clause (U);
8450 end loop;
8451 end Install_Use_Clauses;
8452
8453 ----------------------
8454 -- Mark_Use_Clauses --
8455 ----------------------
8456
8457 procedure Mark_Use_Clauses (Id : Node_Or_Entity_Id) is
8458 procedure Mark_Parameters (Call : Entity_Id);
8459 -- Perform use_type_clause marking for all parameters in a subprogram
8460 -- or operator call.
8461
8462 procedure Mark_Use_Package (Pak : Entity_Id);
8463 -- Move up the Prev_Use_Clause chain for packages denoted by Pak -
8464 -- marking each clause in the chain as effective in the process.
8465
8466 procedure Mark_Use_Type (E : Entity_Id);
8467 -- Similar to Do_Use_Package_Marking except we move up the
8468 -- Prev_Use_Clause chain for the type denoted by E.
8469
8470 ---------------------
8471 -- Mark_Parameters --
8472 ---------------------
8473
8474 procedure Mark_Parameters (Call : Entity_Id) is
8475 Curr : Node_Id;
8476
8477 begin
8478 -- Move through all of the formals
8479
8480 Curr := First_Formal (Call);
8481 while Present (Curr) loop
8482 Mark_Use_Type (Curr);
8483
8484 Next_Formal (Curr);
8485 end loop;
8486
8487 -- Handle the return type
8488
8489 Mark_Use_Type (Call);
8490 end Mark_Parameters;
8491
8492 ----------------------
8493 -- Mark_Use_Package --
8494 ----------------------
8495
8496 procedure Mark_Use_Package (Pak : Entity_Id) is
8497 Curr : Node_Id;
8498
8499 begin
8500 -- Ignore cases where the scope of the type is not a package (e.g.
8501 -- Standard_Standard).
8502
8503 if Ekind (Pak) /= E_Package then
8504 return;
8505 end if;
8506
8507 Curr := Current_Use_Clause (Pak);
8508 while Present (Curr)
8509 and then not Is_Effective_Use_Clause (Curr)
8510 loop
8511 -- We need to mark the previous use clauses as effective, but
8512 -- each use clause may in turn render other use_package_clauses
8513 -- effective. Additionally, it is possible to have a parent
8514 -- package renamed as a child of itself so we must check the
8515 -- prefix entity is not the same as the package we are marking.
8516
8517 if Nkind (Name (Curr)) /= N_Identifier
8518 and then Present (Prefix (Name (Curr)))
8519 and then Entity (Prefix (Name (Curr))) /= Pak
8520 then
8521 Mark_Use_Package (Entity (Prefix (Name (Curr))));
8522
8523 -- It is also possible to have a child package without a prefix
8524 -- that relies on a previous use_package_clause.
8525
8526 elsif Nkind (Name (Curr)) = N_Identifier
8527 and then Is_Child_Unit (Entity (Name (Curr)))
8528 then
8529 Mark_Use_Package (Scope (Entity (Name (Curr))));
8530 end if;
8531
8532 -- Mark the use_package_clause as effective and move up the chain
8533
8534 Set_Is_Effective_Use_Clause (Curr);
8535
8536 Curr := Prev_Use_Clause (Curr);
8537 end loop;
8538 end Mark_Use_Package;
8539
8540 -------------------
8541 -- Mark_Use_Type --
8542 -------------------
8543
8544 procedure Mark_Use_Type (E : Entity_Id) is
8545 Curr : Node_Id;
8546 Base : Entity_Id;
8547
8548 begin
8549 -- Ignore void types and unresolved string literals and primitives
8550
8551 if Nkind (E) = N_String_Literal
8552 or else Nkind (Etype (E)) not in N_Entity
8553 or else not Is_Type (Etype (E))
8554 then
8555 return;
8556 end if;
8557
8558 -- Primitives with class-wide operands might additionally render
8559 -- their base type's use_clauses effective - so do a recursive check
8560 -- here.
8561
8562 Base := Base_Type (Etype (E));
8563
8564 if Ekind (Base) = E_Class_Wide_Type then
8565 Mark_Use_Type (Base);
8566 end if;
8567
8568 -- The package containing the type or operator function being used
8569 -- may be in use as well, so mark any use_package_clauses for it as
8570 -- effective. There are also additional sanity checks performed here
8571 -- for ignoring previous errors.
8572
8573 Mark_Use_Package (Scope (Base));
8574
8575 if Nkind (E) in N_Op
8576 and then Present (Entity (E))
8577 and then Present (Scope (Entity (E)))
8578 then
8579 Mark_Use_Package (Scope (Entity (E)));
8580 end if;
8581
8582 Curr := Current_Use_Clause (Base);
8583 while Present (Curr)
8584 and then not Is_Effective_Use_Clause (Curr)
8585 loop
8586 -- Current use_type_clause may render other use_package_clauses
8587 -- effective.
8588
8589 if Nkind (Subtype_Mark (Curr)) /= N_Identifier
8590 and then Present (Prefix (Subtype_Mark (Curr)))
8591 then
8592 Mark_Use_Package (Entity (Prefix (Subtype_Mark (Curr))));
8593 end if;
8594
8595 -- Mark the use_type_clause as effective and move up the chain
8596
8597 Set_Is_Effective_Use_Clause (Curr);
8598
8599 Curr := Prev_Use_Clause (Curr);
8600 end loop;
8601 end Mark_Use_Type;
8602
8603 -- Start of processing for Mark_Use_Clauses
8604
8605 begin
8606 -- Use clauses in and of themselves do not count as a "use" of a
8607 -- package.
8608
8609 if Nkind_In (Parent (Id), N_Use_Package_Clause, N_Use_Type_Clause) then
8610 return;
8611 end if;
8612
8613 -- Handle entities
8614
8615 if Nkind (Id) in N_Entity then
8616
8617 -- Mark the entity's package
8618
8619 if Is_Potentially_Use_Visible (Id) then
8620 Mark_Use_Package (Scope (Id));
8621 end if;
8622
8623 -- Mark enumeration literals
8624
8625 if Ekind (Id) = E_Enumeration_Literal then
8626 Mark_Use_Type (Id);
8627
8628 -- Mark primitives
8629
8630 elsif (Ekind (Id) in Overloadable_Kind
8631 or else Ekind_In (Id, E_Generic_Function,
8632 E_Generic_Procedure))
8633 and then (Is_Potentially_Use_Visible (Id)
8634 or else Is_Intrinsic_Subprogram (Id)
8635 or else (Ekind_In (Id, E_Function, E_Procedure)
8636 and then Is_Generic_Actual_Subprogram (Id)))
8637 then
8638 Mark_Parameters (Id);
8639 end if;
8640
8641 -- Handle nodes
8642
8643 else
8644 -- Mark operators
8645
8646 if Nkind (Id) in N_Op then
8647
8648 -- At this point the left operand may not be resolved if we are
8649 -- encountering multiple operators next to eachother in an
8650 -- expression.
8651
8652 if Nkind (Id) in N_Binary_Op
8653 and then not (Nkind (Left_Opnd (Id)) in N_Op)
8654 then
8655 Mark_Use_Type (Left_Opnd (Id));
8656 end if;
8657
8658 Mark_Use_Type (Right_Opnd (Id));
8659 Mark_Use_Type (Id);
8660
8661 -- Mark entity identifiers
8662
8663 elsif Nkind (Id) in N_Has_Entity
8664 and then (Is_Potentially_Use_Visible (Entity (Id))
8665 or else (Is_Generic_Instance (Entity (Id))
8666 and then Is_Immediately_Visible (Entity (Id))))
8667 then
8668 -- Ignore fully qualified names as they do not count as a "use" of
8669 -- a package.
8670
8671 if Nkind_In (Id, N_Identifier, N_Operator_Symbol)
8672 or else (Present (Prefix (Id))
8673 and then Scope (Entity (Id)) /= Entity (Prefix (Id)))
8674 then
8675 Mark_Use_Clauses (Entity (Id));
8676 end if;
8677 end if;
8678 end if;
8679 end Mark_Use_Clauses;
8680
8681 --------------------------------
8682 -- Most_Descendant_Use_Clause --
8683 --------------------------------
8684
8685 function Most_Descendant_Use_Clause
8686 (Clause1 : Entity_Id;
8687 Clause2 : Entity_Id) return Entity_Id
8688 is
8689 Scope1 : Entity_Id;
8690 Scope2 : Entity_Id;
8691
8692 begin
8693 if Clause1 = Clause2 then
8694 return Clause1;
8695 end if;
8696
8697 -- We determine which one is the most descendant by the scope distance
8698 -- to the ultimate parent unit.
8699
8700 Scope1 := Entity_Of_Unit (Unit (Parent (Clause1)));
8701 Scope2 := Entity_Of_Unit (Unit (Parent (Clause2)));
8702 while Scope1 /= Standard_Standard
8703 and then Scope2 /= Standard_Standard
8704 loop
8705 Scope1 := Scope (Scope1);
8706 Scope2 := Scope (Scope2);
8707
8708 if not Present (Scope1) then
8709 return Clause1;
8710 elsif not Present (Scope2) then
8711 return Clause2;
8712 end if;
8713 end loop;
8714
8715 if Scope1 = Standard_Standard then
8716 return Clause1;
8717 end if;
8718
8719 return Clause2;
8720 end Most_Descendant_Use_Clause;
8721
8722 ---------------
8723 -- Pop_Scope --
8724 ---------------
8725
8726 procedure Pop_Scope is
8727 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8728 S : constant Entity_Id := SST.Entity;
8729
8730 begin
8731 if Debug_Flag_E then
8732 Write_Info;
8733 end if;
8734
8735 -- Set Default_Storage_Pool field of the library unit if necessary
8736
8737 if Is_Package_Or_Generic_Package (S)
8738 and then
8739 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
8740 then
8741 declare
8742 Aux : constant Node_Id :=
8743 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
8744 begin
8745 if No (Default_Storage_Pool (Aux)) then
8746 Set_Default_Storage_Pool (Aux, Default_Pool);
8747 end if;
8748 end;
8749 end if;
8750
8751 Scope_Suppress := SST.Save_Scope_Suppress;
8752 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
8753 Check_Policy_List := SST.Save_Check_Policy_List;
8754 Default_Pool := SST.Save_Default_Storage_Pool;
8755 No_Tagged_Streams := SST.Save_No_Tagged_Streams;
8756 SPARK_Mode := SST.Save_SPARK_Mode;
8757 SPARK_Mode_Pragma := SST.Save_SPARK_Mode_Pragma;
8758 Default_SSO := SST.Save_Default_SSO;
8759 Uneval_Old := SST.Save_Uneval_Old;
8760
8761 if Debug_Flag_W then
8762 Write_Str ("<-- exiting scope: ");
8763 Write_Name (Chars (Current_Scope));
8764 Write_Str (", Depth=");
8765 Write_Int (Int (Scope_Stack.Last));
8766 Write_Eol;
8767 end if;
8768
8769 End_Use_Clauses (SST.First_Use_Clause);
8770
8771 -- If the actions to be wrapped are still there they will get lost
8772 -- causing incomplete code to be generated. It is better to abort in
8773 -- this case (and we do the abort even with assertions off since the
8774 -- penalty is incorrect code generation).
8775
8776 if SST.Actions_To_Be_Wrapped /= Scope_Actions'(others => No_List) then
8777 raise Program_Error;
8778 end if;
8779
8780 -- Free last subprogram name if allocated, and pop scope
8781
8782 Free (SST.Last_Subprogram_Name);
8783 Scope_Stack.Decrement_Last;
8784 end Pop_Scope;
8785
8786 ----------------
8787 -- Push_Scope --
8788 ----------------
8789
8790 procedure Push_Scope (S : Entity_Id) is
8791 E : constant Entity_Id := Scope (S);
8792
8793 begin
8794 if Ekind (S) = E_Void then
8795 null;
8796
8797 -- Set scope depth if not a non-concurrent type, and we have not yet set
8798 -- the scope depth. This means that we have the first occurrence of the
8799 -- scope, and this is where the depth is set.
8800
8801 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
8802 and then not Scope_Depth_Set (S)
8803 then
8804 if S = Standard_Standard then
8805 Set_Scope_Depth_Value (S, Uint_0);
8806
8807 elsif Is_Child_Unit (S) then
8808 Set_Scope_Depth_Value (S, Uint_1);
8809
8810 elsif not Is_Record_Type (Current_Scope) then
8811 if Ekind (S) = E_Loop then
8812 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
8813 else
8814 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
8815 end if;
8816 end if;
8817 end if;
8818
8819 Scope_Stack.Increment_Last;
8820
8821 declare
8822 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
8823
8824 begin
8825 SST.Entity := S;
8826 SST.Save_Scope_Suppress := Scope_Suppress;
8827 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
8828 SST.Save_Check_Policy_List := Check_Policy_List;
8829 SST.Save_Default_Storage_Pool := Default_Pool;
8830 SST.Save_No_Tagged_Streams := No_Tagged_Streams;
8831 SST.Save_SPARK_Mode := SPARK_Mode;
8832 SST.Save_SPARK_Mode_Pragma := SPARK_Mode_Pragma;
8833 SST.Save_Default_SSO := Default_SSO;
8834 SST.Save_Uneval_Old := Uneval_Old;
8835
8836 -- Each new scope pushed onto the scope stack inherits the component
8837 -- alignment of the previous scope. This emulates the "visibility"
8838 -- semantics of pragma Component_Alignment.
8839
8840 if Scope_Stack.Last > Scope_Stack.First then
8841 SST.Component_Alignment_Default :=
8842 Scope_Stack.Table
8843 (Scope_Stack.Last - 1).Component_Alignment_Default;
8844
8845 -- Otherwise, this is the first scope being pushed on the scope
8846 -- stack. Inherit the component alignment from the configuration
8847 -- form of pragma Component_Alignment (if any).
8848
8849 else
8850 SST.Component_Alignment_Default :=
8851 Configuration_Component_Alignment;
8852 end if;
8853
8854 SST.Last_Subprogram_Name := null;
8855 SST.Is_Transient := False;
8856 SST.Node_To_Be_Wrapped := Empty;
8857 SST.Pending_Freeze_Actions := No_List;
8858 SST.Actions_To_Be_Wrapped := (others => No_List);
8859 SST.First_Use_Clause := Empty;
8860 SST.Is_Active_Stack_Base := False;
8861 SST.Previous_Visibility := False;
8862 SST.Locked_Shared_Objects := No_Elist;
8863 end;
8864
8865 if Debug_Flag_W then
8866 Write_Str ("--> new scope: ");
8867 Write_Name (Chars (Current_Scope));
8868 Write_Str (", Id=");
8869 Write_Int (Int (Current_Scope));
8870 Write_Str (", Depth=");
8871 Write_Int (Int (Scope_Stack.Last));
8872 Write_Eol;
8873 end if;
8874
8875 -- Deal with copying flags from the previous scope to this one. This is
8876 -- not necessary if either scope is standard, or if the new scope is a
8877 -- child unit.
8878
8879 if S /= Standard_Standard
8880 and then Scope (S) /= Standard_Standard
8881 and then not Is_Child_Unit (S)
8882 then
8883 if Nkind (E) not in N_Entity then
8884 return;
8885 end if;
8886
8887 -- Copy categorization flags from Scope (S) to S, this is not done
8888 -- when Scope (S) is Standard_Standard since propagation is from
8889 -- library unit entity inwards. Copy other relevant attributes as
8890 -- well (Discard_Names in particular).
8891
8892 -- We only propagate inwards for library level entities,
8893 -- inner level subprograms do not inherit the categorization.
8894
8895 if Is_Library_Level_Entity (S) then
8896 Set_Is_Preelaborated (S, Is_Preelaborated (E));
8897 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
8898 Set_Discard_Names (S, Discard_Names (E));
8899 Set_Suppress_Value_Tracking_On_Call
8900 (S, Suppress_Value_Tracking_On_Call (E));
8901 Set_Categorization_From_Scope (E => S, Scop => E);
8902 end if;
8903 end if;
8904
8905 if Is_Child_Unit (S)
8906 and then Present (E)
8907 and then Is_Package_Or_Generic_Package (E)
8908 and then
8909 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
8910 then
8911 declare
8912 Aux : constant Node_Id :=
8913 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
8914 begin
8915 if Present (Default_Storage_Pool (Aux)) then
8916 Default_Pool := Default_Storage_Pool (Aux);
8917 end if;
8918 end;
8919 end if;
8920 end Push_Scope;
8921
8922 ---------------------
8923 -- Premature_Usage --
8924 ---------------------
8925
8926 procedure Premature_Usage (N : Node_Id) is
8927 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
8928 E : Entity_Id := Entity (N);
8929
8930 begin
8931 -- Within an instance, the analysis of the actual for a formal object
8932 -- does not see the name of the object itself. This is significant only
8933 -- if the object is an aggregate, where its analysis does not do any
8934 -- name resolution on component associations. (see 4717-008). In such a
8935 -- case, look for the visible homonym on the chain.
8936
8937 if In_Instance and then Present (Homonym (E)) then
8938 E := Homonym (E);
8939 while Present (E) and then not In_Open_Scopes (Scope (E)) loop
8940 E := Homonym (E);
8941 end loop;
8942
8943 if Present (E) then
8944 Set_Entity (N, E);
8945 Set_Etype (N, Etype (E));
8946 return;
8947 end if;
8948 end if;
8949
8950 if Kind = N_Component_Declaration then
8951 Error_Msg_N
8952 ("component&! cannot be used before end of record declaration", N);
8953
8954 elsif Kind = N_Parameter_Specification then
8955 Error_Msg_N
8956 ("formal parameter&! cannot be used before end of specification",
8957 N);
8958
8959 elsif Kind = N_Discriminant_Specification then
8960 Error_Msg_N
8961 ("discriminant&! cannot be used before end of discriminant part",
8962 N);
8963
8964 elsif Kind = N_Procedure_Specification
8965 or else Kind = N_Function_Specification
8966 then
8967 Error_Msg_N
8968 ("subprogram&! cannot be used before end of its declaration",
8969 N);
8970
8971 elsif Kind = N_Full_Type_Declaration then
8972 Error_Msg_N
8973 ("type& cannot be used before end of its declaration!", N);
8974
8975 else
8976 Error_Msg_N
8977 ("object& cannot be used before end of its declaration!", N);
8978
8979 -- If the premature reference appears as the expression in its own
8980 -- declaration, rewrite it to prevent compiler loops in subsequent
8981 -- uses of this mangled declaration in address clauses.
8982
8983 if Nkind (Parent (N)) = N_Object_Declaration then
8984 Set_Entity (N, Any_Id);
8985 end if;
8986 end if;
8987 end Premature_Usage;
8988
8989 ------------------------
8990 -- Present_System_Aux --
8991 ------------------------
8992
8993 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
8994 Loc : Source_Ptr;
8995 Aux_Name : Unit_Name_Type;
8996 Unum : Unit_Number_Type;
8997 Withn : Node_Id;
8998 With_Sys : Node_Id;
8999 The_Unit : Node_Id;
9000
9001 function Find_System (C_Unit : Node_Id) return Entity_Id;
9002 -- Scan context clause of compilation unit to find with_clause
9003 -- for System.
9004
9005 -----------------
9006 -- Find_System --
9007 -----------------
9008
9009 function Find_System (C_Unit : Node_Id) return Entity_Id is
9010 With_Clause : Node_Id;
9011
9012 begin
9013 With_Clause := First (Context_Items (C_Unit));
9014 while Present (With_Clause) loop
9015 if (Nkind (With_Clause) = N_With_Clause
9016 and then Chars (Name (With_Clause)) = Name_System)
9017 and then Comes_From_Source (With_Clause)
9018 then
9019 return With_Clause;
9020 end if;
9021
9022 Next (With_Clause);
9023 end loop;
9024
9025 return Empty;
9026 end Find_System;
9027
9028 -- Start of processing for Present_System_Aux
9029
9030 begin
9031 -- The child unit may have been loaded and analyzed already
9032
9033 if Present (System_Aux_Id) then
9034 return True;
9035
9036 -- If no previous pragma for System.Aux, nothing to load
9037
9038 elsif No (System_Extend_Unit) then
9039 return False;
9040
9041 -- Use the unit name given in the pragma to retrieve the unit.
9042 -- Verify that System itself appears in the context clause of the
9043 -- current compilation. If System is not present, an error will
9044 -- have been reported already.
9045
9046 else
9047 With_Sys := Find_System (Cunit (Current_Sem_Unit));
9048
9049 The_Unit := Unit (Cunit (Current_Sem_Unit));
9050
9051 if No (With_Sys)
9052 and then
9053 (Nkind (The_Unit) = N_Package_Body
9054 or else (Nkind (The_Unit) = N_Subprogram_Body
9055 and then not Acts_As_Spec (Cunit (Current_Sem_Unit))))
9056 then
9057 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
9058 end if;
9059
9060 if No (With_Sys) and then Present (N) then
9061
9062 -- If we are compiling a subunit, we need to examine its
9063 -- context as well (Current_Sem_Unit is the parent unit);
9064
9065 The_Unit := Parent (N);
9066 while Nkind (The_Unit) /= N_Compilation_Unit loop
9067 The_Unit := Parent (The_Unit);
9068 end loop;
9069
9070 if Nkind (Unit (The_Unit)) = N_Subunit then
9071 With_Sys := Find_System (The_Unit);
9072 end if;
9073 end if;
9074
9075 if No (With_Sys) then
9076 return False;
9077 end if;
9078
9079 Loc := Sloc (With_Sys);
9080 Get_Name_String (Chars (Expression (System_Extend_Unit)));
9081 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
9082 Name_Buffer (1 .. 7) := "system.";
9083 Name_Buffer (Name_Len + 8) := '%';
9084 Name_Buffer (Name_Len + 9) := 's';
9085 Name_Len := Name_Len + 9;
9086 Aux_Name := Name_Find;
9087
9088 Unum :=
9089 Load_Unit
9090 (Load_Name => Aux_Name,
9091 Required => False,
9092 Subunit => False,
9093 Error_Node => With_Sys);
9094
9095 if Unum /= No_Unit then
9096 Semantics (Cunit (Unum));
9097 System_Aux_Id :=
9098 Defining_Entity (Specification (Unit (Cunit (Unum))));
9099
9100 Withn :=
9101 Make_With_Clause (Loc,
9102 Name =>
9103 Make_Expanded_Name (Loc,
9104 Chars => Chars (System_Aux_Id),
9105 Prefix =>
9106 New_Occurrence_Of (Scope (System_Aux_Id), Loc),
9107 Selector_Name => New_Occurrence_Of (System_Aux_Id, Loc)));
9108
9109 Set_Entity (Name (Withn), System_Aux_Id);
9110
9111 Set_Corresponding_Spec (Withn, System_Aux_Id);
9112 Set_First_Name (Withn);
9113 Set_Implicit_With (Withn);
9114 Set_Library_Unit (Withn, Cunit (Unum));
9115
9116 Insert_After (With_Sys, Withn);
9117 Mark_Rewrite_Insertion (Withn);
9118 Set_Context_Installed (Withn);
9119
9120 return True;
9121
9122 -- Here if unit load failed
9123
9124 else
9125 Error_Msg_Name_1 := Name_System;
9126 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
9127 Error_Msg_N
9128 ("extension package `%.%` does not exist",
9129 Opt.System_Extend_Unit);
9130 return False;
9131 end if;
9132 end if;
9133 end Present_System_Aux;
9134
9135 -------------------------
9136 -- Restore_Scope_Stack --
9137 -------------------------
9138
9139 procedure Restore_Scope_Stack
9140 (List : Elist_Id;
9141 Handle_Use : Boolean := True)
9142 is
9143 SS_Last : constant Int := Scope_Stack.Last;
9144 Elmt : Elmt_Id;
9145
9146 begin
9147 -- Restore visibility of previous scope stack, if any, using the list
9148 -- we saved (we use Remove, since this list will not be used again).
9149
9150 loop
9151 Elmt := Last_Elmt (List);
9152 exit when Elmt = No_Elmt;
9153 Set_Is_Immediately_Visible (Node (Elmt));
9154 Remove_Last_Elmt (List);
9155 end loop;
9156
9157 -- Restore use clauses
9158
9159 if SS_Last >= Scope_Stack.First
9160 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9161 and then Handle_Use
9162 then
9163 Install_Use_Clauses
9164 (Scope_Stack.Table (SS_Last).First_Use_Clause,
9165 Force_Installation => True);
9166 end if;
9167 end Restore_Scope_Stack;
9168
9169 ----------------------
9170 -- Save_Scope_Stack --
9171 ----------------------
9172
9173 -- Save_Scope_Stack/Restore_Scope_Stack were originally designed to avoid
9174 -- consuming any memory. That is, Save_Scope_Stack took care of removing
9175 -- from immediate visibility entities and Restore_Scope_Stack took care
9176 -- of restoring their visibility analyzing the context of each entity. The
9177 -- problem of such approach is that it was fragile and caused unexpected
9178 -- visibility problems, and indeed one test was found where there was a
9179 -- real problem.
9180
9181 -- Furthermore, the following experiment was carried out:
9182
9183 -- - Save_Scope_Stack was modified to store in an Elist1 all those
9184 -- entities whose attribute Is_Immediately_Visible is modified
9185 -- from True to False.
9186
9187 -- - Restore_Scope_Stack was modified to store in another Elist2
9188 -- all the entities whose attribute Is_Immediately_Visible is
9189 -- modified from False to True.
9190
9191 -- - Extra code was added to verify that all the elements of Elist1
9192 -- are found in Elist2
9193
9194 -- This test shows that there may be more occurrences of this problem which
9195 -- have not yet been detected. As a result, we replaced that approach by
9196 -- the current one in which Save_Scope_Stack returns the list of entities
9197 -- whose visibility is changed, and that list is passed to Restore_Scope_
9198 -- Stack to undo that change. This approach is simpler and safer, although
9199 -- it consumes more memory.
9200
9201 function Save_Scope_Stack (Handle_Use : Boolean := True) return Elist_Id is
9202 Result : constant Elist_Id := New_Elmt_List;
9203 E : Entity_Id;
9204 S : Entity_Id;
9205 SS_Last : constant Int := Scope_Stack.Last;
9206
9207 procedure Remove_From_Visibility (E : Entity_Id);
9208 -- If E is immediately visible then append it to the result and remove
9209 -- it temporarily from visibility.
9210
9211 ----------------------------
9212 -- Remove_From_Visibility --
9213 ----------------------------
9214
9215 procedure Remove_From_Visibility (E : Entity_Id) is
9216 begin
9217 if Is_Immediately_Visible (E) then
9218 Append_Elmt (E, Result);
9219 Set_Is_Immediately_Visible (E, False);
9220 end if;
9221 end Remove_From_Visibility;
9222
9223 -- Start of processing for Save_Scope_Stack
9224
9225 begin
9226 if SS_Last >= Scope_Stack.First
9227 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
9228 then
9229 if Handle_Use then
9230 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
9231 end if;
9232
9233 -- If the call is from within a compilation unit, as when called from
9234 -- Rtsfind, make current entries in scope stack invisible while we
9235 -- analyze the new unit.
9236
9237 for J in reverse 0 .. SS_Last loop
9238 exit when Scope_Stack.Table (J).Entity = Standard_Standard
9239 or else No (Scope_Stack.Table (J).Entity);
9240
9241 S := Scope_Stack.Table (J).Entity;
9242
9243 Remove_From_Visibility (S);
9244
9245 E := First_Entity (S);
9246 while Present (E) loop
9247 Remove_From_Visibility (E);
9248 Next_Entity (E);
9249 end loop;
9250 end loop;
9251
9252 end if;
9253
9254 return Result;
9255 end Save_Scope_Stack;
9256
9257 -------------
9258 -- Set_Use --
9259 -------------
9260
9261 procedure Set_Use (L : List_Id) is
9262 Decl : Node_Id;
9263
9264 begin
9265 if Present (L) then
9266 Decl := First (L);
9267 while Present (Decl) loop
9268 if Nkind (Decl) = N_Use_Package_Clause then
9269 Chain_Use_Clause (Decl);
9270 Use_One_Package (Decl, Name (Decl));
9271
9272 elsif Nkind (Decl) = N_Use_Type_Clause then
9273 Chain_Use_Clause (Decl);
9274 Use_One_Type (Subtype_Mark (Decl));
9275
9276 end if;
9277
9278 Next (Decl);
9279 end loop;
9280 end if;
9281 end Set_Use;
9282
9283 -----------------------------
9284 -- Update_Use_Clause_Chain --
9285 -----------------------------
9286
9287 procedure Update_Use_Clause_Chain is
9288
9289 procedure Update_Chain_In_Scope (Level : Int);
9290 -- Iterate through one level in the scope stack verifying each use-type
9291 -- clause within said level is used then reset the Current_Use_Clause
9292 -- to a redundant use clause outside of the current ending scope if such
9293 -- a clause exists.
9294
9295 ---------------------------
9296 -- Update_Chain_In_Scope --
9297 ---------------------------
9298
9299 procedure Update_Chain_In_Scope (Level : Int) is
9300 Curr : Node_Id;
9301 N : Node_Id;
9302
9303 begin
9304 -- Loop through all use clauses within the scope dictated by Level
9305
9306 Curr := Scope_Stack.Table (Level).First_Use_Clause;
9307 while Present (Curr) loop
9308
9309 -- Retrieve the subtype mark or name within the current current
9310 -- use clause.
9311
9312 if Nkind (Curr) = N_Use_Type_Clause then
9313 N := Subtype_Mark (Curr);
9314 else
9315 N := Name (Curr);
9316 end if;
9317
9318 -- If warnings for unreferenced entities are enabled and the
9319 -- current use clause has not been marked effective.
9320
9321 if Check_Unreferenced
9322 and then Comes_From_Source (Curr)
9323 and then not Is_Effective_Use_Clause (Curr)
9324 and then not In_Instance
9325 and then not In_Inlined_Body
9326 then
9327 -- We are dealing with a potentially unused use_package_clause
9328
9329 if Nkind (Curr) = N_Use_Package_Clause then
9330
9331 -- Renamings and formal subprograms may cause the associated
9332 -- node to be marked as effective instead of the original.
9333
9334 if not (Present (Associated_Node (N))
9335 and then Present
9336 (Current_Use_Clause
9337 (Associated_Node (N)))
9338 and then Is_Effective_Use_Clause
9339 (Current_Use_Clause
9340 (Associated_Node (N))))
9341 then
9342 Error_Msg_Node_1 := Entity (N);
9343 Error_Msg_NE
9344 ("use clause for package & has no effect?u?",
9345 Curr, Entity (N));
9346 end if;
9347
9348 -- We are dealing with an unused use_type_clause
9349
9350 else
9351 Error_Msg_Node_1 := Etype (N);
9352 Error_Msg_NE
9353 ("use clause for } has no effect?u?", Curr, Etype (N));
9354 end if;
9355 end if;
9356
9357 -- Verify that we haven't already processed a redundant
9358 -- use_type_clause within the same scope before we move the
9359 -- current use clause up to a previous one for type T.
9360
9361 if Present (Prev_Use_Clause (Curr)) then
9362 Set_Current_Use_Clause (Entity (N), Prev_Use_Clause (Curr));
9363 end if;
9364
9365 Next_Use_Clause (Curr);
9366 end loop;
9367 end Update_Chain_In_Scope;
9368
9369 -- Start of processing for Update_Use_Clause_Chain
9370
9371 begin
9372 Update_Chain_In_Scope (Scope_Stack.Last);
9373
9374 -- Deal with use clauses within the context area if the current
9375 -- scope is a compilation unit.
9376
9377 if Is_Compilation_Unit (Current_Scope)
9378 and then Sloc (Scope_Stack.Table
9379 (Scope_Stack.Last - 1).Entity) = Standard_Location
9380 then
9381 Update_Chain_In_Scope (Scope_Stack.Last - 1);
9382 end if;
9383 end Update_Use_Clause_Chain;
9384
9385 ---------------------
9386 -- Use_One_Package --
9387 ---------------------
9388
9389 procedure Use_One_Package
9390 (N : Node_Id;
9391 Pack_Name : Entity_Id := Empty;
9392 Force : Boolean := False)
9393 is
9394 procedure Note_Redundant_Use (Clause : Node_Id);
9395 -- Mark the name in a use clause as redundant if the corresponding
9396 -- entity is already use-visible. Emit a warning if the use clause comes
9397 -- from source and the proper warnings are enabled.
9398
9399 ------------------------
9400 -- Note_Redundant_Use --
9401 ------------------------
9402
9403 procedure Note_Redundant_Use (Clause : Node_Id) is
9404 Decl : constant Node_Id := Parent (Clause);
9405 Pack_Name : constant Entity_Id := Entity (Clause);
9406
9407 Cur_Use : Node_Id := Current_Use_Clause (Pack_Name);
9408 Prev_Use : Node_Id := Empty;
9409 Redundant : Node_Id := Empty;
9410 -- The Use_Clause which is actually redundant. In the simplest case
9411 -- it is Pack itself, but when we compile a body we install its
9412 -- context before that of its spec, in which case it is the
9413 -- use_clause in the spec that will appear to be redundant, and we
9414 -- want the warning to be placed on the body. Similar complications
9415 -- appear when the redundancy is between a child unit and one of its
9416 -- ancestors.
9417
9418 begin
9419 -- Could be renamed...
9420
9421 if No (Cur_Use) then
9422 Cur_Use := Current_Use_Clause (Renamed_Entity (Pack_Name));
9423 end if;
9424
9425 Set_Redundant_Use (Clause, True);
9426
9427 if not Comes_From_Source (Clause)
9428 or else In_Instance
9429 or else not Warn_On_Redundant_Constructs
9430 then
9431 return;
9432 end if;
9433
9434 if not Is_Compilation_Unit (Current_Scope) then
9435
9436 -- If the use_clause is in an inner scope, it is made redundant by
9437 -- some clause in the current context, with one exception: If we
9438 -- are compiling a nested package body, and the use_clause comes
9439 -- from then corresponding spec, the clause is not necessarily
9440 -- fully redundant, so we should not warn. If a warning was
9441 -- warranted, it would have been given when the spec was
9442 -- processed.
9443
9444 if Nkind (Parent (Decl)) = N_Package_Specification then
9445 declare
9446 Package_Spec_Entity : constant Entity_Id :=
9447 Defining_Unit_Name (Parent (Decl));
9448 begin
9449 if In_Package_Body (Package_Spec_Entity) then
9450 return;
9451 end if;
9452 end;
9453 end if;
9454
9455 Redundant := Clause;
9456 Prev_Use := Cur_Use;
9457
9458 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9459 declare
9460 Cur_Unit : constant Unit_Number_Type :=
9461 Get_Source_Unit (Cur_Use);
9462 New_Unit : constant Unit_Number_Type :=
9463 Get_Source_Unit (Clause);
9464
9465 Scop : Entity_Id;
9466
9467 begin
9468 if Cur_Unit = New_Unit then
9469
9470 -- Redundant clause in same body
9471
9472 Redundant := Clause;
9473 Prev_Use := Cur_Use;
9474
9475 elsif Cur_Unit = Current_Sem_Unit then
9476
9477 -- If the new clause is not in the current unit it has been
9478 -- analyzed first, and it makes the other one redundant.
9479 -- However, if the new clause appears in a subunit, Cur_Unit
9480 -- is still the parent, and in that case the redundant one
9481 -- is the one appearing in the subunit.
9482
9483 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
9484 Redundant := Clause;
9485 Prev_Use := Cur_Use;
9486
9487 -- Most common case: redundant clause in body, original
9488 -- clause in spec. Current scope is spec entity.
9489
9490 elsif Current_Scope = Cunit_Entity (Current_Sem_Unit) then
9491 Redundant := Cur_Use;
9492 Prev_Use := Clause;
9493
9494 else
9495 -- The new clause may appear in an unrelated unit, when
9496 -- the parents of a generic are being installed prior to
9497 -- instantiation. In this case there must be no warning.
9498 -- We detect this case by checking whether the current
9499 -- top of the stack is related to the current
9500 -- compilation.
9501
9502 Scop := Current_Scope;
9503 while Present (Scop)
9504 and then Scop /= Standard_Standard
9505 loop
9506 if Is_Compilation_Unit (Scop)
9507 and then not Is_Child_Unit (Scop)
9508 then
9509 return;
9510
9511 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
9512 exit;
9513 end if;
9514
9515 Scop := Scope (Scop);
9516 end loop;
9517
9518 Redundant := Cur_Use;
9519 Prev_Use := Clause;
9520 end if;
9521
9522 elsif New_Unit = Current_Sem_Unit then
9523 Redundant := Clause;
9524 Prev_Use := Cur_Use;
9525
9526 else
9527 -- Neither is the current unit, so they appear in parent or
9528 -- sibling units. Warning will be emitted elsewhere.
9529
9530 return;
9531 end if;
9532 end;
9533
9534 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
9535 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
9536 then
9537 -- Use_clause is in child unit of current unit, and the child unit
9538 -- appears in the context of the body of the parent, so it has
9539 -- been installed first, even though it is the redundant one.
9540 -- Depending on their placement in the context, the visible or the
9541 -- private parts of the two units, either might appear as
9542 -- redundant, but the message has to be on the current unit.
9543
9544 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
9545 Redundant := Cur_Use;
9546 Prev_Use := Clause;
9547 else
9548 Redundant := Clause;
9549 Prev_Use := Cur_Use;
9550 end if;
9551
9552 -- If the new use clause appears in the private part of a parent
9553 -- unit it may appear to be redundant w.r.t. a use clause in a
9554 -- child unit, but the previous use clause was needed in the
9555 -- visible part of the child, and no warning should be emitted.
9556
9557 if Nkind (Parent (Decl)) = N_Package_Specification
9558 and then List_Containing (Decl) =
9559 Private_Declarations (Parent (Decl))
9560 then
9561 declare
9562 Par : constant Entity_Id := Defining_Entity (Parent (Decl));
9563 Spec : constant Node_Id :=
9564 Specification (Unit (Cunit (Current_Sem_Unit)));
9565 Cur_List : constant List_Id := List_Containing (Cur_Use);
9566 begin
9567 if Is_Compilation_Unit (Par)
9568 and then Par /= Cunit_Entity (Current_Sem_Unit)
9569 then
9570 if Cur_List = Context_Items (Cunit (Current_Sem_Unit))
9571 or else Cur_List = Visible_Declarations (Spec)
9572 then
9573 return;
9574 end if;
9575 end if;
9576 end;
9577 end if;
9578
9579 -- Finally, if the current use clause is in the context then the
9580 -- clause is redundant when it is nested within the unit.
9581
9582 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
9583 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
9584 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
9585 then
9586 Redundant := Clause;
9587 Prev_Use := Cur_Use;
9588 end if;
9589
9590 if Present (Redundant) and then Parent (Redundant) /= Prev_Use then
9591
9592 -- Make sure we are looking at most-descendant use_package_clause
9593 -- by traversing the chain with Find_Most_Prev and then verifying
9594 -- there is no scope manipulation via Most_Descendant_Use_Clause.
9595
9596 if Nkind (Prev_Use) = N_Use_Package_Clause
9597 and then
9598 (Nkind (Parent (Prev_Use)) /= N_Compilation_Unit
9599 or else Most_Descendant_Use_Clause
9600 (Prev_Use, Find_Most_Prev (Prev_Use)) /= Prev_Use)
9601 then
9602 Prev_Use := Find_Most_Prev (Prev_Use);
9603 end if;
9604
9605 Error_Msg_Sloc := Sloc (Prev_Use);
9606 Error_Msg_NE -- CODEFIX
9607 ("& is already use-visible through previous use_clause #??",
9608 Redundant, Pack_Name);
9609 end if;
9610 end Note_Redundant_Use;
9611
9612 -- Local variables
9613
9614 Current_Instance : Entity_Id := Empty;
9615 Id : Entity_Id;
9616 P : Entity_Id;
9617 Prev : Entity_Id;
9618 Private_With_OK : Boolean := False;
9619 Real_P : Entity_Id;
9620
9621 -- Start of processing for Use_One_Package
9622
9623 begin
9624 -- Use_One_Package may have been called recursively to handle an
9625 -- implicit use for a auxiliary system package, so set P accordingly
9626 -- and skip redundancy checks.
9627
9628 if No (Pack_Name) and then Present_System_Aux (N) then
9629 P := System_Aux_Id;
9630
9631 -- Check for redundant use_package_clauses
9632
9633 else
9634 -- Ignore cases where we are dealing with a non user defined package
9635 -- like Standard_Standard or something other than a valid package.
9636
9637 if not Is_Entity_Name (Pack_Name)
9638 or else No (Entity (Pack_Name))
9639 or else Ekind (Entity (Pack_Name)) /= E_Package
9640 then
9641 return;
9642 end if;
9643
9644 -- When a renaming exists we must check it for redundancy. The
9645 -- original package would have already been seen at this point.
9646
9647 if Present (Renamed_Object (Entity (Pack_Name))) then
9648 P := Renamed_Object (Entity (Pack_Name));
9649 else
9650 P := Entity (Pack_Name);
9651 end if;
9652
9653 -- Check for redundant clauses then set the current use clause for
9654 -- P if were are not "forcing" an installation from a scope
9655 -- reinstallation that is done throughout analysis for various
9656 -- reasons.
9657
9658 if In_Use (P) then
9659 Note_Redundant_Use (Pack_Name);
9660
9661 if not Force then
9662 Set_Current_Use_Clause (P, N);
9663 end if;
9664
9665 return;
9666
9667 -- Warn about detected redundant clauses
9668
9669 elsif not Force
9670 and then In_Open_Scopes (P)
9671 and then not Is_Hidden_Open_Scope (P)
9672 then
9673 if Warn_On_Redundant_Constructs and then P = Current_Scope then
9674 Error_Msg_NE -- CODEFIX
9675 ("& is already use-visible within itself?r?",
9676 Pack_Name, P);
9677 end if;
9678
9679 return;
9680 end if;
9681
9682 -- Set P back to the non-renamed package so that visiblilty of the
9683 -- entities within the package can be properly set below.
9684
9685 P := Entity (Pack_Name);
9686 end if;
9687
9688 Set_In_Use (P);
9689 Set_Current_Use_Clause (P, N);
9690
9691 -- Ada 2005 (AI-50217): Check restriction
9692
9693 if From_Limited_With (P) then
9694 Error_Msg_N ("limited withed package cannot appear in use clause", N);
9695 end if;
9696
9697 -- Find enclosing instance, if any
9698
9699 if In_Instance then
9700 Current_Instance := Current_Scope;
9701 while not Is_Generic_Instance (Current_Instance) loop
9702 Current_Instance := Scope (Current_Instance);
9703 end loop;
9704
9705 if No (Hidden_By_Use_Clause (N)) then
9706 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
9707 end if;
9708 end if;
9709
9710 -- If unit is a package renaming, indicate that the renamed package is
9711 -- also in use (the flags on both entities must remain consistent, and a
9712 -- subsequent use of either of them should be recognized as redundant).
9713
9714 if Present (Renamed_Object (P)) then
9715 Set_In_Use (Renamed_Object (P));
9716 Set_Current_Use_Clause (Renamed_Object (P), N);
9717 Real_P := Renamed_Object (P);
9718 else
9719 Real_P := P;
9720 end if;
9721
9722 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
9723 -- found in the private part of a package specification
9724
9725 if In_Private_Part (Current_Scope)
9726 and then Has_Private_With (P)
9727 and then Is_Child_Unit (Current_Scope)
9728 and then Is_Child_Unit (P)
9729 and then Is_Ancestor_Package (Scope (Current_Scope), P)
9730 then
9731 Private_With_OK := True;
9732 end if;
9733
9734 -- Loop through entities in one package making them potentially
9735 -- use-visible.
9736
9737 Id := First_Entity (P);
9738 while Present (Id)
9739 and then (Id /= First_Private_Entity (P)
9740 or else Private_With_OK) -- Ada 2005 (AI-262)
9741 loop
9742 Prev := Current_Entity (Id);
9743 while Present (Prev) loop
9744 if Is_Immediately_Visible (Prev)
9745 and then (not Is_Overloadable (Prev)
9746 or else not Is_Overloadable (Id)
9747 or else (Type_Conformant (Id, Prev)))
9748 then
9749 if No (Current_Instance) then
9750
9751 -- Potentially use-visible entity remains hidden
9752
9753 goto Next_Usable_Entity;
9754
9755 -- A use clause within an instance hides outer global entities,
9756 -- which are not used to resolve local entities in the
9757 -- instance. Note that the predefined entities in Standard
9758 -- could not have been hidden in the generic by a use clause,
9759 -- and therefore remain visible. Other compilation units whose
9760 -- entities appear in Standard must be hidden in an instance.
9761
9762 -- To determine whether an entity is external to the instance
9763 -- we compare the scope depth of its scope with that of the
9764 -- current instance. However, a generic actual of a subprogram
9765 -- instance is declared in the wrapper package but will not be
9766 -- hidden by a use-visible entity. similarly, an entity that is
9767 -- declared in an enclosing instance will not be hidden by an
9768 -- an entity declared in a generic actual, which can only have
9769 -- been use-visible in the generic and will not have hidden the
9770 -- entity in the generic parent.
9771
9772 -- If Id is called Standard, the predefined package with the
9773 -- same name is in the homonym chain. It has to be ignored
9774 -- because it has no defined scope (being the only entity in
9775 -- the system with this mandated behavior).
9776
9777 elsif not Is_Hidden (Id)
9778 and then Present (Scope (Prev))
9779 and then not Is_Wrapper_Package (Scope (Prev))
9780 and then Scope_Depth (Scope (Prev)) <
9781 Scope_Depth (Current_Instance)
9782 and then (Scope (Prev) /= Standard_Standard
9783 or else Sloc (Prev) > Standard_Location)
9784 then
9785 if In_Open_Scopes (Scope (Prev))
9786 and then Is_Generic_Instance (Scope (Prev))
9787 and then Present (Associated_Formal_Package (P))
9788 then
9789 null;
9790
9791 else
9792 Set_Is_Potentially_Use_Visible (Id);
9793 Set_Is_Immediately_Visible (Prev, False);
9794 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
9795 end if;
9796 end if;
9797
9798 -- A user-defined operator is not use-visible if the predefined
9799 -- operator for the type is immediately visible, which is the case
9800 -- if the type of the operand is in an open scope. This does not
9801 -- apply to user-defined operators that have operands of different
9802 -- types, because the predefined mixed mode operations (multiply
9803 -- and divide) apply to universal types and do not hide anything.
9804
9805 elsif Ekind (Prev) = E_Operator
9806 and then Operator_Matches_Spec (Prev, Id)
9807 and then In_Open_Scopes
9808 (Scope (Base_Type (Etype (First_Formal (Id)))))
9809 and then (No (Next_Formal (First_Formal (Id)))
9810 or else Etype (First_Formal (Id)) =
9811 Etype (Next_Formal (First_Formal (Id)))
9812 or else Chars (Prev) = Name_Op_Expon)
9813 then
9814 goto Next_Usable_Entity;
9815
9816 -- In an instance, two homonyms may become use_visible through the
9817 -- actuals of distinct formal packages. In the generic, only the
9818 -- current one would have been visible, so make the other one
9819 -- not use_visible.
9820
9821 -- In certain pathological cases it is possible that unrelated
9822 -- homonyms from distinct formal packages may exist in an
9823 -- uninstalled scope. We must test for that here.
9824
9825 elsif Present (Current_Instance)
9826 and then Is_Potentially_Use_Visible (Prev)
9827 and then not Is_Overloadable (Prev)
9828 and then Scope (Id) /= Scope (Prev)
9829 and then Used_As_Generic_Actual (Scope (Prev))
9830 and then Used_As_Generic_Actual (Scope (Id))
9831 and then Is_List_Member (Scope (Prev))
9832 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
9833 Current_Use_Clause (Scope (Id)))
9834 then
9835 Set_Is_Potentially_Use_Visible (Prev, False);
9836 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
9837 end if;
9838
9839 Prev := Homonym (Prev);
9840 end loop;
9841
9842 -- On exit, we know entity is not hidden, unless it is private
9843
9844 if not Is_Hidden (Id)
9845 and then ((not Is_Child_Unit (Id)) or else Is_Visible_Lib_Unit (Id))
9846 then
9847 Set_Is_Potentially_Use_Visible (Id);
9848
9849 if Is_Private_Type (Id) and then Present (Full_View (Id)) then
9850 Set_Is_Potentially_Use_Visible (Full_View (Id));
9851 end if;
9852 end if;
9853
9854 <<Next_Usable_Entity>>
9855 Next_Entity (Id);
9856 end loop;
9857
9858 -- Child units are also made use-visible by a use clause, but they may
9859 -- appear after all visible declarations in the parent entity list.
9860
9861 while Present (Id) loop
9862 if Is_Child_Unit (Id) and then Is_Visible_Lib_Unit (Id) then
9863 Set_Is_Potentially_Use_Visible (Id);
9864 end if;
9865
9866 Next_Entity (Id);
9867 end loop;
9868
9869 if Chars (Real_P) = Name_System
9870 and then Scope (Real_P) = Standard_Standard
9871 and then Present_System_Aux (N)
9872 then
9873 Use_One_Package (N);
9874 end if;
9875 end Use_One_Package;
9876
9877 ------------------
9878 -- Use_One_Type --
9879 ------------------
9880
9881 procedure Use_One_Type
9882 (Id : Node_Id;
9883 Installed : Boolean := False;
9884 Force : Boolean := False)
9885 is
9886 function Spec_Reloaded_For_Body return Boolean;
9887 -- Determine whether the compilation unit is a package body and the use
9888 -- type clause is in the spec of the same package. Even though the spec
9889 -- was analyzed first, its context is reloaded when analysing the body.
9890
9891 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
9892 -- AI05-150: if the use_type_clause carries the "all" qualifier,
9893 -- class-wide operations of ancestor types are use-visible if the
9894 -- ancestor type is visible.
9895
9896 ----------------------------
9897 -- Spec_Reloaded_For_Body --
9898 ----------------------------
9899
9900 function Spec_Reloaded_For_Body return Boolean is
9901 begin
9902 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
9903 declare
9904 Spec : constant Node_Id :=
9905 Parent (List_Containing (Parent (Id)));
9906
9907 begin
9908 -- Check whether type is declared in a package specification,
9909 -- and current unit is the corresponding package body. The
9910 -- use clauses themselves may be within a nested package.
9911
9912 return
9913 Nkind (Spec) = N_Package_Specification
9914 and then In_Same_Source_Unit
9915 (Corresponding_Body (Parent (Spec)),
9916 Cunit_Entity (Current_Sem_Unit));
9917 end;
9918 end if;
9919
9920 return False;
9921 end Spec_Reloaded_For_Body;
9922
9923 -------------------------------
9924 -- Use_Class_Wide_Operations --
9925 -------------------------------
9926
9927 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
9928 function Is_Class_Wide_Operation_Of
9929 (Op : Entity_Id;
9930 T : Entity_Id) return Boolean;
9931 -- Determine whether a subprogram has a class-wide parameter or
9932 -- result that is T'Class.
9933
9934 ---------------------------------
9935 -- Is_Class_Wide_Operation_Of --
9936 ---------------------------------
9937
9938 function Is_Class_Wide_Operation_Of
9939 (Op : Entity_Id;
9940 T : Entity_Id) return Boolean
9941 is
9942 Formal : Entity_Id;
9943
9944 begin
9945 Formal := First_Formal (Op);
9946 while Present (Formal) loop
9947 if Etype (Formal) = Class_Wide_Type (T) then
9948 return True;
9949 end if;
9950
9951 Next_Formal (Formal);
9952 end loop;
9953
9954 if Etype (Op) = Class_Wide_Type (T) then
9955 return True;
9956 end if;
9957
9958 return False;
9959 end Is_Class_Wide_Operation_Of;
9960
9961 -- Local variables
9962
9963 Ent : Entity_Id;
9964 Scop : Entity_Id;
9965
9966 -- Start of processing for Use_Class_Wide_Operations
9967
9968 begin
9969 Scop := Scope (Typ);
9970 if not Is_Hidden (Scop) then
9971 Ent := First_Entity (Scop);
9972 while Present (Ent) loop
9973 if Is_Overloadable (Ent)
9974 and then Is_Class_Wide_Operation_Of (Ent, Typ)
9975 and then not Is_Potentially_Use_Visible (Ent)
9976 then
9977 Set_Is_Potentially_Use_Visible (Ent);
9978 Append_Elmt (Ent, Used_Operations (Parent (Id)));
9979 end if;
9980
9981 Next_Entity (Ent);
9982 end loop;
9983 end if;
9984
9985 if Is_Derived_Type (Typ) then
9986 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
9987 end if;
9988 end Use_Class_Wide_Operations;
9989
9990 -- Local variables
9991
9992 Elmt : Elmt_Id;
9993 Is_Known_Used : Boolean;
9994 Op_List : Elist_Id;
9995 T : Entity_Id;
9996
9997 -- Start of processing for Use_One_Type
9998
9999 begin
10000 if Entity (Id) = Any_Type then
10001 return;
10002 end if;
10003
10004 -- It is the type determined by the subtype mark (8.4(8)) whose
10005 -- operations become potentially use-visible.
10006
10007 T := Base_Type (Entity (Id));
10008
10009 -- Either the type itself is used, the package where it is declared is
10010 -- in use or the entity is declared in the current package, thus
10011 -- use-visible.
10012
10013 Is_Known_Used :=
10014 (In_Use (T)
10015 and then ((Present (Current_Use_Clause (T))
10016 and then All_Present (Current_Use_Clause (T)))
10017 or else not All_Present (Parent (Id))))
10018 or else In_Use (Scope (T))
10019 or else Scope (T) = Current_Scope;
10020
10021 Set_Redundant_Use (Id,
10022 Is_Known_Used or else Is_Potentially_Use_Visible (T));
10023
10024 if Ekind (T) = E_Incomplete_Type then
10025 Error_Msg_N ("premature usage of incomplete type", Id);
10026
10027 elsif In_Open_Scopes (Scope (T)) then
10028 null;
10029
10030 -- A limited view cannot appear in a use_type_clause. However, an access
10031 -- type whose designated type is limited has the flag but is not itself
10032 -- a limited view unless we only have a limited view of its enclosing
10033 -- package.
10034
10035 elsif From_Limited_With (T) and then From_Limited_With (Scope (T)) then
10036 Error_Msg_N
10037 ("incomplete type from limited view cannot appear in use clause",
10038 Id);
10039
10040 -- If the use clause is redundant, Used_Operations will usually be
10041 -- empty, but we need to set it to empty here in one case: If we are
10042 -- instantiating a generic library unit, then we install the ancestors
10043 -- of that unit in the scope stack, which involves reprocessing use
10044 -- clauses in those ancestors. Such a use clause will typically have a
10045 -- nonempty Used_Operations unless it was redundant in the generic unit,
10046 -- even if it is redundant at the place of the instantiation.
10047
10048 elsif Redundant_Use (Id) then
10049
10050 -- We must avoid incorrectly setting the Current_Use_Clause when we
10051 -- are working with a redundant clause that has already been linked
10052 -- in the Prev_Use_Clause chain, otherwise the chain will break.
10053
10054 if Present (Current_Use_Clause (T))
10055 and then Present (Prev_Use_Clause (Current_Use_Clause (T)))
10056 and then Parent (Id) = Prev_Use_Clause (Current_Use_Clause (T))
10057 then
10058 null;
10059 else
10060 Set_Current_Use_Clause (T, Parent (Id));
10061 end if;
10062
10063 Set_Used_Operations (Parent (Id), New_Elmt_List);
10064
10065 -- If the subtype mark designates a subtype in a different package,
10066 -- we have to check that the parent type is visible, otherwise the
10067 -- use_type_clause is a no-op. Not clear how to do that???
10068
10069 else
10070 Set_Current_Use_Clause (T, Parent (Id));
10071 Set_In_Use (T);
10072
10073 -- If T is tagged, primitive operators on class-wide operands are
10074 -- also deemed available. Note that this is really necessary only
10075 -- in semantics-only mode, because the primitive operators are not
10076 -- fully constructed in this mode, but we do it in all modes for the
10077 -- sake of uniformity, as this should not matter in practice.
10078
10079 if Is_Tagged_Type (T) then
10080 Set_In_Use (Class_Wide_Type (T));
10081 end if;
10082
10083 -- Iterate over primitive operations of the type. If an operation is
10084 -- already use_visible, it is the result of a previous use_clause,
10085 -- and already appears on the corresponding entity chain. If the
10086 -- clause is being reinstalled, operations are already use-visible.
10087
10088 if Installed then
10089 null;
10090
10091 else
10092 Op_List := Collect_Primitive_Operations (T);
10093 Elmt := First_Elmt (Op_List);
10094 while Present (Elmt) loop
10095 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
10096 or else Chars (Node (Elmt)) in Any_Operator_Name)
10097 and then not Is_Hidden (Node (Elmt))
10098 and then not Is_Potentially_Use_Visible (Node (Elmt))
10099 then
10100 Set_Is_Potentially_Use_Visible (Node (Elmt));
10101 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10102
10103 elsif Ada_Version >= Ada_2012
10104 and then All_Present (Parent (Id))
10105 and then not Is_Hidden (Node (Elmt))
10106 and then not Is_Potentially_Use_Visible (Node (Elmt))
10107 then
10108 Set_Is_Potentially_Use_Visible (Node (Elmt));
10109 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
10110 end if;
10111
10112 Next_Elmt (Elmt);
10113 end loop;
10114 end if;
10115
10116 if Ada_Version >= Ada_2012
10117 and then All_Present (Parent (Id))
10118 and then Is_Tagged_Type (T)
10119 then
10120 Use_Class_Wide_Operations (T);
10121 end if;
10122 end if;
10123
10124 -- If warning on redundant constructs, check for unnecessary WITH
10125
10126 if not Force
10127 and then Warn_On_Redundant_Constructs
10128 and then Is_Known_Used
10129
10130 -- with P; with P; use P;
10131 -- package P is package X is package body X is
10132 -- type T ... use P.T;
10133
10134 -- The compilation unit is the body of X. GNAT first compiles the
10135 -- spec of X, then proceeds to the body. At that point P is marked
10136 -- as use visible. The analysis then reinstalls the spec along with
10137 -- its context. The use clause P.T is now recognized as redundant,
10138 -- but in the wrong context. Do not emit a warning in such cases.
10139 -- Do not emit a warning either if we are in an instance, there is
10140 -- no redundancy between an outer use_clause and one that appears
10141 -- within the generic.
10142
10143 and then not Spec_Reloaded_For_Body
10144 and then not In_Instance
10145 and then not In_Inlined_Body
10146 then
10147 -- The type already has a use clause
10148
10149 if In_Use (T) then
10150
10151 -- Case where we know the current use clause for the type
10152
10153 if Present (Current_Use_Clause (T)) then
10154 Use_Clause_Known : declare
10155 Clause1 : constant Node_Id :=
10156 Find_Most_Prev (Current_Use_Clause (T));
10157 Clause2 : constant Node_Id := Parent (Id);
10158 Ent1 : Entity_Id;
10159 Ent2 : Entity_Id;
10160 Err_No : Node_Id;
10161 Unit1 : Node_Id;
10162 Unit2 : Node_Id;
10163
10164 -- Start of processing for Use_Clause_Known
10165
10166 begin
10167 -- If both current use_type_clause and the use_type_clause
10168 -- for the type are at the compilation unit level, one of
10169 -- the units must be an ancestor of the other, and the
10170 -- warning belongs on the descendant.
10171
10172 if Nkind (Parent (Clause1)) = N_Compilation_Unit
10173 and then
10174 Nkind (Parent (Clause2)) = N_Compilation_Unit
10175 then
10176 -- If the unit is a subprogram body that acts as spec,
10177 -- the context clause is shared with the constructed
10178 -- subprogram spec. Clearly there is no redundancy.
10179
10180 if Clause1 = Clause2 then
10181 return;
10182 end if;
10183
10184 Unit1 := Unit (Parent (Clause1));
10185 Unit2 := Unit (Parent (Clause2));
10186
10187 -- If both clauses are on same unit, or one is the body
10188 -- of the other, or one of them is in a subunit, report
10189 -- redundancy on the later one.
10190
10191 if Unit1 = Unit2 or else Nkind (Unit1) = N_Subunit then
10192 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10193 Error_Msg_NE -- CODEFIX
10194 ("& is already use-visible through previous "
10195 & "use_type_clause #??", Clause1, T);
10196 return;
10197
10198 elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
10199 and then Nkind (Unit1) /= Nkind (Unit2)
10200 and then Nkind (Unit1) /= N_Subunit
10201 then
10202 Error_Msg_Sloc := Sloc (Clause1);
10203 Error_Msg_NE -- CODEFIX
10204 ("& is already use-visible through previous "
10205 & "use_type_clause #??", Current_Use_Clause (T), T);
10206 return;
10207 end if;
10208
10209 -- There is a redundant use_type_clause in a child unit.
10210 -- Determine which of the units is more deeply nested.
10211 -- If a unit is a package instance, retrieve the entity
10212 -- and its scope from the instance spec.
10213
10214 Ent1 := Entity_Of_Unit (Unit1);
10215 Ent2 := Entity_Of_Unit (Unit2);
10216
10217 if Scope (Ent2) = Standard_Standard then
10218 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10219 Err_No := Clause1;
10220
10221 elsif Scope (Ent1) = Standard_Standard then
10222 Error_Msg_Sloc := Sloc (Id);
10223 Err_No := Clause2;
10224
10225 -- If both units are child units, we determine which one
10226 -- is the descendant by the scope distance to the
10227 -- ultimate parent unit.
10228
10229 else
10230 declare
10231 S1 : Entity_Id;
10232 S2 : Entity_Id;
10233
10234 begin
10235 S1 := Scope (Ent1);
10236 S2 := Scope (Ent2);
10237 while Present (S1)
10238 and then Present (S2)
10239 and then S1 /= Standard_Standard
10240 and then S2 /= Standard_Standard
10241 loop
10242 S1 := Scope (S1);
10243 S2 := Scope (S2);
10244 end loop;
10245
10246 if S1 = Standard_Standard then
10247 Error_Msg_Sloc := Sloc (Id);
10248 Err_No := Clause2;
10249 else
10250 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
10251 Err_No := Clause1;
10252 end if;
10253 end;
10254 end if;
10255
10256 if Parent (Id) /= Err_No then
10257 if Most_Descendant_Use_Clause
10258 (Err_No, Parent (Id)) = Parent (Id)
10259 then
10260 Error_Msg_Sloc := Sloc (Err_No);
10261 Err_No := Parent (Id);
10262 end if;
10263
10264 Error_Msg_NE -- CODEFIX
10265 ("& is already use-visible through previous "
10266 & "use_type_clause #??", Err_No, Id);
10267 end if;
10268
10269 -- Case where current use_type_clause and use_type_clause
10270 -- for the type are not both at the compilation unit level.
10271 -- In this case we don't have location information.
10272
10273 else
10274 Error_Msg_NE -- CODEFIX
10275 ("& is already use-visible through previous "
10276 & "use_type_clause??", Id, T);
10277 end if;
10278 end Use_Clause_Known;
10279
10280 -- Here if Current_Use_Clause is not set for T, another case where
10281 -- we do not have the location information available.
10282
10283 else
10284 Error_Msg_NE -- CODEFIX
10285 ("& is already use-visible through previous "
10286 & "use_type_clause??", Id, T);
10287 end if;
10288
10289 -- The package where T is declared is already used
10290
10291 elsif In_Use (Scope (T)) then
10292 -- Due to expansion of contracts we could be attempting to issue
10293 -- a spurious warning - so verify there is a previous use clause.
10294
10295 if Current_Use_Clause (Scope (T)) /=
10296 Find_Most_Prev (Current_Use_Clause (Scope (T)))
10297 then
10298 Error_Msg_Sloc :=
10299 Sloc (Find_Most_Prev (Current_Use_Clause (Scope (T))));
10300 Error_Msg_NE -- CODEFIX
10301 ("& is already use-visible through package use clause #??",
10302 Id, T);
10303 end if;
10304
10305 -- The current scope is the package where T is declared
10306
10307 else
10308 Error_Msg_Node_2 := Scope (T);
10309 Error_Msg_NE -- CODEFIX
10310 ("& is already use-visible inside package &??", Id, T);
10311 end if;
10312 end if;
10313 end Use_One_Type;
10314
10315 ----------------
10316 -- Write_Info --
10317 ----------------
10318
10319 procedure Write_Info is
10320 Id : Entity_Id := First_Entity (Current_Scope);
10321
10322 begin
10323 -- No point in dumping standard entities
10324
10325 if Current_Scope = Standard_Standard then
10326 return;
10327 end if;
10328
10329 Write_Str ("========================================================");
10330 Write_Eol;
10331 Write_Str (" Defined Entities in ");
10332 Write_Name (Chars (Current_Scope));
10333 Write_Eol;
10334 Write_Str ("========================================================");
10335 Write_Eol;
10336
10337 if No (Id) then
10338 Write_Str ("-- none --");
10339 Write_Eol;
10340
10341 else
10342 while Present (Id) loop
10343 Write_Entity_Info (Id, " ");
10344 Next_Entity (Id);
10345 end loop;
10346 end if;
10347
10348 if Scope (Current_Scope) = Standard_Standard then
10349
10350 -- Print information on the current unit itself
10351
10352 Write_Entity_Info (Current_Scope, " ");
10353 end if;
10354
10355 Write_Eol;
10356 end Write_Info;
10357
10358 --------
10359 -- ws --
10360 --------
10361
10362 procedure ws is
10363 S : Entity_Id;
10364 begin
10365 for J in reverse 1 .. Scope_Stack.Last loop
10366 S := Scope_Stack.Table (J).Entity;
10367 Write_Int (Int (S));
10368 Write_Str (" === ");
10369 Write_Name (Chars (S));
10370 Write_Eol;
10371 end loop;
10372 end ws;
10373
10374 --------
10375 -- we --
10376 --------
10377
10378 procedure we (S : Entity_Id) is
10379 E : Entity_Id;
10380 begin
10381 E := First_Entity (S);
10382 while Present (E) loop
10383 Write_Int (Int (E));
10384 Write_Str (" === ");
10385 Write_Name (Chars (E));
10386 Write_Eol;
10387 Next_Entity (E);
10388 end loop;
10389 end we;
10390 end Sem_Ch8;