2013-11-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_ch11.adb
bloba397edfb40e0d4eb24a9bae46797f84b06eb47f6
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-2013, 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_Ch5; use Sem_Ch5;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Ch13; use Sem_Ch13;
43 with Sem_Res; use Sem_Res;
44 with Sem_Util; use Sem_Util;
45 with Sem_Warn; use Sem_Warn;
46 with Sinfo; use Sinfo;
47 with Stand; use Stand;
48 with Uintp; use Uintp;
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_Exception_Code (Id, Uint_0);
64 Set_Etype (Id, Standard_Exception_Type);
65 Set_Is_Statically_Allocated (Id);
66 Set_Is_Pure (Id, PF);
68 if Has_Aspects (N) then
69 Analyze_Aspect_Specifications (N, Id);
70 end if;
71 end Analyze_Exception_Declaration;
73 --------------------------------
74 -- Analyze_Exception_Handlers --
75 --------------------------------
77 procedure Analyze_Exception_Handlers (L : List_Id) is
78 Handler : Node_Id;
79 Choice : Entity_Id;
80 Id : Node_Id;
81 H_Scope : Entity_Id := Empty;
83 procedure Check_Duplication (Id : Node_Id);
84 -- Iterate through the identifiers in each handler to find duplicates
86 function Others_Present return Boolean;
87 -- Returns True if others handler is present
89 -----------------------
90 -- Check_Duplication --
91 -----------------------
93 procedure Check_Duplication (Id : Node_Id) is
94 Handler : Node_Id;
95 Id1 : Node_Id;
96 Id_Entity : Entity_Id := Entity (Id);
98 begin
99 if Present (Renamed_Entity (Id_Entity)) then
100 Id_Entity := Renamed_Entity (Id_Entity);
101 end if;
103 Handler := First_Non_Pragma (L);
104 while Present (Handler) loop
105 Id1 := First (Exception_Choices (Handler));
106 while Present (Id1) loop
108 -- Only check against the exception choices which precede
109 -- Id in the handler, since the ones that follow Id have not
110 -- been analyzed yet and will be checked in a subsequent call.
112 if Id = Id1 then
113 return;
115 elsif Nkind (Id1) /= N_Others_Choice
116 and then
117 (Id_Entity = Entity (Id1)
118 or else (Id_Entity = Renamed_Entity (Entity (Id1))))
119 then
120 if Handler /= Parent (Id) then
121 Error_Msg_Sloc := Sloc (Id1);
122 Error_Msg_NE
123 ("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 end if;
211 Push_Scope (H_Scope);
212 Set_Etype (H_Scope, Standard_Void_Type);
214 Enter_Name (Choice);
215 Set_Ekind (Choice, E_Variable);
217 if RTE_Available (RE_Exception_Occurrence) then
218 Set_Etype (Choice, RTE (RE_Exception_Occurrence));
219 end if;
221 Generate_Definition (Choice);
223 -- Indicate that choice has an initial value, since in effect
224 -- this field is assigned an initial value by the exception.
225 -- We also consider that it is modified in the source.
227 Set_Has_Initial_Value (Choice, True);
228 Set_Never_Set_In_Source (Choice, False);
229 end if;
231 Id := First (Exception_Choices (Handler));
232 while Present (Id) loop
233 if Nkind (Id) = N_Others_Choice then
234 if Present (Next (Id))
235 or else Present (Next (Handler))
236 or else Present (Prev (Id))
237 then
238 Error_Msg_N ("OTHERS must appear alone and last", Id);
239 end if;
241 else
242 Analyze (Id);
244 -- In most cases the choice has already been analyzed in
245 -- Analyze_Handled_Statement_Sequence, in order to expand
246 -- local handlers. This advance analysis does not take into
247 -- account the case in which a choice has the same name as
248 -- the choice parameter of the handler, which may hide an
249 -- outer exception. This pathological case appears in ACATS
250 -- B80001_3.adb, and requires an explicit check to verify
251 -- that the id is not hidden.
253 if not Is_Entity_Name (Id)
254 or else Ekind (Entity (Id)) /= E_Exception
255 or else
256 (Nkind (Id) = N_Identifier
257 and then Chars (Id) = Chars (Choice))
258 then
259 Error_Msg_N ("exception name expected", Id);
261 else
262 -- Emit a warning at the declaration level when a local
263 -- exception is never raised explicitly.
265 if Warn_On_Redundant_Constructs
266 and then not Is_Raised (Entity (Id))
267 and then Scope (Entity (Id)) = Current_Scope
268 then
269 Error_Msg_NE
270 ("exception & is never raised?r?", Entity (Id), Id);
271 end if;
273 if Present (Renamed_Entity (Entity (Id))) then
274 if Entity (Id) = Standard_Numeric_Error then
275 Check_Restriction (No_Obsolescent_Features, Id);
277 if Warn_On_Obsolescent_Feature then
278 Error_Msg_N
279 ("Numeric_Error is an " &
280 "obsolescent feature (RM J.6(1))?j?", Id);
281 Error_Msg_N
282 ("\use Constraint_Error instead?j?", Id);
283 end if;
284 end if;
285 end if;
287 Check_Duplication (Id);
289 -- Check for exception declared within generic formal
290 -- package (which is illegal, see RM 11.2(8))
292 declare
293 Ent : Entity_Id := Entity (Id);
294 Scop : Entity_Id;
296 begin
297 if Present (Renamed_Entity (Ent)) then
298 Ent := Renamed_Entity (Ent);
299 end if;
301 Scop := Scope (Ent);
302 while Scop /= Standard_Standard
303 and then Ekind (Scop) = E_Package
304 loop
305 if Nkind (Declaration_Node (Scop)) =
306 N_Package_Specification
307 and then
308 Nkind (Original_Node (Parent
309 (Declaration_Node (Scop)))) =
310 N_Formal_Package_Declaration
311 then
312 Error_Msg_NE
313 ("exception& is declared in " &
314 "generic formal package", Id, Ent);
315 Error_Msg_N
316 ("\and therefore cannot appear in " &
317 "handler (RM 11.2(8))", Id);
318 exit;
320 -- If the exception is declared in an inner
321 -- instance, nothing else to check.
323 elsif Is_Generic_Instance (Scop) then
324 exit;
325 end if;
327 Scop := Scope (Scop);
328 end loop;
329 end;
330 end if;
331 end if;
333 Next (Id);
334 end loop;
336 -- Check for redundant handler (has only raise statement) and is
337 -- either an others handler, or is a specific handler when no
338 -- others handler is present.
340 if Warn_On_Redundant_Constructs
341 and then List_Length (Statements (Handler)) = 1
342 and then Nkind (First (Statements (Handler))) = N_Raise_Statement
343 and then No (Name (First (Statements (Handler))))
344 and then (not Others_Present
345 or else Nkind (First (Exception_Choices (Handler))) =
346 N_Others_Choice)
347 then
348 Error_Msg_N
349 ("useless handler contains only a reraise statement?r?",
350 Handler);
351 end if;
353 -- Now analyze the statements of this handler
355 Analyze_Statements (Statements (Handler));
357 -- If a choice was present, we created a special scope for it,
358 -- so this is where we pop that special scope to get rid of it.
360 if Present (Choice) then
361 End_Scope;
362 end if;
363 end if;
365 Next (Handler);
366 end loop;
367 end Analyze_Exception_Handlers;
369 --------------------------------
370 -- Analyze_Handled_Statements --
371 --------------------------------
373 procedure Analyze_Handled_Statements (N : Node_Id) is
374 Handlers : constant List_Id := Exception_Handlers (N);
375 Handler : Node_Id;
376 Choice : Node_Id;
378 begin
379 if Present (Handlers) then
380 Kill_All_Checks;
381 end if;
383 -- We are now going to analyze the statements and then the exception
384 -- handlers. We certainly need to do things in this order to get the
385 -- proper sequential semantics for various warnings.
387 -- However, there is a glitch. When we process raise statements, an
388 -- optimization is to look for local handlers and specialize the code
389 -- in this case.
391 -- In order to detect if a handler is matching, we must have at least
392 -- analyzed the choices in the proper scope so that proper visibility
393 -- analysis is performed. Hence we analyze just the choices first,
394 -- before we analyze the statement sequence.
396 Handler := First_Non_Pragma (Handlers);
397 while Present (Handler) loop
398 Choice := First_Non_Pragma (Exception_Choices (Handler));
399 while Present (Choice) loop
400 Analyze (Choice);
401 Next_Non_Pragma (Choice);
402 end loop;
404 Next_Non_Pragma (Handler);
405 end loop;
407 -- Analyze statements in sequence
409 Analyze_Statements (Statements (N));
411 -- If the current scope is a subprogram, then this is the right place to
412 -- check for hanging useless assignments from the statement sequence of
413 -- the subprogram body.
415 if Is_Subprogram (Current_Scope) then
416 Warn_On_Useless_Assignments (Current_Scope);
417 end if;
419 -- Deal with handlers or AT END proc
421 if Present (Handlers) then
422 Analyze_Exception_Handlers (Handlers);
423 elsif Present (At_End_Proc (N)) then
424 Analyze (At_End_Proc (N));
425 end if;
426 end Analyze_Handled_Statements;
428 ------------------------------
429 -- Analyze_Raise_Expression --
430 ------------------------------
432 procedure Analyze_Raise_Expression (N : Node_Id) is
433 Exception_Id : constant Node_Id := Name (N);
434 Exception_Name : Entity_Id := Empty;
436 begin
437 Check_SPARK_Restriction ("raise expression is not allowed", N);
439 -- Check exception restrictions on the original source
441 if Comes_From_Source (N) then
442 Check_Restriction (No_Exceptions, N);
443 end if;
445 Analyze (Exception_Id);
447 if Is_Entity_Name (Exception_Id) then
448 Exception_Name := Entity (Exception_Id);
449 end if;
451 if No (Exception_Name)
452 or else Ekind (Exception_Name) /= E_Exception
453 then
454 Error_Msg_N
455 ("exception name expected in raise statement", Exception_Id);
456 else
457 Set_Is_Raised (Exception_Name);
458 end if;
460 -- Deal with RAISE WITH case
462 if Present (Expression (N)) then
463 Check_Compiler_Unit (Expression (N));
464 Analyze_And_Resolve (Expression (N), Standard_String);
465 end if;
467 -- Check obsolescent use of Numeric_Error
469 if Exception_Name = Standard_Numeric_Error then
470 Check_Restriction (No_Obsolescent_Features, Exception_Id);
471 end if;
473 -- Kill last assignment indication
475 Kill_Current_Values (Last_Assignment_Only => True);
477 -- Set type as Any_Type since we have no information at all on the type
479 Set_Etype (N, Any_Type);
480 end Analyze_Raise_Expression;
482 -----------------------------
483 -- Analyze_Raise_Statement --
484 -----------------------------
486 procedure Analyze_Raise_Statement (N : Node_Id) is
487 Exception_Id : constant Node_Id := Name (N);
488 Exception_Name : Entity_Id := Empty;
489 P : Node_Id;
490 Par : Node_Id;
492 begin
493 if Comes_From_Source (N) then
494 Check_SPARK_Restriction ("raise statement is not allowed", N);
495 end if;
497 Check_Unreachable_Code (N);
499 -- Check exception restrictions on the original source
501 if Comes_From_Source (N) then
502 Check_Restriction (No_Exceptions, N);
503 end if;
505 -- Check for useless assignment to OUT or IN OUT scalar preceding the
506 -- raise. Right now only look at assignment statements, could do more???
508 if Is_List_Member (N) then
509 declare
510 P : Node_Id;
511 L : Node_Id;
513 begin
514 P := Prev (N);
516 -- Skip past null statements and pragmas
518 while Present (P)
519 and then Nkind_In (P, N_Null_Statement, N_Pragma)
520 loop
521 P := Prev (P);
522 end loop;
524 -- See if preceding statement is an assignment
526 if Present (P)
527 and then Nkind (P) = N_Assignment_Statement
528 then
529 L := Name (P);
531 -- Give warning for assignment to scalar formal
533 if Is_Scalar_Type (Etype (L))
534 and then Is_Entity_Name (L)
535 and then Is_Formal (Entity (L))
536 then
537 -- Don't give warning if we are covered by an exception
538 -- handler, since this may result in false positives, since
539 -- the handler may handle the exception and return normally.
541 -- First find the enclosing handled sequence of statements
542 -- (note, we could also look for a handler in an outer block
543 -- but currently we don't, and in that case we'll emit the
544 -- warning).
546 Par := N;
547 loop
548 Par := Parent (Par);
549 exit when Nkind (Par) = N_Handled_Sequence_Of_Statements;
550 end loop;
552 -- See if there is a handler, give message if not
554 if No (Exception_Handlers (Par)) then
555 Error_Msg_N
556 ("assignment to pass-by-copy formal " &
557 "may have no effect??", P);
558 Error_Msg_N
559 ("\RAISE statement may result in abnormal return" &
560 " (RM 6.4.1(17))??", P);
561 end if;
562 end if;
563 end if;
564 end;
565 end if;
567 -- Reraise statement
569 if No (Exception_Id) then
570 P := Parent (N);
571 while not Nkind_In (P, N_Exception_Handler,
572 N_Subprogram_Body,
573 N_Package_Body,
574 N_Task_Body,
575 N_Entry_Body)
576 loop
577 P := Parent (P);
578 end loop;
580 if Nkind (P) /= N_Exception_Handler then
581 Error_Msg_N
582 ("reraise statement must appear directly in a handler", N);
584 -- If a handler has a reraise, it cannot be the target of a local
585 -- raise (goto optimization is impossible), and if the no exception
586 -- propagation restriction is set, this is a violation.
588 else
589 Set_Local_Raise_Not_OK (P);
591 -- Do not check the restriction if the reraise statement is part
592 -- of the code generated for an AT-END handler. That's because
593 -- if the restriction is actually active, we never generate this
594 -- raise anyway, so the apparent violation is bogus.
596 if not From_At_End (N) then
597 Check_Restriction (No_Exception_Propagation, N);
598 end if;
599 end if;
601 -- Normal case with exception id present
603 else
604 Analyze (Exception_Id);
606 if Is_Entity_Name (Exception_Id) then
607 Exception_Name := Entity (Exception_Id);
608 end if;
610 if No (Exception_Name)
611 or else Ekind (Exception_Name) /= E_Exception
612 then
613 Error_Msg_N
614 ("exception name expected in raise statement", Exception_Id);
615 else
616 Set_Is_Raised (Exception_Name);
617 end if;
619 -- Deal with RAISE WITH case
621 if Present (Expression (N)) then
622 Check_Compiler_Unit (Expression (N));
623 Analyze_And_Resolve (Expression (N), Standard_String);
624 end if;
625 end if;
627 -- Check obsolescent use of Numeric_Error
629 if Exception_Name = Standard_Numeric_Error then
630 Check_Restriction (No_Obsolescent_Features, Exception_Id);
631 end if;
633 -- Kill last assignment indication
635 Kill_Current_Values (Last_Assignment_Only => True);
636 end Analyze_Raise_Statement;
638 -----------------------------
639 -- Analyze_Raise_xxx_Error --
640 -----------------------------
642 -- Normally, the Etype is already set (when this node is used within
643 -- an expression, since it is copied from the node which it rewrites).
644 -- If this node is used in a statement context, then we set the type
645 -- Standard_Void_Type. This is used both by Gigi and by the front end
646 -- to distinguish the statement use and the subexpression use.
648 -- The only other required processing is to take care of the Condition
649 -- field if one is present.
651 procedure Analyze_Raise_xxx_Error (N : Node_Id) is
653 function Same_Expression (C1, C2 : Node_Id) return Boolean;
654 -- It often occurs that two identical raise statements are generated in
655 -- succession (for example when dynamic elaboration checks take place on
656 -- separate expressions in a call). If the two statements are identical
657 -- according to the simple criterion that follows, the raise is
658 -- converted into a null statement.
660 ---------------------
661 -- Same_Expression --
662 ---------------------
664 function Same_Expression (C1, C2 : Node_Id) return Boolean is
665 begin
666 if No (C1) and then No (C2) then
667 return True;
669 elsif Is_Entity_Name (C1) and then Is_Entity_Name (C2) then
670 return Entity (C1) = Entity (C2);
672 elsif Nkind (C1) /= Nkind (C2) then
673 return False;
675 elsif Nkind (C1) in N_Unary_Op then
676 return Same_Expression (Right_Opnd (C1), Right_Opnd (C2));
678 elsif Nkind (C1) in N_Binary_Op then
679 return Same_Expression (Left_Opnd (C1), Left_Opnd (C2))
680 and then
681 Same_Expression (Right_Opnd (C1), Right_Opnd (C2));
683 elsif Nkind (C1) = N_Null then
684 return True;
686 else
687 return False;
688 end if;
689 end Same_Expression;
691 -- Start of processing for Analyze_Raise_xxx_Error
693 begin
694 if Nkind (Original_Node (N)) = N_Raise_Statement then
695 Check_SPARK_Restriction ("raise statement is not allowed", N);
696 end if;
698 if No (Etype (N)) then
699 Set_Etype (N, Standard_Void_Type);
700 end if;
702 if Present (Condition (N)) then
703 Analyze_And_Resolve (Condition (N), Standard_Boolean);
704 end if;
706 -- Deal with static cases in obvious manner
708 if Nkind (Condition (N)) = N_Identifier then
709 if Entity (Condition (N)) = Standard_True then
710 Set_Condition (N, Empty);
712 elsif Entity (Condition (N)) = Standard_False then
713 Rewrite (N, Make_Null_Statement (Sloc (N)));
714 end if;
715 end if;
717 -- Remove duplicate raise statements. Note that the previous one may
718 -- already have been removed as well.
720 if not Comes_From_Source (N)
721 and then Nkind (N) /= N_Null_Statement
722 and then Is_List_Member (N)
723 and then Present (Prev (N))
724 and then Nkind (N) = Nkind (Original_Node (Prev (N)))
725 and then Same_Expression
726 (Condition (N), Condition (Original_Node (Prev (N))))
727 then
728 Rewrite (N, Make_Null_Statement (Sloc (N)));
729 end if;
730 end Analyze_Raise_xxx_Error;
732 -----------------------------
733 -- Analyze_Subprogram_Info --
734 -----------------------------
736 procedure Analyze_Subprogram_Info (N : Node_Id) is
737 begin
738 Set_Etype (N, RTE (RE_Code_Loc));
739 end Analyze_Subprogram_Info;
741 end Sem_Ch11;