1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2022, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 pragma Style_Checks
(All_Checks
);
27 -- Turn off subprogram body ordering check. Subprograms are in order by RM
28 -- section rather than alphabetical.
30 with Sinfo
.CN
; use Sinfo
.CN
;
35 -- Local functions, used only in this chapter
37 function P_Case_Statement
return Node_Id
;
38 function P_Case_Statement_Alternative
return Node_Id
;
39 function P_Exit_Statement
return Node_Id
;
40 function P_Goto_Statement
return Node_Id
;
41 function P_If_Statement
return Node_Id
;
42 function P_Label
return Node_Id
;
43 function P_Null_Statement
return Node_Id
;
45 function P_Assignment_Statement
(LHS
: Node_Id
) return Node_Id
;
46 -- Parse assignment statement. On entry, the caller has scanned the left
47 -- hand side (passed in as Lhs), and the colon-equal (or some symbol
48 -- taken to be an error equivalent such as equal).
50 function P_Begin_Statement
(Block_Name
: Node_Id
:= Empty
) return Node_Id
;
51 -- Parse begin-end statement. If Block_Name is non-Empty on entry, it is
52 -- the N_Identifier node for the label on the block. If Block_Name is
53 -- Empty on entry (the default), then the block statement is unlabeled.
55 function P_Declare_Statement
(Block_Name
: Node_Id
:= Empty
) return Node_Id
;
56 -- Parse declare block. If Block_Name is non-Empty on entry, it is
57 -- the N_Identifier node for the label on the block. If Block_Name is
58 -- Empty on entry (the default), then the block statement is unlabeled.
60 function P_For_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
;
61 -- Parse for statement. If Loop_Name is non-Empty on entry, it is
62 -- the N_Identifier node for the label on the loop. If Loop_Name is
63 -- Empty on entry (the default), then the for statement is unlabeled.
65 function P_Loop_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
;
66 -- Parse loop statement. If Loop_Name is non-Empty on entry, it is
67 -- the N_Identifier node for the label on the loop. If Loop_Name is
68 -- Empty on entry (the default), then the loop statement is unlabeled.
70 function P_While_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
;
71 -- Parse while statement. If Loop_Name is non-Empty on entry, it is
72 -- the N_Identifier node for the label on the loop. If Loop_Name is
73 -- Empty on entry (the default), then the while statement is unlabeled.
75 function Set_Loop_Block_Name
(L
: Character) return Name_Id
;
76 -- Given a letter 'L' for a loop or 'B' for a block, returns a name
77 -- of the form L_nn or B_nn where nn is a serial number obtained by
78 -- incrementing the variable Loop_Block_Count.
81 -- Scan past THEN token, testing for illegal junk after it
83 ---------------------------------
84 -- 5.1 Sequence of Statements --
85 ---------------------------------
87 -- SEQUENCE_OF_STATEMENTS ::= STATEMENT {STATEMENT} {LABEL}
88 -- Note: the final label is an Ada 2012 addition.
91 -- {LABEL} SIMPLE_STATEMENT | {LABEL} COMPOUND_STATEMENT
93 -- SIMPLE_STATEMENT ::= NULL_STATEMENT
94 -- | ASSIGNMENT_STATEMENT | EXIT_STATEMENT
95 -- | GOTO_STATEMENT | PROCEDURE_CALL_STATEMENT
96 -- | RETURN_STATEMENT | ENTRY_CALL_STATEMENT
97 -- | REQUEUE_STATEMENT | DELAY_STATEMENT
98 -- | ABORT_STATEMENT | RAISE_STATEMENT
101 -- COMPOUND_STATEMENT ::=
102 -- IF_STATEMENT | CASE_STATEMENT
103 -- | LOOP_STATEMENT | BLOCK_STATEMENT
104 -- | ACCEPT_STATEMENT | SELECT_STATEMENT
106 -- This procedure scans a sequence of statements. The caller sets SS_Flags
107 -- to indicate acceptable termination conditions for the sequence:
109 -- SS_Flags.Eftm Terminate on ELSIF
110 -- SS_Flags.Eltm Terminate on ELSE
111 -- SS_Flags.Extm Terminate on EXCEPTION
112 -- SS_Flags.Ortm Terminate on OR
113 -- SS_Flags.Tatm Terminate on THEN ABORT (Token = ABORT on return)
114 -- SS_Flags.Whtm Terminate on WHEN
115 -- SS_Flags.Unco Unconditional terminate after scanning one statement
117 -- In addition, the scan is always terminated by encountering END or the
118 -- end of file (EOF) condition. If one of the six above terminators is
119 -- encountered with the corresponding SS_Flags flag not set, then the
120 -- action taken is as follows:
122 -- If the keyword occurs to the left of the expected column of the end
123 -- for the current sequence (as recorded in the current end context),
124 -- then it is assumed to belong to an outer context, and is considered
125 -- to terminate the sequence of statements.
127 -- If the keyword occurs to the right of, or in the expected column of
128 -- the end for the current sequence, then an error message is output,
129 -- the keyword together with its associated context is skipped, and
130 -- the statement scan continues until another terminator is found.
132 -- Note that the first action means that control can return to the caller
133 -- with Token set to a terminator other than one of those specified by the
134 -- SS parameter. The caller should treat such a case as equivalent to END.
136 -- In addition, the flag SS_Flags.Sreq is set to True to indicate that at
137 -- least one real statement (other than a pragma) is required in the
138 -- statement sequence. During the processing of the sequence, this
139 -- flag is manipulated to indicate the current status of the requirement
140 -- for a statement. For example, it is turned off by the occurrence of a
141 -- statement, and back on by a label (which requires a following statement)
143 -- Error recovery: cannot raise Error_Resync. If an error occurs during
144 -- parsing a statement, then the scan pointer is advanced past the next
145 -- semicolon and the parse continues.
147 function P_Sequence_Of_Statements
(SS_Flags
: SS_Rec
) return List_Id
is
149 Statement_Required
: Boolean;
150 -- This flag indicates if a subsequent statement (other than a pragma)
151 -- is required. It is initialized from the Sreq flag, and modified as
152 -- statements are scanned (a statement turns it off, and a label turns
153 -- it back on again since a statement must follow a label).
154 -- Note : this final requirement is lifted in Ada 2012.
156 Statement_Seen
: Boolean;
157 -- In Ada 2012, a label can end a sequence of statements, but the
158 -- sequence cannot contain only labels. This flag is set whenever a
159 -- label is encountered, to enforce this rule at the end of a sequence.
161 Declaration_Found
: Boolean := False;
162 -- This flag is set True if a declaration is encountered, so that the
163 -- error message about declarations in the statement part is only
164 -- given once for a given sequence of statements.
166 Scan_State_Label
: Saved_Scan_State
;
167 Scan_State
: Saved_Scan_State
;
169 Statement_List
: List_Id
;
170 Block_Label
: Name_Id
;
174 procedure Junk_Declaration
;
175 -- Procedure called to handle error of declaration encountered in
176 -- statement sequence.
178 procedure Test_Statement_Required
;
179 -- Flag error if Statement_Required flag set
181 ----------------------
182 -- Junk_Declaration --
183 ----------------------
185 procedure Junk_Declaration
is
187 if (not Declaration_Found
) or All_Errors_Mode
then
188 Error_Msg_SC
-- CODEFIX
189 ("declarations must come before BEGIN");
190 Declaration_Found
:= True;
193 Skip_Declaration
(Statement_List
);
194 end Junk_Declaration
;
196 -----------------------------
197 -- Test_Statement_Required --
198 -----------------------------
200 procedure Test_Statement_Required
is
201 function All_Pragmas
return Boolean;
202 -- Return True if statement list is all pragmas
208 function All_Pragmas
return Boolean is
211 S
:= First
(Statement_List
);
212 while Present
(S
) loop
213 if Nkind
(S
) /= N_Pragma
then
223 -- Start of processing for Test_Statement_Required
226 if Statement_Required
then
228 -- Check no statement required after label in Ada 2012, and that
229 -- it is OK to have nothing but pragmas in a statement sequence.
231 if Ada_Version
>= Ada_2012
232 and then not Is_Empty_List
(Statement_List
)
234 ((Nkind
(Last
(Statement_List
)) = N_Label
235 and then Statement_Seen
)
239 Null_Stm
: constant Node_Id
:=
240 Make_Null_Statement
(Token_Ptr
);
242 Set_Comes_From_Source
(Null_Stm
, False);
243 Append_To
(Statement_List
, Null_Stm
);
246 -- If not Ada 2012, or not special case above, give error message
249 Error_Msg_BC
-- CODEFIX
250 ("statement expected");
253 end Test_Statement_Required
;
255 -- Start of processing for P_Sequence_Of_Statements
258 Statement_List
:= New_List
;
259 Statement_Required
:= SS_Flags
.Sreq
;
260 Statement_Seen
:= False;
263 Ignore
(Tok_Semicolon
);
267 Style
.Check_Indentation
;
270 -- Deal with reserved identifier (in assignment or call)
272 if Is_Reserved_Identifier
then
273 Save_Scan_State
(Scan_State
); -- at possible bad identifier
274 Scan
; -- and scan past it
276 -- We have an reserved word which is spelled in identifier
277 -- style, so the question is whether it really is intended
278 -- to be an identifier.
281 -- If followed by a semicolon, then it is an identifier,
282 -- with the exception of the cases tested for below.
284 (Token
= Tok_Semicolon
285 and then Prev_Token
/= Tok_Return
286 and then Prev_Token
/= Tok_Null
287 and then Prev_Token
/= Tok_Raise
288 and then Prev_Token
/= Tok_End
289 and then Prev_Token
/= Tok_Exit
)
291 -- If followed by colon, colon-equal, or dot, then we
292 -- definitely have an identifier (could not be reserved)
294 or else Token
= Tok_Colon
295 or else Token
= Tok_Colon_Equal
296 or else Token
= Tok_Dot
298 -- Left paren means we have an identifier except for those
299 -- reserved words that can legitimately be followed by a
303 (Token
= Tok_Left_Paren
304 and then Prev_Token
/= Tok_Case
305 and then Prev_Token
/= Tok_Delay
306 and then Prev_Token
/= Tok_If
307 and then Prev_Token
/= Tok_Elsif
308 and then Prev_Token
/= Tok_Return
309 and then Prev_Token
/= Tok_When
310 and then Prev_Token
/= Tok_While
311 and then Prev_Token
/= Tok_Separate
)
313 -- Here we have an apparent reserved identifier and the
314 -- token past it is appropriate to this usage (and would
315 -- be a definite error if this is not an identifier). What
316 -- we do is to use P_Identifier to fix up the identifier,
317 -- and then fall into the normal processing.
319 Restore_Scan_State
(Scan_State
); -- back to the ID
320 Scan_Reserved_Identifier
(Force_Msg
=> False);
322 -- Not a reserved identifier after all (or at least we can't
323 -- be sure that it is), so reset the scan and continue.
326 Restore_Scan_State
(Scan_State
); -- back to the reserved word
330 -- Now look to see what kind of statement we have
334 -- Case of end or EOF
339 -- These tokens always terminate the statement sequence
341 Test_Statement_Required
;
348 -- Terminate if Eftm set or if the ELSIF is to the left
349 -- of the expected column of the end for this sequence
352 or else Start_Column
< Scopes
(Scope
.Last
).Ecol
354 Test_Statement_Required
;
357 -- Otherwise complain and skip past ELSIF Condition then
360 Error_Msg_SC
("ELSIF not allowed here");
362 Discard_Junk_Node
(P_Expression_No_Right_Paren
);
364 Statement_Required
:= False;
371 -- Terminate if Eltm set or if the else is to the left
372 -- of the expected column of the end for this sequence
375 or else Start_Column
< Scopes
(Scope
.Last
).Ecol
377 Test_Statement_Required
;
380 -- Otherwise complain and skip past else
383 Error_Msg_SC
("ELSE not allowed here");
385 Statement_Required
:= False;
390 when Tok_Exception
=>
391 Test_Statement_Required
;
393 -- If Extm not set and the exception is not to the left of
394 -- the expected column of the end for this sequence, then we
395 -- assume it belongs to the current sequence, even though it
398 if not SS_Flags
.Extm
and then
399 Start_Column
>= Scopes
(Scope
.Last
).Ecol
402 Error_Msg_SC
("exception handler not permitted here");
403 Scan
; -- past EXCEPTION
404 Discard_Junk_List
(Parse_Exception_Handlers
);
407 -- Always return, in the case where we scanned out handlers
408 -- that we did not expect, Parse_Exception_Handlers returned
409 -- with Token being either end or EOF, so we are OK.
417 -- Terminate if Ortm set or if the or is to the left of the
418 -- expected column of the end for this sequence.
421 or else Start_Column
< Scopes
(Scope
.Last
).Ecol
423 Test_Statement_Required
;
426 -- Otherwise complain and skip past or
429 Error_Msg_SC
("OR not allowed here");
431 Statement_Required
:= False;
434 -- Case of THEN (deal also with THEN ABORT)
437 Save_Scan_State
(Scan_State
); -- at THEN
440 -- Terminate if THEN ABORT allowed (ATC case)
442 exit when SS_Flags
.Tatm
and then Token
= Tok_Abort
;
444 -- Otherwise we treat THEN as some kind of mess where we did
445 -- not see the associated IF, but we pick up assuming it had
448 Restore_Scan_State
(Scan_State
); -- to THEN
449 Append_To
(Statement_List
, P_If_Statement
);
450 Statement_Required
:= False;
452 -- Case of WHEN (error because we are not in a case)
457 -- Terminate if Whtm set or if the WHEN is to the left of
458 -- the expected column of the end for this sequence.
461 or else Start_Column
< Scopes
(Scope
.Last
).Ecol
463 Test_Statement_Required
;
466 -- Otherwise complain and skip when Choice {| Choice} =>
469 Error_Msg_SC
("WHEN not allowed here");
471 Discard_Junk_List
(P_Discrete_Choice_List
);
473 Statement_Required
:= False;
476 -- Cases of statements starting with an identifier
478 when Tok_Identifier
=>
481 -- Save scan pointers and line number in case block label
483 Id_Node
:= Token_Node
;
484 Block_Label
:= Token_Name
;
485 Save_Scan_State
(Scan_State_Label
); -- at possible label
488 -- Check for common case of assignment, since it occurs
489 -- frequently, and we want to process it efficiently.
491 if Token
= Tok_Colon_Equal
then
492 Scan
; -- past the colon-equal
493 Append_To
(Statement_List
,
494 P_Assignment_Statement
(Id_Node
));
495 Statement_Required
:= False;
497 -- Check common case of procedure call, another case that
498 -- we want to speed up as much as possible.
500 elsif Token
= Tok_Semicolon
then
501 Change_Name_To_Procedure_Call_Statement
(Id_Node
);
502 Append_To
(Statement_List
, Id_Node
);
503 Scan
; -- past semicolon
504 Statement_Required
:= False;
506 -- Here is the special test for a suspicious label, more
507 -- accurately a suspicious name, which we think perhaps
508 -- should have been a label. If next token is one of
509 -- LOOP, FOR, WHILE, DECLARE, BEGIN, then make an entry
510 -- in the suspicious label table.
512 if Token
= Tok_Loop
or else
513 Token
= Tok_For
or else
514 Token
= Tok_While
or else
515 Token
= Tok_Declare
or else
518 Suspicious_Labels
.Append
519 ((Proc_Call
=> Id_Node
,
520 Semicolon_Loc
=> Prev_Token_Ptr
,
521 Start_Token
=> Token_Ptr
));
524 -- Check for case of "go to" in place of "goto"
526 elsif Token
= Tok_Identifier
527 and then Block_Label
= Name_Go
528 and then Token_Name
= Name_To
530 Error_Msg_SP
-- CODEFIX
531 ("goto is one word");
532 Append_To
(Statement_List
, P_Goto_Statement
);
533 Statement_Required
:= False;
535 -- Check common case of = used instead of :=, just so we
536 -- give a better error message for this special misuse.
538 elsif Token
= Tok_Equal
then
539 T_Colon_Equal
; -- give := expected message
540 Append_To
(Statement_List
,
541 P_Assignment_Statement
(Id_Node
));
542 Statement_Required
:= False;
544 -- Check case of loop label or block label
546 elsif Token
= Tok_Colon
547 or else (Token
in Token_Class_Labeled_Stmt
548 and then not Token_Is_At_Start_Of_Line
)
550 T_Colon
; -- past colon (if there, or msg for missing one)
552 -- Test for more than one label
555 exit when Token
/= Tok_Identifier
;
556 Save_Scan_State
(Scan_State
); -- at second Id
559 if Token
= Tok_Colon
then
561 ("only one label allowed on block or loop");
562 Scan
; -- past colon on extra label
564 -- Use the second label as the "real" label
566 Scan_State_Label
:= Scan_State
;
568 -- We will set Error_name as the Block_Label since
569 -- we really don't know which of the labels might
570 -- be used at the end of the loop or block.
572 Block_Label
:= Error_Name
;
574 -- If Id with no colon, then backup to point to the
575 -- Id and we will issue the message below when we try
576 -- to scan out the statement as some other form.
579 Restore_Scan_State
(Scan_State
); -- to second Id
584 -- Loop_Statement (labeled Loop_Statement)
586 if Token
= Tok_Loop
then
587 Append_To
(Statement_List
,
588 P_Loop_Statement
(Id_Node
));
590 -- While statement (labeled loop statement with WHILE)
592 elsif Token
= Tok_While
then
593 Append_To
(Statement_List
,
594 P_While_Statement
(Id_Node
));
596 -- Declare statement (labeled block statement with
599 elsif Token
= Tok_Declare
then
600 Append_To
(Statement_List
,
601 P_Declare_Statement
(Id_Node
));
603 -- Begin statement (labeled block statement with no
606 elsif Token
= Tok_Begin
then
607 Append_To
(Statement_List
,
608 P_Begin_Statement
(Id_Node
));
610 -- For statement (labeled loop statement with FOR)
612 elsif Token
= Tok_For
then
613 Append_To
(Statement_List
,
614 P_For_Statement
(Id_Node
));
616 -- Improper statement follows label. If we have an
617 -- expression token, then assume the colon was part
618 -- of a misplaced declaration.
620 elsif Token
not in Token_Class_Eterm
then
621 Restore_Scan_State
(Scan_State_Label
);
624 -- Otherwise complain we have inappropriate statement
628 ("loop or block statement must follow label");
631 Statement_Required
:= False;
633 -- Here we have an identifier followed by something
634 -- other than a colon, semicolon or assignment symbol.
635 -- The only valid possibility is a name extension symbol
637 elsif Token
in Token_Class_Namext
then
638 Restore_Scan_State
(Scan_State_Label
); -- to Id
641 -- Skip junk right parens in this context
643 Ignore
(Tok_Right_Paren
);
645 -- Check context following call
647 if Token
= Tok_Colon_Equal
then
648 Scan
; -- past colon equal
649 Append_To
(Statement_List
,
650 P_Assignment_Statement
(Name_Node
));
651 Statement_Required
:= False;
653 -- Check common case of = used instead of :=
655 elsif Token
= Tok_Equal
then
656 T_Colon_Equal
; -- give := expected message
657 Append_To
(Statement_List
,
658 P_Assignment_Statement
(Name_Node
));
659 Statement_Required
:= False;
661 -- Check apostrophe cases
663 elsif Token
= Tok_Apostrophe
then
664 Append_To
(Statement_List
,
665 P_Code_Statement
(Name_Node
));
666 Statement_Required
:= False;
668 -- The only other valid item after a name is ; which
669 -- means that the item we just scanned was a call.
671 elsif Token
= Tok_Semicolon
then
672 Change_Name_To_Procedure_Call_Statement
(Name_Node
);
673 Append_To
(Statement_List
, Name_Node
);
674 Scan
; -- past semicolon
675 Statement_Required
:= False;
677 -- A slash following an identifier or a selected
678 -- component in this situation is most likely a period
679 -- (see location of keys on keyboard).
681 elsif Token
= Tok_Slash
682 and then (Nkind
(Name_Node
) = N_Identifier
684 Nkind
(Name_Node
) = N_Selected_Component
)
686 Error_Msg_SC
-- CODEFIX
687 ("""/"" should be "".""");
688 Statement_Required
:= False;
691 -- Else we have a missing semicolon
696 -- Normal processing as though semicolon were present
698 Change_Name_To_Procedure_Call_Statement
(Name_Node
);
699 Append_To
(Statement_List
, Name_Node
);
700 Statement_Required
:= False;
703 -- If junk after identifier, check if identifier is an
704 -- instance of an incorrectly spelled keyword. If so, we
705 -- do nothing. The Bad_Spelling_Of will have reset Token
706 -- to the appropriate keyword, so the next time round the
707 -- loop we will process the modified token. Note that we
708 -- check for ELSIF before ELSE here. That's not accidental.
709 -- We don't want to identify a misspelling of ELSE as
710 -- ELSIF, and in particular we do not want to treat ELSEIF
714 Restore_Scan_State
(Scan_State_Label
); -- to identifier
716 if Bad_Spelling_Of
(Tok_Abort
)
717 or else Bad_Spelling_Of
(Tok_Accept
)
718 or else Bad_Spelling_Of
(Tok_Case
)
719 or else Bad_Spelling_Of
(Tok_Declare
)
720 or else Bad_Spelling_Of
(Tok_Delay
)
721 or else Bad_Spelling_Of
(Tok_Elsif
)
722 or else Bad_Spelling_Of
(Tok_Else
)
723 or else Bad_Spelling_Of
(Tok_End
)
724 or else Bad_Spelling_Of
(Tok_Exception
)
725 or else Bad_Spelling_Of
(Tok_Exit
)
726 or else Bad_Spelling_Of
(Tok_For
)
727 or else Bad_Spelling_Of
(Tok_Goto
)
728 or else Bad_Spelling_Of
(Tok_If
)
729 or else Bad_Spelling_Of
(Tok_Loop
)
730 or else Bad_Spelling_Of
(Tok_Or
)
731 or else Bad_Spelling_Of
(Tok_Pragma
)
732 or else Bad_Spelling_Of
(Tok_Raise
)
733 or else Bad_Spelling_Of
(Tok_Requeue
)
734 or else Bad_Spelling_Of
(Tok_Return
)
735 or else Bad_Spelling_Of
(Tok_Select
)
736 or else Bad_Spelling_Of
(Tok_When
)
737 or else Bad_Spelling_Of
(Tok_While
)
741 -- If not a bad spelling, then we really have junk
744 Scan
; -- past identifier again
746 -- If next token is first token on line, then we
747 -- consider that we were missing a semicolon after
748 -- the identifier, and process it as a procedure
749 -- call with no parameters.
751 if Token_Is_At_Start_Of_Line
then
752 Change_Name_To_Procedure_Call_Statement
(Id_Node
);
753 Append_To
(Statement_List
, Id_Node
);
754 T_Semicolon
; -- to give error message
755 Statement_Required
:= False;
757 -- Otherwise we give a missing := message and
758 -- simply abandon the junk that is there now.
761 T_Colon_Equal
; -- give := expected message
768 -- Statement starting with operator symbol. This could be
769 -- a call, a name starting an assignment, or a qualified
772 when Tok_Operator_Symbol
=>
776 -- An attempt at a range attribute or a qualified expression
777 -- must be illegal here (a code statement cannot possibly
778 -- allow qualification by a function name).
780 if Token
= Tok_Apostrophe
then
781 Error_Msg_SC
("apostrophe illegal here");
785 -- Scan possible assignment if we have a name
787 if Expr_Form
= EF_Name
788 and then Token
= Tok_Colon_Equal
790 Scan
; -- past colon equal
791 Append_To
(Statement_List
,
792 P_Assignment_Statement
(Name_Node
));
794 Change_Name_To_Procedure_Call_Statement
(Name_Node
);
795 Append_To
(Statement_List
, Name_Node
);
799 Statement_Required
:= False;
801 -- Label starting with << which must precede real statement
802 -- Note: in Ada 2012, the label may end the sequence.
804 when Tok_Less_Less
=>
805 if Present
(Last
(Statement_List
))
806 and then Nkind
(Last
(Statement_List
)) /= N_Label
808 Statement_Seen
:= True;
811 Append_To
(Statement_List
, P_Label
);
812 Statement_Required
:= True;
814 -- Pragma appearing as a statement in a statement sequence
818 Append_To
(Statement_List
, P_Pragma
);
824 Append_To
(Statement_List
, P_Abort_Statement
);
825 Statement_Required
:= False;
831 Append_To
(Statement_List
, P_Accept_Statement
);
832 Statement_Required
:= False;
834 -- Begin_Statement (Block_Statement with no declare, no label)
838 Append_To
(Statement_List
, P_Begin_Statement
);
839 Statement_Required
:= False;
845 Append_To
(Statement_List
, P_Case_Statement
);
846 Statement_Required
:= False;
848 -- Block_Statement with DECLARE and no label
852 Append_To
(Statement_List
, P_Declare_Statement
);
853 Statement_Required
:= False;
859 Append_To
(Statement_List
, P_Delay_Statement
);
860 Statement_Required
:= False;
866 Append_To
(Statement_List
, P_Exit_Statement
);
867 Statement_Required
:= False;
869 -- Loop_Statement with FOR and no label
873 Append_To
(Statement_List
, P_For_Statement
);
874 Statement_Required
:= False;
880 Append_To
(Statement_List
, P_Goto_Statement
);
881 Statement_Required
:= False;
887 Append_To
(Statement_List
, P_If_Statement
);
888 Statement_Required
:= False;
894 Append_To
(Statement_List
, P_Loop_Statement
);
895 Statement_Required
:= False;
901 Append_To
(Statement_List
, P_Null_Statement
);
902 Statement_Required
:= False;
908 Append_To
(Statement_List
, P_Raise_Statement
);
909 Statement_Required
:= False;
915 Append_To
(Statement_List
, P_Requeue_Statement
);
916 Statement_Required
:= False;
922 Append_To
(Statement_List
, P_Return_Statement
);
923 Statement_Required
:= False;
929 Append_To
(Statement_List
, P_Select_Statement
);
930 Statement_Required
:= False;
932 -- While_Statement (Block_Statement with while and no loop)
936 Append_To
(Statement_List
, P_While_Statement
);
937 Statement_Required
:= False;
939 -- Anything else is some kind of junk, signal an error message
940 -- and then raise Error_Resync, to merge with the normal
941 -- handling of a bad statement.
944 if Token
in Token_Class_Declk
then
948 Error_Msg_BC
-- CODEFIX
949 ("statement expected");
954 -- On error resynchronization, skip past next semicolon, and, since
955 -- we are still in the statement loop, look for next statement. We
956 -- set Statement_Required False to avoid an unnecessary error message
957 -- complaining that no statement was found (i.e. we consider the
958 -- junk to satisfy the requirement for a statement being present).
962 Resync_Past_Semicolon_Or_To_Loop_Or_Then
;
963 Statement_Required
:= False;
966 exit when SS_Flags
.Unco
;
969 return Statement_List
;
970 end P_Sequence_Of_Statements
;
976 ---------------------------
977 -- 5.1 Simple Statement --
978 ---------------------------
980 -- Parsed by P_Sequence_Of_Statements (5.1)
982 -----------------------------
983 -- 5.1 Compound Statement --
984 -----------------------------
986 -- Parsed by P_Sequence_Of_Statements (5.1)
988 -------------------------
989 -- 5.1 Null Statement --
990 -------------------------
992 -- NULL_STATEMENT ::= null;
994 -- The caller has already checked that the current token is null
996 -- Error recovery: cannot raise Error_Resync
998 function P_Null_Statement
return Node_Id
is
999 Null_Stmt_Node
: Node_Id
;
1002 Null_Stmt_Node
:= New_Node
(N_Null_Statement
, Token_Ptr
);
1005 return Null_Stmt_Node
;
1006 end P_Null_Statement
;
1012 -- LABEL ::= <<label_STATEMENT_IDENTIFIER>>
1014 -- STATEMENT_IDENTIFIER ::= DIRECT_NAME
1016 -- The IDENTIFIER of a STATEMENT_IDENTIFIER shall be an identifier
1017 -- (not an OPERATOR_SYMBOL)
1019 -- The caller has already checked that the current token is <<
1021 -- Error recovery: can raise Error_Resync
1023 function P_Label
return Node_Id
is
1024 Label_Node
: Node_Id
;
1027 Label_Node
:= New_Node
(N_Label
, Token_Ptr
);
1029 Set_Identifier
(Label_Node
, P_Identifier
(C_Greater_Greater
));
1031 Append_Elmt
(Label_Node
, Label_List
);
1035 -------------------------------
1036 -- 5.1 Statement Identifier --
1037 -------------------------------
1039 -- Statement label is parsed by P_Label (5.1)
1041 -- Loop label is parsed by P_Loop_Statement (5.5), P_For_Statement (5.5)
1042 -- or P_While_Statement (5.5)
1044 -- Block label is parsed by P_Begin_Statement (5.6) or
1045 -- P_Declare_Statement (5.6)
1047 -------------------------------
1048 -- 5.2 Assignment Statement --
1049 -------------------------------
1051 -- ASSIGNMENT_STATEMENT ::=
1052 -- variable_NAME := EXPRESSION;
1054 -- Error recovery: can raise Error_Resync
1056 function P_Assignment_Statement
(LHS
: Node_Id
) return Node_Id
is
1057 Assign_Node
: Node_Id
;
1060 Assign_Node
:= New_Node
(N_Assignment_Statement
, Prev_Token_Ptr
);
1061 Current_Assign_Node
:= Assign_Node
;
1062 Set_Name
(Assign_Node
, LHS
);
1063 Set_Expression
(Assign_Node
, P_Expression_No_Right_Paren
);
1065 Current_Assign_Node
:= Empty
;
1067 end P_Assignment_Statement
;
1069 -----------------------
1070 -- 5.3 If Statement --
1071 -----------------------
1074 -- if CONDITION then
1075 -- SEQUENCE_OF_STATEMENTS
1076 -- {elsif CONDITION then
1077 -- SEQUENCE_OF_STATEMENTS}
1079 -- SEQUENCE_OF_STATEMENTS]
1082 -- The caller has checked that the initial token is IF (or in the error
1083 -- case of a mysterious THEN, the initial token may simply be THEN, in
1084 -- which case, no condition (or IF) was scanned).
1086 -- Error recovery: can raise Error_Resync
1088 function P_If_Statement
return Node_Id
is
1090 Elsif_Node
: Node_Id
;
1093 procedure Add_Elsif_Part
;
1094 -- An internal procedure used to scan out a single ELSIF part. On entry
1095 -- the ELSIF (or an ELSE which has been determined should be ELSIF) is
1096 -- scanned out and is in Prev_Token.
1098 procedure Check_If_Column
;
1099 -- An internal procedure used to check that THEN, ELSE, or ELSIF
1100 -- appear in the right place if column checking is enabled (i.e. if
1101 -- they are the first token on the line, then they must appear in
1102 -- the same column as the opening IF).
1104 procedure Check_Then_Column
;
1105 -- This procedure carries out the style checks for a THEN token
1106 -- Note that the caller has set Loc to the Source_Ptr value for
1107 -- the previous IF or ELSIF token.
1109 function Else_Should_Be_Elsif
return Boolean;
1110 -- An internal routine used to do a special error recovery check when
1111 -- an ELSE is encountered. It determines if the ELSE should be treated
1112 -- as an ELSIF. A positive decision (TRUE returned, is made if the ELSE
1113 -- is followed by a sequence of tokens, starting on the same line as
1114 -- the ELSE, which are not expression terminators, followed by a THEN.
1115 -- On entry, the ELSE has been scanned out.
1117 procedure Add_Elsif_Part
is
1119 if No
(Elsif_Parts
(If_Node
)) then
1120 Set_Elsif_Parts
(If_Node
, New_List
);
1123 Elsif_Node
:= New_Node
(N_Elsif_Part
, Prev_Token_Ptr
);
1124 Loc
:= Prev_Token_Ptr
;
1125 Set_Condition
(Elsif_Node
, P_Condition
);
1129 (Elsif_Node
, P_Sequence_Of_Statements
(SS_Eftm_Eltm_Sreq
));
1130 Append
(Elsif_Node
, Elsif_Parts
(If_Node
));
1133 procedure Check_If_Column
is
1135 if RM_Column_Check
and then Token_Is_At_Start_Of_Line
1136 and then Start_Column
/= Scopes
(Scope
.Last
).Ecol
1138 Error_Msg_Col
:= Scopes
(Scope
.Last
).Ecol
;
1139 Error_Msg_SC
("(style) this token should be@");
1141 end Check_If_Column
;
1143 procedure Check_Then_Column
is
1145 if Token
= Tok_Then
then
1149 Style
.Check_Then
(Loc
);
1152 end Check_Then_Column
;
1154 function Else_Should_Be_Elsif
return Boolean is
1155 Scan_State
: Saved_Scan_State
;
1158 if Token_Is_At_Start_Of_Line
then
1162 Save_Scan_State
(Scan_State
);
1165 if Token
in Token_Class_Eterm
then
1166 Restore_Scan_State
(Scan_State
);
1169 Scan
; -- past non-expression terminating token
1171 if Token
= Tok_Then
then
1172 Restore_Scan_State
(Scan_State
);
1178 end Else_Should_Be_Elsif
;
1180 -- Start of processing for P_If_Statement
1183 If_Node
:= New_Node
(N_If_Statement
, Token_Ptr
);
1186 Scopes
(Scope
.Last
).Etyp
:= E_If
;
1187 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1188 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1189 Scopes
(Scope
.Last
).Labl
:= Error
;
1190 Scopes
(Scope
.Last
).Node
:= If_Node
;
1192 if Token
= Tok_If
then
1195 Set_Condition
(If_Node
, P_Condition
);
1197 -- Deal with misuse of IF expression => used instead
1198 -- of WHEN expression =>
1200 if Token
= Tok_Arrow
then
1201 Error_Msg_SC
-- CODEFIX
1203 Scan
; -- past the arrow
1204 Pop_Scope_Stack
; -- remove unneeded entry
1211 Error_Msg_SC
("no IF for this THEN");
1212 Set_Condition
(If_Node
, Error
);
1218 (If_Node
, P_Sequence_Of_Statements
(SS_Eftm_Eltm_Sreq
));
1220 -- This loop scans out else and elsif parts
1223 if Token
= Tok_Elsif
then
1226 if Present
(Else_Statements
(If_Node
)) then
1227 Error_Msg_SP
("ELSIF cannot appear after ELSE");
1233 elsif Token
= Tok_Else
then
1237 if Else_Should_Be_Elsif
then
1238 Error_Msg_SP
-- CODEFIX
1239 ("ELSE should be ELSIF");
1243 -- Here we have an else that really is an else
1245 if Present
(Else_Statements
(If_Node
)) then
1246 Error_Msg_SP
("only one ELSE part allowed");
1248 (P_Sequence_Of_Statements
(SS_Eftm_Eltm_Sreq
),
1249 Else_Statements
(If_Node
));
1252 (If_Node
, P_Sequence_Of_Statements
(SS_Eftm_Eltm_Sreq
));
1256 -- If anything other than ELSE or ELSIF, exit the loop. The token
1257 -- had better be END (and in fact it had better be END IF), but
1258 -- we will let End_Statements take care of checking that.
1270 --------------------
1272 --------------------
1274 -- CONDITION ::= boolean_EXPRESSION
1276 function P_Condition
return Node_Id
is
1278 return P_Condition
(P_Expression_No_Right_Paren
);
1281 function P_Condition
(Cond
: Node_Id
) return Node_Id
is
1283 -- It is never possible for := to follow a condition, so if we get
1284 -- a := we assume it is a mistyped equality. Note that we do not try
1285 -- to reconstruct the tree correctly in this case, but we do at least
1286 -- give an accurate error message.
1288 if Token
= Tok_Colon_Equal
then
1289 while Token
= Tok_Colon_Equal
loop
1290 Error_Msg_SC
-- CODEFIX
1291 (""":="" should be ""=""");
1292 Scan
; -- past junk :=
1293 Discard_Junk_Node
(P_Expression_No_Right_Paren
);
1298 -- Otherwise check for redundant parentheses but do not emit messages
1299 -- about expressions that require parentheses (e.g. conditional,
1300 -- quantified or declaration expressions).
1305 Paren_Count
(Cond
) >
1306 (if Nkind
(Cond
) in N_Case_Expression
1307 | N_Expression_With_Actions
1309 | N_Quantified_Expression
1313 Style
.Check_Xtra_Parens
(First_Sloc
(Cond
));
1316 -- And return the result
1322 -------------------------
1323 -- 5.4 Case Statement --
1324 -------------------------
1326 -- CASE_STATEMENT ::=
1327 -- case EXPRESSION is
1328 -- CASE_STATEMENT_ALTERNATIVE
1329 -- {CASE_STATEMENT_ALTERNATIVE}
1332 -- The caller has checked that the first token is CASE
1334 -- Can raise Error_Resync
1336 function P_Case_Statement
return Node_Id
is
1337 Case_Node
: Node_Id
;
1338 Alternatives_List
: List_Id
;
1339 First_When_Loc
: Source_Ptr
;
1342 Case_Node
:= New_Node
(N_Case_Statement
, Token_Ptr
);
1345 Scopes
(Scope
.Last
).Etyp
:= E_Case
;
1346 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1347 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1348 Scopes
(Scope
.Last
).Labl
:= Error
;
1349 Scopes
(Scope
.Last
).Node
:= Case_Node
;
1352 Set_Expression
(Case_Node
, P_Expression_No_Right_Paren
);
1355 -- Prepare to parse case statement alternatives
1357 Alternatives_List
:= New_List
;
1358 P_Pragmas_Opt
(Alternatives_List
);
1359 First_When_Loc
:= Token_Ptr
;
1361 -- Loop through case statement alternatives
1364 -- If we have a WHEN or OTHERS, then that's fine keep going. Note
1365 -- that it is a semantic check to ensure the proper use of OTHERS
1367 if Token
= Tok_When
or else Token
= Tok_Others
then
1368 Append
(P_Case_Statement_Alternative
, Alternatives_List
);
1370 -- If we have an END, then probably we are at the end of the case
1371 -- but we only exit if Check_End thinks the END was reasonable.
1373 elsif Token
= Tok_End
then
1374 exit when Check_End
;
1376 -- Here if token is other than WHEN, OTHERS or END. We definitely
1377 -- have an error, but the question is whether or not to get out of
1378 -- the case statement. We don't want to get out early, or we will
1379 -- get a slew of junk error messages for subsequent when tokens.
1381 -- If the token is not at the start of the line, or if it is indented
1382 -- with respect to the current case statement, then the best guess is
1383 -- that we are still supposed to be inside the case statement. We
1384 -- complain about the missing WHEN, and discard the junk statements.
1386 elsif not Token_Is_At_Start_Of_Line
1387 or else Start_Column
> Scopes
(Scope
.Last
).Ecol
1389 Error_Msg_BC
("WHEN (case statement alternative) expected");
1391 -- Here is a possibility for infinite looping if we don't make
1392 -- progress. So try to process statements, otherwise exit
1395 Error_Ptr
: constant Source_Ptr
:= Scan_Ptr
;
1397 Discard_Junk_List
(P_Sequence_Of_Statements
(SS_Whtm
));
1398 exit when Scan_Ptr
= Error_Ptr
and then Check_End
;
1401 -- Here we have a junk token at the start of the line and it is
1402 -- not indented. If Check_End thinks there is a missing END, then
1403 -- we will get out of the case, otherwise we keep going.
1406 exit when Check_End
;
1410 -- Make sure we have at least one alternative
1412 if No
(First_Non_Pragma
(Alternatives_List
)) then
1414 ("WHEN expected, must have at least one alternative in case",
1419 Set_Alternatives
(Case_Node
, Alternatives_List
);
1422 end P_Case_Statement
;
1424 -------------------------------------
1425 -- 5.4 Case Statement Alternative --
1426 -------------------------------------
1428 -- CASE_STATEMENT_ALTERNATIVE ::=
1429 -- when DISCRETE_CHOICE_LIST =>
1430 -- SEQUENCE_OF_STATEMENTS
1432 -- The caller has checked that the initial token is WHEN or OTHERS
1433 -- Error recovery: can raise Error_Resync
1435 function P_Case_Statement_Alternative
return Node_Id
is
1436 Case_Alt_Node
: Node_Id
;
1440 Style
.Check_Indentation
;
1443 Case_Alt_Node
:= New_Node
(N_Case_Statement_Alternative
, Token_Ptr
);
1444 T_When
; -- past WHEN (or give error in OTHERS case)
1445 Set_Discrete_Choices
(Case_Alt_Node
, P_Discrete_Choice_List
);
1447 Set_Statements
(Case_Alt_Node
, P_Sequence_Of_Statements
(SS_Sreq_Whtm
));
1448 return Case_Alt_Node
;
1449 end P_Case_Statement_Alternative
;
1451 -------------------------
1452 -- 5.5 Loop Statement --
1453 -------------------------
1455 -- LOOP_STATEMENT ::=
1456 -- [LOOP_STATEMENT_IDENTIFIER:]
1457 -- [ITERATION_SCHEME] loop
1458 -- SEQUENCE_OF_STATEMENTS
1459 -- end loop [loop_IDENTIFIER];
1461 -- ITERATION_SCHEME ::=
1463 -- | for LOOP_PARAMETER_SPECIFICATION
1465 -- The parsing of loop statements is handled by one of three functions
1466 -- P_Loop_Statement, P_For_Statement or P_While_Statement depending
1467 -- on the initial keyword in the construct (excluding the identifier)
1471 -- This function parses the case where no iteration scheme is present
1473 -- The caller has checked that the initial token is LOOP. The parameter
1474 -- is the node identifiers for the loop label if any (or is set to Empty
1475 -- if there is no loop label).
1477 -- Error recovery : cannot raise Error_Resync
1479 function P_Loop_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
is
1480 Loop_Node
: Node_Id
;
1481 Created_Name
: Node_Id
;
1485 Scopes
(Scope
.Last
).Labl
:= Loop_Name
;
1486 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1487 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1488 Scopes
(Scope
.Last
).Etyp
:= E_Loop
;
1490 Loop_Node
:= New_Node
(N_Loop_Statement
, Token_Ptr
);
1493 if No
(Loop_Name
) then
1495 Make_Identifier
(Sloc
(Loop_Node
), Set_Loop_Block_Name
('L'));
1496 Set_Comes_From_Source
(Created_Name
, False);
1497 Set_Has_Created_Identifier
(Loop_Node
, True);
1498 Set_Identifier
(Loop_Node
, Created_Name
);
1499 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1501 Set_Identifier
(Loop_Node
, Loop_Name
);
1504 Append_Elmt
(Loop_Node
, Label_List
);
1505 Set_Statements
(Loop_Node
, P_Sequence_Of_Statements
(SS_Sreq
));
1506 End_Statements
(Loop_Node
);
1508 end P_Loop_Statement
;
1512 -- This function parses a loop statement with a FOR iteration scheme
1514 -- The caller has checked that the initial token is FOR. The parameter
1515 -- is the node identifier for the block label if any (or is set to Empty
1516 -- if there is no block label).
1518 -- Note: the caller fills in the Identifier field if a label was present
1520 -- Error recovery: can raise Error_Resync
1522 function P_For_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
is
1523 Loop_Node
: Node_Id
;
1524 Iter_Scheme_Node
: Node_Id
;
1525 Loop_For_Flag
: Boolean;
1526 Created_Name
: Node_Id
;
1531 Scopes
(Scope
.Last
).Labl
:= Loop_Name
;
1532 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1533 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1534 Scopes
(Scope
.Last
).Etyp
:= E_Loop
;
1536 Loop_For_Flag
:= (Prev_Token
= Tok_Loop
);
1538 Iter_Scheme_Node
:= New_Node
(N_Iteration_Scheme
, Token_Ptr
);
1539 Spec
:= P_Loop_Parameter_Specification
;
1541 if Nkind
(Spec
) = N_Loop_Parameter_Specification
then
1542 Set_Loop_Parameter_Specification
(Iter_Scheme_Node
, Spec
);
1544 Set_Iterator_Specification
(Iter_Scheme_Node
, Spec
);
1547 -- The following is a special test so that a miswritten for loop such
1548 -- as "loop for I in 1..10;" is handled nicely, without making an extra
1549 -- entry in the scope stack. We don't bother to actually fix up the
1550 -- tree in this case since it's not worth the effort. Instead we just
1551 -- eat up the loop junk, leaving the entry for what now looks like an
1552 -- unmodified loop intact.
1554 if Loop_For_Flag
and then Token
= Tok_Semicolon
then
1555 Error_Msg_SC
("LOOP belongs here, not before FOR");
1562 Loop_Node
:= New_Node
(N_Loop_Statement
, Token_Ptr
);
1564 if No
(Loop_Name
) then
1566 Make_Identifier
(Sloc
(Loop_Node
), Set_Loop_Block_Name
('L'));
1567 Set_Comes_From_Source
(Created_Name
, False);
1568 Set_Has_Created_Identifier
(Loop_Node
, True);
1569 Set_Identifier
(Loop_Node
, Created_Name
);
1570 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1572 Set_Identifier
(Loop_Node
, Loop_Name
);
1576 Set_Statements
(Loop_Node
, P_Sequence_Of_Statements
(SS_Sreq
));
1577 End_Statements
(Loop_Node
);
1578 Set_Iteration_Scheme
(Loop_Node
, Iter_Scheme_Node
);
1579 Append_Elmt
(Loop_Node
, Label_List
);
1582 end P_For_Statement
;
1584 -- P_While_Statement
1586 -- This procedure scans a loop statement with a WHILE iteration scheme
1588 -- The caller has checked that the initial token is WHILE. The parameter
1589 -- is the node identifier for the block label if any (or is set to Empty
1590 -- if there is no block label).
1592 -- Error recovery: cannot raise Error_Resync
1594 function P_While_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
is
1595 Loop_Node
: Node_Id
;
1596 Iter_Scheme_Node
: Node_Id
;
1597 Loop_While_Flag
: Boolean;
1598 Created_Name
: Node_Id
;
1602 Scopes
(Scope
.Last
).Labl
:= Loop_Name
;
1603 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1604 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1605 Scopes
(Scope
.Last
).Etyp
:= E_Loop
;
1607 Loop_While_Flag
:= (Prev_Token
= Tok_Loop
);
1608 Iter_Scheme_Node
:= New_Node
(N_Iteration_Scheme
, Token_Ptr
);
1610 Set_Condition
(Iter_Scheme_Node
, P_Condition
);
1612 -- The following is a special test so that a miswritten for loop such
1613 -- as "loop while I > 10;" is handled nicely, without making an extra
1614 -- entry in the scope stack. We don't bother to actually fix up the
1615 -- tree in this case since it's not worth the effort. Instead we just
1616 -- eat up the loop junk, leaving the entry for what now looks like an
1617 -- unmodified loop intact.
1619 if Loop_While_Flag
and then Token
= Tok_Semicolon
then
1620 Error_Msg_SC
("LOOP belongs here, not before WHILE");
1627 Loop_Node
:= New_Node
(N_Loop_Statement
, Token_Ptr
);
1630 if No
(Loop_Name
) then
1632 Make_Identifier
(Sloc
(Loop_Node
), Set_Loop_Block_Name
('L'));
1633 Set_Comes_From_Source
(Created_Name
, False);
1634 Set_Has_Created_Identifier
(Loop_Node
, True);
1635 Set_Identifier
(Loop_Node
, Created_Name
);
1636 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1638 Set_Identifier
(Loop_Node
, Loop_Name
);
1641 Set_Statements
(Loop_Node
, P_Sequence_Of_Statements
(SS_Sreq
));
1642 End_Statements
(Loop_Node
);
1643 Set_Iteration_Scheme
(Loop_Node
, Iter_Scheme_Node
);
1644 Append_Elmt
(Loop_Node
, Label_List
);
1647 end P_While_Statement
;
1649 ---------------------------------------
1650 -- 5.5 Loop Parameter Specification --
1651 ---------------------------------------
1653 -- LOOP_PARAMETER_SPECIFICATION ::=
1654 -- DEFINING_IDENTIFIER in [reverse] DISCRETE_SUBTYPE_DEFINITION
1655 -- [Iterator_Filter]
1657 -- Error recovery: cannot raise Error_Resync
1659 function P_Loop_Parameter_Specification
return Node_Id
is
1660 Loop_Param_Specification_Node
: Node_Id
;
1663 Scan_State
: Saved_Scan_State
;
1667 Save_Scan_State
(Scan_State
);
1668 ID_Node
:= P_Defining_Identifier
(C_In
);
1670 -- If the next token is OF, it indicates an Ada 2012 iterator. If the
1671 -- next token is a colon, this is also an Ada 2012 iterator, including
1672 -- a subtype indication for the loop parameter. Otherwise we parse the
1673 -- construct as a loop parameter specification. Note that the form
1674 -- "for A in B" is ambiguous, and must be resolved semantically: if B
1675 -- is a discrete subtype this is a loop specification, but if it is an
1676 -- expression it is an iterator specification. Ambiguity is resolved
1677 -- during analysis of the loop parameter specification.
1679 if Token
= Tok_Of
or else Token
= Tok_Colon
then
1680 Error_Msg_Ada_2012_Feature
("iterator", Token_Ptr
);
1681 return P_Iterator_Specification
(ID_Node
);
1684 -- The span of the Loop_Parameter_Specification starts at the
1685 -- defining identifier.
1687 Loop_Param_Specification_Node
:=
1688 New_Node
(N_Loop_Parameter_Specification
, Sloc
(ID_Node
));
1689 Set_Defining_Identifier
(Loop_Param_Specification_Node
, ID_Node
);
1691 if Token
= Tok_Left_Paren
then
1692 Error_Msg_SC
("subscripted loop parameter not allowed");
1693 Restore_Scan_State
(Scan_State
);
1694 Discard_Junk_Node
(P_Name
);
1696 elsif Token
= Tok_Dot
then
1697 Error_Msg_SC
("selected loop parameter not allowed");
1698 Restore_Scan_State
(Scan_State
);
1699 Discard_Junk_Node
(P_Name
);
1704 if Token
= Tok_Reverse
then
1705 Scan
; -- past REVERSE
1706 Set_Reverse_Present
(Loop_Param_Specification_Node
, True);
1709 Set_Discrete_Subtype_Definition
1710 (Loop_Param_Specification_Node
, P_Discrete_Subtype_Definition
);
1712 if Token
= Tok_When
then
1713 Error_Msg_Ada_2022_Feature
("iterator filter", Token_Ptr
);
1717 (Loop_Param_Specification_Node
, P_Condition
);
1720 return Loop_Param_Specification_Node
;
1723 when Error_Resync
=>
1725 end P_Loop_Parameter_Specification
;
1727 ----------------------------------
1728 -- 5.5.1 Iterator_Specification --
1729 ----------------------------------
1731 function P_Iterator_Specification
(Def_Id
: Node_Id
) return Node_Id
is
1735 Node1
:= New_Node
(N_Iterator_Specification
, Sloc
(Def_Id
));
1736 Set_Defining_Identifier
(Node1
, Def_Id
);
1738 if Token
= Tok_Colon
then
1741 if Token
= Tok_Access
then
1742 Error_Msg_Ada_2022_Feature
1743 ("access definition in loop parameter", Token_Ptr
);
1744 Set_Subtype_Indication
(Node1
, P_Access_Definition
(False));
1747 Set_Subtype_Indication
(Node1
, P_Subtype_Indication
);
1751 if Token
= Tok_Of
then
1752 Set_Of_Present
(Node1
);
1755 elsif Token
= Tok_In
then
1758 elsif Prev_Token
= Tok_In
1759 and then Present
(Subtype_Indication
(Node1
))
1761 -- Simplest recovery is to transform it into an element iterator.
1762 -- Error message on 'in" has already been emitted when parsing the
1763 -- optional constraint.
1765 Set_Of_Present
(Node1
);
1767 ("subtype indication is only legal on an element iterator",
1768 Subtype_Indication
(Node1
));
1774 if Token
= Tok_Reverse
then
1775 Scan
; -- past REVERSE
1776 Set_Reverse_Present
(Node1
, True);
1779 Set_Name
(Node1
, P_Name
);
1781 if Token
= Tok_When
then
1782 Error_Msg_Ada_2022_Feature
("iterator filter", Token_Ptr
);
1786 (Node1
, P_Condition
);
1790 end P_Iterator_Specification
;
1792 --------------------------
1793 -- 5.6 Block Statement --
1794 --------------------------
1796 -- BLOCK_STATEMENT ::=
1797 -- [block_STATEMENT_IDENTIFIER:]
1799 -- DECLARATIVE_PART]
1801 -- HANDLED_SEQUENCE_OF_STATEMENTS
1802 -- end [block_IDENTIFIER];
1804 -- The parsing of block statements is handled by one of the two functions
1805 -- P_Declare_Statement or P_Begin_Statement depending on whether or not
1806 -- a declare section is present
1808 -- P_Declare_Statement
1810 -- This function parses a block statement with DECLARE present
1812 -- The caller has checked that the initial token is DECLARE
1814 -- Error recovery: cannot raise Error_Resync
1816 function P_Declare_Statement
1817 (Block_Name
: Node_Id
:= Empty
)
1820 Block_Node
: Node_Id
;
1821 Created_Name
: Node_Id
;
1824 Block_Node
:= New_Node
(N_Block_Statement
, Token_Ptr
);
1827 Scopes
(Scope
.Last
).Etyp
:= E_Name
;
1828 Scopes
(Scope
.Last
).Lreq
:= Present
(Block_Name
);
1829 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1830 Scopes
(Scope
.Last
).Labl
:= Block_Name
;
1831 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1833 Scan
; -- past DECLARE
1835 if No
(Block_Name
) then
1837 Make_Identifier
(Sloc
(Block_Node
), Set_Loop_Block_Name
('B'));
1838 Set_Comes_From_Source
(Created_Name
, False);
1839 Set_Has_Created_Identifier
(Block_Node
, True);
1840 Set_Identifier
(Block_Node
, Created_Name
);
1841 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1843 Set_Identifier
(Block_Node
, Block_Name
);
1846 Append_Elmt
(Block_Node
, Label_List
);
1847 Parse_Decls_Begin_End
(Block_Node
);
1849 end P_Declare_Statement
;
1851 -- P_Begin_Statement
1853 -- This function parses a block statement with no DECLARE present
1855 -- The caller has checked that the initial token is BEGIN
1857 -- Error recovery: cannot raise Error_Resync
1859 function P_Begin_Statement
1860 (Block_Name
: Node_Id
:= Empty
)
1863 Block_Node
: Node_Id
;
1864 Created_Name
: Node_Id
;
1867 Block_Node
:= New_Node
(N_Block_Statement
, Token_Ptr
);
1870 Scopes
(Scope
.Last
).Etyp
:= E_Name
;
1871 Scopes
(Scope
.Last
).Lreq
:= Present
(Block_Name
);
1872 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1873 Scopes
(Scope
.Last
).Labl
:= Block_Name
;
1874 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1876 if No
(Block_Name
) then
1878 Make_Identifier
(Sloc
(Block_Node
), Set_Loop_Block_Name
('B'));
1879 Set_Comes_From_Source
(Created_Name
, False);
1880 Set_Has_Created_Identifier
(Block_Node
, True);
1881 Set_Identifier
(Block_Node
, Created_Name
);
1882 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1884 Set_Identifier
(Block_Node
, Block_Name
);
1887 Append_Elmt
(Block_Node
, Label_List
);
1889 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1890 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1892 Set_Handled_Statement_Sequence
1893 (Block_Node
, P_Handled_Sequence_Of_Statements
);
1894 End_Statements
(Handled_Statement_Sequence
(Block_Node
));
1896 end P_Begin_Statement
;
1898 -------------------------
1899 -- 5.7 Exit Statement --
1900 -------------------------
1902 -- EXIT_STATEMENT ::=
1903 -- exit [loop_NAME] [when CONDITION];
1905 -- The caller has checked that the initial token is EXIT
1907 -- Error recovery: can raise Error_Resync
1909 function P_Exit_Statement
return Node_Id
is
1910 Exit_Node
: Node_Id
;
1912 -- Start of processing for P_Exit_Statement
1915 Exit_Node
:= New_Node
(N_Exit_Statement
, Token_Ptr
);
1918 if Token
= Tok_Identifier
then
1919 Set_Name
(Exit_Node
, P_Qualified_Simple_Name
);
1921 elsif Style_Check
then
1922 -- This EXIT has no name, so check that
1923 -- the innermost loop is unnamed too.
1925 Check_No_Exit_Name
:
1926 for J
in reverse 1 .. Scope
.Last
loop
1927 if Scopes
(J
).Etyp
= E_Loop
then
1928 if Present
(Scopes
(J
).Labl
)
1929 and then Comes_From_Source
(Scopes
(J
).Labl
)
1931 -- Innermost loop in fact had a name, style check fails
1933 Style
.No_Exit_Name
(Scopes
(J
).Labl
);
1936 exit Check_No_Exit_Name
;
1938 end loop Check_No_Exit_Name
;
1941 if Token
= Tok_When
and then not Missing_Semicolon_On_When
then
1943 Set_Condition
(Exit_Node
, P_Condition
);
1945 -- Allow IF instead of WHEN, giving error message
1947 elsif Token
= Tok_If
then
1949 Scan
; -- past IF used in place of WHEN
1950 Set_Condition
(Exit_Node
, P_Expression_No_Right_Paren
);
1955 end P_Exit_Statement
;
1957 -------------------------
1958 -- 5.8 Goto Statement --
1959 -------------------------
1961 -- GOTO_STATEMENT ::= goto label_NAME;
1963 -- The caller has checked that the initial token is GOTO (or TO in the
1964 -- error case where GO and TO were incorrectly separated).
1966 -- Error recovery: can raise Error_Resync
1968 function P_Goto_Statement
return Node_Id
is
1969 Goto_Node
: Node_Id
;
1972 Goto_Node
:= New_Node
(N_Goto_Statement
, Token_Ptr
);
1973 Scan
; -- past GOTO (or TO)
1974 Set_Name
(Goto_Node
, P_Qualified_Simple_Name_Resync
);
1975 Append_Elmt
(Goto_Node
, Goto_List
);
1977 if Token
= Tok_When
then
1978 Error_Msg_GNAT_Extension
("goto when statement", Token_Ptr
);
1981 Mutate_Nkind
(Goto_Node
, N_Goto_When_Statement
);
1982 Set_Condition
(Goto_Node
, P_Expression_No_Right_Paren
);
1987 end P_Goto_Statement
;
1989 ---------------------------
1990 -- Parse_Decls_Begin_End --
1991 ---------------------------
1993 -- This function parses the construct:
1997 -- HANDLED_SEQUENCE_OF_STATEMENTS
2000 -- The caller has built the scope stack entry, and created the node to
2001 -- whose Declarations and Handled_Statement_Sequence fields are to be
2002 -- set. On return these fields are filled in (except in the case of a
2003 -- task body, where the handled statement sequence is optional, and may
2004 -- thus be Empty), and the scan is positioned past the End sequence.
2006 -- If the BEGIN is missing, then the parent node is used to help construct
2007 -- an appropriate missing BEGIN message. Possibilities for the parent are:
2009 -- N_Block_Statement declare block
2010 -- N_Entry_Body entry body
2011 -- N_Package_Body package body (begin part optional)
2012 -- N_Subprogram_Body procedure or function body
2013 -- N_Task_Body task body
2015 -- Note: in the case of a block statement, there is definitely a DECLARE
2016 -- present (because a Begin statement without a DECLARE is handled by the
2017 -- P_Begin_Statement procedure, which does not call Parse_Decls_Begin_End.
2019 -- Error recovery: cannot raise Error_Resync
2021 procedure Parse_Decls_Begin_End
(Parent
: Node_Id
) is
2022 Body_Decl
: Node_Id
;
2024 Parent_Nkind
: Node_Kind
;
2025 Spec_Node
: Node_Id
;
2028 procedure Missing_Begin
(Msg
: String);
2029 -- Called to post a missing begin message. In the normal case this is
2030 -- posted at the start of the current token. A special case arises when
2031 -- P_Declarative_Items has previously found a missing begin, in which
2032 -- case we replace the original error message.
2034 procedure Set_Null_HSS
(Parent
: Node_Id
);
2035 -- Construct an empty handled statement sequence and install in Parent
2036 -- Leaves HSS set to reference the newly constructed statement sequence.
2042 procedure Missing_Begin
(Msg
: String) is
2044 if Missing_Begin_Msg
= No_Error_Msg
then
2047 Change_Error_Text
(Missing_Begin_Msg
, Msg
);
2049 -- Purge any messages issued after than, since a missing begin
2050 -- can cause a lot of havoc, and it is better not to dump these
2051 -- cascaded messages on the user.
2053 Purge_Messages
(Get_Location
(Missing_Begin_Msg
), Prev_Token_Ptr
);
2061 procedure Set_Null_HSS
(Parent
: Node_Id
) is
2066 Make_Null_Statement
(Token_Ptr
);
2067 Set_Comes_From_Source
(Null_Stm
, False);
2070 Make_Handled_Sequence_Of_Statements
(Token_Ptr
,
2071 Statements
=> New_List
(Null_Stm
));
2072 Set_Comes_From_Source
(HSS
, False);
2074 Set_Handled_Statement_Sequence
(Parent
, HSS
);
2077 -- Start of processing for Parse_Decls_Begin_End
2080 Decls
:= P_Declarative_Part
;
2082 if Ada_Version
= Ada_83
then
2083 Check_Later_Vs_Basic_Declarations
(Decls
, During_Parsing
=> True);
2086 -- Here is where we deal with the case of IS used instead of semicolon.
2087 -- Specifically, if the last declaration in the declarative part is a
2088 -- subprogram body still marked as having a bad IS, then this is where
2089 -- we decide that the IS should really have been a semicolon and that
2090 -- the body should have been a declaration. Note that if the bad IS
2091 -- had turned out to be OK (i.e. a decent begin/end was found for it),
2092 -- then the Bad_Is_Detected flag would have been reset by now.
2094 Body_Decl
:= Last
(Decls
);
2096 if Present
(Body_Decl
)
2097 and then Nkind
(Body_Decl
) = N_Subprogram_Body
2098 and then Bad_Is_Detected
(Body_Decl
)
2100 -- OK, we have the case of a bad IS, so we need to fix up the tree.
2101 -- What we have now is a subprogram body with attached declarations
2102 -- and a possible statement sequence.
2104 -- First step is to take the declarations that were part of the bogus
2105 -- subprogram body and append them to the outer declaration chain.
2106 -- In other words we append them past the body (which we will later
2107 -- convert into a declaration).
2109 Append_List
(Declarations
(Body_Decl
), Decls
);
2111 -- Now take the handled statement sequence of the bogus body and
2112 -- set it as the statement sequence for the outer construct. Note
2113 -- that it may be empty (we specially allowed a missing BEGIN for
2114 -- a subprogram body marked as having a bad IS -- see below).
2116 Set_Handled_Statement_Sequence
(Parent
,
2117 Handled_Statement_Sequence
(Body_Decl
));
2119 -- Next step is to convert the old body node to a declaration node
2121 Spec_Node
:= Specification
(Body_Decl
);
2122 Change_Node
(Body_Decl
, N_Subprogram_Declaration
);
2123 Set_Specification
(Body_Decl
, Spec_Node
);
2125 -- Final step is to put the declarations for the parent where
2126 -- they belong, and then fall through the IF to scan out the
2129 Set_Declarations
(Parent
, Decls
);
2131 -- This is the normal case (i.e. any case except the bad IS case)
2132 -- If we have a BEGIN, then scan out the sequence of statements, and
2133 -- also reset the expected column for the END to match the BEGIN.
2136 Set_Declarations
(Parent
, Decls
);
2138 if Token
= Tok_Begin
then
2140 Style
.Check_Indentation
;
2143 Error_Msg_Col
:= Scopes
(Scope
.Last
).Ecol
;
2146 and then Token_Is_At_Start_Of_Line
2147 and then Start_Column
/= Error_Msg_Col
2149 Error_Msg_SC
("(style) BEGIN in wrong column, should be@");
2152 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
2155 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
2157 Set_Handled_Statement_Sequence
(Parent
,
2158 P_Handled_Sequence_Of_Statements
);
2163 Parent_Nkind
:= Nkind
(Parent
);
2165 -- A special check for the missing IS case. If we have a
2166 -- subprogram body that was marked as having a suspicious
2167 -- IS, and the current token is END, then we simply confirm
2168 -- the suspicion, and do not require a BEGIN to be present
2170 if Parent_Nkind
= N_Subprogram_Body
2171 and then Token
= Tok_End
2172 and then Scopes
(Scope
.Last
).Etyp
= E_Suspicious_Is
2174 Scopes
(Scope
.Last
).Etyp
:= E_Bad_Is
;
2176 -- Otherwise BEGIN is not required for a package body, so we
2177 -- don't mind if it is missing, but we do construct a dummy
2178 -- one (so that we have somewhere to set End_Label).
2180 -- However if we have something other than a BEGIN which
2181 -- looks like it might be statements, then we signal a missing
2182 -- BEGIN for these cases as well. We define "something which
2183 -- looks like it might be statements" as a token other than
2184 -- END, EOF, or a token which starts declarations.
2186 elsif Parent_Nkind
= N_Package_Body
2187 and then (Token
= Tok_End
2188 or else Token
= Tok_EOF
2189 or else Token
in Token_Class_Declk
)
2191 Set_Null_HSS
(Parent
);
2193 -- These are cases in which a BEGIN is required and not present
2196 Set_Null_HSS
(Parent
);
2198 -- Prepare to issue error message
2200 Error_Msg_Sloc
:= Scopes
(Scope
.Last
).Sloc
;
2201 Error_Msg_Node_1
:= Scopes
(Scope
.Last
).Labl
;
2203 -- Now issue appropriate message
2205 if Parent_Nkind
= N_Block_Statement
then
2206 Missing_Begin
("missing BEGIN for DECLARE#!");
2208 elsif Parent_Nkind
= N_Entry_Body
then
2209 Missing_Begin
("missing BEGIN for ENTRY#!");
2211 elsif Parent_Nkind
= N_Subprogram_Body
then
2212 if Nkind
(Specification
(Parent
))
2213 = N_Function_Specification
2215 Missing_Begin
("missing BEGIN for function&#!");
2217 Missing_Begin
("missing BEGIN for procedure&#!");
2220 -- The case for package body arises only when
2221 -- we have possible statement junk present.
2223 elsif Parent_Nkind
= N_Package_Body
then
2224 Missing_Begin
("missing BEGIN for package body&#!");
2227 pragma Assert
(Parent_Nkind
= N_Task_Body
);
2228 Missing_Begin
("missing BEGIN for task body&#!");
2231 -- Here we pick up the statements after the BEGIN that
2232 -- should have been present but was not. We don't insist
2233 -- on statements being present if P_Declarative_Part had
2234 -- already found a missing BEGIN, since it might have
2235 -- swallowed a lone statement into the declarative part.
2237 if Missing_Begin_Msg
/= No_Error_Msg
2238 and then Token
= Tok_End
2242 Set_Handled_Statement_Sequence
(Parent
,
2243 P_Handled_Sequence_Of_Statements
);
2249 -- Here with declarations and handled statement sequence scanned
2251 if Present
(Handled_Statement_Sequence
(Parent
)) then
2252 End_Statements
(Handled_Statement_Sequence
(Parent
));
2257 -- We know that End_Statements removed an entry from the scope stack
2258 -- (because it is required to do so under all circumstances). We can
2259 -- therefore reference the entry it removed one past the stack top.
2260 -- What we are interested in is whether it was a case of a bad IS.
2261 -- We can't call Scopes here.
2263 if Scope
.Table
(Scope
.Last
+ 1).Etyp
= E_Bad_Is
then
2264 Error_Msg
-- CODEFIX
2265 ("|IS should be "";""", Scope
.Table
(Scope
.Last
+ 1).S_Is
);
2266 Set_Bad_Is_Detected
(Parent
, True);
2269 end Parse_Decls_Begin_End
;
2271 -------------------------
2272 -- Set_Loop_Block_Name --
2273 -------------------------
2275 function Set_Loop_Block_Name
(L
: Character) return Name_Id
is
2277 Name_Buffer
(1) := L
;
2278 Name_Buffer
(2) := '_';
2280 Loop_Block_Count
:= Loop_Block_Count
+ 1;
2281 Add_Nat_To_Name_Buffer
(Loop_Block_Count
);
2283 end Set_Loop_Block_Name
;
2289 procedure Then_Scan
is
2293 while Token
= Tok_Then
loop
2294 Error_Msg_SC
-- CODEFIX
2299 if Token
= Tok_And
or else Token
= Tok_Or
then
2300 Error_Msg_SC
("unexpected logical operator");
2301 Scan
; -- past logical operator
2303 if (Prev_Token
= Tok_And
and then Token
= Tok_Then
)
2305 (Prev_Token
= Tok_Or
and then Token
= Tok_Else
)
2310 Discard_Junk_Node
(P_Expression
);
2313 if Token
= Tok_Then
then