sem_util.ads, [...] (Last_Source_Statement): Replaces Last_Source_Node_In_Sequence.
[gcc.git] / gcc / ada / par.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, 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 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;
33 with Lib; use Lib;
34 with Namet; use Namet;
35 with Namet.Sp; use Namet.Sp;
36 with Nlists; use Nlists;
37 with Nmake; use Nmake;
38 with Opt; use Opt;
39 with Output; use Output;
40 with Par_SCO; use Par_SCO;
41 with Scans; use Scans;
42 with Scn; use Scn;
43 with Sinput; use Sinput;
44 with Sinput.L; use Sinput.L;
45 with Sinfo; use Sinfo;
46 with Snames; use Snames;
47 with Style;
48 with Stylesw; use Stylesw;
49 with Table;
50 with Tbuild; use Tbuild;
51
52 ---------
53 -- Par --
54 ---------
55
56 function Par (Configuration_Pragmas : Boolean) return List_Id is
57
58 Num_Library_Units : Natural := 0;
59 -- Count number of units parsed (relevant only in syntax check only mode,
60 -- since in semantics check mode only a single unit is permitted anyway)
61
62 Save_Config_Switches : Config_Switches_Type;
63 -- Variable used to save values of config switches while we parse the
64 -- new unit, to be restored on exit for proper recursive behavior.
65
66 Loop_Block_Count : Nat := 0;
67 -- Counter used for constructing loop/block names (see the routine
68 -- Par.Ch5.Get_Loop_Block_Name)
69
70 --------------------
71 -- Error Recovery --
72 --------------------
73
74 -- When an error is encountered, a call is made to one of the Error_Msg
75 -- routines to record the error. If the syntax scan is not derailed by the
76 -- error (e.g. a complaint that logical operators are inconsistent in an
77 -- EXPRESSION), then control returns from the Error_Msg call, and the
78 -- parse continues unimpeded.
79
80 -- If on the other hand, the Error_Msg represents a situation from which
81 -- the parser cannot recover locally, the exception Error_Resync is raised
82 -- immediately after the call to Error_Msg. Handlers for Error_Resync
83 -- are located at strategic points to resynchronize the parse. For example,
84 -- when an error occurs in a statement, the handler skips to the next
85 -- semicolon and continues the scan from there.
86
87 -- Each parsing procedure contains a note with the heading "Error recovery"
88 -- which shows if it can propagate the Error_Resync exception. In order
89 -- not to propagate the exception, a procedure must either contain its own
90 -- handler for this exception, or it must not call any other routines which
91 -- propagate the exception.
92
93 -- Note: the arrangement of Error_Resync handlers is such that it should
94 -- never be possible to transfer control through a procedure which made
95 -- an entry in the scope stack, invalidating the contents of the stack.
96
97 Error_Resync : exception;
98 -- Exception raised on error that is not handled locally, see above
99
100 Last_Resync_Point : Source_Ptr;
101 -- The resynchronization routines in Par.Sync run a risk of getting
102 -- stuck in an infinite loop if they do not skip a token, and the caller
103 -- keeps repeating the same resync call. On the other hand, if they skip
104 -- a token unconditionally, some recovery opportunities are missed. The
105 -- variable Last_Resync_Point records the token location previously set
106 -- by a Resync call, and if a subsequent Resync call occurs at the same
107 -- location, then the Resync routine does guarantee to skip a token.
108
109 --------------------------------------------
110 -- Handling Semicolon Used in Place of IS --
111 --------------------------------------------
112
113 -- The following global variables are used in handling the error situation
114 -- of using a semicolon in place of IS in a subprogram declaration as in:
115
116 -- procedure X (Y : Integer);
117 -- Q : Integer;
118 -- begin
119 -- ...
120 -- end;
121
122 -- The two contexts in which this can appear are at the outer level, and
123 -- within a declarative region. At the outer level, we know something is
124 -- wrong as soon as we see the Q (or begin, if there are no declarations),
125 -- and we can immediately decide that the semicolon should have been IS.
126
127 -- The situation in a declarative region is more complex. The declaration
128 -- of Q could belong to the outer region, and we do not know that we have
129 -- an error until we hit the begin. It is still not clear at this point
130 -- from a syntactic point of view that something is wrong, because the
131 -- begin could belong to the enclosing subprogram or package. However, we
132 -- can incorporate a bit of semantic knowledge and note that the body of
133 -- X is missing, so we definitely DO have an error. We diagnose this error
134 -- as semicolon in place of IS on the subprogram line.
135
136 -- There are two styles for this diagnostic. If the begin immediately
137 -- follows the semicolon, then we can place a flag (IS expected) right
138 -- on the semicolon. Otherwise we do not detect the error until we hit
139 -- the begin which refers back to the line with the semicolon.
140
141 -- To control the process in the second case, the following global
142 -- variables are set to indicate that we have a subprogram declaration
143 -- whose body is required and has not yet been found. The prefix SIS
144 -- stands for "Subprogram IS" handling.
145
146 SIS_Entry_Active : Boolean := False;
147 -- Set True to indicate that an entry is active (i.e. that a subprogram
148 -- declaration has been encountered, and no body for this subprogram has
149 -- been encountered). The remaining fields are valid only if this is True.
150
151 SIS_Labl : Node_Id;
152 -- Subprogram designator
153
154 SIS_Sloc : Source_Ptr;
155 -- Source location of FUNCTION/PROCEDURE keyword
156
157 SIS_Ecol : Column_Number;
158 -- Column number of FUNCTION/PROCEDURE keyword
159
160 SIS_Semicolon_Sloc : Source_Ptr;
161 -- Source location of semicolon at end of subprogram declaration
162
163 SIS_Declaration_Node : Node_Id;
164 -- Pointer to tree node for subprogram declaration
165
166 SIS_Missing_Semicolon_Message : Error_Msg_Id;
167 -- Used to save message ID of missing semicolon message (which will be
168 -- modified to missing IS if necessary). Set to No_Error_Msg in the
169 -- normal (non-error) case.
170
171 -- Five things can happen to an active SIS entry
172
173 -- 1. If a BEGIN is encountered with an SIS entry active, then we have
174 -- exactly the situation in which we know the body of the subprogram is
175 -- missing. After posting an error message, we change the spec to a body,
176 -- rechaining the declarations that intervened between the spec and BEGIN.
177
178 -- 2. Another subprogram declaration or body is encountered. In this
179 -- case the entry gets overwritten with the information for the new
180 -- subprogram declaration. We don't catch some nested cases this way,
181 -- but it doesn't seem worth the effort.
182
183 -- 3. A nested declarative region (e.g. package declaration or package
184 -- body) is encountered. The SIS active indication is reset at the start
185 -- of such a nested region. Again, like case 2, this causes us to miss
186 -- some nested cases, but it doesn't seen worth the effort to stack and
187 -- unstack the SIS information. Maybe we will reconsider this if we ever
188 -- get a complaint about a missed case.
189
190 -- 4. We encounter a valid pragma INTERFACE or IMPORT that effectively
191 -- supplies the missing body. In this case we reset the entry.
192
193 -- 5. We encounter the end of the declarative region without encountering
194 -- a BEGIN first. In this situation we simply reset the entry. We know
195 -- that there is a missing body, but it seems more reasonable to let the
196 -- later semantic checking discover this.
197
198 ----------------------------------------------------
199 -- Handling of Reserved Words Used as Identifiers --
200 ----------------------------------------------------
201
202 -- Note: throughout the parser, the terms reserved word and keyword are
203 -- used interchangeably to refer to the same set of reserved keywords
204 -- (including until, protected, etc).
205
206 -- If a reserved word is used in place of an identifier, the parser where
207 -- possible tries to recover gracefully. In particular, if the keyword is
208 -- clearly spelled using identifier casing, e.g. Until in a source program
209 -- using mixed case identifiers and lower case keywords, then the keyword
210 -- is treated as an identifier if it appears in a place where an identifier
211 -- is required.
212
213 -- The situation is more complex if the keyword is spelled with normal
214 -- keyword casing. In this case, the parser is more reluctant to consider
215 -- it to be intended as an identifier, unless it has some further
216 -- confirmation.
217
218 -- In the case of an identifier appearing in the identifier list of a
219 -- declaration, the appearance of a comma or colon right after the keyword
220 -- on the same line is taken as confirmation. For an enumeration literal,
221 -- a comma or right paren right after the identifier is also treated as
222 -- adequate confirmation.
223
224 -- The following type is used in calls to Is_Reserved_Identifier and
225 -- also to P_Defining_Identifier and P_Identifier. The default for all
226 -- these functions is that reserved words in reserved word case are not
227 -- considered to be reserved identifiers. The Id_Check value indicates
228 -- tokens, which if they appear immediately after the identifier, are
229 -- taken as confirming that the use of an identifier was expected
230
231 type Id_Check is
232 (None,
233 -- Default, no special token test
234
235 C_Comma_Right_Paren,
236 -- Consider as identifier if followed by comma or right paren
237
238 C_Comma_Colon,
239 -- Consider as identifier if followed by comma or colon
240
241 C_Do,
242 -- Consider as identifier if followed by DO
243
244 C_Dot,
245 -- Consider as identifier if followed by period
246
247 C_Greater_Greater,
248 -- Consider as identifier if followed by >>
249
250 C_In,
251 -- Consider as identifier if followed by IN
252
253 C_Is,
254 -- Consider as identifier if followed by IS
255
256 C_Left_Paren_Semicolon,
257 -- Consider as identifier if followed by left paren or semicolon
258
259 C_Use,
260 -- Consider as identifier if followed by USE
261
262 C_Vertical_Bar_Arrow);
263 -- Consider as identifier if followed by | or =>
264
265 --------------------------------------------
266 -- Handling IS Used in Place of Semicolon --
267 --------------------------------------------
268
269 -- This is a somewhat trickier situation, and we can't catch it in all
270 -- cases, but we do our best to detect common situations resulting from
271 -- a "cut and paste" operation which forgets to change the IS to semicolon.
272 -- Consider the following example:
273
274 -- package body X is
275 -- procedure A;
276 -- procedure B is
277 -- procedure C;
278 -- ...
279 -- procedure D is
280 -- begin
281 -- ...
282 -- end;
283 -- begin
284 -- ...
285 -- end;
286
287 -- The trouble is that the section of text from PROCEDURE B through END;
288 -- constitutes a valid procedure body, and the danger is that we find out
289 -- far too late that something is wrong (indeed most compilers will behave
290 -- uncomfortably on the above example).
291
292 -- We have two approaches to helping to control this situation. First we
293 -- make every attempt to avoid swallowing the last END; if we can be sure
294 -- that some error will result from doing so. In particular, we won't
295 -- accept the END; unless it is exactly correct (in particular it must not
296 -- have incorrect name tokens), and we won't accept it if it is immediately
297 -- followed by end of file, WITH or SEPARATE (all tokens that unmistakeably
298 -- signal the start of a compilation unit, and which therefore allow us to
299 -- reserve the END; for the outer level.) For more details on this aspect
300 -- of the handling, see package Par.Endh.
301
302 -- If we can avoid eating up the END; then the result in the absence of
303 -- any additional steps would be to post a missing END referring back to
304 -- the subprogram with the bogus IS. Similarly, if the enclosing package
305 -- has no BEGIN, then the result is a missing BEGIN message, which again
306 -- refers back to the subprogram header.
307
308 -- Such an error message is not too bad (it's already a big improvement
309 -- over what many parsers do), but it's not ideal, because the declarations
310 -- following the IS have been absorbed into the wrong scope. In the above
311 -- case, this could result for example in a bogus complaint that the body
312 -- of D was missing from the package.
313
314 -- To catch at least some of these cases, we take the following additional
315 -- steps. First, a subprogram body is marked as having a suspicious IS if
316 -- the declaration line is followed by a line which starts with a symbol
317 -- that can start a declaration in the same column, or to the left of the
318 -- column in which the FUNCTION or PROCEDURE starts (normal style is to
319 -- indent any declarations which really belong a subprogram). If such a
320 -- subprogram encounters a missing BEGIN or missing END, then we decide
321 -- that the IS should have been a semicolon, and the subprogram body node
322 -- is marked (by setting the Bad_Is_Detected flag true. Note that we do
323 -- not do this for library level procedures, only for nested procedures,
324 -- since for library level procedures, we must have a body.
325
326 -- The processing for a declarative part checks to see if the last
327 -- declaration scanned is marked in this way, and if it is, the tree
328 -- is modified to reflect the IS being interpreted as a semicolon.
329
330 ---------------------------------------------------
331 -- Parser Type Definitions and Control Variables --
332 ---------------------------------------------------
333
334 -- The following variable and associated type declaration are used by the
335 -- expression parsing routines to return more detailed information about
336 -- the categorization of a parsed expression.
337
338 type Expr_Form_Type is (
339 EF_Simple_Name, -- Simple name, i.e. possibly qualified identifier
340 EF_Name, -- Simple expression which could also be a name
341 EF_Simple, -- Simple expression which is not call or name
342 EF_Range_Attr, -- Range attribute reference
343 EF_Non_Simple); -- Expression that is not a simple expression
344
345 Expr_Form : Expr_Form_Type;
346
347 -- The following type is used for calls to P_Subprogram, P_Package, P_Task,
348 -- P_Protected to indicate which of several possibilities is acceptable.
349
350 type Pf_Rec is record
351 Spcn : Boolean; -- True if specification OK
352 Decl : Boolean; -- True if declaration OK
353 Gins : Boolean; -- True if generic instantiation OK
354 Pbod : Boolean; -- True if proper body OK
355 Rnam : Boolean; -- True if renaming declaration OK
356 Stub : Boolean; -- True if body stub OK
357 Pexp : Boolean; -- True if parametrized expression OK
358 Fil2 : Boolean; -- Filler to fill to 8 bits
359 end record;
360 pragma Pack (Pf_Rec);
361
362 function T return Boolean renames True;
363 function F return Boolean renames False;
364
365 Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp : constant Pf_Rec :=
366 Pf_Rec'(F, T, T, T, T, T, T, F);
367 Pf_Decl_Pexp : constant Pf_Rec :=
368 Pf_Rec'(F, T, F, F, F, F, T, F);
369 Pf_Decl_Gins_Pbod_Rnam_Pexp : constant Pf_Rec :=
370 Pf_Rec'(F, T, T, T, T, F, T, F);
371 Pf_Decl_Pbod_Pexp : constant Pf_Rec :=
372 Pf_Rec'(F, T, F, T, F, F, T, F);
373 Pf_Pbod_Pexp : constant Pf_Rec :=
374 Pf_Rec'(F, F, F, T, F, F, T, F);
375 Pf_Spcn : constant Pf_Rec :=
376 Pf_Rec'(T, F, F, F, F, F, F, F);
377 -- The above are the only allowed values of Pf_Rec arguments
378
379 type SS_Rec is record
380 Eftm : Boolean; -- ELSIF can terminate sequence
381 Eltm : Boolean; -- ELSE can terminate sequence
382 Extm : Boolean; -- EXCEPTION can terminate sequence
383 Ortm : Boolean; -- OR can terminate sequence
384 Sreq : Boolean; -- at least one statement required
385 Tatm : Boolean; -- THEN ABORT can terminate sequence
386 Whtm : Boolean; -- WHEN can terminate sequence
387 Unco : Boolean; -- Unconditional terminate after one statement
388 end record;
389 pragma Pack (SS_Rec);
390
391 SS_Eftm_Eltm_Sreq : constant SS_Rec := SS_Rec'(T, T, F, F, T, F, F, F);
392 SS_Eltm_Ortm_Tatm : constant SS_Rec := SS_Rec'(F, T, F, T, F, T, F, F);
393 SS_Extm_Sreq : constant SS_Rec := SS_Rec'(F, F, T, F, T, F, F, F);
394 SS_None : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, F, F);
395 SS_Ortm_Sreq : constant SS_Rec := SS_Rec'(F, F, F, T, T, F, F, F);
396 SS_Sreq : constant SS_Rec := SS_Rec'(F, F, F, F, T, F, F, F);
397 SS_Sreq_Whtm : constant SS_Rec := SS_Rec'(F, F, F, F, T, F, T, F);
398 SS_Whtm : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, T, F);
399 SS_Unco : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, F, T);
400
401 Goto_List : Elist_Id;
402 -- List of goto nodes appearing in the current compilation. Used to
403 -- recognize natural loops and convert them into bona fide loops for
404 -- optimization purposes.
405
406 Label_List : Elist_Id;
407 -- List of label nodes for labels appearing in the current compilation.
408 -- Used by Par.Labl to construct the corresponding implicit declarations.
409
410 -----------------
411 -- Scope Table --
412 -----------------
413
414 -- The scope table, also referred to as the scope stack, is used to record
415 -- the current scope context. It is organized as a stack, with inner nested
416 -- entries corresponding to higher entries on the stack. An entry is made
417 -- when the parser encounters the opening of a nested construct (such as a
418 -- record, task, package etc.), and then package Par.Endh uses this stack
419 -- to deal with END lines (including properly dealing with END nesting
420 -- errors).
421
422 type SS_End_Type is
423 -- Type of end entry required for this scope. The last two entries are
424 -- used only in the subprogram body case to mark the case of a suspicious
425 -- IS, or a bad IS (i.e. suspicions confirmed by missing BEGIN or END).
426 -- See separate section on dealing with IS used in place of semicolon.
427 -- Note that for many purposes E_Name, E_Suspicious_Is and E_Bad_Is are
428 -- treated the same (E_Suspicious_Is and E_Bad_Is are simply special cases
429 -- of E_Name). They are placed at the end of the enumeration so that a
430 -- test for >= E_Name catches all three cases efficiently.
431
432 (E_Dummy, -- dummy entry at outer level
433 E_Case, -- END CASE;
434 E_If, -- END IF;
435 E_Loop, -- END LOOP;
436 E_Record, -- END RECORD;
437 E_Return, -- END RETURN;
438 E_Select, -- END SELECT;
439 E_Name, -- END [name];
440 E_Suspicious_Is, -- END [name]; (case of suspicious IS)
441 E_Bad_Is); -- END [name]; (case of bad IS)
442
443 -- The following describes a single entry in the scope table
444
445 type Scope_Table_Entry is record
446 Etyp : SS_End_Type;
447 -- Type of end entry, as per above description
448
449 Lreq : Boolean;
450 -- A flag indicating whether the label, if present, is required to
451 -- appear on the end line. It is referenced only in the case of Etyp is
452 -- equal to E_Name or E_Suspicious_Is where the name may or may not be
453 -- required (yes for labeled block, no in other cases). Note that for
454 -- all cases except begin, the question of whether a label is required
455 -- can be determined from the other fields (for loop, it is required if
456 -- it is present, and for the other constructs it is never required or
457 -- allowed).
458
459 Ecol : Column_Number;
460 -- Contains the absolute column number (with tabs expanded) of the
461 -- expected column of the end assuming normal Ada indentation usage. If
462 -- the RM_Column_Check mode is set, this value is used for generating
463 -- error messages about indentation. Otherwise it is used only to
464 -- control heuristic error recovery actions.
465
466 Labl : Node_Id;
467 -- This field is used only for the LOOP and BEGIN cases, and is the
468 -- Node_Id value of the label name. For all cases except child units,
469 -- this value is an entity whose Chars field contains the name pointer
470 -- that identifies the label uniquely. For the child unit case the Labl
471 -- field references an N_Defining_Program_Unit_Name node for the name.
472 -- For cases other than LOOP or BEGIN, the Label field is set to Error,
473 -- indicating that it is an error to have a label on the end line.
474 -- (this is really a misuse of Error since there is no Error ???)
475
476 Decl : List_Id;
477 -- Points to the list of declarations (i.e. the declarative part)
478 -- associated with this construct. It is set only in the END [name]
479 -- cases, and is set to No_List for all other cases which do not have a
480 -- declarative unit associated with them. This is used for determining
481 -- the proper location for implicit label declarations.
482
483 Node : Node_Id;
484 -- Empty except in the case of entries for IF and CASE statements, in
485 -- which case it contains the N_If_Statement or N_Case_Statement node.
486 -- This is used for setting the End_Span field.
487
488 Sloc : Source_Ptr;
489 -- Source location of the opening token of the construct. This is used
490 -- to refer back to this line in error messages (such as missing or
491 -- incorrect end lines). The Sloc field is not used, and is not set, if
492 -- a label is present (the Labl field provides the text name of the
493 -- label in this case, which is fine for error messages).
494
495 S_Is : Source_Ptr;
496 -- S_Is is relevant only if Etyp is set to E_Suspicious_Is or E_Bad_Is.
497 -- It records the location of the IS that is considered to be
498 -- suspicious.
499
500 Junk : Boolean;
501 -- A boolean flag that is set true if the opening entry is the dubious
502 -- result of some prior error, e.g. a record entry where the record
503 -- keyword was missing. It is used to suppress the issuing of a
504 -- corresponding junk complaint about the end line (we do not want
505 -- to complain about a missing end record when there was no record).
506 end record;
507
508 -- The following declares the scope table itself. The Last field is the
509 -- stack pointer, so that Scope.Table (Scope.Last) is the top entry. The
510 -- oldest entry, at Scope_Stack (0), is a dummy entry with Etyp set to
511 -- E_Dummy, and the other fields undefined. This dummy entry ensures that
512 -- Scope_Stack (Scope_Stack_Ptr).Etyp can always be tested, and that the
513 -- scope stack pointer is always in range.
514
515 package Scope is new Table.Table (
516 Table_Component_Type => Scope_Table_Entry,
517 Table_Index_Type => Int,
518 Table_Low_Bound => 0,
519 Table_Initial => 50,
520 Table_Increment => 100,
521 Table_Name => "Scope");
522
523 ---------------------------------
524 -- Parsing Routines by Chapter --
525 ---------------------------------
526
527 -- Uncommented declarations in this section simply parse the construct
528 -- corresponding to their name, and return an ID value for the Node or
529 -- List that is created.
530
531 -------------
532 -- Par.Ch2 --
533 -------------
534
535 package Ch2 is
536 function P_Pragma (Skipping : Boolean := False) return Node_Id;
537 -- Scan out a pragma. If Skipping is True, then the caller is skipping
538 -- the pragma in the context of illegal placement (this is used to avoid
539 -- some junk cascaded messages).
540
541 function P_Identifier (C : Id_Check := None) return Node_Id;
542 -- Scans out an identifier. The parameter C determines the treatment
543 -- of reserved identifiers. See declaration of Id_Check for details.
544
545 function P_Pragmas_Opt return List_Id;
546 -- This function scans for a sequence of pragmas in other than a
547 -- declaration sequence or statement sequence context. All pragmas
548 -- can appear except pragmas Assert and Debug, which are only allowed
549 -- in a declaration or statement sequence context.
550
551 procedure P_Pragmas_Misplaced;
552 -- Skips misplaced pragmas with a complaint
553
554 procedure P_Pragmas_Opt (List : List_Id);
555 -- Parses optional pragmas and appends them to the List
556 end Ch2;
557
558 -------------
559 -- Par.Ch3 --
560 -------------
561
562 package Ch3 is
563 Missing_Begin_Msg : Error_Msg_Id;
564 -- This variable is set by a call to P_Declarative_Part. Normally it
565 -- is set to No_Error_Msg, indicating that no special processing is
566 -- required by the caller. The special case arises when a statement
567 -- is found in the sequence of declarations. In this case the Id of
568 -- the message issued ("declaration expected") is preserved in this
569 -- variable, then the caller can change it to an appropriate missing
570 -- begin message if indeed the BEGIN is missing.
571
572 function P_Array_Type_Definition return Node_Id;
573 function P_Basic_Declarative_Items return List_Id;
574 function P_Constraint_Opt return Node_Id;
575 function P_Declarative_Part return List_Id;
576 function P_Discrete_Choice_List return List_Id;
577 function P_Discrete_Range return Node_Id;
578 function P_Discrete_Subtype_Definition return Node_Id;
579 function P_Known_Discriminant_Part_Opt return List_Id;
580 function P_Signed_Integer_Type_Definition return Node_Id;
581 function P_Range return Node_Id;
582 function P_Range_Constraint return Node_Id;
583 function P_Record_Definition return Node_Id;
584 function P_Subtype_Mark return Node_Id;
585 function P_Subtype_Mark_Resync return Node_Id;
586 function P_Unknown_Discriminant_Part_Opt return Boolean;
587
588 function P_Access_Definition
589 (Null_Exclusion_Present : Boolean) return Node_Id;
590 -- Ada 2005 (AI-231/AI-254): The caller parses the null-exclusion part
591 -- and indicates if it was present
592
593 function P_Access_Type_Definition
594 (Header_Already_Parsed : Boolean := False) return Node_Id;
595 -- Ada 2005 (AI-254): The formal is used to indicate if the caller has
596 -- parsed the null_exclusion part. In this case the caller has also
597 -- removed the ACCESS token
598
599 procedure P_Component_Items (Decls : List_Id);
600 -- Scan out one or more component items and append them to the given
601 -- list. Only scans out more than one declaration in the case where the
602 -- source has a single declaration with multiple defining identifiers.
603
604 function P_Defining_Identifier (C : Id_Check := None) return Node_Id;
605 -- Scan out a defining identifier. The parameter C controls the
606 -- treatment of errors in case a reserved word is scanned. See the
607 -- declaration of this type for details.
608
609 function P_Interface_Type_Definition
610 (Abstract_Present : Boolean) return Node_Id;
611 -- Ada 2005 (AI-251): Parse the interface type definition part. Abstract
612 -- Present indicates if the reserved word "abstract" has been previously
613 -- found. It is used to report an error message because interface types
614 -- are by definition abstract tagged. We generate a record_definition
615 -- node if the list of interfaces is empty; otherwise we generate a
616 -- derived_type_definition node (the first interface in this list is the
617 -- ancestor interface).
618
619 function P_Null_Exclusion
620 (Allow_Anonymous_In_95 : Boolean := False) return Boolean;
621 -- Ada 2005 (AI-231): Parse the null-excluding part. A True result
622 -- indicates that the null-excluding part was present.
623 --
624 -- Allow_Anonymous_In_95 is True if we are in a context that allows
625 -- anonymous access types in Ada 95, in which case "not null" is legal
626 -- if it precedes "access".
627
628 function P_Subtype_Indication
629 (Not_Null_Present : Boolean := False) return Node_Id;
630 -- Ada 2005 (AI-231): The flag Not_Null_Present indicates that the
631 -- null-excluding part has been scanned out and it was present.
632
633 function P_Range_Or_Subtype_Mark
634 (Allow_Simple_Expression : Boolean := False) return Node_Id;
635 -- Scans out a range or subtype mark, and also permits a general simple
636 -- expression if Allow_Simple_Expression is set to True.
637
638 function Init_Expr_Opt (P : Boolean := False) return Node_Id;
639 -- If an initialization expression is present (:= expression), then
640 -- it is scanned out and returned, otherwise Empty is returned if no
641 -- initialization expression is present. This procedure also handles
642 -- certain common error cases cleanly. The parameter P indicates if
643 -- a right paren can follow the expression (default = no right paren
644 -- allowed).
645
646 procedure Skip_Declaration (S : List_Id);
647 -- Used when scanning statements to skip past a misplaced declaration
648 -- The declaration is scanned out and appended to the given list.
649 -- Token is known to be a declaration token (in Token_Class_Declk)
650 -- on entry, so there definition is a declaration to be scanned.
651
652 function P_Subtype_Indication
653 (Subtype_Mark : Node_Id;
654 Not_Null_Present : Boolean := False) return Node_Id;
655 -- This version of P_Subtype_Indication is called when the caller has
656 -- already scanned out the subtype mark which is passed as a parameter.
657 -- Ada 2005 (AI-231): The flag Not_Null_Present indicates that the
658 -- null-excluding part has been scanned out and it was present.
659
660 function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id;
661 -- Parse a subtype mark attribute. The caller has already parsed the
662 -- subtype mark, which is passed in as the argument, and has checked
663 -- that the current token is apostrophe.
664 end Ch3;
665
666 -------------
667 -- Par.Ch4 --
668 -------------
669
670 package Ch4 is
671 function P_Aggregate return Node_Id;
672 function P_Expression return Node_Id;
673 function P_Expression_Or_Range_Attribute return Node_Id;
674 function P_Function_Name return Node_Id;
675 function P_Name return Node_Id;
676 function P_Qualified_Simple_Name return Node_Id;
677 function P_Qualified_Simple_Name_Resync return Node_Id;
678 function P_Simple_Expression return Node_Id;
679 function P_Simple_Expression_Or_Range_Attribute return Node_Id;
680
681 function P_Case_Expression return Node_Id;
682 -- Scans out a case expression. Called with Token pointing to the CASE
683 -- keyword, and returns pointing to the terminating right parent,
684 -- semicolon, or comma, but does not consume this terminating token.
685
686 function P_Conditional_Expression return Node_Id;
687 -- Scans out a conditional expression. Called with Token pointing to
688 -- the IF keyword, and returns pointing to the terminating right paren,
689 -- semicolon or comma, but does not consume this terminating token.
690
691 function P_Expression_If_OK return Node_Id;
692 -- Scans out an expression in a context where a conditional expression
693 -- is permitted to appear without surrounding parentheses.
694
695 function P_Expression_No_Right_Paren return Node_Id;
696 -- Scans out an expression in contexts where the expression cannot be
697 -- terminated by a right paren (gives better error recovery if an errant
698 -- right paren is found after the expression).
699
700 function P_Expression_Or_Range_Attribute_If_OK return Node_Id;
701 -- Scans out an expression or range attribute where a conditional
702 -- expression is permitted to appear without surrounding parentheses.
703
704 function P_Qualified_Expression (Subtype_Mark : Node_Id) return Node_Id;
705 -- This routine scans out a qualified expression when the caller has
706 -- already scanned out the name and apostrophe of the construct.
707
708 function P_Quantified_Expression return Node_Id;
709 -- This routine scans out a quantified expression when the caller has
710 -- already scanned out the keyword "for" of the construct.
711 end Ch4;
712
713 -------------
714 -- Par.Ch5 --
715 -------------
716
717 package Ch5 is
718 function P_Condition return Node_Id;
719 -- Scan out and return a condition
720
721 function P_Loop_Parameter_Specification return Node_Id;
722 -- Used in loop constructs and quantified expressions.
723
724 function P_Statement_Name (Name_Node : Node_Id) return Node_Id;
725 -- Given a node representing a name (which is a call), converts it
726 -- to the syntactically corresponding procedure call statement.
727
728 function P_Sequence_Of_Statements (SS_Flags : SS_Rec) return List_Id;
729 -- The argument indicates the acceptable termination tokens.
730 -- See body in Par.Ch5 for details of the use of this parameter.
731
732 procedure Parse_Decls_Begin_End (Parent : Node_Id);
733 -- Parses declarations and handled statement sequence, setting
734 -- fields of Parent node appropriately.
735 end Ch5;
736
737 -------------
738 -- Par.Ch6 --
739 -------------
740
741 package Ch6 is
742 function P_Designator return Node_Id;
743 function P_Defining_Program_Unit_Name return Node_Id;
744 function P_Formal_Part return List_Id;
745 function P_Parameter_Profile return List_Id;
746 function P_Return_Statement return Node_Id;
747 function P_Subprogram_Specification return Node_Id;
748
749 procedure P_Mode (Node : Node_Id);
750 -- Sets In_Present and/or Out_Present flags in Node scanning past IN,
751 -- OUT or IN OUT tokens in the source.
752
753 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id;
754 -- Scans out any construct starting with either of the keywords
755 -- PROCEDURE or FUNCTION. The parameter indicates which possible
756 -- possible kinds of construct (body, spec, instantiation etc.)
757 -- are permissible in the current context.
758 end Ch6;
759
760 -------------
761 -- Par.Ch7 --
762 -------------
763
764 package Ch7 is
765 function P_Package (Pf_Flags : Pf_Rec) return Node_Id;
766 -- Scans out any construct starting with the keyword PACKAGE. The
767 -- parameter indicates which possible kinds of construct (body, spec,
768 -- instantiation etc.) are permissible in the current context.
769 end Ch7;
770
771 -------------
772 -- Par.Ch8 --
773 -------------
774
775 package Ch8 is
776 function P_Use_Clause return Node_Id;
777 end Ch8;
778
779 -------------
780 -- Par.Ch9 --
781 -------------
782
783 package Ch9 is
784 function P_Abort_Statement return Node_Id;
785 function P_Abortable_Part return Node_Id;
786 function P_Accept_Statement return Node_Id;
787 function P_Delay_Statement return Node_Id;
788 function P_Entry_Body return Node_Id;
789 function P_Protected return Node_Id;
790 function P_Requeue_Statement return Node_Id;
791 function P_Select_Statement return Node_Id;
792 function P_Task return Node_Id;
793 function P_Terminate_Alternative return Node_Id;
794 end Ch9;
795
796 --------------
797 -- Par.Ch10 --
798 --------------
799
800 package Ch10 is
801 function P_Compilation_Unit return Node_Id;
802 -- Note: this function scans a single compilation unit, and checks that
803 -- an end of file follows this unit, diagnosing any unexpected input as
804 -- an error, and then skipping it, so that Token is set to Tok_EOF on
805 -- return. An exception is in syntax-only mode, where multiple
806 -- compilation units are permitted. In this case, P_Compilation_Unit
807 -- does not check for end of file and there may be more compilation
808 -- units to scan. The caller can uniquely detect this situation by the
809 -- fact that Token is not set to Tok_EOF on return.
810 --
811 -- What about multiple unit/file capability that now exists???
812 --
813 -- The Ignore parameter is normally set False. It is set True in the
814 -- multiple unit per file mode if we are skipping past a unit that we
815 -- are not interested in.
816 end Ch10;
817
818 --------------
819 -- Par.Ch11 --
820 --------------
821
822 package Ch11 is
823 function P_Handled_Sequence_Of_Statements return Node_Id;
824 function P_Raise_Statement return Node_Id;
825
826 function Parse_Exception_Handlers return List_Id;
827 -- Parses the partial construct EXCEPTION followed by a list of
828 -- exception handlers which appears in a number of productions, and
829 -- returns the list of exception handlers.
830 end Ch11;
831
832 --------------
833 -- Par.Ch12 --
834 --------------
835
836 package Ch12 is
837 function P_Generic return Node_Id;
838 function P_Generic_Actual_Part_Opt return List_Id;
839 end Ch12;
840
841 --------------
842 -- Par.Ch13 --
843 --------------
844
845 package Ch13 is
846 function P_Representation_Clause return Node_Id;
847
848 function Aspect_Specifications_Present
849 (Strict : Boolean := Ada_Version < Ada_2012) return Boolean;
850 -- This function tests whether the next keyword is WITH followed by
851 -- something that looks reasonably like an aspect specification. If so,
852 -- True is returned. Otherwise False is returned. In either case control
853 -- returns with the token pointer unchanged (i.e. pointing to the WITH
854 -- token in the case where True is returned). This function takes care
855 -- of generating appropriate messages if aspect specifications appear
856 -- in versions of Ada prior to Ada 2012. The parameter strict can be
857 -- set to True, to be rather strict about considering something to be
858 -- an aspect specification. If Strict is False, then the circuitry is
859 -- rather more generous in considering something ill-formed to be an
860 -- attempt at an aspect specification. The default is more strict for
861 -- Ada versions before Ada 2012 (where aspect specifications are not
862 -- permitted). Note: this routine never checks the terminator token
863 -- for aspects so it does not matter whether the aspect speficiations
864 -- are terminated by semicolon or some other character
865
866 procedure P_Aspect_Specifications
867 (Decl : Node_Id;
868 Semicolon : Boolean := True);
869 -- This procedure scans out a series of aspect spefications. If argument
870 -- Semicolon is True, a terminating semicolon is also scanned. If this
871 -- argument is False, the scan pointer is left pointing past the aspects
872 -- and the caller must check for a proper terminator.
873 -- left pointing past the aspects, presumably pointing to a terminator.
874 --
875 -- P_Aspect_Specification is called with the current token pointing to
876 -- either a WITH keyword starting an aspect specification, or an
877 -- instance of the terminator token. In the former case, the aspect
878 -- specifications are scanned out including the terminator token if it
879 -- it is a semicolon, and the Has_Aspect_Specifications flag is set in
880 -- the given declaration node. A list of aspects is built and stored for
881 -- this declaration node using a call to Set_Aspect_Specifications. If
882 -- no WITH keyword is present, then this call has no effect other than
883 -- scanning out the terminator if it is a semicolon. If Decl is Error on
884 -- entry, any scanned aspect specifications are ignored and a message is
885 -- output saying aspect specifications not permitted here.
886
887 function P_Code_Statement (Subtype_Mark : Node_Id) return Node_Id;
888 -- Function to parse a code statement. The caller has scanned out
889 -- the name to be used as the subtype mark (but has not checked that
890 -- it is suitable for use as a subtype mark, i.e. is either an
891 -- identifier or a selected component). The current token is an
892 -- apostrophe and the following token is either a left paren or
893 -- RANGE (the latter being an error to be caught by P_Code_Statement.
894 end Ch13;
895
896 -- Note: the parsing for annexe J features (i.e. obsolescent features)
897 -- is found in the logical section where these features would be if
898 -- they were not obsolescent. In particular:
899
900 -- Delta constraint is parsed by P_Delta_Constraint (3.5.9)
901 -- At clause is parsed by P_At_Clause (13.1)
902 -- Mod clause is parsed by P_Mod_Clause (13.5.1)
903
904 --------------
905 -- Par.Endh --
906 --------------
907
908 -- Routines for handling end lines, including scope recovery
909
910 package Endh is
911 function Check_End (Decl : Node_Id := Empty) return Boolean;
912 -- Called when an end sequence is required. In the absence of an error
913 -- situation, Token contains Tok_End on entry, but in a missing end
914 -- case, this may not be the case. Pop_End_Context is used to determine
915 -- the appropriate action to be taken. The returned result is True if
916 -- an End sequence was encountered and False if no End sequence was
917 -- present. This occurs if the END keyword encountered was determined
918 -- to be improper and deleted (i.e. Pop_End_Context set End_Action to
919 -- Skip_And_Reject). Note that the END sequence includes a semicolon,
920 -- except in the case of END RECORD, where a semicolon follows the END
921 -- RECORD, but is not part of the record type definition itself.
922 --
923 -- If Decl is non-empty, then aspect specifications are permitted
924 -- following the end, and Decl is the declaration node with which
925 -- these aspect specifications are to be associated.
926
927 procedure End_Skip;
928 -- Skip past an end sequence. On entry Token contains Tok_End, and we
929 -- we know that the end sequence is syntactically incorrect, and that
930 -- an appropriate error message has already been posted. The mission
931 -- is simply to position the scan pointer to be the best guess of the
932 -- position after the end sequence. We do not issue any additional
933 -- error messages while carrying this out.
934
935 procedure End_Statements
936 (Parent : Node_Id := Empty;
937 Decl : Node_Id := Empty);
938 -- Called when an end is required or expected to terminate a sequence
939 -- of statements. The caller has already made an appropriate entry in
940 -- the Scope.Table to describe the expected form of the end. This can
941 -- only be used in cases where the only appropriate terminator is end.
942 -- If Parent is non-empty, then if a correct END line is encountered,
943 -- the End_Label field of Parent is set appropriately.
944 --
945 -- If Decl is non-null, then it is a declaration node, and aspect
946 -- specifications are permitted after the end statement. These aspect
947 -- specifications, if present, are stored in this declaration node.
948 end Endh;
949
950 --------------
951 -- Par.Sync --
952 --------------
953
954 -- These procedures are used to resynchronize after errors. Following an
955 -- error which is not immediately locally recoverable, the exception
956 -- Error_Resync is raised. The handler for Error_Resync typically calls
957 -- one of these recovery procedures to resynchronize the source position
958 -- to a point from which parsing can be restarted.
959
960 -- Note: these procedures output an information message that tokens are
961 -- being skipped, but this message is output only if the option for
962 -- Multiple_Errors_Per_Line is set in Options.
963
964 package Sync is
965 procedure Resync_Choice;
966 -- Used if an error occurs scanning a choice. The scan pointer is
967 -- advanced to the next vertical bar, arrow, or semicolon, whichever
968 -- comes first. We also quit if we encounter an end of file.
969
970 procedure Resync_Expression;
971 -- Used if an error is detected during the parsing of an expression.
972 -- It skips past tokens until either a token which cannot be part of
973 -- an expression is encountered (an expression terminator), or if a
974 -- comma or right parenthesis or vertical bar is encountered at the
975 -- current parenthesis level (a parenthesis level counter is maintained
976 -- to carry out this test).
977
978 procedure Resync_Past_Semicolon;
979 -- Used if an error occurs while scanning a sequence of declarations.
980 -- The scan pointer is positioned past the next semicolon and the scan
981 -- resumes. The scan is also resumed on encountering a token which
982 -- starts a declaration (but we make sure to skip at least one token
983 -- in this case, to avoid getting stuck in a loop).
984
985 procedure Resync_To_Semicolon;
986 -- Similar to Resync_Past_Semicolon, except that the scan pointer is
987 -- left pointing to the semicolon rather than past it.
988
989 procedure Resync_Past_Semicolon_Or_To_Loop_Or_Then;
990 -- Used if an error occurs while scanning a sequence of statements. The
991 -- scan pointer is positioned past the next semicolon, or to the next
992 -- occurrence of either then or loop, and the scan resumes.
993
994 procedure Resync_To_When;
995 -- Used when an error occurs scanning an entry index specification. The
996 -- scan pointer is positioned to the next WHEN (or to IS or semicolon if
997 -- either of these appear before WHEN, indicating another error has
998 -- occurred).
999
1000 procedure Resync_Semicolon_List;
1001 -- Used if an error occurs while scanning a parenthesized list of items
1002 -- separated by semicolons. The scan pointer is advanced to the next
1003 -- semicolon or right parenthesis at the outer parenthesis level, or
1004 -- to the next is or RETURN keyword occurrence, whichever comes first.
1005
1006 procedure Resync_Cunit;
1007 -- Synchronize to next token which could be the start of a compilation
1008 -- unit, or to the end of file token.
1009 end Sync;
1010
1011 --------------
1012 -- Par.Tchk --
1013 --------------
1014
1015 -- Routines to check for expected tokens
1016
1017 package Tchk is
1018
1019 -- Procedures with names of the form T_xxx, where Tok_xxx is a token
1020 -- name, check that the current token matches the required token, and
1021 -- if so, scan past it. If not, an error is issued indicating that
1022 -- the required token is not present (xxx expected). In most cases, the
1023 -- scan pointer is not moved in the not-found case, but there are some
1024 -- exceptions to this, see for example T_Id, where the scan pointer is
1025 -- moved across a literal appearing where an identifier is expected.
1026
1027 procedure T_Abort;
1028 procedure T_Arrow;
1029 procedure T_At;
1030 procedure T_Body;
1031 procedure T_Box;
1032 procedure T_Colon;
1033 procedure T_Colon_Equal;
1034 procedure T_Comma;
1035 procedure T_Dot_Dot;
1036 procedure T_For;
1037 procedure T_Greater_Greater;
1038 procedure T_Identifier;
1039 procedure T_In;
1040 procedure T_Is;
1041 procedure T_Left_Paren;
1042 procedure T_Loop;
1043 procedure T_Mod;
1044 procedure T_New;
1045 procedure T_Of;
1046 procedure T_Or;
1047 procedure T_Private;
1048 procedure T_Range;
1049 procedure T_Record;
1050 procedure T_Right_Paren;
1051 procedure T_Semicolon;
1052 procedure T_Then;
1053 procedure T_Type;
1054 procedure T_Use;
1055 procedure T_When;
1056 procedure T_With;
1057
1058 -- Procedures having names of the form TF_xxx, where Tok_xxx is a token
1059 -- name check that the current token matches the required token, and
1060 -- if so, scan past it. If not, an error message is issued indicating
1061 -- that the required token is not present (xxx expected).
1062
1063 -- If the missing token is at the end of the line, then control returns
1064 -- immediately after posting the message. If there are remaining tokens
1065 -- on the current line, a search is conducted to see if the token
1066 -- appears later on the current line, as follows:
1067
1068 -- A call to Scan_Save is issued and a forward search for the token
1069 -- is carried out. If the token is found on the current line before a
1070 -- semicolon, then it is scanned out and the scan continues from that
1071 -- point. If not the scan is restored to the point where it was missing.
1072
1073 procedure TF_Arrow;
1074 procedure TF_Is;
1075 procedure TF_Loop;
1076 procedure TF_Return;
1077 procedure TF_Semicolon;
1078 procedure TF_Then;
1079 procedure TF_Use;
1080
1081 -- Procedures with names of the form U_xxx, where Tok_xxx is a token
1082 -- name, are just like the corresponding T_xxx procedures except that
1083 -- an error message, if given, is unconditional.
1084
1085 procedure U_Left_Paren;
1086 procedure U_Right_Paren;
1087 end Tchk;
1088
1089 --------------
1090 -- Par.Util --
1091 --------------
1092
1093 package Util is
1094 function Bad_Spelling_Of (T : Token_Type) return Boolean;
1095 -- This function is called in an error situation. It checks if the
1096 -- current token is an identifier whose name is a plausible bad
1097 -- spelling of the given keyword token, and if so, issues an error
1098 -- message, sets Token from T, and returns True. Otherwise Token is
1099 -- unchanged, and False is returned.
1100
1101 procedure Check_Bad_Layout;
1102 -- Check for bad indentation in RM checking mode. Used for statements
1103 -- and declarations. Checks if current token is at start of line and
1104 -- is exdented from the current expected end column, and if so an
1105 -- error message is generated.
1106
1107 procedure Check_Misspelling_Of (T : Token_Type);
1108 pragma Inline (Check_Misspelling_Of);
1109 -- This is similar to the function above, except that it does not
1110 -- return a result. It is typically used in a situation where any
1111 -- identifier is an error, and it makes sense to simply convert it
1112 -- to the given token if it is a plausible misspelling of it.
1113
1114 procedure Check_95_Keyword (Token_95, Next : Token_Type);
1115 -- This routine checks if the token after the current one matches the
1116 -- Next argument. If so, the scan is backed up to the current token
1117 -- and Token_Type is changed to Token_95 after issuing an appropriate
1118 -- error message ("(Ada 83) keyword xx cannot be used"). If not,
1119 -- the scan is backed up with Token_Type unchanged. This routine
1120 -- is used to deal with an attempt to use a 95 keyword in Ada 83
1121 -- mode. The caller has typically checked that the current token,
1122 -- an identifier, matches one of the 95 keywords.
1123
1124 procedure Check_Simple_Expression (E : Node_Id);
1125 -- Given an expression E, that has just been scanned, so that Expr_Form
1126 -- is still set, outputs an error if E is a non-simple expression. E is
1127 -- not modified by this call.
1128
1129 procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id);
1130 -- Like Check_Simple_Expression, except that the error message is only
1131 -- given when operating in Ada 83 mode, and includes "in Ada 83".
1132
1133 function Check_Subtype_Mark (Mark : Node_Id) return Node_Id;
1134 -- Called to check that a node representing a name (or call) is
1135 -- suitable for a subtype mark, i.e, that it is an identifier or
1136 -- a selected component. If so, or if it is already Error, then
1137 -- it is returned unchanged. Otherwise an error message is issued
1138 -- and Error is returned.
1139
1140 function Comma_Present return Boolean;
1141 -- Used in comma delimited lists to determine if a comma is present, or
1142 -- can reasonably be assumed to have been present (an error message is
1143 -- generated in the latter case). If True is returned, the scan has been
1144 -- positioned past the comma. If False is returned, the scan position
1145 -- is unchanged. Note that all comma-delimited lists are terminated by
1146 -- a right paren, so the only legitimate tokens when Comma_Present is
1147 -- called are right paren and comma. If some other token is found, then
1148 -- Comma_Present has the job of deciding whether it is better to pretend
1149 -- a comma was present, post a message for a missing comma and return
1150 -- True, or return False and let the caller diagnose the missing right
1151 -- parenthesis.
1152
1153 procedure Discard_Junk_Node (N : Node_Id);
1154 procedure Discard_Junk_List (L : List_Id);
1155 pragma Inline (Discard_Junk_Node);
1156 pragma Inline (Discard_Junk_List);
1157 -- These procedures do nothing at all, their effect is simply to discard
1158 -- the argument. A typical use is to skip by some junk that is not
1159 -- expected in the current context.
1160
1161 procedure Ignore (T : Token_Type);
1162 -- If current token matches T, then give an error message and skip
1163 -- past it, otherwise the call has no effect at all. T may be any
1164 -- reserved word token, or comma, left or right paren, or semicolon.
1165
1166 function Is_Reserved_Identifier (C : Id_Check := None) return Boolean;
1167 -- Test if current token is a reserved identifier. This test is based
1168 -- on the token being a keyword and being spelled in typical identifier
1169 -- style (i.e. starting with an upper case letter). The parameter C
1170 -- determines the special treatment if a reserved word is encountered
1171 -- that has the normal casing of a reserved word.
1172
1173 procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type);
1174 -- Called when the previous token is an identifier (whose Token_Node
1175 -- value is given by Prev) to check if current token is an identifier
1176 -- that can be merged with the previous one adding an underscore. The
1177 -- merge is only attempted if the following token matches Nxt. If all
1178 -- conditions are met, an error message is issued, and the merge is
1179 -- carried out, modifying the Chars field of Prev.
1180
1181 function Next_Token_Is (Tok : Token_Type) return Boolean;
1182 -- Looks at token after current one and returns True if the token type
1183 -- matches Tok. The scan is unconditionally restored on return.
1184
1185 procedure No_Constraint;
1186 -- Called in a place where no constraint is allowed, but one might
1187 -- appear due to a common error (e.g. after the type mark in a procedure
1188 -- parameter. If a constraint is present, an error message is posted,
1189 -- and the constraint is scanned and discarded.
1190
1191 procedure Push_Scope_Stack;
1192 pragma Inline (Push_Scope_Stack);
1193 -- Push a new entry onto the scope stack. Scope.Last (the stack pointer)
1194 -- is incremented. The Junk field is preinitialized to False. The caller
1195 -- is expected to fill in all remaining entries of the new top stack
1196 -- entry at Scope.Table (Scope.Last).
1197
1198 procedure Pop_Scope_Stack;
1199 -- Pop an entry off the top of the scope stack. Scope_Last (the scope
1200 -- table stack pointer) is decremented by one. It is a fatal error to
1201 -- try to pop off the dummy entry at the bottom of the stack (i.e.
1202 -- Scope.Last must be non-zero at the time of call).
1203
1204 function Separate_Present return Boolean;
1205 -- Determines if the current token is either Tok_Separate, or an
1206 -- identifier that is a possible misspelling of "separate" followed
1207 -- by a semicolon. True is returned if so, otherwise False.
1208
1209 procedure Signal_Bad_Attribute;
1210 -- The current token is an identifier that is supposed to be an
1211 -- attribute identifier but is not. This routine posts appropriate
1212 -- error messages, including a check for a near misspelling.
1213
1214 function Token_Is_At_Start_Of_Line return Boolean;
1215 pragma Inline (Token_Is_At_Start_Of_Line);
1216 -- Determines if the current token is the first token on the line
1217
1218 function Token_Is_At_End_Of_Line return Boolean;
1219 -- Determines if the current token is the last token on the line
1220
1221 end Util;
1222
1223 --------------
1224 -- Par.Prag --
1225 --------------
1226
1227 -- The processing for pragmas is split off from chapter 2
1228
1229 function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id;
1230 -- This function is passed a tree for a pragma that has been scanned out.
1231 -- The pragma is syntactically well formed according to the general syntax
1232 -- for pragmas and the pragma identifier is for one of the recognized
1233 -- pragmas. It performs specific syntactic checks for specific pragmas.
1234 -- The result is the input node if it is OK, or Error otherwise. The
1235 -- reason that this is separated out is to facilitate the addition
1236 -- of implementation defined pragmas. The second parameter records the
1237 -- location of the semicolon following the pragma (this is needed for
1238 -- correct processing of the List and Page pragmas). The returned value
1239 -- is a copy of Pragma_Node, or Error if an error is found. Note that
1240 -- at the point where Prag is called, the right paren ending the pragma
1241 -- has been scanned out, and except in the case of pragma Style_Checks,
1242 -- so has the following semicolon. For Style_Checks, the caller delays
1243 -- the scanning of the semicolon so that it will be scanned using the
1244 -- settings from the Style_Checks pragma preceding it.
1245
1246 --------------
1247 -- Par.Labl --
1248 --------------
1249
1250 procedure Labl;
1251 -- This procedure creates implicit label declarations for all labels that
1252 -- are declared in the current unit. Note that this could conceptually be
1253 -- done at the point where the labels are declared, but it is tricky to do
1254 -- it then, since the tree is not hooked up at the point where the label is
1255 -- declared (e.g. a sequence of statements is not yet attached to its
1256 -- containing scope at the point a label in the sequence is found).
1257
1258 --------------
1259 -- Par.Load --
1260 --------------
1261
1262 procedure Load;
1263 -- This procedure loads all subsidiary units that are required by this
1264 -- unit, including with'ed units, specs for bodies, and parents for child
1265 -- units. It does not load bodies for inlined procedures and generics,
1266 -- since we don't know till semantic analysis is complete what is needed.
1267
1268 -----------
1269 -- Stubs --
1270 -----------
1271
1272 -- The package bodies can see all routines defined in all other subpackages
1273
1274 use Ch2;
1275 use Ch3;
1276 use Ch4;
1277 use Ch5;
1278 use Ch6;
1279 use Ch7;
1280 use Ch8;
1281 use Ch9;
1282 use Ch10;
1283 use Ch11;
1284 use Ch12;
1285 use Ch13;
1286
1287 use Endh;
1288 use Tchk;
1289 use Sync;
1290 use Util;
1291
1292 package body Ch2 is separate;
1293 package body Ch3 is separate;
1294 package body Ch4 is separate;
1295 package body Ch5 is separate;
1296 package body Ch6 is separate;
1297 package body Ch7 is separate;
1298 package body Ch8 is separate;
1299 package body Ch9 is separate;
1300 package body Ch10 is separate;
1301 package body Ch11 is separate;
1302 package body Ch12 is separate;
1303 package body Ch13 is separate;
1304
1305 package body Endh is separate;
1306 package body Tchk is separate;
1307 package body Sync is separate;
1308 package body Util is separate;
1309
1310 function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id
1311 is separate;
1312
1313 procedure Labl is separate;
1314 procedure Load is separate;
1315
1316 -- Start of processing for Par
1317
1318 begin
1319 Compiler_State := Parsing;
1320
1321 -- Deal with configuration pragmas case first
1322
1323 if Configuration_Pragmas then
1324 declare
1325 Pragmas : constant List_Id := Empty_List;
1326 P_Node : Node_Id;
1327
1328 begin
1329 loop
1330 if Token = Tok_EOF then
1331 Compiler_State := Analyzing;
1332 return Pragmas;
1333
1334 elsif Token /= Tok_Pragma then
1335 Error_Msg_SC ("only pragmas allowed in configuration file");
1336 Compiler_State := Analyzing;
1337 return Error_List;
1338
1339 else
1340 P_Node := P_Pragma;
1341
1342 if Nkind (P_Node) = N_Pragma then
1343
1344 -- Give error if bad pragma
1345
1346 if not Is_Configuration_Pragma_Name (Pragma_Name (P_Node))
1347 and then Pragma_Name (P_Node) /= Name_Source_Reference
1348 then
1349 if Is_Pragma_Name (Pragma_Name (P_Node)) then
1350 Error_Msg_N
1351 ("only configuration pragmas allowed " &
1352 "in configuration file", P_Node);
1353 else
1354 Error_Msg_N
1355 ("unrecognized pragma in configuration file",
1356 P_Node);
1357 end if;
1358
1359 -- Pragma is OK config pragma, so collect it
1360
1361 else
1362 Append (P_Node, Pragmas);
1363 end if;
1364 end if;
1365 end if;
1366 end loop;
1367 end;
1368
1369 -- Normal case of compilation unit
1370
1371 else
1372 Save_Opt_Config_Switches (Save_Config_Switches);
1373
1374 -- The following loop runs more than once in syntax check mode
1375 -- where we allow multiple compilation units in the same file
1376 -- and in Multiple_Unit_Per_file mode where we skip units till
1377 -- we get to the unit we want.
1378
1379 for Ucount in Pos loop
1380 Set_Opt_Config_Switches
1381 (Is_Internal_File_Name (File_Name (Current_Source_File)),
1382 Current_Source_Unit = Main_Unit);
1383
1384 -- Initialize scope table and other parser control variables
1385
1386 Compiler_State := Parsing;
1387 Scope.Init;
1388 Scope.Increment_Last;
1389 Scope.Table (0).Etyp := E_Dummy;
1390 SIS_Entry_Active := False;
1391 Last_Resync_Point := No_Location;
1392
1393 Goto_List := New_Elmt_List;
1394 Label_List := New_Elmt_List;
1395
1396 -- If in multiple unit per file mode, skip past ignored unit
1397
1398 if Ucount < Multiple_Unit_Index then
1399
1400 -- We skip in syntax check only mode, since we don't want to do
1401 -- anything more than skip past the unit and ignore it. This means
1402 -- we skip processing like setting up a unit table entry.
1403
1404 declare
1405 Save_Operating_Mode : constant Operating_Mode_Type :=
1406 Operating_Mode;
1407
1408 Save_Style_Check : constant Boolean := Style_Check;
1409
1410 begin
1411 Operating_Mode := Check_Syntax;
1412 Style_Check := False;
1413 Discard_Node (P_Compilation_Unit);
1414 Operating_Mode := Save_Operating_Mode;
1415 Style_Check := Save_Style_Check;
1416
1417 -- If we are at an end of file, and not yet at the right unit,
1418 -- then we have a fatal error. The unit is missing.
1419
1420 if Token = Tok_EOF then
1421 Error_Msg_SC ("file has too few compilation units");
1422 raise Unrecoverable_Error;
1423 end if;
1424 end;
1425
1426 -- Here if we are not skipping a file in multiple unit per file mode.
1427 -- Parse the unit that we are interested in. Note that in check
1428 -- syntax mode we are interested in all units in the file.
1429
1430 else
1431 declare
1432 Comp_Unit_Node : constant Node_Id := P_Compilation_Unit;
1433
1434 begin
1435 -- If parsing was successful and we are not in check syntax
1436 -- mode, check that language-defined units are compiled in GNAT
1437 -- mode. For this purpose we do NOT consider renamings in annex
1438 -- J as predefined. That allows users to compile their own
1439 -- versions of these files, and in particular, in the VMS
1440 -- implementation, the DEC versions can be substituted for the
1441 -- standard Ada 95 versions. Another exception is System.RPC
1442 -- and its children. This allows a user to supply their own
1443 -- communication layer.
1444
1445 if Comp_Unit_Node /= Error
1446 and then Operating_Mode = Generate_Code
1447 and then Current_Source_Unit = Main_Unit
1448 and then not GNAT_Mode
1449 then
1450 declare
1451 Uname : constant String :=
1452 Get_Name_String
1453 (Unit_Name (Current_Source_Unit));
1454 Name : String (1 .. Uname'Length - 2);
1455
1456 begin
1457 -- Because Unit_Name includes "%s"/"%b", we need to strip
1458 -- the last two characters to get the real unit name.
1459
1460 Name := Uname (Uname'First .. Uname'Last - 2);
1461
1462 if Name = "ada" or else
1463 Name = "interfaces" or else
1464 Name = "system"
1465 then
1466 Error_Msg
1467 ("language-defined units cannot be recompiled",
1468 Sloc (Unit (Comp_Unit_Node)));
1469
1470 elsif Name'Length > 4
1471 and then
1472 Name (Name'First .. Name'First + 3) = "ada."
1473 then
1474 Error_Msg
1475 ("user-defined descendents of package Ada " &
1476 "are not allowed",
1477 Sloc (Unit (Comp_Unit_Node)));
1478
1479 elsif Name'Length > 11
1480 and then
1481 Name (Name'First .. Name'First + 10) = "interfaces."
1482 then
1483 Error_Msg
1484 ("user-defined descendents of package Interfaces " &
1485 "are not allowed",
1486 Sloc (Unit (Comp_Unit_Node)));
1487
1488 elsif Name'Length > 7
1489 and then Name (Name'First .. Name'First + 6) = "system."
1490 and then Name /= "system.rpc"
1491 and then
1492 (Name'Length < 11
1493 or else Name (Name'First .. Name'First + 10) /=
1494 "system.rpc.")
1495 then
1496 Error_Msg
1497 ("user-defined descendents of package System " &
1498 "are not allowed",
1499 Sloc (Unit (Comp_Unit_Node)));
1500 end if;
1501 end;
1502 end if;
1503 end;
1504
1505 -- All done if at end of file
1506
1507 exit when Token = Tok_EOF;
1508
1509 -- If we are not at an end of file, it means we are in syntax
1510 -- check only mode, and we keep the loop going to parse all
1511 -- remaining units in the file.
1512
1513 end if;
1514
1515 Restore_Opt_Config_Switches (Save_Config_Switches);
1516 end loop;
1517
1518 -- Now that we have completely parsed the source file, we can complete
1519 -- the source file table entry.
1520
1521 Complete_Source_File_Entry;
1522
1523 -- An internal error check, the scope stack should now be empty
1524
1525 pragma Assert (Scope.Last = 0);
1526
1527 -- Here we make the SCO table entries for the main unit
1528
1529 if Generate_SCO then
1530 SCO_Record (Main_Unit);
1531 end if;
1532
1533 -- Remaining steps are to create implicit label declarations and to load
1534 -- required subsidiary sources. These steps are required only if we are
1535 -- doing semantic checking.
1536
1537 if Operating_Mode /= Check_Syntax or else Debug_Flag_F then
1538 Par.Labl;
1539 Par.Load;
1540 end if;
1541
1542 -- Restore settings of switches saved on entry
1543
1544 Restore_Opt_Config_Switches (Save_Config_Switches);
1545 Set_Comes_From_Source_Default (False);
1546 Compiler_State := Analyzing;
1547 return Empty_List;
1548 end if;
1549 end Par;