1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001, Free Software Foundation, Inc.
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. --
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). --
27 ------------------------------------------------------------------------------
29 with Uintp
; use Uintp
;
31 with GNAT
.Spelling_Checker
; use GNAT
.Spelling_Checker
;
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
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
59 if Token
/= Tok_Identifier
then
64 S
(J
) := Fold_Lower
(Tname
(Integer (J
) + 4));
67 Get_Name_String
(Token_Name
);
69 -- A special check for case of PROGRAM used for PROCEDURE
73 and then Name_Buffer
(1 .. 7) = "program"
75 Error_Msg_SC
("PROCEDURE expected");
79 -- A special check for an illegal abbrevation
81 elsif Name_Len
< S
'Length
82 and then Name_Len
>= 4
83 and then Name_Buffer
(1 .. Name_Len
) = S
(1 .. Name_Len
)
85 for J
in 1 .. S
'Last loop
86 M2
(P2
+ J
- 1) := Fold_Upper
(S
(J
));
89 Error_Msg_SC
(M2
(1 .. P2
- 1 + S
'Last));
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
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
)
111 Scan_Ptr
:= Token_Ptr
+ S
'Length;
112 Error_Msg_S
("missing space");
117 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
));
123 Error_Msg_SC
(M1
(1 .. P1
- 1 + S
'Last));
133 ----------------------
134 -- Check_95_Keyword --
135 ----------------------
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.
140 procedure Check_95_Keyword
(Token_95
, Next
: Token_Type
) is
141 Scan_State
: Saved_Scan_State
;
144 Save_Scan_State
(Scan_State
); -- at identifier/keyword
145 Scan
; -- past identifier/keyword
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!");
153 Restore_Scan_State
(Scan_State
); -- to identifier
155 end Check_95_Keyword
;
157 ----------------------
158 -- Check_Bad_Layout --
159 ----------------------
161 procedure Check_Bad_Layout
is
163 if Style
.RM_Column_Check
and then Token_Is_At_Start_Of_Line
164 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
166 Error_Msg_BC
("(style) incorrect layout");
168 end Check_Bad_Layout
;
170 --------------------------
171 -- Check_Misspelling_Of --
172 --------------------------
174 procedure Check_Misspelling_Of
(T
: Token_Type
) is
176 if Bad_Spelling_Of
(T
) then
179 end Check_Misspelling_Of
;
181 -----------------------------
182 -- Check_Simple_Expression --
183 -----------------------------
185 procedure Check_Simple_Expression
(E
: Node_Id
) is
187 if Expr_Form
= EF_Non_Simple
then
188 Error_Msg_N
("this expression must be parenthesized", E
);
190 end Check_Simple_Expression
;
192 ---------------------------------------
193 -- Check_Simple_Expression_In_Ada_83 --
194 ---------------------------------------
196 procedure Check_Simple_Expression_In_Ada_83
(E
: Node_Id
) is
198 if Expr_Form
= EF_Non_Simple
then
200 Error_Msg_N
("(Ada 83) this expression must be parenthesized!", E
);
203 end Check_Simple_Expression_In_Ada_83
;
205 ------------------------
206 -- Check_Subtype_Mark --
207 ------------------------
209 function Check_Subtype_Mark
(Mark
: Node_Id
) return Node_Id
is
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
)))
219 Error_Msg
("subtype mark expected", Sloc
(Mark
));
222 end Check_Subtype_Mark
;
228 function Comma_Present
return Boolean is
229 Scan_State
: Saved_Scan_State
;
233 -- First check, if a comma is present, then a comma is present!
235 if Token
= Tok_Comma
then
239 -- If we have a right paren, then that is taken as ending the list
240 -- i.e. no comma is present.
242 elsif Token
= Tok_Right_Paren
then
245 -- If pragmas, then get rid of them and make a recursive call
246 -- to process what follows these pragmas.
248 elsif Token
= Tok_Pragma
then
250 return Comma_Present
;
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
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.
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).
265 elsif Token
= Tok_Semicolon
then
266 Save_Scan_State
(Scan_State
);
267 Scan
; -- past semicolon
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)
273 if Token
= Tok_Identifier
then
276 if Token
= Tok_Arrow
then
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!
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.
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.
298 if Token
= Tok_Semicolon
or else Token
= Tok_EOF
then
299 Restore_Scan_State
(Scan_State
);
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.
306 elsif Token
= Tok_Comma
then
307 exit when Paren_Count
= 0;
309 -- A left paren just bumps the paren count
311 elsif Token
= Tok_Left_Paren
then
312 Paren_Count
:= Paren_Count
+ 1;
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.
319 elsif Token
= Tok_Right_Paren
then
320 exit when Paren_Count
= 0;
321 Paren_Count
:= Paren_Count
- 1;
324 -- Keep going, we haven't made a decision yet
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.
335 Restore_Scan_State
(Scan_State
);
336 Error_Msg_SC
(""";"" illegal here, replaced by "",""");
337 Scan
; -- past the semicolon
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)
345 elsif Token
in Token_Class_Eterm
then
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.
353 T_Comma
; -- to give error
358 -----------------------
359 -- Discard_Junk_List --
360 -----------------------
362 procedure Discard_Junk_List
(L
: List_Id
) is
365 end Discard_Junk_List
;
367 -----------------------
368 -- Discard_Junk_Node --
369 -----------------------
371 procedure Discard_Junk_Node
(N
: Node_Id
) is
374 end Discard_Junk_Node
;
380 procedure Ignore
(T
: Token_Type
) is
383 if T
= Tok_Comma
then
384 Error_Msg_SC
("unexpected "","" ignored");
386 elsif T
= Tok_Left_Paren
then
387 Error_Msg_SC
("unexpected ""("" ignored");
389 elsif T
= Tok_Right_Paren
then
390 Error_Msg_SC
("unexpected "")"" ignored");
392 elsif T
= Tok_Semicolon
then
393 Error_Msg_SC
("unexpected "";"" ignored");
397 Tname
: constant String := Token_Type
'Image (Token
);
398 Msg
: String := "unexpected keyword ????????????????????????";
401 -- Loop to copy characters of keyword name (ignoring Tok_)
403 for J
in 5 .. Tname
'Last loop
404 Msg
(J
+ 14) := Fold_Upper
(Tname
(J
));
407 Msg
(Tname
'Last + 15 .. Tname
'Last + 22) := " ignored";
408 Error_Msg_SC
(Msg
(1 .. Tname
'Last + 22));
412 Scan
; -- Scan past ignored token
416 ----------------------------
417 -- Is_Reserved_Identifier --
418 ----------------------------
420 function Is_Reserved_Identifier
return Boolean is
422 if not Is_Reserved_Keyword
(Token
) then
427 Ident_Casing
: constant Casing_Type
:=
428 Identifier_Casing
(Current_Source_File
);
430 Key_Casing
: constant Casing_Type
:=
431 Keyword_Casing
(Current_Source_File
);
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.
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
446 -- Otherwise assume that an identifier was intended
453 end Is_Reserved_Identifier
;
455 ----------------------
456 -- Merge_Identifier --
457 ----------------------
459 procedure Merge_Identifier
(Prev
: Node_Id
; Nxt
: Token_Type
) is
461 if Token
/= Tok_Identifier
then
466 S
: Saved_Scan_State
;
473 Restore_Scan_State
(S
);
480 -- Check exactly one space between identifiers
482 if Source
(Token_Ptr
- 1) /= ' '
483 or else Int
(Token_Ptr
) /=
484 Int
(Prev_Token_Ptr
) + Length_Of_Name
(Chars
(Prev
)) + 1
491 Get_Name_String
(Chars
(Token_Node
));
494 Buf
: String (1 .. Name_Len
) := Name_Buffer
(1 .. Name_Len
);
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
);
503 Error_Msg_Node_1
:= Prev
;
505 ("unexpected identifier, possibly & was meant here");
507 end Merge_Identifier
;
513 procedure No_Constraint
is
515 if Token
in Token_Class_Consk
then
516 Error_Msg_SC
("constraint not allowed here");
517 Discard_Junk_Node
(P_Constraint_Opt
);
525 function No_Right_Paren
(Expr
: Node_Id
) return Node_Id
is
527 if Token
= Tok_Right_Paren
then
528 Error_Msg_SC
("unexpected right parenthesis");
536 ---------------------
537 -- Pop_Scope_Stack --
538 ---------------------
540 procedure Pop_Scope_Stack
is
542 pragma Assert
(Scope
.Last
> 0);
543 Scope
.Decrement_Last
;
546 Error_Msg_Uint_1
:= UI_From_Int
(Scope
.Last
);
547 Error_Msg_SC
("decrement scope stack ptr, new value = ^!");
551 ----------------------
552 -- Push_Scope_Stack --
553 ----------------------
555 procedure Push_Scope_Stack
is
557 Scope
.Increment_Last
;
558 Scope
.Table
(Scope
.Last
).Junk
:= False;
559 Scope
.Table
(Scope
.Last
).Node
:= Empty
;
562 Error_Msg_Uint_1
:= UI_From_Int
(Scope
.Last
);
563 Error_Msg_SC
("increment scope stack ptr, new value = ^!");
565 end Push_Scope_Stack
;
567 ----------------------
568 -- Separate_Present --
569 ----------------------
571 function Separate_Present
return Boolean is
572 Scan_State
: Saved_Scan_State
;
575 if Token
= Tok_Separate
then
578 elsif Token
/= Tok_Identifier
then
582 Save_Scan_State
(Scan_State
);
583 Scan
; -- past identifier
585 if Token
= Tok_Semicolon
then
586 Restore_Scan_State
(Scan_State
);
587 return Bad_Spelling_Of
(Tok_Separate
);
590 Restore_Scan_State
(Scan_State
);
594 end Separate_Present
;
596 --------------------------
597 -- Signal_Bad_Attribute --
598 --------------------------
600 procedure Signal_Bad_Attribute
is
602 Error_Msg_N
("unrecognized attribute&", Token_Node
);
604 -- Check for possible misspelling
606 Get_Name_String
(Token_Name
);
609 AN
: constant String := Name_Buffer
(1 .. Name_Len
);
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
);
616 if Is_Bad_Spelling_Of
617 (AN
, Name_Buffer
(1 .. Name_Len
))
620 ("\possible misspelling of %", Token_Node
);
624 Error_Msg_Name_1
:= Error_Msg_Name_1
+ 1;
627 end Signal_Bad_Attribute
;
629 -------------------------------
630 -- Token_Is_At_Start_Of_Line --
631 -------------------------------
633 function Token_Is_At_Start_Of_Line
return Boolean is
635 return (Token_Ptr
= First_Non_Blank_Location
or else Token
= Tok_EOF
);
636 end Token_Is_At_Start_Of_Line
;