matreshka_league_21.0.0_0c8f4d47/source/web/tools/a2js/properties-declarations-defining_names.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
------------------------------------------------------------------------------
--                                                                          --
--                            Matreshka Project                             --
--                                                                          --
--                               Web Framework                              --
--                                                                          --
--                              Tools Component                             --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2015, Vadim Godunko <vgodunko@gmail.com>                     --
-- All rights reserved.                                                     --
--                                                                          --
-- Redistribution and use in source and binary forms, with or without       --
-- modification, are permitted provided that the following conditions       --
-- are met:                                                                 --
--                                                                          --
--  * Redistributions of source code must retain the above copyright        --
--    notice, this list of conditions and the following disclaimer.         --
--                                                                          --
--  * Redistributions in binary form must reproduce the above copyright     --
--    notice, this list of conditions and the following disclaimer in the   --
--    documentation and/or other materials provided with the distribution.  --
--                                                                          --
--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
--    contributors may be used to endorse or promote products derived from  --
--    this software without specific prior written permission.              --
--                                                                          --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 5511 $ $Date: 2015-12-18 16:11:30 +0300 (Пт, 18 дек 2015) $
------------------------------------------------------------------------------
with Ada.Containers.Hashed_Sets;

with Asis.Declarations;
with Asis.Elements;

with League.Strings.Hash;

with Properties.Tools;

package body Properties.Declarations.Defining_Names is

   package String_Maps is new Ada.Containers.Hashed_Sets
     (Element_Type        => League.Strings.Universal_String,
      Hash                => League.Strings.Hash,
      Equivalent_Elements => League.Strings."=",
      "="                 => League.Strings."=");

   Reserved : String_Maps.Set;

   Map : constant array (Asis.Operator_Kinds
                           range Asis.An_And_Operator .. Asis.A_Not_Operator)
     of League.Strings.Universal_String :=
       (Asis.An_And_Operator =>
          League.Strings.To_Universal_String ("_and"),
        Asis.An_Or_Operator =>
          League.Strings.To_Universal_String ("_or"),
        Asis.An_Xor_Operator =>
          League.Strings.To_Universal_String ("_xor"),
        Asis.An_Equal_Operator =>
          League.Strings.To_Universal_String ("_eq"),
        Asis.A_Not_Equal_Operator =>
          League.Strings.To_Universal_String ("_ne"),
        Asis.A_Less_Than_Operator =>
          League.Strings.To_Universal_String ("_lt"),
        Asis.A_Less_Than_Or_Equal_Operator =>
          League.Strings.To_Universal_String ("_le"),
        Asis.A_Greater_Than_Operator =>
          League.Strings.To_Universal_String ("_gt"),
        Asis.A_Greater_Than_Or_Equal_Operator =>
          League.Strings.To_Universal_String ("_ge"),
        Asis.A_Plus_Operator =>
          League.Strings.To_Universal_String ("_plus"),
        Asis.A_Minus_Operator =>
          League.Strings.To_Universal_String ("_minus"),
        Asis.A_Concatenate_Operator =>
          League.Strings.To_Universal_String ("_join"),
        Asis.A_Unary_Plus_Operator =>
          League.Strings.To_Universal_String ("_nop"),
        Asis.A_Unary_Minus_Operator =>
          League.Strings.To_Universal_String ("_neg"),
        Asis.A_Multiply_Operator =>
          League.Strings.To_Universal_String ("_mul"),
        Asis.A_Divide_Operator =>
          League.Strings.To_Universal_String ("_div"),
        Asis.A_Mod_Operator =>
          League.Strings.To_Universal_String ("_mod"),
        Asis.A_Rem_Operator =>
          League.Strings.To_Universal_String ("_rem"),
        Asis.An_Exponentiate_Operator =>
          League.Strings.To_Universal_String ("_power"),
        Asis.An_Abs_Operator =>
          League.Strings.To_Universal_String ("_abs"),
        Asis.A_Not_Operator =>
          League.Strings.To_Universal_String ("_not"));

   function Get_Name
     (Name : Asis.Defining_Name) return League.Strings.Universal_String;

   ----------
   -- Code --
   ----------

   function Code
     (Engine  : access Engines.Contexts.Context;
      Element : Asis.Defining_Name;
      Name    : Engines.Text_Property) return League.Strings.Universal_String
   is

      function Is_This return Boolean;
      function Get_Suffix return League.Strings.Universal_String;
      function Prev_Subprogram_Count return Natural;
      function Is_Subprogram (Kind : Asis.Declaration_Kinds) return Boolean;

      Decl : constant Asis.Declaration :=
        Properties.Tools.Enclosing_Declaration (Element);
      Kind  : constant Asis.Declaration_Kinds :=
        Asis.Elements.Declaration_Kind (Decl);

      Text : League.Strings.Universal_String := Get_Name (Element);

      ----------------
      -- Get_Suffix --
      ----------------

      function Get_Suffix return League.Strings.Universal_String is
         Result : League.Strings.Universal_String;
      begin
         if Is_Subprogram (Kind)
           and then not Asis.Elements.Is_Nil
             (Asis.Elements.Enclosing_Element (Decl))
         then
            declare
               Count : constant Natural := Prev_Subprogram_Count;
               Image : Wide_Wide_String := Natural'Wide_Wide_Image (Count);
            begin
               if Count > 0 then
                  Image (1) := '$';
                  Result.Append (Image);
                  return Result;
               end if;
            end;
         end if;

         if Reserved.Contains (Text) then
            Result.Append ("$");
         end if;

         return Result;
      end Get_Suffix;

      -------------------
      -- Is_Subprogram --
      -------------------

      function Is_Subprogram (Kind : Asis.Declaration_Kinds) return Boolean is
      begin
         return Kind in Asis.A_Procedure_Declaration
           | Asis.A_Procedure_Body_Declaration
             | Asis.An_Expression_Function_Declaration
               | Asis.A_Procedure_Renaming_Declaration
                 | Asis.A_Procedure_Body_Stub
                   | Asis.A_Procedure_Instantiation
                     | Asis.A_Function_Declaration
                       | Asis.A_Function_Body_Declaration
                         | Asis.A_Function_Renaming_Declaration
                           | Asis.A_Function_Body_Stub
                             | Asis.A_Function_Instantiation;
      end Is_Subprogram;

      -------------
      -- Is_This --
      -------------

      function Is_This return Boolean is
         use type Asis.Declaration_Kinds;

         Proc : Asis.Declaration;
      begin
         if Kind = Asis.A_Parameter_Specification then
            Proc := Asis.Elements.Enclosing_Element (Decl);

            if Engine.Boolean.Get_Property (Proc, Engines.Is_Dispatching)
              and then Asis.Elements.Is_Equal
                (Decl, Asis.Declarations.Parameter_Profile (Proc) (1))
            then
               return True;
            end if;
         end if;

         return False;
      end Is_This;

      ---------------------------
      -- Prev_Subprogram_Count --
      ---------------------------

      function Prev_Subprogram_Count return Natural is
         Parent : constant Asis.Declaration :=
           Properties.Tools.Enclosing_Declaration (Decl);
         Kind  : constant Asis.Declaration_Kinds :=
           Asis.Elements.Declaration_Kind (Parent);
      begin
         case Kind is
            when Asis.A_Package_Declaration =>
               declare
                  use type Asis.Declaration_List;
                  Result : Natural := 0;
                  List : constant Asis.Declaration_List :=
                    Asis.Declarations.Visible_Part_Declarative_Items (Parent) &
                    Asis.Declarations.Private_Part_Declarative_Items (Parent);
               begin
                  for J in List'Range loop
                     if Asis.Elements.Is_Equal (Decl, List (J)) then
                        return Result;
                     elsif Is_Subprogram
                       (Asis.Elements.Declaration_Kind (List (J)))
                     then
                        declare
                           use type League.Strings.Universal_String;

                           Name : constant Asis.Defining_Name :=
                             Asis.Declarations.Names (List (J)) (1);
                           Image : constant League.Strings.Universal_String :=
                             Get_Name (Name);
                        begin
                           if Image = Text then
                              Result := Result + 1;
                           end if;
                        end;
                     end if;
                  end loop;

                  return 0;
               end;
            when others =>
               return 0;
         end case;
      end Prev_Subprogram_Count;

      Link_Name : constant Wide_String :=
        Properties.Tools.Get_Aspect (Decl, "Link_Name");
      Spec : constant Asis.Declaration :=
        (if Is_Subprogram (Kind) then
              Asis.Declarations.Corresponding_Declaration (Decl)
         else
            Asis.Nil_Element);
   begin
      if Link_Name = "" then
         if Is_This then
            return League.Strings.To_Universal_String ("this");
         elsif not Asis.Elements.Is_Nil (Spec)
           and then Kind not in Asis.A_Generic_Instantiation
           and then not Asis.Elements.Is_Equal (Decl, Spec)
         then
            --  If this is subprogram body, return name of its specification
            return Engine.Text.Get_Property
              (Asis.Declarations.Names (Spec) (1), Name);
         end if;

         Text.Append (Get_Suffix);
      else
         Text := League.Strings.From_UTF_16_Wide_String (Link_Name);
      end if;

      return Text;
   end Code;

   --------------
   -- Get_Name --
   --------------

   function Get_Name
     (Name : Asis.Defining_Name) return League.Strings.Universal_String is
   begin
      case Asis.Elements.Defining_Name_Kind (Name) is
         when Asis.A_Defining_Operator_Symbol =>
            return Map (Asis.Elements.Operator_Kind (Name));
         when others =>
            declare
               Image : constant Wide_String :=
                 Asis.Declarations.Defining_Name_Image (Name);
               Text : constant League.Strings.Universal_String :=
                 League.Strings.From_UTF_16_Wide_String (Image)
                 .To_Lowercase;
            begin
               return Text;
            end;
      end case;
   end Get_Name;

   -----------------
   -- Method_Name --
   -----------------

   function Method_Name
     (Engine  : access Engines.Contexts.Context;
      Element : Asis.Defining_Name;
      Name    : Engines.Text_Property) return League.Strings.Universal_String
   is
      pragma Unreferenced (Engine, Name);
      Decl : constant Asis.Declaration :=
        Properties.Tools.Enclosing_Declaration (Element);
      Link_Name : constant Wide_String :=
        Properties.Tools.Get_Aspect (Decl, "Link_Name");
      Text : League.Strings.Universal_String := Get_Name (Element);
   begin
      if Link_Name /= "" then
         Text := League.Strings.From_UTF_16_Wide_String (Link_Name);
      end if;

      return Text;
   end Method_Name;

begin
   Reserved.Insert (League.Strings.To_Universal_String ("break"));
   Reserved.Insert (League.Strings.To_Universal_String ("case"));
   Reserved.Insert (League.Strings.To_Universal_String ("class"));
   Reserved.Insert (League.Strings.To_Universal_String ("catch"));
   Reserved.Insert (League.Strings.To_Universal_String ("const"));
   Reserved.Insert (League.Strings.To_Universal_String ("continue"));
   Reserved.Insert (League.Strings.To_Universal_String ("debugger"));
   Reserved.Insert (League.Strings.To_Universal_String ("default"));
   Reserved.Insert (League.Strings.To_Universal_String ("delete"));
   Reserved.Insert (League.Strings.To_Universal_String ("do"));
   Reserved.Insert (League.Strings.To_Universal_String ("else"));
   Reserved.Insert (League.Strings.To_Universal_String ("export"));
   Reserved.Insert (League.Strings.To_Universal_String ("extends"));
   Reserved.Insert (League.Strings.To_Universal_String ("finally"));
   Reserved.Insert (League.Strings.To_Universal_String ("for"));
   Reserved.Insert (League.Strings.To_Universal_String ("function"));
   Reserved.Insert (League.Strings.To_Universal_String ("if"));
   Reserved.Insert (League.Strings.To_Universal_String ("import"));
   Reserved.Insert (League.Strings.To_Universal_String ("in"));
   Reserved.Insert (League.Strings.To_Universal_String ("instanceof"));
   Reserved.Insert (League.Strings.To_Universal_String ("let"));
   Reserved.Insert (League.Strings.To_Universal_String ("new"));
   Reserved.Insert (League.Strings.To_Universal_String ("return"));
   Reserved.Insert (League.Strings.To_Universal_String ("super"));
   Reserved.Insert (League.Strings.To_Universal_String ("switch"));
   Reserved.Insert (League.Strings.To_Universal_String ("this"));
   Reserved.Insert (League.Strings.To_Universal_String ("throw"));
   Reserved.Insert (League.Strings.To_Universal_String ("try"));
   Reserved.Insert (League.Strings.To_Universal_String ("typeof"));
   Reserved.Insert (League.Strings.To_Universal_String ("var"));
   Reserved.Insert (League.Strings.To_Universal_String ("void"));
   Reserved.Insert (League.Strings.To_Universal_String ("while"));
   Reserved.Insert (League.Strings.To_Universal_String ("with"));
   Reserved.Insert (League.Strings.To_Universal_String ("yield"));

   Reserved.Insert (League.Strings.To_Universal_String ("self"));
end Properties.Declarations.Defining_Names;