OSDN Git Service

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