OSDN Git Service

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