awa_unit_2.4.0_59135a52/dynamo/src/asis/asis-text-set_get.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
------------------------------------------------------------------------------
--                                                                          --
--                 ASIS-for-GNAT IMPLEMENTATION COMPONENTS                  --
--                                                                          --
--                    A S I S . T E X T . S E T _ G E T                     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--            Copyright (C) 1995-2008, Free Software Foundation, Inc.       --
--                                                                          --
-- ASIS-for-GNAT is free software; you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software Foundation;  either version 2,  or  (at your option)  any later --
-- version. ASIS-for-GNAT is distributed  in the hope  that it will be use- --
-- ful, but WITHOUT ANY WARRANTY; without even the implied warranty of MER- --
-- CHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General  --
-- Public License for more details. You should have received a copy of the  --
-- GNU General Public License  distributed with ASIS-for-GNAT; see file     --
-- COPYING. If not, write to the Free Software Foundation,  59 Temple Place --
-- - Suite 330,  Boston, MA 02111-1307, USA.                                --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
-- ASIS-for-GNAT was originally developed  by the ASIS-for-GNAT team at the --
-- Software  Engineering  Laboratory  of  the Swiss  Federal  Institute  of --
-- Technology (LGL-EPFL) in Lausanne,  Switzerland, in cooperation with the --
-- Scientific  Research  Computer  Center of  Moscow State University (SRCC --
-- MSU), Russia,  with funding partially provided  by grants from the Swiss --
-- National  Science  Foundation  and  the  Swiss  Academy  of  Engineering --
-- Sciences.  ASIS-for-GNAT is now maintained by  Ada Core Technologies Inc --
-- (http://www.gnat.com).                                                   --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Characters.Handling; use Ada.Characters.Handling;

with Asis.Set_Get;            use Asis.Set_Get;

with A4G.A_Sinput;            use A4G.A_Sinput;
with A4G.Contt;               use A4G.Contt;
with A4G.Contt.TT;            use A4G.Contt.TT;

with Sinput;                  use Sinput;
with Widechar;                use Widechar;

package body Asis.Text.Set_Get is

   ---------
   -- Get --
   ---------

   function Line_Length (L : Line) return Character_Position is
   begin
      return L.Length;
   end Line_Length;

   function Line_Location (L : Line) return Source_Ptr is
   begin
      if not (L.Enclosing_Context = Get_Current_Cont and then
              L.Enclosing_Tree    = Get_Current_Tree)
      then
         Reset_Tree (L.Enclosing_Context, L.Enclosing_Tree);
      end if;

      return L.Sloc;
   end Line_Location;

   function Valid (L : Line) return Boolean is
   begin
      return Is_Opened (L.Enclosing_Context) and then
             Later (Opened_At (L.Enclosing_Context), L.Obtained);
   end Valid;

   ------------------------
   -- Debug_Image (Span) --
   ------------------------

   function Debug_Image (The_Span : Span) return String is
      LT : String renames ASIS_Line_Terminator;
   begin
      return    LT
            & "Debug image for Asis.Text.Span:"
            & LT
            & "First Line   : "
            & Int'Image (Int (The_Span.First_Line))
            & LT
            & "First Column : "
            & Int'Image (Int (The_Span.First_Column))
            & LT
            & "Last Line    : "
            & Int'Image (Int (The_Span.Last_Line))
            & LT
            & "Last Column  : "
            & Int'Image (Int (The_Span.Last_Column))
            & LT
            & LT;
   end Debug_Image;

   ---------------------
   -- Line_Wide_Image --
   ---------------------

   function Line_Wide_Image (L : Line) return Wide_String is
   begin
      if Line_Length (L) = 0 then
         return "";
      end if;

      declare
         Result : Wide_String (1 .. Line_Length (L));

         S       : Source_Ptr := L.Sloc;
         SFI     : constant Source_File_Index := Get_Source_File_Index (S);
         Src     : constant Source_Buffer_Ptr := Source_Text (SFI);
         Next_Ch : Char_Code;
         Success : Boolean;
         pragma Unreferenced (Success);
      begin

         for J in Result'Range loop

               if Is_Start_Of_Wide_Char_For_ASIS (Src, S, L.Comment_Sloc) then
                  Scan_Wide (Src, S, Next_Ch, Success);

                  Result (J) := Wide_Character'Val (Next_Ch);

               else
                  Result (J) := To_Wide_Character (Src (S));
                  S := S + 1;
               end if;

         end loop;

         return Result;
      end;

   end Line_Wide_Image;

   ---------
   -- Set --
   ---------

   procedure Set_Line_Length (L : in out Line; N : Character_Position) is
   begin
      L.Length := N;
   end Set_Line_Length;

   procedure Set_Line_Location (L : in out Line; S : Source_Ptr) is
   begin
      L.Rel_Sloc := L.Rel_Sloc + (S - L.Sloc);
      L.Sloc := S;
   end Set_Line_Location;

   ---------------
   -- Set_Lines --
   ---------------

   procedure Set_Lines (LList : in out Line_List; El : Element) is
      First_Line : constant Line_Number := LList'First;
      Last_Line  : constant Line_Number := LList'Last;
      El_Sloc    : constant Source_Ptr  := Location (El);
      --  This call to Location resets the tree for El, if needed;
      --  and this makes all the routine "tree-swapping-safe"
      Sloc_Move : constant Source_Ptr   := Rel_Sloc (El) - El_Sloc;
      --  Sloc_Move in fact is equal to - Sloc (Top (Enclosing_CU)),
      --  so by adding Sloc_Move we can get relative Sloc for lines:

      --  We define local variables for Element characteristics in order
      --  not to compute this in the loop:
      El_Encl_Unit : constant Unit_Id      := Encl_Unit_Id (El);
      El_Encl_Cont : constant Context_Id   := Encl_Cont_Id (El);
      El_Encl_Tree : constant Tree_Id      := Encl_Tree    (El);
      El_Obtained  : constant ASIS_OS_Time := Obtained     (El);

      SFI : constant Source_File_Index := Get_Source_File_Index (El_Sloc);
      Src_First : constant Source_Ptr := Source_First (SFI);
      Src       : constant Source_Buffer_Ptr := Source_Text (SFI);

      S           : Source_Ptr;
      Wide_Length : Character_Position;
      --  Line length counted in wide characters

      Comment_Pos : Source_Ptr;
      --  Start of the comment in the next line image. Zero if there is no
      --  comment as a part of the line image
   begin
      --  the only thing which requires special processing is
      --  setting of the length of the last Line in LList if
      --  this Line corresponds to the last line in the compilation
      --  containing El.

      --  We start from settings which do not require any
      --  special processing. We take from Element all which can
      --  be safely "transferred" into Lines. Note, that we know,
      --  that El is valid, that is, the Context from which it had been
      --  obtained was not closed after obtaining this Element. So we
      --  simply copy the time when El was obtained in all the Lines
      --  in Line list
      for LN in First_Line .. Last_Line loop
         LList (LN).Enclosing_Unit    := El_Encl_Unit;
         LList (LN).Enclosing_Context := El_Encl_Cont;
         LList (LN).Enclosing_Tree    := El_Encl_Tree;
         LList (LN).Obtained          := El_Obtained;

         LList (LN).Sloc     := Line_Start (Physical_Line_Number (LN), SFI);
         LList (LN).Rel_Sloc := LList (LN).Sloc + Sloc_Move;
      end loop;

      --  Counting Line lengths. Firtst we count length in the internal
      --  representation:
      for LN in First_Line .. Last_Line - 1 loop
         S := LList (LN + 1).Sloc - 1;

         while S > Src_First
            and then
               Is_EOL_Char (Src (S))
            and then
               S >= LList (LN).Sloc
         loop
            S := S - 1;
         end loop;

         if S = Src_First then
            --  Empty lines in the beginning of a source file
            LList (LN).Length := 0;
         else
            LList (LN).Length :=
               Character_Position (S - LList (LN).Sloc + 1);
         end if;

      end loop;

      --  The special case of the last Line in the list:
      S := LList (Last_Line).Sloc;

      while S < Source_Last (SFI)
         and then
            not Is_EOL_Char (Src (S))
      loop
         S := S + 1;
      end loop;

      LList (Last_Line).Length :=
         Character_Position (S - LList (Last_Line).Sloc);

      --  Recompute line lengths, taking into account possible encodings of
      --  upper half characters. The result should be the lengths of the lines
      --  in the original source

      for LN in First_Line .. Last_Line loop

         if LList (LN).Length /= 0 then
            Wide_Length := 0;
            S           := LList (LN).Sloc;
            Comment_Pos := Comment_Beginning
              (Src (S .. S + Source_Ptr (LList (LN).Length) - 1));

            LList (LN).Comment_Sloc := Comment_Pos;

            while S < LList (LN).Sloc + Source_Ptr (LList (LN).Length) loop

               if Is_Start_Of_Wide_Char_For_ASIS (Src, S, Comment_Pos) then
                  Skip_Wide_For_ASIS (Src, S);
               else
                  S := S + 1;
               end if;

               Wide_Length := Wide_Length + 1;
            end loop;

            LList (LN).Length := Wide_Length;
         end if;
      end loop;

   end Set_Lines;

end Asis.Text.Set_Get;