| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S E M _ C H 1 1 -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, 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 Checks; use Checks; |
| 28 |
with Einfo; use Einfo; |
| 29 |
with Einfo.Entities; use Einfo.Entities; |
| 30 |
with Einfo.Utils; use Einfo.Utils; |
| 31 |
with Errout; use Errout; |
| 32 |
with Lib; use Lib; |
| 33 |
with Lib.Xref; use Lib.Xref; |
| 34 |
with Namet; use Namet; |
| 35 |
with Nlists; use Nlists; |
| 36 |
with Nmake; use Nmake; |
| 37 |
with Opt; use Opt; |
| 38 |
with Restrict; use Restrict; |
| 39 |
with Rident; use Rident; |
| 40 |
with Rtsfind; use Rtsfind; |
| 41 |
with Sem; use Sem; |
| 42 |
with Sem_Aux; use Sem_Aux; |
| 43 |
with Sem_Ch5; use Sem_Ch5; |
| 44 |
with Sem_Ch8; use Sem_Ch8; |
| 45 |
with Sem_Ch13; use Sem_Ch13; |
| 46 |
with Sem_Res; use Sem_Res; |
| 47 |
with Sem_Util; use Sem_Util; |
| 48 |
with Sem_Warn; use Sem_Warn; |
| 49 |
with Sinfo; use Sinfo; |
| 50 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 51 |
with Sinfo.Utils; use Sinfo.Utils; |
| 52 |
with Stand; use Stand; |
| 53 |
with Warnsw; use Warnsw; |
| 54 |
|
| 55 |
package body Sem_Ch11 is |
| 56 |
|
| 57 |
----------------------------------- |
| 58 |
-- Analyze_Exception_Declaration -- |
| 59 |
----------------------------------- |
| 60 |
|
| 61 |
procedure Analyze_Exception_Declaration (N : Node_Id) is |
| 62 |
Id : constant Entity_Id := Defining_Identifier (N); |
| 63 |
PF : constant Boolean := Is_Pure (Current_Scope); |
| 64 |
|
| 65 |
begin |
| 66 |
Generate_Definition (Id); |
| 67 |
Enter_Name (Id); |
| 68 |
Mutate_Ekind (Id, E_Exception); |
| 69 |
Set_Etype (Id, Standard_Exception_Type); |
| 70 |
Set_Is_Statically_Allocated (Id); |
| 71 |
Set_Is_Pure (Id, PF); |
| 72 |
|
| 73 |
Analyze_Aspect_Specifications (N, Id); |
| 74 |
end Analyze_Exception_Declaration; |
| 75 |
|
| 76 |
-------------------------------- |
| 77 |
-- Analyze_Exception_Handlers -- |
| 78 |
-------------------------------- |
| 79 |
|
| 80 |
procedure Analyze_Exception_Handlers (L : List_Id) is |
| 81 |
Handler : Node_Id; |
| 82 |
Choice : Entity_Id; |
| 83 |
Id : Node_Id; |
| 84 |
H_Scope : Entity_Id := Empty; |
| 85 |
|
| 86 |
procedure Check_Duplication (Id : Node_Id); |
| 87 |
-- Iterate through the identifiers in each handler to find duplicates |
| 88 |
|
| 89 |
function Others_Present return Boolean; |
| 90 |
-- Returns True if others handler is present |
| 91 |
|
| 92 |
----------------------- |
| 93 |
-- Check_Duplication -- |
| 94 |
----------------------- |
| 95 |
|
| 96 |
procedure Check_Duplication (Id : Node_Id) is |
| 97 |
Handler : Node_Id; |
| 98 |
Id1 : Node_Id; |
| 99 |
Id_Entity : Entity_Id := Entity (Id); |
| 100 |
|
| 101 |
begin |
| 102 |
if Present (Renamed_Entity (Id_Entity)) then |
| 103 |
Id_Entity := Renamed_Entity (Id_Entity); |
| 104 |
end if; |
| 105 |
|
| 106 |
Handler := First_Non_Pragma (L); |
| 107 |
while Present (Handler) loop |
| 108 |
Id1 := First (Exception_Choices (Handler)); |
| 109 |
while Present (Id1) loop |
| 110 |
|
| 111 |
-- Only check against the exception choices which precede |
| 112 |
-- Id in the handler, since the ones that follow Id have not |
| 113 |
-- been analyzed yet and will be checked in a subsequent call. |
| 114 |
|
| 115 |
if Id = Id1 then |
| 116 |
return; |
| 117 |
|
| 118 |
elsif Nkind (Id1) /= N_Others_Choice |
| 119 |
and then |
| 120 |
(Id_Entity = Entity (Id1) |
| 121 |
or else Id_Entity = Renamed_Entity (Entity (Id1))) |
| 122 |
then |
| 123 |
if Handler /= Parent (Id) then |
| 124 |
Error_Msg_Sloc := Sloc (Id1); |
| 125 |
Error_Msg_NE ("exception choice duplicates &#", Id, Id1); |
| 126 |
|
| 127 |
else |
| 128 |
if Ada_Version = Ada_83 |
| 129 |
and then Comes_From_Source (Id) |
| 130 |
then |
| 131 |
Error_Msg_N |
| 132 |
("(Ada 83) duplicate exception choice&", Id); |
| 133 |
end if; |
| 134 |
end if; |
| 135 |
end if; |
| 136 |
|
| 137 |
Next (Id1); |
| 138 |
end loop; |
| 139 |
|
| 140 |
Next_Non_Pragma (Handler); |
| 141 |
end loop; |
| 142 |
end Check_Duplication; |
| 143 |
|
| 144 |
-------------------- |
| 145 |
-- Others_Present -- |
| 146 |
-------------------- |
| 147 |
|
| 148 |
function Others_Present return Boolean is |
| 149 |
H : Node_Id; |
| 150 |
|
| 151 |
begin |
| 152 |
H := First_Non_Pragma (L); |
| 153 |
while Present (H) loop |
| 154 |
if Nkind (First (Exception_Choices (H))) = N_Others_Choice then |
| 155 |
return True; |
| 156 |
end if; |
| 157 |
|
| 158 |
Next_Non_Pragma (H); |
| 159 |
end loop; |
| 160 |
|
| 161 |
return False; |
| 162 |
end Others_Present; |
| 163 |
|
| 164 |
-- Start of processing for Analyze_Exception_Handlers |
| 165 |
|
| 166 |
begin |
| 167 |
Handler := First (L); |
| 168 |
|
| 169 |
-- Pragma Restriction_Warnings has more related semantics than pragma |
| 170 |
-- Restrictions in that it flags exception handlers as violators. Note |
| 171 |
-- that the compiler must still generate handlers for certain critical |
| 172 |
-- scenarios such as finalization. As a result, these handlers should |
| 173 |
-- not be subjected to the restriction check when in warnings mode. |
| 174 |
|
| 175 |
if not Comes_From_Source (Handler) |
| 176 |
and then (Restriction_Warnings (No_Exception_Handlers) |
| 177 |
or else Restriction_Warnings (No_Exception_Propagation) |
| 178 |
or else Restriction_Warnings (No_Exceptions)) |
| 179 |
then |
| 180 |
null; |
| 181 |
|
| 182 |
else |
| 183 |
Check_Restriction (No_Exceptions, Handler); |
| 184 |
Check_Restriction (No_Exception_Handlers, Handler); |
| 185 |
end if; |
| 186 |
|
| 187 |
-- Kill current remembered values, since we don't know where we were |
| 188 |
-- when the exception was raised. |
| 189 |
|
| 190 |
Kill_Current_Values; |
| 191 |
|
| 192 |
-- Loop through handlers (which can include pragmas) |
| 193 |
|
| 194 |
while Present (Handler) loop |
| 195 |
|
| 196 |
-- If pragma just analyze it |
| 197 |
|
| 198 |
if Nkind (Handler) = N_Pragma then |
| 199 |
Analyze (Handler); |
| 200 |
|
| 201 |
-- Otherwise we have a real exception handler |
| 202 |
|
| 203 |
else |
| 204 |
-- Deal with choice parameter. The exception handler is a |
| 205 |
-- declarative part for the choice parameter, so it constitutes a |
| 206 |
-- scope for visibility purposes. We create an entity to denote |
| 207 |
-- the whole exception part, and use it as the scope of all the |
| 208 |
-- choices, which may even have the same name without conflict. |
| 209 |
-- This scope plays no other role in expansion or code generation. |
| 210 |
|
| 211 |
Choice := Choice_Parameter (Handler); |
| 212 |
|
| 213 |
if Present (Choice) then |
| 214 |
Set_Local_Raise_Not_OK (Handler); |
| 215 |
|
| 216 |
if Comes_From_Source (Choice) then |
| 217 |
Check_Restriction (No_Exception_Propagation, Choice); |
| 218 |
Set_Debug_Info_Needed (Choice); |
| 219 |
end if; |
| 220 |
|
| 221 |
if No (H_Scope) then |
| 222 |
H_Scope := |
| 223 |
New_Internal_Entity |
| 224 |
(E_Block, Current_Scope, Sloc (Choice), 'E'); |
| 225 |
Set_Is_Exception_Handler (H_Scope); |
| 226 |
end if; |
| 227 |
|
| 228 |
Push_Scope (H_Scope); |
| 229 |
Set_Etype (H_Scope, Standard_Void_Type); |
| 230 |
|
| 231 |
Enter_Name (Choice); |
| 232 |
Mutate_Ekind (Choice, E_Variable); |
| 233 |
Set_Is_Not_Self_Hidden (Choice); |
| 234 |
|
| 235 |
if RTE_Available (RE_Exception_Occurrence) then |
| 236 |
Set_Etype (Choice, RTE (RE_Exception_Occurrence)); |
| 237 |
end if; |
| 238 |
|
| 239 |
Generate_Definition (Choice); |
| 240 |
|
| 241 |
-- Indicate that choice has an initial value, since in effect |
| 242 |
-- this field is assigned an initial value by the exception. |
| 243 |
-- We also consider that it is modified in the source. |
| 244 |
|
| 245 |
Set_Has_Initial_Value (Choice, True); |
| 246 |
Set_Never_Set_In_Source (Choice, False); |
| 247 |
end if; |
| 248 |
|
| 249 |
Id := First (Exception_Choices (Handler)); |
| 250 |
while Present (Id) loop |
| 251 |
if Nkind (Id) = N_Others_Choice then |
| 252 |
if Present (Next (Id)) |
| 253 |
or else Present (Next (Handler)) |
| 254 |
or else Present (Prev (Id)) |
| 255 |
then |
| 256 |
Error_Msg_N ("OTHERS must appear alone and last", Id); |
| 257 |
end if; |
| 258 |
|
| 259 |
else |
| 260 |
Analyze (Id); |
| 261 |
|
| 262 |
-- In most cases the choice has already been analyzed in |
| 263 |
-- Analyze_Handled_Statement_Sequence, in order to expand |
| 264 |
-- local handlers. This advance analysis does not take into |
| 265 |
-- account the case in which a choice has the same name as |
| 266 |
-- the choice parameter of the handler, which may hide an |
| 267 |
-- outer exception. This pathological case appears in ACATS |
| 268 |
-- B80001_3.adb, and requires an explicit check to verify |
| 269 |
-- that the id is not hidden. |
| 270 |
|
| 271 |
if not Is_Entity_Name (Id) |
| 272 |
or else Ekind (Entity (Id)) /= E_Exception |
| 273 |
or else |
| 274 |
(Nkind (Id) = N_Identifier |
| 275 |
and then Chars (Id) = Chars (Choice)) |
| 276 |
then |
| 277 |
Error_Msg_N ("exception name expected", Id); |
| 278 |
|
| 279 |
else |
| 280 |
-- Emit a warning at the declaration level when a local |
| 281 |
-- exception is never raised explicitly. |
| 282 |
|
| 283 |
if Warn_On_Redundant_Constructs |
| 284 |
and then not Is_Raised (Entity (Id)) |
| 285 |
and then Scope (Entity (Id)) = Current_Scope |
| 286 |
then |
| 287 |
Error_Msg_NE |
| 288 |
("exception & is never raised?r?", Entity (Id), Id); |
| 289 |
end if; |
| 290 |
|
| 291 |
if Present (Renamed_Entity (Entity (Id))) then |
| 292 |
if Entity (Id) = Standard_Numeric_Error then |
| 293 |
Check_Restriction (No_Obsolescent_Features, Id); |
| 294 |
|
| 295 |
if Warn_On_Obsolescent_Feature then |
| 296 |
Error_Msg_N |
| 297 |
("Numeric_Error is an " & |
| 298 |
"obsolescent feature (RM J.6(1))?j?", Id); |
| 299 |
Error_Msg_N |
| 300 |
("\use Constraint_Error instead?j?", Id); |
| 301 |
end if; |
| 302 |
end if; |
| 303 |
end if; |
| 304 |
|
| 305 |
Check_Duplication (Id); |
| 306 |
|
| 307 |
-- Check for exception declared within generic formal |
| 308 |
-- package (which is illegal, see RM 11.2(8)) |
| 309 |
|
| 310 |
declare |
| 311 |
Ent : Entity_Id := Entity (Id); |
| 312 |
Scop : Entity_Id; |
| 313 |
|
| 314 |
begin |
| 315 |
if Present (Renamed_Entity (Ent)) then |
| 316 |
Ent := Renamed_Entity (Ent); |
| 317 |
end if; |
| 318 |
|
| 319 |
Scop := Scope (Ent); |
| 320 |
while Scop /= Standard_Standard |
| 321 |
and then Ekind (Scop) = E_Package |
| 322 |
loop |
| 323 |
if Nkind (Declaration_Node (Scop)) = |
| 324 |
N_Package_Specification |
| 325 |
and then |
| 326 |
Nkind (Original_Node (Parent |
| 327 |
(Declaration_Node (Scop)))) = |
| 328 |
N_Formal_Package_Declaration |
| 329 |
then |
| 330 |
Error_Msg_NE |
| 331 |
("exception& is declared in generic formal " |
| 332 |
& "package", Id, Ent); |
| 333 |
Error_Msg_N |
| 334 |
("\and therefore cannot appear in handler " |
| 335 |
& "(RM 11.2(8))", Id); |
| 336 |
exit; |
| 337 |
|
| 338 |
-- If the exception is declared in an inner |
| 339 |
-- instance, nothing else to check. |
| 340 |
|
| 341 |
elsif Is_Generic_Instance (Scop) then |
| 342 |
exit; |
| 343 |
end if; |
| 344 |
|
| 345 |
Scop := Scope (Scop); |
| 346 |
end loop; |
| 347 |
end; |
| 348 |
end if; |
| 349 |
end if; |
| 350 |
|
| 351 |
Next (Id); |
| 352 |
end loop; |
| 353 |
|
| 354 |
-- Check for redundant handler (has only raise statement) and is |
| 355 |
-- either an others handler, or is a specific handler when no |
| 356 |
-- others handler is present. |
| 357 |
|
| 358 |
if Warn_On_Redundant_Constructs |
| 359 |
and then List_Length (Statements (Handler)) = 1 |
| 360 |
and then Nkind (First (Statements (Handler))) = N_Raise_Statement |
| 361 |
and then No (Name (First (Statements (Handler)))) |
| 362 |
and then (not Others_Present |
| 363 |
or else Nkind (First (Exception_Choices (Handler))) = |
| 364 |
N_Others_Choice) |
| 365 |
then |
| 366 |
Error_Msg_N |
| 367 |
("useless handler contains only a reraise statement?r?", |
| 368 |
Handler); |
| 369 |
end if; |
| 370 |
|
| 371 |
-- Now analyze the statements of this handler |
| 372 |
|
| 373 |
Analyze_Statements (Statements (Handler)); |
| 374 |
|
| 375 |
-- If a choice was present, we created a special scope for it, so |
| 376 |
-- this is where we pop that special scope to get rid of it. |
| 377 |
|
| 378 |
if Present (Choice) then |
| 379 |
End_Scope; |
| 380 |
end if; |
| 381 |
end if; |
| 382 |
|
| 383 |
Next (Handler); |
| 384 |
end loop; |
| 385 |
end Analyze_Exception_Handlers; |
| 386 |
|
| 387 |
-------------------------------- |
| 388 |
-- Analyze_Handled_Statements -- |
| 389 |
-------------------------------- |
| 390 |
|
| 391 |
procedure Analyze_Handled_Statements (N : Node_Id) is |
| 392 |
Handlers : constant List_Id := Exception_Handlers (N); |
| 393 |
Handler : Node_Id; |
| 394 |
Choice : Node_Id; |
| 395 |
|
| 396 |
begin |
| 397 |
if Present (Handlers) then |
| 398 |
Kill_All_Checks; |
| 399 |
end if; |
| 400 |
|
| 401 |
-- We are now going to analyze the statements and then the exception |
| 402 |
-- handlers. We certainly need to do things in this order to get the |
| 403 |
-- proper sequential semantics for various warnings. |
| 404 |
|
| 405 |
-- However, there is a glitch. When we process raise statements, an |
| 406 |
-- optimization is to look for local handlers and specialize the code |
| 407 |
-- in this case. |
| 408 |
|
| 409 |
-- In order to detect if a handler is matching, we must have at least |
| 410 |
-- analyzed the choices in the proper scope so that proper visibility |
| 411 |
-- analysis is performed. Hence we analyze just the choices first, |
| 412 |
-- before we analyze the statement sequence. |
| 413 |
|
| 414 |
Handler := First_Non_Pragma (Handlers); |
| 415 |
while Present (Handler) loop |
| 416 |
Choice := First_Non_Pragma (Exception_Choices (Handler)); |
| 417 |
while Present (Choice) loop |
| 418 |
Analyze (Choice); |
| 419 |
Next_Non_Pragma (Choice); |
| 420 |
end loop; |
| 421 |
|
| 422 |
Next_Non_Pragma (Handler); |
| 423 |
end loop; |
| 424 |
|
| 425 |
-- Analyze statements in sequence |
| 426 |
|
| 427 |
Analyze_Statements (Statements (N)); |
| 428 |
|
| 429 |
-- If the current scope is a subprogram, entry or task body or declare |
| 430 |
-- block then this is the right place to check for hanging useless |
| 431 |
-- assignments from the statement sequence. |
| 432 |
|
| 433 |
if Is_Subprogram_Or_Entry (Current_Scope) |
| 434 |
or else Ekind (Current_Scope) in E_Block | E_Task_Type |
| 435 |
then |
| 436 |
Warn_On_Useless_Assignments (Current_Scope); |
| 437 |
end if; |
| 438 |
|
| 439 |
-- Deal with handlers or AT END proc |
| 440 |
|
| 441 |
if Present (Handlers) then |
| 442 |
Analyze_Exception_Handlers (Handlers); |
| 443 |
elsif Present (At_End_Proc (N)) then |
| 444 |
Analyze (At_End_Proc (N)); |
| 445 |
end if; |
| 446 |
|
| 447 |
if Present (Finally_Statements (N)) then |
| 448 |
Analyze_Statements (Finally_Statements (N)); |
| 449 |
end if; |
| 450 |
end Analyze_Handled_Statements; |
| 451 |
|
| 452 |
------------------------------ |
| 453 |
-- Analyze_Raise_Expression -- |
| 454 |
------------------------------ |
| 455 |
|
| 456 |
procedure Analyze_Raise_Expression (N : Node_Id) is |
| 457 |
Exception_Id : constant Node_Id := Name (N); |
| 458 |
Exception_Name : Entity_Id := Empty; |
| 459 |
|
| 460 |
begin |
| 461 |
-- Check exception restrictions on the original source |
| 462 |
|
| 463 |
if Comes_From_Source (N) then |
| 464 |
Check_Restriction (No_Exceptions, N); |
| 465 |
end if; |
| 466 |
|
| 467 |
Analyze (Exception_Id); |
| 468 |
|
| 469 |
if Is_Entity_Name (Exception_Id) then |
| 470 |
Exception_Name := Entity (Exception_Id); |
| 471 |
end if; |
| 472 |
|
| 473 |
if No (Exception_Name) |
| 474 |
or else Ekind (Exception_Name) /= E_Exception |
| 475 |
then |
| 476 |
Error_Msg_N |
| 477 |
("exception name expected in raise statement", Exception_Id); |
| 478 |
else |
| 479 |
Set_Is_Raised (Exception_Name); |
| 480 |
end if; |
| 481 |
|
| 482 |
-- Deal with RAISE WITH case |
| 483 |
|
| 484 |
if Present (Expression (N)) then |
| 485 |
Analyze_And_Resolve (Expression (N), Standard_String); |
| 486 |
end if; |
| 487 |
|
| 488 |
-- Check obsolescent use of Numeric_Error |
| 489 |
|
| 490 |
if Exception_Name = Standard_Numeric_Error then |
| 491 |
Check_Restriction (No_Obsolescent_Features, Exception_Id); |
| 492 |
end if; |
| 493 |
|
| 494 |
-- Kill last assignment indication |
| 495 |
|
| 496 |
Kill_Current_Values (Last_Assignment_Only => True); |
| 497 |
|
| 498 |
-- Raise_Type is compatible with all other types so that the raise |
| 499 |
-- expression is legal in any expression context. It will be eventually |
| 500 |
-- replaced by the concrete type imposed by the context. |
| 501 |
|
| 502 |
Set_Etype (N, Raise_Type); |
| 503 |
end Analyze_Raise_Expression; |
| 504 |
|
| 505 |
----------------------------- |
| 506 |
-- Analyze_Raise_Statement -- |
| 507 |
----------------------------- |
| 508 |
|
| 509 |
procedure Analyze_Raise_Statement (N : Node_Id) is |
| 510 |
Exception_Id : constant Node_Id := Name (N); |
| 511 |
Exception_Name : Entity_Id := Empty; |
| 512 |
P : Node_Id; |
| 513 |
Par : Node_Id; |
| 514 |
|
| 515 |
begin |
| 516 |
Check_Unreachable_Code (N); |
| 517 |
|
| 518 |
-- Check exception restrictions on the original source |
| 519 |
|
| 520 |
if Comes_From_Source (N) then |
| 521 |
Check_Restriction (No_Exceptions, N); |
| 522 |
end if; |
| 523 |
|
| 524 |
-- Check for useless assignment to OUT or IN OUT scalar preceding the |
| 525 |
-- raise. Right now only look at assignment statements, could do more??? |
| 526 |
|
| 527 |
if Is_List_Member (N) then |
| 528 |
declare |
| 529 |
P : Node_Id; |
| 530 |
L : Node_Id; |
| 531 |
|
| 532 |
begin |
| 533 |
P := Prev (N); |
| 534 |
|
| 535 |
-- Skip past null statements and pragmas |
| 536 |
|
| 537 |
while Present (P) |
| 538 |
and then Nkind (P) in N_Null_Statement | N_Pragma |
| 539 |
loop |
| 540 |
P := Prev (P); |
| 541 |
end loop; |
| 542 |
|
| 543 |
-- See if preceding statement is an assignment |
| 544 |
|
| 545 |
if Present (P) and then Nkind (P) = N_Assignment_Statement then |
| 546 |
L := Name (P); |
| 547 |
|
| 548 |
-- Give warning for assignment to by-copy formal |
| 549 |
|
| 550 |
if Is_Entity_Name (L) |
| 551 |
and then Is_Formal (Entity (L)) |
| 552 |
and then Is_By_Copy_Type (Etype (L)) |
| 553 |
and then not Is_Aliased (Entity (L)) |
| 554 |
|
| 555 |
-- Do this only for parameters to the current subprogram. |
| 556 |
-- This avoids some false positives for the nested case. |
| 557 |
|
| 558 |
and then Nearest_Dynamic_Scope (Current_Scope) = |
| 559 |
Scope (Entity (L)) |
| 560 |
|
| 561 |
then |
| 562 |
-- Don't give warning if we are covered by an exception |
| 563 |
-- handler, since this may result in false positives, since |
| 564 |
-- the handler may handle the exception and return normally. |
| 565 |
|
| 566 |
-- First find the enclosing handled sequence of statements |
| 567 |
-- (note, we could also look for a handler in an outer block |
| 568 |
-- but currently we don't, and in that case we'll emit the |
| 569 |
-- warning). |
| 570 |
|
| 571 |
Par := N; |
| 572 |
loop |
| 573 |
Par := Parent (Par); |
| 574 |
exit when Nkind (Par) = N_Handled_Sequence_Of_Statements; |
| 575 |
end loop; |
| 576 |
|
| 577 |
-- See if there is a handler, give message if not |
| 578 |
|
| 579 |
if No (Exception_Handlers (Par)) then |
| 580 |
Error_Msg_N |
| 581 |
("assignment to pass-by-copy formal " |
| 582 |
& "may have no effect??", P); |
| 583 |
Error_Msg_N |
| 584 |
("\RAISE statement may result in abnormal return " |
| 585 |
& "(RM 6.4.1(17))??", P); |
| 586 |
end if; |
| 587 |
end if; |
| 588 |
end if; |
| 589 |
end; |
| 590 |
end if; |
| 591 |
|
| 592 |
-- Reraise statement |
| 593 |
|
| 594 |
if No (Exception_Id) then |
| 595 |
P := Parent (N); |
| 596 |
while Nkind (P) not in |
| 597 |
N_Exception_Handler | N_Subprogram_Body | N_Package_Body | |
| 598 |
N_Task_Body | N_Entry_Body |
| 599 |
loop |
| 600 |
P := Parent (P); |
| 601 |
end loop; |
| 602 |
|
| 603 |
if Nkind (P) /= N_Exception_Handler then |
| 604 |
Error_Msg_N |
| 605 |
("reraise statement must appear directly in a handler", N); |
| 606 |
|
| 607 |
-- If a handler has a reraise, it cannot be the target of a local |
| 608 |
-- raise (goto optimization is impossible), and if the no exception |
| 609 |
-- propagation restriction is set, this is a violation. |
| 610 |
|
| 611 |
else |
| 612 |
Set_Local_Raise_Not_OK (P); |
| 613 |
Check_Restriction (No_Exception_Propagation, N); |
| 614 |
end if; |
| 615 |
|
| 616 |
-- Normal case with exception id present |
| 617 |
|
| 618 |
else |
| 619 |
Analyze (Exception_Id); |
| 620 |
|
| 621 |
if Is_Entity_Name (Exception_Id) then |
| 622 |
Exception_Name := Entity (Exception_Id); |
| 623 |
end if; |
| 624 |
|
| 625 |
if No (Exception_Name) |
| 626 |
or else Ekind (Exception_Name) /= E_Exception |
| 627 |
then |
| 628 |
Error_Msg_N |
| 629 |
("exception name expected in raise statement", Exception_Id); |
| 630 |
else |
| 631 |
Set_Is_Raised (Exception_Name); |
| 632 |
end if; |
| 633 |
|
| 634 |
-- Deal with RAISE WITH case |
| 635 |
|
| 636 |
if Present (Expression (N)) then |
| 637 |
Analyze_And_Resolve (Expression (N), Standard_String); |
| 638 |
end if; |
| 639 |
end if; |
| 640 |
|
| 641 |
-- Check obsolescent use of Numeric_Error |
| 642 |
|
| 643 |
if Exception_Name = Standard_Numeric_Error then |
| 644 |
Check_Restriction (No_Obsolescent_Features, Exception_Id); |
| 645 |
end if; |
| 646 |
|
| 647 |
-- Kill last assignment indication |
| 648 |
|
| 649 |
Kill_Current_Values (Last_Assignment_Only => True); |
| 650 |
end Analyze_Raise_Statement; |
| 651 |
|
| 652 |
---------------------------------- |
| 653 |
-- Analyze_Raise_When_Statement -- |
| 654 |
---------------------------------- |
| 655 |
|
| 656 |
procedure Analyze_Raise_When_Statement (N : Node_Id) is |
| 657 |
begin |
| 658 |
-- Verify the condition is a Boolean expression |
| 659 |
|
| 660 |
Analyze_And_Resolve (Condition (N), Any_Boolean); |
| 661 |
Check_Unset_Reference (Condition (N)); |
| 662 |
end Analyze_Raise_When_Statement; |
| 663 |
|
| 664 |
----------------------------- |
| 665 |
-- Analyze_Raise_xxx_Error -- |
| 666 |
----------------------------- |
| 667 |
|
| 668 |
-- Normally, the Etype is already set (when this node is used within |
| 669 |
-- an expression, since it is copied from the node which it rewrites). |
| 670 |
-- If this node is used in a statement context, then we set the type |
| 671 |
-- Standard_Void_Type. This is used both by Gigi and by the front end |
| 672 |
-- to distinguish the statement use and the subexpression use. |
| 673 |
|
| 674 |
-- The only other required processing is to take care of the Condition |
| 675 |
-- field if one is present. |
| 676 |
|
| 677 |
procedure Analyze_Raise_xxx_Error (N : Node_Id) is |
| 678 |
|
| 679 |
function Same_Expression (C1, C2 : Node_Id) return Boolean; |
| 680 |
-- It often occurs that two identical raise statements are generated in |
| 681 |
-- succession (for example when dynamic elaboration checks take place on |
| 682 |
-- separate expressions in a call). If the two statements are identical |
| 683 |
-- according to the simple criterion that follows, the raise is |
| 684 |
-- converted into a null statement. |
| 685 |
|
| 686 |
--------------------- |
| 687 |
-- Same_Expression -- |
| 688 |
--------------------- |
| 689 |
|
| 690 |
function Same_Expression (C1, C2 : Node_Id) return Boolean is |
| 691 |
begin |
| 692 |
if No (C1) and then No (C2) then |
| 693 |
return True; |
| 694 |
|
| 695 |
elsif Is_Entity_Name (C1) and then Is_Entity_Name (C2) then |
| 696 |
return Entity (C1) = Entity (C2); |
| 697 |
|
| 698 |
elsif Nkind (C1) /= Nkind (C2) then |
| 699 |
return False; |
| 700 |
|
| 701 |
elsif Nkind (C1) in N_Unary_Op then |
| 702 |
return Same_Expression (Right_Opnd (C1), Right_Opnd (C2)); |
| 703 |
|
| 704 |
elsif Nkind (C1) in N_Binary_Op then |
| 705 |
return Same_Expression (Left_Opnd (C1), Left_Opnd (C2)) |
| 706 |
and then |
| 707 |
Same_Expression (Right_Opnd (C1), Right_Opnd (C2)); |
| 708 |
|
| 709 |
elsif Nkind (C1) = N_Null then |
| 710 |
return True; |
| 711 |
|
| 712 |
else |
| 713 |
return False; |
| 714 |
end if; |
| 715 |
end Same_Expression; |
| 716 |
|
| 717 |
-- Start of processing for Analyze_Raise_xxx_Error |
| 718 |
|
| 719 |
begin |
| 720 |
if No (Etype (N)) then |
| 721 |
Set_Etype (N, Standard_Void_Type); |
| 722 |
end if; |
| 723 |
|
| 724 |
if Present (Condition (N)) then |
| 725 |
Analyze_And_Resolve (Condition (N), Standard_Boolean); |
| 726 |
end if; |
| 727 |
|
| 728 |
-- Deal with static cases in obvious manner |
| 729 |
|
| 730 |
if Nkind (Condition (N)) = N_Identifier then |
| 731 |
if Entity (Condition (N)) = Standard_True then |
| 732 |
Set_Condition (N, Empty); |
| 733 |
|
| 734 |
elsif Entity (Condition (N)) = Standard_False then |
| 735 |
Rewrite (N, Make_Null_Statement (Sloc (N))); |
| 736 |
end if; |
| 737 |
end if; |
| 738 |
|
| 739 |
-- Remove duplicate raise statements. Note that the previous one may |
| 740 |
-- already have been removed as well. |
| 741 |
|
| 742 |
if not Comes_From_Source (N) |
| 743 |
and then Nkind (N) /= N_Null_Statement |
| 744 |
and then Is_List_Member (N) |
| 745 |
and then Present (Prev (N)) |
| 746 |
and then Nkind (N) = Nkind (Original_Node (Prev (N))) |
| 747 |
and then Same_Expression |
| 748 |
(Condition (N), Condition (Original_Node (Prev (N)))) |
| 749 |
then |
| 750 |
Rewrite (N, Make_Null_Statement (Sloc (N))); |
| 751 |
end if; |
| 752 |
end Analyze_Raise_xxx_Error; |
| 753 |
|
| 754 |
end Sem_Ch11; |