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