OSDN Git Service

2010-06-22 Ed Schonberg <schonberg@adacore.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-2010, 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 3,  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 COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Back_End; use Back_End;
28 with Comperr;
29 with Csets;    use Csets;
30 with Debug;    use Debug;
31 with Elists;
32 with Errout;   use Errout;
33 with Exp_CG;
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 Par_SCO;
50 with Prepcomp;
51 with Repinfo;  use Repinfo;
52 with Restrict;
53 with Rtsfind;
54 with SCOs;
55 with Sem;
56 with Sem_Ch8;
57 with Sem_Ch12;
58 with Sem_Ch13;
59 with Sem_Elim;
60 with Sem_Eval;
61 with Sem_Type;
62 with Sinfo;    use Sinfo;
63 with Sinput.L; use Sinput.L;
64 with Snames;
65 with Sprint;   use Sprint;
66 with Stringt;
67 with Stylesw;  use Stylesw;
68 with Targparm; use Targparm;
69 with Tree_Gen;
70 with Treepr;   use Treepr;
71 with Ttypes;
72 with Types;    use Types;
73 with Uintp;    use Uintp;
74 with Uname;    use Uname;
75 with Urealp;
76 with Usage;
77 with Validsw;  use Validsw;
78
79 with System.Assertions;
80
81 procedure Gnat1drv is
82    Main_Unit_Node : Node_Id;
83    --  Compilation unit node for main unit
84
85    Main_Kind : Node_Kind;
86    --  Kind of main compilation unit node
87
88    Back_End_Mode : Back_End.Back_End_Mode_Type;
89    --  Record back end mode
90
91    procedure Adjust_Global_Switches;
92    --  There are various interactions between front end switch settings,
93    --  including debug switch settings and target dependent parameters.
94    --  This procedure takes care of properly handling these interactions.
95    --  We do it after scanning out all the switches, so that we are not
96    --  depending on the order in which switches appear.
97
98    procedure Check_Bad_Body;
99    --  Called to check if the unit we are compiling has a bad body
100
101    procedure Check_Rep_Info;
102    --  Called when we are not generating code, to check if -gnatR was requested
103    --  and if so, explain that we will not be honoring the request.
104
105    procedure Check_Library_Items;
106    --  For debugging -- checks the behavior of Walk_Library_Items
107    pragma Warnings (Off, Check_Library_Items);
108    --  In case the call below is commented out
109
110    ----------------------------
111    -- Adjust_Global_Switches --
112    ----------------------------
113
114    procedure Adjust_Global_Switches is
115    begin
116       --  Debug flag -gnatd.I is a synonym for Generate_SCIL and requires code
117       --  generation.
118
119       if Debug_Flag_Dot_II
120         and then Operating_Mode = Generate_Code
121       then
122          Generate_SCIL := True;
123       end if;
124
125       --  Set ASIS mode if -gnatt and -gnatc are set
126
127       if Operating_Mode = Check_Semantics and then Tree_Output then
128          ASIS_Mode := True;
129
130          --  Turn off inlining in ASIS mode, since ASIS cannot handle the extra
131          --  information in the trees caused by inlining being active.
132
133          --  More specifically, the tree seems to be malformed from the ASIS
134          --  point of view if -gnatc and -gnatn appear together???
135
136          Inline_Active := False;
137
138          --  Turn off SCIL generation in ASIS mode, since SCIL requires front-
139          --  end expansion.
140
141          Generate_SCIL := False;
142       end if;
143
144       --  SCIL mode needs to disable front-end inlining since the generated
145       --  trees (in particular order and consistency between specs compiled
146       --  as part of a main unit or as part of a with-clause) are causing
147       --  troubles.
148
149       if Generate_SCIL then
150          Front_End_Inlining := False;
151       end if;
152
153       --  Tune settings for optimal SCIL generation in CodePeer_Mode
154
155       if CodePeer_Mode then
156
157          --  Turn off inlining, confuses CodePeer output and gains nothing
158
159          Front_End_Inlining := False;
160          Inline_Active      := False;
161
162          --  Turn off ASIS mode: incompatible with front-end expansion
163
164          ASIS_Mode := False;
165
166          --  Disable front-end optimizations, to keep the tree as close to the
167          --  source code as possible, and also to avoid inconsistencies between
168          --  trees when using different optimization switches.
169
170          Optimization_Level := 0;
171
172          --  Disable specific expansions for Restrictions pragmas to avoid
173          --  tree inconsistencies between compilations with different pragmas
174          --  that will cause different SCIL files to be generated for the
175          --  same Ada spec.
176
177          Treat_Restrictions_As_Warnings := True;
178
179          --  Suppress overflow, division by zero and access checks since they
180          --  are handled implicitly by CodePeer.
181
182          --  Turn off dynamic elaboration checks: generates inconsistencies in
183          --  trees between specs compiled as part of a main unit or as part of
184          --  a with-clause.
185
186          --  Turn off alignment checks: these cannot be proved statically by
187          --  CodePeer and generate false positives.
188
189          --  Enable all other language checks
190
191          Suppress_Options :=
192            (Access_Check      => True,
193             Alignment_Check   => True,
194             Division_Check    => True,
195             Elaboration_Check => True,
196             Overflow_Check    => True,
197             others            => False);
198          Enable_Overflow_Checks := False;
199          Dynamic_Elaboration_Checks := False;
200
201          --  Kill debug of generated code, since it messes up sloc values
202
203          Debug_Generated_Code := False;
204
205          --  Turn cross-referencing on in case it was disabled (by e.g. -gnatD)
206          --  Do we really need to spend time generating xref in CodePeer
207          --  mode??? Consider setting Xref_Active to False.
208
209          Xref_Active := True;
210
211          --  Polling mode forced off, since it generates confusing junk
212
213          Polling_Required := False;
214
215          --  Set operating mode to Generate_Code to benefit from full
216          --  front-end expansion (e.g. generics).
217
218          Operating_Mode := Generate_Code;
219
220          --  We need SCIL generation of course
221
222          Generate_SCIL := True;
223
224          --  Enable assertions and debug pragmas, since they give CodePeer
225          --  valuable extra information.
226
227          Assertions_Enabled     := True;
228          Debug_Pragmas_Enabled  := True;
229
230          --  Suppress compiler warnings, since what we are interested in here
231          --  is what CodePeer can find out. Also disable all simple value
232          --  propagation. This is an optimization which is valuable for code
233          --  optimization, and also for generation of compiler warnings, but
234          --  these are being turned off anyway, and CodePeer understands
235          --  things more clearly if references are not optimized in this way.
236
237          Warning_Mode  := Suppress;
238          Debug_Flag_MM := True;
239
240          --  Set normal RM validity checking, and checking of IN OUT parameters
241          --  (this might give CodePeer more useful checks to analyze, to be
242          --  confirmed???). All other validity checking is turned off, since
243          --  this can generate very complex trees that only confuse CodePeer
244          --  and do not bring enough useful info.
245
246          Reset_Validity_Check_Options;
247          Validity_Check_Default       := True;
248          Validity_Check_In_Out_Params := True;
249          Validity_Check_In_Params     := True;
250
251          --  Turn off style check options since we are not interested in any
252          --  front-end warnings when we are getting CodePeer output.
253
254          Reset_Style_Check_Options;
255       end if;
256
257       --  Set Configurable_Run_Time mode if system.ads flag set
258
259       if Targparm.Configurable_Run_Time_On_Target or Debug_Flag_YY then
260          Configurable_Run_Time_Mode := True;
261       end if;
262
263       --  Set -gnatR3m mode if debug flag A set
264
265       if Debug_Flag_AA then
266          Back_Annotate_Rep_Info := True;
267          List_Representation_Info := 1;
268          List_Representation_Info_Mechanisms := True;
269       end if;
270
271       --  Force Target_Strict_Alignment true if debug flag -gnatd.a is set
272
273       if Debug_Flag_Dot_A then
274          Ttypes.Target_Strict_Alignment := True;
275       end if;
276
277       --  Disable static allocation of dispatch tables if -gnatd.t or if layout
278       --  is enabled. The front end's layout phase currently treats types that
279       --  have discriminant-dependent arrays as not being static even when a
280       --  discriminant constraint on the type is static, and this leads to
281       --  problems with subtypes of type Ada.Tags.Dispatch_Table_Wrapper. ???
282
283       if Debug_Flag_Dot_T or else Frontend_Layout_On_Target then
284          Static_Dispatch_Tables := False;
285       end if;
286
287       --  Flip endian mode if -gnatd8 set
288
289       if Debug_Flag_8 then
290          Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
291       end if;
292
293       --  Deal with forcing OpenVMS switches True if debug flag M is set, but
294       --  record the setting of Targparm.Open_VMS_On_Target in True_VMS_Target
295       --  before doing this, so we know if we are in real OpenVMS or not!
296
297       Opt.True_VMS_Target := Targparm.OpenVMS_On_Target;
298
299       if Debug_Flag_M then
300          Targparm.OpenVMS_On_Target := True;
301          Hostparm.OpenVMS := True;
302       end if;
303
304       --  Activate front end layout if debug flag -gnatdF is set
305
306       if Debug_Flag_FF then
307          Targparm.Frontend_Layout_On_Target := True;
308       end if;
309
310       --  Set and check exception mechnism
311
312       if Targparm.ZCX_By_Default_On_Target then
313          if Targparm.GCC_ZCX_Support_On_Target then
314             Exception_Mechanism := Back_End_Exceptions;
315          else
316             Osint.Fail ("Zero Cost Exceptions not supported on this target");
317          end if;
318       end if;
319
320       --  Set proper status for overflow checks. We turn on overflow checks
321       --  if -gnatp was not specified, and either -gnato is set or the back
322       --  end takes care of overflow checks. Otherwise we suppress overflow
323       --  checks by default (since front end checks are expensive).
324
325       if not Opt.Suppress_Checks
326         and then (Opt.Enable_Overflow_Checks
327                     or else
328                       (Targparm.Backend_Divide_Checks_On_Target
329                         and
330                        Targparm.Backend_Overflow_Checks_On_Target))
331       then
332          Suppress_Options (Overflow_Check) := False;
333       else
334          Suppress_Options (Overflow_Check) := True;
335       end if;
336
337       --  Set switch indicating if we can use N_Expression_With_Actions
338
339       --  Debug flag -gnatd.X decisively sets usage on
340
341       if Debug_Flag_Dot_XX then
342          Use_Expression_With_Actions := True;
343
344       --  Debug flag -gnatd.Y decisively sets usage off
345
346       elsif Debug_Flag_Dot_YY then
347          Use_Expression_With_Actions := False;
348
349       --  If no debug flags, usage off for SCIL
350
351       elsif Generate_SCIL then
352          Use_Expression_With_Actions := False;
353
354       --  Otherwise this feature is implemented, so we allow its use
355
356       else
357          Use_Expression_With_Actions := True;
358       end if;
359
360       --  Set switch indicating if back end can handle limited types, and
361       --  guarantee that no incorrect copies are made (e.g. in the context
362       --  of a conditional expression).
363
364       --  Debug flag -gnatd.L decisively sets usage on
365
366       if Debug_Flag_Dot_LL then
367          Back_End_Handles_Limited_Types := True;
368
369       --  If no debug flag, usage off for AAMP, VM, SCIL cases
370
371       elsif AAMP_On_Target
372         or else VM_Target /= No_VM
373         or else Generate_SCIL
374       then
375          Back_End_Handles_Limited_Types := False;
376
377       --  Otherwise normal gcc back end, for now still turn flag off by
378       --  default, since there are unresolved problems in the front end.
379
380       else
381          Back_End_Handles_Limited_Types := False;
382       end if;
383    end Adjust_Global_Switches;
384
385    --------------------
386    -- Check_Bad_Body --
387    --------------------
388
389    procedure Check_Bad_Body is
390       Sname   : Unit_Name_Type;
391       Src_Ind : Source_File_Index;
392       Fname   : File_Name_Type;
393
394       procedure Bad_Body_Error (Msg : String);
395       --  Issue message for bad body found
396
397       --------------------
398       -- Bad_Body_Error --
399       --------------------
400
401       procedure Bad_Body_Error (Msg : String) is
402       begin
403          Error_Msg_N (Msg, Main_Unit_Node);
404          Error_Msg_File_1 := Fname;
405          Error_Msg_N ("remove incorrect body in file{!", Main_Unit_Node);
406       end Bad_Body_Error;
407
408       --  Start of processing for Check_Bad_Body
409
410    begin
411       --  Nothing to do if we are only checking syntax, because we don't know
412       --  enough to know if we require or forbid a body in this case.
413
414       if Operating_Mode = Check_Syntax then
415          return;
416       end if;
417
418       --  Check for body not allowed
419
420       if (Main_Kind = N_Package_Declaration
421            and then not Body_Required (Main_Unit_Node))
422         or else (Main_Kind = N_Generic_Package_Declaration
423                   and then not Body_Required (Main_Unit_Node))
424         or else Main_Kind = N_Package_Renaming_Declaration
425         or else Main_Kind = N_Subprogram_Renaming_Declaration
426         or else Nkind (Original_Node (Unit (Main_Unit_Node)))
427                          in N_Generic_Instantiation
428       then
429          Sname := Unit_Name (Main_Unit);
430
431          --  If we do not already have a body name, then get the body name
432          --  (but how can we have a body name here ???)
433
434          if not Is_Body_Name (Sname) then
435             Sname := Get_Body_Name (Sname);
436          end if;
437
438          Fname := Get_File_Name (Sname, Subunit => False);
439          Src_Ind := Load_Source_File (Fname);
440
441          --  Case where body is present and it is not a subunit. Exclude
442          --  the subunit case, because it has nothing to do with the
443          --  package we are compiling. It is illegal for a child unit and a
444          --  subunit with the same expanded name (RM 10.2(9)) to appear
445          --  together in a partition, but there is nothing to stop a
446          --  compilation environment from having both, and the test here
447          --  simply allows that. If there is an attempt to include both in
448          --  a partition, this is diagnosed at bind time. In Ada 83 mode
449          --  this is not a warning case.
450
451          --  Note: if weird file names are being used, we can have
452          --  situation where the file name that supposedly contains body,
453          --  in fact contains a spec, or we can't tell what it contains.
454          --  Skip the error message in these cases.
455
456          --  Also ignore body that is nothing but pragma No_Body; (that's the
457          --  whole point of this pragma, to be used this way and to cause the
458          --  body file to be ignored in this context).
459
460          if Src_Ind /= No_Source_File
461            and then Get_Expected_Unit_Type (Fname) = Expect_Body
462            and then not Source_File_Is_Subunit (Src_Ind)
463            and then not Source_File_Is_No_Body (Src_Ind)
464          then
465             Errout.Finalize (Last_Call => False);
466
467             Error_Msg_Unit_1 := Sname;
468
469             --  Ada 83 case of a package body being ignored. This is not an
470             --  error as far as the Ada 83 RM is concerned, but it is almost
471             --  certainly not what is wanted so output a warning. Give this
472             --  message only if there were no errors, since otherwise it may
473             --  be incorrect (we may have misinterpreted a junk spec as not
474             --  needing a body when it really does).
475
476             if Main_Kind = N_Package_Declaration
477               and then Ada_Version = Ada_83
478               and then Operating_Mode = Generate_Code
479               and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
480               and then not Compilation_Errors
481             then
482                Error_Msg_N
483                  ("package $$ does not require a body?", Main_Unit_Node);
484                Error_Msg_File_1 := Fname;
485                Error_Msg_N ("body in file{? will be ignored", Main_Unit_Node);
486
487                --  Ada 95 cases of a body file present when no body is
488                --  permitted. This we consider to be an error.
489
490             else
491                --  For generic instantiations, we never allow a body
492
493                if Nkind (Original_Node (Unit (Main_Unit_Node)))
494                in N_Generic_Instantiation
495                then
496                   Bad_Body_Error
497                     ("generic instantiation for $$ does not allow a body");
498
499                   --  A library unit that is a renaming never allows a body
500
501                elsif Main_Kind in N_Renaming_Declaration then
502                   Bad_Body_Error
503                     ("renaming declaration for $$ does not allow a body!");
504
505                   --  Remaining cases are packages and generic packages. Here
506                   --  we only do the test if there are no previous errors,
507                   --  because if there are errors, they may lead us to
508                   --  incorrectly believe that a package does not allow a body
509                   --  when in fact it does.
510
511                elsif not Compilation_Errors then
512                   if Main_Kind = N_Package_Declaration then
513                      Bad_Body_Error
514                        ("package $$ does not allow a body!");
515
516                   elsif Main_Kind = N_Generic_Package_Declaration then
517                      Bad_Body_Error
518                        ("generic package $$ does not allow a body!");
519                   end if;
520                end if;
521
522             end if;
523          end if;
524       end if;
525    end Check_Bad_Body;
526
527    -------------------------
528    -- Check_Library_Items --
529    -------------------------
530
531    --  Walk_Library_Items has plenty of assertions, so all we need to do is
532    --  call it, just for these assertions, not actually doing anything else.
533
534    procedure Check_Library_Items is
535
536       procedure Action (Item : Node_Id);
537       --  Action passed to Walk_Library_Items to do nothing
538
539       ------------
540       -- Action --
541       ------------
542
543       procedure Action (Item : Node_Id) is
544       begin
545          null;
546       end Action;
547
548       procedure Walk is new Sem.Walk_Library_Items (Action);
549
550    --  Start of processing for Check_Library_Items
551
552    begin
553       Walk;
554    end Check_Library_Items;
555
556    --------------------
557    -- Check_Rep_Info --
558    --------------------
559
560    procedure Check_Rep_Info is
561    begin
562       if List_Representation_Info /= 0
563         or else List_Representation_Info_Mechanisms
564       then
565          Set_Standard_Error;
566          Write_Eol;
567          Write_Str
568            ("cannot generate representation information, no code generated");
569          Write_Eol;
570          Write_Eol;
571          Set_Standard_Output;
572       end if;
573    end Check_Rep_Info;
574
575 --  Start of processing for Gnat1drv
576
577 begin
578    --  This inner block is set up to catch assertion errors and constraint
579    --  errors. Since the code for handling these errors can cause another
580    --  exception to be raised (namely Unrecoverable_Error), we need two
581    --  nested blocks, so that the outer one handles unrecoverable error.
582
583    begin
584       --  Initialize all packages. For the most part, these initialization
585       --  calls can be made in any order. Exceptions are as follows:
586
587       --  Lib.Initialize need to be called before Scan_Compiler_Arguments,
588       --  because it initializes a table filled by Scan_Compiler_Arguments.
589
590       Osint.Initialize;
591       Fmap.Reset_Tables;
592       Lib.Initialize;
593       Lib.Xref.Initialize;
594       Scan_Compiler_Arguments;
595       Osint.Add_Default_Search_Dirs;
596
597       Nlists.Initialize;
598       Sinput.Initialize;
599       Sem.Initialize;
600       Exp_CG.Initialize;
601       Csets.Initialize;
602       Uintp.Initialize;
603       Urealp.Initialize;
604       Errout.Initialize;
605       Namet.Initialize;
606       SCOs.Initialize;
607       Snames.Initialize;
608       Stringt.Initialize;
609       Inline.Initialize;
610       Par_SCO.Initialize;
611       Sem_Ch8.Initialize;
612       Sem_Ch12.Initialize;
613       Sem_Ch13.Initialize;
614       Sem_Elim.Initialize;
615       Sem_Eval.Initialize;
616       Sem_Type.Init_Interp_Tables;
617
618       --  Acquire target parameters from system.ads (source of package System)
619
620       declare
621          use Sinput;
622
623          S : Source_File_Index;
624          N : File_Name_Type;
625
626       begin
627          Name_Buffer (1 .. 10) := "system.ads";
628          Name_Len := 10;
629          N := Name_Find;
630          S := Load_Source_File (N);
631
632          if S = No_Source_File then
633             Write_Line
634               ("fatal error, run-time library not installed correctly");
635             Write_Line
636               ("cannot locate file system.ads");
637             raise Unrecoverable_Error;
638
639          --  Remember source index of system.ads (which was read successfully)
640
641          else
642             System_Source_File_Index := S;
643          end if;
644
645          Targparm.Get_Target_Parameters
646            (System_Text  => Source_Text  (S),
647             Source_First => Source_First (S),
648             Source_Last  => Source_Last  (S));
649
650          --  Acquire configuration pragma information from Targparm
651
652          Restrict.Restrictions := Targparm.Restrictions_On_Target;
653       end;
654
655       Adjust_Global_Switches;
656
657       --  Output copyright notice if full list mode unless we have a list
658       --  file, in which case we defer this so that it is output in the file
659
660       if (Verbose_Mode or else (Full_List and then Full_List_File_Name = null))
661         and then not Debug_Flag_7
662       then
663          Write_Eol;
664          Write_Str ("GNAT ");
665          Write_Str (Gnat_Version_String);
666          Write_Eol;
667          Write_Str ("Copyright 1992-" &
668                     Current_Year &
669                     ", Free Software Foundation, Inc.");
670          Write_Eol;
671       end if;
672
673       --  Check we do not have more than one source file, this happens only in
674       --  the case where the driver is called directly, it cannot happen when
675       --  gnat1 is invoked from gcc in the normal case.
676
677       if Osint.Number_Of_Files /= 1 then
678          Usage;
679          Write_Eol;
680          Osint.Fail ("you must provide one source file");
681
682       elsif Usage_Requested then
683          Usage;
684       end if;
685
686       Original_Operating_Mode := Operating_Mode;
687       Frontend;
688
689       --  Exit with errors if the main source could not be parsed
690
691       if Sinput.Main_Source_File = No_Source_File then
692          Errout.Finalize (Last_Call => True);
693          Errout.Output_Messages;
694          Exit_Program (E_Errors);
695       end if;
696
697       Main_Unit_Node := Cunit (Main_Unit);
698       Main_Kind := Nkind (Unit (Main_Unit_Node));
699       Check_Bad_Body;
700
701       --  Exit if compilation errors detected
702
703       Errout.Finalize (Last_Call => False);
704
705       if Compilation_Errors then
706          Treepr.Tree_Dump;
707          Sem_Ch13.Validate_Unchecked_Conversions;
708          Sem_Ch13.Validate_Address_Clauses;
709          Errout.Output_Messages;
710          Namet.Finalize;
711
712          --  Generate ALI file if specially requested
713
714          if Opt.Force_ALI_Tree_File then
715             Write_ALI (Object => False);
716             Tree_Gen;
717          end if;
718
719          Errout.Finalize (Last_Call => True);
720          Exit_Program (E_Errors);
721       end if;
722
723       --  Set Generate_Code on main unit and its spec. We do this even if are
724       --  not generating code, since Lib-Writ uses this to determine which
725       --  units get written in the ali file.
726
727       Set_Generate_Code (Main_Unit);
728
729       --  If we have a corresponding spec, and it comes from source
730       --  or it is not a generated spec for a child subprogram body,
731       --  then we need object code for the spec unit as well.
732
733       if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
734         and then not Acts_As_Spec (Main_Unit_Node)
735       then
736          if Nkind (Unit (Main_Unit_Node)) = N_Subprogram_Body
737            and then not Comes_From_Source (Library_Unit (Main_Unit_Node))
738          then
739             null;
740          else
741             Set_Generate_Code
742               (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
743          end if;
744       end if;
745
746       --  Case of no code required to be generated, exit indicating no error
747
748       if Original_Operating_Mode = Check_Syntax then
749          Treepr.Tree_Dump;
750          Errout.Finalize (Last_Call => True);
751          Errout.Output_Messages;
752          Tree_Gen;
753          Namet.Finalize;
754          Check_Rep_Info;
755
756          --  Use a goto instead of calling Exit_Program so that finalization
757          --  occurs normally.
758
759          goto End_Of_Program;
760
761       elsif Original_Operating_Mode = Check_Semantics then
762          Back_End_Mode := Declarations_Only;
763
764       --  All remaining cases are cases in which the user requested that code
765       --  be generated (i.e. no -gnatc or -gnats switch was used). Check if
766       --  we can in fact satisfy this request.
767
768       --  Cannot generate code if someone has turned off code generation for
769       --  any reason at all. We will try to figure out a reason below.
770
771       elsif Operating_Mode /= Generate_Code then
772          Back_End_Mode := Skip;
773
774       --  We can generate code for a subprogram body unless there were missing
775       --  subunits. Note that we always generate code for all generic units (a
776       --  change from some previous versions of GNAT).
777
778       elsif Main_Kind = N_Subprogram_Body
779         and then not Subunits_Missing
780       then
781          Back_End_Mode := Generate_Object;
782
783       --  We can generate code for a package body unless there are subunits
784       --  missing (note that we always generate code for generic units, which
785       --  is a change from some earlier versions of GNAT).
786
787       elsif Main_Kind = N_Package_Body
788         and then not Subunits_Missing
789       then
790          Back_End_Mode := Generate_Object;
791
792       --  We can generate code for a package declaration or a subprogram
793       --  declaration only if it does not required a body.
794
795       elsif (Main_Kind = N_Package_Declaration
796                or else
797              Main_Kind = N_Subprogram_Declaration)
798         and then
799           (not Body_Required (Main_Unit_Node)
800              or else
801            Distribution_Stub_Mode = Generate_Caller_Stub_Body)
802       then
803          Back_End_Mode := Generate_Object;
804
805       --  We can generate code for a generic package declaration of a generic
806       --  subprogram declaration only if does not require a body.
807
808       elsif (Main_Kind = N_Generic_Package_Declaration
809                or else
810              Main_Kind = N_Generic_Subprogram_Declaration)
811         and then not Body_Required (Main_Unit_Node)
812       then
813          Back_End_Mode := Generate_Object;
814
815       --  Compilation units that are renamings do not require bodies,
816       --  so we can generate code for them.
817
818       elsif Main_Kind = N_Package_Renaming_Declaration
819         or else Main_Kind = N_Subprogram_Renaming_Declaration
820       then
821          Back_End_Mode := Generate_Object;
822
823       --  Compilation units that are generic renamings do not require bodies
824       --  so we can generate code for them.
825
826       elsif Main_Kind in N_Generic_Renaming_Declaration then
827          Back_End_Mode := Generate_Object;
828
829       --  It's not an error to generate SCIL for e.g. a spec which has a body
830
831       elsif CodePeer_Mode then
832          Back_End_Mode := Generate_Object;
833
834       --  In all other cases (specs which have bodies, generics, and bodies
835       --  where subunits are missing), we cannot generate code and we generate
836       --  a warning message. Note that generic instantiations are gone at this
837       --  stage since they have been replaced by their instances.
838
839       else
840          Back_End_Mode := Skip;
841       end if;
842
843       --  At this stage Back_End_Mode is set to indicate if the backend should
844       --  be called to generate code. If it is Skip, then code generation has
845       --  been turned off, even though code was requested by the original
846       --  command. This is not an error from the user point of view, but it is
847       --  an error from the point of view of the gcc driver, so we must exit
848       --  with an error status.
849
850       --  We generate an informative message (from the gcc point of view, it
851       --  is an error message, but from the users point of view this is not an
852       --  error, just a consequence of compiling something that cannot
853       --  generate code).
854
855       if Back_End_Mode = Skip then
856          Set_Standard_Error;
857          Write_Str ("cannot generate code for ");
858          Write_Str ("file ");
859          Write_Name (Unit_File_Name (Main_Unit));
860
861          if Subunits_Missing then
862             Write_Str (" (missing subunits)");
863             Write_Eol;
864             Write_Str ("to check parent unit");
865
866          elsif Main_Kind = N_Subunit then
867             Write_Str (" (subunit)");
868             Write_Eol;
869             Write_Str ("to check subunit");
870
871          elsif Main_Kind = N_Subprogram_Declaration then
872             Write_Str (" (subprogram spec)");
873             Write_Eol;
874             Write_Str ("to check subprogram spec");
875
876          --  Generic package body in GNAT implementation mode
877
878          elsif Main_Kind = N_Package_Body and then GNAT_Mode then
879             Write_Str (" (predefined generic)");
880             Write_Eol;
881             Write_Str ("to check predefined generic");
882
883          --  Only other case is a package spec
884
885          else
886             Write_Str (" (package spec)");
887             Write_Eol;
888             Write_Str ("to check package spec");
889          end if;
890
891          Write_Str (" for errors, use ");
892
893          if Hostparm.OpenVMS then
894             Write_Str ("/NOLOAD");
895          else
896             Write_Str ("-gnatc");
897          end if;
898
899          Write_Eol;
900          Set_Standard_Output;
901
902          Sem_Ch13.Validate_Unchecked_Conversions;
903          Sem_Ch13.Validate_Address_Clauses;
904          Errout.Finalize (Last_Call => True);
905          Errout.Output_Messages;
906          Treepr.Tree_Dump;
907          Tree_Gen;
908          Write_ALI (Object => False);
909          Namet.Finalize;
910          Check_Rep_Info;
911
912          --  Exit program with error indication, to kill object file
913
914          Exit_Program (E_No_Code);
915       end if;
916
917       --  In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
918       --  set as indicated by Back_Annotate_Rep_Info being set to True.
919
920       --  We don't call for annotations on a subunit, because to process those
921       --  the back-end requires that the parent(s) be properly compiled.
922
923       --  Annotation is suppressed for targets where front-end layout is
924       --  enabled, because the front end determines representations.
925
926       --  Annotation is also suppressed in the case of compiling for
927       --  a VM, since representations are largely symbolic there.
928
929       if Back_End_Mode = Declarations_Only
930         and then (not (Back_Annotate_Rep_Info or Generate_SCIL)
931                    or else Main_Kind = N_Subunit
932                    or else Targparm.Frontend_Layout_On_Target
933                    or else Targparm.VM_Target /= No_VM)
934       then
935          Sem_Ch13.Validate_Unchecked_Conversions;
936          Sem_Ch13.Validate_Address_Clauses;
937          Errout.Finalize (Last_Call => True);
938          Errout.Output_Messages;
939          Write_ALI (Object => False);
940          Tree_Dump;
941          Tree_Gen;
942          Namet.Finalize;
943          Check_Rep_Info;
944          return;
945       end if;
946
947       --  Ensure that we properly register a dependency on system.ads, since
948       --  even if we do not semantically depend on this, Targparm has read
949       --  system parameters from the system.ads file.
950
951       Lib.Writ.Ensure_System_Dependency;
952
953       --  Add dependencies, if any, on preprocessing data file and on
954       --  preprocessing definition file(s).
955
956       Prepcomp.Add_Dependencies;
957
958       --  Back end needs to explicitly unlock tables it needs to touch
959
960       Atree.Lock;
961       Elists.Lock;
962       Fname.UF.Lock;
963       Inline.Lock;
964       Lib.Lock;
965       Nlists.Lock;
966       Sem.Lock;
967       Sinput.Lock;
968       Namet.Lock;
969       Stringt.Lock;
970
971       --  ???Check_Library_Items under control of a debug flag, because it
972       --  currently does not work if the -gnatn switch (back end inlining) is
973       --  used.
974
975       if Debug_Flag_Dot_WW then
976          Check_Library_Items;
977       end if;
978
979       --  Here we call the back end to generate the output code
980
981       Generating_Code := True;
982       Back_End.Call_Back_End (Back_End_Mode);
983
984       --  Once the backend is complete, we unlock the names table. This call
985       --  allows a few extra entries, needed for example for the file name for
986       --  the library file output.
987
988       Namet.Unlock;
989
990       --  Generate the call-graph output of dispatching calls
991
992       Exp_CG.Generate_CG_Output;
993
994       --  Validate unchecked conversions (using the values for size and
995       --  alignment annotated by the backend where possible).
996
997       Sem_Ch13.Validate_Unchecked_Conversions;
998
999       --  Validate address clauses (again using alignment values annotated
1000       --  by the backend where possible).
1001
1002       Sem_Ch13.Validate_Address_Clauses;
1003
1004       --  Now we complete output of errors, rep info and the tree info. These
1005       --  are delayed till now, since it is perfectly possible for gigi to
1006       --  generate errors, modify the tree (in particular by setting flags
1007       --  indicating that elaboration is required, and also to back annotate
1008       --  representation information for List_Rep_Info.
1009
1010       Errout.Finalize (Last_Call => True);
1011       Errout.Output_Messages;
1012       List_Rep_Info;
1013
1014       --  Only write the library if the backend did not generate any error
1015       --  messages. Otherwise signal errors to the driver program so that
1016       --  there will be no attempt to generate an object file.
1017
1018       if Compilation_Errors then
1019          Treepr.Tree_Dump;
1020          Exit_Program (E_Errors);
1021       end if;
1022
1023       Write_ALI (Object => (Back_End_Mode = Generate_Object));
1024
1025       --  Generate the ASIS tree after writing the ALI file, since in ASIS
1026       --  mode, Write_ALI may in fact result in further tree decoration from
1027       --  the original tree file. Note that we dump the tree just before
1028       --  generating it, so that the dump will exactly reflect what is written
1029       --  out.
1030
1031       Treepr.Tree_Dump;
1032       Tree_Gen;
1033
1034       --  Finalize name table and we are all done
1035
1036       Namet.Finalize;
1037
1038    exception
1039       --  Handle fatal internal compiler errors
1040
1041       when Rtsfind.RE_Not_Available =>
1042          Comperr.Compiler_Abort ("RE_Not_Available");
1043
1044       when System.Assertions.Assert_Failure =>
1045          Comperr.Compiler_Abort ("Assert_Failure");
1046
1047       when Constraint_Error =>
1048          Comperr.Compiler_Abort ("Constraint_Error");
1049
1050       when Program_Error =>
1051          Comperr.Compiler_Abort ("Program_Error");
1052
1053       when Storage_Error =>
1054
1055          --  Assume this is a bug. If it is real, the message will in any case
1056          --  say Storage_Error, giving a strong hint!
1057
1058          Comperr.Compiler_Abort ("Storage_Error");
1059    end;
1060
1061    <<End_Of_Program>>
1062    null;
1063
1064    --  The outer exception handles an unrecoverable error
1065
1066 exception
1067    when Unrecoverable_Error =>
1068       Errout.Finalize (Last_Call => True);
1069       Errout.Output_Messages;
1070
1071       Set_Standard_Error;
1072       Write_Str ("compilation abandoned");
1073       Write_Eol;
1074
1075       Set_Standard_Output;
1076       Source_Dump;
1077       Tree_Dump;
1078       Exit_Program (E_Errors);
1079
1080 end Gnat1drv;