2003-05-31 Bud Davis <bdavis9659@comcast.net>
[official-gcc.git] / gcc / ada / par.adb
blob412759e80bf2f7ac3542530c910476c2518d4ca0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2002 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Csets; use Csets;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Fname; use Fname;
34 with Lib; use Lib;
35 with Namet; use Namet;
36 with Nlists; use Nlists;
37 with Nmake; use Nmake;
38 with Opt; use Opt;
39 with Output; use Output;
40 with Scans; use Scans;
41 with Scn; use Scn;
42 with Sinput; use Sinput;
43 with Sinput.L; use Sinput.L;
44 with Sinfo; use Sinfo;
45 with Snames; use Snames;
46 with Style;
47 with Table;
49 function Par (Configuration_Pragmas : Boolean) return List_Id is
51 Num_Library_Units : Natural := 0;
52 -- Count number of units parsed (relevant only in syntax check only mode,
53 -- since in semantics check mode only a single unit is permitted anyway)
55 Unit_Node : Node_Id;
56 -- Stores compilation unit node for current unit
58 Save_Config_Switches : Config_Switches_Type;
59 -- Variable used to save values of config switches while we parse the
60 -- new unit, to be restored on exit for proper recursive behavior.
62 Loop_Block_Count : Nat := 0;
63 -- Counter used for constructing loop/block names (see the routine
64 -- Par.Ch5.Get_Loop_Block_Name)
66 --------------------
67 -- Error Recovery --
68 --------------------
70 -- When an error is encountered, a call is made to one of the Error_Msg
71 -- routines to record the error. If the syntax scan is not derailed by the
72 -- error (e.g. a complaint that logical operators are inconsistent in an
73 -- EXPRESSION), then control returns from the Error_Msg call, and the
74 -- parse continues unimpeded.
76 -- If on the other hand, the Error_Msg represents a situation from which
77 -- the parser cannot recover locally, the exception Error_Resync is raised
78 -- immediately after the call to Error_Msg. Handlers for Error_Resync
79 -- are located at strategic points to resynchronize the parse. For example,
80 -- when an error occurs in a statement, the handler skips to the next
81 -- semicolon and continues the scan from there.
83 -- Each parsing procedure contains a note with the heading "Error recovery"
84 -- which shows if it can propagate the Error_Resync exception. In order
85 -- not to propagate the exception, a procedure must either contain its own
86 -- handler for this exception, or it must not call any other routines which
87 -- propagate the exception.
89 -- Note: the arrangement of Error_Resync handlers is such that it should
90 -- never be possible to transfer control through a procedure which made
91 -- an entry in the scope stack, invalidating the contents of the stack.
93 Error_Resync : exception;
94 -- Exception raised on error that is not handled locally, see above.
96 Last_Resync_Point : Source_Ptr;
97 -- The resynchronization routines in Par.Sync run a risk of getting
98 -- stuck in an infinite loop if they do not skip a token, and the caller
99 -- keeps repeating the same resync call. On the other hand, if they skip
100 -- a token unconditionally, some recovery opportunities are missed. The
101 -- variable Last_Resync_Point records the token location previously set
102 -- by a Resync call, and if a subsequent Resync call occurs at the same
103 -- location, then the Resync routine does guarantee to skip a token.
105 --------------------------------------------
106 -- Handling Semicolon Used in Place of IS --
107 --------------------------------------------
109 -- The following global variables are used in handling the error situation
110 -- of using a semicolon in place of IS in a subprogram declaration as in:
112 -- procedure X (Y : Integer);
113 -- Q : Integer;
114 -- begin
115 -- ...
116 -- end;
118 -- The two contexts in which this can appear are at the outer level, and
119 -- within a declarative region. At the outer level, we know something is
120 -- wrong as soon as we see the Q (or begin, if there are no declarations),
121 -- and we can immediately decide that the semicolon should have been IS.
123 -- The situation in a declarative region is more complex. The declaration
124 -- of Q could belong to the outer region, and we do not know that we have
125 -- an error until we hit the begin. It is still not clear at this point
126 -- from a syntactic point of view that something is wrong, because the
127 -- begin could belong to the enclosing subprogram or package. However, we
128 -- can incorporate a bit of semantic knowledge and note that the body of
129 -- X is missing, so we definitely DO have an error. We diagnose this error
130 -- as semicolon in place of IS on the subprogram line.
132 -- There are two styles for this diagnostic. If the begin immediately
133 -- follows the semicolon, then we can place a flag (IS expected) right
134 -- on the semicolon. Otherwise we do not detect the error until we hit
135 -- the begin which refers back to the line with the semicolon.
137 -- To control the process in the second case, the following global
138 -- variables are set to indicate that we have a subprogram declaration
139 -- whose body is required and has not yet been found. The prefix SIS
140 -- stands for "Subprogram IS" handling.
142 SIS_Entry_Active : Boolean;
143 -- Set True to indicate that an entry is active (i.e. that a subprogram
144 -- declaration has been encountered, and no body for this subprogram has
145 -- been encountered). The remaining fields are valid only if this is True.
147 SIS_Labl : Node_Id;
148 -- Subprogram designator
150 SIS_Sloc : Source_Ptr;
151 -- Source location of FUNCTION/PROCEDURE keyword
153 SIS_Ecol : Column_Number;
154 -- Column number of FUNCTION/PROCEDURE keyword
156 SIS_Semicolon_Sloc : Source_Ptr;
157 -- Source location of semicolon at end of subprogram declaration
159 SIS_Declaration_Node : Node_Id;
160 -- Pointer to tree node for subprogram declaration
162 SIS_Missing_Semicolon_Message : Error_Msg_Id;
163 -- Used to save message ID of missing semicolon message (which will be
164 -- modified to missing IS if necessary). Set to No_Error_Msg in the
165 -- normal (non-error) case.
167 -- Five things can happen to an active SIS entry
169 -- 1. If a BEGIN is encountered with an SIS entry active, then we have
170 -- exactly the situation in which we know the body of the subprogram is
171 -- missing. After posting an error message, we change the spec to a body,
172 -- rechaining the declarations that intervened between the spec and BEGIN.
174 -- 2. Another subprogram declaration or body is encountered. In this
175 -- case the entry gets overwritten with the information for the new
176 -- subprogram declaration. We don't catch some nested cases this way,
177 -- but it doesn't seem worth the effort.
179 -- 3. A nested declarative region (e.g. package declaration or package
180 -- body) is encountered. The SIS active indication is reset at the start
181 -- of such a nested region. Again, like case 2, this causes us to miss
182 -- some nested cases, but it doesn't seen worth the effort to stack and
183 -- unstack the SIS information. Maybe we will reconsider this if we ever
184 -- get a complaint about a missed case :-)
186 -- 4. We encounter a valid pragma INTERFACE or IMPORT that effectively
187 -- supplies the missing body. In this case we reset the entry.
189 -- 5. We encounter the end of the declarative region without encoutering
190 -- a BEGIN first. In this situation we simply reset the entry. We know
191 -- that there is a missing body, but it seems more reasonable to let the
192 -- later semantic checking discover this.
194 --------------------------------------------
195 -- Handling IS Used in Place of Semicolon --
196 --------------------------------------------
198 -- This is a somewhat trickier situation, and we can't catch it in all
199 -- cases, but we do our best to detect common situations resulting from
200 -- a "cut and paste" operation which forgets to change the IS to semicolon.
201 -- Consider the following example:
203 -- package body X is
204 -- procedure A;
205 -- procedure B is
206 -- procedure C;
207 -- ...
208 -- procedure D is
209 -- begin
210 -- ...
211 -- end;
212 -- begin
213 -- ...
214 -- end;
216 -- The trouble is that the section of text from PROCEDURE B through END;
217 -- consitutes a valid procedure body, and the danger is that we find out
218 -- far too late that something is wrong (indeed most compilers will behave
219 -- uncomfortably on the above example).
221 -- We have two approaches to helping to control this situation. First we
222 -- make every attempt to avoid swallowing the last END; if we can be
223 -- sure that some error will result from doing so. In particular, we won't
224 -- accept the END; unless it is exactly correct (in particular it must not
225 -- have incorrect name tokens), and we won't accept it if it is immediately
226 -- followed by end of file, WITH or SEPARATE (all tokens that unmistakeably
227 -- signal the start of a compilation unit, and which therefore allow us to
228 -- reserve the END; for the outer level.) For more details on this aspect
229 -- of the handling, see package Par.Endh.
231 -- If we can avoid eating up the END; then the result in the absense of
232 -- any additional steps would be to post a missing END referring back to
233 -- the subprogram with the bogus IS. Similarly, if the enclosing package
234 -- has no BEGIN, then the result is a missing BEGIN message, which again
235 -- refers back to the subprogram header.
237 -- Such an error message is not too bad (it's already a big improvement
238 -- over what many parsers do), but it's not ideal, because the declarations
239 -- following the IS have been absorbed into the wrong scope. In the above
240 -- case, this could result for example in a bogus complaint that the body
241 -- of D was missing from the package.
243 -- To catch at least some of these cases, we take the following additional
244 -- steps. First, a subprogram body is marked as having a suspicious IS if
245 -- the declaration line is followed by a line which starts with a symbol
246 -- that can start a declaration in the same column, or to the left of the
247 -- column in which the FUNCTION or PROCEDURE starts (normal style is to
248 -- indent any declarations which really belong a subprogram). If such a
249 -- subprogram encounters a missing BEGIN or missing END, then we decide
250 -- that the IS should have been a semicolon, and the subprogram body node
251 -- is marked (by setting the Bad_Is_Detected flag true. Note that we do
252 -- not do this for library level procedures, only for nested procedures,
253 -- since for library level procedures, we must have a body.
255 -- The processing for a declarative part checks to see if the last
256 -- declaration scanned is marked in this way, and if it is, the tree
257 -- is modified to reflect the IS being interpreted as a semicolon.
259 ---------------------------------------------------
260 -- Parser Type Definitions and Control Variables --
261 ---------------------------------------------------
263 -- The following variable and associated type declaration are used by the
264 -- expression parsing routines to return more detailed information about
265 -- the categorization of a parsed expression.
267 type Expr_Form_Type is (
268 EF_Simple_Name, -- Simple name, i.e. possibly qualified identifier
269 EF_Name, -- Simple expression which could also be a name
270 EF_Simple, -- Simple expression which is not call or name
271 EF_Range_Attr, -- Range attribute reference
272 EF_Non_Simple); -- Expression that is not a simple expression
274 Expr_Form : Expr_Form_Type;
276 -- The following type is used for calls to P_Subprogram, P_Package, P_Task,
277 -- P_Protected to indicate which of several possibilities is acceptable.
279 type Pf_Rec is record
280 Spcn : Boolean; -- True if specification OK
281 Decl : Boolean; -- True if declaration OK
282 Gins : Boolean; -- True if generic instantiation OK
283 Pbod : Boolean; -- True if proper body OK
284 Rnam : Boolean; -- True if renaming declaration OK
285 Stub : Boolean; -- True if body stub OK
286 Fil1 : Boolean; -- Filler to fill to 8 bits
287 Fil2 : Boolean; -- Filler to fill to 8 bits
288 end record;
289 pragma Pack (Pf_Rec);
291 function T return Boolean renames True;
292 function F return Boolean renames False;
294 Pf_Decl_Gins_Pbod_Rnam_Stub : constant Pf_Rec :=
295 Pf_Rec'(F, T, T, T, T, T, F, F);
296 Pf_Decl : constant Pf_Rec :=
297 Pf_Rec'(F, T, F, F, F, F, F, F);
298 Pf_Decl_Gins_Pbod_Rnam : constant Pf_Rec :=
299 Pf_Rec'(F, T, T, T, T, F, F, F);
300 Pf_Decl_Pbod : constant Pf_Rec :=
301 Pf_Rec'(F, T, F, T, F, F, F, F);
302 Pf_Pbod : constant Pf_Rec :=
303 Pf_Rec'(F, F, F, T, F, F, F, F);
304 Pf_Spcn : constant Pf_Rec :=
305 Pf_Rec'(T, F, F, F, F, F, F, F);
306 -- The above are the only allowed values of Pf_Rec arguments
308 type SS_Rec is record
309 Eftm : Boolean; -- ELSIF can terminate sequence
310 Eltm : Boolean; -- ELSE can terminate sequence
311 Extm : Boolean; -- EXCEPTION can terminate sequence
312 Ortm : Boolean; -- OR can terminate sequence
313 Sreq : Boolean; -- at least one statement required
314 Tatm : Boolean; -- THEN ABORT can terminate sequence
315 Whtm : Boolean; -- WHEN can terminate sequence
316 Unco : Boolean; -- Unconditional terminate after one statement
317 end record;
318 pragma Pack (SS_Rec);
320 SS_Eftm_Eltm_Sreq : constant SS_Rec := SS_Rec'(T, T, F, F, T, F, F, F);
321 SS_Eltm_Ortm_Tatm : constant SS_Rec := SS_Rec'(F, T, F, T, F, T, F, F);
322 SS_Extm_Sreq : constant SS_Rec := SS_Rec'(F, F, T, F, T, F, F, F);
323 SS_None : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, F, F);
324 SS_Ortm_Sreq : constant SS_Rec := SS_Rec'(F, F, F, T, T, F, F, F);
325 SS_Sreq : constant SS_Rec := SS_Rec'(F, F, F, F, T, F, F, F);
326 SS_Sreq_Whtm : constant SS_Rec := SS_Rec'(F, F, F, F, T, F, T, F);
327 SS_Whtm : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, T, F);
328 SS_Unco : constant SS_Rec := SS_Rec'(F, F, F, F, F, F, F, T);
330 Label_List : Elist_Id;
331 -- List of label nodes for labels appearing in the current compilation.
332 -- Used by Par.Labl to construct the corresponding implicit declarations.
334 -----------------
335 -- Scope Table --
336 -----------------
338 -- The scope table, also referred to as the scope stack, is used to
339 -- record the current scope context. It is organized as a stack, with
340 -- inner nested entries corresponding to higher entries on the stack.
341 -- An entry is made when the parser encounters the opening of a nested
342 -- construct (such as a record, task, package etc.), and then package
343 -- Par.Endh uses this stack to deal with END lines (including properly
344 -- dealing with END nesting errors).
346 type SS_End_Type is
347 -- Type of end entry required for this scope. The last two entries are
348 -- used only in the subprogram body case to mark the case of a suspicious
349 -- IS, or a bad IS (i.e. suspicions confirmed by missing BEGIN or END).
350 -- See separate section on dealing with IS used in place of semicolon.
351 -- Note that for many purposes E_Name, E_Suspicious_Is and E_Bad_Is are
352 -- treated the same (E_Suspicious_Is and E_Bad_Is are simply special cases
353 -- of E_Name). They are placed at the end of the enumeration so that a
354 -- test for >= E_Name catches all three cases efficiently.
356 (E_Dummy, -- dummy entry at outer level
357 E_Case, -- END CASE;
358 E_If, -- END IF;
359 E_Loop, -- END LOOP;
360 E_Record, -- END RECORD;
361 E_Select, -- END SELECT;
362 E_Name, -- END [name];
363 E_Suspicious_Is, -- END [name]; (case of suspicious IS)
364 E_Bad_Is); -- END [name]; (case of bad IS)
366 -- The following describes a single entry in the scope table
368 type Scope_Table_Entry is record
369 Etyp : SS_End_Type;
370 -- Type of end entry, as per above description
372 Lreq : Boolean;
373 -- A flag indicating whether the label, if present, is required to
374 -- appear on the end line. It is referenced only in the case of
375 -- Etyp = E_Name or E_Suspicious_Is where the name may or may not be
376 -- required (yes for labeled block, no in other cases). Note that for
377 -- all cases except begin, the question of whether a label is required
378 -- can be determined from the other fields (for loop, it is required if
379 -- it is present, and for the other constructs it is never required or
380 -- allowed).
382 Ecol : Column_Number;
383 -- Contains the absolute column number (with tabs expanded) of the
384 -- the expected column of the end assuming normal Ada indentation
385 -- usage. If the RM_Column_Check mode is set, this value is used for
386 -- generating error messages about indentation. Otherwise it is used
387 -- only to control heuristic error recovery actions.
389 Labl : Node_Id;
390 -- This field is used only for the LOOP and BEGIN cases, and is the
391 -- Node_Id value of the label name. For all cases except child units,
392 -- this value is an entity whose Chars field contains the name pointer
393 -- that identifies the label uniquely. For the child unit case the Labl
394 -- field references an N_Defining_Program_Unit_Name node for the name.
395 -- For cases other than LOOP or BEGIN, the Label field is set to Error,
396 -- indicating that it is an error to have a label on the end line.
397 -- (this is really a misuse of Error since there is no Error ???)
399 Decl : List_Id;
400 -- Points to the list of declarations (i.e. the declarative part)
401 -- associated with this construct. It is set only in the END [name]
402 -- cases, and is set to No_List for all other cases which do not have a
403 -- declarative unit associated with them. This is used for determining
404 -- the proper location for implicit label declarations.
406 Node : Node_Id;
407 -- Empty except in the case of entries for IF and CASE statements,
408 -- in which case it contains the N_If_Statement or N_Case_Statement
409 -- node. This is used for setting the End_Span field.
411 Sloc : Source_Ptr;
412 -- Source location of the opening token of the construct. This is
413 -- used to refer back to this line in error messages (such as missing
414 -- or incorrect end lines). The Sloc field is not used, and is not set,
415 -- if a label is present (the Labl field provides the text name of the
416 -- label in this case, which is fine for error messages).
418 S_Is : Source_Ptr;
419 -- S_Is is relevant only if Etyp is set to E_Suspicious_Is or
420 -- E_Bad_Is. It records the location of the IS that is considered
421 -- to be suspicious.
423 Junk : Boolean;
424 -- A boolean flag that is set true if the opening entry is the dubious
425 -- result of some prior error, e.g. a record entry where the record
426 -- keyword was missing. It is used to suppress the issuing of a
427 -- corresponding junk complaint about the end line (we do not want
428 -- to complain about a missing end record when there was no record).
429 end record;
431 -- The following declares the scope table itself. The Last field is the
432 -- stack pointer, so that Scope.Table (Scope.Last) is the top entry. The
433 -- oldest entry, at Scope_Stack (0), is a dummy entry with Etyp set to
434 -- E_Dummy, and the other fields undefined. This dummy entry ensures that
435 -- Scope_Stack (Scope_Stack_Ptr).Etyp can always be tested, and that the
436 -- scope stack pointer is always in range.
438 package Scope is new Table.Table (
439 Table_Component_Type => Scope_Table_Entry,
440 Table_Index_Type => Int,
441 Table_Low_Bound => 0,
442 Table_Initial => 50,
443 Table_Increment => 100,
444 Table_Name => "Scope");
446 ---------------------------------
447 -- Parsing Routines by Chapter --
448 ---------------------------------
450 -- Uncommented declarations in this section simply parse the construct
451 -- corresponding to their name, and return an ID value for the Node or
452 -- List that is created.
454 package Ch2 is
455 function P_Identifier return Node_Id;
456 function P_Pragma return Node_Id;
458 function P_Pragmas_Opt return List_Id;
459 -- This function scans for a sequence of pragmas in other than a
460 -- declaration sequence or statement sequence context. All pragmas
461 -- can appear except pragmas Assert and Debug, which are only allowed
462 -- in a declaration or statement sequence context.
464 procedure P_Pragmas_Misplaced;
465 -- Skips misplaced pragmas with a complaint
467 procedure P_Pragmas_Opt (List : List_Id);
468 -- Parses optional pragmas and appends them to the List
469 end Ch2;
471 package Ch3 is
472 Missing_Begin_Msg : Error_Msg_Id;
473 -- This variable is set by a call to P_Declarative_Part. Normally it
474 -- is set to No_Error_Msg, indicating that no special processing is
475 -- required by the caller. The special case arises when a statement
476 -- is found in the sequence of declarations. In this case the Id of
477 -- the message issued ("declaration expected") is preserved in this
478 -- variable, then the caller can change it to an appropriate missing
479 -- begin message if indeed the BEGIN is missing.
481 function P_Access_Definition return Node_Id;
482 function P_Access_Type_Definition return Node_Id;
483 function P_Array_Type_Definition return Node_Id;
484 function P_Basic_Declarative_Items return List_Id;
485 function P_Constraint_Opt return Node_Id;
486 function P_Declarative_Part return List_Id;
487 function P_Defining_Identifier return Node_Id;
488 function P_Discrete_Choice_List return List_Id;
489 function P_Discrete_Range return Node_Id;
490 function P_Discrete_Subtype_Definition return Node_Id;
491 function P_Known_Discriminant_Part_Opt return List_Id;
492 function P_Signed_Integer_Type_Definition return Node_Id;
493 function P_Range return Node_Id;
494 function P_Range_Or_Subtype_Mark return Node_Id;
495 function P_Range_Constraint return Node_Id;
496 function P_Record_Definition return Node_Id;
497 function P_Subtype_Indication return Node_Id;
498 function P_Subtype_Mark return Node_Id;
499 function P_Subtype_Mark_Resync return Node_Id;
500 function P_Unknown_Discriminant_Part_Opt return Boolean;
502 procedure P_Component_Items (Decls : List_Id);
503 -- Scan out one or more component items and append them to the
504 -- given list. Only scans out more than one declaration in the
505 -- case where the source has a single declaration with multiple
506 -- defining identifiers.
508 function Init_Expr_Opt (P : Boolean := False) return Node_Id;
509 -- If an initialization expression is present (:= expression), then
510 -- it is scanned out and returned, otherwise Empty is returned if no
511 -- initialization expression is present. This procedure also handles
512 -- certain common error cases cleanly. The parameter P indicates if
513 -- a right paren can follow the expression (default = no right paren
514 -- allowed).
516 procedure Skip_Declaration (S : List_Id);
517 -- Used when scanning statements to skip past a mispaced declaration
518 -- The declaration is scanned out and appended to the given list.
519 -- Token is known to be a declaration token (in Token_Class_Declk)
520 -- on entry, so there definition is a declaration to be scanned.
522 function P_Subtype_Indication (Subtype_Mark : Node_Id) return Node_Id;
523 -- This version of P_Subtype_Indication is called when the caller has
524 -- already scanned out the subtype mark which is passed as a parameter.
526 function P_Subtype_Mark_Attribute (Type_Node : Node_Id) return Node_Id;
527 -- Parse a subtype mark attribute. The caller has already parsed the
528 -- subtype mark, which is passed in as the argument, and has checked
529 -- that the current token is apostrophe.
531 end Ch3;
533 package Ch4 is
534 function P_Aggregate return Node_Id;
535 function P_Expression return Node_Id;
536 function P_Expression_No_Right_Paren return Node_Id;
537 function P_Expression_Or_Range_Attribute return Node_Id;
538 function P_Function_Name return Node_Id;
539 function P_Name return Node_Id;
540 function P_Qualified_Simple_Name return Node_Id;
541 function P_Qualified_Simple_Name_Resync return Node_Id;
542 function P_Simple_Expression return Node_Id;
543 function P_Simple_Expression_Or_Range_Attribute return Node_Id;
545 function P_Qualified_Expression
546 (Subtype_Mark : Node_Id)
547 return Node_Id;
548 -- This routine scans out a qualified expression when the caller has
549 -- already scanned out the name and apostrophe of the construct.
551 end Ch4;
553 package Ch5 is
555 function P_Statement_Name (Name_Node : Node_Id) return Node_Id;
556 -- Given a node representing a name (which is a call), converts it
557 -- to the syntactically corresponding procedure call statement.
559 function P_Sequence_Of_Statements (SS_Flags : SS_Rec) return List_Id;
560 -- The argument indicates the acceptable termination tokens.
561 -- See body in Par.Ch5 for details of the use of this parameter.
563 procedure Parse_Decls_Begin_End (Parent : Node_Id);
564 -- Parses declarations and handled statement sequence, setting
565 -- fields of Parent node appropriately.
567 end Ch5;
569 package Ch6 is
570 function P_Designator return Node_Id;
571 function P_Defining_Program_Unit_Name return Node_Id;
572 function P_Formal_Part return List_Id;
573 function P_Parameter_Profile return List_Id;
574 function P_Return_Statement return Node_Id;
575 function P_Subprogram_Specification return Node_Id;
577 procedure P_Mode (Node : Node_Id);
578 -- Sets In_Present and/or Out_Present flags in Node scanning past
579 -- IN, OUT or IN OUT tokens in the source.
581 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id;
582 -- Scans out any construct starting with either of the keywords
583 -- PROCEDURE or FUNCTION. The parameter indicates which possible
584 -- possible kinds of construct (body, spec, instantiation etc.)
585 -- are permissible in the current context.
587 end Ch6;
589 package Ch7 is
590 function P_Package (Pf_Flags : Pf_Rec) return Node_Id;
591 -- Scans out any construct starting with the keyword PACKAGE. The
592 -- parameter indicates which possible kinds of construct (body, spec,
593 -- instantiation etc.) are permissible in the current context.
594 end Ch7;
596 package Ch8 is
597 function P_Use_Clause return Node_Id;
598 end Ch8;
600 package Ch9 is
601 function P_Abort_Statement return Node_Id;
602 function P_Abortable_Part return Node_Id;
603 function P_Accept_Statement return Node_Id;
604 function P_Delay_Statement return Node_Id;
605 function P_Entry_Body return Node_Id;
606 function P_Protected return Node_Id;
607 function P_Requeue_Statement return Node_Id;
608 function P_Select_Statement return Node_Id;
609 function P_Task return Node_Id;
610 function P_Terminate_Alternative return Node_Id;
611 end Ch9;
613 package Ch10 is
614 function P_Compilation_Unit return Node_Id;
615 -- Note: this function scans a single compilation unit, and
616 -- checks that an end of file follows this unit, diagnosing
617 -- any unexpected input as an error, and then skipping it, so
618 -- that Token is set to Tok_EOF on return. An exception is in
619 -- syntax-only mode, where multiple compilation units are
620 -- permitted. In this case, P_Compilation_Unit does not check
621 -- for end of file and there may be more compilation units to
622 -- scan. The caller can uniquely detect this situation by the
623 -- fact that Token is not set to Tok_EOF on return.
624 end Ch10;
626 package Ch11 is
627 function P_Handled_Sequence_Of_Statements return Node_Id;
628 function P_Raise_Statement return Node_Id;
630 function Parse_Exception_Handlers return List_Id;
631 -- Parses the partial construct EXCEPTION followed by a list of
632 -- exception handlers which appears in a number of productions,
633 -- and returns the list of exception handlers.
635 end Ch11;
637 package Ch12 is
638 function P_Generic return Node_Id;
639 function P_Generic_Actual_Part_Opt return List_Id;
640 end Ch12;
642 package Ch13 is
643 function P_Representation_Clause return Node_Id;
645 function P_Code_Statement (Subtype_Mark : Node_Id) return Node_Id;
646 -- Function to parse a code statement. The caller has scanned out
647 -- the name to be used as the subtype mark (but has not checked that
648 -- it is suitable for use as a subtype mark, i.e. is either an
649 -- identifier or a selected component). The current token is an
650 -- apostrophe and the following token is either a left paren or
651 -- RANGE (the latter being an error to be caught by P_Code_Statement.
652 end Ch13;
654 -- Note: the parsing for annexe J features (i.e. obsolescent features)
655 -- is found in the logical section where these features would be if
656 -- they were not obsolescent. In particular:
658 -- Delta constraint is parsed by P_Delta_Constraint (3.5.9)
659 -- At clause is parsed by P_At_Clause (13.1)
660 -- Mod clause is parsed by P_Mod_Clause (13.5.1)
662 ------------------
663 -- End Handling --
664 ------------------
666 -- Routines for handling end lines, including scope recovery
668 package Endh is
670 function Check_End return Boolean;
671 -- Called when an end sequence is required. In the absence of an error
672 -- situation, Token contains Tok_End on entry, but in a missing end
673 -- case, this may not be the case. Pop_End_Context is used to determine
674 -- the appropriate action to be taken. The returned result is True if
675 -- an End sequence was encountered and False if no End sequence was
676 -- present. This occurs if the END keyword encountered was determined
677 -- to be improper and deleted (i.e. Pop_End_Context set End_Action to
678 -- Skip_And_Reject). Note that the END sequence includes a semicolon,
679 -- except in the case of END RECORD, where a semicolon follows the END
680 -- RECORD, but is not part of the record type definition itself.
682 procedure End_Skip;
683 -- Skip past an end sequence. On entry Token contains Tok_End, and we
684 -- we know that the end sequence is syntactically incorrect, and that
685 -- an appropriate error message has already been posted. The mission
686 -- is simply to position the scan pointer to be the best guess of the
687 -- position after the end sequence. We do not issue any additional
688 -- error messages while carrying this out.
690 procedure End_Statements (Parent : Node_Id := Empty);
691 -- Called when an end is required or expected to terminate a sequence
692 -- of statements. The caller has already made an appropriate entry in
693 -- the Scope.Table to describe the expected form of the end. This can
694 -- only be used in cases where the only appropriate terminator is end.
695 -- If Parent is non-empty, then if a correct END line is encountered,
696 -- the End_Label field of Parent is set appropriately.
698 end Endh;
700 ------------------------------------
701 -- Resynchronization After Errors --
702 ------------------------------------
704 -- These procedures are used to resynchronize after errors. Following an
705 -- error which is not immediately locally recoverable, the exception
706 -- Error_Resync is raised. The handler for Error_Resync typically calls
707 -- one of these recovery procedures to resynchronize the source position
708 -- to a point from which parsing can be restarted.
710 -- Note: these procedures output an information message that tokens are
711 -- being skipped, but this message is output only if the option for
712 -- Multiple_Errors_Per_Line is set in Options.
714 package Sync is
716 procedure Resync_Choice;
717 -- Used if an error occurs scanning a choice. The scan pointer is
718 -- advanced to the next vertical bar, arrow, or semicolon, whichever
719 -- comes first. We also quit if we encounter an end of file.
721 procedure Resync_Expression;
722 -- Used if an error is detected during the parsing of an expression.
723 -- It skips past tokens until either a token which cannot be part of
724 -- an expression is encountered (an expression terminator), or if a
725 -- comma or right parenthesis or vertical bar is encountered at the
726 -- current parenthesis level (a parenthesis level counter is maintained
727 -- to carry out this test).
729 procedure Resync_Past_Semicolon;
730 -- Used if an error occurs while scanning a sequence of declarations.
731 -- The scan pointer is positioned past the next semicolon and the scan
732 -- resumes. The scan is also resumed on encountering a token which
733 -- starts a declaration (but we make sure to skip at least one token
734 -- in this case, to avoid getting stuck in a loop).
736 procedure Resync_Past_Semicolon_Or_To_Loop_Or_Then;
737 -- Used if an error occurs while scanning a sequence of statements.
738 -- The scan pointer is positioned past the next semicolon, or to the
739 -- next occurrence of either then or loop, and the scan resumes.
741 procedure Resync_To_When;
742 -- Used when an error occurs scanning an entry index specification.
743 -- The scan pointer is positioned to the next WHEN (or to IS or
744 -- semicolon if either of these appear before WHEN, indicating
745 -- another error has occurred).
747 procedure Resync_Semicolon_List;
748 -- Used if an error occurs while scanning a parenthesized list of items
749 -- separated by semicolons. The scan pointer is advanced to the next
750 -- semicolon or right parenthesis at the outer parenthesis level, or
751 -- to the next is or RETURN keyword occurrence, whichever comes first.
753 procedure Resync_Cunit;
754 -- Synchronize to next token which could be the start of a compilation
755 -- unit, or to the end of file token.
757 end Sync;
759 -------------------------
760 -- Token Scan Routines --
761 -------------------------
763 -- Routines to check for expected tokens
765 package Tchk is
767 -- Procedures with names of the form T_xxx, where Tok_xxx is a token
768 -- name, check that the current token matches the required token, and
769 -- if so, scan past it. If not, an error is issued indicating that
770 -- the required token is not present (xxx expected). In most cases, the
771 -- scan pointer is not moved in the not-found case, but there are some
772 -- exceptions to this, see for example T_Id, where the scan pointer is
773 -- moved across a literal appearing where an identifier is expected.
775 procedure T_Abort;
776 procedure T_Arrow;
777 procedure T_At;
778 procedure T_Body;
779 procedure T_Box;
780 procedure T_Colon;
781 procedure T_Colon_Equal;
782 procedure T_Comma;
783 procedure T_Dot_Dot;
784 procedure T_For;
785 procedure T_Greater_Greater;
786 procedure T_Identifier;
787 procedure T_In;
788 procedure T_Is;
789 procedure T_Left_Paren;
790 procedure T_Loop;
791 procedure T_Mod;
792 procedure T_New;
793 procedure T_Of;
794 procedure T_Or;
795 procedure T_Private;
796 procedure T_Range;
797 procedure T_Record;
798 procedure T_Right_Paren;
799 procedure T_Semicolon;
800 procedure T_Then;
801 procedure T_Type;
802 procedure T_Use;
803 procedure T_When;
804 procedure T_With;
806 -- Procedures have names of the form TF_xxx, where Tok_xxx is a token
807 -- name check that the current token matches the required token, and
808 -- if so, scan past it. If not, an error message is issued indicating
809 -- that the required token is not present (xxx expected).
811 -- If the missing token is at the end of the line, then control returns
812 -- immediately after posting the message. If there are remaining tokens
813 -- on the current line, a search is conducted to see if the token
814 -- appears later on the current line, as follows:
816 -- A call to Scan_Save is issued and a forward search for the token
817 -- is carried out. If the token is found on the current line before a
818 -- semicolon, then it is scanned out and the scan continues from that
819 -- point. If not the scan is restored to the point where it was missing.
821 procedure TF_Arrow;
822 procedure TF_Is;
823 procedure TF_Loop;
824 procedure TF_Return;
825 procedure TF_Semicolon;
826 procedure TF_Then;
827 procedure TF_Use;
829 end Tchk;
831 ----------------------
832 -- Utility Routines --
833 ----------------------
835 package Util is
837 function Bad_Spelling_Of (T : Token_Type) return Boolean;
838 -- This function is called in an error situation. It checks if the
839 -- current token is an identifier whose name is a plausible bad
840 -- spelling of the given keyword token, and if so, issues an error
841 -- message, sets Token from T, and returns True. Otherwise Token is
842 -- unchanged, and False is returned.
844 procedure Check_Bad_Layout;
845 -- Check for bad indentation in RM checking mode. Used for statements
846 -- and declarations. Checks if current token is at start of line and
847 -- is exdented from the current expected end column, and if so an
848 -- error message is generated.
850 procedure Check_Misspelling_Of (T : Token_Type);
851 pragma Inline (Check_Misspelling_Of);
852 -- This is similar to the function above, except that it does not
853 -- return a result. It is typically used in a situation where any
854 -- identifier is an error, and it makes sense to simply convert it
855 -- to the given token if it is a plausible misspelling of it.
857 procedure Check_95_Keyword (Token_95, Next : Token_Type);
858 -- This routine checks if the token after the current one matches the
859 -- Next argument. If so, the scan is backed up to the current token
860 -- and Token_Type is changed to Token_95 after issuing an appropriate
861 -- error message ("(Ada 83) keyword xx cannot be used"). If not,
862 -- the scan is backed up with Token_Type unchanged. This routine
863 -- is used to deal with an attempt to use a 95 keyword in Ada 83
864 -- mode. The caller has typically checked that the current token,
865 -- an identifier, matches one of the 95 keywords.
867 procedure Check_Simple_Expression (E : Node_Id);
868 -- Given an expression E, that has just been scanned, so that Expr_Form
869 -- is still set, outputs an error if E is a non-simple expression. E is
870 -- not modified by this call.
872 procedure Check_Simple_Expression_In_Ada_83 (E : Node_Id);
873 -- Like Check_Simple_Expression, except that the error message is only
874 -- given when operating in Ada 83 mode, and includes "in Ada 83".
876 function Check_Subtype_Mark (Mark : Node_Id) return Node_Id;
877 -- Called to check that a node representing a name (or call) is
878 -- suitable for a subtype mark, i.e, that it is an identifier or
879 -- a selected component. If so, or if it is already Error, then
880 -- it is returned unchanged. Otherwise an error message is issued
881 -- and Error is returned.
883 function Comma_Present return Boolean;
884 -- Used in comma delimited lists to determine if a comma is present, or
885 -- can reasonably be assumed to have been present (an error message is
886 -- generated in the latter case). If True is returned, the scan has been
887 -- positioned past the comma. If False is returned, the scan position
888 -- is unchanged. Note that all comma-delimited lists are terminated by
889 -- a right paren, so the only legitimate tokens when Comma_Present is
890 -- called are right paren and comma. If some other token is found, then
891 -- Comma_Present has the job of deciding whether it is better to pretend
892 -- a comma was present, post a message for a missing comma and return
893 -- True, or return False and let the caller diagnose the missing right
894 -- parenthesis.
896 procedure Discard_Junk_Node (N : Node_Id);
897 procedure Discard_Junk_List (L : List_Id);
898 pragma Inline (Discard_Junk_Node);
899 pragma Inline (Discard_Junk_List);
900 -- These procedures do nothing at all, their effect is simply to discard
901 -- the argument. A typical use is to skip by some junk that is not
902 -- expected in the current context.
904 procedure Ignore (T : Token_Type);
905 -- If current token matches T, then give an error message and skip
906 -- past it, otherwise the call has no effect at all. T may be any
907 -- reserved word token, or comma, left or right paren, or semicolon.
909 function Is_Reserved_Identifier return Boolean;
910 -- Test if current token is a reserved identifier. This test is based
911 -- on the token being a keyword and being spelled in typical identifier
912 -- style (i.e. starting with an upper case letter).
914 procedure Merge_Identifier (Prev : Node_Id; Nxt : Token_Type);
915 -- Called when the previous token is an identifier (whose Token_Node
916 -- value is given by Prev) to check if current token is an identifier
917 -- that can be merged with the previous one adding an underscore. The
918 -- merge is only attempted if the following token matches Nxt. If all
919 -- conditions are met, an error message is issued, and the merge is
920 -- carried out, modifying the Chars field of Prev.
922 procedure No_Constraint;
923 -- Called in a place where no constraint is allowed, but one might
924 -- appear due to a common error (e.g. after the type mark in a procedure
925 -- parameter. If a constraint is present, an error message is posted,
926 -- and the constraint is scanned and discarded.
928 function No_Right_Paren (Expr : Node_Id) return Node_Id;
929 -- Function to check for no right paren at end of expression, returns
930 -- its argument if no right paren, else flags paren and returns Error.
932 procedure Push_Scope_Stack;
933 pragma Inline (Push_Scope_Stack);
934 -- Push a new entry onto the scope stack. Scope.Last (the stack pointer)
935 -- is incremented. The Junk field is preinitialized to False. The caller
936 -- is expected to fill in all remaining entries of the new new top stack
937 -- entry at Scope.Table (Scope.Last).
939 procedure Pop_Scope_Stack;
940 -- Pop an entry off the top of the scope stack. Scope_Last (the scope
941 -- table stack pointer) is decremented by one. It is a fatal error to
942 -- try to pop off the dummy entry at the bottom of the stack (i.e.
943 -- Scope.Last must be non-zero at the time of call).
945 function Separate_Present return Boolean;
946 -- Determines if the current token is either Tok_Separate, or an
947 -- identifier that is a possible misspelling of "separate" followed
948 -- by a semicolon. True is returned if so, otherwise False.
950 procedure Signal_Bad_Attribute;
951 -- The current token is an identifier that is supposed to be an
952 -- attribute identifier but is not. This routine posts appropriate
953 -- error messages, including a check for a near misspelling.
955 function Token_Is_At_Start_Of_Line return Boolean;
956 pragma Inline (Token_Is_At_Start_Of_Line);
957 -- Determines if the current token is the first token on the line
959 end Util;
961 ---------------------------------------
962 -- Specialized Syntax Check Routines --
963 ---------------------------------------
965 function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id;
966 -- This function is passed a tree for a pragma that has been scanned out.
967 -- The pragma is syntactically well formed according to the general syntax
968 -- for pragmas and the pragma identifier is for one of the recognized
969 -- pragmas. It performs specific syntactic checks for specific pragmas.
970 -- The result is the input node if it is OK, or Error otherwise. The
971 -- reason that this is separated out is to facilitate the addition
972 -- of implementation defined pragmas. The second parameter records the
973 -- location of the semicolon following the pragma (this is needed for
974 -- correct processing of the List and Page pragmas). The returned value
975 -- is a copy of Pragma_Node, or Error if an error is found. Note that
976 -- at the point where Prag is called, the right paren ending the pragma
977 -- has been scanned out, and except in the case of pragma Style_Checks,
978 -- so has the following semicolon. For Style_Checks, the caller delays
979 -- the scanning of the semicolon so that it will be scanned using the
980 -- settings from the Style_Checks pragma preceding it.
982 -------------------------
983 -- Subsidiary Routines --
984 -------------------------
986 procedure Labl;
987 -- This procedure creates implicit label declarations for all label that
988 -- are declared in the current unit. Note that this could conceptually
989 -- be done at the point where the labels are declared, but it is tricky
990 -- to do it then, since the tree is not hooked up at the point where the
991 -- label is declared (e.g. a sequence of statements is not yet attached
992 -- to its containing scope at the point a label in the sequence is found)
994 procedure Load;
995 -- This procedure loads all subsidiary units that are required by this
996 -- unit, including with'ed units, specs for bodies, and parents for child
997 -- units. It does not load bodies for inlined procedures and generics,
998 -- since we don't know till semantic analysis is complete what is needed.
1000 -----------
1001 -- Stubs --
1002 -----------
1004 -- The package bodies can see all routines defined in all other subpackages
1006 use Ch2;
1007 use Ch3;
1008 use Ch4;
1009 use Ch5;
1010 use Ch6;
1011 use Ch7;
1012 use Ch8;
1013 use Ch9;
1014 use Ch10;
1015 use Ch11;
1016 use Ch12;
1017 use Ch13;
1019 use Endh;
1020 use Tchk;
1021 use Sync;
1022 use Util;
1024 package body Ch2 is separate;
1025 package body Ch3 is separate;
1026 package body Ch4 is separate;
1027 package body Ch5 is separate;
1028 package body Ch6 is separate;
1029 package body Ch7 is separate;
1030 package body Ch8 is separate;
1031 package body Ch9 is separate;
1032 package body Ch10 is separate;
1033 package body Ch11 is separate;
1034 package body Ch12 is separate;
1035 package body Ch13 is separate;
1037 package body Endh is separate;
1038 package body Tchk is separate;
1039 package body Sync is separate;
1040 package body Util is separate;
1042 function Prag (Pragma_Node : Node_Id; Semi : Source_Ptr) return Node_Id
1043 is separate;
1045 procedure Labl is separate;
1046 procedure Load is separate;
1048 ---------
1049 -- Par --
1050 ---------
1052 -- This function is the parse routine called at the outer level. It parses
1053 -- the current compilation unit and adds implicit label declarations.
1055 begin
1056 -- Deal with configuration pragmas case first
1058 if Configuration_Pragmas then
1059 declare
1060 Ecount : constant Int := Total_Errors_Detected;
1061 Pragmas : List_Id := Empty_List;
1062 P_Node : Node_Id;
1064 begin
1065 loop
1066 if Token = Tok_EOF then
1067 return Pragmas;
1069 elsif Token /= Tok_Pragma then
1070 Error_Msg_SC ("only pragmas allowed in configuration file");
1071 return Error_List;
1073 else
1074 P_Node := P_Pragma;
1076 if Total_Errors_Detected > Ecount then
1077 return Error_List;
1078 end if;
1080 if Chars (P_Node) > Last_Configuration_Pragma_Name
1081 and then Chars (P_Node) /= Name_Source_Reference
1082 then
1083 Error_Msg_SC
1084 ("only configuration pragmas allowed " &
1085 "in configuration file");
1086 return Error_List;
1087 end if;
1089 Append (P_Node, Pragmas);
1090 end if;
1091 end loop;
1092 end;
1094 -- Normal case of compilation unit
1096 else
1097 Save_Opt_Config_Switches (Save_Config_Switches);
1099 -- Special processing for language defined units. For this purpose
1100 -- we do NOT consider the renamings in annex J as predefined. That
1101 -- allows users to compile their own versions of these files, and
1102 -- in particular, in the VMS implementation, the DEC versions can
1103 -- be substituted for the standard Ada 95 versions.
1105 if Is_Predefined_File_Name
1106 (Fname => File_Name (Current_Source_File),
1107 Renamings_Included => False)
1108 then
1109 Set_Opt_Config_Switches
1110 (Is_Internal_File_Name (File_Name (Current_Source_File)));
1112 -- If this is the main unit, disallow compilation unless the -gnatg
1113 -- (GNAT mode) switch is set (from a user point of view, the rule is
1114 -- that language defined units cannot be recompiled).
1116 -- However, an exception is s-rpc, and its children. We test this
1117 -- by looking at the character after the minus, the rule is that
1118 -- System.RPC and its children are the only children in System
1119 -- whose second level name can start with the letter r.
1121 Get_Name_String (File_Name (Current_Source_File));
1123 if (Name_Len < 3 or else Name_Buffer (1 .. 3) /= "s-r")
1124 and then Current_Source_Unit = Main_Unit
1125 and then not GNAT_Mode
1126 and then Operating_Mode = Generate_Code
1127 then
1128 Error_Msg_SC ("language defined units may not be recompiled");
1129 end if;
1130 end if;
1132 -- The following loop runs more than once only in syntax check mode
1133 -- where we allow multiple compilation units in the same file.
1135 loop
1136 Set_Opt_Config_Switches
1137 (Is_Internal_File_Name (File_Name (Current_Source_File)));
1139 -- Initialize scope table and other parser control variables
1141 Compiler_State := Parsing;
1142 Scope.Init;
1143 Scope.Increment_Last;
1144 Scope.Table (0).Etyp := E_Dummy;
1145 SIS_Entry_Active := False;
1146 Last_Resync_Point := No_Location;
1148 Label_List := New_Elmt_List;
1149 Unit_Node := P_Compilation_Unit;
1151 -- If we are not at an end of file, then this means that we are
1152 -- in syntax scan mode, and we can have another compilation unit,
1153 -- otherwise we will exit from the loop.
1155 exit when Token = Tok_EOF;
1156 Restore_Opt_Config_Switches (Save_Config_Switches);
1157 Set_Comes_From_Source_Default (False);
1158 end loop;
1160 -- Now that we have completely parsed the source file, we can
1161 -- complete the source file table entry.
1163 Complete_Source_File_Entry;
1165 -- An internal error check, the scope stack should now be empty
1167 pragma Assert (Scope.Last = 0);
1169 -- Remaining steps are to create implicit label declarations and to
1170 -- load required subsidiary sources. These steps are required only
1171 -- if we are doing semantic checking.
1173 if Operating_Mode /= Check_Syntax or else Debug_Flag_F then
1174 Par.Labl;
1175 Par.Load;
1176 end if;
1178 -- Restore settings of switches saved on entry
1180 Restore_Opt_Config_Switches (Save_Config_Switches);
1181 Set_Comes_From_Source_Default (False);
1182 return Empty_List;
1183 end if;
1185 end Par;