2015-01-06 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / gnat1drv.adb
blobb4e74f4fcc01126e2f732d01a4611f9f645f972b
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-2014, 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 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Back_End; use Back_End;
28 with Checks;
29 with Comperr;
30 with Csets; use Csets;
31 with Debug; use Debug;
32 with Elists;
33 with Errout; use Errout;
34 with Exp_CG;
35 with Fmap;
36 with Fname; use Fname;
37 with Fname.UF; use Fname.UF;
38 with Frontend;
39 with Gnatvsn; use Gnatvsn;
40 with Inline;
41 with Lib; use Lib;
42 with Lib.Writ; use Lib.Writ;
43 with Lib.Xref;
44 with Namet; use Namet;
45 with Nlists;
46 with Opt; use Opt;
47 with Osint; use Osint;
48 with Output; use Output;
49 with Par_SCO;
50 with Prepcomp;
51 with Repinfo; use Repinfo;
52 with Restrict;
53 with Rident; use Rident;
54 with Rtsfind;
55 with SCOs;
56 with Sem;
57 with Sem_Ch8;
58 with Sem_Ch12;
59 with Sem_Ch13;
60 with Sem_Elim;
61 with Sem_Eval;
62 with Sem_Type;
63 with Set_Targ;
64 with Sinfo; use Sinfo;
65 with Sinput.L; use Sinput.L;
66 with Snames;
67 with Sprint; use Sprint;
68 with Stringt;
69 with Stylesw; use Stylesw;
70 with Targparm; use Targparm;
71 with Tbuild;
72 with Tree_Gen;
73 with Treepr; use Treepr;
74 with Ttypes;
75 with Types; use Types;
76 with Uintp; use Uintp;
77 with Uname; use Uname;
78 with Urealp;
79 with Usage;
80 with Validsw; use Validsw;
82 with System.Assertions;
83 with System.OS_Lib;
85 --------------
86 -- Gnat1drv --
87 --------------
89 procedure Gnat1drv is
90 Main_Unit_Node : Node_Id;
91 -- Compilation unit node for main unit
93 Main_Kind : Node_Kind;
94 -- Kind of main compilation unit node
96 Back_End_Mode : Back_End.Back_End_Mode_Type;
97 -- Record back end mode
99 procedure Adjust_Global_Switches;
100 -- There are various interactions between front end switch settings,
101 -- including debug switch settings and target dependent parameters.
102 -- This procedure takes care of properly handling these interactions.
103 -- We do it after scanning out all the switches, so that we are not
104 -- depending on the order in which switches appear.
106 procedure Check_Bad_Body;
107 -- Called to check if the unit we are compiling has a bad body
109 procedure Check_Rep_Info;
110 -- Called when we are not generating code, to check if -gnatR was requested
111 -- and if so, explain that we will not be honoring the request.
113 procedure Post_Compilation_Validation_Checks;
114 -- This procedure performs various validation checks that have to be left
115 -- to the end of the compilation process, after generating code but before
116 -- issuing error messages. In particular, these checks generally require
117 -- the information provided by the back end in back annotation of declared
118 -- entities (e.g. actual size and alignment values chosen by the back end).
120 ----------------------------
121 -- Adjust_Global_Switches --
122 ----------------------------
124 procedure Adjust_Global_Switches is
125 begin
126 -- -gnatd.M enables Relaxed_RM_Semantics
128 if Debug_Flag_Dot_MM then
129 Relaxed_RM_Semantics := True;
130 end if;
132 -- -gnatd.V or -gnatd.u enables special C expansion mode
134 if Debug_Flag_Dot_VV or Debug_Flag_Dot_U then
135 Modify_Tree_For_C := True;
136 end if;
138 -- -gnatd.E sets Error_To_Warning mode, causing selected error messages
139 -- to be treated as warnings instead of errors.
141 if Debug_Flag_Dot_EE then
142 Error_To_Warning := True;
143 end if;
145 -- Disable CodePeer_Mode in Check_Syntax, since we need front-end
146 -- expansion.
148 if Operating_Mode = Check_Syntax then
149 CodePeer_Mode := False;
150 end if;
152 -- Set ASIS mode if -gnatt and -gnatc are set
154 if Operating_Mode = Check_Semantics and then Tree_Output then
155 ASIS_Mode := True;
157 -- Turn off inlining in ASIS mode, since ASIS cannot handle the extra
158 -- information in the trees caused by inlining being active.
160 -- More specifically, the tree seems to be malformed from the ASIS
161 -- point of view if -gnatc and -gnatn appear together???
163 Inline_Active := False;
165 -- Turn off SCIL generation and CodePeer mode in semantics mode,
166 -- since SCIL requires front-end expansion.
168 Generate_SCIL := False;
169 CodePeer_Mode := False;
170 end if;
172 -- SCIL mode needs to disable front-end inlining since the generated
173 -- trees (in particular order and consistency between specs compiled
174 -- as part of a main unit or as part of a with-clause) are causing
175 -- troubles.
177 if Generate_SCIL then
178 Front_End_Inlining := False;
179 end if;
181 -- Tune settings for optimal SCIL generation in CodePeer mode
183 if CodePeer_Mode then
185 -- Turn off gnatprove mode (if set via e.g. -gnatd.F), not compatible
186 -- with CodePeer mode.
188 GNATprove_Mode := False;
190 -- Turn off inlining, confuses CodePeer output and gains nothing
192 Front_End_Inlining := False;
193 Inline_Active := False;
195 -- Disable front-end optimizations, to keep the tree as close to the
196 -- source code as possible, and also to avoid inconsistencies between
197 -- trees when using different optimization switches.
199 Optimization_Level := 0;
201 -- Enable some restrictions systematically to simplify the generated
202 -- code (and ease analysis). Note that restriction checks are also
203 -- disabled in CodePeer mode, see Restrict.Check_Restriction, and
204 -- user specified Restrictions pragmas are ignored, see
205 -- Sem_Prag.Process_Restrictions_Or_Restriction_Warnings.
207 Restrict.Restrictions.Set (No_Initialize_Scalars) := True;
208 Restrict.Restrictions.Set (No_Task_Hierarchy) := True;
209 Restrict.Restrictions.Set (No_Abort_Statements) := True;
210 Restrict.Restrictions.Set (Max_Asynchronous_Select_Nesting) := True;
211 Restrict.Restrictions.Value (Max_Asynchronous_Select_Nesting) := 0;
213 -- Suppress division by zero and access checks since they are handled
214 -- implicitly by CodePeer.
216 -- Turn off dynamic elaboration checks: generates inconsistencies in
217 -- trees between specs compiled as part of a main unit or as part of
218 -- a with-clause.
220 -- Turn off alignment checks: these cannot be proved statically by
221 -- CodePeer and generate false positives.
223 -- Enable all other language checks
225 Suppress_Options.Suppress :=
226 (Access_Check => True,
227 Alignment_Check => True,
228 Division_Check => True,
229 Elaboration_Check => True,
230 others => False);
232 Dynamic_Elaboration_Checks := False;
234 -- Set STRICT mode for overflow checks if not set explicitly. This
235 -- prevents suppressing of overflow checks by default, in code down
236 -- below.
238 if Suppress_Options.Overflow_Mode_General = Not_Set then
239 Suppress_Options.Overflow_Mode_General := Strict;
240 Suppress_Options.Overflow_Mode_Assertions := Strict;
241 end if;
243 -- CodePeer handles division and overflow checks directly, based on
244 -- the marks set by the frontend, hence no special expansion should
245 -- be performed in the frontend for division and overflow checks.
247 Backend_Divide_Checks_On_Target := True;
248 Backend_Overflow_Checks_On_Target := True;
250 -- Kill debug of generated code, since it messes up sloc values
252 Debug_Generated_Code := False;
254 -- Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
255 -- Do we really need to spend time generating xref in CodePeer
256 -- mode??? Consider setting Xref_Active to False.
258 Xref_Active := True;
260 -- Polling mode forced off, since it generates confusing junk
262 Polling_Required := False;
264 -- Set operating mode to Generate_Code to benefit from full front-end
265 -- expansion (e.g. generics).
267 Operating_Mode := Generate_Code;
269 -- We need SCIL generation of course
271 Generate_SCIL := True;
273 -- Enable assertions, since they give CodePeer valuable extra info
275 Assertions_Enabled := True;
277 -- Disable all simple value propagation. This is an optimization
278 -- which is valuable for code optimization, and also for generation
279 -- of compiler warnings, but these are being turned off by default,
280 -- and CodePeer generates better messages (referencing original
281 -- variables) this way.
283 Debug_Flag_MM := True;
285 -- Set normal RM validity checking, and checking of IN OUT parameters
286 -- (this might give CodePeer more useful checks to analyze, to be
287 -- confirmed???). All other validity checking is turned off, since
288 -- this can generate very complex trees that only confuse CodePeer
289 -- and do not bring enough useful info.
291 Reset_Validity_Check_Options;
292 Validity_Check_Default := True;
293 Validity_Check_In_Out_Params := True;
294 Validity_Check_In_Params := True;
296 -- Turn off style check options and ignore any style check pragmas
297 -- since we are not interested in any front-end warnings when we are
298 -- getting CodePeer output.
300 Reset_Style_Check_Options;
301 Ignore_Style_Checks_Pragmas := True;
303 -- Always perform semantics and generate ali files in CodePeer mode,
304 -- so that a gnatmake -c -k will proceed further when possible.
306 Force_ALI_Tree_File := True;
307 Try_Semantics := True;
309 -- Make the Ada front-end more liberal so that the compiler will
310 -- allow illegal code that is allowed by other compilers. CodePeer
311 -- is in the business of finding problems, not enforcing rules.
312 -- This is useful when using CodePeer mode with other compilers.
314 Relaxed_RM_Semantics := True;
315 end if;
317 -- Enable some individual switches that are implied by relaxed RM
318 -- semantics mode.
320 if Relaxed_RM_Semantics then
321 Opt.Allow_Integer_Address := True;
322 Overriding_Renamings := True;
323 Treat_Categorization_Errors_As_Warnings := True;
324 end if;
326 -- Enable GNATprove_Mode when using -gnatd.F switch
328 if Debug_Flag_Dot_FF then
329 GNATprove_Mode := True;
330 end if;
332 -- GNATprove_Mode is also activated by default in the gnat2why
333 -- executable.
335 if GNATprove_Mode then
337 -- Turn off inlining, which would confuse formal verification output
338 -- and gain nothing.
340 Front_End_Inlining := False;
341 Inline_Active := False;
343 -- Issue warnings for failure to inline subprograms, as otherwise
344 -- expected in GNATprove mode for the local subprograms without
345 -- contracts.
347 Ineffective_Inline_Warnings := True;
349 -- Disable front-end optimizations, to keep the tree as close to the
350 -- source code as possible, and also to avoid inconsistencies between
351 -- trees when using different optimization switches.
353 Optimization_Level := 0;
355 -- Enable some restrictions systematically to simplify the generated
356 -- code (and ease analysis). Note that restriction checks are also
357 -- disabled in SPARK mode, see Restrict.Check_Restriction, and user
358 -- specified Restrictions pragmas are ignored, see
359 -- Sem_Prag.Process_Restrictions_Or_Restriction_Warnings.
361 Restrict.Restrictions.Set (No_Initialize_Scalars) := True;
363 -- Note: at this point we used to suppress various checks, but that
364 -- is not what we want. We need the semantic processing for these
365 -- checks (which will set flags like Do_Overflow_Check, showing the
366 -- points at which potential checks are required semantically). We
367 -- don't want the expansion associated with these checks, but that
368 -- happens anyway because this expansion is simply not done in the
369 -- SPARK version of the expander.
371 -- On the contrary, we need to enable explicitly all language checks,
372 -- as they may have been suppressed by the use of switch -gnatp.
374 Suppress_Options.Suppress := (others => False);
376 -- Turn off dynamic elaboration checks. SPARK mode depends on the
377 -- use of the static elaboration mode.
379 Dynamic_Elaboration_Checks := False;
381 -- Detect overflow on unconstrained floating-point types, such as
382 -- the predefined types Float, Long_Float and Long_Long_Float from
383 -- package Standard. Not necessary if float overflows are checked
384 -- (Machine_Overflow true), since appropriate Do_Overflow_Check flags
385 -- will be set in any case.
387 Check_Float_Overflow := not Machine_Overflows_On_Target;
389 -- Set STRICT mode for overflow checks if not set explicitly. This
390 -- prevents suppressing of overflow checks by default, in code down
391 -- below.
393 if Suppress_Options.Overflow_Mode_General = Not_Set then
394 Suppress_Options.Overflow_Mode_General := Strict;
395 Suppress_Options.Overflow_Mode_Assertions := Strict;
396 end if;
398 -- Kill debug of generated code, since it messes up sloc values
400 Debug_Generated_Code := False;
402 -- Turn cross-referencing on in case it was disabled (e.g. by -gnatD)
403 -- as it is needed for computing effects of subprograms in the formal
404 -- verification backend.
406 Xref_Active := True;
408 -- Polling mode forced off, since it generates confusing junk
410 Polling_Required := False;
412 -- Set operating mode to Check_Semantics, but a light front-end
413 -- expansion is still performed.
415 Operating_Mode := Check_Semantics;
417 -- Enable assertions, since they give valuable extra information for
418 -- formal verification.
420 Assertions_Enabled := True;
422 -- Disable validity checks, since it generates code raising
423 -- exceptions for invalid data, which confuses GNATprove. Invalid
424 -- data is directly detected by GNATprove's flow analysis.
426 Validity_Checks_On := False;
428 -- Turn off style check options since we are not interested in any
429 -- front-end warnings when we are getting SPARK output.
431 Reset_Style_Check_Options;
433 -- Suppress the generation of name tables for enumerations, which are
434 -- not needed for formal verification, and fall outside the SPARK
435 -- subset (use of pointers).
437 Global_Discard_Names := True;
439 -- Suppress the expansion of tagged types and dispatching calls,
440 -- which lead to the generation of non-SPARK code (use of pointers),
441 -- which is more complex to formally verify than the original source.
443 Tagged_Type_Expansion := False;
444 end if;
446 -- Set Configurable_Run_Time mode if system.ads flag set or if the
447 -- special debug flag -gnatdY is set.
449 if Targparm.Configurable_Run_Time_On_Target or Debug_Flag_YY then
450 Configurable_Run_Time_Mode := True;
451 end if;
453 -- Set -gnatR3m mode if debug flag A set
455 if Debug_Flag_AA then
456 Back_Annotate_Rep_Info := True;
457 List_Representation_Info := 1;
458 List_Representation_Info_Mechanisms := True;
459 end if;
461 -- Force Target_Strict_Alignment true if debug flag -gnatd.a is set
463 if Debug_Flag_Dot_A then
464 Ttypes.Target_Strict_Alignment := True;
465 end if;
467 -- Increase size of allocated entities if debug flag -gnatd.N is set
469 if Debug_Flag_Dot_NN then
470 Atree.Num_Extension_Nodes := Atree.Num_Extension_Nodes + 1;
471 end if;
473 -- Disable static allocation of dispatch tables if -gnatd.t or if layout
474 -- is enabled. The front end's layout phase currently treats types that
475 -- have discriminant-dependent arrays as not being static even when a
476 -- discriminant constraint on the type is static, and this leads to
477 -- problems with subtypes of type Ada.Tags.Dispatch_Table_Wrapper. ???
479 if Debug_Flag_Dot_T or else Frontend_Layout_On_Target then
480 Static_Dispatch_Tables := False;
481 end if;
483 -- Flip endian mode if -gnatd8 set
485 if Debug_Flag_8 then
486 Ttypes.Bytes_Big_Endian := not Ttypes.Bytes_Big_Endian;
487 end if;
489 -- Activate front end layout if debug flag -gnatdF is set
491 if Debug_Flag_FF then
492 Targparm.Frontend_Layout_On_Target := True;
493 end if;
495 -- Set and check exception mechanism
497 if Targparm.ZCX_By_Default_On_Target then
498 Exception_Mechanism := Back_End_Exceptions;
499 end if;
501 -- Set proper status for overflow check mechanism
503 -- If already set (by -gnato or above in SPARK or CodePeer mode) then we
504 -- have nothing to do.
506 if Opt.Suppress_Options.Overflow_Mode_General /= Not_Set then
507 null;
509 -- Otherwise set overflow mode defaults
511 else
512 -- Overflow checks are on by default (Suppress set False) except in
513 -- GNAT_Mode, where we want them off by default (we are not ready to
514 -- enable overflow checks in the compiler yet, for one thing the case
515 -- of 64-bit checks needs System.Arith_64 which is not a compiler
516 -- unit and it is a pain to try to include it in the compiler.
518 Suppress_Options.Suppress (Overflow_Check) := GNAT_Mode;
520 -- Set appropriate default overflow handling mode. Note: at present
521 -- we set STRICT in all three of the following cases. They are
522 -- separated because in the future we may make different choices.
524 -- By default set STRICT mode if -gnatg in effect
526 if GNAT_Mode then
527 Suppress_Options.Overflow_Mode_General := Strict;
528 Suppress_Options.Overflow_Mode_Assertions := Strict;
530 -- If we have backend divide and overflow checks, then by default
531 -- overflow checks are STRICT. Historically this code used to also
532 -- activate overflow checks, although no target currently has these
533 -- flags set, so this was dead code anyway.
535 elsif Targparm.Backend_Divide_Checks_On_Target
537 Targparm.Backend_Overflow_Checks_On_Target
538 then
539 Suppress_Options.Overflow_Mode_General := Strict;
540 Suppress_Options.Overflow_Mode_Assertions := Strict;
542 -- Otherwise for now, default is STRICT mode. This may change in the
543 -- future, but for now this is the compatible behavior with previous
544 -- versions of GNAT.
546 else
547 Suppress_Options.Overflow_Mode_General := Strict;
548 Suppress_Options.Overflow_Mode_Assertions := Strict;
549 end if;
550 end if;
552 -- Set default for atomic synchronization. As this synchronization
553 -- between atomic accesses can be expensive, and not typically needed
554 -- on some targets, an optional target parameter can turn the option
555 -- off. Note Atomic Synchronization is implemented as check.
557 Suppress_Options.Suppress (Atomic_Synchronization) :=
558 not Atomic_Sync_Default_On_Target;
560 -- Set switch indicating if back end can handle limited types, and
561 -- guarantee that no incorrect copies are made (e.g. in the context
562 -- of an if or case expression).
564 -- Debug flag -gnatd.L decisively sets usage on
566 if Debug_Flag_Dot_LL then
567 Back_End_Handles_Limited_Types := True;
569 -- If no debug flag, usage off for AAMP, VM, SCIL cases
571 elsif AAMP_On_Target
572 or else VM_Target /= No_VM
573 or else Generate_SCIL
574 then
575 Back_End_Handles_Limited_Types := False;
577 -- Otherwise normal gcc back end, for now still turn flag off by
578 -- default, since there are unresolved problems in the front end.
580 else
581 Back_End_Handles_Limited_Types := False;
582 end if;
584 -- If the inlining level has not been set by the user, compute it from
585 -- the optimization level: 1 at -O1/-O2 (and -Os), 2 at -O3 and above.
587 if Inline_Level = 0 then
588 if Optimization_Level < 3 then
589 Inline_Level := 1;
590 else
591 Inline_Level := 2;
592 end if;
593 end if;
595 -- Treat -gnatn as equivalent to -gnatN for non-GCC targets
597 if Inline_Active and not Front_End_Inlining then
599 -- We really should have a tag for this, what if we added a new
600 -- back end some day, it would not be true for this test, but it
601 -- would be non-GCC, so this is a bit troublesome ???
603 Front_End_Inlining := VM_Target /= No_VM or else AAMP_On_Target;
604 end if;
606 -- Set back end inlining indication
608 Back_End_Inlining :=
610 -- No back end inlining if inlining is suppressed
612 not Suppress_All_Inlining
614 -- No back end inlining available for VM targets
616 and then VM_Target = No_VM
618 -- No back end inlining available on AAMP
620 and then not AAMP_On_Target
622 -- No back end inlining in GNATprove mode, since it just confuses
623 -- the formal verification process.
625 and then not GNATprove_Mode
627 -- No back end inlining if front end inlining explicitly enabled.
628 -- Done to minimize the output differences to customers still using
629 -- this deprecated switch; in addition, this behavior reduces the
630 -- output differences in old tests.
632 and then not Front_End_Inlining
634 -- Back end inlining is disabled if debug flag .z is set
636 and then not Debug_Flag_Dot_Z;
638 -- Output warning if -gnateE specified and cannot be supported
640 if Exception_Extra_Info
641 and then Restrict.No_Exception_Handlers_Set
642 then
643 Set_Standard_Error;
644 Write_Str
645 ("warning: extra exception information (-gnateE) was specified");
646 Write_Eol;
647 Write_Str
648 ("warning: this capability is not available in this configuration");
649 Write_Eol;
650 Set_Standard_Output;
651 end if;
653 -- Finally capture adjusted value of Suppress_Options as the initial
654 -- value for Scope_Suppress, which will be modified as we move from
655 -- scope to scope (by Suppress/Unsuppress/Overflow_Checks pragmas).
657 Sem.Scope_Suppress := Opt.Suppress_Options;
658 end Adjust_Global_Switches;
660 --------------------
661 -- Check_Bad_Body --
662 --------------------
664 procedure Check_Bad_Body is
665 Sname : Unit_Name_Type;
666 Src_Ind : Source_File_Index;
667 Fname : File_Name_Type;
669 procedure Bad_Body_Error (Msg : String);
670 -- Issue message for bad body found
672 --------------------
673 -- Bad_Body_Error --
674 --------------------
676 procedure Bad_Body_Error (Msg : String) is
677 begin
678 Error_Msg_N (Msg, Main_Unit_Node);
679 Error_Msg_File_1 := Fname;
680 Error_Msg_N ("remove incorrect body in file{!", Main_Unit_Node);
681 end Bad_Body_Error;
683 -- Start of processing for Check_Bad_Body
685 begin
686 -- Nothing to do if we are only checking syntax, because we don't know
687 -- enough to know if we require or forbid a body in this case.
689 if Operating_Mode = Check_Syntax then
690 return;
691 end if;
693 -- Check for body not allowed
695 if (Main_Kind = N_Package_Declaration
696 and then not Body_Required (Main_Unit_Node))
697 or else (Main_Kind = N_Generic_Package_Declaration
698 and then not Body_Required (Main_Unit_Node))
699 or else Main_Kind = N_Package_Renaming_Declaration
700 or else Main_Kind = N_Subprogram_Renaming_Declaration
701 or else Nkind (Original_Node (Unit (Main_Unit_Node)))
702 in N_Generic_Instantiation
703 then
704 Sname := Unit_Name (Main_Unit);
706 -- If we do not already have a body name, then get the body name
708 if not Is_Body_Name (Sname) then
709 Sname := Get_Body_Name (Sname);
710 end if;
712 Fname := Get_File_Name (Sname, Subunit => False);
713 Src_Ind := Load_Source_File (Fname);
715 -- Case where body is present and it is not a subunit. Exclude the
716 -- subunit case, because it has nothing to do with the package we are
717 -- compiling. It is illegal for a child unit and a subunit with the
718 -- same expanded name (RM 10.2(9)) to appear together in a partition,
719 -- but there is nothing to stop a compilation environment from having
720 -- both, and the test here simply allows that. If there is an attempt
721 -- to include both in a partition, this is diagnosed at bind time. In
722 -- Ada 83 mode this is not a warning case.
724 -- Note that in general we do not give the message if the file in
725 -- question does not look like a body. This includes weird cases,
726 -- but in particular means that if the file is just a No_Body pragma,
727 -- then we won't give the message (that's the whole point of this
728 -- pragma, to be used this way and to cause the body file to be
729 -- ignored in this context).
731 if Src_Ind /= No_Source_File
732 and then Source_File_Is_Body (Src_Ind)
733 then
734 Errout.Finalize (Last_Call => False);
736 Error_Msg_Unit_1 := Sname;
738 -- Ada 83 case of a package body being ignored. This is not an
739 -- error as far as the Ada 83 RM is concerned, but it is almost
740 -- certainly not what is wanted so output a warning. Give this
741 -- message only if there were no errors, since otherwise it may
742 -- be incorrect (we may have misinterpreted a junk spec as not
743 -- needing a body when it really does).
745 if Main_Kind = N_Package_Declaration
746 and then Ada_Version = Ada_83
747 and then Operating_Mode = Generate_Code
748 and then Distribution_Stub_Mode /= Generate_Caller_Stub_Body
749 and then not Compilation_Errors
750 then
751 Error_Msg_N
752 ("package $$ does not require a body??", Main_Unit_Node);
753 Error_Msg_File_1 := Fname;
754 Error_Msg_N ("body in file{ will be ignored??", Main_Unit_Node);
756 -- Ada 95 cases of a body file present when no body is
757 -- permitted. This we consider to be an error.
759 else
760 -- For generic instantiations, we never allow a body
762 if Nkind (Original_Node (Unit (Main_Unit_Node))) in
763 N_Generic_Instantiation
764 then
765 Bad_Body_Error
766 ("generic instantiation for $$ does not allow a body");
768 -- A library unit that is a renaming never allows a body
770 elsif Main_Kind in N_Renaming_Declaration then
771 Bad_Body_Error
772 ("renaming declaration for $$ does not allow a body!");
774 -- Remaining cases are packages and generic packages. Here
775 -- we only do the test if there are no previous errors,
776 -- because if there are errors, they may lead us to
777 -- incorrectly believe that a package does not allow a
778 -- body when in fact it does.
780 elsif not Compilation_Errors then
781 if Main_Kind = N_Package_Declaration then
782 Bad_Body_Error
783 ("package $$ does not allow a body!");
785 elsif Main_Kind = N_Generic_Package_Declaration then
786 Bad_Body_Error
787 ("generic package $$ does not allow a body!");
788 end if;
789 end if;
791 end if;
792 end if;
793 end if;
794 end Check_Bad_Body;
796 --------------------
797 -- Check_Rep_Info --
798 --------------------
800 procedure Check_Rep_Info is
801 begin
802 if List_Representation_Info /= 0
803 or else List_Representation_Info_Mechanisms
804 then
805 Set_Standard_Error;
806 Write_Eol;
807 Write_Str
808 ("cannot generate representation information, no code generated");
809 Write_Eol;
810 Write_Eol;
811 Set_Standard_Output;
812 end if;
813 end Check_Rep_Info;
815 ----------------------------------------
816 -- Post_Compilation_Validation_Checks --
817 ----------------------------------------
819 procedure Post_Compilation_Validation_Checks is
820 begin
821 -- Validate alignment check warnings. In some cases we generate warnings
822 -- about possible alignment errors because we don't know the alignment
823 -- that will be chosen by the back end. This routine is in charge of
824 -- getting rid of those warnings if we can tell they are not needed.
826 Checks.Validate_Alignment_Check_Warnings;
828 -- Validate unchecked conversions (using the values for size and
829 -- alignment annotated by the backend where possible).
831 Sem_Ch13.Validate_Unchecked_Conversions;
833 -- Validate address clauses (again using alignment values annotated
834 -- by the backend where possible).
836 Sem_Ch13.Validate_Address_Clauses;
838 -- Validate independence pragmas (again using values annotated by
839 -- the back end for component layout etc.)
841 Sem_Ch13.Validate_Independence;
842 end Post_Compilation_Validation_Checks;
844 -- Start of processing for Gnat1drv
846 begin
847 -- This inner block is set up to catch assertion errors and constraint
848 -- errors. Since the code for handling these errors can cause another
849 -- exception to be raised (namely Unrecoverable_Error), we need two
850 -- nested blocks, so that the outer one handles unrecoverable error.
852 begin
853 -- Initialize all packages. For the most part, these initialization
854 -- calls can be made in any order. Exceptions are as follows:
856 -- Lib.Initialize need to be called before Scan_Compiler_Arguments,
857 -- because it initializes a table filled by Scan_Compiler_Arguments.
859 Osint.Initialize;
860 Fmap.Reset_Tables;
861 Lib.Initialize;
862 Lib.Xref.Initialize;
863 Scan_Compiler_Arguments;
864 Osint.Add_Default_Search_Dirs;
866 Atree.Initialize;
867 Nlists.Initialize;
868 Sinput.Initialize;
869 Sem.Initialize;
870 Exp_CG.Initialize;
871 Csets.Initialize;
872 Uintp.Initialize;
873 Urealp.Initialize;
874 Errout.Initialize;
875 SCOs.Initialize;
876 Snames.Initialize;
877 Stringt.Initialize;
878 Inline.Initialize;
879 Par_SCO.Initialize;
880 Sem_Ch8.Initialize;
881 Sem_Ch12.Initialize;
882 Sem_Ch13.Initialize;
883 Sem_Elim.Initialize;
884 Sem_Eval.Initialize;
885 Sem_Type.Init_Interp_Tables;
887 -- Capture compilation date and time
889 Opt.Compilation_Time := System.OS_Lib.Current_Time_String;
891 -- Get the target parameters only when -gnats is not used, to avoid
892 -- failing when there is no default runtime.
894 if Operating_Mode /= Check_Syntax then
896 -- Acquire target parameters from system.ads (package System source)
898 Targparm_Acquire : declare
899 use Sinput;
901 S : Source_File_Index;
902 N : File_Name_Type;
904 begin
905 Name_Buffer (1 .. 10) := "system.ads";
906 Name_Len := 10;
907 N := Name_Find;
908 S := Load_Source_File (N);
910 -- Failed to read system.ads, fatal error
912 if S = No_Source_File then
913 Write_Line
914 ("fatal error, run-time library not installed correctly");
915 Write_Line ("cannot locate file system.ads");
916 raise Unrecoverable_Error;
918 -- Read system.ads successfully, remember its source index
920 else
921 System_Source_File_Index := S;
922 end if;
924 Targparm.Get_Target_Parameters
925 (System_Text => Source_Text (S),
926 Source_First => Source_First (S),
927 Source_Last => Source_Last (S),
928 Make_Id => Tbuild.Make_Id'Access,
929 Make_SC => Tbuild.Make_SC'Access,
930 Set_RND => Tbuild.Set_RND'Access);
932 -- Acquire configuration pragma information from Targparm
934 Restrict.Restrictions := Targparm.Restrictions_On_Target;
935 end Targparm_Acquire;
936 end if;
938 -- Perform various adjustments and settings of global switches
940 Adjust_Global_Switches;
942 -- Output copyright notice if full list mode unless we have a list
943 -- file, in which case we defer this so that it is output in the file.
945 if (Verbose_Mode or else (Full_List and then Full_List_File_Name = null))
947 -- Debug flag gnatd7 suppresses this copyright notice
949 and then not Debug_Flag_7
950 then
951 Write_Eol;
952 Write_Str ("GNAT ");
953 Write_Str (Gnat_Version_String);
954 Write_Eol;
955 Write_Str ("Copyright 1992-" & Current_Year
956 & ", Free Software Foundation, Inc.");
957 Write_Eol;
958 end if;
960 -- Check we do not have more than one source file, this happens only in
961 -- the case where the driver is called directly, it cannot happen when
962 -- gnat1 is invoked from gcc in the normal case.
964 if Osint.Number_Of_Files /= 1 then
965 Usage;
966 Write_Eol;
967 Osint.Fail ("you must provide one source file");
969 elsif Usage_Requested then
970 Usage;
971 end if;
973 -- Generate target dependent output file if requested
975 if Target_Dependent_Info_Write_Name /= null then
976 Set_Targ.Write_Target_Dependent_Values;
977 end if;
979 -- Call the front end
981 Original_Operating_Mode := Operating_Mode;
982 Frontend;
984 -- Exit with errors if the main source could not be parsed.
986 if Sinput.Main_Source_File = No_Source_File then
987 Errout.Finalize (Last_Call => True);
988 Errout.Output_Messages;
989 Exit_Program (E_Errors);
990 end if;
992 Main_Unit_Node := Cunit (Main_Unit);
993 Main_Kind := Nkind (Unit (Main_Unit_Node));
994 Check_Bad_Body;
996 -- In CodePeer mode we always delete old SCIL files before regenerating
997 -- new ones, in case of e.g. errors, and also to remove obsolete scilx
998 -- files generated by CodePeer itself.
1000 if CodePeer_Mode then
1001 Comperr.Delete_SCIL_Files;
1002 end if;
1004 -- Exit if compilation errors detected
1006 Errout.Finalize (Last_Call => False);
1008 if Compilation_Errors then
1009 Treepr.Tree_Dump;
1010 Post_Compilation_Validation_Checks;
1011 Errout.Output_Messages;
1012 Namet.Finalize;
1014 -- Generate ALI file if specially requested
1016 if Opt.Force_ALI_Tree_File then
1017 Write_ALI (Object => False);
1018 Tree_Gen;
1019 end if;
1021 Errout.Finalize (Last_Call => True);
1022 Exit_Program (E_Errors);
1023 end if;
1025 -- Set Generate_Code on main unit and its spec. We do this even if are
1026 -- not generating code, since Lib-Writ uses this to determine which
1027 -- units get written in the ali file.
1029 Set_Generate_Code (Main_Unit);
1031 -- If we have a corresponding spec, and it comes from source or it is
1032 -- not a generated spec for a child subprogram body, then we need object
1033 -- code for the spec unit as well.
1035 if Nkind (Unit (Main_Unit_Node)) in N_Unit_Body
1036 and then not Acts_As_Spec (Main_Unit_Node)
1037 then
1038 if Nkind (Unit (Main_Unit_Node)) = N_Subprogram_Body
1039 and then not Comes_From_Source (Library_Unit (Main_Unit_Node))
1040 then
1041 null;
1042 else
1043 Set_Generate_Code
1044 (Get_Cunit_Unit_Number (Library_Unit (Main_Unit_Node)));
1045 end if;
1046 end if;
1048 -- Case of no code required to be generated, exit indicating no error
1050 if Original_Operating_Mode = Check_Syntax then
1051 Treepr.Tree_Dump;
1052 Errout.Finalize (Last_Call => True);
1053 Errout.Output_Messages;
1054 Tree_Gen;
1055 Namet.Finalize;
1056 Check_Rep_Info;
1058 -- Use a goto instead of calling Exit_Program so that finalization
1059 -- occurs normally.
1061 goto End_Of_Program;
1063 elsif Original_Operating_Mode = Check_Semantics then
1064 Back_End_Mode := Declarations_Only;
1066 -- All remaining cases are cases in which the user requested that code
1067 -- be generated (i.e. no -gnatc or -gnats switch was used). Check if we
1068 -- can in fact satisfy this request.
1070 -- Cannot generate code if someone has turned off code generation for
1071 -- any reason at all. We will try to figure out a reason below.
1073 elsif Operating_Mode /= Generate_Code then
1074 Back_End_Mode := Skip;
1076 -- We can generate code for a subprogram body unless there were missing
1077 -- subunits. Note that we always generate code for all generic units (a
1078 -- change from some previous versions of GNAT).
1080 elsif Main_Kind = N_Subprogram_Body and then not Subunits_Missing then
1081 Back_End_Mode := Generate_Object;
1083 -- We can generate code for a package body unless there are subunits
1084 -- missing (note that we always generate code for generic units, which
1085 -- is a change from some earlier versions of GNAT).
1087 elsif Main_Kind = N_Package_Body and then not Subunits_Missing then
1088 Back_End_Mode := Generate_Object;
1090 -- We can generate code for a package declaration or a subprogram
1091 -- declaration only if it does not required a body.
1093 elsif Nkind_In (Main_Kind,
1094 N_Package_Declaration,
1095 N_Subprogram_Declaration)
1096 and then
1097 (not Body_Required (Main_Unit_Node)
1098 or else
1099 Distribution_Stub_Mode = Generate_Caller_Stub_Body)
1100 then
1101 Back_End_Mode := Generate_Object;
1103 -- We can generate code for a generic package declaration of a generic
1104 -- subprogram declaration only if does not require a body.
1106 elsif Nkind_In (Main_Kind, N_Generic_Package_Declaration,
1107 N_Generic_Subprogram_Declaration)
1108 and then not Body_Required (Main_Unit_Node)
1109 then
1110 Back_End_Mode := Generate_Object;
1112 -- Compilation units that are renamings do not require bodies, so we can
1113 -- generate code for them.
1115 elsif Nkind_In (Main_Kind, N_Package_Renaming_Declaration,
1116 N_Subprogram_Renaming_Declaration)
1117 then
1118 Back_End_Mode := Generate_Object;
1120 -- Compilation units that are generic renamings do not require bodies
1121 -- so we can generate code for them.
1123 elsif Main_Kind in N_Generic_Renaming_Declaration then
1124 Back_End_Mode := Generate_Object;
1126 -- It is not an error to analyze in CodePeer mode a spec which requires
1127 -- a body, in order to generate SCIL for this spec.
1129 elsif CodePeer_Mode then
1130 Back_End_Mode := Generate_Object;
1132 -- It is not an error to analyze in GNATprove mode a spec which requires
1133 -- a body, when the body is not available. During frame condition
1134 -- generation, the corresponding ALI file is generated. During
1135 -- analysis, the spec is analyzed.
1137 elsif GNATprove_Mode then
1138 Back_End_Mode := Declarations_Only;
1140 -- In all other cases (specs which have bodies, generics, and bodies
1141 -- where subunits are missing), we cannot generate code and we generate
1142 -- a warning message. Note that generic instantiations are gone at this
1143 -- stage since they have been replaced by their instances.
1145 else
1146 Back_End_Mode := Skip;
1147 end if;
1149 -- At this stage Back_End_Mode is set to indicate if the backend should
1150 -- be called to generate code. If it is Skip, then code generation has
1151 -- been turned off, even though code was requested by the original
1152 -- command. This is not an error from the user point of view, but it is
1153 -- an error from the point of view of the gcc driver, so we must exit
1154 -- with an error status.
1156 -- We generate an informative message (from the gcc point of view, it
1157 -- is an error message, but from the users point of view this is not an
1158 -- error, just a consequence of compiling something that cannot
1159 -- generate code).
1161 if Back_End_Mode = Skip then
1162 Set_Standard_Error;
1163 Write_Str ("cannot generate code for ");
1164 Write_Str ("file ");
1165 Write_Name (Unit_File_Name (Main_Unit));
1167 if Subunits_Missing then
1168 Write_Str (" (missing subunits)");
1169 Write_Eol;
1171 -- Force generation of ALI file, for backward compatibility
1173 Opt.Force_ALI_Tree_File := True;
1175 elsif Main_Kind = N_Subunit then
1176 Write_Str (" (subunit)");
1177 Write_Eol;
1179 -- Force generation of ALI file, for backward compatibility
1181 Opt.Force_ALI_Tree_File := True;
1183 elsif Main_Kind = N_Subprogram_Declaration then
1184 Write_Str (" (subprogram spec)");
1185 Write_Eol;
1187 -- Generic package body in GNAT implementation mode
1189 elsif Main_Kind = N_Package_Body and then GNAT_Mode then
1190 Write_Str (" (predefined generic)");
1191 Write_Eol;
1193 -- Force generation of ALI file, for backward compatibility
1195 Opt.Force_ALI_Tree_File := True;
1197 -- Only other case is a package spec
1199 else
1200 Write_Str (" (package spec)");
1201 Write_Eol;
1202 end if;
1204 Set_Standard_Output;
1206 Post_Compilation_Validation_Checks;
1207 Errout.Finalize (Last_Call => True);
1208 Errout.Output_Messages;
1209 Treepr.Tree_Dump;
1210 Tree_Gen;
1212 -- Generate ALI file if specially requested, or for missing subunits,
1213 -- subunits or predefined generic.
1215 if Opt.Force_ALI_Tree_File then
1216 Write_ALI (Object => False);
1217 end if;
1219 Namet.Finalize;
1220 Check_Rep_Info;
1222 -- Exit program with error indication, to kill object file
1224 Exit_Program (E_No_Code);
1225 end if;
1227 -- In -gnatc mode, we only do annotation if -gnatt or -gnatR is also set
1228 -- as indicated by Back_Annotate_Rep_Info being set to True.
1230 -- We don't call for annotations on a subunit, because to process those
1231 -- the back-end requires that the parent(s) be properly compiled.
1233 -- Annotation is suppressed for targets where front-end layout is
1234 -- enabled, because the front end determines representations.
1236 -- Annotation is also suppressed in the case of compiling for a VM,
1237 -- since representations are largely symbolic there.
1239 if Back_End_Mode = Declarations_Only
1240 and then
1241 (not (Back_Annotate_Rep_Info or Generate_SCIL or GNATprove_Mode)
1242 or else Main_Kind = N_Subunit
1243 or else Targparm.Frontend_Layout_On_Target
1244 or else Targparm.VM_Target /= No_VM)
1245 then
1246 Post_Compilation_Validation_Checks;
1247 Errout.Finalize (Last_Call => True);
1248 Errout.Output_Messages;
1249 Write_ALI (Object => False);
1250 Tree_Dump;
1251 Tree_Gen;
1252 Namet.Finalize;
1253 Check_Rep_Info;
1254 return;
1255 end if;
1257 -- Ensure that we properly register a dependency on system.ads, since
1258 -- even if we do not semantically depend on this, Targparm has read
1259 -- system parameters from the system.ads file.
1261 Lib.Writ.Ensure_System_Dependency;
1263 -- Add dependencies, if any, on preprocessing data file and on
1264 -- preprocessing definition file(s).
1266 Prepcomp.Add_Dependencies;
1268 -- In gnatprove mode we're writing the ALI much earlier than usual
1269 -- as flow analysis needs the file present in order to append its
1270 -- own globals to it.
1272 if GNATprove_Mode then
1274 -- Note: In GNATprove mode, an "object" file is always generated as
1275 -- the result of calling gnat1 or gnat2why, although this is not the
1276 -- same as the object file produced for compilation.
1278 Write_ALI (Object => True);
1279 end if;
1281 -- Back end needs to explicitly unlock tables it needs to touch
1283 Atree.Lock;
1284 Elists.Lock;
1285 Fname.UF.Lock;
1286 Inline.Lock;
1287 Lib.Lock;
1288 Nlists.Lock;
1289 Sem.Lock;
1290 Sinput.Lock;
1291 Namet.Lock;
1292 Stringt.Lock;
1294 -- Here we call the back end to generate the output code
1296 Generating_Code := True;
1297 Back_End.Call_Back_End (Back_End_Mode);
1299 -- Once the backend is complete, we unlock the names table. This call
1300 -- allows a few extra entries, needed for example for the file name for
1301 -- the library file output.
1303 Namet.Unlock;
1305 -- Generate the call-graph output of dispatching calls
1307 Exp_CG.Generate_CG_Output;
1309 -- Perform post compilation validation checks
1311 Post_Compilation_Validation_Checks;
1313 -- Now we complete output of errors, rep info and the tree info. These
1314 -- are delayed till now, since it is perfectly possible for gigi to
1315 -- generate errors, modify the tree (in particular by setting flags
1316 -- indicating that elaboration is required, and also to back annotate
1317 -- representation information for List_Rep_Info.
1319 Errout.Finalize (Last_Call => True);
1320 Errout.Output_Messages;
1321 List_Rep_Info (Ttypes.Bytes_Big_Endian);
1322 Inline.List_Inlining_Info;
1324 -- Only write the library if the backend did not generate any error
1325 -- messages. Otherwise signal errors to the driver program so that
1326 -- there will be no attempt to generate an object file.
1328 if Compilation_Errors then
1329 Treepr.Tree_Dump;
1330 Exit_Program (E_Errors);
1331 end if;
1333 if not GNATprove_Mode then
1334 Write_ALI (Object => (Back_End_Mode = Generate_Object));
1335 end if;
1337 if not Compilation_Errors then
1339 -- In case of ada backends, we need to make sure that the generated
1340 -- object file has a timestamp greater than the ALI file. We do this
1341 -- to make gnatmake happy when checking the ALI and obj timestamps,
1342 -- where it expects the object file being written after the ali file.
1344 -- Gnatmake's assumption is true for gcc platforms where the gcc
1345 -- wrapper needs to call the assembler after calling gnat1, but is
1346 -- not true for ada backends, where the object files are created
1347 -- directly by gnat1 (so are created before the ali file).
1349 Back_End.Gen_Or_Update_Object_File;
1350 end if;
1352 -- Generate ASIS tree after writing the ALI file, since in ASIS mode,
1353 -- Write_ALI may in fact result in further tree decoration from the
1354 -- original tree file. Note that we dump the tree just before generating
1355 -- it, so that the dump will exactly reflect what is written out.
1357 Treepr.Tree_Dump;
1358 Tree_Gen;
1360 -- Finalize name table and we are all done
1362 Namet.Finalize;
1364 exception
1365 -- Handle fatal internal compiler errors
1367 when Rtsfind.RE_Not_Available =>
1368 Comperr.Compiler_Abort ("RE_Not_Available");
1370 when System.Assertions.Assert_Failure =>
1371 Comperr.Compiler_Abort ("Assert_Failure");
1373 when Constraint_Error =>
1374 Comperr.Compiler_Abort ("Constraint_Error");
1376 when Program_Error =>
1377 Comperr.Compiler_Abort ("Program_Error");
1379 when Storage_Error =>
1381 -- Assume this is a bug. If it is real, the message will in any case
1382 -- say Storage_Error, giving a strong hint.
1384 Comperr.Compiler_Abort ("Storage_Error");
1385 end;
1387 <<End_Of_Program>>
1388 null;
1390 -- The outer exception handles an unrecoverable error
1392 exception
1393 when Unrecoverable_Error =>
1394 Errout.Finalize (Last_Call => True);
1395 Errout.Output_Messages;
1397 Set_Standard_Error;
1398 Write_Str ("compilation abandoned");
1399 Write_Eol;
1401 Set_Standard_Output;
1402 Source_Dump;
1403 Tree_Dump;
1404 Exit_Program (E_Errors);
1406 end Gnat1drv;