* c-decl.c (duplicate_decls): Conditionalize DECL_SAVED_TREE copy.
[official-gcc.git] / gcc / ada / par-load.adb
blob39934caacb0c73aa8fafd0229c85ef8770be8cf8
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P A R . L O A D --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.60 $
10 -- --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 -- --
27 ------------------------------------------------------------------------------
29 -- The Par.Load procedure loads all units that are definitely required before
30 -- it makes any sense at all to proceed with semantic analysis, including
31 -- with'ed units, corresponding specs for bodies, parents of child specs,
32 -- and parents of subunits. All these units are loaded and pointers installed
33 -- in the tree as described in the spec of package Lib.
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
37 with Lib.Load; use Lib.Load;
38 with Uname; use Uname;
39 with Namet; use Namet;
40 with Casing; use Casing;
41 with Opt; use Opt;
42 with Osint; use Osint;
43 with Sinput.L; use Sinput.L;
44 with Stylesw; use Stylesw;
45 with Validsw; use Validsw;
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 : 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 -- In the predefined file case, we know the user did not construct
194 -- their own package, but we got the wrong one. This means that the
195 -- name supplied by the user crunched to something we recognized,
196 -- but then the file did not contain the unit expected. Most likely
197 -- this is due to a misspelling, e.g.
199 -- with Ada.Calender;
201 -- This crunches to a-calend, which indeed contains the unit
202 -- Ada.Calendar, and we can diagnose the misspelling. This is
203 -- a simple heuristic, but it catches many common cases of
204 -- misspelling of predefined unit names without needing a full
205 -- list of them.
207 Error_Msg_Name_1 := Expected_Unit (Cur_Unum);
208 Error_Msg ("% is not a predefined library unit!", Loc);
209 Error_Msg_Name_1 := Unit_Name (Cur_Unum);
210 Error_Msg ("possible misspelling of %!", Loc);
212 -- Non-predefined file name case
214 else
215 Error_Msg ("file { does not contain expected unit!", Loc);
216 Error_Msg_Unit_1 := Expected_Unit (Cur_Unum);
217 Error_Msg ("expected unit $!", Loc);
218 Error_Msg_Unit_1 := Unit_Name (Cur_Unum);
219 Error_Msg ("found unit $!", Loc);
220 end if;
222 raise Unrecoverable_Error;
223 end if;
225 -- If current unit is a body, load its corresponding spec
227 if Nkind (Unit (Curunit)) = N_Package_Body
228 or else Nkind (Unit (Curunit)) = N_Subprogram_Body
229 then
230 Spec_Name := Get_Spec_Name (Unit_Name (Cur_Unum));
231 Unum :=
232 Load_Unit
233 (Load_Name => Spec_Name,
234 Required => False,
235 Subunit => False,
236 Error_Node => Curunit,
237 Corr_Body => Cur_Unum);
239 -- If we successfully load the unit, then set the spec pointer. Once
240 -- again note that if the loaded unit has a fatal error, Load will
241 -- have set our Fatal_Error flag to propagate this condition.
243 if Unum /= No_Unit then
244 Set_Library_Unit (Curunit, Cunit (Unum));
246 -- If this is a separate spec for the main unit, then we reset
247 -- Main_Unit_Entity to point to the entity for this separate spec
249 if Cur_Unum = Main_Unit then
250 Main_Unit_Entity := Cunit_Entity (Unum);
251 end if;
253 -- If we don't find the spec, then if we have a subprogram body, we
254 -- are still OK, we just have a case of a body acting as its own spec
256 elsif Nkind (Unit (Curunit)) = N_Subprogram_Body then
257 Set_Acts_As_Spec (Curunit, True);
258 Set_Library_Unit (Curunit, Curunit);
260 -- Otherwise we do have an error, repeat the load request for the spec
261 -- with Required set True to generate an appropriate error message.
263 else
264 Unum :=
265 Load_Unit
266 (Load_Name => Spec_Name,
267 Required => True,
268 Subunit => False,
269 Error_Node => Curunit);
270 return;
271 end if;
273 -- If current unit is a child unit spec, load its parent
275 elsif Nkind (Unit (Curunit)) = N_Package_Declaration
276 or else Nkind (Unit (Curunit)) = N_Subprogram_Declaration
277 or else Nkind (Unit (Curunit)) in N_Generic_Declaration
278 or else Nkind (Unit (Curunit)) in N_Generic_Instantiation
279 or else Nkind (Unit (Curunit)) in N_Renaming_Declaration
280 then
281 -- Turn style and validity checks off for parent unit
283 if not GNAT_Mode then
284 Reset_Style_Check_Options;
285 Reset_Validity_Check_Options;
286 end if;
288 Spec_Name := Get_Parent_Spec_Name (Unit_Name (Cur_Unum));
290 if Spec_Name /= No_Name then
291 Unum :=
292 Load_Unit
293 (Load_Name => Spec_Name,
294 Required => True,
295 Subunit => False,
296 Error_Node => Curunit);
298 if Unum /= No_Unit then
299 Set_Parent_Spec (Unit (Curunit), Cunit (Unum));
300 end if;
301 end if;
303 -- If current unit is a subunit, then load its parent body
305 elsif Nkind (Unit (Curunit)) = N_Subunit then
306 Body_Name := Get_Parent_Body_Name (Unit_Name (Cur_Unum));
307 Unum :=
308 Load_Unit
309 (Load_Name => Body_Name,
310 Required => True,
311 Subunit => True,
312 Error_Node => Name (Unit (Curunit)));
314 if Unum /= No_Unit then
315 Set_Library_Unit (Curunit, Cunit (Unum));
316 end if;
318 end if;
320 -- Now we load with'ed units, with style/validity checks turned off
322 if not GNAT_Mode then
323 Reset_Style_Check_Options;
324 Reset_Validity_Check_Options;
325 end if;
327 -- Loop through context items
329 Context_Node := First (Context_Items (Curunit));
330 while Present (Context_Node) loop
332 if Nkind (Context_Node) = N_With_Clause then
333 With_Node := Context_Node;
334 Spec_Name := Get_Unit_Name (With_Node);
336 Unum :=
337 Load_Unit
338 (Load_Name => Spec_Name,
339 Required => False,
340 Subunit => False,
341 Error_Node => With_Node,
342 Renamings => True);
344 -- If we find the unit, then set spec pointer in the N_With_Clause
345 -- to point to the compilation unit for the spec. Remember that
346 -- the Load routine itself sets our Fatal_Error flag if the loaded
347 -- unit gets a fatal error, so we don't need to worry about that.
349 if Unum /= No_Unit then
350 Set_Library_Unit (With_Node, Cunit (Unum));
352 -- If the spec isn't found, then try finding the corresponding
353 -- body, since it is possible that we have a subprogram body
354 -- that is acting as a spec (since no spec is present).
356 else
357 Body_Name := Get_Body_Name (Spec_Name);
358 Unum :=
359 Load_Unit
360 (Load_Name => Body_Name,
361 Required => False,
362 Subunit => False,
363 Error_Node => With_Node,
364 Renamings => True);
366 -- If we got a subprogram body, then mark that we are using
367 -- the body as a spec in the file table, and set the spec
368 -- pointer in the N_With_Clause to point to the body entity.
370 if Unum /= No_Unit
371 and then Nkind (Unit (Cunit (Unum))) = N_Subprogram_Body
372 then
373 With_Cunit := Cunit (Unum);
374 Set_Library_Unit (With_Node, With_Cunit);
375 Set_Acts_As_Spec (With_Cunit, True);
376 Set_Library_Unit (With_Cunit, With_Cunit);
378 -- If we couldn't find the body, or if it wasn't a body spec
379 -- then we are in trouble. We make one more call to Load to
380 -- require the spec. We know it will fail of course, the
381 -- purpose is to generate the required error message (we prefer
382 -- that this message refer to the missing spec, not the body)
384 else
385 Unum :=
386 Load_Unit
387 (Load_Name => Spec_Name,
388 Required => True,
389 Subunit => False,
390 Error_Node => With_Node,
391 Renamings => True);
393 -- Here we create a dummy package unit for the missing unit
395 Unum := Create_Dummy_Package_Unit (With_Node, Spec_Name);
396 Set_Library_Unit (With_Node, Cunit (Unum));
397 end if;
398 end if;
399 end if;
401 Next (Context_Node);
402 end loop;
404 -- Restore style/validity check mode for main unit
406 Set_Style_Check_Options (Save_Style_Checks);
407 Opt.Style_Check := Save_Style_Check;
408 Set_Validity_Check_Options (Save_Validity_Checks);
409 Opt.Validity_Checks_On := Save_Validity_Check;
410 end Load;