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