OSDN Git Service

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