| 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-2025, 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 |
begin |
| 61 |
Handled_Stmt_Seq_Node := |
| 62 |
New_Node (N_Handled_Sequence_Of_Statements, Token_Ptr); |
| 63 |
Set_Statements |
| 64 |
(Handled_Stmt_Seq_Node, P_Sequence_Of_Statements (SS_Extm_Fitm_Sreq)); |
| 65 |
|
| 66 |
if Token = Tok_Exception then |
| 67 |
Scan; -- past EXCEPTION |
| 68 |
Set_Exception_Handlers |
| 69 |
(Handled_Stmt_Seq_Node, Parse_Exception_Handlers); |
| 70 |
end if; |
| 71 |
|
| 72 |
if Token = Tok_Finally then |
| 73 |
Scan; -- past FINALLY |
| 74 |
Set_Finally_Statements |
| 75 |
(Handled_Stmt_Seq_Node, P_Sequence_Of_Statements (SS_Sreq)); |
| 76 |
end if; |
| 77 |
|
| 78 |
return Handled_Stmt_Seq_Node; |
| 79 |
end P_Handled_Sequence_Of_Statements; |
| 80 |
|
| 81 |
----------------------------- |
| 82 |
-- 11.2 Exception Handler -- |
| 83 |
----------------------------- |
| 84 |
|
| 85 |
-- EXCEPTION_HANDLER ::= |
| 86 |
-- when [CHOICE_PARAMETER_SPECIFICATION :] |
| 87 |
-- EXCEPTION_CHOICE {| EXCEPTION_CHOICE} => |
| 88 |
-- SEQUENCE_OF_STATEMENTS |
| 89 |
|
| 90 |
-- CHOICE_PARAMETER_SPECIFICATION ::= DEFINING_IDENTIFIER |
| 91 |
|
| 92 |
-- Error recovery: cannot raise Error_Resync |
| 93 |
|
| 94 |
function P_Exception_Handler return Node_Id is |
| 95 |
Scan_State : Saved_Scan_State; |
| 96 |
Handler_Node : Node_Id; |
| 97 |
Choice_Param_Node : Node_Id; |
| 98 |
|
| 99 |
begin |
| 100 |
Exception_Handler_Encountered := True; |
| 101 |
Handler_Node := New_Node (N_Exception_Handler, Token_Ptr); |
| 102 |
Set_Local_Raise_Statements (Handler_Node, No_Elist); |
| 103 |
|
| 104 |
if Style_Check then |
| 105 |
Style.Check_Indentation; |
| 106 |
end if; |
| 107 |
|
| 108 |
T_When; |
| 109 |
|
| 110 |
-- Test for possible choice parameter present |
| 111 |
|
| 112 |
if Token = Tok_Identifier then |
| 113 |
Choice_Param_Node := Token_Node; |
| 114 |
Save_Scan_State (Scan_State); -- at identifier |
| 115 |
Scan; -- past identifier |
| 116 |
|
| 117 |
if Token = Tok_Colon then |
| 118 |
if Ada_Version = Ada_83 then |
| 119 |
Error_Msg_SP ("(Ada 83) choice parameter not allowed!"); |
| 120 |
end if; |
| 121 |
|
| 122 |
Scan; -- past : |
| 123 |
Change_Identifier_To_Defining_Identifier (Choice_Param_Node); |
| 124 |
Warn_If_Standard_Redefinition (Choice_Param_Node); |
| 125 |
Set_Choice_Parameter (Handler_Node, Choice_Param_Node); |
| 126 |
|
| 127 |
elsif Token = Tok_Others then |
| 128 |
Error_Msg_AP -- CODEFIX |
| 129 |
("missing "":"""); |
| 130 |
Change_Identifier_To_Defining_Identifier (Choice_Param_Node); |
| 131 |
Warn_If_Standard_Redefinition (Choice_Param_Node); |
| 132 |
Set_Choice_Parameter (Handler_Node, Choice_Param_Node); |
| 133 |
|
| 134 |
else |
| 135 |
Restore_Scan_State (Scan_State); -- to identifier |
| 136 |
end if; |
| 137 |
end if; |
| 138 |
|
| 139 |
-- Loop through exception choices |
| 140 |
|
| 141 |
Set_Exception_Choices (Handler_Node, New_List); |
| 142 |
|
| 143 |
loop |
| 144 |
Append (P_Exception_Choice, Exception_Choices (Handler_Node)); |
| 145 |
exit when Token /= Tok_Vertical_Bar; |
| 146 |
Scan; -- past vertical bar |
| 147 |
end loop; |
| 148 |
|
| 149 |
TF_Arrow; |
| 150 |
Set_Statements |
| 151 |
(Handler_Node, P_Sequence_Of_Statements (SS_Sreq_Fitm_Whtm)); |
| 152 |
return Handler_Node; |
| 153 |
end P_Exception_Handler; |
| 154 |
|
| 155 |
------------------------------------------ |
| 156 |
-- 11.2 Choice Parameter Specification -- |
| 157 |
------------------------------------------ |
| 158 |
|
| 159 |
-- Parsed by P_Exception_Handler (11.2) |
| 160 |
|
| 161 |
---------------------------- |
| 162 |
-- 11.2 Exception Choice -- |
| 163 |
---------------------------- |
| 164 |
|
| 165 |
-- EXCEPTION_CHOICE ::= exception_NAME | others |
| 166 |
|
| 167 |
-- Error recovery: cannot raise Error_Resync. If an error occurs, then the |
| 168 |
-- scan pointer is advanced to the next arrow or vertical bar or semicolon. |
| 169 |
|
| 170 |
function P_Exception_Choice return Node_Id is |
| 171 |
begin |
| 172 |
if Token = Tok_Others then |
| 173 |
Scan; -- past OTHERS |
| 174 |
return New_Node (N_Others_Choice, Prev_Token_Ptr); |
| 175 |
|
| 176 |
else |
| 177 |
return P_Name; -- exception name |
| 178 |
end if; |
| 179 |
|
| 180 |
exception |
| 181 |
when Error_Resync => |
| 182 |
Resync_Choice; |
| 183 |
return Error; |
| 184 |
end P_Exception_Choice; |
| 185 |
|
| 186 |
---------------------------- |
| 187 |
-- 11.3 Raise Expression -- |
| 188 |
---------------------------- |
| 189 |
|
| 190 |
-- RAISE_EXPRESSION ::= raise [exception_NAME [with string_EXPRESSION]] |
| 191 |
|
| 192 |
-- The caller has verified that the initial token is RAISE |
| 193 |
|
| 194 |
-- Error recovery: can raise Error_Resync |
| 195 |
|
| 196 |
function P_Raise_Expression return Node_Id is |
| 197 |
Raise_Node : Node_Id; |
| 198 |
|
| 199 |
begin |
| 200 |
Error_Msg_Ada_2012_Feature ("raise expression", Token_Ptr); |
| 201 |
Raise_Node := New_Node (N_Raise_Expression, Token_Ptr); |
| 202 |
Scan; -- past RAISE |
| 203 |
|
| 204 |
Set_Name (Raise_Node, P_Name); |
| 205 |
|
| 206 |
if Token = Tok_With then |
| 207 |
Scan; -- past WITH |
| 208 |
Set_Expression (Raise_Node, P_Expression); |
| 209 |
end if; |
| 210 |
|
| 211 |
return Raise_Node; |
| 212 |
end P_Raise_Expression; |
| 213 |
|
| 214 |
--------------------------- |
| 215 |
-- 11.3 Raise Statement -- |
| 216 |
--------------------------- |
| 217 |
|
| 218 |
-- RAISE_STATEMENT ::= raise [exception_NAME with string_EXPRESSION]; |
| 219 |
|
| 220 |
-- The caller has verified that the initial token is RAISE |
| 221 |
|
| 222 |
-- Error recovery: can raise Error_Resync |
| 223 |
|
| 224 |
function P_Raise_Statement return Node_Id is |
| 225 |
Raise_Node : Node_Id; |
| 226 |
|
| 227 |
begin |
| 228 |
Raise_Node := New_Node (N_Raise_Statement, Token_Ptr); |
| 229 |
Scan; -- past RAISE |
| 230 |
|
| 231 |
if Token /= Tok_Semicolon then |
| 232 |
Set_Name (Raise_Node, P_Name); |
| 233 |
end if; |
| 234 |
|
| 235 |
if Token = Tok_With then |
| 236 |
Error_Msg_Ada_2005_Extension ("string expression in raise"); |
| 237 |
|
| 238 |
Scan; -- past WITH |
| 239 |
Set_Expression (Raise_Node, P_Expression); |
| 240 |
end if; |
| 241 |
|
| 242 |
if Token = Tok_When then |
| 243 |
Error_Msg_GNAT_Extension ("raise when statement", Token_Ptr); |
| 244 |
|
| 245 |
Mutate_Nkind (Raise_Node, N_Raise_When_Statement); |
| 246 |
|
| 247 |
if Token = Tok_When and then not Missing_Semicolon_On_When then |
| 248 |
Scan; -- past WHEN |
| 249 |
Set_Condition (Raise_Node, P_Expression_No_Right_Paren); |
| 250 |
|
| 251 |
-- Allow IF instead of WHEN, giving error message |
| 252 |
|
| 253 |
elsif Token = Tok_If then |
| 254 |
T_When; |
| 255 |
Scan; -- past IF used in place of WHEN |
| 256 |
Set_Condition (Raise_Node, P_Expression_No_Right_Paren); |
| 257 |
end if; |
| 258 |
end if; |
| 259 |
|
| 260 |
TF_Semicolon; |
| 261 |
return Raise_Node; |
| 262 |
end P_Raise_Statement; |
| 263 |
|
| 264 |
------------------------------ |
| 265 |
-- Parse_Exception_Handlers -- |
| 266 |
------------------------------ |
| 267 |
|
| 268 |
-- This routine scans out a list of exception handlers appearing in a |
| 269 |
-- construct as: |
| 270 |
|
| 271 |
-- exception |
| 272 |
-- EXCEPTION_HANDLER {EXCEPTION_HANDLER} |
| 273 |
|
| 274 |
-- The caller has scanned out the EXCEPTION keyword |
| 275 |
|
| 276 |
-- Control returns after scanning the last exception handler, presumably |
| 277 |
-- at the keyword END, but this is not checked in this routine. |
| 278 |
|
| 279 |
-- Error recovery: cannot raise Error_Resync |
| 280 |
|
| 281 |
function Parse_Exception_Handlers return List_Id is |
| 282 |
Handler : Node_Id; |
| 283 |
Handlers_List : List_Id; |
| 284 |
|
| 285 |
begin |
| 286 |
Handlers_List := New_List; |
| 287 |
P_Pragmas_Opt (Handlers_List); |
| 288 |
|
| 289 |
if Token = Tok_End then |
| 290 |
Error_Msg_SC ("must have at least one exception handler!"); |
| 291 |
|
| 292 |
else |
| 293 |
loop |
| 294 |
Handler := P_Exception_Handler; |
| 295 |
Append (Handler, Handlers_List); |
| 296 |
|
| 297 |
-- Note: no need to check for pragmas here. Although the |
| 298 |
-- syntax officially allows them in this position, they |
| 299 |
-- will have been swallowed up as part of the statement |
| 300 |
-- sequence of the handler we just scanned out. |
| 301 |
|
| 302 |
exit when Token /= Tok_When; |
| 303 |
end loop; |
| 304 |
end if; |
| 305 |
|
| 306 |
return Handlers_List; |
| 307 |
end Parse_Exception_Handlers; |
| 308 |
|
| 309 |
end Ch11; |