OSDN Git Service

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