OSDN Git Service

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