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