OSDN Git Service

* gcc-interface/Makefile.in (gnatlib-shared-default): Append
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-util.adb
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-2011, 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 Csets;    use Csets;
27 with Namet.Sp; use Namet.Sp;
28 with Stylesw;  use Stylesw;
29 with Uintp;    use Uintp;
30
31 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
32
33 separate (Par)
34 package body Util is
35
36    ---------------------
37    -- Bad_Spelling_Of --
38    ---------------------
39
40    function Bad_Spelling_Of (T : Token_Type) return Boolean is
41       Tname : constant String := Token_Type'Image (T);
42       --  Characters of token name
43
44       S : String (1 .. Tname'Last - 4);
45       --  Characters of token name folded to lower case, omitting TOK_ at start
46
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
50
51       P1 : constant := 30;
52       P2 : constant := 32;
53       --  Starting subscripts in M1, M2 for keyword name
54
55       SL : constant Natural := S'Length;
56       --  Length of expected token name excluding TOK_ at start
57
58    begin
59       if Token /= Tok_Identifier then
60          return False;
61       end if;
62
63       for J in S'Range loop
64          S (J) := Fold_Lower (Tname (J + 4));
65       end loop;
66
67       Get_Name_String (Token_Name);
68
69       --  A special check for case of PROGRAM used for PROCEDURE
70
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 -- CODEFIX
76            ("PROCEDURE expected");
77          Token := T;
78          return True;
79
80       --  A special check for an illegal abbreviation
81
82       elsif Name_Len < S'Length
83         and then Name_Len >= 4
84         and then Name_Buffer (1 .. Name_Len) = S (1 .. Name_Len)
85       then
86          for J in 1 .. S'Last loop
87             M2 (P2 + J - 1) := Fold_Upper (S (J));
88          end loop;
89
90          Error_Msg_SC (M2 (1 .. P2 - 1 + S'Last));
91          Token := T;
92          return True;
93       end if;
94
95       --  Now we go into the full circuit to check for a misspelling
96
97       --  Never consider something a misspelling if either the actual or
98       --  expected string is less than 3 characters (before this check we
99       --  used to consider i to be a misspelled if in some cases!)
100
101       if SL < 3 or else Name_Len < 3 then
102          return False;
103
104       --  Special case: prefix matches, i.e. the leading characters of the
105       --  token that we have exactly match the required keyword. If there
106       --  are at least two characters left over, assume that we have a case
107       --  of two keywords joined together which should not be joined.
108
109       elsif Name_Len > SL + 1
110         and then S = Name_Buffer (1 .. SL)
111       then
112          Scan_Ptr := Token_Ptr + S'Length;
113          Error_Msg_S ("|missing space");
114          Token := T;
115          return True;
116       end if;
117
118       if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
119          for J in 1 .. S'Last loop
120             M1 (P1 + J - 1) := Fold_Upper (S (J));
121          end loop;
122
123          Error_Msg_SC -- CODFIX
124            (M1 (1 .. P1 - 1 + S'Last));
125          Token := T;
126          return True;
127
128       else
129          return False;
130       end if;
131    end Bad_Spelling_Of;
132
133    ----------------------
134    -- Check_95_Keyword --
135    ----------------------
136
137    --  On entry, the caller has checked that current token is an identifier
138    --  whose name matches the name of the 95 keyword New_Tok.
139
140    procedure Check_95_Keyword (Token_95, Next : Token_Type) is
141       Scan_State : Saved_Scan_State;
142
143    begin
144       Save_Scan_State (Scan_State); -- at identifier/keyword
145       Scan; -- past identifier/keyword
146
147       if Token = Next then
148          Restore_Scan_State (Scan_State); -- to identifier
149          Error_Msg_Name_1 := Token_Name;
150          Error_Msg_SC ("(Ada 83) keyword* cannot be used!");
151          Token := Token_95;
152       else
153          Restore_Scan_State (Scan_State); -- to identifier
154       end if;
155    end Check_95_Keyword;
156
157    ----------------------
158    -- Check_Bad_Layout --
159    ----------------------
160
161    procedure Check_Bad_Layout is
162    begin
163       if RM_Column_Check and then Token_Is_At_Start_Of_Line
164         and then Start_Column <= Scope.Table (Scope.Last).Ecol
165       then
166          Error_Msg_BC -- CODEFIX
167            ("(style) incorrect layout");
168       end if;
169    end Check_Bad_Layout;
170
171    --------------------------
172    -- Check_Future_Keyword --
173    --------------------------
174
175    procedure Check_Future_Keyword is
176    begin
177       --  Ada 2005 (AI-284): Compiling in Ada 95 mode we warn that INTERFACE,
178       --  OVERRIDING, and SYNCHRONIZED are new reserved words.
179
180       if Ada_Version = Ada_95
181         and then Warn_On_Ada_2005_Compatibility
182       then
183          if Token_Name = Name_Overriding
184            or else Token_Name = Name_Synchronized
185            or else (Token_Name = Name_Interface
186                      and then Prev_Token /= Tok_Pragma)
187          then
188             Error_Msg_N ("& is a reserved word in Ada 2005?", Token_Node);
189          end if;
190       end if;
191
192       --  Similarly, warn about Ada 2012 reserved words
193
194       if Ada_Version in Ada_95 .. Ada_2005
195         and then Warn_On_Ada_2012_Compatibility
196       then
197          if Token_Name = Name_Some then
198             Error_Msg_N ("& is a reserved word in Ada 2012?", Token_Node);
199          end if;
200       end if;
201
202       --  Note: we deliberately do not emit these warnings when operating in
203       --  Ada 83 mode because in that case we assume the user is building
204       --  legacy code anyway and is not interested in updating Ada versions.
205
206    end Check_Future_Keyword;
207
208    --------------------------
209    -- Check_Misspelling_Of --
210    --------------------------
211
212    procedure Check_Misspelling_Of (T : Token_Type) is
213    begin
214       if Bad_Spelling_Of (T) then
215          null;
216       end if;
217    end Check_Misspelling_Of;
218
219    -----------------------------
220    -- Check_Simple_Expression --
221    -----------------------------
222
223    procedure Check_Simple_Expression (E : Node_Id) is
224    begin
225       if Expr_Form = EF_Non_Simple then
226          Error_Msg_N ("this expression must be parenthesized", E);
227       end if;
228    end Check_Simple_Expression;
229
230    ---------------------------------------
231    -- Check_Simple_Expression_In_Ada_83 --
232    ---------------------------------------
233
234    procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id) is
235    begin
236       if Expr_Form = EF_Non_Simple then
237          if Ada_Version = Ada_83 then
238             Error_Msg_N ("(Ada 83) this expression must be parenthesized!", E);
239          end if;
240       end if;
241    end Check_Simple_Expression_In_Ada_83;
242
243    ------------------------
244    -- Check_Subtype_Mark --
245    ------------------------
246
247    function Check_Subtype_Mark (Mark : Node_Id) return Node_Id is
248    begin
249       if Nkind (Mark) = N_Identifier
250         or else Nkind (Mark) = N_Selected_Component
251         or else (Nkind (Mark) = N_Attribute_Reference
252                   and then Is_Type_Attribute_Name (Attribute_Name (Mark)))
253         or else Mark = Error
254       then
255          return Mark;
256       else
257          Error_Msg ("subtype mark expected", Sloc (Mark));
258          return Error;
259       end if;
260    end Check_Subtype_Mark;
261
262    -------------------
263    -- Comma_Present --
264    -------------------
265
266    function Comma_Present return Boolean is
267       Scan_State  : Saved_Scan_State;
268       Paren_Count : Nat;
269
270    begin
271       --  First check, if a comma is present, then a comma is present!
272
273       if Token = Tok_Comma then
274          T_Comma;
275          return True;
276
277       --  If we have a right paren, then that is taken as ending the list
278       --  i.e. no comma is present.
279
280       elsif Token = Tok_Right_Paren then
281          return False;
282
283       --  If pragmas, then get rid of them and make a recursive call
284       --  to process what follows these pragmas.
285
286       elsif Token = Tok_Pragma then
287          P_Pragmas_Misplaced;
288          return Comma_Present;
289
290       --  At this stage we have an error, and the goal is to decide on whether
291       --  or not we should diagnose an error and report a (non-existent)
292       --  comma as being present, or simply to report no comma is present
293
294       --  If we are a semicolon, then the question is whether we have a missing
295       --  right paren, or whether the semicolon should have been a comma. To
296       --  guess the right answer, we scan ahead keeping track of the paren
297       --  level, looking for a clue that helps us make the right decision.
298
299       --  This approach is highly accurate in the single error case, and does
300       --  not make bad mistakes in the multiple error case (indeed we can't
301       --  really make a very bad decision at this point in any case).
302
303       elsif Token = Tok_Semicolon then
304          Save_Scan_State (Scan_State);
305          Scan; -- past semicolon
306
307          --  Check for being followed by identifier => which almost certainly
308          --  means we are still in a parameter list and the comma should have
309          --  been a semicolon (such a sequence could not follow a semicolon)
310
311          if Token = Tok_Identifier then
312             Scan;
313
314             if Token = Tok_Arrow then
315                goto Assume_Comma;
316             end if;
317          end if;
318
319          --  If that test didn't work, loop ahead looking for a comma or
320          --  semicolon at the same parenthesis level. Always remember that
321          --  we can't go badly wrong in an error situation like this!
322
323          Paren_Count := 0;
324
325          --  Here is the look ahead loop, Paren_Count tells us whether the
326          --  token we are looking at is at the same paren level as the
327          --  suspicious semicolon that we are trying to figure out.
328
329          loop
330
331             --  If we hit another semicolon or an end of file, and we have
332             --  not seen a right paren or another comma on the way, then
333             --  probably the semicolon did end the list. Indeed that is
334             --  certainly the only single error correction possible here.
335
336             if Token = Tok_Semicolon or else Token = Tok_EOF then
337                Restore_Scan_State (Scan_State);
338                return False;
339
340             --  A comma at the same paren level as the semicolon is a strong
341             --  indicator that the semicolon should have been a comma, indeed
342             --  again this is the only possible single error correction.
343
344             elsif Token = Tok_Comma then
345                exit when Paren_Count = 0;
346
347             --  A left paren just bumps the paren count
348
349             elsif Token = Tok_Left_Paren then
350                Paren_Count := Paren_Count + 1;
351
352             --  A right paren that is at the same paren level as the semicolon
353             --  also means that the only possible single error correction is
354             --  to assume that the semicolon should have been a comma. If we
355             --  are not at the same paren level, then adjust the paren level.
356
357             elsif Token = Tok_Right_Paren then
358                exit when Paren_Count = 0;
359                Paren_Count := Paren_Count - 1;
360             end if;
361
362             --  Keep going, we haven't made a decision yet
363
364             Scan;
365          end loop;
366
367          --  If we fall through the loop, it means that we found a terminating
368          --  right paren or another comma. In either case it is reasonable to
369          --  assume that the semicolon was really intended to be a comma. Also
370          --  come here for the identifier arrow case.
371
372          <<Assume_Comma>>
373             Restore_Scan_State (Scan_State);
374             Error_Msg_SC -- CODEFIX
375               ("|"";"" should be "",""");
376             Scan; -- past the semicolon
377             return True;
378
379       --  If we are not at semicolon or a right paren, then we base the
380       --  decision on whether or not the next token can be part of an
381       --  expression. If not, then decide that no comma is present (the
382       --  caller will eventually generate a missing right parent message)
383
384       elsif Token in Token_Class_Eterm then
385          return False;
386
387       --  Otherwise we assume a comma is present, even if none is present,
388       --  since the next token must be part of an expression, so if we were
389       --  at the end of the list, then there is more than one error present.
390
391       else
392          T_Comma; -- to give error
393          return True;
394       end if;
395    end Comma_Present;
396
397    -----------------------
398    -- Discard_Junk_List --
399    -----------------------
400
401    procedure Discard_Junk_List (L : List_Id) is
402       pragma Warnings (Off, L);
403    begin
404       null;
405    end Discard_Junk_List;
406
407    -----------------------
408    -- Discard_Junk_Node --
409    -----------------------
410
411    procedure Discard_Junk_Node (N : Node_Id) is
412       pragma Warnings (Off, N);
413    begin
414       null;
415    end Discard_Junk_Node;
416
417    ------------
418    -- Ignore --
419    ------------
420
421    procedure Ignore (T : Token_Type) is
422    begin
423       while Token = T loop
424          if T = Tok_Comma then
425             Error_Msg_SC -- CODEFIX
426               ("|extra "","" ignored");
427
428          elsif T = Tok_Left_Paren then
429             Error_Msg_SC -- CODEFIX
430               ("|extra ""("" ignored");
431
432          elsif T = Tok_Right_Paren then
433             Error_Msg_SC -- CODEFIX
434               ("|extra "")"" ignored");
435
436          elsif T = Tok_Semicolon then
437             Error_Msg_SC -- CODEFIX
438               ("|extra "";"" ignored");
439
440          elsif T = Tok_Colon then
441             Error_Msg_SC -- CODEFIX
442               ("|extra "":"" ignored");
443
444          else
445             declare
446                Tname : constant String := Token_Type'Image (Token);
447             begin
448                Error_Msg_SC ("|extra " & Tname (5 .. Tname'Last) & "ignored");
449             end;
450          end if;
451
452          Scan; -- Scan past ignored token
453       end loop;
454    end Ignore;
455
456    ----------------------------
457    -- Is_Reserved_Identifier --
458    ----------------------------
459
460    function Is_Reserved_Identifier (C : Id_Check := None) return Boolean is
461    begin
462       if not Is_Reserved_Keyword (Token) then
463          return False;
464
465       else
466          declare
467             Ident_Casing : constant Casing_Type :=
468                              Identifier_Casing (Current_Source_File);
469             Key_Casing   : constant Casing_Type :=
470                              Keyword_Casing (Current_Source_File);
471
472          begin
473             --  If the casing of identifiers and keywords is different in
474             --  this source file, and the casing of this token matches the
475             --  keyword casing, then we return False, since it is pretty
476             --  clearly intended to be a keyword.
477
478             if Ident_Casing = Unknown
479               or else Key_Casing = Unknown
480               or else Ident_Casing = Key_Casing
481               or else Determine_Token_Casing /= Key_Casing
482             then
483                return True;
484
485             --  Here we have a keyword written clearly with keyword casing.
486             --  In default mode, we would not be willing to consider this as
487             --  a reserved identifier, but if C is set, we may still accept it
488
489             elsif C /= None then
490                declare
491                   Scan_State  : Saved_Scan_State;
492                   OK_Next_Tok : Boolean;
493
494                begin
495                   Save_Scan_State (Scan_State);
496                   Scan;
497
498                   if Token_Is_At_Start_Of_Line then
499                      return False;
500                   end if;
501
502                   case C is
503                      when None =>
504                         raise Program_Error;
505
506                      when C_Comma_Right_Paren =>
507                         OK_Next_Tok :=
508                           Token = Tok_Comma or else Token = Tok_Right_Paren;
509
510                      when C_Comma_Colon =>
511                         OK_Next_Tok :=
512                           Token = Tok_Comma or else Token = Tok_Colon;
513
514                      when C_Do =>
515                         OK_Next_Tok :=
516                           Token = Tok_Do;
517
518                      when C_Dot =>
519                         OK_Next_Tok :=
520                           Token = Tok_Dot;
521
522                      when C_Greater_Greater =>
523                         OK_Next_Tok :=
524                           Token = Tok_Greater_Greater;
525
526                      when C_In =>
527                         OK_Next_Tok :=
528                           Token = Tok_In;
529
530                      when C_Is =>
531                         OK_Next_Tok :=
532                           Token = Tok_Is;
533
534                      when C_Left_Paren_Semicolon =>
535                         OK_Next_Tok :=
536                           Token = Tok_Left_Paren or else Token = Tok_Semicolon;
537
538                      when C_Use =>
539                         OK_Next_Tok :=
540                           Token = Tok_Use;
541
542                      when C_Vertical_Bar_Arrow =>
543                         OK_Next_Tok :=
544                           Token = Tok_Vertical_Bar or else Token = Tok_Arrow;
545                   end case;
546
547                   Restore_Scan_State (Scan_State);
548
549                   if OK_Next_Tok then
550                      return True;
551                   end if;
552                end;
553             end if;
554          end;
555       end if;
556
557       --  If we fall through it is not a reserved identifier
558
559       return False;
560    end Is_Reserved_Identifier;
561
562    ----------------------
563    -- Merge_Identifier --
564    ----------------------
565
566    procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type) is
567    begin
568       if Token /= Tok_Identifier then
569          return;
570       end if;
571
572       declare
573          S : Saved_Scan_State;
574          T : Token_Type;
575
576       begin
577          Save_Scan_State (S);
578          Scan;
579          T := Token;
580          Restore_Scan_State (S);
581
582          if T /= Nxt then
583             return;
584          end if;
585       end;
586
587       --  Check exactly one space between identifiers
588
589       if Source (Token_Ptr - 1) /= ' '
590         or else Int (Token_Ptr) /=
591                   Int (Prev_Token_Ptr) + Length_Of_Name (Chars (Prev)) + 1
592       then
593          return;
594       end if;
595
596       --  Do the merge
597
598       Get_Name_String (Chars (Token_Node));
599
600       declare
601          Buf : constant String (1 .. Name_Len) :=
602                  Name_Buffer (1 .. Name_Len);
603
604       begin
605          Get_Name_String (Chars (Prev));
606          Add_Char_To_Name_Buffer ('_');
607          Add_Str_To_Name_Buffer (Buf);
608          Set_Chars (Prev, Name_Find);
609       end;
610
611       Error_Msg_Node_1 := Prev;
612       Error_Msg_SC ("unexpected identifier, possibly & was meant here");
613       Scan;
614    end Merge_Identifier;
615
616    -------------------
617    -- Next_Token_Is --
618    -------------------
619
620    function Next_Token_Is (Tok : Token_Type) return Boolean is
621       Scan_State : Saved_Scan_State;
622       Result     : Boolean;
623    begin
624       Save_Scan_State (Scan_State);
625       Scan;
626       Result := (Token = Tok);
627       Restore_Scan_State (Scan_State);
628       return Result;
629    end Next_Token_Is;
630
631    -------------------
632    -- No_Constraint --
633    -------------------
634
635    procedure No_Constraint is
636    begin
637       if Token in Token_Class_Consk then
638          Error_Msg_SC ("constraint not allowed here");
639          Discard_Junk_Node (P_Constraint_Opt);
640       end if;
641    end No_Constraint;
642
643    ---------------------
644    -- Pop_Scope_Stack --
645    ---------------------
646
647    procedure Pop_Scope_Stack is
648    begin
649       pragma Assert (Scope.Last > 0);
650       Scope.Decrement_Last;
651
652       if Debug_Flag_P then
653          Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
654          Error_Msg_SC ("decrement scope stack ptr, new value = ^!");
655       end if;
656    end Pop_Scope_Stack;
657
658    ----------------------
659    -- Push_Scope_Stack --
660    ----------------------
661
662    procedure Push_Scope_Stack is
663    begin
664       Scope.Increment_Last;
665
666       if Style_Check_Max_Nesting_Level
667         and then Scope.Last = Style_Max_Nesting_Level + 1
668       then
669          Error_Msg
670            ("(style) maximum nesting level exceeded",
671             First_Non_Blank_Location);
672       end if;
673
674       Scope.Table (Scope.Last).Junk := False;
675       Scope.Table (Scope.Last).Node := Empty;
676
677       if Debug_Flag_P then
678          Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
679          Error_Msg_SC ("increment scope stack ptr, new value = ^!");
680       end if;
681    end Push_Scope_Stack;
682
683    ----------------------
684    -- Separate_Present --
685    ----------------------
686
687    function Separate_Present return Boolean is
688       Scan_State : Saved_Scan_State;
689
690    begin
691       if Token = Tok_Separate then
692          return True;
693
694       elsif Token /= Tok_Identifier then
695          return False;
696
697       else
698          Save_Scan_State (Scan_State);
699          Scan; -- past identifier
700
701          if Token = Tok_Semicolon then
702             Restore_Scan_State (Scan_State);
703             return Bad_Spelling_Of (Tok_Separate);
704
705          else
706             Restore_Scan_State (Scan_State);
707             return False;
708          end if;
709       end if;
710    end Separate_Present;
711
712    --------------------------
713    -- Signal_Bad_Attribute --
714    --------------------------
715
716    procedure Signal_Bad_Attribute is
717    begin
718       Error_Msg_N ("unrecognized attribute&", Token_Node);
719
720       --  Check for possible misspelling
721
722       Error_Msg_Name_1 := First_Attribute_Name;
723       while Error_Msg_Name_1 <= Last_Attribute_Name loop
724          if Is_Bad_Spelling_Of (Token_Name, Error_Msg_Name_1) then
725             Error_Msg_N -- CODEFIX
726               ("\possible misspelling of %", Token_Node);
727             exit;
728          end if;
729
730          Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
731       end loop;
732    end Signal_Bad_Attribute;
733
734    -----------------------------
735    -- Token_Is_At_End_Of_Line --
736    -----------------------------
737
738    function Token_Is_At_End_Of_Line return Boolean is
739       S : Source_Ptr;
740
741    begin
742       --  Skip past blanks and horizontal tabs
743
744       S := Scan_Ptr;
745       while Source (S) = ' ' or else Source (S) = ASCII.HT loop
746          S := S + 1;
747       end loop;
748
749       --  We are at end of line if at a control character (CR/LF/VT/FF/EOF)
750       --  or if we are at the start of an end of line comment sequence.
751
752       return Source (S) < ' '
753         or else (Source (S) = '-' and then Source (S + 1) = '-');
754    end Token_Is_At_End_Of_Line;
755
756    -------------------------------
757    -- Token_Is_At_Start_Of_Line --
758    -------------------------------
759
760    function Token_Is_At_Start_Of_Line return Boolean is
761    begin
762       return (Token_Ptr = First_Non_Blank_Location or else Token = Tok_EOF);
763    end Token_Is_At_Start_Of_Line;
764
765 end Util;