2015-05-22 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch11.adb
blobca6c9639188dc2203783f504baab345185d1e337
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 Ghost; use Ghost;
31 with Lib; use Lib;
32 with Lib.Xref; use Lib.Xref;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
36 with Opt; use Opt;
37 with Restrict; use Restrict;
38 with Rident; use Rident;
39 with Rtsfind; use Rtsfind;
40 with Sem; use Sem;
41 with Sem_Aux; use Sem_Aux;
42 with Sem_Ch5; use Sem_Ch5;
43 with Sem_Ch8; use Sem_Ch8;
44 with Sem_Ch13; use Sem_Ch13;
45 with Sem_Res; use Sem_Res;
46 with Sem_Util; use Sem_Util;
47 with Sem_Warn; use Sem_Warn;
48 with Sinfo; use Sinfo;
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 -- The exception declaration may be subject to pragma Ghost with policy
63 -- Ignore. Set the mode now to ensure that any nodes generated during
64 -- analysis and expansion are properly flagged as ignored Ghost.
66 Set_Ghost_Mode (N);
68 Generate_Definition (Id);
69 Enter_Name (Id);
70 Set_Ekind (Id, E_Exception);
71 Set_Etype (Id, Standard_Exception_Type);
72 Set_Is_Statically_Allocated (Id);
73 Set_Is_Pure (Id, PF);
75 -- An exception declared within a Ghost region is automatically Ghost
76 -- (SPARK RM 6.9(2)).
78 if Ghost_Mode > None then
79 Set_Is_Ghost_Entity (Id);
80 end if;
82 if Has_Aspects (N) then
83 Analyze_Aspect_Specifications (N, Id);
84 end if;
85 end Analyze_Exception_Declaration;
87 --------------------------------
88 -- Analyze_Exception_Handlers --
89 --------------------------------
91 procedure Analyze_Exception_Handlers (L : List_Id) is
92 Handler : Node_Id;
93 Choice : Entity_Id;
94 Id : Node_Id;
95 H_Scope : Entity_Id := Empty;
97 procedure Check_Duplication (Id : Node_Id);
98 -- Iterate through the identifiers in each handler to find duplicates
100 function Others_Present return Boolean;
101 -- Returns True if others handler is present
103 -----------------------
104 -- Check_Duplication --
105 -----------------------
107 procedure Check_Duplication (Id : Node_Id) is
108 Handler : Node_Id;
109 Id1 : Node_Id;
110 Id_Entity : Entity_Id := Entity (Id);
112 begin
113 if Present (Renamed_Entity (Id_Entity)) then
114 Id_Entity := Renamed_Entity (Id_Entity);
115 end if;
117 Handler := First_Non_Pragma (L);
118 while Present (Handler) loop
119 Id1 := First (Exception_Choices (Handler));
120 while Present (Id1) loop
122 -- Only check against the exception choices which precede
123 -- Id in the handler, since the ones that follow Id have not
124 -- been analyzed yet and will be checked in a subsequent call.
126 if Id = Id1 then
127 return;
129 elsif Nkind (Id1) /= N_Others_Choice
130 and then
131 (Id_Entity = Entity (Id1)
132 or else (Id_Entity = Renamed_Entity (Entity (Id1))))
133 then
134 if Handler /= Parent (Id) then
135 Error_Msg_Sloc := Sloc (Id1);
136 Error_Msg_NE ("exception choice duplicates &#", Id, Id1);
138 else
139 if Ada_Version = Ada_83
140 and then Comes_From_Source (Id)
141 then
142 Error_Msg_N
143 ("(Ada 83): duplicate exception choice&", Id);
144 end if;
145 end if;
146 end if;
148 Next_Non_Pragma (Id1);
149 end loop;
151 Next (Handler);
152 end loop;
153 end Check_Duplication;
155 --------------------
156 -- Others_Present --
157 --------------------
159 function Others_Present return Boolean is
160 H : Node_Id;
162 begin
163 H := First (L);
164 while Present (H) loop
165 if Nkind (H) /= N_Pragma
166 and then Nkind (First (Exception_Choices (H))) = N_Others_Choice
167 then
168 return True;
169 end if;
171 Next (H);
172 end loop;
174 return False;
175 end Others_Present;
177 -- Start of processing for Analyze_Exception_Handlers
179 begin
180 Handler := First (L);
181 Check_Restriction (No_Exceptions, Handler);
182 Check_Restriction (No_Exception_Handlers, Handler);
184 -- Kill current remembered values, since we don't know where we were
185 -- when the exception was raised.
187 Kill_Current_Values;
189 -- Loop through handlers (which can include pragmas)
191 while Present (Handler) loop
193 -- If pragma just analyze it
195 if Nkind (Handler) = N_Pragma then
196 Analyze (Handler);
198 -- Otherwise we have a real exception handler
200 else
201 -- Deal with choice parameter. The exception handler is a
202 -- declarative part for the choice parameter, so it constitutes a
203 -- scope for visibility purposes. We create an entity to denote
204 -- the whole exception part, and use it as the scope of all the
205 -- choices, which may even have the same name without conflict.
206 -- This scope plays no other role in expansion or code generation.
208 Choice := Choice_Parameter (Handler);
210 if Present (Choice) then
211 Set_Local_Raise_Not_OK (Handler);
213 if Comes_From_Source (Choice) then
214 Check_Restriction (No_Exception_Propagation, Choice);
215 Set_Debug_Info_Needed (Choice);
216 end if;
218 if No (H_Scope) then
219 H_Scope :=
220 New_Internal_Entity
221 (E_Block, Current_Scope, Sloc (Choice), 'E');
222 end if;
224 Push_Scope (H_Scope);
225 Set_Etype (H_Scope, Standard_Void_Type);
227 Enter_Name (Choice);
228 Set_Ekind (Choice, E_Variable);
230 if RTE_Available (RE_Exception_Occurrence) then
231 Set_Etype (Choice, RTE (RE_Exception_Occurrence));
232 end if;
234 Generate_Definition (Choice);
236 -- Indicate that choice has an initial value, since in effect
237 -- this field is assigned an initial value by the exception.
238 -- We also consider that it is modified in the source.
240 Set_Has_Initial_Value (Choice, True);
241 Set_Never_Set_In_Source (Choice, False);
242 end if;
244 Id := First (Exception_Choices (Handler));
245 while Present (Id) loop
246 if Nkind (Id) = N_Others_Choice then
247 if Present (Next (Id))
248 or else Present (Next (Handler))
249 or else Present (Prev (Id))
250 then
251 Error_Msg_N ("OTHERS must appear alone and last", Id);
252 end if;
254 else
255 Analyze (Id);
257 -- In most cases the choice has already been analyzed in
258 -- Analyze_Handled_Statement_Sequence, in order to expand
259 -- local handlers. This advance analysis does not take into
260 -- account the case in which a choice has the same name as
261 -- the choice parameter of the handler, which may hide an
262 -- outer exception. This pathological case appears in ACATS
263 -- B80001_3.adb, and requires an explicit check to verify
264 -- that the id is not hidden.
266 if not Is_Entity_Name (Id)
267 or else Ekind (Entity (Id)) /= E_Exception
268 or else
269 (Nkind (Id) = N_Identifier
270 and then Chars (Id) = Chars (Choice))
271 then
272 Error_Msg_N ("exception name expected", Id);
274 else
275 -- Emit a warning at the declaration level when a local
276 -- exception is never raised explicitly.
278 if Warn_On_Redundant_Constructs
279 and then not Is_Raised (Entity (Id))
280 and then Scope (Entity (Id)) = Current_Scope
281 then
282 Error_Msg_NE
283 ("exception & is never raised?r?", Entity (Id), Id);
284 end if;
286 if Present (Renamed_Entity (Entity (Id))) then
287 if Entity (Id) = Standard_Numeric_Error then
288 Check_Restriction (No_Obsolescent_Features, Id);
290 if Warn_On_Obsolescent_Feature then
291 Error_Msg_N
292 ("Numeric_Error is an " &
293 "obsolescent feature (RM J.6(1))?j?", Id);
294 Error_Msg_N
295 ("\use Constraint_Error instead?j?", Id);
296 end if;
297 end if;
298 end if;
300 Check_Duplication (Id);
302 -- Check for exception declared within generic formal
303 -- package (which is illegal, see RM 11.2(8))
305 declare
306 Ent : Entity_Id := Entity (Id);
307 Scop : Entity_Id;
309 begin
310 if Present (Renamed_Entity (Ent)) then
311 Ent := Renamed_Entity (Ent);
312 end if;
314 Scop := Scope (Ent);
315 while Scop /= Standard_Standard
316 and then Ekind (Scop) = E_Package
317 loop
318 if Nkind (Declaration_Node (Scop)) =
319 N_Package_Specification
320 and then
321 Nkind (Original_Node (Parent
322 (Declaration_Node (Scop)))) =
323 N_Formal_Package_Declaration
324 then
325 Error_Msg_NE
326 ("exception& is declared in " &
327 "generic formal package", Id, Ent);
328 Error_Msg_N
329 ("\and therefore cannot appear in " &
330 "handler (RM 11.2(8))", Id);
331 exit;
333 -- If the exception is declared in an inner
334 -- instance, nothing else to check.
336 elsif Is_Generic_Instance (Scop) then
337 exit;
338 end if;
340 Scop := Scope (Scop);
341 end loop;
342 end;
343 end if;
344 end if;
346 Next (Id);
347 end loop;
349 -- Check for redundant handler (has only raise statement) and is
350 -- either an others handler, or is a specific handler when no
351 -- others handler is present.
353 if Warn_On_Redundant_Constructs
354 and then List_Length (Statements (Handler)) = 1
355 and then Nkind (First (Statements (Handler))) = N_Raise_Statement
356 and then No (Name (First (Statements (Handler))))
357 and then (not Others_Present
358 or else Nkind (First (Exception_Choices (Handler))) =
359 N_Others_Choice)
360 then
361 Error_Msg_N
362 ("useless handler contains only a reraise statement?r?",
363 Handler);
364 end if;
366 -- Now analyze the statements of this handler
368 Analyze_Statements (Statements (Handler));
370 -- If a choice was present, we created a special scope for it,
371 -- so this is where we pop that special scope to get rid of it.
373 if Present (Choice) then
374 End_Scope;
375 end if;
376 end if;
378 Next (Handler);
379 end loop;
380 end Analyze_Exception_Handlers;
382 --------------------------------
383 -- Analyze_Handled_Statements --
384 --------------------------------
386 procedure Analyze_Handled_Statements (N : Node_Id) is
387 Handlers : constant List_Id := Exception_Handlers (N);
388 Handler : Node_Id;
389 Choice : Node_Id;
391 begin
392 if Present (Handlers) then
393 Kill_All_Checks;
394 end if;
396 -- We are now going to analyze the statements and then the exception
397 -- handlers. We certainly need to do things in this order to get the
398 -- proper sequential semantics for various warnings.
400 -- However, there is a glitch. When we process raise statements, an
401 -- optimization is to look for local handlers and specialize the code
402 -- in this case.
404 -- In order to detect if a handler is matching, we must have at least
405 -- analyzed the choices in the proper scope so that proper visibility
406 -- analysis is performed. Hence we analyze just the choices first,
407 -- before we analyze the statement sequence.
409 Handler := First_Non_Pragma (Handlers);
410 while Present (Handler) loop
411 Choice := First_Non_Pragma (Exception_Choices (Handler));
412 while Present (Choice) loop
413 Analyze (Choice);
414 Next_Non_Pragma (Choice);
415 end loop;
417 Next_Non_Pragma (Handler);
418 end loop;
420 -- Analyze statements in sequence
422 Analyze_Statements (Statements (N));
424 -- If the current scope is a subprogram, then this is the right place to
425 -- check for hanging useless assignments from the statement sequence of
426 -- the subprogram body.
428 if Is_Subprogram (Current_Scope) then
429 Warn_On_Useless_Assignments (Current_Scope);
430 end if;
432 -- Deal with handlers or AT END proc
434 if Present (Handlers) then
435 Analyze_Exception_Handlers (Handlers);
436 elsif Present (At_End_Proc (N)) then
437 Analyze (At_End_Proc (N));
438 end if;
439 end Analyze_Handled_Statements;
441 ------------------------------
442 -- Analyze_Raise_Expression --
443 ------------------------------
445 procedure Analyze_Raise_Expression (N : Node_Id) is
446 Exception_Id : constant Node_Id := Name (N);
447 Exception_Name : Entity_Id := Empty;
449 begin
450 if Comes_From_Source (N) then
451 Check_Compiler_Unit ("raise expression", N);
452 end if;
454 Check_SPARK_05_Restriction ("raise expression is not allowed", N);
456 -- Check exception restrictions on the original source
458 if Comes_From_Source (N) then
459 Check_Restriction (No_Exceptions, N);
460 end if;
462 Analyze (Exception_Id);
464 if Is_Entity_Name (Exception_Id) then
465 Exception_Name := Entity (Exception_Id);
466 end if;
468 if No (Exception_Name)
469 or else Ekind (Exception_Name) /= E_Exception
470 then
471 Error_Msg_N
472 ("exception name expected in raise statement", Exception_Id);
473 else
474 Set_Is_Raised (Exception_Name);
475 end if;
477 -- Deal with RAISE WITH case
479 if Present (Expression (N)) then
480 Analyze_And_Resolve (Expression (N), Standard_String);
481 end if;
483 -- Check obsolescent use of Numeric_Error
485 if Exception_Name = Standard_Numeric_Error then
486 Check_Restriction (No_Obsolescent_Features, Exception_Id);
487 end if;
489 -- Kill last assignment indication
491 Kill_Current_Values (Last_Assignment_Only => True);
493 -- Raise_Type is compatible with all other types so that the raise
494 -- expression is legal in any expression context. It will be eventually
495 -- replaced by the concrete type imposed by the context.
497 Set_Etype (N, Raise_Type);
498 end Analyze_Raise_Expression;
500 -----------------------------
501 -- Analyze_Raise_Statement --
502 -----------------------------
504 procedure Analyze_Raise_Statement (N : Node_Id) is
505 Exception_Id : constant Node_Id := Name (N);
506 Exception_Name : Entity_Id := Empty;
507 P : Node_Id;
508 Par : Node_Id;
510 begin
511 if Comes_From_Source (N) then
512 Check_SPARK_05_Restriction ("raise statement is not allowed", N);
513 end if;
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_In (P, 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 not Nkind_In (P, N_Exception_Handler,
595 N_Subprogram_Body,
596 N_Package_Body,
597 N_Task_Body,
598 N_Entry_Body)
599 loop
600 P := Parent (P);
601 end loop;
603 if Nkind (P) /= N_Exception_Handler then
604 Error_Msg_N
605 ("reraise statement must appear directly in a handler", N);
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.
611 else
612 Set_Local_Raise_Not_OK (P);
614 -- Do not check the restriction if the reraise statement is part
615 -- of the code generated for an AT-END handler. That's because
616 -- if the restriction is actually active, we never generate this
617 -- raise anyway, so the apparent violation is bogus.
619 if not From_At_End (N) then
620 Check_Restriction (No_Exception_Propagation, N);
621 end if;
622 end if;
624 -- Normal case with exception id present
626 else
627 Analyze (Exception_Id);
629 if Is_Entity_Name (Exception_Id) then
630 Exception_Name := Entity (Exception_Id);
631 end if;
633 if No (Exception_Name)
634 or else Ekind (Exception_Name) /= E_Exception
635 then
636 Error_Msg_N
637 ("exception name expected in raise statement", Exception_Id);
638 else
639 Set_Is_Raised (Exception_Name);
640 end if;
642 -- Deal with RAISE WITH case
644 if Present (Expression (N)) then
645 Analyze_And_Resolve (Expression (N), Standard_String);
646 end if;
647 end if;
649 -- Check obsolescent use of Numeric_Error
651 if Exception_Name = Standard_Numeric_Error then
652 Check_Restriction (No_Obsolescent_Features, Exception_Id);
653 end if;
655 -- Kill last assignment indication
657 Kill_Current_Values (Last_Assignment_Only => True);
658 end Analyze_Raise_Statement;
660 -----------------------------
661 -- Analyze_Raise_xxx_Error --
662 -----------------------------
664 -- Normally, the Etype is already set (when this node is used within
665 -- an expression, since it is copied from the node which it rewrites).
666 -- If this node is used in a statement context, then we set the type
667 -- Standard_Void_Type. This is used both by Gigi and by the front end
668 -- to distinguish the statement use and the subexpression use.
670 -- The only other required processing is to take care of the Condition
671 -- field if one is present.
673 procedure Analyze_Raise_xxx_Error (N : Node_Id) is
675 function Same_Expression (C1, C2 : Node_Id) return Boolean;
676 -- It often occurs that two identical raise statements are generated in
677 -- succession (for example when dynamic elaboration checks take place on
678 -- separate expressions in a call). If the two statements are identical
679 -- according to the simple criterion that follows, the raise is
680 -- converted into a null statement.
682 ---------------------
683 -- Same_Expression --
684 ---------------------
686 function Same_Expression (C1, C2 : Node_Id) return Boolean is
687 begin
688 if No (C1) and then No (C2) then
689 return True;
691 elsif Is_Entity_Name (C1) and then Is_Entity_Name (C2) then
692 return Entity (C1) = Entity (C2);
694 elsif Nkind (C1) /= Nkind (C2) then
695 return False;
697 elsif Nkind (C1) in N_Unary_Op then
698 return Same_Expression (Right_Opnd (C1), Right_Opnd (C2));
700 elsif Nkind (C1) in N_Binary_Op then
701 return Same_Expression (Left_Opnd (C1), Left_Opnd (C2))
702 and then
703 Same_Expression (Right_Opnd (C1), Right_Opnd (C2));
705 elsif Nkind (C1) = N_Null then
706 return True;
708 else
709 return False;
710 end if;
711 end Same_Expression;
713 -- Start of processing for Analyze_Raise_xxx_Error
715 begin
716 if Nkind (Original_Node (N)) = N_Raise_Statement then
717 Check_SPARK_05_Restriction ("raise statement is not allowed", N);
718 end if;
720 if No (Etype (N)) then
721 Set_Etype (N, Standard_Void_Type);
722 end if;
724 if Present (Condition (N)) then
725 Analyze_And_Resolve (Condition (N), Standard_Boolean);
726 end if;
728 -- Deal with static cases in obvious manner
730 if Nkind (Condition (N)) = N_Identifier then
731 if Entity (Condition (N)) = Standard_True then
732 Set_Condition (N, Empty);
734 elsif Entity (Condition (N)) = Standard_False then
735 Rewrite (N, Make_Null_Statement (Sloc (N)));
736 end if;
737 end if;
739 -- Remove duplicate raise statements. Note that the previous one may
740 -- already have been removed as well.
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;
754 end Sem_Ch11;