2014-12-12 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_ch11.adb
blob2e3dbd9fe87b17d94c720a950dcaadadeaab39ea
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-2014, 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 Stand; use Stand;
50 package body Sem_Ch11 is
52 -----------------------------------
53 -- Analyze_Exception_Declaration --
54 -----------------------------------
56 procedure Analyze_Exception_Declaration (N : Node_Id) is
57 Id : constant Entity_Id := Defining_Identifier (N);
58 PF : constant Boolean := Is_Pure (Current_Scope);
59 begin
60 Generate_Definition (Id);
61 Enter_Name (Id);
62 Set_Ekind (Id, E_Exception);
63 Set_Etype (Id, Standard_Exception_Type);
64 Set_Is_Statically_Allocated (Id);
65 Set_Is_Pure (Id, PF);
67 -- An exception declared within a Ghost scope is automatically Ghost
68 -- (SPARK RM 6.9(2)).
70 if Within_Ghost_Scope then
71 Set_Is_Ghost_Entity (Id);
72 end if;
74 if Has_Aspects (N) then
75 Analyze_Aspect_Specifications (N, Id);
76 end if;
77 end Analyze_Exception_Declaration;
79 --------------------------------
80 -- Analyze_Exception_Handlers --
81 --------------------------------
83 procedure Analyze_Exception_Handlers (L : List_Id) is
84 Handler : Node_Id;
85 Choice : Entity_Id;
86 Id : Node_Id;
87 H_Scope : Entity_Id := Empty;
89 procedure Check_Duplication (Id : Node_Id);
90 -- Iterate through the identifiers in each handler to find duplicates
92 function Others_Present return Boolean;
93 -- Returns True if others handler is present
95 -----------------------
96 -- Check_Duplication --
97 -----------------------
99 procedure Check_Duplication (Id : Node_Id) is
100 Handler : Node_Id;
101 Id1 : Node_Id;
102 Id_Entity : Entity_Id := Entity (Id);
104 begin
105 if Present (Renamed_Entity (Id_Entity)) then
106 Id_Entity := Renamed_Entity (Id_Entity);
107 end if;
109 Handler := First_Non_Pragma (L);
110 while Present (Handler) loop
111 Id1 := First (Exception_Choices (Handler));
112 while Present (Id1) loop
114 -- Only check against the exception choices which precede
115 -- Id in the handler, since the ones that follow Id have not
116 -- been analyzed yet and will be checked in a subsequent call.
118 if Id = Id1 then
119 return;
121 elsif Nkind (Id1) /= N_Others_Choice
122 and then
123 (Id_Entity = Entity (Id1)
124 or else (Id_Entity = Renamed_Entity (Entity (Id1))))
125 then
126 if Handler /= Parent (Id) then
127 Error_Msg_Sloc := Sloc (Id1);
128 Error_Msg_NE
129 ("exception choice duplicates &#", Id, Id1);
131 else
132 if Ada_Version = Ada_83
133 and then Comes_From_Source (Id)
134 then
135 Error_Msg_N
136 ("(Ada 83): duplicate exception choice&", Id);
137 end if;
138 end if;
139 end if;
141 Next_Non_Pragma (Id1);
142 end loop;
144 Next (Handler);
145 end loop;
146 end Check_Duplication;
148 --------------------
149 -- Others_Present --
150 --------------------
152 function Others_Present return Boolean is
153 H : Node_Id;
155 begin
156 H := First (L);
157 while Present (H) loop
158 if Nkind (H) /= N_Pragma
159 and then Nkind (First (Exception_Choices (H))) = N_Others_Choice
160 then
161 return True;
162 end if;
164 Next (H);
165 end loop;
167 return False;
168 end Others_Present;
170 -- Start of processing for Analyze_Exception_Handlers
172 begin
173 Handler := First (L);
174 Check_Restriction (No_Exceptions, Handler);
175 Check_Restriction (No_Exception_Handlers, Handler);
177 -- Kill current remembered values, since we don't know where we were
178 -- when the exception was raised.
180 Kill_Current_Values;
182 -- Loop through handlers (which can include pragmas)
184 while Present (Handler) loop
186 -- If pragma just analyze it
188 if Nkind (Handler) = N_Pragma then
189 Analyze (Handler);
191 -- Otherwise we have a real exception handler
193 else
194 -- Deal with choice parameter. The exception handler is a
195 -- declarative part for the choice parameter, so it constitutes a
196 -- scope for visibility purposes. We create an entity to denote
197 -- the whole exception part, and use it as the scope of all the
198 -- choices, which may even have the same name without conflict.
199 -- This scope plays no other role in expansion or code generation.
201 Choice := Choice_Parameter (Handler);
203 if Present (Choice) then
204 Set_Local_Raise_Not_OK (Handler);
206 if Comes_From_Source (Choice) then
207 Check_Restriction (No_Exception_Propagation, Choice);
208 Set_Debug_Info_Needed (Choice);
209 end if;
211 if No (H_Scope) then
212 H_Scope :=
213 New_Internal_Entity
214 (E_Block, Current_Scope, Sloc (Choice), 'E');
215 end if;
217 Push_Scope (H_Scope);
218 Set_Etype (H_Scope, Standard_Void_Type);
220 Enter_Name (Choice);
221 Set_Ekind (Choice, E_Variable);
223 if RTE_Available (RE_Exception_Occurrence) then
224 Set_Etype (Choice, RTE (RE_Exception_Occurrence));
225 end if;
227 Generate_Definition (Choice);
229 -- Indicate that choice has an initial value, since in effect
230 -- this field is assigned an initial value by the exception.
231 -- We also consider that it is modified in the source.
233 Set_Has_Initial_Value (Choice, True);
234 Set_Never_Set_In_Source (Choice, False);
235 end if;
237 Id := First (Exception_Choices (Handler));
238 while Present (Id) loop
239 if Nkind (Id) = N_Others_Choice then
240 if Present (Next (Id))
241 or else Present (Next (Handler))
242 or else Present (Prev (Id))
243 then
244 Error_Msg_N ("OTHERS must appear alone and last", Id);
245 end if;
247 else
248 Analyze (Id);
250 -- In most cases the choice has already been analyzed in
251 -- Analyze_Handled_Statement_Sequence, in order to expand
252 -- local handlers. This advance analysis does not take into
253 -- account the case in which a choice has the same name as
254 -- the choice parameter of the handler, which may hide an
255 -- outer exception. This pathological case appears in ACATS
256 -- B80001_3.adb, and requires an explicit check to verify
257 -- that the id is not hidden.
259 if not Is_Entity_Name (Id)
260 or else Ekind (Entity (Id)) /= E_Exception
261 or else
262 (Nkind (Id) = N_Identifier
263 and then Chars (Id) = Chars (Choice))
264 then
265 Error_Msg_N ("exception name expected", Id);
267 else
268 -- Emit a warning at the declaration level when a local
269 -- exception is never raised explicitly.
271 if Warn_On_Redundant_Constructs
272 and then not Is_Raised (Entity (Id))
273 and then Scope (Entity (Id)) = Current_Scope
274 then
275 Error_Msg_NE
276 ("exception & is never raised?r?", Entity (Id), Id);
277 end if;
279 if Present (Renamed_Entity (Entity (Id))) then
280 if Entity (Id) = Standard_Numeric_Error then
281 Check_Restriction (No_Obsolescent_Features, Id);
283 if Warn_On_Obsolescent_Feature then
284 Error_Msg_N
285 ("Numeric_Error is an " &
286 "obsolescent feature (RM J.6(1))?j?", Id);
287 Error_Msg_N
288 ("\use Constraint_Error instead?j?", Id);
289 end if;
290 end if;
291 end if;
293 Check_Duplication (Id);
295 -- Check for exception declared within generic formal
296 -- package (which is illegal, see RM 11.2(8))
298 declare
299 Ent : Entity_Id := Entity (Id);
300 Scop : Entity_Id;
302 begin
303 if Present (Renamed_Entity (Ent)) then
304 Ent := Renamed_Entity (Ent);
305 end if;
307 Scop := Scope (Ent);
308 while Scop /= Standard_Standard
309 and then Ekind (Scop) = E_Package
310 loop
311 if Nkind (Declaration_Node (Scop)) =
312 N_Package_Specification
313 and then
314 Nkind (Original_Node (Parent
315 (Declaration_Node (Scop)))) =
316 N_Formal_Package_Declaration
317 then
318 Error_Msg_NE
319 ("exception& is declared in " &
320 "generic formal package", Id, Ent);
321 Error_Msg_N
322 ("\and therefore cannot appear in " &
323 "handler (RM 11.2(8))", Id);
324 exit;
326 -- If the exception is declared in an inner
327 -- instance, nothing else to check.
329 elsif Is_Generic_Instance (Scop) then
330 exit;
331 end if;
333 Scop := Scope (Scop);
334 end loop;
335 end;
336 end if;
337 end if;
339 Next (Id);
340 end loop;
342 -- Check for redundant handler (has only raise statement) and is
343 -- either an others handler, or is a specific handler when no
344 -- others handler is present.
346 if Warn_On_Redundant_Constructs
347 and then List_Length (Statements (Handler)) = 1
348 and then Nkind (First (Statements (Handler))) = N_Raise_Statement
349 and then No (Name (First (Statements (Handler))))
350 and then (not Others_Present
351 or else Nkind (First (Exception_Choices (Handler))) =
352 N_Others_Choice)
353 then
354 Error_Msg_N
355 ("useless handler contains only a reraise statement?r?",
356 Handler);
357 end if;
359 -- Now analyze the statements of this handler
361 Analyze_Statements (Statements (Handler));
363 -- If a choice was present, we created a special scope for it,
364 -- so this is where we pop that special scope to get rid of it.
366 if Present (Choice) then
367 End_Scope;
368 end if;
369 end if;
371 Next (Handler);
372 end loop;
373 end Analyze_Exception_Handlers;
375 --------------------------------
376 -- Analyze_Handled_Statements --
377 --------------------------------
379 procedure Analyze_Handled_Statements (N : Node_Id) is
380 Handlers : constant List_Id := Exception_Handlers (N);
381 Handler : Node_Id;
382 Choice : Node_Id;
384 begin
385 if Present (Handlers) then
386 Kill_All_Checks;
387 end if;
389 -- We are now going to analyze the statements and then the exception
390 -- handlers. We certainly need to do things in this order to get the
391 -- proper sequential semantics for various warnings.
393 -- However, there is a glitch. When we process raise statements, an
394 -- optimization is to look for local handlers and specialize the code
395 -- in this case.
397 -- In order to detect if a handler is matching, we must have at least
398 -- analyzed the choices in the proper scope so that proper visibility
399 -- analysis is performed. Hence we analyze just the choices first,
400 -- before we analyze the statement sequence.
402 Handler := First_Non_Pragma (Handlers);
403 while Present (Handler) loop
404 Choice := First_Non_Pragma (Exception_Choices (Handler));
405 while Present (Choice) loop
406 Analyze (Choice);
407 Next_Non_Pragma (Choice);
408 end loop;
410 Next_Non_Pragma (Handler);
411 end loop;
413 -- Analyze statements in sequence
415 Analyze_Statements (Statements (N));
417 -- If the current scope is a subprogram, then this is the right place to
418 -- check for hanging useless assignments from the statement sequence of
419 -- the subprogram body.
421 if Is_Subprogram (Current_Scope) 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)
538 and then Nkind (P) = N_Assignment_Statement
539 then
540 L := Name (P);
542 -- Give warning for assignment to scalar formal
544 if Is_Scalar_Type (Etype (L))
545 and then Is_Entity_Name (L)
546 and then Is_Formal (Entity (L))
548 -- Do this only for parameters to the current subprogram.
549 -- This avoids some false positives for the nested case.
551 and then Nearest_Dynamic_Scope (Current_Scope) =
552 Scope (Entity (L))
554 then
555 -- Don't give warning if we are covered by an exception
556 -- handler, since this may result in false positives, since
557 -- the handler may handle the exception and return normally.
559 -- First find the enclosing handled sequence of statements
560 -- (note, we could also look for a handler in an outer block
561 -- but currently we don't, and in that case we'll emit the
562 -- warning).
564 Par := N;
565 loop
566 Par := Parent (Par);
567 exit when Nkind (Par) = N_Handled_Sequence_Of_Statements;
568 end loop;
570 -- See if there is a handler, give message if not
572 if No (Exception_Handlers (Par)) then
573 Error_Msg_N
574 ("assignment to pass-by-copy formal " &
575 "may have no effect??", P);
576 Error_Msg_N
577 ("\RAISE statement may result in abnormal return" &
578 " (RM 6.4.1(17))??", P);
579 end if;
580 end if;
581 end if;
582 end;
583 end if;
585 -- Reraise statement
587 if No (Exception_Id) then
588 P := Parent (N);
589 while not Nkind_In (P, N_Exception_Handler,
590 N_Subprogram_Body,
591 N_Package_Body,
592 N_Task_Body,
593 N_Entry_Body)
594 loop
595 P := Parent (P);
596 end loop;
598 if Nkind (P) /= N_Exception_Handler then
599 Error_Msg_N
600 ("reraise statement must appear directly in a handler", N);
602 -- If a handler has a reraise, it cannot be the target of a local
603 -- raise (goto optimization is impossible), and if the no exception
604 -- propagation restriction is set, this is a violation.
606 else
607 Set_Local_Raise_Not_OK (P);
609 -- Do not check the restriction if the reraise statement is part
610 -- of the code generated for an AT-END handler. That's because
611 -- if the restriction is actually active, we never generate this
612 -- raise anyway, so the apparent violation is bogus.
614 if not From_At_End (N) then
615 Check_Restriction (No_Exception_Propagation, N);
616 end if;
617 end if;
619 -- Normal case with exception id present
621 else
622 Analyze (Exception_Id);
624 if Is_Entity_Name (Exception_Id) then
625 Exception_Name := Entity (Exception_Id);
626 end if;
628 if No (Exception_Name)
629 or else Ekind (Exception_Name) /= E_Exception
630 then
631 Error_Msg_N
632 ("exception name expected in raise statement", Exception_Id);
633 else
634 Set_Is_Raised (Exception_Name);
635 end if;
637 -- Deal with RAISE WITH case
639 if Present (Expression (N)) then
640 Analyze_And_Resolve (Expression (N), Standard_String);
641 end if;
642 end if;
644 -- Check obsolescent use of Numeric_Error
646 if Exception_Name = Standard_Numeric_Error then
647 Check_Restriction (No_Obsolescent_Features, Exception_Id);
648 end if;
650 -- Kill last assignment indication
652 Kill_Current_Values (Last_Assignment_Only => True);
653 end Analyze_Raise_Statement;
655 -----------------------------
656 -- Analyze_Raise_xxx_Error --
657 -----------------------------
659 -- Normally, the Etype is already set (when this node is used within
660 -- an expression, since it is copied from the node which it rewrites).
661 -- If this node is used in a statement context, then we set the type
662 -- Standard_Void_Type. This is used both by Gigi and by the front end
663 -- to distinguish the statement use and the subexpression use.
665 -- The only other required processing is to take care of the Condition
666 -- field if one is present.
668 procedure Analyze_Raise_xxx_Error (N : Node_Id) is
670 function Same_Expression (C1, C2 : Node_Id) return Boolean;
671 -- It often occurs that two identical raise statements are generated in
672 -- succession (for example when dynamic elaboration checks take place on
673 -- separate expressions in a call). If the two statements are identical
674 -- according to the simple criterion that follows, the raise is
675 -- converted into a null statement.
677 ---------------------
678 -- Same_Expression --
679 ---------------------
681 function Same_Expression (C1, C2 : Node_Id) return Boolean is
682 begin
683 if No (C1) and then No (C2) then
684 return True;
686 elsif Is_Entity_Name (C1) and then Is_Entity_Name (C2) then
687 return Entity (C1) = Entity (C2);
689 elsif Nkind (C1) /= Nkind (C2) then
690 return False;
692 elsif Nkind (C1) in N_Unary_Op then
693 return Same_Expression (Right_Opnd (C1), Right_Opnd (C2));
695 elsif Nkind (C1) in N_Binary_Op then
696 return Same_Expression (Left_Opnd (C1), Left_Opnd (C2))
697 and then
698 Same_Expression (Right_Opnd (C1), Right_Opnd (C2));
700 elsif Nkind (C1) = N_Null then
701 return True;
703 else
704 return False;
705 end if;
706 end Same_Expression;
708 -- Start of processing for Analyze_Raise_xxx_Error
710 begin
711 if Nkind (Original_Node (N)) = N_Raise_Statement then
712 Check_SPARK_05_Restriction ("raise statement is not allowed", N);
713 end if;
715 if No (Etype (N)) then
716 Set_Etype (N, Standard_Void_Type);
717 end if;
719 if Present (Condition (N)) then
720 Analyze_And_Resolve (Condition (N), Standard_Boolean);
721 end if;
723 -- Deal with static cases in obvious manner
725 if Nkind (Condition (N)) = N_Identifier then
726 if Entity (Condition (N)) = Standard_True then
727 Set_Condition (N, Empty);
729 elsif Entity (Condition (N)) = Standard_False then
730 Rewrite (N, Make_Null_Statement (Sloc (N)));
731 end if;
732 end if;
734 -- Remove duplicate raise statements. Note that the previous one may
735 -- already have been removed as well.
737 if not Comes_From_Source (N)
738 and then Nkind (N) /= N_Null_Statement
739 and then Is_List_Member (N)
740 and then Present (Prev (N))
741 and then Nkind (N) = Nkind (Original_Node (Prev (N)))
742 and then Same_Expression
743 (Condition (N), Condition (Original_Node (Prev (N))))
744 then
745 Rewrite (N, Make_Null_Statement (Sloc (N)));
746 end if;
747 end Analyze_Raise_xxx_Error;
749 end Sem_Ch11;