| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- P A R . C H 6 -- |
| 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 Ch6 is |
| 34 |
|
| 35 |
-- Local subprograms, used only in this chapter |
| 36 |
|
| 37 |
function P_Defining_Designator return Node_Id; |
| 38 |
function P_Defining_Operator_Symbol return Node_Id; |
| 39 |
function P_Return_Object_Declaration return Node_Id; |
| 40 |
|
| 41 |
procedure P_Return_Subtype_Indication (Decl_Node : Node_Id); |
| 42 |
-- Decl_Node is a N_Object_Declaration. Set the Null_Exclusion_Present and |
| 43 |
-- Object_Definition fields of Decl_Node. |
| 44 |
|
| 45 |
procedure Check_Junk_Semicolon_Before_Return; |
| 46 |
-- Check for common error of junk semicolon before RETURN keyword of |
| 47 |
-- function specification. If present, skip over it with appropriate error |
| 48 |
-- message, leaving Scan_Ptr pointing to the RETURN after. This routine |
| 49 |
-- also deals with a possibly misspelled version of Return. |
| 50 |
|
| 51 |
procedure No_Constraint_Maybe_Expr_Func; |
| 52 |
-- Called after scanning return subtype to check for missing constraint, |
| 53 |
-- taking into account the possibility of an occurrence of an expression |
| 54 |
-- function where the IS has been forgotten. |
| 55 |
|
| 56 |
---------------------------------------- |
| 57 |
-- Check_Junk_Semicolon_Before_Return -- |
| 58 |
---------------------------------------- |
| 59 |
|
| 60 |
procedure Check_Junk_Semicolon_Before_Return is |
| 61 |
Scan_State : Saved_Scan_State; |
| 62 |
|
| 63 |
begin |
| 64 |
if Token = Tok_Semicolon then |
| 65 |
Save_Scan_State (Scan_State); |
| 66 |
Scan; -- past the semicolon |
| 67 |
|
| 68 |
if Token = Tok_Return then |
| 69 |
Restore_Scan_State (Scan_State); |
| 70 |
Error_Msg_SC -- CODEFIX |
| 71 |
("|extra "";"" ignored"); |
| 72 |
Scan; -- rescan past junk semicolon |
| 73 |
else |
| 74 |
Restore_Scan_State (Scan_State); |
| 75 |
end if; |
| 76 |
end if; |
| 77 |
end Check_Junk_Semicolon_Before_Return; |
| 78 |
|
| 79 |
----------------------------------- |
| 80 |
-- No_Constraint_Maybe_Expr_Func -- |
| 81 |
----------------------------------- |
| 82 |
|
| 83 |
procedure No_Constraint_Maybe_Expr_Func is |
| 84 |
begin |
| 85 |
-- If we have a left paren at the start of the line, then assume this is |
| 86 |
-- the case of an expression function with missing IS. We do not have to |
| 87 |
-- diagnose the missing IS, that is done elsewhere. We do this game in |
| 88 |
-- Ada 2012 mode where expression functions are legal. |
| 89 |
|
| 90 |
if Token = Tok_Left_Paren |
| 91 |
and Ada_Version >= Ada_2012 |
| 92 |
and Token_Is_At_Start_Of_Line |
| 93 |
then |
| 94 |
-- One exception if we have "(token .." then this is a constraint |
| 95 |
|
| 96 |
declare |
| 97 |
Scan_State : Saved_Scan_State; |
| 98 |
|
| 99 |
begin |
| 100 |
Save_Scan_State (Scan_State); |
| 101 |
Scan; -- past left paren |
| 102 |
Scan; -- past following token |
| 103 |
|
| 104 |
-- If we have "(token .." then restore scan state and treat as |
| 105 |
-- unexpected constraint. |
| 106 |
|
| 107 |
if Token = Tok_Dot_Dot then |
| 108 |
Restore_Scan_State (Scan_State); |
| 109 |
No_Constraint; |
| 110 |
|
| 111 |
-- Otherwise we treat this as an expression function |
| 112 |
|
| 113 |
else |
| 114 |
Restore_Scan_State (Scan_State); |
| 115 |
end if; |
| 116 |
end; |
| 117 |
|
| 118 |
-- Otherwise use standard routine to check for no constraint present |
| 119 |
|
| 120 |
else |
| 121 |
No_Constraint; |
| 122 |
end if; |
| 123 |
end No_Constraint_Maybe_Expr_Func; |
| 124 |
|
| 125 |
----------------------------------------------------- |
| 126 |
-- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) -- |
| 127 |
----------------------------------------------------- |
| 128 |
|
| 129 |
-- This routine scans out a subprogram declaration, subprogram body, |
| 130 |
-- subprogram renaming declaration or subprogram generic instantiation. |
| 131 |
-- It also handles the new Ada 2012 expression function form |
| 132 |
|
| 133 |
-- SUBPROGRAM_DECLARATION ::= |
| 134 |
-- SUBPROGRAM_SPECIFICATION |
| 135 |
-- [ASPECT_SPECIFICATIONS]; |
| 136 |
|
| 137 |
-- ABSTRACT_SUBPROGRAM_DECLARATION ::= |
| 138 |
-- SUBPROGRAM_SPECIFICATION is abstract |
| 139 |
-- [ASPECT_SPECIFICATIONS]; |
| 140 |
|
| 141 |
-- SUBPROGRAM_SPECIFICATION ::= |
| 142 |
-- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE |
| 143 |
-- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE |
| 144 |
|
| 145 |
-- PARAMETER_PROFILE ::= [FORMAL_PART] |
| 146 |
|
| 147 |
-- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK |
| 148 |
|
| 149 |
-- SUBPROGRAM_BODY ::= |
| 150 |
-- SUBPROGRAM_SPECIFICATION [ASPECT_SPECIFICATIONS] is |
| 151 |
-- DECLARATIVE_PART |
| 152 |
-- begin |
| 153 |
-- HANDLED_SEQUENCE_OF_STATEMENTS |
| 154 |
-- end [DESIGNATOR]; |
| 155 |
|
| 156 |
-- SUBPROGRAM_RENAMING_DECLARATION ::= |
| 157 |
-- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME |
| 158 |
-- [ASPECT_SPECIFICATIONS]; |
| 159 |
|
| 160 |
-- SUBPROGRAM_BODY_STUB ::= |
| 161 |
-- SUBPROGRAM_SPECIFICATION is separate |
| 162 |
-- [ASPECT_SPECIFICATIONS]; |
| 163 |
|
| 164 |
-- GENERIC_INSTANTIATION ::= |
| 165 |
-- procedure DEFINING_PROGRAM_UNIT_NAME is |
| 166 |
-- new generic_procedure_NAME [GENERIC_ACTUAL_PART] |
| 167 |
-- [ASPECT_SPECIFICATIONS]; |
| 168 |
-- | function DEFINING_DESIGNATOR is |
| 169 |
-- new generic_function_NAME [GENERIC_ACTUAL_PART] |
| 170 |
-- [ASPECT_SPECIFICATIONS]; |
| 171 |
|
| 172 |
-- NULL_PROCEDURE_DECLARATION ::= |
| 173 |
-- SUBPROGRAM_SPECIFICATION is null; |
| 174 |
|
| 175 |
-- Null procedures are an Ada 2005 feature. A null procedure declaration |
| 176 |
-- is classified as a basic declarative item, but it is parsed here, with |
| 177 |
-- other subprogram constructs. |
| 178 |
|
| 179 |
-- EXPRESSION_FUNCTION ::= |
| 180 |
-- FUNCTION SPECIFICATION IS (EXPRESSION) |
| 181 |
-- [ASPECT_SPECIFICATIONS]; |
| 182 |
|
| 183 |
-- The caller has checked that the initial token is FUNCTION, PROCEDURE, |
| 184 |
-- NOT or OVERRIDING. |
| 185 |
|
| 186 |
-- Error recovery: cannot raise Error_Resync |
| 187 |
|
| 188 |
function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is |
| 189 |
|
| 190 |
function Contains_Import_Aspect (Aspects : List_Id) return Boolean; |
| 191 |
-- Return True if Aspects contains an Import aspect. |
| 192 |
|
| 193 |
---------------------------- |
| 194 |
-- Contains_Import_Aspect -- |
| 195 |
---------------------------- |
| 196 |
|
| 197 |
function Contains_Import_Aspect (Aspects : List_Id) return Boolean is |
| 198 |
Aspect : Node_Id := First (Aspects); |
| 199 |
begin |
| 200 |
while Present (Aspect) loop |
| 201 |
if Chars (Identifier (Aspect)) = Name_Import then |
| 202 |
return True; |
| 203 |
end if; |
| 204 |
|
| 205 |
Next (Aspect); |
| 206 |
end loop; |
| 207 |
|
| 208 |
return False; |
| 209 |
end Contains_Import_Aspect; |
| 210 |
|
| 211 |
Specification_Node : Node_Id; |
| 212 |
Name_Node : Node_Id; |
| 213 |
Aspects : List_Id; |
| 214 |
Fpart_List : List_Id; |
| 215 |
Fpart_Sloc : Source_Ptr; |
| 216 |
Result_Not_Null : Boolean := False; |
| 217 |
Result_Node : Node_Id; |
| 218 |
Inst_Node : Node_Id; |
| 219 |
Body_Node : Node_Id; |
| 220 |
Decl_Node : Node_Id; |
| 221 |
Rename_Node : Node_Id; |
| 222 |
Absdec_Node : Node_Id; |
| 223 |
Stub_Node : Node_Id; |
| 224 |
Fproc_Sloc : Source_Ptr; |
| 225 |
Func : Boolean; |
| 226 |
Scan_State : Saved_Scan_State; |
| 227 |
|
| 228 |
-- Flags for optional overriding indication. Two flags are needed, |
| 229 |
-- to distinguish positive and negative overriding indicators from |
| 230 |
-- the absence of any indicator. |
| 231 |
|
| 232 |
Is_Overriding : Boolean := False; |
| 233 |
Not_Overriding : Boolean := False; |
| 234 |
|
| 235 |
begin |
| 236 |
-- Set up scope stack entry. Note that the Labl field will be set later |
| 237 |
|
| 238 |
SIS_Entry_Active := False; |
| 239 |
SIS_Aspect_Import_Seen := False; |
| 240 |
SIS_Missing_Semicolon_Message := No_Error_Msg; |
| 241 |
Push_Scope_Stack; |
| 242 |
Scopes (Scope.Last).Sloc := Token_Ptr; |
| 243 |
Scopes (Scope.Last).Etyp := E_Name; |
| 244 |
Scopes (Scope.Last).Ecol := Start_Column; |
| 245 |
Scopes (Scope.Last).Lreq := False; |
| 246 |
|
| 247 |
Aspects := Empty_List; |
| 248 |
|
| 249 |
-- Ada 2005: Scan leading NOT OVERRIDING indicator |
| 250 |
|
| 251 |
if Token = Tok_Not then |
| 252 |
Scan; -- past NOT |
| 253 |
|
| 254 |
if Token = Tok_Overriding then |
| 255 |
Scan; -- past OVERRIDING |
| 256 |
Not_Overriding := True; |
| 257 |
|
| 258 |
-- Overriding keyword used in non Ada 2005 mode |
| 259 |
|
| 260 |
elsif Token = Tok_Identifier |
| 261 |
and then Token_Name = Name_Overriding |
| 262 |
then |
| 263 |
Error_Msg_SC ("overriding indicator is an Ada 2005 extension"); |
| 264 |
Error_Msg_SC ("\unit must be compiled with -gnat05 switch"); |
| 265 |
Scan; -- past Overriding |
| 266 |
Not_Overriding := True; |
| 267 |
|
| 268 |
else |
| 269 |
Error_Msg_SC -- CODEFIX |
| 270 |
("OVERRIDING expected!"); |
| 271 |
end if; |
| 272 |
|
| 273 |
-- Ada 2005: scan leading OVERRIDING indicator |
| 274 |
|
| 275 |
-- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the |
| 276 |
-- declaration circuit already gave an error message and changed the |
| 277 |
-- token to Tok_Overriding. |
| 278 |
|
| 279 |
elsif Token = Tok_Overriding then |
| 280 |
Scan; -- past OVERRIDING |
| 281 |
Is_Overriding := True; |
| 282 |
end if; |
| 283 |
|
| 284 |
if Is_Overriding or else Not_Overriding then |
| 285 |
|
| 286 |
-- Note that if we are not in Ada_2005 mode, error messages have |
| 287 |
-- already been given, so no need to give another message here. |
| 288 |
|
| 289 |
-- An overriding indicator is allowed for subprogram declarations, |
| 290 |
-- bodies (including subunits), renamings, stubs, and instantiations. |
| 291 |
-- The test against Pf_Decl_Pbod is added to account for the case of |
| 292 |
-- subprograms declared in a protected type, where only subprogram |
| 293 |
-- declarations and bodies can occur. The Pf_Pbod case is for |
| 294 |
-- subunits. |
| 295 |
|
| 296 |
if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp |
| 297 |
and then |
| 298 |
Pf_Flags /= Pf_Decl_Pbod_Pexp |
| 299 |
and then |
| 300 |
Pf_Flags /= Pf_Pbod_Pexp |
| 301 |
then |
| 302 |
Error_Msg_SC ("overriding indicator not allowed here!"); |
| 303 |
|
| 304 |
elsif Token not in Tok_Function | Tok_Procedure then |
| 305 |
Error_Msg_SC -- CODEFIX |
| 306 |
("FUNCTION or PROCEDURE expected!"); |
| 307 |
end if; |
| 308 |
end if; |
| 309 |
|
| 310 |
Func := (Token = Tok_Function); |
| 311 |
Fproc_Sloc := Token_Ptr; |
| 312 |
Scan; -- past FUNCTION or PROCEDURE |
| 313 |
Ignore (Tok_Type); |
| 314 |
Ignore (Tok_Body); |
| 315 |
|
| 316 |
if Func then |
| 317 |
Name_Node := P_Defining_Designator; |
| 318 |
|
| 319 |
if Nkind (Name_Node) = N_Defining_Operator_Symbol |
| 320 |
and then Scope.Last = 1 |
| 321 |
then |
| 322 |
Error_Msg_SP ("operator symbol not allowed at library level"); |
| 323 |
Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node)); |
| 324 |
|
| 325 |
-- Set name from file name, we need some junk name, and that's |
| 326 |
-- as good as anything. This is only approximate, since we do |
| 327 |
-- not do anything with non-standard name translations. |
| 328 |
|
| 329 |
Get_Name_String (File_Name (Current_Source_File)); |
| 330 |
|
| 331 |
for J in 1 .. Name_Len loop |
| 332 |
if Name_Buffer (J) = '.' then |
| 333 |
Name_Len := J - 1; |
| 334 |
exit; |
| 335 |
end if; |
| 336 |
end loop; |
| 337 |
|
| 338 |
Set_Chars (Name_Node, Name_Find); |
| 339 |
Set_Error_Posted (Name_Node); |
| 340 |
end if; |
| 341 |
|
| 342 |
else |
| 343 |
Name_Node := P_Defining_Program_Unit_Name; |
| 344 |
end if; |
| 345 |
|
| 346 |
Scopes (Scope.Last).Labl := Name_Node; |
| 347 |
Ignore (Tok_Colon); |
| 348 |
|
| 349 |
-- Deal with generic instantiation, the one case in which we do not |
| 350 |
-- have a subprogram specification as part of whatever we are parsing |
| 351 |
|
| 352 |
if Token = Tok_Is then |
| 353 |
Save_Scan_State (Scan_State); -- at the IS |
| 354 |
T_Is; -- checks for redundant IS |
| 355 |
|
| 356 |
if Token = Tok_New then |
| 357 |
if not Pf_Flags.Gins then |
| 358 |
Error_Msg_SC ("generic instantiation not allowed here!"); |
| 359 |
end if; |
| 360 |
|
| 361 |
Scan; -- past NEW |
| 362 |
|
| 363 |
if Func then |
| 364 |
Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc); |
| 365 |
Set_Name (Inst_Node, P_Function_Name); |
| 366 |
else |
| 367 |
Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc); |
| 368 |
Set_Name (Inst_Node, P_Qualified_Simple_Name); |
| 369 |
end if; |
| 370 |
|
| 371 |
Set_Defining_Unit_Name (Inst_Node, Name_Node); |
| 372 |
Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt); |
| 373 |
P_Aspect_Specifications (Inst_Node, Semicolon => True); |
| 374 |
Pop_Scope_Stack; -- Don't need scope stack entry in this case |
| 375 |
|
| 376 |
if Is_Overriding then |
| 377 |
Set_Must_Override (Inst_Node); |
| 378 |
|
| 379 |
elsif Not_Overriding then |
| 380 |
Set_Must_Not_Override (Inst_Node); |
| 381 |
end if; |
| 382 |
|
| 383 |
return Inst_Node; |
| 384 |
|
| 385 |
else |
| 386 |
Restore_Scan_State (Scan_State); -- to the IS |
| 387 |
end if; |
| 388 |
end if; |
| 389 |
|
| 390 |
-- If not a generic instantiation, then we definitely have a subprogram |
| 391 |
-- specification (all possibilities at this stage include one here) |
| 392 |
|
| 393 |
Fpart_Sloc := Token_Ptr; |
| 394 |
|
| 395 |
Check_Misspelling_Of (Tok_Return); |
| 396 |
|
| 397 |
-- Scan formal part. First a special error check. If we have an |
| 398 |
-- identifier here, then we have a definite error. If this identifier |
| 399 |
-- is on the same line as the designator, then we assume it is the |
| 400 |
-- first formal after a missing left parenthesis |
| 401 |
|
| 402 |
if Token = Tok_Identifier |
| 403 |
and then not Token_Is_At_Start_Of_Line |
| 404 |
then |
| 405 |
T_Left_Paren; -- to generate message |
| 406 |
Fpart_List := P_Formal_Part; |
| 407 |
|
| 408 |
-- Otherwise scan out an optional formal part in the usual manner |
| 409 |
|
| 410 |
else |
| 411 |
Fpart_List := P_Parameter_Profile; |
| 412 |
end if; |
| 413 |
|
| 414 |
-- We treat what we have as a function specification if FUNCTION was |
| 415 |
-- used, or if a RETURN is present. This gives better error recovery |
| 416 |
-- since later RETURN statements will be valid in either case. |
| 417 |
|
| 418 |
Check_Junk_Semicolon_Before_Return; |
| 419 |
Result_Node := Error; |
| 420 |
|
| 421 |
if Token = Tok_Return then |
| 422 |
if not Func then |
| 423 |
Error_Msg -- CODEFIX |
| 424 |
("PROCEDURE should be FUNCTION", Fproc_Sloc); |
| 425 |
Func := True; |
| 426 |
end if; |
| 427 |
|
| 428 |
Scan; -- past RETURN |
| 429 |
|
| 430 |
Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231) |
| 431 |
|
| 432 |
-- Ada 2005 (AI-318-02) |
| 433 |
|
| 434 |
if Token = Tok_Access then |
| 435 |
Error_Msg_Ada_2005_Extension ("anonymous access result type"); |
| 436 |
|
| 437 |
Result_Node := P_Access_Definition (Result_Not_Null); |
| 438 |
|
| 439 |
else |
| 440 |
Result_Node := P_Subtype_Mark; |
| 441 |
No_Constraint_Maybe_Expr_Func; |
| 442 |
end if; |
| 443 |
|
| 444 |
else |
| 445 |
-- Skip extra parenthesis at end of formal part |
| 446 |
|
| 447 |
Ignore (Tok_Right_Paren); |
| 448 |
|
| 449 |
-- For function, scan result subtype |
| 450 |
|
| 451 |
if Func then |
| 452 |
TF_Return; |
| 453 |
|
| 454 |
if Prev_Token = Tok_Return then |
| 455 |
Result_Node := P_Subtype_Mark; |
| 456 |
end if; |
| 457 |
end if; |
| 458 |
end if; |
| 459 |
|
| 460 |
if Func then |
| 461 |
Specification_Node := |
| 462 |
New_Node (N_Function_Specification, Fproc_Sloc); |
| 463 |
|
| 464 |
Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null); |
| 465 |
Set_Result_Definition (Specification_Node, Result_Node); |
| 466 |
|
| 467 |
else |
| 468 |
Specification_Node := |
| 469 |
New_Node (N_Procedure_Specification, Fproc_Sloc); |
| 470 |
end if; |
| 471 |
|
| 472 |
Set_Defining_Unit_Name (Specification_Node, Name_Node); |
| 473 |
Set_Parameter_Specifications (Specification_Node, Fpart_List); |
| 474 |
|
| 475 |
if Is_Overriding then |
| 476 |
Set_Must_Override (Specification_Node); |
| 477 |
|
| 478 |
elsif Not_Overriding then |
| 479 |
Set_Must_Not_Override (Specification_Node); |
| 480 |
end if; |
| 481 |
|
| 482 |
-- Error check: barriers not allowed on protected functions/procedures |
| 483 |
|
| 484 |
if Token = Tok_When then |
| 485 |
if Func then |
| 486 |
Error_Msg_SC ("barrier not allowed on function, only on entry"); |
| 487 |
else |
| 488 |
Error_Msg_SC ("barrier not allowed on procedure, only on entry"); |
| 489 |
end if; |
| 490 |
|
| 491 |
Scan; -- past WHEN |
| 492 |
Discard_Junk_Node (P_Expression); |
| 493 |
end if; |
| 494 |
|
| 495 |
-- Deal with semicolon followed by IS. We want to treat this as IS |
| 496 |
|
| 497 |
if Token = Tok_Semicolon then |
| 498 |
Save_Scan_State (Scan_State); |
| 499 |
Scan; -- past semicolon |
| 500 |
|
| 501 |
if Token = Tok_Is then |
| 502 |
Error_Msg_SP -- CODEFIX |
| 503 |
("extra "";"" ignored"); |
| 504 |
else |
| 505 |
Restore_Scan_State (Scan_State); |
| 506 |
end if; |
| 507 |
end if; |
| 508 |
|
| 509 |
-- Subprogram declaration ended by aspect specifications |
| 510 |
|
| 511 |
if Aspect_Specifications_Present then |
| 512 |
goto Subprogram_Declaration; |
| 513 |
|
| 514 |
-- Deal with case of semicolon ending a subprogram declaration |
| 515 |
|
| 516 |
elsif Token = Tok_Semicolon then |
| 517 |
if not Pf_Flags.Decl then |
| 518 |
T_Is; |
| 519 |
end if; |
| 520 |
|
| 521 |
Save_Scan_State (Scan_State); |
| 522 |
Scan; -- past semicolon |
| 523 |
|
| 524 |
-- If semicolon is immediately followed by IS, then ignore the |
| 525 |
-- semicolon, and go process the body. |
| 526 |
|
| 527 |
if Token = Tok_Is then |
| 528 |
Error_Msg_SP -- CODEFIX |
| 529 |
("|extra "";"" ignored"); |
| 530 |
T_Is; -- scan past IS |
| 531 |
goto Subprogram_Body; |
| 532 |
|
| 533 |
-- If BEGIN follows in an appropriate column, we immediately |
| 534 |
-- commence the error action of assuming that the previous |
| 535 |
-- subprogram declaration should have been a subprogram body, |
| 536 |
-- i.e. that the terminating semicolon should have been IS. |
| 537 |
|
| 538 |
elsif Token = Tok_Begin |
| 539 |
and then Start_Column >= Scopes (Scope.Last).Ecol |
| 540 |
then |
| 541 |
Error_Msg_SP -- CODEFIX |
| 542 |
("|"";"" should be IS!"); |
| 543 |
goto Subprogram_Body; |
| 544 |
|
| 545 |
else |
| 546 |
Restore_Scan_State (Scan_State); |
| 547 |
goto Subprogram_Declaration; |
| 548 |
end if; |
| 549 |
|
| 550 |
-- Case of not followed by semicolon |
| 551 |
|
| 552 |
else |
| 553 |
-- Subprogram renaming declaration case |
| 554 |
|
| 555 |
Check_Misspelling_Of (Tok_Renames); |
| 556 |
|
| 557 |
if Token = Tok_Renames then |
| 558 |
if not Pf_Flags.Rnam then |
| 559 |
Error_Msg_SC ("renaming declaration not allowed here!"); |
| 560 |
end if; |
| 561 |
|
| 562 |
Rename_Node := |
| 563 |
New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr); |
| 564 |
Scan; -- past RENAMES |
| 565 |
Set_Name (Rename_Node, P_Name); |
| 566 |
Set_Specification (Rename_Node, Specification_Node); |
| 567 |
P_Aspect_Specifications (Rename_Node, Semicolon => True); |
| 568 |
TF_Semicolon; |
| 569 |
Pop_Scope_Stack; |
| 570 |
return Rename_Node; |
| 571 |
|
| 572 |
-- Case of IS following subprogram specification |
| 573 |
|
| 574 |
elsif Token = Tok_Is then |
| 575 |
T_Is; -- ignore redundant Is's |
| 576 |
|
| 577 |
if Token_Name = Name_Abstract then |
| 578 |
Check_95_Keyword (Tok_Abstract, Tok_Semicolon); |
| 579 |
end if; |
| 580 |
|
| 581 |
-- Deal nicely with (now obsolete) use of <> in place of abstract |
| 582 |
|
| 583 |
if Token = Tok_Box then |
| 584 |
Error_Msg_SC -- CODEFIX |
| 585 |
("ABSTRACT expected"); |
| 586 |
Token := Tok_Abstract; |
| 587 |
end if; |
| 588 |
|
| 589 |
-- Abstract subprogram declaration case |
| 590 |
|
| 591 |
if Token = Tok_Abstract then |
| 592 |
Absdec_Node := |
| 593 |
New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr); |
| 594 |
Set_Specification (Absdec_Node, Specification_Node); |
| 595 |
Pop_Scope_Stack; -- discard unneeded entry |
| 596 |
Scan; -- past ABSTRACT |
| 597 |
P_Aspect_Specifications (Absdec_Node, Semicolon => True); |
| 598 |
return Absdec_Node; |
| 599 |
|
| 600 |
-- Ada 2005 (AI-248): Parse a null procedure declaration |
| 601 |
|
| 602 |
elsif Token = Tok_Null then |
| 603 |
Error_Msg_Ada_2005_Extension ("null procedure"); |
| 604 |
|
| 605 |
Scan; -- past NULL |
| 606 |
|
| 607 |
if Func then |
| 608 |
Error_Msg_SP ("only procedures can be null"); |
| 609 |
else |
| 610 |
Set_Null_Present (Specification_Node); |
| 611 |
Set_Null_Statement (Specification_Node, |
| 612 |
New_Node (N_Null_Statement, Prev_Token_Ptr)); |
| 613 |
end if; |
| 614 |
|
| 615 |
goto Subprogram_Declaration; |
| 616 |
|
| 617 |
-- Check for IS NEW with Formal_Part present and handle nicely |
| 618 |
|
| 619 |
elsif Token = Tok_New then |
| 620 |
Error_Msg |
| 621 |
("formal part not allowed in instantiation", Fpart_Sloc); |
| 622 |
Scan; -- past NEW |
| 623 |
|
| 624 |
if Func then |
| 625 |
Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc); |
| 626 |
else |
| 627 |
Inst_Node := |
| 628 |
New_Node (N_Procedure_Instantiation, Fproc_Sloc); |
| 629 |
end if; |
| 630 |
|
| 631 |
Set_Defining_Unit_Name (Inst_Node, Name_Node); |
| 632 |
Set_Name (Inst_Node, P_Name); |
| 633 |
Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt); |
| 634 |
TF_Semicolon; |
| 635 |
Pop_Scope_Stack; -- Don't need scope stack entry in this case |
| 636 |
return Inst_Node; |
| 637 |
|
| 638 |
else |
| 639 |
goto Subprogram_Body; |
| 640 |
end if; |
| 641 |
|
| 642 |
-- Aspect specifications present |
| 643 |
|
| 644 |
elsif Aspect_Specifications_Present then |
| 645 |
goto Subprogram_Declaration; |
| 646 |
|
| 647 |
-- Here we have a missing IS or missing semicolon |
| 648 |
|
| 649 |
else |
| 650 |
-- If the next token is a left paren at the start of a line, then |
| 651 |
-- this is almost certainly the start of the expression for an |
| 652 |
-- expression function, so in this case guess a missing IS. |
| 653 |
|
| 654 |
if Token = Tok_Left_Paren and then Token_Is_At_Start_Of_Line then |
| 655 |
Error_Msg_AP -- CODEFIX |
| 656 |
("missing IS"); |
| 657 |
|
| 658 |
-- In all other cases, we guess a missing semicolon, since we are |
| 659 |
-- good at fixing up a semicolon which should really be an IS. |
| 660 |
|
| 661 |
else |
| 662 |
Error_Msg_AP -- CODEFIX |
| 663 |
("|missing "";"""); |
| 664 |
SIS_Missing_Semicolon_Message := Get_Msg_Id; |
| 665 |
goto Subprogram_Declaration; |
| 666 |
end if; |
| 667 |
end if; |
| 668 |
end if; |
| 669 |
|
| 670 |
-- Processing for stub or subprogram body or expression function |
| 671 |
|
| 672 |
<<Subprogram_Body>> |
| 673 |
|
| 674 |
-- Subprogram body stub case |
| 675 |
|
| 676 |
if Separate_Present then |
| 677 |
if not Pf_Flags.Stub then |
| 678 |
Error_Msg_SC ("body stub not allowed here!"); |
| 679 |
end if; |
| 680 |
|
| 681 |
if Nkind (Name_Node) = N_Defining_Operator_Symbol then |
| 682 |
Error_Msg |
| 683 |
("operator symbol cannot be used as subunit name", |
| 684 |
Sloc (Name_Node)); |
| 685 |
end if; |
| 686 |
|
| 687 |
Scan; -- past SEPARATE |
| 688 |
|
| 689 |
Stub_Node := |
| 690 |
New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node)); |
| 691 |
Set_Specification (Stub_Node, Specification_Node); |
| 692 |
|
| 693 |
if Is_Non_Empty_List (Aspects) then |
| 694 |
Error_Msg |
| 695 |
("aspect specifications must come after SEPARATE", |
| 696 |
Sloc (First (Aspects))); |
| 697 |
end if; |
| 698 |
|
| 699 |
P_Aspect_Specifications (Stub_Node, Semicolon => False); |
| 700 |
TF_Semicolon; |
| 701 |
Pop_Scope_Stack; |
| 702 |
return Stub_Node; |
| 703 |
|
| 704 |
-- Subprogram body or expression function case |
| 705 |
|
| 706 |
else |
| 707 |
Scan_Body_Or_Expression_Function : declare |
| 708 |
|
| 709 |
function Likely_Expression_Function return Boolean; |
| 710 |
-- Returns True if we have a probable case of an expression |
| 711 |
-- function omitting the parentheses, if so, returns True |
| 712 |
-- and emits an appropriate error message, else returns False. |
| 713 |
|
| 714 |
-------------------------------- |
| 715 |
-- Likely_Expression_Function -- |
| 716 |
-------------------------------- |
| 717 |
|
| 718 |
function Likely_Expression_Function return Boolean is |
| 719 |
begin |
| 720 |
-- If currently pointing to BEGIN or a declaration keyword |
| 721 |
-- or a pragma, then we definitely have a subprogram body. |
| 722 |
-- This is a common case, so worth testing first. |
| 723 |
|
| 724 |
if Token in Tok_Begin | Token_Class_Declk | Tok_Pragma then |
| 725 |
return False; |
| 726 |
|
| 727 |
-- Test for tokens which could only start an expression and |
| 728 |
-- thus signal the case of a expression function. |
| 729 |
|
| 730 |
elsif Token in |
| 731 |
Token_Class_Literal | Token_Class_Unary_Addop | |
| 732 |
Tok_Left_Paren | Tok_Abs | Tok_Null | Tok_New | Tok_Not |
| 733 |
then |
| 734 |
null; |
| 735 |
|
| 736 |
-- Anything other than an identifier must be a body |
| 737 |
|
| 738 |
elsif Token /= Tok_Identifier then |
| 739 |
return False; |
| 740 |
|
| 741 |
-- Here for an identifier |
| 742 |
|
| 743 |
else |
| 744 |
-- If the identifier is the first token on its line, then |
| 745 |
-- let's assume that we have a missing begin and this is |
| 746 |
-- intended as a subprogram body. However, if the context |
| 747 |
-- is a function and the unit is a package declaration, a |
| 748 |
-- body would be illegal, so try for an unparenthesized |
| 749 |
-- expression function. |
| 750 |
|
| 751 |
if Token_Is_At_Start_Of_Line then |
| 752 |
declare |
| 753 |
-- The enclosing scope entry is a subprogram spec |
| 754 |
|
| 755 |
Spec_Node : constant Node_Id := |
| 756 |
Parent |
| 757 |
(Scopes (Scope.Last).Labl); |
| 758 |
Lib_Node : Node_Id := Spec_Node; |
| 759 |
|
| 760 |
begin |
| 761 |
-- Check whether there is an enclosing scope that |
| 762 |
-- is a package declaration. |
| 763 |
|
| 764 |
if Scope.Last > 1 then |
| 765 |
Lib_Node := |
| 766 |
Parent (Scopes (Scope.Last - 1).Labl); |
| 767 |
end if; |
| 768 |
|
| 769 |
if Ada_Version >= Ada_2012 |
| 770 |
and then |
| 771 |
Nkind (Lib_Node) = N_Package_Specification |
| 772 |
and then |
| 773 |
Nkind (Spec_Node) = N_Function_Specification |
| 774 |
then |
| 775 |
null; |
| 776 |
else |
| 777 |
return False; |
| 778 |
end if; |
| 779 |
end; |
| 780 |
|
| 781 |
-- Otherwise we have to scan ahead. If the identifier is |
| 782 |
-- followed by a colon or a comma, it is a declaration |
| 783 |
-- and hence we have a subprogram body. Otherwise assume |
| 784 |
-- a expression function. |
| 785 |
|
| 786 |
else |
| 787 |
declare |
| 788 |
Scan_State : Saved_Scan_State; |
| 789 |
Tok : Token_Type; |
| 790 |
|
| 791 |
begin |
| 792 |
Save_Scan_State (Scan_State); |
| 793 |
Scan; -- past identifier |
| 794 |
Tok := Token; |
| 795 |
Restore_Scan_State (Scan_State); |
| 796 |
|
| 797 |
if Tok = Tok_Colon or else Tok = Tok_Comma then |
| 798 |
return False; |
| 799 |
end if; |
| 800 |
end; |
| 801 |
end if; |
| 802 |
end if; |
| 803 |
|
| 804 |
-- Fall through if we have a likely expression function. |
| 805 |
-- If the starting keyword is not "function" the error |
| 806 |
-- will be reported elsewhere. |
| 807 |
|
| 808 |
if Func then |
| 809 |
Error_Msg_SC |
| 810 |
("expression function must be enclosed in parentheses"); |
| 811 |
end if; |
| 812 |
|
| 813 |
return True; |
| 814 |
end Likely_Expression_Function; |
| 815 |
|
| 816 |
-- Start of processing for Scan_Body_Or_Expression_Function |
| 817 |
|
| 818 |
begin |
| 819 |
-- Expression_Function case |
| 820 |
|
| 821 |
-- If likely an aggregate, check we are in Ada 2022 mode |
| 822 |
|
| 823 |
if Token = Tok_Left_Bracket then |
| 824 |
Error_Msg_Ada_2022_Feature |
| 825 |
("!aggregates as expression function", Token_Ptr); |
| 826 |
end if; |
| 827 |
|
| 828 |
if Token in Tok_Left_Paren | Tok_Left_Bracket |
| 829 |
or else Likely_Expression_Function |
| 830 |
then |
| 831 |
-- Check expression function allowed here |
| 832 |
|
| 833 |
if not Pf_Flags.Pexp then |
| 834 |
Error_Msg_SC ("expression function not allowed here!"); |
| 835 |
end if; |
| 836 |
|
| 837 |
-- Check we are in Ada 2012 mode |
| 838 |
|
| 839 |
Error_Msg_Ada_2012_Feature |
| 840 |
("!expression function", Token_Ptr); |
| 841 |
|
| 842 |
-- Catch an illegal placement of the aspect specification |
| 843 |
-- list: |
| 844 |
|
| 845 |
-- function_specification |
| 846 |
-- [aspect_specification] is (expression); |
| 847 |
|
| 848 |
-- This case is correctly processed by the parser because |
| 849 |
-- the expression function first appears as a subprogram |
| 850 |
-- declaration to the parser. The starting keyword may |
| 851 |
-- not have been "function" in which case the error is |
| 852 |
-- on a malformed procedure. |
| 853 |
|
| 854 |
if Is_Non_Empty_List (Aspects) then |
| 855 |
if Func then |
| 856 |
Error_Msg |
| 857 |
("aspect specifications must come after " |
| 858 |
& "parenthesized expression", |
| 859 |
Sloc (First (Aspects))); |
| 860 |
else |
| 861 |
Error_Msg |
| 862 |
("aspect specifications must come after subprogram " |
| 863 |
& "specification", Sloc (First (Aspects))); |
| 864 |
end if; |
| 865 |
end if; |
| 866 |
|
| 867 |
-- Parse out expression and build expression function |
| 868 |
|
| 869 |
Body_Node := |
| 870 |
New_Node |
| 871 |
(N_Expression_Function, Sloc (Specification_Node)); |
| 872 |
Set_Specification (Body_Node, Specification_Node); |
| 873 |
|
| 874 |
declare |
| 875 |
Expr : constant Node_Id := P_Expression; |
| 876 |
begin |
| 877 |
Set_Expression (Body_Node, Expr); |
| 878 |
|
| 879 |
-- Check that the full expression is properly |
| 880 |
-- parenthesized since we may have a left-operand that is |
| 881 |
-- parenthesized but that is not one of the allowed cases |
| 882 |
-- with syntactic parentheses. |
| 883 |
|
| 884 |
if not (Paren_Count (Expr) /= 0 |
| 885 |
or else Nkind (Expr) in N_Aggregate |
| 886 |
| N_Delta_Aggregate |
| 887 |
| N_Extension_Aggregate |
| 888 |
| N_Quantified_Expression) |
| 889 |
then |
| 890 |
Error_Msg |
| 891 |
("expression function must be enclosed in " |
| 892 |
& "parentheses", Sloc (Expr)); |
| 893 |
end if; |
| 894 |
end; |
| 895 |
|
| 896 |
-- Expression functions can carry pre/postconditions |
| 897 |
|
| 898 |
P_Aspect_Specifications (Body_Node, Semicolon => True); |
| 899 |
Pop_Scope_Stack; |
| 900 |
|
| 901 |
-- Subprogram body case |
| 902 |
|
| 903 |
else |
| 904 |
-- Check body allowed here |
| 905 |
|
| 906 |
if not Pf_Flags.Pbod then |
| 907 |
Error_Msg_SP ("subprogram body not allowed here!"); |
| 908 |
end if; |
| 909 |
|
| 910 |
-- Here is the test for a suspicious IS (i.e. one that |
| 911 |
-- looks like it might more properly be a semicolon). |
| 912 |
-- See separate section describing use of IS instead |
| 913 |
-- of semicolon in package Parse. |
| 914 |
|
| 915 |
if (Token in Token_Class_Declk |
| 916 |
or else |
| 917 |
Token = Tok_Identifier) |
| 918 |
and then Start_Column <= Scopes (Scope.Last).Ecol |
| 919 |
and then Scope.Last /= 1 |
| 920 |
then |
| 921 |
Scopes (Scope.Last).Etyp := E_Suspicious_Is; |
| 922 |
Scopes (Scope.Last).S_Is := Prev_Token_Ptr; |
| 923 |
end if; |
| 924 |
|
| 925 |
-- Build and return subprogram body, parsing declarations |
| 926 |
-- and statement sequence that belong to the body. |
| 927 |
|
| 928 |
Body_Node := |
| 929 |
New_Node (N_Subprogram_Body, Sloc (Specification_Node)); |
| 930 |
Set_Specification (Body_Node, Specification_Node); |
| 931 |
|
| 932 |
-- If aspects are present, the specification is parsed as |
| 933 |
-- a subprogram declaration, and we jump here after seeing |
| 934 |
-- the keyword IS. Attach asspects previously collected to |
| 935 |
-- the body. |
| 936 |
|
| 937 |
if Is_Non_Empty_List (Aspects) then |
| 938 |
Set_Aspect_Specifications (Body_Node, Aspects); |
| 939 |
end if; |
| 940 |
|
| 941 |
Parse_Decls_Begin_End (Body_Node); |
| 942 |
end if; |
| 943 |
|
| 944 |
return Body_Node; |
| 945 |
end Scan_Body_Or_Expression_Function; |
| 946 |
end if; |
| 947 |
|
| 948 |
-- Processing for subprogram declaration |
| 949 |
|
| 950 |
<<Subprogram_Declaration>> |
| 951 |
Decl_Node := |
| 952 |
New_Node (N_Subprogram_Declaration, Sloc (Specification_Node)); |
| 953 |
Set_Specification (Decl_Node, Specification_Node); |
| 954 |
Aspects := Get_Aspect_Specifications (Semicolon => False); |
| 955 |
|
| 956 |
-- Aspects may be present on a subprogram body. The source parsed |
| 957 |
-- so far is that of its specification. Go parse the body and attach |
| 958 |
-- the collected aspects, if any, to the body. |
| 959 |
|
| 960 |
if Token = Tok_Is then |
| 961 |
|
| 962 |
-- If the subprogram is a procedure and already has a |
| 963 |
-- specification, we can't define another. |
| 964 |
|
| 965 |
if Nkind (Specification (Decl_Node)) = N_Procedure_Specification |
| 966 |
and then Null_Present (Specification (Decl_Node)) |
| 967 |
then |
| 968 |
Error_Msg_AP ("null procedure cannot have a body"); |
| 969 |
end if; |
| 970 |
|
| 971 |
Scan; |
| 972 |
goto Subprogram_Body; |
| 973 |
|
| 974 |
else |
| 975 |
if Is_Non_Empty_List (Aspects) then |
| 976 |
Set_Aspect_Specifications (Decl_Node, Aspects); |
| 977 |
end if; |
| 978 |
|
| 979 |
TF_Semicolon; |
| 980 |
end if; |
| 981 |
|
| 982 |
-- If this is a context in which a subprogram body is permitted, |
| 983 |
-- set active SIS entry in case (see section titled "Handling |
| 984 |
-- Semicolon Used in Place of IS" in body of Parser package) |
| 985 |
-- Note that SIS_Missing_Semicolon_Message is already set properly. |
| 986 |
|
| 987 |
if Pf_Flags.Pbod |
| 988 |
|
| 989 |
-- Disconnect this processing if we have scanned a null procedure |
| 990 |
-- or an Import aspect because in this case the spec is complete |
| 991 |
-- anyway with no body. |
| 992 |
|
| 993 |
and then (Nkind (Specification_Node) /= N_Procedure_Specification |
| 994 |
or else not Null_Present (Specification_Node)) |
| 995 |
and then not Contains_Import_Aspect (Aspects) |
| 996 |
then |
| 997 |
SIS_Labl := Scopes (Scope.Last).Labl; |
| 998 |
SIS_Sloc := Scopes (Scope.Last).Sloc; |
| 999 |
SIS_Ecol := Scopes (Scope.Last).Ecol; |
| 1000 |
SIS_Declaration_Node := Decl_Node; |
| 1001 |
SIS_Semicolon_Sloc := Prev_Token_Ptr; |
| 1002 |
|
| 1003 |
-- Do not activate the entry if we have "with Import" |
| 1004 |
|
| 1005 |
if not SIS_Aspect_Import_Seen then |
| 1006 |
SIS_Entry_Active := True; |
| 1007 |
end if; |
| 1008 |
end if; |
| 1009 |
|
| 1010 |
Pop_Scope_Stack; |
| 1011 |
return Decl_Node; |
| 1012 |
end P_Subprogram; |
| 1013 |
|
| 1014 |
--------------------------------- |
| 1015 |
-- 6.1 Subprogram Declaration -- |
| 1016 |
--------------------------------- |
| 1017 |
|
| 1018 |
-- Parsed by P_Subprogram (6.1) |
| 1019 |
|
| 1020 |
------------------------------------------ |
| 1021 |
-- 6.1 Abstract Subprogram Declaration -- |
| 1022 |
------------------------------------------ |
| 1023 |
|
| 1024 |
-- Parsed by P_Subprogram (6.1) |
| 1025 |
|
| 1026 |
----------------------------------- |
| 1027 |
-- 6.1 Subprogram Specification -- |
| 1028 |
----------------------------------- |
| 1029 |
|
| 1030 |
-- SUBPROGRAM_SPECIFICATION ::= |
| 1031 |
-- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE |
| 1032 |
-- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE |
| 1033 |
|
| 1034 |
-- PARAMETER_PROFILE ::= [FORMAL_PART] |
| 1035 |
|
| 1036 |
-- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK |
| 1037 |
|
| 1038 |
-- Subprogram specifications that appear in subprogram declarations |
| 1039 |
-- are parsed by P_Subprogram (6.1). This routine is used in other |
| 1040 |
-- contexts where subprogram specifications occur. |
| 1041 |
|
| 1042 |
-- Note: this routine does not affect the scope stack in any way |
| 1043 |
|
| 1044 |
-- Error recovery: can raise Error_Resync |
| 1045 |
|
| 1046 |
function P_Subprogram_Specification return Node_Id is |
| 1047 |
Specification_Node : Node_Id; |
| 1048 |
Result_Not_Null : Boolean; |
| 1049 |
Result_Node : Node_Id; |
| 1050 |
|
| 1051 |
begin |
| 1052 |
if Token = Tok_Function then |
| 1053 |
Specification_Node := New_Node (N_Function_Specification, Token_Ptr); |
| 1054 |
Scan; -- past FUNCTION |
| 1055 |
Ignore (Tok_Body); |
| 1056 |
Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator); |
| 1057 |
Set_Parameter_Specifications |
| 1058 |
(Specification_Node, P_Parameter_Profile); |
| 1059 |
Check_Junk_Semicolon_Before_Return; |
| 1060 |
TF_Return; |
| 1061 |
|
| 1062 |
Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231) |
| 1063 |
|
| 1064 |
-- Ada 2005 (AI-318-02) |
| 1065 |
|
| 1066 |
if Token = Tok_Access then |
| 1067 |
Error_Msg_Ada_2005_Extension ("anonymous access result type"); |
| 1068 |
|
| 1069 |
Result_Node := P_Access_Definition (Result_Not_Null); |
| 1070 |
|
| 1071 |
else |
| 1072 |
Result_Node := P_Subtype_Mark; |
| 1073 |
No_Constraint_Maybe_Expr_Func; |
| 1074 |
end if; |
| 1075 |
|
| 1076 |
Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null); |
| 1077 |
Set_Result_Definition (Specification_Node, Result_Node); |
| 1078 |
return Specification_Node; |
| 1079 |
|
| 1080 |
elsif Token = Tok_Procedure then |
| 1081 |
Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr); |
| 1082 |
Scan; -- past PROCEDURE |
| 1083 |
Ignore (Tok_Body); |
| 1084 |
Set_Defining_Unit_Name |
| 1085 |
(Specification_Node, P_Defining_Program_Unit_Name); |
| 1086 |
Set_Parameter_Specifications |
| 1087 |
(Specification_Node, P_Parameter_Profile); |
| 1088 |
return Specification_Node; |
| 1089 |
|
| 1090 |
else |
| 1091 |
Error_Msg_SC ("subprogram specification expected"); |
| 1092 |
raise Error_Resync; |
| 1093 |
end if; |
| 1094 |
end P_Subprogram_Specification; |
| 1095 |
|
| 1096 |
--------------------- |
| 1097 |
-- 6.1 Designator -- |
| 1098 |
--------------------- |
| 1099 |
|
| 1100 |
-- DESIGNATOR ::= |
| 1101 |
-- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL |
| 1102 |
|
| 1103 |
-- The caller has checked that the initial token is an identifier, |
| 1104 |
-- operator symbol, or string literal. Note that we don't bother to |
| 1105 |
-- do much error diagnosis in this routine, since it is only used for |
| 1106 |
-- the label on END lines, and the routines in package Par.Endh will |
| 1107 |
-- check that the label is appropriate. |
| 1108 |
|
| 1109 |
-- Error recovery: cannot raise Error_Resync |
| 1110 |
|
| 1111 |
function P_Designator return Node_Id is |
| 1112 |
Ident_Node : Node_Id; |
| 1113 |
Name_Node : Node_Id; |
| 1114 |
Prefix_Node : Node_Id; |
| 1115 |
|
| 1116 |
function Real_Dot return Boolean; |
| 1117 |
-- Tests if a current token is an interesting period, i.e. is followed |
| 1118 |
-- by an identifier or operator symbol or string literal. If not, it is |
| 1119 |
-- probably just incorrect punctuation to be caught by our caller. Note |
| 1120 |
-- that the case of an operator symbol or string literal is also an |
| 1121 |
-- error, but that is an error that we catch here. If the result is |
| 1122 |
-- True, a real dot has been scanned and we are positioned past it, |
| 1123 |
-- if the result is False, the scan position is unchanged. |
| 1124 |
|
| 1125 |
-------------- |
| 1126 |
-- Real_Dot -- |
| 1127 |
-------------- |
| 1128 |
|
| 1129 |
function Real_Dot return Boolean is |
| 1130 |
Scan_State : Saved_Scan_State; |
| 1131 |
|
| 1132 |
begin |
| 1133 |
if Token /= Tok_Dot then |
| 1134 |
return False; |
| 1135 |
|
| 1136 |
else |
| 1137 |
Save_Scan_State (Scan_State); |
| 1138 |
Scan; -- past dot |
| 1139 |
|
| 1140 |
if Token in |
| 1141 |
Tok_Identifier | Tok_Operator_Symbol | Tok_String_Literal |
| 1142 |
then |
| 1143 |
return True; |
| 1144 |
|
| 1145 |
else |
| 1146 |
Restore_Scan_State (Scan_State); |
| 1147 |
return False; |
| 1148 |
end if; |
| 1149 |
end if; |
| 1150 |
end Real_Dot; |
| 1151 |
|
| 1152 |
-- Start of processing for P_Designator |
| 1153 |
|
| 1154 |
begin |
| 1155 |
Ident_Node := Token_Node; |
| 1156 |
Scan; -- past initial token |
| 1157 |
|
| 1158 |
if Prev_Token in Tok_Operator_Symbol | Tok_String_Literal |
| 1159 |
or else not Real_Dot |
| 1160 |
then |
| 1161 |
return Ident_Node; |
| 1162 |
|
| 1163 |
-- Child name case |
| 1164 |
|
| 1165 |
else |
| 1166 |
Prefix_Node := Ident_Node; |
| 1167 |
|
| 1168 |
-- Loop through child names, on entry to this loop, Prefix contains |
| 1169 |
-- the name scanned so far, and Ident_Node is the last identifier. |
| 1170 |
|
| 1171 |
loop |
| 1172 |
Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr); |
| 1173 |
Set_Prefix (Name_Node, Prefix_Node); |
| 1174 |
Ident_Node := P_Identifier; |
| 1175 |
Set_Selector_Name (Name_Node, Ident_Node); |
| 1176 |
Prefix_Node := Name_Node; |
| 1177 |
exit when not Real_Dot; |
| 1178 |
end loop; |
| 1179 |
|
| 1180 |
-- On exit from the loop, Ident_Node is the last identifier scanned, |
| 1181 |
-- i.e. the defining identifier, and Prefix_Node is a node for the |
| 1182 |
-- entire name, structured (incorrectly) as a selected component. |
| 1183 |
|
| 1184 |
Name_Node := Prefix (Prefix_Node); |
| 1185 |
Change_Node (Prefix_Node, N_Designator); |
| 1186 |
Set_Name (Prefix_Node, Name_Node); |
| 1187 |
Set_Identifier (Prefix_Node, Ident_Node); |
| 1188 |
return Prefix_Node; |
| 1189 |
end if; |
| 1190 |
|
| 1191 |
exception |
| 1192 |
when Error_Resync => |
| 1193 |
while Token in Tok_Dot | Tok_Identifier loop |
| 1194 |
Scan; |
| 1195 |
end loop; |
| 1196 |
|
| 1197 |
return Error; |
| 1198 |
end P_Designator; |
| 1199 |
|
| 1200 |
------------------------------ |
| 1201 |
-- 6.1 Defining Designator -- |
| 1202 |
------------------------------ |
| 1203 |
|
| 1204 |
-- DEFINING_DESIGNATOR ::= |
| 1205 |
-- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL |
| 1206 |
|
| 1207 |
-- Error recovery: cannot raise Error_Resync |
| 1208 |
|
| 1209 |
function P_Defining_Designator return Node_Id is |
| 1210 |
begin |
| 1211 |
if Token = Tok_Operator_Symbol then |
| 1212 |
return P_Defining_Operator_Symbol; |
| 1213 |
|
| 1214 |
elsif Token = Tok_String_Literal then |
| 1215 |
Error_Msg_SC ("invalid operator name"); |
| 1216 |
Scan; -- past junk string |
| 1217 |
return Error; |
| 1218 |
|
| 1219 |
else |
| 1220 |
return P_Defining_Program_Unit_Name; |
| 1221 |
end if; |
| 1222 |
end P_Defining_Designator; |
| 1223 |
|
| 1224 |
------------------------------------- |
| 1225 |
-- 6.1 Defining Program Unit Name -- |
| 1226 |
------------------------------------- |
| 1227 |
|
| 1228 |
-- DEFINING_PROGRAM_UNIT_NAME ::= |
| 1229 |
-- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER |
| 1230 |
|
| 1231 |
-- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level |
| 1232 |
|
| 1233 |
-- Error recovery: cannot raise Error_Resync |
| 1234 |
|
| 1235 |
function P_Defining_Program_Unit_Name return Node_Id is |
| 1236 |
Ident_Node : Node_Id; |
| 1237 |
Name_Node : Node_Id; |
| 1238 |
Prefix_Node : Node_Id; |
| 1239 |
|
| 1240 |
begin |
| 1241 |
-- Set identifier casing if not already set and scan initial identifier |
| 1242 |
|
| 1243 |
if Token = Tok_Identifier |
| 1244 |
and then Identifier_Casing (Current_Source_File) = Unknown |
| 1245 |
then |
| 1246 |
Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing); |
| 1247 |
end if; |
| 1248 |
|
| 1249 |
Ident_Node := P_Identifier (C_Dot); |
| 1250 |
Merge_Identifier (Ident_Node, Tok_Return); |
| 1251 |
|
| 1252 |
-- Normal case (not child library unit name) |
| 1253 |
|
| 1254 |
if Token /= Tok_Dot then |
| 1255 |
Change_Identifier_To_Defining_Identifier (Ident_Node); |
| 1256 |
Warn_If_Standard_Redefinition (Ident_Node); |
| 1257 |
return Ident_Node; |
| 1258 |
|
| 1259 |
-- Child library unit name case |
| 1260 |
|
| 1261 |
else |
| 1262 |
if Scope.Last > 1 then |
| 1263 |
Error_Msg_SP ("child unit allowed only at library level"); |
| 1264 |
raise Error_Resync; |
| 1265 |
|
| 1266 |
elsif Ada_Version = Ada_83 then |
| 1267 |
Error_Msg_SP ("(Ada 83) child unit not allowed!"); |
| 1268 |
|
| 1269 |
end if; |
| 1270 |
|
| 1271 |
Prefix_Node := Ident_Node; |
| 1272 |
|
| 1273 |
-- Loop through child names, on entry to this loop, Prefix contains |
| 1274 |
-- the name scanned so far, and Ident_Node is the last identifier. |
| 1275 |
|
| 1276 |
loop |
| 1277 |
exit when Token /= Tok_Dot; |
| 1278 |
Name_Node := New_Node (N_Selected_Component, Token_Ptr); |
| 1279 |
Scan; -- past period |
| 1280 |
Set_Prefix (Name_Node, Prefix_Node); |
| 1281 |
Ident_Node := P_Identifier (C_Dot); |
| 1282 |
Set_Selector_Name (Name_Node, Ident_Node); |
| 1283 |
Prefix_Node := Name_Node; |
| 1284 |
end loop; |
| 1285 |
|
| 1286 |
-- On exit from the loop, Ident_Node is the last identifier scanned, |
| 1287 |
-- i.e. the defining identifier, and Prefix_Node is a node for the |
| 1288 |
-- entire name, structured (incorrectly) as a selected component. |
| 1289 |
|
| 1290 |
Name_Node := Prefix (Prefix_Node); |
| 1291 |
Change_Node (Prefix_Node, N_Defining_Program_Unit_Name); |
| 1292 |
Set_Name (Prefix_Node, Name_Node); |
| 1293 |
Change_Identifier_To_Defining_Identifier (Ident_Node); |
| 1294 |
Warn_If_Standard_Redefinition (Ident_Node); |
| 1295 |
Set_Defining_Identifier (Prefix_Node, Ident_Node); |
| 1296 |
|
| 1297 |
-- All set with unit name parsed |
| 1298 |
|
| 1299 |
return Prefix_Node; |
| 1300 |
end if; |
| 1301 |
|
| 1302 |
exception |
| 1303 |
when Error_Resync => |
| 1304 |
while Token in Tok_Dot | Tok_Identifier loop |
| 1305 |
Scan; |
| 1306 |
end loop; |
| 1307 |
|
| 1308 |
return Error; |
| 1309 |
end P_Defining_Program_Unit_Name; |
| 1310 |
|
| 1311 |
-------------------------- |
| 1312 |
-- 6.1 Operator Symbol -- |
| 1313 |
-------------------------- |
| 1314 |
|
| 1315 |
-- OPERATOR_SYMBOL ::= STRING_LITERAL |
| 1316 |
|
| 1317 |
-- Operator symbol is returned by the scanner as Tok_Operator_Symbol |
| 1318 |
|
| 1319 |
----------------------------------- |
| 1320 |
-- 6.1 Defining Operator Symbol -- |
| 1321 |
----------------------------------- |
| 1322 |
|
| 1323 |
-- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL |
| 1324 |
|
| 1325 |
-- The caller has checked that the initial symbol is an operator symbol |
| 1326 |
|
| 1327 |
function P_Defining_Operator_Symbol return Node_Id is |
| 1328 |
Op_Node : Node_Id; |
| 1329 |
|
| 1330 |
begin |
| 1331 |
Op_Node := Token_Node; |
| 1332 |
Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node); |
| 1333 |
Scan; -- past operator symbol |
| 1334 |
return Op_Node; |
| 1335 |
end P_Defining_Operator_Symbol; |
| 1336 |
|
| 1337 |
---------------------------- |
| 1338 |
-- 6.1 Parameter_Profile -- |
| 1339 |
---------------------------- |
| 1340 |
|
| 1341 |
-- PARAMETER_PROFILE ::= [FORMAL_PART] |
| 1342 |
|
| 1343 |
-- Empty is returned if no formal part is present |
| 1344 |
|
| 1345 |
-- Error recovery: cannot raise Error_Resync |
| 1346 |
|
| 1347 |
function P_Parameter_Profile return List_Id is |
| 1348 |
begin |
| 1349 |
if Token = Tok_Left_Paren then |
| 1350 |
Scan; -- part left paren |
| 1351 |
return P_Formal_Part; |
| 1352 |
else |
| 1353 |
return No_List; |
| 1354 |
end if; |
| 1355 |
end P_Parameter_Profile; |
| 1356 |
|
| 1357 |
--------------------------------------- |
| 1358 |
-- 6.1 Parameter And Result Profile -- |
| 1359 |
--------------------------------------- |
| 1360 |
|
| 1361 |
-- Parsed by its parent construct, which uses P_Parameter_Profile to |
| 1362 |
-- parse the parameters, and P_Subtype_Mark to parse the return type. |
| 1363 |
|
| 1364 |
---------------------- |
| 1365 |
-- 6.1 Formal part -- |
| 1366 |
---------------------- |
| 1367 |
|
| 1368 |
-- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION}) |
| 1369 |
|
| 1370 |
-- PARAMETER_SPECIFICATION ::= |
| 1371 |
-- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION] |
| 1372 |
-- SUBTYPE_MARK [:= DEFAULT_EXPRESSION] |
| 1373 |
-- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION |
| 1374 |
-- [:= DEFAULT_EXPRESSION] |
| 1375 |
|
| 1376 |
-- This scans the construct Formal_Part. The caller has already checked |
| 1377 |
-- that the initial token is a left parenthesis, and skipped past it, so |
| 1378 |
-- that on entry Token is the first token following the left parenthesis. |
| 1379 |
|
| 1380 |
-- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142) |
| 1381 |
|
| 1382 |
-- Error recovery: cannot raise Error_Resync |
| 1383 |
|
| 1384 |
function P_Formal_Part return List_Id is |
| 1385 |
Specification_List : List_Id; |
| 1386 |
Specification_Node : Node_Id; |
| 1387 |
Scan_State : Saved_Scan_State; |
| 1388 |
Num_Idents : Nat; |
| 1389 |
Ident : Nat; |
| 1390 |
Ident_Sloc : Source_Ptr; |
| 1391 |
Not_Null_Present : Boolean := False; |
| 1392 |
Not_Null_Sloc : Source_Ptr; |
| 1393 |
|
| 1394 |
Idents : array (Int range 1 .. 4096) of Entity_Id; |
| 1395 |
-- This array holds the list of defining identifiers. The upper bound |
| 1396 |
-- of 4096 is intended to be essentially infinite, and we do not even |
| 1397 |
-- bother to check for it being exceeded. |
| 1398 |
|
| 1399 |
begin |
| 1400 |
Specification_List := New_List; |
| 1401 |
Specification_Loop : loop |
| 1402 |
begin |
| 1403 |
if Token = Tok_Pragma then |
| 1404 |
Error_Msg_SC ("pragma not allowed in formal part"); |
| 1405 |
Discard_Junk_Node (P_Pragma (Skipping => True)); |
| 1406 |
end if; |
| 1407 |
|
| 1408 |
Ignore (Tok_Left_Paren); |
| 1409 |
Ident_Sloc := Token_Ptr; |
| 1410 |
Idents (1) := P_Defining_Identifier (C_Comma_Colon); |
| 1411 |
Num_Idents := 1; |
| 1412 |
|
| 1413 |
Ident_Loop : loop |
| 1414 |
exit Ident_Loop when Token = Tok_Colon; |
| 1415 |
|
| 1416 |
-- The only valid tokens are colon and comma, so if we have |
| 1417 |
-- neither do a bit of investigation to see which is the |
| 1418 |
-- better choice for insertion. |
| 1419 |
|
| 1420 |
if Token /= Tok_Comma then |
| 1421 |
|
| 1422 |
-- Assume colon if ALIASED, IN or OUT keyword found |
| 1423 |
|
| 1424 |
exit Ident_Loop when Token = Tok_Aliased or else |
| 1425 |
Token = Tok_In or else |
| 1426 |
Token = Tok_Out; |
| 1427 |
|
| 1428 |
-- Otherwise scan ahead |
| 1429 |
|
| 1430 |
Save_Scan_State (Scan_State); |
| 1431 |
Look_Ahead : loop |
| 1432 |
|
| 1433 |
-- If we run into a semicolon, then assume that a |
| 1434 |
-- colon was missing, e.g. Parms (X Y; ...). Also |
| 1435 |
-- assume missing colon on EOF (a real disaster) |
| 1436 |
-- and on a right paren, e.g. Parms (X Y), and also |
| 1437 |
-- on an assignment symbol, e.g. Parms (X Y := ..) |
| 1438 |
|
| 1439 |
if Token in Tok_Semicolon | Tok_Right_Paren | |
| 1440 |
Tok_EOF | Tok_Colon_Equal |
| 1441 |
then |
| 1442 |
Restore_Scan_State (Scan_State); |
| 1443 |
exit Ident_Loop; |
| 1444 |
|
| 1445 |
-- If we run into a colon, assume that we had a missing |
| 1446 |
-- comma, e.g. Parms (A B : ...). Also assume a missing |
| 1447 |
-- comma if we hit another comma, e.g. Parms (A B, C ..) |
| 1448 |
|
| 1449 |
elsif Token in Tok_Colon | Tok_Comma then |
| 1450 |
Restore_Scan_State (Scan_State); |
| 1451 |
exit Look_Ahead; |
| 1452 |
end if; |
| 1453 |
|
| 1454 |
Scan; |
| 1455 |
end loop Look_Ahead; |
| 1456 |
end if; |
| 1457 |
|
| 1458 |
-- Here if a comma is present, or to be assumed |
| 1459 |
|
| 1460 |
T_Comma; |
| 1461 |
Num_Idents := Num_Idents + 1; |
| 1462 |
Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon); |
| 1463 |
end loop Ident_Loop; |
| 1464 |
|
| 1465 |
-- Fall through the loop on encountering a colon, or deciding |
| 1466 |
-- that there is a missing colon. |
| 1467 |
|
| 1468 |
T_Colon; |
| 1469 |
|
| 1470 |
-- If there are multiple identifiers, we repeatedly scan the |
| 1471 |
-- type and initialization expression information by resetting |
| 1472 |
-- the scan pointer (so that we get completely separate trees |
| 1473 |
-- for each occurrence). |
| 1474 |
|
| 1475 |
if Num_Idents > 1 then |
| 1476 |
Save_Scan_State (Scan_State); |
| 1477 |
end if; |
| 1478 |
|
| 1479 |
-- Loop through defining identifiers in list |
| 1480 |
|
| 1481 |
Ident := 1; |
| 1482 |
|
| 1483 |
Ident_List_Loop : loop |
| 1484 |
Specification_Node := |
| 1485 |
New_Node (N_Parameter_Specification, Ident_Sloc); |
| 1486 |
Set_Defining_Identifier (Specification_Node, Idents (Ident)); |
| 1487 |
|
| 1488 |
-- Scan possible ALIASED for Ada 2012 (AI-142) |
| 1489 |
|
| 1490 |
if Token = Tok_Aliased then |
| 1491 |
if Ada_Version < Ada_2012 then |
| 1492 |
Error_Msg_Ada_2012_Feature |
| 1493 |
("ALIASED parameter", Token_Ptr); |
| 1494 |
else |
| 1495 |
Set_Aliased_Present (Specification_Node); |
| 1496 |
end if; |
| 1497 |
|
| 1498 |
Scan; -- past ALIASED |
| 1499 |
end if; |
| 1500 |
|
| 1501 |
-- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447) |
| 1502 |
|
| 1503 |
Not_Null_Sloc := Token_Ptr; |
| 1504 |
Not_Null_Present := |
| 1505 |
P_Null_Exclusion (Allow_Anonymous_In_95 => True); |
| 1506 |
|
| 1507 |
-- Case of ACCESS keyword present |
| 1508 |
|
| 1509 |
if Token = Tok_Access then |
| 1510 |
Set_Null_Exclusion_Present |
| 1511 |
(Specification_Node, Not_Null_Present); |
| 1512 |
|
| 1513 |
if Ada_Version = Ada_83 then |
| 1514 |
Error_Msg_SC ("(Ada 83) access parameters not allowed"); |
| 1515 |
end if; |
| 1516 |
|
| 1517 |
Set_Parameter_Type |
| 1518 |
(Specification_Node, |
| 1519 |
P_Access_Definition (Not_Null_Present)); |
| 1520 |
|
| 1521 |
-- Case of IN or OUT present |
| 1522 |
|
| 1523 |
else |
| 1524 |
if Token in Tok_In | Tok_Out then |
| 1525 |
if Not_Null_Present then |
| 1526 |
Error_Msg |
| 1527 |
("`NOT NULL` can only be used with `ACCESS`", |
| 1528 |
Not_Null_Sloc); |
| 1529 |
|
| 1530 |
if Token = Tok_In then |
| 1531 |
Error_Msg |
| 1532 |
("\`IN` not allowed together with `ACCESS`", |
| 1533 |
Not_Null_Sloc); |
| 1534 |
else |
| 1535 |
Error_Msg |
| 1536 |
("\`OUT` not allowed together with `ACCESS`", |
| 1537 |
Not_Null_Sloc); |
| 1538 |
end if; |
| 1539 |
end if; |
| 1540 |
|
| 1541 |
P_Mode (Specification_Node); |
| 1542 |
Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231) |
| 1543 |
end if; |
| 1544 |
|
| 1545 |
Set_Null_Exclusion_Present |
| 1546 |
(Specification_Node, Not_Null_Present); |
| 1547 |
|
| 1548 |
if Token = Tok_Procedure |
| 1549 |
or else |
| 1550 |
Token = Tok_Function |
| 1551 |
then |
| 1552 |
Error_Msg_SC ("formal subprogram parameter not allowed"); |
| 1553 |
Scan; |
| 1554 |
|
| 1555 |
if Token = Tok_Left_Paren then |
| 1556 |
Discard_Junk_List (P_Formal_Part); |
| 1557 |
end if; |
| 1558 |
|
| 1559 |
if Token = Tok_Return then |
| 1560 |
Scan; |
| 1561 |
Discard_Junk_Node (P_Subtype_Mark); |
| 1562 |
end if; |
| 1563 |
|
| 1564 |
Set_Parameter_Type (Specification_Node, Error); |
| 1565 |
|
| 1566 |
else |
| 1567 |
Set_Parameter_Type (Specification_Node, P_Subtype_Mark); |
| 1568 |
No_Constraint; |
| 1569 |
end if; |
| 1570 |
end if; |
| 1571 |
|
| 1572 |
Set_Expression (Specification_Node, Init_Expr_Opt (True)); |
| 1573 |
|
| 1574 |
if Ident > 1 then |
| 1575 |
Set_Prev_Ids (Specification_Node, True); |
| 1576 |
end if; |
| 1577 |
|
| 1578 |
if Ident < Num_Idents then |
| 1579 |
Set_More_Ids (Specification_Node, True); |
| 1580 |
end if; |
| 1581 |
|
| 1582 |
Append (Specification_Node, Specification_List); |
| 1583 |
exit Ident_List_Loop when Ident = Num_Idents; |
| 1584 |
Ident := Ident + 1; |
| 1585 |
Restore_Scan_State (Scan_State); |
| 1586 |
end loop Ident_List_Loop; |
| 1587 |
|
| 1588 |
exception |
| 1589 |
when Error_Resync => |
| 1590 |
Resync_Semicolon_List; |
| 1591 |
end; |
| 1592 |
|
| 1593 |
if Token = Tok_Semicolon then |
| 1594 |
Save_Scan_State (Scan_State); |
| 1595 |
Scan; -- past semicolon |
| 1596 |
|
| 1597 |
-- If we have RETURN or IS after the semicolon, then assume |
| 1598 |
-- that semicolon should have been a right parenthesis and exit |
| 1599 |
|
| 1600 |
if Token in Tok_Is | Tok_Return then |
| 1601 |
Error_Msg_SP -- CODEFIX |
| 1602 |
("|"";"" should be "")"""); |
| 1603 |
exit Specification_Loop; |
| 1604 |
end if; |
| 1605 |
|
| 1606 |
-- If we have a declaration keyword after the semicolon, then |
| 1607 |
-- assume we had a missing right parenthesis and terminate list |
| 1608 |
|
| 1609 |
if Token in Token_Class_Declk then |
| 1610 |
Error_Msg_AP -- CODEFIX |
| 1611 |
("missing "")"""); |
| 1612 |
Restore_Scan_State (Scan_State); |
| 1613 |
exit Specification_Loop; |
| 1614 |
end if; |
| 1615 |
|
| 1616 |
elsif Token = Tok_Right_Paren then |
| 1617 |
Scan; -- past right paren |
| 1618 |
exit Specification_Loop; |
| 1619 |
|
| 1620 |
-- Support for aspects on formal parameters is a GNAT extension for |
| 1621 |
-- the time being. |
| 1622 |
|
| 1623 |
elsif Token = Tok_With then |
| 1624 |
Error_Msg_Ada_2022_Feature |
| 1625 |
("aspect on formal parameter", Token_Ptr); |
| 1626 |
|
| 1627 |
P_Aspect_Specifications (Specification_Node, Semicolon => False); |
| 1628 |
|
| 1629 |
-- Set the aspect specifications for previous Ids |
| 1630 |
|
| 1631 |
if Has_Aspects (Specification_Node) |
| 1632 |
and then Prev_Ids (Specification_Node) |
| 1633 |
then |
| 1634 |
-- Loop through each previous id |
| 1635 |
|
| 1636 |
declare |
| 1637 |
Prev_Id : Node_Id := Prev (Specification_Node); |
| 1638 |
begin |
| 1639 |
loop |
| 1640 |
Set_Aspect_Specifications |
| 1641 |
(Prev_Id, Aspect_Specifications (Specification_Node)); |
| 1642 |
|
| 1643 |
-- Exit when we reach the first parameter in the list |
| 1644 |
|
| 1645 |
exit when not Prev_Ids (Prev_Id); |
| 1646 |
Prev_Id := Prev (Prev_Id); |
| 1647 |
end loop; |
| 1648 |
end; |
| 1649 |
end if; |
| 1650 |
|
| 1651 |
if Token = Tok_Right_Paren then |
| 1652 |
Scan; -- past right paren |
| 1653 |
exit Specification_Loop; |
| 1654 |
|
| 1655 |
elsif Token = Tok_Semicolon then |
| 1656 |
Save_Scan_State (Scan_State); |
| 1657 |
Scan; -- past semicolon |
| 1658 |
end if; |
| 1659 |
|
| 1660 |
-- Special check for common error of using comma instead of semicolon |
| 1661 |
|
| 1662 |
elsif Token = Tok_Comma then |
| 1663 |
T_Semicolon; |
| 1664 |
|
| 1665 |
-- Special check for omitted separator |
| 1666 |
|
| 1667 |
elsif Token = Tok_Identifier then |
| 1668 |
T_Semicolon; |
| 1669 |
|
| 1670 |
-- If nothing sensible, skip to next semicolon or right paren |
| 1671 |
|
| 1672 |
else |
| 1673 |
T_Semicolon; |
| 1674 |
Resync_Semicolon_List; |
| 1675 |
|
| 1676 |
if Token = Tok_Semicolon then |
| 1677 |
Scan; -- past semicolon |
| 1678 |
else |
| 1679 |
T_Right_Paren; |
| 1680 |
exit Specification_Loop; |
| 1681 |
end if; |
| 1682 |
end if; |
| 1683 |
end loop Specification_Loop; |
| 1684 |
|
| 1685 |
return Specification_List; |
| 1686 |
end P_Formal_Part; |
| 1687 |
|
| 1688 |
---------------------------------- |
| 1689 |
-- 6.1 Parameter Specification -- |
| 1690 |
---------------------------------- |
| 1691 |
|
| 1692 |
-- Parsed by P_Formal_Part (6.1) |
| 1693 |
|
| 1694 |
--------------- |
| 1695 |
-- 6.1 Mode -- |
| 1696 |
--------------- |
| 1697 |
|
| 1698 |
-- MODE ::= [in] | in out | out |
| 1699 |
|
| 1700 |
-- There is no explicit node in the tree for the Mode. Instead the |
| 1701 |
-- In_Present and Out_Present flags are set in the parent node to |
| 1702 |
-- record the presence of keywords specifying the mode. |
| 1703 |
|
| 1704 |
-- Error_Recovery: cannot raise Error_Resync |
| 1705 |
|
| 1706 |
procedure P_Mode (Node : Node_Id) is |
| 1707 |
begin |
| 1708 |
if Token = Tok_In then |
| 1709 |
Scan; -- past IN |
| 1710 |
Set_In_Present (Node, True); |
| 1711 |
|
| 1712 |
if Style.Mode_In_Check and then Token /= Tok_Out then |
| 1713 |
Error_Msg_SP -- CODEFIX |
| 1714 |
("(style) IN should be omitted?I?"); |
| 1715 |
end if; |
| 1716 |
|
| 1717 |
-- Since Ada 2005, formal objects can have an anonymous access type, |
| 1718 |
-- and of course carry a mode indicator. |
| 1719 |
|
| 1720 |
if Token = Tok_Access |
| 1721 |
and then Nkind (Node) /= N_Formal_Object_Declaration |
| 1722 |
then |
| 1723 |
Error_Msg_SP ("IN not allowed together with ACCESS"); |
| 1724 |
Scan; -- past ACCESS |
| 1725 |
end if; |
| 1726 |
end if; |
| 1727 |
|
| 1728 |
if Token = Tok_Out then |
| 1729 |
Scan; -- past OUT |
| 1730 |
Set_Out_Present (Node, True); |
| 1731 |
end if; |
| 1732 |
|
| 1733 |
if Token = Tok_In then |
| 1734 |
Error_Msg_SC ("IN must precede OUT in parameter mode"); |
| 1735 |
Scan; -- past IN |
| 1736 |
Set_In_Present (Node, True); |
| 1737 |
end if; |
| 1738 |
end P_Mode; |
| 1739 |
|
| 1740 |
-------------------------- |
| 1741 |
-- 6.3 Subprogram Body -- |
| 1742 |
-------------------------- |
| 1743 |
|
| 1744 |
-- Parsed by P_Subprogram (6.1) |
| 1745 |
|
| 1746 |
----------------------------------- |
| 1747 |
-- 6.4 Procedure Call Statement -- |
| 1748 |
----------------------------------- |
| 1749 |
|
| 1750 |
-- Parsed by P_Sequence_Of_Statements (5.1) |
| 1751 |
|
| 1752 |
------------------------ |
| 1753 |
-- 6.4 Function Call -- |
| 1754 |
------------------------ |
| 1755 |
|
| 1756 |
-- Parsed by P_Name (4.1) |
| 1757 |
|
| 1758 |
-------------------------------- |
| 1759 |
-- 6.4 Actual Parameter Part -- |
| 1760 |
-------------------------------- |
| 1761 |
|
| 1762 |
-- Parsed by P_Name (4.1) |
| 1763 |
|
| 1764 |
-------------------------------- |
| 1765 |
-- 6.4 Parameter Association -- |
| 1766 |
-------------------------------- |
| 1767 |
|
| 1768 |
-- Parsed by P_Name (4.1) |
| 1769 |
|
| 1770 |
------------------------------------ |
| 1771 |
-- 6.4 Explicit Actual Parameter -- |
| 1772 |
------------------------------------ |
| 1773 |
|
| 1774 |
-- Parsed by P_Name (4.1) |
| 1775 |
|
| 1776 |
--------------------------- |
| 1777 |
-- 6.5 Return Statement -- |
| 1778 |
--------------------------- |
| 1779 |
|
| 1780 |
-- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION]; |
| 1781 |
-- |
| 1782 |
-- EXTENDED_RETURN_STATEMENT ::= |
| 1783 |
-- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION |
| 1784 |
-- [:= EXPRESSION] |
| 1785 |
-- [ASPECT_SPECIFICATION] [do |
| 1786 |
-- HANDLED_SEQUENCE_OF_STATEMENTS |
| 1787 |
-- end return]; |
| 1788 |
-- |
| 1789 |
-- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION |
| 1790 |
|
| 1791 |
-- RETURN_STATEMENT ::= return [EXPRESSION]; |
| 1792 |
|
| 1793 |
-- Error recovery: can raise Error_Resync |
| 1794 |
|
| 1795 |
procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is |
| 1796 |
|
| 1797 |
-- Note: We don't need to check Ada_Version here, because this is |
| 1798 |
-- only called in >= Ada 2005 cases anyway. |
| 1799 |
|
| 1800 |
Not_Null_Present : constant Boolean := P_Null_Exclusion; |
| 1801 |
|
| 1802 |
begin |
| 1803 |
Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present); |
| 1804 |
|
| 1805 |
if Token = Tok_Access then |
| 1806 |
Set_Object_Definition |
| 1807 |
(Decl_Node, P_Access_Definition (Not_Null_Present)); |
| 1808 |
else |
| 1809 |
Set_Object_Definition |
| 1810 |
(Decl_Node, P_Subtype_Indication (Not_Null_Present)); |
| 1811 |
end if; |
| 1812 |
end P_Return_Subtype_Indication; |
| 1813 |
|
| 1814 |
-- Error recovery: can raise Error_Resync |
| 1815 |
|
| 1816 |
function P_Return_Object_Declaration return Node_Id is |
| 1817 |
Return_Obj : Node_Id; |
| 1818 |
Decl_Node : Node_Id; |
| 1819 |
|
| 1820 |
begin |
| 1821 |
Return_Obj := Token_Node; |
| 1822 |
Change_Identifier_To_Defining_Identifier (Return_Obj); |
| 1823 |
Warn_If_Standard_Redefinition (Return_Obj); |
| 1824 |
Decl_Node := New_Node (N_Object_Declaration, Token_Ptr); |
| 1825 |
Set_Defining_Identifier (Decl_Node, Return_Obj); |
| 1826 |
|
| 1827 |
Scan; -- past identifier |
| 1828 |
Scan; -- past : |
| 1829 |
|
| 1830 |
-- First an error check, if we have two identifiers in a row, a likely |
| 1831 |
-- possibility is that the first of the identifiers is an incorrectly |
| 1832 |
-- spelled keyword. See similar check in P_Identifier_Declarations. |
| 1833 |
|
| 1834 |
if Token = Tok_Identifier then |
| 1835 |
declare |
| 1836 |
SS : Saved_Scan_State; |
| 1837 |
I2 : Boolean; |
| 1838 |
|
| 1839 |
begin |
| 1840 |
Save_Scan_State (SS); |
| 1841 |
Scan; -- past initial identifier |
| 1842 |
I2 := (Token = Tok_Identifier); |
| 1843 |
Restore_Scan_State (SS); |
| 1844 |
|
| 1845 |
if I2 |
| 1846 |
and then |
| 1847 |
(Bad_Spelling_Of (Tok_Access) or else |
| 1848 |
Bad_Spelling_Of (Tok_Aliased) or else |
| 1849 |
Bad_Spelling_Of (Tok_Constant)) |
| 1850 |
then |
| 1851 |
null; |
| 1852 |
end if; |
| 1853 |
end; |
| 1854 |
end if; |
| 1855 |
|
| 1856 |
-- We allow "constant" here (as in "return Result : constant |
| 1857 |
-- T..."). This is not in the latest RM, but the ARG is considering an |
| 1858 |
-- AI on the subject (see AI05-0015-1), which we expect to be approved. |
| 1859 |
|
| 1860 |
if Token = Tok_Constant then |
| 1861 |
Scan; -- past CONSTANT |
| 1862 |
Set_Constant_Present (Decl_Node); |
| 1863 |
|
| 1864 |
if Token = Tok_Aliased then |
| 1865 |
Error_Msg_SC -- CODEFIX |
| 1866 |
("ALIASED should be before CONSTANT"); |
| 1867 |
Scan; -- past ALIASED |
| 1868 |
Set_Aliased_Present (Decl_Node); |
| 1869 |
end if; |
| 1870 |
|
| 1871 |
elsif Token = Tok_Aliased then |
| 1872 |
Scan; -- past ALIASED |
| 1873 |
Set_Aliased_Present (Decl_Node); |
| 1874 |
|
| 1875 |
-- The restrictions on the use of aliased in an extended return |
| 1876 |
-- are semantic, not syntactic. |
| 1877 |
|
| 1878 |
if Token = Tok_Constant then |
| 1879 |
Scan; -- past CONSTANT |
| 1880 |
Set_Constant_Present (Decl_Node); |
| 1881 |
end if; |
| 1882 |
end if; |
| 1883 |
|
| 1884 |
P_Return_Subtype_Indication (Decl_Node); |
| 1885 |
|
| 1886 |
if Token = Tok_Colon_Equal then |
| 1887 |
Scan; -- past := |
| 1888 |
Set_Expression (Decl_Node, P_Expression_No_Right_Paren); |
| 1889 |
Set_Has_Init_Expression (Decl_Node); |
| 1890 |
end if; |
| 1891 |
|
| 1892 |
return Decl_Node; |
| 1893 |
end P_Return_Object_Declaration; |
| 1894 |
|
| 1895 |
-- Error recovery: can raise Error_Resync |
| 1896 |
|
| 1897 |
function P_Return_Statement return Node_Id is |
| 1898 |
-- The caller has checked that the initial token is RETURN |
| 1899 |
|
| 1900 |
function Is_Extended return Boolean; |
| 1901 |
-- Scan state is just after RETURN (and is left that way). Determine |
| 1902 |
-- whether this is a simple or extended return statement by looking |
| 1903 |
-- ahead for "identifier :", which implies extended. |
| 1904 |
|
| 1905 |
----------------- |
| 1906 |
-- Is_Extended -- |
| 1907 |
----------------- |
| 1908 |
|
| 1909 |
function Is_Extended return Boolean is |
| 1910 |
Scan_State : Saved_Scan_State; |
| 1911 |
Is_Extended : Boolean := False; |
| 1912 |
|
| 1913 |
begin |
| 1914 |
if Token = Tok_Identifier then |
| 1915 |
Save_Scan_State (Scan_State); -- at identifier |
| 1916 |
Scan; -- past identifier |
| 1917 |
|
| 1918 |
if Token = Tok_Colon then |
| 1919 |
Is_Extended := True; |
| 1920 |
end if; |
| 1921 |
|
| 1922 |
Restore_Scan_State (Scan_State); -- to identifier |
| 1923 |
end if; |
| 1924 |
|
| 1925 |
return Is_Extended; |
| 1926 |
end Is_Extended; |
| 1927 |
|
| 1928 |
Ret_Sloc : constant Source_Ptr := Token_Ptr; |
| 1929 |
Ret_Strt : constant Column_Number := Start_Column; |
| 1930 |
Ret_Node : Node_Id; |
| 1931 |
Decl : Node_Id; |
| 1932 |
|
| 1933 |
-- Start of processing for P_Return_Statement |
| 1934 |
|
| 1935 |
begin |
| 1936 |
Scan; -- past RETURN |
| 1937 |
|
| 1938 |
-- Simple_return_statement, no expression, return an |
| 1939 |
-- N_Simple_Return_Statement node with the expression field left Empty. |
| 1940 |
|
| 1941 |
if Token = Tok_Semicolon then |
| 1942 |
Scan; -- past ; |
| 1943 |
Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc); |
| 1944 |
|
| 1945 |
-- Nontrivial case |
| 1946 |
|
| 1947 |
else |
| 1948 |
-- Extended_return_statement (Ada 2005 only -- AI-318): |
| 1949 |
|
| 1950 |
if Is_Extended then |
| 1951 |
Error_Msg_Ada_2005_Extension ("extended return statement"); |
| 1952 |
|
| 1953 |
Ret_Node := New_Node (N_Extended_Return_Statement, Ret_Sloc); |
| 1954 |
Decl := P_Return_Object_Declaration; |
| 1955 |
Set_Return_Object_Declarations (Ret_Node, New_List (Decl)); |
| 1956 |
|
| 1957 |
if Token = Tok_With then |
| 1958 |
P_Aspect_Specifications (Decl, Semicolon => False); |
| 1959 |
end if; |
| 1960 |
|
| 1961 |
if Token = Tok_Do then |
| 1962 |
Push_Scope_Stack; |
| 1963 |
Scopes (Scope.Last).Ecol := Ret_Strt; |
| 1964 |
Scopes (Scope.Last).Etyp := E_Return; |
| 1965 |
Scopes (Scope.Last).Labl := Error; |
| 1966 |
Scopes (Scope.Last).Sloc := Ret_Sloc; |
| 1967 |
Scan; -- past DO |
| 1968 |
Set_Handled_Statement_Sequence |
| 1969 |
(Ret_Node, P_Handled_Sequence_Of_Statements); |
| 1970 |
End_Statements; |
| 1971 |
|
| 1972 |
-- Do we need to handle Error_Resync here??? |
| 1973 |
end if; |
| 1974 |
|
| 1975 |
-- Simple_return_statement or Return_when_Statement |
| 1976 |
-- with expression. |
| 1977 |
|
| 1978 |
-- We avoid trying to scan an expression if we are at an |
| 1979 |
-- expression terminator since in that case the best error |
| 1980 |
-- message is probably that we have a missing semicolon. |
| 1981 |
|
| 1982 |
else |
| 1983 |
Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc); |
| 1984 |
|
| 1985 |
if Token not in Token_Class_Eterm then |
| 1986 |
Set_Expression (Ret_Node, P_Expression_No_Right_Paren); |
| 1987 |
end if; |
| 1988 |
|
| 1989 |
-- When the next token is WHEN or IF we know that we are looking |
| 1990 |
-- at a Return_when_statement |
| 1991 |
|
| 1992 |
if Token = Tok_When and then not Missing_Semicolon_On_When then |
| 1993 |
Error_Msg_GNAT_Extension ("return when statement", Token_Ptr); |
| 1994 |
Mutate_Nkind (Ret_Node, N_Return_When_Statement); |
| 1995 |
|
| 1996 |
Scan; -- past WHEN |
| 1997 |
Set_Condition (Ret_Node, P_Condition); |
| 1998 |
|
| 1999 |
-- Allow IF instead of WHEN, giving error message |
| 2000 |
|
| 2001 |
elsif Token = Tok_If then |
| 2002 |
Error_Msg_GNAT_Extension ("return when statement", Token_Ptr); |
| 2003 |
Mutate_Nkind (Ret_Node, N_Return_When_Statement); |
| 2004 |
|
| 2005 |
T_When; |
| 2006 |
Scan; -- past IF used in place of WHEN |
| 2007 |
Set_Condition (Ret_Node, P_Condition); |
| 2008 |
end if; |
| 2009 |
end if; |
| 2010 |
|
| 2011 |
TF_Semicolon; |
| 2012 |
end if; |
| 2013 |
|
| 2014 |
return Ret_Node; |
| 2015 |
end P_Return_Statement; |
| 2016 |
|
| 2017 |
end Ch6; |