OSDN Git Service

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