OSDN Git Service

2008-05-27 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch11.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             P A R . C H 1 1                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, 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 ------------------------------------------------------------------------------
25
26 pragma Style_Checks (All_Checks);
27 --  Turn off subprogram body ordering check. Subprograms are in order
28 --  by RM section rather than alphabetical
29
30 with Sinfo.CN; use Sinfo.CN;
31
32 separate (Par)
33 package body Ch11 is
34
35    --  Local functions, used only in this chapter
36
37    function P_Exception_Handler  return Node_Id;
38    function P_Exception_Choice   return Node_Id;
39
40    ---------------------------------
41    -- 11.1  Exception Declaration --
42    ---------------------------------
43
44    --  Parsed by P_Identifier_Declaration (3.3.1)
45
46    ------------------------------------------
47    -- 11.2  Handled Sequence Of Statements --
48    ------------------------------------------
49
50    --  HANDLED_SEQUENCE_OF_STATEMENTS ::=
51    --      SEQUENCE_OF_STATEMENTS
52    --    [exception
53    --      EXCEPTION_HANDLER
54    --      {EXCEPTION_HANDLER}]
55
56    --  Error_Recovery : Cannot raise Error_Resync
57
58    function P_Handled_Sequence_Of_Statements return Node_Id is
59       Handled_Stmt_Seq_Node : Node_Id;
60
61    begin
62       Handled_Stmt_Seq_Node :=
63         New_Node (N_Handled_Sequence_Of_Statements, Token_Ptr);
64       Set_Statements
65         (Handled_Stmt_Seq_Node, P_Sequence_Of_Statements (SS_Extm_Sreq));
66
67       if Token = Tok_Exception then
68          Scan; -- past EXCEPTION
69          Set_Exception_Handlers
70            (Handled_Stmt_Seq_Node, Parse_Exception_Handlers);
71       end if;
72
73       return Handled_Stmt_Seq_Node;
74    end P_Handled_Sequence_Of_Statements;
75
76    -----------------------------
77    -- 11.2  Exception Handler --
78    -----------------------------
79
80    --  EXCEPTION_HANDLER ::=
81    --    when [CHOICE_PARAMETER_SPECIFICATION :]
82    --      EXCEPTION_CHOICE {| EXCEPTION_CHOICE} =>
83    --        SEQUENCE_OF_STATEMENTS
84
85    --  CHOICE_PARAMETER_SPECIFICATION ::= DEFINING_IDENTIFIER
86
87    --  Error recovery: cannot raise Error_Resync
88
89    function P_Exception_Handler return Node_Id is
90       Scan_State        : Saved_Scan_State;
91       Handler_Node      : Node_Id;
92       Choice_Param_Node : Node_Id;
93
94    begin
95       Handler_Node := New_Node (N_Exception_Handler, Token_Ptr);
96       Set_Local_Raise_Statements (Handler_Node, No_Elist);
97
98       if Style_Check then
99          Style.Check_Indentation;
100       end if;
101
102       T_When;
103
104       --  Test for possible choice parameter present
105
106       if Token = Tok_Identifier then
107          Choice_Param_Node := Token_Node;
108          Save_Scan_State (Scan_State); -- at identifier
109          Scan; -- past identifier
110
111          if Token = Tok_Colon then
112             if Ada_Version = Ada_83 then
113                Error_Msg_SP ("(Ada 83) choice parameter not allowed!");
114             end if;
115
116             Scan; -- past :
117             Change_Identifier_To_Defining_Identifier (Choice_Param_Node);
118             Set_Choice_Parameter (Handler_Node, Choice_Param_Node);
119
120          elsif Token = Tok_Others then
121             Error_Msg_AP ("missing "":""");
122             Change_Identifier_To_Defining_Identifier (Choice_Param_Node);
123             Set_Choice_Parameter (Handler_Node, Choice_Param_Node);
124
125          else
126             Restore_Scan_State (Scan_State); -- to identifier
127          end if;
128       end if;
129
130       --  Loop through exception choices
131
132       Set_Exception_Choices (Handler_Node, New_List);
133
134       loop
135          Append (P_Exception_Choice, Exception_Choices (Handler_Node));
136          exit when Token /= Tok_Vertical_Bar;
137          Scan; -- past vertical bar
138       end loop;
139
140       TF_Arrow;
141       Set_Statements (Handler_Node, P_Sequence_Of_Statements (SS_Sreq_Whtm));
142       return Handler_Node;
143    end P_Exception_Handler;
144
145    ------------------------------------------
146    -- 11.2  Choice Parameter Specification --
147    ------------------------------------------
148
149    --  Parsed by P_Exception_Handler (11.2)
150
151    ----------------------------
152    -- 11.2  Exception Choice --
153    ----------------------------
154
155    --  EXCEPTION_CHOICE ::= exception_NAME | others
156
157    --  Error recovery: cannot raise Error_Resync. If an error occurs, then the
158    --  scan pointer is advanced to the next arrow or vertical bar or semicolon.
159
160    function P_Exception_Choice return Node_Id is
161    begin
162
163       if Token = Tok_Others then
164          Scan; -- past OTHERS
165          return New_Node (N_Others_Choice, Prev_Token_Ptr);
166
167       else
168          return P_Name; -- exception name
169       end if;
170
171    exception
172       when Error_Resync =>
173          Resync_Choice;
174          return Error;
175    end P_Exception_Choice;
176
177    ---------------------------
178    -- 11.3  Raise Statement --
179    ---------------------------
180
181    --  RAISE_STATEMENT ::= raise [exception_NAME];
182
183    --  The caller has verified that the initial token is RAISE
184
185    --  Error recovery: can raise Error_Resync
186
187    function P_Raise_Statement return Node_Id is
188       Raise_Node : Node_Id;
189
190    begin
191       Raise_Node := New_Node (N_Raise_Statement, Token_Ptr);
192       Scan; -- past RAISE
193
194       if Token /= Tok_Semicolon then
195          Set_Name (Raise_Node, P_Name);
196       end if;
197
198       if Token = Tok_With then
199          if Ada_Version < Ada_05 then
200             Error_Msg_SC ("string expression in raise is Ada 2005 extension");
201             Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
202          end if;
203
204          Scan; -- past WITH
205          Set_Expression (Raise_Node, P_Expression);
206       end if;
207
208       TF_Semicolon;
209       return Raise_Node;
210    end P_Raise_Statement;
211
212    ------------------------------
213    -- Parse_Exception_Handlers --
214    ------------------------------
215
216    --  This routine scans out a list of exception handlers appearing in a
217    --  construct as:
218
219    --    exception
220    --      EXCEPTION_HANDLER {EXCEPTION_HANDLER}
221
222    --  The caller has scanned out the EXCEPTION keyword
223
224    --  Control returns after scanning the last exception handler, presumably
225    --  at the keyword END, but this is not checked in this routine.
226
227    --  Error recovery: cannot raise Error_Resync
228
229    function Parse_Exception_Handlers return List_Id is
230       Handler       : Node_Id;
231       Handlers_List : List_Id;
232
233    begin
234       Handlers_List := New_List;
235       P_Pragmas_Opt (Handlers_List);
236
237       if Token = Tok_End then
238          Error_Msg_SC ("must have at least one exception handler!");
239
240       else
241          loop
242             Handler := P_Exception_Handler;
243             Append (Handler, Handlers_List);
244
245             --  Note: no need to check for pragmas here. Although the
246             --  syntax officially allows them in this position, they
247             --  will have been swallowed up as part of the statement
248             --  sequence of the handler we just scanned out.
249
250             exit when Token /= Tok_When;
251          end loop;
252       end if;
253
254       return Handlers_List;
255    end Parse_Exception_Handlers;
256
257 end Ch11;