OSDN Git Service

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