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