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