OSDN Git Service

New Language: Ada
[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 --                            $Revision: 1.64 $
10 --                                                                          --
11 --          Copyright (C) 1992-2001, Free Software Foundation, Inc.
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
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 (Integer (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 ("PROCEDURE expected");
76          Token := T;
77          return True;
78
79       --  A special check for an illegal abbrevation
80
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;
88
89          Error_Msg_SC (M2 (1 .. P2 - 1 + S'Last));
90          Token := T;
91          return True;
92       end if;
93
94       --  Now we go into the full circuit to check for a misspelling
95
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!)
99
100       if SL < 3 or else Name_Len < 3 then
101          return False;
102
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.
107
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;
116
117       if Is_Bad_Spelling_Of (Name_Buffer (1 .. Name_Len), S) then
118
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 (M1 (1 .. P1 - 1 + S'Last));
124          Token := T;
125          return True;
126
127       else
128          return False;
129       end if;
130
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 Style.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 ("(style) incorrect layout");
167       end if;
168    end Check_Bad_Layout;
169
170    --------------------------
171    -- Check_Misspelling_Of --
172    --------------------------
173
174    procedure Check_Misspelling_Of (T : Token_Type) is
175    begin
176       if Bad_Spelling_Of (T) then
177          null;
178       end if;
179    end Check_Misspelling_Of;
180
181    -----------------------------
182    -- Check_Simple_Expression --
183    -----------------------------
184
185    procedure Check_Simple_Expression (E : Node_Id) is
186    begin
187       if Expr_Form = EF_Non_Simple then
188          Error_Msg_N ("this expression must be parenthesized", E);
189       end if;
190    end Check_Simple_Expression;
191
192    ---------------------------------------
193    -- Check_Simple_Expression_In_Ada_83 --
194    ---------------------------------------
195
196    procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id) is
197    begin
198       if Expr_Form = EF_Non_Simple then
199          if Ada_83 then
200             Error_Msg_N ("(Ada 83) this expression must be parenthesized!", E);
201          end if;
202       end if;
203    end Check_Simple_Expression_In_Ada_83;
204
205    ------------------------
206    -- Check_Subtype_Mark --
207    ------------------------
208
209    function Check_Subtype_Mark (Mark : Node_Id) return Node_Id is
210    begin
211       if Nkind (Mark) = N_Identifier
212         or else Nkind (Mark) = N_Selected_Component
213         or else (Nkind (Mark) = N_Attribute_Reference
214                   and then Is_Type_Attribute_Name (Attribute_Name (Mark)))
215         or else Mark = Error
216       then
217          return Mark;
218       else
219          Error_Msg ("subtype mark expected", Sloc (Mark));
220          return Error;
221       end if;
222    end Check_Subtype_Mark;
223
224    -------------------
225    -- Comma_Present --
226    -------------------
227
228    function Comma_Present return Boolean is
229       Scan_State  : Saved_Scan_State;
230       Paren_Count : Nat;
231
232    begin
233       --  First check, if a comma is present, then a comma is present!
234
235       if Token = Tok_Comma then
236          T_Comma;
237          return True;
238
239       --  If we have a right paren, then that is taken as ending the list
240       --  i.e. no comma is present.
241
242       elsif Token = Tok_Right_Paren then
243          return False;
244
245       --  If pragmas, then get rid of them and make a recursive call
246       --  to process what follows these pragmas.
247
248       elsif Token = Tok_Pragma then
249          P_Pragmas_Misplaced;
250          return Comma_Present;
251
252       --  At this stage we have an error, and the goal is to decide on whether
253       --  or not we should diagnose an error and report a (non-existent)
254       --  comma as being present, or simply to report no comma is present
255
256       --  If we are a semicolon, then the question is whether we have a missing
257       --  right paren, or whether the semicolon should have been a comma. To
258       --  guess the right answer, we scan ahead keeping track of the paren
259       --  level, looking for a clue that helps us make the right decision.
260
261       --  This approach is highly accurate in the single error case, and does
262       --  not make bad mistakes in the multiple error case (indeed we can't
263       --  really make a very bad decision at this point in any case).
264
265       elsif Token = Tok_Semicolon then
266          Save_Scan_State (Scan_State);
267          Scan; -- past semicolon
268
269          --  Check for being followed by identifier => which almost certainly
270          --  means we are still in a parameter list and the comma should have
271          --  been a semicolon (such a sequence could not follow a semicolon)
272
273          if Token = Tok_Identifier then
274             Scan;
275
276             if Token = Tok_Arrow then
277                goto Assume_Comma;
278             end if;
279          end if;
280
281          --  If that test didn't work, loop ahead looking for a comma or
282          --  semicolon at the same parenthesis level. Always remember that
283          --  we can't go badly wrong in an error situation like this!
284
285          Paren_Count := 0;
286
287          --  Here is the look ahead loop, Paren_Count tells us whether the
288          --  token we are looking at is at the same paren level as the
289          --  suspicious semicolon that we are trying to figure out.
290
291          loop
292
293             --  If we hit another semicolon or an end of file, and we have
294             --  not seen a right paren or another comma on the way, then
295             --  probably the semicolon did end the list. Indeed that is
296             --  certainly the only single error correction possible here.
297
298             if Token = Tok_Semicolon or else Token = Tok_EOF then
299                Restore_Scan_State (Scan_State);
300                return False;
301
302             --  A comma at the same paren level as the semicolon is a strong
303             --  indicator that the semicolon should have been a comma, indeed
304             --  again this is the only possible single error correction.
305
306             elsif Token = Tok_Comma then
307                exit when Paren_Count = 0;
308
309             --  A left paren just bumps the paren count
310
311             elsif Token = Tok_Left_Paren then
312                Paren_Count := Paren_Count + 1;
313
314             --  A right paren that is at the same paren level as the semicolon
315             --  also means that the only possible single error correction is
316             --  to assume that the semicolon should have been a comma. If we
317             --  are not at the same paren level, then adjust the paren level.
318
319             elsif Token = Tok_Right_Paren then
320                exit when Paren_Count = 0;
321                Paren_Count := Paren_Count - 1;
322             end if;
323
324             --  Keep going, we haven't made a decision yet
325
326             Scan;
327          end loop;
328
329          --  If we fall through the loop, it means that we found a terminating
330          --  right paren or another comma. In either case it is reasonable to
331          --  assume that the semicolon was really intended to be a comma. Also
332          --  come here for the identifier arrow case.
333
334          <<Assume_Comma>>
335             Restore_Scan_State (Scan_State);
336             Error_Msg_SC (""";"" illegal here, replaced by "",""");
337             Scan; -- past the semicolon
338             return True;
339
340       --  If we are not at semicolon or a right paren, then we base the
341       --  decision on whether or not the next token can be part of an
342       --  expression. If not, then decide that no comma is present (the
343       --  caller will eventually generate a missing right parent message)
344
345       elsif Token in Token_Class_Eterm then
346          return False;
347
348       --  Otherwise we assume a comma is present, even if none is present,
349       --  since the next token must be part of an expression, so if we were
350       --  at the end of the list, then there is more than one error present.
351
352       else
353          T_Comma; -- to give error
354          return True;
355       end if;
356    end Comma_Present;
357
358    -----------------------
359    -- Discard_Junk_List --
360    -----------------------
361
362    procedure Discard_Junk_List (L : List_Id) is
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    begin
373       null;
374    end Discard_Junk_Node;
375
376    ------------
377    -- Ignore --
378    ------------
379
380    procedure Ignore (T : Token_Type) is
381    begin
382       if Token = T then
383          if T = Tok_Comma then
384             Error_Msg_SC ("unexpected "","" ignored");
385
386          elsif T = Tok_Left_Paren then
387             Error_Msg_SC ("unexpected ""("" ignored");
388
389          elsif T = Tok_Right_Paren then
390             Error_Msg_SC ("unexpected "")"" ignored");
391
392          elsif T = Tok_Semicolon then
393             Error_Msg_SC ("unexpected "";"" ignored");
394
395          else
396             declare
397                Tname : constant String := Token_Type'Image (Token);
398                Msg   : String := "unexpected keyword ????????????????????????";
399
400             begin
401                --  Loop to copy characters of keyword name (ignoring Tok_)
402
403                for J in 5 .. Tname'Last loop
404                   Msg (J + 14) := Fold_Upper (Tname (J));
405                end loop;
406
407                Msg (Tname'Last + 15 .. Tname'Last + 22) := " ignored";
408                Error_Msg_SC (Msg (1 .. Tname'Last + 22));
409             end;
410          end if;
411
412          Scan; -- Scan past ignored token
413       end if;
414    end Ignore;
415
416    ----------------------------
417    -- Is_Reserved_Identifier --
418    ----------------------------
419
420    function Is_Reserved_Identifier return Boolean is
421    begin
422       if not Is_Reserved_Keyword (Token) then
423          return False;
424
425       else
426          declare
427             Ident_Casing : constant Casing_Type :=
428                              Identifier_Casing (Current_Source_File);
429
430             Key_Casing   : constant Casing_Type :=
431                              Keyword_Casing (Current_Source_File);
432
433          begin
434             --  If the casing of identifiers and keywords is different in
435             --  this source file, and the casing of this token matches the
436             --  keyword casing, then we return False, since it is pretty
437             --  clearly intended to be a keyword.
438
439             if Ident_Casing /= Unknown
440               and then Key_Casing /= Unknown
441               and then Ident_Casing /= Key_Casing
442               and then Determine_Token_Casing = Key_Casing
443             then
444                return False;
445
446             --  Otherwise assume that an identifier was intended
447
448             else
449                return True;
450             end if;
451          end;
452       end if;
453    end Is_Reserved_Identifier;
454
455    ----------------------
456    -- Merge_Identifier --
457    ----------------------
458
459    procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type) is
460    begin
461       if Token /= Tok_Identifier then
462          return;
463       end if;
464
465       declare
466          S : Saved_Scan_State;
467          T : Token_Type;
468
469       begin
470          Save_Scan_State (S);
471          Scan;
472          T := Token;
473          Restore_Scan_State (S);
474
475          if T /= Nxt then
476             return;
477          end if;
478       end;
479
480       --  Check exactly one space between identifiers
481
482       if Source (Token_Ptr - 1) /= ' '
483         or else Int (Token_Ptr) /=
484                   Int (Prev_Token_Ptr) + Length_Of_Name (Chars (Prev)) + 1
485       then
486          return;
487       end if;
488
489       --  Do the merge
490
491       Get_Name_String (Chars (Token_Node));
492
493       declare
494          Buf : String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
495
496       begin
497          Get_Name_String (Chars (Prev));
498          Add_Char_To_Name_Buffer ('_');
499          Add_Str_To_Name_Buffer (Buf);
500          Set_Chars (Prev, Name_Find);
501       end;
502
503       Error_Msg_Node_1 := Prev;
504       Error_Msg_SC
505         ("unexpected identifier, possibly & was meant here");
506       Scan;
507    end Merge_Identifier;
508
509    -------------------
510    -- No_Constraint --
511    -------------------
512
513    procedure No_Constraint is
514    begin
515       if Token in Token_Class_Consk then
516          Error_Msg_SC ("constraint not allowed here");
517          Discard_Junk_Node (P_Constraint_Opt);
518       end if;
519    end No_Constraint;
520
521    --------------------
522    -- No_Right_Paren --
523    --------------------
524
525    function No_Right_Paren (Expr : Node_Id) return Node_Id is
526    begin
527       if Token = Tok_Right_Paren then
528          Error_Msg_SC ("unexpected right parenthesis");
529          Resync_Expression;
530          return Error;
531       else
532          return Expr;
533       end if;
534    end No_Right_Paren;
535
536    ---------------------
537    -- Pop_Scope_Stack --
538    ---------------------
539
540    procedure Pop_Scope_Stack is
541    begin
542       pragma Assert (Scope.Last > 0);
543       Scope.Decrement_Last;
544
545       if Debug_Flag_P then
546          Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
547          Error_Msg_SC ("decrement scope stack ptr, new value = ^!");
548       end if;
549    end Pop_Scope_Stack;
550
551    ----------------------
552    -- Push_Scope_Stack --
553    ----------------------
554
555    procedure Push_Scope_Stack is
556    begin
557       Scope.Increment_Last;
558       Scope.Table (Scope.Last).Junk := False;
559       Scope.Table (Scope.Last).Node := Empty;
560
561       if Debug_Flag_P then
562          Error_Msg_Uint_1 := UI_From_Int (Scope.Last);
563          Error_Msg_SC ("increment scope stack ptr, new value = ^!");
564       end if;
565    end Push_Scope_Stack;
566
567    ----------------------
568    -- Separate_Present --
569    ----------------------
570
571    function Separate_Present return Boolean is
572       Scan_State : Saved_Scan_State;
573
574    begin
575       if Token = Tok_Separate then
576          return True;
577
578       elsif Token /= Tok_Identifier then
579          return False;
580
581       else
582          Save_Scan_State (Scan_State);
583          Scan; -- past identifier
584
585          if Token = Tok_Semicolon then
586             Restore_Scan_State (Scan_State);
587             return Bad_Spelling_Of (Tok_Separate);
588
589          else
590             Restore_Scan_State (Scan_State);
591             return False;
592          end if;
593       end if;
594    end Separate_Present;
595
596    --------------------------
597    -- Signal_Bad_Attribute --
598    --------------------------
599
600    procedure Signal_Bad_Attribute is
601    begin
602       Error_Msg_N ("unrecognized attribute&", Token_Node);
603
604       --  Check for possible misspelling
605
606       Get_Name_String (Token_Name);
607
608       declare
609          AN : constant String := Name_Buffer (1 .. Name_Len);
610
611       begin
612          Error_Msg_Name_1 := First_Attribute_Name;
613          while Error_Msg_Name_1 <= Last_Attribute_Name loop
614             Get_Name_String (Error_Msg_Name_1);
615
616             if Is_Bad_Spelling_Of
617                  (AN, Name_Buffer (1 .. Name_Len))
618             then
619                Error_Msg_N
620                  ("\possible misspelling of %", Token_Node);
621                exit;
622             end if;
623
624             Error_Msg_Name_1 := Error_Msg_Name_1 + 1;
625          end loop;
626       end;
627    end Signal_Bad_Attribute;
628
629    -------------------------------
630    -- Token_Is_At_Start_Of_Line --
631    -------------------------------
632
633    function Token_Is_At_Start_Of_Line return Boolean is
634    begin
635       return (Token_Ptr = First_Non_Blank_Location or else Token = Tok_EOF);
636    end Token_Is_At_Start_Of_Line;
637
638 end Util;