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