OSDN Git Service

* 41intnam.ads, 42intnam.ads, 4aintnam.ads, 4cintnam.ads,
[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 --                            $Revision$
10 --                                                                          --
11 --          Copyright (C) 1992-2002 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 with Atree;    use Atree;
30 with Back_End; use Back_End;
31 with Comperr;
32 with Csets;    use Csets;
33 with Debug;    use Debug;
34 with Elists;
35 with Errout;   use Errout;
36 with Fname;    use Fname;
37 with Fname.UF; use Fname.UF;
38 with Frontend;
39 with Gnatvsn;  use Gnatvsn;
40 with Hostparm;
41 with Inline;
42 with Lib;      use Lib;
43 with Lib.Writ; use Lib.Writ;
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 Repinfo;  use Repinfo;
50 with Restrict; use Restrict;
51 with Sem;
52 with Sem_Ch13;
53 with Sinfo;    use Sinfo;
54 with Sinput.L; use Sinput.L;
55 with Snames;
56 with Sprint;   use Sprint;
57 with Stringt;
58 with Targparm;
59 with Tree_Gen;
60 with Treepr;   use Treepr;
61 with Ttypes;
62 with Types;    use Types;
63 with Uintp;
64 with Uname;    use Uname;
65 with Urealp;
66 with Usage;
67
68 with System.Assertions;
69
70 procedure Gnat1drv is
71    Main_Unit_Node : Node_Id;
72    --  Compilation unit node for main unit
73
74    Main_Unit_Entity : Node_Id;
75    --  Compilation unit entity for main unit
76
77    Main_Kind : Node_Kind;
78    --  Kind of main compilation unit node.
79
80    Original_Operating_Mode : Operating_Mode_Type;
81    --  Save operating type specified by options
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       Lib.Initialize;
98       Scan_Compiler_Arguments;
99       Osint.Add_Default_Search_Dirs;
100
101       Sinput.Initialize;
102       Sem.Initialize;
103       Csets.Initialize;
104       Uintp.Initialize;
105       Urealp.Initialize;
106       Errout.Initialize;
107       Namet.Initialize;
108       Snames.Initialize;
109       Stringt.Initialize;
110       Inline.Initialize;
111       Sem_Ch13.Initialize;
112
113       --  Acquire target parameters and perform required setup
114
115       Targparm.Get_Target_Parameters;
116
117       if Targparm.High_Integrity_Mode_On_Target then
118          Set_No_Run_Time_Mode;
119       end if;
120
121       --  Output copyright notice if full list mode
122
123       if (Verbose_Mode or Full_List)
124         and then (not Debug_Flag_7)
125       then
126          Write_Eol;
127          Write_Str ("GNAT ");
128
129          if Targparm.High_Integrity_Mode_On_Target then
130             Write_Str ("Pro High Integrity ");
131          end if;
132
133          Write_Str (Gnat_Version_String);
134          Write_Str (" Copyright 1992-2002 Free Software Foundation, Inc.");
135          Write_Eol;
136       end if;
137
138       --  Before we do anything else, adjust certain global values for
139       --  debug switches which modify their normal natural settings.
140
141       if Debug_Flag_8 then
142          Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
143       end if;
144
145       if Debug_Flag_M then
146          Targparm.OpenVMS_On_Target := True;
147          Hostparm.OpenVMS := True;
148       end if;
149
150       if Debug_Flag_FF then
151          Targparm.Frontend_Layout_On_Target := True;
152       end if;
153
154       --  We take the default exception mechanism into account
155
156       if Targparm.ZCX_By_Default_On_Target then
157          if Targparm.GCC_ZCX_Support_On_Target then
158             Exception_Mechanism := GCC_ZCX;
159          else
160             Exception_Mechanism := Front_End_ZCX;
161          end if;
162       end if;
163
164       --  We take the command line exception mechanism into account
165
166       if Opt.Zero_Cost_Exceptions_Set then
167          if Opt.Zero_Cost_Exceptions_Val = False then
168             Exception_Mechanism := Setjmp_Longjmp;
169
170          elsif Targparm.GCC_ZCX_Support_On_Target then
171             Exception_Mechanism := GCC_ZCX;
172
173          elsif Targparm.Front_End_ZCX_Support_On_Target
174            or else Debug_Flag_XX
175          then
176             Exception_Mechanism := Front_End_ZCX;
177
178          else
179             Osint.Fail
180               ("Zero Cost Exceptions not supported on this target");
181          end if;
182       end if;
183
184       --  Set proper status for overflow checks. We turn on overflow checks
185       --  if -gnatp was not specified, and either -gnato is set or the back
186       --  end takes care of overflow checks. Otherwise we suppress overflow
187       --  checks by default (since front end checks are expensive).
188
189       if not Opt.Suppress_Checks
190         and then (Opt.Enable_Overflow_Checks
191                     or else
192                       (Targparm.Backend_Divide_Checks_On_Target
193                         and
194                        Targparm.Backend_Overflow_Checks_On_Target))
195       then
196          Suppress_Options.Overflow_Checks := False;
197       else
198          Suppress_Options.Overflow_Checks := True;
199       end if;
200
201       --  Check we have exactly one source file, this happens only in
202       --  the case where the driver is called directly, it cannot happen
203       --  when gnat1 is invoked from gcc in the normal case.
204
205       if Osint.Number_Of_Files /= 1 then
206          Usage;
207          Write_Eol;
208          Osint.Fail ("you must provide one source file");
209
210       elsif Usage_Requested then
211          Usage;
212       end if;
213
214       Original_Operating_Mode := Operating_Mode;
215       Frontend;
216       Main_Unit_Node := Cunit (Main_Unit);
217       Main_Unit_Entity := Cunit_Entity (Main_Unit);
218       Main_Kind := Nkind (Unit (Main_Unit_Node));
219
220       --  Check for suspicious or incorrect body present if we are doing
221       --  semantic checking. We omit this check in syntax only mode, because
222       --  in that case we do not know if we need a body or not.
223
224       if Operating_Mode /= Check_Syntax
225         and then
226           ((Main_Kind = N_Package_Declaration
227              and then not Body_Required (Main_Unit_Node))
228            or else (Main_Kind = N_Generic_Package_Declaration
229                      and then not Body_Required (Main_Unit_Node))
230            or else Main_Kind = N_Package_Renaming_Declaration
231            or else Main_Kind = N_Subprogram_Renaming_Declaration
232            or else Nkind (Original_Node (Unit (Main_Unit_Node)))
233                            in N_Generic_Instantiation)
234       then
235          declare
236             Sname   : Unit_Name_Type := Unit_Name (Main_Unit);
237             Src_Ind : Source_File_Index;
238             Fname   : File_Name_Type;
239
240             procedure Bad_Body (Msg : String);
241             --  Issue message for bad body found
242
243             procedure Bad_Body (Msg : String) is
244             begin
245                Error_Msg_N (Msg, Main_Unit_Node);
246                Error_Msg_Name_1 := Fname;
247                Error_Msg_N
248                  ("remove incorrect body in file{!", Main_Unit_Node);
249             end Bad_Body;
250
251          begin
252             Sname := Unit_Name (Main_Unit);
253
254             --  If we do not already have a body name, then get the body
255             --  name (but how can we have a body name here ???)
256
257             if not Is_Body_Name (Sname) then
258                Sname := Get_Body_Name (Sname);
259             end if;
260
261             Fname := Get_File_Name (Sname, Subunit => False);
262             Src_Ind := Load_Source_File (Fname);
263
264             --  Case where body is present and it is not a subunit. Exclude
265             --  the subunit case, because it has nothing to do with the
266             --  package we are compiling. It is illegal for a child unit
267             --  and a subunit with the same expanded name (RM 10.2(9)) to
268             --  appear together in a partition, but there is nothing to
269             --  stop a compilation environment from having both, and the
270             --  test here simply allows that. If there is an attempt to
271             --  include both in a partition, this is diagnosed at bind time.
272             --  In Ada 83 mode this is not a warning case.
273
274             if Src_Ind /= No_Source_File
275               and then not Source_File_Is_Subunit (Src_Ind)
276             then
277                Error_Msg_Name_1 := Sname;
278
279                --  Ada 83 case of a package body being ignored. This is not
280                --  an error as far as the Ada 83 RM is concerned, but it is
281                --  almost certainly not what is wanted so output a warning.
282                --  Give this message only if there were no errors, since
283                --  otherwise it may be incorrect (we may have misinterpreted
284                --  a junk spec as not needing a body when it really does).
285
286                if Main_Kind = N_Package_Declaration
287                  and then Ada_83
288                  and then Operating_Mode = Generate_Code
289                  and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
290                  and then not Compilation_Errors
291                then
292                   Error_Msg_N
293                     ("package % does not require a body?!", Main_Unit_Node);
294                   Error_Msg_Name_1 := Fname;
295                   Error_Msg_N
296                     ("body in file{?! will be ignored", Main_Unit_Node);
297
298                --  Ada 95 cases of a body file present when no body is
299                --  permitted. This we consider to be an error.
300
301                else
302                   --  For generic instantiations, we never allow a body
303
304                   if Nkind (Original_Node (Unit (Main_Unit_Node)))
305                       in N_Generic_Instantiation
306                   then
307                      Bad_Body
308                        ("generic instantiation for % does not allow a body");
309
310                   --  A library unit that is a renaming never allows a body
311
312                   elsif Main_Kind in N_Renaming_Declaration then
313                      Bad_Body
314                        ("renaming declaration for % does not allow a body!");
315
316                   --  Remaining cases are packages and generic packages.
317                   --  Here we only do the test if there are no previous
318                   --  errors, because if there are errors, they may lead
319                   --  us to incorrectly believe that a package does not
320                   --  allow a body when in fact it does.
321
322                   elsif not Compilation_Errors then
323                      if Main_Kind = N_Package_Declaration then
324                         Bad_Body ("package % does not allow a body!");
325
326                      elsif Main_Kind = N_Generic_Package_Declaration then
327                         Bad_Body ("generic package % does not allow a body!");
328                      end if;
329                   end if;
330
331                end if;
332             end if;
333          end;
334       end if;
335
336       --  Exit if compilation errors detected
337
338       if Compilation_Errors then
339          Treepr.Tree_Dump;
340          Sem_Ch13.Validate_Unchecked_Conversions;
341          Errout.Finalize;
342          Namet.Finalize;
343
344          --  Generate ALI file if specially requested
345
346          if Opt.Force_ALI_Tree_File then
347             Write_ALI (Object => False);
348             Tree_Gen;
349          end if;
350
351          Exit_Program (E_Errors);
352       end if;
353
354       --  Set Generate_Code on main unit and its spec. We do this even if
355       --  are not generating code, since Lib-Writ uses this to determine
356       --  which units get written in the ali file.
357
358       Set_Generate_Code (Main_Unit);
359
360       --  If we have a corresponding spec, then we need object
361       --  code for the spec unit as well
362
363       if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
364         and then not Acts_As_Spec (Main_Unit_Node)
365       then
366          Set_Generate_Code
367            (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
368       end if;
369
370       --  Case of no code required to be generated, exit indicating no error
371
372       if Original_Operating_Mode = Check_Syntax then
373          Treepr.Tree_Dump;
374          Errout.Finalize;
375          Tree_Gen;
376          Namet.Finalize;
377          Exit_Program (E_Success);
378
379       elsif Original_Operating_Mode = Check_Semantics then
380          Back_End_Mode := Declarations_Only;
381
382       --  All remaining cases are cases in which the user requested that code
383       --  be generated (i.e. no -gnatc or -gnats switch was used). Check if
384       --  we can in fact satisfy this request.
385
386       --  Cannot generate code if someone has turned off code generation
387       --  for any reason at all. We will try to figure out a reason below.
388
389       elsif Operating_Mode /= Generate_Code then
390          Back_End_Mode := Skip;
391
392       --  We can generate code for a subprogram body unless its corresponding
393       --  subprogram spec is a generic delaration. Note that the check for
394       --  No (Library_Unit) here is a defensive check that should not be
395       --  necessary, since the Library_Unit field should be set properly.
396
397       elsif Main_Kind = N_Subprogram_Body
398         and then not Subunits_Missing
399         and then (No (Library_Unit (Main_Unit_Node))
400                    or else Nkind (Unit (Library_Unit (Main_Unit_Node))) /=
401                                           N_Generic_Subprogram_Declaration
402                    or else Generic_Separately_Compiled (Main_Unit_Entity))
403       then
404          Back_End_Mode := Generate_Object;
405
406       --  We can generate code for a package body unless its corresponding
407       --  package spec is a generic declaration. As described above, the
408       --  check for No (LIbrary_Unit) is a defensive check.
409
410       elsif Main_Kind = N_Package_Body
411         and then not Subunits_Missing
412         and then (No (Library_Unit (Main_Unit_Node))
413            or else Nkind (Unit (Library_Unit (Main_Unit_Node))) /=
414                       N_Generic_Package_Declaration
415            or else Generic_Separately_Compiled (Main_Unit_Entity))
416
417       then
418          Back_End_Mode := Generate_Object;
419
420       --  We can generate code for a package declaration or a subprogram
421       --  declaration only if it does not required a body.
422
423       elsif (Main_Kind = N_Package_Declaration
424                or else
425              Main_Kind = N_Subprogram_Declaration)
426         and then
427           (not Body_Required (Main_Unit_Node)
428              or else
429            Distribution_Stub_Mode = Generate_Caller_Stub_Body)
430       then
431          Back_End_Mode := Generate_Object;
432
433       --  We can generate code for a generic package declaration of a generic
434       --  subprogram declaration only if does not require a body, and if it
435       --  is a generic that is separately compiled.
436
437       elsif (Main_Kind = N_Generic_Package_Declaration
438                or else
439              Main_Kind = N_Generic_Subprogram_Declaration)
440         and then not Body_Required (Main_Unit_Node)
441         and then Generic_Separately_Compiled (Main_Unit_Entity)
442       then
443          Back_End_Mode := Generate_Object;
444
445       --  Compilation units that are renamings do not require bodies,
446       --  so we can generate code for them.
447
448       elsif Main_Kind = N_Package_Renaming_Declaration
449         or else Main_Kind = N_Subprogram_Renaming_Declaration
450       then
451          Back_End_Mode := Generate_Object;
452
453       --  Compilation units that are generic renamings do not require bodies
454       --  so we can generate code for them in the separately compiled case
455
456       elsif Main_Kind in N_Generic_Renaming_Declaration
457         and then Generic_Separately_Compiled (Main_Unit_Entity)
458       then
459          Back_End_Mode := Generate_Object;
460
461       --  In all other cases (specs which have bodies, generics, and bodies
462       --  where subunits are missing), we cannot generate code and we generate
463       --  a warning message. Note that generic instantiations are gone at this
464       --  stage since they have been replaced by their instances.
465
466       else
467          Back_End_Mode := Skip;
468       end if;
469
470       --  At this stage Call_Back_End is set to indicate if the backend
471       --  should be called to generate code. If it is not set, then code
472       --  generation has been turned off, even though code was requested
473       --  by the original command. This is not an error from the user
474       --  point of view, but it is an error from the point of view of
475       --  the gcc driver, so we must exit with an error status.
476
477       --  We generate an informative message (from the gcc point of view,
478       --  it is an error message, but from the users point of view this
479       --  is not an error, just a consequence of compiling something that
480       --  cannot generate code).
481
482       if Back_End_Mode = Skip then
483          Write_Str ("No code generated for ");
484          Write_Str ("file ");
485          Write_Name (Unit_File_Name (Main_Unit));
486
487          if Subunits_Missing then
488             Write_Str (" (missing subunits)");
489
490          elsif Main_Kind = N_Subunit then
491             Write_Str (" (subunit)");
492
493          elsif Main_Kind = N_Package_Body
494            or else Main_Kind = N_Subprogram_Body
495          then
496             Write_Str (" (generic unit)");
497
498          elsif Main_Kind = N_Subprogram_Declaration then
499             Write_Str (" (subprogram spec)");
500
501          --  Only other case is a package spec
502
503          else
504             Write_Str (" (package spec)");
505          end if;
506
507          Write_Eol;
508
509          Sem_Ch13.Validate_Unchecked_Conversions;
510          Errout.Finalize;
511          Treepr.Tree_Dump;
512          Tree_Gen;
513          Write_ALI (Object => False);
514          Namet.Finalize;
515
516          --  Exit program with error indication, to kill object file
517
518          Exit_Program (E_No_Code);
519       end if;
520
521       --  In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
522       --  set as indicated by Back_Annotate_Rep_Info being set to True.
523
524       --  We don't call for annotations on a subunit, because to process those
525       --  the back-end requires that the parent(s) be properly compiled.
526
527       --  Annotation is also suppressed in the case of compiling for
528       --  the Java VM, since representations are largely symbolic there.
529
530       if Back_End_Mode = Declarations_Only
531         and then (not (Back_Annotate_Rep_Info or Debug_Flag_AA)
532                    or else Main_Kind = N_Subunit
533                    or else Hostparm.Java_VM)
534       then
535          Sem_Ch13.Validate_Unchecked_Conversions;
536          Errout.Finalize;
537          Write_ALI (Object => False);
538          Tree_Dump;
539          Tree_Gen;
540          Namet.Finalize;
541          return;
542       end if;
543
544       --  Ensure that we properly register a dependency on system.ads,
545       --  since even if we do not semantically depend on this, Targparm
546       --  has read system parameters from the system.ads file.
547
548       Lib.Writ.Ensure_System_Dependency;
549
550       --  Back end needs to explicitly unlock tables it needs to touch
551
552       Atree.Lock;
553       Elists.Lock;
554       Fname.UF.Lock;
555       Inline.Lock;
556       Lib.Lock;
557       Nlists.Lock;
558       Sem.Lock;
559       Sinput.Lock;
560       Namet.Lock;
561       Stringt.Lock;
562
563       --  There are cases where the back end emits warnings, e.g. on objects
564       --  that are too large and will cause Storage_Error. If such a warning
565       --  appears in a generic context, then it is always appropriately
566       --  placed on the instance rather than the template, since gigi only
567       --  deals with generated code in instances (in particular the warning
568       --  for oversize objects clearly belongs on the instance).
569
570       Warn_On_Instance := True;
571
572       --  Here we call the backend to generate the output code
573
574       Back_End.Call_Back_End (Back_End_Mode);
575
576       --  Once the backend is complete, we unlock the names table. This
577       --  call allows a few extra entries, needed for example for the file
578       --  name for the library file output.
579
580       Namet.Unlock;
581
582       --  Validate unchecked conversions (using the values for size
583       --  and alignment annotated by the backend where possible).
584
585       Sem_Ch13.Validate_Unchecked_Conversions;
586
587       --  Now we complete output of errors, rep info and the tree info.
588       --  These are delayed till now, since it is perfectly possible for
589       --  gigi to generate errors, modify the tree (in particular by setting
590       --  flags indicating that elaboration is required, and also to back
591       --  annotate representation information for List_Rep_Info.
592
593       Errout.Finalize;
594
595       if Opt.List_Representation_Info /= 0 or else Debug_Flag_AA then
596          List_Rep_Info;
597       end if;
598
599       --  Only write the library if the backend did not generate any error
600       --  messages. Otherwise signal errors to the driver program so that
601       --  there will be no attempt to generate an object file.
602
603       if Compilation_Errors then
604          Treepr.Tree_Dump;
605          Exit_Program (E_Errors);
606       end if;
607
608       Write_ALI (Object => (Back_End_Mode = Generate_Object));
609
610       --  Generate the ASIS tree after writing the ALI file, since in
611       --  ASIS mode, Write_ALI may in fact result in further tree
612       --  decoration from the original tree file. Note that we dump
613       --  the tree just before generating it, so that the dump will
614       --  exactly reflect what is written out.
615
616       Treepr.Tree_Dump;
617       Tree_Gen;
618
619       --  Finalize name table and we are all done
620
621       Namet.Finalize;
622
623    exception
624       --  Handle fatal internal compiler errors
625
626       when System.Assertions.Assert_Failure =>
627          Comperr.Compiler_Abort ("Assert_Failure");
628
629       when Constraint_Error =>
630          Comperr.Compiler_Abort ("Constraint_Error");
631
632       when Program_Error =>
633          Comperr.Compiler_Abort ("Program_Error");
634
635       when Storage_Error =>
636
637          --  Assume this is a bug. If it is real, the message will in
638          --  any case say Storage_Error, giving a strong hint!
639
640          Comperr.Compiler_Abort ("Storage_Error");
641    end;
642
643 --  The outer exception handles an unrecoverable error
644
645 exception
646    when Unrecoverable_Error =>
647       Errout.Finalize;
648
649       Set_Standard_Error;
650       Write_Str ("compilation abandoned");
651       Write_Eol;
652
653       Set_Standard_Output;
654       Source_Dump;
655       Tree_Dump;
656       Exit_Program (E_Errors);
657
658 end Gnat1drv;