Rebase.
[official-gcc.git] / gcc / ada / sem_ch11.adb
blob45b4a082a47be6c405b2e562ba494477dd72ea39
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 if Has_Aspects (N) then
68 Analyze_Aspect_Specifications (N, Id);
69 end if;
70 end Analyze_Exception_Declaration;
72 --------------------------------
73 -- Analyze_Exception_Handlers --
74 --------------------------------
76 procedure Analyze_Exception_Handlers (L : List_Id) is
77 Handler : Node_Id;
78 Choice : Entity_Id;
79 Id : Node_Id;
80 H_Scope : Entity_Id := Empty;
82 procedure Check_Duplication (Id : Node_Id);
83 -- Iterate through the identifiers in each handler to find duplicates
85 function Others_Present return Boolean;
86 -- Returns True if others handler is present
88 -----------------------
89 -- Check_Duplication --
90 -----------------------
92 procedure Check_Duplication (Id : Node_Id) is
93 Handler : Node_Id;
94 Id1 : Node_Id;
95 Id_Entity : Entity_Id := Entity (Id);
97 begin
98 if Present (Renamed_Entity (Id_Entity)) then
99 Id_Entity := Renamed_Entity (Id_Entity);
100 end if;
102 Handler := First_Non_Pragma (L);
103 while Present (Handler) loop
104 Id1 := First (Exception_Choices (Handler));
105 while Present (Id1) loop
107 -- Only check against the exception choices which precede
108 -- Id in the handler, since the ones that follow Id have not
109 -- been analyzed yet and will be checked in a subsequent call.
111 if Id = Id1 then
112 return;
114 elsif Nkind (Id1) /= N_Others_Choice
115 and then
116 (Id_Entity = Entity (Id1)
117 or else (Id_Entity = Renamed_Entity (Entity (Id1))))
118 then
119 if Handler /= Parent (Id) then
120 Error_Msg_Sloc := Sloc (Id1);
121 Error_Msg_NE
122 ("exception choice duplicates &#", Id, Id1);
124 else
125 if Ada_Version = Ada_83
126 and then Comes_From_Source (Id)
127 then
128 Error_Msg_N
129 ("(Ada 83): duplicate exception choice&", Id);
130 end if;
131 end if;
132 end if;
134 Next_Non_Pragma (Id1);
135 end loop;
137 Next (Handler);
138 end loop;
139 end Check_Duplication;
141 --------------------
142 -- Others_Present --
143 --------------------
145 function Others_Present return Boolean is
146 H : Node_Id;
148 begin
149 H := First (L);
150 while Present (H) loop
151 if Nkind (H) /= N_Pragma
152 and then Nkind (First (Exception_Choices (H))) = N_Others_Choice
153 then
154 return True;
155 end if;
157 Next (H);
158 end loop;
160 return False;
161 end Others_Present;
163 -- Start of processing for Analyze_Exception_Handlers
165 begin
166 Handler := First (L);
167 Check_Restriction (No_Exceptions, Handler);
168 Check_Restriction (No_Exception_Handlers, Handler);
170 -- Kill current remembered values, since we don't know where we were
171 -- when the exception was raised.
173 Kill_Current_Values;
175 -- Loop through handlers (which can include pragmas)
177 while Present (Handler) loop
179 -- If pragma just analyze it
181 if Nkind (Handler) = N_Pragma then
182 Analyze (Handler);
184 -- Otherwise we have a real exception handler
186 else
187 -- Deal with choice parameter. The exception handler is a
188 -- declarative part for the choice parameter, so it constitutes a
189 -- scope for visibility purposes. We create an entity to denote
190 -- the whole exception part, and use it as the scope of all the
191 -- choices, which may even have the same name without conflict.
192 -- This scope plays no other role in expansion or code generation.
194 Choice := Choice_Parameter (Handler);
196 if Present (Choice) then
197 Set_Local_Raise_Not_OK (Handler);
199 if Comes_From_Source (Choice) then
200 Check_Restriction (No_Exception_Propagation, Choice);
201 Set_Debug_Info_Needed (Choice);
202 end if;
204 if No (H_Scope) then
205 H_Scope :=
206 New_Internal_Entity
207 (E_Block, Current_Scope, Sloc (Choice), 'E');
208 end if;
210 Push_Scope (H_Scope);
211 Set_Etype (H_Scope, Standard_Void_Type);
213 Enter_Name (Choice);
214 Set_Ekind (Choice, E_Variable);
216 if RTE_Available (RE_Exception_Occurrence) then
217 Set_Etype (Choice, RTE (RE_Exception_Occurrence));
218 end if;
220 Generate_Definition (Choice);
222 -- Indicate that choice has an initial value, since in effect
223 -- this field is assigned an initial value by the exception.
224 -- We also consider that it is modified in the source.
226 Set_Has_Initial_Value (Choice, True);
227 Set_Never_Set_In_Source (Choice, False);
228 end if;
230 Id := First (Exception_Choices (Handler));
231 while Present (Id) loop
232 if Nkind (Id) = N_Others_Choice then
233 if Present (Next (Id))
234 or else Present (Next (Handler))
235 or else Present (Prev (Id))
236 then
237 Error_Msg_N ("OTHERS must appear alone and last", Id);
238 end if;
240 else
241 Analyze (Id);
243 -- In most cases the choice has already been analyzed in
244 -- Analyze_Handled_Statement_Sequence, in order to expand
245 -- local handlers. This advance analysis does not take into
246 -- account the case in which a choice has the same name as
247 -- the choice parameter of the handler, which may hide an
248 -- outer exception. This pathological case appears in ACATS
249 -- B80001_3.adb, and requires an explicit check to verify
250 -- that the id is not hidden.
252 if not Is_Entity_Name (Id)
253 or else Ekind (Entity (Id)) /= E_Exception
254 or else
255 (Nkind (Id) = N_Identifier
256 and then Chars (Id) = Chars (Choice))
257 then
258 Error_Msg_N ("exception name expected", Id);
260 else
261 -- Emit a warning at the declaration level when a local
262 -- exception is never raised explicitly.
264 if Warn_On_Redundant_Constructs
265 and then not Is_Raised (Entity (Id))
266 and then Scope (Entity (Id)) = Current_Scope
267 then
268 Error_Msg_NE
269 ("exception & is never raised?r?", Entity (Id), Id);
270 end if;
272 if Present (Renamed_Entity (Entity (Id))) then
273 if Entity (Id) = Standard_Numeric_Error then
274 Check_Restriction (No_Obsolescent_Features, Id);
276 if Warn_On_Obsolescent_Feature then
277 Error_Msg_N
278 ("Numeric_Error is an " &
279 "obsolescent feature (RM J.6(1))?j?", Id);
280 Error_Msg_N
281 ("\use Constraint_Error instead?j?", Id);
282 end if;
283 end if;
284 end if;
286 Check_Duplication (Id);
288 -- Check for exception declared within generic formal
289 -- package (which is illegal, see RM 11.2(8))
291 declare
292 Ent : Entity_Id := Entity (Id);
293 Scop : Entity_Id;
295 begin
296 if Present (Renamed_Entity (Ent)) then
297 Ent := Renamed_Entity (Ent);
298 end if;
300 Scop := Scope (Ent);
301 while Scop /= Standard_Standard
302 and then Ekind (Scop) = E_Package
303 loop
304 if Nkind (Declaration_Node (Scop)) =
305 N_Package_Specification
306 and then
307 Nkind (Original_Node (Parent
308 (Declaration_Node (Scop)))) =
309 N_Formal_Package_Declaration
310 then
311 Error_Msg_NE
312 ("exception& is declared in " &
313 "generic formal package", Id, Ent);
314 Error_Msg_N
315 ("\and therefore cannot appear in " &
316 "handler (RM 11.2(8))", Id);
317 exit;
319 -- If the exception is declared in an inner
320 -- instance, nothing else to check.
322 elsif Is_Generic_Instance (Scop) then
323 exit;
324 end if;
326 Scop := Scope (Scop);
327 end loop;
328 end;
329 end if;
330 end if;
332 Next (Id);
333 end loop;
335 -- Check for redundant handler (has only raise statement) and is
336 -- either an others handler, or is a specific handler when no
337 -- others handler is present.
339 if Warn_On_Redundant_Constructs
340 and then List_Length (Statements (Handler)) = 1
341 and then Nkind (First (Statements (Handler))) = N_Raise_Statement
342 and then No (Name (First (Statements (Handler))))
343 and then (not Others_Present
344 or else Nkind (First (Exception_Choices (Handler))) =
345 N_Others_Choice)
346 then
347 Error_Msg_N
348 ("useless handler contains only a reraise statement?r?",
349 Handler);
350 end if;
352 -- Now analyze the statements of this handler
354 Analyze_Statements (Statements (Handler));
356 -- If a choice was present, we created a special scope for it,
357 -- so this is where we pop that special scope to get rid of it.
359 if Present (Choice) then
360 End_Scope;
361 end if;
362 end if;
364 Next (Handler);
365 end loop;
366 end Analyze_Exception_Handlers;
368 --------------------------------
369 -- Analyze_Handled_Statements --
370 --------------------------------
372 procedure Analyze_Handled_Statements (N : Node_Id) is
373 Handlers : constant List_Id := Exception_Handlers (N);
374 Handler : Node_Id;
375 Choice : Node_Id;
377 begin
378 if Present (Handlers) then
379 Kill_All_Checks;
380 end if;
382 -- We are now going to analyze the statements and then the exception
383 -- handlers. We certainly need to do things in this order to get the
384 -- proper sequential semantics for various warnings.
386 -- However, there is a glitch. When we process raise statements, an
387 -- optimization is to look for local handlers and specialize the code
388 -- in this case.
390 -- In order to detect if a handler is matching, we must have at least
391 -- analyzed the choices in the proper scope so that proper visibility
392 -- analysis is performed. Hence we analyze just the choices first,
393 -- before we analyze the statement sequence.
395 Handler := First_Non_Pragma (Handlers);
396 while Present (Handler) loop
397 Choice := First_Non_Pragma (Exception_Choices (Handler));
398 while Present (Choice) loop
399 Analyze (Choice);
400 Next_Non_Pragma (Choice);
401 end loop;
403 Next_Non_Pragma (Handler);
404 end loop;
406 -- Analyze statements in sequence
408 Analyze_Statements (Statements (N));
410 -- If the current scope is a subprogram, then this is the right place to
411 -- check for hanging useless assignments from the statement sequence of
412 -- the subprogram body.
414 if Is_Subprogram (Current_Scope) then
415 Warn_On_Useless_Assignments (Current_Scope);
416 end if;
418 -- Deal with handlers or AT END proc
420 if Present (Handlers) then
421 Analyze_Exception_Handlers (Handlers);
422 elsif Present (At_End_Proc (N)) then
423 Analyze (At_End_Proc (N));
424 end if;
425 end Analyze_Handled_Statements;
427 ------------------------------
428 -- Analyze_Raise_Expression --
429 ------------------------------
431 procedure Analyze_Raise_Expression (N : Node_Id) is
432 Exception_Id : constant Node_Id := Name (N);
433 Exception_Name : Entity_Id := Empty;
435 begin
436 if Comes_From_Source (N) then
437 Check_Compiler_Unit ("raise expression", N);
438 end if;
440 Check_SPARK_05_Restriction ("raise expression is not allowed", N);
442 -- Check exception restrictions on the original source
444 if Comes_From_Source (N) then
445 Check_Restriction (No_Exceptions, N);
446 end if;
448 Analyze (Exception_Id);
450 if Is_Entity_Name (Exception_Id) then
451 Exception_Name := Entity (Exception_Id);
452 end if;
454 if No (Exception_Name)
455 or else Ekind (Exception_Name) /= E_Exception
456 then
457 Error_Msg_N
458 ("exception name expected in raise statement", Exception_Id);
459 else
460 Set_Is_Raised (Exception_Name);
461 end if;
463 -- Deal with RAISE WITH case
465 if Present (Expression (N)) then
466 Analyze_And_Resolve (Expression (N), Standard_String);
467 end if;
469 -- Check obsolescent use of Numeric_Error
471 if Exception_Name = Standard_Numeric_Error then
472 Check_Restriction (No_Obsolescent_Features, Exception_Id);
473 end if;
475 -- Kill last assignment indication
477 Kill_Current_Values (Last_Assignment_Only => True);
479 -- Raise_Type is compatible with all other types so that the raise
480 -- expression is legal in any expression context. It will be eventually
481 -- replaced by the concrete type imposed by the context.
483 Set_Etype (N, Raise_Type);
484 end Analyze_Raise_Expression;
486 -----------------------------
487 -- Analyze_Raise_Statement --
488 -----------------------------
490 procedure Analyze_Raise_Statement (N : Node_Id) is
491 Exception_Id : constant Node_Id := Name (N);
492 Exception_Name : Entity_Id := Empty;
493 P : Node_Id;
494 Par : Node_Id;
496 begin
497 if Comes_From_Source (N) then
498 Check_SPARK_05_Restriction ("raise statement is not allowed", N);
499 end if;
501 Check_Unreachable_Code (N);
503 -- Check exception restrictions on the original source
505 if Comes_From_Source (N) then
506 Check_Restriction (No_Exceptions, N);
507 end if;
509 -- Check for useless assignment to OUT or IN OUT scalar preceding the
510 -- raise. Right now only look at assignment statements, could do more???
512 if Is_List_Member (N) then
513 declare
514 P : Node_Id;
515 L : Node_Id;
517 begin
518 P := Prev (N);
520 -- Skip past null statements and pragmas
522 while Present (P)
523 and then Nkind_In (P, N_Null_Statement, N_Pragma)
524 loop
525 P := Prev (P);
526 end loop;
528 -- See if preceding statement is an assignment
530 if Present (P)
531 and then Nkind (P) = N_Assignment_Statement
532 then
533 L := Name (P);
535 -- Give warning for assignment to scalar formal
537 if Is_Scalar_Type (Etype (L))
538 and then Is_Entity_Name (L)
539 and then Is_Formal (Entity (L))
541 -- Do this only for parameters to the current subprogram.
542 -- This avoids some false positives for the nested case.
544 and then Nearest_Dynamic_Scope (Current_Scope) =
545 Scope (Entity (L))
547 then
548 -- Don't give warning if we are covered by an exception
549 -- handler, since this may result in false positives, since
550 -- the handler may handle the exception and return normally.
552 -- First find the enclosing handled sequence of statements
553 -- (note, we could also look for a handler in an outer block
554 -- but currently we don't, and in that case we'll emit the
555 -- warning).
557 Par := N;
558 loop
559 Par := Parent (Par);
560 exit when Nkind (Par) = N_Handled_Sequence_Of_Statements;
561 end loop;
563 -- See if there is a handler, give message if not
565 if No (Exception_Handlers (Par)) then
566 Error_Msg_N
567 ("assignment to pass-by-copy formal " &
568 "may have no effect??", P);
569 Error_Msg_N
570 ("\RAISE statement may result in abnormal return" &
571 " (RM 6.4.1(17))??", P);
572 end if;
573 end if;
574 end if;
575 end;
576 end if;
578 -- Reraise statement
580 if No (Exception_Id) then
581 P := Parent (N);
582 while not Nkind_In (P, N_Exception_Handler,
583 N_Subprogram_Body,
584 N_Package_Body,
585 N_Task_Body,
586 N_Entry_Body)
587 loop
588 P := Parent (P);
589 end loop;
591 if Nkind (P) /= N_Exception_Handler then
592 Error_Msg_N
593 ("reraise statement must appear directly in a handler", N);
595 -- If a handler has a reraise, it cannot be the target of a local
596 -- raise (goto optimization is impossible), and if the no exception
597 -- propagation restriction is set, this is a violation.
599 else
600 Set_Local_Raise_Not_OK (P);
602 -- Do not check the restriction if the reraise statement is part
603 -- of the code generated for an AT-END handler. That's because
604 -- if the restriction is actually active, we never generate this
605 -- raise anyway, so the apparent violation is bogus.
607 if not From_At_End (N) then
608 Check_Restriction (No_Exception_Propagation, N);
609 end if;
610 end if;
612 -- Normal case with exception id present
614 else
615 Analyze (Exception_Id);
617 if Is_Entity_Name (Exception_Id) then
618 Exception_Name := Entity (Exception_Id);
619 end if;
621 if No (Exception_Name)
622 or else Ekind (Exception_Name) /= E_Exception
623 then
624 Error_Msg_N
625 ("exception name expected in raise statement", Exception_Id);
626 else
627 Set_Is_Raised (Exception_Name);
628 end if;
630 -- Deal with RAISE WITH case
632 if Present (Expression (N)) then
633 Analyze_And_Resolve (Expression (N), Standard_String);
634 end if;
635 end if;
637 -- Check obsolescent use of Numeric_Error
639 if Exception_Name = Standard_Numeric_Error then
640 Check_Restriction (No_Obsolescent_Features, Exception_Id);
641 end if;
643 -- Kill last assignment indication
645 Kill_Current_Values (Last_Assignment_Only => True);
646 end Analyze_Raise_Statement;
648 -----------------------------
649 -- Analyze_Raise_xxx_Error --
650 -----------------------------
652 -- Normally, the Etype is already set (when this node is used within
653 -- an expression, since it is copied from the node which it rewrites).
654 -- If this node is used in a statement context, then we set the type
655 -- Standard_Void_Type. This is used both by Gigi and by the front end
656 -- to distinguish the statement use and the subexpression use.
658 -- The only other required processing is to take care of the Condition
659 -- field if one is present.
661 procedure Analyze_Raise_xxx_Error (N : Node_Id) is
663 function Same_Expression (C1, C2 : Node_Id) return Boolean;
664 -- It often occurs that two identical raise statements are generated in
665 -- succession (for example when dynamic elaboration checks take place on
666 -- separate expressions in a call). If the two statements are identical
667 -- according to the simple criterion that follows, the raise is
668 -- converted into a null statement.
670 ---------------------
671 -- Same_Expression --
672 ---------------------
674 function Same_Expression (C1, C2 : Node_Id) return Boolean is
675 begin
676 if No (C1) and then No (C2) then
677 return True;
679 elsif Is_Entity_Name (C1) and then Is_Entity_Name (C2) then
680 return Entity (C1) = Entity (C2);
682 elsif Nkind (C1) /= Nkind (C2) then
683 return False;
685 elsif Nkind (C1) in N_Unary_Op then
686 return Same_Expression (Right_Opnd (C1), Right_Opnd (C2));
688 elsif Nkind (C1) in N_Binary_Op then
689 return Same_Expression (Left_Opnd (C1), Left_Opnd (C2))
690 and then
691 Same_Expression (Right_Opnd (C1), Right_Opnd (C2));
693 elsif Nkind (C1) = N_Null then
694 return True;
696 else
697 return False;
698 end if;
699 end Same_Expression;
701 -- Start of processing for Analyze_Raise_xxx_Error
703 begin
704 if Nkind (Original_Node (N)) = N_Raise_Statement then
705 Check_SPARK_05_Restriction ("raise statement is not allowed", N);
706 end if;
708 if No (Etype (N)) then
709 Set_Etype (N, Standard_Void_Type);
710 end if;
712 if Present (Condition (N)) then
713 Analyze_And_Resolve (Condition (N), Standard_Boolean);
714 end if;
716 -- Deal with static cases in obvious manner
718 if Nkind (Condition (N)) = N_Identifier then
719 if Entity (Condition (N)) = Standard_True then
720 Set_Condition (N, Empty);
722 elsif Entity (Condition (N)) = Standard_False then
723 Rewrite (N, Make_Null_Statement (Sloc (N)));
724 end if;
725 end if;
727 -- Remove duplicate raise statements. Note that the previous one may
728 -- already have been removed as well.
730 if not Comes_From_Source (N)
731 and then Nkind (N) /= N_Null_Statement
732 and then Is_List_Member (N)
733 and then Present (Prev (N))
734 and then Nkind (N) = Nkind (Original_Node (Prev (N)))
735 and then Same_Expression
736 (Condition (N), Condition (Original_Node (Prev (N))))
737 then
738 Rewrite (N, Make_Null_Statement (Sloc (N)));
739 end if;
740 end Analyze_Raise_xxx_Error;
742 end Sem_Ch11;