* dwarf2out.c, fold-const.c, ipa-type-escape.c,
[official-gcc.git] / gcc / ada / switch-m.adb
blob32775667cbb1bbd16007a2905cbb5d00f43ec82d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S W I T C H - M --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2005 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 ------------------------------------------------------------------------------
27 with Debug; use Debug;
28 with Osint; use Osint;
29 with Opt; use Opt;
30 with Table;
32 package body Switch.M is
34 package Normalized_Switches is new Table.Table
35 (Table_Component_Type => String_Access,
36 Table_Index_Type => Integer,
37 Table_Low_Bound => 1,
38 Table_Initial => 20,
39 Table_Increment => 100,
40 Table_Name => "Switch.M.Normalized_Switches");
41 -- This table is used to keep the normalized switches, so that they may be
42 -- reused for subsequent invocations of Normalize_Compiler_Switches with
43 -- similar switches.
45 Initial_Number_Of_Switches : constant := 10;
47 Global_Switches : Argument_List_Access := null;
48 -- Used by function Normalize_Compiler_Switches
50 ---------------------------------
51 -- Normalize_Compiler_Switches --
52 ---------------------------------
54 procedure Normalize_Compiler_Switches
55 (Switch_Chars : String;
56 Switches : in out Argument_List_Access;
57 Last : out Natural)
59 Switch_Starts_With_Gnat : Boolean;
61 Ptr : Integer := Switch_Chars'First;
62 Max : constant Integer := Switch_Chars'Last;
63 C : Character := ' ';
65 Storing : String := Switch_Chars;
66 First_Stored : Positive := Ptr + 1;
67 Last_Stored : Positive := First_Stored;
69 procedure Add_Switch_Component (S : String);
70 -- Add a new String_Access component in Switches. If a string equal
71 -- to S is already stored in the table Normalized_Switches, use it.
72 -- Other wise add a new component to the table.
74 --------------------------
75 -- Add_Switch_Component --
76 --------------------------
78 procedure Add_Switch_Component (S : String) is
79 begin
80 -- If Switches is null, allocate a new array
82 if Switches = null then
83 Switches := new Argument_List (1 .. Initial_Number_Of_Switches);
85 -- otherwise, if Switches is full, extend it
87 elsif Last = Switches'Last then
88 declare
89 New_Switches : constant Argument_List_Access :=
90 new Argument_List
91 (1 .. Switches'Length + Switches'Length);
92 begin
93 New_Switches (1 .. Switches'Length) := Switches.all;
94 Last := Switches'Length;
95 Switches := New_Switches;
96 end;
97 end if;
99 -- If this is the first switch, Last designates the first component
101 if Last = 0 then
102 Last := Switches'First;
103 else
104 Last := Last + 1;
105 end if;
107 -- Look into the table Normalized_Switches for a similar string.
108 -- If one is found, put it at the added component, and return.
110 for Index in 1 .. Normalized_Switches.Last loop
111 if S = Normalized_Switches.Table (Index).all then
112 Switches (Last) := Normalized_Switches.Table (Index);
113 return;
114 end if;
115 end loop;
117 -- No string equal to S was found in the table Normalized_Switches.
118 -- Add a new component in the table.
120 Switches (Last) := new String'(S);
121 Normalized_Switches.Increment_Last;
122 Normalized_Switches.Table (Normalized_Switches.Last) :=
123 Switches (Last);
124 end Add_Switch_Component;
126 -- Start of processing for Normalize_Compiler_Switches
128 begin
129 Last := 0;
131 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
132 return;
133 end if;
135 Ptr := Ptr + 1;
137 Switch_Starts_With_Gnat :=
138 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
140 if Switch_Starts_With_Gnat then
141 Ptr := Ptr + 4;
142 First_Stored := Ptr;
143 end if;
145 while Ptr <= Max loop
146 C := Switch_Chars (Ptr);
148 -- Processing for a switch
150 case Switch_Starts_With_Gnat is
152 when False =>
154 -- All switches that don't start with -gnat stay as is,
155 -- except -v and -pg
157 if Switch_Chars = "-pg" then
159 -- The gcc driver converts -pg to -p, so that is what
160 -- is stored in the ALI file.
162 Add_Switch_Component ("-p");
164 elsif C /= 'v' then
165 Add_Switch_Component (Switch_Chars);
166 end if;
168 return;
170 when True =>
172 case C is
174 -- One-letter switches
176 when 'a' | 'A' | 'b' | 'c' | 'D' | 'E' | 'f' |
177 'F' | 'g' | 'h' | 'H' | 'k' | 'l' | 'L' | 'n' | 'N' |
178 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 's' | 't' |
179 'u' | 'U' | 'v' | 'x' | 'X' | 'Z' =>
180 Storing (First_Stored) := C;
181 Add_Switch_Component
182 (Storing (Storing'First .. First_Stored));
183 Ptr := Ptr + 1;
185 -- One-letter switches followed by a positive number
187 when 'm' | 'T' =>
188 Storing (First_Stored) := C;
189 Last_Stored := First_Stored;
191 loop
192 Ptr := Ptr + 1;
193 exit when Ptr > Max
194 or else Switch_Chars (Ptr) not in '0' .. '9';
195 Last_Stored := Last_Stored + 1;
196 Storing (Last_Stored) := Switch_Chars (Ptr);
197 end loop;
199 Add_Switch_Component
200 (Storing (Storing'First .. Last_Stored));
202 when 'd' =>
203 Storing (First_Stored) := 'd';
205 while Ptr < Max loop
206 Ptr := Ptr + 1;
207 C := Switch_Chars (Ptr);
208 exit when C = ASCII.NUL or else C = '/'
209 or else C = '-';
211 if C in '1' .. '9' or else
212 C in 'a' .. 'z' or else
213 C in 'A' .. 'Z'
214 then
215 Storing (First_Stored + 1) := C;
216 Add_Switch_Component
217 (Storing (Storing'First .. First_Stored + 1));
219 else
220 Last := 0;
221 return;
222 end if;
223 end loop;
225 return;
227 when 'e' =>
229 -- Only -gnateD and -gnatep= need storing in ALI file
231 Storing (First_Stored) := 'e';
232 Ptr := Ptr + 1;
234 if Ptr > Max
235 or else (Switch_Chars (Ptr) /= 'D'
236 and then Switch_Chars (Ptr) /= 'p')
237 then
238 Last := 0;
239 return;
240 end if;
242 -- Processing for -gnateD
244 if Switch_Chars (Ptr) = 'D' then
245 Storing (First_Stored + 1 ..
246 First_Stored + Max - Ptr + 1) :=
247 Switch_Chars (Ptr .. Max);
248 Add_Switch_Component
249 (Storing (Storing'First ..
250 First_Stored + Max - Ptr + 1));
252 -- Processing for -gnatep=
254 else
255 Ptr := Ptr + 1;
257 if Ptr = Max then
258 Last := 0;
259 return;
260 end if;
262 if Switch_Chars (Ptr) = '=' then
263 Ptr := Ptr + 1;
264 end if;
266 -- To normalize, always put a '=' after -gnatep.
267 -- Because that could lengthen the switch string,
268 -- declare a local variable.
270 declare
271 To_Store : String (1 .. Max - Ptr + 9);
272 begin
273 To_Store (1 .. 8) := "-gnatep=";
274 To_Store (9 .. Max - Ptr + 9) :=
275 Switch_Chars (Ptr .. Max);
276 Add_Switch_Component (To_Store);
277 end;
278 end if;
280 return;
282 when 'i' =>
283 Storing (First_Stored) := 'i';
285 Ptr := Ptr + 1;
287 if Ptr > Max then
288 Last := 0;
289 return;
290 end if;
292 C := Switch_Chars (Ptr);
294 if C in '1' .. '5'
295 or else C = '8'
296 or else C = 'p'
297 or else C = 'f'
298 or else C = 'n'
299 or else C = 'w'
300 then
301 Storing (First_Stored + 1) := C;
302 Add_Switch_Component
303 (Storing (Storing'First .. First_Stored + 1));
304 Ptr := Ptr + 1;
306 else
307 Last := 0;
308 return;
309 end if;
311 -- -gnatR may be followed by '0', '1', '2' or '3',
312 -- then by 's'
314 when 'R' =>
315 Last_Stored := First_Stored;
316 Storing (Last_Stored) := 'R';
317 Ptr := Ptr + 1;
319 if Ptr <= Max
320 and then Switch_Chars (Ptr) in '0' .. '9'
321 then
322 C := Switch_Chars (Ptr);
324 if C in '4' .. '9' then
325 Last := 0;
326 return;
328 else
329 Last_Stored := Last_Stored + 1;
330 Storing (Last_Stored) := C;
331 Ptr := Ptr + 1;
333 if Ptr <= Max
334 and then Switch_Chars (Ptr) = 's' then
335 Last_Stored := Last_Stored + 1;
336 Storing (Last_Stored) := 's';
337 Ptr := Ptr + 1;
338 end if;
339 end if;
340 end if;
342 Add_Switch_Component
343 (Storing (Storing'First .. Last_Stored));
345 -- Multiple switches
347 when 'V' | 'w' | 'y' =>
348 Storing (First_Stored) := C;
349 Ptr := Ptr + 1;
351 if Ptr > Max then
352 if C = 'y' then
353 Add_Switch_Component
354 (Storing (Storing'First .. First_Stored));
356 else
357 Last := 0;
358 return;
359 end if;
360 end if;
362 while Ptr <= Max loop
363 C := Switch_Chars (Ptr);
364 Ptr := Ptr + 1;
366 -- 'w' should be skipped in -gnatw
368 if C /= 'w' or else Storing (First_Stored) /= 'w' then
370 -- -gnatyMxxx
372 if C = 'M'
373 and then Storing (First_Stored) = 'y' then
374 Last_Stored := First_Stored + 1;
375 Storing (Last_Stored) := 'M';
377 while Ptr <= Max loop
378 C := Switch_Chars (Ptr);
379 exit when C not in '0' .. '9';
380 Last_Stored := Last_Stored + 1;
381 Storing (Last_Stored) := C;
382 Ptr := Ptr + 1;
383 end loop;
385 -- If there is no digit after -gnatyM,
386 -- the switch is invalid.
388 if Last_Stored = First_Stored + 1 then
389 Last := 0;
390 return;
392 else
393 Add_Switch_Component
394 (Storing (Storing'First .. Last_Stored));
395 end if;
397 -- All other switches are -gnatxx
399 else
400 Storing (First_Stored + 1) := C;
401 Add_Switch_Component
402 (Storing (Storing'First .. First_Stored + 1));
403 end if;
404 end if;
405 end loop;
407 -- -gnat95 -gnat05
409 when '0' | '9' =>
410 Last_Stored := First_Stored;
411 Storing (Last_Stored) := C;
412 Ptr := Ptr + 1;
414 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
416 -- Invalid switch
418 Last := 0;
419 return;
421 else
422 Last_Stored := Last_Stored + 1;
423 Storing (Last_Stored) := '5';
424 Add_Switch_Component
425 (Storing (Storing'First .. Last_Stored));
426 Ptr := Ptr + 1;
427 end if;
429 -- -gnat83
431 when '8' =>
432 Last_Stored := First_Stored;
433 Storing (Last_Stored) := '8';
434 Ptr := Ptr + 1;
436 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
438 -- Invalid switch
440 Last := 0;
441 return;
443 else
444 Last_Stored := Last_Stored + 1;
445 Storing (Last_Stored) := '3';
446 Add_Switch_Component
447 (Storing (Storing'First .. Last_Stored));
448 Ptr := Ptr + 1;
449 end if;
451 -- Not a valid switch
453 when others =>
454 Last := 0;
455 return;
457 end case;
459 end case;
460 end loop;
461 end Normalize_Compiler_Switches;
463 function Normalize_Compiler_Switches
464 (Switch_Chars : String)
465 return Argument_List
467 Last : Natural;
469 begin
470 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
472 if Last = 0 then
473 return (1 .. 0 => null);
475 else
476 return Global_Switches (Global_Switches'First .. Last);
477 end if;
479 end Normalize_Compiler_Switches;
481 ------------------------
482 -- Scan_Make_Switches --
483 ------------------------
485 procedure Scan_Make_Switches (Switch_Chars : String) is
486 Ptr : Integer := Switch_Chars'First;
487 Max : constant Integer := Switch_Chars'Last;
488 C : Character := ' ';
490 begin
491 -- Skip past the initial character (must be the switch character)
493 if Ptr = Max then
494 raise Bad_Switch;
496 else
497 Ptr := Ptr + 1;
498 end if;
500 -- A little check, "gnat" at the start of a switch is not allowed
501 -- except for the compiler (where it was already removed)
503 if Switch_Chars'Length >= Ptr + 3
504 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
505 then
506 Osint.Fail
507 ("invalid switch: """, Switch_Chars, """ (gnat not needed here)");
508 end if;
510 -- Loop to scan through switches given in switch string
512 Check_Switch : begin
513 C := Switch_Chars (Ptr);
515 -- Processing for a switch
517 case C is
519 when 'a' =>
520 Ptr := Ptr + 1;
521 Check_Readonly_Files := True;
523 -- Processing for b switch
525 when 'b' =>
526 Ptr := Ptr + 1;
527 Bind_Only := True;
528 Make_Steps := True;
530 -- Processing for B switch
532 when 'B' =>
533 Ptr := Ptr + 1;
534 Build_Bind_And_Link_Full_Project := True;
536 -- Processing for c switch
538 when 'c' =>
539 Ptr := Ptr + 1;
540 Compile_Only := True;
541 Make_Steps := True;
543 -- Processing for C switch
545 when 'C' =>
546 Ptr := Ptr + 1;
547 Create_Mapping_File := True;
549 -- Processing for D switch
551 when 'D' =>
552 Ptr := Ptr + 1;
554 if Object_Directory_Present then
555 Osint.Fail ("duplicate -D switch");
557 else
558 Object_Directory_Present := True;
559 end if;
561 -- Processing for d switch
563 when 'd' =>
565 -- Note: for the debug switch, the remaining characters in this
566 -- switch field must all be debug flags, since all valid switch
567 -- characters are also valid debug characters. This switch is not
568 -- documented on purpose because it is only used by the
569 -- implementors.
571 -- Loop to scan out debug flags
573 while Ptr < Max loop
574 Ptr := Ptr + 1;
575 C := Switch_Chars (Ptr);
576 exit when C = ASCII.NUL or else C = '/' or else C = '-';
578 if C in '1' .. '9' or else
579 C in 'a' .. 'z' or else
580 C in 'A' .. 'Z'
581 then
582 Set_Debug_Flag (C);
583 else
584 raise Bad_Switch;
585 end if;
586 end loop;
588 -- Make sure Zero_Cost_Exceptions is set if gnatdX set. This
589 -- is for backwards compatibility with old versions and usage.
591 if Debug_Flag_XX then
592 Zero_Cost_Exceptions_Set := True;
593 Zero_Cost_Exceptions_Val := True;
594 end if;
596 return;
598 -- Processing for e switch
600 when 'e' =>
601 Ptr := Ptr + 1;
603 if Ptr > Max then
604 raise Bad_Switch;
605 end if;
607 case Switch_Chars (Ptr) is
609 -- processing for eI switch
611 when 'I' =>
612 Ptr := Ptr + 1;
613 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index);
615 -- processing for eL switch
617 when 'L' =>
618 Ptr := Ptr + 1;
619 Follow_Links := True;
621 when others =>
622 raise Bad_Switch;
623 end case;
625 -- Processing for f switch
627 when 'f' =>
628 Ptr := Ptr + 1;
629 Force_Compilations := True;
631 -- Processing for F switch
633 when 'F' =>
634 Ptr := Ptr + 1;
635 Full_Path_Name_For_Brief_Errors := True;
637 -- Processing for h switch
639 when 'h' =>
640 Ptr := Ptr + 1;
641 Usage_Requested := True;
643 -- Processing for i switch
645 when 'i' =>
646 Ptr := Ptr + 1;
647 In_Place_Mode := True;
649 -- Processing for j switch
651 when 'j' =>
652 Ptr := Ptr + 1;
654 declare
655 Max_Proc : Pos;
656 begin
657 Scan_Pos (Switch_Chars, Max, Ptr, Max_Proc);
658 Maximum_Processes := Positive (Max_Proc);
659 end;
661 -- Processing for k switch
663 when 'k' =>
664 Ptr := Ptr + 1;
665 Keep_Going := True;
667 -- Processing for l switch
669 when 'l' =>
670 Ptr := Ptr + 1;
671 Link_Only := True;
672 Make_Steps := True;
674 when 'M' =>
675 Ptr := Ptr + 1;
676 List_Dependencies := True;
678 -- Processing for n switch
680 when 'n' =>
681 Ptr := Ptr + 1;
682 Do_Not_Execute := True;
684 -- Processing for o switch
686 when 'o' =>
687 Ptr := Ptr + 1;
689 if Output_File_Name_Present then
690 raise Too_Many_Output_Files;
691 else
692 Output_File_Name_Present := True;
693 end if;
695 -- Processing for q switch
697 when 'q' =>
698 Ptr := Ptr + 1;
699 Quiet_Output := True;
701 -- Processing for R switch
703 when 'R' =>
704 Ptr := Ptr + 1;
705 Run_Path_Option := False;
707 -- Processing for s switch
709 when 's' =>
710 Ptr := Ptr + 1;
711 Check_Switches := True;
713 -- Processing for v switch
715 when 'v' =>
716 Ptr := Ptr + 1;
717 Verbose_Mode := True;
719 -- Processing for x switch
721 when 'x' =>
722 Ptr := Ptr + 1;
723 External_Unit_Compilation_Allowed := True;
725 -- Processing for z switch
727 when 'z' =>
728 Ptr := Ptr + 1;
729 No_Main_Subprogram := True;
731 -- Ignore extra switch character
733 when '/' | '-' =>
734 Ptr := Ptr + 1;
736 -- Anything else is an error (illegal switch character)
738 when others =>
739 raise Bad_Switch;
741 end case;
743 if Ptr <= Max then
744 Osint.Fail ("invalid switch: ", Switch_Chars);
745 end if;
747 end Check_Switch;
749 exception
750 when Bad_Switch =>
751 Osint.Fail ("invalid switch: ", (1 => C));
753 when Bad_Switch_Value =>
754 Osint.Fail ("numeric value out of range for switch: ", (1 => C));
756 when Missing_Switch_Value =>
757 Osint.Fail ("missing numeric value for switch: ", (1 => C));
759 when Too_Many_Output_Files =>
760 Osint.Fail ("duplicate -o switch");
762 end Scan_Make_Switches;
764 end Switch.M;