OSDN Git Service

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