Debian Codegraph @

gcc/ada/par-ch5.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R . C H 5 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Sinfo.CN; use Sinfo.CN;
27
28 separate (Par)
29 package body Ch5 is
30
31 -- Local functions, used only in this chapter
32
33 function P_Case_Statement return Node_Id;
34 function P_Case_Statement_Alternative return Node_Id;
35 function P_Exit_Statement return Node_Id;
36 function P_Goto_Statement return Node_Id;
37 function P_If_Statement return Node_Id;
38 function P_Label return Node_Id;
39 function P_Null_Statement return Node_Id;
40
41 function P_Assignment_Statement (LHS : Node_Id) return Node_Id;
42 -- Parse assignment statement. On entry, the caller has scanned the left
43 -- hand side (passed in as Lhs), and the colon-equal (or some symbol
44 -- taken to be an error equivalent such as equal).
45
46 function P_Begin_Statement (Block_Name : Node_Id := Empty) return Node_Id;
47 -- Parse begin-end statement. If Block_Name is non-Empty on entry, it is
48 -- the N_Identifier node for the label on the block. If Block_Name is
49 -- Empty on entry (the default), then the block statement is unlabeled.
50
51 function P_Declare_Statement (Block_Name : Node_Id := Empty) return Node_Id;
52 -- Parse declare block. If Block_Name is non-Empty on entry, it is
53 -- the N_Identifier node for the label on the block. If Block_Name is
54 -- Empty on entry (the default), then the block statement is unlabeled.
55
56 function P_For_Statement (Loop_Name : Node_Id := Empty) return Node_Id;
57 -- Parse for statement. If Loop_Name is non-Empty on entry, it is
58 -- the N_Identifier node for the label on the loop. If Loop_Name is
59 -- Empty on entry (the default), then the for statement is unlabeled.
60
61 function P_Loop_Statement (Loop_Name : Node_Id := Empty) return Node_Id;
62 -- Parse loop statement. If Loop_Name is non-Empty on entry, it is
63 -- the N_Identifier node for the label on the loop. If Loop_Name is
64 -- Empty on entry (the default), then the loop statement is unlabeled.
65
66 function P_While_Statement (Loop_Name : Node_Id := Empty) return Node_Id;
67 -- Parse while statement. If Loop_Name is non-Empty on entry, it is
68 -- the N_Identifier node for the label on the loop. If Loop_Name is
69 -- Empty on entry (the default), then the while statement is unlabeled.
70
71 function Set_Loop_Block_Name (L : Character) return Name_Id;
72 -- Given a letter 'L' for a loop or 'B' for a block, returns a name
73 -- of the form L_nn or B_nn where nn is a serial number obtained by
74 -- incrementing the variable Loop_Block_Count.
75
76 procedure Then_Scan;
77 -- Scan past THEN token, testing for illegal junk after it
78
79 ---------------------------------
80 -- 5.1 Sequence of Statements --
81 ---------------------------------
82
83 -- SEQUENCE_OF_STATEMENTS ::= STATEMENT {STATEMENT} {LABEL}
84 -- Note: the final label is an Ada 2012 addition.
85
86 -- STATEMENT ::=
87 -- {LABEL} SIMPLE_STATEMENT | {LABEL} COMPOUND_STATEMENT
88
89 -- SIMPLE_STATEMENT ::= NULL_STATEMENT
90 -- | ASSIGNMENT_STATEMENT | EXIT_STATEMENT
91 -- | GOTO_STATEMENT | PROCEDURE_CALL_STATEMENT
92 -- | RETURN_STATEMENT | ENTRY_CALL_STATEMENT
93 -- | REQUEUE_STATEMENT | DELAY_STATEMENT
94 -- | ABORT_STATEMENT | RAISE_STATEMENT
95 -- | CODE_STATEMENT
96
97 -- COMPOUND_STATEMENT ::=
98 -- IF_STATEMENT | CASE_STATEMENT
99 -- | LOOP_STATEMENT | BLOCK_STATEMENT
100 -- | ACCEPT_STATEMENT | SELECT_STATEMENT
101
102 -- This procedure scans a sequence of statements. SS_Flags indicates
103 -- termination conditions for the sequence. In addition, the sequence is
104 -- always terminated by encountering END or end of file. If one of the six
105 -- above terminators is encountered with the corresponding SS_Flags flag
106 -- not set, then the action taken is as follows:
107
108 -- If the keyword occurs to the left of the expected column of the end
109 -- for the current sequence (as recorded in the current end context),
110 -- then it is assumed to belong to an outer context, and is considered
111 -- to terminate the sequence of statements.
112
113 -- If the keyword occurs to the right of, or in the expected column of
114 -- the end for the current sequence, then an error message is output,
115 -- the keyword together with its associated context is skipped, and
116 -- the statement scan continues until another terminator is found.
117
118 -- Note that the first action means that control can return to the caller
119 -- with Token set to a terminator other than one of those specified by the
120 -- SS_Flags parameter. The caller should treat such a case as equivalent to
121 -- END.
122
123 -- In addition, the flag SS_Flags.Sreq is set to True to indicate that at
124 -- least one real statement (other than a pragma) is required in the
125 -- statement sequence. During the processing of the sequence, this
126 -- flag is manipulated to indicate the current status of the requirement
127 -- for a statement. For example, it is turned off by the occurrence of a
128 -- statement, and back on by a label (which requires a following statement)
129
130 -- Error recovery: cannot raise Error_Resync. If an error occurs during
131 -- parsing a statement, then the scan pointer is advanced past the next
132 -- semicolon and the parse continues.
133
134 function P_Sequence_Of_Statements (SS_Flags : SS_Rec) return List_Id
135 is
136 Statement_Required : Boolean := SS_Flags.Sreq;
137 -- This flag indicates if a subsequent statement (other than a pragma)
138 -- is required. It is initialized from the Sreq flag, and modified as
139 -- statements are scanned (a statement turns it off, and a label turns
140 -- it back on again since a statement must follow a label).
141 -- Note : this final requirement is lifted in Ada 2012.
142
143 Statement_Seen : Boolean := False;
144 -- In Ada 2012, a label can end a sequence of statements, but the
145 -- sequence cannot contain only labels. This flag is set whenever a
146 -- label is encountered, to enforce this rule at the end of a sequence.
147
148 Scan_State_Label : Saved_Scan_State;
149 Scan_State : Saved_Scan_State;
150
151 Statement_List : constant List_Id := New_List;
152 Block_Label : Name_Id;
153 Id_Node : Node_Id;
154 Name_Node : Node_Id;
155
156 Decl_Loc, Label_Loc : Source_Ptr := No_Location;
157 -- Sloc of the first declaration/label encountered, if any.
158
159 procedure Test_Statement_Required;
160 -- Flag error if Statement_Required flag set
161
162 -----------------------------
163 -- Test_Statement_Required --
164 -----------------------------
165
166 procedure Test_Statement_Required is
167 function All_Pragmas return Boolean;
168 -- Return True if statement list is all pragmas
169
170 -----------------
171 -- All_Pragmas --
172 -----------------
173
174 function All_Pragmas return Boolean is
175 S : Node_Id;
176 begin
177 S := First (Statement_List);
178 while Present (S) loop
179 if Nkind (S) /= N_Pragma then
180 return False;
181 else
182 Next (S);
183 end if;
184 end loop;
185
186 return True;
187 end All_Pragmas;
188
189 -- Start of processing for Test_Statement_Required
190
191 begin
192 if Statement_Required then
193
194 -- Check no statement required after label in Ada 2012, and that
195 -- it is OK to have nothing but pragmas in a statement sequence.
196
197 if Ada_Version >= Ada_2012
198 and then not Is_Empty_List (Statement_List)
199 and then
200 ((Nkind (Last (Statement_List)) = N_Label
201 and then Statement_Seen)
202 or else All_Pragmas)
203 then
204 null;
205
206 -- If not Ada 2012, or not special case above, and no declaration
207 -- seen (as allowed in Ada 2022), give error message.
208
209 elsif No (Decl_Loc) then
210 Error_Msg_BC -- CODEFIX
211 ("statement expected");
212 end if;
213 end if;
214 end Test_Statement_Required;
215
216 -- Start of processing for P_Sequence_Of_Statements
217
218 begin
219 -- When extensions are active, we allow declarative items to be mixed
220 -- with statements. The loop below alternates between calling
221 -- P_Declarative_Items to parse zero or more declarative items, and
222 -- parsing a statement.
223
224 loop
225 Ignore (Tok_Semicolon);
226
227 declare
228 Num_Statements : constant Nat := List_Length (Statement_List);
229 Decl : Node_Id;
230 begin
231 P_Declarative_Items
232 (Statement_List, Declare_Expression => False,
233 In_Spec => False, In_Statements => True);
234
235 -- Use the length of the list to determine whether we parsed
236 -- any declarative items.
237
238 if List_Length (Statement_List) > Num_Statements then
239 Decl := Pick (Statement_List, Num_Statements + 1);
240
241 -- If so, it's an error unless language extensions are enabled.
242
243 if All_Errors_Mode or else No (Decl_Loc) then
244 Decl_Loc := Sloc (Decl);
245
246 Error_Msg_GNAT_Extension
247 ("declarations mixed with statements", Sloc (Decl),
248 Is_Core_Extension => True);
249
250 end if;
251
252 -- Check every declaration added to the list, to see whether
253 -- it's part of the allowed subset of declarations. Only check
254 -- that if core extensions are allowed.
255
256 if Core_Extensions_Allowed then
257 while Present (Decl) loop
258 if not (Nkind (Decl) in
259 N_Object_Declaration | N_Object_Renaming_Declaration |
260 N_Use_Type_Clause | N_Use_Package_Clause)
261 then
262 Error_Msg
263 ("Declaration kind not allowed in statements lists",
264 Sloc (Decl));
265 end if;
266
267 Next (Decl);
268 end loop;
269 end if;
270 end if;
271 end;
272
273 begin -- handle Error_Resync
274 if Style_Check then
275 Style.Check_Indentation;
276 end if;
277
278 -- Deal with reserved identifier (in assignment or call)
279
280 if Is_Reserved_Identifier then
281 Save_Scan_State (Scan_State); -- at possible bad identifier
282 Scan; -- and scan past it
283
284 -- We have an reserved word which is spelled in identifier
285 -- style, so the question is whether it really is intended
286 -- to be an identifier.
287
288 if
289 -- If followed by a semicolon, then it is an identifier,
290 -- with the exception of the cases tested for below.
291
292 (Token = Tok_Semicolon
293 and then Prev_Token not in
294 Tok_Return | Tok_Null | Tok_Raise | Tok_End | Tok_Exit)
295
296 -- If followed by colon, colon-equal, or dot, then we
297 -- definitely have an identifier (could not be reserved)
298
299 or else Token in Tok_Colon | Tok_Colon_Equal | Tok_Dot
300
301 -- Left paren means we have an identifier except for those
302 -- reserved words that can legitimately be followed by a
303 -- left paren.
304
305 or else
306 (Token = Tok_Left_Paren
307 and then Prev_Token not in
308 Tok_Case | Tok_Delay | Tok_If | Tok_Elsif | Tok_Return |
309 Tok_When | Tok_While | Tok_Separate)
310 then
311 -- Here we have an apparent reserved identifier and the
312 -- token past it is appropriate to this usage (and would
313 -- be a definite error if this is not an identifier). What
314 -- we do is to use P_Identifier to fix up the identifier,
315 -- and then fall into the normal processing.
316
317 Restore_Scan_State (Scan_State); -- back to the ID
318 Scan_Reserved_Identifier (Force_Msg => False);
319
320 -- Not a reserved identifier after all (or at least we can't
321 -- be sure that it is), so reset the scan and continue.
322
323 else
324 Restore_Scan_State (Scan_State); -- back to the reserved word
325 end if;
326 end if;
327
328 -- Now look to see what kind of statement we have
329
330 case Token is
331
332 -- Case of end or EOF
333
334 when Tok_End
335 | Tok_EOF
336 =>
337 -- These tokens always terminate the statement sequence
338
339 Test_Statement_Required;
340 exit;
341
342 -- Case of ELSIF
343
344 when Tok_Elsif =>
345
346 -- Terminate if Eftm set or if the ELSIF is to the left
347 -- of the expected column of the end for this sequence
348
349 if SS_Flags.Eftm
350 or else Start_Column < Scopes (Scope.Last).Ecol
351 then
352 Test_Statement_Required;
353 exit;
354
355 -- Otherwise complain and skip past ELSIF Condition then
356
357 else
358 Error_Msg_SC ("ELSIF not allowed here");
359 Scan; -- past ELSIF
360 Discard_Junk_Node (P_Expression_No_Right_Paren);
361 Then_Scan;
362 Statement_Required := False;
363 end if;
364
365 -- Case of ELSE
366
367 when Tok_Else =>
368
369 -- Terminate if Eltm set or if the else is to the left
370 -- of the expected column of the end for this sequence
371
372 if SS_Flags.Eltm
373 or else Start_Column < Scopes (Scope.Last).Ecol
374 then
375 Test_Statement_Required;
376 exit;
377
378 -- Otherwise complain and skip past else
379
380 else
381 Error_Msg_SC ("ELSE not allowed here");
382 Scan; -- past ELSE
383 Statement_Required := False;
384 end if;
385
386 -- Case of exception
387
388 when Tok_Exception =>
389 Test_Statement_Required;
390
391 -- If Extm not set and the exception is not to the left of
392 -- the expected column of the end for this sequence, then we
393 -- assume it belongs to the current sequence, even though it
394 -- is not permitted.
395
396 if not SS_Flags.Extm and then
397 Start_Column >= Scopes (Scope.Last).Ecol
398
399 then
400 Error_Msg_SC ("exception handler not permitted here");
401 Scan; -- past EXCEPTION
402 Discard_Junk_List (Parse_Exception_Handlers);
403 end if;
404
405 -- Always return, in the case where we scanned out handlers
406 -- that we did not expect, Parse_Exception_Handlers returned
407 -- with Token being either end or EOF, so we are OK.
408
409 exit;
410
411 -- Case of finally
412
413 when Tok_Finally =>
414 Test_Statement_Required;
415
416 -- See the analogous comment in the Tok_Exception branch.
417
418 if not SS_Flags.Fitm
419 and then Start_Column >= Scopes (Scope.Last).Ecol
420 then
421 Error_Msg_SC ("finally construct not permitted here");
422 Scan; -- past FINALLY
423 Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
424 end if;
425
426 -- We exit like in the exception branch, should we really???
427
428 exit;
429
430 -- Case of OR
431
432 when Tok_Or =>
433
434 -- Terminate if Ortm set or if the or is to the left of the
435 -- expected column of the end for this sequence.
436
437 if SS_Flags.Ortm
438 or else Start_Column < Scopes (Scope.Last).Ecol
439 then
440 Test_Statement_Required;
441 exit;
442
443 -- Otherwise complain and skip past or
444
445 else
446 Error_Msg_SC ("OR not allowed here");
447 Scan; -- past or
448 Statement_Required := False;
449 end if;
450
451 -- Case of THEN (deal also with THEN ABORT)
452
453 when Tok_Then =>
454 Save_Scan_State (Scan_State); -- at THEN
455 Scan; -- past THEN
456
457 -- Terminate if THEN ABORT allowed (ATC case)
458
459 exit when SS_Flags.Tatm and then Token = Tok_Abort;
460
461 -- Otherwise we treat THEN as some kind of mess where we did
462 -- not see the associated IF, but we pick up assuming it had
463 -- been there.
464
465 Restore_Scan_State (Scan_State); -- to THEN
466 Append_To (Statement_List, P_If_Statement);
467 Statement_Required := False;
468
469 -- Case of WHEN (error because we are not in a case)
470
471 when Tok_Others
472 | Tok_When
473 =>
474 -- Terminate if Whtm set or if the WHEN is to the left of
475 -- the expected column of the end for this sequence.
476
477 if SS_Flags.Whtm
478 or else Start_Column < Scopes (Scope.Last).Ecol
479 then
480 Test_Statement_Required;
481 exit;
482
483 -- Otherwise complain and skip when Choice {| Choice} =>
484
485 else
486 Error_Msg_SC ("WHEN not allowed here");
487 Scan; -- past when
488 Discard_Junk_List (P_Discrete_Choice_List);
489 TF_Arrow;
490 Statement_Required := False;
491 end if;
492
493 -- Cases of statements starting with an identifier
494
495 when Tok_Identifier =>
496 Check_Bad_Layout;
497
498 -- Save scan pointers and line number in case block label
499
500 Id_Node := Token_Node;
501 Block_Label := Token_Name;
502 Save_Scan_State (Scan_State_Label); -- at possible label
503 Scan; -- past Id
504
505 -- Check for common case of assignment, since it occurs
506 -- frequently, and we want to process it efficiently.
507
508 if Token = Tok_Colon_Equal then
509 Scan; -- past the colon-equal
510 Append_To (Statement_List,
511 P_Assignment_Statement (Id_Node));
512 Statement_Required := False;
513
514 -- Check common case of procedure call, another case that
515 -- we want to speed up as much as possible.
516
517 elsif Token = Tok_Semicolon then
518 Change_Name_To_Procedure_Call_Statement (Id_Node);
519 Append_To (Statement_List, Id_Node);
520 Scan; -- past semicolon
521 Statement_Required := False;
522
523 -- Here is the special test for a suspicious label, more
524 -- accurately a suspicious name, which we think perhaps
525 -- should have been a label. If next token is one of
526 -- LOOP, FOR, WHILE, DECLARE, BEGIN, then make an entry
527 -- in the suspicious label table.
528
529 if Token = Tok_Loop or else
530 Token = Tok_For or else
531 Token = Tok_While or else
532 Token = Tok_Declare or else
533 Token = Tok_Begin
534 then
535 Suspicious_Labels.Append
536 ((Proc_Call => Id_Node,
537 Semicolon_Loc => Prev_Token_Ptr,
538 Start_Token => Token_Ptr));
539 end if;
540
541 -- Check for case of "go to" in place of "goto"
542
543 elsif Token = Tok_Identifier
544 and then Block_Label = Name_Go
545 and then Token_Name = Name_To
546 then
547 Error_Msg_SP -- CODEFIX
548 ("goto is one word");
549 Append_To (Statement_List, P_Goto_Statement);
550 Statement_Required := False;
551
552 -- Check common case of = used instead of :=, just so we
553 -- give a better error message for this special misuse.
554
555 elsif Token = Tok_Equal then
556 T_Colon_Equal; -- give := expected message
557 Append_To (Statement_List,
558 P_Assignment_Statement (Id_Node));
559 Statement_Required := False;
560
561 -- Check case of loop label or block label
562
563 elsif Token = Tok_Colon
564 or else (Token in Token_Class_Labeled_Stmt
565 and then not Token_Is_At_Start_Of_Line)
566 then
567 T_Colon; -- past colon (if there, or msg for missing one)
568
569 -- Test for more than one label
570
571 loop
572 exit when Token /= Tok_Identifier;
573 Save_Scan_State (Scan_State); -- at second Id
574 Scan; -- past Id
575
576 if Token = Tok_Colon then
577 Error_Msg_SP
578 ("only one label allowed on block or loop");
579 Scan; -- past colon on extra label
580
581 -- Use the second label as the "real" label
582
583 Scan_State_Label := Scan_State;
584
585 -- We will set Error_name as the Block_Label since
586 -- we really don't know which of the labels might
587 -- be used at the end of the loop or block.
588
589 Block_Label := Error_Name;
590
591 -- If Id with no colon, then backup to point to the
592 -- Id and we will issue the message below when we try
593 -- to scan out the statement as some other form.
594
595 else
596 Restore_Scan_State (Scan_State); -- to second Id
597 exit;
598 end if;
599 end loop;
600
601 -- Loop_Statement (labeled Loop_Statement)
602
603 if Token = Tok_Loop then
604 Append_To (Statement_List,
605 P_Loop_Statement (Id_Node));
606
607 -- While statement (labeled loop statement with WHILE)
608
609 elsif Token = Tok_While then
610 Append_To (Statement_List,
611 P_While_Statement (Id_Node));
612
613 -- Declare statement (labeled block statement with
614 -- DECLARE part)
615
616 elsif Token = Tok_Declare then
617 Append_To (Statement_List,
618 P_Declare_Statement (Id_Node));
619
620 -- Begin statement (labeled block statement with no
621 -- DECLARE part)
622
623 elsif Token = Tok_Begin then
624 Append_To (Statement_List,
625 P_Begin_Statement (Id_Node));
626
627 -- For statement (labeled loop statement with FOR)
628
629 elsif Token = Tok_For then
630 Append_To (Statement_List,
631 P_For_Statement (Id_Node));
632
633 -- Otherwise complain we have inappropriate statement
634
635 else
636 Error_Msg_AP
637 ("loop or block statement must follow label");
638 end if;
639
640 Statement_Required := False;
641
642 -- Here we have an identifier followed by something
643 -- other than a colon, semicolon or assignment symbol.
644 -- The only valid possibility is a name extension symbol
645
646 elsif Token in Token_Class_Namext then
647 Restore_Scan_State (Scan_State_Label); -- to Id
648 Name_Node := P_Name;
649
650 -- Skip junk right parens in this context
651
652 Ignore (Tok_Right_Paren);
653
654 -- Check context following call
655
656 if Token = Tok_Colon_Equal then
657 Scan; -- past colon equal
658 Append_To (Statement_List,
659 P_Assignment_Statement (Name_Node));
660 Statement_Required := False;
661
662 -- Check common case of = used instead of :=
663
664 elsif Token = Tok_Equal then
665 T_Colon_Equal; -- give := expected message
666 Append_To (Statement_List,
667 P_Assignment_Statement (Name_Node));
668 Statement_Required := False;
669
670 -- Check apostrophe cases
671
672 elsif Token = Tok_Apostrophe then
673 Append_To (Statement_List,
674 P_Code_Statement (Name_Node));
675 Statement_Required := False;
676
677 -- The only other valid item after a name is ; which
678 -- means that the item we just scanned was a call.
679
680 elsif Token = Tok_Semicolon then
681 Change_Name_To_Procedure_Call_Statement (Name_Node);
682 Append_To (Statement_List, Name_Node);
683 Scan; -- past semicolon
684 Statement_Required := False;
685
686 -- A slash following an identifier or a selected
687 -- component in this situation is most likely a period
688 -- (see location of keys on keyboard).
689
690 elsif Token = Tok_Slash
691 and then (Nkind (Name_Node) = N_Identifier
692 or else
693 Nkind (Name_Node) = N_Selected_Component)
694 then
695 Error_Msg_SC -- CODEFIX
696 ("""/"" should be "".""");
697 Statement_Required := False;
698 raise Error_Resync;
699
700 -- Else we have a missing semicolon
701
702 else
703 TF_Semicolon;
704
705 -- Normal processing as though semicolon were present
706
707 Change_Name_To_Procedure_Call_Statement (Name_Node);
708 Append_To (Statement_List, Name_Node);
709 Statement_Required := False;
710 end if;
711
712 -- If junk after identifier, check if identifier is an
713 -- instance of an incorrectly spelled keyword. If so, we
714 -- do nothing. The Bad_Spelling_Of will have reset Token
715 -- to the appropriate keyword, so the next time round the
716 -- loop we will process the modified token.
717 --
718 -- Note that we check for ELSIF before ELSE here, because
719 -- we don't want to identify a misspelling of ELSE as ELSIF,
720 -- and in particular we do not want to treat ELSEIF as
721 -- ELSE IF.
722
723 else
724 Restore_Scan_State (Scan_State_Label); -- to identifier
725
726 if Bad_Spelling_Of (Tok_Abort)
727 or else Bad_Spelling_Of (Tok_Accept)
728 or else Bad_Spelling_Of (Tok_Case)
729 or else Bad_Spelling_Of (Tok_Declare)
730 or else Bad_Spelling_Of (Tok_Delay)
731 or else Bad_Spelling_Of (Tok_Elsif)
732 or else Bad_Spelling_Of (Tok_Else)
733 or else Bad_Spelling_Of (Tok_End)
734 or else Bad_Spelling_Of (Tok_Exception)
735 or else Bad_Spelling_Of (Tok_Exit)
736 or else Bad_Spelling_Of (Tok_For)
737 or else Bad_Spelling_Of (Tok_Goto)
738 or else Bad_Spelling_Of (Tok_If)
739 or else Bad_Spelling_Of (Tok_Loop)
740 or else Bad_Spelling_Of (Tok_Or)
741 or else Bad_Spelling_Of (Tok_Pragma)
742 or else Bad_Spelling_Of (Tok_Raise)
743 or else Bad_Spelling_Of (Tok_Requeue)
744 or else Bad_Spelling_Of (Tok_Return)
745 or else Bad_Spelling_Of (Tok_Select)
746 or else Bad_Spelling_Of (Tok_When)
747 or else Bad_Spelling_Of (Tok_While)
748 then
749 null;
750
751 -- If not a bad spelling, then we really have junk
752
753 else
754 Scan; -- past identifier again
755
756 -- If next token is first token on line, then we
757 -- consider that we were missing a semicolon after
758 -- the identifier, and process it as a procedure
759 -- call with no parameters.
760
761 if Token_Is_At_Start_Of_Line then
762 Change_Name_To_Procedure_Call_Statement (Id_Node);
763 Append_To (Statement_List, Id_Node);
764 T_Semicolon; -- to give error message
765 Statement_Required := False;
766
767 -- Otherwise we give a missing := message and
768 -- simply abandon the junk that is there now.
769
770 else
771 T_Colon_Equal; -- give := expected message
772 raise Error_Resync;
773 end if;
774
775 end if;
776 end if;
777
778 -- Statement starting with operator symbol. This could be
779 -- a call, a name starting an assignment, or a qualified
780 -- expression.
781
782 when Tok_Operator_Symbol =>
783 Check_Bad_Layout;
784 Name_Node := P_Name;
785
786 -- An attempt at a range attribute or a qualified expression
787 -- must be illegal here (a code statement cannot possibly
788 -- allow qualification by a function name).
789
790 if Token = Tok_Apostrophe then
791 Error_Msg_SC ("apostrophe illegal here");
792 raise Error_Resync;
793 end if;
794
795 -- Scan possible assignment if we have a name
796
797 if Expr_Form = EF_Name
798 and then Token = Tok_Colon_Equal
799 then
800 Scan; -- past colon equal
801 Append_To (Statement_List,
802 P_Assignment_Statement (Name_Node));
803 else
804 Change_Name_To_Procedure_Call_Statement (Name_Node);
805 Append_To (Statement_List, Name_Node);
806 end if;
807
808 TF_Semicolon;
809 Statement_Required := False;
810
811 -- Label starting with << which must precede real statement
812 -- Note: in Ada 2012, the label may end the sequence.
813
814 when Tok_Less_Less =>
815 if Present (Last (Statement_List))
816 and then Nkind (Last (Statement_List)) /= N_Label
817 then
818 Statement_Seen := True;
819 end if;
820
821 Append_To (Statement_List, P_Label);
822 Statement_Required := True;
823
824 if No (Label_Loc) then
825 Label_Loc := Sloc (Last (Statement_List));
826 end if;
827
828 -- Pragma appearing as a statement in a statement sequence
829
830 when Tok_Pragma =>
831 Check_Bad_Layout;
832 Append_To (Statement_List, P_Pragma);
833
834 -- Abort_Statement
835
836 when Tok_Abort =>
837 Check_Bad_Layout;
838 Append_To (Statement_List, P_Abort_Statement);
839 Statement_Required := False;
840
841 -- Accept_Statement
842
843 when Tok_Accept =>
844 Check_Bad_Layout;
845 Append_To (Statement_List, P_Accept_Statement);
846 Statement_Required := False;
847
848 -- Begin_Statement (Block_Statement with no declare, no label)
849
850 when Tok_Begin =>
851 Check_Bad_Layout;
852 Append_To (Statement_List, P_Begin_Statement);
853 Statement_Required := False;
854
855 -- Case_Statement
856
857 when Tok_Case =>
858 Check_Bad_Layout;
859 Append_To (Statement_List, P_Case_Statement);
860 Statement_Required := False;
861
862 -- Block_Statement with DECLARE and no label
863
864 when Tok_Declare =>
865 Check_Bad_Layout;
866 Append_To (Statement_List, P_Declare_Statement);
867 Statement_Required := False;
868
869 -- Delay_Statement
870
871 when Tok_Delay =>
872 Check_Bad_Layout;
873 Append_To (Statement_List, P_Delay_Statement);
874 Statement_Required := False;
875
876 -- Exit_Statement
877
878 when Tok_Exit =>
879 Check_Bad_Layout;
880 Append_To (Statement_List, P_Exit_Statement);
881 Statement_Required := False;
882
883 -- Loop_Statement with FOR and no label
884
885 when Tok_For =>
886 Check_Bad_Layout;
887 Append_To (Statement_List, P_For_Statement);
888 Statement_Required := False;
889
890 -- Goto_Statement
891
892 when Tok_Goto =>
893 Check_Bad_Layout;
894 Append_To (Statement_List, P_Goto_Statement);
895 Statement_Required := False;
896
897 -- If_Statement
898
899 when Tok_If =>
900 Check_Bad_Layout;
901 Append_To (Statement_List, P_If_Statement);
902 Statement_Required := False;
903
904 -- Loop_Statement
905
906 when Tok_Loop =>
907 Check_Bad_Layout;
908 Append_To (Statement_List, P_Loop_Statement);
909 Statement_Required := False;
910
911 -- Null_Statement
912
913 when Tok_Null =>
914 Check_Bad_Layout;
915 Append_To (Statement_List, P_Null_Statement);
916 Statement_Required := False;
917
918 -- Raise_Statement
919
920 when Tok_Raise =>
921 Check_Bad_Layout;
922 Append_To (Statement_List, P_Raise_Statement);
923 Statement_Required := False;
924
925 -- Requeue_Statement
926
927 when Tok_Requeue =>
928 Check_Bad_Layout;
929 Append_To (Statement_List, P_Requeue_Statement);
930 Statement_Required := False;
931
932 -- Return_Statement
933
934 when Tok_Return =>
935 Check_Bad_Layout;
936 Append_To (Statement_List, P_Return_Statement);
937 Statement_Required := False;
938
939 -- Select_Statement
940
941 when Tok_Select =>
942 Check_Bad_Layout;
943 Append_To (Statement_List, P_Select_Statement);
944 Statement_Required := False;
945
946 -- While_Statement (Block_Statement with while and no loop)
947
948 when Tok_While =>
949 Check_Bad_Layout;
950 Append_To (Statement_List, P_While_Statement);
951 Statement_Required := False;
952
953 -- Anything else is some kind of junk, signal an error message
954 -- and then raise Error_Resync, to merge with the normal
955 -- handling of a bad statement.
956
957 when others =>
958 Error_Msg_BC -- CODEFIX
959 ("statement expected");
960 raise Error_Resync;
961 end case;
962
963 -- On error resynchronization, skip past next semicolon, and, since
964 -- we are still in the statement loop, look for next statement. We
965 -- set Statement_Required False to avoid an unnecessary error message
966 -- complaining that no statement was found (i.e. we consider the
967 -- junk to satisfy the requirement for a statement being present).
968
969 exception
970 when Error_Resync =>
971 Resync_Past_Semicolon_Or_To_Loop_Or_Then;
972 Statement_Required := False;
973 end;
974
975 exit when SS_Flags.Unco;
976 end loop;
977
978 -- If there are declarative items in the list, we always wrap it in a
979 -- block, so that anything declared in a statement list is not visible
980 -- from the exception handlers. Example:
981 --
982 -- if ... then
983 -- use Some_Package;
984 -- Do_Something (...);
985 -- end if;
986 --
987 -- is tranformed into:
988 --
989 -- if ... then
990 -- begin
991 -- use Some_Package;
992 -- Do_Something (...);
993 -- end;
994 -- end if;
995 --
996 -- This:
997 --
998 -- declare
999 -- X : Integer;
1000 -- begin
1001 -- X : Integer;
1002 --
1003 -- is transformed into this:
1004 --
1005 -- declare
1006 -- X : Integer;
1007 -- begin
1008 -- declare
1009 -- X : Integer;
1010 -- begin
1011 -- ...
1012 --
1013 -- We hence don't try to detect this case, even though it can be
1014 -- confusing to users, and might possibly deserve a warning.
1015
1016 if Present (Decl_Loc) then
1017 -- Forbid labels and declarative items from coexisting. Otherwise,
1018 -- one could jump past a declaration, leading to chaos. Jumping
1019 -- backward past a declaration is also questionable -- does the
1020 -- declaration get elaborated again? Is secondary stack storage
1021 -- reclaimed? (A more liberal rule was proposed, but this is what
1022 -- we're doing for now.)
1023
1024 if Present (Label_Loc) then
1025 Error_Msg ("declarative item in same list as label", Decl_Loc);
1026 Error_Msg ("label in same list as declarative item", Label_Loc);
1027 end if;
1028
1029 declare
1030 Loc : constant Source_Ptr := Sloc (First (Statement_List));
1031 Block : constant Node_Id :=
1032 Make_Block_Statement
1033 (Loc,
1034 Handled_Statement_Sequence =>
1035 Make_Handled_Sequence_Of_Statements
1036 (Loc, Statements => Statement_List));
1037 begin
1038 return New_List (Block);
1039 end;
1040 else
1041 return Statement_List;
1042 end if;
1043 end P_Sequence_Of_Statements;
1044
1045 --------------------
1046 -- 5.1 Statement --
1047 --------------------
1048
1049 ---------------------------
1050 -- 5.1 Simple Statement --
1051 ---------------------------
1052
1053 -- Parsed by P_Sequence_Of_Statements (5.1)
1054
1055 -----------------------------
1056 -- 5.1 Compound Statement --
1057 -----------------------------
1058
1059 -- Parsed by P_Sequence_Of_Statements (5.1)
1060
1061 -------------------------
1062 -- 5.1 Null Statement --
1063 -------------------------
1064
1065 -- NULL_STATEMENT ::= null;
1066
1067 -- The caller has already checked that the current token is null
1068
1069 -- Error recovery: cannot raise Error_Resync
1070
1071 function P_Null_Statement return Node_Id is
1072 Null_Stmt_Node : Node_Id;
1073
1074 begin
1075 Null_Stmt_Node := New_Node (N_Null_Statement, Token_Ptr);
1076 Scan; -- past NULL
1077 TF_Semicolon;
1078 return Null_Stmt_Node;
1079 end P_Null_Statement;
1080
1081 ----------------
1082 -- 5.1 Label --
1083 ----------------
1084
1085 -- LABEL ::= <<label_STATEMENT_IDENTIFIER>>
1086
1087 -- STATEMENT_IDENTIFIER ::= DIRECT_NAME
1088
1089 -- The IDENTIFIER of a STATEMENT_IDENTIFIER shall be an identifier
1090 -- (not an OPERATOR_SYMBOL)
1091
1092 -- The caller has already checked that the current token is <<
1093
1094 -- Error recovery: can raise Error_Resync
1095
1096 function P_Label return Node_Id is
1097 Label_Node : Node_Id;
1098
1099 begin
1100 Label_Node := New_Node (N_Label, Token_Ptr);
1101 Scan; -- past <<
1102 Set_Identifier (Label_Node, P_Identifier (C_Greater_Greater));
1103 T_Greater_Greater;
1104 Append_Elmt (Label_Node, Label_List);
1105 return Label_Node;
1106 end P_Label;
1107
1108 -------------------------------
1109 -- 5.1 Statement Identifier --
1110 -------------------------------
1111
1112 -- Statement label is parsed by P_Label (5.1)
1113
1114 -- Loop label is parsed by P_Loop_Statement (5.5), P_For_Statement (5.5)
1115 -- or P_While_Statement (5.5)
1116
1117 -- Block label is parsed by P_Begin_Statement (5.6) or
1118 -- P_Declare_Statement (5.6)
1119
1120 -------------------------------
1121 -- 5.2 Assignment Statement --
1122 -------------------------------
1123
1124 -- ASSIGNMENT_STATEMENT ::=
1125 -- variable_NAME := EXPRESSION;
1126
1127 -- Error recovery: can raise Error_Resync
1128
1129 function P_Assignment_Statement (LHS : Node_Id) return Node_Id is
1130 Assign_Node : Node_Id;
1131
1132 begin
1133 Assign_Node := New_Node (N_Assignment_Statement, Prev_Token_Ptr);
1134 Current_Assign_Node := Assign_Node;
1135 Set_Name (Assign_Node, LHS);
1136 Set_Expression (Assign_Node, P_Expression_No_Right_Paren);
1137 TF_Semicolon;
1138 Current_Assign_Node := Empty;
1139 return Assign_Node;
1140 end P_Assignment_Statement;
1141
1142 -----------------------
1143 -- 5.3 If Statement --
1144 -----------------------
1145
1146 -- IF_STATEMENT ::=
1147 -- if CONDITION then
1148 -- SEQUENCE_OF_STATEMENTS
1149 -- {elsif CONDITION then
1150 -- SEQUENCE_OF_STATEMENTS}
1151 -- [else
1152 -- SEQUENCE_OF_STATEMENTS]
1153 -- end if;
1154
1155 -- The caller has checked that the initial token is IF (or in the error
1156 -- case of a mysterious THEN, the initial token may simply be THEN, in
1157 -- which case, no condition (or IF) was scanned).
1158
1159 -- Error recovery: can raise Error_Resync
1160
1161 function P_If_Statement return Node_Id is
1162 If_Node : Node_Id;
1163 Elsif_Node : Node_Id;
1164 Loc : Source_Ptr;
1165
1166 procedure Add_Elsif_Part;
1167 -- An internal procedure used to scan out a single ELSIF part. On entry
1168 -- the ELSIF (or an ELSE which has been determined should be ELSIF) is
1169 -- scanned out and is in Prev_Token.
1170
1171 procedure Check_If_Column;
1172 -- An internal procedure used to check that THEN, ELSE, or ELSIF
1173 -- appear in the right place if column checking is enabled (i.e. if
1174 -- they are the first token on the line, then they must appear in
1175 -- the same column as the opening IF).
1176
1177 procedure Check_Then_Column;
1178 -- This procedure carries out the style checks for a THEN token
1179 -- Note that the caller has set Loc to the Source_Ptr value for
1180 -- the previous IF or ELSIF token.
1181
1182 function Else_Should_Be_Elsif return Boolean;
1183 -- An internal routine used to do a special error recovery check when
1184 -- an ELSE is encountered. It determines if the ELSE should be treated
1185 -- as an ELSIF. A positive decision (TRUE returned, is made if the ELSE
1186 -- is followed by a sequence of tokens, starting on the same line as
1187 -- the ELSE, which are not expression terminators, followed by a THEN.
1188 -- On entry, the ELSE has been scanned out.
1189
1190 procedure Add_Elsif_Part is
1191 begin
1192 if No (Elsif_Parts (If_Node)) then
1193 Set_Elsif_Parts (If_Node, New_List);
1194 end if;
1195
1196 Elsif_Node := New_Node (N_Elsif_Part, Prev_Token_Ptr);
1197 Loc := Prev_Token_Ptr;
1198 Set_Condition (Elsif_Node, P_Condition);
1199 Check_Then_Column;
1200 Then_Scan;
1201 Set_Then_Statements
1202 (Elsif_Node, P_Sequence_Of_Statements (SS_Eftm_Eltm_Sreq));
1203 Append (Elsif_Node, Elsif_Parts (If_Node));
1204 end Add_Elsif_Part;
1205
1206 procedure Check_If_Column is
1207 begin
1208 if RM_Column_Check and then Token_Is_At_Start_Of_Line
1209 and then Start_Column /= Scopes (Scope.Last).Ecol
1210 then
1211 Error_Msg_Col := Scopes (Scope.Last).Ecol;
1212 Error_Msg_SC ("(style) this token should be@?l?");
1213 end if;
1214 end Check_If_Column;
1215
1216 procedure Check_Then_Column is
1217 begin
1218 if Token = Tok_Then then
1219 Check_If_Column;
1220
1221 if Style_Check then
1222 Style.Check_Then (Loc);
1223 end if;
1224 end if;
1225 end Check_Then_Column;
1226
1227 function Else_Should_Be_Elsif return Boolean is
1228 Scan_State : Saved_Scan_State;
1229
1230 begin
1231 if Token_Is_At_Start_Of_Line then
1232 return False;
1233
1234 else
1235 Save_Scan_State (Scan_State);
1236
1237 loop
1238 if Token in Token_Class_Eterm then
1239 Restore_Scan_State (Scan_State);
1240 return False;
1241 else
1242 Scan; -- past non-expression terminating token
1243
1244 if Token = Tok_Then then
1245 Restore_Scan_State (Scan_State);
1246 return True;
1247 end if;
1248 end if;
1249 end loop;
1250 end if;
1251 end Else_Should_Be_Elsif;
1252
1253 -- Start of processing for P_If_Statement
1254
1255 begin
1256 If_Node := New_Node (N_If_Statement, Token_Ptr);
1257
1258 Push_Scope_Stack;
1259 Scopes (Scope.Last).Etyp := E_If;
1260 Scopes (Scope.Last).Ecol := Start_Column;
1261 Scopes (Scope.Last).Sloc := Token_Ptr;
1262 Scopes (Scope.Last).Labl := Error;
1263 Scopes (Scope.Last).Node := If_Node;
1264
1265 if Token = Tok_If then
1266 Loc := Token_Ptr;
1267 Scan; -- past IF
1268 Set_Condition (If_Node, P_Condition);
1269
1270 -- Deal with misuse of IF expression => used instead
1271 -- of WHEN expression =>
1272
1273 if Token = Tok_Arrow then
1274 Error_Msg_SC -- CODEFIX
1275 ("THEN expected");
1276 Scan; -- past the arrow
1277 Pop_Scope_Stack; -- remove unneeded entry
1278 raise Error_Resync;
1279 end if;
1280
1281 Check_Then_Column;
1282
1283 else
1284 Error_Msg_SC ("no IF for this THEN");
1285 Set_Condition (If_Node, Error);
1286 end if;
1287
1288 Then_Scan;
1289
1290 Set_Then_Statements
1291 (If_Node, P_Sequence_Of_Statements (SS_Eftm_Eltm_Sreq));
1292
1293 -- This loop scans out else and elsif parts
1294
1295 loop
1296 if Token = Tok_Elsif then
1297 Check_If_Column;
1298
1299 if Present (Else_Statements (If_Node)) then
1300 Error_Msg_SP ("ELSIF cannot appear after ELSE");
1301 end if;
1302
1303 Scan; -- past ELSIF
1304 Add_Elsif_Part;
1305
1306 elsif Token = Tok_Else then
1307 Check_If_Column;
1308 Scan; -- past ELSE
1309
1310 if Else_Should_Be_Elsif then
1311 Error_Msg_SP -- CODEFIX
1312 ("ELSE should be ELSIF");
1313 Add_Elsif_Part;
1314
1315 else
1316 -- Here we have an else that really is an else
1317
1318 if Present (Else_Statements (If_Node)) then
1319 Error_Msg_SP ("only one ELSE part allowed");
1320 Append_List
1321 (P_Sequence_Of_Statements (SS_Eftm_Eltm_Sreq),
1322 Else_Statements (If_Node));
1323 else
1324 Set_Else_Statements
1325 (If_Node, P_Sequence_Of_Statements (SS_Eftm_Eltm_Sreq));
1326 end if;
1327 end if;
1328
1329 -- If anything other than ELSE or ELSIF, exit the loop. The token
1330 -- had better be END (and in fact it had better be END IF), but
1331 -- we will let End_Statements take care of checking that.
1332
1333 else
1334 exit;
1335 end if;
1336 end loop;
1337
1338 End_Statements;
1339 return If_Node;
1340
1341 end P_If_Statement;
1342
1343 --------------------
1344 -- 5.3 Condition --
1345 --------------------
1346
1347 -- CONDITION ::= boolean_EXPRESSION
1348
1349 function P_Condition return Node_Id is
1350 begin
1351 return P_Condition (P_Expression_No_Right_Paren);
1352 end P_Condition;
1353
1354 function P_Condition (Cond : Node_Id) return Node_Id is
1355 begin
1356 -- It is never possible for := to follow a condition, so if we get
1357 -- a := we assume it is a mistyped equality. Note that we do not try
1358 -- to reconstruct the tree correctly in this case, but we do at least
1359 -- give an accurate error message.
1360
1361 if Token = Tok_Colon_Equal then
1362 while Token = Tok_Colon_Equal loop
1363 Error_Msg_SC -- CODEFIX
1364 (""":="" should be ""=""");
1365 Scan; -- past junk :=
1366 Discard_Junk_Node (P_Expression_No_Right_Paren);
1367 end loop;
1368
1369 return Cond;
1370
1371 -- Otherwise check for redundant parentheses
1372
1373 else
1374 if Style_Check then
1375 Style.Check_Xtra_Parens (Cond);
1376
1377 -- When the condition is an operator then examine parentheses
1378 -- surrounding the condition's operands - taking care to avoid
1379 -- flagging operands which themselves are operators since they
1380 -- may be required for resolution or precedence.
1381
1382 if Nkind (Cond) in N_Op
1383 | N_Membership_Test
1384 | N_Short_Circuit
1385 and then Nkind (Right_Opnd (Cond)) not in N_Op
1386 | N_Membership_Test
1387 | N_Short_Circuit
1388 then
1389 Style.Check_Xtra_Parens (Right_Opnd (Cond));
1390 end if;
1391
1392 if Nkind (Cond) in N_Binary_Op
1393 | N_Membership_Test
1394 | N_Short_Circuit
1395 and then Nkind (Left_Opnd (Cond)) not in N_Op
1396 | N_Membership_Test
1397 | N_Short_Circuit
1398 then
1399 Style.Check_Xtra_Parens (Left_Opnd (Cond));
1400 end if;
1401 end if;
1402
1403 -- And return the result
1404
1405 return Cond;
1406 end if;
1407 end P_Condition;
1408
1409 -------------------------
1410 -- 5.4 Case Statement --
1411 -------------------------
1412
1413 -- CASE_STATEMENT ::=
1414 -- case EXPRESSION is
1415 -- CASE_STATEMENT_ALTERNATIVE
1416 -- {CASE_STATEMENT_ALTERNATIVE}
1417 -- end case;
1418
1419 -- The caller has checked that the first token is CASE
1420
1421 -- Can raise Error_Resync
1422
1423 function P_Case_Statement return Node_Id is
1424 Case_Node : Node_Id;
1425 Expr : Node_Id;
1426 Alternatives_List : List_Id;
1427 First_When_Loc : Source_Ptr;
1428
1429 begin
1430 Case_Node := New_Node (N_Case_Statement, Token_Ptr);
1431
1432 Push_Scope_Stack;
1433 Scopes (Scope.Last).Etyp := E_Case;
1434 Scopes (Scope.Last).Ecol := Start_Column;
1435 Scopes (Scope.Last).Sloc := Token_Ptr;
1436 Scopes (Scope.Last).Labl := Error;
1437 Scopes (Scope.Last).Node := Case_Node;
1438
1439 Scan; -- past CASE
1440
1441 Expr := P_Expression_No_Right_Paren;
1442
1443 if Style_Check then
1444 Style.Check_Xtra_Parens (Expr);
1445 end if;
1446
1447 Set_Expression (Case_Node, Expr);
1448 TF_Is;
1449
1450 -- Prepare to parse case statement alternatives
1451
1452 Alternatives_List := New_List;
1453 P_Pragmas_Opt (Alternatives_List);
1454 First_When_Loc := Token_Ptr;
1455
1456 -- Loop through case statement alternatives
1457
1458 loop
1459 -- If we have a WHEN or OTHERS, then that's fine keep going. Note
1460 -- that it is a semantic check to ensure the proper use of OTHERS
1461
1462 if Token in Tok_When | Tok_Others then
1463 Append (P_Case_Statement_Alternative, Alternatives_List);
1464
1465 -- If we have an END, then probably we are at the end of the case
1466 -- but we only exit if Check_End thinks the END was reasonable.
1467
1468 elsif Token = Tok_End then
1469 exit when Check_End;
1470
1471 -- Here if token is other than WHEN, OTHERS or END. We definitely
1472 -- have an error, but the question is whether or not to get out of
1473 -- the case statement. We don't want to get out early, or we will
1474 -- get a slew of junk error messages for subsequent when tokens.
1475
1476 -- If the token is not at the start of the line, or if it is indented
1477 -- with respect to the current case statement, then the best guess is
1478 -- that we are still supposed to be inside the case statement. We
1479 -- complain about the missing WHEN, and discard the junk statements.
1480
1481 elsif not Token_Is_At_Start_Of_Line
1482 or else Start_Column > Scopes (Scope.Last).Ecol
1483 then
1484 Error_Msg_BC ("WHEN (case statement alternative) expected");
1485
1486 -- Here is a possibility for infinite looping if we don't make
1487 -- progress. So try to process statements, otherwise exit
1488
1489 declare
1490 Error_Ptr : constant Source_Ptr := Scan_Ptr;
1491 begin
1492 Discard_Junk_List (P_Sequence_Of_Statements (SS_Whtm));
1493 exit when Scan_Ptr = Error_Ptr and then Check_End;
1494 end;
1495
1496 -- Here we have a junk token at the start of the line and it is
1497 -- not indented. If Check_End thinks there is a missing END, then
1498 -- we will get out of the case, otherwise we keep going.
1499
1500 else
1501 exit when Check_End;
1502 end if;
1503 end loop;
1504
1505 -- Make sure we have at least one alternative
1506
1507 if No (First_Non_Pragma (Alternatives_List)) then
1508 Error_Msg
1509 ("WHEN expected, must have at least one alternative in case",
1510 First_When_Loc);
1511 return Error;
1512
1513 else
1514 Set_Alternatives (Case_Node, Alternatives_List);
1515 return Case_Node;
1516 end if;
1517 end P_Case_Statement;
1518
1519 -------------------------------------
1520 -- 5.4 Case Statement Alternative --
1521 -------------------------------------
1522
1523 -- CASE_STATEMENT_ALTERNATIVE ::=
1524 -- when DISCRETE_CHOICE_LIST =>
1525 -- SEQUENCE_OF_STATEMENTS
1526
1527 -- The caller has checked that the initial token is WHEN or OTHERS
1528 -- Error recovery: can raise Error_Resync
1529
1530 function P_Case_Statement_Alternative return Node_Id is
1531 Case_Alt_Node : Node_Id;
1532
1533 begin
1534 if Style_Check then
1535 Style.Check_Indentation;
1536 end if;
1537
1538 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Token_Ptr);
1539 T_When; -- past WHEN (or give error in OTHERS case)
1540 Set_Discrete_Choices (Case_Alt_Node, P_Discrete_Choice_List);
1541 TF_Arrow;
1542 Set_Statements (Case_Alt_Node, P_Sequence_Of_Statements (SS_Sreq_Whtm));
1543 return Case_Alt_Node;
1544 end P_Case_Statement_Alternative;
1545
1546 -------------------------
1547 -- 5.5 Loop Statement --
1548 -------------------------
1549
1550 -- LOOP_STATEMENT ::=
1551 -- [LOOP_STATEMENT_IDENTIFIER:]
1552 -- [ITERATION_SCHEME] loop
1553 -- SEQUENCE_OF_STATEMENTS
1554 -- end loop [loop_IDENTIFIER];
1555
1556 -- ITERATION_SCHEME ::=
1557 -- while CONDITION
1558 -- | for LOOP_PARAMETER_SPECIFICATION
1559
1560 -- The parsing of loop statements is handled by one of three functions
1561 -- P_Loop_Statement, P_For_Statement or P_While_Statement depending
1562 -- on the initial keyword in the construct (excluding the identifier)
1563
1564 -- P_Loop_Statement
1565
1566 -- This function parses the case where no iteration scheme is present
1567
1568 -- The caller has checked that the initial token is LOOP. The parameter
1569 -- is the node identifiers for the loop label if any (or is set to Empty
1570 -- if there is no loop label).
1571
1572 -- Error recovery : cannot raise Error_Resync
1573
1574 function P_Loop_Statement (Loop_Name : Node_Id := Empty) return Node_Id is
1575 Loop_Node : Node_Id;
1576 Created_Name : Node_Id;
1577
1578 begin
1579 Push_Scope_Stack;
1580 Scopes (Scope.Last).Labl := Loop_Name;
1581 Scopes (Scope.Last).Ecol := Start_Column;
1582 Scopes (Scope.Last).Sloc := Token_Ptr;
1583 Scopes (Scope.Last).Etyp := E_Loop;
1584
1585 Loop_Node := New_Node (N_Loop_Statement, Token_Ptr);
1586 TF_Loop;
1587
1588 if No (Loop_Name) then
1589 Created_Name :=
1590 Make_Identifier (Sloc (Loop_Node), Set_Loop_Block_Name ('L'));
1591 Set_Comes_From_Source (Created_Name, False);
1592 Set_Has_Created_Identifier (Loop_Node, True);
1593 Set_Identifier (Loop_Node, Created_Name);
1594 Scopes (Scope.Last).Labl := Created_Name;
1595 else
1596 Set_Identifier (Loop_Node, Loop_Name);
1597 end if;
1598
1599 Append_Elmt (Loop_Node, Label_List);
1600 Set_Statements (Loop_Node, P_Sequence_Of_Statements (SS_Sreq));
1601 End_Statements (Loop_Node);
1602 return Loop_Node;
1603 end P_Loop_Statement;
1604
1605 -- P_For_Statement
1606
1607 -- This function parses a loop statement with a FOR iteration scheme
1608
1609 -- The caller has checked that the initial token is FOR. The parameter
1610 -- is the node identifier for the block label if any (or is set to Empty
1611 -- if there is no block label).
1612
1613 -- Note: the caller fills in the Identifier field if a label was present
1614
1615 -- Error recovery: can raise Error_Resync
1616
1617 function P_For_Statement (Loop_Name : Node_Id := Empty) return Node_Id is
1618 Loop_Node : Node_Id;
1619 Iter_Scheme_Node : Node_Id;
1620 Loop_For_Flag : Boolean;
1621 Created_Name : Node_Id;
1622 Spec : Node_Id;
1623
1624 begin
1625 Push_Scope_Stack;
1626 Scopes (Scope.Last).Labl := Loop_Name;
1627 Scopes (Scope.Last).Ecol := Start_Column;
1628 Scopes (Scope.Last).Sloc := Token_Ptr;
1629 Scopes (Scope.Last).Etyp := E_Loop;
1630
1631 Loop_For_Flag := (Prev_Token = Tok_Loop);
1632 Scan; -- past FOR
1633 Iter_Scheme_Node := New_Node (N_Iteration_Scheme, Token_Ptr);
1634 Spec := P_Loop_Parameter_Specification;
1635
1636 if Nkind (Spec) = N_Loop_Parameter_Specification then
1637 Set_Loop_Parameter_Specification (Iter_Scheme_Node, Spec);
1638 else
1639 Set_Iterator_Specification (Iter_Scheme_Node, Spec);
1640 end if;
1641
1642 -- The following is a special test so that a miswritten for loop such
1643 -- as "loop for I in 1..10;" is handled nicely, without making an extra
1644 -- entry in the scope stack. We don't bother to actually fix up the
1645 -- tree in this case since it's not worth the effort. Instead we just
1646 -- eat up the loop junk, leaving the entry for what now looks like an
1647 -- unmodified loop intact.
1648
1649 if Loop_For_Flag and then Token = Tok_Semicolon then
1650 Error_Msg_SC ("LOOP belongs here, not before FOR");
1651 Pop_Scope_Stack;
1652 return Error;
1653
1654 -- Normal case
1655
1656 else
1657 Loop_Node := New_Node (N_Loop_Statement, Token_Ptr);
1658
1659 if No (Loop_Name) then
1660 Created_Name :=
1661 Make_Identifier (Sloc (Loop_Node), Set_Loop_Block_Name ('L'));
1662 Set_Comes_From_Source (Created_Name, False);
1663 Set_Has_Created_Identifier (Loop_Node, True);
1664 Set_Identifier (Loop_Node, Created_Name);
1665 Scopes (Scope.Last).Labl := Created_Name;
1666 else
1667 Set_Identifier (Loop_Node, Loop_Name);
1668 end if;
1669
1670 TF_Loop;
1671 Set_Statements (Loop_Node, P_Sequence_Of_Statements (SS_Sreq));
1672 End_Statements (Loop_Node);
1673 Set_Iteration_Scheme (Loop_Node, Iter_Scheme_Node);
1674 Append_Elmt (Loop_Node, Label_List);
1675 return Loop_Node;
1676 end if;
1677 end P_For_Statement;
1678
1679 -- P_While_Statement
1680
1681 -- This procedure scans a loop statement with a WHILE iteration scheme
1682
1683 -- The caller has checked that the initial token is WHILE. The parameter
1684 -- is the node identifier for the block label if any (or is set to Empty
1685 -- if there is no block label).
1686
1687 -- Error recovery: cannot raise Error_Resync
1688
1689 function P_While_Statement (Loop_Name : Node_Id := Empty) return Node_Id is
1690 Loop_Node : Node_Id;
1691 Iter_Scheme_Node : Node_Id;
1692 Loop_While_Flag : Boolean;
1693 Created_Name : Node_Id;
1694
1695 begin
1696 Push_Scope_Stack;
1697 Scopes (Scope.Last).Labl := Loop_Name;
1698 Scopes (Scope.Last).Ecol := Start_Column;
1699 Scopes (Scope.Last).Sloc := Token_Ptr;
1700 Scopes (Scope.Last).Etyp := E_Loop;
1701
1702 Loop_While_Flag := (Prev_Token = Tok_Loop);
1703 Iter_Scheme_Node := New_Node (N_Iteration_Scheme, Token_Ptr);
1704 Scan; -- past WHILE
1705 Set_Condition (Iter_Scheme_Node, P_Condition);
1706
1707 -- The following is a special test so that a miswritten for loop such
1708 -- as "loop while I > 10;" is handled nicely, without making an extra
1709 -- entry in the scope stack. We don't bother to actually fix up the
1710 -- tree in this case since it's not worth the effort. Instead we just
1711 -- eat up the loop junk, leaving the entry for what now looks like an
1712 -- unmodified loop intact.
1713
1714 if Loop_While_Flag and then Token = Tok_Semicolon then
1715 Error_Msg_SC ("LOOP belongs here, not before WHILE");
1716 Pop_Scope_Stack;
1717 return Error;
1718
1719 -- Normal case
1720
1721 else
1722 Loop_Node := New_Node (N_Loop_Statement, Token_Ptr);
1723 TF_Loop;
1724
1725 if No (Loop_Name) then
1726 Created_Name :=
1727 Make_Identifier (Sloc (Loop_Node), Set_Loop_Block_Name ('L'));
1728 Set_Comes_From_Source (Created_Name, False);
1729 Set_Has_Created_Identifier (Loop_Node, True);
1730 Set_Identifier (Loop_Node, Created_Name);
1731 Scopes (Scope.Last).Labl := Created_Name;
1732 else
1733 Set_Identifier (Loop_Node, Loop_Name);
1734 end if;
1735
1736 Set_Statements (Loop_Node, P_Sequence_Of_Statements (SS_Sreq));
1737 End_Statements (Loop_Node);
1738 Set_Iteration_Scheme (Loop_Node, Iter_Scheme_Node);
1739 Append_Elmt (Loop_Node, Label_List);
1740 return Loop_Node;
1741 end if;
1742 end P_While_Statement;
1743
1744 ---------------------------------------
1745 -- 5.5 Loop Parameter Specification --
1746 ---------------------------------------
1747
1748 -- LOOP_PARAMETER_SPECIFICATION ::=
1749 -- DEFINING_IDENTIFIER in [reverse] DISCRETE_SUBTYPE_DEFINITION
1750 -- [Iterator_Filter]
1751
1752 -- Error recovery: cannot raise Error_Resync
1753
1754 function P_Loop_Parameter_Specification return Node_Id is
1755 Loop_Param_Specification_Node : Node_Id;
1756
1757 ID_Node : Node_Id;
1758 Scan_State : Saved_Scan_State;
1759
1760 begin
1761 Save_Scan_State (Scan_State);
1762 ID_Node := P_Defining_Identifier (C_In);
1763
1764 -- If the next token is OF, it indicates an Ada 2012 iterator. If the
1765 -- next token is a colon, this is also an Ada 2012 iterator, including
1766 -- a subtype indication for the loop parameter. Otherwise we parse the
1767 -- construct as a loop parameter specification. Note that the form
1768 -- "for A in B" is ambiguous, and must be resolved semantically: if B
1769 -- is a discrete subtype this is a loop specification, but if it is an
1770 -- expression it is an iterator specification. Ambiguity is resolved
1771 -- during analysis of the loop parameter specification.
1772
1773 if Token in Tok_Of | Tok_Colon then
1774 Error_Msg_Ada_2012_Feature ("iterator", Token_Ptr);
1775 return P_Iterator_Specification (ID_Node);
1776 end if;
1777
1778 -- The span of the Loop_Parameter_Specification starts at the
1779 -- defining identifier.
1780
1781 Loop_Param_Specification_Node :=
1782 New_Node (N_Loop_Parameter_Specification, Sloc (ID_Node));
1783 Set_Defining_Identifier (Loop_Param_Specification_Node, ID_Node);
1784
1785 if Token = Tok_Left_Paren then
1786 Error_Msg_SC ("subscripted loop parameter not allowed");
1787 Restore_Scan_State (Scan_State);
1788 Discard_Junk_Node (P_Name);
1789
1790 elsif Token = Tok_Dot then
1791 Error_Msg_SC ("selected loop parameter not allowed");
1792 Restore_Scan_State (Scan_State);
1793 Discard_Junk_Node (P_Name);
1794 end if;
1795
1796 T_In;
1797
1798 if Token = Tok_Reverse then
1799 Scan; -- past REVERSE
1800 Set_Reverse_Present (Loop_Param_Specification_Node, True);
1801 end if;
1802
1803 Set_Discrete_Subtype_Definition
1804 (Loop_Param_Specification_Node, P_Discrete_Subtype_Definition);
1805
1806 if Token = Tok_When then
1807 Error_Msg_Ada_2022_Feature ("iterator filter", Token_Ptr);
1808
1809 Scan; -- past WHEN
1810 Set_Iterator_Filter
1811 (Loop_Param_Specification_Node, P_Condition);
1812 end if;
1813
1814 return Loop_Param_Specification_Node;
1815
1816 exception
1817 when Error_Resync =>
1818 return Error;
1819 end P_Loop_Parameter_Specification;
1820
1821 ----------------------------------
1822 -- 5.5.1 Iterator_Specification --
1823 ----------------------------------
1824
1825 function P_Iterator_Specification (Def_Id : Node_Id) return Node_Id is
1826 Node1 : Node_Id;
1827
1828 begin
1829 Node1 := New_Node (N_Iterator_Specification, Sloc (Def_Id));
1830 Set_Defining_Identifier (Node1, Def_Id);
1831
1832 if Token = Tok_Colon then
1833 Scan; -- past :
1834
1835 if Token = Tok_Access then
1836 Error_Msg_Ada_2022_Feature
1837 ("access definition in loop parameter", Token_Ptr);
1838 Set_Subtype_Indication (Node1, P_Access_Definition (False));
1839
1840 else
1841 Set_Subtype_Indication (Node1, P_Subtype_Indication);
1842 end if;
1843 end if;
1844
1845 if Token = Tok_Of then
1846 Set_Of_Present (Node1);
1847 Scan; -- past OF
1848
1849 elsif Token = Tok_In then
1850 Scan; -- past IN
1851
1852 else
1853 return Error;
1854 end if;
1855
1856 if Token = Tok_Reverse then
1857 Scan; -- past REVERSE
1858 Set_Reverse_Present (Node1, True);
1859 end if;
1860
1861 Set_Name (Node1, P_Name);
1862
1863 if Token = Tok_When then
1864 Error_Msg_Ada_2022_Feature ("iterator filter", Token_Ptr);
1865
1866 Scan; -- past WHEN
1867 Set_Iterator_Filter
1868 (Node1, P_Condition);
1869 end if;
1870
1871 return Node1;
1872 end P_Iterator_Specification;
1873
1874 --------------------------
1875 -- 5.6 Block Statement --
1876 --------------------------
1877
1878 -- BLOCK_STATEMENT ::=
1879 -- [block_STATEMENT_IDENTIFIER:]
1880 -- [declare
1881 -- DECLARATIVE_PART]
1882 -- begin
1883 -- HANDLED_SEQUENCE_OF_STATEMENTS
1884 -- end [block_IDENTIFIER];
1885
1886 -- The parsing of block statements is handled by one of the two functions
1887 -- P_Declare_Statement or P_Begin_Statement depending on whether or not
1888 -- a declare section is present
1889
1890 -- P_Declare_Statement
1891
1892 -- This function parses a block statement with DECLARE present
1893
1894 -- The caller has checked that the initial token is DECLARE
1895
1896 -- Error recovery: cannot raise Error_Resync
1897
1898 function P_Declare_Statement
1899 (Block_Name : Node_Id := Empty)
1900 return Node_Id
1901 is
1902 Block_Node : Node_Id;
1903 Created_Name : Node_Id;
1904
1905 begin
1906 Block_Node := New_Node (N_Block_Statement, Token_Ptr);
1907
1908 Push_Scope_Stack;
1909 Scopes (Scope.Last).Etyp := E_Name;
1910 Scopes (Scope.Last).Lreq := Present (Block_Name);
1911 Scopes (Scope.Last).Ecol := Start_Column;
1912 Scopes (Scope.Last).Labl := Block_Name;
1913 Scopes (Scope.Last).Sloc := Token_Ptr;
1914
1915 Scan; -- past DECLARE
1916
1917 if No (Block_Name) then
1918 Created_Name :=
1919 Make_Identifier (Sloc (Block_Node), Set_Loop_Block_Name ('B'));
1920 Set_Comes_From_Source (Created_Name, False);
1921 Set_Has_Created_Identifier (Block_Node, True);
1922 Set_Identifier (Block_Node, Created_Name);
1923 Scopes (Scope.Last).Labl := Created_Name;
1924 else
1925 Set_Identifier (Block_Node, Block_Name);
1926 end if;
1927
1928 Append_Elmt (Block_Node, Label_List);
1929 Parse_Decls_Begin_End (Block_Node);
1930 return Block_Node;
1931 end P_Declare_Statement;
1932
1933 -- P_Begin_Statement
1934
1935 -- This function parses a block statement with no DECLARE present
1936
1937 -- The caller has checked that the initial token is BEGIN
1938
1939 -- Error recovery: cannot raise Error_Resync
1940
1941 function P_Begin_Statement
1942 (Block_Name : Node_Id := Empty)
1943 return Node_Id
1944 is
1945 Block_Node : Node_Id;
1946 Created_Name : Node_Id;
1947
1948 begin
1949 Block_Node := New_Node (N_Block_Statement, Token_Ptr);
1950
1951 Push_Scope_Stack;
1952 Scopes (Scope.Last).Etyp := E_Name;
1953 Scopes (Scope.Last).Lreq := Present (Block_Name);
1954 Scopes (Scope.Last).Ecol := Start_Column;
1955 Scopes (Scope.Last).Labl := Block_Name;
1956 Scopes (Scope.Last).Sloc := Token_Ptr;
1957
1958 if No (Block_Name) then
1959 Created_Name :=
1960 Make_Identifier (Sloc (Block_Node), Set_Loop_Block_Name ('B'));
1961 Set_Comes_From_Source (Created_Name, False);
1962 Set_Has_Created_Identifier (Block_Node, True);
1963 Set_Identifier (Block_Node, Created_Name);
1964 Scopes (Scope.Last).Labl := Created_Name;
1965 else
1966 Set_Identifier (Block_Node, Block_Name);
1967 end if;
1968
1969 Append_Elmt (Block_Node, Label_List);
1970
1971 Scopes (Scope.Last).Ecol := Start_Column;
1972 Scopes (Scope.Last).Sloc := Token_Ptr;
1973 Scan; -- past BEGIN
1974 Set_Handled_Statement_Sequence
1975 (Block_Node, P_Handled_Sequence_Of_Statements);
1976 End_Statements (Handled_Statement_Sequence (Block_Node));
1977 return Block_Node;
1978 end P_Begin_Statement;
1979
1980 -------------------------
1981 -- 5.7 Exit Statement --
1982 -------------------------
1983
1984 -- EXIT_STATEMENT ::=
1985 -- exit [loop_NAME] [when CONDITION];
1986
1987 -- The caller has checked that the initial token is EXIT
1988
1989 -- Error recovery: can raise Error_Resync
1990
1991 function P_Exit_Statement return Node_Id is
1992 Exit_Node : Node_Id;
1993
1994 -- Start of processing for P_Exit_Statement
1995
1996 begin
1997 Exit_Node := New_Node (N_Exit_Statement, Token_Ptr);
1998 Scan; -- past EXIT
1999
2000 if Token = Tok_Identifier then
2001 Set_Name (Exit_Node, P_Qualified_Simple_Name);
2002
2003 elsif Style_Check then
2004 -- This EXIT has no name, so check that
2005 -- the innermost loop is unnamed too.
2006
2007 Check_No_Exit_Name :
2008 for J in reverse 1 .. Scope.Last loop
2009 if Scopes (J).Etyp = E_Loop then
2010 if Present (Scopes (J).Labl)
2011 and then Comes_From_Source (Scopes (J).Labl)
2012 then
2013 -- Innermost loop in fact had a name, style check fails
2014
2015 Style.No_Exit_Name (Scopes (J).Labl);
2016 end if;
2017
2018 exit Check_No_Exit_Name;
2019 end if;
2020 end loop Check_No_Exit_Name;
2021 end if;
2022
2023 if Token = Tok_When and then not Missing_Semicolon_On_When then
2024 Scan; -- past WHEN
2025 Set_Condition (Exit_Node, P_Condition);
2026
2027 -- Allow IF instead of WHEN, giving error message
2028
2029 elsif Token = Tok_If then
2030 T_When;
2031 Scan; -- past IF used in place of WHEN
2032 Set_Condition (Exit_Node, P_Expression_No_Right_Paren);
2033 end if;
2034
2035 TF_Semicolon;
2036 return Exit_Node;
2037 end P_Exit_Statement;
2038
2039 -------------------------
2040 -- 5.8 Goto Statement --
2041 -------------------------
2042
2043 -- GOTO_STATEMENT ::= goto label_NAME;
2044
2045 -- The caller has checked that the initial token is GOTO (or TO in the
2046 -- error case where GO and TO were incorrectly separated).
2047
2048 -- Error recovery: can raise Error_Resync
2049
2050 function P_Goto_Statement return Node_Id is
2051 Goto_Node : Node_Id;
2052
2053 begin
2054 Goto_Node := New_Node (N_Goto_Statement, Token_Ptr);
2055 Scan; -- past GOTO (or TO)
2056 Set_Name (Goto_Node, P_Qualified_Simple_Name_Resync);
2057 Append_Elmt (Goto_Node, Goto_List);
2058
2059 if Token = Tok_When then
2060 Error_Msg_GNAT_Extension ("goto when statement", Token_Ptr);
2061
2062 Scan; -- past WHEN
2063 Mutate_Nkind (Goto_Node, N_Goto_When_Statement);
2064 Set_Condition (Goto_Node, P_Expression_No_Right_Paren);
2065 end if;
2066
2067 TF_Semicolon;
2068 return Goto_Node;
2069 end P_Goto_Statement;
2070
2071 ---------------------------
2072 -- Parse_Decls_Begin_End --
2073 ---------------------------
2074
2075 -- This function parses the construct:
2076
2077 -- DECLARATIVE_PART
2078 -- begin
2079 -- HANDLED_SEQUENCE_OF_STATEMENTS
2080 -- end [NAME];
2081
2082 -- The caller has built the scope stack entry, and created the node to
2083 -- whose Declarations and Handled_Statement_Sequence fields are to be
2084 -- set. On return these fields are filled in (except in the case of a
2085 -- task body, where the handled statement sequence is optional, and may
2086 -- thus be Empty), and the scan is positioned past the End sequence.
2087
2088 -- If the BEGIN is missing, then the parent node is used to help construct
2089 -- an appropriate missing BEGIN message. Possibilities for the parent are:
2090
2091 -- N_Block_Statement declare block
2092 -- N_Entry_Body entry body
2093 -- N_Package_Body package body (begin part optional)
2094 -- N_Subprogram_Body procedure or function body
2095 -- N_Task_Body task body
2096
2097 -- Note: in the case of a block statement, there is definitely a DECLARE
2098 -- present (because a Begin statement without a DECLARE is handled by the
2099 -- P_Begin_Statement procedure, which does not call Parse_Decls_Begin_End.
2100
2101 -- Error recovery: cannot raise Error_Resync
2102
2103 procedure Parse_Decls_Begin_End (Parent : Node_Id) is
2104 Body_Decl : Node_Id;
2105 Decls : List_Id;
2106 Parent_Nkind : Node_Kind;
2107 Spec_Node : Node_Id;
2108 HSS : Node_Id;
2109
2110 procedure Missing_Begin (Msg : String);
2111 -- Called to post a missing begin message. In the normal case this is
2112 -- posted at the start of the current token. A special case arises when
2113 -- P_Declarative_Items has previously found a missing begin, in which
2114 -- case we replace the original error message.
2115
2116 procedure Set_Null_HSS (Parent : Node_Id);
2117 -- Construct an empty handled statement sequence and install in Parent
2118 -- Leaves HSS set to reference the newly constructed statement sequence.
2119
2120 -------------------
2121 -- Missing_Begin --
2122 -------------------
2123
2124 procedure Missing_Begin (Msg : String) is
2125 begin
2126 if Missing_Begin_Msg = No_Error_Msg then
2127 Error_Msg_BC (Msg);
2128 else
2129 Change_Error_Text (Missing_Begin_Msg, Msg);
2130
2131 -- Purge any messages issued after than, since a missing begin
2132 -- can cause a lot of havoc, and it is better not to dump these
2133 -- cascaded messages on the user.
2134
2135 Purge_Messages (Get_Location (Missing_Begin_Msg), Prev_Token_Ptr);
2136 end if;
2137 end Missing_Begin;
2138
2139 ------------------
2140 -- Set_Null_HSS --
2141 ------------------
2142
2143 procedure Set_Null_HSS (Parent : Node_Id) is
2144 Null_Stm : Node_Id;
2145
2146 begin
2147 Null_Stm :=
2148 Make_Null_Statement (Token_Ptr);
2149 Set_Comes_From_Source (Null_Stm, False);
2150
2151 HSS :=
2152 Make_Handled_Sequence_Of_Statements (Token_Ptr,
2153 Statements => New_List (Null_Stm));
2154 Set_Comes_From_Source (HSS, False);
2155
2156 Set_Handled_Statement_Sequence (Parent, HSS);
2157 end Set_Null_HSS;
2158
2159 -- Start of processing for Parse_Decls_Begin_End
2160
2161 begin
2162 Decls := P_Declarative_Part;
2163
2164 if Ada_Version = Ada_83 then
2165 Check_Later_Vs_Basic_Declarations (Decls, During_Parsing => True);
2166 end if;
2167
2168 -- Here is where we deal with the case of IS used instead of semicolon.
2169 -- Specifically, if the last declaration in the declarative part is a
2170 -- subprogram body still marked as having a bad IS, then this is where
2171 -- we decide that the IS should really have been a semicolon and that
2172 -- the body should have been a declaration. Note that if the bad IS
2173 -- had turned out to be OK (i.e. a decent begin/end was found for it),
2174 -- then the Bad_Is_Detected flag would have been reset by now.
2175
2176 Body_Decl := Last (Decls);
2177
2178 if Present (Body_Decl)
2179 and then Nkind (Body_Decl) = N_Subprogram_Body
2180 and then Bad_Is_Detected (Body_Decl)
2181 then
2182 -- OK, we have the case of a bad IS, so we need to fix up the tree.
2183 -- What we have now is a subprogram body with attached declarations
2184 -- and a possible statement sequence.
2185
2186 -- First step is to take the declarations that were part of the bogus
2187 -- subprogram body and append them to the outer declaration chain.
2188 -- In other words we append them past the body (which we will later
2189 -- convert into a declaration).
2190
2191 Append_List (Declarations (Body_Decl), Decls);
2192
2193 -- Now take the handled statement sequence of the bogus body and
2194 -- set it as the statement sequence for the outer construct. Note
2195 -- that it may be empty (we specially allowed a missing BEGIN for
2196 -- a subprogram body marked as having a bad IS -- see below).
2197
2198 Set_Handled_Statement_Sequence (Parent,
2199 Handled_Statement_Sequence (Body_Decl));
2200
2201 -- Next step is to convert the old body node to a declaration node
2202
2203 Spec_Node := Specification (Body_Decl);
2204 Change_Node (Body_Decl, N_Subprogram_Declaration);
2205 Set_Specification (Body_Decl, Spec_Node);
2206
2207 -- Final step is to put the declarations for the parent where
2208 -- they belong, and then fall through the IF to scan out the
2209 -- END statements.
2210
2211 Set_Declarations (Parent, Decls);
2212
2213 -- This is the normal case (i.e. any case except the bad IS case)
2214 -- If we have a BEGIN, then scan out the sequence of statements, and
2215 -- also reset the expected column for the END to match the BEGIN.
2216
2217 else
2218 Set_Declarations (Parent, Decls);
2219
2220 if Token = Tok_Begin then
2221 if Style_Check then
2222 Style.Check_Indentation;
2223 end if;
2224
2225 Error_Msg_Col := Scopes (Scope.Last).Ecol;
2226
2227 if RM_Column_Check
2228 and then Token_Is_At_Start_Of_Line
2229 and then Start_Column /= Error_Msg_Col
2230 then
2231 Error_Msg_SC ("(style) BEGIN in wrong column, should be@?l?");
2232
2233 else
2234 Scopes (Scope.Last).Ecol := Start_Column;
2235 end if;
2236
2237 Scopes (Scope.Last).Sloc := Token_Ptr;
2238 Scan; -- past BEGIN
2239 Set_Handled_Statement_Sequence (Parent,
2240 P_Handled_Sequence_Of_Statements);
2241
2242 -- No BEGIN present
2243
2244 else
2245 Parent_Nkind := Nkind (Parent);
2246
2247 -- A special check for the missing IS case. If we have a
2248 -- subprogram body that was marked as having a suspicious
2249 -- IS, and the current token is END, then we simply confirm
2250 -- the suspicion, and do not require a BEGIN to be present
2251
2252 if Parent_Nkind = N_Subprogram_Body
2253 and then Token = Tok_End
2254 and then Scopes (Scope.Last).Etyp = E_Suspicious_Is
2255 then
2256 Scopes (Scope.Last).Etyp := E_Bad_Is;
2257
2258 -- Otherwise BEGIN is not required for a package body, so we
2259 -- don't mind if it is missing, but we do construct a dummy
2260 -- one (so that we have somewhere to set End_Label).
2261
2262 -- However if we have something other than a BEGIN which
2263 -- looks like it might be statements, then we signal a missing
2264 -- BEGIN for these cases as well. We define "something which
2265 -- looks like it might be statements" as a token other than
2266 -- END, EOF, or a token which starts declarations.
2267
2268 elsif Parent_Nkind = N_Package_Body
2269 and then Token in Tok_End | Tok_EOF | Token_Class_Declk
2270 then
2271 Set_Null_HSS (Parent);
2272
2273 -- These are cases in which a BEGIN is required and not present
2274
2275 else
2276 Set_Null_HSS (Parent);
2277
2278 -- Prepare to issue error message
2279
2280 Error_Msg_Sloc := Scopes (Scope.Last).Sloc;
2281 Error_Msg_Node_1 := Scopes (Scope.Last).Labl;
2282
2283 -- Now issue appropriate message
2284
2285 if Parent_Nkind = N_Block_Statement then
2286 Missing_Begin ("missing BEGIN for DECLARE#!");
2287
2288 elsif Parent_Nkind = N_Entry_Body then
2289 Missing_Begin ("missing BEGIN for ENTRY#!");
2290
2291 elsif Parent_Nkind = N_Subprogram_Body then
2292 if Nkind (Specification (Parent))
2293 = N_Function_Specification
2294 then
2295 Missing_Begin ("missing BEGIN for function&#!");
2296 else
2297 Missing_Begin ("missing BEGIN for procedure&#!");
2298 end if;
2299
2300 -- The case for package body arises only when
2301 -- we have possible statement junk present.
2302
2303 elsif Parent_Nkind = N_Package_Body then
2304 Missing_Begin ("missing BEGIN for package body&#!");
2305
2306 else
2307 pragma Assert (Parent_Nkind = N_Task_Body);
2308 Missing_Begin ("missing BEGIN for task body&#!");
2309 end if;
2310
2311 -- Here we pick up the statements after the BEGIN that
2312 -- should have been present but was not. We don't insist
2313 -- on statements being present if P_Declarative_Part had
2314 -- already found a missing BEGIN, since it might have
2315 -- swallowed a lone statement into the declarative part.
2316
2317 if Missing_Begin_Msg /= No_Error_Msg
2318 and then Token = Tok_End
2319 then
2320 null;
2321 else
2322 Set_Handled_Statement_Sequence (Parent,
2323 P_Handled_Sequence_Of_Statements);
2324 end if;
2325 end if;
2326 end if;
2327 end if;
2328
2329 -- Here with declarations and handled statement sequence scanned
2330
2331 if Present (Handled_Statement_Sequence (Parent)) then
2332 End_Statements (Handled_Statement_Sequence (Parent));
2333 else
2334 End_Statements;
2335 end if;
2336
2337 -- We know that End_Statements removed an entry from the scope stack
2338 -- (because it is required to do so under all circumstances). We can
2339 -- therefore reference the entry it removed one past the stack top.
2340 -- What we are interested in is whether it was a case of a bad IS.
2341 -- We can't call Scopes here.
2342
2343 if Scope.Table (Scope.Last + 1).Etyp = E_Bad_Is then
2344 Error_Msg -- CODEFIX
2345 ("|IS should be "";""", Scope.Table (Scope.Last + 1).S_Is);
2346 Set_Bad_Is_Detected (Parent, True);
2347 end if;
2348
2349 end Parse_Decls_Begin_End;
2350
2351 -------------------------
2352 -- Set_Loop_Block_Name --
2353 -------------------------
2354
2355 function Set_Loop_Block_Name (L : Character) return Name_Id is
2356 begin
2357 Name_Buffer (1) := L;
2358 Name_Buffer (2) := '_';
2359 Name_Len := 2;
2360 Loop_Block_Count := Loop_Block_Count + 1;
2361 Add_Nat_To_Name_Buffer (Loop_Block_Count);
2362 return Name_Find;
2363 end Set_Loop_Block_Name;
2364
2365 ---------------
2366 -- Then_Scan --
2367 ---------------
2368
2369 procedure Then_Scan is
2370 begin
2371 TF_Then;
2372
2373 while Token = Tok_Then loop
2374 Error_Msg_SC -- CODEFIX
2375 ("redundant THEN");
2376 TF_Then;
2377 end loop;
2378
2379 if Token in Tok_And | Tok_Or then
2380 Error_Msg_SC ("unexpected logical operator");
2381 Scan; -- past logical operator
2382
2383 if (Prev_Token = Tok_And and then Token = Tok_Then)
2384 or else
2385 (Prev_Token = Tok_Or and then Token = Tok_Else)
2386 then
2387 Scan;
2388 end if;
2389
2390 Discard_Junk_Node (P_Expression);
2391 end if;
2392
2393 if Token = Tok_Then then
2394 Scan;
2395 end if;
2396 end Then_Scan;
2397
2398 end Ch5;