OSDN Git Service

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