1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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 Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Casing
; use Casing
;
29 with Debug
; use Debug
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Fname
; use Fname
;
34 with Namet
; use Namet
;
35 with Namet
.Sp
; use Namet
.Sp
;
36 with Nlists
; use Nlists
;
37 with Nmake
; use Nmake
;
39 with Output
; use Output
;
40 with Par_SCO
; use Par_SCO
;
41 with Restrict
; use Restrict
;
42 with Scans
; use Scans
;
44 with Sem_Util
; use Sem_Util
;
45 with Sinput
; use Sinput
;
46 with Sinput
.L
; use Sinput
.L
;
47 with Sinfo
; use Sinfo
;
48 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
49 with Sinfo
.Utils
; use Sinfo
.Utils
;
50 with Snames
; use Snames
;
51 with Stringt
; use Stringt
;
53 with Stylesw
; use Stylesw
;
55 with Tbuild
; use Tbuild
;
61 function Par
(Configuration_Pragmas
: Boolean) return List_Id
is
63 Inside_Record_Definition
: Boolean := False;
64 -- True within a record definition. Used to control warning for
65 -- redefinition of standard entities (not issued for field names).
67 Loop_Block_Count
: Nat
:= 0;
68 -- Counter used for constructing loop/block names (see the routine
69 -- Par.Ch5.Get_Loop_Block_Name).
71 Num_Library_Units
: Natural := 0;
72 -- Count number of units parsed (relevant only in syntax check only mode,
73 -- since in semantics check mode only a single unit is permitted anyway).
75 Save_Config_Attrs
: Config_Switches_Type
;
76 -- Variable used to save values of config switches while we parse the
77 -- new unit, to be restored on exit for proper recursive behavior.
83 -- When an error is encountered, a call is made to one of the Error_Msg
84 -- routines to record the error. If the syntax scan is not derailed by the
85 -- error (e.g. a complaint that logical operators are inconsistent in an
86 -- EXPRESSION), then control returns from the Error_Msg call, and the
87 -- parse continues unimpeded.
89 -- If on the other hand, the Error_Msg represents a situation from which
90 -- the parser cannot recover locally, the exception Error_Resync is raised
91 -- immediately after the call to Error_Msg. Handlers for Error_Resync
92 -- are located at strategic points to resynchronize the parse. For example,
93 -- when an error occurs in a statement, the handler skips to the next
94 -- semicolon and continues the scan from there.
96 -- Each parsing procedure contains a note with the heading "Error recovery"
97 -- which shows if it can propagate the Error_Resync exception. In order
98 -- not to propagate the exception, a procedure must either contain its own
99 -- handler for this exception, or it must not call any other routines which
100 -- propagate the exception.
102 -- Note: the arrangement of Error_Resync handlers is such that it should
103 -- never be possible to transfer control through a procedure which made
104 -- an entry in the scope stack, invalidating the contents of the stack.
106 Error_Resync
: exception;
107 -- Exception raised on error that is not handled locally, see above
109 Last_Resync_Point
: Source_Ptr
;
110 -- The resynchronization routines in Par.Sync run a risk of getting
111 -- stuck in an infinite loop if they do not skip a token, and the caller
112 -- keeps repeating the same resync call. On the other hand, if they skip
113 -- a token unconditionally, some recovery opportunities are missed. The
114 -- variable Last_Resync_Point records the token location previously set
115 -- by a Resync call, and if a subsequent Resync call occurs at the same
116 -- location, then the Resync routine does guarantee to skip a token.
118 --------------------------------------------
119 -- Handling Semicolon Used in Place of IS --
120 --------------------------------------------
122 -- The following global variables are used in handling the error situation
123 -- of using a semicolon in place of IS in a subprogram declaration as in:
125 -- procedure X (Y : Integer);
131 -- The two contexts in which this can appear are at the outer level, and
132 -- within a declarative region. At the outer level, we know something is
133 -- wrong as soon as we see the Q (or begin, if there are no declarations),
134 -- and we can immediately decide that the semicolon should have been IS.
136 -- The situation in a declarative region is more complex. The declaration
137 -- of Q could belong to the outer region, and we do not know that we have
138 -- an error until we hit the begin. It is still not clear at this point
139 -- from a syntactic point of view that something is wrong, because the
140 -- begin could belong to the enclosing subprogram or package. However, we
141 -- can incorporate a bit of semantic knowledge and note that the body of
142 -- X is missing, so we definitely DO have an error. We diagnose this error
143 -- as semicolon in place of IS on the subprogram line.
145 -- There are two styles for this diagnostic. If the begin immediately
146 -- follows the semicolon, then we can place a flag (IS expected) right
147 -- on the semicolon. Otherwise we do not detect the error until we hit
148 -- the begin which refers back to the line with the semicolon.
150 -- To control the process in the second case, the following global
151 -- variables are set to indicate that we have a subprogram declaration
152 -- whose body is required and has not yet been found. The prefix SIS
153 -- stands for "Subprogram IS" handling.
155 SIS_Entry_Active
: Boolean := False;
156 -- Set True to indicate that an entry is active (i.e. that a subprogram
157 -- declaration has been encountered, and no body for this subprogram
158 -- has been encountered). The remaining variables other than
159 -- SIS_Aspect_Import_Seen are valid only if this is True.
161 SIS_Aspect_Import_Seen
: Boolean := False;
162 -- If this is True when a subprogram declaration has been encountered, we
163 -- do not set SIS_Entry_Active, because the Import means there is no body.
164 -- Set False at the start of P_Subprogram, set True when an Import aspect
165 -- specification is seen, and used when P_Subprogram finds a subprogram
166 -- declaration. This is necessary because the aspects are parsed before
167 -- we know we have a subprogram declaration.
170 -- Subprogram designator
172 SIS_Sloc
: Source_Ptr
;
173 -- Source location of FUNCTION/PROCEDURE keyword
175 SIS_Ecol
: Column_Number
;
176 -- Column number of FUNCTION/PROCEDURE keyword
178 SIS_Semicolon_Sloc
: Source_Ptr
;
179 -- Source location of semicolon at end of subprogram declaration
181 SIS_Declaration_Node
: Node_Id
;
182 -- Pointer to tree node for subprogram declaration
184 SIS_Missing_Semicolon_Message
: Error_Msg_Id
;
185 -- Used to save message ID of missing semicolon message (which will be
186 -- modified to missing IS if necessary). Set to No_Error_Msg in the
187 -- normal (non-error) case.
189 -- Five things can happen to an active SIS entry
191 -- 1. If a BEGIN is encountered with an SIS entry active, then we have
192 -- exactly the situation in which we know the body of the subprogram is
193 -- missing. After posting an error message, we change the spec to a body,
194 -- rechaining the declarations that intervened between the spec and BEGIN.
196 -- 2. Another subprogram declaration or body is encountered. In this
197 -- case the entry gets overwritten with the information for the new
198 -- subprogram declaration. We don't catch some nested cases this way,
199 -- but it doesn't seem worth the effort.
201 -- 3. A nested declarative region (e.g. package declaration or package
202 -- body) is encountered. The SIS active indication is reset at the start
203 -- of such a nested region. Again, like case 2, this causes us to miss
204 -- some nested cases, but it doesn't seen worth the effort to stack and
205 -- unstack the SIS information. Maybe we will reconsider this if we ever
206 -- get a complaint about a missed case.
208 -- 4. We encounter a valid pragma INTERFACE or IMPORT that effectively
209 -- supplies the missing body. In this case we reset the entry.
211 -- 5. We encounter the end of the declarative region without encountering
212 -- a BEGIN first. In this situation we simply reset the entry. We know
213 -- that there is a missing body, but it seems more reasonable to let the
214 -- later semantic checking discover this.
216 ----------------------------------------------------
217 -- Handling of Reserved Words Used as Identifiers --
218 ----------------------------------------------------
220 -- Note: throughout the parser, the terms reserved word and keyword are
221 -- used interchangeably to refer to the same set of reserved keywords
222 -- (including until, protected, etc).
224 -- If a reserved word is used in place of an identifier, the parser where
225 -- possible tries to recover gracefully. In particular, if the keyword is
226 -- clearly spelled using identifier casing, e.g. Until in a source program
227 -- using mixed case identifiers and lower case keywords, then the keyword
228 -- is treated as an identifier if it appears in a place where an identifier
231 -- The situation is more complex if the keyword is spelled with normal
232 -- keyword casing. In this case, the parser is more reluctant to consider
233 -- it to be intended as an identifier, unless it has some further
236 -- In the case of an identifier appearing in the identifier list of a
237 -- declaration, the appearance of a comma or colon right after the keyword
238 -- on the same line is taken as confirmation. For an enumeration literal,
239 -- a comma or right paren right after the identifier is also treated as
240 -- adequate confirmation.
242 -- The following type is used in calls to Is_Reserved_Identifier and
243 -- also to P_Defining_Identifier and P_Identifier. The default for all
244 -- these functions is that reserved words in reserved word case are not
245 -- considered to be reserved identifiers. The Id_Check value indicates
246 -- tokens, which if they appear immediately after the identifier, are
247 -- taken as confirming that the use of an identifier was expected
251 -- Default, no special token test
254 -- Consider as identifier if followed by comma or right paren
257 -- Consider as identifier if followed by comma or colon
260 -- Consider as identifier if followed by DO
263 -- Consider as identifier if followed by period
266 -- Consider as identifier if followed by >>
269 -- Consider as identifier if followed by IN
272 -- Consider as identifier if followed by IS
274 C_Left_Paren_Semicolon
,
275 -- Consider as identifier if followed by left paren or semicolon
278 -- Consider as identifier if followed by USE
280 C_Vertical_Bar_Arrow
);
281 -- Consider as identifier if followed by | or =>
283 --------------------------------------------
284 -- Handling IS Used in Place of Semicolon --
285 --------------------------------------------
287 -- This is a somewhat trickier situation, and we can't catch it in all
288 -- cases, but we do our best to detect common situations resulting from
289 -- a "cut and paste" operation which forgets to change the IS to semicolon.
290 -- Consider the following example:
305 -- The trouble is that the section of text from PROCEDURE B through END;
306 -- constitutes a valid procedure body, and the danger is that we find out
307 -- far too late that something is wrong (indeed most compilers will behave
308 -- uncomfortably on the above example).
310 -- We have two approaches to helping to control this situation. First we
311 -- make every attempt to avoid swallowing the last END; if we can be sure
312 -- that some error will result from doing so. In particular, we won't
313 -- accept the END; unless it is exactly correct (in particular it must not
314 -- have incorrect name tokens), and we won't accept it if it is immediately
315 -- followed by end of file, WITH or SEPARATE (all tokens that unmistakeably
316 -- signal the start of a compilation unit, and which therefore allow us to
317 -- reserve the END; for the outer level.) For more details on this aspect
318 -- of the handling, see package Par.Endh.
320 -- If we can avoid eating up the END; then the result in the absence of
321 -- any additional steps would be to post a missing END referring back to
322 -- the subprogram with the bogus IS. Similarly, if the enclosing package
323 -- has no BEGIN, then the result is a missing BEGIN message, which again
324 -- refers back to the subprogram header.
326 -- Such an error message is not too bad (it's already a big improvement
327 -- over what many parsers do), but it's not ideal, because the declarations
328 -- following the IS have been absorbed into the wrong scope. In the above
329 -- case, this could result for example in a bogus complaint that the body
330 -- of D was missing from the package.
332 -- To catch at least some of these cases, we take the following additional
333 -- steps. First, a subprogram body is marked as having a suspicious IS if
334 -- the declaration line is followed by a line which starts with a symbol
335 -- that can start a declaration in the same column, or to the left of the
336 -- column in which the FUNCTION or PROCEDURE starts (normal style is to
337 -- indent any declarations which really belong a subprogram). If such a
338 -- subprogram encounters a missing BEGIN or missing END, then we decide
339 -- that the IS should have been a semicolon, and the subprogram body node
340 -- is marked (by setting the Bad_Is_Detected flag true. Note that we do
341 -- not do this for library level procedures, only for nested procedures,
342 -- since for library level procedures, we must have a body.
344 -- The processing for a declarative part checks to see if the last
345 -- declaration scanned is marked in this way, and if it is, the tree
346 -- is modified to reflect the IS being interpreted as a semicolon.
348 ---------------------------------------------------
349 -- Parser Type Definitions and Control Variables --
350 ---------------------------------------------------
352 -- The following variable and associated type declaration are used by the
353 -- expression parsing routines to return more detailed information about
354 -- the categorization of a parsed expression.
356 type Expr_Form_Type
is (
357 EF_Simple_Name
, -- Simple name, i.e. possibly qualified identifier
358 EF_Name
, -- Simple expression which could also be a name
359 EF_Simple
, -- Simple expression which is not call or name
360 EF_Range_Attr
, -- Range attribute reference
361 EF_Non_Simple
); -- Expression that is not a simple expression
363 Expr_Form
: Expr_Form_Type
;
365 -- The following type is used by P_Subprogram, P_Package, to indicate which
366 -- of several possibilities is acceptable.
368 type Pf_Rec
is record
369 Spcn
: Boolean; -- True if specification OK
370 Decl
: Boolean; -- True if declaration OK
371 Gins
: Boolean; -- True if generic instantiation OK
372 Pbod
: Boolean; -- True if proper body OK
373 Rnam
: Boolean; -- True if renaming declaration OK
374 Stub
: Boolean; -- True if body stub OK
375 Pexp
: Boolean; -- True if parameterized expression OK
377 pragma Pack
(Pf_Rec
);
379 function T
return Boolean renames True;
380 function F
return Boolean renames False;
382 Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
: constant Pf_Rec
:= (F
, T
, T
, T
, T
, T
, T
);
383 Pf_Decl_Pexp
: constant Pf_Rec
:= (F
, T
, F
, F
, F
, F
, T
);
384 Pf_Decl_Gins_Pbod_Rnam_Pexp
: constant Pf_Rec
:= (F
, T
, T
, T
, T
, F
, T
);
385 Pf_Decl_Pbod_Pexp
: constant Pf_Rec
:= (F
, T
, F
, T
, F
, F
, T
);
386 Pf_Pbod_Pexp
: constant Pf_Rec
:= (F
, F
, F
, T
, F
, F
, T
);
387 Pf_Spcn
: constant Pf_Rec
:= (T
, F
, F
, F
, F
, F
, F
);
388 -- The above are the only allowed values of Pf_Rec arguments
390 type SS_Rec
is record
391 Eftm
: Boolean; -- ELSIF can terminate sequence
392 Eltm
: Boolean; -- ELSE can terminate sequence
393 Extm
: Boolean; -- EXCEPTION can terminate sequence
394 Ortm
: Boolean; -- OR can terminate sequence
395 Sreq
: Boolean; -- at least one statement required
396 Tatm
: Boolean; -- THEN ABORT can terminate sequence
397 Whtm
: Boolean; -- WHEN can terminate sequence
398 Unco
: Boolean; -- Unconditional terminate after one statement
400 pragma Pack
(SS_Rec
);
402 SS_Eftm_Eltm_Sreq
: constant SS_Rec
:= (T
, T
, F
, F
, T
, F
, F
, F
);
403 SS_Eltm_Ortm_Tatm
: constant SS_Rec
:= (F
, T
, F
, T
, F
, T
, F
, F
);
404 SS_Extm_Sreq
: constant SS_Rec
:= (F
, F
, T
, F
, T
, F
, F
, F
);
405 SS_None
: constant SS_Rec
:= (F
, F
, F
, F
, F
, F
, F
, F
);
406 SS_Ortm_Sreq
: constant SS_Rec
:= (F
, F
, F
, T
, T
, F
, F
, F
);
407 SS_Sreq
: constant SS_Rec
:= (F
, F
, F
, F
, T
, F
, F
, F
);
408 SS_Sreq_Whtm
: constant SS_Rec
:= (F
, F
, F
, F
, T
, F
, T
, F
);
409 SS_Whtm
: constant SS_Rec
:= (F
, F
, F
, F
, F
, F
, T
, F
);
410 SS_Unco
: constant SS_Rec
:= (F
, F
, F
, F
, F
, F
, F
, T
);
412 Goto_List
: Elist_Id
;
413 -- List of goto nodes appearing in the current compilation. Used to
414 -- recognize natural loops and convert them into bona fide loops for
415 -- optimization purposes.
417 Label_List
: Elist_Id
;
418 -- List of label nodes for labels appearing in the current compilation.
419 -- Used by Par.Labl to construct the corresponding implicit declarations.
425 -- The scope table, also referred to as the scope stack, is used to record
426 -- the current scope context. It is organized as a stack, with inner nested
427 -- entries corresponding to higher entries on the stack. An entry is made
428 -- when the parser encounters the opening of a nested construct (such as a
429 -- record, task, package etc.), and then package Par.Endh uses this stack
430 -- to deal with END lines (including properly dealing with END nesting
434 -- Type of end entry required for this scope. The last two entries are
435 -- used only in the subprogram body case to mark the case of a suspicious
436 -- IS, or a bad IS (i.e. suspicions confirmed by missing BEGIN or END).
437 -- See separate section on dealing with IS used in place of semicolon.
438 -- Note that for many purposes E_Name, E_Suspicious_Is and E_Bad_Is are
439 -- treated the same (E_Suspicious_Is and E_Bad_Is are simply special cases
440 -- of E_Name). They are placed at the end of the enumeration so that a
441 -- test for >= E_Name catches all three cases efficiently.
443 (E_Dummy
, -- dummy entry at outer level
447 E_Record
, -- END RECORD;
448 E_Return
, -- END RETURN;
449 E_Select
, -- END SELECT;
450 E_Name
, -- END [name];
451 E_Suspicious_Is
, -- END [name]; (case of suspicious IS)
452 E_Bad_Is
); -- END [name]; (case of bad IS)
454 -- The following describes a single entry in the scope table
456 type Scope_Table_Entry
is record
458 -- Type of end entry, as per above description
461 -- A flag indicating whether the label, if present, is required to
462 -- appear on the end line. It is referenced only in the case of Etyp is
463 -- equal to E_Name or E_Suspicious_Is where the name may or may not be
464 -- required (yes for labeled block, no in other cases). Note that for
465 -- all cases except begin, the question of whether a label is required
466 -- can be determined from the other fields (for loop, it is required if
467 -- it is present, and for the other constructs it is never required or
470 Ecol
: Column_Number
;
471 -- Contains the absolute column number (with tabs expanded) of the
472 -- expected column of the end assuming normal Ada indentation usage. If
473 -- the RM_Column_Check mode is set, this value is used for generating
474 -- error messages about indentation. Otherwise it is used only to
475 -- control heuristic error recovery actions. This value is zero origin.
478 -- This field is used to provide the name of the construct being parsed
479 -- and indirectly its kind. For loops and blocks, the field contains the
480 -- source name or the generated one. For package specifications, bodies,
481 -- subprogram specifications and bodies the field holds the
482 -- corresponding program unit name. For task declarations and bodies,
483 -- protected types and bodies, and accept statements the field hold the
484 -- name of the type or operation. For if-statements, case-statements,
485 -- return statements, and selects, the field is initialized to Error.
487 -- Note: this is a bit of an odd (mis)use of Error, since there is no
488 -- Error, but we use this value as a place holder to indicate that it
489 -- is an error to have a label on the end line.
491 -- Whenever the field is a name, it is attached to the parent node of
492 -- the construct being parsed. Thus the parent node indicates the kind
493 -- of construct whose parse tree is being built. This is used in error
497 -- Points to the list of declarations (i.e. the declarative part)
498 -- associated with this construct. It is set only in the END [name]
499 -- cases, and is set to No_List for all other cases which do not have a
500 -- declarative unit associated with them. This is used for determining
501 -- the proper location for implicit label declarations.
504 -- Empty except in the case of entries for IF and CASE statements, in
505 -- which case it contains the N_If_Statement or N_Case_Statement node.
506 -- This is used for setting the End_Span field.
509 -- Source location of the opening token of the construct. This is used
510 -- to refer back to this line in error messages (such as missing or
511 -- incorrect end lines). The Sloc field is not used, and is not set, if
512 -- a label is present (the Labl field provides the text name of the
513 -- label in this case, which is fine for error messages).
516 -- S_Is is relevant only if Etyp is set to E_Suspicious_Is or E_Bad_Is.
517 -- It records the location of the IS that is considered to be
521 -- A boolean flag that is set true if the opening entry is the dubious
522 -- result of some prior error, e.g. a record entry where the record
523 -- keyword was missing. It is used to suppress the issuing of a
524 -- corresponding junk complaint about the end line (we do not want
525 -- to complain about a missing end record when there was no record).
528 -- The following declares the scope table itself. The Last field is the
529 -- stack pointer, so that Scope.Table (Scope.Last) is the top entry. The
530 -- oldest entry, at Scope_Stack (0), is a dummy entry with Etyp set to
531 -- E_Dummy, and the other fields undefined. This dummy entry ensures that
532 -- Scope_Stack (Scope_Stack_Ptr).Etyp can always be tested, and that the
533 -- scope stack pointer is always in range.
535 package Scope
is new Table
.Table
(
536 Table_Component_Type
=> Scope_Table_Entry
,
537 Table_Index_Type
=> Int
,
538 Table_Low_Bound
=> 0,
540 Table_Increment
=> 100,
541 Table_Name
=> "Scope");
543 type Scope_Table_Entry_Ptr
is access all Scope_Table_Entry
;
545 function Scopes
(Index
: Int
) return Scope_Table_Entry_Ptr
;
546 -- Return the indicated Scope_Table_Entry. We use a pointer for
547 -- efficiency. Callers should not save the pointer, but should do things
548 -- like Scopes (Scope.Last).Something. Note that there is one place in
549 -- Par.Ch5 that indexes the stack out of bounds, and can't call this.
551 function Scopes
(Index
: Int
) return Scope_Table_Entry_Ptr
is
553 pragma Assert
(Index
in Scope
.First
.. Scope
.Last
);
554 return Scope
.Table
(Index
)'Unrestricted_Access;
557 ------------------------------------------
558 -- Table for Handling Suspicious Labels --
559 ------------------------------------------
561 -- This is a special data structure which is used to deal very specifically
562 -- with the following error case
569 -- Similar cases apply to FOR, WHILE, DECLARE, or BEGIN
571 -- In each case the opening line looks like a procedure call because of
572 -- the semicolon. And the end line looks illegal because of an unexpected
573 -- label. If we did nothing special, we would just diagnose the label on
574 -- the end as unexpected. But that does not help point to the real error
575 -- which is that the semicolon after label should be a colon.
577 -- To deal with this, we build an entry in the Suspicious_Labels table
578 -- whenever we encounter an identifier followed by a semicolon, followed
579 -- by one of LOOP, FOR, WHILE, DECLARE, BEGIN. Then this entry is used to
580 -- issue the right message when we hit the END that confirms that this was
583 type Suspicious_Label_Entry
is record
585 -- Node for the procedure call statement built for the label; construct
587 Semicolon_Loc
: Source_Ptr
;
588 -- Location of the possibly wrong semicolon
590 Start_Token
: Source_Ptr
;
591 -- Source location of the LOOP, FOR, WHILE, DECLARE, BEGIN token
594 package Suspicious_Labels
is new Table
.Table
(
595 Table_Component_Type
=> Suspicious_Label_Entry
,
596 Table_Index_Type
=> Int
,
597 Table_Low_Bound
=> 1,
599 Table_Increment
=> 100,
600 Table_Name
=> "Suspicious_Labels");
602 -- Now when we are about to issue a message complaining about an END label
603 -- that should not be there because it appears to end a construct that has
604 -- no label, we first search the suspicious labels table entry, using the
605 -- source location stored in the scope table as a key. If we find a match,
606 -- then we check that the label on the end matches the name in the call,
607 -- and if so, we issue a message saying the semicolon should be a colon.
609 -- Quite a bit of work, but really helpful in the case where it helps, and
610 -- the need for this is based on actual experience with tracking down this
611 -- kind of error (the eye often easily mistakes semicolon for colon).
613 -- Note: we actually have enough information to patch up the tree, but
614 -- this may not be worth the effort. Also we could deal with the same
615 -- situation for EXIT with a label, but for now don't bother with that.
617 Current_Assign_Node
: Node_Id
:= Empty
;
618 -- This is the node of the current assignment statement being compiled.
619 -- It is used to record the presence of target_names on its RHS. This
620 -- context-dependent trick simplifies the analysis of such nodes, where
621 -- the RHS must first be analyzed with expansion disabled.
623 ---------------------------------
624 -- Parsing Routines by Chapter --
625 ---------------------------------
627 -- Uncommented declarations in this section simply parse the construct
628 -- corresponding to their name, and return an ID value for the Node or
629 -- List that is created.
636 function P_Pragma
(Skipping
: Boolean := False) return Node_Id
;
637 -- Scan out a pragma. If Skipping is True, then the caller is skipping
638 -- the pragma in the context of illegal placement (this is used to avoid
639 -- some junk cascaded messages). Some pragmas must be dealt with during
640 -- the parsing phase (e.g. pragma Page, since we can generate a listing
641 -- in syntax only mode). It is possible that the parser uses the rescan
642 -- logic (using Save/Restore_Scan_State) with the effect of calling this
643 -- procedure more than once for the same pragma. All parse-time pragma
644 -- handling must be prepared to handle such multiple calls correctly.
646 function P_Identifier
647 (C
: Id_Check
:= None
;
648 Force_Msg
: Boolean := False) return Node_Id
;
649 -- Scans out an identifier. The parameter C determines the treatment
650 -- of reserved identifiers. See declaration of Id_Check for details.
652 -- An appropriate error message, pointing to the token, is also issued
653 -- if either this is the first occurrence of misuse of this identifier,
654 -- or if Force_Msg is True.
656 function P_Interpolated_String_Literal
return Node_Id
;
658 function P_Pragmas_Opt
return List_Id
;
659 -- This function scans for a sequence of pragmas in other than a
660 -- declaration sequence or statement sequence context. All pragmas
661 -- can appear except pragmas Assert and Debug, which are only allowed
662 -- in a declaration or statement sequence context.
664 procedure P_Pragmas_Misplaced
;
665 -- Skips misplaced pragmas with a complaint
667 procedure P_Pragmas_Opt
(List
: List_Id
);
668 -- Parses optional pragmas and appends them to the List
676 Missing_Begin_Msg
: Error_Msg_Id
;
677 -- This variable is set by a call to P_Declarative_Part. Normally it
678 -- is set to No_Error_Msg, indicating that no special processing is
679 -- required by the caller. The special case arises when a statement
680 -- is found in the sequence of declarations. In this case the Id of
681 -- the message issued ("declaration expected") is preserved in this
682 -- variable, then the caller can change it to an appropriate missing
683 -- begin message if indeed the BEGIN is missing.
685 function P_Array_Type_Definition
return Node_Id
;
686 function P_Constraint_Opt
return Node_Id
;
687 function P_Declarative_Part
return List_Id
;
688 function P_Discrete_Choice_List
return List_Id
;
689 function P_Discrete_Range
return Node_Id
;
690 function P_Discrete_Subtype_Definition
return Node_Id
;
691 function P_Known_Discriminant_Part_Opt
return List_Id
;
692 function P_Signed_Integer_Type_Definition
return Node_Id
;
693 function P_Range
return Node_Id
;
694 function P_Range_Constraint
return Node_Id
;
695 function P_Record_Definition
return Node_Id
;
696 function P_Subtype_Mark
return Node_Id
;
697 function P_Subtype_Mark_Resync
return Node_Id
;
698 function P_Unknown_Discriminant_Part_Opt
return Boolean;
700 procedure P_Declarative_Items
702 Declare_Expression
: Boolean;
704 In_Statements
: Boolean);
705 -- Parses a sequence of zero or more declarative items, and appends them
706 -- to Decls. Done indicates whether or not there might be additional
707 -- declarative items to parse. If Done is True, then there are no more
708 -- to parse; otherwise there might be more.
710 -- Declare_Expression is true if we are parsing a declare_expression, in
711 -- which case we want to suppress certain style checking.
713 -- In_Spec is true if we are scanning a package declaration, and is used
714 -- to generate an appropriate message if a statement is encountered in
717 -- In_Statements is true if we are called to parse declarative items in
718 -- a sequence of statements. In this case, we do not give an error upon
719 -- encountering a statement, but return to the caller with Done = True,
720 -- so the caller can resume parsing statements.
722 function P_Basic_Declarative_Items
723 (Declare_Expression
: Boolean) return List_Id
;
724 -- Used to parse the declarative items in a package visible or
725 -- private part (in which case Declare_Expression is False), and
726 -- the declare_items of a declare_expression (in which case
727 -- Declare_Expression is True). Declare_Expression is used to
728 -- affect the wording of error messages, and to control style
731 function P_Access_Definition
732 (Null_Exclusion_Present
: Boolean) return Node_Id
;
733 -- Ada 2005 (AI-231/AI-254): The caller parses the null-exclusion part
734 -- and indicates if it was present
736 function P_Access_Type_Definition
737 (Header_Already_Parsed
: Boolean := False) return Node_Id
;
738 -- Ada 2005 (AI-254): The formal is used to indicate if the caller has
739 -- parsed the null_exclusion part. In this case the caller has also
740 -- removed the ACCESS token
742 procedure P_Component_Items
(Decls
: List_Id
);
743 -- Scan out one or more component items and append them to the given
744 -- list. Only scans out more than one declaration in the case where the
745 -- source has a single declaration with multiple defining identifiers.
747 function P_Defining_Identifier
(C
: Id_Check
:= None
) return Node_Id
;
748 -- Scan out a defining identifier. The parameter C controls the
749 -- treatment of errors in case a reserved word is scanned. See the
750 -- declaration of this type for details.
752 function P_Interface_Type_Definition
753 (Abstract_Present
: Boolean) return Node_Id
;
754 -- Ada 2005 (AI-251): Parse the interface type definition part. Abstract
755 -- Present indicates if the reserved word "abstract" has been previously
756 -- found. It is used to report an error message because interface types
757 -- are by definition abstract tagged. We generate a record_definition
758 -- node if the list of interfaces is empty; otherwise we generate a
759 -- derived_type_definition node (the first interface in this list is the
760 -- ancestor interface).
762 function P_Null_Exclusion
763 (Allow_Anonymous_In_95
: Boolean := False) return Boolean;
764 -- Ada 2005 (AI-231): Parse the null-excluding part. A True result
765 -- indicates that the null-excluding part was present.
767 -- Allow_Anonymous_In_95 is True if we are in a context that allows
768 -- anonymous access types in Ada 95, in which case "not null" is legal
769 -- if it precedes "access".
771 function P_Subtype_Indication
772 (Not_Null_Present
: Boolean := False) return Node_Id
;
773 -- Ada 2005 (AI-231): The flag Not_Null_Present indicates that the
774 -- null-excluding part has been scanned out and it was present.
776 function P_Range_Or_Subtype_Mark
777 (Allow_Simple_Expression
: Boolean := False) return Node_Id
;
778 -- Scans out a range or subtype mark, and also permits a general simple
779 -- expression if Allow_Simple_Expression is set to True.
781 function Init_Expr_Opt
(P
: Boolean := False) return Node_Id
;
782 -- If an initialization expression is present (:= expression), then
783 -- it is scanned out and returned, otherwise Empty is returned if no
784 -- initialization expression is present. This procedure also handles
785 -- certain common error cases cleanly. The parameter P indicates if
786 -- a right paren can follow the expression (default = no right paren
789 procedure Skip_Declaration
(S
: List_Id
);
790 -- Used when scanning statements to skip past a misplaced declaration
791 -- The declaration is scanned out and appended to the given list.
792 -- Token is known to be a declaration token (in Token_Class_Declk)
793 -- on entry, so there definition is a declaration to be scanned.
795 function P_Subtype_Indication
796 (Subtype_Mark
: Node_Id
;
797 Not_Null_Present
: Boolean := False) return Node_Id
;
798 -- This version of P_Subtype_Indication is called when the caller has
799 -- already scanned out the subtype mark which is passed as a parameter.
800 -- Ada 2005 (AI-231): The flag Not_Null_Present indicates that the
801 -- null-excluding part has been scanned out and it was present.
803 function P_Subtype_Mark_Attribute
(Type_Node
: Node_Id
) return Node_Id
;
804 -- Parse a subtype mark attribute. The caller has already parsed the
805 -- subtype mark, which is passed in as the argument, and has checked
806 -- that the current token is apostrophe.
814 function P_Aggregate
return Node_Id
;
815 function P_Expression
return Node_Id
;
816 function P_Expression_Or_Range_Attribute
return Node_Id
;
817 function P_Function_Name
return Node_Id
;
818 function P_Name
return Node_Id
;
819 function P_Qualified_Simple_Name
return Node_Id
;
820 function P_Qualified_Simple_Name_Resync
return Node_Id
;
821 function P_Simple_Expression
return Node_Id
;
822 function P_Simple_Expression_Or_Range_Attribute
return Node_Id
;
824 function P_Expression_If_OK
return Node_Id
;
825 -- Scans out an expression allowing an unparenthesized case expression,
826 -- if expression, or quantified expression to appear without enclosing
827 -- parentheses. However, if such an expression is not preceded by a left
828 -- paren, and followed by a right paren, an error message will be output
829 -- noting that parenthesization is required.
831 function P_Expression_No_Right_Paren
return Node_Id
;
832 -- Scans out an expression in contexts where the expression cannot be
833 -- terminated by a right paren (gives better error recovery if an errant
834 -- right paren is found after the expression).
836 function P_Expression_Or_Range_Attribute_If_OK
return Node_Id
;
837 -- Scans out an expression or range attribute where a conditional
838 -- expression is permitted to appear without surrounding parentheses.
839 -- However, if such an expression is not preceded by a left paren, and
840 -- followed by a right paren, an error message will be output noting
841 -- that parenthesization is required.
843 function P_If_Expression
return Node_Id
;
844 -- Scans out an if expression. Called with Token pointing to the
845 -- IF keyword, and returns pointing to the terminating right paren,
846 -- semicolon or comma, but does not consume this terminating token.
848 function P_Qualified_Expression
(Subtype_Mark
: Node_Id
) return Node_Id
;
849 -- This routine scans out a qualified expression when the caller has
850 -- already scanned out the name and apostrophe of the construct.
852 function P_Quantified_Expression
return Node_Id
;
853 -- This routine scans out a quantified expression when the caller has
854 -- already scanned out the keyword "for" of the construct.
862 function P_Condition
return Node_Id
;
863 -- Scan out and return a condition. Note that an error is given if
864 -- the condition is followed by a right parenthesis.
866 function P_Condition
(Cond
: Node_Id
) return Node_Id
;
867 -- Similar to the above, but the caller has already scanned out the
868 -- conditional expression and passes it as an argument. This form of
869 -- the call does not check for a following right parenthesis.
871 function P_Iterator_Specification
(Def_Id
: Node_Id
) return Node_Id
;
872 -- Parse an iterator specification. The defining identifier has already
873 -- been scanned, as it is the common prefix between loop and iterator
876 function P_Loop_Parameter_Specification
return Node_Id
;
877 -- Used in loop constructs and quantified expressions.
879 function P_Sequence_Of_Statements
880 (SS_Flags
: SS_Rec
; Handled
: Boolean := False) return List_Id
;
881 -- SS_Flags indicates the acceptable termination tokens; see body for
882 -- details. Handled is true if we are parsing a handled sequence of
885 procedure Parse_Decls_Begin_End
(Parent
: Node_Id
);
886 -- Parses declarations and handled statement sequence, setting
887 -- fields of Parent node appropriately.
895 function P_Designator
return Node_Id
;
896 function P_Defining_Program_Unit_Name
return Node_Id
;
897 function P_Formal_Part
return List_Id
;
898 function P_Parameter_Profile
return List_Id
;
899 function P_Return_Statement
return Node_Id
;
900 function P_Subprogram_Specification
return Node_Id
;
902 procedure P_Mode
(Node
: Node_Id
);
903 -- Sets In_Present and/or Out_Present flags in Node scanning past IN,
904 -- OUT or IN OUT tokens in the source.
906 function P_Subprogram
(Pf_Flags
: Pf_Rec
) return Node_Id
;
907 -- Scans out any construct starting with either of the keywords
908 -- PROCEDURE or FUNCTION. The parameter indicates which possible
909 -- possible kinds of construct (body, spec, instantiation etc.)
910 -- are permissible in the current context.
918 function P_Package
(Pf_Flags
: Pf_Rec
) return Node_Id
;
919 -- Scans out any construct starting with the keyword PACKAGE. The
920 -- parameter indicates which possible kinds of construct (body, spec,
921 -- instantiation etc.) are permissible in the current context.
929 procedure P_Use_Clause
(Item_List
: List_Id
);
937 function P_Abort_Statement
return Node_Id
;
938 function P_Abortable_Part
return Node_Id
;
939 function P_Accept_Statement
return Node_Id
;
940 function P_Delay_Statement
return Node_Id
;
941 function P_Entry_Body
return Node_Id
;
942 function P_Protected
return Node_Id
;
943 function P_Requeue_Statement
return Node_Id
;
944 function P_Select_Statement
return Node_Id
;
945 function P_Task
return Node_Id
;
946 function P_Terminate_Alternative
return Node_Id
;
954 function P_Compilation_Unit
return Node_Id
;
955 -- Note: this function scans a single compilation unit, and checks that
956 -- an end of file follows this unit, diagnosing any unexpected input as
957 -- an error, and then skipping it, so that Token is set to Tok_EOF on
958 -- return. An exception is in syntax-only mode, where multiple
959 -- compilation units are permitted. In this case, P_Compilation_Unit
960 -- does not check for end of file and there may be more compilation
961 -- units to scan. The caller can uniquely detect this situation by the
962 -- fact that Token is not set to Tok_EOF on return.
964 -- What about multiple unit/file capability that now exists???
966 -- The Ignore parameter is normally set False. It is set True in the
967 -- multiple unit per file mode if we are skipping past a unit that we
968 -- are not interested in.
976 function P_Handled_Sequence_Of_Statements
return Node_Id
;
977 function P_Raise_Expression
return Node_Id
;
978 function P_Raise_Statement
return Node_Id
;
980 function Parse_Exception_Handlers
return List_Id
;
981 -- Parses the partial construct EXCEPTION followed by a list of
982 -- exception handlers which appears in a number of productions, and
983 -- returns the list of exception handlers.
991 function P_Generic
return Node_Id
;
992 function P_Generic_Actual_Part_Opt
return List_Id
;
1000 function P_Representation_Clause
return Node_Id
;
1002 function Aspect_Specifications_Present
1003 (Strict
: Boolean := Ada_Version
< Ada_2012
) return Boolean;
1004 -- This function tests whether the next keyword is WITH followed by
1005 -- something that looks reasonably like an aspect specification. If so,
1006 -- True is returned. Otherwise False is returned. In either case control
1007 -- returns with the token pointer unchanged (i.e. pointing to the WITH
1008 -- token in the case where True is returned). This function takes care
1009 -- of generating appropriate messages if aspect specifications appear
1010 -- in versions of Ada prior to Ada 2012. The parameter strict can be
1011 -- set to True, to be rather strict about considering something to be
1012 -- an aspect specification. If Strict is False, then the circuitry is
1013 -- rather more generous in considering something ill-formed to be an
1014 -- attempt at an aspect specification. The default is more strict for
1015 -- Ada versions before Ada 2012 (where aspect specifications are not
1016 -- permitted). Note: this routine never checks the terminator token
1017 -- for aspects so it does not matter whether the aspect specifications
1018 -- are terminated by semicolon or some other character.
1020 -- Note: This function also handles the case of WHEN used where WITH
1021 -- was intended, and in that case posts an error and returns True.
1023 procedure P_Aspect_Specifications
1025 Semicolon
: Boolean := True);
1026 -- This procedure scans out a series of aspect specifications. If
1027 -- argument Semicolon is True, a terminating semicolon is also scanned.
1028 -- If this argument is False, the scan pointer is left pointing past the
1029 -- aspects and the caller must check for a proper terminator.
1031 -- P_Aspect_Specifications is called with the current token pointing
1032 -- to either a WITH keyword starting an aspect specification, or an
1033 -- instance of what shpould be a terminator token. In the former case,
1034 -- the aspect specifications are scanned out including the terminator
1035 -- token if it is a semicolon, and the Has_Aspect_Specifications
1036 -- flag is set in the given declaration node. A list of aspects
1037 -- is built and stored for this declaration node using a call to
1038 -- Set_Aspect_Specifications. If no WITH keyword is present, then this
1039 -- call has no effect other than scanning out the terminator if it is a
1040 -- semicolon (with the exception that it detects WHEN used in place of
1043 -- If Decl is Error on entry, any scanned aspect specifications are
1044 -- ignored and a message is output saying aspect specifications not
1045 -- permitted here. If Decl is Empty, then scanned aspect specifications
1046 -- are also ignored, but no error message is given (this is used when
1047 -- the caller has already taken care of the error message).
1049 function Get_Aspect_Specifications
1050 (Semicolon
: Boolean := True) return List_Id
;
1051 -- Parse a list of aspects but do not attach them to a declaration node.
1052 -- Subsidiary to P_Aspect_Specifications procedure. Used when parsing
1053 -- a subprogram specification that may be a declaration or a body.
1054 -- Semicolon has the same meaning as for P_Aspect_Specifications above.
1056 function P_Code_Statement
(Subtype_Mark
: Node_Id
) return Node_Id
;
1057 -- Function to parse a code statement. The caller has scanned out
1058 -- the name to be used as the subtype mark (but has not checked that
1059 -- it is suitable for use as a subtype mark, i.e. is either an
1060 -- identifier or a selected component). The current token is an
1061 -- apostrophe and the following token is either a left paren or
1062 -- RANGE (the latter being an error to be caught by P_Code_Statement.
1065 -- Note: the parsing for annexe J features (i.e. obsolescent features)
1066 -- is found in the logical section where these features would be if
1067 -- they were not obsolescent. In particular:
1069 -- Delta constraint is parsed by P_Delta_Constraint (3.5.9)
1070 -- At clause is parsed by P_At_Clause (13.1)
1071 -- Mod clause is parsed by P_Mod_Clause (13.5.1)
1077 -- Routines for handling end lines, including scope recovery
1081 (Decl
: Node_Id
:= Empty
;
1082 Is_Loc
: Source_Ptr
:= No_Location
) return Boolean;
1083 -- Called when an end sequence is required. In the absence of an error
1084 -- situation, Token contains Tok_End on entry, but in a missing end
1085 -- case, this may not be the case. Pop_End_Context is used to determine
1086 -- the appropriate action to be taken. The returned result is True if
1087 -- an End sequence was encountered and False if no End sequence was
1088 -- present. This occurs if the END keyword encountered was determined
1089 -- to be improper and deleted (i.e. Pop_End_Context set End_Action to
1090 -- Skip_And_Reject). Note that the END sequence includes a semicolon,
1091 -- except in the case of END RECORD, where a semicolon follows the END
1092 -- RECORD, but is not part of the record type definition itself.
1094 -- If Decl is non-empty, then aspect specifications are permitted
1095 -- following the end, and Decl is the declaration node with which
1096 -- these aspect specifications are to be associated. If Decl is empty,
1097 -- then aspect specifications are not permitted and will generate an
1100 -- Is_Loc is set to other than the default only for the case of a
1101 -- package declaration. It points to the IS keyword of the declaration,
1102 -- and is used to specialize the error messages for misplaced aspect
1103 -- specifications in this case. Note that Decl is always Empty if Is_Loc
1107 -- Skip past an end sequence. On entry Token contains Tok_End, and we
1108 -- we know that the end sequence is syntactically incorrect, and that
1109 -- an appropriate error message has already been posted. The mission
1110 -- is simply to position the scan pointer to be the best guess of the
1111 -- position after the end sequence. We do not issue any additional
1112 -- error messages while carrying this out.
1114 procedure End_Statements
1115 (Parent
: Node_Id
:= Empty
;
1116 Decl
: Node_Id
:= Empty
;
1117 Is_Sloc
: Source_Ptr
:= No_Location
);
1118 -- Called when an end is required or expected to terminate a sequence
1119 -- of statements. The caller has already made an appropriate entry in
1120 -- the Scope.Table to describe the expected form of the end. This can
1121 -- only be used in cases where the only appropriate terminator is end.
1122 -- If Parent is non-empty, then if a correct END line is encountered,
1123 -- the End_Label field of Parent is set appropriately.
1125 -- If Decl is non-null, then it is a declaration node, and aspect
1126 -- specifications are permitted after the end statement. These aspect
1127 -- specifications, if present, are stored in this declaration node.
1128 -- If Decl is null, then aspect specifications are not permitted after
1129 -- the end statement.
1131 -- In the case where Decl is null, Is_Sloc determines the handling. If
1132 -- it is set to No_Location, then aspect specifications are ignored and
1133 -- an error message is given. Is_Sloc is used in the package declaration
1134 -- case to point to the IS, and is used to specialize the error emssages
1135 -- issued in this case.
1142 -- These procedures are used to resynchronize after errors. Following an
1143 -- error which is not immediately locally recoverable, the exception
1144 -- Error_Resync is raised. The handler for Error_Resync typically calls
1145 -- one of these recovery procedures to resynchronize the source position
1146 -- to a point from which parsing can be restarted.
1148 -- Note: these procedures output an information message that tokens are
1149 -- being skipped, but this message is output only if the option for
1150 -- Multiple_Errors_Per_Line is set in Options.
1153 procedure Resync_Choice
;
1154 -- Used if an error occurs scanning a choice. The scan pointer is
1155 -- advanced to the next vertical bar, arrow, or semicolon, whichever
1156 -- comes first. We also quit if we encounter an end of file.
1158 procedure Resync_Cunit
;
1159 -- Synchronize to next token which could be the start of a compilation
1160 -- unit, or to the end of file token.
1162 procedure Resync_Expression
;
1163 -- Used if an error is detected during the parsing of an expression.
1164 -- It skips past tokens until either a token which cannot be part of
1165 -- an expression is encountered (an expression terminator), or if a
1166 -- comma or right parenthesis or vertical bar is encountered at the
1167 -- current parenthesis level (a parenthesis level counter is maintained
1168 -- to carry out this test).
1170 procedure Resync_Past_Malformed_Aspect
;
1171 -- Used when parsing aspect specifications to skip a malformed aspect.
1172 -- The scan pointer is positioned next to a comma, a semicolon or "is"
1173 -- when the aspect applies to a body.
1175 procedure Resync_Past_Semicolon
;
1176 -- Used if an error occurs while scanning a sequence of declarations.
1177 -- The scan pointer is positioned past the next semicolon and the scan
1178 -- resumes. The scan is also resumed on encountering a token which
1179 -- starts a declaration (but we make sure to skip at least one token
1180 -- in this case, to avoid getting stuck in a loop).
1182 procedure Resync_Past_Semicolon_Or_To_Loop_Or_Then
;
1183 -- Used if an error occurs while scanning a sequence of statements. The
1184 -- scan pointer is positioned past the next semicolon, or to the next
1185 -- occurrence of either then or loop, and the scan resumes.
1187 procedure Resync_Semicolon_List
;
1188 -- Used if an error occurs while scanning a parenthesized list of items
1189 -- separated by semicolons. The scan pointer is advanced to the next
1190 -- semicolon or right parenthesis at the outer parenthesis level, or
1191 -- to the next is or RETURN keyword occurrence, whichever comes first.
1193 procedure Resync_To_Semicolon
;
1194 -- Similar to Resync_Past_Semicolon, except that the scan pointer is
1195 -- left pointing to the semicolon rather than past it.
1197 procedure Resync_To_When
;
1198 -- Used when an error occurs scanning an entry index specification. The
1199 -- scan pointer is positioned to the next WHEN (or to IS or semicolon if
1200 -- either of these appear before WHEN, indicating another error has
1208 -- Routines to check for expected tokens
1212 -- Procedures with names of the form T_xxx, where Tok_xxx is a token
1213 -- name, check that the current token matches the required token, and
1214 -- if so, scan past it. If not, an error is issued indicating that
1215 -- the required token is not present (xxx expected). In most cases, the
1216 -- scan pointer is not moved in the not-found case, but there are some
1217 -- exceptions to this, see for example T_Id, where the scan pointer is
1218 -- moved across a literal appearing where an identifier is expected.
1226 procedure T_Colon_Equal
;
1228 procedure T_Dot_Dot
;
1230 procedure T_Greater_Greater
;
1231 procedure T_Identifier
;
1234 procedure T_Left_Paren
;
1240 procedure T_Private
;
1243 procedure T_Right_Bracket
;
1244 procedure T_Right_Curly_Bracket
;
1245 procedure T_Right_Paren
;
1246 procedure T_Semicolon
;
1253 -- Procedures having names of the form TF_xxx, where Tok_xxx is a token
1254 -- name check that the current token matches the required token, and
1255 -- if so, scan past it. If not, an error message is issued indicating
1256 -- that the required token is not present (xxx expected).
1258 -- If the missing token is at the end of the line, then control returns
1259 -- immediately after posting the message. If there are remaining tokens
1260 -- on the current line, a search is conducted to see if the token
1261 -- appears later on the current line, as follows:
1263 -- A call to Scan_Save is issued and a forward search for the token
1264 -- is carried out. If the token is found on the current line before a
1265 -- semicolon, then it is scanned out and the scan continues from that
1266 -- point. If not the scan is restored to the point where it was missing.
1271 procedure TF_Return
;
1272 procedure TF_Semicolon
;
1276 -- Procedures with names of the form U_xxx, where Tok_xxx is a token
1277 -- name, are just like the corresponding T_xxx procedures except that
1278 -- an error message, if given, is unconditional.
1280 procedure U_Left_Paren
;
1281 procedure U_Right_Paren
;
1289 function Bad_Spelling_Of
(T
: Token_Type
) return Boolean;
1290 -- This function is called in an error situation. It checks if the
1291 -- current token is an identifier whose name is a plausible bad
1292 -- spelling of the given keyword token, and if so, issues an error
1293 -- message, sets Token from T, and returns True. Otherwise Token is
1294 -- unchanged, and False is returned.
1296 procedure Check_Bad_Layout
;
1297 -- Check for bad indentation in RM checking mode. Used for statements
1298 -- and declarations. Checks if current token is at start of line and
1299 -- is exdented from the current expected end column, and if so an
1300 -- error message is generated.
1302 procedure Check_Misspelling_Of
(T
: Token_Type
);
1303 pragma Inline
(Check_Misspelling_Of
);
1304 -- This is similar to the function above, except that it does not
1305 -- return a result. It is typically used in a situation where any
1306 -- identifier is an error, and it makes sense to simply convert it
1307 -- to the given token if it is a plausible misspelling of it.
1309 procedure Check_95_Keyword
(Token_95
, Next
: Token_Type
);
1310 -- This routine checks if the token after the current one matches the
1311 -- Next argument. If so, the scan is backed up to the current token
1312 -- and Token_Type is changed to Token_95 after issuing an appropriate
1313 -- error message ("(Ada 83) keyword xx cannot be used"). If not,
1314 -- the scan is backed up with Token_Type unchanged. This routine
1315 -- is used to deal with an attempt to use a 95 keyword in Ada 83
1316 -- mode. The caller has typically checked that the current token,
1317 -- an identifier, matches one of the 95 keywords.
1319 procedure Check_Future_Keyword
;
1320 -- Emit a warning if the current token is a valid identifier in the
1321 -- language version in use, but is a reserved word in a later language
1322 -- version (unless the language version in use is Ada 83).
1324 procedure Check_Simple_Expression
(E
: Node_Id
);
1325 -- Given an expression E, that has just been scanned, so that Expr_Form
1326 -- is still set, outputs an error if E is a non-simple expression. E is
1327 -- not modified by this call.
1329 procedure Check_Simple_Expression_In_Ada_83
(E
: Node_Id
);
1330 -- Like Check_Simple_Expression, except that the error message is only
1331 -- given when operating in Ada 83 mode, and includes "in Ada 83".
1333 function Check_Subtype_Mark
(Mark
: Node_Id
) return Node_Id
;
1334 -- Called to check that a node representing a name (or call) is
1335 -- suitable for a subtype mark, i.e, that it is an identifier or
1336 -- a selected component. If so, or if it is already Error, then
1337 -- it is returned unchanged. Otherwise an error message is issued
1338 -- and Error is returned.
1340 function Comma_Present
return Boolean;
1341 -- Used in comma delimited lists to determine if a comma is present, or
1342 -- can reasonably be assumed to have been present (an error message is
1343 -- generated in the latter case). If True is returned, the scan has been
1344 -- positioned past the comma. If False is returned, the scan position
1345 -- is unchanged. Note that all comma-delimited lists are terminated by
1346 -- a right paren, so the only legitimate tokens when Comma_Present is
1347 -- called are right paren and comma. If some other token is found, then
1348 -- Comma_Present has the job of deciding whether it is better to pretend
1349 -- a comma was present, post a message for a missing comma and return
1350 -- True, or return False and let the caller diagnose the missing right
1353 procedure Discard_Junk_Node
(N
: Node_Id
);
1354 procedure Discard_Junk_List
(L
: List_Id
);
1355 pragma Inline
(Discard_Junk_Node
);
1356 pragma Inline
(Discard_Junk_List
);
1357 -- These procedures do nothing at all, their effect is simply to discard
1358 -- the argument. A typical use is to skip by some junk that is not
1359 -- expected in the current context.
1361 procedure Ignore
(T
: Token_Type
);
1362 -- If current token matches T, then give an error message and skip
1363 -- past it, otherwise the call has no effect at all. T may be any
1364 -- reserved word token, or comma, left or right paren, or semicolon.
1366 function Is_Reserved_Identifier
(C
: Id_Check
:= None
) return Boolean;
1367 -- Test if current token is a reserved identifier. This test is based
1368 -- on the token being a keyword and being spelled in typical identifier
1369 -- style (i.e. starting with an upper case letter). The parameter C
1370 -- determines the special treatment if a reserved word is encountered
1371 -- that has the normal casing of a reserved word.
1373 procedure Merge_Identifier
(Prev
: Node_Id
; Nxt
: Token_Type
);
1374 -- Called when the previous token is an identifier (whose Token_Node
1375 -- value is given by Prev) to check if current token is an identifier
1376 -- that can be merged with the previous one adding an underscore. The
1377 -- merge is only attempted if the following token matches Nxt. If all
1378 -- conditions are met, an error message is issued, and the merge is
1379 -- carried out, modifying the Chars field of Prev.
1381 function Missing_Semicolon_On_When
return Boolean;
1382 -- This function deals with the following specialized situations
1385 -- exit/return [identifier]
1388 -- This looks like a messed up EXIT WHEN or RETURN WHEN, when in fact
1389 -- the problem is a missing semicolon. It is called with Token pointing
1390 -- to the WHEN token, and returns True if a semicolon is missing before
1391 -- the WHEN as in the above example.
1393 function Next_Token_Is
(Tok
: Token_Type
) return Boolean;
1394 -- Looks at token after current one and returns True if the token type
1395 -- matches Tok. The scan is unconditionally restored on return.
1397 procedure No_Constraint
;
1398 -- Called in a place where no constraint is allowed, but one might
1399 -- appear due to a common error (e.g. after the type mark in a procedure
1400 -- parameter. If a constraint is present, an error message is posted,
1401 -- and the constraint is scanned and discarded.
1403 procedure Push_Scope_Stack
;
1404 pragma Inline
(Push_Scope_Stack
);
1405 -- Push a new entry onto the scope stack. Scope.Last (the stack pointer)
1406 -- is incremented. The Junk field is preinitialized to False. The caller
1407 -- is expected to fill in all remaining entries of the new top stack
1408 -- entry at Scopes (Scope.Last).
1410 procedure Pop_Scope_Stack
;
1411 -- Pop an entry off the top of the scope stack. Scope_Last (the scope
1412 -- table stack pointer) is decremented by one. It is a fatal error to
1413 -- try to pop off the dummy entry at the bottom of the stack (i.e.
1414 -- Scope.Last must be non-zero at the time of call).
1416 function Separate_Present
return Boolean;
1417 -- Determines if the current token is either Tok_Separate, or an
1418 -- identifier that is a possible misspelling of "separate" followed
1419 -- by a semicolon. True is returned if so, otherwise False.
1421 procedure Signal_Bad_Attribute
;
1422 -- The current token is an identifier that is supposed to be an
1423 -- attribute identifier but is not. This routine posts appropriate
1424 -- error messages, including a check for a near misspelling.
1426 function Token_Is_At_Start_Of_Line
return Boolean;
1427 pragma Inline
(Token_Is_At_Start_Of_Line
);
1428 -- Determines if the current token is the first token on the line
1430 function Token_Is_At_End_Of_Line
return Boolean;
1431 -- Determines if the current token is the last token on the line
1433 procedure Warn_If_Standard_Redefinition
(N
: Node_Id
);
1434 -- Issues a warning if Warn_On_Standard_Redefinition is set True, and
1435 -- the Node N (which is a Defining_Identifier node with the Chars field
1436 -- set) is a renaming of an entity in package Standard.
1444 -- The processing for pragmas is split off from chapter 2
1446 function Prag
(Pragma_Node
: Node_Id
; Semi
: Source_Ptr
) return Node_Id
;
1447 -- This function is passed a tree for a pragma that has been scanned out.
1448 -- The pragma is syntactically well formed according to the general syntax
1449 -- for pragmas and the pragma identifier is for one of the recognized
1450 -- pragmas. It performs specific syntactic checks for specific pragmas.
1451 -- The result is the input node if it is OK, or Error otherwise. The
1452 -- reason that this is separated out is to facilitate the addition
1453 -- of implementation defined pragmas. The second parameter records the
1454 -- location of the semicolon following the pragma (this is needed for
1455 -- correct processing of the List and Page pragmas). The returned value
1456 -- is a copy of Pragma_Node, or Error if an error is found. Note that
1457 -- at the point where Prag is called, the right paren ending the pragma
1458 -- has been scanned out, and except in the case of pragma Style_Checks,
1459 -- so has the following semicolon. For Style_Checks, the caller delays
1460 -- the scanning of the semicolon so that it will be scanned using the
1461 -- settings from the Style_Checks pragma preceding it.
1468 -- This procedure creates implicit label declarations for all labels that
1469 -- are declared in the current unit. Note that this could conceptually be
1470 -- done at the point where the labels are declared, but it is tricky to do
1471 -- it then, since the tree is not hooked up at the point where the label is
1472 -- declared (e.g. a sequence of statements is not yet attached to its
1473 -- containing scope at the point a label in the sequence is found).
1480 -- This procedure loads all subsidiary units that are required by this
1481 -- unit, including with'ed units, specs for bodies, and parents for child
1482 -- units. It does not load bodies for inlined procedures and generics,
1483 -- since we don't know till semantic analysis is complete what is needed.
1489 -- The package bodies can see all routines defined in all other subpackages
1509 package body Ch2
is separate;
1510 package body Ch3
is separate;
1511 package body Ch4
is separate;
1512 package body Ch5
is separate;
1513 package body Ch6
is separate;
1514 package body Ch7
is separate;
1515 package body Ch8
is separate;
1516 package body Ch9
is separate;
1517 package body Ch10
is separate;
1518 package body Ch11
is separate;
1519 package body Ch12
is separate;
1520 package body Ch13
is separate;
1522 package body Endh
is separate;
1523 package body Tchk
is separate;
1524 package body Sync
is separate;
1525 package body Util
is separate;
1527 function Prag
(Pragma_Node
: Node_Id
; Semi
: Source_Ptr
) return Node_Id
1530 procedure Labl
is separate;
1531 procedure Load
is separate;
1533 Result
: List_Id
:= Empty_List
;
1535 -- Start of processing for Par
1538 Compiler_State
:= Parsing
;
1540 -- Deal with configuration pragmas case first
1542 if Configuration_Pragmas
then
1544 Pragmas
: constant List_Id
:= Empty_List
;
1549 if Token
= Tok_EOF
then
1553 elsif Token
/= Tok_Pragma
then
1554 Error_Msg_SC
("only pragmas allowed in configuration file");
1555 Result
:= Error_List
;
1561 if Nkind
(P_Node
) = N_Pragma
then
1563 -- Give error if bad pragma
1565 if not Is_Configuration_Pragma_Name
1566 (Pragma_Name_Unmapped
(P_Node
))
1568 Pragma_Name_Unmapped
(P_Node
) /= Name_Source_Reference
1570 if Is_Pragma_Name
(Pragma_Name_Unmapped
(P_Node
)) then
1572 ("only configuration pragmas allowed " &
1573 "in configuration file", P_Node
);
1576 ("unrecognized pragma in configuration file",
1580 -- Pragma is OK config pragma, so collect it
1583 Append
(P_Node
, Pragmas
);
1590 if Config_Files_Store_Basename
then
1591 Complete_Source_File_Entry
;
1594 -- Normal case of compilation unit
1597 Save_Config_Attrs
:= Save_Config_Switches
;
1599 -- The following loop runs more than once in syntax check mode
1600 -- where we allow multiple compilation units in the same file
1601 -- and in Multiple_Unit_Per_file mode where we skip units till
1602 -- we get to the unit we want.
1604 for Ucount
in Pos
loop
1606 (Is_Internal_Unit
(Current_Source_Unit
),
1607 Main_Unit
=> Current_Source_Unit
= Main_Unit
);
1609 -- Initialize scope table and other parser control variables
1611 Compiler_State
:= Parsing
;
1613 Scope
.Increment_Last
;
1614 Scopes
(0).Etyp
:= E_Dummy
;
1615 SIS_Entry_Active
:= False;
1616 Last_Resync_Point
:= No_Location
;
1618 Goto_List
:= New_Elmt_List
;
1619 Label_List
:= New_Elmt_List
;
1621 -- If in multiple unit per file mode, skip past ignored unit
1623 if Ucount
< Multiple_Unit_Index
then
1625 -- We skip in syntax check only mode, since we don't want to do
1626 -- anything more than skip past the unit and ignore it. This means
1627 -- we skip processing like setting up a unit table entry.
1630 Save_Operating_Mode
: constant Operating_Mode_Type
:=
1633 Save_Style_Check
: constant Boolean := Style_Check
;
1636 Operating_Mode
:= Check_Syntax
;
1637 Style_Check
:= False;
1638 Discard_Node
(P_Compilation_Unit
);
1639 Operating_Mode
:= Save_Operating_Mode
;
1640 Style_Check
:= Save_Style_Check
;
1642 -- If we are at an end of file, and not yet at the right unit,
1643 -- then we have a fatal error. The unit is missing.
1645 if Token
= Tok_EOF
then
1646 Error_Msg_SC
("file has too few compilation units");
1647 raise Unrecoverable_Error
;
1651 -- Here if we are not skipping a file in multiple unit per file mode.
1652 -- Parse the unit that we are interested in. Note that in check
1653 -- syntax mode we are interested in all units in the file.
1657 Comp_Unit_Node
: constant Node_Id
:= P_Compilation_Unit
;
1660 -- If parsing was successful and we are not in check syntax
1661 -- mode, check that language-defined units are compiled in GNAT
1662 -- mode. For this purpose we do NOT consider renamings in annex
1663 -- J as predefined. That allows users to compile their own
1664 -- versions of these files. Another exception is System.RPC
1665 -- and its children. This allows a user to supply their own
1666 -- communication layer.
1667 -- Similarly, we do not generate an error in CodePeer mode,
1668 -- to allow users to analyze third-party compiler packages.
1670 if Comp_Unit_Node
/= Error
1671 and then Operating_Mode
= Generate_Code
1672 and then Current_Source_Unit
= Main_Unit
1673 and then not GNAT_Mode
1674 and then not CodePeer_Mode
1677 Uname
: constant String :=
1679 (Unit_Name
(Current_Source_Unit
));
1680 Name
: String renames
1681 Uname
(Uname
'First .. Uname
'Last - 2);
1682 -- Because Unit_Name includes "%s"/"%b", we need to strip
1683 -- the last two characters to get the real unit name.
1686 if Name
= "ada" or else
1687 Name
= "interfaces" or else
1691 ("language-defined units cannot be recompiled",
1692 Sloc
(Unit
(Comp_Unit_Node
)));
1694 elsif Name
'Length > 4
1696 Name
(Name
'First .. Name
'First + 3) = "ada."
1699 ("user-defined descendants of package Ada " &
1701 Sloc
(Unit
(Comp_Unit_Node
)));
1703 elsif Name
'Length > 11
1705 Name
(Name
'First .. Name
'First + 10) = "interfaces."
1708 ("user-defined descendants of package Interfaces " &
1710 Sloc
(Unit
(Comp_Unit_Node
)));
1712 elsif Name
'Length > 7
1713 and then Name
(Name
'First .. Name
'First + 6) = "system."
1714 and then Name
/= "system.rpc"
1717 or else Name
(Name
'First .. Name
'First + 10) /=
1721 ("user-defined descendants of package System " &
1723 Sloc
(Unit
(Comp_Unit_Node
)));
1729 -- All done if at end of file
1731 exit when Token
= Tok_EOF
;
1733 -- If we are not at an end of file, it means we are in syntax
1734 -- check only mode, and we keep the loop going to parse all
1735 -- remaining units in the file.
1739 Restore_Config_Switches
(Save_Config_Attrs
);
1742 -- Now that we have completely parsed the source file, we can complete
1743 -- the source file table entry.
1745 Complete_Source_File_Entry
;
1747 -- An internal error check, the scope stack should now be empty
1749 pragma Assert
(Scope
.Last
= 0);
1751 -- Here we make the SCO table entries for the main unit
1753 if Generate_SCO
then
1754 SCO_Record_Raw
(Main_Unit
);
1757 -- Remaining steps are to create implicit label declarations and to load
1758 -- required subsidiary sources. These steps are required only if we are
1759 -- doing semantic checking.
1761 if Operating_Mode
/= Check_Syntax
or else Debug_Flag_F
then
1766 -- Restore settings of switches saved on entry
1768 Restore_Config_Switches
(Save_Config_Attrs
);
1769 Set_Comes_From_Source_Default
(False);
1772 Compiler_State
:= Analyzing
;
1773 Current_Source_File
:= No_Source_File
;