OSDN Git Service

2008-03-26 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / frontend.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             F R O N T E N D                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with System.Strings; use System.Strings;
27
28 with Atree;    use Atree;
29 with Checks;
30 with CStand;
31 with Debug;    use Debug;
32 with Elists;
33 with Exp_Dbug;
34 with Fmap;
35 with Fname.UF;
36 with Inline;   use Inline;
37 with Lib;      use Lib;
38 with Lib.Load; use Lib.Load;
39 with Live;     use Live;
40 with Namet;    use Namet;
41 with Nlists;   use Nlists;
42 with Opt;      use Opt;
43 with Osint;
44 with Par;
45 with Prepcomp;
46 with Rtsfind;
47 with Sprint;
48 with Scn;      use Scn;
49 with Sem;      use Sem;
50 with Sem_Ch8;  use Sem_Ch8;
51 with Sem_Elab; use Sem_Elab;
52 with Sem_Prag; use Sem_Prag;
53 with Sem_Warn; use Sem_Warn;
54 with Sinfo;    use Sinfo;
55 with Sinput;   use Sinput;
56 with Sinput.L; use Sinput.L;
57 with Targparm; use Targparm;
58 with Tbuild;   use Tbuild;
59 with Types;    use Types;
60
61 procedure Frontend is
62    Config_Pragmas : List_Id;
63    --  Gather configuration pragmas
64
65 begin
66    --  Carry out package initializations. These are initializations which
67    --  might logically be performed at elaboration time, were it not for
68    --  the fact that we may be doing things more than once in the big loop
69    --  over files. Like elaboration, the order in which these calls are
70    --  made is in some cases important. For example, Lib cannot be
71    --  initialized until Namet, since it uses names table entries.
72
73    Rtsfind.Initialize;
74    Atree.Initialize;
75    Nlists.Initialize;
76    Elists.Initialize;
77    Lib.Load.Initialize;
78    Sem_Ch8.Initialize;
79    Fname.UF.Initialize;
80    Checks.Initialize;
81    Sem_Warn.Initialize;
82
83    --  Create package Standard
84
85    CStand.Create_Standard;
86
87    --  Check possible symbol definitions specified by -gnateD switches
88
89    Prepcomp.Process_Command_Line_Symbol_Definitions;
90
91    --  If -gnatep= was specified, parse the preprocessing data file
92
93    if Preprocessing_Data_File /= null then
94       Name_Len := Preprocessing_Data_File'Length;
95       Name_Buffer (1 .. Name_Len) := Preprocessing_Data_File.all;
96       Prepcomp.Parse_Preprocessing_Data_File (Name_Find);
97
98    --  Otherwise, check if there were preprocessing symbols on the command
99    --  line and set preprocessing if there are.
100
101    else
102       Prepcomp.Check_Symbols;
103    end if;
104
105    --  Now that the preprocessing situation is established, we are able to
106    --  load the main source (this is no longer done by Lib.Load.Initalize).
107
108    Lib.Load.Load_Main_Source;
109
110    --  Return immediately if the main source could not be parsed
111
112    if Sinput.Main_Source_File = No_Source_File then
113       return;
114    end if;
115
116    --  Read and process configuration pragma files if present
117
118    declare
119       Save_Style_Check : constant Boolean := Opt.Style_Check;
120       --  Save style check mode so it can be restored later
121
122       Source_Config_File : Source_File_Index;
123       --  Source reference for -gnatec configuration file
124
125       Prag : Node_Id;
126
127    begin
128       --  We always analyze config files with style checks off, since
129       --  we don't want a miscellaneous gnat.adc that is around to
130       --  discombobulate intended -gnatg or -gnaty compilations. We
131       --  also disconnect checking for maximum line length.
132
133       Opt.Style_Check := False;
134       Style_Check := False;
135
136       --  Capture current suppress options, which may get modified
137
138       Scope_Suppress := Opt.Suppress_Options;
139
140       --  First deal with gnat.adc file
141
142       if Opt.Config_File then
143          Name_Buffer (1 .. 8) := "gnat.adc";
144          Name_Len := 8;
145          Source_gnat_adc := Load_Config_File (Name_Enter);
146
147          if Source_gnat_adc /= No_Source_File then
148             Initialize_Scanner (No_Unit, Source_gnat_adc);
149             Config_Pragmas := Par (Configuration_Pragmas => True);
150
151          else
152             Config_Pragmas := Empty_List;
153          end if;
154
155       else
156          Config_Pragmas := Empty_List;
157       end if;
158
159       --  Now deal with specified config pragmas files if there are any
160
161       if Opt.Config_File_Names /= null then
162          for Index in Opt.Config_File_Names'Range loop
163             Name_Len := Config_File_Names (Index)'Length;
164             Name_Buffer (1 .. Name_Len) := Config_File_Names (Index).all;
165             Source_Config_File := Load_Config_File (Name_Enter);
166
167             if Source_Config_File = No_Source_File then
168                Osint.Fail
169                  ("cannot find configuration pragmas file ",
170                   Config_File_Names (Index).all);
171             end if;
172
173             Initialize_Scanner (No_Unit, Source_Config_File);
174             Append_List_To
175               (Config_Pragmas, Par (Configuration_Pragmas => True));
176          end loop;
177       end if;
178
179       --  Now analyze all pragmas except those whose analysis must be
180       --  deferred till after the main unit is analyzed.
181
182       if Config_Pragmas /= Error_List
183         and then Operating_Mode /= Check_Syntax
184       then
185          Prag := First (Config_Pragmas);
186          while Present (Prag) loop
187             if not Delay_Config_Pragma_Analyze (Prag) then
188                Analyze_Pragma (Prag);
189             end if;
190
191             Next (Prag);
192          end loop;
193       end if;
194
195       --  Restore style check, but if config file turned on checks, leave on!
196
197       Opt.Style_Check := Save_Style_Check or Style_Check;
198
199       --  Capture any modifications to suppress options from config pragmas
200
201       Opt.Suppress_Options := Scope_Suppress;
202    end;
203
204    --  If there was a -gnatem switch, initialize the mappings of unit names to
205    --  file names and of file names to path names from the mapping file.
206
207    if Mapping_File_Name /= null then
208       Fmap.Initialize (Mapping_File_Name.all);
209    end if;
210
211    --  Adjust Optimize_Alignment mode from debug switches if necessary
212
213    if Debug_Flag_Dot_SS then
214       Optimize_Alignment := 'S';
215    elsif Debug_Flag_Dot_TT then
216       Optimize_Alignment := 'T';
217    end if;
218
219    --  We have now processed the command line switches, and the gnat.adc
220    --  file, so this is the point at which we want to capture the values
221    --  of the configuration switches (see Opt for further details).
222
223    Opt.Register_Opt_Config_Switches;
224
225    --  Check for file which contains No_Body pragma
226
227    if Source_File_Is_No_Body (Source_Index (Main_Unit)) then
228       Change_Main_Unit_To_Spec;
229    end if;
230
231    --  Initialize the scanner. Note that we do this after the call to
232    --  Create_Standard, which uses the scanner in its processing of
233    --  floating-point bounds.
234
235    Initialize_Scanner (Main_Unit, Source_Index (Main_Unit));
236
237    --  Here we call the parser to parse the compilation unit (or units in
238    --  the check syntax mode, but in that case we won't go on to the
239    --  semantics in any case).
240
241    Discard_List (Par (Configuration_Pragmas => False));
242
243    --  The main unit is now loaded, and subunits of it can be loaded,
244    --  without reporting spurious loading circularities.
245
246    Set_Loading (Main_Unit, False);
247
248    --  Now that the main unit is installed, we can complete the analysis
249    --  of the pragmas in gnat.adc and the configuration file, that require
250    --  a context for their semantic processing.
251
252    if Config_Pragmas /= Error_List
253      and then Operating_Mode /= Check_Syntax
254    then
255       --  Pragmas that require some semantic activity, such as
256       --  Interrupt_State, cannot be processed until the main unit
257       --  is installed, because they require a compilation unit on
258       --  which to attach with_clauses, etc. So analyze them now.
259
260       declare
261          Prag : Node_Id;
262
263       begin
264          Prag := First (Config_Pragmas);
265          while Present (Prag) loop
266             if Delay_Config_Pragma_Analyze (Prag) then
267                Analyze_Pragma (Prag);
268             end if;
269
270             Next (Prag);
271          end loop;
272       end;
273    end if;
274
275    --  Now on to the semantics. Skip if in syntax only mode
276
277    if Operating_Mode /= Check_Syntax then
278
279       --  Install the configuration pragmas in the tree
280
281       Set_Config_Pragmas (Aux_Decls_Node (Cunit (Main_Unit)), Config_Pragmas);
282
283       --  Following steps are skipped if we had a fatal error during parsing
284
285       if not Fatal_Error (Main_Unit) then
286
287          --  Reset Operating_Mode to Check_Semantics for subunits. We cannot
288          --  actually generate code for subunits, so we suppress expansion.
289          --  This also corrects certain problems that occur if we try to
290          --  incorporate subunits at a lower level.
291
292          if Operating_Mode = Generate_Code
293             and then Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
294          then
295             Operating_Mode := Check_Semantics;
296          end if;
297
298          --  Analyze (and possibly expand) main unit
299
300          Scope_Suppress := Suppress_Options;
301          Semantics (Cunit (Main_Unit));
302
303          --  Cleanup processing after completing main analysis
304
305          if Operating_Mode = Generate_Code
306             or else (Operating_Mode = Check_Semantics
307                       and then ASIS_Mode)
308          then
309             Instantiate_Bodies;
310          end if;
311
312          if Operating_Mode = Generate_Code then
313             if Inline_Processing_Required then
314                Analyze_Inlined_Bodies;
315             end if;
316
317             --  Remove entities from program that do not have any
318             --  execution time references.
319
320             if Debug_Flag_UU then
321                Collect_Garbage_Entities;
322             end if;
323
324             Check_Elab_Calls;
325          end if;
326
327          --  List library units if requested
328
329          if List_Units then
330             Lib.List;
331          end if;
332
333          --  Output waiting warning messages
334
335          Sem_Warn.Output_Non_Modifed_In_Out_Warnings;
336          Sem_Warn.Output_Unreferenced_Messages;
337          Sem_Warn.Check_Unused_Withs;
338          Sem_Warn.Output_Unused_Warnings_Off_Warnings;
339       end if;
340    end if;
341
342    --  Qualify all entity names in inner packages, package bodies, etc.,
343    --  except when compiling for the VM back-ends, which depend on
344    --  having unqualified names in certain cases and handles the
345    --  generation of qualified names when needed.
346
347    if VM_Target = No_VM then
348       Exp_Dbug.Qualify_All_Entity_Names;
349    end if;
350
351    --  Dump the source now. Note that we do this as soon as the analysis
352    --  of the tree is complete, because it is not just a dump in the case
353    --  of -gnatD, where it rewrites all source locations in the tree.
354
355    Sprint.Source_Dump;
356
357    --  If a mapping file has been specified by a -gnatem switch, update
358    --  it if there has been some sourcs that were not in the mappings.
359
360    if Mapping_File_Name /= null then
361       Fmap.Update_Mapping_File (Mapping_File_Name.all);
362    end if;
363
364    return;
365 end Frontend;