]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch8.adb
[multiple changes]
[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-- --
1138cf59 9-- Copyright (C) 1992-2011, 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
1138cf59
AC
1617 function Check_Class_Wide_Actual return Entity_Id;
1618 -- AI05-0071: In an instance, if the actual for a formal type FT with
1619 -- unknown discriminants is a class-wide type CT, and the generic has
1620 -- a formal subprogram with a box for a primitive operation of FT,
1621 -- then the corresponding actual subprogram denoted by the default is a
1622 -- class-wide operation whose body is a dispatching call. We replace the
1623 -- generated renaming declaration:
1624 --
1625 -- procedure P (X : CT) renames P;
1626 --
1627 -- by a different renaming and a class-wide operation:
1628 --
1629 -- procedure Pr (X : T) renames P; -- renames primitive operation
1630 -- procedure P (X : CT); -- class-wide operation
1631 -- ...
1632 -- procedure P (X : CT) is begin Pr (X); end; -- dispatching call
1633
1634 -- This rule only applies if there is no explicit visible class-wide
1635 -- operation at the point of the instantiation.
1636
1637 -----------------------------
1638 -- Check_Class_Wide_Actual --
1639 -----------------------------
1640
1641 function Check_Class_Wide_Actual return Entity_Id is
1642 Loc : constant Source_Ptr := Sloc (N);
1643
1644 F : Entity_Id;
1645 Formal_Type : Entity_Id;
1646 Actual_Type : Entity_Id;
1647 New_Body : Node_Id;
1648 New_Decl : Node_Id;
1649 Result : Entity_Id;
1650
1651 function Make_Call (Prim_Op : Entity_Id) return Node_Id;
1652 -- Build dispatching call for body of class-wide operation
1653
1654 function Make_Spec return Node_Id;
1655 -- Create subprogram specification for declaration and body of
1656 -- class-wide operation, using signature of renaming declaration.
1657
1658 ---------------
1659 -- Make_Call --
1660 ---------------
1661
1662 function Make_Call (Prim_Op : Entity_Id) return Node_Id is
1663 Actuals : List_Id;
1664 F : Node_Id;
1665
1666 begin
1667 Actuals := New_List;
1668 F := First (Parameter_Specifications (Specification (New_Decl)));
1669 while Present (F) loop
1670 Append_To (Actuals,
1671 Make_Identifier (Loc, Chars (Defining_Identifier (F))));
1672 Next (F);
1673 end loop;
1674
1675 if Ekind (Prim_Op) = E_Function then
1676 return Make_Simple_Return_Statement (Loc,
1677 Expression =>
1678 Make_Function_Call (Loc,
1679 Name => New_Occurrence_Of (Prim_Op, Loc),
1680 Parameter_Associations => Actuals));
1681 else
1682 return
1683 Make_Procedure_Call_Statement (Loc,
1684 Name => New_Occurrence_Of (Prim_Op, Loc),
1685 Parameter_Associations => Actuals);
1686 end if;
1687 end Make_Call;
1688
1689 ---------------
1690 -- Make_Spec --
1691 ---------------
1692
1693 function Make_Spec return Node_Id is
1694 Param_Specs : constant List_Id := Copy_Parameter_List (New_S);
1695
1696 begin
1697 if Ekind (New_S) = E_Procedure then
1698 return
1699 Make_Procedure_Specification (Loc,
1700 Defining_Unit_Name =>
1701 Make_Defining_Identifier (Loc,
1702 Chars (Defining_Unit_Name (Spec))),
1703 Parameter_Specifications => Param_Specs);
1704 else
1705 return
1706 Make_Function_Specification (Loc,
1707 Defining_Unit_Name =>
1708 Make_Defining_Identifier (Loc,
1709 Chars (Defining_Unit_Name (Spec))),
1710 Parameter_Specifications => Param_Specs,
1711 Result_Definition =>
1712 New_Copy_Tree (Result_Definition (Spec)));
1713 end if;
1714 end Make_Spec;
1715
1716 -- Start of processing for Check_Class_Wide_Actual
1717
1718 begin
1719 Result := Any_Id;
1720 Formal_Type := Empty;
1721 Actual_Type := Empty;
1722
1723 F := First_Formal (Formal_Spec);
1724 while Present (F) loop
1725 if Has_Unknown_Discriminants (Etype (F))
1726 and then Is_Class_Wide_Type (Get_Instance_Of (Etype (F)))
1727 then
1728 Formal_Type := Etype (F);
1729 Actual_Type := Etype (Get_Instance_Of (Formal_Type));
1730 exit;
1731 end if;
1732
1733 Next_Formal (F);
1734 end loop;
1735
1736 if Present (Formal_Type) then
1737
1738 -- Create declaration and body for class-wide operation
1739
1740 New_Decl :=
1741 Make_Subprogram_Declaration (Loc, Specification => Make_Spec);
1742
1743 New_Body :=
1744 Make_Subprogram_Body (Loc,
1745 Specification => Make_Spec,
1746 Declarations => No_List,
1747 Handled_Statement_Sequence =>
1748 Make_Handled_Sequence_Of_Statements (Loc, New_List));
1749
1750 -- Modify Spec and create internal name for renaming of primitive
1751 -- operation.
1752
1753 Set_Defining_Unit_Name (Spec, Make_Temporary (Loc, 'R'));
1754 F := First (Parameter_Specifications (Spec));
1755 while Present (F) loop
1756 if Nkind (Parameter_Type (F)) = N_Identifier
1757 and then Is_Class_Wide_Type (Entity (Parameter_Type (F)))
1758 then
1759 Set_Parameter_Type (F, New_Occurrence_Of (Actual_Type, Loc));
1760 end if;
1761 Next (F);
1762 end loop;
1763
1764 New_S := Analyze_Subprogram_Specification (Spec);
1765 Result := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
1766 end if;
1767
1768 if Result /= Any_Id then
1769 Insert_Before (N, New_Decl);
1770 Analyze (New_Decl);
1771
1772 -- Add dispatching call to body of class-wide operation
1773
1774 Append (Make_Call (Result),
1775 Statements (Handled_Statement_Sequence (New_Body)));
1776
1777 -- The generated body does not freeze. It is analyzed when the
1778 -- generated operation is frozen.
1779
1780 Append_Freeze_Action (Defining_Entity (New_Decl), New_Body);
1781
1782 Result := Defining_Entity (New_Decl);
1783 end if;
1784
1785 -- Return the class-wide operation if one was created.
1786
1787 return Result;
1788 end Check_Class_Wide_Actual;
1789
923fa078
RD
1790 --------------------------
1791 -- Check_Null_Exclusion --
1792 --------------------------
1793
1794 procedure Check_Null_Exclusion
1795 (Ren : Entity_Id;
1796 Sub : Entity_Id)
1797 is
16ca248a
ES
1798 Ren_Formal : Entity_Id;
1799 Sub_Formal : Entity_Id;
923fa078
RD
1800
1801 begin
1802 -- Parameter check
1803
16ca248a
ES
1804 Ren_Formal := First_Formal (Ren);
1805 Sub_Formal := First_Formal (Sub);
923fa078
RD
1806 while Present (Ren_Formal)
1807 and then Present (Sub_Formal)
1808 loop
1809 if Has_Null_Exclusion (Parent (Ren_Formal))
1810 and then
1811 not (Has_Null_Exclusion (Parent (Sub_Formal))
1812 or else Can_Never_Be_Null (Etype (Sub_Formal)))
1813 then
fbe627af
RD
1814 Error_Msg_NE
1815 ("`NOT NULL` required for parameter &",
1816 Parent (Sub_Formal), Sub_Formal);
923fa078
RD
1817 end if;
1818
1819 Next_Formal (Ren_Formal);
1820 Next_Formal (Sub_Formal);
1821 end loop;
1822
1823 -- Return profile check
1824
1825 if Nkind (Parent (Ren)) = N_Function_Specification
1826 and then Nkind (Parent (Sub)) = N_Function_Specification
1827 and then Has_Null_Exclusion (Parent (Ren))
1828 and then
1829 not (Has_Null_Exclusion (Parent (Sub))
1830 or else Can_Never_Be_Null (Etype (Sub)))
1831 then
fbe627af
RD
1832 Error_Msg_N
1833 ("return must specify `NOT NULL`",
1834 Result_Definition (Parent (Sub)));
923fa078
RD
1835 end if;
1836 end Check_Null_Exclusion;
1837
07fc65c4
GB
1838 -------------------------
1839 -- Original_Subprogram --
1840 -------------------------
1841
1842 function Original_Subprogram (Subp : Entity_Id) return Entity_Id is
1843 Orig_Decl : Node_Id;
1844 Orig_Subp : Entity_Id;
1845
1846 begin
1847 -- First case: renamed entity is itself a renaming
1848
1849 if Present (Alias (Subp)) then
1850 return Alias (Subp);
1851
1852 elsif
1853 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1854 and then Present
1855 (Corresponding_Body (Unit_Declaration_Node (Subp)))
1856 then
1857 -- Check if renamed entity is a renaming_as_body
1858
1859 Orig_Decl :=
1860 Unit_Declaration_Node
1861 (Corresponding_Body (Unit_Declaration_Node (Subp)));
1862
1863 if Nkind (Orig_Decl) = N_Subprogram_Renaming_Declaration then
1864 Orig_Subp := Entity (Name (Orig_Decl));
1865
1866 if Orig_Subp = Rename_Spec then
1867
bc41faa2 1868 -- Circularity detected
07fc65c4
GB
1869
1870 return Orig_Subp;
1871
1872 else
1873 return (Original_Subprogram (Orig_Subp));
1874 end if;
1875 else
1876 return Subp;
1877 end if;
1878 else
1879 return Subp;
1880 end if;
1881 end Original_Subprogram;
1882
fbf5a39b 1883 -- Start of processing for Analyze_Subprogram_Renaming
07fc65c4 1884
996ae0b0
RK
1885 begin
1886 -- We must test for the attribute renaming case before the Analyze
1887 -- call because otherwise Sem_Attr will complain that the attribute
1888 -- is missing an argument when it is analyzed.
1889
1890 if Nkind (Nam) = N_Attribute_Reference then
d239991f 1891
16ca248a
ES
1892 -- In the case of an abstract formal subprogram association, rewrite
1893 -- an actual given by a stream attribute as the name of the
1894 -- corresponding stream primitive of the type.
d239991f 1895
16ca248a
ES
1896 -- In a generic context the stream operations are not generated, and
1897 -- this must be treated as a normal attribute reference, to be
1898 -- expanded in subsequent instantiations.
d4810530 1899
16ca248a 1900 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec)
d4810530
ES
1901 and then Expander_Active
1902 then
d239991f
GD
1903 declare
1904 Stream_Prim : Entity_Id;
1905 Prefix_Type : constant Entity_Id := Entity (Prefix (Nam));
1906
1907 begin
1908 -- The class-wide forms of the stream attributes are not
1909 -- primitive dispatching operations (even though they
1910 -- internally dispatch to a stream attribute).
1911
1912 if Is_Class_Wide_Type (Prefix_Type) then
1913 Error_Msg_N
1914 ("attribute must be a primitive dispatching operation",
1915 Nam);
1916 return;
1917 end if;
1918
1919 -- Retrieve the primitive subprogram associated with the
16ca248a
ES
1920 -- attribute. This can only be a stream attribute, since those
1921 -- are the only ones that are dispatching (and the actual for
1922 -- an abstract formal subprogram must be dispatching
1923 -- operation).
d239991f 1924
6a4d72a6
ES
1925 begin
1926 case Attribute_Name (Nam) is
1927 when Name_Input =>
1928 Stream_Prim :=
1929 Find_Prim_Op (Prefix_Type, TSS_Stream_Input);
1930 when Name_Output =>
1931 Stream_Prim :=
1932 Find_Prim_Op (Prefix_Type, TSS_Stream_Output);
1933 when Name_Read =>
1934 Stream_Prim :=
1935 Find_Prim_Op (Prefix_Type, TSS_Stream_Read);
1936 when Name_Write =>
1937 Stream_Prim :=
1938 Find_Prim_Op (Prefix_Type, TSS_Stream_Write);
1939 when others =>
1940 Error_Msg_N
1941 ("attribute must be a primitive"
1942 & " dispatching operation", Nam);
1943 return;
1944 end case;
a7a0d4dd 1945
6a4d72a6
ES
1946 exception
1947
1948 -- If no operation was found, and the type is limited,
1949 -- the user should have defined one.
1950
1951 when Program_Error =>
1952 if Is_Limited_Type (Prefix_Type) then
1953 Error_Msg_NE
1954 ("stream operation not defined for type&",
1955 N, Prefix_Type);
1956 return;
1957
a7a0d4dd 1958 -- Otherwise, compiler should have generated default
6a4d72a6
ES
1959
1960 else
1961 raise;
1962 end if;
1963 end;
d239991f
GD
1964
1965 -- Rewrite the attribute into the name of its corresponding
1966 -- primitive dispatching subprogram. We can then proceed with
1967 -- the usual processing for subprogram renamings.
1968
1969 declare
1970 Prim_Name : constant Node_Id :=
1971 Make_Identifier (Sloc (Nam),
1972 Chars => Chars (Stream_Prim));
1973 begin
1974 Set_Entity (Prim_Name, Stream_Prim);
1975 Rewrite (Nam, Prim_Name);
1976 Analyze (Nam);
1977 end;
1978 end;
1979
1980 -- Normal processing for a renaming of an attribute
1981
1982 else
1983 Attribute_Renaming (N);
1984 return;
1985 end if;
996ae0b0
RK
1986 end if;
1987
1988 -- Check whether this declaration corresponds to the instantiation
82c80734 1989 -- of a formal subprogram.
996ae0b0 1990
16ca248a
ES
1991 -- If this is an instantiation, the corresponding actual is frozen and
1992 -- error messages can be made more precise. If this is a default
1993 -- subprogram, the entity is already established in the generic, and is
1994 -- not retrieved by visibility. If it is a default with a box, the
996ae0b0 1995 -- candidate interpretations, if any, have been collected when building
16ca248a
ES
1996 -- the renaming declaration. If overloaded, the proper interpretation is
1997 -- determined in Find_Renamed_Entity. If the entity is an operator,
996ae0b0
RK
1998 -- Find_Renamed_Entity applies additional visibility checks.
1999
d239991f
GD
2000 if Is_Actual then
2001 Inst_Node := Unit_Declaration_Node (Formal_Spec);
996ae0b0
RK
2002
2003 if Is_Entity_Name (Nam)
2004 and then Present (Entity (Nam))
2005 and then not Comes_From_Source (Nam)
2006 and then not Is_Overloaded (Nam)
2007 then
2008 Old_S := Entity (Nam);
fbf5a39b 2009 New_S := Analyze_Subprogram_Specification (Spec);
996ae0b0 2010
2e071734
AC
2011 -- Operator case
2012
2013 if Ekind (Entity (Nam)) = E_Operator then
2014
2015 -- Box present
2016
2017 if Box_Present (Inst_Node) then
2018 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2019
2020 -- If there is an immediately visible homonym of the operator
2021 -- and the declaration has a default, this is worth a warning
2022 -- because the user probably did not intend to get the pre-
16ca248a
ES
2023 -- defined operator, visible in the generic declaration. To
2024 -- find if there is an intended candidate, analyze the renaming
2025 -- again in the current context.
2e071734
AC
2026
2027 elsif Scope (Old_S) = Standard_Standard
2028 and then Present (Default_Name (Inst_Node))
2029 then
2030 declare
2031 Decl : constant Node_Id := New_Copy_Tree (N);
2032 Hidden : Entity_Id;
2033
2034 begin
2035 Set_Entity (Name (Decl), Empty);
2036 Analyze (Name (Decl));
2037 Hidden :=
2038 Find_Renamed_Entity (Decl, Name (Decl), New_S, True);
2039
2040 if Present (Hidden)
2041 and then In_Open_Scopes (Scope (Hidden))
2042 and then Is_Immediately_Visible (Hidden)
2043 and then Comes_From_Source (Hidden)
923fa078 2044 and then Hidden /= Old_S
2e071734
AC
2045 then
2046 Error_Msg_Sloc := Sloc (Hidden);
2047 Error_Msg_N ("?default subprogram is resolved " &
2048 "in the generic declaration " &
11560bcc 2049 "(RM 12.6(17))", N);
2e071734
AC
2050 Error_Msg_NE ("\?and will not use & #", N, Hidden);
2051 end if;
2052 end;
2053 end if;
996ae0b0
RK
2054 end if;
2055
2056 else
2057 Analyze (Nam);
fbf5a39b 2058 New_S := Analyze_Subprogram_Specification (Spec);
996ae0b0
RK
2059 end if;
2060
996ae0b0
RK
2061 else
2062 -- Renamed entity must be analyzed first, to avoid being hidden by
2063 -- new name (which might be the same in a generic instance).
2064
2065 Analyze (Nam);
2066
2067 -- The renaming defines a new overloaded entity, which is analyzed
2068 -- like a subprogram declaration.
2069
fbf5a39b 2070 New_S := Analyze_Subprogram_Specification (Spec);
996ae0b0
RK
2071 end if;
2072
2073 if Current_Scope /= Standard_Standard then
2074 Set_Is_Pure (New_S, Is_Pure (Current_Scope));
2075 end if;
2076
2077 Rename_Spec := Find_Corresponding_Spec (N);
2078
294ccb21
RD
2079 -- Case of Renaming_As_Body
2080
996ae0b0
RK
2081 if Present (Rename_Spec) then
2082
294ccb21
RD
2083 -- Renaming declaration is the completion of the declaration of
2084 -- Rename_Spec. We build an actual body for it at the freezing point.
996ae0b0
RK
2085
2086 Set_Corresponding_Spec (N, Rename_Spec);
fbe627af 2087
ff81221b
ES
2088 -- Deal with special case of stream functions of abstract types
2089 -- and interfaces.
294ccb21 2090
d4810530
ES
2091 if Nkind (Unit_Declaration_Node (Rename_Spec)) =
2092 N_Abstract_Subprogram_Declaration
2093 then
ff81221b
ES
2094 -- Input stream functions are abstract if the object type is
2095 -- abstract. Similarly, all default stream functions for an
30783513 2096 -- interface type are abstract. However, these subprograms may
ff81221b
ES
2097 -- receive explicit declarations in representation clauses, making
2098 -- the attribute subprograms usable as defaults in subsequent
2099 -- type extensions.
d4810530
ES
2100 -- In this case we rewrite the declaration to make the subprogram
2101 -- non-abstract. We remove the previous declaration, and insert
2102 -- the new one at the point of the renaming, to prevent premature
2103 -- access to unfrozen types. The new declaration reuses the
2104 -- specification of the previous one, and must not be analyzed.
2105
ff81221b
ES
2106 pragma Assert
2107 (Is_Primitive (Entity (Nam))
2108 and then
2109 Is_Abstract_Type (Find_Dispatching_Type (Entity (Nam))));
d4810530
ES
2110 declare
2111 Old_Decl : constant Node_Id :=
2112 Unit_Declaration_Node (Rename_Spec);
2113 New_Decl : constant Node_Id :=
2114 Make_Subprogram_Declaration (Sloc (N),
2115 Specification =>
2116 Relocate_Node (Specification (Old_Decl)));
2117 begin
2118 Remove (Old_Decl);
2119 Insert_After (N, New_Decl);
16ca248a 2120 Set_Is_Abstract_Subprogram (Rename_Spec, False);
d4810530
ES
2121 Set_Analyzed (New_Decl);
2122 end;
2123 end if;
2124
996ae0b0
RK
2125 Set_Corresponding_Body (Unit_Declaration_Node (Rename_Spec), New_S);
2126
0ab80019 2127 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
996ae0b0
RK
2128 Error_Msg_N ("(Ada 83) renaming cannot serve as a body", N);
2129 end if;
2130
923fa078 2131 Set_Convention (New_S, Convention (Rename_Spec));
996ae0b0
RK
2132 Check_Fully_Conformant (New_S, Rename_Spec);
2133 Set_Public_Status (New_S);
2134
294ccb21
RD
2135 -- The specification does not introduce new formals, but only
2136 -- repeats the formals of the original subprogram declaration.
2137 -- For cross-reference purposes, and for refactoring tools, we
2138 -- treat the formals of the renaming declaration as body formals.
2139
2140 Reference_Body_Formals (Rename_Spec, New_S);
2141
4de287c4
ES
2142 -- Indicate that the entity in the declaration functions like the
2143 -- corresponding body, and is not a new entity. The body will be
2144 -- constructed later at the freeze point, so indicate that the
2145 -- completion has not been seen yet.
996ae0b0
RK
2146
2147 Set_Ekind (New_S, E_Subprogram_Body);
2148 New_S := Rename_Spec;
5eb10f25 2149 Set_Has_Completion (Rename_Spec, False);
996ae0b0 2150
4de287c4 2151 -- Ada 2005: check overriding indicator
edd63e9b 2152
038140ed 2153 if Present (Overridden_Operation (Rename_Spec)) then
235f4375
AC
2154 if Must_Not_Override (Specification (N)) then
2155 Error_Msg_NE
2156 ("subprogram& overrides inherited operation",
2157 N, Rename_Spec);
2158 elsif
2159 Style_Check and then not Must_Override (Specification (N))
2160 then
2161 Style.Missing_Overriding (N, Rename_Spec);
2162 end if;
edd63e9b 2163
235f4375
AC
2164 elsif Must_Override (Specification (N)) then
2165 Error_Msg_NE ("subprogram& is not overriding", N, Rename_Spec);
edd63e9b
ES
2166 end if;
2167
294ccb21
RD
2168 -- Normal subprogram renaming (not renaming as body)
2169
996ae0b0
RK
2170 else
2171 Generate_Definition (New_S);
2172 New_Overloaded_Entity (New_S);
edd63e9b 2173
996ae0b0
RK
2174 if Is_Entity_Name (Nam)
2175 and then Is_Intrinsic_Subprogram (Entity (Nam))
2176 then
2177 null;
2178 else
2179 Check_Delayed_Subprogram (New_S);
2180 end if;
2181 end if;
2182
4de287c4
ES
2183 -- There is no need for elaboration checks on the new entity, which may
2184 -- be called before the next freezing point where the body will appear.
2185 -- Elaboration checks refer to the real entity, not the one created by
2186 -- the renaming declaration.
996ae0b0 2187
fbf5a39b 2188 Set_Kill_Elaboration_Checks (New_S, True);
996ae0b0
RK
2189
2190 if Etype (Nam) = Any_Type then
2191 Set_Has_Completion (New_S);
2192 return;
2193
2194 elsif Nkind (Nam) = N_Selected_Component then
2195
294ccb21
RD
2196 -- A prefix of the form A.B can designate an entry of task A, a
2197 -- protected operation of protected object A, or finally a primitive
2198 -- operation of object A. In the later case, A is an object of some
2199 -- tagged type, or an access type that denotes one such. To further
2200 -- distinguish these cases, note that the scope of a task entry or
2201 -- protected operation is type of the prefix.
996ae0b0 2202
294ccb21
RD
2203 -- The prefix could be an overloaded function call that returns both
2204 -- kinds of operations. This overloading pathology is left to the
2205 -- dedicated reader ???
2206
2207 declare
2208 T : constant Entity_Id := Etype (Prefix (Nam));
2209
2210 begin
2211 if Present (T)
2212 and then
2213 (Is_Tagged_Type (T)
2214 or else
2215 (Is_Access_Type (T)
2216 and then
2217 Is_Tagged_Type (Designated_Type (T))))
2218 and then Scope (Entity (Selector_Name (Nam))) /= T
2219 then
2220 Analyze_Renamed_Primitive_Operation
2221 (N, New_S, Present (Rename_Spec));
2222 return;
2223
2224 else
2225 -- Renamed entity is an entry or protected operation. For those
2226 -- cases an explicit body is built (at the point of freezing of
2227 -- this entity) that contains a call to the renamed entity.
2228
2229 -- This is not allowed for renaming as body if the renamed
2230 -- spec is already frozen (see RM 8.5.4(5) for details).
2231
2232 if Present (Rename_Spec)
2233 and then Is_Frozen (Rename_Spec)
2234 then
2235 Error_Msg_N
2236 ("renaming-as-body cannot rename entry as subprogram", N);
2237 Error_Msg_NE
2238 ("\since & is already frozen (RM 8.5.4(5))",
2239 N, Rename_Spec);
2240 else
2241 Analyze_Renamed_Entry (N, New_S, Present (Rename_Spec));
2242 end if;
2243
2244 return;
2245 end if;
2246 end;
996ae0b0
RK
2247
2248 elsif Nkind (Nam) = N_Explicit_Dereference then
2249
2250 -- Renamed entity is designated by access_to_subprogram expression.
2251 -- Must build body to encapsulate call, as in the entry case.
2252
2253 Analyze_Renamed_Dereference (N, New_S, Present (Rename_Spec));
2254 return;
2255
2256 elsif Nkind (Nam) = N_Indexed_Component then
2257 Analyze_Renamed_Family_Member (N, New_S, Present (Rename_Spec));
2258 return;
2259
2260 elsif Nkind (Nam) = N_Character_Literal then
2261 Analyze_Renamed_Character (N, New_S, Present (Rename_Spec));
2262 return;
2263
a3f2babd 2264 elsif not Is_Entity_Name (Nam)
996ae0b0
RK
2265 or else not Is_Overloadable (Entity (Nam))
2266 then
2267 Error_Msg_N ("expect valid subprogram name in renaming", N);
2268 return;
996ae0b0
RK
2269 end if;
2270
11560bcc
TQ
2271 -- Find the renamed entity that matches the given specification. Disable
2272 -- Ada_83 because there is no requirement of full conformance between
2273 -- renamed entity and new entity, even though the same circuit is used.
2274
2275 -- This is a bit of a kludge, which introduces a really irregular use of
2276 -- Ada_Version[_Explicit]. Would be nice to find cleaner way to do this
2277 -- ???
2278
2279 Ada_Version := Ada_Version_Type'Max (Ada_Version, Ada_95);
2280 Ada_Version_Explicit := Ada_Version;
2281
2282 if No (Old_S) then
2283 Old_S := Find_Renamed_Entity (N, Name (N), New_S, Is_Actual);
2284
1366997b
AC
2285 -- The visible operation may be an inherited abstract operation that
2286 -- was overridden in the private part, in which case a call will
2287 -- dispatch to the overriding operation. Use the overriding one in
2288 -- the renaming declaration, to prevent spurious errors below.
2289
2290 if Is_Overloadable (Old_S)
2291 and then Is_Abstract_Subprogram (Old_S)
2292 and then No (DTC_Entity (Old_S))
2293 and then Present (Alias (Old_S))
2294 and then not Is_Abstract_Subprogram (Alias (Old_S))
038140ed 2295 and then Present (Overridden_Operation (Alias (Old_S)))
1366997b
AC
2296 then
2297 Old_S := Alias (Old_S);
2298 end if;
2299
11560bcc
TQ
2300 -- When the renamed subprogram is overloaded and used as an actual
2301 -- of a generic, its entity is set to the first available homonym.
2302 -- We must first disambiguate the name, then set the proper entity.
2303
2304 if Is_Actual
2305 and then Is_Overloaded (Nam)
2306 then
2307 Set_Entity (Nam, Old_S);
2308 end if;
2309 end if;
2310
4de287c4
ES
2311 -- Most common case: subprogram renames subprogram. No body is generated
2312 -- in this case, so we must indicate the declaration is complete as is.
b2c4d56d 2313 -- and inherit various attributes of the renamed subprogram.
996ae0b0
RK
2314
2315 if No (Rename_Spec) then
923fa078 2316 Set_Has_Completion (New_S);
b2c4d56d 2317 Set_Is_Imported (New_S, Is_Imported (Entity (Nam)));
923fa078
RD
2318 Set_Is_Pure (New_S, Is_Pure (Entity (Nam)));
2319 Set_Is_Preelaborated (New_S, Is_Preelaborated (Entity (Nam)));
2320
2321 -- Ada 2005 (AI-423): Check the consistency of null exclusions
11560bcc 2322 -- between a subprogram and its correct renaming.
923fa078 2323
11560bcc
TQ
2324 -- Note: the Any_Id check is a guard that prevents compiler crashes
2325 -- when performing a null exclusion check between a renaming and a
2326 -- renamed subprogram that has been found to be illegal.
2327
0791fbe9 2328 if Ada_Version >= Ada_2005
11560bcc
TQ
2329 and then Entity (Nam) /= Any_Id
2330 then
923fa078
RD
2331 Check_Null_Exclusion
2332 (Ren => New_S,
2333 Sub => Entity (Nam));
2334 end if;
90067a15
ES
2335
2336 -- Enforce the Ada 2005 rule that the renamed entity cannot require
2337 -- overriding. The flag Requires_Overriding is set very selectively
2338 -- and misses some other illegal cases. The additional conditions
2339 -- checked below are sufficient but not necessary ???
2340
2341 -- The rule does not apply to the renaming generated for an actual
2342 -- subprogram in an instance.
2343
2344 if Is_Actual then
2345 null;
2346
f3d57416 2347 -- Guard against previous errors, and omit renamings of predefined
90067a15
ES
2348 -- operators.
2349
bce79204 2350 elsif not Ekind_In (Old_S, E_Function, E_Procedure) then
90067a15
ES
2351 null;
2352
2353 elsif Requires_Overriding (Old_S)
2354 or else
2355 (Is_Abstract_Subprogram (Old_S)
2356 and then Present (Find_Dispatching_Type (Old_S))
2357 and then
2358 not Is_Abstract_Type (Find_Dispatching_Type (Old_S)))
2359 then
2360 Error_Msg_N
2361 ("renamed entity cannot be "
2362 & "subprogram that requires overriding (RM 8.5.4 (5.1))", N);
2363 end if;
996ae0b0
RK
2364 end if;
2365
1138cf59
AC
2366 -- If no renamed entity was found, check whether the renaming is for
2367 -- a defaulted actual subprogram with a class-wide actual.
2368
2369 if Old_S = Any_Id
2370 and then Is_Actual
2371 and then From_Default (N)
2372 then
2373 Old_S := Check_Class_Wide_Actual;
2374 end if;
2375
996ae0b0 2376 if Old_S /= Any_Id then
996ae0b0 2377 if Is_Actual
6d11af89 2378 and then From_Default (N)
996ae0b0
RK
2379 then
2380 -- This is an implicit reference to the default actual
2381
2382 Generate_Reference (Old_S, Nam, Typ => 'i', Force => True);
2383 else
2384 Generate_Reference (Old_S, Nam);
2385 end if;
2386
edd63e9b
ES
2387 -- For a renaming-as-body, require subtype conformance, but if the
2388 -- declaration being completed has not been frozen, then inherit the
2389 -- convention of the renamed subprogram prior to checking conformance
2390 -- (unless the renaming has an explicit convention established; the
996ae0b0
RK
2391 -- rule stated in the RM doesn't seem to address this ???).
2392
2393 if Present (Rename_Spec) then
2394 Generate_Reference (Rename_Spec, Defining_Entity (Spec), 'b');
2395 Style.Check_Identifier (Defining_Entity (Spec), Rename_Spec);
2396
07fc65c4
GB
2397 if not Is_Frozen (Rename_Spec) then
2398 if not Has_Convention_Pragma (Rename_Spec) then
2399 Set_Convention (New_S, Convention (Old_S));
2400 end if;
2401
2402 if Ekind (Old_S) /= E_Operator then
2403 Check_Mode_Conformant (New_S, Old_S, Spec);
2404 end if;
2405
2406 if Original_Subprogram (Old_S) = Rename_Spec then
2407 Error_Msg_N ("unfrozen subprogram cannot rename itself ", N);
2408 end if;
2409 else
2410 Check_Subtype_Conformant (New_S, Old_S, Spec);
996ae0b0
RK
2411 end if;
2412
2413 Check_Frozen_Renaming (N, Rename_Spec);
996ae0b0 2414
5eb10f25 2415 -- Check explicitly that renamed entity is not intrinsic, because
16b05213 2416 -- in a generic the renamed body is not built. In this case,
5eb10f25
ES
2417 -- the renaming_as_body is a completion.
2418
2419 if Inside_A_Generic then
2420 if Is_Frozen (Rename_Spec)
2421 and then Is_Intrinsic_Subprogram (Old_S)
2422 then
2423 Error_Msg_N
2424 ("subprogram in renaming_as_body cannot be intrinsic",
2425 Name (N));
2426 end if;
2427
2428 Set_Has_Completion (Rename_Spec);
2429 end if;
2430
996ae0b0 2431 elsif Ekind (Old_S) /= E_Operator then
1138cf59
AC
2432
2433 -- If this is a default subprogram, it may be for a class-wide
2434 -- actual, in which case there is no check for mode conformance,
2435 -- given that the signatures do not match (the source mentions T,
2436 -- but the actual mentions T'Class).
2437
2438 if Is_Actual
2439 and then From_Default (N)
2440 then
2441 null;
2442
2443 else
2444 Check_Mode_Conformant (New_S, Old_S);
2445 end if;
996ae0b0
RK
2446
2447 if Is_Actual
2448 and then Error_Posted (New_S)
2449 then
2450 Error_Msg_NE ("invalid actual subprogram: & #!", N, Old_S);
2451 end if;
2452 end if;
2453
2454 if No (Rename_Spec) then
2455
2456 -- The parameter profile of the new entity is that of the renamed
2457 -- entity: the subtypes given in the specification are irrelevant.
2458
2459 Inherit_Renamed_Profile (New_S, Old_S);
2460
2461 -- A call to the subprogram is transformed into a call to the
2462 -- renamed entity. This is transitive if the renamed entity is
2463 -- itself a renaming.
2464
2465 if Present (Alias (Old_S)) then
2466 Set_Alias (New_S, Alias (Old_S));
2467 else
2468 Set_Alias (New_S, Old_S);
2469 end if;
2470
edd63e9b
ES
2471 -- Note that we do not set Is_Intrinsic_Subprogram if we have a
2472 -- renaming as body, since the entity in this case is not an
2473 -- intrinsic (it calls an intrinsic, but we have a real body for
2474 -- this call, and it is in this body that the required intrinsic
2475 -- processing will take place).
996ae0b0 2476
edd63e9b
ES
2477 -- Also, if this is a renaming of inequality, the renamed operator
2478 -- is intrinsic, but what matters is the corresponding equality
2479 -- operator, which may be user-defined.
fbf5a39b 2480
996ae0b0 2481 Set_Is_Intrinsic_Subprogram
fbf5a39b
AC
2482 (New_S,
2483 Is_Intrinsic_Subprogram (Old_S)
2484 and then
2485 (Chars (Old_S) /= Name_Op_Ne
2486 or else Ekind (Old_S) = E_Operator
2487 or else
2488 Is_Intrinsic_Subprogram
2489 (Corresponding_Equality (Old_S))));
996ae0b0
RK
2490
2491 if Ekind (Alias (New_S)) = E_Operator then
2492 Set_Has_Delayed_Freeze (New_S, False);
2493 end if;
2494
82c80734
RD
2495 -- If the renaming corresponds to an association for an abstract
2496 -- formal subprogram, then various attributes must be set to
2497 -- indicate that the renaming is an abstract dispatching operation
2498 -- with a controlling type.
2499
16ca248a
ES
2500 if Is_Actual and then Is_Abstract_Subprogram (Formal_Spec) then
2501
82c80734
RD
2502 -- Mark the renaming as abstract here, so Find_Dispatching_Type
2503 -- see it as corresponding to a generic association for a
2504 -- formal abstract subprogram
2505
16ca248a 2506 Set_Is_Abstract_Subprogram (New_S);
82c80734
RD
2507
2508 declare
2509 New_S_Ctrl_Type : constant Entity_Id :=
2510 Find_Dispatching_Type (New_S);
2511 Old_S_Ctrl_Type : constant Entity_Id :=
2512 Find_Dispatching_Type (Old_S);
2513
2514 begin
2515 if Old_S_Ctrl_Type /= New_S_Ctrl_Type then
2516 Error_Msg_NE
2517 ("actual must be dispatching subprogram for type&",
2518 Nam, New_S_Ctrl_Type);
2519
2520 else
2521 Set_Is_Dispatching_Operation (New_S);
2522 Check_Controlling_Formals (New_S_Ctrl_Type, New_S);
2523
16ca248a
ES
2524 -- If the actual in the formal subprogram is itself a
2525 -- formal abstract subprogram association, there's no
2526 -- dispatch table component or position to inherit.
82c80734
RD
2527
2528 if Present (DTC_Entity (Old_S)) then
2529 Set_DTC_Entity (New_S, DTC_Entity (Old_S));
2530 Set_DT_Position (New_S, DT_Position (Old_S));
2531 end if;
2532 end if;
2533 end;
2534 end if;
996ae0b0
RK
2535 end if;
2536
2537 if not Is_Actual
2538 and then (Old_S = New_S
2539 or else (Nkind (Nam) /= N_Expanded_Name
2540 and then Chars (Old_S) = Chars (New_S)))
2541 then
2542 Error_Msg_N ("subprogram cannot rename itself", N);
2543 end if;
2544
2545 Set_Convention (New_S, Convention (Old_S));
16ca248a
ES
2546
2547 if Is_Abstract_Subprogram (Old_S) then
2548 if Present (Rename_Spec) then
2549 Error_Msg_N
2550 ("a renaming-as-body cannot rename an abstract subprogram",
2551 N);
2552 Set_Has_Completion (Rename_Spec);
2553 else
2554 Set_Is_Abstract_Subprogram (New_S);
2555 end if;
2556 end if;
2557
996ae0b0
RK
2558 Check_Library_Unit_Renaming (N, Old_S);
2559
edd63e9b
ES
2560 -- Pathological case: procedure renames entry in the scope of its
2561 -- task. Entry is given by simple name, but body must be built for
2562 -- procedure. Of course if called it will deadlock.
996ae0b0
RK
2563
2564 if Ekind (Old_S) = E_Entry then
2565 Set_Has_Completion (New_S, False);
2566 Set_Alias (New_S, Empty);
2567 end if;
2568
2569 if Is_Actual then
2570 Freeze_Before (N, Old_S);
2571 Set_Has_Delayed_Freeze (New_S, False);
2572 Freeze_Before (N, New_S);
2573
82c80734
RD
2574 -- An abstract subprogram is only allowed as an actual in the case
2575 -- where the formal subprogram is also abstract.
2576
996ae0b0 2577 if (Ekind (Old_S) = E_Procedure or else Ekind (Old_S) = E_Function)
16ca248a
ES
2578 and then Is_Abstract_Subprogram (Old_S)
2579 and then not Is_Abstract_Subprogram (Formal_Spec)
996ae0b0
RK
2580 then
2581 Error_Msg_N
2582 ("abstract subprogram not allowed as generic actual", Nam);
2583 end if;
2584 end if;
2585
2586 else
edd63e9b
ES
2587 -- A common error is to assume that implicit operators for types are
2588 -- defined in Standard, or in the scope of a subtype. In those cases
2589 -- where the renamed entity is given with an expanded name, it is
2590 -- worth mentioning that operators for the type are not declared in
2591 -- the scope given by the prefix.
996ae0b0
RK
2592
2593 if Nkind (Nam) = N_Expanded_Name
2594 and then Nkind (Selector_Name (Nam)) = N_Operator_Symbol
2595 and then Scope (Entity (Nam)) = Standard_Standard
2596 then
2597 declare
2598 T : constant Entity_Id :=
2599 Base_Type (Etype (First_Formal (New_S)));
996ae0b0
RK
2600 begin
2601 Error_Msg_Node_2 := Prefix (Nam);
fbf5a39b
AC
2602 Error_Msg_NE
2603 ("operator for type& is not declared in&", Prefix (Nam), T);
996ae0b0 2604 end;
fbf5a39b 2605
996ae0b0
RK
2606 else
2607 Error_Msg_NE
2608 ("no visible subprogram matches the specification for&",
2609 Spec, New_S);
2610 end if;
2611
2612 if Present (Candidate_Renaming) then
2613 declare
2614 F1 : Entity_Id;
2615 F2 : Entity_Id;
70b70ce8 2616 T1 : Entity_Id;
996ae0b0
RK
2617
2618 begin
2619 F1 := First_Formal (Candidate_Renaming);
2620 F2 := First_Formal (New_S);
70b70ce8 2621 T1 := First_Subtype (Etype (F1));
996ae0b0
RK
2622
2623 while Present (F1) and then Present (F2) loop
2624 Next_Formal (F1);
2625 Next_Formal (F2);
2626 end loop;
2627
2628 if Present (F1) and then Present (Default_Value (F1)) then
2629 if Present (Next_Formal (F1)) then
2630 Error_Msg_NE
2631 ("\missing specification for &" &
2632 " and other formals with defaults", Spec, F1);
2633 else
2634 Error_Msg_NE
2635 ("\missing specification for &", Spec, F1);
2636 end if;
2637 end if;
70b70ce8
AC
2638
2639 if Nkind (Nam) = N_Operator_Symbol
2640 and then From_Default (N)
2641 then
2642 Error_Msg_Node_2 := T1;
2643 Error_Msg_NE
2644 ("default & on & is not directly visible",
2645 Nam, Nam);
2646 end if;
996ae0b0
RK
2647 end;
2648 end if;
2649 end if;
2650
edd63e9b
ES
2651 -- Ada 2005 AI 404: if the new subprogram is dispatching, verify that
2652 -- controlling access parameters are known non-null for the renamed
2653 -- subprogram. Test also applies to a subprogram instantiation that
cdc8c54c
BD
2654 -- is dispatching. Test is skipped if some previous error was detected
2655 -- that set Old_S to Any_Id.
edd63e9b 2656
0791fbe9 2657 if Ada_Version >= Ada_2005
cdc8c54c 2658 and then Old_S /= Any_Id
edd63e9b
ES
2659 and then not Is_Dispatching_Operation (Old_S)
2660 and then Is_Dispatching_Operation (New_S)
2661 then
2662 declare
2663 Old_F : Entity_Id;
2664 New_F : Entity_Id;
2665
2666 begin
2667 Old_F := First_Formal (Old_S);
2668 New_F := First_Formal (New_S);
2669 while Present (Old_F) loop
2670 if Ekind (Etype (Old_F)) = E_Anonymous_Access_Type
2671 and then Is_Controlling_Formal (New_F)
2672 and then not Can_Never_Be_Null (Old_F)
2673 then
2674 Error_Msg_N ("access parameter is controlling,", New_F);
fbe627af
RD
2675 Error_Msg_NE
2676 ("\corresponding parameter of& "
2677 & "must be explicitly null excluding", New_F, Old_S);
edd63e9b
ES
2678 end if;
2679
2680 Next_Formal (Old_F);
2681 Next_Formal (New_F);
2682 end loop;
2683 end;
2684 end if;
2685
725393ea 2686 -- A useful warning, suggested by Ada Bug Finder (Ada-Europe 2005)
b5c739f9 2687 -- is to warn if an operator is being renamed as a different operator.
880dabb5
AC
2688 -- If the operator is predefined, examine the kind of the entity, not
2689 -- the abbreviated declaration in Standard.
725393ea
ES
2690
2691 if Comes_From_Source (N)
2692 and then Present (Old_S)
880dabb5
AC
2693 and then
2694 (Nkind (Old_S) = N_Defining_Operator_Symbol
2695 or else Ekind (Old_S) = E_Operator)
725393ea
ES
2696 and then Nkind (New_S) = N_Defining_Operator_Symbol
2697 and then Chars (Old_S) /= Chars (New_S)
2698 then
2699 Error_Msg_NE
880dabb5 2700 ("?& is being renamed as a different operator", N, Old_S);
725393ea
ES
2701 end if;
2702
b5c739f9
RD
2703 -- Check for renaming of obsolescent subprogram
2704
2705 Check_Obsolescent_2005_Entity (Entity (Nam), Nam);
2706
11560bcc
TQ
2707 -- Another warning or some utility: if the new subprogram as the same
2708 -- name as the old one, the old one is not hidden by an outer homograph,
2709 -- the new one is not a public symbol, and the old one is otherwise
2710 -- directly visible, the renaming is superfluous.
2711
2712 if Chars (Old_S) = Chars (New_S)
2713 and then Comes_From_Source (N)
2714 and then Scope (Old_S) /= Standard_Standard
2715 and then Warn_On_Redundant_Constructs
2716 and then
2717 (Is_Immediately_Visible (Old_S)
2718 or else Is_Potentially_Use_Visible (Old_S))
2719 and then Is_Overloadable (Current_Scope)
2720 and then Chars (Current_Scope) /= Chars (Old_S)
2721 then
2722 Error_Msg_N
2723 ("?redundant renaming, entity is directly visible", Name (N));
2724 end if;
2725
0ab80019 2726 Ada_Version := Save_AV;
edd63e9b 2727 Ada_Version_Explicit := Save_AV_Exp;
996ae0b0
RK
2728 end Analyze_Subprogram_Renaming;
2729
2730 -------------------------
2731 -- Analyze_Use_Package --
2732 -------------------------
2733
2734 -- Resolve the package names in the use clause, and make all the visible
2735 -- entities defined in the package potentially use-visible. If the package
2736 -- is already in use from a previous use clause, its visible entities are
2737 -- already use-visible. In that case, mark the occurrence as a redundant
2738 -- use. If the package is an open scope, i.e. if the use clause occurs
2739 -- within the package itself, ignore it.
2740
2741 procedure Analyze_Use_Package (N : Node_Id) is
2742 Pack_Name : Node_Id;
2743 Pack : Entity_Id;
2744
996ae0b0
RK
2745 -- Start of processing for Analyze_Use_Package
2746
2747 begin
fe5d3068 2748 Check_Formal_Restriction ("use clause is not allowed", N);
1d801f21 2749
996ae0b0
RK
2750 Set_Hidden_By_Use_Clause (N, No_Elist);
2751
7394c8cc
AC
2752 -- Use clause not allowed in a spec of a predefined package declaration
2753 -- except that packages whose file name starts a-n are OK (these are
2754 -- children of Ada.Numerics, which are never loaded by Rtsfind).
996ae0b0
RK
2755
2756 if Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
2757 and then Name_Buffer (1 .. 3) /= "a-n"
2758 and then
2759 Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
2760 then
2761 Error_Msg_N ("use clause not allowed in predefined spec", N);
2762 end if;
2763
bc41faa2 2764 -- Chain clause to list of use clauses in current scope
996ae0b0
RK
2765
2766 if Nkind (Parent (N)) /= N_Compilation_Unit then
2767 Chain_Use_Clause (N);
2768 end if;
2769
2770 -- Loop through package names to identify referenced packages
2771
2772 Pack_Name := First (Names (N));
996ae0b0
RK
2773 while Present (Pack_Name) loop
2774 Analyze (Pack_Name);
2775
2776 if Nkind (Parent (N)) = N_Compilation_Unit
2777 and then Nkind (Pack_Name) = N_Expanded_Name
2778 then
2779 declare
16ca248a 2780 Pref : Node_Id;
996ae0b0
RK
2781
2782 begin
16ca248a 2783 Pref := Prefix (Pack_Name);
996ae0b0
RK
2784 while Nkind (Pref) = N_Expanded_Name loop
2785 Pref := Prefix (Pref);
2786 end loop;
2787
2788 if Entity (Pref) = Standard_Standard then
2789 Error_Msg_N
2790 ("predefined package Standard cannot appear"
2791 & " in a context clause", Pref);
2792 end if;
2793 end;
2794 end if;
2795
2796 Next (Pack_Name);
2797 end loop;
2798
2799 -- Loop through package names to mark all entities as potentially
2800 -- use visible.
2801
2802 Pack_Name := First (Names (N));
996ae0b0 2803 while Present (Pack_Name) loop
996ae0b0
RK
2804 if Is_Entity_Name (Pack_Name) then
2805 Pack := Entity (Pack_Name);
2806
2807 if Ekind (Pack) /= E_Package
2808 and then Etype (Pack) /= Any_Type
2809 then
2810 if Ekind (Pack) = E_Generic_Package then
483c78cb 2811 Error_Msg_N -- CODEFIX
996ae0b0
RK
2812 ("a generic package is not allowed in a use clause",
2813 Pack_Name);
2814 else
ed2233dc 2815 Error_Msg_N ("& is not a usable package", Pack_Name);
996ae0b0
RK
2816 end if;
2817
fbf5a39b
AC
2818 else
2819 if Nkind (Parent (N)) = N_Compilation_Unit then
2820 Check_In_Previous_With_Clause (N, Pack_Name);
2821 end if;
996ae0b0 2822
fbf5a39b
AC
2823 if Applicable_Use (Pack_Name) then
2824 Use_One_Package (Pack, N);
2825 end if;
996ae0b0 2826 end if;
462c31ef
ST
2827
2828 -- Report error because name denotes something other than a package
2829
2830 else
2831 Error_Msg_N ("& is not a package", Pack_Name);
996ae0b0
RK
2832 end if;
2833
2834 Next (Pack_Name);
2835 end loop;
996ae0b0
RK
2836 end Analyze_Use_Package;
2837
2838 ----------------------
2839 -- Analyze_Use_Type --
2840 ----------------------
2841
2842 procedure Analyze_Use_Type (N : Node_Id) is
954c111a 2843 E : Entity_Id;
ecc4ddde 2844 Id : Node_Id;
996ae0b0
RK
2845
2846 begin
2847 Set_Hidden_By_Use_Clause (N, No_Elist);
2848
bc41faa2 2849 -- Chain clause to list of use clauses in current scope
996ae0b0
RK
2850
2851 if Nkind (Parent (N)) /= N_Compilation_Unit then
2852 Chain_Use_Clause (N);
2853 end if;
2854
780d052e
RD
2855 -- If the Used_Operations list is already initialized, the clause has
2856 -- been analyzed previously, and it is begin reinstalled, for example
2857 -- when the clause appears in a package spec and we are compiling the
2858 -- corresponding package body. In that case, make the entities on the
806f6d37 2859 -- existing list use_visible, and mark the corresponding types In_Use.
29efbb8c 2860
780d052e 2861 if Present (Used_Operations (N)) then
29efbb8c 2862 declare
806f6d37 2863 Mark : Node_Id;
29efbb8c 2864 Elmt : Elmt_Id;
806f6d37 2865
29efbb8c 2866 begin
806f6d37
AC
2867 Mark := First (Subtype_Marks (N));
2868 while Present (Mark) loop
7ff2d234 2869 Use_One_Type (Mark, Installed => True);
806f6d37
AC
2870 Next (Mark);
2871 end loop;
2872
29efbb8c
ES
2873 Elmt := First_Elmt (Used_Operations (N));
2874 while Present (Elmt) loop
2875 Set_Is_Potentially_Use_Visible (Node (Elmt));
2876 Next_Elmt (Elmt);
2877 end loop;
2878 end;
2879
2880 return;
2881 end if;
2882
780d052e
RD
2883 -- Otherwise, create new list and attach to it the operations that
2884 -- are made use-visible by the clause.
2885
29efbb8c 2886 Set_Used_Operations (N, New_Elmt_List);
996ae0b0 2887 Id := First (Subtype_Marks (N));
996ae0b0
RK
2888 while Present (Id) loop
2889 Find_Type (Id);
954c111a 2890 E := Entity (Id);
996ae0b0 2891
954c111a 2892 if E /= Any_Type then
07fc65c4 2893 Use_One_Type (Id);
fbf5a39b
AC
2894
2895 if Nkind (Parent (N)) = N_Compilation_Unit then
923fa078 2896 if Nkind (Id) = N_Identifier then
edd63e9b 2897 Error_Msg_N ("type is not directly visible", Id);
fbf5a39b 2898
954c111a
HK
2899 elsif Is_Child_Unit (Scope (E))
2900 and then Scope (E) /= System_Aux_Id
fbf5a39b
AC
2901 then
2902 Check_In_Previous_With_Clause (N, Prefix (Id));
2903 end if;
2904 end if;
ecc4ddde
AC
2905
2906 else
4c8a5bb8 2907 -- If the use_type_clause appears in a compilation unit context,
ecc4ddde 2908 -- check whether it comes from a unit that may appear in a
4c8a5bb8 2909 -- limited_with_clause, for a better error message.
ecc4ddde
AC
2910
2911 if Nkind (Parent (N)) = N_Compilation_Unit
2912 and then Nkind (Id) /= N_Identifier
2913 then
2914 declare
2915 Item : Node_Id;
2916 Pref : Node_Id;
2917
2918 function Mentioned (Nam : Node_Id) return Boolean;
4c8a5bb8
AC
2919 -- Check whether the prefix of expanded name for the type
2920 -- appears in the prefix of some limited_with_clause.
2921
2922 ---------------
2923 -- Mentioned --
2924 ---------------
ecc4ddde
AC
2925
2926 function Mentioned (Nam : Node_Id) return Boolean is
2927 begin
4c8a5bb8
AC
2928 return Nkind (Name (Item)) = N_Selected_Component
2929 and then
2930 Chars (Prefix (Name (Item))) = Chars (Nam);
ecc4ddde
AC
2931 end Mentioned;
2932
2933 begin
2934 Pref := Prefix (Id);
2935 Item := First (Context_Items (Parent (N)));
4c8a5bb8
AC
2936
2937 while Present (Item) and then Item /= N loop
ecc4ddde
AC
2938 if Nkind (Item) = N_With_Clause
2939 and then Limited_Present (Item)
2940 and then Mentioned (Pref)
2941 then
4c8a5bb8
AC
2942 Change_Error_Text
2943 (Get_Msg_Id, "premature usage of incomplete type");
ecc4ddde
AC
2944 end if;
2945
2946 Next (Item);
2947 end loop;
2948 end;
2949 end if;
996ae0b0
RK
2950 end if;
2951
2952 Next (Id);
2953 end loop;
2954 end Analyze_Use_Type;
2955
2956 --------------------
2957 -- Applicable_Use --
2958 --------------------
2959
2960 function Applicable_Use (Pack_Name : Node_Id) return Boolean is
2961 Pack : constant Entity_Id := Entity (Pack_Name);
2962
2963 begin
2964 if In_Open_Scopes (Pack) then
954c111a
HK
2965 if Warn_On_Redundant_Constructs
2966 and then Pack = Current_Scope
2967 then
ed2233dc 2968 Error_Msg_NE -- CODEFIX
954c111a
HK
2969 ("& is already use-visible within itself?", Pack_Name, Pack);
2970 end if;
2971
996ae0b0
RK
2972 return False;
2973
2974 elsif In_Use (Pack) then
d4810530 2975 Note_Redundant_Use (Pack_Name);
996ae0b0
RK
2976 return False;
2977
2978 elsif Present (Renamed_Object (Pack))
2979 and then In_Use (Renamed_Object (Pack))
2980 then
d4810530 2981 Note_Redundant_Use (Pack_Name);
996ae0b0
RK
2982 return False;
2983
2984 else
2985 return True;
2986 end if;
2987 end Applicable_Use;
2988
2989 ------------------------
2990 -- Attribute_Renaming --
2991 ------------------------
2992
2993 procedure Attribute_Renaming (N : Node_Id) is
2994 Loc : constant Source_Ptr := Sloc (N);
2995 Nam : constant Node_Id := Name (N);
2996 Spec : constant Node_Id := Specification (N);
2997 New_S : constant Entity_Id := Defining_Unit_Name (Spec);
2998 Aname : constant Name_Id := Attribute_Name (Nam);
2999
3000 Form_Num : Nat := 0;
3001 Expr_List : List_Id := No_List;
3002
3003 Attr_Node : Node_Id;
3004 Body_Node : Node_Id;
3005 Param_Spec : Node_Id;
3006
3007 begin
3008 Generate_Definition (New_S);
3009
4c8a5bb8
AC
3010 -- This procedure is called in the context of subprogram renaming, and
3011 -- thus the attribute must be one that is a subprogram. All of those
3012 -- have at least one formal parameter, with the singular exception of
3013 -- AST_Entry (which is a real oddity, it is odd that this can be renamed
3014 -- at all!)
996ae0b0
RK
3015
3016 if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then
3017 if Aname /= Name_AST_Entry then
3018 Error_Msg_N
3019 ("subprogram renaming an attribute must have formals", N);
3020 return;
3021 end if;
3022
3023 else
3024 Param_Spec := First (Parameter_Specifications (Spec));
996ae0b0
RK
3025 while Present (Param_Spec) loop
3026 Form_Num := Form_Num + 1;
3027
3028 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
3029 Find_Type (Parameter_Type (Param_Spec));
3030
3031 -- The profile of the new entity denotes the base type (s) of
3032 -- the types given in the specification. For access parameters
3033 -- there are no subtypes involved.
3034
3035 Rewrite (Parameter_Type (Param_Spec),
3036 New_Reference_To
3037 (Base_Type (Entity (Parameter_Type (Param_Spec))), Loc));
3038 end if;
3039
3040 if No (Expr_List) then
3041 Expr_List := New_List;
3042 end if;
3043
3044 Append_To (Expr_List,
3045 Make_Identifier (Loc,
3046 Chars => Chars (Defining_Identifier (Param_Spec))));
3047
fbf5a39b 3048 -- The expressions in the attribute reference are not freeze
4c8a5bb8 3049 -- points. Neither is the attribute as a whole, see below.
fbf5a39b
AC
3050
3051 Set_Must_Not_Freeze (Last (Expr_List));
996ae0b0
RK
3052 Next (Param_Spec);
3053 end loop;
3054 end if;
3055
4c8a5bb8
AC
3056 -- Immediate error if too many formals. Other mismatches in number or
3057 -- types of parameters are detected when we analyze the body of the
3058 -- subprogram that we construct.
996ae0b0
RK
3059
3060 if Form_Num > 2 then
3061 Error_Msg_N ("too many formals for attribute", N);
3062
4c8a5bb8
AC
3063 -- Error if the attribute reference has expressions that look like
3064 -- formal parameters.
0da2c8ac
AC
3065
3066 elsif Present (Expressions (Nam)) then
3067 Error_Msg_N ("illegal expressions in attribute reference", Nam);
3068
996ae0b0
RK
3069 elsif
3070 Aname = Name_Compose or else
3071 Aname = Name_Exponent or else
3072 Aname = Name_Leading_Part or else
3073 Aname = Name_Pos or else
3074 Aname = Name_Round or else
3075 Aname = Name_Scaling or else
3076 Aname = Name_Val
3077 then
3078 if Nkind (N) = N_Subprogram_Renaming_Declaration
82c80734 3079 and then Present (Corresponding_Formal_Spec (N))
996ae0b0
RK
3080 then
3081 Error_Msg_N
3082 ("generic actual cannot be attribute involving universal type",
3083 Nam);
3084 else
3085 Error_Msg_N
3086 ("attribute involving a universal type cannot be renamed",
3087 Nam);
3088 end if;
3089 end if;
3090
4c8a5bb8
AC
3091 -- AST_Entry is an odd case. It doesn't really make much sense to allow
3092 -- it to be renamed, but that's the DEC rule, so we have to do it right.
3093 -- The point is that the AST_Entry call should be made now, and what the
3094 -- function will return is the returned value.
996ae0b0
RK
3095
3096 -- Note that there is no Expr_List in this case anyway
3097
3098 if Aname = Name_AST_Entry then
996ae0b0 3099 declare
092ef350 3100 Ent : constant Entity_Id := Make_Temporary (Loc, 'R', Nam);
996ae0b0
RK
3101 Decl : Node_Id;
3102
3103 begin
996ae0b0
RK
3104 Decl :=
3105 Make_Object_Declaration (Loc,
3106 Defining_Identifier => Ent,
092ef350 3107 Object_Definition =>
996ae0b0 3108 New_Occurrence_Of (RTE (RE_AST_Handler), Loc),
092ef350
RD
3109 Expression => Nam,
3110 Constant_Present => True);
996ae0b0
RK
3111
3112 Set_Assignment_OK (Decl, True);
3113 Insert_Action (N, Decl);
3114 Attr_Node := Make_Identifier (Loc, Chars (Ent));
3115 end;
3116
3117 -- For all other attributes, we rewrite the attribute node to have
3118 -- a list of expressions corresponding to the subprogram formals.
3119 -- A renaming declaration is not a freeze point, and the analysis of
3120 -- the attribute reference should not freeze the type of the prefix.
3121
3122 else
3123 Attr_Node :=
3124 Make_Attribute_Reference (Loc,
3125 Prefix => Prefix (Nam),
3126 Attribute_Name => Aname,
3127 Expressions => Expr_List);
3128
3129 Set_Must_Not_Freeze (Attr_Node);
3130 Set_Must_Not_Freeze (Prefix (Nam));
3131 end if;
3132
3133 -- Case of renaming a function
3134
3135 if Nkind (Spec) = N_Function_Specification then
996ae0b0
RK
3136 if Is_Procedure_Attribute_Name (Aname) then
3137 Error_Msg_N ("attribute can only be renamed as procedure", Nam);
3138 return;
3139 end if;
3140
725393ea
ES
3141 Find_Type (Result_Definition (Spec));
3142 Rewrite (Result_Definition (Spec),
3143 New_Reference_To (
3144 Base_Type (Entity (Result_Definition (Spec))), Loc));
996ae0b0
RK
3145
3146 Body_Node :=
3147 Make_Subprogram_Body (Loc,
3148 Specification => Spec,
3149 Declarations => New_List,
3150 Handled_Statement_Sequence =>
3151 Make_Handled_Sequence_Of_Statements (Loc,
3152 Statements => New_List (
11560bcc 3153 Make_Simple_Return_Statement (Loc,
996ae0b0
RK
3154 Expression => Attr_Node))));
3155
3156 -- Case of renaming a procedure
3157
3158 else
3159 if not Is_Procedure_Attribute_Name (Aname) then
3160 Error_Msg_N ("attribute can only be renamed as function", Nam);
3161 return;
3162 end if;
3163
3164 Body_Node :=
3165 Make_Subprogram_Body (Loc,
3166 Specification => Spec,
3167 Declarations => New_List,
3168 Handled_Statement_Sequence =>
3169 Make_Handled_Sequence_Of_Statements (Loc,
3170 Statements => New_List (Attr_Node)));
3171 end if;
3172
fbe627af
RD
3173 -- In case of tagged types we add the body of the generated function to
3174 -- the freezing actions of the type (because in the general case such
3175 -- type is still not frozen). We exclude from this processing generic
3176 -- formal subprograms found in instantiations and AST_Entry renamings.
3177
ddc1515a
AC
3178 -- We must exclude VM targets because entity AST_Handler is defined in
3179 -- package System.Aux_Dec which is not available in those platforms.
3180
3181 if VM_Target = No_VM
3182 and then not Present (Corresponding_Formal_Spec (N))
fbe627af
RD
3183 and then Etype (Nam) /= RTE (RE_AST_Handler)
3184 then
3185 declare
3186 P : constant Entity_Id := Prefix (Nam);
3187
3188 begin
3189 Find_Type (P);
3190
3191 if Is_Tagged_Type (Etype (P)) then
3192 Ensure_Freeze_Node (Etype (P));
3193 Append_Freeze_Action (Etype (P), Body_Node);
3194 else
3195 Rewrite (N, Body_Node);
3196 Analyze (N);
3197 Set_Etype (New_S, Base_Type (Etype (New_S)));
3198 end if;
3199 end;
3200
3201 -- Generic formal subprograms or AST_Handler renaming
3202
3203 else
3204 Rewrite (N, Body_Node);
3205 Analyze (N);
3206 Set_Etype (New_S, Base_Type (Etype (New_S)));
3207 end if;
996ae0b0 3208
615cbd95
AC
3209 if Is_Compilation_Unit (New_S) then
3210 Error_Msg_N
3211 ("a library unit can only rename another library unit", N);
3212 end if;
3213
996ae0b0
RK
3214 -- We suppress elaboration warnings for the resulting entity, since
3215 -- clearly they are not needed, and more particularly, in the case
3216 -- of a generic formal subprogram, the resulting entity can appear
3217 -- after the instantiation itself, and thus look like a bogus case
3218 -- of access before elaboration.
3219
3220 Set_Suppress_Elaboration_Warnings (New_S);
3221
3222 end Attribute_Renaming;
3223
3224 ----------------------
3225 -- Chain_Use_Clause --
3226 ----------------------
3227
3228 procedure Chain_Use_Clause (N : Node_Id) is
d4810530
ES
3229 Pack : Entity_Id;
3230 Level : Int := Scope_Stack.Last;
3231
996ae0b0 3232 begin
d4810530
ES
3233 if not Is_Compilation_Unit (Current_Scope)
3234 or else not Is_Child_Unit (Current_Scope)
3235 then
3236 null; -- Common case
3237
3238 elsif Defining_Entity (Parent (N)) = Current_Scope then
3239 null; -- Common case for compilation unit
3240
3241 else
3242 -- If declaration appears in some other scope, it must be in some
3243 -- parent unit when compiling a child.
3244
3245 Pack := Defining_Entity (Parent (N));
3246 if not In_Open_Scopes (Pack) then
3247 null; -- default as well
3248
3249 else
3250 -- Find entry for parent unit in scope stack
3251
3252 while Scope_Stack.Table (Level).Entity /= Pack loop
3253 Level := Level - 1;
3254 end loop;
3255 end if;
3256 end if;
3257
996ae0b0 3258 Set_Next_Use_Clause (N,
d4810530
ES
3259 Scope_Stack.Table (Level).First_Use_Clause);
3260 Scope_Stack.Table (Level).First_Use_Clause := N;
996ae0b0
RK
3261 end Chain_Use_Clause;
3262
15ce9ca2
AC
3263 ---------------------------
3264 -- Check_Frozen_Renaming --
3265 ---------------------------
996ae0b0
RK
3266
3267 procedure Check_Frozen_Renaming (N : Node_Id; Subp : Entity_Id) is
3268 B_Node : Node_Id;
3269 Old_S : Entity_Id;
3270
3271 begin
3272 if Is_Frozen (Subp)
3273 and then not Has_Completion (Subp)
3274 then
3275 B_Node :=
3276 Build_Renamed_Body
3277 (Parent (Declaration_Node (Subp)), Defining_Entity (N));
3278
3279 if Is_Entity_Name (Name (N)) then
3280 Old_S := Entity (Name (N));
3281
fbf5a39b
AC
3282 if not Is_Frozen (Old_S)
3283 and then Operating_Mode /= Check_Semantics
3284 then
3285 Append_Freeze_Action (Old_S, B_Node);
996ae0b0
RK
3286 else
3287 Insert_After (N, B_Node);
3288 Analyze (B_Node);
3289 end if;
3290
3291 if Is_Intrinsic_Subprogram (Old_S)
3292 and then not In_Instance
3293 then
3294 Error_Msg_N
3295 ("subprogram used in renaming_as_body cannot be intrinsic",
3296 Name (N));
3297 end if;
3298
3299 else
3300 Insert_After (N, B_Node);
3301 Analyze (B_Node);
3302 end if;
3303 end if;
3304 end Check_Frozen_Renaming;
3305
4c484f40
AC
3306 -------------------------------
3307 -- Set_Entity_Or_Discriminal --
3308 -------------------------------
3309
3310 procedure Set_Entity_Or_Discriminal (N : Node_Id; E : Entity_Id) is
3311 P : Node_Id;
3312
3313 begin
3314 -- If the entity is not a discriminant, or else expansion is disabled,
3315 -- simply set the entity.
3316
3317 if not In_Spec_Expression
3318 or else Ekind (E) /= E_Discriminant
3319 or else Inside_A_Generic
3320 then
3321 Set_Entity_With_Style_Check (N, E);
3322
3323 -- The replacement of a discriminant by the corresponding discriminal
3324 -- is not done for a task discriminant that appears in a default
8779dffa 3325 -- expression of an entry parameter. See Exp_Ch2.Expand_Discriminant
4c484f40
AC
3326 -- for details on their handling.
3327
3328 elsif Is_Concurrent_Type (Scope (E)) then
3329
3330 P := Parent (N);
3331 while Present (P)
3332 and then not Nkind_In (P, N_Parameter_Specification,
3333 N_Component_Declaration)
3334 loop
3335 P := Parent (P);
3336 end loop;
3337
3338 if Present (P)
3339 and then Nkind (P) = N_Parameter_Specification
3340 then
3341 null;
3342
3343 else
3344 Set_Entity (N, Discriminal (E));
3345 end if;
3346
3347 -- Otherwise, this is a discriminant in a context in which
3348 -- it is a reference to the corresponding parameter of the
3349 -- init proc for the enclosing type.
3350
3351 else
3352 Set_Entity (N, Discriminal (E));
3353 end if;
3354 end Set_Entity_Or_Discriminal;
3355
fbf5a39b
AC
3356 -----------------------------------
3357 -- Check_In_Previous_With_Clause --
3358 -----------------------------------
3359
3360 procedure Check_In_Previous_With_Clause
3361 (N : Node_Id;
3362 Nam : Entity_Id)
3363 is
3364 Pack : constant Entity_Id := Entity (Original_Node (Nam));
3365 Item : Node_Id;
3366 Par : Node_Id;
3367
3368 begin
3369 Item := First (Context_Items (Parent (N)));
3370
3371 while Present (Item)
3372 and then Item /= N
3373 loop
3374 if Nkind (Item) = N_With_Clause
edd63e9b 3375
16ca248a 3376 -- Protect the frontend against previous critical errors
edd63e9b
ES
3377
3378 and then Nkind (Name (Item)) /= N_Selected_Component
fbf5a39b
AC
3379 and then Entity (Name (Item)) = Pack
3380 then
3381 Par := Nam;
3382
bc41faa2 3383 -- Find root library unit in with_clause
fbf5a39b
AC
3384
3385 while Nkind (Par) = N_Expanded_Name loop
3386 Par := Prefix (Par);
3387 end loop;
3388
3389 if Is_Child_Unit (Entity (Original_Node (Par))) then
ed2233dc 3390 Error_Msg_NE ("& is not directly visible", Par, Entity (Par));
fbf5a39b
AC
3391 else
3392 return;
3393 end if;
3394 end if;
3395
3396 Next (Item);
3397 end loop;
3398
3399 -- On exit, package is not mentioned in a previous with_clause.
3400 -- Check if its prefix is.
3401
3402 if Nkind (Nam) = N_Expanded_Name then
3403 Check_In_Previous_With_Clause (N, Prefix (Nam));
3404
3405 elsif Pack /= Any_Id then
3406 Error_Msg_NE ("& is not visible", Nam, Pack);
3407 end if;
3408 end Check_In_Previous_With_Clause;
3409
996ae0b0
RK
3410 ---------------------------------
3411 -- Check_Library_Unit_Renaming --
3412 ---------------------------------
3413
3414 procedure Check_Library_Unit_Renaming (N : Node_Id; Old_E : Entity_Id) is
3415 New_E : Entity_Id;
3416
3417 begin
3418 if Nkind (Parent (N)) /= N_Compilation_Unit then
3419 return;
3420
fbe627af
RD
3421 -- Check for library unit. Note that we used to check for the scope
3422 -- being Standard here, but that was wrong for Standard itself.
3423
3424 elsif not Is_Compilation_Unit (Old_E)
996ae0b0
RK
3425 and then not Is_Child_Unit (Old_E)
3426 then
3427 Error_Msg_N ("renamed unit must be a library unit", Name (N));
3428
615cbd95
AC
3429 -- Entities defined in Standard (operators and boolean literals) cannot
3430 -- be renamed as library units.
3431
3432 elsif Scope (Old_E) = Standard_Standard
3433 and then Sloc (Old_E) = Standard_Location
3434 then
3435 Error_Msg_N ("renamed unit must be a library unit", Name (N));
3436
996ae0b0
RK
3437 elsif Present (Parent_Spec (N))
3438 and then Nkind (Unit (Parent_Spec (N))) = N_Generic_Package_Declaration
3439 and then not Is_Child_Unit (Old_E)
3440 then
3441 Error_Msg_N
3442 ("renamed unit must be a child unit of generic parent", Name (N));
3443
3444 elsif Nkind (N) in N_Generic_Renaming_Declaration
3445 and then Nkind (Name (N)) = N_Expanded_Name
3446 and then Is_Generic_Instance (Entity (Prefix (Name (N))))
3447 and then Is_Generic_Unit (Old_E)
3448 then
3449 Error_Msg_N
3450 ("renamed generic unit must be a library unit", Name (N));
3451
b9b2405f
ST
3452 elsif Is_Package_Or_Generic_Package (Old_E) then
3453
996ae0b0
RK
3454 -- Inherit categorization flags
3455
3456 New_E := Defining_Entity (N);
3457 Set_Is_Pure (New_E, Is_Pure (Old_E));
3458 Set_Is_Preelaborated (New_E, Is_Preelaborated (Old_E));
3459 Set_Is_Remote_Call_Interface (New_E,
3460 Is_Remote_Call_Interface (Old_E));
3461 Set_Is_Remote_Types (New_E, Is_Remote_Types (Old_E));
3462 Set_Is_Shared_Passive (New_E, Is_Shared_Passive (Old_E));
3463 end if;
3464 end Check_Library_Unit_Renaming;
3465
3466 ---------------
3467 -- End_Scope --
3468 ---------------
3469
3470 procedure End_Scope is
3471 Id : Entity_Id;
3472 Prev : Entity_Id;
3473 Outer : Entity_Id;
3474
3475 begin
3476 Id := First_Entity (Current_Scope);
996ae0b0
RK
3477 while Present (Id) loop
3478 -- An entity in the current scope is not necessarily the first one
3479 -- on its homonym chain. Find its predecessor if any,
3480 -- If it is an internal entity, it will not be in the visibility
3481 -- chain altogether, and there is nothing to unchain.
3482
3483 if Id /= Current_Entity (Id) then
3484 Prev := Current_Entity (Id);
3485 while Present (Prev)
3486 and then Present (Homonym (Prev))
3487 and then Homonym (Prev) /= Id
3488 loop
3489 Prev := Homonym (Prev);
3490 end loop;
3491
3492 -- Skip to end of loop if Id is not in the visibility chain
3493
3494 if No (Prev) or else Homonym (Prev) /= Id then
3495 goto Next_Ent;
3496 end if;
3497
3498 else
3499 Prev := Empty;
3500 end if;
3501
996ae0b0
RK
3502 Set_Is_Immediately_Visible (Id, False);
3503
16ca248a 3504 Outer := Homonym (Id);
996ae0b0
RK
3505 while Present (Outer) and then Scope (Outer) = Current_Scope loop
3506 Outer := Homonym (Outer);
3507 end loop;
3508
3509 -- Reset homonym link of other entities, but do not modify link
3510 -- between entities in current scope, so that the back-end can have
3511 -- a proper count of local overloadings.
3512
3513 if No (Prev) then
3514 Set_Name_Entity_Id (Chars (Id), Outer);
3515
3516 elsif Scope (Prev) /= Scope (Id) then
3517 Set_Homonym (Prev, Outer);
3518 end if;
3519
3520 <<Next_Ent>>
3521 Next_Entity (Id);
3522 end loop;
3523
3524 -- If the scope generated freeze actions, place them before the
3525 -- current declaration and analyze them. Type declarations and
3526 -- the bodies of initialization procedures can generate such nodes.
3527 -- We follow the parent chain until we reach a list node, which is
3528 -- the enclosing list of declarations. If the list appears within
3529 -- a protected definition, move freeze nodes outside the protected
3530 -- type altogether.
3531
3532 if Present
3533 (Scope_Stack.Table (Scope_Stack.Last).Pending_Freeze_Actions)
3534 then
3535 declare
3536 Decl : Node_Id;
3537 L : constant List_Id := Scope_Stack.Table
3538 (Scope_Stack.Last).Pending_Freeze_Actions;
3539
3540 begin
3541 if Is_Itype (Current_Scope) then
3542 Decl := Associated_Node_For_Itype (Current_Scope);
3543 else
3544 Decl := Parent (Current_Scope);
3545 end if;
3546
3547 Pop_Scope;
3548
3549 while not (Is_List_Member (Decl))
294ccb21
RD
3550 or else Nkind_In (Parent (Decl), N_Protected_Definition,
3551 N_Task_Definition)
996ae0b0
RK
3552 loop
3553 Decl := Parent (Decl);
3554 end loop;
3555
3556 Insert_List_Before_And_Analyze (Decl, L);
3557 end;
3558
3559 else
3560 Pop_Scope;
3561 end if;
3562
3563 end End_Scope;
3564
3565 ---------------------
3566 -- End_Use_Clauses --
3567 ---------------------
3568
3569 procedure End_Use_Clauses (Clause : Node_Id) is
fbf5a39b 3570 U : Node_Id;
996ae0b0
RK
3571
3572 begin
fbf5a39b
AC
3573 -- Remove Use_Type clauses first, because they affect the
3574 -- visibility of operators in subsequent used packages.
3575
3576 U := Clause;
3577 while Present (U) loop
3578 if Nkind (U) = N_Use_Type_Clause then
3579 End_Use_Type (U);
3580 end if;
3581
3582 Next_Use_Clause (U);
3583 end loop;
3584
3585 U := Clause;
996ae0b0
RK
3586 while Present (U) loop
3587 if Nkind (U) = N_Use_Package_Clause then
3588 End_Use_Package (U);
996ae0b0
RK
3589 end if;
3590
3591 Next_Use_Clause (U);
3592 end loop;
3593 end End_Use_Clauses;
3594
3595 ---------------------
3596 -- End_Use_Package --
3597 ---------------------
3598
3599 procedure End_Use_Package (N : Node_Id) is
3600 Pack_Name : Node_Id;
3601 Pack : Entity_Id;
3602 Id : Entity_Id;
3603 Elmt : Elmt_Id;
3604
a780db15 3605 function Is_Primitive_Operator_In_Use
2e071734
AC
3606 (Op : Entity_Id;
3607 F : Entity_Id) return Boolean;
fbf5a39b
AC
3608 -- Check whether Op is a primitive operator of a use-visible type
3609
a780db15
AC
3610 ----------------------------------
3611 -- Is_Primitive_Operator_In_Use --
3612 ----------------------------------
fbf5a39b 3613
a780db15 3614 function Is_Primitive_Operator_In_Use
2e071734
AC
3615 (Op : Entity_Id;
3616 F : Entity_Id) return Boolean
fbf5a39b
AC
3617 is
3618 T : constant Entity_Id := Etype (F);
fbf5a39b 3619 begin
a780db15
AC
3620 return (In_Use (T)
3621 or else Present (Current_Use_Clause (Base_Type (T))))
fbf5a39b 3622 and then Scope (T) = Scope (Op);
a780db15 3623 end Is_Primitive_Operator_In_Use;
fbf5a39b
AC
3624
3625 -- Start of processing for End_Use_Package
3626
996ae0b0
RK
3627 begin
3628 Pack_Name := First (Names (N));
996ae0b0 3629 while Present (Pack_Name) loop
996ae0b0 3630
462c31ef
ST
3631 -- Test that Pack_Name actually denotes a package before processing
3632
3633 if Is_Entity_Name (Pack_Name)
3634 and then Ekind (Entity (Pack_Name)) = E_Package
3635 then
3636 Pack := Entity (Pack_Name);
3637
996ae0b0
RK
3638 if In_Open_Scopes (Pack) then
3639 null;
3640
3641 elsif not Redundant_Use (Pack_Name) then
3642 Set_In_Use (Pack, False);
d4810530 3643 Set_Current_Use_Clause (Pack, Empty);
996ae0b0 3644
16ca248a 3645 Id := First_Entity (Pack);
996ae0b0
RK
3646 while Present (Id) loop
3647
fbf5a39b 3648 -- Preserve use-visibility of operators that are primitive
954c111a 3649 -- operators of a type that is use-visible through an active
fbf5a39b 3650 -- use_type clause.
996ae0b0
RK
3651
3652 if Nkind (Id) = N_Defining_Operator_Symbol
3653 and then
a780db15
AC
3654 (Is_Primitive_Operator_In_Use
3655 (Id, First_Formal (Id))
996ae0b0 3656 or else
fbf5a39b
AC
3657 (Present (Next_Formal (First_Formal (Id)))
3658 and then
a780db15 3659 Is_Primitive_Operator_In_Use
fbf5a39b 3660 (Id, Next_Formal (First_Formal (Id)))))
996ae0b0
RK
3661 then
3662 null;
3663
3664 else
3665 Set_Is_Potentially_Use_Visible (Id, False);
3666 end if;
3667
3668 if Is_Private_Type (Id)
3669 and then Present (Full_View (Id))
3670 then
3671 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
3672 end if;
3673
3674 Next_Entity (Id);
3675 end loop;
3676
3677 if Present (Renamed_Object (Pack)) then
3678 Set_In_Use (Renamed_Object (Pack), False);
d4810530 3679 Set_Current_Use_Clause (Renamed_Object (Pack), Empty);
996ae0b0
RK
3680 end if;
3681
3682 if Chars (Pack) = Name_System
3683 and then Scope (Pack) = Standard_Standard
3684 and then Present_System_Aux
3685 then
3686 Id := First_Entity (System_Aux_Id);
996ae0b0
RK
3687 while Present (Id) loop
3688 Set_Is_Potentially_Use_Visible (Id, False);
3689
3690 if Is_Private_Type (Id)
3691 and then Present (Full_View (Id))
3692 then
3693 Set_Is_Potentially_Use_Visible (Full_View (Id), False);
3694 end if;
3695
3696 Next_Entity (Id);
3697 end loop;
3698
3699 Set_In_Use (System_Aux_Id, False);
3700 end if;
3701
3702 else
3703 Set_Redundant_Use (Pack_Name, False);
3704 end if;
996ae0b0
RK
3705 end if;
3706
3707 Next (Pack_Name);
3708 end loop;
3709
3710 if Present (Hidden_By_Use_Clause (N)) then
3711 Elmt := First_Elmt (Hidden_By_Use_Clause (N));
996ae0b0 3712 while Present (Elmt) loop
e69614ad
AC
3713 declare
3714 E : constant Entity_Id := Node (Elmt);
3715
3716 begin
3717 -- Reset either Use_Visibility or Direct_Visibility, depending
3718 -- on how the entity was hidden by the use clause.
3719
3720 if In_Use (Scope (E))
3721 and then Used_As_Generic_Actual (Scope (E))
3722 then
3723 Set_Is_Potentially_Use_Visible (Node (Elmt));
3724 else
3725 Set_Is_Immediately_Visible (Node (Elmt));
3726 end if;
3727
3728 Next_Elmt (Elmt);
3729 end;
996ae0b0
RK
3730 end loop;
3731
3732 Set_Hidden_By_Use_Clause (N, No_Elist);
3733 end if;
3734 end End_Use_Package;
3735
3736 ------------------
3737 -- End_Use_Type --
3738 ------------------
3739
3740 procedure End_Use_Type (N : Node_Id) is
4a214958 3741 Elmt : Elmt_Id;
996ae0b0 3742 Id : Entity_Id;
996ae0b0
RK
3743 T : Entity_Id;
3744
4a214958
AC
3745 -- Start of processing for End_Use_Type
3746
996ae0b0
RK
3747 begin
3748 Id := First (Subtype_Marks (N));
996ae0b0 3749 while Present (Id) loop
fbf5a39b 3750
4a214958 3751 -- A call to Rtsfind may occur while analyzing a use_type clause,
fbf5a39b
AC
3752 -- in which case the type marks are not resolved yet, and there is
3753 -- nothing to remove.
3754
4a214958 3755 if not Is_Entity_Name (Id) or else No (Entity (Id)) then
fbf5a39b
AC
3756 goto Continue;
3757 end if;
3758
996ae0b0
RK
3759 T := Entity (Id);
3760
4a214958 3761 if T = Any_Type or else From_With_Type (T) then
996ae0b0
RK
3762 null;
3763
4a214958 3764 -- Note that the use_type clause may mention a subtype of the type
16ca248a
ES
3765 -- whose primitive operations have been made visible. Here as
3766 -- elsewhere, it is the base type that matters for visibility.
996ae0b0
RK
3767
3768 elsif In_Open_Scopes (Scope (Base_Type (T))) then
3769 null;
3770
3771 elsif not Redundant_Use (Id) then
3772 Set_In_Use (T, False);
3773 Set_In_Use (Base_Type (T), False);
21d27997
RD
3774 Set_Current_Use_Clause (T, Empty);
3775 Set_Current_Use_Clause (Base_Type (T), Empty);
996ae0b0
RK
3776 end if;
3777
fbf5a39b 3778 <<Continue>>
29efbb8c 3779 Next (Id);
996ae0b0 3780 end loop;
29efbb8c
ES
3781
3782 if Is_Empty_Elmt_List (Used_Operations (N)) then
3783 return;
3784
3785 else
3786 Elmt := First_Elmt (Used_Operations (N));
3787 while Present (Elmt) loop
3788 Set_Is_Potentially_Use_Visible (Node (Elmt), False);
3789 Next_Elmt (Elmt);
3790 end loop;
3791 end if;
996ae0b0
RK
3792 end End_Use_Type;
3793
3794 ----------------------
3795 -- Find_Direct_Name --
3796 ----------------------
3797
3798 procedure Find_Direct_Name (N : Node_Id) is
3799 E : Entity_Id;
3800 E2 : Entity_Id;
3801 Msg : Boolean;
3802
3803 Inst : Entity_Id := Empty;
bc41faa2 3804 -- Enclosing instance, if any
996ae0b0
RK
3805
3806 Homonyms : Entity_Id;
3807 -- Saves start of homonym chain
3808
3809 Nvis_Entity : Boolean;
3764bb00
BD
3810 -- Set True to indicate that there is at least one entity on the homonym
3811 -- chain which, while not visible, is visible enough from the user point
3812 -- of view to warrant an error message of "not visible" rather than
3813 -- undefined.
996ae0b0 3814
9bc856dd 3815 Nvis_Is_Private_Subprg : Boolean := False;
0ab80019 3816 -- Ada 2005 (AI-262): Set True to indicate that a form of Beaujolais
9bc856dd
AC
3817 -- effect concerning library subprograms has been detected. Used to
3818 -- generate the precise error message.
3819
996ae0b0
RK
3820 function From_Actual_Package (E : Entity_Id) return Boolean;
3821 -- Returns true if the entity is declared in a package that is
3822 -- an actual for a formal package of the current instance. Such an
3823 -- entity requires special handling because it may be use-visible
3824 -- but hides directly visible entities defined outside the instance.
3825
67ce0d7e
RD
3826 function Is_Actual_Parameter return Boolean;
3827 -- This function checks if the node N is an identifier that is an actual
3828 -- parameter of a procedure call. If so it returns True, otherwise it
3829 -- return False. The reason for this check is that at this stage we do
3830 -- not know what procedure is being called if the procedure might be
3831 -- overloaded, so it is premature to go setting referenced flags or
3832 -- making calls to Generate_Reference. We will wait till Resolve_Actuals
3833 -- for that processing
3834
996ae0b0
RK
3835 function Known_But_Invisible (E : Entity_Id) return Boolean;
3836 -- This function determines whether the entity E (which is not
3837 -- visible) can reasonably be considered to be known to the writer
3838 -- of the reference. This is a heuristic test, used only for the
3839 -- purposes of figuring out whether we prefer to complain that an
3840 -- entity is undefined or invisible (and identify the declaration
3841 -- of the invisible entity in the latter case). The point here is
3842 -- that we don't want to complain that something is invisible and
3843 -- then point to something entirely mysterious to the writer.
3844
3845 procedure Nvis_Messages;
3846 -- Called if there are no visible entries for N, but there is at least
3847 -- one non-directly visible, or hidden declaration. This procedure
3848 -- outputs an appropriate set of error messages.
3849
3850 procedure Undefined (Nvis : Boolean);
3851 -- This function is called if the current node has no corresponding
3852 -- visible entity or entities. The value set in Msg indicates whether
3853 -- an error message was generated (multiple error messages for the
3854 -- same variable are generally suppressed, see body for details).
3855 -- Msg is True if an error message was generated, False if not. This
3856 -- value is used by the caller to determine whether or not to output
3857 -- additional messages where appropriate. The parameter is set False
3858 -- to get the message "X is undefined", and True to get the message
3859 -- "X is not visible".
3860
3861 -------------------------
3862 -- From_Actual_Package --
3863 -------------------------
3864
3865 function From_Actual_Package (E : Entity_Id) return Boolean is
3866 Scop : constant Entity_Id := Scope (E);
3867 Act : Entity_Id;
3868
3869 begin
3870 if not In_Instance then
3871 return False;
3872 else
3873 Inst := Current_Scope;
996ae0b0
RK
3874 while Present (Inst)
3875 and then Ekind (Inst) /= E_Package
3876 and then not Is_Generic_Instance (Inst)
3877 loop
3878 Inst := Scope (Inst);
3879 end loop;
3880
3881 if No (Inst) then
3882 return False;
3883 end if;
3884
3885 Act := First_Entity (Inst);
996ae0b0
RK
3886 while Present (Act) loop
3887 if Ekind (Act) = E_Package then
3888
3889 -- Check for end of actuals list
3890
3891 if Renamed_Object (Act) = Inst then
3892 return False;
3893
3894 elsif Present (Associated_Formal_Package (Act))
3895 and then Renamed_Object (Act) = Scop
3896 then
3897 -- Entity comes from (instance of) formal package
3898
3899 return True;
3900
3901 else
3902 Next_Entity (Act);
3903 end if;
3904
3905 else
3906 Next_Entity (Act);
3907 end if;
3908 end loop;
3909
3910 return False;
3911 end if;
3912 end From_Actual_Package;
3913
67ce0d7e
RD
3914 -------------------------
3915 -- Is_Actual_Parameter --
3916 -------------------------
3917
3918 function Is_Actual_Parameter return Boolean is
3919 begin
3920 return
3921 Nkind (N) = N_Identifier
3922 and then
3923 (Nkind (Parent (N)) = N_Procedure_Call_Statement
3924 or else
3925 (Nkind (Parent (N)) = N_Parameter_Association
3926 and then N = Explicit_Actual_Parameter (Parent (N))
3927 and then Nkind (Parent (Parent (N))) =
3928 N_Procedure_Call_Statement));
3929 end Is_Actual_Parameter;
3930
996ae0b0
RK
3931 -------------------------
3932 -- Known_But_Invisible --
3933 -------------------------
3934
3935 function Known_But_Invisible (E : Entity_Id) return Boolean is
3936 Fname : File_Name_Type;
3937
3938 begin
3939 -- Entities in Standard are always considered to be known
3940
3941 if Sloc (E) <= Standard_Location then
3942 return True;
3943
3944 -- An entity that does not come from source is always considered
3945 -- to be unknown, since it is an artifact of code expansion.
3946
3947 elsif not Comes_From_Source (E) then
3948 return False;
3949
3950 -- In gnat internal mode, we consider all entities known
3951
3952 elsif GNAT_Mode then
3953 return True;
3954 end if;
3955
3956 -- Here we have an entity that is not from package Standard, and
3957 -- which comes from Source. See if it comes from an internal file.
3958
3959 Fname := Unit_File_Name (Get_Source_Unit (E));
3960
3961 -- Case of from internal file
3962
3963 if Is_Internal_File_Name (Fname) then
3964
3965 -- Private part entities in internal files are never considered
3966 -- to be known to the writer of normal application code.
3967
3968 if Is_Hidden (E) then
3969 return False;
3970 end if;
3971
3972 -- Entities from System packages other than System and
3973 -- System.Storage_Elements are not considered to be known.
3974 -- System.Auxxxx files are also considered known to the user.
3975
3976 -- Should refine this at some point to generally distinguish
3977 -- between known and unknown internal files ???
3978
3979 Get_Name_String (Fname);
3980
3981 return
3982 Name_Len < 2
3983 or else
3984 Name_Buffer (1 .. 2) /= "s-"
3985 or else
3986 Name_Buffer (3 .. 8) = "stoele"
3987 or else
3988 Name_Buffer (3 .. 5) = "aux";
3989
3990 -- If not an internal file, then entity is definitely known,
3991 -- even if it is in a private part (the message generated will
3992 -- note that it is in a private part)
3993
3994 else
3995 return True;
3996 end if;
3997 end Known_But_Invisible;
3998
3999 -------------------
4000 -- Nvis_Messages --
4001 -------------------
4002
4003 procedure Nvis_Messages is
9bc856dd
AC
4004 Comp_Unit : Node_Id;
4005 Ent : Entity_Id;
1175f0b6 4006 Found : Boolean := False;
9bc856dd
AC
4007 Hidden : Boolean := False;
4008 Item : Node_Id;
996ae0b0
RK
4009
4010 begin
0ab80019 4011 -- Ada 2005 (AI-262): Generate a precise error concerning the
9bc856dd
AC
4012 -- Beaujolais effect that was previously detected
4013
4014 if Nvis_Is_Private_Subprg then
4015
4016 pragma Assert (Nkind (E2) = N_Defining_Identifier
16ca248a
ES
4017 and then Ekind (E2) = E_Function
4018 and then Scope (E2) = Standard_Standard
4019 and then Has_Private_With (E2));
9bc856dd
AC
4020
4021 -- Find the sloc corresponding to the private with'ed unit
4022
16ca248a 4023 Comp_Unit := Cunit (Current_Sem_Unit);
9bc856dd
AC
4024 Error_Msg_Sloc := No_Location;
4025
16ca248a 4026 Item := First (Context_Items (Comp_Unit));
9bc856dd
AC
4027 while Present (Item) loop
4028 if Nkind (Item) = N_With_Clause
4029 and then Private_Present (Item)
4030 and then Entity (Name (Item)) = E2
4031 then
4032 Error_Msg_Sloc := Sloc (Item);
4033 exit;
4034 end if;
4035
4036 Next (Item);
4037 end loop;
4038
4039 pragma Assert (Error_Msg_Sloc /= No_Location);
4040
0ab80019 4041 Error_Msg_N ("(Ada 2005): hidden by private with clause #", N);
9bc856dd
AC
4042 return;
4043 end if;
4044
996ae0b0
RK
4045 Undefined (Nvis => True);
4046
4047 if Msg then
4048
4049 -- First loop does hidden declarations
4050
4051 Ent := Homonyms;
4052 while Present (Ent) loop
4053 if Is_Potentially_Use_Visible (Ent) then
996ae0b0 4054 if not Hidden then
deef4289
AC
4055 Error_Msg_N -- CODEFIX
4056 ("multiple use clauses cause hiding!", N);
996ae0b0
RK
4057 Hidden := True;
4058 end if;
4059
4060 Error_Msg_Sloc := Sloc (Ent);
4e7a4f6e
AC
4061 Error_Msg_N -- CODEFIX
4062 ("hidden declaration#!", N);
996ae0b0
RK
4063 end if;
4064
4065 Ent := Homonym (Ent);
4066 end loop;
4067
4068 -- If we found hidden declarations, then that's enough, don't
4069 -- bother looking for non-visible declarations as well.
4070
4071 if Hidden then
4072 return;
4073 end if;
4074
4075 -- Second loop does non-directly visible declarations
4076
4077 Ent := Homonyms;
4078 while Present (Ent) loop
4079 if not Is_Potentially_Use_Visible (Ent) then
4080
4081 -- Do not bother the user with unknown entities
4082
4083 if not Known_But_Invisible (Ent) then
4084 goto Continue;
4085 end if;
4086
4087 Error_Msg_Sloc := Sloc (Ent);
4088
4089 -- Output message noting that there is a non-visible
4090 -- declaration, distinguishing the private part case.
4091
4092 if Is_Hidden (Ent) then
4093 Error_Msg_N ("non-visible (private) declaration#!", N);
1175f0b6
AC
4094
4095 -- If the entity is declared in a generic package, it
4096 -- cannot be visible, so there is no point in adding it
4097 -- to the list of candidates if another homograph from a
4098 -- non-generic package has been seen.
4099
4100 elsif Ekind (Scope (Ent)) = E_Generic_Package
4101 and then Found
4102 then
4103 null;
4104
996ae0b0 4105 else
483c78cb
RD
4106 Error_Msg_N -- CODEFIX
4107 ("non-visible declaration#!", N);
fbf5a39b 4108
1175f0b6
AC
4109 if Ekind (Scope (Ent)) /= E_Generic_Package then
4110 Found := True;
4111 end if;
4112
fbf5a39b
AC
4113 if Is_Compilation_Unit (Ent)
4114 and then
4115 Nkind (Parent (Parent (N))) = N_Use_Package_Clause
4116 then
16ca248a 4117 Error_Msg_Qual_Level := 99;
ed2233dc
AC
4118 Error_Msg_NE -- CODEFIX
4119 ("\\missing `WITH &;`", N, Ent);
16ca248a 4120 Error_Msg_Qual_Level := 0;
fbf5a39b 4121 end if;
e1b871e9
AC
4122
4123 if Ekind (Ent) = E_Discriminant
4124 and then Present (Corresponding_Discriminant (Ent))
4125 and then Scope (Corresponding_Discriminant (Ent)) =
4126 Etype (Scope (Ent))
4127 then
4128 Error_Msg_N
4129 ("inherited discriminant not allowed here" &
4130 " (RM 3.8 (12), 3.8.1 (6))!", N);
4131 end if;
996ae0b0 4132 end if;
07fc65c4
GB
4133
4134 -- Set entity and its containing package as referenced. We
4135 -- can't be sure of this, but this seems a better choice
4136 -- to avoid unused entity messages.
4137
4138 if Comes_From_Source (Ent) then
4139 Set_Referenced (Ent);
4140 Set_Referenced (Cunit_Entity (Get_Source_Unit (Ent)));
4141 end if;
996ae0b0
RK
4142 end if;
4143
4144 <<Continue>>
4145 Ent := Homonym (Ent);
4146 end loop;
996ae0b0
RK
4147 end if;
4148 end Nvis_Messages;
4149
4150 ---------------
4151 -- Undefined --
4152 ---------------
4153
4154 procedure Undefined (Nvis : Boolean) is
4155 Emsg : Error_Msg_Id;
4156
4157 begin
fbf5a39b
AC
4158 -- We should never find an undefined internal name. If we do, then
4159 -- see if we have previous errors. If so, ignore on the grounds that
4160 -- it is probably a cascaded message (e.g. a block label from a badly
4161 -- formed block). If no previous errors, then we have a real internal
4162 -- error of some kind so raise an exception.
4163
4164 if Is_Internal_Name (Chars (N)) then
4165 if Total_Errors_Detected /= 0 then
4166 return;
4167 else
4168 raise Program_Error;
4169 end if;
4170 end if;
4171
996ae0b0
RK
4172 -- A very specialized error check, if the undefined variable is
4173 -- a case tag, and the case type is an enumeration type, check
4174 -- for a possible misspelling, and if so, modify the identifier
4175
4176 -- Named aggregate should also be handled similarly ???
4177
4178 if Nkind (N) = N_Identifier
4179 and then Nkind (Parent (N)) = N_Case_Statement_Alternative
4180 then
996ae0b0 4181 declare
996ae0b0
RK
4182 Case_Stm : constant Node_Id := Parent (Parent (N));
4183 Case_Typ : constant Entity_Id := Etype (Expression (Case_Stm));
4184
4185 Lit : Node_Id;
4186
4187 begin
4188 if Is_Enumeration_Type (Case_Typ)
21d27997 4189 and then not Is_Standard_Character_Type (Case_Typ)
996ae0b0
RK
4190 then
4191 Lit := First_Literal (Case_Typ);
4192 Get_Name_String (Chars (Lit));
4193
4194 if Chars (Lit) /= Chars (N)
294ccb21 4195 and then Is_Bad_Spelling_Of (Chars (N), Chars (Lit)) then
996ae0b0 4196 Error_Msg_Node_2 := Lit;
ed2233dc 4197 Error_Msg_N -- CODEFIX
996ae0b0
RK
4198 ("& is undefined, assume misspelling of &", N);
4199 Rewrite (N, New_Occurrence_Of (Lit, Sloc (N)));
4200 return;
4201 end if;
4202
4203 Lit := Next_Literal (Lit);
4204 end if;
4205 end;
4206 end if;
4207
4208 -- Normal processing
4209
4210 Set_Entity (N, Any_Id);
4211 Set_Etype (N, Any_Type);
4212
4213 -- We use the table Urefs to keep track of entities for which we
4214 -- have issued errors for undefined references. Multiple errors
4215 -- for a single name are normally suppressed, however we modify
4216 -- the error message to alert the programmer to this effect.
4217
4218 for J in Urefs.First .. Urefs.Last loop
4219 if Chars (N) = Chars (Urefs.Table (J).Node) then
4220 if Urefs.Table (J).Err /= No_Error_Msg
4221 and then Sloc (N) /= Urefs.Table (J).Loc
4222 then
4223 Error_Msg_Node_1 := Urefs.Table (J).Node;
4224
4225 if Urefs.Table (J).Nvis then
4226 Change_Error_Text (Urefs.Table (J).Err,
4227 "& is not visible (more references follow)");
4228 else
4229 Change_Error_Text (Urefs.Table (J).Err,
4230 "& is undefined (more references follow)");
4231 end if;
4232
4233 Urefs.Table (J).Err := No_Error_Msg;
4234 end if;
4235
4236 -- Although we will set Msg False, and thus suppress the
4237 -- message, we also set Error_Posted True, to avoid any
4238 -- cascaded messages resulting from the undefined reference.
4239
4240 Msg := False;
4241 Set_Error_Posted (N, True);
4242 return;
4243 end if;
4244 end loop;
4245
4246 -- If entry not found, this is first undefined occurrence
4247
4248 if Nvis then
4249 Error_Msg_N ("& is not visible!", N);
4250 Emsg := Get_Msg_Id;
4251
4252 else
4253 Error_Msg_N ("& is undefined!", N);
4254 Emsg := Get_Msg_Id;
4255
4256 -- A very bizarre special check, if the undefined identifier
4257 -- is put or put_line, then add a special error message (since
4258 -- this is a very common error for beginners to make).
4259
4260 if Chars (N) = Name_Put or else Chars (N) = Name_Put_Line then
ed2233dc 4261 Error_Msg_N -- CODEFIX
16ca248a
ES
4262 ("\\possible missing `WITH Ada.Text_'I'O; " &
4263 "USE Ada.Text_'I'O`!", N);
4264
4265 -- Another special check if N is the prefix of a selected
4266 -- component which is a known unit, add message complaining
fbe627af 4267 -- about missing with for this unit.
16ca248a
ES
4268
4269 elsif Nkind (Parent (N)) = N_Selected_Component
4270 and then N = Prefix (Parent (N))
4271 and then Is_Known_Unit (Parent (N))
4272 then
4273 Error_Msg_Node_2 := Selector_Name (Parent (N));
ed2233dc
AC
4274 Error_Msg_N -- CODEFIX
4275 ("\\missing `WITH &.&;`", Prefix (Parent (N)));
996ae0b0
RK
4276 end if;
4277
4278 -- Now check for possible misspellings
4279
996ae0b0
RK
4280 declare
4281 E : Entity_Id;
4282 Ematch : Entity_Id := Empty;
4283
4284 Last_Name_Id : constant Name_Id :=
4285 Name_Id (Nat (First_Name_Id) +
4286 Name_Entries_Count - 1);
4287
996ae0b0 4288 begin
294ccb21
RD
4289 for Nam in First_Name_Id .. Last_Name_Id loop
4290 E := Get_Name_Entity_Id (Nam);
996ae0b0
RK
4291
4292 if Present (E)
4293 and then (Is_Immediately_Visible (E)
4294 or else
4295 Is_Potentially_Use_Visible (E))
4296 then
294ccb21 4297 if Is_Bad_Spelling_Of (Chars (N), Nam) then
996ae0b0
RK
4298 Ematch := E;
4299 exit;
4300 end if;
4301 end if;
4302 end loop;
4303
4304 if Present (Ematch) then
deef4289
AC
4305 Error_Msg_NE -- CODEFIX
4306 ("\possible misspelling of&", N, Ematch);
996ae0b0
RK
4307 end if;
4308 end;
4309 end if;
4310
16ca248a
ES
4311 -- Make entry in undefined references table unless the full errors
4312 -- switch is set, in which case by refraining from generating the
4313 -- table entry, we guarantee that we get an error message for every
4314 -- undefined reference.
996ae0b0
RK
4315
4316 if not All_Errors_Mode then
11560bcc
TQ
4317 Urefs.Append (
4318 (Node => N,
4319 Err => Emsg,
4320 Nvis => Nvis,
4321 Loc => Sloc (N)));
996ae0b0
RK
4322 end if;
4323
4324 Msg := True;
4325 end Undefined;
4326
4327 -- Start of processing for Find_Direct_Name
4328
4329 begin
4330 -- If the entity pointer is already set, this is an internal node, or
4331 -- a node that is analyzed more than once, after a tree modification.
4332 -- In such a case there is no resolution to perform, just set the type.
4333
4334 if Present (Entity (N)) then
4335 if Is_Type (Entity (N)) then
4336 Set_Etype (N, Entity (N));
4337
4338 else
4339 declare
4340 Entyp : constant Entity_Id := Etype (Entity (N));
4341
4342 begin
4343 -- One special case here. If the Etype field is already set,
4344 -- and references the packed array type corresponding to the
4345 -- etype of the referenced entity, then leave it alone. This
4346 -- happens for trees generated from Exp_Pakd, where expressions
4347 -- can be deliberately "mis-typed" to the packed array type.
4348
4349 if Is_Array_Type (Entyp)
4350 and then Is_Packed (Entyp)
4351 and then Present (Etype (N))
4352 and then Etype (N) = Packed_Array_Type (Entyp)
4353 then
4354 null;
4355
4356 -- If not that special case, then just reset the Etype
4357
4358 else
4359 Set_Etype (N, Etype (Entity (N)));
4360 end if;
4361 end;
4362 end if;
4363
4364 return;
4365 end if;
4366
4367 -- Here if Entity pointer was not set, we need full visibility analysis
4368 -- First we generate debugging output if the debug E flag is set.
4369
4370 if Debug_Flag_E then
4371 Write_Str ("Looking for ");
4372 Write_Name (Chars (N));
4373 Write_Eol;
4374 end if;
4375
4376 Homonyms := Current_Entity (N);
4377 Nvis_Entity := False;
4378
4379 E := Homonyms;
4380 while Present (E) loop
4381
ff81221b
ES
4382 -- If entity is immediately visible or potentially use visible, then
4383 -- process the entity and we are done.
996ae0b0
RK
4384
4385 if Is_Immediately_Visible (E) then
4386 goto Immediately_Visible_Entity;
4387
4388 elsif Is_Potentially_Use_Visible (E) then
4389 goto Potentially_Use_Visible_Entity;
4390
4391 -- Note if a known but invisible entity encountered
4392
4393 elsif Known_But_Invisible (E) then
4394 Nvis_Entity := True;
4395 end if;
4396
4397 -- Move to next entity in chain and continue search
4398
4399 E := Homonym (E);
4400 end loop;
4401
4402 -- If no entries on homonym chain that were potentially visible,
4403 -- and no entities reasonably considered as non-visible, then
4404 -- we have a plain undefined reference, with no additional
4405 -- explanation required!
4406
4407 if not Nvis_Entity then
4408 Undefined (Nvis => False);
996ae0b0
RK
4409
4410 -- Otherwise there is at least one entry on the homonym chain that
4411 -- is reasonably considered as being known and non-visible.
4412
4413 else
4414 Nvis_Messages;
996ae0b0
RK
4415 end if;
4416
fbf5a39b
AC
4417 return;
4418
996ae0b0
RK
4419 -- Processing for a potentially use visible entry found. We must search
4420 -- the rest of the homonym chain for two reasons. First, if there is a
4421 -- directly visible entry, then none of the potentially use-visible
4422 -- entities are directly visible (RM 8.4(10)). Second, we need to check
4423 -- for the case of multiple potentially use-visible entries hiding one
4424 -- another and as a result being non-directly visible (RM 8.4(11)).
4425
4426 <<Potentially_Use_Visible_Entity>> declare
4427 Only_One_Visible : Boolean := True;
4428 All_Overloadable : Boolean := Is_Overloadable (E);
4429
4430 begin
4431 E2 := Homonym (E);
996ae0b0
RK
4432 while Present (E2) loop
4433 if Is_Immediately_Visible (E2) then
4434
4435 -- If the use-visible entity comes from the actual for a
4436 -- formal package, it hides a directly visible entity from
4437 -- outside the instance.
4438
4439 if From_Actual_Package (E)
4440 and then Scope_Depth (E2) < Scope_Depth (Inst)
4441 then
4442 goto Found;
4443 else
4444 E := E2;
4445 goto Immediately_Visible_Entity;
4446 end if;
4447
4448 elsif Is_Potentially_Use_Visible (E2) then
4449 Only_One_Visible := False;
4450 All_Overloadable := All_Overloadable and Is_Overloadable (E2);
9bc856dd 4451
30783513 4452 -- Ada 2005 (AI-262): Protect against a form of Beaujolais effect
f3d57416 4453 -- that can occur in private_with clauses. Example:
9bc856dd
AC
4454
4455 -- with A;
4456 -- private with B; package A is
4457 -- package C is function B return Integer;
4458 -- use A; end A;
4459 -- V1 : Integer := B;
4460 -- private function B return Integer;
4461 -- V2 : Integer := B;
4462 -- end C;
4463
bc41faa2 4464 -- V1 resolves to A.B, but V2 resolves to library unit B
9bc856dd
AC
4465
4466 elsif Ekind (E2) = E_Function
4467 and then Scope (E2) = Standard_Standard
4468 and then Has_Private_With (E2)
4469 then
4470 Only_One_Visible := False;
4471 All_Overloadable := False;
4472 Nvis_Is_Private_Subprg := True;
4473 exit;
996ae0b0
RK
4474 end if;
4475
4476 E2 := Homonym (E2);
4477 end loop;
4478
4479 -- On falling through this loop, we have checked that there are no
4480 -- immediately visible entities. Only_One_Visible is set if exactly
4481 -- one potentially use visible entity exists. All_Overloadable is
4482 -- set if all the potentially use visible entities are overloadable.
4483 -- The condition for legality is that either there is one potentially
4484 -- use visible entity, or if there is more than one, then all of them
4485 -- are overloadable.
4486
4487 if Only_One_Visible or All_Overloadable then
4488 goto Found;
4489
4490 -- If there is more than one potentially use-visible entity and at
4491 -- least one of them non-overloadable, we have an error (RM 8.4(11).
4492 -- Note that E points to the first such entity on the homonym list.
4493 -- Special case: if one of the entities is declared in an actual
4494 -- package, it was visible in the generic, and takes precedence over
fbf5a39b
AC
4495 -- other entities that are potentially use-visible. Same if it is
4496 -- declared in a local instantiation of the current instance.
996ae0b0
RK
4497
4498 else
4499 if In_Instance then
fbf5a39b 4500
bc41faa2 4501 -- Find current instance
fbf5a39b 4502
16ca248a 4503 Inst := Current_Scope;
fbf5a39b
AC
4504 while Present (Inst)
4505 and then Inst /= Standard_Standard
4506 loop
4507 if Is_Generic_Instance (Inst) then
4508 exit;
4509 end if;
4510
4511 Inst := Scope (Inst);
4512 end loop;
4513
996ae0b0 4514 E2 := E;
996ae0b0 4515 while Present (E2) loop
fbf5a39b
AC
4516 if From_Actual_Package (E2)
4517 or else
4518 (Is_Generic_Instance (Scope (E2))
4519 and then Scope_Depth (Scope (E2)) > Scope_Depth (Inst))
4520 then
996ae0b0
RK
4521 E := E2;
4522 goto Found;
4523 end if;
4524
4525 E2 := Homonym (E2);
4526 end loop;
4527
4528 Nvis_Messages;
4529 return;
4530
5eb10f25
ES
4531 elsif
4532 Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
4533 then
4de287c4
ES
4534 -- A use-clause in the body of a system file creates conflict
4535 -- with some entity in a user scope, while rtsfind is active.
4536 -- Keep only the entity coming from another predefined unit.
5eb10f25
ES
4537
4538 E2 := E;
4539 while Present (E2) loop
4540 if Is_Predefined_File_Name
4541 (Unit_File_Name (Get_Source_Unit (Sloc (E2))))
4542 then
4543 E := E2;
4544 goto Found;
4545 end if;
4546
4547 E2 := Homonym (E2);
4548 end loop;
4549
4de287c4 4550 -- Entity must exist because predefined unit is correct
5eb10f25
ES
4551
4552 raise Program_Error;
4553
996ae0b0
RK
4554 else
4555 Nvis_Messages;
4556 return;
4557 end if;
4558 end if;
4559 end;
4560
4561 -- Come here with E set to the first immediately visible entity on
4562 -- the homonym chain. This is the one we want unless there is another
ff81221b
ES
4563 -- immediately visible entity further on in the chain for an inner
4564 -- scope (RM 8.3(8)).
996ae0b0
RK
4565
4566 <<Immediately_Visible_Entity>> declare
4567 Level : Int;
4568 Scop : Entity_Id;
4569
4570 begin
ff81221b 4571 -- Find scope level of initial entity. When compiling through
996ae0b0
RK
4572 -- Rtsfind, the previous context is not completely invisible, and
4573 -- an outer entity may appear on the chain, whose scope is below
4574 -- the entry for Standard that delimits the current scope stack.
4575 -- Indicate that the level for this spurious entry is outside of
4576 -- the current scope stack.
4577
4578 Level := Scope_Stack.Last;
4579 loop
4580 Scop := Scope_Stack.Table (Level).Entity;
4581 exit when Scop = Scope (E);
4582 Level := Level - 1;
4583 exit when Scop = Standard_Standard;
4584 end loop;
4585
4586 -- Now search remainder of homonym chain for more inner entry
4587 -- If the entity is Standard itself, it has no scope, and we
4588 -- compare it with the stack entry directly.
4589
4590 E2 := Homonym (E);
4591 while Present (E2) loop
4592 if Is_Immediately_Visible (E2) then
4de287c4
ES
4593
4594 -- If a generic package contains a local declaration that
4595 -- has the same name as the generic, there may be a visibility
4596 -- conflict in an instance, where the local declaration must
4597 -- also hide the name of the corresponding package renaming.
4598 -- We check explicitly for a package declared by a renaming,
4599 -- whose renamed entity is an instance that is on the scope
4600 -- stack, and that contains a homonym in the same scope. Once
4601 -- we have found it, we know that the package renaming is not
4602 -- immediately visible, and that the identifier denotes the
4603 -- other entity (and its homonyms if overloaded).
4604
4605 if Scope (E) = Scope (E2)
4606 and then Ekind (E) = E_Package
4607 and then Present (Renamed_Object (E))
4608 and then Is_Generic_Instance (Renamed_Object (E))
4609 and then In_Open_Scopes (Renamed_Object (E))
4610 and then Comes_From_Source (N)
4611 then
4612 Set_Is_Immediately_Visible (E, False);
4613 E := E2;
4614
4615 else
4616 for J in Level + 1 .. Scope_Stack.Last loop
4617 if Scope_Stack.Table (J).Entity = Scope (E2)
4618 or else Scope_Stack.Table (J).Entity = E2
4619 then
4620 Level := J;
4621 E := E2;
4622 exit;
4623 end if;
4624 end loop;
4625 end if;
996ae0b0
RK
4626 end if;
4627
4628 E2 := Homonym (E2);
4629 end loop;
4630
4631 -- At the end of that loop, E is the innermost immediately
4632 -- visible entity, so we are all set.
4633 end;
4634
4635 -- Come here with entity found, and stored in E
4636
4637 <<Found>> begin
4638
30196a76
RD
4639 -- Check violation of No_Wide_Characters restriction
4640
4641 Check_Wide_Character_Restriction (E, N);
4642
294ccb21
RD
4643 -- When distribution features are available (Get_PCS_Name /=
4644 -- Name_No_DSA), a remote access-to-subprogram type is converted
4645 -- into a record type holding whatever information is needed to
f3d57416 4646 -- perform a remote call on an RCI subprogram. In that case we
294ccb21
RD
4647 -- rewrite any occurrence of the RAS type into the equivalent record
4648 -- type here. 'Access attribute references and RAS dereferences are
4649 -- then implemented using specific TSSs. However when distribution is
4650 -- not available (case of Get_PCS_Name = Name_No_DSA), we bypass the
4651 -- generation of these TSSs, and we must keep the RAS type in its
4652 -- original access-to-subprogram form (since all calls through a
4653 -- value of such type will be local anyway in the absence of a PCS).
4654
996ae0b0
RK
4655 if Comes_From_Source (N)
4656 and then Is_Remote_Access_To_Subprogram_Type (E)
4657 and then Expander_Active
a77842bd 4658 and then Get_PCS_Name /= Name_No_DSA
996ae0b0
RK
4659 then
4660 Rewrite (N,
4661 New_Occurrence_Of (Equivalent_Type (E), Sloc (N)));
4662 return;
4663 end if;
4664
6989bc1f
AC
4665 -- Set the entity. Note that the reason we call Set_Entity for the
4666 -- overloadable case, as opposed to Set_Entity_With_Style_Check is
4667 -- that in the overloaded case, the initial call can set the wrong
4668 -- homonym. The call that sets the right homonym is in Sem_Res and
4669 -- that call does use Set_Entity_With_Style_Check, so we don't miss
4670 -- a style check.
4671
4672 if Is_Overloadable (E) then
4673 Set_Entity (N, E);
4674 else
4675 Set_Entity_With_Style_Check (N, E);
4676 end if;
996ae0b0
RK
4677
4678 if Is_Type (E) then
4679 Set_Etype (N, E);
4680 else
4681 Set_Etype (N, Get_Full_View (Etype (E)));
4682 end if;
4683
4684 if Debug_Flag_E then
4685 Write_Str (" found ");
4686 Write_Entity_Info (E, " ");
4687 end if;
4688
4689 -- If the Ekind of the entity is Void, it means that all homonyms
4690 -- are hidden from all visibility (RM 8.3(5,14-20)). However, this
4691 -- test is skipped if the current scope is a record and the name is
4692 -- a pragma argument expression (case of Atomic and Volatile pragmas
4693 -- and possibly other similar pragmas added later, which are allowed
4694 -- to reference components in the current record).
4695
4696 if Ekind (E) = E_Void
4697 and then
4698 (not Is_Record_Type (Current_Scope)
4699 or else Nkind (Parent (N)) /= N_Pragma_Argument_Association)
4700 then
4701 Premature_Usage (N);
4702
16ca248a
ES
4703 -- If the entity is overloadable, collect all interpretations of the
4704 -- name for subsequent overload resolution. We optimize a bit here to
4705 -- do this only if we have an overloadable entity that is not on its
4706 -- own on the homonym chain.
996ae0b0
RK
4707
4708 elsif Is_Overloadable (E)
4709 and then (Present (Homonym (E)) or else Current_Entity (N) /= E)
4710 then
4711 Collect_Interps (N);
4712
bc41faa2 4713 -- If no homonyms were visible, the entity is unambiguous
996ae0b0
RK
4714
4715 if not Is_Overloaded (N) then
67ce0d7e
RD
4716 if not Is_Actual_Parameter then
4717 Generate_Reference (E, N);
4718 end if;
996ae0b0
RK
4719 end if;
4720
4721 -- Case of non-overloadable entity, set the entity providing that
4722 -- we do not have the case of a discriminant reference within a
4723 -- default expression. Such references are replaced with the
4724 -- corresponding discriminal, which is the formal corresponding to
4725 -- to the discriminant in the initialization procedure.
4726
996ae0b0 4727 else
294ccb21
RD
4728 -- Entity is unambiguous, indicate that it is referenced here
4729
4730 -- For a renaming of an object, always generate simple reference,
4731 -- we don't try to keep track of assignments in this case.
4732
4733 if Is_Object (E) and then Present (Renamed_Object (E)) then
4734 Generate_Reference (E, N);
996ae0b0 4735
21d27997
RD
4736 -- If the renamed entity is a private protected component,
4737 -- reference the original component as well. This needs to be
4738 -- done because the private renamings are installed before any
f3d0f304 4739 -- analysis has occurred. Reference to a private component will
21d27997
RD
4740 -- resolve to the renaming and the original component will be
4741 -- left unreferenced, hence the following.
4742
4743 if Is_Prival (E) then
4744 Generate_Reference (Prival_Link (E), N);
4745 end if;
4746
294ccb21
RD
4747 -- One odd case is that we do not want to set the Referenced flag
4748 -- if the entity is a label, and the identifier is the label in
4749 -- the source, since this is not a reference from the point of
4750 -- view of the user.
4751
4752 elsif Nkind (Parent (N)) = N_Label then
996ae0b0
RK
4753 declare
4754 R : constant Boolean := Referenced (E);
294ccb21 4755
996ae0b0 4756 begin
294ccb21
RD
4757 -- Generate reference unless this is an actual parameter
4758 -- (see comment below)
4759
4760 if Is_Actual_Parameter then
67ce0d7e
RD
4761 Generate_Reference (E, N);
4762 Set_Referenced (E, R);
4763 end if;
996ae0b0
RK
4764 end;
4765
11560bcc
TQ
4766 -- Normal case, not a label: generate reference
4767
61c161b2
AC
4768 -- ??? It is too early to generate a reference here even if the
4769 -- entity is unambiguous, because the tree is not sufficiently
4770 -- typed at this point for Generate_Reference to determine
4771 -- whether this reference modifies the denoted object (because
4772 -- implicit dereferences cannot be identified prior to full type
4773 -- resolution).
4774
21d27997
RD
4775 -- The Is_Actual_Parameter routine takes care of one of these
4776 -- cases but there are others probably ???
61c161b2 4777
84df40f7 4778 -- If the entity is the LHS of an assignment, and is a variable
61c161b2 4779 -- (rather than a package prefix), we can mark it as a
84df40f7 4780 -- modification right away, to avoid duplicate references.
fbf5a39b 4781
996ae0b0 4782 else
67ce0d7e 4783 if not Is_Actual_Parameter then
84df40f7
AC
4784 if Is_LHS (N)
4785 and then Ekind (E) /= E_Package
4786 and then Ekind (E) /= E_Generic_Package
4787 then
4788 Generate_Reference (E, N, 'm');
4789 else
4790 Generate_Reference (E, N);
4791 end if;
67ce0d7e
RD
4792 end if;
4793
fbe627af 4794 Check_Nested_Access (E);
996ae0b0
RK
4795 end if;
4796
4c484f40 4797 Set_Entity_Or_Discriminal (N, E);
996ae0b0
RK
4798 end if;
4799 end;
4800 end Find_Direct_Name;
4801
4802 ------------------------
4803 -- Find_Expanded_Name --
4804 ------------------------
4805
4806 -- This routine searches the homonym chain of the entity until it finds
4807 -- an entity declared in the scope denoted by the prefix. If the entity
4808 -- is private, it may nevertheless be immediately visible, if we are in
4809 -- the scope of its declaration.
4810
4811 procedure Find_Expanded_Name (N : Node_Id) is
07fc65c4
GB
4812 Selector : constant Node_Id := Selector_Name (N);
4813 Candidate : Entity_Id := Empty;
996ae0b0
RK
4814 P_Name : Entity_Id;
4815 O_Name : Entity_Id;
4816 Id : Entity_Id;
4817
4818 begin
4819 P_Name := Entity (Prefix (N));
4820 O_Name := P_Name;
4821
ff81221b
ES
4822 -- If the prefix is a renamed package, look for the entity in the
4823 -- original package.
996ae0b0
RK
4824
4825 if Ekind (P_Name) = E_Package
4826 and then Present (Renamed_Object (P_Name))
4827 then
4828 P_Name := Renamed_Object (P_Name);
4829
4830 -- Rewrite node with entity field pointing to renamed object
4831
4832 Rewrite (Prefix (N), New_Copy (Prefix (N)));
4833 Set_Entity (Prefix (N), P_Name);
4834
4835 -- If the prefix is an object of a concurrent type, look for
4836 -- the entity in the associated task or protected type.
4837
4838 elsif Is_Concurrent_Type (Etype (P_Name)) then
4839 P_Name := Etype (P_Name);
4840 end if;
4841
4842 Id := Current_Entity (Selector);
4843
923fa078
RD
4844 declare
4845 Is_New_Candidate : Boolean;
996ae0b0 4846
923fa078
RD
4847 begin
4848 while Present (Id) loop
4849 if Scope (Id) = P_Name then
4850 Candidate := Id;
4851 Is_New_Candidate := True;
4852
4853 -- Ada 2005 (AI-217): Handle shadow entities associated with types
4854 -- declared in limited-withed nested packages. We don't need to
4855 -- handle E_Incomplete_Subtype entities because the entities in
4856 -- the limited view are always E_Incomplete_Type entities (see
4857 -- Build_Limited_Views). Regarding the expression used to evaluate
4858 -- the scope, it is important to note that the limited view also
4859 -- has shadow entities associated nested packages. For this reason
4860 -- the correct scope of the entity is the scope of the real entity
11560bcc
TQ
4861 -- The non-limited view may itself be incomplete, in which case
4862 -- get the full view if available.
923fa078
RD
4863
4864 elsif From_With_Type (Id)
4865 and then Is_Type (Id)
4866 and then Ekind (Id) = E_Incomplete_Type
4867 and then Present (Non_Limited_View (Id))
4868 and then Scope (Non_Limited_View (Id)) = P_Name
4869 then
11560bcc 4870 Candidate := Get_Full_View (Non_Limited_View (Id));
923fa078 4871 Is_New_Candidate := True;
996ae0b0
RK
4872
4873 else
923fa078 4874 Is_New_Candidate := False;
996ae0b0 4875 end if;
996ae0b0 4876
923fa078
RD
4877 if Is_New_Candidate then
4878 if Is_Child_Unit (Id) then
4879 exit when Is_Visible_Child_Unit (Id)
4880 or else Is_Immediately_Visible (Id);
4881
4882 else
4883 exit when not Is_Hidden (Id)
4884 or else Is_Immediately_Visible (Id);
4885 end if;
4886 end if;
4887
4888 Id := Homonym (Id);
4889 end loop;
4890 end;
996ae0b0
RK
4891
4892 if No (Id)
4893 and then (Ekind (P_Name) = E_Procedure
4894 or else
4895 Ekind (P_Name) = E_Function)
4896 and then Is_Generic_Instance (P_Name)
4897 then
4898 -- Expanded name denotes entity in (instance of) generic subprogram.
4899 -- The entity may be in the subprogram instance, or may denote one of
4900 -- the formals, which is declared in the enclosing wrapper package.
4901
4902 P_Name := Scope (P_Name);
996ae0b0 4903
bc41faa2 4904 Id := Current_Entity (Selector);
996ae0b0 4905 while Present (Id) loop
bc41faa2 4906 exit when Scope (Id) = P_Name;
996ae0b0
RK
4907 Id := Homonym (Id);
4908 end loop;
4909 end if;
4910
bc41faa2 4911 if No (Id) or else Chars (Id) /= Chars (Selector) then
996ae0b0
RK
4912 Set_Etype (N, Any_Type);
4913
ff81221b
ES
4914 -- If we are looking for an entity defined in System, try to find it
4915 -- in the child package that may have been provided as an extension
4916 -- to System. The Extend_System pragma will have supplied the name of
4917 -- the extension, which may have to be loaded.
996ae0b0
RK
4918
4919 if Chars (P_Name) = Name_System
4920 and then Scope (P_Name) = Standard_Standard
fbf5a39b 4921 and then Present (System_Extend_Unit)
996ae0b0
RK
4922 and then Present_System_Aux (N)
4923 then
4924 Set_Entity (Prefix (N), System_Aux_Id);
4925 Find_Expanded_Name (N);
4926 return;
4927
fbf5a39b
AC
4928 elsif Nkind (Selector) = N_Operator_Symbol
4929 and then Has_Implicit_Operator (N)
996ae0b0
RK
4930 then
4931 -- There is an implicit instance of the predefined operator in
4932 -- the given scope. The operator entity is defined in Standard.
4933 -- Has_Implicit_Operator makes the node into an Expanded_Name.
4934
4935 return;
4936
4937 elsif Nkind (Selector) = N_Character_Literal
4938 and then Has_Implicit_Character_Literal (N)
4939 then
4940 -- If there is no literal defined in the scope denoted by the
4941 -- prefix, the literal may belong to (a type derived from)
4942 -- Standard_Character, for which we have no explicit literals.
4943
4944 return;
4945
4946 else
ff81221b
ES
4947 -- If the prefix is a single concurrent object, use its name in
4948 -- the error message, rather than that of the anonymous type.
996ae0b0
RK
4949
4950 if Is_Concurrent_Type (P_Name)
4951 and then Is_Internal_Name (Chars (P_Name))
4952 then
4953 Error_Msg_Node_2 := Entity (Prefix (N));
4954 else
4955 Error_Msg_Node_2 := P_Name;
4956 end if;
4957
4958 if P_Name = System_Aux_Id then
4959 P_Name := Scope (P_Name);
4960 Set_Entity (Prefix (N), P_Name);
4961 end if;
4962
4963 if Present (Candidate) then
4964
16ca248a
ES
4965 -- If we know that the unit is a child unit we can give a more
4966 -- accurate error message.
4967
996ae0b0 4968 if Is_Child_Unit (Candidate) then
edd63e9b 4969
16ca248a
ES
4970 -- If the candidate is a private child unit and we are in
4971 -- the visible part of a public unit, specialize the error
4972 -- message. There might be a private with_clause for it,
4973 -- but it is not currently active.
edd63e9b
ES
4974
4975 if Is_Private_Descendant (Candidate)
4976 and then Ekind (Current_Scope) = E_Package
4977 and then not In_Private_Part (Current_Scope)
4978 and then not Is_Private_Descendant (Current_Scope)
4979 then
4980 Error_Msg_N ("private child unit& is not visible here",
16ca248a
ES
4981 Selector);
4982
4983 -- Normal case where we have a missing with for a child unit
4984
edd63e9b 4985 else
16ca248a 4986 Error_Msg_Qual_Level := 99;
ed2233dc
AC
4987 Error_Msg_NE -- CODEFIX
4988 ("missing `WITH &;`", Selector, Candidate);
16ca248a 4989 Error_Msg_Qual_Level := 0;
edd63e9b 4990 end if;
16ca248a
ES
4991
4992 -- Here we don't know that this is a child unit
4993
996ae0b0
RK
4994 else
4995 Error_Msg_NE ("& is not a visible entity of&", N, Selector);
4996 end if;
4997
4998 else
4999 -- Within the instantiation of a child unit, the prefix may
16ca248a
ES
5000 -- denote the parent instance, but the selector has the name
5001 -- of the original child. Find whether we are within the
5002 -- corresponding instance, and get the proper entity, which
996ae0b0
RK
5003 -- can only be an enclosing scope.
5004
5005 if O_Name /= P_Name
5006 and then In_Open_Scopes (P_Name)
5007 and then Is_Generic_Instance (P_Name)
5008 then
5009 declare
5010 S : Entity_Id := Current_Scope;
5011 P : Entity_Id;
5012
5013 begin
5014 for J in reverse 0 .. Scope_Stack.Last loop
5015 S := Scope_Stack.Table (J).Entity;
5016
5017 exit when S = Standard_Standard;
5018
bce79204
AC
5019 if Ekind_In (S, E_Function,
5020 E_Package,
5021 E_Procedure)
996ae0b0
RK
5022 then
5023 P := Generic_Parent (Specification
5024 (Unit_Declaration_Node (S)));
5025
5026 if Present (P)
5027 and then Chars (Scope (P)) = Chars (O_Name)
5028 and then Chars (P) = Chars (Selector)
5029 then
5030 Id := S;
a77842bd 5031 goto Found;
996ae0b0
RK
5032 end if;
5033 end if;
5034
5035 end loop;
5036 end;
5037 end if;
5038
16ca248a
ES
5039 -- If this is a selection from Ada, System or Interfaces, then
5040 -- we assume a missing with for the corresponding package.
5041
5042 if Is_Known_Unit (N) then
fbe627af
RD
5043 if not Error_Posted (N) then
5044 Error_Msg_Node_2 := Selector;
ed2233dc
AC
5045 Error_Msg_N -- CODEFIX
5046 ("missing `WITH &.&;`", Prefix (N));
fbe627af 5047 end if;
996ae0b0 5048
16ca248a
ES
5049 -- If this is a selection from a dummy package, then suppress
5050 -- the error message, of course the entity is missing if the
5051 -- package is missing!
996ae0b0
RK
5052
5053 elsif Sloc (Error_Msg_Node_2) = No_Location then
5054 null;
5055
5056 -- Here we have the case of an undefined component
5057
5058 else
d6a24cdb
AC
5059
5060 -- The prefix may hide a homonym in the context that
5061 -- declares the desired entity. This error can use a
5062 -- specialized message.
5063
5064 if In_Open_Scopes (P_Name)
5065 and then Present (Homonym (P_Name))
5066 and then Is_Compilation_Unit (Homonym (P_Name))
5067 and then
5068 (Is_Immediately_Visible (Homonym (P_Name))
5069 or else Is_Visible_Child_Unit (Homonym (P_Name)))
5070 then
5071 declare
5072 H : constant Entity_Id := Homonym (P_Name);
0d354370 5073
d6a24cdb
AC
5074 begin
5075 Id := First_Entity (H);
5076 while Present (Id) loop
d6a24cdb
AC
5077 if Chars (Id) = Chars (Selector) then
5078 Error_Msg_Qual_Level := 99;
5079 Error_Msg_Name_1 := Chars (Selector);
5080 Error_Msg_NE
5081 ("% not declared in&", N, P_Name);
5082 Error_Msg_NE
5083 ("\use fully qualified name starting with"
5084 & " Standard to make& visible", N, H);
5085 Error_Msg_Qual_Level := 0;
689cb4ac 5086 goto Done;
d6a24cdb
AC
5087 end if;
5088
5089 Next_Entity (Id);
5090 end loop;
689cb4ac
AC
5091
5092 -- If not found, standard error message.
5093
5094 Error_Msg_NE ("& not declared in&", N, Selector);
5095
5096 <<Done>> null;
d6a24cdb
AC
5097 end;
5098
5099 else
5100 Error_Msg_NE ("& not declared in&", N, Selector);
5101 end if;
996ae0b0 5102
bc41faa2 5103 -- Check for misspelling of some entity in prefix
996ae0b0
RK
5104
5105 Id := First_Entity (P_Name);
294ccb21
RD
5106 while Present (Id) loop
5107 if Is_Bad_Spelling_Of (Chars (Id), Chars (Selector))
5108 and then not Is_Internal_Name (Chars (Id))
5109 then
deef4289 5110 Error_Msg_NE -- CODEFIX
294ccb21
RD
5111 ("possible misspelling of&", Selector, Id);
5112 exit;
5113 end if;
996ae0b0 5114
294ccb21
RD
5115 Next_Entity (Id);
5116 end loop;
996ae0b0
RK
5117
5118 -- Specialize the message if this may be an instantiation
5119 -- of a child unit that was not mentioned in the context.
5120
5121 if Nkind (Parent (N)) = N_Package_Instantiation
5122 and then Is_Generic_Instance (Entity (Prefix (N)))
5123 and then Is_Compilation_Unit
294ccb21 5124 (Generic_Parent (Parent (Entity (Prefix (N)))))
996ae0b0 5125 then
16ca248a 5126 Error_Msg_Node_2 := Selector;
ed2233dc
AC
5127 Error_Msg_N -- CODEFIX
5128 ("\missing `WITH &.&;`", Prefix (N));
996ae0b0
RK
5129 end if;
5130 end if;
5131 end if;
5132
5133 Id := Any_Id;
5134 end if;
5135 end if;
5136
a77842bd 5137 <<Found>>
996ae0b0
RK
5138 if Comes_From_Source (N)
5139 and then Is_Remote_Access_To_Subprogram_Type (Id)
a77842bd 5140 and then Present (Equivalent_Type (Id))
996ae0b0 5141 then
16ca248a
ES
5142 -- If we are not actually generating distribution code (i.e. the
5143 -- current PCS is the dummy non-distributed version), then the
5144 -- Equivalent_Type will be missing, and Id should be treated as
5145 -- a regular access-to-subprogram type.
a77842bd 5146
996ae0b0
RK
5147 Id := Equivalent_Type (Id);
5148 Set_Chars (Selector, Chars (Id));
5149 end if;
5150
0ab80019 5151 -- Ada 2005 (AI-50217): Check usage of entities in limited withed units
19f0526a 5152
996ae0b0
RK
5153 if Ekind (P_Name) = E_Package
5154 and then From_With_Type (P_Name)
5155 then
5156 if From_With_Type (Id)
0fb2ea01
AC
5157 or else Is_Type (Id)
5158 or else Ekind (Id) = E_Package
996ae0b0
RK
5159 then
5160 null;
5161 else
5162 Error_Msg_N
657a9dd9 5163 ("limited withed package can only be used to access "
21d27997 5164 & "incomplete types",
996ae0b0
RK
5165 N);
5166 end if;
5167 end if;
5168
5169 if Is_Task_Type (P_Name)
5170 and then ((Ekind (Id) = E_Entry
294ccb21
RD
5171 and then Nkind (Parent (N)) /= N_Attribute_Reference)
5172 or else
5173 (Ekind (Id) = E_Entry_Family
5174 and then
5175 Nkind (Parent (Parent (N))) /= N_Attribute_Reference))
996ae0b0 5176 then
16ca248a
ES
5177 -- It is an entry call after all, either to the current task (which
5178 -- will deadlock) or to an enclosing task.
996ae0b0
RK
5179
5180 Analyze_Selected_Component (N);
5181 return;
5182 end if;
5183
5184 Change_Selected_Component_To_Expanded_Name (N);
07fc65c4
GB
5185
5186 -- Do style check and generate reference, but skip both steps if this
16ca248a
ES
5187 -- entity has homonyms, since we may not have the right homonym set yet.
5188 -- The proper homonym will be set during the resolve phase.
07fc65c4
GB
5189
5190 if Has_Homonym (Id) then
5191 Set_Entity (N, Id);
5192 else
4c484f40 5193 Set_Entity_Or_Discriminal (N, Id);
84df40f7
AC
5194
5195 if Is_LHS (N) then
5196 Generate_Reference (Id, N, 'm');
5197 else
5198 Generate_Reference (Id, N);
5199 end if;
07fc65c4 5200 end if;
996ae0b0
RK
5201
5202 if Is_Type (Id) then
5203 Set_Etype (N, Id);
5204 else
5205 Set_Etype (N, Get_Full_View (Etype (Id)));
5206 end if;
5207
30196a76
RD
5208 -- Check for violation of No_Wide_Characters
5209
5210 Check_Wide_Character_Restriction (Id, N);
5211
16ca248a
ES
5212 -- If the Ekind of the entity is Void, it means that all homonyms are
5213 -- hidden from all visibility (RM 8.3(5,14-20)).
996ae0b0
RK
5214
5215 if Ekind (Id) = E_Void then
5216 Premature_Usage (N);
5217
5218 elsif Is_Overloadable (Id)
5219 and then Present (Homonym (Id))
5220 then
5221 declare
5222 H : Entity_Id := Homonym (Id);
5223
5224 begin
5225 while Present (H) loop
35ae2ed8
AC
5226 if Scope (H) = Scope (Id)
5227 and then
5228 (not Is_Hidden (H)
5229 or else Is_Immediately_Visible (H))
5230 then
996ae0b0
RK
5231 Collect_Interps (N);
5232 exit;
5233 end if;
5234
5235 H := Homonym (H);
5236 end loop;
fbf5a39b 5237
16ca248a
ES
5238 -- If an extension of System is present, collect possible explicit
5239 -- overloadings declared in the extension.
fbf5a39b
AC
5240
5241 if Chars (P_Name) = Name_System
5242 and then Scope (P_Name) = Standard_Standard
5243 and then Present (System_Extend_Unit)
5244 and then Present_System_Aux (N)
5245 then
5246 H := Current_Entity (Id);
5247
5248 while Present (H) loop
5249 if Scope (H) = System_Aux_Id then
5250 Add_One_Interp (N, H, Etype (H));
5251 end if;
5252
5253 H := Homonym (H);
5254 end loop;
5255 end if;
996ae0b0
RK
5256 end;
5257 end if;
5258
5259 if Nkind (Selector_Name (N)) = N_Operator_Symbol
5260 and then Scope (Id) /= Standard_Standard
5261 then
16ca248a
ES
5262 -- In addition to user-defined operators in the given scope, there
5263 -- may be an implicit instance of the predefined operator. The
5264 -- operator (defined in Standard) is found in Has_Implicit_Operator,
5265 -- and added to the interpretations. Procedure Add_One_Interp will
5266 -- determine which hides which.
996ae0b0
RK
5267
5268 if Has_Implicit_Operator (N) then
5269 null;
5270 end if;
5271 end if;
5272 end Find_Expanded_Name;
5273
5274 -------------------------
5275 -- Find_Renamed_Entity --
5276 -------------------------
5277
5278 function Find_Renamed_Entity
5279 (N : Node_Id;
5280 Nam : Node_Id;
5281 New_S : Entity_Id;
5282 Is_Actual : Boolean := False) return Entity_Id
5283 is
fbf5a39b 5284 Ind : Interp_Index;
996ae0b0
RK
5285 I1 : Interp_Index := 0; -- Suppress junk warnings
5286 It : Interp;
5287 It1 : Interp;
5288 Old_S : Entity_Id;
5289 Inst : Entity_Id;
5290
5291 function Enclosing_Instance return Entity_Id;
5292 -- If the renaming determines the entity for the default of a formal
5293 -- subprogram nested within another instance, choose the innermost
5294 -- candidate. This is because if the formal has a box, and we are within
5295 -- an enclosing instance where some candidate interpretations are local
5296 -- to this enclosing instance, we know that the default was properly
5297 -- resolved when analyzing the generic, so we prefer the local
5298 -- candidates to those that are external. This is not always the case
16ca248a
ES
5299 -- but is a reasonable heuristic on the use of nested generics. The
5300 -- proper solution requires a full renaming model.
996ae0b0 5301
996ae0b0
RK
5302 function Is_Visible_Operation (Op : Entity_Id) return Boolean;
5303 -- If the renamed entity is an implicit operator, check whether it is
16ca248a
ES
5304 -- visible because its operand type is properly visible. This check
5305 -- applies to explicit renamed entities that appear in the source in a
5306 -- renaming declaration or a formal subprogram instance, but not to
5307 -- default generic actuals with a name.
996ae0b0 5308
923fa078
RD
5309 function Report_Overload return Entity_Id;
5310 -- List possible interpretations, and specialize message in the
5311 -- case of a generic actual.
5312
5313 function Within (Inner, Outer : Entity_Id) return Boolean;
16ca248a
ES
5314 -- Determine whether a candidate subprogram is defined within the
5315 -- enclosing instance. If yes, it has precedence over outer candidates.
923fa078 5316
996ae0b0
RK
5317 ------------------------
5318 -- Enclosing_Instance --
5319 ------------------------
5320
5321 function Enclosing_Instance return Entity_Id is
5322 S : Entity_Id;
5323
5324 begin
5325 if not Is_Generic_Instance (Current_Scope)
5326 and then not Is_Actual
5327 then
5328 return Empty;
5329 end if;
5330
5331 S := Scope (Current_Scope);
996ae0b0 5332 while S /= Standard_Standard loop
996ae0b0
RK
5333 if Is_Generic_Instance (S) then
5334 return S;
5335 end if;
5336
5337 S := Scope (S);
5338 end loop;
5339
5340 return Empty;
5341 end Enclosing_Instance;
5342
5343 --------------------------
5344 -- Is_Visible_Operation --
5345 --------------------------
5346
5347 function Is_Visible_Operation (Op : Entity_Id) return Boolean is
5348 Scop : Entity_Id;
5349 Typ : Entity_Id;
5350 Btyp : Entity_Id;
5351
5352 begin
5353 if Ekind (Op) /= E_Operator
5354 or else Scope (Op) /= Standard_Standard
5355 or else (In_Instance
5356 and then
5357 (not Is_Actual
5358 or else Present (Enclosing_Instance)))
5359 then
5360 return True;
5361
5362 else
5363 -- For a fixed point type operator, check the resulting type,
5364 -- because it may be a mixed mode integer * fixed operation.
5365
5366 if Present (Next_Formal (First_Formal (New_S)))
5367 and then Is_Fixed_Point_Type (Etype (New_S))
5368 then
5369 Typ := Etype (New_S);
5370 else
5371 Typ := Etype (First_Formal (New_S));
5372 end if;
5373
5374 Btyp := Base_Type (Typ);
5375
5376 if Nkind (Nam) /= N_Expanded_Name then
5377 return (In_Open_Scopes (Scope (Btyp))
5378 or else Is_Potentially_Use_Visible (Btyp)
5379 or else In_Use (Btyp)
5380 or else In_Use (Scope (Btyp)));
5381
5382 else
5383 Scop := Entity (Prefix (Nam));
5384
5385 if Ekind (Scop) = E_Package
5386 and then Present (Renamed_Object (Scop))
5387 then
5388 Scop := Renamed_Object (Scop);
5389 end if;
5390
5391 -- Operator is visible if prefix of expanded name denotes
16b05213 5392 -- scope of type, or else type is defined in System_Aux
996ae0b0
RK
5393 -- and the prefix denotes System.
5394
5395 return Scope (Btyp) = Scop
5396 or else (Scope (Btyp) = System_Aux_Id
5397 and then Scope (Scope (Btyp)) = Scop);
5398 end if;
5399 end if;
5400 end Is_Visible_Operation;
5401
5402 ------------
5403 -- Within --
5404 ------------
5405
5406 function Within (Inner, Outer : Entity_Id) return Boolean is
16ca248a 5407 Sc : Entity_Id;
996ae0b0
RK
5408
5409 begin
16ca248a 5410 Sc := Scope (Inner);
996ae0b0 5411 while Sc /= Standard_Standard loop
996ae0b0
RK
5412 if Sc = Outer then
5413 return True;
5414 else
5415 Sc := Scope (Sc);
5416 end if;
5417 end loop;
5418
5419 return False;
5420 end Within;
5421
923fa078
RD
5422 ---------------------
5423 -- Report_Overload --
5424 ---------------------
fbf5a39b
AC
5425
5426 function Report_Overload return Entity_Id is
5427 begin
5428 if Is_Actual then
ed2233dc 5429 Error_Msg_NE -- CODEFIX
fbf5a39b 5430 ("ambiguous actual subprogram&, " &
923fa078 5431 "possible interpretations:", N, Nam);
fbf5a39b 5432 else
ed2233dc 5433 Error_Msg_N -- CODEFIX
fbf5a39b 5434 ("ambiguous subprogram, " &
923fa078 5435 "possible interpretations:", N);
fbf5a39b
AC
5436 end if;
5437
5438 List_Interps (Nam, N);
5439 return Old_S;
5440 end Report_Overload;
5441
996ae0b0
RK
5442 -- Start of processing for Find_Renamed_Entry
5443
5444 begin
5445 Old_S := Any_Id;
5446 Candidate_Renaming := Empty;
5447
5448 if not Is_Overloaded (Nam) then
70b70ce8
AC
5449 if Entity_Matches_Spec (Entity (Nam), New_S) then
5450 Candidate_Renaming := New_S;
5451
5452 if Is_Visible_Operation (Entity (Nam)) then
5453 Old_S := Entity (Nam);
5454 end if;
996ae0b0
RK
5455
5456 elsif
5457 Present (First_Formal (Entity (Nam)))
5458 and then Present (First_Formal (New_S))
5459 and then (Base_Type (Etype (First_Formal (Entity (Nam))))
5460 = Base_Type (Etype (First_Formal (New_S))))
5461 then
5462 Candidate_Renaming := Entity (Nam);
5463 end if;
5464
5465 else
fbf5a39b 5466 Get_First_Interp (Nam, Ind, It);
996ae0b0 5467 while Present (It.Nam) loop
996ae0b0
RK
5468 if Entity_Matches_Spec (It.Nam, New_S)
5469 and then Is_Visible_Operation (It.Nam)
5470 then
5471 if Old_S /= Any_Id then
5472
5473 -- Note: The call to Disambiguate only happens if a
5474 -- previous interpretation was found, in which case I1
5475 -- has received a value.
5476
fbf5a39b 5477 It1 := Disambiguate (Nam, I1, Ind, Etype (Old_S));
996ae0b0
RK
5478
5479 if It1 = No_Interp then
996ae0b0
RK
5480 Inst := Enclosing_Instance;
5481
5482 if Present (Inst) then
996ae0b0
RK
5483 if Within (It.Nam, Inst) then
5484 return (It.Nam);
996ae0b0
RK
5485 elsif Within (Old_S, Inst) then
5486 return (Old_S);
996ae0b0 5487 else
fbf5a39b 5488 return Report_Overload;
996ae0b0
RK
5489 end if;
5490
5491 else
fbf5a39b 5492 return Report_Overload;
996ae0b0
RK
5493 end if;
5494
5495 else
5496 Old_S := It1.Nam;
5497 exit;
5498 end if;
5499
5500 else
fbf5a39b 5501 I1 := Ind;
996ae0b0
RK
5502 Old_S := It.Nam;
5503 end if;
5504
5505 elsif
5506 Present (First_Formal (It.Nam))
5507 and then Present (First_Formal (New_S))
5508 and then (Base_Type (Etype (First_Formal (It.Nam)))
5509 = Base_Type (Etype (First_Formal (New_S))))
5510 then
5511 Candidate_Renaming := It.Nam;
5512 end if;
5513
fbf5a39b 5514 Get_Next_Interp (Ind, It);
996ae0b0
RK
5515 end loop;
5516
5517 Set_Entity (Nam, Old_S);
1138cf59
AC
5518
5519 if Old_S /= Any_Id then
5520 Set_Is_Overloaded (Nam, False);
5521 end if;
996ae0b0
RK
5522 end if;
5523
5524 return Old_S;
5525 end Find_Renamed_Entity;
5526
5527 -----------------------------
5528 -- Find_Selected_Component --
5529 -----------------------------
5530
5531 procedure Find_Selected_Component (N : Node_Id) is
fbf5a39b 5532 P : constant Node_Id := Prefix (N);
996ae0b0
RK
5533
5534 P_Name : Entity_Id;
5535 -- Entity denoted by prefix
5536
5537 P_Type : Entity_Id;
5538 -- and its type
5539
5540 Nam : Node_Id;
5541
5542 begin
5543 Analyze (P);
5544
5545 if Nkind (P) = N_Error then
5546 return;
23685ae6
AC
5547 end if;
5548
5549 -- Selector name cannot be a character literal or an operator symbol in
db72f10a 5550 -- SPARK, except for the operator symbol in a renaming.
23685ae6
AC
5551
5552 if SPARK_Mode or else Restriction_Check_Required (SPARK) then
5553 if Nkind (Selector_Name (N)) = N_Character_Literal then
5554 Check_Formal_Restriction
5555 ("character literal cannot be prefixed", N);
db72f10a
AC
5556 elsif Nkind (Selector_Name (N)) = N_Operator_Symbol
5557 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
5558 then
23685ae6
AC
5559 Check_Formal_Restriction ("operator symbol cannot be prefixed", N);
5560 end if;
5561 end if;
996ae0b0 5562
16ca248a
ES
5563 -- If the selector already has an entity, the node has been constructed
5564 -- in the course of expansion, and is known to be valid. Do not verify
5565 -- that it is defined for the type (it may be a private component used
5566 -- in the expansion of record equality).
996ae0b0 5567
23685ae6 5568 if Present (Entity (Selector_Name (N))) then
996ae0b0
RK
5569 if No (Etype (N))
5570 or else Etype (N) = Any_Type
5571 then
5572 declare
fbf5a39b
AC
5573 Sel_Name : constant Node_Id := Selector_Name (N);
5574 Selector : constant Entity_Id := Entity (Sel_Name);
996ae0b0
RK
5575 C_Etype : Node_Id;
5576
5577 begin
5578 Set_Etype (Sel_Name, Etype (Selector));
5579
5580 if not Is_Entity_Name (P) then
fbf5a39b 5581 Resolve (P);
996ae0b0
RK
5582 end if;
5583
5584 -- Build an actual subtype except for the first parameter
fbf5a39b 5585 -- of an init proc, where this actual subtype is by
996ae0b0
RK
5586 -- definition incorrect, since the object is uninitialized
5587 -- (and does not even have defined discriminants etc.)
5588
5589 if Is_Entity_Name (P)
5590 and then Ekind (Entity (P)) = E_Function
5591 then
5592 Nam := New_Copy (P);
5593
5594 if Is_Overloaded (P) then
5595 Save_Interps (P, Nam);
5596 end if;
5597
5598 Rewrite (P,
5599 Make_Function_Call (Sloc (P), Name => Nam));
5600 Analyze_Call (P);
5601 Analyze_Selected_Component (N);
5602 return;
5603
5604 elsif Ekind (Selector) = E_Component
5605 and then (not Is_Entity_Name (P)
5606 or else Chars (Entity (P)) /= Name_uInit)
5607 then
9fc2854d
AC
5608 -- Do not build the subtype when referencing components of
5609 -- dispatch table wrappers. Required to avoid generating
5610 -- elaboration code with HI runtimes.
5611
5612 if RTU_Loaded (Ada_Tags)
5613 and then RTE_Available (RE_Dispatch_Table_Wrapper)
5614 and then Scope (Selector) = RTE (RE_Dispatch_Table_Wrapper)
5615 then
5616 C_Etype := Empty;
5617
5618 elsif RTU_Loaded (Ada_Tags)
5619 and then RTE_Available (RE_No_Dispatch_Table_Wrapper)
5620 and then Scope (Selector)
5621 = RTE (RE_No_Dispatch_Table_Wrapper)
5622 then
5623 C_Etype := Empty;
5624
5625 else
5626 C_Etype :=
5627 Build_Actual_Subtype_Of_Component (
5628 Etype (Selector), N);
5629 end if;
5630
996ae0b0
RK
5631 else
5632 C_Etype := Empty;
5633 end if;
5634
5635 if No (C_Etype) then
5636 C_Etype := Etype (Selector);
5637 else
5638 Insert_Action (N, C_Etype);
5639 C_Etype := Defining_Identifier (C_Etype);
5640 end if;
5641
5642 Set_Etype (N, C_Etype);
5643 end;
5644
5645 -- If this is the name of an entry or protected operation, and
5646 -- the prefix is an access type, insert an explicit dereference,
5647 -- so that entry calls are treated uniformly.
5648
5649 if Is_Access_Type (Etype (P))
5650 and then Is_Concurrent_Type (Designated_Type (Etype (P)))
5651 then
5652 declare
fbf5a39b
AC
5653 New_P : constant Node_Id :=
5654 Make_Explicit_Dereference (Sloc (P),
5655 Prefix => Relocate_Node (P));
996ae0b0
RK
5656 begin
5657 Rewrite (P, New_P);
5658 Set_Etype (P, Designated_Type (Etype (Prefix (P))));
5659 end;
5660 end if;
5661
5662 -- If the selected component appears within a default expression
5663 -- and it has an actual subtype, the pre-analysis has not yet
5664 -- completed its analysis, because Insert_Actions is disabled in
fbf5a39b 5665 -- that context. Within the init proc of the enclosing type we
996ae0b0
RK
5666 -- must complete this analysis, if an actual subtype was created.
5667
5668 elsif Inside_Init_Proc then
5669 declare
5670 Typ : constant Entity_Id := Etype (N);
5671 Decl : constant Node_Id := Declaration_Node (Typ);
996ae0b0
RK
5672 begin
5673 if Nkind (Decl) = N_Subtype_Declaration
5674 and then not Analyzed (Decl)
5675 and then Is_List_Member (Decl)
5676 and then No (Parent (Decl))
5677 then
5678 Remove (Decl);
5679 Insert_Action (N, Decl);
5680 end if;
5681 end;
5682 end if;
5683
5684 return;
5685
5686 elsif Is_Entity_Name (P) then
5687 P_Name := Entity (P);
5688
5689 -- The prefix may denote an enclosing type which is the completion
5690 -- of an incomplete type declaration.
5691
5692 if Is_Type (P_Name) then
5693 Set_Entity (P, Get_Full_View (P_Name));
5694 Set_Etype (P, Entity (P));
5695 P_Name := Entity (P);
5696 end if;
5697
5698 P_Type := Base_Type (Etype (P));
5699
5700 if Debug_Flag_E then
5701 Write_Str ("Found prefix type to be ");
5702 Write_Entity_Info (P_Type, " "); Write_Eol;
5703 end if;
5704
5705 -- First check for components of a record object (not the
5706 -- result of a call, which is handled below).
5707
5708 if Is_Appropriate_For_Record (P_Type)
5709 and then not Is_Overloadable (P_Name)
5710 and then not Is_Type (P_Name)
5711 then
5712 -- Selected component of record. Type checking will validate
5713 -- name of selector.
11560bcc
TQ
5714 -- ??? could we rewrite an implicit dereference into an explicit
5715 -- one here?
996ae0b0
RK
5716
5717 Analyze_Selected_Component (N);
5718
f6b5dc8e
AC
5719 -- Reference to type name in predicate/invariant expression
5720
996ae0b0
RK
5721 elsif Is_Appropriate_For_Entry_Prefix (P_Type)
5722 and then not In_Open_Scopes (P_Name)
5723 and then (not Is_Concurrent_Type (Etype (P_Name))
5724 or else not In_Open_Scopes (Etype (P_Name)))
5725 then
5726 -- Call to protected operation or entry. Type checking is
5727 -- needed on the prefix.
5728
5729 Analyze_Selected_Component (N);
5730
5731 elsif (In_Open_Scopes (P_Name)
f6b5dc8e
AC
5732 and then Ekind (P_Name) /= E_Void
5733 and then not Is_Overloadable (P_Name))
996ae0b0 5734 or else (Is_Concurrent_Type (Etype (P_Name))
f6b5dc8e 5735 and then In_Open_Scopes (Etype (P_Name)))
996ae0b0
RK
5736 then
5737 -- Prefix denotes an enclosing loop, block, or task, i.e. an
5738 -- enclosing construct that is not a subprogram or accept.
5739
5740 Find_Expanded_Name (N);
5741
5742 elsif Ekind (P_Name) = E_Package then
5743 Find_Expanded_Name (N);
5744
5745 elsif Is_Overloadable (P_Name) then
5746
5747 -- The subprogram may be a renaming (of an enclosing scope) as
5748 -- in the case of the name of the generic within an instantiation.
5749
f6b5dc8e 5750 if Ekind_In (P_Name, E_Procedure, E_Function)
996ae0b0
RK
5751 and then Present (Alias (P_Name))
5752 and then Is_Generic_Instance (Alias (P_Name))
5753 then
5754 P_Name := Alias (P_Name);
5755 end if;
5756
5757 if Is_Overloaded (P) then
5758
bc41faa2 5759 -- The prefix must resolve to a unique enclosing construct
996ae0b0
RK
5760
5761 declare
5762 Found : Boolean := False;
fbf5a39b 5763 Ind : Interp_Index;
996ae0b0
RK
5764 It : Interp;
5765
5766 begin
fbf5a39b 5767 Get_First_Interp (P, Ind, It);
996ae0b0 5768 while Present (It.Nam) loop
996ae0b0
RK
5769 if In_Open_Scopes (It.Nam) then
5770 if Found then
5771 Error_Msg_N (
5772 "prefix must be unique enclosing scope", N);
5773 Set_Entity (N, Any_Id);
5774 Set_Etype (N, Any_Type);
5775 return;
5776
5777 else
5778 Found := True;
5779 P_Name := It.Nam;
5780 end if;
5781 end if;
5782
fbf5a39b 5783 Get_Next_Interp (Ind, It);
996ae0b0
RK
5784 end loop;
5785 end;
5786 end if;
5787
5788 if In_Open_Scopes (P_Name) then
5789 Set_Entity (P, P_Name);
5790 Set_Is_Overloaded (P, False);
5791 Find_Expanded_Name (N);
5792
5793 else
5794 -- If no interpretation as an expanded name is possible, it
5795 -- must be a selected component of a record returned by a
16ca248a
ES
5796 -- function call. Reformat prefix as a function call, the rest
5797 -- is done by type resolution. If the prefix is procedure or
5798 -- entry, as is P.X; this is an error.
996ae0b0
RK
5799
5800 if Ekind (P_Name) /= E_Function
5801 and then (not Is_Overloaded (P)
5802 or else
5803 Nkind (Parent (N)) = N_Procedure_Call_Statement)
5804 then
996ae0b0 5805 -- Prefix may mention a package that is hidden by a local
290986ed
GB
5806 -- declaration: let the user know. Scan the full homonym
5807 -- chain, the candidate package may be anywhere on it.
996ae0b0 5808
290986ed
GB
5809 if Present (Homonym (Current_Entity (P_Name))) then
5810
5811 P_Name := Current_Entity (P_Name);
996ae0b0
RK
5812
5813 while Present (P_Name) loop
5814 exit when Ekind (P_Name) = E_Package;
5815 P_Name := Homonym (P_Name);
5816 end loop;
5817
5818 if Present (P_Name) then
5819 Error_Msg_Sloc := Sloc (Entity (Prefix (N)));
5820
5821 Error_Msg_NE
5822 ("package& is hidden by declaration#",
5823 N, P_Name);
5824
5825 Set_Entity (Prefix (N), P_Name);
5826 Find_Expanded_Name (N);
5827 return;
5828 else
5829 P_Name := Entity (Prefix (N));
5830 end if;
5831 end if;
5832
5833 Error_Msg_NE
5834 ("invalid prefix in selected component&", N, P_Name);
5835 Change_Selected_Component_To_Expanded_Name (N);
5836 Set_Entity (N, Any_Id);
5837 Set_Etype (N, Any_Type);
5838
5839 else
5840 Nam := New_Copy (P);
5841 Save_Interps (P, Nam);
5842 Rewrite (P,
5843 Make_Function_Call (Sloc (P), Name => Nam));
5844 Analyze_Call (P);
5845 Analyze_Selected_Component (N);
5846 end if;
5847 end if;
5848
5849 -- Remaining cases generate various error messages
5850
5851 else
5852 -- Format node as expanded name, to avoid cascaded errors
5853
fbf5a39b 5854 Change_Selected_Component_To_Expanded_Name (N);
996ae0b0
RK
5855 Set_Entity (N, Any_Id);
5856 Set_Etype (N, Any_Type);
5857
996ae0b0
RK
5858 -- Issue error message, but avoid this if error issued already.
5859 -- Use identifier of prefix if one is available.
5860
5861 if P_Name = Any_Id then
5862 null;
5863
5864 elsif Ekind (P_Name) = E_Void then
5865 Premature_Usage (P);
5866
5867 elsif Nkind (P) /= N_Attribute_Reference then
5868 Error_Msg_N (
5869 "invalid prefix in selected component&", P);
5870
9596236a
AC
5871 if Is_Access_Type (P_Type)
5872 and then Ekind (Designated_Type (P_Type)) = E_Incomplete_Type
5873 then
65356e64
AC
5874 Error_Msg_N
5875 ("\dereference must not be of an incomplete type " &
11560bcc 5876 "(RM 3.10.1)", P);
9596236a
AC
5877 end if;
5878
996ae0b0
RK
5879 else
5880 Error_Msg_N (
5881 "invalid prefix in selected component", P);
5882 end if;
5883 end if;
5884
db72f10a
AC
5885 -- Selector name is restricted in SPARK
5886
5887 if Nkind (N) = N_Expanded_Name
5888 and then (SPARK_Mode or else Restriction_Check_Required (SPARK))
5889 then
5890 if Is_Subprogram (P_Name) then
5891 Check_Formal_Restriction
5892 ("prefix of expanded name cannot be a subprogram", P);
5893 elsif Ekind (P_Name) = E_Loop then
5894 Check_Formal_Restriction
5895 ("prefix of expanded name cannot be a loop statement", P);
5896 end if;
5897 end if;
5898
996ae0b0
RK
5899 else
5900 -- If prefix is not the name of an entity, it must be an expression,
5901 -- whose type is appropriate for a record. This is determined by
5902 -- type resolution.
5903
5904 Analyze_Selected_Component (N);
5905 end if;
5906 end Find_Selected_Component;
5907
5908 ---------------
5909 -- Find_Type --
5910 ---------------
5911
5912 procedure Find_Type (N : Node_Id) is
5913 C : Entity_Id;
5914 Typ : Entity_Id;
5915 T : Entity_Id;
5916 T_Name : Entity_Id;
5917
5918 begin
5919 if N = Error then
5920 return;
5921
5922 elsif Nkind (N) = N_Attribute_Reference then
5923
11560bcc
TQ
5924 -- Class attribute. This is not valid in Ada 83 mode, but we do not
5925 -- need to enforce that at this point, since the declaration of the
5926 -- tagged type in the prefix would have been flagged already.
996ae0b0
RK
5927
5928 if Attribute_Name (N) = Name_Class then
5929 Check_Restriction (No_Dispatch, N);
5930 Find_Type (Prefix (N));
5931
5932 -- Propagate error from bad prefix
5933
5934 if Etype (Prefix (N)) = Any_Type then
5935 Set_Entity (N, Any_Type);
5936 Set_Etype (N, Any_Type);
5937 return;
5938 end if;
5939
5940 T := Base_Type (Entity (Prefix (N)));
5941
11560bcc
TQ
5942 -- Case where type is not known to be tagged. Its appearance in
5943 -- the prefix of the 'Class attribute indicates that the full view
16ca248a 5944 -- will be tagged.
996ae0b0
RK
5945
5946 if not Is_Tagged_Type (T) then
5947 if Ekind (T) = E_Incomplete_Type then
5948
5949 -- It is legal to denote the class type of an incomplete
5950 -- type. The full type will have to be tagged, of course.
90067a15 5951 -- In Ada 2005 this usage is declared obsolescent, so we
ae247488
ES
5952 -- warn accordingly. This usage is only legal if the type
5953 -- is completed in the current scope, and not for a limited
5954 -- view of a type.
5955
5956 if not Is_Tagged_Type (T)
0791fbe9 5957 and then Ada_Version >= Ada_2005
ae247488
ES
5958 then
5959 if From_With_Type (T) then
5960 Error_Msg_N
5961 ("prefix of Class attribute must be tagged", N);
5962 Set_Etype (N, Any_Type);
5963 Set_Entity (N, Any_Type);
5964 return;
11560bcc
TQ
5965
5966 -- ??? This test is temporarily disabled (always False)
5967 -- because it causes an unwanted warning on GNAT sources
5968 -- (built with -gnatg, which includes Warn_On_Obsolescent_
5969 -- Feature). Once this issue is cleared in the sources, it
5970 -- can be enabled.
5971
ae247488
ES
5972 elsif Warn_On_Obsolescent_Feature
5973 and then False
5974 then
5975 Error_Msg_N
5976 ("applying 'Class to an untagged incomplete type"
5977 & " is an obsolescent feature (RM J.11)", N);
5978 end if;
11560bcc 5979 end if;
996ae0b0
RK
5980
5981 Set_Is_Tagged_Type (T);
ef2a63ba 5982 Set_Direct_Primitive_Operations (T, New_Elmt_List);
996ae0b0
RK
5983 Make_Class_Wide_Type (T);
5984 Set_Entity (N, Class_Wide_Type (T));
5985 Set_Etype (N, Class_Wide_Type (T));
5986
5987 elsif Ekind (T) = E_Private_Type
5988 and then not Is_Generic_Type (T)
5989 and then In_Private_Part (Scope (T))
5990 then
16ca248a
ES
5991 -- The Class attribute can be applied to an untagged private
5992 -- type fulfilled by a tagged type prior to the full type
5993 -- declaration (but only within the parent package's private
5994 -- part). Create the class-wide type now and check that the
5995 -- full type is tagged later during its analysis. Note that
5996 -- we do not mark the private type as tagged, unlike the
5997 -- case of incomplete types, because the type must still
996ae0b0
RK
5998 -- appear untagged to outside units.
5999
cdc8c54c 6000 if No (Class_Wide_Type (T)) then
996ae0b0
RK
6001 Make_Class_Wide_Type (T);
6002 end if;
6003
6004 Set_Entity (N, Class_Wide_Type (T));
6005 Set_Etype (N, Class_Wide_Type (T));
6006
6007 else
16ca248a
ES
6008 -- Should we introduce a type Any_Tagged and use Wrong_Type
6009 -- here, it would be a bit more consistent???
996ae0b0
RK
6010
6011 Error_Msg_NE
6012 ("tagged type required, found}",
6013 Prefix (N), First_Subtype (T));
6014 Set_Entity (N, Any_Type);
6015 return;
6016 end if;
6017
6018 -- Case of tagged type
6019
6020 else
39edfb45 6021 if Is_Concurrent_Type (T) then
cdc8c54c
BD
6022 if No (Corresponding_Record_Type (Entity (Prefix (N)))) then
6023
6024 -- Previous error. Use current type, which at least
6025 -- provides some operations.
6026
6027 C := Entity (Prefix (N));
6028
6029 else
6030 C := Class_Wide_Type
6031 (Corresponding_Record_Type (Entity (Prefix (N))));
6032 end if;
6033
39edfb45
JM
6034 else
6035 C := Class_Wide_Type (Entity (Prefix (N)));
6036 end if;
6037
996ae0b0
RK
6038 Set_Entity_With_Style_Check (N, C);
6039 Generate_Reference (C, N);
6040 Set_Etype (N, C);
996ae0b0
RK
6041 end if;
6042
0ab80019 6043 -- Base attribute, not allowed in Ada 83
996ae0b0
RK
6044
6045 elsif Attribute_Name (N) = Name_Base then
7a489a2b
AC
6046 Error_Msg_Name_1 := Name_Base;
6047 Check_Formal_Restriction
6048 ("attribute% is only allowed as prefix of another attribute", N);
6049
0ab80019 6050 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
996ae0b0
RK
6051 Error_Msg_N
6052 ("(Ada 83) Base attribute not allowed in subtype mark", N);
6053
6054 else
6055 Find_Type (Prefix (N));
6056 Typ := Entity (Prefix (N));
6057
0ab80019 6058 if Ada_Version >= Ada_95
fbf5a39b
AC
6059 and then not Is_Scalar_Type (Typ)
6060 and then not Is_Generic_Type (Typ)
6061 then
6062 Error_Msg_N
bc41faa2
AC
6063 ("prefix of Base attribute must be scalar type",
6064 Prefix (N));
fbf5a39b 6065
a8930b80 6066 elsif Warn_On_Redundant_Constructs
996ae0b0 6067 and then Base_Type (Typ) = Typ
996ae0b0 6068 then
ed2233dc 6069 Error_Msg_NE -- CODEFIX
ad6b5b00 6070 ("?redundant attribute, & is its own base type", N, Typ);
996ae0b0
RK
6071 end if;
6072
6073 T := Base_Type (Typ);
996ae0b0
RK
6074
6075 -- Rewrite attribute reference with type itself (see similar
a8930b80
AC
6076 -- processing in Analyze_Attribute, case Base). Preserve prefix
6077 -- if present, for other legality checks.
fbf5a39b
AC
6078
6079 if Nkind (Prefix (N)) = N_Expanded_Name then
6080 Rewrite (N,
6081 Make_Expanded_Name (Sloc (N),
11560bcc
TQ
6082 Chars => Chars (T),
6083 Prefix => New_Copy (Prefix (Prefix (N))),
6084 Selector_Name => New_Reference_To (T, Sloc (N))));
996ae0b0 6085
fbf5a39b 6086 else
11560bcc 6087 Rewrite (N, New_Reference_To (T, Sloc (N)));
fbf5a39b
AC
6088 end if;
6089
6090 Set_Entity (N, T);
996ae0b0
RK
6091 Set_Etype (N, T);
6092 end if;
6093
923fa078
RD
6094 elsif Attribute_Name (N) = Name_Stub_Type then
6095
6096 -- This is handled in Analyze_Attribute
6097
6098 Analyze (N);
6099
996ae0b0
RK
6100 -- All other attributes are invalid in a subtype mark
6101
6102 else
6103 Error_Msg_N ("invalid attribute in subtype mark", N);
6104 end if;
6105
6106 else
6107 Analyze (N);
6108
6109 if Is_Entity_Name (N) then
6110 T_Name := Entity (N);
6111 else
6112 Error_Msg_N ("subtype mark required in this context", N);
6113 Set_Etype (N, Any_Type);
6114 return;
6115 end if;
6116
6117 if T_Name = Any_Id or else Etype (N) = Any_Type then
6118
6119 -- Undefined id. Make it into a valid type
6120
6121 Set_Entity (N, Any_Type);
6122
6123 elsif not Is_Type (T_Name)
6124 and then T_Name /= Standard_Void_Type
6125 then
6126 Error_Msg_Sloc := Sloc (T_Name);
6127 Error_Msg_N ("subtype mark required in this context", N);
923fa078 6128 Error_Msg_NE ("\\found & declared#", N, T_Name);
996ae0b0
RK
6129 Set_Entity (N, Any_Type);
6130
6131 else
11560bcc
TQ
6132 -- If the type is an incomplete type created to handle
6133 -- anonymous access components of a record type, then the
6134 -- incomplete type is the visible entity and subsequent
6135 -- references will point to it. Mark the original full
6136 -- type as referenced, to prevent spurious warnings.
6137
6138 if Is_Incomplete_Type (T_Name)
6139 and then Present (Full_View (T_Name))
6140 and then not Comes_From_Source (T_Name)
6141 then
6142 Set_Referenced (Full_View (T_Name));
6143 end if;
6144
996ae0b0
RK
6145 T_Name := Get_Full_View (T_Name);
6146
11560bcc
TQ
6147 -- Ada 2005 (AI-251, AI-50217): Handle interfaces visible through
6148 -- limited-with clauses
6149
6150 if From_With_Type (T_Name)
6151 and then Ekind (T_Name) in Incomplete_Kind
6152 and then Present (Non_Limited_View (T_Name))
6153 and then Is_Interface (Non_Limited_View (T_Name))
6154 then
6155 T_Name := Non_Limited_View (T_Name);
6156 end if;
6157
996ae0b0
RK
6158 if In_Open_Scopes (T_Name) then
6159 if Ekind (Base_Type (T_Name)) = E_Task_Type then
923fa078
RD
6160
6161 -- In Ada 2005, a task name can be used in an access
e264efcc
AC
6162 -- definition within its own body. It cannot be used
6163 -- in the discriminant part of the task declaration,
6164 -- nor anywhere else in the declaration because entries
6165 -- cannot have access parameters.
923fa078 6166
0791fbe9 6167 if Ada_Version >= Ada_2005
923fa078
RD
6168 and then Nkind (Parent (N)) = N_Access_Definition
6169 then
6170 Set_Entity (N, T_Name);
6171 Set_Etype (N, T_Name);
e264efcc
AC
6172
6173 if Has_Completion (T_Name) then
6174 return;
6175
6176 else
6177 Error_Msg_N
6178 ("task type cannot be used as type mark " &
6179 "within its own declaration", N);
6180 end if;
923fa078
RD
6181
6182 else
6183 Error_Msg_N
6184 ("task type cannot be used as type mark " &
fbe627af 6185 "within its own spec or body", N);
923fa078 6186 end if;
fbe627af
RD
6187
6188 elsif Ekind (Base_Type (T_Name)) = E_Protected_Type then
6189
6190 -- In Ada 2005, a protected name can be used in an access
6191 -- definition within its own body.
6192
0791fbe9 6193 if Ada_Version >= Ada_2005
fbe627af
RD
6194 and then Nkind (Parent (N)) = N_Access_Definition
6195 then
6196 Set_Entity (N, T_Name);
6197 Set_Etype (N, T_Name);
6198 return;
6199
6200 else
6201 Error_Msg_N
6202 ("protected type cannot be used as type mark " &
6203 "within its own spec or body", N);
6204 end if;
6205
996ae0b0
RK
6206 else
6207 Error_Msg_N ("type declaration cannot refer to itself", N);
6208 end if;
6209
6210 Set_Etype (N, Any_Type);
6211 Set_Entity (N, Any_Type);
6212 Set_Error_Posted (T_Name);
6213 return;
6214 end if;
6215
6216 Set_Entity (N, T_Name);
6217 Set_Etype (N, T_Name);
6218 end if;
6219 end if;
6220
07fc65c4 6221 if Present (Etype (N)) and then Comes_From_Source (N) then
996ae0b0
RK
6222 if Is_Fixed_Point_Type (Etype (N)) then
6223 Check_Restriction (No_Fixed_Point, N);
6224 elsif Is_Floating_Point_Type (Etype (N)) then
6225 Check_Restriction (No_Floating_Point, N);
6226 end if;
6227 end if;
6228 end Find_Type;
6229
996ae0b0
RK
6230 ------------------------------------
6231 -- Has_Implicit_Character_Literal --
6232 ------------------------------------
6233
6234 function Has_Implicit_Character_Literal (N : Node_Id) return Boolean is
6235 Id : Entity_Id;
6236 Found : Boolean := False;
6237 P : constant Entity_Id := Entity (Prefix (N));
6238 Priv_Id : Entity_Id := Empty;
6239
6240 begin
6241 if Ekind (P) = E_Package
6242 and then not In_Open_Scopes (P)
6243 then
6244 Priv_Id := First_Private_Entity (P);
6245 end if;
6246
6247 if P = Standard_Standard then
6248 Change_Selected_Component_To_Expanded_Name (N);
6249 Rewrite (N, Selector_Name (N));
6250 Analyze (N);
6251 Set_Etype (Original_Node (N), Standard_Character);
6252 return True;
6253 end if;
6254
6255 Id := First_Entity (P);
996ae0b0
RK
6256 while Present (Id)
6257 and then Id /= Priv_Id
6258 loop
d347f572
AC
6259 if Is_Standard_Character_Type (Id) and then Is_Base_Type (Id) then
6260
996ae0b0
RK
6261 -- We replace the node with the literal itself, resolve as a
6262 -- character, and set the type correctly.
6263
6264 if not Found then
6265 Change_Selected_Component_To_Expanded_Name (N);
6266 Rewrite (N, Selector_Name (N));
6267 Analyze (N);
6268 Set_Etype (N, Id);
6269 Set_Etype (Original_Node (N), Id);
6270 Found := True;
6271
6272 else
6273 -- More than one type derived from Character in given scope.
6274 -- Collect all possible interpretations.
6275
6276 Add_One_Interp (N, Id, Id);
6277 end if;
6278 end if;
6279
6280 Next_Entity (Id);
6281 end loop;
6282
6283 return Found;
6284 end Has_Implicit_Character_Literal;
6285
9bc856dd
AC
6286 ----------------------
6287 -- Has_Private_With --
6288 ----------------------
6289
6290 function Has_Private_With (E : Entity_Id) return Boolean is
6291 Comp_Unit : constant Node_Id := Cunit (Current_Sem_Unit);
6292 Item : Node_Id;
6293
6294 begin
6295 Item := First (Context_Items (Comp_Unit));
6296 while Present (Item) loop
6297 if Nkind (Item) = N_With_Clause
6298 and then Private_Present (Item)
6299 and then Entity (Name (Item)) = E
6300 then
6301 return True;
6302 end if;
6303
6304 Next (Item);
6305 end loop;
6306
6307 return False;
6308 end Has_Private_With;
6309
996ae0b0
RK
6310 ---------------------------
6311 -- Has_Implicit_Operator --
6312 ---------------------------
6313
6314 function Has_Implicit_Operator (N : Node_Id) return Boolean is
6315 Op_Id : constant Name_Id := Chars (Selector_Name (N));
6316 P : constant Entity_Id := Entity (Prefix (N));
6317 Id : Entity_Id;
6318 Priv_Id : Entity_Id := Empty;
6319
fbf5a39b
AC
6320 procedure Add_Implicit_Operator
6321 (T : Entity_Id;
6322 Op_Type : Entity_Id := Empty);
fbe627af
RD
6323 -- Add implicit interpretation to node N, using the type for which a
6324 -- predefined operator exists. If the operator yields a boolean type,
6325 -- the Operand_Type is implicitly referenced by the operator, and a
6326 -- reference to it must be generated.
996ae0b0
RK
6327
6328 ---------------------------
6329 -- Add_Implicit_Operator --
6330 ---------------------------
6331
fbf5a39b
AC
6332 procedure Add_Implicit_Operator
6333 (T : Entity_Id;
6334 Op_Type : Entity_Id := Empty)
6335 is
996ae0b0
RK
6336 Predef_Op : Entity_Id;
6337
6338 begin
6339 Predef_Op := Current_Entity (Selector_Name (N));
6340
6341 while Present (Predef_Op)
6342 and then Scope (Predef_Op) /= Standard_Standard
6343 loop
6344 Predef_Op := Homonym (Predef_Op);
6345 end loop;
6346
6347 if Nkind (N) = N_Selected_Component then
6348 Change_Selected_Component_To_Expanded_Name (N);
6349 end if;
6350
719aaf4d
AC
6351 -- If the context is an unanalyzed function call, determine whether
6352 -- a binary or unary interpretation is required.
996ae0b0 6353
719aaf4d
AC
6354 if Nkind (Parent (N)) = N_Indexed_Component then
6355 declare
964f13da
RD
6356 Is_Binary_Call : constant Boolean :=
6357 Present
6358 (Next (First (Expressions (Parent (N)))));
6359 Is_Binary_Op : constant Boolean :=
6360 First_Entity
6361 (Predef_Op) /= Last_Entity (Predef_Op);
719aaf4d
AC
6362 Predef_Op2 : constant Entity_Id := Homonym (Predef_Op);
6363
6364 begin
6365 if Is_Binary_Call then
6366 if Is_Binary_Op then
6367 Add_One_Interp (N, Predef_Op, T);
6368 else
6369 Add_One_Interp (N, Predef_Op2, T);
6370 end if;
996ae0b0 6371
719aaf4d
AC
6372 else
6373 if not Is_Binary_Op then
6374 Add_One_Interp (N, Predef_Op, T);
6375 else
6376 Add_One_Interp (N, Predef_Op2, T);
6377 end if;
6378 end if;
6379 end;
6380
6381 else
6382 Add_One_Interp (N, Predef_Op, T);
6383
6384 -- For operators with unary and binary interpretations, if
6385 -- context is not a call, add both
6386
6387 if Present (Homonym (Predef_Op)) then
6388 Add_One_Interp (N, Homonym (Predef_Op), T);
6389 end if;
996ae0b0 6390 end if;
fbf5a39b
AC
6391
6392 -- The node is a reference to a predefined operator, and
6393 -- an implicit reference to the type of its operands.
6394
6395 if Present (Op_Type) then
6396 Generate_Operator_Reference (N, Op_Type);
6397 else
6398 Generate_Operator_Reference (N, T);
6399 end if;
996ae0b0
RK
6400 end Add_Implicit_Operator;
6401
6402 -- Start of processing for Has_Implicit_Operator
6403
6404 begin
996ae0b0
RK
6405 if Ekind (P) = E_Package
6406 and then not In_Open_Scopes (P)
6407 then
6408 Priv_Id := First_Private_Entity (P);
6409 end if;
6410
6411 Id := First_Entity (P);
6412
6413 case Op_Id is
6414
6415 -- Boolean operators: an implicit declaration exists if the scope
6416 -- contains a declaration for a derived Boolean type, or for an
6417 -- array of Boolean type.
6418
6419 when Name_Op_And | Name_Op_Not | Name_Op_Or | Name_Op_Xor =>
996ae0b0 6420 while Id /= Priv_Id loop
d347f572 6421 if Valid_Boolean_Arg (Id) and then Is_Base_Type (Id) then
996ae0b0
RK
6422 Add_Implicit_Operator (Id);
6423 return True;
6424 end if;
6425
6426 Next_Entity (Id);
6427 end loop;
6428
bc41faa2 6429 -- Equality: look for any non-limited type (result is Boolean)
996ae0b0
RK
6430
6431 when Name_Op_Eq | Name_Op_Ne =>
996ae0b0 6432 while Id /= Priv_Id loop
996ae0b0
RK
6433 if Is_Type (Id)
6434 and then not Is_Limited_Type (Id)
d347f572 6435 and then Is_Base_Type (Id)
996ae0b0 6436 then
fbf5a39b 6437 Add_Implicit_Operator (Standard_Boolean, Id);
996ae0b0
RK
6438 return True;
6439 end if;
6440
6441 Next_Entity (Id);
6442 end loop;
6443
bc41faa2 6444 -- Comparison operators: scalar type, or array of scalar
996ae0b0
RK
6445
6446 when Name_Op_Lt | Name_Op_Le | Name_Op_Gt | Name_Op_Ge =>
996ae0b0
RK
6447 while Id /= Priv_Id loop
6448 if (Is_Scalar_Type (Id)
d347f572
AC
6449 or else (Is_Array_Type (Id)
6450 and then Is_Scalar_Type (Component_Type (Id))))
6451 and then Is_Base_Type (Id)
996ae0b0 6452 then
fbf5a39b 6453 Add_Implicit_Operator (Standard_Boolean, Id);
996ae0b0
RK
6454 return True;
6455 end if;
6456
6457 Next_Entity (Id);
6458 end loop;
6459
6460 -- Arithmetic operators: any numeric type
6461
6462 when Name_Op_Abs |
6463 Name_Op_Add |
6464 Name_Op_Mod |
6465 Name_Op_Rem |
6466 Name_Op_Subtract |
6467 Name_Op_Multiply |
6468 Name_Op_Divide |
6469 Name_Op_Expon =>
996ae0b0 6470 while Id /= Priv_Id loop
d347f572 6471 if Is_Numeric_Type (Id) and then Is_Base_Type (Id) then
996ae0b0
RK
6472 Add_Implicit_Operator (Id);
6473 return True;
6474 end if;
6475
6476 Next_Entity (Id);
6477 end loop;
6478
6479 -- Concatenation: any one-dimensional array type
6480
6481 when Name_Op_Concat =>
996ae0b0 6482 while Id /= Priv_Id loop
d347f572
AC
6483 if Is_Array_Type (Id)
6484 and then Number_Dimensions (Id) = 1
6485 and then Is_Base_Type (Id)
996ae0b0
RK
6486 then
6487 Add_Implicit_Operator (Id);
6488 return True;
6489 end if;
6490
6491 Next_Entity (Id);
6492 end loop;
6493
6494 -- What is the others condition here? Should we be using a
6495 -- subtype of Name_Id that would restrict to operators ???
6496
6497 when others => null;
996ae0b0
RK
6498 end case;
6499
6500 -- If we fall through, then we do not have an implicit operator
6501
6502 return False;
6503
6504 end Has_Implicit_Operator;
6505
607d0635
AC
6506 -----------------------------------
6507 -- Has_Loop_In_Inner_Open_Scopes --
6508 -----------------------------------
6509
6510 function Has_Loop_In_Inner_Open_Scopes (S : Entity_Id) return Boolean is
6511 begin
6512 -- Several scope stacks are maintained by Scope_Stack. The base of the
6513 -- currently active scope stack is denoted by the Is_Active_Stack_Base
6514 -- flag in the scope stack entry. Note that the scope stacks used to
6515 -- simply be delimited implicitly by the presence of Standard_Standard
6516 -- at their base, but there now are cases where this is not sufficient
6517 -- because Standard_Standard actually may appear in the middle of the
6518 -- active set of scopes.
6519
6520 for J in reverse 0 .. Scope_Stack.Last loop
8d606a78
RD
6521
6522 -- S was reached without seing a loop scope first
6523
607d0635 6524 if Scope_Stack.Table (J).Entity = S then
607d0635 6525 return False;
8d606a78
RD
6526
6527 -- S was not yet reached, so it contains at least one inner loop
6528
607d0635 6529 elsif Ekind (Scope_Stack.Table (J).Entity) = E_Loop then
607d0635
AC
6530 return True;
6531 end if;
6532
6533 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
6534 -- cases where Standard_Standard appears in the middle of the active
6535 -- set of scopes. This affects the declaration and overriding of
6536 -- private inherited operations in instantiations of generic child
6537 -- units.
6538
6539 pragma Assert (not Scope_Stack.Table (J).Is_Active_Stack_Base);
6540 end loop;
6541
2010d078 6542 raise Program_Error; -- unreachable
607d0635
AC
6543 end Has_Loop_In_Inner_Open_Scopes;
6544
996ae0b0
RK
6545 --------------------
6546 -- In_Open_Scopes --
6547 --------------------
6548
6549 function In_Open_Scopes (S : Entity_Id) return Boolean is
6550 begin
923fa078
RD
6551 -- Several scope stacks are maintained by Scope_Stack. The base of the
6552 -- currently active scope stack is denoted by the Is_Active_Stack_Base
6553 -- flag in the scope stack entry. Note that the scope stacks used to
6554 -- simply be delimited implicitly by the presence of Standard_Standard
6555 -- at their base, but there now are cases where this is not sufficient
6556 -- because Standard_Standard actually may appear in the middle of the
6557 -- active set of scopes.
996ae0b0
RK
6558
6559 for J in reverse 0 .. Scope_Stack.Last loop
6560 if Scope_Stack.Table (J).Entity = S then
6561 return True;
6562 end if;
6563
923fa078
RD
6564 -- Check Is_Active_Stack_Base to tell us when to stop, as there are
6565 -- cases where Standard_Standard appears in the middle of the active
6566 -- set of scopes. This affects the declaration and overriding of
6567 -- private inherited operations in instantiations of generic child
6568 -- units.
996ae0b0
RK
6569
6570 exit when Scope_Stack.Table (J).Is_Active_Stack_Base;
6571 end loop;
6572
6573 return False;
6574 end In_Open_Scopes;
6575
6576 -----------------------------
6577 -- Inherit_Renamed_Profile --
6578 -----------------------------
6579
6580 procedure Inherit_Renamed_Profile (New_S : Entity_Id; Old_S : Entity_Id) is
6581 New_F : Entity_Id;
6582 Old_F : Entity_Id;
6583 Old_T : Entity_Id;
6584 New_T : Entity_Id;
6585
6586 begin
6587 if Ekind (Old_S) = E_Operator then
996ae0b0
RK
6588 New_F := First_Formal (New_S);
6589
6590 while Present (New_F) loop
6591 Set_Etype (New_F, Base_Type (Etype (New_F)));
6592 Next_Formal (New_F);
6593 end loop;
6594
6595 Set_Etype (New_S, Base_Type (Etype (New_S)));
6596
6597 else
6598 New_F := First_Formal (New_S);
6599 Old_F := First_Formal (Old_S);
6600
6601 while Present (New_F) loop
6602 New_T := Etype (New_F);
6603 Old_T := Etype (Old_F);
6604
6605 -- If the new type is a renaming of the old one, as is the
6606 -- case for actuals in instances, retain its name, to simplify
6607 -- later disambiguation.
6608
6609 if Nkind (Parent (New_T)) = N_Subtype_Declaration
6610 and then Is_Entity_Name (Subtype_Indication (Parent (New_T)))
6611 and then Entity (Subtype_Indication (Parent (New_T))) = Old_T
6612 then
6613 null;
6614 else
6615 Set_Etype (New_F, Old_T);
6616 end if;
6617
6618 Next_Formal (New_F);
6619 Next_Formal (Old_F);
6620 end loop;
6621
bce79204 6622 if Ekind_In (Old_S, E_Function, E_Enumeration_Literal) then
996ae0b0
RK
6623 Set_Etype (New_S, Etype (Old_S));
6624 end if;
6625 end if;
6626 end Inherit_Renamed_Profile;
6627
6628 ----------------
6629 -- Initialize --
6630 ----------------
6631
6632 procedure Initialize is
6633 begin
6634 Urefs.Init;
6635 end Initialize;
6636
6637 -------------------------
6638 -- Install_Use_Clauses --
6639 -------------------------
6640
0da2c8ac
AC
6641 procedure Install_Use_Clauses
6642 (Clause : Node_Id;
6643 Force_Installation : Boolean := False)
6644 is
16ca248a 6645 U : Node_Id;
996ae0b0
RK
6646 P : Node_Id;
6647 Id : Entity_Id;
6648
6649 begin
16ca248a 6650 U := Clause;
996ae0b0
RK
6651 while Present (U) loop
6652
6653 -- Case of USE package
6654
6655 if Nkind (U) = N_Use_Package_Clause then
6656 P := First (Names (U));
996ae0b0
RK
6657 while Present (P) loop
6658 Id := Entity (P);
6659
6660 if Ekind (Id) = E_Package then
996ae0b0 6661 if In_Use (Id) then
d4810530 6662 Note_Redundant_Use (P);
996ae0b0
RK
6663
6664 elsif Present (Renamed_Object (Id))
6665 and then In_Use (Renamed_Object (Id))
6666 then
d4810530 6667 Note_Redundant_Use (P);
996ae0b0 6668
0da2c8ac 6669 elsif Force_Installation or else Applicable_Use (P) then
996ae0b0 6670 Use_One_Package (Id, U);
0da2c8ac 6671
996ae0b0
RK
6672 end if;
6673 end if;
6674
6675 Next (P);
6676 end loop;
6677
16ca248a 6678 -- Case of USE TYPE
996ae0b0
RK
6679
6680 else
6681 P := First (Subtype_Marks (U));
996ae0b0 6682 while Present (P) loop
fbf5a39b
AC
6683 if not Is_Entity_Name (P)
6684 or else No (Entity (P))
6685 then
6686 null;
996ae0b0 6687
fbf5a39b 6688 elsif Entity (P) /= Any_Type then
07fc65c4 6689 Use_One_Type (P);
996ae0b0
RK
6690 end if;
6691
6692 Next (P);
6693 end loop;
6694 end if;
6695
6696 Next_Use_Clause (U);
6697 end loop;
6698 end Install_Use_Clauses;
6699
6700 -------------------------------------
6701 -- Is_Appropriate_For_Entry_Prefix --
6702 -------------------------------------
6703
6704 function Is_Appropriate_For_Entry_Prefix (T : Entity_Id) return Boolean is
6705 P_Type : Entity_Id := T;
6706
6707 begin
6708 if Is_Access_Type (P_Type) then
6709 P_Type := Designated_Type (P_Type);
6710 end if;
6711
6712 return Is_Task_Type (P_Type) or else Is_Protected_Type (P_Type);
6713 end Is_Appropriate_For_Entry_Prefix;
6714
6715 -------------------------------
6716 -- Is_Appropriate_For_Record --
6717 -------------------------------
6718
2e071734
AC
6719 function Is_Appropriate_For_Record (T : Entity_Id) return Boolean is
6720
996ae0b0
RK
6721 function Has_Components (T1 : Entity_Id) return Boolean;
6722 -- Determine if given type has components (i.e. is either a record
6723 -- type or a type that has discriminants).
6724
16ca248a
ES
6725 --------------------
6726 -- Has_Components --
6727 --------------------
6728
996ae0b0
RK
6729 function Has_Components (T1 : Entity_Id) return Boolean is
6730 begin
6731 return Is_Record_Type (T1)
6732 or else (Is_Private_Type (T1) and then Has_Discriminants (T1))
16ca248a
ES
6733 or else (Is_Task_Type (T1) and then Has_Discriminants (T1))
6734 or else (Is_Incomplete_Type (T1)
6735 and then From_With_Type (T1)
6736 and then Present (Non_Limited_View (T1))
11560bcc
TQ
6737 and then Is_Record_Type
6738 (Get_Full_View (Non_Limited_View (T1))));
996ae0b0
RK
6739 end Has_Components;
6740
6741 -- Start of processing for Is_Appropriate_For_Record
6742
6743 begin
6744 return
6745 Present (T)
6746 and then (Has_Components (T)
16ca248a
ES
6747 or else (Is_Access_Type (T)
6748 and then Has_Components (Designated_Type (T))));
996ae0b0
RK
6749 end Is_Appropriate_For_Record;
6750
d4810530
ES
6751 ------------------------
6752 -- Note_Redundant_Use --
6753 ------------------------
6754
6755 procedure Note_Redundant_Use (Clause : Node_Id) is
6756 Pack_Name : constant Entity_Id := Entity (Clause);
6757 Cur_Use : constant Node_Id := Current_Use_Clause (Pack_Name);
6758 Decl : constant Node_Id := Parent (Clause);
6759
6760 Prev_Use : Node_Id := Empty;
6761 Redundant : Node_Id := Empty;
f7ca1d04
AC
6762 -- The Use_Clause which is actually redundant. In the simplest case it
6763 -- is Pack itself, but when we compile a body we install its context
6764 -- before that of its spec, in which case it is the use_clause in the
6765 -- spec that will appear to be redundant, and we want the warning to be
6766 -- placed on the body. Similar complications appear when the redundancy
6767 -- is between a child unit and one of its ancestors.
d4810530
ES
6768
6769 begin
6770 Set_Redundant_Use (Clause, True);
6771
6772 if not Comes_From_Source (Clause)
6773 or else In_Instance
6774 or else not Warn_On_Redundant_Constructs
6775 then
6776 return;
6777 end if;
6778
6779 if not Is_Compilation_Unit (Current_Scope) then
6780
f7ca1d04
AC
6781 -- If the use_clause is in an inner scope, it is made redundant by
6782 -- some clause in the current context, with one exception: If we're
6783 -- compiling a nested package body, and the use_clause comes from the
6784 -- corresponding spec, the clause is not necessarily fully redundant,
6785 -- so we should not warn. If a warning was warranted, it would have
6786 -- been given when the spec was processed.
cdc8c54c
BD
6787
6788 if Nkind (Parent (Decl)) = N_Package_Specification then
6789 declare
6790 Package_Spec_Entity : constant Entity_Id :=
6791 Defining_Unit_Name (Parent (Decl));
6792 begin
6793 if In_Package_Body (Package_Spec_Entity) then
6794 return;
6795 end if;
6796 end;
6797 end if;
d4810530
ES
6798
6799 Redundant := Clause;
6800 Prev_Use := Cur_Use;
6801
6802 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
6803 declare
6804 Cur_Unit : constant Unit_Number_Type := Get_Source_Unit (Cur_Use);
6805 New_Unit : constant Unit_Number_Type := Get_Source_Unit (Clause);
6806 Scop : Entity_Id;
6807
6808 begin
6809 if Cur_Unit = New_Unit then
6810
6811 -- Redundant clause in same body
6812
6813 Redundant := Clause;
6814 Prev_Use := Cur_Use;
6815
6816 elsif Cur_Unit = Current_Sem_Unit then
6817
6818 -- If the new clause is not in the current unit it has been
6819 -- analyzed first, and it makes the other one redundant.
6820 -- However, if the new clause appears in a subunit, Cur_Unit
6821 -- is still the parent, and in that case the redundant one
6822 -- is the one appearing in the subunit.
6823
6824 if Nkind (Unit (Cunit (New_Unit))) = N_Subunit then
6825 Redundant := Clause;
6826 Prev_Use := Cur_Use;
6827
6828 -- Most common case: redundant clause in body,
6829 -- original clause in spec. Current scope is spec entity.
6830
6831 elsif
6832 Current_Scope =
6833 Defining_Entity (
6834 Unit (Library_Unit (Cunit (Current_Sem_Unit))))
6835 then
6836 Redundant := Cur_Use;
6837 Prev_Use := Clause;
6838
6839 else
6840 -- The new clause may appear in an unrelated unit, when
6841 -- the parents of a generic are being installed prior to
6842 -- instantiation. In this case there must be no warning.
6843 -- We detect this case by checking whether the current top
6844 -- of the stack is related to the current compilation.
6845
6846 Scop := Current_Scope;
6847 while Present (Scop)
6848 and then Scop /= Standard_Standard
6849 loop
6850 if Is_Compilation_Unit (Scop)
6851 and then not Is_Child_Unit (Scop)
6852 then
6853 return;
6854
6855 elsif Scop = Cunit_Entity (Current_Sem_Unit) then
6856 exit;
6857 end if;
6858
6859 Scop := Scope (Scop);
6860 end loop;
6861
6862 Redundant := Cur_Use;
6863 Prev_Use := Clause;
6864 end if;
6865
6866 elsif New_Unit = Current_Sem_Unit then
6867 Redundant := Clause;
6868 Prev_Use := Cur_Use;
6869
6870 else
6871 -- Neither is the current unit, so they appear in parent or
6872 -- sibling units. Warning will be emitted elsewhere.
6873
6874 return;
6875 end if;
6876 end;
6877
6878 elsif Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
6879 and then Present (Parent_Spec (Unit (Cunit (Current_Sem_Unit))))
6880 then
f7ca1d04
AC
6881 -- Use_clause is in child unit of current unit, and the child unit
6882 -- appears in the context of the body of the parent, so it has been
6883 -- installed first, even though it is the redundant one. Depending on
6884 -- their placement in the context, the visible or the private parts
6885 -- of the two units, either might appear as redundant, but the
6886 -- message has to be on the current unit.
d4810530
ES
6887
6888 if Get_Source_Unit (Cur_Use) = Current_Sem_Unit then
6889 Redundant := Cur_Use;
6890 Prev_Use := Clause;
6891 else
6892 Redundant := Clause;
6893 Prev_Use := Cur_Use;
6894 end if;
6895
6896 -- If the new use clause appears in the private part of a parent unit
f3d57416 6897 -- it may appear to be redundant w.r.t. a use clause in a child unit,
d4810530
ES
6898 -- but the previous use clause was needed in the visible part of the
6899 -- child, and no warning should be emitted.
6900
6901 if Nkind (Parent (Decl)) = N_Package_Specification
6902 and then
6903 List_Containing (Decl) = Private_Declarations (Parent (Decl))
6904 then
6905 declare
6906 Par : constant Entity_Id := Defining_Entity (Parent (Decl));
6907 Spec : constant Node_Id :=
6908 Specification (Unit (Cunit (Current_Sem_Unit)));
6909
6910 begin
6911 if Is_Compilation_Unit (Par)
6912 and then Par /= Cunit_Entity (Current_Sem_Unit)
6913 and then Parent (Cur_Use) = Spec
6914 and then
6915 List_Containing (Cur_Use) = Visible_Declarations (Spec)
6916 then
6917 return;
6918 end if;
6919 end;
6920 end if;
6921
ff81221b
ES
6922 -- Finally, if the current use clause is in the context then
6923 -- the clause is redundant when it is nested within the unit.
6924
6925 elsif Nkind (Parent (Cur_Use)) = N_Compilation_Unit
6926 and then Nkind (Parent (Parent (Clause))) /= N_Compilation_Unit
6927 and then Get_Source_Unit (Cur_Use) = Get_Source_Unit (Clause)
6928 then
6929 Redundant := Clause;
6930 Prev_Use := Cur_Use;
6931
d4810530
ES
6932 else
6933 null;
6934 end if;
6935
6936 if Present (Redundant) then
6937 Error_Msg_Sloc := Sloc (Prev_Use);
ed2233dc 6938 Error_Msg_NE -- CODEFIX
954c111a
HK
6939 ("& is already use-visible through previous use clause #?",
6940 Redundant, Pack_Name);
d4810530
ES
6941 end if;
6942 end Note_Redundant_Use;
6943
996ae0b0
RK
6944 ---------------
6945 -- Pop_Scope --
6946 ---------------
6947
6948 procedure Pop_Scope is
fbf5a39b 6949 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
fab2daeb 6950 S : constant Entity_Id := SST.Entity;
996ae0b0
RK
6951
6952 begin
6953 if Debug_Flag_E then
6954 Write_Info;
6955 end if;
6956
fab2daeb
AC
6957 -- Set Default_Storage_Pool field of the library unit if necessary
6958
6959 if Ekind_In (S, E_Package, E_Generic_Package)
6960 and then
6961 Nkind (Parent (Unit_Declaration_Node (S))) = N_Compilation_Unit
6962 then
6963 declare
6964 Aux : constant Node_Id :=
4adf3c50 6965 Aux_Decls_Node (Parent (Unit_Declaration_Node (S)));
fab2daeb
AC
6966 begin
6967 if No (Default_Storage_Pool (Aux)) then
6968 Set_Default_Storage_Pool (Aux, Default_Pool);
6969 end if;
6970 end;
6971 end if;
6972
21d27997 6973 Scope_Suppress := SST.Save_Scope_Suppress;
11560bcc 6974 Local_Suppress_Stack_Top := SST.Save_Local_Suppress_Stack_Top;
21d27997 6975 Check_Policy_List := SST.Save_Check_Policy_List;
fab2daeb 6976 Default_Pool := SST.Save_Default_Storage_Pool;
996ae0b0
RK
6977
6978 if Debug_Flag_W then
fab2daeb 6979 Write_Str ("<-- exiting scope: ");
996ae0b0
RK
6980 Write_Name (Chars (Current_Scope));
6981 Write_Str (", Depth=");
6982 Write_Int (Int (Scope_Stack.Last));
6983 Write_Eol;
6984 end if;
6985
fbf5a39b 6986 End_Use_Clauses (SST.First_Use_Clause);
996ae0b0
RK
6987
6988 -- If the actions to be wrapped are still there they will get lost
6989 -- causing incomplete code to be generated. It is better to abort in
fbf5a39b
AC
6990 -- this case (and we do the abort even with assertions off since the
6991 -- penalty is incorrect code generation)
996ae0b0 6992
fbf5a39b
AC
6993 if SST.Actions_To_Be_Wrapped_Before /= No_List
6994 or else
6995 SST.Actions_To_Be_Wrapped_After /= No_List
6996 then
46202729 6997 raise Program_Error;
fbf5a39b 6998 end if;
996ae0b0
RK
6999
7000 -- Free last subprogram name if allocated, and pop scope
7001
fbf5a39b 7002 Free (SST.Last_Subprogram_Name);
996ae0b0
RK
7003 Scope_Stack.Decrement_Last;
7004 end Pop_Scope;
7005
fbe627af
RD
7006 ---------------
7007 -- Push_Scope --
7008 ---------------
7009
7010 procedure Push_Scope (S : Entity_Id) is
fab2daeb 7011 E : constant Entity_Id := Scope (S);
fbe627af
RD
7012
7013 begin
7014 if Ekind (S) = E_Void then
7015 null;
7016
f7ca1d04
AC
7017 -- Set scope depth if not a non-concurrent type, and we have not yet set
7018 -- the scope depth. This means that we have the first occurrence of the
7019 -- scope, and this is where the depth is set.
fbe627af
RD
7020
7021 elsif (not Is_Type (S) or else Is_Concurrent_Type (S))
7022 and then not Scope_Depth_Set (S)
7023 then
7024 if S = Standard_Standard then
7025 Set_Scope_Depth_Value (S, Uint_0);
7026
7027 elsif Is_Child_Unit (S) then
7028 Set_Scope_Depth_Value (S, Uint_1);
7029
7030 elsif not Is_Record_Type (Current_Scope) then
7031 if Ekind (S) = E_Loop then
7032 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope));
7033 else
7034 Set_Scope_Depth_Value (S, Scope_Depth (Current_Scope) + 1);
7035 end if;
7036 end if;
7037 end if;
7038
7039 Scope_Stack.Increment_Last;
7040
7041 declare
7042 SST : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
7043
7044 begin
11560bcc
TQ
7045 SST.Entity := S;
7046 SST.Save_Scope_Suppress := Scope_Suppress;
7047 SST.Save_Local_Suppress_Stack_Top := Local_Suppress_Stack_Top;
21d27997 7048 SST.Save_Check_Policy_List := Check_Policy_List;
fab2daeb 7049 SST.Save_Default_Storage_Pool := Default_Pool;
fbe627af
RD
7050
7051 if Scope_Stack.Last > Scope_Stack.First then
7052 SST.Component_Alignment_Default := Scope_Stack.Table
7053 (Scope_Stack.Last - 1).
7054 Component_Alignment_Default;
7055 end if;
7056
7057 SST.Last_Subprogram_Name := null;
7058 SST.Is_Transient := False;
7059 SST.Node_To_Be_Wrapped := Empty;
7060 SST.Pending_Freeze_Actions := No_List;
7061 SST.Actions_To_Be_Wrapped_Before := No_List;
7062 SST.Actions_To_Be_Wrapped_After := No_List;
7063 SST.First_Use_Clause := Empty;
7064 SST.Is_Active_Stack_Base := False;
7065 SST.Previous_Visibility := False;
7066 end;
7067
7068 if Debug_Flag_W then
7069 Write_Str ("--> new scope: ");
7070 Write_Name (Chars (Current_Scope));
7071 Write_Str (", Id=");
7072 Write_Int (Int (Current_Scope));
7073 Write_Str (", Depth=");
7074 Write_Int (Int (Scope_Stack.Last));
7075 Write_Eol;
7076 end if;
7077
f7ca1d04
AC
7078 -- Deal with copying flags from the previous scope to this one. This is
7079 -- not necessary if either scope is standard, or if the new scope is a
7080 -- child unit.
fbe627af
RD
7081
7082 if S /= Standard_Standard
7083 and then Scope (S) /= Standard_Standard
7084 and then not Is_Child_Unit (S)
7085 then
fbe627af
RD
7086 if Nkind (E) not in N_Entity then
7087 return;
7088 end if;
7089
7090 -- Copy categorization flags from Scope (S) to S, this is not done
7091 -- when Scope (S) is Standard_Standard since propagation is from
7092 -- library unit entity inwards. Copy other relevant attributes as
7093 -- well (Discard_Names in particular).
7094
7095 -- We only propagate inwards for library level entities,
7096 -- inner level subprograms do not inherit the categorization.
7097
7098 if Is_Library_Level_Entity (S) then
7099 Set_Is_Preelaborated (S, Is_Preelaborated (E));
7100 Set_Is_Shared_Passive (S, Is_Shared_Passive (E));
7101 Set_Discard_Names (S, Discard_Names (E));
7102 Set_Suppress_Value_Tracking_On_Call
7103 (S, Suppress_Value_Tracking_On_Call (E));
7104 Set_Categorization_From_Scope (E => S, Scop => E);
7105 end if;
7106 end if;
fab2daeb
AC
7107
7108 if Is_Child_Unit (S)
7109 and then Present (E)
7110 and then Ekind_In (E, E_Package, E_Generic_Package)
7111 and then
7112 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
7113 then
7114 declare
7115 Aux : constant Node_Id :=
4adf3c50 7116 Aux_Decls_Node (Parent (Unit_Declaration_Node (E)));
fab2daeb
AC
7117 begin
7118 if Present (Default_Storage_Pool (Aux)) then
7119 Default_Pool := Default_Storage_Pool (Aux);
7120 end if;
7121 end;
7122 end if;
fbe627af
RD
7123 end Push_Scope;
7124
996ae0b0
RK
7125 ---------------------
7126 -- Premature_Usage --
7127 ---------------------
7128
7129 procedure Premature_Usage (N : Node_Id) is
fbf5a39b 7130 Kind : constant Node_Kind := Nkind (Parent (Entity (N)));
996ae0b0
RK
7131 E : Entity_Id := Entity (N);
7132
7133 begin
7134 -- Within an instance, the analysis of the actual for a formal object
16ca248a
ES
7135 -- does not see the name of the object itself. This is significant only
7136 -- if the object is an aggregate, where its analysis does not do any
7137 -- name resolution on component associations. (see 4717-008). In such a
7138 -- case, look for the visible homonym on the chain.
996ae0b0
RK
7139
7140 if In_Instance
7141 and then Present (Homonym (E))
7142 then
7143 E := Homonym (E);
7144
7145 while Present (E)
7146 and then not In_Open_Scopes (Scope (E))
7147 loop
7148 E := Homonym (E);
7149 end loop;
7150
7151 if Present (E) then
7152 Set_Entity (N, E);
7153 Set_Etype (N, Etype (E));
7154 return;
7155 end if;
7156 end if;
7157
7158 if Kind = N_Component_Declaration then
7159 Error_Msg_N
7160 ("component&! cannot be used before end of record declaration", N);
7161
7162 elsif Kind = N_Parameter_Specification then
7163 Error_Msg_N
7164 ("formal parameter&! cannot be used before end of specification",
7165 N);
7166
7167 elsif Kind = N_Discriminant_Specification then
7168 Error_Msg_N
7169 ("discriminant&! cannot be used before end of discriminant part",
7170 N);
7171
7172 elsif Kind = N_Procedure_Specification
7173 or else Kind = N_Function_Specification
7174 then
7175 Error_Msg_N
7176 ("subprogram&! cannot be used before end of its declaration",
7177 N);
90067a15
ES
7178
7179 elsif Kind = N_Full_Type_Declaration then
7180 Error_Msg_N
7181 ("type& cannot be used before end of its declaration!", N);
7182
996ae0b0
RK
7183 else
7184 Error_Msg_N
7185 ("object& cannot be used before end of its declaration!", N);
7186 end if;
7187 end Premature_Usage;
7188
7189 ------------------------
7190 -- Present_System_Aux --
7191 ------------------------
7192
7193 function Present_System_Aux (N : Node_Id := Empty) return Boolean is
7194 Loc : Source_Ptr;
fbe627af 7195 Aux_Name : Unit_Name_Type;
996ae0b0
RK
7196 Unum : Unit_Number_Type;
7197 Withn : Node_Id;
7198 With_Sys : Node_Id;
7199 The_Unit : Node_Id;
7200
7201 function Find_System (C_Unit : Node_Id) return Entity_Id;
16ca248a 7202 -- Scan context clause of compilation unit to find with_clause
996ae0b0
RK
7203 -- for System.
7204
2e071734
AC
7205 -----------------
7206 -- Find_System --
7207 -----------------
7208
996ae0b0
RK
7209 function Find_System (C_Unit : Node_Id) return Entity_Id is
7210 With_Clause : Node_Id;
7211
7212 begin
7213 With_Clause := First (Context_Items (C_Unit));
996ae0b0
RK
7214 while Present (With_Clause) loop
7215 if (Nkind (With_Clause) = N_With_Clause
7216 and then Chars (Name (With_Clause)) = Name_System)
7217 and then Comes_From_Source (With_Clause)
7218 then
7219 return With_Clause;
7220 end if;
7221
7222 Next (With_Clause);
7223 end loop;
7224
7225 return Empty;
7226 end Find_System;
7227
7228 -- Start of processing for Present_System_Aux
7229
7230 begin
bc41faa2 7231 -- The child unit may have been loaded and analyzed already
996ae0b0
RK
7232
7233 if Present (System_Aux_Id) then
7234 return True;
7235
7236 -- If no previous pragma for System.Aux, nothing to load
7237
fbf5a39b 7238 elsif No (System_Extend_Unit) then
996ae0b0
RK
7239 return False;
7240
7241 -- Use the unit name given in the pragma to retrieve the unit.
7242 -- Verify that System itself appears in the context clause of the
7243 -- current compilation. If System is not present, an error will
7244 -- have been reported already.
7245
7246 else
7247 With_Sys := Find_System (Cunit (Current_Sem_Unit));
7248
7249 The_Unit := Unit (Cunit (Current_Sem_Unit));
7250
7251 if No (With_Sys)
294ccb21
RD
7252 and then
7253 (Nkind (The_Unit) = N_Package_Body
7254 or else (Nkind (The_Unit) = N_Subprogram_Body
7255 and then
7256 not Acts_As_Spec (Cunit (Current_Sem_Unit))))
996ae0b0
RK
7257 then
7258 With_Sys := Find_System (Library_Unit (Cunit (Current_Sem_Unit)));
7259 end if;
7260
7261 if No (With_Sys)
7262 and then Present (N)
7263 then
7264 -- If we are compiling a subunit, we need to examine its
7265 -- context as well (Current_Sem_Unit is the parent unit);
7266
7267 The_Unit := Parent (N);
996ae0b0
RK
7268 while Nkind (The_Unit) /= N_Compilation_Unit loop
7269 The_Unit := Parent (The_Unit);
7270 end loop;
7271
7272 if Nkind (Unit (The_Unit)) = N_Subunit then
7273 With_Sys := Find_System (The_Unit);
7274 end if;
7275 end if;
7276
7277 if No (With_Sys) then
7278 return False;
7279 end if;
7280
7281 Loc := Sloc (With_Sys);
fbf5a39b 7282 Get_Name_String (Chars (Expression (System_Extend_Unit)));
996ae0b0
RK
7283 Name_Buffer (8 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
7284 Name_Buffer (1 .. 7) := "system.";
7285 Name_Buffer (Name_Len + 8) := '%';
7286 Name_Buffer (Name_Len + 9) := 's';
7287 Name_Len := Name_Len + 9;
7288 Aux_Name := Name_Find;
7289
7290 Unum :=
7291 Load_Unit
7292 (Load_Name => Aux_Name,
7293 Required => False,
7294 Subunit => False,
7295 Error_Node => With_Sys);
7296
7297 if Unum /= No_Unit then
7298 Semantics (Cunit (Unum));
7299 System_Aux_Id :=
7300 Defining_Entity (Specification (Unit (Cunit (Unum))));
7301
16ca248a
ES
7302 Withn :=
7303 Make_With_Clause (Loc,
7304 Name =>
7305 Make_Expanded_Name (Loc,
7306 Chars => Chars (System_Aux_Id),
7307 Prefix => New_Reference_To (Scope (System_Aux_Id), Loc),
7308 Selector_Name => New_Reference_To (System_Aux_Id, Loc)));
996ae0b0
RK
7309
7310 Set_Entity (Name (Withn), System_Aux_Id);
7311
16ca248a
ES
7312 Set_Library_Unit (Withn, Cunit (Unum));
7313 Set_Corresponding_Spec (Withn, System_Aux_Id);
7314 Set_First_Name (Withn, True);
7315 Set_Implicit_With (Withn, True);
996ae0b0
RK
7316
7317 Insert_After (With_Sys, Withn);
7318 Mark_Rewrite_Insertion (Withn);
7319 Set_Context_Installed (Withn);
7320
7321 return True;
7322
7323 -- Here if unit load failed
7324
7325 else
7326 Error_Msg_Name_1 := Name_System;
fbf5a39b 7327 Error_Msg_Name_2 := Chars (Expression (System_Extend_Unit));
996ae0b0
RK
7328 Error_Msg_N
7329 ("extension package `%.%` does not exist",
fbf5a39b 7330 Opt.System_Extend_Unit);
996ae0b0
RK
7331 return False;
7332 end if;
7333 end if;
7334 end Present_System_Aux;
7335
7336 -------------------------
7337 -- Restore_Scope_Stack --
7338 -------------------------
7339
ecad994d 7340 procedure Restore_Scope_Stack (Handle_Use : Boolean := True) is
996ae0b0
RK
7341 E : Entity_Id;
7342 S : Entity_Id;
7343 Comp_Unit : Node_Id;
7344 In_Child : Boolean := False;
7345 Full_Vis : Boolean := True;
fbf5a39b 7346 SS_Last : constant Int := Scope_Stack.Last;
996ae0b0
RK
7347
7348 begin
bc41faa2 7349 -- Restore visibility of previous scope stack, if any
996ae0b0
RK
7350
7351 for J in reverse 0 .. Scope_Stack.Last loop
7352 exit when Scope_Stack.Table (J).Entity = Standard_Standard
7353 or else No (Scope_Stack.Table (J).Entity);
7354
7355 S := Scope_Stack.Table (J).Entity;
7356
7357 if not Is_Hidden_Open_Scope (S) then
7358
7359 -- If the parent scope is hidden, its entities are hidden as
7360 -- well, unless the entity is the instantiation currently
7361 -- being analyzed.
7362
7363 if not Is_Hidden_Open_Scope (Scope (S))
7364 or else not Analyzed (Parent (S))
7365 or else Scope (S) = Standard_Standard
7366 then
7367 Set_Is_Immediately_Visible (S, True);
7368 end if;
7369
7370 E := First_Entity (S);
996ae0b0
RK
7371 while Present (E) loop
7372 if Is_Child_Unit (E) then
a523b302
JM
7373 if not From_With_Type (E) then
7374 Set_Is_Immediately_Visible (E,
7375 Is_Visible_Child_Unit (E) or else In_Open_Scopes (E));
f7ca1d04 7376
a523b302
JM
7377 else
7378 pragma Assert
7379 (Nkind (Parent (E)) = N_Defining_Program_Unit_Name
7380 and then
7381 Nkind (Parent (Parent (E))) = N_Package_Specification);
7382 Set_Is_Immediately_Visible (E,
7383 Limited_View_Installed (Parent (Parent (E))));
7384 end if;
996ae0b0
RK
7385 else
7386 Set_Is_Immediately_Visible (E, True);
7387 end if;
7388
7389 Next_Entity (E);
7390
8dbd1460
AC
7391 if not Full_Vis
7392 and then Is_Package_Or_Generic_Package (S)
7393 then
7394 -- We are in the visible part of the package scope
7395
996ae0b0
RK
7396 exit when E = First_Private_Entity (S);
7397 end if;
7398 end loop;
7399
7400 -- The visibility of child units (siblings of current compilation)
7401 -- must be restored in any case. Their declarations may appear
7402 -- after the private part of the parent.
7403
16ca248a 7404 if not Full_Vis then
996ae0b0
RK
7405 while Present (E) loop
7406 if Is_Child_Unit (E) then
7407 Set_Is_Immediately_Visible (E,
7408 Is_Visible_Child_Unit (E) or else In_Open_Scopes (E));
7409 end if;
7410
7411 Next_Entity (E);
7412 end loop;
7413 end if;
7414 end if;
7415
7416 if Is_Child_Unit (S)
fbe627af 7417 and not In_Child -- check only for current unit
996ae0b0
RK
7418 then
7419 In_Child := True;
7420
fbe627af 7421 -- Restore visibility of parents according to whether the child
996ae0b0
RK
7422 -- is private and whether we are in its visible part.
7423
7424 Comp_Unit := Parent (Unit_Declaration_Node (S));
7425
7426 if Nkind (Comp_Unit) = N_Compilation_Unit
7427 and then Private_Present (Comp_Unit)
7428 then
7429 Full_Vis := True;
7430
b9b2405f 7431 elsif Is_Package_Or_Generic_Package (S)
d7ba4df4 7432 and then (In_Private_Part (S) or else In_Package_Body (S))
996ae0b0
RK
7433 then
7434 Full_Vis := True;
7435
7d823354
ES
7436 -- if S is the scope of some instance (which has already been
7437 -- seen on the stack) it does not affect the visibility of
7438 -- other scopes.
7439
7440 elsif Is_Hidden_Open_Scope (S) then
7441 null;
7442
996ae0b0
RK
7443 elsif (Ekind (S) = E_Procedure
7444 or else Ekind (S) = E_Function)
7445 and then Has_Completion (S)
7446 then
7447 Full_Vis := True;
7448 else
7449 Full_Vis := False;
7450 end if;
7451 else
7452 Full_Vis := True;
7453 end if;
7454 end loop;
fbf5a39b
AC
7455
7456 if SS_Last >= Scope_Stack.First
7457 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
ecad994d 7458 and then Handle_Use
fbf5a39b
AC
7459 then
7460 Install_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
7461 end if;
996ae0b0
RK
7462 end Restore_Scope_Stack;
7463
7464 ----------------------
7465 -- Save_Scope_Stack --
7466 ----------------------
7467
ecad994d 7468 procedure Save_Scope_Stack (Handle_Use : Boolean := True) is
996ae0b0
RK
7469 E : Entity_Id;
7470 S : Entity_Id;
7471 SS_Last : constant Int := Scope_Stack.Last;
7472
7473 begin
7474 if SS_Last >= Scope_Stack.First
7475 and then Scope_Stack.Table (SS_Last).Entity /= Standard_Standard
7476 then
ecad994d
AC
7477 if Handle_Use then
7478 End_Use_Clauses (Scope_Stack.Table (SS_Last).First_Use_Clause);
7479 end if;
fbf5a39b 7480
16ca248a
ES
7481 -- If the call is from within a compilation unit, as when called from
7482 -- Rtsfind, make current entries in scope stack invisible while we
7483 -- analyze the new unit.
996ae0b0
RK
7484
7485 for J in reverse 0 .. SS_Last loop
7486 exit when Scope_Stack.Table (J).Entity = Standard_Standard
7487 or else No (Scope_Stack.Table (J).Entity);
7488
7489 S := Scope_Stack.Table (J).Entity;
7490 Set_Is_Immediately_Visible (S, False);
996ae0b0 7491
16ca248a 7492 E := First_Entity (S);
996ae0b0
RK
7493 while Present (E) loop
7494 Set_Is_Immediately_Visible (E, False);
7495 Next_Entity (E);
7496 end loop;
7497 end loop;
7498
7499 end if;
7500 end Save_Scope_Stack;
7501
7502 -------------
7503 -- Set_Use --
7504 -------------
7505
7506 procedure Set_Use (L : List_Id) is
7507 Decl : Node_Id;
7508 Pack_Name : Node_Id;
7509 Pack : Entity_Id;
7510 Id : Entity_Id;
7511
7512 begin
7513 if Present (L) then
7514 Decl := First (L);
996ae0b0
RK
7515 while Present (Decl) loop
7516 if Nkind (Decl) = N_Use_Package_Clause then
7517 Chain_Use_Clause (Decl);
996ae0b0 7518
16ca248a 7519 Pack_Name := First (Names (Decl));
996ae0b0
RK
7520 while Present (Pack_Name) loop
7521 Pack := Entity (Pack_Name);
7522
7523 if Ekind (Pack) = E_Package
7524 and then Applicable_Use (Pack_Name)
7525 then
7526 Use_One_Package (Pack, Decl);
7527 end if;
7528
7529 Next (Pack_Name);
7530 end loop;
7531
7532 elsif Nkind (Decl) = N_Use_Type_Clause then
7533 Chain_Use_Clause (Decl);
996ae0b0 7534
16ca248a 7535 Id := First (Subtype_Marks (Decl));
996ae0b0
RK
7536 while Present (Id) loop
7537 if Entity (Id) /= Any_Type then
07fc65c4 7538 Use_One_Type (Id);
996ae0b0
RK
7539 end if;
7540
7541 Next (Id);
7542 end loop;
7543 end if;
7544
7545 Next (Decl);
7546 end loop;
7547 end if;
7548 end Set_Use;
7549
7550 ---------------------
7551 -- Use_One_Package --
7552 ---------------------
7553
7554 procedure Use_One_Package (P : Entity_Id; N : Node_Id) is
7555 Id : Entity_Id;
7556 Prev : Entity_Id;
7557 Current_Instance : Entity_Id := Empty;
7558 Real_P : Entity_Id;
9bc856dd 7559 Private_With_OK : Boolean := False;
996ae0b0
RK
7560
7561 begin
7562 if Ekind (P) /= E_Package then
7563 return;
7564 end if;
7565
7566 Set_In_Use (P);
d4810530 7567 Set_Current_Use_Clause (P, N);
996ae0b0 7568
0ab80019 7569 -- Ada 2005 (AI-50217): Check restriction
19f0526a 7570
996ae0b0 7571 if From_With_Type (P) then
657a9dd9 7572 Error_Msg_N ("limited withed package cannot appear in use clause", N);
996ae0b0
RK
7573 end if;
7574
bc41faa2 7575 -- Find enclosing instance, if any
996ae0b0
RK
7576
7577 if In_Instance then
7578 Current_Instance := Current_Scope;
996ae0b0
RK
7579 while not Is_Generic_Instance (Current_Instance) loop
7580 Current_Instance := Scope (Current_Instance);
7581 end loop;
7582
7583 if No (Hidden_By_Use_Clause (N)) then
7584 Set_Hidden_By_Use_Clause (N, New_Elmt_List);
7585 end if;
7586 end if;
7587
7588 -- If unit is a package renaming, indicate that the renamed
7589 -- package is also in use (the flags on both entities must
7590 -- remain consistent, and a subsequent use of either of them
7591 -- should be recognized as redundant).
7592
7593 if Present (Renamed_Object (P)) then
7594 Set_In_Use (Renamed_Object (P));
d4810530 7595 Set_Current_Use_Clause (Renamed_Object (P), N);
996ae0b0
RK
7596 Real_P := Renamed_Object (P);
7597 else
7598 Real_P := P;
7599 end if;
7600
0ab80019 7601 -- Ada 2005 (AI-262): Check the use_clause of a private withed package
9bc856dd
AC
7602 -- found in the private part of a package specification
7603
7604 if In_Private_Part (Current_Scope)
7605 and then Has_Private_With (P)
7606 and then Is_Child_Unit (Current_Scope)
7607 and then Is_Child_Unit (P)
7608 and then Is_Ancestor_Package (Scope (Current_Scope), P)
7609 then
7610 Private_With_OK := True;
7611 end if;
7612
996ae0b0
RK
7613 -- Loop through entities in one package making them potentially
7614 -- use-visible.
7615
7616 Id := First_Entity (P);
7617 while Present (Id)
9bc856dd 7618 and then (Id /= First_Private_Entity (P)
0ab80019 7619 or else Private_With_OK) -- Ada 2005 (AI-262)
996ae0b0
RK
7620 loop
7621 Prev := Current_Entity (Id);
996ae0b0
RK
7622 while Present (Prev) loop
7623 if Is_Immediately_Visible (Prev)
7624 and then (not Is_Overloadable (Prev)
7625 or else not Is_Overloadable (Id)
7626 or else (Type_Conformant (Id, Prev)))
7627 then
7628 if No (Current_Instance) then
7629
7630 -- Potentially use-visible entity remains hidden
7631
7632 goto Next_Usable_Entity;
7633
16ca248a
ES
7634 -- A use clause within an instance hides outer global entities,
7635 -- which are not used to resolve local entities in the
7636 -- instance. Note that the predefined entities in Standard
7637 -- could not have been hidden in the generic by a use clause,
7638 -- and therefore remain visible. Other compilation units whose
7639 -- entities appear in Standard must be hidden in an instance.
996ae0b0
RK
7640
7641 -- To determine whether an entity is external to the instance
7642 -- we compare the scope depth of its scope with that of the
7643 -- current instance. However, a generic actual of a subprogram
7644 -- instance is declared in the wrapper package but will not be
e074d476
AC
7645 -- hidden by a use-visible entity. similarly, an entity that is
7646 -- declared in an enclosing instance will not be hidden by an
7647 -- an entity declared in a generic actual, which can only have
7648 -- been use-visible in the generic and will not have hidden the
7649 -- entity in the generic parent.
996ae0b0 7650
82c80734
RD
7651 -- If Id is called Standard, the predefined package with the
7652 -- same name is in the homonym chain. It has to be ignored
7653 -- because it has no defined scope (being the only entity in
7654 -- the system with this mandated behavior).
7655
996ae0b0 7656 elsif not Is_Hidden (Id)
82c80734 7657 and then Present (Scope (Prev))
996ae0b0
RK
7658 and then not Is_Wrapper_Package (Scope (Prev))
7659 and then Scope_Depth (Scope (Prev)) <
7660 Scope_Depth (Current_Instance)
7661 and then (Scope (Prev) /= Standard_Standard
7662 or else Sloc (Prev) > Standard_Location)
7663 then
e074d476
AC
7664 if In_Open_Scopes (Scope (Prev))
7665 and then Is_Generic_Instance (Scope (Prev))
c94a0b9d
AC
7666 and then Present (Associated_Formal_Package (P))
7667 then
7668 null;
7669
7670 else
7671 Set_Is_Potentially_Use_Visible (Id);
7672 Set_Is_Immediately_Visible (Prev, False);
7673 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
7674 end if;
996ae0b0
RK
7675 end if;
7676
16ca248a
ES
7677 -- A user-defined operator is not use-visible if the predefined
7678 -- operator for the type is immediately visible, which is the case
7679 -- if the type of the operand is in an open scope. This does not
7680 -- apply to user-defined operators that have operands of different
7681 -- types, because the predefined mixed mode operations (multiply
7682 -- and divide) apply to universal types and do not hide anything.
996ae0b0
RK
7683
7684 elsif Ekind (Prev) = E_Operator
7685 and then Operator_Matches_Spec (Prev, Id)
7686 and then In_Open_Scopes
7687 (Scope (Base_Type (Etype (First_Formal (Id)))))
7688 and then (No (Next_Formal (First_Formal (Id)))
7689 or else Etype (First_Formal (Id))
7690 = Etype (Next_Formal (First_Formal (Id)))
7691 or else Chars (Prev) = Name_Op_Expon)
7692 then
7693 goto Next_Usable_Entity;
e69614ad
AC
7694
7695 -- In an instance, two homonyms may become use_visible through the
7696 -- actuals of distinct formal packages. In the generic, only the
7697 -- current one would have been visible, so make the other one
7698 -- not use_visible.
7699
7700 elsif Present (Current_Instance)
7701 and then Is_Potentially_Use_Visible (Prev)
7702 and then not Is_Overloadable (Prev)
7703 and then Scope (Id) /= Scope (Prev)
7704 and then Used_As_Generic_Actual (Scope (Prev))
7705 and then Used_As_Generic_Actual (Scope (Id))
30196a76
RD
7706 and then not In_Same_List (Current_Use_Clause (Scope (Prev)),
7707 Current_Use_Clause (Scope (Id)))
e69614ad
AC
7708 then
7709 Set_Is_Potentially_Use_Visible (Prev, False);
7710 Append_Elmt (Prev, Hidden_By_Use_Clause (N));
996ae0b0
RK
7711 end if;
7712
7713 Prev := Homonym (Prev);
7714 end loop;
7715
bc41faa2 7716 -- On exit, we know entity is not hidden, unless it is private
996ae0b0
RK
7717
7718 if not Is_Hidden (Id)
7719 and then ((not Is_Child_Unit (Id))
7720 or else Is_Visible_Child_Unit (Id))
7721 then
7722 Set_Is_Potentially_Use_Visible (Id);
7723
7724 if Is_Private_Type (Id)
7725 and then Present (Full_View (Id))
7726 then
7727 Set_Is_Potentially_Use_Visible (Full_View (Id));
7728 end if;
7729 end if;
7730
7731 <<Next_Usable_Entity>>
7732 Next_Entity (Id);
7733 end loop;
7734
16ca248a
ES
7735 -- Child units are also made use-visible by a use clause, but they may
7736 -- appear after all visible declarations in the parent entity list.
996ae0b0
RK
7737
7738 while Present (Id) loop
996ae0b0
RK
7739 if Is_Child_Unit (Id)
7740 and then Is_Visible_Child_Unit (Id)
7741 then
7742 Set_Is_Potentially_Use_Visible (Id);
7743 end if;
7744
7745 Next_Entity (Id);
7746 end loop;
7747
7748 if Chars (Real_P) = Name_System
7749 and then Scope (Real_P) = Standard_Standard
7750 and then Present_System_Aux (N)
7751 then
7752 Use_One_Package (System_Aux_Id, N);
7753 end if;
7754
7755 end Use_One_Package;
7756
7757 ------------------
7758 -- Use_One_Type --
7759 ------------------
7760
7ff2d234 7761 procedure Use_One_Type (Id : Node_Id; Installed : Boolean := False) is
954c111a
HK
7762 Elmt : Elmt_Id;
7763 Is_Known_Used : Boolean;
7764 Op_List : Elist_Id;
7765 T : Entity_Id;
7766
7767 function Spec_Reloaded_For_Body return Boolean;
7768 -- Determine whether the compilation unit is a package body and the use
7769 -- type clause is in the spec of the same package. Even though the spec
7770 -- was analyzed first, its context is reloaded when analysing the body.
7771
29efbb8c
ES
7772 procedure Use_Class_Wide_Operations (Typ : Entity_Id);
7773 -- AI05-150: if the use_type_clause carries the "all" qualifier,
7774 -- class-wide operations of ancestor types are use-visible if the
7775 -- ancestor type is visible.
7776
954c111a
HK
7777 ----------------------------
7778 -- Spec_Reloaded_For_Body --
7779 ----------------------------
7780
7781 function Spec_Reloaded_For_Body return Boolean is
7782 begin
7783 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Body then
7784 declare
7785 Spec : constant Node_Id :=
7786 Parent (List_Containing (Parent (Id)));
7787 begin
7788 return
7789 Nkind (Spec) = N_Package_Specification
7790 and then Corresponding_Body (Parent (Spec)) =
7791 Cunit_Entity (Current_Sem_Unit);
7792 end;
7793 end if;
7794
7795 return False;
7796 end Spec_Reloaded_For_Body;
7797
29efbb8c
ES
7798 -------------------------------
7799 -- Use_Class_Wide_Operations --
7800 -------------------------------
7801
7802 procedure Use_Class_Wide_Operations (Typ : Entity_Id) is
7803 Scop : Entity_Id;
7804 Ent : Entity_Id;
7805
7806 function Is_Class_Wide_Operation_Of
7807 (Op : Entity_Id;
7808 T : Entity_Id) return Boolean;
7809 -- Determine whether a subprogram has a class-wide parameter or
7810 -- result that is T'Class.
7811
7812 ---------------------------------
7813 -- Is_Class_Wide_Operation_Of --
7814 ---------------------------------
7815
7816 function Is_Class_Wide_Operation_Of
7817 (Op : Entity_Id;
7818 T : Entity_Id) return Boolean
7819 is
7820 Formal : Entity_Id;
7821
7822 begin
7823 Formal := First_Formal (Op);
7824 while Present (Formal) loop
7825 if Etype (Formal) = Class_Wide_Type (T) then
7826 return True;
7827 end if;
7828 Next_Formal (Formal);
7829 end loop;
7830
7831 if Etype (Op) = Class_Wide_Type (T) then
7832 return True;
7833 end if;
7834
7835 return False;
7836 end Is_Class_Wide_Operation_Of;
7837
7838 -- Start of processing for Use_Class_Wide_Operations
7839
7840 begin
7841 Scop := Scope (Typ);
7842 if not Is_Hidden (Scop) then
7843 Ent := First_Entity (Scop);
7844 while Present (Ent) loop
7845 if Is_Overloadable (Ent)
7846 and then Is_Class_Wide_Operation_Of (Ent, Typ)
7847 and then not Is_Potentially_Use_Visible (Ent)
7848 then
7849 Set_Is_Potentially_Use_Visible (Ent);
7850 Append_Elmt (Ent, Used_Operations (Parent (Id)));
7851 end if;
7852
7853 Next_Entity (Ent);
7854 end loop;
7855 end if;
7856
7857 if Is_Derived_Type (Typ) then
7858 Use_Class_Wide_Operations (Etype (Base_Type (Typ)));
7859 end if;
7860 end Use_Class_Wide_Operations;
7861
954c111a 7862 -- Start of processing for Use_One_Type;
996ae0b0
RK
7863
7864 begin
7865 -- It is the type determined by the subtype mark (8.4(8)) whose
7866 -- operations become potentially use-visible.
7867
7868 T := Base_Type (Entity (Id));
7869
954c111a
HK
7870 -- Either the type itself is used, the package where it is declared
7871 -- is in use or the entity is declared in the current package, thus
7872 -- use-visible.
7873
7874 Is_Known_Used :=
7875 In_Use (T)
7876 or else In_Use (Scope (T))
7877 or else Scope (T) = Current_Scope;
7878
7879 Set_Redundant_Use (Id,
7880 Is_Known_Used or else Is_Potentially_Use_Visible (T));
996ae0b0 7881
ecc4ddde
AC
7882 if Ekind (T) = E_Incomplete_Type then
7883 Error_Msg_N ("premature usage of incomplete type", Id);
7884
7885 elsif In_Open_Scopes (Scope (T)) then
996ae0b0
RK
7886 null;
7887
f7ca1d04
AC
7888 -- A limited view cannot appear in a use_type clause. However, an access
7889 -- type whose designated type is limited has the flag but is not itself
7890 -- a limited view unless we only have a limited view of its enclosing
7891 -- package.
294ccb21
RD
7892
7893 elsif From_With_Type (T)
7894 and then From_With_Type (Scope (T))
7895 then
923fa078
RD
7896 Error_Msg_N
7897 ("incomplete type from limited view "
7898 & "cannot appear in use clause", Id);
7899
fbf5a39b
AC
7900 -- If the subtype mark designates a subtype in a different package,
7901 -- we have to check that the parent type is visible, otherwise the
7902 -- use type clause is a noop. Not clear how to do that???
7903
996ae0b0
RK
7904 elsif not Redundant_Use (Id) then
7905 Set_In_Use (T);
c3b36d48
AC
7906
7907 -- If T is tagged, primitive operators on class-wide operands
7908 -- are also available.
7909
7910 if Is_Tagged_Type (T) then
7911 Set_In_Use (Class_Wide_Type (T));
7912 end if;
7913
21d27997 7914 Set_Current_Use_Clause (T, Parent (Id));
996ae0b0 7915
29efbb8c
ES
7916 -- Iterate over primitive operations of the type. If an operation is
7917 -- already use_visible, it is the result of a previous use_clause,
7ff2d234
AC
7918 -- and already appears on the corresponding entity chain. If the
7919 -- clause is being reinstalled, operations are already use-visible.
29efbb8c 7920
7ff2d234
AC
7921 if Installed then
7922 null;
29efbb8c 7923
7ff2d234
AC
7924 else
7925 Op_List := Collect_Primitive_Operations (T);
7926 Elmt := First_Elmt (Op_List);
7927 while Present (Elmt) loop
7928 if (Nkind (Node (Elmt)) = N_Defining_Operator_Symbol
7929 or else Chars (Node (Elmt)) in Any_Operator_Name)
7930 and then not Is_Hidden (Node (Elmt))
7931 and then not Is_Potentially_Use_Visible (Node (Elmt))
7932 then
7933 Set_Is_Potentially_Use_Visible (Node (Elmt));
7934 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
996ae0b0 7935
7ff2d234
AC
7936 elsif Ada_Version >= Ada_2012
7937 and then All_Present (Parent (Id))
7938 and then not Is_Hidden (Node (Elmt))
7939 and then not Is_Potentially_Use_Visible (Node (Elmt))
7940 then
7941 Set_Is_Potentially_Use_Visible (Node (Elmt));
7942 Append_Elmt (Node (Elmt), Used_Operations (Parent (Id)));
7943 end if;
954c111a 7944
7ff2d234
AC
7945 Next_Elmt (Elmt);
7946 end loop;
7947 end if;
7948
7949 if Ada_Version >= Ada_2012
7950 and then All_Present (Parent (Id))
7951 and then Is_Tagged_Type (T)
7952 then
7953 Use_Class_Wide_Operations (T);
7954 end if;
29efbb8c
ES
7955 end if;
7956
954c111a
HK
7957 -- If warning on redundant constructs, check for unnecessary WITH
7958
7959 if Warn_On_Redundant_Constructs
7960 and then Is_Known_Used
7961
7962 -- with P; with P; use P;
7963 -- package P is package X is package body X is
7964 -- type T ... use P.T;
7965
7966 -- The compilation unit is the body of X. GNAT first compiles the
f3d57416 7967 -- spec of X, then proceeds to the body. At that point P is marked
954c111a
HK
7968 -- as use visible. The analysis then reinstalls the spec along with
7969 -- its context. The use clause P.T is now recognized as redundant,
7970 -- but in the wrong context. Do not emit a warning in such cases.
f7ca1d04
AC
7971 -- Do not emit a warning either if we are in an instance, there is
7972 -- no redundancy between an outer use_clause and one that appears
c56094bd 7973 -- within the generic.
954c111a
HK
7974
7975 and then not Spec_Reloaded_For_Body
c56094bd 7976 and then not In_Instance
954c111a
HK
7977 then
7978 -- The type already has a use clause
7979
7980 if In_Use (T) then
3ea52b2e
ES
7981
7982 -- Case where we know the current use clause for the type
7983
21d27997 7984 if Present (Current_Use_Clause (T)) then
44b90160 7985 Use_Clause_Known : declare
21d27997
RD
7986 Clause1 : constant Node_Id := Parent (Id);
7987 Clause2 : constant Node_Id := Current_Use_Clause (T);
3ea52b2e
ES
7988 Ent1 : Entity_Id;
7989 Ent2 : Entity_Id;
21d27997
RD
7990 Err_No : Node_Id;
7991 Unit1 : Node_Id;
7992 Unit2 : Node_Id;
7993
7ca139d3
TQ
7994 function Entity_Of_Unit (U : Node_Id) return Entity_Id;
7995 -- Return the appropriate entity for determining which unit
7996 -- has a deeper scope: the defining entity for U, unless U
7997 -- is a package instance, in which case we retrieve the
7998 -- entity of the instance spec.
7999
8000 --------------------
8001 -- Entity_Of_Unit --
8002 --------------------
8003
8004 function Entity_Of_Unit (U : Node_Id) return Entity_Id is
8005 begin
8006 if Nkind (U) = N_Package_Instantiation
8007 and then Analyzed (U)
8008 then
8009 return Defining_Entity (Instance_Spec (U));
8010 else
8011 return Defining_Entity (U);
8012 end if;
8013 end Entity_Of_Unit;
8014
44b90160
RD
8015 -- Start of processing for Use_Clause_Known
8016
21d27997 8017 begin
f7ca1d04
AC
8018 -- If both current use type clause and the use type clause
8019 -- for the type are at the compilation unit level, one of
8020 -- the units must be an ancestor of the other, and the
8021 -- warning belongs on the descendant.
3ea52b2e 8022
21d27997 8023 if Nkind (Parent (Clause1)) = N_Compilation_Unit
3ea52b2e
ES
8024 and then
8025 Nkind (Parent (Clause2)) = N_Compilation_Unit
21d27997 8026 then
b0d3b11d
AC
8027
8028 -- If the unit is a subprogram body that acts as spec,
8029 -- the context clause is shared with the constructed
8030 -- subprogram spec. Clearly there is no redundancy.
8031
8032 if Clause1 = Clause2 then
8033 return;
8034 end if;
8035
3ea52b2e
ES
8036 Unit1 := Unit (Parent (Clause1));
8037 Unit2 := Unit (Parent (Clause2));
8038
32beb1f3
AC
8039 -- If both clauses are on same unit, or one is the body
8040 -- of the other, or one of them is in a subunit, report
8041 -- redundancy on the later one.
f7ca1d04
AC
8042
8043 if Unit1 = Unit2 then
8044 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
ed2233dc 8045 Error_Msg_NE -- CODEFIX
f7ca1d04
AC
8046 ("& is already use-visible through previous "
8047 & "use_type_clause #?", Clause1, T);
8048 return;
32beb1f3
AC
8049
8050 elsif Nkind (Unit1) = N_Subunit then
8051 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
ed2233dc 8052 Error_Msg_NE -- CODEFIX
32beb1f3
AC
8053 ("& is already use-visible through previous "
8054 & "use_type_clause #?", Clause1, T);
8055 return;
8056
8057 elsif Nkind_In (Unit2, N_Package_Body, N_Subprogram_Body)
8058 and then Nkind (Unit1) /= Nkind (Unit2)
8059 and then Nkind (Unit1) /= N_Subunit
8060 then
8061 Error_Msg_Sloc := Sloc (Clause1);
ed2233dc 8062 Error_Msg_NE -- CODEFIX
32beb1f3
AC
8063 ("& is already use-visible through previous "
8064 & "use_type_clause #?", Current_Use_Clause (T), T);
8065 return;
f7ca1d04
AC
8066 end if;
8067
21d27997
RD
8068 -- There is a redundant use type clause in a child unit.
8069 -- Determine which of the units is more deeply nested.
3ea52b2e 8070 -- If a unit is a package instance, retrieve the entity
7ca139d3 8071 -- and its scope from the instance spec.
21d27997 8072
7ca139d3
TQ
8073 Ent1 := Entity_Of_Unit (Unit1);
8074 Ent2 := Entity_Of_Unit (Unit2);
3ea52b2e
ES
8075
8076 if Scope (Ent2) = Standard_Standard then
21d27997
RD
8077 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8078 Err_No := Clause1;
8079
3ea52b2e 8080 elsif Scope (Ent1) = Standard_Standard then
21d27997
RD
8081 Error_Msg_Sloc := Sloc (Id);
8082 Err_No := Clause2;
8083
7ca139d3
TQ
8084 -- If both units are child units, we determine which one
8085 -- is the descendant by the scope distance to the
3ea52b2e 8086 -- ultimate parent unit.
21d27997 8087
3ea52b2e 8088 else
21d27997
RD
8089 declare
8090 S1, S2 : Entity_Id;
8091
8092 begin
3ea52b2e
ES
8093 S1 := Scope (Ent1);
8094 S2 := Scope (Ent2);
c97c0163
AC
8095 while Present (S1)
8096 and then Present (S2)
8097 and then S1 /= Standard_Standard
8098 and then S2 /= Standard_Standard
21d27997
RD
8099 loop
8100 S1 := Scope (S1);
8101 S2 := Scope (S2);
8102 end loop;
8103
8104 if S1 = Standard_Standard then
8105 Error_Msg_Sloc := Sloc (Id);
8106 Err_No := Clause2;
8107 else
8108 Error_Msg_Sloc := Sloc (Current_Use_Clause (T));
8109 Err_No := Clause1;
8110 end if;
8111 end;
8112 end if;
8113
ed2233dc 8114 Error_Msg_NE -- CODEFIX
21d27997
RD
8115 ("& is already use-visible through previous "
8116 & "use_type_clause #?", Err_No, Id);
3ea52b2e
ES
8117
8118 -- Case where current use type clause and the use type
8119 -- clause for the type are not both at the compilation unit
8120 -- level. In this case we don't have location information.
8121
21d27997 8122 else
ed2233dc 8123 Error_Msg_NE -- CODEFIX
3ea52b2e 8124 ("& is already use-visible through previous "
37951d8e 8125 & "use type clause?", Id, T);
21d27997 8126 end if;
44b90160 8127 end Use_Clause_Known;
3ea52b2e
ES
8128
8129 -- Here if Current_Use_Clause is not set for T, another case
8130 -- where we do not have the location information available.
8131
21d27997 8132 else
ed2233dc 8133 Error_Msg_NE -- CODEFIX
3ea52b2e 8134 ("& is already use-visible through previous "
37951d8e 8135 & "use type clause?", Id, T);
21d27997 8136 end if;
954c111a
HK
8137
8138 -- The package where T is declared is already used
8139
8140 elsif In_Use (Scope (T)) then
8141 Error_Msg_Sloc := Sloc (Current_Use_Clause (Scope (T)));
ed2233dc 8142 Error_Msg_NE -- CODEFIX
954c111a 8143 ("& is already use-visible through package use clause #?",
37951d8e 8144 Id, T);
954c111a
HK
8145
8146 -- The current scope is the package where T is declared
8147
8148 else
8149 Error_Msg_Node_2 := Scope (T);
ed2233dc 8150 Error_Msg_NE -- CODEFIX
37951d8e 8151 ("& is already use-visible inside package &?", Id, T);
954c111a
HK
8152 end if;
8153 end if;
996ae0b0
RK
8154 end Use_One_Type;
8155
8156 ----------------
8157 -- Write_Info --
8158 ----------------
8159
8160 procedure Write_Info is
8161 Id : Entity_Id := First_Entity (Current_Scope);
8162
8163 begin
8164 -- No point in dumping standard entities
8165
8166 if Current_Scope = Standard_Standard then
8167 return;
8168 end if;
8169
8170 Write_Str ("========================================================");
8171 Write_Eol;
8172 Write_Str (" Defined Entities in ");
8173 Write_Name (Chars (Current_Scope));
8174 Write_Eol;
8175 Write_Str ("========================================================");
8176 Write_Eol;
8177
8178 if No (Id) then
8179 Write_Str ("-- none --");
8180 Write_Eol;
8181
8182 else
8183 while Present (Id) loop
8184 Write_Entity_Info (Id, " ");
8185 Next_Entity (Id);
8186 end loop;
8187 end if;
8188
8189 if Scope (Current_Scope) = Standard_Standard then
8190
8191 -- Print information on the current unit itself
8192
8193 Write_Entity_Info (Current_Scope, " ");
8194 end if;
8195
8196 Write_Eol;
8197 end Write_Info;
8198
67536dcb
RD
8199 --------
8200 -- ws --
8201 --------
996ae0b0 8202
67536dcb 8203 procedure ws is
996ae0b0 8204 S : Entity_Id;
996ae0b0
RK
8205 begin
8206 for J in reverse 1 .. Scope_Stack.Last loop
8207 S := Scope_Stack.Table (J).Entity;
8208 Write_Int (Int (S));
8209 Write_Str (" === ");
8210 Write_Name (Chars (S));
8211 Write_Eol;
8212 end loop;
67536dcb 8213 end ws;
996ae0b0
RK
8214
8215end Sem_Ch8;