OSDN Git Service

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