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-2003 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 2,  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 COPYING.  If not, write --
19-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20-- MA 02111-1307, USA.                                                      --
21--                                                                          --
22-- GNAT was originally developed  by the GNAT team at  New York University. --
23-- Extensive contributions were provided by Ada Core Technologies Inc.      --
24--                                                                          --
25------------------------------------------------------------------------------
26
27with GNAT.Strings; use GNAT.Strings;
28
29with Atree;    use Atree;
30with Checks;
31with CStand;
32with Debug;    use Debug;
33with Elists;
34with Exp_Ch11;
35with Exp_Dbug;
36with Fmap;
37with Fname.UF;
38with Hostparm; use Hostparm;
39with Inline;   use Inline;
40with Lib;      use Lib;
41with Lib.Load; use Lib.Load;
42with Live;     use Live;
43with Namet;    use Namet;
44with Nlists;   use Nlists;
45with Opt;      use Opt;
46with Osint;
47with Output;   use Output;
48with Par;
49with Prepcomp;
50with Rtsfind;
51with Sprint;
52with Scn;      use Scn;
53with Sem;      use Sem;
54with Sem_Ch8;  use Sem_Ch8;
55with Sem_Elab; use Sem_Elab;
56with Sem_Prag; use Sem_Prag;
57with Sem_Warn; use Sem_Warn;
58with Sinfo;    use Sinfo;
59with Sinput;   use Sinput;
60with Sinput.L; use Sinput.L;
61with Tbuild;   use Tbuild;
62with Types;    use Types;
63
64procedure Frontend is
65      Config_Pragmas : List_Id;
66      --  Gather configuration pragmas
67
68begin
69   --  Carry out package initializations. These are initializations which
70   --  might logically be performed at elaboration time, were it not for
71   --  the fact that we may be doing things more than once in the big loop
72   --  over files. Like elaboration, the order in which these calls are
73   --  made is in some cases important. For example, Lib cannot be
74   --  initialized until Namet, 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_Ch8.Initialize;
82   Fname.UF.Initialize;
83   Exp_Ch11.Initialize;
84   Checks.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.Initalize).
110
111   Lib.Load.Load_Main_Source;
112
113   --  Read and process configuration pragma files if present
114
115   declare
116      Save_Style_Check : constant Boolean := Opt.Style_Check;
117      --  Save style check mode so it can be restored later
118
119      Source_Config_File : Source_File_Index;
120      --  Source reference for -gnatec configuration file
121
122      Prag : Node_Id;
123
124   begin
125      --  We always analyze config files with style checks off, since
126      --  we don't want a miscellaneous gnat.adc that is around to
127      --  discombobulate intended -gnatg or -gnaty compilations.
128
129      Opt.Style_Check := False;
130      Style_Check := False;
131
132      --  Capture current suppress options, which may get modified
133
134      Scope_Suppress := Opt.Suppress_Options;
135
136      --  First deal with gnat.adc file
137
138      if Opt.Config_File then
139         Name_Buffer (1 .. 8) := "gnat.adc";
140         Name_Len := 8;
141         Source_gnat_adc := Load_Config_File (Name_Enter);
142
143         if Source_gnat_adc /= No_Source_File then
144            Initialize_Scanner (No_Unit, Source_gnat_adc);
145            Config_Pragmas := Par (Configuration_Pragmas => True);
146
147         else
148            Config_Pragmas := Empty_List;
149         end if;
150
151      else
152         Config_Pragmas := Empty_List;
153      end if;
154
155      --  Now deal with specified config pragmas files if there are any
156
157      if Opt.Config_File_Names /= null then
158         for Index in Opt.Config_File_Names'Range loop
159            Name_Len := Config_File_Names (Index)'Length;
160            Name_Buffer (1 .. Name_Len) := Config_File_Names (Index).all;
161            Source_Config_File := Load_Config_File (Name_Enter);
162
163            if Source_Config_File = No_Source_File then
164               Osint.Fail
165                 ("cannot find configuration pragmas file ",
166                  Config_File_Names (Index).all);
167            end if;
168
169            Initialize_Scanner (No_Unit, Source_Config_File);
170            Append_List_To
171              (Config_Pragmas, Par (Configuration_Pragmas => True));
172         end loop;
173      end if;
174
175      --  Now analyze all pragmas except those whose analysis must be
176      --  deferred till after the main unit is analyzed.
177
178      if Config_Pragmas /= Error_List
179        and then Operating_Mode /= Check_Syntax
180      then
181         Prag := First (Config_Pragmas);
182         while Present (Prag) loop
183            if not Delay_Config_Pragma_Analyze (Prag) then
184               Analyze_Pragma (Prag);
185            end if;
186
187            Next (Prag);
188         end loop;
189      end if;
190
191      --  Restore style check, but if config file turned on checks, leave on!
192
193      Opt.Style_Check := Save_Style_Check or Style_Check;
194
195      --  Capture any modifications to suppress options from config pragmas
196
197      Opt.Suppress_Options := Scope_Suppress;
198   end;
199
200   --  If there was a -gnatem switch, initialize the mappings of unit names to
201   --  file names and of file names to path names from the mapping file.
202
203   if Mapping_File_Name /= null then
204      Fmap.Initialize (Mapping_File_Name.all);
205   end if;
206
207   --  We have now processed the command line switches, and the gnat.adc
208   --  file, so this is the point at which we want to capture the values
209   --  of the configuration switches (see Opt for further details).
210
211   Opt.Register_Opt_Config_Switches;
212
213   --  Initialize the scanner. Note that we do this after the call to
214   --  Create_Standard, which uses the scanner in its processing of
215   --  floating-point bounds.
216
217   Initialize_Scanner (Main_Unit, Source_Index (Main_Unit));
218
219   --  Output header if in verbose mode or full list mode
220
221   if Verbose_Mode or Full_List then
222      Write_Eol;
223
224      if Operating_Mode = Generate_Code then
225         Write_Str ("Compiling: ");
226      else
227         Write_Str ("Checking: ");
228      end if;
229
230      Write_Name (Full_File_Name (Current_Source_File));
231
232      if not Debug_Flag_7 then
233         Write_Str (" (source file time stamp: ");
234         Write_Time_Stamp (Current_Source_File);
235         Write_Char (')');
236      end if;
237
238      Write_Eol;
239   end if;
240
241   --  Here we call the parser to parse the compilation unit (or units in
242   --  the check syntax mode, but in that case we won't go on to the
243   --  semantics in any case).
244
245   Discard_List (Par (Configuration_Pragmas => False));
246
247   --  The main unit is now loaded, and subunits of it can be loaded,
248   --  without reporting spurious loading circularities.
249
250   Set_Loading (Main_Unit, False);
251
252   --  Now that the main unit is installed, we can complete the analysis
253   --  of the pragmas in gnat.adc and the configuration file, that require
254   --  a context for their semantic processing.
255
256   if Config_Pragmas /= Error_List
257     and then Operating_Mode /= Check_Syntax
258   then
259      --  Pragmas that require some semantic activity, such as
260      --  Interrupt_State, cannot be processed until the main unit
261      --  is installed, because they require a compilation unit on
262      --  which to attach with_clauses, etc. So analyze them now.
263
264      declare
265         Prag : Node_Id;
266
267      begin
268         Prag := First (Config_Pragmas);
269         while Present (Prag) loop
270            if Delay_Config_Pragma_Analyze (Prag) then
271               Analyze_Pragma (Prag);
272            end if;
273
274            Next (Prag);
275         end loop;
276      end;
277   end if;
278
279   --  Now on to the semantics. Skip if in syntax only mode
280
281   if Operating_Mode /= Check_Syntax then
282
283      --  Install the configuration pragmas in the tree
284
285      Set_Config_Pragmas (Aux_Decls_Node (Cunit (Main_Unit)), Config_Pragmas);
286
287      --  Following steps are skipped if we had a fatal error during parsing
288
289      if not Fatal_Error (Main_Unit) then
290
291         --  Reset Operating_Mode to Check_Semantics for subunits. We cannot
292         --  actually generate code for subunits, so we suppress expansion.
293         --  This also corrects certain problems that occur if we try to
294         --  incorporate subunits at a lower level.
295
296         if Operating_Mode = Generate_Code
297            and then Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
298         then
299            Operating_Mode := Check_Semantics;
300         end if;
301
302         --  Analyze (and possibly expand) main unit
303
304         Scope_Suppress := Suppress_Options;
305         Semantics (Cunit (Main_Unit));
306
307         --  Cleanup processing after completing main analysis
308
309         if Operating_Mode = Generate_Code
310            or else (Operating_Mode = Check_Semantics
311                      and then ASIS_Mode)
312         then
313            Instantiate_Bodies;
314         end if;
315
316         if Operating_Mode = Generate_Code then
317            if Inline_Processing_Required then
318               Analyze_Inlined_Bodies;
319            end if;
320
321            --  Remove entities from program that do not have any
322            --  execution time references.
323
324            if Debug_Flag_UU then
325               Collect_Garbage_Entities;
326            end if;
327
328            Check_Elab_Calls;
329
330            --  Build unit exception table. We leave this up to the end to
331            --  make sure that all the necessary information is at hand.
332
333            Exp_Ch11.Generate_Unit_Exception_Table;
334         end if;
335
336         --  List library units if requested
337
338         if List_Units then
339            Lib.List;
340         end if;
341
342         --  Output any messages for unreferenced entities
343
344         Output_Unreferenced_Messages;
345         Sem_Warn.Check_Unused_Withs;
346      end if;
347   end if;
348
349   --  Qualify all entity names in inner packages, package bodies, etc.,
350   --  except when compiling for the JVM back end, which depends on
351   --  having unqualified names in certain cases and handles the
352   --  generation of qualified names when needed.
353
354   if not Java_VM then
355      Exp_Dbug.Qualify_All_Entity_Names;
356   end if;
357
358   --  Dump the source now. Note that we do this as soon as the analysis
359   --  of the tree is complete, because it is not just a dump in the case
360   --  of -gnatD, where it rewrites all source locations in the tree.
361
362   Sprint.Source_Dump;
363
364   --  If a mapping file has been specified by a -gnatem switch, update
365   --  it if there has been some sourcs that were not in the mappings.
366
367   if Mapping_File_Name /= null then
368      Fmap.Update_Mapping_File (Mapping_File_Name.all);
369   end if;
370
371   return;
372end Frontend;
373