merge with trunk @ 139506
[official-gcc.git] / gcc / ada / par-util.adb
blobbf9d7dfe5a3afb344b953e783a77dbe654580a1a
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R . U T I L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2008, 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 ------------------------------------------------------------------------------
26 with Csets; use Csets;
27 with Namet.Sp; use Namet.Sp;
28 with Stylesw; use Stylesw;
29 with Uintp; use Uintp;
31 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
33 separate (Par)
34 package body Util is
36 ---------------------
37 -- Bad_Spelling_Of --
38 ---------------------
40 function Bad_Spelling_Of (T : Token_Type) return Boolean is
41 Tname : constant String := Token_Type'Image (T);
42 -- Characters of token name
44 S : String (1 .. Tname'Last - 4);
45 -- Characters of token name folded to lower case, omitting TOK_ at start
47 M1 : String (1 .. 42) := "incorrect spelling of keyword ************";
48 M2 : String (1 .. 44) := "illegal abbreviation of keyword ************";
49 -- Buffers used to construct error message
51 P1 : constant := 30;
52 P2 : constant := 32;
53 -- Starting subscripts in M1, M2 for keyword name
55 SL : constant Natural := S'Length;
56 -- Length of expected token name excluding TOK_ at start
58 begin
59 if Token /= Tok_Identifier then
60 return False;
61 end if;
63 for J in S'Range loop
64 S (J) := Fold_Lower (Tname (J + 4));
65 end loop;
67 Get_Name_String (Token_Name);
69 -- A special check for case of PROGRAM used for PROCEDURE
71 if T = Tok_Procedure
72 and then Name_Len = 7
73 and then Name_Buffer (1 .. 7) = "program"
74 then
75 Error_Msg_SC ("PROCEDURE expected");
76 Token := T;
77 return True;
79 -- A special check for an illegal abbreviation
81 elsif Name_Len < S'Length
82 and then Name_Len >= 4
83 and then Name_Buffer (1 .. Name_Len) = S (1 .. Name_Len)
84 then
85 for J in 1 .. S'Last loop
86 M2 (P2 + J - 1) := Fold_Upper (S (J));
87 end loop;
89 Error_Msg_SC (M2 (1 .. P2 - 1 + S'Last));
90 Token := T;
91 return True;
92 end if;
94 -- Now we go into the full circuit to check for a misspelling
96 -- Never consider something a misspelling if either the actual or
97 -- expected string is less than 3 characters (before this check we
98 -- used to consider i to be a misspelled if in some cases!)
100 if SL < 3 or else Name_Len < 3 then
101 return False;
103 -- Special case: prefix matches, i.e. the leading characters of the
104 -- token that we have exactly match the required keyword. If there
105 -- are at least two characters left over, assume that we have a case
106 -- of two keywords joined together which should not be joined.
108 elsif Name_Len > SL + 1
109 and then S = Name_Buffer (1 .. SL)
110 then
111 Scan_Ptr := Token_Ptr + S'Length;
112 Error_Msg_S ("|missing space");
113 Token := T;
114 return True;
115 end if;
117 if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
118 for J in 1 .. S'Last loop
119 M1 (P1 + J - 1) := Fold_Upper (S (J));
120 end loop;
122 Error_Msg_SC (M1 (1 .. P1 - 1 + S'Last));
123 Token := T;
124 return True;
126 else
127 return False;
128 end if;
129 end Bad_Spelling_Of;
131 ----------------------
132 -- Check_95_Keyword --
133 ----------------------
135 -- On entry, the caller has checked that current token is an identifier
136 -- whose name matches the name of the 95 keyword New_Tok.
138 procedure Check_95_Keyword (Token_95, Next : Token_Type) is
139 Scan_State : Saved_Scan_State;
141 begin
142 Save_Scan_State (Scan_State); -- at identifier/keyword
143 Scan; -- past identifier/keyword
145 if Token = Next then
146 Restore_Scan_State (Scan_State); -- to identifier
147 Error_Msg_Name_1 := Token_Name;
148 Error_Msg_SC ("(Ada 83) keyword* cannot be used!");
149 Token := Token_95;
150 else
151 Restore_Scan_State (Scan_State); -- to identifier
152 end if;
153 end Check_95_Keyword;
155 ----------------------
156 -- Check_Bad_Layout --
157 ----------------------
159 procedure Check_Bad_Layout is
160 begin
161 if Style.RM_Column_Check and then Token_Is_At_Start_Of_Line
162 and then Start_Column <= Scope.Table (Scope.Last).Ecol
163 then
164 Error_Msg_BC ("(style) incorrect layout");
165 end if;
166 end Check_Bad_Layout;
168 --------------------------
169 -- Check_Misspelling_Of --
170 --------------------------
172 procedure Check_Misspelling_Of (T : Token_Type) is
173 begin
174 if Bad_Spelling_Of (T) then
175 null;
176 end if;
177 end Check_Misspelling_Of;
179 -----------------------------
180 -- Check_Simple_Expression --
181 -----------------------------
183 procedure Check_Simple_Expression (E : Node_Id) is
184 begin
185 if Expr_Form = EF_Non_Simple then
186 Error_Msg_N ("this expression must be parenthesized", E);
187 end if;
188 end Check_Simple_Expression;
190 ---------------------------------------
191 -- Check_Simple_Expression_In_Ada_83 --
192 ---------------------------------------
194 procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id) is
195 begin
196 if Expr_Form = EF_Non_Simple then
197 if Ada_Version = Ada_83 then
198 Error_Msg_N ("(Ada 83) this expression must be parenthesized!", E);
199 end if;
200 end if;
201 end Check_Simple_Expression_In_Ada_83;
203 ------------------------
204 -- Check_Subtype_Mark --
205 ------------------------
207 function Check_Subtype_Mark (Mark : Node_Id) return Node_Id is
208 begin
209 if Nkind (Mark) = N_Identifier
210 or else Nkind (Mark) = N_Selected_Component
211 or else (Nkind (Mark) = N_Attribute_Reference
212 and then Is_Type_Attribute_Name (Attribute_Name (Mark)))
213 or else Mark = Error
214 then
215 return Mark;
216 else
217 Error_Msg ("subtype mark expected", Sloc (Mark));
218 return Error;
219 end if;
220 end Check_Subtype_Mark;
222 -------------------
223 -- Comma_Present --
224 -------------------
226 function Comma_Present return Boolean is
227 Scan_State : Saved_Scan_State;
228 Paren_Count : Nat;
230 begin
231 -- First check, if a comma is present, then a comma is present!
233 if Token = Tok_Comma then
234 T_Comma;
235 return True;
237 -- If we have a right paren, then that is taken as ending the list
238 -- i.e. no comma is present.
240 elsif Token = Tok_Right_Paren then
241 return False;
243 -- If pragmas, then get rid of them and make a recursive call
244 -- to process what follows these pragmas.
246 elsif Token = Tok_Pragma then
247 P_Pragmas_Misplaced;
248 return Comma_Present;
250 -- At this stage we have an error, and the goal is to decide on whether
251 -- or not we should diagnose an error and report a (non-existent)
252 -- comma as being present, or simply to report no comma is present
254 -- If we are a semicolon, then the question is whether we have a missing
255 -- right paren, or whether the semicolon should have been a comma. To
256 -- guess the right answer, we scan ahead keeping track of the paren
257 -- level, looking for a clue that helps us make the right decision.
259 -- This approach is highly accurate in the single error case, and does
260 -- not make bad mistakes in the multiple error case (indeed we can't
261 -- really make a very bad decision at this point in any case).
263 elsif Token = Tok_Semicolon then
264 Save_Scan_State (Scan_State);
265 Scan; -- past semicolon
267 -- Check for being followed by identifier => which almost certainly
268 -- means we are still in a parameter list and the comma should have
269 -- been a semicolon (such a sequence could not follow a semicolon)
271 if Token = Tok_Identifier then
272 Scan;
274 if Token = Tok_Arrow then
275 goto Assume_Comma;
276 end if;
277 end if;
279 -- If that test didn't work, loop ahead looking for a comma or
280 -- semicolon at the same parenthesis level. Always remember that
281 -- we can't go badly wrong in an error situation like this!
283 Paren_Count := 0;
285 -- Here is the look ahead loop, Paren_Count tells us whether the
286 -- token we are looking at is at the same paren level as the
287 -- suspicious semicolon that we are trying to figure out.
289 loop
291 -- If we hit another semicolon or an end of file, and we have
292 -- not seen a right paren or another comma on the way, then
293 -- probably the semicolon did end the list. Indeed that is
294 -- certainly the only single error correction possible here.
296 if Token = Tok_Semicolon or else Token = Tok_EOF then
297 Restore_Scan_State (Scan_State);
298 return False;
300 -- A comma at the same paren level as the semicolon is a strong
301 -- indicator that the semicolon should have been a comma, indeed
302 -- again this is the only possible single error correction.
304 elsif Token = Tok_Comma then
305 exit when Paren_Count = 0;
307 -- A left paren just bumps the paren count
309 elsif Token = Tok_Left_Paren then
310 Paren_Count := Paren_Count + 1;
312 -- A right paren that is at the same paren level as the semicolon
313 -- also means that the only possible single error correction is
314 -- to assume that the semicolon should have been a comma. If we
315 -- are not at the same paren level, then adjust the paren level.
317 elsif Token = Tok_Right_Paren then
318 exit when Paren_Count = 0;
319 Paren_Count := Paren_Count - 1;
320 end if;
322 -- Keep going, we haven't made a decision yet
324 Scan;
325 end loop;
327 -- If we fall through the loop, it means that we found a terminating
328 -- right paren or another comma. In either case it is reasonable to
329 -- assume that the semicolon was really intended to be a comma. Also
330 -- come here for the identifier arrow case.
332 <<Assume_Comma>>
333 Restore_Scan_State (Scan_State);
334 Error_Msg_SC ("|"";"" should be "",""");
335 Scan; -- past the semicolon
336 return True;
338 -- If we are not at semicolon or a right paren, then we base the
339 -- decision on whether or not the next token can be part of an
340 -- expression. If not, then decide that no comma is present (the
341 -- caller will eventually generate a missing right parent message)
343 elsif Token in Token_Class_Eterm then
344 return False;
346 -- Otherwise we assume a comma is present, even if none is present,
347 -- since the next token must be part of an expression, so if we were
348 -- at the end of the list, then there is more than one error present.
350 else
351 T_Comma; -- to give error
352 return True;
353 end if;
354 end Comma_Present;
356 -----------------------
357 -- Discard_Junk_List --
358 -----------------------
360 procedure Discard_Junk_List (L : List_Id) is
361 pragma Warnings (Off, L);
362 begin
363 null;
364 end Discard_Junk_List;
366 -----------------------
367 -- Discard_Junk_Node --
368 -----------------------
370 procedure Discard_Junk_Node (N : Node_Id) is
371 pragma Warnings (Off, N);
372 begin
373 null;
374 end Discard_Junk_Node;
376 ------------
377 -- Ignore --
378 ------------
380 procedure Ignore (T : Token_Type) is
381 begin
382 while Token = T loop
383 if T = Tok_Comma then
384 Error_Msg_SC ("|extra "","" ignored");
386 elsif T = Tok_Left_Paren then
387 Error_Msg_SC ("|extra ""("" ignored");
389 elsif T = Tok_Right_Paren then
390 Error_Msg_SC ("|extra "")"" ignored");
392 elsif T = Tok_Semicolon then
393 Error_Msg_SC ("|extra "";"" ignored");
395 elsif T = Tok_Colon then
396 Error_Msg_SC ("|extra "":"" ignored");
398 else
399 declare
400 Tname : constant String := Token_Type'Image (Token);
401 begin
402 Error_Msg_SC
403 ("|extra " & Tname (5 .. Tname'Last) & "ignored");
404 end;
405 end if;
407 Scan; -- Scan past ignored token
408 end loop;
409 end Ignore;
411 ----------------------------
412 -- Is_Reserved_Identifier --
413 ----------------------------
415 function Is_Reserved_Identifier (C : Id_Check := None) return Boolean is
416 begin
417 if not Is_Reserved_Keyword (Token) then
418 return False;
420 else
421 declare
422 Ident_Casing : constant Casing_Type :=
423 Identifier_Casing (Current_Source_File);
424 Key_Casing : constant Casing_Type :=
425 Keyword_Casing (Current_Source_File);
427 begin
428 -- If the casing of identifiers and keywords is different in
429 -- this source file, and the casing of this token matches the
430 -- keyword casing, then we return False, since it is pretty
431 -- clearly intended to be a keyword.
433 if Ident_Casing = Unknown
434 or else Key_Casing = Unknown
435 or else Ident_Casing = Key_Casing
436 or else Determine_Token_Casing /= Key_Casing
437 then
438 return True;
440 -- Here we have a keyword written clearly with keyword casing.
441 -- In default mode, we would not be willing to consider this as
442 -- a reserved identifier, but if C is set, we may still accept it
444 elsif C /= None then
445 declare
446 Scan_State : Saved_Scan_State;
447 OK_Next_Tok : Boolean;
449 begin
450 Save_Scan_State (Scan_State);
451 Scan;
453 if Token_Is_At_Start_Of_Line then
454 return False;
455 end if;
457 case C is
458 when None =>
459 raise Program_Error;
461 when C_Comma_Right_Paren =>
462 OK_Next_Tok :=
463 Token = Tok_Comma or else Token = Tok_Right_Paren;
465 when C_Comma_Colon =>
466 OK_Next_Tok :=
467 Token = Tok_Comma or else Token = Tok_Colon;
469 when C_Do =>
470 OK_Next_Tok :=
471 Token = Tok_Do;
473 when C_Dot =>
474 OK_Next_Tok :=
475 Token = Tok_Dot;
477 when C_Greater_Greater =>
478 OK_Next_Tok :=
479 Token = Tok_Greater_Greater;
481 when C_In =>
482 OK_Next_Tok :=
483 Token = Tok_In;
485 when C_Is =>
486 OK_Next_Tok :=
487 Token = Tok_Is;
489 when C_Left_Paren_Semicolon =>
490 OK_Next_Tok :=
491 Token = Tok_Left_Paren or else Token = Tok_Semicolon;
493 when C_Use =>
494 OK_Next_Tok :=
495 Token = Tok_Use;
497 when C_Vertical_Bar_Arrow =>
498 OK_Next_Tok :=
499 Token = Tok_Vertical_Bar or else Token = Tok_Arrow;
500 end case;
502 Restore_Scan_State (Scan_State);
504 if OK_Next_Tok then
505 return True;
506 end if;
507 end;
508 end if;
509 end;
510 end if;
512 -- If we fall through it is not a reserved identifier
514 return False;
515 end Is_Reserved_Identifier;
517 ----------------------
518 -- Merge_Identifier --
519 ----------------------
521 procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type) is
522 begin
523 if Token /= Tok_Identifier then
524 return;
525 end if;
527 declare
528 S : Saved_Scan_State;
529 T : Token_Type;
531 begin
532 Save_Scan_State (S);
533 Scan;
534 T := Token;
535 Restore_Scan_State (S);
537 if T /= Nxt then
538 return;
539 end if;
540 end;
542 -- Check exactly one space between identifiers
544 if Source (Token_Ptr - 1) /= ' '
545 or else Int (Token_Ptr) /=
546 Int (Prev_Token_Ptr) + Length_Of_Name (Chars (Prev)) + 1
547 then
548 return;
549 end if;
551 -- Do the merge
553 Get_Name_String (Chars (Token_Node));
555 declare
556 Buf : constant String (1 .. Name_Len) :=
557 Name_Buffer (1 .. Name_Len);
559 begin
560 Get_Name_String (Chars (Prev));
561 Add_Char_To_Name_Buffer ('_');
562 Add_Str_To_Name_Buffer (Buf);
563 Set_Chars (Prev, Name_Find);
564 end;
566 Error_Msg_Node_1 := Prev;
567 Error_Msg_SC
568 ("unexpected identifier, possibly & was meant here");
569 Scan;
570 end Merge_Identifier;
572 -------------------
573 -- Next_Token_Is --
574 -------------------
576 function Next_Token_Is (Tok : Token_Type) return Boolean is
577 Scan_State : Saved_Scan_State;
578 Result : Boolean;
579 begin
580 Save_Scan_State (Scan_State);
581 Scan;
582 Result := (Token = Tok);
583 Restore_Scan_State (Scan_State);
584 return Result;
585 end Next_Token_Is;
587 -------------------
588 -- No_Constraint --
589 -------------------
591 procedure No_Constraint is
592 begin
593 if Token in Token_Class_Consk then
594 Error_Msg_SC ("constraint not allowed here");
595 Discard_Junk_Node (P_Constraint_Opt);
596 end if;
597 end No_Constraint;
599 ---------------------
600 -- Pop_Scope_Stack --
601 ---------------------
603 procedure Pop_Scope_Stack is
604 begin
605 pragma Assert (Scope.Last > 0);
606 Scope.Decrement_Last;
608 if Debug_Flag_P then
609 Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
610 Error_Msg_SC ("decrement scope stack ptr, new value = ^!");
611 end if;
612 end Pop_Scope_Stack;
614 ----------------------
615 -- Push_Scope_Stack --
616 ----------------------
618 procedure Push_Scope_Stack is
619 begin
620 Scope.Increment_Last;
622 if Style_Check_Max_Nesting_Level
623 and then Scope.Last = Style_Max_Nesting_Level + 1
624 then
625 Error_Msg
626 ("(style) maximum nesting level exceeded",
627 First_Non_Blank_Location);
628 end if;
630 Scope.Table (Scope.Last).Junk := False;
631 Scope.Table (Scope.Last).Node := Empty;
633 if Debug_Flag_P then
634 Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
635 Error_Msg_SC ("increment scope stack ptr, new value = ^!");
636 end if;
637 end Push_Scope_Stack;
639 ----------------------
640 -- Separate_Present --
641 ----------------------
643 function Separate_Present return Boolean is
644 Scan_State : Saved_Scan_State;
646 begin
647 if Token = Tok_Separate then
648 return True;
650 elsif Token /= Tok_Identifier then
651 return False;
653 else
654 Save_Scan_State (Scan_State);
655 Scan; -- past identifier
657 if Token = Tok_Semicolon then
658 Restore_Scan_State (Scan_State);
659 return Bad_Spelling_Of (Tok_Separate);
661 else
662 Restore_Scan_State (Scan_State);
663 return False;
664 end if;
665 end if;
666 end Separate_Present;
668 --------------------------
669 -- Signal_Bad_Attribute --
670 --------------------------
672 procedure Signal_Bad_Attribute is
673 begin
674 Error_Msg_N ("unrecognized attribute&", Token_Node);
676 -- Check for possible misspelling
678 Error_Msg_Name_1 := First_Attribute_Name;
679 while Error_Msg_Name_1 <= Last_Attribute_Name loop
680 if Is_Bad_Spelling_Of (Token_Name, Error_Msg_Name_1) then
681 Error_Msg_N ("\possible misspelling of %", Token_Node);
682 exit;
683 end if;
685 Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
686 end loop;
687 end Signal_Bad_Attribute;
689 -----------------------------
690 -- Token_Is_At_End_Of_Line --
691 -----------------------------
693 function Token_Is_At_End_Of_Line return Boolean is
694 S : Source_Ptr;
696 begin
697 -- Skip past blanks and horizontal tabs
699 S := Scan_Ptr;
700 while Source (S) = ' ' or else Source (S) = ASCII.HT loop
701 S := S + 1;
702 end loop;
704 -- We are at end of line if at a control character (CR/LF/VT/FF/EOF)
705 -- or if we are at the start of an end of line comment sequence.
707 return Source (S) < ' '
708 or else (Source (S) = '-' and then Source (S + 1) = '-');
709 end Token_Is_At_End_Of_Line;
711 -------------------------------
712 -- Token_Is_At_Start_Of_Line --
713 -------------------------------
715 function Token_Is_At_Start_Of_Line return Boolean is
716 begin
717 return (Token_Ptr = First_Non_Blank_Location or else Token = Tok_EOF);
718 end Token_Is_At_Start_Of_Line;
720 end Util;