2005-01-22 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / ada / gnat1drv.adb
blob0a31d559fae24c877d2c5accc1c8b0f6471bd186
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T 1 D R V --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2004 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 Back_End; use Back_End;
29 with Comperr;
30 with Csets; use Csets;
31 with Debug; use Debug;
32 with Elists;
33 with Errout; use Errout;
34 with Fmap;
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
37 with Frontend;
38 with Gnatvsn; use Gnatvsn;
39 with Hostparm;
40 with Inline;
41 with Lib; use Lib;
42 with Lib.Writ; use Lib.Writ;
43 with Lib.Xref;
44 with Namet; use Namet;
45 with Nlists;
46 with Opt; use Opt;
47 with Osint; use Osint;
48 with Output; use Output;
49 with Prepcomp;
50 with Repinfo; use Repinfo;
51 with Restrict;
52 with Sem;
53 with Sem_Ch8;
54 with Sem_Ch12;
55 with Sem_Ch13;
56 with Sem_Elim;
57 with Sem_Eval;
58 with Sem_Type;
59 with Sinfo; use Sinfo;
60 with Sinput.L; use Sinput.L;
61 with Snames;
62 with Sprint; use Sprint;
63 with Stringt;
64 with Targparm;
65 with Tree_Gen;
66 with Treepr; use Treepr;
67 with Ttypes;
68 with Types; use Types;
69 with Uintp; use Uintp;
70 with Uname; use Uname;
71 with Urealp;
72 with Usage;
74 with System.Assertions;
76 procedure Gnat1drv is
77 Main_Unit_Node : Node_Id;
78 -- Compilation unit node for main unit
80 Main_Kind : Node_Kind;
81 -- Kind of main compilation unit node
83 Back_End_Mode : Back_End.Back_End_Mode_Type;
84 -- Record back end mode
86 begin
87 -- This inner block is set up to catch assertion errors and constraint
88 -- errors. Since the code for handling these errors can cause another
89 -- exception to be raised (namely Unrecoverable_Error), we need two
90 -- nested blocks, so that the outer one handles unrecoverable error.
92 begin
93 -- Lib.Initialize need to be called before Scan_Compiler_Arguments,
94 -- because it initialize a table that is filled by
95 -- Scan_Compiler_Arguments.
97 Osint.Initialize;
98 Fmap.Reset_Tables;
99 Lib.Initialize;
100 Lib.Xref.Initialize;
101 Scan_Compiler_Arguments;
102 Osint.Add_Default_Search_Dirs;
104 Nlists.Initialize;
105 Sinput.Initialize;
106 Sem.Initialize;
107 Csets.Initialize;
108 Uintp.Initialize;
109 Urealp.Initialize;
110 Errout.Initialize;
111 Namet.Initialize;
112 Snames.Initialize;
113 Stringt.Initialize;
114 Inline.Initialize;
115 Sem_Ch8.Initialize;
116 Sem_Ch12.Initialize;
117 Sem_Ch13.Initialize;
118 Sem_Elim.Initialize;
119 Sem_Eval.Initialize;
120 Sem_Type.Init_Interp_Tables;
122 -- Acquire target parameters from system.ads (source of package System)
124 declare
125 use Sinput;
127 S : Source_File_Index;
128 N : Name_Id;
130 begin
131 Name_Buffer (1 .. 10) := "system.ads";
132 Name_Len := 10;
133 N := Name_Find;
134 S := Load_Source_File (N);
136 if S = No_Source_File then
137 Write_Line
138 ("fatal error, run-time library not installed correctly");
139 Write_Line
140 ("cannot locate file system.ads");
141 raise Unrecoverable_Error;
143 -- Remember source index of system.ads (which was read successfully)
145 else
146 System_Source_File_Index := S;
147 end if;
149 Targparm.Get_Target_Parameters
150 (System_Text => Source_Text (S),
151 Source_First => Source_First (S),
152 Source_Last => Source_Last (S));
154 -- Acquire configuration pragma information from Targparm
156 Restrict.Restrictions := Targparm.Restrictions_On_Target;
157 end;
159 -- Set Configurable_Run_Time mode if system.ads flag set
161 if Targparm.Configurable_Run_Time_On_Target or Debug_Flag_YY then
162 Configurable_Run_Time_Mode := True;
163 end if;
165 -- Set -gnatR3m mode if debug flag A set
167 if Debug_Flag_AA then
168 Back_Annotate_Rep_Info := True;
169 List_Representation_Info := 1;
170 List_Representation_Info_Mechanisms := True;
171 end if;
173 -- Output copyright notice if full list mode
175 if (Verbose_Mode or Full_List)
176 and then (not Debug_Flag_7)
177 then
178 Write_Eol;
179 Write_Str ("GNAT ");
180 Write_Str (Gnat_Version_String);
181 Write_Eol;
182 Write_Str ("Copyright 1992-2004 Free Software Foundation, Inc.");
183 Write_Eol;
184 end if;
186 -- Before we do anything else, adjust certain global values for
187 -- debug switches which modify their normal natural settings.
189 if Debug_Flag_8 then
190 Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
191 end if;
193 if Debug_Flag_M then
194 Targparm.OpenVMS_On_Target := True;
195 Hostparm.OpenVMS := True;
196 end if;
198 if Debug_Flag_FF then
199 Targparm.Frontend_Layout_On_Target := True;
200 end if;
202 -- We take the default exception mechanism into account
204 if Targparm.ZCX_By_Default_On_Target then
205 if Targparm.GCC_ZCX_Support_On_Target then
206 Exception_Mechanism := Back_End_ZCX_Exceptions;
207 else
208 Exception_Mechanism := Front_End_ZCX_Exceptions;
209 end if;
210 end if;
212 -- We take the command line exception mechanism into account
214 if Opt.Zero_Cost_Exceptions_Set then
215 if Opt.Zero_Cost_Exceptions_Val = False then
216 Exception_Mechanism := Front_End_Setjmp_Longjmp_Exceptions;
218 elsif Debug_Flag_XX then
219 Exception_Mechanism := Front_End_ZCX_Exceptions;
221 elsif Targparm.GCC_ZCX_Support_On_Target then
222 Exception_Mechanism := Back_End_ZCX_Exceptions;
224 elsif Targparm.Front_End_ZCX_Support_On_Target then
225 Exception_Mechanism := Front_End_ZCX_Exceptions;
227 else
228 Osint.Fail
229 ("Zero Cost Exceptions not supported on this target");
230 end if;
231 end if;
233 -- Set proper status for overflow checks. We turn on overflow checks
234 -- if -gnatp was not specified, and either -gnato is set or the back
235 -- end takes care of overflow checks. Otherwise we suppress overflow
236 -- checks by default (since front end checks are expensive).
238 if not Opt.Suppress_Checks
239 and then (Opt.Enable_Overflow_Checks
240 or else
241 (Targparm.Backend_Divide_Checks_On_Target
243 Targparm.Backend_Overflow_Checks_On_Target))
244 then
245 Suppress_Options (Overflow_Check) := False;
246 else
247 Suppress_Options (Overflow_Check) := True;
248 end if;
250 -- Check we have exactly one source file, this happens only in
251 -- the case where the driver is called directly, it cannot happen
252 -- when gnat1 is invoked from gcc in the normal case.
254 if Osint.Number_Of_Files /= 1 then
255 Usage;
256 Write_Eol;
257 Osint.Fail ("you must provide one source file");
259 elsif Usage_Requested then
260 Usage;
261 end if;
263 Original_Operating_Mode := Operating_Mode;
264 Frontend;
265 Main_Unit_Node := Cunit (Main_Unit);
266 Main_Kind := Nkind (Unit (Main_Unit_Node));
268 -- Check for suspicious or incorrect body present if we are doing
269 -- semantic checking. We omit this check in syntax only mode, because
270 -- in that case we do not know if we need a body or not.
272 if Operating_Mode /= Check_Syntax
273 and then
274 ((Main_Kind = N_Package_Declaration
275 and then not Body_Required (Main_Unit_Node))
276 or else (Main_Kind = N_Generic_Package_Declaration
277 and then not Body_Required (Main_Unit_Node))
278 or else Main_Kind = N_Package_Renaming_Declaration
279 or else Main_Kind = N_Subprogram_Renaming_Declaration
280 or else Nkind (Original_Node (Unit (Main_Unit_Node)))
281 in N_Generic_Instantiation)
282 then
283 declare
284 Sname : Unit_Name_Type := Unit_Name (Main_Unit);
285 Src_Ind : Source_File_Index;
286 Fname : File_Name_Type;
288 procedure Bad_Body (Msg : String);
289 -- Issue message for bad body found
291 procedure Bad_Body (Msg : String) is
292 begin
293 Error_Msg_N (Msg, Main_Unit_Node);
294 Error_Msg_Name_1 := Fname;
295 Error_Msg_N
296 ("remove incorrect body in file{!", Main_Unit_Node);
297 end Bad_Body;
299 begin
300 Sname := Unit_Name (Main_Unit);
302 -- If we do not already have a body name, then get the body
303 -- name (but how can we have a body name here ???)
305 if not Is_Body_Name (Sname) then
306 Sname := Get_Body_Name (Sname);
307 end if;
309 Fname := Get_File_Name (Sname, Subunit => False);
310 Src_Ind := Load_Source_File (Fname);
312 -- Case where body is present and it is not a subunit. Exclude
313 -- the subunit case, because it has nothing to do with the
314 -- package we are compiling. It is illegal for a child unit
315 -- and a subunit with the same expanded name (RM 10.2(9)) to
316 -- appear together in a partition, but there is nothing to
317 -- stop a compilation environment from having both, and the
318 -- test here simply allows that. If there is an attempt to
319 -- include both in a partition, this is diagnosed at bind time.
320 -- In Ada 83 mode this is not a warning case.
322 -- Note: if weird file names are being used, we can have a
323 -- situation where the file name that supposedly contains a
324 -- body, in fact contains a spec, or we can't tell what it
325 -- contains. Skip the error message in these cases.
327 if Src_Ind /= No_Source_File
328 and then Get_Expected_Unit_Type (Fname) = Expect_Body
329 and then not Source_File_Is_Subunit (Src_Ind)
330 then
331 Error_Msg_Name_1 := Sname;
333 -- Ada 83 case of a package body being ignored. This is not
334 -- an error as far as the Ada 83 RM is concerned, but it is
335 -- almost certainly not what is wanted so output a warning.
336 -- Give this message only if there were no errors, since
337 -- otherwise it may be incorrect (we may have misinterpreted
338 -- a junk spec as not needing a body when it really does).
340 if Main_Kind = N_Package_Declaration
341 and then Ada_Version = Ada_83
342 and then Operating_Mode = Generate_Code
343 and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
344 and then not Compilation_Errors
345 then
346 Error_Msg_N
347 ("package % does not require a body?!", Main_Unit_Node);
348 Error_Msg_Name_1 := Fname;
349 Error_Msg_N
350 ("body in file{?! will be ignored", Main_Unit_Node);
352 -- Ada 95 cases of a body file present when no body is
353 -- permitted. This we consider to be an error.
355 else
356 -- For generic instantiations, we never allow a body
358 if Nkind (Original_Node (Unit (Main_Unit_Node)))
359 in N_Generic_Instantiation
360 then
361 Bad_Body
362 ("generic instantiation for % does not allow a body");
364 -- A library unit that is a renaming never allows a body
366 elsif Main_Kind in N_Renaming_Declaration then
367 Bad_Body
368 ("renaming declaration for % does not allow a body!");
370 -- Remaining cases are packages and generic packages.
371 -- Here we only do the test if there are no previous
372 -- errors, because if there are errors, they may lead
373 -- us to incorrectly believe that a package does not
374 -- allow a body when in fact it does.
376 elsif not Compilation_Errors then
377 if Main_Kind = N_Package_Declaration then
378 Bad_Body ("package % does not allow a body!");
380 elsif Main_Kind = N_Generic_Package_Declaration then
381 Bad_Body ("generic package % does not allow a body!");
382 end if;
383 end if;
385 end if;
386 end if;
387 end;
388 end if;
390 -- Exit if compilation errors detected
392 if Compilation_Errors then
393 Treepr.Tree_Dump;
394 Sem_Ch13.Validate_Unchecked_Conversions;
395 Errout.Finalize;
396 Namet.Finalize;
398 -- Generate ALI file if specially requested
400 if Opt.Force_ALI_Tree_File then
401 Write_ALI (Object => False);
402 Tree_Gen;
403 end if;
405 Exit_Program (E_Errors);
406 end if;
408 -- Set Generate_Code on main unit and its spec. We do this even if
409 -- are not generating code, since Lib-Writ uses this to determine
410 -- which units get written in the ali file.
412 Set_Generate_Code (Main_Unit);
414 -- If we have a corresponding spec, then we need object
415 -- code for the spec unit as well
417 if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
418 and then not Acts_As_Spec (Main_Unit_Node)
419 then
420 Set_Generate_Code
421 (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
422 end if;
424 -- Case of no code required to be generated, exit indicating no error
426 if Original_Operating_Mode = Check_Syntax then
427 Treepr.Tree_Dump;
428 Errout.Finalize;
429 Tree_Gen;
430 Namet.Finalize;
431 Exit_Program (E_Success);
433 elsif Original_Operating_Mode = Check_Semantics then
434 Back_End_Mode := Declarations_Only;
436 -- All remaining cases are cases in which the user requested that code
437 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if
438 -- we can in fact satisfy this request.
440 -- Cannot generate code if someone has turned off code generation
441 -- for any reason at all. We will try to figure out a reason below.
443 elsif Operating_Mode /= Generate_Code then
444 Back_End_Mode := Skip;
446 -- We can generate code for a subprogram body unless there were
447 -- missing subunits. Note that we always generate code for all
448 -- generic units (a change from some previous versions of GNAT).
450 elsif Main_Kind = N_Subprogram_Body
451 and then not Subunits_Missing
452 then
453 Back_End_Mode := Generate_Object;
455 -- We can generate code for a package body unless there are subunits
456 -- missing (note that we always generate code for generic units, which
457 -- is a change from some earlier versions of GNAT).
459 elsif Main_Kind = N_Package_Body
460 and then not Subunits_Missing
461 then
462 Back_End_Mode := Generate_Object;
464 -- We can generate code for a package declaration or a subprogram
465 -- declaration only if it does not required a body.
467 elsif (Main_Kind = N_Package_Declaration
468 or else
469 Main_Kind = N_Subprogram_Declaration)
470 and then
471 (not Body_Required (Main_Unit_Node)
472 or else
473 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
474 then
475 Back_End_Mode := Generate_Object;
477 -- We can generate code for a generic package declaration of a generic
478 -- subprogram declaration only if does not require a body.
480 elsif (Main_Kind = N_Generic_Package_Declaration
481 or else
482 Main_Kind = N_Generic_Subprogram_Declaration)
483 and then not Body_Required (Main_Unit_Node)
484 then
485 Back_End_Mode := Generate_Object;
487 -- Compilation units that are renamings do not require bodies,
488 -- so we can generate code for them.
490 elsif Main_Kind = N_Package_Renaming_Declaration
491 or else Main_Kind = N_Subprogram_Renaming_Declaration
492 then
493 Back_End_Mode := Generate_Object;
495 -- Compilation units that are generic renamings do not require bodies
496 -- so we can generate code for them.
498 elsif Main_Kind in N_Generic_Renaming_Declaration then
499 Back_End_Mode := Generate_Object;
501 -- In all other cases (specs which have bodies, generics, and bodies
502 -- where subunits are missing), we cannot generate code and we generate
503 -- a warning message. Note that generic instantiations are gone at this
504 -- stage since they have been replaced by their instances.
506 else
507 Back_End_Mode := Skip;
508 end if;
510 -- At this stage Call_Back_End is set to indicate if the backend
511 -- should be called to generate code. If it is not set, then code
512 -- generation has been turned off, even though code was requested
513 -- by the original command. This is not an error from the user
514 -- point of view, but it is an error from the point of view of
515 -- the gcc driver, so we must exit with an error status.
517 -- We generate an informative message (from the gcc point of view,
518 -- it is an error message, but from the users point of view this
519 -- is not an error, just a consequence of compiling something that
520 -- cannot generate code).
522 if Back_End_Mode = Skip then
523 Write_Str ("cannot generate code for ");
524 Write_Str ("file ");
525 Write_Name (Unit_File_Name (Main_Unit));
527 if Subunits_Missing then
528 Write_Str (" (missing subunits)");
529 Write_Eol;
530 Write_Str ("to check parent unit");
532 elsif Main_Kind = N_Subunit then
533 Write_Str (" (subunit)");
534 Write_Eol;
535 Write_Str ("to check subunit");
537 elsif Main_Kind = N_Subprogram_Declaration then
538 Write_Str (" (subprogram spec)");
539 Write_Eol;
540 Write_Str ("to check subprogram spec");
542 -- Generic package body in GNAT implementation mode
544 elsif Main_Kind = N_Package_Body and then GNAT_Mode then
545 Write_Str (" (predefined generic)");
546 Write_Eol;
547 Write_Str ("to check predefined generic");
549 -- Only other case is a package spec
551 else
552 Write_Str (" (package spec)");
553 Write_Eol;
554 Write_Str ("to check package spec");
555 end if;
557 Write_Str (" for errors, use ");
559 if Hostparm.OpenVMS then
560 Write_Str ("/NOLOAD");
561 else
562 Write_Str ("-gnatc");
563 end if;
565 Write_Eol;
567 Sem_Ch13.Validate_Unchecked_Conversions;
568 Errout.Finalize;
569 Treepr.Tree_Dump;
570 Tree_Gen;
571 Write_ALI (Object => False);
572 Namet.Finalize;
574 -- Exit program with error indication, to kill object file
576 Exit_Program (E_No_Code);
577 end if;
579 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
580 -- set as indicated by Back_Annotate_Rep_Info being set to True.
582 -- We don't call for annotations on a subunit, because to process those
583 -- the back-end requires that the parent(s) be properly compiled.
585 -- Annotation is suppressed for targets where front-end layout is
586 -- enabled, because the front end determines representations.
588 -- Annotation is also suppressed in the case of compiling for
589 -- the Java VM, since representations are largely symbolic there.
591 if Back_End_Mode = Declarations_Only
592 and then (not Back_Annotate_Rep_Info
593 or else Main_Kind = N_Subunit
594 or else Targparm.Frontend_Layout_On_Target
595 or else Hostparm.Java_VM)
596 then
597 Sem_Ch13.Validate_Unchecked_Conversions;
598 Errout.Finalize;
599 Write_ALI (Object => False);
600 Tree_Dump;
601 Tree_Gen;
602 Namet.Finalize;
603 return;
604 end if;
606 -- Ensure that we properly register a dependency on system.ads,
607 -- since even if we do not semantically depend on this, Targparm
608 -- has read system parameters from the system.ads file.
610 Lib.Writ.Ensure_System_Dependency;
612 -- Add dependencies, if any, on preprocessing data file and on
613 -- preprocessing definition file(s).
615 Prepcomp.Add_Dependencies;
617 -- Back end needs to explicitly unlock tables it needs to touch
619 Atree.Lock;
620 Elists.Lock;
621 Fname.UF.Lock;
622 Inline.Lock;
623 Lib.Lock;
624 Nlists.Lock;
625 Sem.Lock;
626 Sinput.Lock;
627 Namet.Lock;
628 Stringt.Lock;
630 -- Here we call the back end to generate the output code
632 Back_End.Call_Back_End (Back_End_Mode);
634 -- Once the backend is complete, we unlock the names table. This
635 -- call allows a few extra entries, needed for example for the file
636 -- name for the library file output.
638 Namet.Unlock;
640 -- Validate unchecked conversions (using the values for size
641 -- and alignment annotated by the backend where possible).
643 Sem_Ch13.Validate_Unchecked_Conversions;
645 -- Now we complete output of errors, rep info and the tree info.
646 -- These are delayed till now, since it is perfectly possible for
647 -- gigi to generate errors, modify the tree (in particular by setting
648 -- flags indicating that elaboration is required, and also to back
649 -- annotate representation information for List_Rep_Info.
651 Errout.Finalize;
652 List_Rep_Info;
654 -- Only write the library if the backend did not generate any error
655 -- messages. Otherwise signal errors to the driver program so that
656 -- there will be no attempt to generate an object file.
658 if Compilation_Errors then
659 Treepr.Tree_Dump;
660 Exit_Program (E_Errors);
661 end if;
663 Write_ALI (Object => (Back_End_Mode = Generate_Object));
665 -- Generate the ASIS tree after writing the ALI file, since in
666 -- ASIS mode, Write_ALI may in fact result in further tree
667 -- decoration from the original tree file. Note that we dump
668 -- the tree just before generating it, so that the dump will
669 -- exactly reflect what is written out.
671 Treepr.Tree_Dump;
672 Tree_Gen;
674 -- Finalize name table and we are all done
676 Namet.Finalize;
678 exception
679 -- Handle fatal internal compiler errors
681 when System.Assertions.Assert_Failure =>
682 Comperr.Compiler_Abort ("Assert_Failure");
684 when Constraint_Error =>
685 Comperr.Compiler_Abort ("Constraint_Error");
687 when Program_Error =>
688 Comperr.Compiler_Abort ("Program_Error");
690 when Storage_Error =>
692 -- Assume this is a bug. If it is real, the message will in
693 -- any case say Storage_Error, giving a strong hint!
695 Comperr.Compiler_Abort ("Storage_Error");
696 end;
698 -- The outer exception handles an unrecoverable error
700 exception
701 when Unrecoverable_Error =>
702 Errout.Finalize;
704 Set_Standard_Error;
705 Write_Str ("compilation abandoned");
706 Write_Eol;
708 Set_Standard_Output;
709 Source_Dump;
710 Tree_Dump;
711 Exit_Program (E_Errors);
713 end Gnat1drv;