Debian Codegraph @

gcc/ada/par-endh.adb

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