Debian Codegraph @

gcc/ada/prep.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P R E P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2002-2025, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Csets; use Csets;
27 with Err_Vars; use Err_Vars;
28 with Opt; use Opt;
29 with Osint; use Osint;
30 with Output; use Output;
31 with Scans; use Scans;
32 with Snames; use Snames;
33 with Sinput;
34 with Stringt; use Stringt;
35 with Table;
36 with Uintp; use Uintp;
37
38 with GNAT.Heap_Sort_G;
39
40 package body Prep is
41
42 use Symbol_Table;
43
44 type Token_Name_Array is array (Token_Type) of Name_Id;
45 Token_Names : constant Token_Name_Array :=
46 (Tok_Abort => Name_Abort,
47 Tok_Abs => Name_Abs,
48 Tok_Abstract => Name_Abstract,
49 Tok_Accept => Name_Accept,
50 Tok_Aliased => Name_Aliased,
51 Tok_All => Name_All,
52 Tok_Array => Name_Array,
53 Tok_And => Name_And,
54 Tok_At => Name_At,
55 Tok_Begin => Name_Begin,
56 Tok_Body => Name_Body,
57 Tok_Case => Name_Case,
58 Tok_Constant => Name_Constant,
59 Tok_Declare => Name_Declare,
60 Tok_Delay => Name_Delay,
61 Tok_Delta => Name_Delta,
62 Tok_Digits => Name_Digits,
63 Tok_Else => Name_Else,
64 Tok_Elsif => Name_Elsif,
65 Tok_End => Name_End,
66 Tok_Entry => Name_Entry,
67 Tok_Exception => Name_Exception,
68 Tok_Exit => Name_Exit,
69 Tok_For => Name_For,
70 Tok_Function => Name_Function,
71 Tok_Generic => Name_Generic,
72 Tok_Goto => Name_Goto,
73 Tok_If => Name_If,
74 Tok_Is => Name_Is,
75 Tok_Limited => Name_Limited,
76 Tok_Loop => Name_Loop,
77 Tok_Mod => Name_Mod,
78 Tok_New => Name_New,
79 Tok_Null => Name_Null,
80 Tok_Of => Name_Of,
81 Tok_Or => Name_Or,
82 Tok_Others => Name_Others,
83 Tok_Out => Name_Out,
84 Tok_Package => Name_Package,
85 Tok_Pragma => Name_Pragma,
86 Tok_Private => Name_Private,
87 Tok_Procedure => Name_Procedure,
88 Tok_Protected => Name_Protected,
89 Tok_Raise => Name_Raise,
90 Tok_Range => Name_Range,
91 Tok_Record => Name_Record,
92 Tok_Rem => Name_Rem,
93 Tok_Renames => Name_Renames,
94 Tok_Requeue => Name_Requeue,
95 Tok_Return => Name_Return,
96 Tok_Reverse => Name_Reverse,
97 Tok_Select => Name_Select,
98 Tok_Separate => Name_Separate,
99 Tok_Subtype => Name_Subtype,
100 Tok_Tagged => Name_Tagged,
101 Tok_Task => Name_Task,
102 Tok_Terminate => Name_Terminate,
103 Tok_Then => Name_Then,
104 Tok_Type => Name_Type,
105 Tok_Until => Name_Until,
106 Tok_Use => Name_Use,
107 Tok_When => Name_When,
108 Tok_While => Name_While,
109 Tok_With => Name_With,
110 Tok_Xor => Name_Xor,
111 others => No_Name);
112
113 Already_Initialized : Boolean := False;
114 -- Used to avoid repetition of the part of the initialisation that needs
115 -- to be done only once.
116
117 String_False : String_Id;
118 -- "false", as a string_id
119
120 --------------
121 -- Behavior --
122 --------------
123
124 -- Accesses to procedure specified by procedure Initialize
125
126 Error_Msg : Error_Msg_Proc;
127 -- Report an error
128
129 Scan : Scan_Proc;
130 -- Scan one token
131
132 Set_Ignore_Errors : Set_Ignore_Errors_Proc;
133 -- Indicate if error should be taken into account
134
135 Put_Char : Put_Char_Proc;
136 -- Output one character
137
138 New_EOL : New_EOL_Proc;
139 -- Output an end of line indication
140
141 -------------------------------
142 -- State of the Preprocessor --
143 -------------------------------
144
145 type Pp_State is record
146 If_Ptr : Source_Ptr;
147 -- The location of the #if statement (used to flag #if with no
148 -- corresponding #end if, at the end).
149
150 Else_Ptr : Source_Ptr;
151 -- The location of the #else statement (used to detect multiple #else's)
152
153 Deleting : Boolean;
154 -- Set to True when the code should be deleted or commented out
155
156 Match_Seen : Boolean;
157 -- Set to True when a condition in an #if or an #elsif is True. Also set
158 -- to True if Deleting at the previous level is True. Used to decide if
159 -- Deleting should be set to True in a following #elsif or #else.
160
161 end record;
162
163 type Pp_Depth is new Nat;
164
165 Ground : constant Pp_Depth := 0;
166
167 package Pp_States is new Table.Table
168 (Table_Component_Type => Pp_State,
169 Table_Index_Type => Pp_Depth,
170 Table_Low_Bound => 1,
171 Table_Initial => 10,
172 Table_Increment => 100,
173 Table_Name => "Prep.Pp_States");
174 -- A stack of the states of the preprocessor, for nested #if
175
176 type Operator is (None, Op_Or, Op_And);
177
178 -----------------
179 -- Subprograms --
180 -----------------
181
182 function Deleting return Boolean;
183 -- Return True if code should be deleted or commented out
184
185 function Expression
186 (Evaluate_It : Boolean;
187 Complemented : Boolean := False) return Boolean;
188 -- Evaluate a condition in an #if or an #elsif statement. If Evaluate_It
189 -- is False, the condition is effectively evaluated, otherwise, only the
190 -- syntax is checked.
191
192 procedure Go_To_End_Of_Line;
193 -- Advance the scan pointer until we reach an end of line or the end of the
194 -- buffer.
195
196 function Matching_Strings (S1, S2 : String_Id) return Boolean;
197 -- Returns True if the two string parameters are equal (case insensitive)
198
199 ---------------------------------------
200 -- Change_Reserved_Keyword_To_Symbol --
201 ---------------------------------------
202
203 procedure Change_Reserved_Keyword_To_Symbol
204 (All_Keywords : Boolean := False)
205 is
206 New_Name : constant Name_Id := Token_Names (Token);
207
208 begin
209 if New_Name /= No_Name then
210 case Token is
211 when Tok_And
212 | Tok_Else
213 | Tok_Elsif
214 | Tok_End
215 | Tok_If
216 | Tok_Or
217 | Tok_Then
218 =>
219 if All_Keywords then
220 Token := Tok_Identifier;
221 Token_Name := New_Name;
222 end if;
223
224 when others =>
225 Token := Tok_Identifier;
226 Token_Name := New_Name;
227 end case;
228 end if;
229 end Change_Reserved_Keyword_To_Symbol;
230
231 ------------------------------------------
232 -- Check_Command_Line_Symbol_Definition --
233 ------------------------------------------
234
235 procedure Check_Command_Line_Symbol_Definition
236 (Definition : String;
237 Data : out Symbol_Data)
238 is
239 Index : Natural := 0;
240 Result : Symbol_Data;
241
242 begin
243 -- Look for the character '='
244
245 for J in Definition'Range loop
246 if Definition (J) = '=' then
247 Index := J;
248 exit;
249 end if;
250 end loop;
251
252 -- If no character '=', then the value is True
253
254 if Index = 0 then
255
256 -- Put the symbol in the name buffer
257
258 Name_Len := Definition'Length;
259 Name_Buffer (1 .. Name_Len) := Definition;
260 Result := True_Value;
261
262 elsif Index = Definition'First then
263 Fail ("invalid symbol definition """ & Definition & """");
264
265 else
266 -- Put the symbol in the name buffer
267
268 Name_Len := Index - Definition'First;
269 Name_Buffer (1 .. Name_Len) :=
270 String'(Definition (Definition'First .. Index - 1));
271
272 -- Check the syntax of the value
273
274 if Definition (Index + 1) /= '"'
275 or else Definition (Definition'Last) /= '"'
276 then
277 for J in Index + 1 .. Definition'Last loop
278 case Definition (J) is
279 when '_' | '.' | '0' .. '9' | 'a' .. 'z' | 'A' .. 'Z' =>
280 null;
281
282 when others =>
283 Fail ("illegal value """
284 & Definition (Index + 1 .. Definition'Last)
285 & """");
286 end case;
287 end loop;
288 end if;
289
290 -- Even if the value is a string, we still set Is_A_String to False,
291 -- to avoid adding additional quotes in the preprocessed sources when
292 -- replacing $<symbol>.
293
294 Result.Is_A_String := False;
295
296 -- Put the value in the result
297
298 Start_String;
299 Store_String_Chars (Definition (Index + 1 .. Definition'Last));
300 Result.Value := End_String;
301 end if;
302
303 -- Now, check the syntax of the symbol (we don't allow accented or
304 -- wide characters).
305
306 if Name_Buffer (1) not in 'a' .. 'z'
307 and then Name_Buffer (1) not in 'A' .. 'Z'
308 then
309 Fail ("symbol """
310 & Name_Buffer (1 .. Name_Len)
311 & """ does not start with a letter");
312 end if;
313
314 for J in 2 .. Name_Len loop
315 case Name_Buffer (J) is
316 when 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' =>
317 null;
318
319 when '_' =>
320 if J = Name_Len then
321 Fail ("symbol """
322 & Name_Buffer (1 .. Name_Len)
323 & """ end with a '_'");
324
325 elsif Name_Buffer (J + 1) = '_' then
326 Fail ("symbol """
327 & Name_Buffer (1 .. Name_Len)
328 & """ contains consecutive '_'");
329 end if;
330
331 when others =>
332 Fail ("symbol """
333 & Name_Buffer (1 .. Name_Len)
334 & """ contains illegal character(s)");
335 end case;
336 end loop;
337
338 Result.On_The_Command_Line := True;
339
340 -- Put the symbol name in the result
341
342 declare
343 Sym : constant String := Name_Buffer (1 .. Name_Len);
344
345 begin
346 for Index in 1 .. Name_Len loop
347 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
348 end loop;
349
350 Result.Symbol := Name_Find;
351 Name_Len := Sym'Length;
352 Name_Buffer (1 .. Name_Len) := Sym;
353 Result.Original := Name_Find;
354 end;
355
356 Data := Result;
357 end Check_Command_Line_Symbol_Definition;
358
359 --------------
360 -- Deleting --
361 --------------
362
363 function Deleting return Boolean is
364 begin
365 -- Always return False when not inside an #if statement
366
367 if Opt.No_Deletion or else Pp_States.Last = Ground then
368 return False;
369 else
370 return Pp_States.Table (Pp_States.Last).Deleting;
371 end if;
372 end Deleting;
373
374 ----------------
375 -- Expression --
376 ----------------
377
378 function Expression
379 (Evaluate_It : Boolean;
380 Complemented : Boolean := False) return Boolean
381 is
382 Evaluation : Boolean := Evaluate_It;
383 -- Is set to False after an "or else" when left term is True and after
384 -- an "and then" when left term is False.
385
386 Final_Result : Boolean := False;
387
388 Current_Result : Boolean := False;
389 -- Value of a term
390
391 Current_Operator : Operator := None;
392 Symbol1 : Symbol_Id;
393 Symbol2 : Symbol_Id;
394 Symbol_Name1 : Name_Id;
395 Symbol_Name2 : Name_Id;
396 Symbol_Pos1 : Source_Ptr;
397 Symbol_Pos2 : Source_Ptr;
398 Symbol_Value1 : String_Id;
399 Symbol_Value2 : String_Id;
400
401 Relop : Token_Type;
402
403 begin
404 -- Loop for each term
405
406 loop
407 Change_Reserved_Keyword_To_Symbol;
408
409 Current_Result := False;
410
411 -- Scan current term, starting with Token
412
413 case Token is
414
415 -- Handle parenthesized expression
416
417 when Tok_Left_Paren =>
418 Scan.all;
419 Current_Result := Expression (Evaluation);
420
421 if Token = Tok_Right_Paren then
422 Scan.all;
423
424 else
425 Error_Msg -- CODEFIX
426 ("`)` expected", Token_Ptr);
427 end if;
428
429 -- Handle not expression
430
431 when Tok_Not =>
432 Scan.all;
433 Current_Result :=
434 not Expression (Evaluation, Complemented => True);
435
436 -- Handle sequence starting with identifier
437
438 when Tok_Identifier =>
439 Symbol_Name1 := Token_Name;
440 Symbol_Pos1 := Token_Ptr;
441 Scan.all;
442
443 if Token = Tok_Apostrophe then
444
445 -- symbol'Defined
446
447 Scan.all;
448
449 if Token = Tok_Identifier
450 and then Token_Name = Name_Defined
451 then
452 Scan.all;
453
454 else
455 Error_Msg ("identifier `Defined` expected", Token_Ptr);
456 end if;
457
458 if Evaluation then
459 Current_Result := Index_Of (Symbol_Name1) /= No_Symbol;
460 end if;
461
462 -- Handle relational operator
463
464 elsif Token in Tok_Equal | Tok_Less | Tok_Less_Equal |
465 Tok_Greater | Tok_Greater_Equal
466 then
467 Relop := Token;
468 Scan.all;
469 Change_Reserved_Keyword_To_Symbol;
470
471 if Token = Tok_Integer_Literal then
472
473 -- symbol = integer
474 -- symbol < integer
475 -- symbol <= integer
476 -- symbol > integer
477 -- symbol >= integer
478
479 declare
480 Value : constant Int := UI_To_Int (Int_Literal_Value);
481 Data : Symbol_Data;
482
483 Symbol_Value : Int;
484 -- Value of symbol as Int
485
486 begin
487 if Evaluation then
488 Symbol1 := Index_Of (Symbol_Name1);
489
490 if Symbol1 = No_Symbol then
491 Error_Msg_Name_1 := Symbol_Name1;
492 if Undefined_Symbols_Are_False then
493 Error_Msg
494 ("symbol % value is implicitly False"
495 & " and so not an integer",
496 Symbol_Pos1);
497 else
498 Error_Msg ("unknown symbol %", Symbol_Pos1);
499 end if;
500 Symbol_Value1 := No_String;
501
502 else
503 Data := Mapping.Table (Symbol1);
504
505 if Data.Is_A_String then
506 Error_Msg_Name_1 := Symbol_Name1;
507 Error_Msg
508 ("symbol % value is not an integer",
509 Symbol_Pos1);
510
511 else
512 begin
513 String_To_Name_Buffer (Data.Value);
514 Symbol_Value :=
515 Int'Value (Name_Buffer (1 .. Name_Len));
516
517 case Relop is
518 when Tok_Equal =>
519 Current_Result :=
520 Symbol_Value = Value;
521
522 when Tok_Less =>
523 Current_Result :=
524 Symbol_Value < Value;
525
526 when Tok_Less_Equal =>
527 Current_Result :=
528 Symbol_Value <= Value;
529
530 when Tok_Greater =>
531 Current_Result :=
532 Symbol_Value > Value;
533
534 when Tok_Greater_Equal =>
535 Current_Result :=
536 Symbol_Value >= Value;
537
538 when others =>
539 null;
540 end case;
541
542 exception
543 when Constraint_Error =>
544 Error_Msg_Name_1 := Symbol_Name1;
545 Error_Msg
546 ("symbol % value is not an integer",
547 Symbol_Pos1);
548 end;
549 end if;
550 end if;
551 end if;
552
553 Scan.all;
554 end;
555
556 -- Error if relational operator other than = if not numbers
557
558 elsif Relop /= Tok_Equal then
559 Error_Msg ("number expected", Token_Ptr);
560
561 -- Equality comparison of two strings
562
563 elsif Token = Tok_Identifier then
564
565 -- symbol = symbol
566
567 Symbol_Name2 := Token_Name;
568 Symbol_Pos2 := Token_Ptr;
569 Scan.all;
570
571 if Evaluation then
572 Symbol1 := Index_Of (Symbol_Name1);
573
574 if Symbol1 = No_Symbol then
575 if Undefined_Symbols_Are_False then
576 Symbol_Value1 := String_False;
577
578 else
579 Error_Msg_Name_1 := Symbol_Name1;
580 Error_Msg ("unknown symbol %", Symbol_Pos1);
581 Symbol_Value1 := No_String;
582 end if;
583
584 else
585 Symbol_Value1 :=
586 Mapping.Table (Symbol1).Value;
587 end if;
588
589 Symbol2 := Index_Of (Symbol_Name2);
590
591 if Symbol2 = No_Symbol then
592 if Undefined_Symbols_Are_False then
593 Symbol_Value2 := String_False;
594
595 else
596 Error_Msg_Name_1 := Symbol_Name2;
597 Error_Msg ("unknown symbol %", Symbol_Pos2);
598 Symbol_Value2 := No_String;
599 end if;
600
601 else
602 Symbol_Value2 := Mapping.Table (Symbol2).Value;
603 end if;
604
605 if Symbol_Value1 /= No_String
606 and then
607 Symbol_Value2 /= No_String
608 then
609 Current_Result :=
610 Matching_Strings (Symbol_Value1, Symbol_Value2);
611 end if;
612 end if;
613
614 elsif Token = Tok_String_Literal then
615
616 -- symbol = "value"
617
618 if Evaluation then
619 Symbol1 := Index_Of (Symbol_Name1);
620
621 if Symbol1 = No_Symbol then
622 if Undefined_Symbols_Are_False then
623 Symbol_Value1 := String_False;
624
625 else
626 Error_Msg_Name_1 := Symbol_Name1;
627 Error_Msg ("unknown symbol %", Symbol_Pos1);
628 Symbol_Value1 := No_String;
629 end if;
630
631 else
632 Symbol_Value1 := Mapping.Table (Symbol1).Value;
633 end if;
634
635 if Symbol_Value1 /= No_String then
636 Current_Result :=
637 Matching_Strings
638 (Symbol_Value1,
639 String_Literal_Id);
640 end if;
641 end if;
642
643 Scan.all;
644
645 else
646 Error_Msg
647 ("literal integer, symbol or literal string expected",
648 Token_Ptr);
649 end if;
650
651 -- Handle True or False
652
653 else
654 if Evaluation then
655 Symbol1 := Index_Of (Symbol_Name1);
656
657 if Symbol1 = No_Symbol then
658 if Undefined_Symbols_Are_False then
659 Symbol_Value1 := String_False;
660
661 else
662 Error_Msg_Name_1 := Symbol_Name1;
663 Error_Msg ("unknown symbol %", Symbol_Pos1);
664 Symbol_Value1 := No_String;
665 end if;
666
667 else
668 Symbol_Value1 := Mapping.Table (Symbol1).Value;
669 end if;
670
671 if Symbol_Value1 /= No_String then
672 String_To_Name_Buffer (Symbol_Value1);
673
674 for Index in 1 .. Name_Len loop
675 Name_Buffer (Index) :=
676 Fold_Lower (Name_Buffer (Index));
677 end loop;
678
679 if Name_Buffer (1 .. Name_Len) = "true" then
680 Current_Result := True;
681
682 elsif Name_Buffer (1 .. Name_Len) = "false" then
683 Current_Result := False;
684
685 else
686 Error_Msg_Name_1 := Symbol_Name1;
687 Error_Msg
688 ("value of symbol % is not True or False",
689 Symbol_Pos1);
690 end if;
691 end if;
692 end if;
693 end if;
694
695 -- Unrecognized sequence
696
697 when others =>
698 Error_Msg ("`(`, NOT or symbol expected", Token_Ptr);
699 end case;
700
701 -- Update the cumulative final result
702
703 case Current_Operator is
704 when None =>
705 Final_Result := Current_Result;
706
707 when Op_Or =>
708 Final_Result := Final_Result or Current_Result;
709
710 when Op_And =>
711 Final_Result := Final_Result and Current_Result;
712 end case;
713
714 -- Handle AND
715
716 if Token = Tok_And then
717 if Complemented then
718 Error_Msg
719 ("mixing NOT and AND is not allowed, parentheses are required",
720 Token_Ptr);
721
722 elsif Current_Operator = Op_Or then
723 Error_Msg ("mixing OR and AND is not allowed", Token_Ptr);
724 end if;
725
726 Current_Operator := Op_And;
727 Scan.all;
728
729 if Token = Tok_Then then
730 Scan.all;
731
732 if Final_Result = False then
733 Evaluation := False;
734 end if;
735 end if;
736
737 -- Handle OR
738
739 elsif Token = Tok_Or then
740 if Complemented then
741 Error_Msg
742 ("mixing NOT and OR is not allowed, parentheses are required",
743 Token_Ptr);
744
745 elsif Current_Operator = Op_And then
746 Error_Msg ("mixing AND and OR is not allowed", Token_Ptr);
747 end if;
748
749 Current_Operator := Op_Or;
750 Scan.all;
751
752 if Token = Tok_Else then
753 Scan.all;
754
755 if Final_Result then
756 Evaluation := False;
757 end if;
758 end if;
759
760 -- No AND/OR operator, so exit from the loop through terms
761
762 else
763 exit;
764 end if;
765 end loop;
766
767 return Final_Result;
768 end Expression;
769
770 -----------------------
771 -- Go_To_End_Of_Line --
772 -----------------------
773
774 procedure Go_To_End_Of_Line is
775 begin
776 -- Scan until we get an end of line or we reach the end of the buffer
777
778 while Token not in Tok_End_Of_Line | Tok_EOF loop
779 Scan.all;
780 end loop;
781 end Go_To_End_Of_Line;
782
783 --------------
784 -- Index_Of --
785 --------------
786
787 function Index_Of (Symbol : Name_Id) return Symbol_Id is
788 begin
789 if Mapping.Table /= null then
790 for J in Symbol_Id range 1 .. Symbol_Table.Last (Mapping) loop
791 if Mapping.Table (J).Symbol = Symbol then
792 return J;
793 end if;
794 end loop;
795 end if;
796
797 return No_Symbol;
798 end Index_Of;
799
800 ----------------
801 -- Initialize --
802 ----------------
803
804 procedure Initialize is
805 begin
806 if not Already_Initialized then
807 Start_String;
808 Store_String_Chars ("True");
809 True_Value.Value := End_String;
810
811 Start_String;
812 Store_String_Chars ("False");
813 String_False := End_String;
814
815 Already_Initialized := True;
816 end if;
817 end Initialize;
818
819 ------------------
820 -- List_Symbols --
821 ------------------
822
823 procedure List_Symbols (Foreword : String) is
824 Order : array (0 .. Integer (Symbol_Table.Last (Mapping)))
825 of Symbol_Id;
826 -- After alphabetical sorting, this array stores the indexes of the
827 -- symbols in the order they are displayed.
828
829 function Lt (Op1, Op2 : Natural) return Boolean;
830 -- Comparison routine for sort call
831
832 procedure Move (From : Natural; To : Natural);
833 -- Move routine for sort call
834
835 --------
836 -- Lt --
837 --------
838
839 function Lt (Op1, Op2 : Natural) return Boolean is
840 S1 : constant String :=
841 Get_Name_String (Mapping.Table (Order (Op1)).Symbol);
842 S2 : constant String :=
843 Get_Name_String (Mapping.Table (Order (Op2)).Symbol);
844 begin
845 return S1 < S2;
846 end Lt;
847
848 ----------
849 -- Move --
850 ----------
851
852 procedure Move (From : Natural; To : Natural) is
853 begin
854 Order (To) := Order (From);
855 end Move;
856
857 package Sort_Syms is new GNAT.Heap_Sort_G (Move, Lt);
858
859 Max_L : Natural;
860 -- Maximum length of any symbol
861
862 -- Start of processing for List_Symbols_Case
863
864 begin
865 if Symbol_Table.Last (Mapping) = 0 then
866 return;
867 end if;
868
869 if Foreword'Length > 0 then
870 Write_Eol;
871 Write_Line (Foreword);
872
873 for J in Foreword'Range loop
874 Write_Char ('=');
875 end loop;
876 end if;
877
878 -- Initialize the order
879
880 for J in Order'Range loop
881 Order (J) := Symbol_Id (J);
882 end loop;
883
884 -- Sort alphabetically
885
886 Sort_Syms.Sort (Order'Last);
887
888 Max_L := 7;
889
890 for J in 1 .. Symbol_Table.Last (Mapping) loop
891 Get_Name_String (Mapping.Table (J).Original);
892 Max_L := Integer'Max (Max_L, Name_Len);
893 end loop;
894
895 Write_Eol;
896 Write_Str ("Symbol");
897
898 for J in 1 .. Max_L - 5 loop
899 Write_Char (' ');
900 end loop;
901
902 Write_Line ("Value");
903
904 Write_Str ("------");
905
906 for J in 1 .. Max_L - 5 loop
907 Write_Char (' ');
908 end loop;
909
910 Write_Line ("------");
911
912 for J in 1 .. Order'Last loop
913 declare
914 Data : constant Symbol_Data := Mapping.Table (Order (J));
915
916 begin
917 Get_Name_String (Data.Original);
918 Write_Str (Name_Buffer (1 .. Name_Len));
919
920 for K in Name_Len .. Max_L loop
921 Write_Char (' ');
922 end loop;
923
924 String_To_Name_Buffer (Data.Value);
925
926 if Data.Is_A_String then
927 Write_Char ('"');
928
929 for J in 1 .. Name_Len loop
930 Write_Char (Name_Buffer (J));
931
932 if Name_Buffer (J) = '"' then
933 Write_Char ('"');
934 end if;
935 end loop;
936
937 Write_Char ('"');
938
939 else
940 Write_Str (Name_Buffer (1 .. Name_Len));
941 end if;
942 end;
943
944 Write_Eol;
945 end loop;
946
947 Write_Eol;
948 end List_Symbols;
949
950 ----------------------
951 -- Matching_Strings --
952 ----------------------
953
954 function Matching_Strings (S1, S2 : String_Id) return Boolean is
955 begin
956 String_To_Name_Buffer (S1);
957
958 for Index in 1 .. Name_Len loop
959 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
960 end loop;
961
962 declare
963 String1 : constant String := Name_Buffer (1 .. Name_Len);
964
965 begin
966 String_To_Name_Buffer (S2);
967
968 for Index in 1 .. Name_Len loop
969 Name_Buffer (Index) := Fold_Lower (Name_Buffer (Index));
970 end loop;
971
972 return String1 = Name_Buffer (1 .. Name_Len);
973 end;
974 end Matching_Strings;
975
976 --------------------
977 -- Parse_Def_File --
978 --------------------
979
980 -- This procedure REALLY needs some more comments ???
981
982 procedure Parse_Def_File is
983 Symbol : Symbol_Id;
984 Symbol_Name : Name_Id;
985 Original_Name : Name_Id;
986 Data : Symbol_Data;
987 Value_Start : Source_Ptr;
988 Value_End : Source_Ptr;
989 Ch : Character;
990
991 use ASCII;
992
993 begin
994 Def_Line_Loop :
995 loop
996 Scan.all;
997
998 exit Def_Line_Loop when Token = Tok_EOF;
999
1000 if Token /= Tok_End_Of_Line then
1001 Change_Reserved_Keyword_To_Symbol;
1002
1003 if Token /= Tok_Identifier then
1004 Error_Msg ("identifier expected", Token_Ptr);
1005 goto Cleanup;
1006 end if;
1007
1008 Symbol_Name := Token_Name;
1009 Name_Len := 0;
1010
1011 for Ptr in Token_Ptr .. Scan_Ptr - 1 loop
1012 Name_Len := Name_Len + 1;
1013 Name_Buffer (Name_Len) := Sinput.Source (Ptr);
1014 end loop;
1015
1016 Original_Name := Name_Find;
1017 Scan.all;
1018
1019 if Token /= Tok_Colon_Equal then
1020 Error_Msg -- CODEFIX
1021 ("`:=` expected", Token_Ptr);
1022 goto Cleanup;
1023 end if;
1024
1025 Scan.all;
1026
1027 if Token = Tok_Integer_Literal then
1028 declare
1029 Ptr : Source_Ptr := Token_Ptr;
1030
1031 begin
1032 Start_String;
1033 while Ptr < Scan_Ptr loop
1034 Store_String_Char (Sinput.Source (Ptr));
1035 Ptr := Ptr + 1;
1036 end loop;
1037
1038 Data := (Symbol => Symbol_Name,
1039 Original => Original_Name,
1040 On_The_Command_Line => False,
1041 Is_A_String => False,
1042 Value => End_String);
1043 end;
1044
1045 Scan.all;
1046
1047 if Token not in Tok_End_Of_Line | Tok_EOF then
1048 Error_Msg ("extraneous text in definition", Token_Ptr);
1049 goto Cleanup;
1050 end if;
1051
1052 elsif Token = Tok_String_Literal then
1053 Data := (Symbol => Symbol_Name,
1054 Original => Original_Name,
1055 On_The_Command_Line => False,
1056 Is_A_String => True,
1057 Value => String_Literal_Id);
1058
1059 Scan.all;
1060
1061 if Token not in Tok_End_Of_Line | Tok_EOF then
1062 Error_Msg ("extraneous text in definition", Token_Ptr);
1063 goto Cleanup;
1064 end if;
1065
1066 elsif Token in Tok_End_Of_Line | Tok_EOF then
1067 Data := (Symbol => Symbol_Name,
1068 Original => Original_Name,
1069 On_The_Command_Line => False,
1070 Is_A_String => False,
1071 Value => Null_String_Id);
1072
1073 else
1074 Value_Start := Token_Ptr;
1075 Value_End := Token_Ptr - 1;
1076 Scan_Ptr := Token_Ptr;
1077
1078 Value_Chars_Loop :
1079 loop
1080 Ch := Sinput.Source (Scan_Ptr);
1081
1082 case Ch is
1083 when '_' | '.' | '0' .. '9' | 'a' .. 'z' | 'A' .. 'Z' =>
1084 Value_End := Scan_Ptr;
1085 Scan_Ptr := Scan_Ptr + 1;
1086
1087 when ' ' | HT | VT | CR | LF | FF =>
1088 exit Value_Chars_Loop;
1089
1090 when others =>
1091 Error_Msg ("illegal character", Scan_Ptr);
1092 goto Cleanup;
1093 end case;
1094 end loop Value_Chars_Loop;
1095
1096 Scan.all;
1097
1098 if Token not in Tok_End_Of_Line | Tok_EOF then
1099 Error_Msg ("extraneous text in definition", Token_Ptr);
1100 goto Cleanup;
1101 end if;
1102
1103 Start_String;
1104
1105 while Value_Start <= Value_End loop
1106 Store_String_Char (Sinput.Source (Value_Start));
1107 Value_Start := Value_Start + 1;
1108 end loop;
1109
1110 Data := (Symbol => Symbol_Name,
1111 Original => Original_Name,
1112 On_The_Command_Line => False,
1113 Is_A_String => False,
1114 Value => End_String);
1115 end if;
1116
1117 -- Now that we have the value, get the symbol index
1118
1119 Symbol := Index_Of (Symbol_Name);
1120
1121 if Symbol /= No_Symbol then
1122
1123 -- If we already have an entry for this symbol, replace it
1124 -- with the new value, except if the symbol was declared on
1125 -- the command line.
1126
1127 if Mapping.Table (Symbol).On_The_Command_Line then
1128 goto Continue;
1129 end if;
1130
1131 else
1132 -- As it is the first time we see this symbol, create a new
1133 -- entry in the table.
1134
1135 if Mapping.Table = null then
1136 Symbol_Table.Init (Mapping);
1137 end if;
1138
1139 Symbol_Table.Increment_Last (Mapping);
1140 Symbol := Symbol_Table.Last (Mapping);
1141 end if;
1142
1143 Mapping.Table (Symbol) := Data;
1144 goto Continue;
1145
1146 <<Cleanup>>
1147 Set_Ignore_Errors (To => True);
1148
1149 while Token not in Tok_End_Of_Line | Tok_EOF loop
1150 Scan.all;
1151 end loop;
1152
1153 Set_Ignore_Errors (To => False);
1154
1155 <<Continue>>
1156 null;
1157 end if;
1158 end loop Def_Line_Loop;
1159 end Parse_Def_File;
1160
1161 ----------------
1162 -- Preprocess --
1163 ----------------
1164
1165 procedure Preprocess (Source_Modified : out Boolean) is
1166 Start_Of_Processing : Source_Ptr;
1167 Cond : Boolean;
1168 Preprocessor_Line : Boolean := False;
1169 No_Error_Found : Boolean := True;
1170 Modified : Boolean := False;
1171
1172 procedure Output (From, To : Source_Ptr);
1173 -- Output the characters with indexes From .. To in the buffer to the
1174 -- output file.
1175
1176 procedure Output_Line (From, To : Source_Ptr);
1177 -- Output a line or the end of a line from the buffer to the output
1178 -- file, followed by an end of line terminator. Depending on the value
1179 -- of Deleting and the switches, the line may be commented out, blank or
1180 -- not output at all.
1181
1182 ------------
1183 -- Output --
1184 ------------
1185
1186 procedure Output (From, To : Source_Ptr) is
1187 begin
1188 for J in From .. To loop
1189 Put_Char (Sinput.Source (J));
1190 end loop;
1191 end Output;
1192
1193 -----------------
1194 -- Output_Line --
1195 -----------------
1196
1197 procedure Output_Line (From, To : Source_Ptr) is
1198 begin
1199 if Deleting or else Preprocessor_Line then
1200 if Blank_Deleted_Lines then
1201 New_EOL.all;
1202
1203 elsif Comment_Deleted_Lines then
1204 Put_Char ('-');
1205 Put_Char ('-');
1206 Put_Char ('!');
1207
1208 if From < To then
1209 Put_Char (' ');
1210 Output (From, To);
1211 end if;
1212
1213 New_EOL.all;
1214 end if;
1215
1216 else
1217 Output (From, To);
1218 New_EOL.all;
1219 end if;
1220 end Output_Line;
1221
1222 -- Start of processing for Preprocess
1223
1224 begin
1225 Start_Of_Processing := Scan_Ptr;
1226
1227 -- First a call to Scan, because Initialize_Scanner is not doing it
1228
1229 Scan.all;
1230
1231 Input_Line_Loop : loop
1232 exit Input_Line_Loop when Token = Tok_EOF;
1233
1234 Preprocessor_Line := False;
1235
1236 if Token /= Tok_End_Of_Line then
1237
1238 -- Preprocessor line
1239
1240 if Token = Tok_Special and then Special_Character = '#' then
1241 Modified := True;
1242 Preprocessor_Line := True;
1243 Scan.all;
1244
1245 case Token is
1246
1247 -- #if
1248
1249 when Tok_If =>
1250 declare
1251 If_Ptr : constant Source_Ptr := Token_Ptr;
1252
1253 begin
1254 Scan.all;
1255 Cond := Expression (not Deleting);
1256
1257 -- Check for an eventual "then"
1258
1259 if Token = Tok_Then then
1260 Scan.all;
1261 end if;
1262
1263 -- It is an error to have trailing characters after
1264 -- the condition or "then".
1265
1266 if Token not in Tok_End_Of_Line | Tok_EOF then
1267 Error_Msg
1268 ("extraneous text on preprocessor line",
1269 Token_Ptr);
1270 No_Error_Found := False;
1271 Go_To_End_Of_Line;
1272 end if;
1273
1274 declare
1275 -- Set the initial state of this new "#if". This
1276 -- must be done before incrementing the Last of
1277 -- the table, otherwise function Deleting does
1278 -- not report the correct value.
1279
1280 New_State : constant Pp_State :=
1281 (If_Ptr => If_Ptr,
1282 Else_Ptr => 0,
1283 Deleting => Deleting
1284 or else not Cond,
1285 Match_Seen => Deleting or else Cond);
1286
1287 begin
1288 Pp_States.Increment_Last;
1289 Pp_States.Table (Pp_States.Last) := New_State;
1290 end;
1291 end;
1292
1293 -- #elsif
1294
1295 when Tok_Elsif =>
1296 Cond := False;
1297
1298 if Pp_States.Last = 0
1299 or else Pp_States.Table (Pp_States.Last).Else_Ptr /= 0
1300 then
1301 Error_Msg ("no IF for this ELSIF", Token_Ptr);
1302 No_Error_Found := False;
1303
1304 else
1305 Cond :=
1306 not Pp_States.Table (Pp_States.Last).Match_Seen;
1307 end if;
1308
1309 Scan.all;
1310 Cond := Expression (Cond);
1311
1312 -- Check for an eventual "then"
1313
1314 if Token = Tok_Then then
1315 Scan.all;
1316 end if;
1317
1318 -- It is an error to have trailing characters after the
1319 -- condition or "then".
1320
1321 if Token not in Tok_End_Of_Line | Tok_EOF then
1322 Error_Msg
1323 ("extraneous text on preprocessor line",
1324 Token_Ptr);
1325 No_Error_Found := False;
1326
1327 Go_To_End_Of_Line;
1328 end if;
1329
1330 -- Depending on the value of the condition, set the new
1331 -- values of Deleting and Match_Seen.
1332
1333 if Pp_States.Last > 0 then
1334 if Pp_States.Table (Pp_States.Last).Match_Seen then
1335 Pp_States.Table (Pp_States.Last).Deleting := True;
1336 else
1337 if Cond then
1338 Pp_States.Table (Pp_States.Last).Match_Seen :=
1339 True;
1340 Pp_States.Table (Pp_States.Last).Deleting :=
1341 False;
1342 end if;
1343 end if;
1344 end if;
1345
1346 -- #else
1347
1348 when Tok_Else =>
1349 if Pp_States.Last = 0 then
1350 Error_Msg ("no IF for this ELSE", Token_Ptr);
1351 No_Error_Found := False;
1352
1353 elsif
1354 Pp_States.Table (Pp_States.Last).Else_Ptr /= 0
1355 then
1356 Error_Msg -- CODEFIX
1357 ("duplicate ELSE line", Token_Ptr);
1358 No_Error_Found := False;
1359 end if;
1360
1361 -- Set the possibly new values of Deleting and Match_Seen
1362
1363 if Pp_States.Last > 0 then
1364 if Pp_States.Table (Pp_States.Last).Match_Seen then
1365 Pp_States.Table (Pp_States.Last).Deleting :=
1366 True;
1367
1368 else
1369 Pp_States.Table (Pp_States.Last).Match_Seen :=
1370 True;
1371 Pp_States.Table (Pp_States.Last).Deleting :=
1372 False;
1373 end if;
1374
1375 -- Set the Else_Ptr to check for illegal #elsif later
1376
1377 Pp_States.Table (Pp_States.Last).Else_Ptr :=
1378 Token_Ptr;
1379 end if;
1380
1381 Scan.all;
1382
1383 -- Error of character present after "#else"
1384
1385 if Token not in Tok_End_Of_Line | Tok_EOF then
1386 Error_Msg
1387 ("extraneous text on preprocessor line",
1388 Token_Ptr);
1389 No_Error_Found := False;
1390 Go_To_End_Of_Line;
1391 end if;
1392
1393 -- #end if;
1394
1395 when Tok_End =>
1396 if Pp_States.Last = 0 then
1397 Error_Msg ("no IF for this END", Token_Ptr);
1398 No_Error_Found := False;
1399 end if;
1400
1401 Scan.all;
1402
1403 -- Ignore all recoverable errors if Relaxed_RM_Semantics
1404
1405 if Relaxed_RM_Semantics then
1406 null;
1407
1408 elsif Token /= Tok_If then
1409 Error_Msg -- CODEFIX
1410 ("IF expected", Token_Ptr);
1411 No_Error_Found := False;
1412
1413 else
1414 Scan.all;
1415
1416 if Token /= Tok_Semicolon then
1417 Error_Msg -- CODEFIX
1418 ("`;` Expected", Token_Ptr);
1419 No_Error_Found := False;
1420
1421 else
1422 Scan.all;
1423
1424 -- Error of character present after "#end if;"
1425
1426 if Token not in Tok_End_Of_Line | Tok_EOF then
1427 Error_Msg
1428 ("extraneous text on preprocessor line",
1429 Token_Ptr);
1430 No_Error_Found := False;
1431 end if;
1432 end if;
1433 end if;
1434
1435 -- In case of one of the errors above, skip the tokens
1436 -- until the end of line is reached.
1437
1438 Go_To_End_Of_Line;
1439
1440 -- Decrement the depth of the #if stack
1441
1442 if Pp_States.Last > 0 then
1443 Pp_States.Decrement_Last;
1444 end if;
1445
1446 -- Illegal preprocessor line
1447
1448 when others =>
1449 if Pp_States.Last = 0 then
1450 Error_Msg -- CODEFIX
1451 ("IF expected", Token_Ptr);
1452 No_Error_Found := False;
1453
1454 elsif Relaxed_RM_Semantics
1455 and then Get_Name_String (Token_Name) = "endif"
1456 then
1457 -- In relaxed mode, accept "endif" instead of
1458 -- "end if".
1459
1460 -- Decrement the depth of the #if stack
1461
1462 if Pp_States.Last > 0 then
1463 Pp_States.Decrement_Last;
1464 end if;
1465 elsif Pp_States.Table (Pp_States.Last).Else_Ptr = 0 then
1466 Error_Msg
1467 ("IF, ELSIF, ELSE, or `END IF` expected",
1468 Token_Ptr);
1469 No_Error_Found := False;
1470
1471 else
1472 Error_Msg ("IF or `END IF` expected", Token_Ptr);
1473 No_Error_Found := False;
1474 end if;
1475
1476 -- Skip to the end of this illegal line
1477
1478 Go_To_End_Of_Line;
1479 end case;
1480
1481 -- Not a preprocessor line
1482
1483 else
1484 -- Do not report errors for those lines, even if there are
1485 -- Ada parsing errors.
1486
1487 Set_Ignore_Errors (To => True);
1488
1489 if Deleting then
1490 Go_To_End_Of_Line;
1491
1492 else
1493 while Token not in Tok_End_Of_Line | Tok_EOF loop
1494 if Token = Tok_Special
1495 and then Special_Character = '$'
1496 then
1497 Modified := True;
1498
1499 declare
1500 Dollar_Ptr : constant Source_Ptr := Token_Ptr;
1501 Symbol : Symbol_Id;
1502
1503 begin
1504 Scan.all;
1505 Change_Reserved_Keyword_To_Symbol;
1506
1507 if Token = Tok_Identifier
1508 and then Token_Ptr = Dollar_Ptr + 1
1509 then
1510 -- $symbol
1511
1512 Symbol := Index_Of (Token_Name);
1513
1514 -- If symbol exists, replace by its value
1515
1516 if Symbol /= No_Symbol then
1517 Output (Start_Of_Processing, Dollar_Ptr - 1);
1518 Start_Of_Processing := Scan_Ptr;
1519 String_To_Name_Buffer
1520 (Mapping.Table (Symbol).Value);
1521
1522 if Mapping.Table (Symbol).Is_A_String then
1523
1524 -- Value is an Ada string
1525
1526 Put_Char ('"');
1527
1528 for J in 1 .. Name_Len loop
1529 Put_Char (Name_Buffer (J));
1530
1531 if Name_Buffer (J) = '"' then
1532 Put_Char ('"');
1533 end if;
1534 end loop;
1535
1536 Put_Char ('"');
1537
1538 else
1539 -- Value is a sequence of characters, not
1540 -- an Ada string.
1541
1542 for J in 1 .. Name_Len loop
1543 Put_Char (Name_Buffer (J));
1544 end loop;
1545 end if;
1546 end if;
1547 end if;
1548 end;
1549 end if;
1550
1551 Scan.all;
1552 end loop;
1553 end if;
1554
1555 Set_Ignore_Errors (To => False);
1556 end if;
1557 end if;
1558
1559 pragma Assert (Token in Tok_End_Of_Line | Tok_EOF);
1560
1561 -- At this point, the token is either end of line or EOF. The line to
1562 -- possibly output stops just before the token.
1563
1564 Output_Line (Start_Of_Processing, Token_Ptr - 1);
1565
1566 -- If we are at the end of a line, the scan pointer is at the first
1567 -- non-blank character (may not be the first character of the line),
1568 -- so we have to deduct Start_Of_Processing from the token pointer.
1569
1570 if Token = Tok_End_Of_Line then
1571 if Sinput.Source (Token_Ptr) = ASCII.CR
1572 and then Sinput.Source (Token_Ptr + 1) = ASCII.LF
1573 then
1574 Start_Of_Processing := Token_Ptr + 2;
1575 else
1576 Start_Of_Processing := Token_Ptr + 1;
1577 end if;
1578 end if;
1579
1580 -- Now, scan the first token of the next line. If the token is EOF,
1581 -- the scan pointer will not move, and the token will still be EOF.
1582
1583 Set_Ignore_Errors (To => True);
1584 Scan.all;
1585 Set_Ignore_Errors (To => False);
1586 end loop Input_Line_Loop;
1587
1588 -- Report an error for any missing some "#end if;"
1589
1590 for Level in reverse 1 .. Pp_States.Last loop
1591 Error_Msg ("no `END IF` for this IF", Pp_States.Table (Level).If_Ptr);
1592 No_Error_Found := False;
1593 end loop;
1594
1595 Source_Modified := No_Error_Found and Modified;
1596 end Preprocess;
1597
1598 -----------------
1599 -- Setup_Hooks --
1600 -----------------
1601
1602 procedure Setup_Hooks
1603 (Error_Msg : Error_Msg_Proc;
1604 Scan : Scan_Proc;
1605 Set_Ignore_Errors : Set_Ignore_Errors_Proc;
1606 Put_Char : Put_Char_Proc;
1607 New_EOL : New_EOL_Proc)
1608 is
1609 begin
1610 pragma Assert (Already_Initialized);
1611
1612 Prep.Error_Msg := Error_Msg;
1613 Prep.Scan := Scan;
1614 Prep.Set_Ignore_Errors := Set_Ignore_Errors;
1615 Prep.Put_Char := Put_Char;
1616 Prep.New_EOL := New_EOL;
1617 end Setup_Hooks;
1618
1619 end Prep;