OSDN Git Service

2009-04-08 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch10.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             P A R . C H 1 0                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, 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 pragma Style_Checks (All_Checks);
27 --  Turn off subprogram body ordering check. Subprograms are in order
28 --  by RM section rather than alphabetical
29
30 with Fname.UF; use Fname.UF;
31 with Uname;    use Uname;
32
33 separate (Par)
34 package body Ch10 is
35
36    --  Local functions, used only in this chapter
37
38    function P_Context_Clause    return List_Id;
39    function P_Subunit           return Node_Id;
40
41    function Set_Location return Source_Ptr;
42    --  The current compilation unit starts with Token at Token_Ptr. This
43    --  function determines the corresponding source location for the start
44    --  of the unit, including any preceding comment lines.
45
46    procedure Unit_Display
47      (Cunit      : Node_Id;
48       Loc        : Source_Ptr;
49       SR_Present : Boolean);
50    --  This procedure is used to generate a line of output for a unit in
51    --  the source program. Cunit is the node for the compilation unit, and
52    --  Loc is the source location for the start of the unit in the source
53    --  file (which is not necessarily the Sloc of the Cunit node). This
54    --  output is written to the standard output file for use by gnatchop.
55
56    procedure Unit_Location (Sind : Source_File_Index; Loc : Source_Ptr);
57    --  This routine has the same calling sequence as Unit_Display, but
58    --  it outputs only the line number and offset of the location, Loc,
59    --  using Cunit to obtain the proper source file index.
60
61    -------------------------
62    -- 10.1.1  Compilation --
63    -------------------------
64
65    --  COMPILATION ::= {COMPILATION_UNIT}
66
67    --  There is no specific parsing routine for a compilation, since we only
68    --  permit a single compilation in a source file, so there is no explicit
69    --  occurrence of compilations as such (our representation of a compilation
70    --  is a series of separate source files).
71
72    ------------------------------
73    -- 10.1.1  Compilation unit --
74    ------------------------------
75
76    --  COMPILATION_UNIT ::=
77    --    CONTEXT_CLAUSE LIBRARY_ITEM
78    --  | CONTEXT_CLAUSE SUBUNIT
79
80    --  LIBRARY_ITEM ::=
81    --    private LIBRARY_UNIT_DECLARATION
82    --  | LIBRARY_UNIT_BODY
83    --  | [private] LIBRARY_UNIT_RENAMING_DECLARATION
84
85    --  LIBRARY_UNIT_DECLARATION ::=
86    --    SUBPROGRAM_DECLARATION | PACKAGE_DECLARATION
87    --  | GENERIC_DECLARATION    | GENERIC_INSTANTIATION
88
89    --  LIBRARY_UNIT_RENAMING_DECLARATION ::=
90    --    PACKAGE_RENAMING_DECLARATION
91    --  | GENERIC_RENAMING_DECLARATION
92    --  | SUBPROGRAM_RENAMING_DECLARATION
93
94    --  LIBRARY_UNIT_BODY ::= SUBPROGRAM_BODY | PACKAGE_BODY
95
96    --  Error recovery: cannot raise Error_Resync. If an error occurs, tokens
97    --  are skipped up to the next possible beginning of a compilation unit.
98
99    --  Note: if only configuration pragmas are found, Empty is returned
100
101    --  Note: in syntax-only mode, it is possible for P_Compilation_Unit
102    --  to return strange things that are not really compilation units.
103    --  This is done to help out gnatchop when it is faced with nonsense.
104
105    function P_Compilation_Unit return Node_Id is
106       Scan_State         : Saved_Scan_State;
107       Body_Node          : Node_Id;
108       Specification_Node : Node_Id;
109       Unit_Node          : Node_Id;
110       Comp_Unit_Node     : Node_Id;
111       Name_Node          : Node_Id;
112       Item               : Node_Id;
113       Private_Sloc       : Source_Ptr := No_Location;
114       Config_Pragmas     : List_Id;
115       P                  : Node_Id;
116       SR_Present         : Boolean;
117
118       Cunit_Error_Flag : Boolean := False;
119       --  This flag is set True if we have to scan for a compilation unit
120       --  token. It is used to ensure clean termination in such cases by
121       --  not insisting on being at the end of file, and, in the syntax only
122       --  case by not scanning for additional compilation units.
123
124       Cunit_Location : Source_Ptr;
125       --  Location of unit for unit identification output (List_Unit option)
126
127    begin
128       Num_Library_Units := Num_Library_Units + 1;
129
130       --  Set location of the compilation unit if unit list option set
131       --  and we are in syntax check only mode
132
133       if List_Units and then Operating_Mode = Check_Syntax then
134          Cunit_Location := Set_Location;
135       else
136          Cunit_Location := No_Location;
137       end if;
138
139       --  Deal with initial pragmas
140
141       Config_Pragmas := No_List;
142
143       --  If we have an initial Source_Reference pragma, then remember the fact
144       --  to generate an NR parameter in the output line.
145
146       SR_Present := False;
147
148       if Token = Tok_Pragma then
149          Save_Scan_State (Scan_State);
150          Item := P_Pragma;
151
152          if Item = Error
153            or else Pragma_Name (Item) /= Name_Source_Reference
154          then
155             Restore_Scan_State (Scan_State);
156
157          else
158             SR_Present := True;
159
160             --  If first unit, record the file name for gnatchop use
161
162             if Operating_Mode = Check_Syntax
163               and then List_Units
164               and then Num_Library_Units = 1
165             then
166                Write_Str ("Source_Reference pragma for file """);
167                Write_Name (Full_Ref_Name (Current_Source_File));
168                Write_Char ('"');
169                Write_Eol;
170             end if;
171
172             Config_Pragmas := New_List (Item);
173          end if;
174       end if;
175
176       --  Scan out any configuration pragmas
177
178       while Token = Tok_Pragma loop
179          Save_Scan_State (Scan_State);
180          Item := P_Pragma;
181
182          if Item = Error
183            or else not Is_Configuration_Pragma_Name (Pragma_Name (Item))
184          then
185             Restore_Scan_State (Scan_State);
186             exit;
187          end if;
188
189          if Config_Pragmas = No_List then
190             Config_Pragmas := Empty_List;
191
192             if Operating_Mode = Check_Syntax and then List_Units then
193                Write_Str ("Configuration pragmas at");
194                Unit_Location (Current_Source_File, Cunit_Location);
195                Write_Eol;
196             end if;
197          end if;
198
199          Append (Item, Config_Pragmas);
200          Cunit_Location := Set_Location;
201       end loop;
202
203       --  Establish compilation unit node and scan context items
204
205       Comp_Unit_Node := New_Node (N_Compilation_Unit, No_Location);
206       Set_Cunit (Current_Source_Unit, Comp_Unit_Node);
207       Set_Context_Items (Comp_Unit_Node, P_Context_Clause);
208       Set_Aux_Decls_Node
209         (Comp_Unit_Node, New_Node (N_Compilation_Unit_Aux, No_Location));
210
211       if Present (Config_Pragmas) then
212
213          --  Check for case of only configuration pragmas present
214
215          if Token = Tok_EOF
216            and then Is_Empty_List (Context_Items (Comp_Unit_Node))
217          then
218             if Operating_Mode = Check_Syntax then
219                return Empty;
220
221             else
222                Item := First (Config_Pragmas);
223                Error_Msg_N
224                  ("cannot compile configuration pragmas with gcc!", Item);
225                Error_Msg_N
226                  ("\use gnatchop -c to process configuration pragmas!", Item);
227                raise Unrecoverable_Error;
228             end if;
229
230          --  Otherwise configuration pragmas are simply prepended to the
231          --  context of the current unit.
232
233          else
234             Append_List (Context_Items (Comp_Unit_Node), Config_Pragmas);
235             Set_Context_Items (Comp_Unit_Node, Config_Pragmas);
236          end if;
237       end if;
238
239       --  Check for PRIVATE. Note that for the moment we allow this in
240       --  Ada_83 mode, since we do not yet know if we are compiling a
241       --  predefined unit, and if we are then it would be allowed anyway.
242
243       if Token = Tok_Private then
244          Private_Sloc := Token_Ptr;
245          Set_Keyword_Casing (Current_Source_File, Determine_Token_Casing);
246          if Style_Check then
247             Style.Check_Indentation;
248          end if;
249
250          Save_Scan_State (Scan_State); -- at PRIVATE
251          Scan; -- past PRIVATE
252
253          if Token = Tok_Separate then
254             Error_Msg_SP ("cannot have private subunits!");
255
256          elsif Token = Tok_Package then
257             Scan; -- past PACKAGE
258
259             if Token = Tok_Body then
260                Restore_Scan_State (Scan_State); -- to PRIVATE
261                Error_Msg_SC ("cannot have private package body!");
262                Scan; -- ignore PRIVATE
263
264             else
265                Restore_Scan_State (Scan_State); -- to PRIVATE
266                Scan; -- past PRIVATE
267                Set_Private_Present (Comp_Unit_Node, True);
268             end if;
269
270          elsif Token = Tok_Procedure
271            or else Token = Tok_Function
272            or else Token = Tok_Generic
273          then
274             Set_Private_Present (Comp_Unit_Node, True);
275          end if;
276       end if;
277
278       --  Loop to find our way to a compilation unit token
279
280       loop
281          exit when Token in Token_Class_Cunit and then Token /= Tok_With;
282
283          exit when Bad_Spelling_Of (Tok_Package)
284            or else Bad_Spelling_Of (Tok_Function)
285            or else Bad_Spelling_Of (Tok_Generic)
286            or else Bad_Spelling_Of (Tok_Separate)
287            or else Bad_Spelling_Of (Tok_Procedure);
288
289          --  Allow task and protected for nice error recovery purposes
290
291          exit when Token = Tok_Task
292            or else Token = Tok_Protected;
293
294          if Token = Tok_With then
295             Error_Msg_SC ("misplaced WITH");
296             Append_List (P_Context_Clause, Context_Items (Comp_Unit_Node));
297
298          elsif Bad_Spelling_Of (Tok_With) then
299             Append_List (P_Context_Clause, Context_Items (Comp_Unit_Node));
300
301          else
302             if Operating_Mode = Check_Syntax and then Token = Tok_EOF then
303                Error_Msg_SC ("?file contains no compilation units");
304             else
305                Error_Msg_SC ("compilation unit expected");
306                Cunit_Error_Flag := True;
307                Resync_Cunit;
308             end if;
309
310             --  If we are at an end of file, then just quit, the above error
311             --  message was complaint enough.
312
313             if Token = Tok_EOF then
314                return Error;
315             end if;
316          end if;
317       end loop;
318
319       --  We have a compilation unit token, so that's a reasonable choice for
320       --  determining the standard casing convention used for keywords in case
321       --  it hasn't already been done on seeing a WITH or PRIVATE.
322
323       Set_Keyword_Casing (Current_Source_File, Determine_Token_Casing);
324       if Style_Check then
325          Style.Check_Indentation;
326       end if;
327
328       --  Remaining processing depends on particular type of compilation unit
329
330       if Token = Tok_Package then
331
332          --  A common error is to omit the body keyword after package. We can
333          --  often diagnose this early on (before getting loads of errors from
334          --  contained subprogram bodies), by knowing that the file we
335          --  are compiling has a name that requires a body to be found.
336
337          Save_Scan_State (Scan_State);
338          Scan; -- past Package keyword
339
340          if Token /= Tok_Body
341            and then
342              Get_Expected_Unit_Type
343                (File_Name (Current_Source_File)) = Expect_Body
344          then
345             Error_Msg_BC ("keyword BODY expected here [see file name]");
346             Restore_Scan_State (Scan_State);
347             Set_Unit (Comp_Unit_Node, P_Package (Pf_Pbod));
348          else
349             Restore_Scan_State (Scan_State);
350             Set_Unit (Comp_Unit_Node, P_Package (Pf_Decl_Gins_Pbod_Rnam));
351          end if;
352
353       elsif Token = Tok_Generic then
354          Set_Unit (Comp_Unit_Node, P_Generic);
355
356       elsif Token = Tok_Separate then
357          Set_Unit (Comp_Unit_Node, P_Subunit);
358
359       elsif Token = Tok_Function
360         or else Token = Tok_Not
361         or else Token = Tok_Overriding
362         or else Token = Tok_Procedure
363       then
364          Set_Unit (Comp_Unit_Node, P_Subprogram (Pf_Decl_Gins_Pbod_Rnam));
365
366          --  A little bit of an error recovery check here. If we just scanned
367          --  a subprogram declaration (as indicated by an SIS entry being
368          --  active), then if the following token is BEGIN or an identifier,
369          --  or a token which can reasonably start a declaration but cannot
370          --  start a compilation unit, then we assume that the semicolon in
371          --  the declaration should have been IS.
372
373          if SIS_Entry_Active then
374
375             if Token = Tok_Begin
376                or else Token = Tok_Identifier
377                or else Token in Token_Class_Deckn
378             then
379                Push_Scope_Stack;
380                Scope.Table (Scope.Last).Etyp := E_Name;
381                Scope.Table (Scope.Last).Sloc := SIS_Sloc;
382                Scope.Table (Scope.Last).Ecol := SIS_Ecol;
383                Scope.Table (Scope.Last).Lreq := False;
384                SIS_Entry_Active := False;
385
386                --  If we had a missing semicolon in the declaration, then
387                --  change the message to from <missing ";"> to <missing "is">
388
389                if SIS_Missing_Semicolon_Message /= No_Error_Msg then
390                   Change_Error_Text     -- Replace: "missing "";"" "
391                     (SIS_Missing_Semicolon_Message, "missing IS");
392
393                --  Otherwise we saved the semicolon position, so complain
394
395                else
396                   Error_Msg (""";"" should be IS", SIS_Semicolon_Sloc);
397                end if;
398
399                Body_Node := Unit (Comp_Unit_Node);
400                Specification_Node := Specification (Body_Node);
401                Change_Node (Body_Node, N_Subprogram_Body);
402                Set_Specification (Body_Node, Specification_Node);
403                Parse_Decls_Begin_End (Body_Node);
404                Set_Unit (Comp_Unit_Node, Body_Node);
405             end if;
406
407          --  If we scanned a subprogram body, make sure we did not have private
408
409          elsif Private_Sloc /= No_Location
410            and then
411              Nkind (Unit (Comp_Unit_Node)) not in N_Subprogram_Instantiation
412            and then
413              Nkind (Unit (Comp_Unit_Node)) /= N_Subprogram_Renaming_Declaration
414          then
415             Error_Msg ("cannot have private subprogram body", Private_Sloc);
416
417          --  P_Subprogram can yield an abstract subprogram, but this cannot
418          --  be a compilation unit. Treat as a subprogram declaration.
419
420          elsif
421            Nkind (Unit (Comp_Unit_Node)) = N_Abstract_Subprogram_Declaration
422          then
423             Error_Msg_N
424               ("compilation unit cannot be abstract subprogram",
425                  Unit (Comp_Unit_Node));
426
427             Unit_Node :=
428               New_Node (N_Subprogram_Declaration, Sloc (Comp_Unit_Node));
429             Set_Specification (Unit_Node,
430               Specification (Unit (Comp_Unit_Node)));
431             Set_Unit (Comp_Unit_Node, Unit_Node);
432          end if;
433
434       --  Otherwise we have TASK. This is not really an acceptable token,
435       --  but we accept it to improve error recovery.
436
437       elsif Token = Tok_Task then
438          Scan; -- Past TASK
439
440          if Token = Tok_Type then
441             Error_Msg_SP
442               ("task type cannot be used as compilation unit");
443          else
444             Error_Msg_SP
445               ("task declaration cannot be used as compilation unit");
446          end if;
447
448          --  If in check syntax mode, accept the task anyway. This is done
449          --  particularly to improve the behavior of GNATCHOP in this case.
450
451          if Operating_Mode = Check_Syntax then
452             Set_Unit (Comp_Unit_Node, P_Task);
453
454          --  If not in syntax only mode, treat this as horrible error
455
456          else
457             Cunit_Error_Flag := True;
458             return Error;
459          end if;
460
461       else pragma Assert (Token = Tok_Protected);
462          Scan; -- Past PROTECTED
463
464          if Token = Tok_Type then
465             Error_Msg_SP
466               ("protected type cannot be used as compilation unit");
467          else
468             Error_Msg_SP
469               ("protected declaration cannot be used as compilation unit");
470          end if;
471
472          --  If in check syntax mode, accept protected anyway. This is done
473          --  particularly to improve the behavior of GNATCHOP in this case.
474
475          if Operating_Mode = Check_Syntax then
476             Set_Unit (Comp_Unit_Node, P_Protected);
477
478          --  If not in syntax only mode, treat this as horrible error
479
480          else
481             Cunit_Error_Flag := True;
482             return Error;
483          end if;
484       end if;
485
486       --  Here is where locate the compilation unit entity. This is a little
487       --  tricky, since it is buried in various places.
488
489       Unit_Node := Unit (Comp_Unit_Node);
490
491       --  Another error from which it is hard to recover
492
493       if Nkind (Unit_Node) = N_Subprogram_Body_Stub
494         or else Nkind (Unit_Node) = N_Package_Body_Stub
495       then
496          Cunit_Error_Flag := True;
497          return Error;
498       end if;
499
500       --  Only try this if we got an OK unit!
501
502       if Unit_Node /= Error then
503          if Nkind (Unit_Node) = N_Subunit then
504             Unit_Node := Proper_Body (Unit_Node);
505          end if;
506
507          if Nkind (Unit_Node) in N_Generic_Declaration then
508             Unit_Node := Specification (Unit_Node);
509          end if;
510
511          if Nkind (Unit_Node) = N_Package_Declaration
512            or else Nkind (Unit_Node) = N_Subprogram_Declaration
513            or else Nkind (Unit_Node) = N_Subprogram_Body
514            or else Nkind (Unit_Node) = N_Subprogram_Renaming_Declaration
515          then
516             Unit_Node := Specification (Unit_Node);
517
518          elsif Nkind (Unit_Node) = N_Subprogram_Renaming_Declaration then
519             if Ada_Version = Ada_83 then
520                Error_Msg_N
521                  ("(Ada 83) library unit renaming not allowed", Unit_Node);
522             end if;
523          end if;
524
525          if Nkind (Unit_Node) = N_Task_Body
526            or else Nkind (Unit_Node) = N_Protected_Body
527            or else Nkind (Unit_Node) = N_Task_Type_Declaration
528            or else Nkind (Unit_Node) = N_Protected_Type_Declaration
529            or else Nkind (Unit_Node) = N_Single_Task_Declaration
530            or else Nkind (Unit_Node) = N_Single_Protected_Declaration
531          then
532             Name_Node := Defining_Identifier (Unit_Node);
533
534          elsif Nkind (Unit_Node) = N_Function_Instantiation
535            or else Nkind (Unit_Node) = N_Function_Specification
536            or else Nkind (Unit_Node) = N_Generic_Function_Renaming_Declaration
537            or else Nkind (Unit_Node) = N_Generic_Package_Renaming_Declaration
538            or else Nkind (Unit_Node) = N_Generic_Procedure_Renaming_Declaration
539            or else Nkind (Unit_Node) = N_Package_Body
540            or else Nkind (Unit_Node) = N_Package_Instantiation
541            or else Nkind (Unit_Node) = N_Package_Renaming_Declaration
542            or else Nkind (Unit_Node) = N_Package_Specification
543            or else Nkind (Unit_Node) = N_Procedure_Instantiation
544            or else Nkind (Unit_Node) = N_Procedure_Specification
545          then
546             Name_Node := Defining_Unit_Name (Unit_Node);
547
548          --  Anything else is a serious error, abandon scan
549
550          else
551             raise Error_Resync;
552          end if;
553
554          Set_Sloc (Comp_Unit_Node, Sloc (Name_Node));
555          Set_Sloc (Aux_Decls_Node (Comp_Unit_Node), Sloc (Name_Node));
556
557          --  Set Entity field in file table. Easier now that we have name!
558          --  Note that this is also skipped if we had a bad unit
559
560          if Nkind (Name_Node) = N_Defining_Program_Unit_Name then
561             Set_Cunit_Entity
562               (Current_Source_Unit, Defining_Identifier (Name_Node));
563          else
564             Set_Cunit_Entity (Current_Source_Unit, Name_Node);
565          end if;
566
567          Set_Unit_Name
568            (Current_Source_Unit, Get_Unit_Name (Unit (Comp_Unit_Node)));
569
570       --  If we had a bad unit, make sure the fatal flag is set in the file
571       --  table entry, since this is surely a fatal error and also set our
572       --  flag to inhibit the requirement that we be at end of file.
573
574       else
575          Cunit_Error_Flag := True;
576          Set_Fatal_Error (Current_Source_Unit);
577       end if;
578
579       --  Clear away any missing semicolon indication, we are done with that
580       --  unit, so what's done is done, and we don't want anything hanging
581       --  around from the attempt to parse it!
582
583       SIS_Entry_Active := False;
584
585       --  Scan out pragmas after unit
586
587       while Token = Tok_Pragma loop
588          Save_Scan_State (Scan_State);
589
590          --  If we are in syntax scan mode allowing multiple units, then start
591          --  the next unit if we encounter a configuration pragma, or a source
592          --  reference pragma. We take care not to actually scan the pragma in
593          --  this case (we don't want it to take effect for the current unit).
594
595          if Operating_Mode = Check_Syntax then
596             Scan;  -- past Pragma
597
598             if Token = Tok_Identifier
599               and then
600                 (Is_Configuration_Pragma_Name (Token_Name)
601                    or else Token_Name = Name_Source_Reference)
602             then
603                Restore_Scan_State (Scan_State); -- to Pragma
604                exit;
605             end if;
606          end if;
607
608          --  Otherwise eat the pragma, it definitely belongs with the
609          --  current unit, and not with the following unit.
610
611          Restore_Scan_State (Scan_State); -- to Pragma
612          P := P_Pragma;
613
614          if No (Pragmas_After (Aux_Decls_Node (Comp_Unit_Node))) then
615             Set_Pragmas_After
616               (Aux_Decls_Node (Comp_Unit_Node), New_List);
617          end if;
618
619          Append (P, Pragmas_After (Aux_Decls_Node (Comp_Unit_Node)));
620       end loop;
621
622       --  Cancel effect of any outstanding pragma Warnings (Off)
623
624       Set_Warnings_Mode_On (Scan_Ptr);
625
626       --  Ada 83 error checks
627
628       if Ada_Version = Ada_83 then
629
630          --  Check we did not with any child units
631
632          Item := First (Context_Items (Comp_Unit_Node));
633
634          while Present (Item) loop
635             if Nkind (Item) = N_With_Clause
636               and then Nkind (Name (Item)) /= N_Identifier
637             then
638                Error_Msg_N ("(Ada 83) child units not allowed", Item);
639             end if;
640
641             Next (Item);
642          end loop;
643
644          --  Check that we did not have a PRIVATE keyword present
645
646          if Private_Present (Comp_Unit_Node) then
647             Error_Msg
648               ("(Ada 83) private units not allowed", Private_Sloc);
649          end if;
650       end if;
651
652       --  If no serious error, then output possible unit information line
653       --  for gnatchop if we are in syntax only, list units mode.
654
655       if not Cunit_Error_Flag
656         and then List_Units
657         and then Operating_Mode = Check_Syntax
658       then
659          Unit_Display (Comp_Unit_Node, Cunit_Location, SR_Present);
660       end if;
661
662       --  And now we should be at the end of file
663
664       if Token /= Tok_EOF then
665
666          --  If we already had to scan for a compilation unit, then don't
667          --  give any further error message, since it just seems to make
668          --  things worse, and we already gave a serious error message.
669
670          if Cunit_Error_Flag then
671             null;
672
673          --  If we are in check syntax mode, then we allow multiple units
674          --  so we just return with Token not set to Tok_EOF and no message.
675
676          elsif Operating_Mode = Check_Syntax then
677             return Comp_Unit_Node;
678
679          --  We also allow multiple units if we are in multiple unit mode
680
681          elsif Multiple_Unit_Index /= 0 then
682
683             --  Skip tokens to end of file, so that the -gnatl listing
684             --  will be complete in this situation, but no need to parse
685             --  the remaining units; no style checking either.
686
687             declare
688                Save_Style_Check : constant Boolean := Style_Check;
689
690             begin
691                Style_Check := False;
692
693                while Token /= Tok_EOF loop
694                   Scan;
695                end loop;
696
697                Style_Check := Save_Style_Check;
698             end;
699
700             return Comp_Unit_Node;
701
702          --  Otherwise we have an error. We suppress the error message
703          --  if we already had a fatal error, since this stops junk
704          --  cascaded messages in some situations.
705
706          else
707             if not Fatal_Error (Current_Source_Unit) then
708                if Token in Token_Class_Cunit then
709                   Error_Msg_SC
710                     ("end of file expected, " &
711                      "file can have only one compilation unit");
712                else
713                   Error_Msg_SC ("end of file expected");
714                end if;
715             end if;
716          end if;
717
718          --  Skip tokens to end of file, so that the -gnatl listing
719          --  will be complete in this situation, but no error checking
720          --  other than that provided at the token level.
721
722          while Token /= Tok_EOF loop
723             Scan;
724          end loop;
725
726          return Error;
727
728       --  Normal return (we were at the end of file as expected)
729
730       else
731          return Comp_Unit_Node;
732       end if;
733
734    exception
735
736       --  An error resync is a serious bomb, so indicate result unit no good
737
738       when Error_Resync =>
739          Set_Fatal_Error (Current_Source_Unit);
740          return Error;
741    end P_Compilation_Unit;
742
743    --------------------------
744    -- 10.1.1  Library Item --
745    --------------------------
746
747    --  Parsed by P_Compilation_Unit (10.1.1)
748
749    --------------------------------------
750    -- 10.1.1  Library Unit Declaration --
751    --------------------------------------
752
753    --  Parsed by P_Compilation_Unit (10.1.1)
754
755    ------------------------------------------------
756    -- 10.1.1  Library Unit Renaming Declaration  --
757    ------------------------------------------------
758
759    --  Parsed by P_Compilation_Unit (10.1.1)
760
761    -------------------------------
762    -- 10.1.1  Library Unit Body --
763    -------------------------------
764
765    --  Parsed by P_Compilation_Unit (10.1.1)
766
767    ------------------------------
768    -- 10.1.1  Parent Unit Name --
769    ------------------------------
770
771    --  Parsed (as a name) by its parent construct
772
773    ----------------------------
774    -- 10.1.2  Context Clause --
775    ----------------------------
776
777    --  CONTEXT_CLAUSE ::= {CONTEXT_ITEM}
778
779    --  CONTEXT_ITEM ::= WITH_CLAUSE | USE_CLAUSE | WITH_TYPE_CLAUSE
780
781    --  WITH_CLAUSE ::=
782    --  [LIMITED] [PRIVATE]  with library_unit_NAME {,library_unit_NAME};
783    --  Note: the two qualifiers are Ada 2005 extensions.
784
785    --  WITH_TYPE_CLAUSE ::=
786    --    with type type_NAME is access; | with type type_NAME is tagged;
787    --  Note: this form is obsolete (old GNAT extension).
788
789    --  Error recovery: Cannot raise Error_Resync
790
791    function P_Context_Clause return List_Id is
792       Item_List   : List_Id;
793       Has_Limited : Boolean := False;
794       Has_Private : Boolean := False;
795       Scan_State  : Saved_Scan_State;
796       With_Node   : Node_Id;
797       First_Flag  : Boolean;
798
799    begin
800       Item_List := New_List;
801
802       --  Get keyword casing from WITH keyword in case not set yet
803
804       if Token = Tok_With then
805          Set_Keyword_Casing (Current_Source_File, Determine_Token_Casing);
806       end if;
807
808       --  Loop through context items
809
810       loop
811          if Style_Check then
812             Style.Check_Indentation;
813          end if;
814
815          --  Gather any pragmas appearing in the context clause
816
817          P_Pragmas_Opt (Item_List);
818
819          --  Processing for WITH clause
820
821          --  Ada 2005 (AI-50217, AI-262): First check for LIMITED WITH,
822          --  PRIVATE WITH, or both.
823
824          if Token = Tok_Limited then
825             Has_Limited := True;
826             Has_Private := False;
827             Scan; -- past LIMITED
828
829             --  In the context, LIMITED can only appear in a with_clause
830
831             if Token = Tok_Private then
832                Has_Private := True;
833                Scan;  -- past PRIVATE
834             end if;
835
836             if Token /= Tok_With then
837                Error_Msg_SC ("unexpected LIMITED ignored");
838             end if;
839
840             if Ada_Version < Ada_05 then
841                Error_Msg_SP ("LIMITED WITH is an Ada 2005 extension");
842                Error_Msg_SP
843                  ("\unit must be compiled with -gnat05 switch");
844             end if;
845
846          elsif Token = Tok_Private then
847             Has_Limited := False;
848             Has_Private := True;
849             Save_Scan_State (Scan_State);
850             Scan;  -- past PRIVATE
851
852             if Token /= Tok_With then
853
854                --  Keyword is beginning of private child unit
855
856                Restore_Scan_State (Scan_State); -- to PRIVATE
857                return Item_List;
858
859             elsif Ada_Version < Ada_05 then
860                Error_Msg_SP ("`PRIVATE WITH` is an Ada 2005 extension");
861                Error_Msg_SP
862                  ("\unit must be compiled with -gnat05 switch");
863             end if;
864
865          else
866             Has_Limited := False;
867             Has_Private := False;
868          end if;
869
870          if Token = Tok_With then
871             Scan; -- past WITH
872
873             if Token = Tok_Type then
874
875                --  WITH TYPE is an obsolete GNAT specific extension
876
877                Error_Msg_SP
878                  ("`WITH TYPE` is an obsolete 'G'N'A'T extension");
879                Error_Msg_SP ("\use Ada 2005 `LIMITED WITH` clause instead");
880
881                Scan;  -- past TYPE
882
883                T_Is;
884
885                if Token = Tok_Tagged then
886                   Scan;
887
888                elsif Token = Tok_Access then
889                   Scan;
890
891                else
892                   Error_Msg_SC ("expect tagged or access qualifier");
893                end if;
894
895                TF_Semicolon;
896
897             else
898                First_Flag := True;
899
900                --  Loop through names in one with clause, generating a separate
901                --  N_With_Clause node for each name encountered.
902
903                loop
904                   With_Node := New_Node (N_With_Clause, Token_Ptr);
905                   Append (With_Node, Item_List);
906
907                   --  Note that we allow with'ing of child units, even in
908                   --  Ada 83 mode, since presumably if this is not desired,
909                   --  then the compilation of the child unit itself is the
910                   --  place where such an "error" should be caught.
911
912                   Set_Name (With_Node, P_Qualified_Simple_Name);
913                   Set_First_Name (With_Node, First_Flag);
914                   Set_Limited_Present (With_Node, Has_Limited);
915                   Set_Private_Present (With_Node, Has_Private);
916                   First_Flag := False;
917
918                   --  All done if no comma
919
920                   exit when Token /= Tok_Comma;
921
922                   --  If comma is followed by compilation unit token
923                   --  or by USE, or PRAGMA, then it should have been a
924                   --  semicolon after all
925
926                   Save_Scan_State (Scan_State);
927                   Scan; -- past comma
928
929                   if Token in Token_Class_Cunit
930                     or else Token = Tok_Use
931                     or else Token = Tok_Pragma
932                   then
933                      Restore_Scan_State (Scan_State);
934                      exit;
935                   end if;
936                end loop;
937
938                Set_Last_Name (With_Node, True);
939                TF_Semicolon;
940             end if;
941
942          --  Processing for USE clause
943
944          elsif Token = Tok_Use then
945             Append (P_Use_Clause, Item_List);
946
947          --  Anything else is end of context clause
948
949          else
950             exit;
951          end if;
952       end loop;
953
954       return Item_List;
955    end P_Context_Clause;
956
957    --------------------------
958    -- 10.1.2  Context Item --
959    --------------------------
960
961    --  Parsed by P_Context_Clause (10.1.2)
962
963    -------------------------
964    -- 10.1.2  With Clause --
965    -------------------------
966
967    --  Parsed by P_Context_Clause (10.1.2)
968
969    -----------------------
970    -- 10.1.3  Body Stub --
971    -----------------------
972
973    --  Subprogram stub parsed by P_Subprogram (6.1)
974    --  Package stub parsed by P_Package (7.1)
975    --  Task stub parsed by P_Task (9.1)
976    --  Protected stub parsed by P_Protected (9.4)
977
978    ----------------------------------
979    -- 10.1.3  Subprogram Body Stub --
980    ----------------------------------
981
982    --  Parsed by P_Subprogram (6.1)
983
984    -------------------------------
985    -- 10.1.3  Package Body Stub --
986    -------------------------------
987
988    --  Parsed by P_Package (7.1)
989
990    ----------------------------
991    -- 10.1.3  Task Body Stub --
992    ----------------------------
993
994    --  Parsed by P_Task (9.1)
995
996    ---------------------------------
997    -- 10.1.3  Protected Body Stub --
998    ---------------------------------
999
1000    --  Parsed by P_Protected (9.4)
1001
1002    ---------------------
1003    -- 10.1.3  Subunit --
1004    ---------------------
1005
1006    --  SUBUNIT ::= separate (PARENT_UNIT_NAME) PROPER_BODY
1007
1008    --  PARENT_UNIT_NAME ::= NAME
1009
1010    --  The caller has checked that the initial token is SEPARATE
1011
1012    --  Error recovery: cannot raise Error_Resync
1013
1014    function P_Subunit return Node_Id is
1015       Subunit_Node : Node_Id;
1016       Body_Node    : Node_Id;
1017
1018    begin
1019       Subunit_Node := New_Node (N_Subunit, Token_Ptr);
1020       Body_Node := Error; -- in case no good body found
1021       Scan; -- past SEPARATE;
1022
1023       U_Left_Paren;
1024       Set_Name (Subunit_Node, P_Qualified_Simple_Name);
1025       U_Right_Paren;
1026
1027       Ignore (Tok_Semicolon);
1028
1029       if Token = Tok_Function or else Token = Tok_Procedure then
1030          Body_Node := P_Subprogram (Pf_Pbod);
1031
1032       elsif Token = Tok_Package then
1033          Body_Node := P_Package (Pf_Pbod);
1034
1035       elsif Token = Tok_Protected then
1036          Scan; -- past PROTECTED
1037
1038          if Token = Tok_Body then
1039             Body_Node := P_Protected;
1040          else
1041             Error_Msg_AP ("BODY expected");
1042             return Error;
1043          end if;
1044
1045       elsif Token = Tok_Task then
1046          Scan; -- past TASK
1047
1048          if Token = Tok_Body then
1049             Body_Node := P_Task;
1050          else
1051             Error_Msg_AP ("BODY expected");
1052             return Error;
1053          end if;
1054
1055       else
1056          Error_Msg_SC ("proper body expected");
1057          return Error;
1058       end if;
1059
1060       Set_Proper_Body  (Subunit_Node, Body_Node);
1061       return Subunit_Node;
1062
1063    end P_Subunit;
1064
1065    ------------------
1066    -- Set_Location --
1067    ------------------
1068
1069    function Set_Location return Source_Ptr is
1070       Physical   : Boolean;
1071       Loc        : Source_Ptr;
1072       Scan_State : Saved_Scan_State;
1073
1074    begin
1075       --  A special check. If the first token is pragma, and this is a
1076       --  Source_Reference pragma, then do NOT eat previous comments, since
1077       --  the Source_Reference pragma is required to be the first line in
1078       --  the source file.
1079
1080       if Token = Tok_Pragma then
1081          Save_Scan_State (Scan_State);
1082          Scan; --  past Pragma
1083
1084          if Token = Tok_Identifier
1085            and then Token_Name = Name_Source_Reference
1086          then
1087             Restore_Scan_State (Scan_State);
1088             return Token_Ptr;
1089          end if;
1090
1091          Restore_Scan_State (Scan_State);
1092       end if;
1093
1094       --  Otherwise acquire previous comments and blank lines
1095
1096       if Prev_Token = No_Token then
1097          return Source_First (Current_Source_File);
1098
1099       else
1100          Loc := Prev_Token_Ptr;
1101          loop
1102             exit when Loc = Token_Ptr;
1103
1104             --  Should we worry about UTF_32 line terminators here
1105
1106             if Source (Loc) in Line_Terminator then
1107                Skip_Line_Terminators (Loc, Physical);
1108                exit when Physical;
1109             end if;
1110
1111             Loc := Loc + 1;
1112          end loop;
1113
1114          return Loc;
1115       end if;
1116    end Set_Location;
1117
1118    ------------------
1119    -- Unit_Display --
1120    ------------------
1121
1122    --  The format of the generated line, as expected by GNATCHOP is
1123
1124    --    Unit {unit} line {line}, file offset {offs} [, SR], file name {file}
1125
1126    --  where
1127
1128    --     {unit}     unit name with terminating (spec) or (body)
1129    --     {line}     starting line number
1130    --     {offs}     offset to start of text in file
1131    --     {file}     source file name
1132
1133    --  The SR parameter is present only if a source reference pragma was
1134    --  scanned for this unit. The significance is that gnatchop should not
1135    --  attempt to add another one.
1136
1137    procedure Unit_Display
1138      (Cunit      : Node_Id;
1139       Loc        : Source_Ptr;
1140       SR_Present : Boolean)
1141    is
1142       Unum : constant Unit_Number_Type    := Get_Cunit_Unit_Number (Cunit);
1143       Sind : constant Source_File_Index   := Source_Index (Unum);
1144       Unam : constant Unit_Name_Type      := Unit_Name (Unum);
1145
1146    begin
1147       if List_Units then
1148          Write_Str ("Unit ");
1149          Write_Unit_Name (Unit_Name (Unum));
1150          Unit_Location (Sind, Loc);
1151
1152          if SR_Present then
1153             Write_Str (", SR");
1154          end if;
1155
1156          Write_Str (", file name ");
1157          Write_Name (Get_File_Name (Unam, Nkind (Unit (Cunit)) = N_Subunit));
1158          Write_Eol;
1159       end if;
1160    end Unit_Display;
1161
1162    -------------------
1163    -- Unit_Location --
1164    -------------------
1165
1166    procedure Unit_Location (Sind : Source_File_Index; Loc : Source_Ptr) is
1167       Line : constant Logical_Line_Number := Get_Logical_Line_Number (Loc);
1168       --  Should the above be the physical line number ???
1169
1170    begin
1171       Write_Str (" line ");
1172       Write_Int (Int (Line));
1173
1174       Write_Str (", file offset ");
1175       Write_Int (Int (Loc) - Int (Source_First (Sind)));
1176    end Unit_Location;
1177
1178 end Ch10;