* c-decl.c (duplicate_decls): Conditionalize DECL_SAVED_TREE copy.
[official-gcc.git] / gcc / ada / gnat1drv.adb
blobafa04c6409a7d372467b54028f3226721e78c4a8
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T 1 D R V --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.129 $
10 -- --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 -- --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Back_End; use Back_End;
31 with Comperr;
32 with Csets; use Csets;
33 with Debug; use Debug;
34 with Elists;
35 with Errout; use Errout;
36 with Fname; use Fname;
37 with Fname.UF; use Fname.UF;
38 with Frontend;
39 with Gnatvsn; use Gnatvsn;
40 with Hostparm;
41 with Inline;
42 with Lib; use Lib;
43 with Lib.Writ; use Lib.Writ;
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 Repinfo; use Repinfo;
50 with Restrict; use Restrict;
51 with Sem;
52 with Sem_Ch13;
53 with Sem_Warn;
54 with Sinfo; use Sinfo;
55 with Sinput.L; use Sinput.L;
56 with Snames;
57 with Sprint; use Sprint;
58 with Stringt;
59 with Targparm;
60 with Tree_Gen;
61 with Treepr; use Treepr;
62 with Ttypes;
63 with Types; use Types;
64 with Uintp;
65 with Uname; use Uname;
66 with Urealp;
67 with Usage;
69 with System.Assertions;
71 procedure Gnat1drv is
72 Main_Unit_Node : Node_Id;
73 -- Compilation unit node for main unit
75 Main_Unit_Entity : Node_Id;
76 -- Compilation unit entity for main unit
78 Main_Kind : Node_Kind;
79 -- Kind of main compilation unit node.
81 Original_Operating_Mode : Operating_Mode_Type;
82 -- Save operating type specified by options
84 Back_End_Mode : Back_End.Back_End_Mode_Type;
85 -- Record back end mode
87 begin
88 -- This inner block is set up to catch assertion errors and constraint
89 -- errors. Since the code for handling these errors can cause another
90 -- exception to be raised (namely Unrecoverable_Error), we need two
91 -- nested blocks, so that the outer one handles unrecoverable error.
93 begin
94 Osint.Initialize (Compiler);
95 Scan_Compiler_Arguments;
96 Osint.Add_Default_Search_Dirs;
98 Sinput.Initialize;
99 Lib.Initialize;
100 Sem.Initialize;
101 Csets.Initialize;
102 Uintp.Initialize;
103 Urealp.Initialize;
104 Errout.Initialize;
105 Namet.Initialize;
106 Snames.Initialize;
107 Stringt.Initialize;
108 Inline.Initialize;
109 Sem_Ch13.Initialize;
111 -- Output copyright notice if full list mode
113 if (Verbose_Mode or Full_List)
114 and then (not Debug_Flag_7)
115 then
116 Write_Eol;
117 Write_Str ("GNAT ");
118 Write_Str (Gnat_Version_String);
119 Write_Str (" Copyright 1992-2001 Free Software Foundation, Inc.");
120 Write_Eol;
121 end if;
123 -- Acquire target parameters and perform required setup
125 Targparm.Get_Target_Parameters;
127 if Targparm.High_Integrity_Mode_On_Target then
128 Set_No_Run_Time_Mode;
129 end if;
131 -- Before we do anything else, adjust certain global values for
132 -- debug switches which modify their normal natural settings.
134 if Debug_Flag_8 then
135 Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
136 end if;
138 if Debug_Flag_M then
139 Targparm.OpenVMS_On_Target := True;
140 Hostparm.OpenVMS := True;
141 end if;
143 if Debug_Flag_FF then
144 Targparm.Frontend_Layout_On_Target := True;
145 end if;
147 -- We take the default exception mechanism into account
149 if Targparm.ZCX_By_Default_On_Target then
150 if Targparm.GCC_ZCX_Support_On_Target then
151 Exception_Mechanism := GCC_ZCX;
152 else
153 Exception_Mechanism := Front_End_ZCX;
154 end if;
155 end if;
157 -- We take the command line exception mechanism into account
159 if Opt.Zero_Cost_Exceptions_Set then
160 if Opt.Zero_Cost_Exceptions_Val = False then
161 Exception_Mechanism := Setjmp_Longjmp;
163 elsif Targparm.GCC_ZCX_Support_On_Target then
164 Exception_Mechanism := GCC_ZCX;
166 elsif Targparm.Front_End_ZCX_Support_On_Target
167 or else Debug_Flag_XX
168 then
169 Exception_Mechanism := Front_End_ZCX;
171 else
172 Osint.Fail
173 ("Zero Cost Exceptions not supported on this target");
174 end if;
175 end if;
177 -- Check we have exactly one source file, this happens only in
178 -- the case where the driver is called directly, it cannot happen
179 -- when gnat1 is invoked from gcc in the normal case.
181 if Osint.Number_Of_Files /= 1 then
182 Usage;
183 Write_Eol;
184 Osint.Fail ("you must provide one source file");
186 elsif Usage_Requested then
187 Usage;
188 end if;
190 Original_Operating_Mode := Operating_Mode;
191 Frontend;
192 Main_Unit_Node := Cunit (Main_Unit);
193 Main_Unit_Entity := Cunit_Entity (Main_Unit);
194 Main_Kind := Nkind (Unit (Main_Unit_Node));
196 -- Check for suspicious or incorrect body present if we are doing
197 -- semantic checking. We omit this check in syntax only mode, because
198 -- in that case we do not know if we need a body or not.
200 if Operating_Mode /= Check_Syntax
201 and then
202 ((Main_Kind = N_Package_Declaration
203 and then not Body_Required (Main_Unit_Node))
204 or else (Main_Kind = N_Generic_Package_Declaration
205 and then not Body_Required (Main_Unit_Node))
206 or else Main_Kind = N_Package_Renaming_Declaration
207 or else Main_Kind = N_Subprogram_Renaming_Declaration
208 or else Nkind (Original_Node (Unit (Main_Unit_Node)))
209 in N_Generic_Instantiation)
210 then
211 declare
212 Sname : Unit_Name_Type := Unit_Name (Main_Unit);
213 Src_Ind : Source_File_Index;
214 Fname : File_Name_Type;
216 procedure Bad_Body (Msg : String);
217 -- Issue message for bad body found
219 procedure Bad_Body (Msg : String) is
220 begin
221 Error_Msg_N (Msg, Main_Unit_Node);
222 Error_Msg_Name_1 := Fname;
223 Error_Msg_N
224 ("remove incorrect body in file{!", Main_Unit_Node);
225 end Bad_Body;
227 begin
228 Sname := Unit_Name (Main_Unit);
230 -- If we do not already have a body name, then get the body
231 -- name (but how can we have a body name here ???)
233 if not Is_Body_Name (Sname) then
234 Sname := Get_Body_Name (Sname);
235 end if;
237 Fname := Get_File_Name (Sname, Subunit => False);
238 Src_Ind := Load_Source_File (Fname);
240 -- Case where body is present and it is not a subunit. Exclude
241 -- the subunit case, because it has nothing to do with the
242 -- package we are compiling. It is illegal for a child unit
243 -- and a subunit with the same expanded name (RM 10.2(9)) to
244 -- appear together in a partition, but there is nothing to
245 -- stop a compilation environment from having both, and the
246 -- test here simply allows that. If there is an attempt to
247 -- include both in a partition, this is diagnosed at bind time.
248 -- In Ada 83 mode this is not a warning case.
250 if Src_Ind /= No_Source_File
251 and then not Source_File_Is_Subunit (Src_Ind)
252 then
253 Error_Msg_Name_1 := Sname;
255 -- Ada 83 case of a package body being ignored. This is not
256 -- an error as far as the Ada 83 RM is concerned, but it is
257 -- almost certainly not what is wanted so output a warning.
258 -- Give this message only if there were no errors, since
259 -- otherwise it may be incorrect (we may have misinterpreted
260 -- a junk spec as not needing a body when it really does).
262 if Main_Kind = N_Package_Declaration
263 and then Ada_83
264 and then Operating_Mode = Generate_Code
265 and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
266 and then not Compilation_Errors
267 then
268 Error_Msg_N
269 ("package % does not require a body?!", Main_Unit_Node);
270 Error_Msg_Name_1 := Fname;
271 Error_Msg_N
272 ("body in file{?! will be ignored", Main_Unit_Node);
274 -- Ada 95 cases of a body file present when no body is
275 -- permitted. This we consider to be an error.
277 else
278 -- For generic instantiations, we never allow a body
280 if Nkind (Original_Node (Unit (Main_Unit_Node)))
281 in N_Generic_Instantiation
282 then
283 Bad_Body
284 ("generic instantiation for % does not allow a body");
286 -- A library unit that is a renaming never allows a body
288 elsif Main_Kind in N_Renaming_Declaration then
289 Bad_Body
290 ("renaming declaration for % does not allow a body!");
292 -- Remaining cases are packages and generic packages.
293 -- Here we only do the test if there are no previous
294 -- errors, because if there are errors, they may lead
295 -- us to incorrectly believe that a package does not
296 -- allow a body when in fact it does.
298 elsif not Compilation_Errors then
299 if Main_Kind = N_Package_Declaration then
300 Bad_Body ("package % does not allow a body!");
302 elsif Main_Kind = N_Generic_Package_Declaration then
303 Bad_Body ("generic package % does not allow a body!");
304 end if;
305 end if;
307 end if;
308 end if;
309 end;
310 end if;
312 -- Exit if compilation errors detected
314 if Compilation_Errors then
315 Treepr.Tree_Dump;
316 Sem_Ch13.Validate_Unchecked_Conversions;
317 Errout.Finalize;
318 Namet.Finalize;
320 -- Generate ALI file if specially requested
322 if Opt.Force_ALI_Tree_File then
323 Write_ALI (Object => False);
324 Tree_Gen;
325 end if;
327 Exit_Program (E_Errors);
328 end if;
330 -- Check for unused with's. We do this whether or not code is generated
332 Sem_Warn.Check_Unused_Withs;
334 -- Set Generate_Code on main unit and its spec. We do this even if
335 -- are not generating code, since Lib-Writ uses this to determine
336 -- which units get written in the ali file.
338 Set_Generate_Code (Main_Unit);
340 -- If we have a corresponding spec, then we need object
341 -- code for the spec unit as well
343 if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
344 and then not Acts_As_Spec (Main_Unit_Node)
345 then
346 Set_Generate_Code
347 (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
348 end if;
350 -- Check for unused with's. We do this whether or not code is generated
352 Sem_Warn.Check_Unused_Withs;
354 -- Case of no code required to be generated, exit indicating no error
356 if Original_Operating_Mode = Check_Syntax then
357 Treepr.Tree_Dump;
358 Errout.Finalize;
359 Tree_Gen;
360 Namet.Finalize;
361 Exit_Program (E_Success);
363 elsif Original_Operating_Mode = Check_Semantics then
364 Back_End_Mode := Declarations_Only;
366 -- All remaining cases are cases in which the user requested that code
367 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if
368 -- we can in fact satisfy this request.
370 -- Cannot generate code if someone has turned off code generation
371 -- for any reason at all. We will try to figure out a reason below.
373 elsif Operating_Mode /= Generate_Code then
374 Back_End_Mode := Skip;
376 -- We can generate code for a subprogram body unless its corresponding
377 -- subprogram spec is a generic delaration. Note that the check for
378 -- No (Library_Unit) here is a defensive check that should not be
379 -- necessary, since the Library_Unit field should be set properly.
381 elsif Main_Kind = N_Subprogram_Body
382 and then not Subunits_Missing
383 and then (No (Library_Unit (Main_Unit_Node))
384 or else Nkind (Unit (Library_Unit (Main_Unit_Node))) /=
385 N_Generic_Subprogram_Declaration
386 or else Generic_Separately_Compiled (Main_Unit_Entity))
387 then
388 Back_End_Mode := Generate_Object;
390 -- We can generate code for a package body unless its corresponding
391 -- package spec is a generic declaration. As described above, the
392 -- check for No (LIbrary_Unit) is a defensive check.
394 elsif Main_Kind = N_Package_Body
395 and then not Subunits_Missing
396 and then (No (Library_Unit (Main_Unit_Node))
397 or else Nkind (Unit (Library_Unit (Main_Unit_Node))) /=
398 N_Generic_Package_Declaration
399 or else Generic_Separately_Compiled (Main_Unit_Entity))
401 then
402 Back_End_Mode := Generate_Object;
404 -- We can generate code for a package declaration or a subprogram
405 -- declaration only if it does not required a body.
407 elsif (Main_Kind = N_Package_Declaration
408 or else
409 Main_Kind = N_Subprogram_Declaration)
410 and then
411 (not Body_Required (Main_Unit_Node)
412 or else
413 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
414 then
415 Back_End_Mode := Generate_Object;
417 -- We can generate code for a generic package declaration of a generic
418 -- subprogram declaration only if does not require a body, and if it
419 -- is a generic that is separately compiled.
421 elsif (Main_Kind = N_Generic_Package_Declaration
422 or else
423 Main_Kind = N_Generic_Subprogram_Declaration)
424 and then not Body_Required (Main_Unit_Node)
425 and then Generic_Separately_Compiled (Main_Unit_Entity)
426 then
427 Back_End_Mode := Generate_Object;
429 -- Compilation units that are renamings do not require bodies,
430 -- so we can generate code for them.
432 elsif Main_Kind = N_Package_Renaming_Declaration
433 or else Main_Kind = N_Subprogram_Renaming_Declaration
434 then
435 Back_End_Mode := Generate_Object;
437 -- Compilation units that are generic renamings do not require bodies
438 -- so we can generate code for them in the separately compiled case
440 elsif Main_Kind in N_Generic_Renaming_Declaration
441 and then Generic_Separately_Compiled (Main_Unit_Entity)
442 then
443 Back_End_Mode := Generate_Object;
445 -- In all other cases (specs which have bodies, generics, and bodies
446 -- where subunits are missing), we cannot generate code and we generate
447 -- a warning message. Note that generic instantiations are gone at this
448 -- stage since they have been replaced by their instances.
450 else
451 Back_End_Mode := Skip;
452 end if;
454 -- At this stage Call_Back_End is set to indicate if the backend
455 -- should be called to generate code. If it is not set, then code
456 -- generation has been turned off, even though code was requested
457 -- by the original command. This is not an error from the user
458 -- point of view, but it is an error from the point of view of
459 -- the gcc driver, so we must exit with an error status.
461 -- We generate an informative message (from the gcc point of view,
462 -- it is an error message, but from the users point of view this
463 -- is not an error, just a consequence of compiling something that
464 -- cannot generate code).
466 if Back_End_Mode = Skip then
467 Write_Str ("No code generated for ");
468 Write_Str ("file ");
469 Write_Name (Unit_File_Name (Main_Unit));
471 if Subunits_Missing then
472 Write_Str (" (missing subunits)");
474 elsif Main_Kind = N_Subunit then
475 Write_Str (" (subunit)");
477 elsif Main_Kind = N_Package_Body
478 or else Main_Kind = N_Subprogram_Body
479 then
480 Write_Str (" (generic unit)");
482 elsif Main_Kind = N_Subprogram_Declaration then
483 Write_Str (" (subprogram spec)");
485 -- Only other case is a package spec
487 else
488 Write_Str (" (package spec)");
489 end if;
491 Write_Eol;
493 Sem_Ch13.Validate_Unchecked_Conversions;
494 Errout.Finalize;
495 Treepr.Tree_Dump;
496 Tree_Gen;
497 Write_ALI (Object => False);
498 Namet.Finalize;
500 -- Exit program with error indication, to kill object file
502 Exit_Program (E_No_Code);
503 end if;
505 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
506 -- set as indicated by Back_Annotate_Rep_Info being set to True.
508 -- We don't call for annotations on a subunit, because to process those
509 -- the back-end requires that the parent(s) be properly compiled.
511 -- Annotation is also suppressed in the case of compiling for
512 -- the Java VM, since representations are largely symbolic there.
514 if Back_End_Mode = Declarations_Only
515 and then (not (Back_Annotate_Rep_Info or Debug_Flag_AA)
516 or else Main_Kind = N_Subunit
517 or else Hostparm.Java_VM)
518 then
519 Sem_Ch13.Validate_Unchecked_Conversions;
520 Errout.Finalize;
521 Write_ALI (Object => False);
522 Tree_Dump;
523 Tree_Gen;
524 Namet.Finalize;
525 return;
526 end if;
528 -- Ensure that we properly register a dependency on system.ads,
529 -- since even if we do not semantically depend on this, Targparm
530 -- has read system parameters from the system.ads file.
532 Lib.Writ.Ensure_System_Dependency;
534 -- Back end needs to explicitly unlock tables it needs to touch
536 Atree.Lock;
537 Elists.Lock;
538 Fname.UF.Lock;
539 Inline.Lock;
540 Lib.Lock;
541 Nlists.Lock;
542 Sem.Lock;
543 Sinput.Lock;
544 Namet.Lock;
545 Stringt.Lock;
547 -- There are cases where the back end emits warnings, e.g. on objects
548 -- that are too large and will cause Storage_Error. If such a warning
549 -- appears in a generic context, then it is always appropriately
550 -- placed on the instance rather than the template, since gigi only
551 -- deals with generated code in instances (in particular the warning
552 -- for oversize objects clearly belongs on the instance).
554 Warn_On_Instance := True;
556 -- Here we call the backend to generate the output code
558 Back_End.Call_Back_End (Back_End_Mode);
560 -- Once the backend is complete, we unlock the names table. This
561 -- call allows a few extra entries, needed for example for the file
562 -- name for the library file output.
564 Namet.Unlock;
566 -- Validate unchecked conversions (using the values for size
567 -- and alignment annotated by the backend where possible).
569 Sem_Ch13.Validate_Unchecked_Conversions;
571 -- Now we complete output of errors, rep info and the tree info.
572 -- These are delayed till now, since it is perfectly possible for
573 -- gigi to generate errors, modify the tree (in particular by setting
574 -- flags indicating that elaboration is required, and also to back
575 -- annotate representation information for List_Rep_Info.
577 Errout.Finalize;
579 if Opt.List_Representation_Info /= 0 or else Debug_Flag_AA then
580 List_Rep_Info;
581 end if;
583 -- Only write the library if the backend did not generate any error
584 -- messages. Otherwise signal errors to the driver program so that
585 -- there will be no attempt to generate an object file.
587 if Compilation_Errors then
588 Treepr.Tree_Dump;
589 Exit_Program (E_Errors);
590 end if;
592 Write_ALI (Object => (Back_End_Mode = Generate_Object));
594 -- Generate the ASIS tree after writing the ALI file, since in
595 -- ASIS mode, Write_ALI may in fact result in further tree
596 -- decoration from the original tree file. Note that we dump
597 -- the tree just before generating it, so that the dump will
598 -- exactly reflect what is written out.
600 Treepr.Tree_Dump;
601 Tree_Gen;
603 -- Finalize name table and we are all done
605 Namet.Finalize;
607 exception
608 -- Handle fatal internal compiler errors
610 when System.Assertions.Assert_Failure =>
611 Comperr.Compiler_Abort ("Assert_Failure");
613 when Constraint_Error =>
614 Comperr.Compiler_Abort ("Constraint_Error");
616 when Program_Error =>
617 Comperr.Compiler_Abort ("Program_Error");
619 when Storage_Error =>
621 -- Assume this is a bug. If it is real, the message will in
622 -- any case say Storage_Error, giving a strong hint!
624 Comperr.Compiler_Abort ("Storage_Error");
625 end;
627 -- The outer exception handles an unrecoverable error
629 exception
630 when Unrecoverable_Error =>
631 Errout.Finalize;
633 Set_Standard_Error;
634 Write_Str ("compilation abandoned");
635 Write_Eol;
637 Set_Standard_Output;
638 Source_Dump;
639 Tree_Dump;
640 Exit_Program (E_Errors);
642 end Gnat1drv;