OSDN Git Service

Nathanael Nerode <neroden@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-endh.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             P A R . E N D H                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1992-2001, Free Software Foundation, Inc.         --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- GNAT was originally developed  by the GNAT team at  New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
25 --                                                                          --
26 ------------------------------------------------------------------------------
27
28 with Stringt; use Stringt;
29 with Uintp;   use Uintp;
30
31 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
32
33 separate (Par)
34 package body Endh is
35
36    ----------------
37    -- Local Data --
38    ----------------
39
40    type End_Action_Type is (
41    --  Type used to describe the result of the Pop_End_Context call
42
43       Accept_As_Scanned,
44       --  Current end sequence is entirely c correct. In this case Token and
45       --  the scan pointer are left pointing past the end sequence (i.e. they
46       --  are unchanged from the values set on entry to Pop_End_Context).
47
48       Insert_And_Accept,
49       --  Current end sequence is to be left in place to satisfy some outer
50       --  scope. Token and the scan pointer are set to point to the end
51       --  token, and should be left there. A message has been generated
52       --  indicating a missing end sequence. This status is also used for
53       --  the case when no end token is present.
54
55       Skip_And_Accept,
56       --  The end sequence is incorrect (and an error message has been
57       --  posted), but it will still be accepted. In this case Token and
58       --  the scan pointer point back to the end token, and the caller
59       --  should skip past the end sequence before proceeding.
60
61       Skip_And_Reject);
62       --  The end sequence is judged to belong to an unrecognized inner
63       --  scope. An appropriate message has been issued and the caller
64       --  should skip past the end sequence and then proceed as though
65       --  no end sequence had been encountered.
66
67    End_Action : End_Action_Type;
68    --  The variable set by Pop_End_Context call showing which of the four
69    --  decisions described above is judged the best.
70
71    End_Sloc : Source_Ptr;
72    --  Source location of END token
73
74    End_OK : Boolean;
75    --  Set False if error is found in END line
76
77    End_Column : Column_Number;
78    --  Column of END line
79
80    End_Type : SS_End_Type;
81    --  Type of END expected. The special value E_Dummy is set to indicate that
82    --  no END token was present (so a missing END inserted message is needed)
83
84    End_Labl : Node_Id;
85    --  Node_Id value for explicit name on END line, or for compiler supplied
86    --  name in the case where an optional name is not given. Empty if no name
87    --  appears. If non-empty, then it is either an N_Designator node for a
88    --  child unit or a node with a Chars field identifying the actual label.
89
90    End_Labl_Present : Boolean;
91    --  Indicates that the value in End_Labl was for an explicit label.
92
93    Syntax_OK : Boolean;
94    --  Set True if the entry is syntactically correct
95
96    Token_OK : Boolean;
97    --  Set True if the keyword in the END sequence matches, or if neither
98    --  the END sequence nor the END stack entry has a keyword.
99
100    Label_OK : Boolean;
101    --  Set True if both the END sequence and the END stack entry contained
102    --  labels (other than No_Name or Error_Name) and the labels matched.
103    --  This is a stronger condition than SYNTAX_OK, since it means that a
104    --  label was present, even in a case where it was optional. Note that
105    --  the case of no label required, and no label present does NOT set
106    --  Label_OK to True, it is True only if a positive label match is found.
107
108    Column_OK : Boolean;
109    --  Column_OK is set True if the END sequence appears in the expected column
110
111    Scan_State : Saved_Scan_State;
112    --  Save state at start of END sequence, in case we decide not to eat it up
113
114    -----------------------
115    -- Local Subprograms --
116    -----------------------
117
118    procedure Evaluate_End_Entry (SS_Index : Int);
119    --  Compare scanned END entry (as recorded by a prior call to P_End_Scan)
120    --  with a specified entry in the scope stack (the single parameter is the
121    --  entry index in the scope stack). Note that Scan is not called. The above
122    --  variables xxx_OK are set to indicate the result of the evaluation.
123
124    procedure Output_End_Deleted;
125    --  Output a message complaining that the current END structure does not
126    --  match anything and is being deleted.
127
128    procedure Output_End_Expected (Ins : Boolean);
129    --  Output a message at the start of the current token which is always an
130    --  END, complaining that the END is not of the right form. The message
131    --  indicates the expected form. The information for the message is taken
132    --  from the top entry in the scope stack. The Ins parameter is True if
133    --  an end is being inserted, and false if an existing end is being
134    --  replaced. Note that in the case of a suspicious IS for the Ins case,
135    --  we do not output the message, but instead simply mark the scope stack
136    --  entry as being a case of a bad IS.
137
138    procedure Output_End_Missing;
139    --  Output a message just before the current token, complaining that the
140    --  END is not of the right form. The message indicates the expected form.
141    --  The information for the message is taken from the top entry in the
142    --  scope stack. Note that in the case of a suspicious IS, we do not output
143    --  the message, but instead simply mark the scope stack entry as a bad IS.
144
145    procedure Pop_End_Context;
146    --  Pop_End_Context is called after processing a construct, to pop the
147    --  top entry off the end stack. It decides on the appropriate action to
148    --  to take, signalling the result by setting End_Action as described in
149    --  the global variable section.
150
151    function Same_Label (Label1, Label2 : Node_Id) return Boolean;
152    --  This function compares the two names associated with the given nodes.
153    --  If they are both simple (i.e. have Chars fields), then they have to
154    --  be the same name. Otherwise they must both be N_Selected_Component
155    --  nodes, referring to the same set of names, or Label1 is an N_Designator
156    --  referring to the same set of names as the N_Defining_Program_Unit_Name
157    --  in Label2. Any other combination returns False. This routine is used
158    --  to compare the End_Labl scanned from the End line with the saved label
159    --  value in the scope stack.
160
161    ---------------
162    -- Check_End --
163    ---------------
164
165    function Check_End return Boolean is
166       Name_On_Separate_Line : Boolean;
167       --  Set True if the name on an END line is on a separate source line
168       --  from the END. This is highly suspicious, but is allowed. The point
169       --  is that we want to make sure that we don't just have a missing
170       --  semicolon misleading us into swallowing an identifier from the
171       --  following line.
172
173       Name_Scan_State : Saved_Scan_State;
174       --  Save state at start of name if Name_On_Separate_Line is TRUE
175
176       Span_Node : constant Node_Id := Scope.Table (Scope.Last).Node;
177
178    begin
179       End_Labl_Present := False;
180       End_Labl := Empty;
181
182       --  Our first task is to scan out the END sequence if one is present.
183       --  If none is present, signal by setting End_Type to E_Dummy.
184
185       if Token /= Tok_End then
186          End_Type := E_Dummy;
187
188       else
189          Save_Scan_State (Scan_State); -- at END
190          End_Sloc := Token_Ptr;
191          End_Column := Start_Column;
192          End_OK := True;
193          Scan; -- past END
194
195          --  Set End_Span if expected. note that this will be useless
196          --  if we do not have the right ending keyword, but in this
197          --  case we have a malformed program anyway, and the setting
198          --  of End_Span will simply be unreliable in this case anyway.
199
200          if Present (Span_Node) then
201             Set_End_Location (Span_Node, Token_Ptr);
202          end if;
203
204          --  Cases of keywords where no label is allowed
205
206          if Token = Tok_Case then
207             End_Type := E_Case;
208             Scan; -- past CASE
209
210          elsif Token = Tok_If then
211             End_Type := E_If;
212             Scan; -- past IF
213
214          elsif Token = Tok_Record then
215             End_Type := E_Record;
216             Scan; -- past RECORD
217
218          elsif Token = Tok_Select then
219             End_Type := E_Select;
220             Scan; -- past SELECT
221
222          --  Cases which do allow labels
223
224          else
225             --  LOOP
226
227             if Token = Tok_Loop then
228                Scan; -- past LOOP
229                End_Type := E_Loop;
230
231             --  FOR or WHILE allowed (signalling error) to substitute for LOOP
232             --  if on the same line as the END
233
234             elsif (Token = Tok_For or else Token = Tok_While)
235               and then not Token_Is_At_Start_Of_Line
236             then
237                Scan; -- past FOR or WHILE
238                End_Type := E_Loop;
239                End_OK := False;
240
241             --  Cases with no keyword
242
243             else
244                End_Type := E_Name;
245             end if;
246
247             --  Now see if a name is present
248
249             if Token = Tok_Identifier or else
250                Token = Tok_String_Literal or else
251                Token = Tok_Operator_Symbol
252             then
253                if Token_Is_At_Start_Of_Line then
254                   Name_On_Separate_Line := True;
255                   Save_Scan_State (Name_Scan_State);
256                else
257                   Name_On_Separate_Line := False;
258                end if;
259
260                End_Labl := P_Designator;
261                End_Labl_Present := True;
262
263                --  We have now scanned out a name. Here is where we do a check
264                --  to catch the cases like:
265                --
266                --    end loop
267                --    X := 3;
268                --
269                --  where the missing semicolon might make us swallow up the X
270                --  as a bogus end label. In a situation like this, where the
271                --  apparent name is on a separate line, we accept it only if
272                --  it matches the label and is followed by a semicolon.
273
274                if Name_On_Separate_Line then
275                   if Token /= Tok_Semicolon or else
276                     not Same_Label (End_Labl, Scope.Table (Scope.Last).Labl)
277                   then
278                      Restore_Scan_State (Name_Scan_State);
279                      End_Labl := Empty;
280                      End_Labl_Present := False;
281                   end if;
282                end if;
283
284             --  Here for case of name allowed, but no name present. We will
285             --  supply an implicit matching name, with source location set
286             --  to the scan location past the END token.
287
288             else
289                End_Labl := Scope.Table (Scope.Last).Labl;
290
291                if End_Labl > Empty_Or_Error then
292
293                   --  The task here is to construct a designator from the
294                   --  opening label, with the components all marked as not
295                   --  from source, and Is_End_Label set in the identifier
296                   --  or operator symbol. The location for all components
297                   --  is the curent token location.
298
299                   --  Case of child unit name
300
301                   if Nkind (End_Labl) = N_Defining_Program_Unit_Name then
302                      declare
303                         Eref : constant Node_Id :=
304                                  Make_Identifier (Token_Ptr,
305                                    Chars =>
306                                      Chars (Defining_Identifier (End_Labl)));
307
308                         function Copy_Name (N : Node_Id) return Node_Id;
309                         --  Copies a selected component or identifier
310
311                         function Copy_Name (N : Node_Id) return Node_Id is
312                            R : Node_Id;
313
314                         begin
315                            if Nkind (N) = N_Selected_Component then
316                               return
317                                 Make_Selected_Component (Token_Ptr,
318                                   Prefix        =>
319                                     Copy_Name (Prefix (N)),
320                                   Selector_Name =>
321                                     Copy_Name (Selector_Name (N)));
322
323                            else
324                               R :=
325                                 Make_Identifier (Token_Ptr,
326                                   Chars => Chars (N));
327                               Set_Comes_From_Source (N, False);
328                               return R;
329                            end if;
330                         end Copy_Name;
331
332                      begin
333                         Set_Comes_From_Source (Eref, False);
334
335                         End_Labl :=
336                           Make_Designator (Token_Ptr,
337                             Name       => Copy_Name (Name (End_Labl)),
338                             Identifier => Eref);
339                      end;
340
341                   --  Simple identifier case
342
343                   elsif Nkind (End_Labl) = N_Defining_Identifier
344                     or else Nkind (End_Labl) = N_Identifier
345                   then
346                      End_Labl :=
347                        Make_Identifier (Token_Ptr,
348                          Chars => Chars (End_Labl));
349
350                   elsif Nkind (End_Labl) = N_Defining_Operator_Symbol
351                     or else Nkind (End_Labl) = N_Operator_Symbol
352                   then
353                      Get_Decoded_Name_String (Chars (End_Labl));
354
355                      End_Labl :=
356                        Make_Operator_Symbol (Token_Ptr,
357                          Chars  => Chars (End_Labl),
358                          Strval => String_From_Name_Buffer);
359                   end if;
360
361                   Set_Comes_From_Source (End_Labl, False);
362                   End_Labl_Present := False;
363
364                   --  Do style check for missing label
365
366                   if Style_Check
367                     and then End_Type = E_Name
368                     and then Present (Scope.Table (Scope.Last).Labl)
369                   then
370                      Style.No_End_Name (Scope.Table (Scope.Last).Labl);
371                   end if;
372                end if;
373             end if;
374          end if;
375
376          --  Except in case of END RECORD, semicolon must follow. For END
377          --  RECORD, a semicolon does follow, but it is part of a higher level
378          --  construct. In any case, a missing semicolon is not serious enough
379          --  to consider the END statement to be bad in the sense that we
380          --  are dealing with (i.e. to be suspicious that it is not in fact
381          --  the END statement we are looking for!)
382
383          if End_Type /= E_Record then
384             if Token = Tok_Semicolon then
385                T_Semicolon;
386
387             --  Semicolon is missing. If the missing semicolon is at the end
388             --  of the line, i.e. we are at the start of the line now, then
389             --  a missing semicolon gets flagged, but is not serious enough
390             --  to consider the END statement to be bad in the sense that we
391             --  are dealing with (i.e. to be suspicious that this END is not
392             --  the END statement we are looking for).
393
394             --  Similarly, if we are at a colon, we flag it but a colon for
395             --  a semicolon is not serious enough to consider the END to be
396             --  incorrect. Same thing for a period in place of a semicolon.
397
398             elsif Token_Is_At_Start_Of_Line
399               or else Token = Tok_Colon
400               or else Token = Tok_Dot
401             then
402                T_Semicolon;
403
404             --  If the missing semicolon is not at the start of the line,
405             --  then we do consider the END line to be dubious in this sense.
406
407             else
408                End_OK := False;
409             end if;
410          end if;
411       end if;
412
413       --  Now we call the Pop_End_Context routine to get a recommendation
414       --  as to what should be done with the END sequence we have scanned.
415
416       Pop_End_Context;
417
418       --  Remaining action depends on End_Action set by Pop_End_Context
419
420       case End_Action is
421
422          --  Accept_As_Scanned. In this case, Pop_End_Context left Token
423          --  pointing past the last token of a syntactically correct END
424
425          when Accept_As_Scanned =>
426
427             --  Syntactically correct included the possibility of a missing
428             --  semicolon. If we do have a missing semicolon, then we have
429             --  already given a message, but now we scan out possible rubbish
430             --  on the same line as the END
431
432             while not Token_Is_At_Start_Of_Line
433               and then Prev_Token /= Tok_Record
434               and then Prev_Token /= Tok_Semicolon
435               and then Token /= Tok_End
436               and then Token /= Tok_EOF
437             loop
438                Scan; -- past junk
439             end loop;
440
441             return True;
442
443          --  Insert_And_Accept. In this case, Pop_End_Context has reset Token
444          --  to point to the start of the END sequence, and recommends that it
445          --  be left in place to satisfy an outer scope level END. This means
446          --  that we proceed as though an END were present, and leave the scan
447          --  pointer unchanged.
448
449          when Insert_And_Accept =>
450             return True;
451
452          --  Skip_And_Accept. In this case, Pop_End_Context has reset Token
453          --  to point to the start of the END sequence. This END sequence is
454          --  syntactically incorrect, and an appropriate error message has
455          --  already been posted. Pop_End_Context recommends accepting the
456          --  END sequence as the one we want, so we skip past it and then
457          --  proceed as though an END were present.
458
459          when Skip_And_Accept =>
460             End_Skip;
461             return True;
462
463          --  Skip_And_Reject. In this case, Pop_End_Context has reset Token
464          --  to point to the start of the END sequence. This END sequence is
465          --  syntactically incorrect, and an appropriate error message has
466          --  already been posted. Pop_End_Context recommends entirely ignoring
467          --  this END sequence, so we skip past it and then return False, since
468          --  as far as the caller is concerned, no END sequence is present.
469
470          when Skip_And_Reject =>
471             End_Skip;
472             return False;
473       end case;
474    end Check_End;
475
476    --------------
477    -- End Skip --
478    --------------
479
480    --  This procedure skips past an END sequence. On entry Token contains
481    --  Tok_End, and we know that the END sequence is syntactically incorrect,
482    --  and that an appropriate error message has already been posted. The
483    --  mission is simply to position the scan pointer to be the best guess of
484    --  the position after the END sequence. We do not issue any additional
485    --  error messages while carrying this out.
486
487    --  Error recovery: does not raise Error_Resync
488
489    procedure End_Skip is
490    begin
491       Scan; -- past END
492
493       --  If the scan past the END leaves us on the next line, that's probably
494       --  where we should quit the scan, since it is likely that what we have
495       --  is a missing semicolon. Consider the following:
496
497       --       END
498       --       Process_Input;
499
500       --  This will have looked like a syntactically valid END sequence to the
501       --  initial scan of the END, but subsequent checking will have determined
502       --  that the label Process_Input is not an appropriate label. The real
503       --  error is a missing semicolon after the END, and by leaving the scan
504       --  pointer just past the END, we will improve the error recovery.
505
506       if Token_Is_At_Start_Of_Line then
507          return;
508       end if;
509
510       --  If there is a semicolon after the END, scan it out and we are done
511
512       if Token = Tok_Semicolon then
513          T_Semicolon;
514          return;
515       end if;
516
517       --  Otherwise skip past a token after the END on the same line. Note
518       --  that we do not eat a token on the following line since it seems
519       --  very unlikely in any case that the END gets separated from its
520       --  token, and we do not want to swallow up a keyword that starts a
521       --  legitimate construct following the bad END.
522
523       if not Token_Is_At_Start_Of_Line
524         and then
525
526          --  Cases of normal tokens following an END
527
528           (Token = Tok_Case   or else
529            Token = Tok_For    or else
530            Token = Tok_If     or else
531            Token = Tok_Loop   or else
532            Token = Tok_Record or else
533            Token = Tok_Select or else
534
535          --  Cases of bogus keywords ending loops
536
537            Token = Tok_For    or else
538            Token = Tok_While  or else
539
540          --  Cases of operator symbol names without quotes
541
542            Token = Tok_Abs    or else
543            Token = Tok_And    or else
544            Token = Tok_Mod    or else
545            Token = Tok_Not    or else
546            Token = Tok_Or     or else
547            Token = Tok_Xor)
548
549       then
550          Scan; -- past token after END
551
552          --  If that leaves us on the next line, then we are done. This is the
553          --  same principle described above for the case of END at line end
554
555          if Token_Is_At_Start_Of_Line then
556             return;
557
558          --  If we just scanned out record, then we are done, since the
559          --  semicolon after END RECORD is not part of the END sequence
560
561          elsif Prev_Token = Tok_Record then
562             return;
563
564          --  If we have a semicolon, scan it out and we are done
565
566          elsif Token = Tok_Semicolon then
567             T_Semicolon;
568             return;
569          end if;
570       end if;
571
572       --  Check for a label present on the same line
573
574       loop
575          if Token_Is_At_Start_Of_Line then
576             return;
577          end if;
578
579          if Token /= Tok_Identifier
580            and then Token /= Tok_Operator_Symbol
581            and then Token /= Tok_String_Literal
582          then
583             exit;
584          end if;
585
586          Scan; -- past identifier, operator symbol or string literal
587
588          if Token_Is_At_Start_Of_Line then
589             return;
590          elsif Token = Tok_Dot then
591             Scan; -- past dot
592          end if;
593       end loop;
594
595       --  Skip final semicolon
596
597       if Token = Tok_Semicolon then
598          T_Semicolon;
599
600       --  If we don't have a final semicolon, skip until we either encounter
601       --  an END token, or a semicolon or the start of the next line. This
602       --  allows general junk to follow the end line (normally it is hard to
603       --  think that anyone will put anything deliberate here, and remember
604       --  that we know there is a missing semicolon in any case). We also
605       --  quite on an EOF (or else we would get stuck in an infinite loop
606       --  if there is no line end at the end of the last line of the file)
607
608       else
609          while Token /= Tok_End
610            and then Token /= Tok_EOF
611            and then Token /= Tok_Semicolon
612            and then not Token_Is_At_Start_Of_Line
613          loop
614             Scan; -- past junk token on same line
615          end loop;
616       end if;
617
618       return;
619    end End_Skip;
620
621    --------------------
622    -- End Statements --
623    --------------------
624
625    --  This procedure is called when END is required or expected to terminate
626    --  a sequence of statements. The caller has already made an appropriate
627    --  entry on the scope stack to describe the expected form of the END.
628    --  End_Statements should only be used in cases where the only appropriate
629    --  terminator is END.
630
631    --  Error recovery: cannot raise Error_Resync;
632
633    procedure End_Statements (Parent : Node_Id := Empty) is
634    begin
635       --  This loop runs more than once in the case where Check_End rejects
636       --  the END sequence, as indicated by Check_End returning False.
637
638       loop
639          if Check_End then
640             if Present (Parent) then
641                Set_End_Label (Parent, End_Labl);
642             end if;
643
644             return;
645          end if;
646
647          --  Extra statements past the bogus END are discarded. This is not
648          --  ideal for maximum error recovery, but it's too much trouble to
649          --  find an appropriate place to put them!
650
651          Discard_Junk_List (P_Sequence_Of_Statements (SS_None));
652       end loop;
653    end End_Statements;
654
655    ------------------------
656    -- Evaluate End Entry --
657    ------------------------
658
659    procedure Evaluate_End_Entry (SS_Index : Int) is
660    begin
661       Column_OK := (End_Column = Scope.Table (SS_Index).Ecol);
662
663       Token_OK  := (End_Type = Scope.Table (SS_Index).Etyp or else
664                      (End_Type = E_Name and then
665                        Scope.Table (SS_Index).Etyp >= E_Name));
666
667       Label_OK := End_Labl_Present
668                     and then
669                       (Same_Label (End_Labl, Scope.Table (SS_Index).Labl)
670                         or else Scope.Table (SS_Index).Labl = Error);
671
672       --  Compute setting of Syntax_OK. We definitely have a syntax error
673       --  if the Token does not match properly or if P_End_Scan detected
674       --  a syntax error such as a missing semicolon.
675
676       if not Token_OK or not End_OK then
677          Syntax_OK := False;
678
679       --  Final check is that label is OK. Certainly it is OK if there
680       --  was an exact match on the label (the END label = the stack label)
681
682       elsif Label_OK then
683          Syntax_OK := True;
684
685       --  Case of label present
686
687       elsif End_Labl_Present then
688
689          --  If probably misspelling, then complain, and pretend it is OK
690
691          declare
692             Nam : constant Node_Or_Entity_Id := Scope.Table (SS_Index).Labl;
693
694          begin
695             if Nkind (End_Labl) in N_Has_Chars
696               and then Nkind (Nam) in N_Has_Chars
697               and then Chars (End_Labl) > Error_Name
698               and then Chars (Nam) > Error_Name
699             then
700                Get_Name_String (Chars (End_Labl));
701                Error_Msg_Name_1 := Chars (Nam);
702
703                if Error_Msg_Name_1 > Error_Name then
704                   declare
705                      S : String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
706
707                   begin
708                      Get_Name_String (Error_Msg_Name_1);
709
710                      if Is_Bad_Spelling_Of
711                          (Name_Buffer (1 .. Name_Len), S)
712                      then
713                         Error_Msg_N ("misspelling of %", End_Labl);
714                         Syntax_OK := True;
715                         return;
716                      end if;
717                   end;
718                end if;
719             end if;
720          end;
721
722          Syntax_OK := False;
723
724       --  Otherwise we have cases of no label on the END line. For the loop
725       --  case, this is acceptable only if the loop is unlabeled.
726
727       elsif End_Type = E_Loop then
728          Syntax_OK := (Scope.Table (SS_Index).Labl = Empty);
729
730       --  Cases where a label is definitely allowed on the END line
731
732       elsif End_Type = E_Name then
733          Syntax_OK := (Scope.Table (SS_Index).Labl = Empty or else
734                          not Scope.Table (SS_Index).Lreq);
735
736       --  Otherwise we have cases which don't allow labels anyway, so we
737       --  certainly accept an END which does not have a label.
738
739       else
740          Syntax_OK := True;
741       end if;
742    end Evaluate_End_Entry;
743
744    ------------------------
745    -- Output End Deleted --
746    ------------------------
747
748    procedure Output_End_Deleted is
749    begin
750
751       if End_Type = E_Loop then
752          Error_Msg_SC ("no LOOP for this `END LOOP`!");
753
754       elsif End_Type = E_Case then
755          Error_Msg_SC ("no CASE for this `END CASE`");
756
757       elsif End_Type = E_If then
758          Error_Msg_SC ("no IF for this `END IF`!");
759
760       elsif End_Type = E_Record then
761          Error_Msg_SC ("no RECORD for this `END RECORD`!");
762
763       elsif End_Type = E_Select then
764          Error_Msg_SC ("no SELECT for this `END SELECT`!");
765
766       else
767          Error_Msg_SC ("no BEGIN for this END!");
768       end if;
769    end Output_End_Deleted;
770
771    -------------------------
772    -- Output End Expected --
773    -------------------------
774
775    procedure Output_End_Expected (Ins : Boolean) is
776       End_Type : SS_End_Type;
777
778    begin
779       --  Suppress message if this was a potentially junk entry (e.g. a
780       --  record entry where no record keyword was present.
781
782       if Scope.Table (Scope.Last).Junk then
783          return;
784       end if;
785
786       End_Type := Scope.Table (Scope.Last).Etyp;
787       Error_Msg_Col    := Scope.Table (Scope.Last).Ecol;
788       Error_Msg_Node_1 := Scope.Table (Scope.Last).Labl;
789       Error_Msg_Sloc   := Scope.Table (Scope.Last).Sloc;
790
791       --  Suppress message if error was posted on opening label
792
793       if Error_Msg_Node_1 > Empty_Or_Error
794         and then Error_Posted (Error_Msg_Node_1)
795       then
796          return;
797       end if;
798
799       if End_Type = E_Case then
800          Error_Msg_SC ("`END CASE;` expected@ for CASE#!");
801
802       elsif End_Type = E_If then
803          Error_Msg_SC ("`END IF;` expected@ for IF#!");
804
805       elsif End_Type = E_Loop then
806          if Error_Msg_Node_1 = Empty then
807             Error_Msg_SC
808               ("`END LOOP;` expected@ for LOOP#!");
809          else
810             Error_Msg_SC ("`END LOOP &;` expected@!");
811          end if;
812
813       elsif End_Type = E_Record then
814          Error_Msg_SC
815            ("`END RECORD;` expected@ for RECORD#!");
816
817       elsif End_Type = E_Select then
818          Error_Msg_SC
819            ("`END SELECT;` expected@ for SELECT#!");
820
821       --  All remaining cases are cases with a name (we do not treat
822       --  the suspicious is cases specially for a replaced end, only
823       --  for an inserted end).
824
825       elsif End_Type = E_Name or else (not Ins) then
826          if Error_Msg_Node_1 = Empty then
827             Error_Msg_SC ("`END;` expected@ for BEGIN#!");
828          else
829             Error_Msg_SC ("`END &;` expected@!");
830          end if;
831
832       --  The other possibility is a missing END for a subprogram with a
833       --  suspicious IS (that probably should have been a semicolon). The
834       --  Missing IS confirms the suspicion!
835
836       else -- End_Type = E_Suspicious_Is or E_Bad_Is
837          Scope.Table (Scope.Last).Etyp := E_Bad_Is;
838       end if;
839    end Output_End_Expected;
840
841    ------------------------
842    -- Output End Missing --
843    ------------------------
844
845    procedure Output_End_Missing is
846       End_Type : SS_End_Type;
847
848    begin
849       --  Suppress message if this was a potentially junk entry (e.g. a
850       --  record entry where no record keyword was present.
851
852       if Scope.Table (Scope.Last).Junk then
853          return;
854       end if;
855
856       End_Type := Scope.Table (Scope.Last).Etyp;
857       Error_Msg_Node_1 := Scope.Table (Scope.Last).Labl;
858       Error_Msg_Sloc   := Scope.Table (Scope.Last).Sloc;
859
860       if End_Type = E_Case then
861          Error_Msg_BC ("missing `END CASE;` for CASE#!");
862
863       elsif End_Type = E_If then
864          Error_Msg_BC ("missing `END IF;` for IF#!");
865
866       elsif End_Type = E_Loop then
867          if Error_Msg_Node_1 = Empty then
868             Error_Msg_BC ("missing `END LOOP;` for LOOP#!");
869          else
870             Error_Msg_BC ("missing `END LOOP &;`!");
871          end if;
872
873       elsif End_Type = E_Record then
874          Error_Msg_SC
875            ("missing `END RECORD;` for RECORD#!");
876
877       elsif End_Type = E_Select then
878          Error_Msg_BC
879            ("missing `END SELECT;` for SELECT#!");
880
881       elsif End_Type = E_Name then
882          if Error_Msg_Node_1 = Empty then
883             Error_Msg_BC ("missing `END;` for BEGIN#!");
884          else
885             Error_Msg_BC ("missing `END &;`!");
886          end if;
887
888       else -- End_Type = E_Suspicious_Is or E_Bad_Is
889          Scope.Table (Scope.Last).Etyp := E_Bad_Is;
890       end if;
891    end Output_End_Missing;
892
893    ---------------------
894    -- Pop End Context --
895    ---------------------
896
897    procedure Pop_End_Context is
898
899       Pretty_Good : Boolean;
900       --  This flag is set True if the END sequence is syntactically incorrect,
901       --  but is (from a heuristic point of view), pretty likely to be simply
902       --  a misspelling of the intended END.
903
904       Outer_Match : Boolean;
905       --  This flag is set True if we decide that the current END sequence
906       --  belongs to some outer level entry in the scope stack, and thus
907       --  we will NOT eat it up in matching the current expected END.
908
909    begin
910       --  If not at END, then output END expected message
911
912       if End_Type = E_Dummy then
913          Output_End_Missing;
914          Pop_Scope_Stack;
915          End_Action := Insert_And_Accept;
916          return;
917
918       --  Otherwise we do have an END present
919
920       else
921          --  A special check. If we have END; followed by an end of file,
922          --  WITH or SEPARATE, then if we are not at the outer level, then
923          --  we have a sytax error. Consider the example:
924
925          --   ...
926          --      declare
927          --         X : Integer;
928          --      begin
929          --         X := Father (A);
930          --         Process (X, X);
931          --   end;
932          --   with Package1;
933          --   ...
934
935          --  Now the END; here is a syntactically correct closer for the
936          --  declare block, but if we eat it up, then we obviously have
937          --  a missing END for the outer context (since WITH can only appear
938          --  at the outer level.
939
940          --  In this situation, we always reserve the END; for the outer level,
941          --  even if it is in the wrong column. This is because it's much more
942          --  useful to have the error message point to the DECLARE than to the
943          --  package header in this case.
944
945          --  We also reserve an end with a name before the end of file if the
946          --  name is the one we expect at the outer level.
947
948          if (Token = Tok_EOF or else
949              Token = Tok_With or else
950              Token = Tok_Separate)
951            and then End_Type >= E_Name
952            and then (not End_Labl_Present
953                       or else Same_Label (End_Labl, Scope.Table (1).Labl))
954            and then Scope.Last > 1
955          then
956             Restore_Scan_State (Scan_State); -- to END
957             Output_End_Expected (Ins => True);
958             Pop_Scope_Stack;
959             End_Action := Insert_And_Accept;
960             return;
961          end if;
962
963          --  Otherwise we go through the normal END evaluation procedure
964
965          Evaluate_End_Entry (Scope.Last);
966
967          --  If top entry in stack is syntactically correct, then we have
968          --  scanned it out and everything is fine. This is the required
969          --  action to properly process correct Ada programs.
970
971          if Syntax_OK then
972
973             --  Complain if checking columns and END is not in right column.
974             --  Right in this context means exactly right, or on the same
975             --  line as the opener.
976
977             if Style.RM_Column_Check then
978                if End_Column /= Scope.Table (Scope.Last).Ecol
979                  and then Current_Line_Start > Scope.Table (Scope.Last).Sloc
980                then
981                   Error_Msg_Col := Scope.Table (Scope.Last).Ecol;
982                   Error_Msg
983                     ("(style) END in wrong column, should be@", End_Sloc);
984                end if;
985             end if;
986
987             --  One final check. If the end had a label, check for an exact
988             --  duplicate of this end sequence, and if so, skip it with an
989             --  appropriate message.
990
991             if End_Labl_Present and then Token = Tok_End then
992                declare
993                   Scan_State : Saved_Scan_State;
994                   End_Loc    : constant Source_Ptr := Token_Ptr;
995                   Nxt_Labl   : Node_Id;
996                   Dup_Found  : Boolean := False;
997
998                begin
999                   Save_Scan_State (Scan_State);
1000
1001                   Scan; -- past END
1002
1003                   if Token = Tok_Identifier
1004                     or else Token = Tok_Operator_Symbol
1005                   then
1006                      Nxt_Labl := P_Designator;
1007
1008                      --  We only consider it an error if the label is a match
1009                      --  and would be wrong for the level one above us, and
1010                      --  the indentation is the same.
1011
1012                      if Token = Tok_Semicolon
1013                        and then Same_Label (End_Labl, Nxt_Labl)
1014                        and then End_Column = Start_Column
1015                        and then
1016                          (Scope.Last = 1
1017                             or else
1018                               (No (Scope.Table (Scope.Last - 1).Labl)
1019                                 or else
1020                                not Same_Label
1021                                      (End_Labl,
1022                                       Scope.Table (Scope.Last - 1).Labl)))
1023                      then
1024                         T_Semicolon;
1025                         Error_Msg ("duplicate end line ignored", End_Loc);
1026                         Dup_Found := True;
1027                      end if;
1028                   end if;
1029
1030                   if not Dup_Found then
1031                      Restore_Scan_State (Scan_State);
1032                   end if;
1033                end;
1034             end if;
1035
1036             --  All OK, so return to caller indicating END is OK
1037
1038             Pop_Scope_Stack;
1039             End_Action := Accept_As_Scanned;
1040             return;
1041          end if;
1042
1043          --  If that check failed, then we definitely have an error. The issue
1044          --  is how to choose among three possible courses of action:
1045
1046          --   1. Ignore the current END text completely, scanning past it,
1047          --      deciding that it belongs neither to the current context,
1048          --      nor to any outer context.
1049
1050          --   2. Accept the current END text, scanning past it, and issuing
1051          --      an error message that it does not have the right form.
1052
1053          --   3. Leave the current END text in place, NOT scanning past it,
1054          --      issuing an error message indicating the END expected for the
1055          --      current context. In this case, the END is available to match
1056          --      some outer END context.
1057
1058          --  From a correct functioning point of view, it does not make any
1059          --  difference which of these three approaches we take, the program
1060          --  will work correctly in any case. However, making an accurate
1061          --  choice among these alternatives, i.e. choosing the one that
1062          --  corresponds to what the programmer had in mind, does make a
1063          --  significant difference in the quality of error recovery.
1064
1065          Restore_Scan_State (Scan_State); -- to END
1066
1067          --  First we see how good the current END entry is with respect to
1068          --  what we expect. It is considered pretty good if the token is OK,
1069          --  and either the label or the column matches. an END for RECORD is
1070          --  always considered to be pretty good in the record case. This is
1071          --  because not only does a record disallow a nested structure, but
1072          --  also it is unlikely that such nesting could occur by accident.
1073
1074          Pretty_Good := (Token_OK and (Column_OK or Label_OK))
1075                           or else Scope.Table (Scope.Last).Etyp = E_Record;
1076
1077          --  Next check, if there is a deeper entry in the stack which
1078          --  has a very high probability of being acceptable, then insert
1079          --  the END entry we want, leaving the higher level entry for later
1080
1081          for J in reverse 1 .. Scope.Last - 1 loop
1082             Evaluate_End_Entry (J);
1083
1084             --  To even consider the deeper entry to be immediately acceptable,
1085             --  it must be syntactically correct. Furthermore it must either
1086             --  have a correct label, or the correct column. If the current
1087             --  entry was a close match (Pretty_Good set), then we are even
1088             --  more strict in accepting the outer level one: even if it has
1089             --  the right label, it must have the right column as well.
1090
1091             if Syntax_OK then
1092                if Pretty_Good then
1093                   Outer_Match := Label_OK and Column_OK;
1094                else
1095                   Outer_Match := Label_OK or Column_OK;
1096                end if;
1097             else
1098                Outer_Match := False;
1099             end if;
1100
1101             --  If the outer entry does convincingly match the END text, then
1102             --  back up the scan to the start of the END sequence, issue an
1103             --  error message indicating the END we expected, and return with
1104             --  Token pointing to the END (case 3 from above discussion).
1105
1106             if Outer_Match then
1107                Output_End_Missing;
1108                Pop_Scope_Stack;
1109                End_Action := Insert_And_Accept;
1110                return;
1111             end if;
1112          end loop;
1113
1114          --  Here we have a situation in which the current END entry is
1115          --  syntactically incorrect, but there is no deeper entry in the
1116          --  END stack which convincingly matches it.
1117
1118          --  If the END text was judged to be a Pretty_Good match for the
1119          --  expected token or if it appears left of the expected column,
1120          --  then we will accept it as the one we want, scanning past it, even
1121          --  though it is not completely right (we issue a message showing what
1122          --  we expected it to be). This is action 2 from the discussion above.
1123          --  There is one other special case to consider: the LOOP case.
1124          --  Consider the example:
1125
1126          --     Lbl: loop
1127          --             null;
1128          --          end loop;
1129
1130          --  Here the column lines up with Lbl, so END LOOP is to the right,
1131          --  but it is still acceptable. LOOP is the one case where alignment
1132          --  practices vary substantially in practice.
1133
1134          if Pretty_Good
1135             or else End_Column <= Scope.Table (Scope.Last).Ecol
1136             or else (End_Type = Scope.Table (Scope.Last).Etyp
1137                         and then End_Type = E_Loop)
1138          then
1139             Output_End_Expected (Ins => False);
1140             Pop_Scope_Stack;
1141             End_Action := Skip_And_Accept;
1142             return;
1143
1144          --  Here we have the case where the END is to the right of the
1145          --  expected column and does not have a correct label to convince
1146          --  us that it nevertheless belongs to the current scope. For this
1147          --  we consider that it probably belongs not to the current context,
1148          --  but to some inner context that was not properly recognized (due to
1149          --  other syntax errors), and for which no proper scope stack entry
1150          --  was made. The proper action in this case is to delete the END text
1151          --  and return False to the caller as a signal to keep on looking for
1152          --  an acceptable END. This is action 1 from the discussion above.
1153
1154          else
1155             Output_End_Deleted;
1156             End_Action := Skip_And_Reject;
1157             return;
1158          end if;
1159       end if;
1160    end Pop_End_Context;
1161
1162    ----------------
1163    -- Same_Label --
1164    ----------------
1165
1166    function Same_Label (Label1, Label2 : Node_Id) return Boolean is
1167    begin
1168       if Nkind (Label1) in N_Has_Chars
1169         and then Nkind (Label2) in N_Has_Chars
1170       then
1171          return Chars (Label1) = Chars (Label2);
1172
1173       elsif Nkind (Label1) = N_Selected_Component
1174         and then Nkind (Label2) = N_Selected_Component
1175       then
1176          return Same_Label (Prefix (Label1), Prefix (Label2)) and then
1177            Same_Label (Selector_Name (Label1), Selector_Name (Label2));
1178
1179       elsif Nkind (Label1) = N_Designator
1180         and then Nkind (Label2) = N_Defining_Program_Unit_Name
1181       then
1182          return Same_Label (Name (Label1), Name (Label2)) and then
1183            Same_Label (Identifier (Label1), Defining_Identifier (Label2));
1184
1185       else
1186          return False;
1187       end if;
1188    end Same_Label;
1189
1190 end Endh;