matreshka_league_21.0.0_0c8f4d47/source/web/tools/a2js/properties-statements-procedure_call_statement.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
------------------------------------------------------------------------------
--                                                                          --
--                            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: 5866 $ $Date: 2018-08-24 18:13:18 +0300 (Пт, 24 авг 2018) $
------------------------------------------------------------------------------
with Ada.Wide_Wide_Text_IO;

with Asis.Declarations;
with Asis.Definitions;
with Asis.Elements;
with Asis.Expressions;
with Asis.Statements;

with Properties.Tools;
with Properties.Expressions.Identifiers;

package body Properties.Statements.Procedure_Call_Statement is

   function Intrinsic
     (Engine  : access Engines.Contexts.Context;
      Element : Asis.Expression;
      Name    : Engines.Text_Property) return League.Strings.Universal_String;

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

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

      function Get_Parameters
        (Prefix : Asis.Expression;
         Is_Dispatching : Boolean;
         Is_JavaScript  : Boolean)
         return Asis.Parameter_Specification_List
      is
      begin
         if not Is_Dispatching and not Is_JavaScript then
            return Properties.Tools.Parameter_Profile (Prefix);
         elsif Asis.Statements.Is_Dispatching_Call (Element) then
            declare
               Decl : constant Asis.Declaration :=
                 Properties.Tools.Corresponding_Declaration (Prefix);
            begin
               case Asis.Elements.Declaration_Kind (Decl) is
                  when
                       Asis.A_Procedure_Declaration |
                       Asis.A_Function_Declaration |
                       Asis.A_Procedure_Body_Declaration |
                       Asis.A_Function_Body_Declaration |
                       Asis.A_Null_Procedure_Declaration |
                       Asis.A_Procedure_Renaming_Declaration |
                       Asis.A_Function_Renaming_Declaration |
                       Asis.An_Entry_Declaration |
                       Asis.An_Entry_Body_Declaration |
                       Asis.A_Procedure_Body_Stub |
                       Asis.A_Function_Body_Stub |
                       Asis.A_Generic_Function_Declaration |
                       Asis.A_Generic_Procedure_Declaration |
                       Asis.A_Formal_Function_Declaration |
                       Asis.A_Formal_Procedure_Declaration |
                       Asis.An_Expression_Function_Declaration =>
                     return Asis.Declarations.Parameter_Profile (Decl);
                  when others =>
                     return Asis.Nil_Element_List;
               end case;
            end;
         else
            return Asis.Nil_Element_List;
         end if;
      end Get_Parameters;

      Text   : League.Strings.Universal_String;
      Prefix : constant Asis.Expression :=
        Asis.Statements.Called_Name (Element);
      Conv   : constant Engines.Convention_Kind :=
        Engine.Call_Convention.Get_Property
          (Prefix,
           Engines.Call_Convention);
      Is_Dispatching : constant Boolean := Engine.Boolean.Get_Property
        (Prefix, Engines.Is_Dispatching);
      Is_Prefixed    : constant Boolean :=
        Conv in Engines.JavaScript_Property_Setter |
                Engines.JavaScript_Method;
      Params : constant Asis.Parameter_Specification_List :=
        Get_Parameters (Prefix, Is_Dispatching, Is_Prefixed);
      Has_Output : constant Boolean :=
        Engine.Boolean.Get_Property
          (List  => Params,
           Name  => Engines.Has_Simple_Output,
           Empty => False,
           Sum   => Properties.Tools."or"'Access);
      List   : constant Asis.Association_List :=
        Asis.Statements.Call_Statement_Parameters
          (Element, Normalized => False);
      Arg    : League.Strings.Universal_String;
   begin
      if Has_Output  then
         Text.Append ("var _r=");
      end if;

      if Conv = Engines.Intrinsic then
         Arg := Intrinsic (Engine, Element, Name);
         Text.Append (Arg);
      elsif not Is_Dispatching and not Is_Prefixed then
         Arg := Engine.Text.Get_Property (Prefix, Name);
         Text.Append (Arg);

         Text.Append ("(");

         for J in 1 .. List'Last loop
            Arg := Engine.Text.Get_Property
              (Asis.Expressions.Actual_Parameter (List (J)), Name);

            Text.Append (Arg);

            if J /= List'Last then
               Text.Append (", ");
            end if;
         end loop;

         Text.Append (")");
      elsif Conv = Engines.JavaScript_Property_Setter then
         Arg := Engine.Text.Get_Property
           (Asis.Expressions.Actual_Parameter (List (1)), Name);
         Text.Append (Arg);
         Text.Append (".");
         Text.Append
           (Engine.Text.Get_Property (Prefix, Engines.Method_Name));
         Text.Append (" = ");

         Arg := Engine.Text.Get_Property
           (Asis.Expressions.Actual_Parameter (List (2)), Name);

         Text.Append (Arg);
      elsif Asis.Statements.Is_Dispatching_Call (Element) or
        Conv = Engines.JavaScript_Method
      then
         declare
            Expr   : Asis.Expression;
            Decl   : Asis.Declaration;
            Comp   : Asis.Definition;
         begin
            Arg := Engine.Text.Get_Property
              (Asis.Expressions.Actual_Parameter (List (1)), Name);
            Text.Append (Arg);
            Text.Append (".");
            Text.Append
              (Engine.Text.Get_Property (Prefix, Engines.Method_Name));

            Text.Append ("(");

            for J in 2 .. List'Last loop
               Expr := Asis.Expressions.Actual_Parameter (List (J));
               Arg := Engine.Text.Get_Property (Expr, Name);

               Text.Append (Arg);
               Decl := Asis.Expressions.Corresponding_Expression_Type (Expr);

               if Tools.Is_Array (Expr)
                 and then Tools.Is_Array_Buffer (Decl)
               then
                  Comp := Asis.Definitions.Array_Component_Definition
                    (Tools.Type_Declaration_View (Decl));
                  Comp := Asis.Definitions.Component_Definition_View (Comp);

                  Arg := Engine.Text.Get_Property
                    (Comp, Engines.Typed_Array_Item_Type);

                  if not Arg.Is_Empty then
                     Text.Append (".");
                     Text.Append (Arg);
                  end if;
               end if;

               if J /= List'Last then
                  Text.Append (", ");
               end if;
            end loop;

            Text.Append (")");
         end;
      else
         declare
            Proc   : Asis.Declaration :=
              Asis.Statements.Corresponding_Called_Entity (Element);
         begin
            while Asis.Elements.Is_Part_Of_Inherited (Proc) loop
               Proc :=
                 Asis.Declarations.Corresponding_Subprogram_Derivation (Proc);
            end loop;

            Arg := Properties.Expressions.Identifiers.Name_Prefix
              (Engine => Engine,
               Name   => Element,
               Decl   => Proc);
            Text.Append (Arg);

            Text.Append
              (Engine.Text.Get_Property
                 (Asis.Declarations.Names (Proc) (1), Name));

            Text.Append (".call(");

            Text.Append
              (Engine.Text.Get_Property
                 (Asis.Expressions.Actual_Parameter (List (1)), Name));

            for J in 2 .. List'Last loop
               Arg := Engine.Text.Get_Property
                 (Asis.Expressions.Actual_Parameter (List (J)), Name);

               Text.Append (", ");
               Text.Append (Arg);
            end loop;

            Text.Append (")");
         end;
      end if;

      for J in Params'Range loop
         if Engine.Boolean.Get_Property
           (Params (J), Engines.Has_Simple_Output)
         then
            Text.Append (";");
            Arg := Engine.Text.Get_Property
              (Asis.Expressions.Actual_Parameter (List (J)), Name);
            Text.Append (Arg);
            Text.Append ("=_r.");
            Arg := Engine.Text.Get_Property
              (Asis.Declarations.Names (Params (J)) (1), Name);
            Text.Append (Arg);
         end if;
      end loop;

      Text.Append (";");
      return Text;
   end Code;

   ---------------
   -- Intrinsic --
   ---------------

   function Intrinsic
     (Engine  : access Engines.Contexts.Context;
      Element : Asis.Expression;
      Name    : Engines.Text_Property) return League.Strings.Universal_String
   is
      pragma Unreferenced (Name);
      Func : constant League.Strings.Universal_String :=
        Engine.Text.Get_Property
          (Asis.Statements.Called_Name (Element),
           Engines.Intrinsic_Name);

      List   : constant Asis.Association_List :=
        Asis.Statements.Call_Statement_Parameters
          (Element, Normalized => False);
      pragma Unreferenced (List);
   begin
      Ada.Wide_Wide_Text_IO.Put ("Unimplemented Intrinsic: ");
      Ada.Wide_Wide_Text_IO.Put (Func.To_Wide_Wide_String);
      raise Program_Error;
      return League.Strings.Empty_Universal_String;
   end Intrinsic;

end Properties.Statements.Procedure_Call_Statement;