OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [ada/] [prep.adb] - Blame information for rev 311

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.