1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Back_End
; use Back_End
;
30 with Csets
; use Csets
;
31 with Debug
; use Debug
;
33 with Errout
; use Errout
;
36 with Fname
; use Fname
;
37 with Fname
.UF
; use Fname
.UF
;
40 with Gnatvsn
; use Gnatvsn
;
43 with Lib
.Writ
; use Lib
.Writ
;
45 with Namet
; use Namet
;
48 with Osint
; use Osint
;
49 with Osint
.C
; use Osint
.C
;
50 with Output
; use Output
;
53 with Repinfo
; use Repinfo
;
55 with Rident
; use Rident
;
66 with Sinfo
; use Sinfo
;
67 with Sinput
.L
; use Sinput
.L
;
69 with Sprint
; use Sprint
;
71 with Stylesw
; use Stylesw
;
72 with Targparm
; use Targparm
;
75 with Treepr
; use Treepr
;
77 with Types
; use Types
;
78 with Uintp
; use Uintp
;
79 with Uname
; use Uname
;
82 with Validsw
; use Validsw
;
84 with System
.Assertions
;
92 Main_Unit_Node
: Node_Id
;
93 -- Compilation unit node for main unit
95 Main_Kind
: Node_Kind
;
96 -- Kind of main compilation unit node
98 Back_End_Mode
: Back_End
.Back_End_Mode_Type
;
99 -- Record back-end mode
101 procedure Adjust_Global_Switches
;
102 -- There are various interactions between front-end switch settings,
103 -- including debug switch settings and target dependent parameters.
104 -- This procedure takes care of properly handling these interactions.
105 -- We do it after scanning out all the switches, so that we are not
106 -- depending on the order in which switches appear.
108 procedure Check_Bad_Body
;
109 -- Called to check if the unit we are compiling has a bad body
111 procedure Check_Rep_Info
;
112 -- Called when we are not generating code, to check if -gnatR was requested
113 -- and if so, explain that we will not be honoring the request.
115 procedure Post_Compilation_Validation_Checks
;
116 -- This procedure performs various validation checks that have to be left
117 -- to the end of the compilation process, after generating code but before
118 -- issuing error messages. In particular, these checks generally require
119 -- the information provided by the back end in back annotation of declared
120 -- entities (e.g. actual size and alignment values chosen by the back end).
122 ----------------------------
123 -- Adjust_Global_Switches --
124 ----------------------------
126 procedure Adjust_Global_Switches
is
128 -- -gnatd.M enables Relaxed_RM_Semantics
130 if Debug_Flag_Dot_MM
then
131 Relaxed_RM_Semantics
:= True;
134 -- -gnatd.1 enables unnesting of subprograms
136 if Debug_Flag_Dot_1
then
137 Unnest_Subprogram_Mode
:= True;
140 -- -gnatd.u enables special C expansion mode
142 if Debug_Flag_Dot_U
then
143 Modify_Tree_For_C
:= True;
146 -- Set all flags required when generating C code
148 if Generate_C_Code
then
149 Modify_Tree_For_C
:= True;
150 Unnest_Subprogram_Mode
:= True;
151 Minimize_Expression_With_Actions
:= True;
153 -- Set operating mode to Generate_Code to benefit from full front-end
154 -- expansion (e.g. generics).
156 Operating_Mode
:= Generate_Code
;
158 -- Suppress alignment checks since we do not have access to alignment
159 -- info on the target.
161 Suppress_Options
.Suppress
(Alignment_Check
) := False;
164 -- -gnatd.E sets Error_To_Warning mode, causing selected error messages
165 -- to be treated as warnings instead of errors.
167 if Debug_Flag_Dot_EE
then
168 Error_To_Warning
:= True;
171 -- Disable CodePeer_Mode in Check_Syntax, since we need front-end
174 if Operating_Mode
= Check_Syntax
then
175 CodePeer_Mode
:= False;
178 -- Set ASIS mode if -gnatt and -gnatc are set
180 if Operating_Mode
= Check_Semantics
and then Tree_Output
then
183 -- Set ASIS GNSA mode if -gnatd.H is set
185 if Debug_Flag_Dot_HH
then
186 ASIS_GNSA_Mode
:= True;
189 -- Turn off inlining in ASIS mode, since ASIS cannot handle the extra
190 -- information in the trees caused by inlining being active.
192 -- More specifically, the tree seems to be malformed from the ASIS
193 -- point of view if -gnatc and -gnatn appear together???
195 Inline_Active
:= False;
197 -- Turn off SCIL generation and CodePeer mode in semantics mode,
198 -- since SCIL requires front-end expansion.
200 Generate_SCIL
:= False;
201 CodePeer_Mode
:= False;
204 -- SCIL mode needs to disable front-end inlining since the generated
205 -- trees (in particular order and consistency between specs compiled
206 -- as part of a main unit or as part of a with-clause) are causing
209 if Generate_SCIL
then
210 Front_End_Inlining
:= False;
213 -- Tune settings for optimal SCIL generation in CodePeer mode
215 if CodePeer_Mode
then
217 -- Turn off gnatprove mode (which can be set via e.g. -gnatd.F), not
218 -- compatible with CodePeer mode.
220 GNATprove_Mode
:= False;
221 Debug_Flag_Dot_FF
:= False;
223 -- Turn off C tree generation, not compatible with CodePeer mode. We
224 -- do not expect this to happen in normal use, since both modes are
225 -- enabled by special tools, but it is useful to turn off these flags
226 -- this way when we are doing CodePeer tests on existing test suites
227 -- that may have -gnateg set, to avoid the need for special casing.
229 Modify_Tree_For_C
:= False;
230 Generate_C_Code
:= False;
231 Unnest_Subprogram_Mode
:= False;
233 -- Turn off inlining, confuses CodePeer output and gains nothing
235 Front_End_Inlining
:= False;
236 Inline_Active
:= False;
238 -- Disable front-end optimizations, to keep the tree as close to the
239 -- source code as possible, and also to avoid inconsistencies between
240 -- trees when using different optimization switches.
242 Optimization_Level
:= 0;
244 -- Enable some restrictions systematically to simplify the generated
245 -- code (and ease analysis). Note that restriction checks are also
246 -- disabled in CodePeer mode, see Restrict.Check_Restriction, and
247 -- user specified Restrictions pragmas are ignored, see
248 -- Sem_Prag.Process_Restrictions_Or_Restriction_Warnings.
250 Restrict
.Restrictions
.Set
(No_Exception_Registration
) := True;
251 Restrict
.Restrictions
.Set
(No_Initialize_Scalars
) := True;
252 Restrict
.Restrictions
.Set
(No_Task_Hierarchy
) := True;
253 Restrict
.Restrictions
.Set
(No_Abort_Statements
) := True;
254 Restrict
.Restrictions
.Set
(Max_Asynchronous_Select_Nesting
) := True;
255 Restrict
.Restrictions
.Value
(Max_Asynchronous_Select_Nesting
) := 0;
257 -- Suppress division by zero and access checks since they are handled
258 -- implicitly by CodePeer.
260 -- Turn off dynamic elaboration checks: generates inconsistencies in
261 -- trees between specs compiled as part of a main unit or as part of
264 -- Turn off alignment checks: these cannot be proved statically by
265 -- CodePeer and generate false positives.
267 -- Enable all other language checks
269 Suppress_Options
.Suppress
:=
270 (Alignment_Check
=> True,
271 Division_Check
=> True,
272 Elaboration_Check
=> True,
275 Dynamic_Elaboration_Checks
:= False;
277 -- Set STRICT mode for overflow checks if not set explicitly. This
278 -- prevents suppressing of overflow checks by default, in code down
281 if Suppress_Options
.Overflow_Mode_General
= Not_Set
then
282 Suppress_Options
.Overflow_Mode_General
:= Strict
;
283 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
286 -- CodePeer handles division and overflow checks directly, based on
287 -- the marks set by the frontend, hence no special expansion should
288 -- be performed in the frontend for division and overflow checks.
290 Backend_Divide_Checks_On_Target
:= True;
291 Backend_Overflow_Checks_On_Target
:= True;
293 -- Kill debug of generated code, since it messes up sloc values
295 Debug_Generated_Code
:= False;
297 -- Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
298 -- to support source navigation.
302 -- Polling mode forced off, since it generates confusing junk
304 Polling_Required
:= False;
306 -- Set operating mode to Generate_Code to benefit from full front-end
307 -- expansion (e.g. generics).
309 Operating_Mode
:= Generate_Code
;
311 -- We need SCIL generation of course
313 Generate_SCIL
:= True;
315 -- Enable assertions, since they give CodePeer valuable extra info
317 Assertions_Enabled
:= True;
319 -- Set normal RM validity checking and checking of copies (to catch
320 -- e.g. wrong values used in unchecked conversions).
321 -- All other validity checking is turned off, since this can generate
322 -- very complex trees that only confuse CodePeer and do not bring
323 -- enough useful info.
325 Reset_Validity_Check_Options
;
326 Validity_Check_Default
:= True;
327 Validity_Check_Copies
:= True;
329 -- Turn off style check options and ignore any style check pragmas
330 -- since we are not interested in any front-end warnings when we are
331 -- getting CodePeer output.
333 Reset_Style_Check_Options
;
334 Ignore_Style_Checks_Pragmas
:= True;
336 -- Always perform semantics and generate ali files in CodePeer mode,
337 -- so that a gnatmake -c -k will proceed further when possible.
339 Force_ALI_Tree_File
:= True;
340 Try_Semantics
:= True;
342 -- Make the Ada front end more liberal so that the compiler will
343 -- allow illegal code that is allowed by other compilers. CodePeer
344 -- is in the business of finding problems, not enforcing rules.
345 -- This is useful when using CodePeer mode with other compilers.
347 Relaxed_RM_Semantics
:= True;
349 -- Disable all simple value propagation. This is an optimization
350 -- which is valuable for code optimization, and also for generation
351 -- of compiler warnings, but these are being turned off by default,
352 -- and CodePeer generates better messages (referencing original
353 -- variables) this way.
354 -- Do this only is -gnatws is set (the default with -gnatcC), so that
355 -- if warnings are enabled, we'll get better messages from GNAT.
357 if Warning_Mode
= Suppress
then
358 Debug_Flag_MM
:= True;
362 -- Enable some individual switches that are implied by relaxed RM
365 if Relaxed_RM_Semantics
then
366 Opt
.Allow_Integer_Address
:= True;
367 Overriding_Renamings
:= True;
368 Treat_Categorization_Errors_As_Warnings
:= True;
371 -- Enable GNATprove_Mode when using -gnatd.F switch
373 if Debug_Flag_Dot_FF
then
374 GNATprove_Mode
:= True;
377 -- GNATprove_Mode is also activated by default in the gnat2why
380 if GNATprove_Mode
then
382 -- Turn off inlining, which would confuse formal verification output
385 Front_End_Inlining
:= False;
386 Inline_Active
:= False;
388 -- Issue warnings for failure to inline subprograms, as otherwise
389 -- expected in GNATprove mode for the local subprograms without
392 Ineffective_Inline_Warnings
:= True;
394 -- Disable front-end optimizations, to keep the tree as close to the
395 -- source code as possible, and also to avoid inconsistencies between
396 -- trees when using different optimization switches.
398 Optimization_Level
:= 0;
400 -- Enable some restrictions systematically to simplify the generated
401 -- code (and ease analysis).
403 Restrict
.Restrictions
.Set
(No_Initialize_Scalars
) := True;
405 -- Note: at this point we used to suppress various checks, but that
406 -- is not what we want. We need the semantic processing for these
407 -- checks (which will set flags like Do_Overflow_Check, showing the
408 -- points at which potential checks are required semantically). We
409 -- don't want the expansion associated with these checks, but that
410 -- happens anyway because this expansion is simply not done in the
411 -- SPARK version of the expander.
413 -- On the contrary, we need to enable explicitly all language checks,
414 -- as they may have been suppressed by the use of switch -gnatp.
416 Suppress_Options
.Suppress
:= (others => False);
418 -- Turn off dynamic elaboration checks. SPARK mode depends on the
419 -- use of the static elaboration mode.
421 Dynamic_Elaboration_Checks
:= False;
423 -- Detect overflow on unconstrained floating-point types, such as
424 -- the predefined types Float, Long_Float and Long_Long_Float from
425 -- package Standard. Not necessary if float overflows are checked
426 -- (Machine_Overflow true), since appropriate Do_Overflow_Check flags
427 -- will be set in any case.
429 Check_Float_Overflow
:= not Machine_Overflows_On_Target
;
431 -- Set STRICT mode for overflow checks if not set explicitly. This
432 -- prevents suppressing of overflow checks by default, in code down
435 if Suppress_Options
.Overflow_Mode_General
= Not_Set
then
436 Suppress_Options
.Overflow_Mode_General
:= Strict
;
437 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
440 -- Kill debug of generated code, since it messes up sloc values
442 Debug_Generated_Code
:= False;
444 -- Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
445 -- as it is needed for computing effects of subprograms in the formal
446 -- verification backend.
450 -- Polling mode forced off, since it generates confusing junk
452 Polling_Required
:= False;
454 -- Set operating mode to Check_Semantics, but a light front-end
455 -- expansion is still performed.
457 Operating_Mode
:= Check_Semantics
;
459 -- Enable assertions, since they give valuable extra information for
460 -- formal verification.
462 Assertions_Enabled
:= True;
464 -- Disable validity checks, since it generates code raising
465 -- exceptions for invalid data, which confuses GNATprove. Invalid
466 -- data is directly detected by GNATprove's flow analysis.
468 Validity_Checks_On
:= False;
470 -- Turn off style check options since we are not interested in any
471 -- front-end warnings when we are getting SPARK output.
473 Reset_Style_Check_Options
;
475 -- Suppress the generation of name tables for enumerations, which are
476 -- not needed for formal verification, and fall outside the SPARK
477 -- subset (use of pointers).
479 Global_Discard_Names
:= True;
481 -- Suppress the expansion of tagged types and dispatching calls,
482 -- which lead to the generation of non-SPARK code (use of pointers),
483 -- which is more complex to formally verify than the original source.
485 Tagged_Type_Expansion
:= False;
488 -- Set Configurable_Run_Time mode if system.ads flag set or if the
489 -- special debug flag -gnatdY is set.
491 if Targparm
.Configurable_Run_Time_On_Target
or Debug_Flag_YY
then
492 Configurable_Run_Time_Mode
:= True;
495 -- Set -gnatR3m mode if debug flag A set
497 if Debug_Flag_AA
then
498 Back_Annotate_Rep_Info
:= True;
499 List_Representation_Info
:= 1;
500 List_Representation_Info_Mechanisms
:= True;
503 -- Force Target_Strict_Alignment true if debug flag -gnatd.a is set
505 if Debug_Flag_Dot_A
then
506 Ttypes
.Target_Strict_Alignment
:= True;
509 -- Increase size of allocated entities if debug flag -gnatd.N is set
511 if Debug_Flag_Dot_NN
then
512 Atree
.Num_Extension_Nodes
:= Atree
.Num_Extension_Nodes
+ 1;
515 -- Disable static allocation of dispatch tables if -gnatd.t or if layout
516 -- is enabled. The front end's layout phase currently treats types that
517 -- have discriminant-dependent arrays as not being static even when a
518 -- discriminant constraint on the type is static, and this leads to
519 -- problems with subtypes of type Ada.Tags.Dispatch_Table_Wrapper. ???
521 if Debug_Flag_Dot_T
or else Frontend_Layout_On_Target
then
522 Static_Dispatch_Tables
:= False;
525 -- Flip endian mode if -gnatd8 set
528 Ttypes
.Bytes_Big_Endian
:= not Ttypes
.Bytes_Big_Endian
;
531 -- Activate front-end layout if debug flag -gnatdF is set
533 if Debug_Flag_FF
then
534 Targparm
.Frontend_Layout_On_Target
:= True;
537 -- Set and check exception mechanism
539 case Targparm
.Frontend_Exceptions_On_Target
is
541 case Targparm
.ZCX_By_Default_On_Target
is
544 ("Run-time library configured incorrectly");
546 ("(requesting support for Frontend ZCX exceptions)");
547 raise Unrecoverable_Error
;
549 Exception_Mechanism
:= Front_End_SJLJ
;
552 case Targparm
.ZCX_By_Default_On_Target
is
554 Exception_Mechanism
:= Back_End_ZCX
;
556 Exception_Mechanism
:= Back_End_SJLJ
;
560 -- Set proper status for overflow check mechanism
562 -- If already set (by -gnato or above in SPARK or CodePeer mode) then we
563 -- have nothing to do.
565 if Opt
.Suppress_Options
.Overflow_Mode_General
/= Not_Set
then
568 -- Otherwise set overflow mode defaults
571 -- Overflow checks are on by default (Suppress set False) except in
572 -- GNAT_Mode, where we want them off by default (we are not ready to
573 -- enable overflow checks in the compiler yet, for one thing the case
574 -- of 64-bit checks needs System.Arith_64 which is not a compiler
575 -- unit and it is a pain to try to include it in the compiler.
577 Suppress_Options
.Suppress
(Overflow_Check
) := GNAT_Mode
;
579 -- Set appropriate default overflow handling mode. Note: at present
580 -- we set STRICT in all three of the following cases. They are
581 -- separated because in the future we may make different choices.
583 -- By default set STRICT mode if -gnatg in effect
586 Suppress_Options
.Overflow_Mode_General
:= Strict
;
587 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
589 -- If we have backend divide and overflow checks, then by default
590 -- overflow checks are STRICT. Historically this code used to also
591 -- activate overflow checks, although no target currently has these
592 -- flags set, so this was dead code anyway.
594 elsif Targparm
.Backend_Divide_Checks_On_Target
596 Targparm
.Backend_Overflow_Checks_On_Target
598 Suppress_Options
.Overflow_Mode_General
:= Strict
;
599 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
601 -- Otherwise for now, default is STRICT mode. This may change in the
602 -- future, but for now this is the compatible behavior with previous
606 Suppress_Options
.Overflow_Mode_General
:= Strict
;
607 Suppress_Options
.Overflow_Mode_Assertions
:= Strict
;
611 -- Set default for atomic synchronization. As this synchronization
612 -- between atomic accesses can be expensive, and not typically needed
613 -- on some targets, an optional target parameter can turn the option
614 -- off. Note Atomic Synchronization is implemented as check.
616 Suppress_Options
.Suppress
(Atomic_Synchronization
) :=
617 not Atomic_Sync_Default_On_Target
;
619 -- Set default for Alignment_Check, if we are on a machine with non-
620 -- strict alignment, then we suppress this check, since it is over-
621 -- zealous for such machines.
623 if not Ttypes
.Target_Strict_Alignment
then
624 Suppress_Options
.Suppress
(Alignment_Check
) := True;
627 -- Set switch indicating if back end can handle limited types, and
628 -- guarantee that no incorrect copies are made (e.g. in the context
629 -- of an if or case expression).
631 -- Debug flag -gnatd.L decisively sets usage on
633 if Debug_Flag_Dot_LL
then
634 Back_End_Handles_Limited_Types
:= True;
636 -- If no debug flag, usage off for SCIL cases
638 elsif Generate_SCIL
then
639 Back_End_Handles_Limited_Types
:= False;
641 -- Otherwise normal gcc back end, for now still turn flag off by
642 -- default, since there are unresolved problems in the front end.
645 Back_End_Handles_Limited_Types
:= False;
648 -- If the inlining level has not been set by the user, compute it from
649 -- the optimization level: 1 at -O1/-O2 (and -Os), 2 at -O3 and above.
651 if Inline_Level
= 0 then
652 if Optimization_Level
< 3 then
659 -- Treat -gnatn as equivalent to -gnatN for non-GCC targets
661 if Inline_Active
and not Front_End_Inlining
then
663 -- We really should have a tag for this, what if we added a new
664 -- back end some day, it would not be true for this test, but it
665 -- would be non-GCC, so this is a bit troublesome ???
667 Front_End_Inlining
:= Generate_C_Code
;
670 -- Set back-end inlining indication
674 -- No back-end inlining available on C generation
678 -- No back-end inlining in GNATprove mode, since it just confuses
679 -- the formal verification process.
681 and then not GNATprove_Mode
683 -- No back-end inlining if front-end inlining explicitly enabled.
684 -- Done to minimize the output differences to customers still using
685 -- this deprecated switch; in addition, this behavior reduces the
686 -- output differences in old tests.
688 and then not Front_End_Inlining
690 -- Back-end inlining is disabled if debug flag .z is set
692 and then not Debug_Flag_Dot_Z
;
694 -- Output warning if -gnateE specified and cannot be supported
696 if Exception_Extra_Info
697 and then Restrict
.No_Exception_Handlers_Set
701 ("warning: extra exception information (-gnateE) was specified");
704 ("warning: this capability is not available in this configuration");
709 -- Finally capture adjusted value of Suppress_Options as the initial
710 -- value for Scope_Suppress, which will be modified as we move from
711 -- scope to scope (by Suppress/Unsuppress/Overflow_Checks pragmas).
713 Sem
.Scope_Suppress
:= Opt
.Suppress_Options
;
714 end Adjust_Global_Switches
;
720 procedure Check_Bad_Body
is
721 Sname
: Unit_Name_Type
;
722 Src_Ind
: Source_File_Index
;
723 Fname
: File_Name_Type
;
725 procedure Bad_Body_Error
(Msg
: String);
726 -- Issue message for bad body found
732 procedure Bad_Body_Error
(Msg
: String) is
734 Error_Msg_N
(Msg
, Main_Unit_Node
);
735 Error_Msg_File_1
:= Fname
;
736 Error_Msg_N
("remove incorrect body in file{!", Main_Unit_Node
);
739 -- Start of processing for Check_Bad_Body
742 -- Nothing to do if we are only checking syntax, because we don't know
743 -- enough to know if we require or forbid a body in this case.
745 if Operating_Mode
= Check_Syntax
then
749 -- Check for body not allowed
751 if (Main_Kind
= N_Package_Declaration
752 and then not Body_Required
(Main_Unit_Node
))
753 or else (Main_Kind
= N_Generic_Package_Declaration
754 and then not Body_Required
(Main_Unit_Node
))
755 or else Main_Kind
= N_Package_Renaming_Declaration
756 or else Main_Kind
= N_Subprogram_Renaming_Declaration
757 or else Nkind
(Original_Node
(Unit
(Main_Unit_Node
)))
758 in N_Generic_Instantiation
760 Sname
:= Unit_Name
(Main_Unit
);
762 -- If we do not already have a body name, then get the body name
764 if not Is_Body_Name
(Sname
) then
765 Sname
:= Get_Body_Name
(Sname
);
768 Fname
:= Get_File_Name
(Sname
, Subunit
=> False);
769 Src_Ind
:= Load_Source_File
(Fname
);
771 -- Case where body is present and it is not a subunit. Exclude the
772 -- subunit case, because it has nothing to do with the package we are
773 -- compiling. It is illegal for a child unit and a subunit with the
774 -- same expanded name (RM 10.2(9)) to appear together in a partition,
775 -- but there is nothing to stop a compilation environment from having
776 -- both, and the test here simply allows that. If there is an attempt
777 -- to include both in a partition, this is diagnosed at bind time. In
778 -- Ada 83 mode this is not a warning case.
780 -- Note that in general we do not give the message if the file in
781 -- question does not look like a body. This includes weird cases,
782 -- but in particular means that if the file is just a No_Body pragma,
783 -- then we won't give the message (that's the whole point of this
784 -- pragma, to be used this way and to cause the body file to be
785 -- ignored in this context).
787 if Src_Ind
/= No_Source_File
788 and then Source_File_Is_Body
(Src_Ind
)
790 Errout
.Finalize
(Last_Call
=> False);
792 Error_Msg_Unit_1
:= Sname
;
794 -- Ada 83 case of a package body being ignored. This is not an
795 -- error as far as the Ada 83 RM is concerned, but it is almost
796 -- certainly not what is wanted so output a warning. Give this
797 -- message only if there were no errors, since otherwise it may
798 -- be incorrect (we may have misinterpreted a junk spec as not
799 -- needing a body when it really does).
801 if Main_Kind
= N_Package_Declaration
802 and then Ada_Version
= Ada_83
803 and then Operating_Mode
= Generate_Code
804 and then Distribution_Stub_Mode
/= Generate_Caller_Stub_Body
805 and then not Compilation_Errors
808 ("package $$ does not require a body??", Main_Unit_Node
);
809 Error_Msg_File_1
:= Fname
;
810 Error_Msg_N
("body in file{ will be ignored??", Main_Unit_Node
);
812 -- Ada 95 cases of a body file present when no body is
813 -- permitted. This we consider to be an error.
816 -- For generic instantiations, we never allow a body
818 if Nkind
(Original_Node
(Unit
(Main_Unit_Node
))) in
819 N_Generic_Instantiation
822 ("generic instantiation for $$ does not allow a body");
824 -- A library unit that is a renaming never allows a body
826 elsif Main_Kind
in N_Renaming_Declaration
then
828 ("renaming declaration for $$ does not allow a body!");
830 -- Remaining cases are packages and generic packages. Here
831 -- we only do the test if there are no previous errors,
832 -- because if there are errors, they may lead us to
833 -- incorrectly believe that a package does not allow a
834 -- body when in fact it does.
836 elsif not Compilation_Errors
then
837 if Main_Kind
= N_Package_Declaration
then
839 ("package $$ does not allow a body!");
841 elsif Main_Kind
= N_Generic_Package_Declaration
then
843 ("generic package $$ does not allow a body!");
856 procedure Check_Rep_Info
is
858 if List_Representation_Info
/= 0
859 or else List_Representation_Info_Mechanisms
864 ("cannot generate representation information, no code generated");
871 ----------------------------------------
872 -- Post_Compilation_Validation_Checks --
873 ----------------------------------------
875 procedure Post_Compilation_Validation_Checks
is
877 -- Validate alignment check warnings. In some cases we generate warnings
878 -- about possible alignment errors because we don't know the alignment
879 -- that will be chosen by the back end. This routine is in charge of
880 -- getting rid of those warnings if we can tell they are not needed.
882 Checks
.Validate_Alignment_Check_Warnings
;
884 -- Validate unchecked conversions (using the values for size and
885 -- alignment annotated by the backend where possible).
887 Sem_Ch13
.Validate_Unchecked_Conversions
;
889 -- Validate address clauses (again using alignment values annotated
890 -- by the backend where possible).
892 Sem_Ch13
.Validate_Address_Clauses
;
894 -- Validate independence pragmas (again using values annotated by the
895 -- back end for component layout where possible) but only for non-GCC
896 -- back ends, as this is done a priori for GCC back ends.
898 if AAMP_On_Target
then
899 Sem_Ch13
.Validate_Independence
;
902 end Post_Compilation_Validation_Checks
;
904 -- Start of processing for Gnat1drv
907 -- This inner block is set up to catch assertion errors and constraint
908 -- errors. Since the code for handling these errors can cause another
909 -- exception to be raised (namely Unrecoverable_Error), we need two
910 -- nested blocks, so that the outer one handles unrecoverable error.
913 -- Initialize all packages. For the most part, these initialization
914 -- calls can be made in any order. Exceptions are as follows:
916 -- Lib.Initialize need to be called before Scan_Compiler_Arguments,
917 -- because it initializes a table filled by Scan_Compiler_Arguments.
923 Scan_Compiler_Arguments
;
924 Osint
.Add_Default_Search_Dirs
;
945 Sem_Type
.Init_Interp_Tables
;
947 -- Capture compilation date and time
949 Opt
.Compilation_Time
:= System
.OS_Lib
.Current_Time_String
;
951 -- Get the target parameters only when -gnats is not used, to avoid
952 -- failing when there is no default runtime.
954 if Operating_Mode
/= Check_Syntax
then
956 -- Acquire target parameters from system.ads (package System source)
958 Targparm_Acquire
: declare
961 S
: Source_File_Index
;
965 Name_Buffer
(1 .. 10) := "system.ads";
968 S
:= Load_Source_File
(N
);
970 -- Failed to read system.ads, fatal error
972 if S
= No_Source_File
then
974 ("fatal error, run-time library not installed correctly");
975 Write_Line
("cannot locate file system.ads");
976 raise Unrecoverable_Error
;
978 -- Read system.ads successfully, remember its source index
981 System_Source_File_Index
:= S
;
984 -- Call to get target parameters. Note that the actual interface
985 -- routines are in Tbuild. They can't be in this procedure because
986 -- of accessibility issues.
988 Targparm
.Get_Target_Parameters
989 (System_Text
=> Source_Text
(S
),
990 Source_First
=> Source_First
(S
),
991 Source_Last
=> Source_Last
(S
),
992 Make_Id
=> Tbuild
.Make_Id
'Access,
993 Make_SC
=> Tbuild
.Make_SC
'Access,
994 Set_NOD
=> Tbuild
.Set_NOD
'Access,
995 Set_NSA
=> Tbuild
.Set_NSA
'Access,
996 Set_NUA
=> Tbuild
.Set_NUA
'Access,
997 Set_NUP
=> Tbuild
.Set_NUP
'Access);
999 -- Acquire configuration pragma information from Targparm
1001 Restrict
.Restrictions
:= Targparm
.Restrictions_On_Target
;
1002 end Targparm_Acquire
;
1005 -- Perform various adjustments and settings of global switches
1007 Adjust_Global_Switches
;
1009 -- Output copyright notice if full list mode unless we have a list
1010 -- file, in which case we defer this so that it is output in the file.
1012 if (Verbose_Mode
or else (Full_List
and then Full_List_File_Name
= null))
1014 -- Debug flag gnatd7 suppresses this copyright notice
1016 and then not Debug_Flag_7
1019 Write_Str
("GNAT ");
1020 Write_Str
(Gnat_Version_String
);
1022 Write_Str
("Copyright 1992-" & Current_Year
1023 & ", Free Software Foundation, Inc.");
1027 -- Check we do not have more than one source file, this happens only in
1028 -- the case where the driver is called directly, it cannot happen when
1029 -- gnat1 is invoked from gcc in the normal case.
1031 if Osint
.Number_Of_Files
/= 1 then
1034 Osint
.Fail
("you must provide one source file");
1036 elsif Usage_Requested
then
1040 -- Generate target dependent output file if requested
1042 if Target_Dependent_Info_Write_Name
/= null then
1043 Set_Targ
.Write_Target_Dependent_Values
;
1046 -- Call the front end
1048 Original_Operating_Mode
:= Operating_Mode
;
1051 -- In GNATprove mode, force loading of System unit to ensure that
1052 -- System.Interrupt_Priority is available to GNATprove for the
1053 -- generation of VCs related to ceiling priority.
1055 if GNATprove_Mode
then
1057 Unused_E
: constant Entity_Id
:=
1058 Rtsfind
.RTE
(Rtsfind
.RE_Interrupt_Priority
);
1064 -- Exit with errors if the main source could not be parsed
1066 if Sinput
.Main_Source_File
= No_Source_File
then
1067 Errout
.Finalize
(Last_Call
=> True);
1068 Errout
.Output_Messages
;
1069 Exit_Program
(E_Errors
);
1072 Main_Unit_Node
:= Cunit
(Main_Unit
);
1073 Main_Kind
:= Nkind
(Unit
(Main_Unit_Node
));
1076 -- In CodePeer mode we always delete old SCIL files before regenerating
1077 -- new ones, in case of e.g. errors, and also to remove obsolete scilx
1078 -- files generated by CodePeer itself.
1080 if CodePeer_Mode
then
1081 Comperr
.Delete_SCIL_Files
;
1084 -- Ditto for old C files before regenerating new ones
1086 if Generate_C_Code
then
1091 -- Exit if compilation errors detected
1093 Errout
.Finalize
(Last_Call
=> False);
1095 if Compilation_Errors
then
1097 Post_Compilation_Validation_Checks
;
1098 Errout
.Output_Messages
;
1101 -- Generate ALI file if specially requested
1103 if Opt
.Force_ALI_Tree_File
then
1104 Write_ALI
(Object
=> False);
1108 Errout
.Finalize
(Last_Call
=> True);
1109 Exit_Program
(E_Errors
);
1112 -- Set Generate_Code on main unit and its spec. We do this even if are
1113 -- not generating code, since Lib-Writ uses this to determine which
1114 -- units get written in the ali file.
1116 Set_Generate_Code
(Main_Unit
);
1118 -- If we have a corresponding spec, and it comes from source or it is
1119 -- not a generated spec for a child subprogram body, then we need object
1120 -- code for the spec unit as well.
1122 if Nkind
(Unit
(Main_Unit_Node
)) in N_Unit_Body
1123 and then not Acts_As_Spec
(Main_Unit_Node
)
1125 if Nkind
(Unit
(Main_Unit_Node
)) = N_Subprogram_Body
1126 and then not Comes_From_Source
(Library_Unit
(Main_Unit_Node
))
1131 (Get_Cunit_Unit_Number
(Library_Unit
(Main_Unit_Node
)));
1135 -- Case of no code required to be generated, exit indicating no error
1137 if Original_Operating_Mode
= Check_Syntax
then
1139 Errout
.Finalize
(Last_Call
=> True);
1140 Errout
.Output_Messages
;
1145 -- Use a goto instead of calling Exit_Program so that finalization
1148 goto End_Of_Program
;
1150 elsif Original_Operating_Mode
= Check_Semantics
then
1151 Back_End_Mode
:= Declarations_Only
;
1153 -- All remaining cases are cases in which the user requested that code
1154 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if we
1155 -- can in fact satisfy this request.
1157 -- Cannot generate code if someone has turned off code generation for
1158 -- any reason at all. We will try to figure out a reason below.
1160 elsif Operating_Mode
/= Generate_Code
then
1161 Back_End_Mode
:= Skip
;
1163 -- We can generate code for a subprogram body unless there were missing
1164 -- subunits. Note that we always generate code for all generic units (a
1165 -- change from some previous versions of GNAT).
1167 elsif Main_Kind
= N_Subprogram_Body
and then not Subunits_Missing
then
1168 Back_End_Mode
:= Generate_Object
;
1170 -- We can generate code for a package body unless there are subunits
1171 -- missing (note that we always generate code for generic units, which
1172 -- is a change from some earlier versions of GNAT).
1174 elsif Main_Kind
= N_Package_Body
and then not Subunits_Missing
then
1175 Back_End_Mode
:= Generate_Object
;
1177 -- We can generate code for a package declaration or a subprogram
1178 -- declaration only if it does not required a body.
1180 elsif Nkind_In
(Main_Kind
, N_Package_Declaration
,
1181 N_Subprogram_Declaration
)
1183 (not Body_Required
(Main_Unit_Node
)
1184 or else Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
1186 Back_End_Mode
:= Generate_Object
;
1188 -- We can generate code for a generic package declaration of a generic
1189 -- subprogram declaration only if does not require a body.
1191 elsif Nkind_In
(Main_Kind
, N_Generic_Package_Declaration
,
1192 N_Generic_Subprogram_Declaration
)
1193 and then not Body_Required
(Main_Unit_Node
)
1195 Back_End_Mode
:= Generate_Object
;
1197 -- Compilation units that are renamings do not require bodies, so we can
1198 -- generate code for them.
1200 elsif Nkind_In
(Main_Kind
, N_Package_Renaming_Declaration
,
1201 N_Subprogram_Renaming_Declaration
)
1203 Back_End_Mode
:= Generate_Object
;
1205 -- Compilation units that are generic renamings do not require bodies
1206 -- so we can generate code for them.
1208 elsif Main_Kind
in N_Generic_Renaming_Declaration
then
1209 Back_End_Mode
:= Generate_Object
;
1211 -- It is not an error to analyze in CodePeer mode a spec which requires
1212 -- a body, in order to generate SCIL for this spec.
1213 -- Ditto for Generate_C_Code mode and generate a C header for a spec.
1215 elsif CodePeer_Mode
or Generate_C_Code
then
1216 Back_End_Mode
:= Generate_Object
;
1218 -- It is not an error to analyze in GNATprove mode a spec which requires
1219 -- a body, when the body is not available. During frame condition
1220 -- generation, the corresponding ALI file is generated. During
1221 -- analysis, the spec is analyzed.
1223 elsif GNATprove_Mode
then
1224 Back_End_Mode
:= Declarations_Only
;
1226 -- In all other cases (specs which have bodies, generics, and bodies
1227 -- where subunits are missing), we cannot generate code and we generate
1228 -- a warning message. Note that generic instantiations are gone at this
1229 -- stage since they have been replaced by their instances.
1232 Back_End_Mode
:= Skip
;
1235 -- At this stage Back_End_Mode is set to indicate if the backend should
1236 -- be called to generate code. If it is Skip, then code generation has
1237 -- been turned off, even though code was requested by the original
1238 -- command. This is not an error from the user point of view, but it is
1239 -- an error from the point of view of the gcc driver, so we must exit
1240 -- with an error status.
1242 -- We generate an informative message (from the gcc point of view, it
1243 -- is an error message, but from the users point of view this is not an
1244 -- error, just a consequence of compiling something that cannot
1247 if Back_End_Mode
= Skip
then
1249 Write_Str
("cannot generate code for file ");
1250 Write_Name
(Unit_File_Name
(Main_Unit
));
1252 if Subunits_Missing
then
1253 Write_Str
(" (missing subunits)");
1256 -- Force generation of ALI file, for backward compatibility
1258 Opt
.Force_ALI_Tree_File
:= True;
1260 elsif Main_Kind
= N_Subunit
then
1261 Write_Str
(" (subunit)");
1264 -- Force generation of ALI file, for backward compatibility
1266 Opt
.Force_ALI_Tree_File
:= True;
1268 elsif Main_Kind
= N_Subprogram_Declaration
then
1269 Write_Str
(" (subprogram spec)");
1272 -- Generic package body in GNAT implementation mode
1274 elsif Main_Kind
= N_Package_Body
and then GNAT_Mode
then
1275 Write_Str
(" (predefined generic)");
1278 -- Force generation of ALI file, for backward compatibility
1280 Opt
.Force_ALI_Tree_File
:= True;
1282 -- Only other case is a package spec
1285 Write_Str
(" (package spec)");
1289 Set_Standard_Output
;
1291 Post_Compilation_Validation_Checks
;
1292 Errout
.Finalize
(Last_Call
=> True);
1293 Errout
.Output_Messages
;
1297 -- Generate ALI file if specially requested, or for missing subunits,
1298 -- subunits or predefined generic.
1300 if Opt
.Force_ALI_Tree_File
then
1301 Write_ALI
(Object
=> False);
1307 -- Exit program with error indication, to kill object file
1309 Exit_Program
(E_No_Code
);
1312 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also set
1313 -- as indicated by Back_Annotate_Rep_Info being set to True.
1315 -- We don't call for annotations on a subunit, because to process those
1316 -- the back end requires that the parent(s) be properly compiled.
1318 -- Annotation is suppressed for targets where front-end layout is
1319 -- enabled, because the front end determines representations.
1321 -- The back end is not invoked in ASIS mode with GNSA because all type
1322 -- representation information will be provided by the GNSA back end, not
1325 if Back_End_Mode
= Declarations_Only
1327 (not (Back_Annotate_Rep_Info
or Generate_SCIL
or GNATprove_Mode
)
1328 or else Main_Kind
= N_Subunit
1329 or else Frontend_Layout_On_Target
1330 or else ASIS_GNSA_Mode
)
1332 Post_Compilation_Validation_Checks
;
1333 Errout
.Finalize
(Last_Call
=> True);
1334 Errout
.Output_Messages
;
1335 Write_ALI
(Object
=> False);
1343 -- Ensure that we properly register a dependency on system.ads, since
1344 -- even if we do not semantically depend on this, Targparm has read
1345 -- system parameters from the system.ads file.
1347 Lib
.Writ
.Ensure_System_Dependency
;
1349 -- Add dependencies, if any, on preprocessing data file and on
1350 -- preprocessing definition file(s).
1352 Prepcomp
.Add_Dependencies
;
1354 -- In gnatprove mode we're writing the ALI much earlier than usual
1355 -- as flow analysis needs the file present in order to append its
1356 -- own globals to it.
1358 if GNATprove_Mode
then
1360 -- Note: In GNATprove mode, an "object" file is always generated as
1361 -- the result of calling gnat1 or gnat2why, although this is not the
1362 -- same as the object file produced for compilation.
1364 Write_ALI
(Object
=> True);
1367 -- Some back ends (for instance Gigi) are known to rely on SCOs for code
1368 -- generation. Make sure they are available.
1370 if Generate_SCO
then
1371 Par_SCO
.SCO_Record_Filtered
;
1374 -- Back end needs to explicitly unlock tables it needs to touch
1388 -- Here we call the back end to generate the output code
1390 Generating_Code
:= True;
1391 Back_End
.Call_Back_End
(Back_End_Mode
);
1393 -- Once the backend is complete, we unlock the names table. This call
1394 -- allows a few extra entries, needed for example for the file name
1395 -- for the library file output.
1399 -- Generate the call-graph output of dispatching calls
1401 Exp_CG
.Generate_CG_Output
;
1403 -- Perform post compilation validation checks
1405 Post_Compilation_Validation_Checks
;
1407 -- Now we complete output of errors, rep info and the tree info. These
1408 -- are delayed till now, since it is perfectly possible for gigi to
1409 -- generate errors, modify the tree (in particular by setting flags
1410 -- indicating that elaboration is required, and also to back annotate
1411 -- representation information for List_Rep_Info.
1413 Errout
.Finalize
(Last_Call
=> True);
1414 Errout
.Output_Messages
;
1415 List_Rep_Info
(Ttypes
.Bytes_Big_Endian
);
1416 Inline
.List_Inlining_Info
;
1418 -- Only write the library if the backend did not generate any error
1419 -- messages. Otherwise signal errors to the driver program so that
1420 -- there will be no attempt to generate an object file.
1422 if Compilation_Errors
then
1424 Exit_Program
(E_Errors
);
1427 if not GNATprove_Mode
then
1428 Write_ALI
(Object
=> (Back_End_Mode
= Generate_Object
));
1431 if not Compilation_Errors
then
1433 -- In case of ada backends, we need to make sure that the generated
1434 -- object file has a timestamp greater than the ALI file. We do this
1435 -- to make gnatmake happy when checking the ALI and obj timestamps,
1436 -- where it expects the object file being written after the ali file.
1438 -- Gnatmake's assumption is true for gcc platforms where the gcc
1439 -- wrapper needs to call the assembler after calling gnat1, but is
1440 -- not true for ada backends, where the object files are created
1441 -- directly by gnat1 (so are created before the ali file).
1443 Back_End
.Gen_Or_Update_Object_File
;
1446 -- Generate ASIS tree after writing the ALI file, since in ASIS mode,
1447 -- Write_ALI may in fact result in further tree decoration from the
1448 -- original tree file. Note that we dump the tree just before generating
1449 -- it, so that the dump will exactly reflect what is written out.
1454 -- Finalize name table and we are all done
1459 -- Handle fatal internal compiler errors
1461 when Rtsfind
.RE_Not_Available
=>
1462 Comperr
.Compiler_Abort
("RE_Not_Available");
1464 when System
.Assertions
.Assert_Failure
=>
1465 Comperr
.Compiler_Abort
("Assert_Failure");
1467 when Constraint_Error
=>
1468 Comperr
.Compiler_Abort
("Constraint_Error");
1470 when Program_Error
=>
1471 Comperr
.Compiler_Abort
("Program_Error");
1473 when Storage_Error
=>
1475 -- Assume this is a bug. If it is real, the message will in any case
1476 -- say Storage_Error, giving a strong hint.
1478 Comperr
.Compiler_Abort
("Storage_Error");
1480 when Unrecoverable_Error
=>
1484 Comperr
.Compiler_Abort
("exception");
1490 -- The outer exception handles an unrecoverable error
1493 when Unrecoverable_Error
=>
1494 Errout
.Finalize
(Last_Call
=> True);
1495 Errout
.Output_Messages
;
1498 Write_Str
("compilation abandoned");
1501 Set_Standard_Output
;
1504 Exit_Program
(E_Errors
);