OSDN Git Service

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