fmap.adb: Put routines in alpha order
[gcc.git] / gcc / ada / makeutl.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E U T L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2006, 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 with Namet; use Namet;
28 with Osint; use Osint;
29 with Prj.Ext;
30 with Prj.Util;
31 with Snames; use Snames;
32 with Table;
33
34 with System.HTable;
35
36 package body Makeutl is
37
38 type Mark_Key is record
39 File : File_Name_Type;
40 Index : Int;
41 end record;
42 -- Identify either a mono-unit source (when Index = 0) or a specific unit
43 -- in a multi-unit source.
44
45 -- There follow many global undocumented declarations, comments needed ???
46
47 Max_Mask_Num : constant := 2048;
48
49 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
50
51 function Hash (Key : Mark_Key) return Mark_Num;
52
53 package Marks is new System.HTable.Simple_HTable
54 (Header_Num => Mark_Num,
55 Element => Boolean,
56 No_Element => False,
57 Key => Mark_Key,
58 Hash => Hash,
59 Equal => "=");
60 -- A hash table to keep tracks of the marked units
61
62 type Linker_Options_Data is record
63 Project : Project_Id;
64 Options : String_List_Id;
65 end record;
66
67 Linker_Option_Initial_Count : constant := 20;
68
69 Linker_Options_Buffer : String_List_Access :=
70 new String_List (1 .. Linker_Option_Initial_Count);
71
72 Last_Linker_Option : Natural := 0;
73
74 package Linker_Opts is new Table.Table (
75 Table_Component_Type => Linker_Options_Data,
76 Table_Index_Type => Integer,
77 Table_Low_Bound => 1,
78 Table_Initial => 10,
79 Table_Increment => 100,
80 Table_Name => "Make.Linker_Opts");
81
82 procedure Add_Linker_Option (Option : String);
83
84 -----------------------
85 -- Add_Linker_Option --
86 -----------------------
87
88 procedure Add_Linker_Option (Option : String) is
89 begin
90 if Option'Length > 0 then
91 if Last_Linker_Option = Linker_Options_Buffer'Last then
92 declare
93 New_Buffer : constant String_List_Access :=
94 new String_List
95 (1 .. Linker_Options_Buffer'Last +
96 Linker_Option_Initial_Count);
97 begin
98 New_Buffer (Linker_Options_Buffer'Range) :=
99 Linker_Options_Buffer.all;
100 Linker_Options_Buffer.all := (others => null);
101 Free (Linker_Options_Buffer);
102 Linker_Options_Buffer := New_Buffer;
103 end;
104 end if;
105
106 Last_Linker_Option := Last_Linker_Option + 1;
107 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
108 end if;
109 end Add_Linker_Option;
110
111 ----------------------
112 -- Delete_All_Marks --
113 ----------------------
114
115 procedure Delete_All_Marks is
116 begin
117 Marks.Reset;
118 end Delete_All_Marks;
119
120 ----------
121 -- Hash --
122 ----------
123
124 function Hash (Key : Mark_Key) return Mark_Num is
125 begin
126 return Union_Id (Key.File) mod Max_Mask_Num;
127 end Hash;
128
129 ----------------------------
130 -- Is_External_Assignment --
131 ----------------------------
132
133 function Is_External_Assignment (Argv : String) return Boolean is
134 Start : Positive := 3;
135 Finish : Natural := Argv'Last;
136 Equal_Pos : Natural;
137
138 pragma Assert (Argv'First = 1);
139 pragma Assert (Argv (1 .. 2) = "-X");
140
141 begin
142 if Argv'Last < 5 then
143 return False;
144
145 elsif Argv (3) = '"' then
146 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
147 return False;
148 else
149 Start := 4;
150 Finish := Argv'Last - 1;
151 end if;
152 end if;
153
154 Equal_Pos := Start;
155
156 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
157 Equal_Pos := Equal_Pos + 1;
158 end loop;
159
160 if Equal_Pos = Start
161 or else Equal_Pos >= Finish
162 then
163 return False;
164 else
165 Prj.Ext.Add
166 (External_Name => Argv (Start .. Equal_Pos - 1),
167 Value => Argv (Equal_Pos + 1 .. Finish));
168 return True;
169 end if;
170 end Is_External_Assignment;
171
172 ---------------
173 -- Is_Marked --
174 ---------------
175
176 function Is_Marked
177 (Source_File : File_Name_Type;
178 Index : Int := 0) return Boolean
179 is
180 begin
181 return Marks.Get (K => (File => Source_File, Index => Index));
182 end Is_Marked;
183
184 -----------------------------
185 -- Linker_Options_Switches --
186 -----------------------------
187
188 function Linker_Options_Switches
189 (Project : Project_Id;
190 In_Tree : Project_Tree_Ref) return String_List
191 is
192 procedure Recursive_Add_Linker_Options (Proj : Project_Id);
193 -- The recursive routine used to add linker options
194
195 ----------------------------------
196 -- Recursive_Add_Linker_Options --
197 ----------------------------------
198
199 procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
200 Data : Project_Data;
201 Linker_Package : Package_Id;
202 Options : Variable_Value;
203 Imported : Project_List;
204
205 begin
206 if Proj /= No_Project then
207 Data := In_Tree.Projects.Table (Proj);
208
209 if not Data.Seen then
210 In_Tree.Projects.Table (Proj).Seen := True;
211 Imported := Data.Imported_Projects;
212
213 while Imported /= Empty_Project_List loop
214 Recursive_Add_Linker_Options
215 (In_Tree.Project_Lists.Table
216 (Imported).Project);
217 Imported := In_Tree.Project_Lists.Table
218 (Imported).Next;
219 end loop;
220
221 if Proj /= Project then
222 Linker_Package :=
223 Prj.Util.Value_Of
224 (Name => Name_Linker,
225 In_Packages => Data.Decl.Packages,
226 In_Tree => In_Tree);
227 Options :=
228 Prj.Util.Value_Of
229 (Name => Name_Ada,
230 Index => 0,
231 Attribute_Or_Array_Name => Name_Linker_Options,
232 In_Package => Linker_Package,
233 In_Tree => In_Tree);
234
235 -- If attribute is present, add the project with
236 -- the attribute to table Linker_Opts.
237
238 if Options /= Nil_Variable_Value then
239 Linker_Opts.Increment_Last;
240 Linker_Opts.Table (Linker_Opts.Last) :=
241 (Project => Proj, Options => Options.Values);
242 end if;
243 end if;
244 end if;
245 end if;
246 end Recursive_Add_Linker_Options;
247
248 -- Start of processing for Linker_Options_Switches
249
250 begin
251 Linker_Opts.Init;
252
253 for Index in Project_Table.First ..
254 Project_Table.Last (In_Tree.Projects)
255 loop
256 In_Tree.Projects.Table (Index).Seen := False;
257 end loop;
258
259 Recursive_Add_Linker_Options (Project);
260
261 Last_Linker_Option := 0;
262
263 for Index in reverse 1 .. Linker_Opts.Last loop
264 declare
265 Options : String_List_Id := Linker_Opts.Table (Index).Options;
266 Proj : constant Project_Id :=
267 Linker_Opts.Table (Index).Project;
268 Option : Name_Id;
269
270 begin
271 -- If Dir_Path has not been computed for this project, do it now
272
273 if In_Tree.Projects.Table (Proj).Dir_Path = null then
274 In_Tree.Projects.Table (Proj).Dir_Path :=
275 new String'
276 (Get_Name_String
277 (In_Tree.Projects.Table
278 (Proj). Directory));
279 end if;
280
281 while Options /= Nil_String loop
282 Option :=
283 In_Tree.String_Elements.Table (Options).Value;
284 Get_Name_String (Option);
285
286 -- Do not consider empty linker options
287
288 if Name_Len /= 0 then
289 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
290
291 -- Object files and -L switches specified with relative
292 -- paths must be converted to absolute paths.
293
294 Test_If_Relative_Path
295 (Switch =>
296 Linker_Options_Buffer (Last_Linker_Option),
297 Parent =>
298 In_Tree.Projects.Table (Proj).Dir_Path,
299 Including_L_Switch => True);
300 end if;
301
302 Options :=
303 In_Tree.String_Elements.Table (Options).Next;
304 end loop;
305 end;
306 end loop;
307
308 return Linker_Options_Buffer (1 .. Last_Linker_Option);
309 end Linker_Options_Switches;
310
311 -----------
312 -- Mains --
313 -----------
314
315 package body Mains is
316
317 package Names is new Table.Table
318 (Table_Component_Type => File_Name_Type,
319 Table_Index_Type => Integer,
320 Table_Low_Bound => 1,
321 Table_Initial => 10,
322 Table_Increment => 100,
323 Table_Name => "Makeutl.Mains.Names");
324 -- The table that stores the mains
325
326 Current : Natural := 0;
327 -- The index of the last main retrieved from the table
328
329 --------------
330 -- Add_Main --
331 --------------
332
333 procedure Add_Main (Name : String) is
334 begin
335 Name_Len := 0;
336 Add_Str_To_Name_Buffer (Name);
337 Names.Increment_Last;
338 Names.Table (Names.Last) := Name_Find;
339 end Add_Main;
340
341 ------------
342 -- Delete --
343 ------------
344
345 procedure Delete is
346 begin
347 Names.Set_Last (0);
348 Mains.Reset;
349 end Delete;
350
351 ---------------
352 -- Next_Main --
353 ---------------
354
355 function Next_Main return String is
356 begin
357 if Current >= Names.Last then
358 return "";
359
360 else
361 Current := Current + 1;
362 return Get_Name_String (Names.Table (Current));
363 end if;
364 end Next_Main;
365
366 ---------------------
367 -- Number_Of_Mains --
368 ---------------------
369
370 function Number_Of_Mains return Natural is
371 begin
372 return Names.Last;
373 end Number_Of_Mains;
374
375 -----------
376 -- Reset --
377 -----------
378
379 procedure Reset is
380 begin
381 Current := 0;
382 end Reset;
383
384 end Mains;
385
386 ----------
387 -- Mark --
388 ----------
389
390 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
391 begin
392 Marks.Set (K => (File => Source_File, Index => Index), E => True);
393 end Mark;
394
395 ---------------------------
396 -- Test_If_Relative_Path --
397 ---------------------------
398
399 procedure Test_If_Relative_Path
400 (Switch : in out String_Access;
401 Parent : String_Access;
402 Including_L_Switch : Boolean := True)
403 is
404 begin
405 if Switch /= null then
406 declare
407 Sw : String (1 .. Switch'Length);
408 Start : Positive;
409
410 begin
411 Sw := Switch.all;
412
413 if Sw (1) = '-' then
414 if Sw'Length >= 3
415 and then (Sw (2) = 'A'
416 or else Sw (2) = 'I'
417 or else (Including_L_Switch and then Sw (2) = 'L'))
418 then
419 Start := 3;
420
421 if Sw = "-I-" then
422 return;
423 end if;
424
425 elsif Sw'Length >= 4
426 and then (Sw (2 .. 3) = "aL"
427 or else Sw (2 .. 3) = "aO"
428 or else Sw (2 .. 3) = "aI")
429 then
430 Start := 4;
431
432 else
433 return;
434 end if;
435
436 -- Because relative path arguments to --RTS= may be relative
437 -- to the search directory prefix, those relative path
438 -- arguments are not converted.
439
440 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
441 if Parent = null or else Parent'Length = 0 then
442 Do_Fail
443 ("relative search path switches (""",
444 Sw,
445 """) are not allowed");
446
447 else
448 Switch :=
449 new String'
450 (Sw (1 .. Start - 1) &
451 Parent.all &
452 Directory_Separator &
453 Sw (Start .. Sw'Last));
454 end if;
455 end if;
456
457 else
458 if not Is_Absolute_Path (Sw) then
459 if Parent = null or else Parent'Length = 0 then
460 Do_Fail
461 ("relative paths (""", Sw, """) are not allowed");
462
463 else
464 Switch :=
465 new String'(Parent.all & Directory_Separator & Sw);
466 end if;
467 end if;
468 end if;
469 end;
470 end if;
471 end Test_If_Relative_Path;
472
473 -------------------
474 -- Unit_Index_Of --
475 -------------------
476
477 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
478 Start : Natural;
479 Finish : Natural;
480 Result : Int := 0;
481
482 begin
483 Get_Name_String (ALI_File);
484
485 -- First, find the last dot
486
487 Finish := Name_Len;
488
489 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
490 Finish := Finish - 1;
491 end loop;
492
493 if Finish = 1 then
494 return 0;
495 end if;
496
497 -- Now check that the dot is preceded by digits
498
499 Start := Finish;
500 Finish := Finish - 1;
501
502 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
503 Start := Start - 1;
504 end loop;
505
506 -- If there is no difits, or if the digits are not preceded by
507 -- the character that precedes a unit index, this is not the ALI file
508 -- of a unit in a multi-unit source.
509
510 if Start > Finish
511 or else Start = 1
512 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
513 then
514 return 0;
515 end if;
516
517 -- Build the index from the digit(s)
518
519 while Start <= Finish loop
520 Result := Result * 10 +
521 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
522 Start := Start + 1;
523 end loop;
524
525 return Result;
526 end Unit_Index_Of;
527
528 end Makeutl;