1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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
)
238 -- This Ada 2012 construct not allowed in a compiler unit
240 Check_Compiler_Unit
("null statement list", Token_Ptr
);
243 Null_Stm
: constant Node_Id
:=
244 Make_Null_Statement
(Token_Ptr
);
246 Set_Comes_From_Source
(Null_Stm
, False);
247 Append_To
(Statement_List
, Null_Stm
);
250 -- If not Ada 2012, or not special case above, give error message
253 Error_Msg_BC
-- CODEFIX
254 ("statement expected");
257 end Test_Statement_Required
;
259 -- Start of processing for P_Sequence_Of_Statements
262 Statement_List
:= New_List
;
263 Statement_Required
:= SS_Flags
.Sreq
;
264 Statement_Seen
:= False;
267 Ignore
(Tok_Semicolon
);
271 Style
.Check_Indentation
;
274 -- Deal with reserved identifier (in assignment or call)
276 if Is_Reserved_Identifier
then
277 Save_Scan_State
(Scan_State
); -- at possible bad identifier
278 Scan
; -- and scan past it
280 -- We have an reserved word which is spelled in identifier
281 -- style, so the question is whether it really is intended
282 -- to be an identifier.
285 -- If followed by a semicolon, then it is an identifier,
286 -- with the exception of the cases tested for below.
288 (Token
= Tok_Semicolon
289 and then Prev_Token
/= Tok_Return
290 and then Prev_Token
/= Tok_Null
291 and then Prev_Token
/= Tok_Raise
292 and then Prev_Token
/= Tok_End
293 and then Prev_Token
/= Tok_Exit
)
295 -- If followed by colon, colon-equal, or dot, then we
296 -- definitely have an identifier (could not be reserved)
298 or else Token
= Tok_Colon
299 or else Token
= Tok_Colon_Equal
300 or else Token
= Tok_Dot
302 -- Left paren means we have an identifier except for those
303 -- reserved words that can legitimately be followed by a
307 (Token
= Tok_Left_Paren
308 and then Prev_Token
/= Tok_Case
309 and then Prev_Token
/= Tok_Delay
310 and then Prev_Token
/= Tok_If
311 and then Prev_Token
/= Tok_Elsif
312 and then Prev_Token
/= Tok_Return
313 and then Prev_Token
/= Tok_When
314 and then Prev_Token
/= Tok_While
315 and then Prev_Token
/= Tok_Separate
)
317 -- Here we have an apparent reserved identifier and the
318 -- token past it is appropriate to this usage (and would
319 -- be a definite error if this is not an identifier). What
320 -- we do is to use P_Identifier to fix up the identifier,
321 -- and then fall into the normal processing.
323 Restore_Scan_State
(Scan_State
); -- back to the ID
324 Scan_Reserved_Identifier
(Force_Msg
=> False);
326 -- Not a reserved identifier after all (or at least we can't
327 -- be sure that it is), so reset the scan and continue.
330 Restore_Scan_State
(Scan_State
); -- back to the reserved word
334 -- Now look to see what kind of statement we have
338 -- Case of end or EOF
343 -- These tokens always terminate the statement sequence
345 Test_Statement_Required
;
352 -- Terminate if Eftm set or if the ELSIF is to the left
353 -- of the expected column of the end for this sequence
356 or else Start_Column
< Scopes
(Scope
.Last
).Ecol
358 Test_Statement_Required
;
361 -- Otherwise complain and skip past ELSIF Condition then
364 Error_Msg_SC
("ELSIF not allowed here");
366 Discard_Junk_Node
(P_Expression_No_Right_Paren
);
368 Statement_Required
:= False;
375 -- Terminate if Eltm set or if the else is to the left
376 -- of the expected column of the end for this sequence
379 or else Start_Column
< Scopes
(Scope
.Last
).Ecol
381 Test_Statement_Required
;
384 -- Otherwise complain and skip past else
387 Error_Msg_SC
("ELSE not allowed here");
389 Statement_Required
:= False;
394 when Tok_Exception
=>
395 Test_Statement_Required
;
397 -- If Extm not set and the exception is not to the left of
398 -- the expected column of the end for this sequence, then we
399 -- assume it belongs to the current sequence, even though it
402 if not SS_Flags
.Extm
and then
403 Start_Column
>= Scopes
(Scope
.Last
).Ecol
406 Error_Msg_SC
("exception handler not permitted here");
407 Scan
; -- past EXCEPTION
408 Discard_Junk_List
(Parse_Exception_Handlers
);
411 -- Always return, in the case where we scanned out handlers
412 -- that we did not expect, Parse_Exception_Handlers returned
413 -- with Token being either end or EOF, so we are OK.
421 -- Terminate if Ortm set or if the or is to the left of the
422 -- expected column of the end for this sequence.
425 or else Start_Column
< Scopes
(Scope
.Last
).Ecol
427 Test_Statement_Required
;
430 -- Otherwise complain and skip past or
433 Error_Msg_SC
("OR not allowed here");
435 Statement_Required
:= False;
438 -- Case of THEN (deal also with THEN ABORT)
441 Save_Scan_State
(Scan_State
); -- at THEN
444 -- Terminate if THEN ABORT allowed (ATC case)
446 exit when SS_Flags
.Tatm
and then Token
= Tok_Abort
;
448 -- Otherwise we treat THEN as some kind of mess where we did
449 -- not see the associated IF, but we pick up assuming it had
452 Restore_Scan_State
(Scan_State
); -- to THEN
453 Append_To
(Statement_List
, P_If_Statement
);
454 Statement_Required
:= False;
456 -- Case of WHEN (error because we are not in a case)
461 -- Terminate if Whtm set or if the WHEN is to the left of
462 -- the expected column of the end for this sequence.
465 or else Start_Column
< Scopes
(Scope
.Last
).Ecol
467 Test_Statement_Required
;
470 -- Otherwise complain and skip when Choice {| Choice} =>
473 Error_Msg_SC
("WHEN not allowed here");
475 Discard_Junk_List
(P_Discrete_Choice_List
);
477 Statement_Required
:= False;
480 -- Cases of statements starting with an identifier
482 when Tok_Identifier
=>
485 -- Save scan pointers and line number in case block label
487 Id_Node
:= Token_Node
;
488 Block_Label
:= Token_Name
;
489 Save_Scan_State
(Scan_State_Label
); -- at possible label
492 -- Check for common case of assignment, since it occurs
493 -- frequently, and we want to process it efficiently.
495 if Token
= Tok_Colon_Equal
then
496 Scan
; -- past the colon-equal
497 Append_To
(Statement_List
,
498 P_Assignment_Statement
(Id_Node
));
499 Statement_Required
:= False;
501 -- Check common case of procedure call, another case that
502 -- we want to speed up as much as possible.
504 elsif Token
= Tok_Semicolon
then
505 Change_Name_To_Procedure_Call_Statement
(Id_Node
);
506 Append_To
(Statement_List
, Id_Node
);
507 Scan
; -- past semicolon
508 Statement_Required
:= False;
510 -- Here is the special test for a suspicious label, more
511 -- accurately a suspicious name, which we think perhaps
512 -- should have been a label. If next token is one of
513 -- LOOP, FOR, WHILE, DECLARE, BEGIN, then make an entry
514 -- in the suspicious label table.
516 if Token
= Tok_Loop
or else
517 Token
= Tok_For
or else
518 Token
= Tok_While
or else
519 Token
= Tok_Declare
or else
522 Suspicious_Labels
.Append
523 ((Proc_Call
=> Id_Node
,
524 Semicolon_Loc
=> Prev_Token_Ptr
,
525 Start_Token
=> Token_Ptr
));
528 -- Check for case of "go to" in place of "goto"
530 elsif Token
= Tok_Identifier
531 and then Block_Label
= Name_Go
532 and then Token_Name
= Name_To
534 Error_Msg_SP
-- CODEFIX
535 ("goto is one word");
536 Append_To
(Statement_List
, P_Goto_Statement
);
537 Statement_Required
:= False;
539 -- Check common case of = used instead of :=, just so we
540 -- give a better error message for this special misuse.
542 elsif Token
= Tok_Equal
then
543 T_Colon_Equal
; -- give := expected message
544 Append_To
(Statement_List
,
545 P_Assignment_Statement
(Id_Node
));
546 Statement_Required
:= False;
548 -- Check case of loop label or block label
550 elsif Token
= Tok_Colon
551 or else (Token
in Token_Class_Labeled_Stmt
552 and then not Token_Is_At_Start_Of_Line
)
554 T_Colon
; -- past colon (if there, or msg for missing one)
556 -- Test for more than one label
559 exit when Token
/= Tok_Identifier
;
560 Save_Scan_State
(Scan_State
); -- at second Id
563 if Token
= Tok_Colon
then
565 ("only one label allowed on block or loop");
566 Scan
; -- past colon on extra label
568 -- Use the second label as the "real" label
570 Scan_State_Label
:= Scan_State
;
572 -- We will set Error_name as the Block_Label since
573 -- we really don't know which of the labels might
574 -- be used at the end of the loop or block.
576 Block_Label
:= Error_Name
;
578 -- If Id with no colon, then backup to point to the
579 -- Id and we will issue the message below when we try
580 -- to scan out the statement as some other form.
583 Restore_Scan_State
(Scan_State
); -- to second Id
588 -- Loop_Statement (labeled Loop_Statement)
590 if Token
= Tok_Loop
then
591 Append_To
(Statement_List
,
592 P_Loop_Statement
(Id_Node
));
594 -- While statement (labeled loop statement with WHILE)
596 elsif Token
= Tok_While
then
597 Append_To
(Statement_List
,
598 P_While_Statement
(Id_Node
));
600 -- Declare statement (labeled block statement with
603 elsif Token
= Tok_Declare
then
604 Append_To
(Statement_List
,
605 P_Declare_Statement
(Id_Node
));
607 -- Begin statement (labeled block statement with no
610 elsif Token
= Tok_Begin
then
611 Append_To
(Statement_List
,
612 P_Begin_Statement
(Id_Node
));
614 -- For statement (labeled loop statement with FOR)
616 elsif Token
= Tok_For
then
617 Append_To
(Statement_List
,
618 P_For_Statement
(Id_Node
));
620 -- Improper statement follows label. If we have an
621 -- expression token, then assume the colon was part
622 -- of a misplaced declaration.
624 elsif Token
not in Token_Class_Eterm
then
625 Restore_Scan_State
(Scan_State_Label
);
628 -- Otherwise complain we have inappropriate statement
632 ("loop or block statement must follow label");
635 Statement_Required
:= False;
637 -- Here we have an identifier followed by something
638 -- other than a colon, semicolon or assignment symbol.
639 -- The only valid possibility is a name extension symbol
641 elsif Token
in Token_Class_Namext
then
642 Restore_Scan_State
(Scan_State_Label
); -- to Id
645 -- Skip junk right parens in this context
647 Ignore
(Tok_Right_Paren
);
649 -- Check context following call
651 if Token
= Tok_Colon_Equal
then
652 Scan
; -- past colon equal
653 Append_To
(Statement_List
,
654 P_Assignment_Statement
(Name_Node
));
655 Statement_Required
:= False;
657 -- Check common case of = used instead of :=
659 elsif Token
= Tok_Equal
then
660 T_Colon_Equal
; -- give := expected message
661 Append_To
(Statement_List
,
662 P_Assignment_Statement
(Name_Node
));
663 Statement_Required
:= False;
665 -- Check apostrophe cases
667 elsif Token
= Tok_Apostrophe
then
668 Append_To
(Statement_List
,
669 P_Code_Statement
(Name_Node
));
670 Statement_Required
:= False;
672 -- The only other valid item after a name is ; which
673 -- means that the item we just scanned was a call.
675 elsif Token
= Tok_Semicolon
then
676 Change_Name_To_Procedure_Call_Statement
(Name_Node
);
677 Append_To
(Statement_List
, Name_Node
);
678 Scan
; -- past semicolon
679 Statement_Required
:= False;
681 -- A slash following an identifier or a selected
682 -- component in this situation is most likely a period
683 -- (see location of keys on keyboard).
685 elsif Token
= Tok_Slash
686 and then (Nkind
(Name_Node
) = N_Identifier
688 Nkind
(Name_Node
) = N_Selected_Component
)
690 Error_Msg_SC
-- CODEFIX
691 ("""/"" should be "".""");
692 Statement_Required
:= False;
695 -- Else we have a missing semicolon
700 -- Normal processing as though semicolon were present
702 Change_Name_To_Procedure_Call_Statement
(Name_Node
);
703 Append_To
(Statement_List
, Name_Node
);
704 Statement_Required
:= False;
707 -- If junk after identifier, check if identifier is an
708 -- instance of an incorrectly spelled keyword. If so, we
709 -- do nothing. The Bad_Spelling_Of will have reset Token
710 -- to the appropriate keyword, so the next time round the
711 -- loop we will process the modified token. Note that we
712 -- check for ELSIF before ELSE here. That's not accidental.
713 -- We don't want to identify a misspelling of ELSE as
714 -- ELSIF, and in particular we do not want to treat ELSEIF
718 Restore_Scan_State
(Scan_State_Label
); -- to identifier
720 if Bad_Spelling_Of
(Tok_Abort
)
721 or else Bad_Spelling_Of
(Tok_Accept
)
722 or else Bad_Spelling_Of
(Tok_Case
)
723 or else Bad_Spelling_Of
(Tok_Declare
)
724 or else Bad_Spelling_Of
(Tok_Delay
)
725 or else Bad_Spelling_Of
(Tok_Elsif
)
726 or else Bad_Spelling_Of
(Tok_Else
)
727 or else Bad_Spelling_Of
(Tok_End
)
728 or else Bad_Spelling_Of
(Tok_Exception
)
729 or else Bad_Spelling_Of
(Tok_Exit
)
730 or else Bad_Spelling_Of
(Tok_For
)
731 or else Bad_Spelling_Of
(Tok_Goto
)
732 or else Bad_Spelling_Of
(Tok_If
)
733 or else Bad_Spelling_Of
(Tok_Loop
)
734 or else Bad_Spelling_Of
(Tok_Or
)
735 or else Bad_Spelling_Of
(Tok_Pragma
)
736 or else Bad_Spelling_Of
(Tok_Raise
)
737 or else Bad_Spelling_Of
(Tok_Requeue
)
738 or else Bad_Spelling_Of
(Tok_Return
)
739 or else Bad_Spelling_Of
(Tok_Select
)
740 or else Bad_Spelling_Of
(Tok_When
)
741 or else Bad_Spelling_Of
(Tok_While
)
745 -- If not a bad spelling, then we really have junk
748 Scan
; -- past identifier again
750 -- If next token is first token on line, then we
751 -- consider that we were missing a semicolon after
752 -- the identifier, and process it as a procedure
753 -- call with no parameters.
755 if Token_Is_At_Start_Of_Line
then
756 Change_Name_To_Procedure_Call_Statement
(Id_Node
);
757 Append_To
(Statement_List
, Id_Node
);
758 T_Semicolon
; -- to give error message
759 Statement_Required
:= False;
761 -- Otherwise we give a missing := message and
762 -- simply abandon the junk that is there now.
765 T_Colon_Equal
; -- give := expected message
772 -- Statement starting with operator symbol. This could be
773 -- a call, a name starting an assignment, or a qualified
776 when Tok_Operator_Symbol
=>
780 -- An attempt at a range attribute or a qualified expression
781 -- must be illegal here (a code statement cannot possibly
782 -- allow qualification by a function name).
784 if Token
= Tok_Apostrophe
then
785 Error_Msg_SC
("apostrophe illegal here");
789 -- Scan possible assignment if we have a name
791 if Expr_Form
= EF_Name
792 and then Token
= Tok_Colon_Equal
794 Scan
; -- past colon equal
795 Append_To
(Statement_List
,
796 P_Assignment_Statement
(Name_Node
));
798 Change_Name_To_Procedure_Call_Statement
(Name_Node
);
799 Append_To
(Statement_List
, Name_Node
);
803 Statement_Required
:= False;
805 -- Label starting with << which must precede real statement
806 -- Note: in Ada 2012, the label may end the sequence.
808 when Tok_Less_Less
=>
809 if Present
(Last
(Statement_List
))
810 and then Nkind
(Last
(Statement_List
)) /= N_Label
812 Statement_Seen
:= True;
815 Append_To
(Statement_List
, P_Label
);
816 Statement_Required
:= True;
818 -- Pragma appearing as a statement in a statement sequence
822 Append_To
(Statement_List
, P_Pragma
);
828 Append_To
(Statement_List
, P_Abort_Statement
);
829 Statement_Required
:= False;
835 Append_To
(Statement_List
, P_Accept_Statement
);
836 Statement_Required
:= False;
838 -- Begin_Statement (Block_Statement with no declare, no label)
842 Append_To
(Statement_List
, P_Begin_Statement
);
843 Statement_Required
:= False;
849 Append_To
(Statement_List
, P_Case_Statement
);
850 Statement_Required
:= False;
852 -- Block_Statement with DECLARE and no label
856 Append_To
(Statement_List
, P_Declare_Statement
);
857 Statement_Required
:= False;
863 Append_To
(Statement_List
, P_Delay_Statement
);
864 Statement_Required
:= False;
870 Append_To
(Statement_List
, P_Exit_Statement
);
871 Statement_Required
:= False;
873 -- Loop_Statement with FOR and no label
877 Append_To
(Statement_List
, P_For_Statement
);
878 Statement_Required
:= False;
884 Append_To
(Statement_List
, P_Goto_Statement
);
885 Statement_Required
:= False;
891 Append_To
(Statement_List
, P_If_Statement
);
892 Statement_Required
:= False;
898 Append_To
(Statement_List
, P_Loop_Statement
);
899 Statement_Required
:= False;
905 Append_To
(Statement_List
, P_Null_Statement
);
906 Statement_Required
:= False;
912 Append_To
(Statement_List
, P_Raise_Statement
);
913 Statement_Required
:= False;
919 Append_To
(Statement_List
, P_Requeue_Statement
);
920 Statement_Required
:= False;
926 Append_To
(Statement_List
, P_Return_Statement
);
927 Statement_Required
:= False;
933 Append_To
(Statement_List
, P_Select_Statement
);
934 Statement_Required
:= False;
936 -- While_Statement (Block_Statement with while and no loop)
940 Append_To
(Statement_List
, P_While_Statement
);
941 Statement_Required
:= False;
943 -- Anything else is some kind of junk, signal an error message
944 -- and then raise Error_Resync, to merge with the normal
945 -- handling of a bad statement.
948 if Token
in Token_Class_Declk
then
952 Error_Msg_BC
-- CODEFIX
953 ("statement expected");
958 -- On error resynchronization, skip past next semicolon, and, since
959 -- we are still in the statement loop, look for next statement. We
960 -- set Statement_Required False to avoid an unnecessary error message
961 -- complaining that no statement was found (i.e. we consider the
962 -- junk to satisfy the requirement for a statement being present).
966 Resync_Past_Semicolon_Or_To_Loop_Or_Then
;
967 Statement_Required
:= False;
970 exit when SS_Flags
.Unco
;
973 return Statement_List
;
974 end P_Sequence_Of_Statements
;
980 ---------------------------
981 -- 5.1 Simple Statement --
982 ---------------------------
984 -- Parsed by P_Sequence_Of_Statements (5.1)
986 -----------------------------
987 -- 5.1 Compound Statement --
988 -----------------------------
990 -- Parsed by P_Sequence_Of_Statements (5.1)
992 -------------------------
993 -- 5.1 Null Statement --
994 -------------------------
996 -- NULL_STATEMENT ::= null;
998 -- The caller has already checked that the current token is null
1000 -- Error recovery: cannot raise Error_Resync
1002 function P_Null_Statement
return Node_Id
is
1003 Null_Stmt_Node
: Node_Id
;
1006 Null_Stmt_Node
:= New_Node
(N_Null_Statement
, Token_Ptr
);
1009 return Null_Stmt_Node
;
1010 end P_Null_Statement
;
1016 -- LABEL ::= <<label_STATEMENT_IDENTIFIER>>
1018 -- STATEMENT_IDENTIFIER ::= DIRECT_NAME
1020 -- The IDENTIFIER of a STATEMENT_IDENTIFIER shall be an identifier
1021 -- (not an OPERATOR_SYMBOL)
1023 -- The caller has already checked that the current token is <<
1025 -- Error recovery: can raise Error_Resync
1027 function P_Label
return Node_Id
is
1028 Label_Node
: Node_Id
;
1031 Label_Node
:= New_Node
(N_Label
, Token_Ptr
);
1033 Set_Identifier
(Label_Node
, P_Identifier
(C_Greater_Greater
));
1035 Append_Elmt
(Label_Node
, Label_List
);
1039 -------------------------------
1040 -- 5.1 Statement Identifier --
1041 -------------------------------
1043 -- Statement label is parsed by P_Label (5.1)
1045 -- Loop label is parsed by P_Loop_Statement (5.5), P_For_Statement (5.5)
1046 -- or P_While_Statement (5.5)
1048 -- Block label is parsed by P_Begin_Statement (5.6) or
1049 -- P_Declare_Statement (5.6)
1051 -------------------------------
1052 -- 5.2 Assignment Statement --
1053 -------------------------------
1055 -- ASSIGNMENT_STATEMENT ::=
1056 -- variable_NAME := EXPRESSION;
1058 -- Error recovery: can raise Error_Resync
1060 function P_Assignment_Statement
(LHS
: Node_Id
) return Node_Id
is
1061 Assign_Node
: Node_Id
;
1064 Assign_Node
:= New_Node
(N_Assignment_Statement
, Prev_Token_Ptr
);
1065 Current_Assign_Node
:= Assign_Node
;
1066 Set_Name
(Assign_Node
, LHS
);
1067 Set_Expression
(Assign_Node
, P_Expression_No_Right_Paren
);
1069 Current_Assign_Node
:= Empty
;
1071 end P_Assignment_Statement
;
1073 -----------------------
1074 -- 5.3 If Statement --
1075 -----------------------
1078 -- if CONDITION then
1079 -- SEQUENCE_OF_STATEMENTS
1080 -- {elsif CONDITION then
1081 -- SEQUENCE_OF_STATEMENTS}
1083 -- SEQUENCE_OF_STATEMENTS]
1086 -- The caller has checked that the initial token is IF (or in the error
1087 -- case of a mysterious THEN, the initial token may simply be THEN, in
1088 -- which case, no condition (or IF) was scanned).
1090 -- Error recovery: can raise Error_Resync
1092 function P_If_Statement
return Node_Id
is
1094 Elsif_Node
: Node_Id
;
1097 procedure Add_Elsif_Part
;
1098 -- An internal procedure used to scan out a single ELSIF part. On entry
1099 -- the ELSIF (or an ELSE which has been determined should be ELSIF) is
1100 -- scanned out and is in Prev_Token.
1102 procedure Check_If_Column
;
1103 -- An internal procedure used to check that THEN, ELSE, or ELSIF
1104 -- appear in the right place if column checking is enabled (i.e. if
1105 -- they are the first token on the line, then they must appear in
1106 -- the same column as the opening IF).
1108 procedure Check_Then_Column
;
1109 -- This procedure carries out the style checks for a THEN token
1110 -- Note that the caller has set Loc to the Source_Ptr value for
1111 -- the previous IF or ELSIF token.
1113 function Else_Should_Be_Elsif
return Boolean;
1114 -- An internal routine used to do a special error recovery check when
1115 -- an ELSE is encountered. It determines if the ELSE should be treated
1116 -- as an ELSIF. A positive decision (TRUE returned, is made if the ELSE
1117 -- is followed by a sequence of tokens, starting on the same line as
1118 -- the ELSE, which are not expression terminators, followed by a THEN.
1119 -- On entry, the ELSE has been scanned out.
1121 procedure Add_Elsif_Part
is
1123 if No
(Elsif_Parts
(If_Node
)) then
1124 Set_Elsif_Parts
(If_Node
, New_List
);
1127 Elsif_Node
:= New_Node
(N_Elsif_Part
, Prev_Token_Ptr
);
1128 Loc
:= Prev_Token_Ptr
;
1129 Set_Condition
(Elsif_Node
, P_Condition
);
1133 (Elsif_Node
, P_Sequence_Of_Statements
(SS_Eftm_Eltm_Sreq
));
1134 Append
(Elsif_Node
, Elsif_Parts
(If_Node
));
1137 procedure Check_If_Column
is
1139 if RM_Column_Check
and then Token_Is_At_Start_Of_Line
1140 and then Start_Column
/= Scopes
(Scope
.Last
).Ecol
1142 Error_Msg_Col
:= Scopes
(Scope
.Last
).Ecol
;
1143 Error_Msg_SC
("(style) this token should be@");
1145 end Check_If_Column
;
1147 procedure Check_Then_Column
is
1149 if Token
= Tok_Then
then
1153 Style
.Check_Then
(Loc
);
1156 end Check_Then_Column
;
1158 function Else_Should_Be_Elsif
return Boolean is
1159 Scan_State
: Saved_Scan_State
;
1162 if Token_Is_At_Start_Of_Line
then
1166 Save_Scan_State
(Scan_State
);
1169 if Token
in Token_Class_Eterm
then
1170 Restore_Scan_State
(Scan_State
);
1173 Scan
; -- past non-expression terminating token
1175 if Token
= Tok_Then
then
1176 Restore_Scan_State
(Scan_State
);
1182 end Else_Should_Be_Elsif
;
1184 -- Start of processing for P_If_Statement
1187 If_Node
:= New_Node
(N_If_Statement
, Token_Ptr
);
1190 Scopes
(Scope
.Last
).Etyp
:= E_If
;
1191 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1192 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1193 Scopes
(Scope
.Last
).Labl
:= Error
;
1194 Scopes
(Scope
.Last
).Node
:= If_Node
;
1196 if Token
= Tok_If
then
1199 Set_Condition
(If_Node
, P_Condition
);
1201 -- Deal with misuse of IF expression => used instead
1202 -- of WHEN expression =>
1204 if Token
= Tok_Arrow
then
1205 Error_Msg_SC
-- CODEFIX
1207 Scan
; -- past the arrow
1208 Pop_Scope_Stack
; -- remove unneeded entry
1215 Error_Msg_SC
("no IF for this THEN");
1216 Set_Condition
(If_Node
, Error
);
1222 (If_Node
, P_Sequence_Of_Statements
(SS_Eftm_Eltm_Sreq
));
1224 -- This loop scans out else and elsif parts
1227 if Token
= Tok_Elsif
then
1230 if Present
(Else_Statements
(If_Node
)) then
1231 Error_Msg_SP
("ELSIF cannot appear after ELSE");
1237 elsif Token
= Tok_Else
then
1241 if Else_Should_Be_Elsif
then
1242 Error_Msg_SP
-- CODEFIX
1243 ("ELSE should be ELSIF");
1247 -- Here we have an else that really is an else
1249 if Present
(Else_Statements
(If_Node
)) then
1250 Error_Msg_SP
("only one ELSE part allowed");
1252 (P_Sequence_Of_Statements
(SS_Eftm_Eltm_Sreq
),
1253 Else_Statements
(If_Node
));
1256 (If_Node
, P_Sequence_Of_Statements
(SS_Eftm_Eltm_Sreq
));
1260 -- If anything other than ELSE or ELSIF, exit the loop. The token
1261 -- had better be END (and in fact it had better be END IF), but
1262 -- we will let End_Statements take care of checking that.
1274 --------------------
1276 --------------------
1278 -- CONDITION ::= boolean_EXPRESSION
1280 function P_Condition
return Node_Id
is
1282 return P_Condition
(P_Expression_No_Right_Paren
);
1285 function P_Condition
(Cond
: Node_Id
) return Node_Id
is
1287 -- It is never possible for := to follow a condition, so if we get
1288 -- a := we assume it is a mistyped equality. Note that we do not try
1289 -- to reconstruct the tree correctly in this case, but we do at least
1290 -- give an accurate error message.
1292 if Token
= Tok_Colon_Equal
then
1293 while Token
= Tok_Colon_Equal
loop
1294 Error_Msg_SC
-- CODEFIX
1295 (""":="" should be ""=""");
1296 Scan
; -- past junk :=
1297 Discard_Junk_Node
(P_Expression_No_Right_Paren
);
1302 -- Otherwise check for redundant parentheses but do not emit messages
1303 -- about expressions that require parentheses (e.g. conditional,
1304 -- quantified or declaration expressions).
1309 Paren_Count
(Cond
) >
1310 (if Nkind
(Cond
) in N_Case_Expression
1311 | N_Expression_With_Actions
1313 | N_Quantified_Expression
1317 Style
.Check_Xtra_Parens
(First_Sloc
(Cond
));
1320 -- And return the result
1326 -------------------------
1327 -- 5.4 Case Statement --
1328 -------------------------
1330 -- CASE_STATEMENT ::=
1331 -- case EXPRESSION is
1332 -- CASE_STATEMENT_ALTERNATIVE
1333 -- {CASE_STATEMENT_ALTERNATIVE}
1336 -- The caller has checked that the first token is CASE
1338 -- Can raise Error_Resync
1340 function P_Case_Statement
return Node_Id
is
1341 Case_Node
: Node_Id
;
1342 Alternatives_List
: List_Id
;
1343 First_When_Loc
: Source_Ptr
;
1346 Case_Node
:= New_Node
(N_Case_Statement
, Token_Ptr
);
1349 Scopes
(Scope
.Last
).Etyp
:= E_Case
;
1350 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1351 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1352 Scopes
(Scope
.Last
).Labl
:= Error
;
1353 Scopes
(Scope
.Last
).Node
:= Case_Node
;
1356 Set_Expression
(Case_Node
, P_Expression_No_Right_Paren
);
1359 -- Prepare to parse case statement alternatives
1361 Alternatives_List
:= New_List
;
1362 P_Pragmas_Opt
(Alternatives_List
);
1363 First_When_Loc
:= Token_Ptr
;
1365 -- Loop through case statement alternatives
1368 -- If we have a WHEN or OTHERS, then that's fine keep going. Note
1369 -- that it is a semantic check to ensure the proper use of OTHERS
1371 if Token
= Tok_When
or else Token
= Tok_Others
then
1372 Append
(P_Case_Statement_Alternative
, Alternatives_List
);
1374 -- If we have an END, then probably we are at the end of the case
1375 -- but we only exit if Check_End thinks the END was reasonable.
1377 elsif Token
= Tok_End
then
1378 exit when Check_End
;
1380 -- Here if token is other than WHEN, OTHERS or END. We definitely
1381 -- have an error, but the question is whether or not to get out of
1382 -- the case statement. We don't want to get out early, or we will
1383 -- get a slew of junk error messages for subsequent when tokens.
1385 -- If the token is not at the start of the line, or if it is indented
1386 -- with respect to the current case statement, then the best guess is
1387 -- that we are still supposed to be inside the case statement. We
1388 -- complain about the missing WHEN, and discard the junk statements.
1390 elsif not Token_Is_At_Start_Of_Line
1391 or else Start_Column
> Scopes
(Scope
.Last
).Ecol
1393 Error_Msg_BC
("WHEN (case statement alternative) expected");
1395 -- Here is a possibility for infinite looping if we don't make
1396 -- progress. So try to process statements, otherwise exit
1399 Error_Ptr
: constant Source_Ptr
:= Scan_Ptr
;
1401 Discard_Junk_List
(P_Sequence_Of_Statements
(SS_Whtm
));
1402 exit when Scan_Ptr
= Error_Ptr
and then Check_End
;
1405 -- Here we have a junk token at the start of the line and it is
1406 -- not indented. If Check_End thinks there is a missing END, then
1407 -- we will get out of the case, otherwise we keep going.
1410 exit when Check_End
;
1414 -- Make sure we have at least one alternative
1416 if No
(First_Non_Pragma
(Alternatives_List
)) then
1418 ("WHEN expected, must have at least one alternative in case",
1423 Set_Alternatives
(Case_Node
, Alternatives_List
);
1426 end P_Case_Statement
;
1428 -------------------------------------
1429 -- 5.4 Case Statement Alternative --
1430 -------------------------------------
1432 -- CASE_STATEMENT_ALTERNATIVE ::=
1433 -- when DISCRETE_CHOICE_LIST =>
1434 -- SEQUENCE_OF_STATEMENTS
1436 -- The caller has checked that the initial token is WHEN or OTHERS
1437 -- Error recovery: can raise Error_Resync
1439 function P_Case_Statement_Alternative
return Node_Id
is
1440 Case_Alt_Node
: Node_Id
;
1444 Style
.Check_Indentation
;
1447 Case_Alt_Node
:= New_Node
(N_Case_Statement_Alternative
, Token_Ptr
);
1448 T_When
; -- past WHEN (or give error in OTHERS case)
1449 Set_Discrete_Choices
(Case_Alt_Node
, P_Discrete_Choice_List
);
1451 Set_Statements
(Case_Alt_Node
, P_Sequence_Of_Statements
(SS_Sreq_Whtm
));
1452 return Case_Alt_Node
;
1453 end P_Case_Statement_Alternative
;
1455 -------------------------
1456 -- 5.5 Loop Statement --
1457 -------------------------
1459 -- LOOP_STATEMENT ::=
1460 -- [LOOP_STATEMENT_IDENTIFIER:]
1461 -- [ITERATION_SCHEME] loop
1462 -- SEQUENCE_OF_STATEMENTS
1463 -- end loop [loop_IDENTIFIER];
1465 -- ITERATION_SCHEME ::=
1467 -- | for LOOP_PARAMETER_SPECIFICATION
1469 -- The parsing of loop statements is handled by one of three functions
1470 -- P_Loop_Statement, P_For_Statement or P_While_Statement depending
1471 -- on the initial keyword in the construct (excluding the identifier)
1475 -- This function parses the case where no iteration scheme is present
1477 -- The caller has checked that the initial token is LOOP. The parameter
1478 -- is the node identifiers for the loop label if any (or is set to Empty
1479 -- if there is no loop label).
1481 -- Error recovery : cannot raise Error_Resync
1483 function P_Loop_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
is
1484 Loop_Node
: Node_Id
;
1485 Created_Name
: Node_Id
;
1489 Scopes
(Scope
.Last
).Labl
:= Loop_Name
;
1490 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1491 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1492 Scopes
(Scope
.Last
).Etyp
:= E_Loop
;
1494 Loop_Node
:= New_Node
(N_Loop_Statement
, Token_Ptr
);
1497 if No
(Loop_Name
) then
1499 Make_Identifier
(Sloc
(Loop_Node
), Set_Loop_Block_Name
('L'));
1500 Set_Comes_From_Source
(Created_Name
, False);
1501 Set_Has_Created_Identifier
(Loop_Node
, True);
1502 Set_Identifier
(Loop_Node
, Created_Name
);
1503 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1505 Set_Identifier
(Loop_Node
, Loop_Name
);
1508 Append_Elmt
(Loop_Node
, Label_List
);
1509 Set_Statements
(Loop_Node
, P_Sequence_Of_Statements
(SS_Sreq
));
1510 End_Statements
(Loop_Node
);
1512 end P_Loop_Statement
;
1516 -- This function parses a loop statement with a FOR iteration scheme
1518 -- The caller has checked that the initial token is FOR. The parameter
1519 -- is the node identifier for the block label if any (or is set to Empty
1520 -- if there is no block label).
1522 -- Note: the caller fills in the Identifier field if a label was present
1524 -- Error recovery: can raise Error_Resync
1526 function P_For_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
is
1527 Loop_Node
: Node_Id
;
1528 Iter_Scheme_Node
: Node_Id
;
1529 Loop_For_Flag
: Boolean;
1530 Created_Name
: Node_Id
;
1535 Scopes
(Scope
.Last
).Labl
:= Loop_Name
;
1536 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1537 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1538 Scopes
(Scope
.Last
).Etyp
:= E_Loop
;
1540 Loop_For_Flag
:= (Prev_Token
= Tok_Loop
);
1542 Iter_Scheme_Node
:= New_Node
(N_Iteration_Scheme
, Token_Ptr
);
1543 Spec
:= P_Loop_Parameter_Specification
;
1545 if Nkind
(Spec
) = N_Loop_Parameter_Specification
then
1546 Set_Loop_Parameter_Specification
(Iter_Scheme_Node
, Spec
);
1548 Set_Iterator_Specification
(Iter_Scheme_Node
, Spec
);
1551 -- The following is a special test so that a miswritten for loop such
1552 -- as "loop for I in 1..10;" is handled nicely, without making an extra
1553 -- entry in the scope stack. We don't bother to actually fix up the
1554 -- tree in this case since it's not worth the effort. Instead we just
1555 -- eat up the loop junk, leaving the entry for what now looks like an
1556 -- unmodified loop intact.
1558 if Loop_For_Flag
and then Token
= Tok_Semicolon
then
1559 Error_Msg_SC
("LOOP belongs here, not before FOR");
1566 Loop_Node
:= New_Node
(N_Loop_Statement
, Token_Ptr
);
1568 if No
(Loop_Name
) then
1570 Make_Identifier
(Sloc
(Loop_Node
), Set_Loop_Block_Name
('L'));
1571 Set_Comes_From_Source
(Created_Name
, False);
1572 Set_Has_Created_Identifier
(Loop_Node
, True);
1573 Set_Identifier
(Loop_Node
, Created_Name
);
1574 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1576 Set_Identifier
(Loop_Node
, Loop_Name
);
1580 Set_Statements
(Loop_Node
, P_Sequence_Of_Statements
(SS_Sreq
));
1581 End_Statements
(Loop_Node
);
1582 Set_Iteration_Scheme
(Loop_Node
, Iter_Scheme_Node
);
1583 Append_Elmt
(Loop_Node
, Label_List
);
1586 end P_For_Statement
;
1588 -- P_While_Statement
1590 -- This procedure scans a loop statement with a WHILE iteration scheme
1592 -- The caller has checked that the initial token is WHILE. The parameter
1593 -- is the node identifier for the block label if any (or is set to Empty
1594 -- if there is no block label).
1596 -- Error recovery: cannot raise Error_Resync
1598 function P_While_Statement
(Loop_Name
: Node_Id
:= Empty
) return Node_Id
is
1599 Loop_Node
: Node_Id
;
1600 Iter_Scheme_Node
: Node_Id
;
1601 Loop_While_Flag
: Boolean;
1602 Created_Name
: Node_Id
;
1606 Scopes
(Scope
.Last
).Labl
:= Loop_Name
;
1607 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1608 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1609 Scopes
(Scope
.Last
).Etyp
:= E_Loop
;
1611 Loop_While_Flag
:= (Prev_Token
= Tok_Loop
);
1612 Iter_Scheme_Node
:= New_Node
(N_Iteration_Scheme
, Token_Ptr
);
1614 Set_Condition
(Iter_Scheme_Node
, P_Condition
);
1616 -- The following is a special test so that a miswritten for loop such
1617 -- as "loop while I > 10;" is handled nicely, without making an extra
1618 -- entry in the scope stack. We don't bother to actually fix up the
1619 -- tree in this case since it's not worth the effort. Instead we just
1620 -- eat up the loop junk, leaving the entry for what now looks like an
1621 -- unmodified loop intact.
1623 if Loop_While_Flag
and then Token
= Tok_Semicolon
then
1624 Error_Msg_SC
("LOOP belongs here, not before WHILE");
1631 Loop_Node
:= New_Node
(N_Loop_Statement
, Token_Ptr
);
1634 if No
(Loop_Name
) then
1636 Make_Identifier
(Sloc
(Loop_Node
), Set_Loop_Block_Name
('L'));
1637 Set_Comes_From_Source
(Created_Name
, False);
1638 Set_Has_Created_Identifier
(Loop_Node
, True);
1639 Set_Identifier
(Loop_Node
, Created_Name
);
1640 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1642 Set_Identifier
(Loop_Node
, Loop_Name
);
1645 Set_Statements
(Loop_Node
, P_Sequence_Of_Statements
(SS_Sreq
));
1646 End_Statements
(Loop_Node
);
1647 Set_Iteration_Scheme
(Loop_Node
, Iter_Scheme_Node
);
1648 Append_Elmt
(Loop_Node
, Label_List
);
1651 end P_While_Statement
;
1653 ---------------------------------------
1654 -- 5.5 Loop Parameter Specification --
1655 ---------------------------------------
1657 -- LOOP_PARAMETER_SPECIFICATION ::=
1658 -- DEFINING_IDENTIFIER in [reverse] DISCRETE_SUBTYPE_DEFINITION
1659 -- [Iterator_Filter]
1661 -- Error recovery: cannot raise Error_Resync
1663 function P_Loop_Parameter_Specification
return Node_Id
is
1664 Loop_Param_Specification_Node
: Node_Id
;
1667 Scan_State
: Saved_Scan_State
;
1671 Save_Scan_State
(Scan_State
);
1672 ID_Node
:= P_Defining_Identifier
(C_In
);
1674 -- If the next token is OF, it indicates an Ada 2012 iterator. If the
1675 -- next token is a colon, this is also an Ada 2012 iterator, including
1676 -- a subtype indication for the loop parameter. Otherwise we parse the
1677 -- construct as a loop parameter specification. Note that the form
1678 -- "for A in B" is ambiguous, and must be resolved semantically: if B
1679 -- is a discrete subtype this is a loop specification, but if it is an
1680 -- expression it is an iterator specification. Ambiguity is resolved
1681 -- during analysis of the loop parameter specification.
1683 if Token
= Tok_Of
or else Token
= Tok_Colon
then
1684 Error_Msg_Ada_2012_Feature
("iterator", Token_Ptr
);
1685 return P_Iterator_Specification
(ID_Node
);
1688 -- The span of the Loop_Parameter_Specification starts at the
1689 -- defining identifier.
1691 Loop_Param_Specification_Node
:=
1692 New_Node
(N_Loop_Parameter_Specification
, Sloc
(ID_Node
));
1693 Set_Defining_Identifier
(Loop_Param_Specification_Node
, ID_Node
);
1695 if Token
= Tok_Left_Paren
then
1696 Error_Msg_SC
("subscripted loop parameter not allowed");
1697 Restore_Scan_State
(Scan_State
);
1698 Discard_Junk_Node
(P_Name
);
1700 elsif Token
= Tok_Dot
then
1701 Error_Msg_SC
("selected loop parameter not allowed");
1702 Restore_Scan_State
(Scan_State
);
1703 Discard_Junk_Node
(P_Name
);
1708 if Token
= Tok_Reverse
then
1709 Scan
; -- past REVERSE
1710 Set_Reverse_Present
(Loop_Param_Specification_Node
, True);
1713 Set_Discrete_Subtype_Definition
1714 (Loop_Param_Specification_Node
, P_Discrete_Subtype_Definition
);
1716 if Token
= Tok_When
then
1717 Error_Msg_Ada_2022_Feature
("iterator filter", Token_Ptr
);
1721 (Loop_Param_Specification_Node
, P_Condition
);
1724 return Loop_Param_Specification_Node
;
1727 when Error_Resync
=>
1729 end P_Loop_Parameter_Specification
;
1731 ----------------------------------
1732 -- 5.5.1 Iterator_Specification --
1733 ----------------------------------
1735 function P_Iterator_Specification
(Def_Id
: Node_Id
) return Node_Id
is
1739 Node1
:= New_Node
(N_Iterator_Specification
, Sloc
(Def_Id
));
1740 Set_Defining_Identifier
(Node1
, Def_Id
);
1742 if Token
= Tok_Colon
then
1745 if Token
= Tok_Access
then
1746 Error_Msg_Ada_2022_Feature
1747 ("access definition in loop parameter", Token_Ptr
);
1748 Set_Subtype_Indication
(Node1
, P_Access_Definition
(False));
1751 Set_Subtype_Indication
(Node1
, P_Subtype_Indication
);
1755 if Token
= Tok_Of
then
1756 Set_Of_Present
(Node1
);
1759 elsif Token
= Tok_In
then
1762 elsif Prev_Token
= Tok_In
1763 and then Present
(Subtype_Indication
(Node1
))
1765 -- Simplest recovery is to transform it into an element iterator.
1766 -- Error message on 'in" has already been emitted when parsing the
1767 -- optional constraint.
1769 Set_Of_Present
(Node1
);
1771 ("subtype indication is only legal on an element iterator",
1772 Subtype_Indication
(Node1
));
1778 if Token
= Tok_Reverse
then
1779 Scan
; -- past REVERSE
1780 Set_Reverse_Present
(Node1
, True);
1783 Set_Name
(Node1
, P_Name
);
1785 if Token
= Tok_When
then
1786 Error_Msg_Ada_2022_Feature
("iterator filter", Token_Ptr
);
1790 (Node1
, P_Condition
);
1794 end P_Iterator_Specification
;
1796 --------------------------
1797 -- 5.6 Block Statement --
1798 --------------------------
1800 -- BLOCK_STATEMENT ::=
1801 -- [block_STATEMENT_IDENTIFIER:]
1803 -- DECLARATIVE_PART]
1805 -- HANDLED_SEQUENCE_OF_STATEMENTS
1806 -- end [block_IDENTIFIER];
1808 -- The parsing of block statements is handled by one of the two functions
1809 -- P_Declare_Statement or P_Begin_Statement depending on whether or not
1810 -- a declare section is present
1812 -- P_Declare_Statement
1814 -- This function parses a block statement with DECLARE present
1816 -- The caller has checked that the initial token is DECLARE
1818 -- Error recovery: cannot raise Error_Resync
1820 function P_Declare_Statement
1821 (Block_Name
: Node_Id
:= Empty
)
1824 Block_Node
: Node_Id
;
1825 Created_Name
: Node_Id
;
1828 Block_Node
:= New_Node
(N_Block_Statement
, Token_Ptr
);
1831 Scopes
(Scope
.Last
).Etyp
:= E_Name
;
1832 Scopes
(Scope
.Last
).Lreq
:= Present
(Block_Name
);
1833 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1834 Scopes
(Scope
.Last
).Labl
:= Block_Name
;
1835 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1837 Scan
; -- past DECLARE
1839 if No
(Block_Name
) then
1841 Make_Identifier
(Sloc
(Block_Node
), Set_Loop_Block_Name
('B'));
1842 Set_Comes_From_Source
(Created_Name
, False);
1843 Set_Has_Created_Identifier
(Block_Node
, True);
1844 Set_Identifier
(Block_Node
, Created_Name
);
1845 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1847 Set_Identifier
(Block_Node
, Block_Name
);
1850 Append_Elmt
(Block_Node
, Label_List
);
1851 Parse_Decls_Begin_End
(Block_Node
);
1853 end P_Declare_Statement
;
1855 -- P_Begin_Statement
1857 -- This function parses a block statement with no DECLARE present
1859 -- The caller has checked that the initial token is BEGIN
1861 -- Error recovery: cannot raise Error_Resync
1863 function P_Begin_Statement
1864 (Block_Name
: Node_Id
:= Empty
)
1867 Block_Node
: Node_Id
;
1868 Created_Name
: Node_Id
;
1871 Block_Node
:= New_Node
(N_Block_Statement
, Token_Ptr
);
1874 Scopes
(Scope
.Last
).Etyp
:= E_Name
;
1875 Scopes
(Scope
.Last
).Lreq
:= Present
(Block_Name
);
1876 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1877 Scopes
(Scope
.Last
).Labl
:= Block_Name
;
1878 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1880 if No
(Block_Name
) then
1882 Make_Identifier
(Sloc
(Block_Node
), Set_Loop_Block_Name
('B'));
1883 Set_Comes_From_Source
(Created_Name
, False);
1884 Set_Has_Created_Identifier
(Block_Node
, True);
1885 Set_Identifier
(Block_Node
, Created_Name
);
1886 Scopes
(Scope
.Last
).Labl
:= Created_Name
;
1888 Set_Identifier
(Block_Node
, Block_Name
);
1891 Append_Elmt
(Block_Node
, Label_List
);
1893 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
1894 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
1896 Set_Handled_Statement_Sequence
1897 (Block_Node
, P_Handled_Sequence_Of_Statements
);
1898 End_Statements
(Handled_Statement_Sequence
(Block_Node
));
1900 end P_Begin_Statement
;
1902 -------------------------
1903 -- 5.7 Exit Statement --
1904 -------------------------
1906 -- EXIT_STATEMENT ::=
1907 -- exit [loop_NAME] [when CONDITION];
1909 -- The caller has checked that the initial token is EXIT
1911 -- Error recovery: can raise Error_Resync
1913 function P_Exit_Statement
return Node_Id
is
1914 Exit_Node
: Node_Id
;
1916 -- Start of processing for P_Exit_Statement
1919 Exit_Node
:= New_Node
(N_Exit_Statement
, Token_Ptr
);
1922 if Token
= Tok_Identifier
then
1923 Set_Name
(Exit_Node
, P_Qualified_Simple_Name
);
1925 elsif Style_Check
then
1926 -- This EXIT has no name, so check that
1927 -- the innermost loop is unnamed too.
1929 Check_No_Exit_Name
:
1930 for J
in reverse 1 .. Scope
.Last
loop
1931 if Scopes
(J
).Etyp
= E_Loop
then
1932 if Present
(Scopes
(J
).Labl
)
1933 and then Comes_From_Source
(Scopes
(J
).Labl
)
1935 -- Innermost loop in fact had a name, style check fails
1937 Style
.No_Exit_Name
(Scopes
(J
).Labl
);
1940 exit Check_No_Exit_Name
;
1942 end loop Check_No_Exit_Name
;
1945 if Token
= Tok_When
and then not Missing_Semicolon_On_When
then
1947 Set_Condition
(Exit_Node
, P_Condition
);
1949 -- Allow IF instead of WHEN, giving error message
1951 elsif Token
= Tok_If
then
1953 Scan
; -- past IF used in place of WHEN
1954 Set_Condition
(Exit_Node
, P_Expression_No_Right_Paren
);
1959 end P_Exit_Statement
;
1961 -------------------------
1962 -- 5.8 Goto Statement --
1963 -------------------------
1965 -- GOTO_STATEMENT ::= goto label_NAME;
1967 -- The caller has checked that the initial token is GOTO (or TO in the
1968 -- error case where GO and TO were incorrectly separated).
1970 -- Error recovery: can raise Error_Resync
1972 function P_Goto_Statement
return Node_Id
is
1973 Goto_Node
: Node_Id
;
1976 Goto_Node
:= New_Node
(N_Goto_Statement
, Token_Ptr
);
1977 Scan
; -- past GOTO (or TO)
1978 Set_Name
(Goto_Node
, P_Qualified_Simple_Name_Resync
);
1979 Append_Elmt
(Goto_Node
, Goto_List
);
1981 if Token
= Tok_When
then
1982 Error_Msg_GNAT_Extension
("goto when statement");
1985 Mutate_Nkind
(Goto_Node
, N_Goto_When_Statement
);
1986 Set_Condition
(Goto_Node
, P_Expression_No_Right_Paren
);
1991 end P_Goto_Statement
;
1993 ---------------------------
1994 -- Parse_Decls_Begin_End --
1995 ---------------------------
1997 -- This function parses the construct:
2001 -- HANDLED_SEQUENCE_OF_STATEMENTS
2004 -- The caller has built the scope stack entry, and created the node to
2005 -- whose Declarations and Handled_Statement_Sequence fields are to be
2006 -- set. On return these fields are filled in (except in the case of a
2007 -- task body, where the handled statement sequence is optional, and may
2008 -- thus be Empty), and the scan is positioned past the End sequence.
2010 -- If the BEGIN is missing, then the parent node is used to help construct
2011 -- an appropriate missing BEGIN message. Possibilities for the parent are:
2013 -- N_Block_Statement declare block
2014 -- N_Entry_Body entry body
2015 -- N_Package_Body package body (begin part optional)
2016 -- N_Subprogram_Body procedure or function body
2017 -- N_Task_Body task body
2019 -- Note: in the case of a block statement, there is definitely a DECLARE
2020 -- present (because a Begin statement without a DECLARE is handled by the
2021 -- P_Begin_Statement procedure, which does not call Parse_Decls_Begin_End.
2023 -- Error recovery: cannot raise Error_Resync
2025 procedure Parse_Decls_Begin_End
(Parent
: Node_Id
) is
2026 Body_Decl
: Node_Id
;
2028 Parent_Nkind
: Node_Kind
;
2029 Spec_Node
: Node_Id
;
2032 procedure Missing_Begin
(Msg
: String);
2033 -- Called to post a missing begin message. In the normal case this is
2034 -- posted at the start of the current token. A special case arises when
2035 -- P_Declarative_Items has previously found a missing begin, in which
2036 -- case we replace the original error message.
2038 procedure Set_Null_HSS
(Parent
: Node_Id
);
2039 -- Construct an empty handled statement sequence and install in Parent
2040 -- Leaves HSS set to reference the newly constructed statement sequence.
2046 procedure Missing_Begin
(Msg
: String) is
2048 if Missing_Begin_Msg
= No_Error_Msg
then
2051 Change_Error_Text
(Missing_Begin_Msg
, Msg
);
2053 -- Purge any messages issued after than, since a missing begin
2054 -- can cause a lot of havoc, and it is better not to dump these
2055 -- cascaded messages on the user.
2057 Purge_Messages
(Get_Location
(Missing_Begin_Msg
), Prev_Token_Ptr
);
2065 procedure Set_Null_HSS
(Parent
: Node_Id
) is
2070 Make_Null_Statement
(Token_Ptr
);
2071 Set_Comes_From_Source
(Null_Stm
, False);
2074 Make_Handled_Sequence_Of_Statements
(Token_Ptr
,
2075 Statements
=> New_List
(Null_Stm
));
2076 Set_Comes_From_Source
(HSS
, False);
2078 Set_Handled_Statement_Sequence
(Parent
, HSS
);
2081 -- Start of processing for Parse_Decls_Begin_End
2084 Decls
:= P_Declarative_Part
;
2086 if Ada_Version
= Ada_83
then
2087 Check_Later_Vs_Basic_Declarations
(Decls
, During_Parsing
=> True);
2090 -- Here is where we deal with the case of IS used instead of semicolon.
2091 -- Specifically, if the last declaration in the declarative part is a
2092 -- subprogram body still marked as having a bad IS, then this is where
2093 -- we decide that the IS should really have been a semicolon and that
2094 -- the body should have been a declaration. Note that if the bad IS
2095 -- had turned out to be OK (i.e. a decent begin/end was found for it),
2096 -- then the Bad_Is_Detected flag would have been reset by now.
2098 Body_Decl
:= Last
(Decls
);
2100 if Present
(Body_Decl
)
2101 and then Nkind
(Body_Decl
) = N_Subprogram_Body
2102 and then Bad_Is_Detected
(Body_Decl
)
2104 -- OK, we have the case of a bad IS, so we need to fix up the tree.
2105 -- What we have now is a subprogram body with attached declarations
2106 -- and a possible statement sequence.
2108 -- First step is to take the declarations that were part of the bogus
2109 -- subprogram body and append them to the outer declaration chain.
2110 -- In other words we append them past the body (which we will later
2111 -- convert into a declaration).
2113 Append_List
(Declarations
(Body_Decl
), Decls
);
2115 -- Now take the handled statement sequence of the bogus body and
2116 -- set it as the statement sequence for the outer construct. Note
2117 -- that it may be empty (we specially allowed a missing BEGIN for
2118 -- a subprogram body marked as having a bad IS -- see below).
2120 Set_Handled_Statement_Sequence
(Parent
,
2121 Handled_Statement_Sequence
(Body_Decl
));
2123 -- Next step is to convert the old body node to a declaration node
2125 Spec_Node
:= Specification
(Body_Decl
);
2126 Change_Node
(Body_Decl
, N_Subprogram_Declaration
);
2127 Set_Specification
(Body_Decl
, Spec_Node
);
2129 -- Final step is to put the declarations for the parent where
2130 -- they belong, and then fall through the IF to scan out the
2133 Set_Declarations
(Parent
, Decls
);
2135 -- This is the normal case (i.e. any case except the bad IS case)
2136 -- If we have a BEGIN, then scan out the sequence of statements, and
2137 -- also reset the expected column for the END to match the BEGIN.
2140 Set_Declarations
(Parent
, Decls
);
2142 if Token
= Tok_Begin
then
2144 Style
.Check_Indentation
;
2147 Error_Msg_Col
:= Scopes
(Scope
.Last
).Ecol
;
2150 and then Token_Is_At_Start_Of_Line
2151 and then Start_Column
/= Error_Msg_Col
2153 Error_Msg_SC
("(style) BEGIN in wrong column, should be@");
2156 Scopes
(Scope
.Last
).Ecol
:= Start_Column
;
2159 Scopes
(Scope
.Last
).Sloc
:= Token_Ptr
;
2161 Set_Handled_Statement_Sequence
(Parent
,
2162 P_Handled_Sequence_Of_Statements
);
2167 Parent_Nkind
:= Nkind
(Parent
);
2169 -- A special check for the missing IS case. If we have a
2170 -- subprogram body that was marked as having a suspicious
2171 -- IS, and the current token is END, then we simply confirm
2172 -- the suspicion, and do not require a BEGIN to be present
2174 if Parent_Nkind
= N_Subprogram_Body
2175 and then Token
= Tok_End
2176 and then Scopes
(Scope
.Last
).Etyp
= E_Suspicious_Is
2178 Scopes
(Scope
.Last
).Etyp
:= E_Bad_Is
;
2180 -- Otherwise BEGIN is not required for a package body, so we
2181 -- don't mind if it is missing, but we do construct a dummy
2182 -- one (so that we have somewhere to set End_Label).
2184 -- However if we have something other than a BEGIN which
2185 -- looks like it might be statements, then we signal a missing
2186 -- BEGIN for these cases as well. We define "something which
2187 -- looks like it might be statements" as a token other than
2188 -- END, EOF, or a token which starts declarations.
2190 elsif Parent_Nkind
= N_Package_Body
2191 and then (Token
= Tok_End
2192 or else Token
= Tok_EOF
2193 or else Token
in Token_Class_Declk
)
2195 Set_Null_HSS
(Parent
);
2197 -- These are cases in which a BEGIN is required and not present
2200 Set_Null_HSS
(Parent
);
2202 -- Prepare to issue error message
2204 Error_Msg_Sloc
:= Scopes
(Scope
.Last
).Sloc
;
2205 Error_Msg_Node_1
:= Scopes
(Scope
.Last
).Labl
;
2207 -- Now issue appropriate message
2209 if Parent_Nkind
= N_Block_Statement
then
2210 Missing_Begin
("missing BEGIN for DECLARE#!");
2212 elsif Parent_Nkind
= N_Entry_Body
then
2213 Missing_Begin
("missing BEGIN for ENTRY#!");
2215 elsif Parent_Nkind
= N_Subprogram_Body
then
2216 if Nkind
(Specification
(Parent
))
2217 = N_Function_Specification
2219 Missing_Begin
("missing BEGIN for function&#!");
2221 Missing_Begin
("missing BEGIN for procedure&#!");
2224 -- The case for package body arises only when
2225 -- we have possible statement junk present.
2227 elsif Parent_Nkind
= N_Package_Body
then
2228 Missing_Begin
("missing BEGIN for package body&#!");
2231 pragma Assert
(Parent_Nkind
= N_Task_Body
);
2232 Missing_Begin
("missing BEGIN for task body&#!");
2235 -- Here we pick up the statements after the BEGIN that
2236 -- should have been present but was not. We don't insist
2237 -- on statements being present if P_Declarative_Part had
2238 -- already found a missing BEGIN, since it might have
2239 -- swallowed a lone statement into the declarative part.
2241 if Missing_Begin_Msg
/= No_Error_Msg
2242 and then Token
= Tok_End
2246 Set_Handled_Statement_Sequence
(Parent
,
2247 P_Handled_Sequence_Of_Statements
);
2253 -- Here with declarations and handled statement sequence scanned
2255 if Present
(Handled_Statement_Sequence
(Parent
)) then
2256 End_Statements
(Handled_Statement_Sequence
(Parent
));
2261 -- We know that End_Statements removed an entry from the scope stack
2262 -- (because it is required to do so under all circumstances). We can
2263 -- therefore reference the entry it removed one past the stack top.
2264 -- What we are interested in is whether it was a case of a bad IS.
2265 -- We can't call Scopes here.
2267 if Scope
.Table
(Scope
.Last
+ 1).Etyp
= E_Bad_Is
then
2268 Error_Msg
-- CODEFIX
2269 ("|IS should be "";""", Scope
.Table
(Scope
.Last
+ 1).S_Is
);
2270 Set_Bad_Is_Detected
(Parent
, True);
2273 end Parse_Decls_Begin_End
;
2275 -------------------------
2276 -- Set_Loop_Block_Name --
2277 -------------------------
2279 function Set_Loop_Block_Name
(L
: Character) return Name_Id
is
2281 Name_Buffer
(1) := L
;
2282 Name_Buffer
(2) := '_';
2284 Loop_Block_Count
:= Loop_Block_Count
+ 1;
2285 Add_Nat_To_Name_Buffer
(Loop_Block_Count
);
2287 end Set_Loop_Block_Name
;
2293 procedure Then_Scan
is
2297 while Token
= Tok_Then
loop
2298 Error_Msg_SC
-- CODEFIX
2303 if Token
= Tok_And
or else Token
= Tok_Or
then
2304 Error_Msg_SC
("unexpected logical operator");
2305 Scan
; -- past logical operator
2307 if (Prev_Token
= Tok_And
and then Token
= Tok_Then
)
2309 (Prev_Token
= Tok_Or
and then Token
= Tok_Else
)
2314 Discard_Junk_Node
(P_Expression
);
2317 if Token
= Tok_Then
then