1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Csets
; use Csets
;
27 with Namet
.Sp
; use Namet
.Sp
;
28 with Stylesw
; use Stylesw
;
29 with Uintp
; use Uintp
;
30 with Warnsw
; use Warnsw
;
32 with GNAT
.Spelling_Checker
; use GNAT
.Spelling_Checker
;
41 function Bad_Spelling_Of
(T
: Token_Type
) return Boolean is
42 Tname
: constant String := Token_Type
'Image (T
);
43 -- Characters of token name
45 S
: String (1 .. Tname
'Last - 4);
46 -- Characters of token name folded to lower case, omitting TOK_ at start
48 M1
: String (1 .. 42) := "incorrect spelling of keyword ************";
49 M2
: String (1 .. 44) := "illegal abbreviation of keyword ************";
50 -- Buffers used to construct error message
54 -- Starting subscripts in M1, M2 for keyword name
56 SL
: constant Natural := S
'Length;
57 -- Length of expected token name excluding TOK_ at start
60 if Token
/= Tok_Identifier
then
65 S
(J
) := Fold_Lower
(Tname
(J
+ 4));
68 Get_Name_String
(Token_Name
);
70 -- A special check for case of PROGRAM used for PROCEDURE
74 and then Name_Buffer
(1 .. 7) = "program"
76 Error_Msg_SC
-- CODEFIX
77 ("PROCEDURE expected");
81 -- A special check for an illegal abbreviation
83 elsif Name_Len
< S
'Length
84 and then Name_Len
>= 4
85 and then Name_Buffer
(1 .. Name_Len
) = S
(1 .. Name_Len
)
87 for J
in 1 .. S
'Last loop
88 M2
(P2
+ J
- 1) := Fold_Upper
(S
(J
));
91 Error_Msg_SC
(M2
(1 .. P2
- 1 + S
'Last));
96 -- Now we go into the full circuit to check for a misspelling
98 -- Never consider something a misspelling if either the actual or
99 -- expected string is less than 3 characters (before this check we
100 -- used to consider i to be a misspelled if in some cases).
102 if SL
< 3 or else Name_Len
< 3 then
105 -- Special case: prefix matches, i.e. the leading characters of the
106 -- token that we have exactly match the required keyword. If there
107 -- are at least two characters left over, assume that we have a case
108 -- of two keywords joined together which should not be joined.
110 elsif Name_Len
> SL
+ 1
111 and then S
= Name_Buffer
(1 .. SL
)
113 Scan_Ptr
:= Token_Ptr
+ S
'Length;
114 Error_Msg_S
("|missing space");
119 if Is_Bad_Spelling_Of
(Name_Buffer
(1 .. Name_Len
), S
) then
120 for J
in 1 .. S
'Last loop
121 M1
(P1
+ J
- 1) := Fold_Upper
(S
(J
));
124 Error_Msg_SC
-- CODFIX
125 (M1
(1 .. P1
- 1 + S
'Last));
134 ----------------------
135 -- Check_95_Keyword --
136 ----------------------
138 -- On entry, the caller has checked that current token is an identifier
139 -- whose name matches the name of the 95 keyword New_Tok.
141 procedure Check_95_Keyword
(Token_95
, Next
: Token_Type
) is
142 Scan_State
: Saved_Scan_State
;
145 Save_Scan_State
(Scan_State
); -- at identifier/keyword
146 Scan
; -- past identifier/keyword
149 Restore_Scan_State
(Scan_State
); -- to identifier
150 Error_Msg_Name_1
:= Token_Name
;
151 Error_Msg_SC
("(Ada 83) keyword* cannot be used!");
154 Restore_Scan_State
(Scan_State
); -- to identifier
156 end Check_95_Keyword
;
158 ----------------------
159 -- Check_Bad_Layout --
160 ----------------------
162 procedure Check_Bad_Layout
is
164 if RM_Column_Check
and then Token_Is_At_Start_Of_Line
165 and then Start_Column
<= Scope
.Table
(Scope
.Last
).Ecol
167 Error_Msg_BC
-- CODEFIX
168 ("(style) incorrect layout");
170 end Check_Bad_Layout
;
172 --------------------------
173 -- Check_Future_Keyword --
174 --------------------------
176 procedure Check_Future_Keyword
is
178 -- Ada 2005 (AI-284): Compiling in Ada 95 mode we warn that INTERFACE,
179 -- OVERRIDING, and SYNCHRONIZED are new reserved words.
181 if Ada_Version
= Ada_95
182 and then Warn_On_Ada_2005_Compatibility
184 if Nam_In
(Token_Name
, Name_Overriding
, Name_Synchronized
)
185 or else (Token_Name
= Name_Interface
186 and then Prev_Token
/= Tok_Pragma
)
188 Error_Msg_N
("& is a reserved word in Ada 2005?y?", Token_Node
);
192 -- Similarly, warn about Ada 2012 reserved words
194 if Ada_Version
in Ada_95
.. Ada_2005
195 and then Warn_On_Ada_2012_Compatibility
197 if Token_Name
= Name_Some
then
198 Error_Msg_N
("& is a reserved word in Ada 2012?y?", Token_Node
);
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.
206 end Check_Future_Keyword
;
208 --------------------------
209 -- Check_Misspelling_Of --
210 --------------------------
212 procedure Check_Misspelling_Of
(T
: Token_Type
) is
214 if Bad_Spelling_Of
(T
) then
217 end Check_Misspelling_Of
;
219 -----------------------------
220 -- Check_Simple_Expression --
221 -----------------------------
223 procedure Check_Simple_Expression
(E
: Node_Id
) is
225 if Expr_Form
= EF_Non_Simple
then
226 Error_Msg_N
("this expression must be parenthesized", E
);
228 end Check_Simple_Expression
;
230 ---------------------------------------
231 -- Check_Simple_Expression_In_Ada_83 --
232 ---------------------------------------
234 procedure Check_Simple_Expression_In_Ada_83
(E
: Node_Id
) is
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
);
241 end Check_Simple_Expression_In_Ada_83
;
243 ------------------------
244 -- Check_Subtype_Mark --
245 ------------------------
247 function Check_Subtype_Mark
(Mark
: Node_Id
) return Node_Id
is
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
)))
257 Error_Msg
("subtype mark expected", Sloc
(Mark
));
260 end Check_Subtype_Mark
;
266 function Comma_Present
return Boolean is
267 Scan_State
: Saved_Scan_State
;
271 -- First check, if a comma is present, then a comma is present
273 if Token
= Tok_Comma
then
277 -- If we have a right paren, then that is taken as ending the list
278 -- i.e. no comma is present.
280 elsif Token
= Tok_Right_Paren
then
283 -- If pragmas, then get rid of them and make a recursive call
284 -- to process what follows these pragmas.
286 elsif Token
= Tok_Pragma
then
288 return Comma_Present
;
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
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.
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).
303 elsif Token
= Tok_Semicolon
then
304 Save_Scan_State
(Scan_State
);
305 Scan
; -- past semicolon
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)
311 if Token
= Tok_Identifier
then
314 if Token
= Tok_Arrow
then
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.
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.
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.
336 if Token
= Tok_Semicolon
or else Token
= Tok_EOF
then
337 Restore_Scan_State
(Scan_State
);
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.
344 elsif Token
= Tok_Comma
then
345 exit when Paren_Count
= 0;
347 -- A left paren just bumps the paren count
349 elsif Token
= Tok_Left_Paren
then
350 Paren_Count
:= Paren_Count
+ 1;
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.
357 elsif Token
= Tok_Right_Paren
then
358 exit when Paren_Count
= 0;
359 Paren_Count
:= Paren_Count
- 1;
362 -- Keep going, we haven't made a decision yet
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.
373 Restore_Scan_State
(Scan_State
);
374 Error_Msg_SC
-- CODEFIX
375 ("|"";"" should be "",""");
376 Scan
; -- past the semicolon
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)
384 elsif Token
in Token_Class_Eterm
then
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.
392 T_Comma
; -- to give error
397 -----------------------
398 -- Discard_Junk_List --
399 -----------------------
401 procedure Discard_Junk_List
(L
: List_Id
) is
402 pragma Warnings
(Off
, L
);
405 end Discard_Junk_List
;
407 -----------------------
408 -- Discard_Junk_Node --
409 -----------------------
411 procedure Discard_Junk_Node
(N
: Node_Id
) is
412 pragma Warnings
(Off
, N
);
415 end Discard_Junk_Node
;
421 procedure Ignore
(T
: Token_Type
) is
424 if T
= Tok_Comma
then
425 Error_Msg_SC
-- CODEFIX
426 ("|extra "","" ignored");
428 elsif T
= Tok_Left_Paren
then
429 Error_Msg_SC
-- CODEFIX
430 ("|extra ""("" ignored");
432 -- Note: the following error used to be labeled as a non-serious
433 -- error like the other similar messages here (with a | at the start
434 -- of the message). But this caused some annoying cascaded errors
435 -- that were confusing, as shown by this example:
437 -- A : array (1 .. 9) of Integer :=
440 -- >>> positional aggregate cannot have one component
441 -- >>> named association cannot follow positional association
442 -- >>> extra ")" ignored
444 -- So we decided to label it as serious after all
446 elsif T
= Tok_Right_Paren
then
447 Error_Msg_SC
-- CODEFIX
448 ("extra "")"" ignored");
450 elsif T
= Tok_Semicolon
then
451 Error_Msg_SC
-- CODEFIX
452 ("|extra "";"" ignored");
454 elsif T
= Tok_Colon
then
455 Error_Msg_SC
-- CODEFIX
456 ("|extra "":"" ignored");
460 Tname
: constant String := Token_Type
'Image (Token
);
462 Error_Msg_SC
("|extra " & Tname
(5 .. Tname
'Last) & "ignored");
466 Scan
; -- Scan past ignored token
470 ----------------------------
471 -- Is_Reserved_Identifier --
472 ----------------------------
474 function Is_Reserved_Identifier
(C
: Id_Check
:= None
) return Boolean is
476 if not Is_Reserved_Keyword
(Token
) then
481 Ident_Casing
: constant Casing_Type
:=
482 Identifier_Casing
(Current_Source_File
);
483 Key_Casing
: constant Casing_Type
:=
484 Keyword_Casing
(Current_Source_File
);
487 -- If the casing of identifiers and keywords is different in
488 -- this source file, and the casing of this token matches the
489 -- keyword casing, then we return False, since it is pretty
490 -- clearly intended to be a keyword.
492 if Ident_Casing
= Unknown
493 or else Key_Casing
= Unknown
494 or else Ident_Casing
= Key_Casing
495 or else Determine_Token_Casing
/= Key_Casing
499 -- Here we have a keyword written clearly with keyword casing.
500 -- In default mode, we would not be willing to consider this as
501 -- a reserved identifier, but if C is set, we may still accept it
505 Scan_State
: Saved_Scan_State
;
506 OK_Next_Tok
: Boolean;
509 Save_Scan_State
(Scan_State
);
512 if Token_Is_At_Start_Of_Line
then
520 when C_Comma_Right_Paren
=>
522 Token
= Tok_Comma
or else Token
= Tok_Right_Paren
;
524 when C_Comma_Colon
=>
526 Token
= Tok_Comma
or else Token
= Tok_Colon
;
536 when C_Greater_Greater
=>
538 Token
= Tok_Greater_Greater
;
548 when C_Left_Paren_Semicolon
=>
550 Token
= Tok_Left_Paren
or else Token
= Tok_Semicolon
;
556 when C_Vertical_Bar_Arrow
=>
558 Token
= Tok_Vertical_Bar
or else Token
= Tok_Arrow
;
561 Restore_Scan_State
(Scan_State
);
571 -- If we fall through it is not a reserved identifier
574 end Is_Reserved_Identifier
;
576 ----------------------
577 -- Merge_Identifier --
578 ----------------------
580 procedure Merge_Identifier
(Prev
: Node_Id
; Nxt
: Token_Type
) is
582 if Token
/= Tok_Identifier
then
587 S
: Saved_Scan_State
;
594 Restore_Scan_State
(S
);
601 -- Check exactly one space between identifiers
603 if Source
(Token_Ptr
- 1) /= ' '
604 or else Int
(Token_Ptr
) /=
605 Int
(Prev_Token_Ptr
) + Length_Of_Name
(Chars
(Prev
)) + 1
612 Get_Name_String
(Chars
(Token_Node
));
615 Buf
: constant String (1 .. Name_Len
) :=
616 Name_Buffer
(1 .. Name_Len
);
619 Get_Name_String
(Chars
(Prev
));
620 Add_Char_To_Name_Buffer
('_');
621 Add_Str_To_Name_Buffer
(Buf
);
622 Set_Chars
(Prev
, Name_Find
);
625 Error_Msg_Node_1
:= Prev
;
626 Error_Msg_SC
("unexpected identifier, possibly & was meant here");
628 end Merge_Identifier
;
634 function Next_Token_Is
(Tok
: Token_Type
) return Boolean is
635 Scan_State
: Saved_Scan_State
;
638 Save_Scan_State
(Scan_State
);
640 Result
:= (Token
= Tok
);
641 Restore_Scan_State
(Scan_State
);
649 procedure No_Constraint
is
651 -- If we have a token that could start a constraint on the same line
652 -- then cnsider this an illegal constraint. It seems unlikely it could
653 -- be anything else if it is on the same line.
655 if Token
in Token_Class_Consk
then
656 Error_Msg_SC
("constraint not allowed here");
657 Discard_Junk_Node
(P_Constraint_Opt
);
661 ---------------------
662 -- Pop_Scope_Stack --
663 ---------------------
665 procedure Pop_Scope_Stack
is
667 pragma Assert
(Scope
.Last
> 0);
668 Scope
.Decrement_Last
;
670 if Include_Subprogram_In_Messages
671 and then Scope
.Table
(Scope
.Last
).Labl
/= Error
673 Current_Node
:= Scope
.Table
(Scope
.Last
).Labl
;
677 Error_Msg_Uint_1
:= UI_From_Int
(Scope
.Last
);
678 Error_Msg_SC
("decrement scope stack ptr, new value = ^!");
682 ----------------------
683 -- Push_Scope_Stack --
684 ----------------------
686 procedure Push_Scope_Stack
is
688 Scope
.Increment_Last
;
690 if Style_Check_Max_Nesting_Level
691 and then Scope
.Last
= Style_Max_Nesting_Level
+ 1
694 ("(style) maximum nesting level exceeded",
695 First_Non_Blank_Location
);
698 Scope
.Table
(Scope
.Last
).Junk
:= False;
699 Scope
.Table
(Scope
.Last
).Node
:= Empty
;
702 Error_Msg_Uint_1
:= UI_From_Int
(Scope
.Last
);
703 Error_Msg_SC
("increment scope stack ptr, new value = ^!");
705 end Push_Scope_Stack
;
707 ----------------------
708 -- Separate_Present --
709 ----------------------
711 function Separate_Present
return Boolean is
712 Scan_State
: Saved_Scan_State
;
715 if Token
= Tok_Separate
then
718 elsif Token
/= Tok_Identifier
then
722 Save_Scan_State
(Scan_State
);
723 Scan
; -- past identifier
725 if Token
= Tok_Semicolon
then
726 Restore_Scan_State
(Scan_State
);
727 return Bad_Spelling_Of
(Tok_Separate
);
730 Restore_Scan_State
(Scan_State
);
734 end Separate_Present
;
736 --------------------------
737 -- Signal_Bad_Attribute --
738 --------------------------
740 procedure Signal_Bad_Attribute
is
742 Bad_Attribute
(Token_Node
, Token_Name
, Warn
=> False);
743 end Signal_Bad_Attribute
;
745 -----------------------------
746 -- Token_Is_At_End_Of_Line --
747 -----------------------------
749 function Token_Is_At_End_Of_Line
return Boolean is
753 -- Skip past blanks and horizontal tabs
756 while Source
(S
) = ' ' or else Source
(S
) = ASCII
.HT
loop
760 -- We are at end of line if at a control character (CR/LF/VT/FF/EOF)
761 -- or if we are at the start of an end of line comment sequence.
763 return Source
(S
) < ' '
764 or else (Source
(S
) = '-' and then Source
(S
+ 1) = '-');
765 end Token_Is_At_End_Of_Line
;
767 -------------------------------
768 -- Token_Is_At_Start_Of_Line --
769 -------------------------------
771 function Token_Is_At_Start_Of_Line
return Boolean is
773 return (Token_Ptr
= First_Non_Blank_Location
or else Token
= Tok_EOF
);
774 end Token_Is_At_Start_Of_Line
;
776 -----------------------------------
777 -- Warn_If_Standard_Redefinition --
778 -----------------------------------
780 procedure Warn_If_Standard_Redefinition
(N
: Node_Id
) is
782 if Warn_On_Standard_Redefinition
then
784 C
: constant Entity_Id
:= Current_Entity
(N
);
786 if Present
(C
) and then Sloc
(C
) = Standard_Location
then
787 Error_Msg_N
("redefinition of entity& in Standard?K?", N
);
791 end Warn_If_Standard_Redefinition
;