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