OSDN Git Service

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