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