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