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