Debian Codegraph @

gcc/ada/scans.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S C A N S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2025, 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 Namet; use Namet;
27 with Types; use Types;
28 with Uintp; use Uintp;
29 with Urealp; use Urealp;
30
31 package Scans is
32
33 -- The scanner maintains a current state in the global variables defined
34 -- in this package. The call to the Scan routine advances this state to
35 -- the next token. The state is initialized by the call to one of the
36 -- initialization routines in Sinput.
37
38 -- The following type is used to identify token types returned by Scan.
39 -- The class column in this table indicates the token classes which
40 -- apply to the token, as defined by subsequent subtype declarations.
41
42 type Token_Type is (
43
44 -- Token name Token type Class(es)
45
46 Tok_Integer_Literal, -- numeric lit Literal, Lit_Or_Name
47
48 Tok_Real_Literal, -- numeric lit Literal, Lit_Or_Name
49
50 Tok_String_Literal, -- string lit Literal. Lit_Or_Name
51
52 Tok_Char_Literal, -- char lit Name, Literal. Lit_Or_Name
53
54 Tok_Operator_Symbol, -- op symbol Name, Literal, Lit_Or_Name, Desig
55
56 Tok_Identifier, -- identifier Name, Lit_Or_Name, Desig
57
58 Tok_At_Sign, -- @ AI12-0125-3 : target name
59
60 Tok_Double_Asterisk, -- **
61
62 Tok_Ampersand, -- & Binary_Addop
63 Tok_Minus, -- - Binary_Addop, Unary_Addop
64 Tok_Plus, -- + Binary_Addop, Unary_Addop
65
66 Tok_Asterisk, -- * Mulop
67 Tok_Mod, -- MOD Mulop
68 Tok_Rem, -- REM Mulop
69 Tok_Slash, -- / Mulop
70
71 Tok_New, -- NEW
72
73 Tok_Abs, -- ABS
74 Tok_Others, -- OTHERS
75 Tok_Null, -- NULL
76
77 -- Note: Tok_Raise is in no categories now, it used to be Cterm, Eterm,
78 -- After_SM, but now that Ada 2012 has added raise expressions, the
79 -- raise token can appear anywhere. Note in particular that Tok_Raise
80 -- being in Eterm stopped the parser from recognizing "return raise
81 -- exception-name". This degrades error recovery slightly, and perhaps
82 -- we could do better, but not worth the effort.
83
84 -- Ada 2022 introduces square brackets as delimiters for array and
85 -- container aggregates.
86
87 -- The left delimiter token of interpolated strings, and tokens { and }
88 -- of interpolated expressions are currently placed in no category since
89 -- they don't fit well in the existing categories.
90
91 Tok_Left_Interpolated_String, -- f"
92 Tok_Left_Curly_Bracket, -- {
93 Tok_Raise, -- RAISE
94 Tok_Right_Curly_Bracket, -- }
95
96 Tok_Left_Bracket, -- [
97
98 Tok_Dot, -- . Namext
99 Tok_Apostrophe, -- ' Namext
100
101 Tok_Left_Paren, -- ( Namext, Consk
102
103 Tok_Delta, -- DELTA Atkwd, Sterm, Consk
104 Tok_Digits, -- DIGITS Atkwd, Sterm, Consk
105 Tok_Range, -- RANGE Atkwd, Sterm, Consk
106
107 Tok_Right_Paren, -- ) Sterm
108 Tok_Right_Bracket, -- ] Sterm
109 Tok_Comma, -- , Sterm
110
111 Tok_And, -- AND Logop, Sterm
112 Tok_Or, -- OR Logop, Sterm
113 Tok_Xor, -- XOR Logop, Sterm
114
115 Tok_Less, -- < Relop, Sterm
116 Tok_Equal, -- = Relop, Sterm
117 Tok_Greater, -- > Relop, Sterm
118 Tok_Not_Equal, -- /= Relop, Sterm
119 Tok_Greater_Equal, -- >= Relop, Sterm
120 Tok_Less_Equal, -- <= Relop, Sterm
121
122 Tok_In, -- IN Relop, Sterm
123 Tok_Not, -- NOT Relop, Sterm
124
125 Tok_Box, -- <> Relop, Eterm, Sterm
126 Tok_Colon_Equal, -- := Eterm, Sterm
127 Tok_Colon, -- : Eterm, Sterm
128 Tok_Greater_Greater, -- >> Eterm, Sterm
129
130 Tok_Abstract, -- ABSTRACT Eterm, Sterm
131 Tok_Access, -- ACCESS Eterm, Sterm
132 Tok_Aliased, -- ALIASED Eterm, Sterm
133 Tok_All, -- ALL Eterm, Sterm
134 Tok_Array, -- ARRAY Eterm, Sterm
135 Tok_At, -- AT Eterm, Sterm
136 Tok_Body, -- BODY Eterm, Sterm
137 Tok_Constant, -- CONSTANT Eterm, Sterm
138 Tok_Do, -- DO Eterm, Sterm
139 Tok_Is, -- IS Eterm, Sterm
140 Tok_Interface, -- INTERFACE Eterm, Sterm
141 Tok_Limited, -- LIMITED Eterm, Sterm
142 Tok_Of, -- OF Eterm, Sterm
143 Tok_Out, -- OUT Eterm, Sterm
144 Tok_Record, -- RECORD Eterm, Sterm
145 Tok_Renames, -- RENAMES Eterm, Sterm
146 Tok_Reverse, -- REVERSE Eterm, Sterm
147 Tok_Some, -- SOME Eterm, Sterm
148 Tok_Tagged, -- TAGGED Eterm, Sterm
149 Tok_Then, -- THEN Eterm, Sterm
150
151 Tok_Less_Less, -- << Eterm, Sterm, After_SM
152
153 Tok_Abort, -- ABORT Eterm, Sterm, After_SM
154 Tok_Accept, -- ACCEPT Eterm, Sterm, After_SM
155 Tok_Case, -- CASE Eterm, Sterm, After_SM
156 Tok_Delay, -- DELAY Eterm, Sterm, After_SM
157 Tok_Else, -- ELSE Eterm, Sterm, After_SM
158 Tok_Elsif, -- ELSIF Eterm, Sterm, After_SM
159 Tok_End, -- END Eterm, Sterm, After_SM
160 Tok_Exception, -- EXCEPTION Eterm, Sterm, After_SM
161 Tok_Exit, -- EXIT Eterm, Sterm, After_SM
162 Tok_Finally, -- FINALLY Eterm, Sterm, After_SM
163 Tok_Goto, -- GOTO Eterm, Sterm, After_SM
164 Tok_If, -- IF Eterm, Sterm, After_SM
165 Tok_Pragma, -- PRAGMA Eterm, Sterm, After_SM
166 Tok_Requeue, -- REQUEUE Eterm, Sterm, After_SM
167 Tok_Return, -- RETURN Eterm, Sterm, After_SM
168 Tok_Select, -- SELECT Eterm, Sterm, After_SM
169 Tok_Terminate, -- TERMINATE Eterm, Sterm, After_SM
170 Tok_Until, -- UNTIL Eterm, Sterm, After_SM
171 Tok_When, -- WHEN Eterm, Sterm, After_SM
172
173 Tok_Begin, -- BEGIN Eterm, Sterm, After_SM, Labeled_Stmt
174 Tok_Declare, -- DECLARE Eterm, Sterm, After_SM, Labeled_Stmt
175 Tok_For, -- FOR Eterm, Sterm, After_SM, Labeled_Stmt
176 Tok_Loop, -- LOOP Eterm, Sterm, After_SM, Labeled_Stmt
177 Tok_While, -- WHILE Eterm, Sterm, After_SM, Labeled_Stmt
178
179 Tok_Entry, -- ENTRY Eterm, Sterm, Declk, Deckn, After_SM
180 Tok_Protected, -- PROTECTED Eterm, Sterm, Declk, Deckn, After_SM
181 Tok_Task, -- TASK Eterm, Sterm, Declk, Deckn, After_SM
182 Tok_Type, -- TYPE Eterm, Sterm, Declk, Deckn, After_SM
183 Tok_Subtype, -- SUBTYPE Eterm, Sterm, Declk, Deckn, After_SM
184 Tok_Overriding, -- OVERRIDING Eterm, Sterm, Declk, Declk, After_SM
185 Tok_Synchronized, -- SYNCHRONIZED Eterm, Sterm, Declk, Deckn, After_SM
186 Tok_Use, -- USE Eterm, Sterm, Declk, Deckn, After_SM
187
188 Tok_Function, -- FUNCTION Eterm, Sterm, Cunit, Declk, After_SM
189 Tok_Generic, -- GENERIC Eterm, Sterm, Cunit, Declk, After_SM
190 Tok_Package, -- PACKAGE Eterm, Sterm, Cunit, Declk, After_SM
191 Tok_Procedure, -- PROCEDURE Eterm, Sterm, Cunit, Declk, After_SM
192
193 Tok_Private, -- PRIVATE Eterm, Sterm, Cunit, After_SM
194 Tok_With, -- WITH Eterm, Sterm, Cunit, After_SM
195 Tok_Separate, -- SEPARATE Eterm, Sterm, Cunit, After_SM
196
197 Tok_EOF, -- End of file Eterm, Sterm, Cterm, After_SM
198
199 Tok_Semicolon, -- ; Eterm, Sterm, Cterm
200
201 Tok_Arrow, -- => Sterm, Cterm, Chtok
202
203 Tok_Vertical_Bar, -- | Cterm, Sterm, Chtok
204
205 Tok_Dot_Dot, -- .. Sterm, Chtok
206
207 Tok_Project,
208 Tok_Extends,
209 Tok_External,
210 Tok_External_As_List,
211 -- These four entries represent keywords for the project file language
212 -- and can be returned only in the case of scanning project files.
213
214 Tok_Comment,
215 -- This entry is used when scanning project files (where it represents
216 -- an entire comment), and in preprocessing with the -C switch set
217 -- (where it represents just the "--" of a comment). For the project
218 -- file case, the text of the comment is stored in Comment_Id.
219
220 Tok_End_Of_Line,
221 -- Represents an end of line. Not used during normal compilation scans
222 -- where end of line is ignored. Active for preprocessor scanning.
223
224 Tok_Special,
225 -- Special character used by the preprocessor. The character itself is
226 -- stored in Special_Character below.
227
228 No_Token);
229 -- No_Token is used for initializing Token values to indicate that
230 -- no value has been set yet.
231
232 function Keyword_Name (Token : Token_Type) return Name_Id;
233 -- Given a token that is a reserved word, return the corresponding Name_Id
234 -- in lower case. E.g. Keyword_Name (Tok_Begin) = Name_Find ("begin").
235 -- It is an error to pass any other kind of token.
236
237 -- Note: in the RM, operator symbol is a special case of string literal.
238 -- We distinguish at the lexical level in this compiler, since there are
239 -- many syntactic situations in which only an operator symbol is allowed.
240
241 -- The following subtype declarations group the token types into classes.
242 -- These are used for class tests in the parser.
243
244 subtype Token_Class_Numeric_Literal is
245 Token_Type range Tok_Integer_Literal .. Tok_Real_Literal;
246 -- Numeric literal
247
248 subtype Token_Class_Literal is
249 Token_Type range Tok_Integer_Literal .. Tok_Operator_Symbol;
250 -- Literal
251
252 subtype Token_Class_Lit_Or_Name is
253 Token_Type range Tok_Integer_Literal .. Tok_Identifier;
254
255 subtype Token_Class_Binary_Addop is
256 Token_Type range Tok_Ampersand .. Tok_Plus;
257 -- Binary adding operator (& + -)
258
259 subtype Token_Class_Unary_Addop is
260 Token_Type range Tok_Minus .. Tok_Plus;
261 -- Unary adding operator (+ -)
262
263 subtype Token_Class_Mulop is
264 Token_Type range Tok_Asterisk .. Tok_Slash;
265 -- Multiplying operator
266
267 subtype Token_Class_Logop is
268 Token_Type range Tok_And .. Tok_Xor;
269 -- Logical operator (and, or, xor)
270
271 subtype Token_Class_Relop is
272 Token_Type range Tok_Less .. Tok_Box;
273 -- Relational operator (= /= < <= > >= not, in plus <> to catch misuse
274 -- of Pascal style not equal operator).
275
276 subtype Token_Class_Name is
277 Token_Type range Tok_Char_Literal .. Tok_At_Sign;
278 -- First token of name (4.1),
279 -- (identifier, char literal, operator symbol)
280 -- Includes '@' after Ada2012 corrigendum.
281
282 subtype Token_Class_Desig is
283 Token_Type range Tok_Operator_Symbol .. Tok_At_Sign;
284 -- Token which can be a Designator (identifier, operator symbol)
285
286 subtype Token_Class_Namext is
287 Token_Type range Tok_Dot .. Tok_Left_Paren;
288 -- Name extension tokens. These are tokens which can appear immediately
289 -- after a name to extend it recursively (period, quote, left paren)
290
291 subtype Token_Class_Consk is
292 Token_Type range Tok_Left_Paren .. Tok_Range;
293 -- Keywords which can start constraint
294 -- (left paren, delta, digits, range)
295
296 subtype Token_Class_Eterm is
297 Token_Type range Tok_Colon_Equal .. Tok_Semicolon;
298 -- Expression terminators. These tokens can never appear within a simple
299 -- expression. This is used for error recovery purposes (if we encounter
300 -- an error in an expression, we simply scan to the next Eterm token).
301
302 subtype Token_Class_Sterm is
303 Token_Type range Tok_Delta .. Tok_Dot_Dot;
304 -- Simple_Expression terminators. A Simple_Expression must be followed
305 -- by a token in this class, or an error message is issued complaining
306 -- about a missing binary operator.
307
308 subtype Token_Class_Atkwd is
309 Token_Type range Tok_Delta .. Tok_Range;
310 -- Attribute keywords. This class includes keywords which can be used
311 -- as an Attribute_Designator, namely DELTA, DIGITS and RANGE
312
313 subtype Token_Class_Cterm is
314 Token_Type range Tok_EOF .. Tok_Vertical_Bar;
315 -- Choice terminators. These tokens terminate a choice. This is used for
316 -- error recovery purposes (if we encounter an error in a Choice, we
317 -- simply scan to the next Cterm token).
318
319 subtype Token_Class_Chtok is
320 Token_Type range Tok_Arrow .. Tok_Dot_Dot;
321 -- Choice tokens. These tokens signal a choice when used in an Aggregate
322
323 subtype Token_Class_Cunit is
324 Token_Type range Tok_Function .. Tok_Separate;
325 -- Tokens which can begin a compilation unit
326
327 subtype Token_Class_Declk is
328 Token_Type range Tok_Entry .. Tok_Procedure;
329 -- Keywords which start a declaration
330
331 subtype Token_Class_Deckn is
332 Token_Type range Tok_Entry .. Tok_Use;
333 -- Keywords which start a declaration but can't start a compilation unit
334
335 subtype Token_Class_After_SM is
336 Token_Type range Tok_Less_Less .. Tok_EOF;
337 -- Tokens which always, or almost always, appear after a semicolon. Used
338 -- in the Resync_Past_Semicolon routine to avoid gobbling up stuff when
339 -- a semicolon is missing. Of significance only for error recovery.
340
341 subtype Token_Class_Labeled_Stmt is
342 Token_Type range Tok_Begin .. Tok_While;
343 -- Tokens which start labeled statements
344
345 type Token_Flag_Array is array (Token_Type) of Boolean;
346 Is_Reserved_Keyword : constant Token_Flag_Array :=
347 Token_Flag_Array'
348 (Tok_Mod .. Tok_Rem => True,
349 Tok_New .. Tok_Null => True,
350 Tok_Delta .. Tok_Range => True,
351 Tok_And .. Tok_Xor => True,
352 Tok_In .. Tok_Not => True,
353 Tok_Abstract .. Tok_Then => True,
354 Tok_Abort .. Tok_Separate => True,
355 others => False);
356 -- Flag array used to test for reserved word
357
358 procedure Initialize_Ada_Keywords;
359 -- Set up Token_Type values in Names table entries for Ada reserved
360 -- words. This ignores Ada_Version; Ada_Version is taken into account in
361 -- Snames.Is_Keyword_Name.
362
363 --------------------------
364 -- Scan State Variables --
365 --------------------------
366
367 -- Note: these variables can only be referenced during the parsing of a
368 -- file. Reference to any of them from Sem or the expander is wrong.
369
370 -- These variables are initialized by Scn.Initialize_Scanner, and should
371 -- not be referenced before such a call, except for saving and restoring
372 -- them.
373
374 Scan_Ptr : Source_Ptr := No_Location;
375 -- Current scan pointer location. After a call to Scan, this points
376 -- just past the end of the token just scanned.
377
378 Token : Token_Type := No_Token;
379 -- Type of current token
380
381 Token_Ptr : Source_Ptr := No_Location;
382 -- Pointer to first character of current token
383
384 Current_Line_Start : Source_Ptr := No_Location;
385 -- Pointer to first character of line containing current token
386
387 Start_Column : Column_Number := No_Column_Number;
388 -- Starting column number (zero origin) of the first non-blank character
389 -- on the line containing the current token. This is used for error
390 -- recovery circuits which depend on looking at the column line up.
391
392 Type_Token_Location : Source_Ptr := No_Location;
393 -- Within a type declaration, gives the location of the TYPE keyword that
394 -- opened the type declaration. Used in checking the end column of a record
395 -- declaration, which can line up either with the TYPE keyword, or with the
396 -- start of the line containing the RECORD keyword.
397
398 Checksum : Word := 0;
399 -- Used to accumulate a CRC representing the tokens in the source
400 -- file being compiled. This CRC includes only program tokens, and
401 -- excludes comments.
402
403 Limited_Checksum : Word := 0;
404 -- Used to accumulate a CRC representing significant tokens in the
405 -- limited view of a package, i.e. visible type names and related
406 -- tagged indicators.
407
408 First_Non_Blank_Location : Source_Ptr := No_Location;
409 -- Location of first non-blank character on the line containing the
410 -- current token (i.e. the location of the character whose column number
411 -- is stored in Start_Column).
412
413 Token_Node : Node_Id := Empty;
414 -- Node table Id for the current token. This is set only if the current
415 -- token is one for which the scanner constructs a node (i.e. it is an
416 -- identifier, operator symbol, or literal). For other token types,
417 -- Token_Node is undefined.
418
419 Token_Name : Name_Id := No_Name;
420 -- For identifiers, this is set to the Name_Id of the identifier scanned.
421 -- For all other tokens, Token_Name is set to Error_Name. Note that it
422 -- would be possible for the caller to extract this information from
423 -- Token_Node. We set Token_Name separately for two reasons. First it
424 -- allows a quicker test for a specific identifier. Second, it allows
425 -- a version of the parser to be built that does not build tree nodes,
426 -- usable as a syntax checker.
427
428 Prev_Token : Token_Type := No_Token;
429 -- Type of previous token
430
431 Prev_Token_Ptr : Source_Ptr;
432 -- Pointer to first character of previous token
433
434 Version_To_Be_Found : Boolean;
435 -- This flag is True if the scanner is still looking for an RCS version
436 -- number in a comment. Normally it is initialized to False so that this
437 -- circuit is not activated. If the -dv switch is set, then this flag is
438 -- initialized to True, and then reset when the version number is found.
439 -- We do things this way to minimize the impact on comment scanning.
440
441 Character_Code : Char_Code;
442 -- Valid only when Token is Tok_Char_Literal. Contains the value of the
443 -- scanned literal.
444
445 Real_Literal_Value : Ureal;
446 -- Valid only when Token is Tok_Real_Literal. Contains the value of the
447 -- scanned literal.
448
449 Int_Literal_Value : Uint;
450 -- Valid only when Token = Tok_Integer_Literal, and we are not in
451 -- syntax-only mode. Contains the value of the scanned literal.
452
453 Based_Literal_Uses_Colon : Boolean;
454 -- Valid only when Token = Tok_Integer_Literal or Tok_Real_Literal. Set
455 -- True only for the case of a based literal using ':' instead of '#'.
456
457 String_Literal_Id : String_Id;
458 -- Valid only when Token = Tok_String_Literal or Tok_Operator_Symbol.
459 -- Contains the Id for currently scanned string value.
460
461 Wide_Character_Found : Boolean := False;
462 -- Valid only when Token = Tok_String_Literal. Set True if wide character
463 -- found (i.e. a character that does not fit in Character, but fits in
464 -- Wide_Wide_Character).
465
466 Wide_Wide_Character_Found : Boolean := False;
467 -- Valid only when Token = Tok_String_Literal. Set True if wide wide
468 -- character found (i.e. a character that does not fit in Character or
469 -- Wide_Character).
470
471 subtype Special_Preprocessor_Character is Character with
472 Predicate => Special_Preprocessor_Character in '#' | '$';
473 Special_Character : Special_Preprocessor_Character;
474
475 Comment_Id : Name_Id := No_Name;
476 -- Valid only when Token = Tok_Comment. Store the string that follows
477 -- the "--" of a comment when scanning project files.
478 --
479 -- Is it really right for this to be a Name rather than a String, what
480 -- about the case of Wide_Wide_Characters???
481
482 Inside_Depends : Boolean := False;
483 -- True while parsing the argument of a Depends or Refined_Depends pragma
484 -- or aspect. Used to allow/require nonstandard style rules for =>+ with
485 -- -gnatyt.
486
487 Inside_Interpolated_String_Expression : Boolean := False;
488 -- True while parsing an interpolated string expression
489
490 Inside_Interpolated_String_Literal : Boolean := False;
491 -- True while parsing an interpolated string literal
492
493 Inside_If_Expression : Nat := 0;
494 -- This is a counter that is set non-zero while scanning out an if
495 -- expression (incremented on entry, decremented on exit). It is used to
496 -- disconnect format checks that normally apply to keywords THEN, ELSE etc.
497
498 Inside_Pragma : Boolean := False;
499 -- True within a pragma. Used to avoid complaining about reserved words
500 -- within pragmas (see Scan_Reserved_Identifier).
501
502 --------------------------------------------------------
503 -- Procedures for Saving and Restoring the Scan State --
504 --------------------------------------------------------
505
506 -- The following procedures can be used to save and restore the entire
507 -- scan state. They are used in cases where it is necessary to backup
508 -- the scan during the parse.
509
510 type Saved_Scan_State is private;
511 -- Used for saving and restoring the scan state
512
513 procedure Save_Scan_State (Saved_State : out Saved_Scan_State);
514 pragma Inline (Save_Scan_State);
515 -- Saves the current scan state for possible later restoration. Note that
516 -- there is no harm in saving the state and then never restoring it.
517
518 procedure Restore_Scan_State (Saved_State : Saved_Scan_State);
519 pragma Inline (Restore_Scan_State);
520 -- Restores a scan state saved by a call to Save_Scan_State.
521 -- The saved scan state must refer to the current source file.
522
523 private
524 type Saved_Scan_State is record
525 Save_Scan_Ptr : Source_Ptr;
526 Save_Token : Token_Type;
527 Save_Token_Ptr : Source_Ptr;
528 Save_Current_Line_Start : Source_Ptr;
529 Save_Start_Column : Column_Number;
530 Save_Checksum : Word;
531 Save_First_Non_Blank_Location : Source_Ptr;
532 Save_Token_Node : Node_Id;
533 Save_Token_Name : Name_Id;
534 Save_Prev_Token : Token_Type;
535 Save_Prev_Token_Ptr : Source_Ptr;
536 end record;
537
538 end Scans;