1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Back_End
; use Back_End
;
30 with Csets
; use Csets
;
31 with Debug
; use Debug
;
33 with Errout
; use Errout
;
35 with Fname
; use Fname
;
36 with Fname
.UF
; use Fname
.UF
;
38 with Gnatvsn
; use Gnatvsn
;
42 with Lib
.Writ
; use Lib
.Writ
;
44 with Namet
; use Namet
;
47 with Osint
; use Osint
;
48 with Output
; use Output
;
50 with Repinfo
; use Repinfo
;
59 with Sinfo
; use Sinfo
;
60 with Sinput
.L
; use Sinput
.L
;
62 with Sprint
; use Sprint
;
66 with Treepr
; use Treepr
;
68 with Types
; use Types
;
69 with Uintp
; use Uintp
;
70 with Uname
; use Uname
;
74 with System
.Assertions
;
77 Main_Unit_Node
: Node_Id
;
78 -- Compilation unit node for main unit
80 Main_Kind
: Node_Kind
;
81 -- Kind of main compilation unit node
83 Back_End_Mode
: Back_End
.Back_End_Mode_Type
;
84 -- Record back end mode
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.
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.
101 Scan_Compiler_Arguments
;
102 Osint
.Add_Default_Search_Dirs
;
120 Sem_Type
.Init_Interp_Tables
;
122 -- Acquire target parameters from system.ads (source of package System)
127 S
: Source_File_Index
;
131 Name_Buffer
(1 .. 10) := "system.ads";
134 S
:= Load_Source_File
(N
);
136 if S
= No_Source_File
then
138 ("fatal error, run-time library not installed correctly");
140 ("cannot locate file system.ads");
141 raise Unrecoverable_Error
;
143 -- Remember source index of system.ads (which was read successfully)
146 System_Source_File_Index
:= S
;
149 Targparm
.Get_Target_Parameters
150 (System_Text
=> Source_Text
(S
),
151 Source_First
=> Source_First
(S
),
152 Source_Last
=> Source_Last
(S
));
154 -- Acquire configuration pragma information from Targparm
156 Restrict
.Restrictions
:= Targparm
.Restrictions_On_Target
;
159 -- Set Configurable_Run_Time mode if system.ads flag set
161 if Targparm
.Configurable_Run_Time_On_Target
or Debug_Flag_YY
then
162 Configurable_Run_Time_Mode
:= True;
165 -- Set -gnatR3m mode if debug flag A set
167 if Debug_Flag_AA
then
168 Back_Annotate_Rep_Info
:= True;
169 List_Representation_Info
:= 1;
170 List_Representation_Info_Mechanisms
:= True;
173 -- Output copyright notice if full list mode
175 if (Verbose_Mode
or Full_List
)
176 and then (not Debug_Flag_7
)
180 Write_Str
(Gnat_Version_String
);
182 Write_Str
("Copyright 1992-2005 Free Software Foundation, Inc.");
186 -- Before we do anything else, adjust certain global values for
187 -- debug switches which modify their normal natural settings.
190 Ttypes
.Bytes_Big_Endian
:= not Ttypes
.Bytes_Big_Endian
;
194 Targparm
.OpenVMS_On_Target
:= True;
195 Hostparm
.OpenVMS
:= True;
198 if Debug_Flag_FF
then
199 Targparm
.Frontend_Layout_On_Target
:= True;
202 -- We take the default exception mechanism into account
204 if Targparm
.ZCX_By_Default_On_Target
then
205 if Targparm
.GCC_ZCX_Support_On_Target
then
206 Exception_Mechanism
:= Back_End_ZCX_Exceptions
;
208 Exception_Mechanism
:= Front_End_ZCX_Exceptions
;
212 -- We take the command line exception mechanism into account
214 if Opt
.Zero_Cost_Exceptions_Set
then
215 if Opt
.Zero_Cost_Exceptions_Val
= False then
216 Exception_Mechanism
:= Front_End_Setjmp_Longjmp_Exceptions
;
218 elsif Debug_Flag_XX
then
219 Exception_Mechanism
:= Front_End_ZCX_Exceptions
;
221 elsif Targparm
.GCC_ZCX_Support_On_Target
then
222 Exception_Mechanism
:= Back_End_ZCX_Exceptions
;
224 elsif Targparm
.Front_End_ZCX_Support_On_Target
then
225 Exception_Mechanism
:= Front_End_ZCX_Exceptions
;
229 ("Zero Cost Exceptions not supported on this target");
233 -- Set proper status for overflow checks. We turn on overflow checks
234 -- if -gnatp was not specified, and either -gnato is set or the back
235 -- end takes care of overflow checks. Otherwise we suppress overflow
236 -- checks by default (since front end checks are expensive).
238 if not Opt
.Suppress_Checks
239 and then (Opt
.Enable_Overflow_Checks
241 (Targparm
.Backend_Divide_Checks_On_Target
243 Targparm
.Backend_Overflow_Checks_On_Target
))
245 Suppress_Options
(Overflow_Check
) := False;
247 Suppress_Options
(Overflow_Check
) := True;
250 -- Check we have exactly one source file, this happens only in the case
251 -- where the driver is called directly, it cannot happen when gnat1 is
252 -- invoked from gcc in the normal case.
254 if Osint
.Number_Of_Files
/= 1 then
257 Osint
.Fail
("you must provide one source file");
259 elsif Usage_Requested
then
263 Original_Operating_Mode
:= Operating_Mode
;
265 Main_Unit_Node
:= Cunit
(Main_Unit
);
266 Main_Kind
:= Nkind
(Unit
(Main_Unit_Node
));
268 -- Check for suspicious or incorrect body present if we are doing
269 -- semantic checking. We omit this check in syntax only mode, because
270 -- in that case we do not know if we need a body or not.
272 if Operating_Mode
/= Check_Syntax
274 ((Main_Kind
= N_Package_Declaration
275 and then not Body_Required
(Main_Unit_Node
))
276 or else (Main_Kind
= N_Generic_Package_Declaration
277 and then not Body_Required
(Main_Unit_Node
))
278 or else Main_Kind
= N_Package_Renaming_Declaration
279 or else Main_Kind
= N_Subprogram_Renaming_Declaration
280 or else Nkind
(Original_Node
(Unit
(Main_Unit_Node
)))
281 in N_Generic_Instantiation
)
284 Sname
: Unit_Name_Type
:= Unit_Name
(Main_Unit
);
285 Src_Ind
: Source_File_Index
;
286 Fname
: File_Name_Type
;
288 procedure Bad_Body_Error
(Msg
: String);
289 -- Issue message for bad body found
295 procedure Bad_Body_Error
(Msg
: String) is
297 Error_Msg_N
(Msg
, Main_Unit_Node
);
298 Error_Msg_Name_1
:= Fname
;
300 ("remove incorrect body in file{!", Main_Unit_Node
);
303 -- Start of processing for Bad_Body
306 Sname
:= Unit_Name
(Main_Unit
);
308 -- If we do not already have a body name, then get the body name
309 -- (but how can we have a body name here ???)
311 if not Is_Body_Name
(Sname
) then
312 Sname
:= Get_Body_Name
(Sname
);
315 Fname
:= Get_File_Name
(Sname
, Subunit
=> False);
316 Src_Ind
:= Load_Source_File
(Fname
);
318 -- Case where body is present and it is not a subunit. Exclude
319 -- the subunit case, because it has nothing to do with the
320 -- package we are compiling. It is illegal for a child unit and a
321 -- subunit with the same expanded name (RM 10.2(9)) to appear
322 -- together in a partition, but there is nothing to stop a
323 -- compilation environment from having both, and the test here
324 -- simply allows that. If there is an attempt to include both in
325 -- a partition, this is diagnosed at bind time. In Ada 83 mode
326 -- this is not a warning case.
328 -- Note: if weird file names are being used, we can have
329 -- situation where the file name that supposedly contains body,
330 -- in fact contains a spec, or we can't tell what it contains.
331 -- Skip the error message in these cases.
333 if Src_Ind
/= No_Source_File
334 and then Get_Expected_Unit_Type
(Fname
) = Expect_Body
335 and then not Source_File_Is_Subunit
(Src_Ind
)
337 Error_Msg_Name_1
:= Sname
;
339 -- Ada 83 case of a package body being ignored. This is not an
340 -- error as far as the Ada 83 RM is concerned, but it is
341 -- almost certainly not what is wanted so output a warning.
342 -- Give this message only if there were no errors, since
343 -- otherwise it may be incorrect (we may have misinterpreted a
344 -- junk spec as not needing a body when it really does).
346 if Main_Kind
= N_Package_Declaration
347 and then Ada_Version
= Ada_83
348 and then Operating_Mode
= Generate_Code
349 and then Distribution_Stub_Mode
/= Generate_Caller_Stub_Body
350 and then not Compilation_Errors
353 ("package % does not require a body?!", Main_Unit_Node
);
354 Error_Msg_Name_1
:= Fname
;
356 ("body in file{?! will be ignored", Main_Unit_Node
);
358 -- Ada 95 cases of a body file present when no body is
359 -- permitted. This we consider to be an error.
362 -- For generic instantiations, we never allow a body
364 if Nkind
(Original_Node
(Unit
(Main_Unit_Node
)))
365 in N_Generic_Instantiation
368 ("generic instantiation for % does not allow a body");
370 -- A library unit that is a renaming never allows a body
372 elsif Main_Kind
in N_Renaming_Declaration
then
374 ("renaming declaration for % does not allow a body!");
376 -- Remaining cases are packages and generic packages. Here
377 -- we only do the test if there are no previous errors,
378 -- because if there are errors, they may lead us to
379 -- incorrectly believe that a package does not allow a body
380 -- when in fact it does.
382 elsif not Compilation_Errors
then
383 if Main_Kind
= N_Package_Declaration
then
385 ("package % does not allow a body!");
387 elsif Main_Kind
= N_Generic_Package_Declaration
then
389 ("generic package % does not allow a body!");
398 -- Exit if compilation errors detected
400 if Compilation_Errors
then
402 Sem_Ch13
.Validate_Unchecked_Conversions
;
406 -- Generate ALI file if specially requested
408 if Opt
.Force_ALI_Tree_File
then
409 Write_ALI
(Object
=> False);
413 Exit_Program
(E_Errors
);
416 -- Set Generate_Code on main unit and its spec. We do this even if are
417 -- not generating code, since Lib-Writ uses this to determine which
418 -- units get written in the ali file.
420 Set_Generate_Code
(Main_Unit
);
422 -- If we have a corresponding spec, then we need object
423 -- code for the spec unit as well
425 if Nkind
(Unit
(Main_Unit_Node
)) in N_Unit_Body
426 and then not Acts_As_Spec
(Main_Unit_Node
)
429 (Get_Cunit_Unit_Number
(Library_Unit
(Main_Unit_Node
)));
432 -- Case of no code required to be generated, exit indicating no error
434 if Original_Operating_Mode
= Check_Syntax
then
439 Exit_Program
(E_Success
);
441 elsif Original_Operating_Mode
= Check_Semantics
then
442 Back_End_Mode
:= Declarations_Only
;
444 -- All remaining cases are cases in which the user requested that code
445 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if
446 -- we can in fact satisfy this request.
448 -- Cannot generate code if someone has turned off code generation for
449 -- any reason at all. We will try to figure out a reason below.
451 elsif Operating_Mode
/= Generate_Code
then
452 Back_End_Mode
:= Skip
;
454 -- We can generate code for a subprogram body unless there were missing
455 -- subunits. Note that we always generate code for all generic units (a
456 -- change from some previous versions of GNAT).
458 elsif Main_Kind
= N_Subprogram_Body
459 and then not Subunits_Missing
461 Back_End_Mode
:= Generate_Object
;
463 -- We can generate code for a package body unless there are subunits
464 -- missing (note that we always generate code for generic units, which
465 -- is a change from some earlier versions of GNAT).
467 elsif Main_Kind
= N_Package_Body
468 and then not Subunits_Missing
470 Back_End_Mode
:= Generate_Object
;
472 -- We can generate code for a package declaration or a subprogram
473 -- declaration only if it does not required a body.
475 elsif (Main_Kind
= N_Package_Declaration
477 Main_Kind
= N_Subprogram_Declaration
)
479 (not Body_Required
(Main_Unit_Node
)
481 Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
483 Back_End_Mode
:= Generate_Object
;
485 -- We can generate code for a generic package declaration of a generic
486 -- subprogram declaration only if does not require a body.
488 elsif (Main_Kind
= N_Generic_Package_Declaration
490 Main_Kind
= N_Generic_Subprogram_Declaration
)
491 and then not Body_Required
(Main_Unit_Node
)
493 Back_End_Mode
:= Generate_Object
;
495 -- Compilation units that are renamings do not require bodies,
496 -- so we can generate code for them.
498 elsif Main_Kind
= N_Package_Renaming_Declaration
499 or else Main_Kind
= N_Subprogram_Renaming_Declaration
501 Back_End_Mode
:= Generate_Object
;
503 -- Compilation units that are generic renamings do not require bodies
504 -- so we can generate code for them.
506 elsif Main_Kind
in N_Generic_Renaming_Declaration
then
507 Back_End_Mode
:= Generate_Object
;
509 -- In all other cases (specs which have bodies, generics, and bodies
510 -- where subunits are missing), we cannot generate code and we generate
511 -- a warning message. Note that generic instantiations are gone at this
512 -- stage since they have been replaced by their instances.
515 Back_End_Mode
:= Skip
;
518 -- At this stage Call_Back_End is set to indicate if the backend should
519 -- be called to generate code. If it is not set, then code generation
520 -- has been turned off, even though code was requested by the original
521 -- command. This is not an error from the user point of view, but it is
522 -- an error from the point of view of the gcc driver, so we must exit
523 -- with an error status.
525 -- We generate an informative message (from the gcc point of view, it
526 -- is an error message, but from the users point of view this is not an
527 -- error, just a consequence of compiling something that cannot
530 if Back_End_Mode
= Skip
then
531 Write_Str
("cannot generate code for ");
533 Write_Name
(Unit_File_Name
(Main_Unit
));
535 if Subunits_Missing
then
536 Write_Str
(" (missing subunits)");
538 Write_Str
("to check parent unit");
540 elsif Main_Kind
= N_Subunit
then
541 Write_Str
(" (subunit)");
543 Write_Str
("to check subunit");
545 elsif Main_Kind
= N_Subprogram_Declaration
then
546 Write_Str
(" (subprogram spec)");
548 Write_Str
("to check subprogram spec");
550 -- Generic package body in GNAT implementation mode
552 elsif Main_Kind
= N_Package_Body
and then GNAT_Mode
then
553 Write_Str
(" (predefined generic)");
555 Write_Str
("to check predefined generic");
557 -- Only other case is a package spec
560 Write_Str
(" (package spec)");
562 Write_Str
("to check package spec");
565 Write_Str
(" for errors, use ");
567 if Hostparm
.OpenVMS
then
568 Write_Str
("/NOLOAD");
570 Write_Str
("-gnatc");
575 Sem_Ch13
.Validate_Unchecked_Conversions
;
579 Write_ALI
(Object
=> False);
582 -- Exit program with error indication, to kill object file
584 Exit_Program
(E_No_Code
);
587 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also
588 -- set as indicated by Back_Annotate_Rep_Info being set to True.
590 -- We don't call for annotations on a subunit, because to process those
591 -- the back-end requires that the parent(s) be properly compiled.
593 -- Annotation is suppressed for targets where front-end layout is
594 -- enabled, because the front end determines representations.
596 -- Annotation is also suppressed in the case of compiling for
597 -- the Java VM, since representations are largely symbolic there.
599 if Back_End_Mode
= Declarations_Only
600 and then (not Back_Annotate_Rep_Info
601 or else Main_Kind
= N_Subunit
602 or else Targparm
.Frontend_Layout_On_Target
603 or else Hostparm
.Java_VM
)
605 Sem_Ch13
.Validate_Unchecked_Conversions
;
607 Write_ALI
(Object
=> False);
614 -- Ensure that we properly register a dependency on system.ads, since
615 -- even if we do not semantically depend on this, Targparm has read
616 -- system parameters from the system.ads file.
618 Lib
.Writ
.Ensure_System_Dependency
;
620 -- Add dependencies, if any, on preprocessing data file and on
621 -- preprocessing definition file(s).
623 Prepcomp
.Add_Dependencies
;
625 -- Back end needs to explicitly unlock tables it needs to touch
638 -- Here we call the back end to generate the output code
640 Back_End
.Call_Back_End
(Back_End_Mode
);
642 -- Once the backend is complete, we unlock the names table. This call
643 -- allows a few extra entries, needed for example for the file name for
644 -- the library file output.
648 -- Validate unchecked conversions (using the values for size and
649 -- alignment annotated by the backend where possible).
651 Sem_Ch13
.Validate_Unchecked_Conversions
;
653 -- Now we complete output of errors, rep info and the tree info. These
654 -- are delayed till now, since it is perfectly possible for gigi to
655 -- generate errors, modify the tree (in particular by setting flags
656 -- indicating that elaboration is required, and also to back annotate
657 -- representation information for List_Rep_Info.
662 -- Only write the library if the backend did not generate any error
663 -- messages. Otherwise signal errors to the driver program so that
664 -- there will be no attempt to generate an object file.
666 if Compilation_Errors
then
668 Exit_Program
(E_Errors
);
671 Write_ALI
(Object
=> (Back_End_Mode
= Generate_Object
));
673 -- Generate the ASIS tree after writing the ALI file, since in ASIS
674 -- mode, Write_ALI may in fact result in further tree decoration from
675 -- the original tree file. Note that we dump the tree just before
676 -- generating it, so that the dump will exactly reflect what is written
682 -- Finalize name table and we are all done
687 -- Handle fatal internal compiler errors
689 when System
.Assertions
.Assert_Failure
=>
690 Comperr
.Compiler_Abort
("Assert_Failure");
692 when Constraint_Error
=>
693 Comperr
.Compiler_Abort
("Constraint_Error");
695 when Program_Error
=>
696 Comperr
.Compiler_Abort
("Program_Error");
698 when Storage_Error
=>
700 -- Assume this is a bug. If it is real, the message will in any case
701 -- say Storage_Error, giving a strong hint!
703 Comperr
.Compiler_Abort
("Storage_Error");
706 -- The outer exception handles an unrecoverable error
709 when Unrecoverable_Error
=>
713 Write_Str
("compilation abandoned");
719 Exit_Program
(E_Errors
);