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