OSDN Git Service

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