1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
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. --
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). --
27 ------------------------------------------------------------------------------
29 with Stringt
; use Stringt
;
30 with Uintp
; use Uintp
;
32 with GNAT
.Spelling_Checker
; use GNAT
.Spelling_Checker
;
41 type End_Action_Type
is (
42 -- Type used to describe the result of the Pop_End_Context call
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).
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.
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.
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.
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.
72 End_Sloc
: Source_Ptr
;
73 -- Source location of END token
76 -- Set False if error is found in END line
78 End_Column
: Column_Number
;
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)
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.
91 End_Labl_Present
: Boolean;
92 -- Indicates that the value in End_Labl was for an explicit label.
95 -- Set True if the entry is syntactically correct
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.
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.
110 -- Column_OK is set True if the END sequence appears in the expected column
112 Scan_State
: Saved_Scan_State
;
113 -- Save state at start of END sequence, in case we decide not to eat it up
115 -----------------------
116 -- Local Subprograms --
117 -----------------------
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.
125 procedure Output_End_Deleted
;
126 -- Output a message complaining that the current END structure does not
127 -- match anything and is being deleted.
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.
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.
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.
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.
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
174 Name_Scan_State
: Saved_Scan_State
;
175 -- Save state at start of name if Name_On_Separate_Line is TRUE
177 Span_Node
: constant Node_Id
:= Scope
.Table
(Scope
.Last
).Node
;
180 End_Labl_Present
:= False;
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.
186 if Token
/= Tok_End
then
190 Save_Scan_State
(Scan_State
); -- at END
191 End_Sloc
:= Token_Ptr
;
192 End_Column
:= Start_Column
;
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.
201 if Present
(Span_Node
) then
202 Set_End_Location
(Span_Node
, Token_Ptr
);
205 -- Cases of keywords where no label is allowed
207 if Token
= Tok_Case
then
211 elsif Token
= Tok_If
then
215 elsif Token
= Tok_Record
then
216 End_Type
:= E_Record
;
219 elsif Token
= Tok_Select
then
220 End_Type
:= E_Select
;
223 -- Cases which do allow labels
228 if Token
= Tok_Loop
then
232 -- FOR or WHILE allowed (signalling error) to substitute for LOOP
233 -- if on the same line as the END
235 elsif (Token
= Tok_For
or else Token
= Tok_While
)
236 and then not Token_Is_At_Start_Of_Line
238 Scan
; -- past FOR or WHILE
242 -- Cases with no keyword
248 -- Now see if a name is present
250 if Token
= Tok_Identifier
or else
251 Token
= Tok_String_Literal
or else
252 Token
= Tok_Operator_Symbol
254 if Token_Is_At_Start_Of_Line
then
255 Name_On_Separate_Line
:= True;
256 Save_Scan_State
(Name_Scan_State
);
258 Name_On_Separate_Line
:= False;
261 End_Labl
:= P_Designator
;
262 End_Labl_Present
:= True;
264 -- We have now scanned out a name. Here is where we do a check
265 -- to catch the cases like:
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.
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
)
279 Restore_Scan_State
(Name_Scan_State
);
281 End_Labl_Present
:= False;
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.
290 End_Labl
:= Scope
.Table
(Scope
.Last
).Labl
;
292 if End_Labl
> Empty_Or_Error
then
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.
300 -- Case of child unit name
302 if Nkind
(End_Labl
) = N_Defining_Program_Unit_Name
then
304 Eref
: constant Node_Id
:=
305 Make_Identifier
(Token_Ptr
,
307 Chars
(Defining_Identifier
(End_Labl
)));
309 function Copy_Name
(N
: Node_Id
) return Node_Id
;
310 -- Copies a selected component or identifier
312 function Copy_Name
(N
: Node_Id
) return Node_Id
is
316 if Nkind
(N
) = N_Selected_Component
then
318 Make_Selected_Component
(Token_Ptr
,
320 Copy_Name
(Prefix
(N
)),
322 Copy_Name
(Selector_Name
(N
)));
326 Make_Identifier
(Token_Ptr
,
328 Set_Comes_From_Source
(N
, False);
334 Set_Comes_From_Source
(Eref
, False);
337 Make_Designator
(Token_Ptr
,
338 Name
=> Copy_Name
(Name
(End_Labl
)),
342 -- Simple identifier case
344 elsif Nkind
(End_Labl
) = N_Defining_Identifier
345 or else Nkind
(End_Labl
) = N_Identifier
348 Make_Identifier
(Token_Ptr
,
349 Chars
=> Chars
(End_Labl
));
351 elsif Nkind
(End_Labl
) = N_Defining_Operator_Symbol
352 or else Nkind
(End_Labl
) = N_Operator_Symbol
354 Get_Decoded_Name_String
(Chars
(End_Labl
));
357 Make_Operator_Symbol
(Token_Ptr
,
358 Chars
=> Chars
(End_Labl
),
359 Strval
=> String_From_Name_Buffer
);
362 Set_Comes_From_Source
(End_Labl
, False);
363 End_Labl_Present
:= False;
365 -- Do style check for missing label
368 and then End_Type
= E_Name
369 and then Present
(Scope
.Table
(Scope
.Last
).Labl
)
371 Style
.No_End_Name
(Scope
.Table
(Scope
.Last
).Labl
);
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!)
384 if End_Type
/= E_Record
then
385 if Token
= Tok_Semicolon
then
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).
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.
399 elsif Token_Is_At_Start_Of_Line
400 or else Token
= Tok_Colon
401 or else Token
= Tok_Dot
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.
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.
419 -- Remaining action depends on End_Action set by Pop_End_Context
423 -- Accept_As_Scanned. In this case, Pop_End_Context left Token
424 -- pointing past the last token of a syntactically correct END
426 when Accept_As_Scanned
=>
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
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
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.
450 when Insert_And_Accept
=>
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.
460 when Skip_And_Accept
=>
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.
471 when Skip_And_Reject
=>
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.
488 -- Error recovery: does not raise Error_Resync
490 procedure End_Skip
is
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:
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.
507 if Token_Is_At_Start_Of_Line
then
511 -- If there is a semicolon after the END, scan it out and we are done
513 if Token
= Tok_Semicolon
then
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.
524 if not Token_Is_At_Start_Of_Line
527 -- Cases of normal tokens following an END
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
536 -- Cases of bogus keywords ending loops
538 Token
= Tok_For
or else
539 Token
= Tok_While
or else
541 -- Cases of operator symbol names without quotes
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
551 Scan
; -- past token after END
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
556 if Token_Is_At_Start_Of_Line
then
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
562 elsif Prev_Token
= Tok_Record
then
565 -- If we have a semicolon, scan it out and we are done
567 elsif Token
= Tok_Semicolon
then
573 -- Check for a label present on the same line
576 if Token_Is_At_Start_Of_Line
then
580 if Token
/= Tok_Identifier
581 and then Token
/= Tok_Operator_Symbol
582 and then Token
/= Tok_String_Literal
587 Scan
; -- past identifier, operator symbol or string literal
589 if Token_Is_At_Start_Of_Line
then
591 elsif Token
= Tok_Dot
then
596 -- Skip final semicolon
598 if Token
= Tok_Semicolon
then
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)
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
615 Scan
; -- past junk token on same line
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.
632 -- Error recovery: cannot raise Error_Resync;
634 procedure End_Statements
(Parent
: Node_Id
:= Empty
) is
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.
641 if Present
(Parent
) then
642 Set_End_Label
(Parent
, End_Labl
);
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!
652 Discard_Junk_List
(P_Sequence_Of_Statements
(SS_None
));
656 ------------------------
657 -- Evaluate End Entry --
658 ------------------------
660 procedure Evaluate_End_Entry
(SS_Index
: Int
) is
662 Column_OK
:= (End_Column
= Scope
.Table
(SS_Index
).Ecol
);
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
));
668 Label_OK
:= End_Labl_Present
670 (Same_Label
(End_Labl
, Scope
.Table
(SS_Index
).Labl
)
671 or else Scope
.Table
(SS_Index
).Labl
= Error
);
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.
677 if not Token_OK
or not End_OK
then
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)
686 -- Case of label present
688 elsif End_Labl_Present
then
690 -- If probably misspelling, then complain, and pretend it is OK
693 Nam
: constant Node_Or_Entity_Id
:= Scope
.Table
(SS_Index
).Labl
;
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
701 Get_Name_String
(Chars
(End_Labl
));
702 Error_Msg_Name_1
:= Chars
(Nam
);
704 if Error_Msg_Name_1
> Error_Name
then
706 S
: String (1 .. Name_Len
) := Name_Buffer
(1 .. Name_Len
);
709 Get_Name_String
(Error_Msg_Name_1
);
711 if Is_Bad_Spelling_Of
712 (Name_Buffer
(1 .. Name_Len
), S
)
714 Error_Msg_N
("misspelling of %", End_Labl
);
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.
728 elsif End_Type
= E_Loop
then
729 Syntax_OK
:= (Scope
.Table
(SS_Index
).Labl
= Empty
);
731 -- Cases where a label is definitely allowed on the END line
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
);
737 -- Otherwise we have cases which don't allow labels anyway, so we
738 -- certainly accept an END which does not have a label.
743 end Evaluate_End_Entry
;
745 ------------------------
746 -- Output End Deleted --
747 ------------------------
749 procedure Output_End_Deleted
is
752 if End_Type
= E_Loop
then
753 Error_Msg_SC
("no LOOP for this `END LOOP`!");
755 elsif End_Type
= E_Case
then
756 Error_Msg_SC
("no CASE for this `END CASE`");
758 elsif End_Type
= E_If
then
759 Error_Msg_SC
("no IF for this `END IF`!");
761 elsif End_Type
= E_Record
then
762 Error_Msg_SC
("no RECORD for this `END RECORD`!");
764 elsif End_Type
= E_Select
then
765 Error_Msg_SC
("no SELECT for this `END SELECT`!");
768 Error_Msg_SC
("no BEGIN for this END!");
770 end Output_End_Deleted
;
772 -------------------------
773 -- Output End Expected --
774 -------------------------
776 procedure Output_End_Expected
(Ins
: Boolean) is
777 End_Type
: SS_End_Type
;
780 -- Suppress message if this was a potentially junk entry (e.g. a
781 -- record entry where no record keyword was present.
783 if Scope
.Table
(Scope
.Last
).Junk
then
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
;
792 -- Suppress message if error was posted on opening label
794 if Error_Msg_Node_1
> Empty_Or_Error
795 and then Error_Posted
(Error_Msg_Node_1
)
800 if End_Type
= E_Case
then
801 Error_Msg_SC
("`END CASE;` expected@ for CASE#!");
803 elsif End_Type
= E_If
then
804 Error_Msg_SC
("`END IF;` expected@ for IF#!");
806 elsif End_Type
= E_Loop
then
807 if Error_Msg_Node_1
= Empty
then
809 ("`END LOOP;` expected@ for LOOP#!");
811 Error_Msg_SC
("`END LOOP &;` expected@!");
814 elsif End_Type
= E_Record
then
816 ("`END RECORD;` expected@ for RECORD#!");
818 elsif End_Type
= E_Select
then
820 ("`END SELECT;` expected@ for SELECT#!");
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).
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#!");
830 Error_Msg_SC
("`END &;` expected@!");
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!
837 else -- End_Type = E_Suspicious_Is or E_Bad_Is
838 Scope
.Table
(Scope
.Last
).Etyp
:= E_Bad_Is
;
840 end Output_End_Expected
;
842 ------------------------
843 -- Output End Missing --
844 ------------------------
846 procedure Output_End_Missing
is
847 End_Type
: SS_End_Type
;
850 -- Suppress message if this was a potentially junk entry (e.g. a
851 -- record entry where no record keyword was present.
853 if Scope
.Table
(Scope
.Last
).Junk
then
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
;
861 if End_Type
= E_Case
then
862 Error_Msg_BC
("missing `END CASE;` for CASE#!");
864 elsif End_Type
= E_If
then
865 Error_Msg_BC
("missing `END IF;` for IF#!");
867 elsif End_Type
= E_Loop
then
868 if Error_Msg_Node_1
= Empty
then
869 Error_Msg_BC
("missing `END LOOP;` for LOOP#!");
871 Error_Msg_BC
("missing `END LOOP &;`!");
874 elsif End_Type
= E_Record
then
876 ("missing `END RECORD;` for RECORD#!");
878 elsif End_Type
= E_Select
then
880 ("missing `END SELECT;` for SELECT#!");
882 elsif End_Type
= E_Name
then
883 if Error_Msg_Node_1
= Empty
then
884 Error_Msg_BC
("missing `END;` for BEGIN#!");
886 Error_Msg_BC
("missing `END &;`!");
889 else -- End_Type = E_Suspicious_Is or E_Bad_Is
890 Scope
.Table
(Scope
.Last
).Etyp
:= E_Bad_Is
;
892 end Output_End_Missing
;
894 ---------------------
895 -- Pop End Context --
896 ---------------------
898 procedure Pop_End_Context
is
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.
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.
911 -- If not at END, then output END expected message
913 if End_Type
= E_Dummy
then
916 End_Action
:= Insert_And_Accept
;
919 -- Otherwise we do have an END present
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:
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.
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.
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.
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
957 Restore_Scan_State
(Scan_State
); -- to END
958 Output_End_Expected
(Ins
=> True);
960 End_Action
:= Insert_And_Accept
;
964 -- Otherwise we go through the normal END evaluation procedure
966 Evaluate_End_Entry
(Scope
.Last
);
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.
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.
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
982 Error_Msg_Col
:= Scope
.Table
(Scope
.Last
).Ecol
;
984 ("(style) END in wrong column, should be@", End_Sloc
);
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.
992 if End_Labl_Present
and then Token
= Tok_End
then
994 Scan_State
: Saved_Scan_State
;
995 End_Loc
: constant Source_Ptr
:= Token_Ptr
;
997 Dup_Found
: Boolean := False;
1000 Save_Scan_State
(Scan_State
);
1004 if Token
= Tok_Identifier
1005 or else Token
= Tok_Operator_Symbol
1007 Nxt_Labl
:= P_Designator
;
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.
1013 if Token
= Tok_Semicolon
1014 and then Same_Label
(End_Labl
, Nxt_Labl
)
1015 and then End_Column
= Start_Column
1019 (No
(Scope
.Table
(Scope
.Last
- 1).Labl
)
1023 Scope
.Table
(Scope
.Last
- 1).Labl
)))
1026 Error_Msg
("duplicate end line ignored", End_Loc
);
1031 if not Dup_Found
then
1032 Restore_Scan_State
(Scan_State
);
1037 -- All OK, so return to caller indicating END is OK
1040 End_Action
:= Accept_As_Scanned
;
1044 -- If that check failed, then we definitely have an error. The issue
1045 -- is how to choose among three possible courses of action:
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.
1051 -- 2. Accept the current END text, scanning past it, and issuing
1052 -- an error message that it does not have the right form.
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.
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.
1066 Restore_Scan_State
(Scan_State
); -- to END
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.
1075 Pretty_Good
:= (Token_OK
and (Column_OK
or Label_OK
))
1076 or else Scope
.Table
(Scope
.Last
).Etyp
= E_Record
;
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
1082 for J
in reverse 1 .. Scope
.Last
- 1 loop
1083 Evaluate_End_Entry
(J
);
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.
1094 Outer_Match
:= Label_OK
and Column_OK
;
1096 Outer_Match
:= Label_OK
or Column_OK
;
1099 Outer_Match
:= False;
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).
1110 End_Action
:= Insert_And_Accept
;
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.
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:
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.
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
)
1140 Output_End_Expected
(Ins
=> False);
1142 End_Action
:= Skip_And_Accept
;
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.
1157 End_Action
:= Skip_And_Reject
;
1161 end Pop_End_Context
;
1167 function Same_Label
(Label1
, Label2
: Node_Id
) return Boolean is
1169 if Nkind
(Label1
) in N_Has_Chars
1170 and then Nkind
(Label2
) in N_Has_Chars
1172 return Chars
(Label1
) = Chars
(Label2
);
1174 elsif Nkind
(Label1
) = N_Selected_Component
1175 and then Nkind
(Label2
) = N_Selected_Component
1177 return Same_Label
(Prefix
(Label1
), Prefix
(Label2
)) and then
1178 Same_Label
(Selector_Name
(Label1
), Selector_Name
(Label2
));
1180 elsif Nkind
(Label1
) = N_Designator
1181 and then Nkind
(Label2
) = N_Defining_Program_Unit_Name
1183 return Same_Label
(Name
(Label1
), Name
(Label2
)) and then
1184 Same_Label
(Identifier
(Label1
), Defining_Identifier
(Label2
));