OSDN Git Service

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