OSDN Git Service

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