OSDN Git Service

* config/pa/fptr.c: Update license header.
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch11.adb
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-2007, 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 2,  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 COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Einfo;    use Einfo;
30 with Errout;   use Errout;
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_Ch5;  use Sem_Ch5;
42 with Sem_Ch8;  use Sem_Ch8;
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;
49
50 package body Sem_Ch11 is
51
52    -----------------------------------
53    -- Analyze_Exception_Declaration --
54    -----------------------------------
55
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);
67    end Analyze_Exception_Declaration;
68
69    --------------------------------
70    -- Analyze_Exception_Handlers --
71    --------------------------------
72
73    procedure Analyze_Exception_Handlers (L : List_Id) is
74       Handler : Node_Id;
75       Choice  : Entity_Id;
76       Id      : Node_Id;
77       H_Scope : Entity_Id := Empty;
78
79       procedure Check_Duplication (Id : Node_Id);
80       --  Iterate through the identifiers in each handler to find duplicates
81
82       function Others_Present return Boolean;
83       --  Returns True if others handler is present
84
85       -----------------------
86       -- Check_Duplication --
87       -----------------------
88
89       procedure Check_Duplication (Id : Node_Id) is
90          Handler   : Node_Id;
91          Id1       : Node_Id;
92          Id_Entity : Entity_Id := Entity (Id);
93
94       begin
95          if Present (Renamed_Entity (Id_Entity)) then
96             Id_Entity := Renamed_Entity (Id_Entity);
97          end if;
98
99          Handler := First_Non_Pragma (L);
100          while Present (Handler) loop
101             Id1 := First (Exception_Choices (Handler));
102             while Present (Id1) loop
103
104                --  Only check against the exception choices which precede
105                --  Id in the handler, since the ones that follow Id have not
106                --  been analyzed yet and will be checked in a subsequent call.
107
108                if Id = Id1 then
109                   return;
110
111                elsif Nkind (Id1) /= N_Others_Choice
112                  and then
113                    (Id_Entity = Entity (Id1)
114                       or else (Id_Entity = Renamed_Entity (Entity (Id1))))
115                then
116                   if Handler /= Parent (Id) then
117                      Error_Msg_Sloc := Sloc (Id1);
118                      Error_Msg_NE
119                        ("exception choice duplicates &#", Id, Id1);
120
121                   else
122                      if Ada_Version = Ada_83
123                        and then Comes_From_Source (Id)
124                      then
125                         Error_Msg_N
126                           ("(Ada 83): duplicate exception choice&", Id);
127                      end if;
128                   end if;
129                end if;
130
131                Next_Non_Pragma (Id1);
132             end loop;
133
134             Next (Handler);
135          end loop;
136       end Check_Duplication;
137
138       --------------------
139       -- Others_Present --
140       --------------------
141
142       function Others_Present return Boolean is
143          H : Node_Id;
144
145       begin
146          H := First (L);
147          while Present (H) loop
148             if Nkind (H) /= N_Pragma
149               and then Nkind (First (Exception_Choices (H))) = N_Others_Choice
150             then
151                return True;
152             end if;
153
154             Next (H);
155          end loop;
156
157          return False;
158       end Others_Present;
159
160    --  Start processing for Analyze_Exception_Handlers
161
162    begin
163       Handler := First (L);
164       Check_Restriction (No_Exceptions, Handler);
165       Check_Restriction (No_Exception_Handlers, Handler);
166
167       --  Kill current remembered values, since we don't know where we were
168       --  when the exception was raised.
169
170       Kill_Current_Values;
171
172       --  Loop through handlers (which can include pragmas)
173
174       while Present (Handler) loop
175
176          --  If pragma just analyze it
177
178          if Nkind (Handler) = N_Pragma then
179             Analyze (Handler);
180
181          --  Otherwise we have a real exception handler
182
183          else
184             --  Deal with choice parameter. The exception handler is a
185             --  declarative part for the choice parameter, so it constitutes a
186             --  scope for visibility purposes. We create an entity to denote
187             --  the whole exception part, and use it as the scope of all the
188             --  choices, which may even have the same name without conflict.
189             --  This scope plays no other role in expansion or or code
190             --  generation.
191
192             Choice := Choice_Parameter (Handler);
193
194             if Present (Choice) then
195                Set_Local_Raise_Not_OK (Handler);
196
197                if Comes_From_Source (Choice) then
198                   Check_Restriction (No_Exception_Propagation, Choice);
199                end if;
200
201                if No (H_Scope) then
202                   H_Scope :=
203                     New_Internal_Entity
204                      (E_Block, Current_Scope, Sloc (Choice), 'E');
205                end if;
206
207                Push_Scope (H_Scope);
208                Set_Etype (H_Scope, Standard_Void_Type);
209
210                --  Set the Finalization Chain entity to Error means that it
211                --  should not be used at that level but the parent one should
212                --  be used instead.
213
214                --  ??? this usage needs documenting in Einfo/Exp_Ch7 ???
215                --  ??? using Error for this non-error condition is nasty ???
216
217                Set_Finalization_Chain_Entity (H_Scope, Error);
218
219                Enter_Name (Choice);
220                Set_Ekind (Choice, E_Variable);
221
222                if RTE_Available (RE_Exception_Occurrence) then
223                   Set_Etype (Choice, RTE (RE_Exception_Occurrence));
224                end if;
225
226                Generate_Definition (Choice);
227
228                --  Set source assigned flag, since in effect this field is
229                --  always assigned an initial value by the exception.
230
231                Set_Never_Set_In_Source (Choice, False);
232             end if;
233
234             Id := First (Exception_Choices (Handler));
235             while Present (Id) loop
236                if Nkind (Id) = N_Others_Choice then
237                   if Present (Next (Id))
238                     or else Present (Next (Handler))
239                     or else Present (Prev (Id))
240                   then
241                      Error_Msg_N ("OTHERS must appear alone and last", Id);
242                   end if;
243
244                else
245                   Analyze (Id);
246
247                   --  In most cases the choice has already been analyzed in
248                   --  Analyze_Handled_Statement_Sequence, in order to expand
249                   --  local handlers. This advance analysis does not take into
250                   --  account the case in which a choice has the same name as
251                   --  the choice parameter of the handler, which may hide an
252                   --  outer exception. This pathological case appears in ACATS
253                   --  B80001_3.adb, and requires an explicit check to verify
254                   --  that the id is not hidden.
255
256                   if not Is_Entity_Name (Id)
257                     or else Ekind (Entity (Id)) /= E_Exception
258                     or else
259                       (Nkind (Id) = N_Identifier
260                         and then Chars (Id) = Chars (Choice))
261                   then
262                      Error_Msg_N ("exception name expected", Id);
263
264                   else
265                      if Present (Renamed_Entity (Entity (Id))) then
266                         if Entity (Id) = Standard_Numeric_Error then
267                            Check_Restriction (No_Obsolescent_Features, Id);
268
269                            if Warn_On_Obsolescent_Feature then
270                               Error_Msg_N
271                                 ("Numeric_Error is an " &
272                                  "obsolescent feature ('R'M 'J.6(1))?", Id);
273                               Error_Msg_N
274                                 ("\use Constraint_Error instead?", Id);
275                            end if;
276                         end if;
277                      end if;
278
279                      Check_Duplication (Id);
280
281                      --  Check for exception declared within generic formal
282                      --  package (which is illegal, see RM 11.2(8))
283
284                      declare
285                         Ent  : Entity_Id := Entity (Id);
286                         Scop : Entity_Id;
287
288                      begin
289                         if Present (Renamed_Entity (Ent)) then
290                            Ent := Renamed_Entity (Ent);
291                         end if;
292
293                         Scop := Scope (Ent);
294                         while Scop /= Standard_Standard
295                           and then Ekind (Scop) = E_Package
296                         loop
297                            if Nkind (Declaration_Node (Scop)) =
298                                            N_Package_Specification
299                              and then
300                                Nkind (Original_Node (Parent
301                                  (Declaration_Node (Scop)))) =
302                                            N_Formal_Package_Declaration
303                            then
304                               Error_Msg_NE
305                                 ("exception& is declared in "  &
306                                  "generic formal package", Id, Ent);
307                               Error_Msg_N
308                                 ("\and therefore cannot appear in " &
309                                  "handler ('R'M 11.2(8))", Id);
310                               exit;
311
312                            --  If the exception is declared in an inner
313                            --  instance, nothing else to check.
314
315                            elsif Is_Generic_Instance (Scop) then
316                               exit;
317                            end if;
318
319                            Scop := Scope (Scop);
320                         end loop;
321                      end;
322                   end if;
323                end if;
324
325                Next (Id);
326             end loop;
327
328             --  Check for redundant handler (has only raise statement) and is
329             --  either an others handler, or is a specific handler when no
330             --  others handler is present.
331
332             if Warn_On_Redundant_Constructs
333               and then List_Length (Statements (Handler)) = 1
334               and then Nkind (First (Statements (Handler))) = N_Raise_Statement
335               and then No (Name (First (Statements (Handler))))
336               and then (not Others_Present
337                           or else Nkind (First (Exception_Choices (Handler))) =
338                                               N_Others_Choice)
339             then
340                Error_Msg_N
341                  ("useless handler contains only a reraise statement?",
342                   Handler);
343             end if;
344
345             --  Now analyze the statements of this handler
346
347             Analyze_Statements (Statements (Handler));
348
349             --  If a choice was present, we created a special scope for it,
350             --  so this is where we pop that special scope to get rid of it.
351
352             if Present (Choice) then
353                End_Scope;
354             end if;
355          end if;
356
357          Next (Handler);
358       end loop;
359    end Analyze_Exception_Handlers;
360
361    --------------------------------
362    -- Analyze_Handled_Statements --
363    --------------------------------
364
365    procedure Analyze_Handled_Statements (N : Node_Id) is
366       Handlers : constant List_Id := Exception_Handlers (N);
367       Handler  : Node_Id;
368       Choice   : Node_Id;
369
370    begin
371       if Present (Handlers) then
372          Kill_All_Checks;
373       end if;
374
375       --  We are now going to analyze the statements and then the exception
376       --  handlers. We certainly need to do things in this order to get the
377       --  proper sequential semantics for various warnings.
378
379       --  However, there is a glitch. When we process raise statements, an
380       --  optimization is to look for local handlers and specialize the code
381       --  in this case.
382
383       --  In order to detect if a handler is matching, we must have at least
384       --  analyzed the choices in the proper scope so that proper visibility
385       --  analysis is performed. Hence we analyze just the choices first,
386       --  before we analyze the statement sequence.
387
388       Handler := First_Non_Pragma (Handlers);
389       while Present (Handler) loop
390          Choice := First_Non_Pragma (Exception_Choices (Handler));
391          while Present (Choice) loop
392             Analyze (Choice);
393             Next_Non_Pragma (Choice);
394          end loop;
395
396          Next_Non_Pragma (Handler);
397       end loop;
398
399       --  Analyze statements in sequence
400
401       Analyze_Statements (Statements (N));
402
403       --  If the current scope is a subprogram, then this is the right place to
404       --  check for hanging useless assignments from the statement sequence of
405       --  the subprogram body.
406
407       if Is_Subprogram (Current_Scope) then
408          Warn_On_Useless_Assignments (Current_Scope);
409       end if;
410
411       --  Deal with handlers or AT END proc
412
413       if Present (Handlers) then
414          Analyze_Exception_Handlers (Handlers);
415       elsif Present (At_End_Proc (N)) then
416          Analyze (At_End_Proc (N));
417       end if;
418    end Analyze_Handled_Statements;
419
420    -----------------------------
421    -- Analyze_Raise_Statement --
422    -----------------------------
423
424    procedure Analyze_Raise_Statement (N : Node_Id) is
425       Exception_Id   : constant Node_Id := Name (N);
426       Exception_Name : Entity_Id        := Empty;
427       P              : Node_Id;
428       Nkind_P        : Node_Kind;
429
430    begin
431       Check_Unreachable_Code (N);
432
433       --  Check exception restrictions on the original source
434
435       if Comes_From_Source (N) then
436          Check_Restriction (No_Exceptions, N);
437       end if;
438
439       --  Check for useless assignment to OUT or IN OUT scalar immediately
440       --  preceding the raise. Right now we only look at assignment statements,
441       --  we could do more.
442
443       if Is_List_Member (N) then
444          declare
445             P : Node_Id;
446             L : Node_Id;
447
448          begin
449             P := Prev (N);
450
451             if Present (P)
452               and then Nkind (P) = N_Assignment_Statement
453             then
454                L := Name (P);
455
456                if Is_Scalar_Type (Etype (L))
457                  and then Is_Entity_Name (L)
458                  and then Is_Formal (Entity (L))
459                then
460                   Error_Msg_N
461                     ("?assignment to pass-by-copy formal may have no effect",
462                       P);
463                   Error_Msg_N
464                     ("\?RAISE statement may result in abnormal return" &
465                      " ('R'M 6.4.1(17))", P);
466                end if;
467             end if;
468          end;
469       end if;
470
471       --  Reraise statement
472
473       if No (Exception_Id) then
474          P := Parent (N);
475          Nkind_P := Nkind (P);
476
477          while Nkind_P /= N_Exception_Handler
478            and then Nkind_P /= N_Subprogram_Body
479            and then Nkind_P /= N_Package_Body
480            and then Nkind_P /= N_Task_Body
481            and then Nkind_P /= N_Entry_Body
482          loop
483             P := Parent (P);
484             Nkind_P := Nkind (P);
485          end loop;
486
487          if Nkind (P) /= N_Exception_Handler then
488             Error_Msg_N
489               ("reraise statement must appear directly in a handler", N);
490
491          --  If a handler has a reraise, it cannot be the target of a local
492          --  raise (goto optimization is impossible), and if the no exception
493          --  propagation restriction is set, this is a violation.
494
495          else
496             Set_Local_Raise_Not_OK (P);
497             Check_Restriction (No_Exception_Propagation, N);
498          end if;
499
500       --  Normal case with exception id present
501
502       else
503          Analyze (Exception_Id);
504
505          if Is_Entity_Name (Exception_Id) then
506             Exception_Name := Entity (Exception_Id);
507          end if;
508
509          if No (Exception_Name)
510            or else Ekind (Exception_Name) /= E_Exception
511          then
512             Error_Msg_N
513               ("exception name expected in raise statement", Exception_Id);
514          end if;
515
516          if Present (Expression (N)) then
517             Analyze_And_Resolve (Expression (N), Standard_String);
518          end if;
519       end if;
520    end Analyze_Raise_Statement;
521
522    -----------------------------
523    -- Analyze_Raise_xxx_Error --
524    -----------------------------
525
526    --  Normally, the Etype is already set (when this node is used within
527    --  an expression, since it is copied from the node which it rewrites).
528    --  If this node is used in a statement context, then we set the type
529    --  Standard_Void_Type. This is used both by Gigi and by the front end
530    --  to distinguish the statement use and the subexpression use.
531
532    --  The only other required processing is to take care of the Condition
533    --  field if one is present.
534
535    procedure Analyze_Raise_xxx_Error (N : Node_Id) is
536    begin
537       if No (Etype (N)) then
538          Set_Etype (N, Standard_Void_Type);
539       end if;
540
541       if Present (Condition (N)) then
542          Analyze_And_Resolve (Condition (N), Standard_Boolean);
543       end if;
544
545       --  Deal with static cases in obvious manner
546
547       if Nkind (Condition (N)) = N_Identifier then
548          if Entity (Condition (N)) = Standard_True then
549             Set_Condition (N, Empty);
550
551          elsif Entity (Condition (N)) = Standard_False then
552             Rewrite (N, Make_Null_Statement (Sloc (N)));
553          end if;
554       end if;
555    end Analyze_Raise_xxx_Error;
556
557    -----------------------------
558    -- Analyze_Subprogram_Info --
559    -----------------------------
560
561    procedure Analyze_Subprogram_Info (N : Node_Id) is
562    begin
563       Set_Etype (N, RTE (RE_Code_Loc));
564    end Analyze_Subprogram_Info;
565
566 end Sem_Ch11;