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