OSDN Git Service

2003-10-21 Arnaud Charlet <charlet@act-europe.fr>
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-load.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             P A R . L O A D                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2002 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
27 --  The Par.Load procedure loads all units that are definitely required before
28 --  it makes any sense at all to proceed with semantic analysis, including
29 --  with'ed units, corresponding specs for bodies, parents of child specs,
30 --  and parents of subunits. All these units are loaded and pointers installed
31 --  in the tree as described in the spec of package Lib.
32
33 with Fname;    use Fname;
34 with Fname.UF; use Fname.UF;
35 with Lib.Load; use Lib.Load;
36 with Uname;    use Uname;
37 with Namet;    use Namet;
38 with Casing;   use Casing;
39 with Opt;      use Opt;
40 with Osint;    use Osint;
41 with Sinput.L; use Sinput.L;
42 with Stylesw;  use Stylesw;
43 with Validsw;  use Validsw;
44
45 separate (Par)
46 procedure Load is
47
48    File_Name : File_Name_Type;
49    --  Name of file for current unit, derived from unit name
50
51    Cur_Unum : constant Unit_Number_Type := Current_Source_Unit;
52    --  Unit number of unit that we just finished parsing. Note that we need
53    --  to capture this, because Source_Unit will change as we parse new
54    --  source files in the multiple main source file case.
55
56    Curunit : constant Node_Id := Cunit (Cur_Unum);
57    --  Compilation unit node for current compilation unit
58
59    Loc : Source_Ptr := Sloc (Curunit);
60    --  Source location for compilation unit node
61
62    Save_Style_Check  : Boolean;
63    Save_Style_Checks : Style_Check_Options;
64    --  Save style check so it can be restored later
65
66    Save_Validity_Check  : Boolean;
67    Save_Validity_Checks : Validity_Check_Options;
68    --  Save validity check so it can be restored later
69
70    With_Cunit : Node_Id;
71    --  Compilation unit node for withed unit
72
73    Context_Node : Node_Id;
74    --  Next node in context items list
75
76    With_Node : Node_Id;
77    --  N_With_Clause node
78
79    Spec_Name : Unit_Name_Type;
80    --  Unit name of required spec
81
82    Body_Name : Unit_Name_Type;
83    --  Unit name of corresponding body
84
85    Unum : Unit_Number_Type;
86    --  Unit number of loaded unit
87
88    function Same_File_Name_Except_For_Case
89      (Expected_File_Name : File_Name_Type;
90       Actual_File_Name   : File_Name_Type)
91       return               Boolean;
92    --  Given an actual file name and an expected file name (the latter being
93    --  derived from the unit name), determine if they are the same except for
94    --  possibly different casing of letters.
95
96    function Same_File_Name_Except_For_Case
97      (Expected_File_Name : File_Name_Type;
98       Actual_File_Name   : File_Name_Type)
99       return               Boolean
100    is
101    begin
102       Get_Name_String (Actual_File_Name);
103       Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
104
105       declare
106          Lower_Case_Actual_File_Name : String (1 .. Name_Len);
107
108       begin
109          Lower_Case_Actual_File_Name := Name_Buffer (1 .. Name_Len);
110          Get_Name_String (Expected_File_Name);
111          Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
112          return Lower_Case_Actual_File_Name = Name_Buffer (1 .. Name_Len);
113       end;
114
115    end Same_File_Name_Except_For_Case;
116
117 --  Start of processing for Load
118
119 begin
120    --  Don't do any loads if we already had a fatal error
121
122    if Fatal_Error (Cur_Unum) then
123       return;
124    end if;
125
126    Save_Style_Check_Options (Save_Style_Checks);
127    Save_Style_Check := Opt.Style_Check;
128
129    Save_Validity_Check_Options (Save_Validity_Checks);
130    Save_Validity_Check := Opt.Validity_Checks_On;
131
132    --  If main unit, set Main_Unit_Entity (this will get overwritten if
133    --  the main unit has a separate spec, that happens later on in Load)
134
135    if Cur_Unum = Main_Unit then
136       Main_Unit_Entity := Cunit_Entity (Main_Unit);
137    end if;
138
139    --  If we have no unit name, things are seriously messed up by previous
140    --  errors, and we should not try to continue compilation.
141
142    if Unit_Name (Cur_Unum) = No_Name then
143       raise Unrecoverable_Error;
144    end if;
145
146    --  Next step, make sure that the unit name matches the file name
147    --  and issue a warning message if not. We only output this for the
148    --  main unit, since for other units it is more serious and is
149    --  caught in a separate test below.
150
151    File_Name :=
152      Get_File_Name
153        (Unit_Name (Cur_Unum),
154         Subunit => Nkind (Unit (Cunit (Cur_Unum))) = N_Subunit);
155
156    if Cur_Unum = Main_Unit
157      and then File_Name /= Unit_File_Name (Cur_Unum)
158      and then (File_Names_Case_Sensitive
159                 or not Same_File_Name_Except_For_Case
160                          (File_Name, Unit_File_Name (Cur_Unum)))
161    then
162       Error_Msg_Name_1 := File_Name;
163       Error_Msg
164         ("?file name does not match unit name, should be{", Sloc (Curunit));
165    end if;
166
167    --  For units other than the main unit, the expected unit name is set and
168    --  must be the same as the actual unit name, or we are in big trouble, and
169    --  abandon the compilation since there are situations where this really
170    --  gets us into bad trouble (e.g. some subunit situations).
171
172    if Cur_Unum /= Main_Unit
173      and then Expected_Unit (Cur_Unum) /= Unit_Name (Cur_Unum)
174    then
175       Loc := Error_Location (Cur_Unum);
176       Error_Msg_Name_1 := Unit_File_Name (Cur_Unum);
177       Get_Name_String (Error_Msg_Name_1);
178
179       --  Check for predefined file case
180
181       if Name_Len > 1
182         and then Name_Buffer (2) = '-'
183         and then (Name_Buffer (1) = 'a'
184                     or else
185                   Name_Buffer (1) = 's'
186                     or else
187                   Name_Buffer (1) = 'i'
188                     or else
189                   Name_Buffer (1) = 'g')
190       then
191          --  In the predefined file case, we know the user did not construct
192          --  their own package, but we got the wrong one. This means that the
193          --  name supplied by the user crunched to something we recognized,
194          --  but then the file did not contain the unit expected. Most likely
195          --  this is due to a misspelling, e.g.
196
197          --    with Ada.Calender;
198
199          --  This crunches to a-calend, which indeed contains the unit
200          --  Ada.Calendar, and we can diagnose the misspelling. This is
201          --  a simple heuristic, but it catches many common cases of
202          --  misspelling of predefined unit names without needing a full
203          --  list of them.
204
205          Error_Msg_Name_1 := Expected_Unit (Cur_Unum);
206          Error_Msg ("% is not a predefined library unit!", Loc);
207          Error_Msg_Name_1 := Unit_Name (Cur_Unum);
208          Error_Msg ("possible misspelling of %!", Loc);
209
210       --  Non-predefined file name case
211
212       else
213          Error_Msg ("file { does not contain expected unit!", Loc);
214          Error_Msg_Unit_1 := Expected_Unit (Cur_Unum);
215          Error_Msg ("expected unit $!", Loc);
216          Error_Msg_Unit_1 := Unit_Name (Cur_Unum);
217          Error_Msg ("found unit $!", Loc);
218       end if;
219
220       raise Unrecoverable_Error;
221    end if;
222
223    --  If current unit is a body, load its corresponding spec
224
225    if Nkind (Unit (Curunit)) = N_Package_Body
226      or else Nkind (Unit (Curunit)) = N_Subprogram_Body
227    then
228       Spec_Name := Get_Spec_Name (Unit_Name (Cur_Unum));
229       Unum :=
230         Load_Unit
231           (Load_Name  => Spec_Name,
232            Required   => False,
233            Subunit    => False,
234            Error_Node => Curunit,
235            Corr_Body  => Cur_Unum);
236
237       --  If we successfully load the unit, then set the spec pointer. Once
238       --  again note that if the loaded unit has a fatal error, Load will
239       --  have set our Fatal_Error flag to propagate this condition.
240
241       if Unum /= No_Unit then
242          Set_Library_Unit (Curunit, Cunit (Unum));
243
244          --  If this is a separate spec for the main unit, then we reset
245          --  Main_Unit_Entity to point to the entity for this separate spec
246
247          if Cur_Unum = Main_Unit then
248             Main_Unit_Entity := Cunit_Entity (Unum);
249          end if;
250
251       --  If we don't find the spec, then if we have a subprogram body, we
252       --  are still OK, we just have a case of a body acting as its own spec
253
254       elsif Nkind (Unit (Curunit)) = N_Subprogram_Body then
255          Set_Acts_As_Spec (Curunit, True);
256          Set_Library_Unit (Curunit, Curunit);
257
258       --  Otherwise we do have an error, repeat the load request for the spec
259       --  with Required set True to generate an appropriate error message.
260
261       else
262          Unum :=
263            Load_Unit
264              (Load_Name  => Spec_Name,
265               Required   => True,
266               Subunit    => False,
267               Error_Node => Curunit);
268          return;
269       end if;
270
271    --  If current unit is a child unit spec, load its parent
272
273    elsif Nkind (Unit (Curunit)) = N_Package_Declaration
274      or else Nkind (Unit (Curunit)) =  N_Subprogram_Declaration
275      or else Nkind (Unit (Curunit)) in N_Generic_Declaration
276      or else Nkind (Unit (Curunit)) in N_Generic_Instantiation
277      or else Nkind (Unit (Curunit)) in N_Renaming_Declaration
278    then
279       --  Turn style and validity checks off for parent unit
280
281       if not GNAT_Mode then
282          Reset_Style_Check_Options;
283          Reset_Validity_Check_Options;
284       end if;
285
286       Spec_Name := Get_Parent_Spec_Name (Unit_Name (Cur_Unum));
287
288       if Spec_Name /= No_Name then
289          Unum :=
290            Load_Unit
291              (Load_Name  => Spec_Name,
292               Required   => True,
293               Subunit    => False,
294               Error_Node => Curunit);
295
296          if Unum /= No_Unit then
297             Set_Parent_Spec (Unit (Curunit), Cunit (Unum));
298          end if;
299       end if;
300
301    --  If current unit is a subunit, then load its parent body
302
303    elsif Nkind (Unit (Curunit)) = N_Subunit then
304       Body_Name := Get_Parent_Body_Name (Unit_Name (Cur_Unum));
305       Unum :=
306         Load_Unit
307           (Load_Name  => Body_Name,
308            Required   => True,
309            Subunit    => True,
310            Error_Node => Name (Unit (Curunit)));
311
312       if Unum /= No_Unit then
313          Set_Library_Unit (Curunit, Cunit (Unum));
314       end if;
315
316    end if;
317
318    --  Now we load with'ed units, with style/validity checks turned off
319
320    if not GNAT_Mode then
321       Reset_Style_Check_Options;
322       Reset_Validity_Check_Options;
323    end if;
324
325    --  Loop through context items
326
327    Context_Node := First (Context_Items (Curunit));
328    while Present (Context_Node) loop
329
330       if Nkind (Context_Node) = N_With_Clause then
331          With_Node := Context_Node;
332          Spec_Name := Get_Unit_Name (With_Node);
333
334          Unum :=
335            Load_Unit
336              (Load_Name  => Spec_Name,
337               Required   => False,
338               Subunit    => False,
339               Error_Node => With_Node,
340               Renamings  => True);
341
342          --  If we find the unit, then set spec pointer in the N_With_Clause
343          --  to point to the compilation unit for the spec. Remember that
344          --  the Load routine itself sets our Fatal_Error flag if the loaded
345          --  unit gets a fatal error, so we don't need to worry about that.
346
347          if Unum /= No_Unit then
348             Set_Library_Unit (With_Node, Cunit (Unum));
349
350          --  If the spec isn't found, then try finding the corresponding
351          --  body, since it is possible that we have a subprogram body
352          --  that is acting as a spec (since no spec is present).
353
354          else
355             Body_Name := Get_Body_Name (Spec_Name);
356             Unum :=
357               Load_Unit
358                 (Load_Name  => Body_Name,
359                  Required   => False,
360                  Subunit    => False,
361                  Error_Node => With_Node,
362                  Renamings  => True);
363
364             --  If we got a subprogram body, then mark that we are using
365             --  the body as a spec in the file table, and set the spec
366             --  pointer in the N_With_Clause to point to the body entity.
367
368             if Unum /= No_Unit
369               and then Nkind (Unit (Cunit (Unum))) = N_Subprogram_Body
370             then
371                With_Cunit := Cunit (Unum);
372                Set_Library_Unit (With_Node, With_Cunit);
373                Set_Acts_As_Spec (With_Cunit, True);
374                Set_Library_Unit (With_Cunit, With_Cunit);
375
376             --  If we couldn't find the body, or if it wasn't a body spec
377             --  then we are in trouble. We make one more call to Load to
378             --  require the spec. We know it will fail of course, the
379             --  purpose is to generate the required error message (we prefer
380             --  that this message refer to the missing spec, not the body)
381
382             else
383                Unum :=
384                  Load_Unit
385                    (Load_Name  => Spec_Name,
386                     Required   => True,
387                     Subunit    => False,
388                     Error_Node => With_Node,
389                     Renamings  => True);
390
391                --  Here we create a dummy package unit for the missing unit
392
393                Unum := Create_Dummy_Package_Unit (With_Node, Spec_Name);
394                Set_Library_Unit (With_Node, Cunit (Unum));
395             end if;
396          end if;
397       end if;
398
399       Next (Context_Node);
400    end loop;
401
402    --  Restore style/validity check mode for main unit
403
404    Set_Style_Check_Options (Save_Style_Checks);
405    Opt.Style_Check := Save_Style_Check;
406    Set_Validity_Check_Options (Save_Validity_Checks);
407    Opt.Validity_Checks_On := Save_Validity_Check;
408 end Load;