1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
;
40 with Osint
; use Osint
;
41 with Sinput
.L
; use Sinput
.L
;
42 with Stylesw
; use Stylesw
;
43 with Validsw
; use Validsw
;
48 File_Name
: File_Name_Type
;
49 -- Name of file for current unit, derived from unit name
51 Cur_Unum
: Unit_Number_Type
:= Current_Source_Unit
;
52 -- Unit number of unit that we just finished parsing. Note that we need
53 -- to capture this, because Source_Unit will change as we parse new
54 -- source files in the multiple main source file case.
56 Curunit
: constant Node_Id
:= Cunit
(Cur_Unum
);
57 -- Compilation unit node for current compilation unit
59 Loc
: Source_Ptr
:= Sloc
(Curunit
);
60 -- Source location for compilation unit node
62 Save_Style_Check
: Boolean;
63 Save_Style_Checks
: Style_Check_Options
;
64 -- Save style check so it can be restored later
66 Save_Validity_Check
: Boolean;
67 Save_Validity_Checks
: Validity_Check_Options
;
68 -- Save validity check so it can be restored later
71 -- Compilation unit node for withed unit
73 Context_Node
: Node_Id
;
74 -- Next node in context items list
79 Spec_Name
: Unit_Name_Type
;
80 -- Unit name of required spec
82 Body_Name
: Unit_Name_Type
;
83 -- Unit name of corresponding body
85 Unum
: Unit_Number_Type
;
86 -- Unit number of loaded unit
88 function Same_File_Name_Except_For_Case
89 (Expected_File_Name
: File_Name_Type
;
90 Actual_File_Name
: File_Name_Type
)
92 -- Given an actual file name and an expected file name (the latter being
93 -- derived from the unit name), determine if they are the same except for
94 -- possibly different casing of letters.
96 function Same_File_Name_Except_For_Case
97 (Expected_File_Name
: File_Name_Type
;
98 Actual_File_Name
: File_Name_Type
)
102 Get_Name_String
(Actual_File_Name
);
103 Canonical_Case_File_Name
(Name_Buffer
(1 .. Name_Len
));
106 Lower_Case_Actual_File_Name
: String (1 .. Name_Len
);
109 Lower_Case_Actual_File_Name
:= Name_Buffer
(1 .. Name_Len
);
110 Get_Name_String
(Expected_File_Name
);
111 Canonical_Case_File_Name
(Name_Buffer
(1 .. Name_Len
));
112 return Lower_Case_Actual_File_Name
= Name_Buffer
(1 .. Name_Len
);
115 end Same_File_Name_Except_For_Case
;
117 -- Start of processing for Load
120 -- Don't do any loads if we already had a fatal error
122 if Fatal_Error
(Cur_Unum
) then
126 Save_Style_Check_Options
(Save_Style_Checks
);
127 Save_Style_Check
:= Opt
.Style_Check
;
129 Save_Validity_Check_Options
(Save_Validity_Checks
);
130 Save_Validity_Check
:= Opt
.Validity_Checks_On
;
132 -- If main unit, set Main_Unit_Entity (this will get overwritten if
133 -- the main unit has a separate spec, that happens later on in Load)
135 if Cur_Unum
= Main_Unit
then
136 Main_Unit_Entity
:= Cunit_Entity
(Main_Unit
);
139 -- If we have no unit name, things are seriously messed up by previous
140 -- errors, and we should not try to continue compilation.
142 if Unit_Name
(Cur_Unum
) = No_Name
then
143 raise Unrecoverable_Error
;
146 -- Next step, make sure that the unit name matches the file name
147 -- and issue a warning message if not. We only output this for the
148 -- main unit, since for other units it is more serious and is
149 -- caught in a separate test below.
153 (Unit_Name
(Cur_Unum
),
154 Subunit
=> Nkind
(Unit
(Cunit
(Cur_Unum
))) = N_Subunit
);
156 if Cur_Unum
= Main_Unit
157 and then File_Name
/= Unit_File_Name
(Cur_Unum
)
158 and then (File_Names_Case_Sensitive
159 or not Same_File_Name_Except_For_Case
160 (File_Name
, Unit_File_Name
(Cur_Unum
)))
162 Error_Msg_Name_1
:= File_Name
;
164 ("?file name does not match unit name, should be{", Sloc
(Curunit
));
167 -- For units other than the main unit, the expected unit name is set and
168 -- must be the same as the actual unit name, or we are in big trouble, and
169 -- abandon the compilation since there are situations where this really
170 -- gets us into bad trouble (e.g. some subunit situations).
172 if Cur_Unum
/= Main_Unit
173 and then Expected_Unit
(Cur_Unum
) /= Unit_Name
(Cur_Unum
)
175 Loc
:= Error_Location
(Cur_Unum
);
176 Error_Msg_Name_1
:= Unit_File_Name
(Cur_Unum
);
177 Get_Name_String
(Error_Msg_Name_1
);
179 -- Check for predefined file case
182 and then Name_Buffer
(2) = '-'
183 and then (Name_Buffer
(1) = 'a'
185 Name_Buffer
(1) = 's'
187 Name_Buffer
(1) = 'i'
189 Name_Buffer
(1) = 'g')
191 -- In the predefined file case, we know the user did not construct
192 -- their own package, but we got the wrong one. This means that the
193 -- name supplied by the user crunched to something we recognized,
194 -- but then the file did not contain the unit expected. Most likely
195 -- this is due to a misspelling, e.g.
197 -- with Ada.Calender;
199 -- This crunches to a-calend, which indeed contains the unit
200 -- Ada.Calendar, and we can diagnose the misspelling. This is
201 -- a simple heuristic, but it catches many common cases of
202 -- misspelling of predefined unit names without needing a full
205 Error_Msg_Name_1
:= Expected_Unit
(Cur_Unum
);
206 Error_Msg
("% is not a predefined library unit!", Loc
);
207 Error_Msg_Name_1
:= Unit_Name
(Cur_Unum
);
208 Error_Msg
("possible misspelling of %!", Loc
);
210 -- Non-predefined file name case
213 Error_Msg
("file { does not contain expected unit!", Loc
);
214 Error_Msg_Unit_1
:= Expected_Unit
(Cur_Unum
);
215 Error_Msg
("expected unit $!", Loc
);
216 Error_Msg_Unit_1
:= Unit_Name
(Cur_Unum
);
217 Error_Msg
("found unit $!", Loc
);
220 raise Unrecoverable_Error
;
223 -- If current unit is a body, load its corresponding spec
225 if Nkind
(Unit
(Curunit
)) = N_Package_Body
226 or else Nkind
(Unit
(Curunit
)) = N_Subprogram_Body
228 Spec_Name
:= Get_Spec_Name
(Unit_Name
(Cur_Unum
));
231 (Load_Name
=> Spec_Name
,
234 Error_Node
=> Curunit
,
235 Corr_Body
=> Cur_Unum
);
237 -- If we successfully load the unit, then set the spec pointer. Once
238 -- again note that if the loaded unit has a fatal error, Load will
239 -- have set our Fatal_Error flag to propagate this condition.
241 if Unum
/= No_Unit
then
242 Set_Library_Unit
(Curunit
, Cunit
(Unum
));
244 -- If this is a separate spec for the main unit, then we reset
245 -- Main_Unit_Entity to point to the entity for this separate spec
247 if Cur_Unum
= Main_Unit
then
248 Main_Unit_Entity
:= Cunit_Entity
(Unum
);
251 -- If we don't find the spec, then if we have a subprogram body, we
252 -- are still OK, we just have a case of a body acting as its own spec
254 elsif Nkind
(Unit
(Curunit
)) = N_Subprogram_Body
then
255 Set_Acts_As_Spec
(Curunit
, True);
256 Set_Library_Unit
(Curunit
, Curunit
);
258 -- Otherwise we do have an error, repeat the load request for the spec
259 -- with Required set True to generate an appropriate error message.
264 (Load_Name
=> Spec_Name
,
267 Error_Node
=> Curunit
);
271 -- If current unit is a child unit spec, load its parent
273 elsif Nkind
(Unit
(Curunit
)) = N_Package_Declaration
274 or else Nkind
(Unit
(Curunit
)) = N_Subprogram_Declaration
275 or else Nkind
(Unit
(Curunit
)) in N_Generic_Declaration
276 or else Nkind
(Unit
(Curunit
)) in N_Generic_Instantiation
277 or else Nkind
(Unit
(Curunit
)) in N_Renaming_Declaration
279 -- Turn style and validity checks off for parent unit
281 if not GNAT_Mode
then
282 Reset_Style_Check_Options
;
283 Reset_Validity_Check_Options
;
286 Spec_Name
:= Get_Parent_Spec_Name
(Unit_Name
(Cur_Unum
));
288 if Spec_Name
/= No_Name
then
291 (Load_Name
=> Spec_Name
,
294 Error_Node
=> Curunit
);
296 if Unum
/= No_Unit
then
297 Set_Parent_Spec
(Unit
(Curunit
), Cunit
(Unum
));
301 -- If current unit is a subunit, then load its parent body
303 elsif Nkind
(Unit
(Curunit
)) = N_Subunit
then
304 Body_Name
:= Get_Parent_Body_Name
(Unit_Name
(Cur_Unum
));
307 (Load_Name
=> Body_Name
,
310 Error_Node
=> Name
(Unit
(Curunit
)));
312 if Unum
/= No_Unit
then
313 Set_Library_Unit
(Curunit
, Cunit
(Unum
));
318 -- Now we load with'ed units, with style/validity checks turned off
320 if not GNAT_Mode
then
321 Reset_Style_Check_Options
;
322 Reset_Validity_Check_Options
;
325 -- Loop through context items
327 Context_Node
:= First
(Context_Items
(Curunit
));
328 while Present
(Context_Node
) loop
330 if Nkind
(Context_Node
) = N_With_Clause
then
331 With_Node
:= Context_Node
;
332 Spec_Name
:= Get_Unit_Name
(With_Node
);
336 (Load_Name
=> Spec_Name
,
339 Error_Node
=> With_Node
,
342 -- If we find the unit, then set spec pointer in the N_With_Clause
343 -- to point to the compilation unit for the spec. Remember that
344 -- the Load routine itself sets our Fatal_Error flag if the loaded
345 -- unit gets a fatal error, so we don't need to worry about that.
347 if Unum
/= No_Unit
then
348 Set_Library_Unit
(With_Node
, Cunit
(Unum
));
350 -- If the spec isn't found, then try finding the corresponding
351 -- body, since it is possible that we have a subprogram body
352 -- that is acting as a spec (since no spec is present).
355 Body_Name
:= Get_Body_Name
(Spec_Name
);
358 (Load_Name
=> Body_Name
,
361 Error_Node
=> With_Node
,
364 -- If we got a subprogram body, then mark that we are using
365 -- the body as a spec in the file table, and set the spec
366 -- pointer in the N_With_Clause to point to the body entity.
369 and then Nkind
(Unit
(Cunit
(Unum
))) = N_Subprogram_Body
371 With_Cunit
:= Cunit
(Unum
);
372 Set_Library_Unit
(With_Node
, With_Cunit
);
373 Set_Acts_As_Spec
(With_Cunit
, True);
374 Set_Library_Unit
(With_Cunit
, With_Cunit
);
376 -- If we couldn't find the body, or if it wasn't a body spec
377 -- then we are in trouble. We make one more call to Load to
378 -- require the spec. We know it will fail of course, the
379 -- purpose is to generate the required error message (we prefer
380 -- that this message refer to the missing spec, not the body)
385 (Load_Name
=> Spec_Name
,
388 Error_Node
=> With_Node
,
391 -- Here we create a dummy package unit for the missing unit
393 Unum
:= Create_Dummy_Package_Unit
(With_Node
, Spec_Name
);
394 Set_Library_Unit
(With_Node
, Cunit
(Unum
));
402 -- Restore style/validity check mode for main unit
404 Set_Style_Check_Options
(Save_Style_Checks
);
405 Opt
.Style_Check
:= Save_Style_Check
;
406 Set_Validity_Check_Options
(Save_Validity_Checks
);
407 Opt
.Validity_Checks_On
:= Save_Validity_Check
;