Add an UNSPEC_PROLOGUE_USE to prevent the link register from being considered dead.
[official-gcc.git] / gcc / ada / par-ch10.adb
blob0f52757dd6b2742a094657b48e5f04bd906d430c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R . C H 1 0 --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 -- --
26 ------------------------------------------------------------------------------
28 pragma Style_Checks (All_Checks);
29 -- Turn off subprogram body ordering check. Subprograms are in order
30 -- by RM section rather than alphabetical
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Hostparm; use Hostparm;
35 with Uname; use Uname;
37 separate (Par)
38 package body Ch10 is
40 -- Local functions, used only in this chapter
42 function P_Context_Clause return List_Id;
43 function P_Subunit return Node_Id;
45 function Set_Location return Source_Ptr;
46 -- The current compilation unit starts with Token at Token_Ptr. This
47 -- function determines the corresponding source location for the start
48 -- of the unit, including any preceding comment lines.
50 procedure Unit_Display
51 (Cunit : Node_Id;
52 Loc : Source_Ptr;
53 SR_Present : Boolean);
54 -- This procedure is used to generate a line of output for the a unit in
55 -- the source program. Cunit is the node for the compilation unit, and
56 -- Loc is the source location for the start of the unit in the source
57 -- file (which is not necessarily the Sloc of the Cunit node). This
58 -- output is written to the standard output file for use by gnatchop.
60 procedure Unit_Location (Sind : Source_File_Index; Loc : Source_Ptr);
61 -- This routine has the same calling sequence as Unit_Display, but
62 -- it outputs only the line number and offset of the location, Loc,
63 -- using Cunit to obtain the proper source file index.
65 -------------------------
66 -- 10.1.1 Compilation --
67 -------------------------
69 -- COMPILATION ::= {COMPILATION_UNIT}
71 -- There is no specific parsing routine for a compilation, since we only
72 -- permit a single compilation in a source file, so there is no explicit
73 -- occurrence of compilations as such (our representation of a compilation
74 -- is a series of separate source files).
76 ------------------------------
77 -- 10.1.1 Compilation unit --
78 ------------------------------
80 -- COMPILATION_UNIT ::=
81 -- CONTEXT_CLAUSE LIBRARY_ITEM
82 -- | CONTEXT_CLAUSE SUBUNIT
84 -- LIBRARY_ITEM ::=
85 -- private LIBRARY_UNIT_DECLARATION
86 -- | LIBRARY_UNIT_BODY
87 -- | [private] LIBRARY_UNIT_RENAMING_DECLARATION
89 -- LIBRARY_UNIT_DECLARATION ::=
90 -- SUBPROGRAM_DECLARATION | PACKAGE_DECLARATION
91 -- | GENERIC_DECLARATION | GENERIC_INSTANTIATION
93 -- LIBRARY_UNIT_RENAMING_DECLARATION ::=
94 -- PACKAGE_RENAMING_DECLARATION
95 -- | GENERIC_RENAMING_DECLARATION
96 -- | SUBPROGRAM_RENAMING_DECLARATION
98 -- LIBRARY_UNIT_BODY ::= SUBPROGRAM_BODY | PACKAGE_BODY
100 -- Error recovery: cannot raise Error_Resync. If an error occurs, tokens
101 -- are skipped up to the next possible beginning of a compilation unit.
103 -- Note: if only configuration pragmas are found, Empty is returned
105 -- Note: in syntax-only mode, it is possible for P_Compilation_Unit
106 -- to return strange things that are not really compilation units.
107 -- This is done to help out gnatchop when it is faced with nonsense.
109 function P_Compilation_Unit return Node_Id is
110 Scan_State : Saved_Scan_State;
111 Body_Node : Node_Id;
112 Specification_Node : Node_Id;
113 Unit_Node : Node_Id;
114 Comp_Unit_Node : Node_Id;
115 Name_Node : Node_Id;
116 Item : Node_Id;
117 Private_Sloc : Source_Ptr := No_Location;
118 Config_Pragmas : List_Id;
119 P : Node_Id;
120 SR_Present : Boolean;
122 Cunit_Error_Flag : Boolean := False;
123 -- This flag is set True if we have to scan for a compilation unit
124 -- token. It is used to ensure clean termination in such cases by
125 -- not insisting on being at the end of file, and, in the sytax only
126 -- case by not scanning for additional compilation units.
128 Cunit_Location : Source_Ptr;
129 -- Location of unit for unit identification output (List_Unit option)
131 begin
132 Num_Library_Units := Num_Library_Units + 1;
134 -- Set location of the compilation unit if unit list option set
135 -- and we are in syntax check only mode
137 if List_Units and then Operating_Mode = Check_Syntax then
138 Cunit_Location := Set_Location;
139 else
140 Cunit_Location := No_Location;
141 end if;
143 -- Deal with initial pragmas
145 Config_Pragmas := No_List;
147 -- If we have an initial Source_Reference pragma, then remember
148 -- the fact to generate an NR parameter in the output line.
150 SR_Present := False;
152 if Token = Tok_Pragma then
153 Save_Scan_State (Scan_State);
154 Item := P_Pragma;
156 if Item = Error
157 or else Chars (Item) /= Name_Source_Reference
158 then
159 Restore_Scan_State (Scan_State);
161 else
162 SR_Present := True;
164 -- If first unit, record the file name for gnatchop use
166 if Operating_Mode = Check_Syntax
167 and then List_Units
168 and then Num_Library_Units = 1
169 then
170 Write_Str ("Source_Reference pragma for file """);
171 Write_Name (Full_Ref_Name (Current_Source_File));
172 Write_Char ('"');
173 Write_Eol;
174 end if;
176 Config_Pragmas := New_List (Item);
177 end if;
178 end if;
180 -- Scan out any configuration pragmas
182 while Token = Tok_Pragma loop
183 Save_Scan_State (Scan_State);
184 Item := P_Pragma;
186 if Item = Error
187 or else Chars (Item) > Last_Configuration_Pragma_Name
188 then
189 Restore_Scan_State (Scan_State);
190 exit;
191 end if;
193 if Config_Pragmas = No_List then
194 Config_Pragmas := Empty_List;
196 if Operating_Mode = Check_Syntax and then List_Units then
197 Write_Str ("Configuration pragmas at");
198 Unit_Location (Current_Source_File, Cunit_Location);
199 Write_Eol;
200 end if;
201 end if;
203 Append (Item, Config_Pragmas);
204 Cunit_Location := Set_Location;
205 end loop;
207 -- Establish compilation unit node and scan context items
209 Comp_Unit_Node := New_Node (N_Compilation_Unit, No_Location);
210 Set_Cunit (Current_Source_Unit, Comp_Unit_Node);
211 Set_Context_Items (Comp_Unit_Node, P_Context_Clause);
212 Set_Aux_Decls_Node
213 (Comp_Unit_Node, New_Node (N_Compilation_Unit_Aux, No_Location));
215 if Present (Config_Pragmas) then
217 -- Check for case of only configuration pragmas present
219 if Token = Tok_EOF
220 and then Is_Empty_List (Context_Items (Comp_Unit_Node))
221 then
222 if Operating_Mode = Check_Syntax then
223 return Empty;
225 else
226 Item := First (Config_Pragmas);
227 Error_Msg_N
228 ("cannot compile configuration pragmas with gcc", Item);
229 Error_Msg_N
230 ("use gnatchop -c to process configuration pragmas!", Item);
231 raise Unrecoverable_Error;
232 end if;
234 -- Otherwise configuration pragmas are simply prepended to the
235 -- context of the current unit.
237 else
238 Append_List (Context_Items (Comp_Unit_Node), Config_Pragmas);
239 Set_Context_Items (Comp_Unit_Node, Config_Pragmas);
240 end if;
241 end if;
243 -- Check for PRIVATE. Note that for the moment we allow this in
244 -- Ada_83 mode, since we do not yet know if we are compiling a
245 -- predefined unit, and if we are then it would be allowed anyway.
247 if Token = Tok_Private then
248 Private_Sloc := Token_Ptr;
249 Set_Keyword_Casing (Current_Source_File, Determine_Token_Casing);
250 if Style_Check then Style.Check_Indentation; end if;
252 Save_Scan_State (Scan_State); -- at PRIVATE
253 Scan; -- past PRIVATE
255 if Token = Tok_Separate then
256 Error_Msg_SP ("cannot have private subunits!");
258 elsif Token = Tok_Package then
259 Scan; -- past PACKAGE
261 if Token = Tok_Body then
262 Restore_Scan_State (Scan_State); -- to PRIVATE
263 Error_Msg_SC ("cannot have private package body!");
264 Scan; -- ignore PRIVATE
266 else
267 Restore_Scan_State (Scan_State); -- to PRIVATE
268 Scan; -- past PRIVATE
269 Set_Private_Present (Comp_Unit_Node, True);
270 end if;
272 elsif Token = Tok_Procedure
273 or else Token = Tok_Function
274 or else Token = Tok_Generic
275 then
276 Set_Private_Present (Comp_Unit_Node, True);
277 end if;
278 end if;
280 -- Loop to find our way to a compilation unit token
282 loop
283 exit when Token in Token_Class_Cunit and then Token /= Tok_With;
285 exit when Bad_Spelling_Of (Tok_Package)
286 or else Bad_Spelling_Of (Tok_Function)
287 or else Bad_Spelling_Of (Tok_Generic)
288 or else Bad_Spelling_Of (Tok_Separate)
289 or else Bad_Spelling_Of (Tok_Procedure);
291 -- Allow task and protected for nice error recovery purposes
293 exit when Token = Tok_Task
294 or else Token = Tok_Protected;
296 if Token = Tok_With then
297 Error_Msg_SC ("misplaced WITH");
298 Append_List (P_Context_Clause, Context_Items (Comp_Unit_Node));
300 elsif Bad_Spelling_Of (Tok_With) then
301 Append_List (P_Context_Clause, Context_Items (Comp_Unit_Node));
303 else
304 Error_Msg_SC ("compilation unit expected");
305 Cunit_Error_Flag := True;
306 Resync_Cunit;
308 -- If we are at an end of file, then just quit, the above error
309 -- message was complaint enough.
311 if Token = Tok_EOF then
312 return Error;
313 end if;
314 end if;
315 end loop;
317 -- We have a compilation unit token, so that's a reasonable choice for
318 -- determining the standard casing convention used for keywords in case
319 -- it hasn't already been done on seeing a WITH or PRIVATE.
321 Set_Keyword_Casing (Current_Source_File, Determine_Token_Casing);
322 if Style_Check then Style.Check_Indentation; end if;
324 -- Remaining processing depends on particular type of compilation unit
326 if Token = Tok_Package then
328 -- A common error is to omit the body keyword after package. We can
329 -- often diagnose this early on (before getting loads of errors from
330 -- contained subprogram bodies), by knowing that that the file we
331 -- are compiling has a name that requires a body to be found.
333 -- However, we do not do this check if we are operating in syntax
334 -- checking only mode, because in that case there may be multiple
335 -- units in the same file, and the file name is not a reliable guide.
337 Save_Scan_State (Scan_State);
338 Scan; -- past Package keyword
340 if Token /= Tok_Body
341 and then Operating_Mode /= Check_Syntax
342 and then
343 Get_Expected_Unit_Type
344 (File_Name (Current_Source_File)) = Expect_Body
345 then
346 Error_Msg_BC ("keyword BODY expected here [see file name]");
347 Restore_Scan_State (Scan_State);
348 Set_Unit (Comp_Unit_Node, P_Package (Pf_Pbod));
349 else
350 Restore_Scan_State (Scan_State);
351 Set_Unit (Comp_Unit_Node, P_Package (Pf_Decl_Gins_Pbod_Rnam));
352 end if;
354 elsif Token = Tok_Generic then
355 Set_Unit (Comp_Unit_Node, P_Generic);
357 elsif Token = Tok_Separate then
358 Set_Unit (Comp_Unit_Node, P_Subunit);
360 elsif Token = Tok_Procedure
361 or else Token = Tok_Function
362 then
363 Set_Unit (Comp_Unit_Node, P_Subprogram (Pf_Decl_Gins_Pbod_Rnam));
365 -- A little bit of an error recovery check here. If we just scanned
366 -- a subprogram declaration (as indicated by an SIS entry being
367 -- active), then if the following token is BEGIN or an identifier,
368 -- or a token which can reasonably start a declaration but cannot
369 -- start a compilation unit, then we assume that the semicolon in
370 -- the declaration should have been IS.
372 if SIS_Entry_Active then
374 if Token = Tok_Begin
375 or else Token = Tok_Identifier
376 or else Token in Token_Class_Deckn
377 then
378 Push_Scope_Stack;
379 Scope.Table (Scope.Last).Etyp := E_Name;
380 Scope.Table (Scope.Last).Sloc := SIS_Sloc;
381 Scope.Table (Scope.Last).Ecol := SIS_Ecol;
382 Scope.Table (Scope.Last).Lreq := False;
383 SIS_Entry_Active := False;
385 -- If we had a missing semicolon in the declaration, then
386 -- change the message to from <missing ";"> to <missing "is">
388 if SIS_Missing_Semicolon_Message /= No_Error_Msg then
389 Change_Error_Text -- Replace: "missing "";"" "
390 (SIS_Missing_Semicolon_Message, "missing IS");
392 -- Otherwise we saved the semicolon position, so complain
394 else
395 Error_Msg (""";"" should be IS", SIS_Semicolon_Sloc);
396 end if;
398 Body_Node := Unit (Comp_Unit_Node);
399 Specification_Node := Specification (Body_Node);
400 Change_Node (Body_Node, N_Subprogram_Body);
401 Set_Specification (Body_Node, Specification_Node);
402 Parse_Decls_Begin_End (Body_Node);
403 Set_Unit (Comp_Unit_Node, Body_Node);
404 end if;
406 -- If we scanned a subprogram body, make sure we did not have private
408 elsif Private_Sloc /= No_Location
409 and then Nkind (Unit (Comp_Unit_Node)) /= N_Function_Instantiation
410 and then Nkind (Unit (Comp_Unit_Node)) /= N_Procedure_Instantiation
411 then
412 Error_Msg ("cannot have private subprogram body", Private_Sloc);
414 -- P_Subprogram can yield an abstract subprogram, but this cannot
415 -- be a compilation unit. Treat as a subprogram declaration.
417 elsif
418 Nkind (Unit (Comp_Unit_Node)) = N_Abstract_Subprogram_Declaration
419 then
420 Error_Msg_N
421 ("compilation unit cannot be abstract subprogram",
422 Unit (Comp_Unit_Node));
424 Unit_Node :=
425 New_Node (N_Subprogram_Declaration, Sloc (Comp_Unit_Node));
426 Set_Specification (Unit_Node,
427 Specification (Unit (Comp_Unit_Node)));
428 Set_Unit (Comp_Unit_Node, Unit_Node);
429 end if;
431 -- Otherwise we have TASK. This is not really an acceptable token,
432 -- but we accept it to improve error recovery.
434 elsif Token = Tok_Task then
435 Scan; -- Past TASK
437 if Token = Tok_Type then
438 Error_Msg_SP
439 ("task type cannot be used as compilation unit");
440 else
441 Error_Msg_SP
442 ("task declaration cannot be used as compilation unit");
443 end if;
445 -- If in check syntax mode, accept the task anyway. This is done
446 -- particularly to improve the behavior of GNATCHOP in this case.
448 if Operating_Mode = Check_Syntax then
449 Set_Unit (Comp_Unit_Node, P_Task);
451 -- If not in syntax only mode, treat this as horrible error
453 else
454 Cunit_Error_Flag := True;
455 return Error;
456 end if;
458 else pragma Assert (Token = Tok_Protected);
459 Scan; -- Past PROTECTED
461 if Token = Tok_Type then
462 Error_Msg_SP
463 ("protected type cannot be used as compilation unit");
464 else
465 Error_Msg_SP
466 ("protected declaration cannot be used as compilation unit");
467 end if;
469 -- If in check syntax mode, accept protected anyway. This is done
470 -- particularly to improve the behavior of GNATCHOP in this case.
472 if Operating_Mode = Check_Syntax then
473 Set_Unit (Comp_Unit_Node, P_Protected);
475 -- If not in syntax only mode, treat this as horrible error
477 else
478 Cunit_Error_Flag := True;
479 return Error;
480 end if;
481 end if;
483 -- Here is where locate the compilation unit entity. This is a little
484 -- tricky, since it is buried in various places.
486 Unit_Node := Unit (Comp_Unit_Node);
488 -- Another error from which it is hard to recover
490 if Nkind (Unit_Node) = N_Subprogram_Body_Stub
491 or else Nkind (Unit_Node) = N_Package_Body_Stub
492 then
493 Cunit_Error_Flag := True;
494 return Error;
495 end if;
497 -- Only try this if we got an OK unit!
499 if Unit_Node /= Error then
500 if Nkind (Unit_Node) = N_Subunit then
501 Unit_Node := Proper_Body (Unit_Node);
502 end if;
504 if Nkind (Unit_Node) in N_Generic_Declaration then
505 Unit_Node := Specification (Unit_Node);
506 end if;
508 if Nkind (Unit_Node) = N_Package_Declaration
509 or else Nkind (Unit_Node) = N_Subprogram_Declaration
510 or else Nkind (Unit_Node) = N_Subprogram_Body
511 or else Nkind (Unit_Node) = N_Subprogram_Renaming_Declaration
512 then
513 Unit_Node := Specification (Unit_Node);
515 elsif Nkind (Unit_Node) = N_Subprogram_Renaming_Declaration then
516 if Ada_83 then
517 Error_Msg_N
518 ("(Ada 83) library unit renaming not allowed", Unit_Node);
519 end if;
520 end if;
522 if Nkind (Unit_Node) = N_Task_Body
523 or else Nkind (Unit_Node) = N_Protected_Body
524 or else Nkind (Unit_Node) = N_Task_Type_Declaration
525 or else Nkind (Unit_Node) = N_Protected_Type_Declaration
526 or else Nkind (Unit_Node) = N_Single_Task_Declaration
527 or else Nkind (Unit_Node) = N_Single_Protected_Declaration
528 then
529 Name_Node := Defining_Identifier (Unit_Node);
530 else
531 Name_Node := Defining_Unit_Name (Unit_Node);
532 end if;
534 Set_Sloc (Comp_Unit_Node, Sloc (Name_Node));
535 Set_Sloc (Aux_Decls_Node (Comp_Unit_Node), Sloc (Name_Node));
537 -- Set Entity field in file table. Easier now that we have name!
538 -- Note that this is also skipped if we had a bad unit
540 if Nkind (Name_Node) = N_Defining_Program_Unit_Name then
541 Set_Cunit_Entity
542 (Current_Source_Unit, Defining_Identifier (Name_Node));
543 else
544 Set_Cunit_Entity (Current_Source_Unit, Name_Node);
545 end if;
547 Set_Unit_Name
548 (Current_Source_Unit, Get_Unit_Name (Unit (Comp_Unit_Node)));
550 -- If we had a bad unit, make sure the fatal flag is set in the file
551 -- table entry, since this is surely a fatal error and also set our
552 -- flag to inhibit the requirement that we be at end of file.
554 else
555 Cunit_Error_Flag := True;
556 Set_Fatal_Error (Current_Source_Unit);
557 end if;
559 -- Clear away any missing semicolon indication, we are done with that
560 -- unit, so what's done is done, and we don't want anything hanging
561 -- around from the attempt to parse it!
563 SIS_Entry_Active := False;
565 -- Scan out pragmas after unit
567 while Token = Tok_Pragma loop
568 Save_Scan_State (Scan_State);
570 -- If we are in syntax scan mode allowing multiple units, then
571 -- start the next unit if we encounter a configuration pragma,
572 -- or a source reference pragma. We take care not to actually
573 -- scan the pragma in this case since we don't want it to take
574 -- effect for the current unit.
576 if Operating_Mode = Check_Syntax then
577 Scan; -- past Pragma
579 if Token = Tok_Identifier
580 and then
581 (Token_Name in
582 First_Pragma_Name .. Last_Configuration_Pragma_Name
583 or else Token_Name = Name_Source_Reference)
584 then
585 Restore_Scan_State (Scan_State); -- to Pragma
586 exit;
587 end if;
588 end if;
590 -- Otherwise eat the pragma, it definitely belongs with the
591 -- current unit, and not with the following unit.
593 Restore_Scan_State (Scan_State); -- to Pragma
594 P := P_Pragma;
596 if No (Pragmas_After (Aux_Decls_Node (Comp_Unit_Node))) then
597 Set_Pragmas_After
598 (Aux_Decls_Node (Comp_Unit_Node), New_List);
599 end if;
601 Append (P, Pragmas_After (Aux_Decls_Node (Comp_Unit_Node)));
602 end loop;
604 -- Cancel effect of any outstanding pragma Warnings (Off)
606 Set_Warnings_Mode_On (Scan_Ptr);
608 -- Ada 83 error checks
610 if Ada_83 then
612 -- Check we did not with any child units
614 Item := First (Context_Items (Comp_Unit_Node));
616 while Present (Item) loop
617 if Nkind (Item) = N_With_Clause
618 and then Nkind (Name (Item)) /= N_Identifier
619 then
620 Error_Msg_N ("(Ada 83) child units not allowed", Item);
621 end if;
623 Next (Item);
624 end loop;
626 -- Check that we did not have a PRIVATE keyword present
628 if Private_Present (Comp_Unit_Node) then
629 Error_Msg
630 ("(Ada 83) private units not allowed", Private_Sloc);
631 end if;
632 end if;
634 -- If no serious error, then output possible unit information line
635 -- for gnatchop if we are in syntax only, list units mode.
637 if not Cunit_Error_Flag
638 and then List_Units
639 and then Operating_Mode = Check_Syntax
640 then
641 Unit_Display (Comp_Unit_Node, Cunit_Location, SR_Present);
642 end if;
644 -- And now we should be at the end of file
646 if Token /= Tok_EOF then
648 -- If we already had to scan for a compilation unit, then don't
649 -- give any further error message, since it just sems to make
650 -- things worse, and we already gave a serious error message.
652 if Cunit_Error_Flag then
653 null;
655 -- If we are in check syntax mode, then we allow multiple units
656 -- so we just return with Token not set to Tok_EOF and no message.
658 elsif Operating_Mode = Check_Syntax then
659 return Comp_Unit_Node;
661 -- Otherwise we have an error. We suppress the error message
662 -- if we already had a fatal error, since this stops junk
663 -- cascaded messages in some situations.
665 else
666 if not Fatal_Error (Current_Source_Unit) then
668 if Token in Token_Class_Cunit then
669 Error_Msg_SC
670 ("end of file expected, " &
671 "file can have only one compilation unit");
673 else
674 Error_Msg_SC ("end of file expected");
675 end if;
676 end if;
677 end if;
679 -- Skip tokens to end of file, so that the -gnatl listing
680 -- will be complete in this situation, but no error checking
681 -- other than that provided at the token level.
683 while Token /= Tok_EOF loop
684 Scan;
685 end loop;
687 return Error;
689 -- Normal return (we were at the end of file as expected)
691 else
692 return Comp_Unit_Node;
693 end if;
695 exception
697 -- An error resync is a serious bomb, so indicate result unit no good
699 when Error_Resync =>
700 Set_Fatal_Error (Current_Source_Unit);
701 return Error;
703 end P_Compilation_Unit;
705 --------------------------
706 -- 10.1.1 Library Item --
707 --------------------------
709 -- Parsed by P_Compilation_Unit (10.1.1)
711 --------------------------------------
712 -- 10.1.1 Library Unit Declaration --
713 --------------------------------------
715 -- Parsed by P_Compilation_Unit (10.1.1)
717 ------------------------------------------------
718 -- 10.1.1 Library Unit Renaming Declaration --
719 ------------------------------------------------
721 -- Parsed by P_Compilation_Unit (10.1.1)
723 -------------------------------
724 -- 10.1.1 Library Unit Body --
725 -------------------------------
727 -- Parsed by P_Compilation_Unit (10.1.1)
729 ------------------------------
730 -- 10.1.1 Parent Unit Name --
731 ------------------------------
733 -- Parsed (as a name) by its parent construct
735 ----------------------------
736 -- 10.1.2 Context Clause --
737 ----------------------------
739 -- CONTEXT_CLAUSE ::= {CONTEXT_ITEM}
741 -- CONTEXT_ITEM ::= WITH_CLAUSE | USE_CLAUSE | WITH_TYPE_CLAUSE
743 -- WITH_CLAUSE ::=
744 -- with library_unit_NAME {,library_unit_NAME};
746 -- WITH_TYPE_CLAUSE ::=
747 -- with type type_NAME is access; | with type type_NAME is tagged;
749 -- Error recovery: Cannot raise Error_Resync
751 function P_Context_Clause return List_Id is
752 Item_List : List_Id;
753 With_Node : Node_Id;
754 First_Flag : Boolean;
756 begin
757 Item_List := New_List;
759 -- Get keyword casing from WITH keyword in case not set yet
761 if Token = Tok_With then
762 Set_Keyword_Casing (Current_Source_File, Determine_Token_Casing);
763 end if;
765 -- Loop through context items
767 loop
768 if Style_Check then Style.Check_Indentation; end if;
770 -- Gather any pragmas appearing in the context clause
772 P_Pragmas_Opt (Item_List);
774 -- Processing for WITH clause
776 if Token = Tok_With then
777 Scan; -- past WITH
779 if Token = Tok_Type then
781 -- WITH TYPE is an extension
783 if not Extensions_Allowed then
784 Error_Msg_SP ("`WITH TYPE` is a non-standard extension");
786 if OpenVMS then
787 Error_Msg_SP
788 ("\unit must be compiled with " &
789 "'/'E'X'T'E'N'S'I'O'N'S'_'A'L'L'O'W'E'D qualifier");
790 else
791 Error_Msg_SP
792 ("\unit must be compiled with -gnatX switch");
793 end if;
794 end if;
796 Scan; -- past TYPE
797 With_Node := New_Node (N_With_Type_Clause, Token_Ptr);
798 Append (With_Node, Item_List);
799 Set_Name (With_Node, P_Qualified_Simple_Name);
801 T_Is;
803 if Token = Tok_Tagged then
804 Set_Tagged_Present (With_Node);
805 Scan;
807 elsif Token = Tok_Access then
808 Scan;
810 else
811 Error_Msg_SC ("expect tagged or access qualifier");
812 end if;
814 TF_Semicolon;
816 else
817 First_Flag := True;
819 -- Loop through names in one with clause, generating a separate
820 -- N_With_Clause node for each nam encountered.
822 loop
823 With_Node := New_Node (N_With_Clause, Token_Ptr);
824 Append (With_Node, Item_List);
826 -- Note that we allow with'ing of child units, even in
827 -- Ada 83 mode, since presumably if this is not desired,
828 -- then the compilation of the child unit itself is the
829 -- place where such an "error" should be caught.
831 Set_Name (With_Node, P_Qualified_Simple_Name);
832 Set_First_Name (With_Node, First_Flag);
833 First_Flag := False;
834 exit when Token /= Tok_Comma;
835 Scan; -- past comma
836 end loop;
838 Set_Last_Name (With_Node, True);
839 TF_Semicolon;
840 end if;
842 -- Processing for USE clause
844 elsif Token = Tok_Use then
845 Append (P_Use_Clause, Item_List);
847 -- Anything else is end of context clause
849 else
850 exit;
851 end if;
852 end loop;
854 return Item_List;
855 end P_Context_Clause;
857 --------------------------
858 -- 10.1.2 Context Item --
859 --------------------------
861 -- Parsed by P_Context_Clause (10.1.2)
863 -------------------------
864 -- 10.1.2 With Clause --
865 -------------------------
867 -- Parsed by P_Context_Clause (10.1.2)
869 -----------------------
870 -- 10.1.3 Body Stub --
871 -----------------------
873 -- Subprogram stub parsed by P_Subprogram (6.1)
874 -- Package stub parsed by P_Package (7.1)
875 -- Task stub parsed by P_Task (9.1)
876 -- Protected stub parsed by P_Protected (9.4)
878 ----------------------------------
879 -- 10.1.3 Subprogram Body Stub --
880 ----------------------------------
882 -- Parsed by P_Subprogram (6.1)
884 -------------------------------
885 -- 10.1.3 Package Body Stub --
886 -------------------------------
888 -- Parsed by P_Package (7.1)
890 ----------------------------
891 -- 10.1.3 Task Body Stub --
892 ----------------------------
894 -- Parsed by P_Task (9.1)
896 ---------------------------------
897 -- 10.1.3 Protected Body Stub --
898 ---------------------------------
900 -- Parsed by P_Protected (9.4)
902 ---------------------
903 -- 10.1.3 Subunit --
904 ---------------------
906 -- SUBUNIT ::= separate (PARENT_UNIT_NAME) PROPER_BODY
908 -- PARENT_UNIT_NAME ::= NAME
910 -- The caller has checked that the initial token is SEPARATE
912 -- Error recovery: cannot raise Error_Resync
914 function P_Subunit return Node_Id is
915 Subunit_Node : Node_Id;
916 Body_Node : Node_Id;
918 begin
919 Subunit_Node := New_Node (N_Subunit, Token_Ptr);
920 Body_Node := Error; -- in case no good body found
921 Scan; -- past SEPARATE;
923 T_Left_Paren;
924 Set_Name (Subunit_Node, P_Qualified_Simple_Name);
925 T_Right_Paren;
927 if Token = Tok_Semicolon then
928 Error_Msg_SC ("unexpected semicolon ignored");
929 Scan;
930 end if;
932 if Token = Tok_Function or else Token = Tok_Procedure then
933 Body_Node := P_Subprogram (Pf_Pbod);
935 elsif Token = Tok_Package then
936 Body_Node := P_Package (Pf_Pbod);
938 elsif Token = Tok_Protected then
939 Scan; -- past PROTECTED
941 if Token = Tok_Body then
942 Body_Node := P_Protected;
943 else
944 Error_Msg_AP ("BODY expected");
945 return Error;
946 end if;
948 elsif Token = Tok_Task then
949 Scan; -- past TASK
951 if Token = Tok_Body then
952 Body_Node := P_Task;
953 else
954 Error_Msg_AP ("BODY expected");
955 return Error;
956 end if;
958 else
959 Error_Msg_SC ("proper body expected");
960 return Error;
961 end if;
963 Set_Proper_Body (Subunit_Node, Body_Node);
964 return Subunit_Node;
966 end P_Subunit;
968 ------------------
969 -- Set_Location --
970 ------------------
972 function Set_Location return Source_Ptr is
973 Physical : Boolean;
974 Loc : Source_Ptr;
975 Scan_State : Saved_Scan_State;
977 begin
978 -- A special check. If the first token is pragma, and this is a
979 -- Source_Reference pragma, then do NOT eat previous comments, since
980 -- the Source_Reference pragma is required to be the first line in
981 -- the source file.
983 if Token = Tok_Pragma then
984 Save_Scan_State (Scan_State);
985 Scan; -- past Pragma
987 if Token = Tok_Identifier
988 and then Token_Name = Name_Source_Reference
989 then
990 Restore_Scan_State (Scan_State);
991 return Token_Ptr;
992 end if;
994 Restore_Scan_State (Scan_State);
995 end if;
997 -- Otherwise acquire previous comments and blank lines
999 if Prev_Token = No_Token then
1000 return Source_First (Current_Source_File);
1002 else
1003 Loc := Prev_Token_Ptr;
1004 loop
1005 exit when Loc = Token_Ptr;
1007 if Source (Loc) in Line_Terminator then
1008 Skip_Line_Terminators (Loc, Physical);
1009 exit when Physical;
1010 end if;
1012 Loc := Loc + 1;
1013 end loop;
1015 return Loc;
1016 end if;
1017 end Set_Location;
1019 ------------------
1020 -- Unit_Display --
1021 ------------------
1023 -- The format of the generated line, as expected by GNATCHOP is
1025 -- Unit {unit} line {line}, file offset {offs} [, SR], file name {file}
1027 -- where
1029 -- {unit} unit name with terminating (spec) or (body)
1030 -- {line} starting line number
1031 -- {offs} offset to start of text in file
1032 -- {file} source file name
1034 -- The SR parameter is present only if a source reference pragma was
1035 -- scanned for this unit. The significance is that gnatchop should not
1036 -- attempt to add another one.
1038 procedure Unit_Display
1039 (Cunit : Node_Id;
1040 Loc : Source_Ptr;
1041 SR_Present : Boolean)
1043 Unum : constant Unit_Number_Type := Get_Cunit_Unit_Number (Cunit);
1044 Sind : constant Source_File_Index := Source_Index (Unum);
1045 Unam : constant Unit_Name_Type := Unit_Name (Unum);
1047 begin
1048 if List_Units then
1049 Write_Str ("Unit ");
1050 Write_Unit_Name (Unit_Name (Unum));
1051 Unit_Location (Sind, Loc);
1053 if SR_Present then
1054 Write_Str (", SR");
1055 end if;
1057 Write_Str (", file name ");
1058 Write_Name (Get_File_Name (Unam, Nkind (Unit (Cunit)) = N_Subunit));
1059 Write_Eol;
1060 end if;
1061 end Unit_Display;
1063 -------------------
1064 -- Unit_Location --
1065 -------------------
1067 procedure Unit_Location (Sind : Source_File_Index; Loc : Source_Ptr) is
1068 Line : constant Logical_Line_Number := Get_Logical_Line_Number (Loc);
1069 -- Should the above be the physical line number ???
1071 begin
1072 Write_Str (" line ");
1073 Write_Int (Int (Line));
1075 Write_Str (", file offset ");
1076 Write_Int (Int (Loc) - Int (Source_First (Sind)));
1077 end Unit_Location;
1079 end Ch10;