OSDN Git Service

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