OSDN Git Service

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