OSDN Git Service

2003-10-21 Arnaud Charlet <charlet@act-europe.fr>
[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-2003, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Uintp; use Uintp;
28
29 with GNAT.Spelling_Checker; use GNAT.Spelling_Checker;
30
31 separate (Par)
32 package body Util is
33
34    ---------------------
35    -- Bad_Spelling_Of --
36    ---------------------
37
38    function Bad_Spelling_Of (T : Token_Type) return Boolean is
39       Tname : constant String := Token_Type'Image (T);
40       --  Characters of token name
41
42       S : String (1 .. Tname'Last - 4);
43       --  Characters of token name folded to lower case, omitting TOK_ at start
44
45       M1 : String (1 .. 42) := "incorrect spelling of keyword ************";
46       M2 : String (1 .. 44) := "illegal abbreviation of keyword ************";
47       --  Buffers used to construct error message
48
49       P1 : constant := 30;
50       P2 : constant := 32;
51       --  Starting subscripts in M1, M2 for keyword name
52
53       SL : constant Natural := S'Length;
54       --  Length of expected token name excluding TOK_ at start
55
56    begin
57       if Token /= Tok_Identifier then
58          return False;
59       end if;
60
61       for J in S'Range loop
62          S (J) := Fold_Lower (Tname (Integer (J) + 4));
63       end loop;
64
65       Get_Name_String (Token_Name);
66
67       --  A special check for case of PROGRAM used for PROCEDURE
68
69       if T = Tok_Procedure
70         and then Name_Len = 7
71         and then Name_Buffer (1 .. 7) = "program"
72       then
73          Error_Msg_SC ("PROCEDURE expected");
74          Token := T;
75          return True;
76
77       --  A special check for an illegal abbrevation
78
79       elsif Name_Len < S'Length
80         and then Name_Len >= 4
81         and then Name_Buffer (1 .. Name_Len) = S (1 .. Name_Len)
82       then
83          for J in 1 .. S'Last loop
84             M2 (P2 + J - 1) := Fold_Upper (S (J));
85          end loop;
86
87          Error_Msg_SC (M2 (1 .. P2 - 1 + S'Last));
88          Token := T;
89          return True;
90       end if;
91
92       --  Now we go into the full circuit to check for a misspelling
93
94       --  Never consider something a misspelling if either the actual or
95       --  expected string is less than 3 characters (before this check we
96       --  used to consider i to be a misspelled if in some cases!)
97
98       if SL < 3 or else Name_Len < 3 then
99          return False;
100
101       --  Special case: prefix matches, i.e. the leading characters of the
102       --  token that we have exactly match the required keyword. If there
103       --  are at least two characters left over, assume that we have a case
104       --  of two keywords joined together which should not be joined.
105
106       elsif Name_Len > SL + 1
107         and then S = Name_Buffer (1 .. SL)
108       then
109          Scan_Ptr := Token_Ptr + S'Length;
110          Error_Msg_S ("missing space");
111          Token := T;
112          return True;
113       end if;
114
115       if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
116
117          for J in 1 .. S'Last loop
118             M1 (P1 + J - 1) := Fold_Upper (S (J));
119          end loop;
120
121          Error_Msg_SC (M1 (1 .. P1 - 1 + S'Last));
122          Token := T;
123          return True;
124
125       else
126          return False;
127       end if;
128
129    end Bad_Spelling_Of;
130
131    ----------------------
132    -- Check_95_Keyword --
133    ----------------------
134
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.
137
138    procedure Check_95_Keyword (Token_95, Next : Token_Type) is
139       Scan_State : Saved_Scan_State;
140
141    begin
142       Save_Scan_State (Scan_State); -- at identifier/keyword
143       Scan; -- past identifier/keyword
144
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;
154
155    ----------------------
156    -- Check_Bad_Layout --
157    ----------------------
158
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;
167
168    --------------------------
169    -- Check_Misspelling_Of --
170    --------------------------
171
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;
178
179    -----------------------------
180    -- Check_Simple_Expression --
181    -----------------------------
182
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;
189
190    ---------------------------------------
191    -- Check_Simple_Expression_In_Ada_83 --
192    ---------------------------------------
193
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_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;
202
203    ------------------------
204    -- Check_Subtype_Mark --
205    ------------------------
206
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;
221
222    -------------------
223    -- Comma_Present --
224    -------------------
225
226    function Comma_Present return Boolean is
227       Scan_State  : Saved_Scan_State;
228       Paren_Count : Nat;
229
230    begin
231       --  First check, if a comma is present, then a comma is present!
232
233       if Token = Tok_Comma then
234          T_Comma;
235          return True;
236
237       --  If we have a right paren, then that is taken as ending the list
238       --  i.e. no comma is present.
239
240       elsif Token = Tok_Right_Paren then
241          return False;
242
243       --  If pragmas, then get rid of them and make a recursive call
244       --  to process what follows these pragmas.
245
246       elsif Token = Tok_Pragma then
247          P_Pragmas_Misplaced;
248          return Comma_Present;
249
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
253
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.
258
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).
262
263       elsif Token = Tok_Semicolon then
264          Save_Scan_State (Scan_State);
265          Scan; -- past semicolon
266
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)
270
271          if Token = Tok_Identifier then
272             Scan;
273
274             if Token = Tok_Arrow then
275                goto Assume_Comma;
276             end if;
277          end if;
278
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!
282
283          Paren_Count := 0;
284
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.
288
289          loop
290
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.
295
296             if Token = Tok_Semicolon or else Token = Tok_EOF then
297                Restore_Scan_State (Scan_State);
298                return False;
299
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.
303
304             elsif Token = Tok_Comma then
305                exit when Paren_Count = 0;
306
307             --  A left paren just bumps the paren count
308
309             elsif Token = Tok_Left_Paren then
310                Paren_Count := Paren_Count + 1;
311
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.
316
317             elsif Token = Tok_Right_Paren then
318                exit when Paren_Count = 0;
319                Paren_Count := Paren_Count - 1;
320             end if;
321
322             --  Keep going, we haven't made a decision yet
323
324             Scan;
325          end loop;
326
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.
331
332          <<Assume_Comma>>
333             Restore_Scan_State (Scan_State);
334             Error_Msg_SC (""";"" illegal here, replaced by "",""");
335             Scan; -- past the semicolon
336             return True;
337
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)
342
343       elsif Token in Token_Class_Eterm then
344          return False;
345
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.
349
350       else
351          T_Comma; -- to give error
352          return True;
353       end if;
354    end Comma_Present;
355
356    -----------------------
357    -- Discard_Junk_List --
358    -----------------------
359
360    procedure Discard_Junk_List (L : List_Id) is
361       pragma Warnings (Off, L);
362
363    begin
364       null;
365    end Discard_Junk_List;
366
367    -----------------------
368    -- Discard_Junk_Node --
369    -----------------------
370
371    procedure Discard_Junk_Node (N : Node_Id) is
372       pragma Warnings (Off, N);
373
374    begin
375       null;
376    end Discard_Junk_Node;
377
378    ------------
379    -- Ignore --
380    ------------
381
382    procedure Ignore (T : Token_Type) is
383    begin
384       if Token = T then
385          if T = Tok_Comma then
386             Error_Msg_SC ("unexpected "","" ignored");
387
388          elsif T = Tok_Left_Paren then
389             Error_Msg_SC ("unexpected ""("" ignored");
390
391          elsif T = Tok_Right_Paren then
392             Error_Msg_SC ("unexpected "")"" ignored");
393
394          elsif T = Tok_Semicolon then
395             Error_Msg_SC ("unexpected "";"" ignored");
396
397          else
398             declare
399                Tname : constant String := Token_Type'Image (Token);
400                Msg   : String := "unexpected keyword ????????????????????????";
401
402             begin
403                --  Loop to copy characters of keyword name (ignoring Tok_)
404
405                for J in 5 .. Tname'Last loop
406                   Msg (J + 14) := Fold_Upper (Tname (J));
407                end loop;
408
409                Msg (Tname'Last + 15 .. Tname'Last + 22) := " ignored";
410                Error_Msg_SC (Msg (1 .. Tname'Last + 22));
411             end;
412          end if;
413
414          Scan; -- Scan past ignored token
415       end if;
416    end Ignore;
417
418    ----------------------------
419    -- Is_Reserved_Identifier --
420    ----------------------------
421
422    function Is_Reserved_Identifier return Boolean is
423    begin
424       if not Is_Reserved_Keyword (Token) then
425          return False;
426
427       else
428          declare
429             Ident_Casing : constant Casing_Type :=
430                              Identifier_Casing (Current_Source_File);
431
432             Key_Casing   : constant Casing_Type :=
433                              Keyword_Casing (Current_Source_File);
434
435          begin
436             --  If the casing of identifiers and keywords is different in
437             --  this source file, and the casing of this token matches the
438             --  keyword casing, then we return False, since it is pretty
439             --  clearly intended to be a keyword.
440
441             if Ident_Casing /= Unknown
442               and then Key_Casing /= Unknown
443               and then Ident_Casing /= Key_Casing
444               and then Determine_Token_Casing = Key_Casing
445             then
446                return False;
447
448             --  Otherwise assume that an identifier was intended
449
450             else
451                return True;
452             end if;
453          end;
454       end if;
455    end Is_Reserved_Identifier;
456
457    ----------------------
458    -- Merge_Identifier --
459    ----------------------
460
461    procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type) is
462    begin
463       if Token /= Tok_Identifier then
464          return;
465       end if;
466
467       declare
468          S : Saved_Scan_State;
469          T : Token_Type;
470
471       begin
472          Save_Scan_State (S);
473          Scan;
474          T := Token;
475          Restore_Scan_State (S);
476
477          if T /= Nxt then
478             return;
479          end if;
480       end;
481
482       --  Check exactly one space between identifiers
483
484       if Source (Token_Ptr - 1) /= ' '
485         or else Int (Token_Ptr) /=
486                   Int (Prev_Token_Ptr) + Length_Of_Name (Chars (Prev)) + 1
487       then
488          return;
489       end if;
490
491       --  Do the merge
492
493       Get_Name_String (Chars (Token_Node));
494
495       declare
496          Buf : constant String (1 .. Name_Len) :=
497                  Name_Buffer (1 .. Name_Len);
498
499       begin
500          Get_Name_String (Chars (Prev));
501          Add_Char_To_Name_Buffer ('_');
502          Add_Str_To_Name_Buffer (Buf);
503          Set_Chars (Prev, Name_Find);
504       end;
505
506       Error_Msg_Node_1 := Prev;
507       Error_Msg_SC
508         ("unexpected identifier, possibly & was meant here");
509       Scan;
510    end Merge_Identifier;
511
512    -------------------
513    -- No_Constraint --
514    -------------------
515
516    procedure No_Constraint is
517    begin
518       if Token in Token_Class_Consk then
519          Error_Msg_SC ("constraint not allowed here");
520          Discard_Junk_Node (P_Constraint_Opt);
521       end if;
522    end No_Constraint;
523
524    --------------------
525    -- No_Right_Paren --
526    --------------------
527
528    function No_Right_Paren (Expr : Node_Id) return Node_Id is
529    begin
530       if Token = Tok_Right_Paren then
531          Error_Msg_SC ("unexpected right parenthesis");
532          Resync_Expression;
533          return Error;
534       else
535          return Expr;
536       end if;
537    end No_Right_Paren;
538
539    ---------------------
540    -- Pop_Scope_Stack --
541    ---------------------
542
543    procedure Pop_Scope_Stack is
544    begin
545       pragma Assert (Scope.Last > 0);
546       Scope.Decrement_Last;
547
548       if Debug_Flag_P then
549          Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
550          Error_Msg_SC ("decrement scope stack ptr, new value = ^!");
551       end if;
552    end Pop_Scope_Stack;
553
554    ----------------------
555    -- Push_Scope_Stack --
556    ----------------------
557
558    procedure Push_Scope_Stack is
559    begin
560       Scope.Increment_Last;
561       Scope.Table (Scope.Last).Junk := False;
562       Scope.Table (Scope.Last).Node := Empty;
563
564       if Debug_Flag_P then
565          Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
566          Error_Msg_SC ("increment scope stack ptr, new value = ^!");
567       end if;
568    end Push_Scope_Stack;
569
570    ----------------------
571    -- Separate_Present --
572    ----------------------
573
574    function Separate_Present return Boolean is
575       Scan_State : Saved_Scan_State;
576
577    begin
578       if Token = Tok_Separate then
579          return True;
580
581       elsif Token /= Tok_Identifier then
582          return False;
583
584       else
585          Save_Scan_State (Scan_State);
586          Scan; -- past identifier
587
588          if Token = Tok_Semicolon then
589             Restore_Scan_State (Scan_State);
590             return Bad_Spelling_Of (Tok_Separate);
591
592          else
593             Restore_Scan_State (Scan_State);
594             return False;
595          end if;
596       end if;
597    end Separate_Present;
598
599    --------------------------
600    -- Signal_Bad_Attribute --
601    --------------------------
602
603    procedure Signal_Bad_Attribute is
604    begin
605       Error_Msg_N ("unrecognized attribute&", Token_Node);
606
607       --  Check for possible misspelling
608
609       Get_Name_String (Token_Name);
610
611       declare
612          AN : constant String := Name_Buffer (1 .. Name_Len);
613
614       begin
615          Error_Msg_Name_1 := First_Attribute_Name;
616          while Error_Msg_Name_1 <= Last_Attribute_Name loop
617             Get_Name_String (Error_Msg_Name_1);
618
619             if Is_Bad_Spelling_Of
620                  (AN, Name_Buffer (1 .. Name_Len))
621             then
622                Error_Msg_N
623                  ("\possible misspelling of %", Token_Node);
624                exit;
625             end if;
626
627             Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
628          end loop;
629       end;
630    end Signal_Bad_Attribute;
631
632    -----------------------------
633    -- Token_Is_At_End_Of_Line --
634    -----------------------------
635
636    function Token_Is_At_End_Of_Line return Boolean is
637       S : Source_Ptr;
638
639    begin
640       --  Skip past blanks and horizontal tabs
641
642       S := Scan_Ptr;
643       while Source (S) = ' ' or else Source (S) = ASCII.HT loop
644          S := S + 1;
645       end loop;
646
647       --  We are at end of line if at a control character (CR/LF/VT/FF/EOF)
648       --  or if we are at the start of an end of line comment sequence.
649
650       return Source (S) < ' '
651         or else (Source (S) = '-' and then Source (S + 1) = '-');
652    end Token_Is_At_End_Of_Line;
653
654    -------------------------------
655    -- Token_Is_At_Start_Of_Line --
656    -------------------------------
657
658    function Token_Is_At_Start_Of_Line return Boolean is
659    begin
660       return (Token_Ptr = First_Non_Blank_Location or else Token = Tok_EOF);
661    end Token_Is_At_Start_Of_Line;
662
663 end Util;