aws_24.0.0_2b75fe6d/config/ssl/dynamo/dynamo.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
------------------------------------------------------------------------------
--                            Secure Sockets Layer                          --
--                                                                          --
--                        Copyright (C) 2022, AdaCore                       --
--                                                                          --
--  This library 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 3,  or (at your  option) any --
--  later version. This library is distributed in the hope that it will be  --
--  useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    --
--                                                                          --
--  As a special exception under Section 7 of GPL version 3, you are        --
--  granted additional permissions described in the GCC Runtime Library     --
--  Exception, version 3.1, as published by the Free Software Foundation.   --
--                                                                          --
--  You should have received a copy of the GNU General Public License and   --
--  a copy of the GCC Runtime Library Exception along with this program;    --
--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see   --
--  <http://www.gnu.org/licenses/>.                                         --
--                                                                          --
--  As a special exception, if other files instantiate generics from this   --
--  unit, or you link this unit with other files to produce an executable,  --
--  this  unit  does not  by itself cause  the resulting executable to be   --
--  covered by the GNU General Public License. This exception does not      --
--  however invalidate any other reasons why the executable file  might be  --
--  covered by the  GNU Public License.                                     --
------------------------------------------------------------------------------
--
--  This program is to convert thin binding specification into optional dynamic
--  thin binding. Each routine and object aspect list containing Import aspect
--  and not containing Address aspect will be modified to have Address aspect
--  pointing to the user defined function call Symbol with String parameter
--  returning System.Address. String parameter taken either from External_Name
--  acpect if exists or from lowercased imported object name if External_Name
--  is absent.

with Ada.Command_Line;
with Ada.Strings.Wide_Wide_Unbounded;
with Ada.Text_IO;
with Ada.Wide_Wide_Characters.Handling;

with Langkit_Support.Text;

with Libadalang.Analysis;
with Libadalang.Common;

function Dynamo return Integer is

   use Ada.Command_Line;
   use Libadalang.Analysis;
   use Libadalang.Common;

   package TIO renames Ada.Text_IO;
   package WCH renames Ada.Wide_Wide_Characters.Handling;

   Source_Path : constant String := Argument (1);
   Main_Source : constant String := Argument (2);

   Ctx  : constant Analysis_Context := Create_Context;
   Unit : constant Analysis_Unit := Ctx.Get_From_File (Source_Path);

   Charset : constant String := Get_Charset (Unit);

   function WW_Argument (P : Positive) return Wide_Wide_String is
     (Langkit_Support.Text.Decode (Argument (P), Charset));
   --  Returns P command line argument in Wide_Wide_String type

   Loader_Pkg : constant Wide_Wide_String := WW_Argument (3);
   No_Rebind  : constant Wide_Wide_String := """_AWS_";
   --  No_Rebind prefix mean that this import should not be rebinded to dinamic
   --  library.

   First : Token_Reference := Unit.First_Token;

   WLF : constant Wide_Wide_Character :=
           Wide_Wide_Character'Val (Character'Pos (ASCII.LF));

   Counter : Positive := 1;

   CU : constant Ada_Node := Unit.Root;
   S  : TIO.File_Type;

   procedure Put (File : TIO.File_Type; Item : Wide_Wide_String);
   --  Encode Text as String with Charset and output it into the File

   procedure Flush_S (Node : Ada_Node'Class; Skip : Boolean);
   --  Output source text from token First to token previous before Node and
   --  move First token into first Node token if Skip is False or to next token
   --  after Node if Skip is True.

   procedure Process_Object_Decl (Node : Ada_Node);
   --  Process object declaration. Changes External_Name aspect into Address.
   --  Address value taken from another generated package with function call
   --  by External_Name aspect value if exists or by lowercased object name
   --  otherwise.

   procedure Process_Subp_Decl (Node : Ada_Node);
   --  Process routine declaration. Changes External_Name aspect into Address.
   --  Address value taken from another generated package with function call
   --  by External_Name aspect value if exists or by lowercased routine name
   --  otherwise.

   procedure Process_Ada_Node_List (Node : Ada_Node);
   --  Iterated over Node children and calls Process_Subp_Decl or
   --  Process_Object_Decl on appropriate nodes.

   procedure Process_Public_Part (Node : Ada_Node);
   --  Choose Ada node list from children and calls Process_Ada_Node_List for
   --  it.

   procedure Process_Package_Decl (Node : Ada_Node);
   --  Choose public part from children and calls Process_Public_Part for it

   procedure Process_Library_Item (Node : Ada_Node);
   --  Choose package declaration from children and calls Process_Package_Decl
   --  for it.

   -------------
   -- Flush_S --
   -------------

   procedure Flush_S (Node : Ada_Node'Class; Skip : Boolean) is
      First : constant Token_Reference := Node.Token_Start;
   begin
      Put (S, Text (Dynamo.First, Previous (First)));

      Dynamo.First := (if Skip then Next (Node.Token_End) else First);
   end Flush_S;

   ---------
   -- Put --
   ---------

   procedure Put (File : TIO.File_Type; Item : Wide_Wide_String) is
   begin
      TIO.Put (File, Langkit_Support.Text.Encode (Item, Charset));
   end Put;

   ------------------
   -- Process_Decl --
   ------------------

   procedure Process_Decl (Name : Wide_Wide_String; SA : Aspect_Spec) is

      Import_Aspect  : constant Wide_Wide_String := "import";
      Address_Aspect : constant Wide_Wide_String := "address";
      External_Name  : constant Wide_Wide_String := "external_name";

      procedure Apply_Name (Name : Wide_Wide_String);

      function Count_Image return Wide_Wide_String;

      -----------------
      -- Count_Image --
      -----------------

      function Count_Image return Wide_Wide_String is
         Result : constant Wide_Wide_String :=
                    Integer'Wide_Wide_Image (Counter);
      begin
         return Result
           (Result'First + (if Result (Result'First) = ' ' then 1 else 0)
            .. Result'Last);
      end Count_Image;

      CI : constant Wide_Wide_String := '_' & Count_Image;

      Name_Applied : Boolean := False;
      Has_Import   : Boolean := False;
      Last_A       : Aspect_Assoc;

      ----------------
      -- Apply_Name --
      ----------------

      procedure Apply_Name (Name : Wide_Wide_String) is
      begin
         Put (S, "Address => " & Loader_Pkg & ".Symbol (" & Name & ")");
         Name_Applied := True;
      end Apply_Name;

   begin
      if SA.Is_Null then
         return;
      end if;

      for A of SA.F_Aspect_Assocs loop
         declare
            A_Name : constant Wide_Wide_String := WCH.To_Lower (A.F_Id.Text);
         begin
            if A_Name = External_Name then
               declare
                  AN : constant Wide_Wide_String := A.F_Expr.Text;
               begin
                  if AN'Length > No_Rebind'Length
                    and then AN (AN'First .. AN'First + No_Rebind'Length - 1)
                             = No_Rebind
                  then
                     return;
                  else
                     Flush_S (A, Skip => True);
                     Apply_Name (AN);
                  end if;
               end;

            else
               if A_Name = Import_Aspect then
                  Has_Import := True;
               elsif A_Name = Address_Aspect then
                  return;
               end if;

               if not Name_Applied then
                  Last_A := Aspect_Assoc (A);
               end if;
            end if;
         end;
      end loop;

      if Has_Import and then not Name_Applied then
         Flush_S (Last_A, Skip => False);
         Apply_Name ('"' & WCH.To_Lower (Name) & '"');
         Put (S, ", ");
      end if;

      Counter := Counter + 1;
   end Process_Decl;

   -------------------------
   -- Process_Object_Decl --
   -------------------------

   procedure Process_Object_Decl (Node : Ada_Node) is
      D : constant Object_Decl := Node.As_Object_Decl;
   begin
      Process_Decl (D.F_Ids.Text, D.F_Aspects);
   end Process_Object_Decl;

   -----------------------
   -- Process_Subp_Decl --
   -----------------------

   procedure Process_Subp_Decl (Node : Ada_Node) is
      D : constant Subp_Decl := Node.As_Subp_Decl;
   begin
      Process_Decl (D.F_Subp_Spec.F_Subp_Name.Text, D.F_Aspects);
   end Process_Subp_Decl;

   ---------------------------
   -- Process_Ada_Node_List --
   ---------------------------

   procedure Process_Ada_Node_List (Node : Ada_Node) is
   begin
      for Item of Node.Children loop
         case Item.Kind is
            when Ada_Subp_Decl =>
               Process_Subp_Decl (Item);

            when Ada_Object_Decl =>
               Process_Object_Decl (Item);

            when others =>
               null;
         end case;
      end loop;
   end Process_Ada_Node_List;

   -------------------------
   -- Process_Public_Part --
   -------------------------

   procedure Process_Public_Part (Node : Ada_Node) is
   begin
      for Item of Node.Children loop
         if Item.Kind = Ada_Ada_Node_List then
            Process_Ada_Node_List (Item);
         end if;
      end loop;
   end Process_Public_Part;

   --------------------------
   -- Process_Package_Decl --
   --------------------------

   procedure Process_Package_Decl (Node : Ada_Node) is
   begin
      for Item of Node.Children loop
         if not Item.Is_Null and then Item.Kind = Ada_Public_Part then
            Process_Public_Part (Item);
         end if;
      end loop;
   end Process_Package_Decl;

   --------------------------
   -- Process_Library_Item --
   --------------------------

   procedure Process_Library_Item (Node : Ada_Node) is
   begin
      for Item of Node.Children loop
         if Item.Kind = Ada_Package_Decl then
            Process_Package_Decl (Item);
         end if;
      end loop;
   end Process_Library_Item;

begin
   if CU.Is_Null then
      TIO.Put_Line
        (TIO.Standard_Error, "Unable to parse file " & Source_Path);
      return 1;
   end if;

   TIO.Create (S, Name => Main_Source);

   for Item of CU.Children loop
      if Item.Kind = Ada_Library_Item then
         Flush_S (Item, Skip => False);
         Put (S, "with " & Loader_Pkg & ';' & WLF & WLF);
         Process_Library_Item (Item);
      end if;
   end loop;

   declare
      Footer : constant Wide_Wide_String :=
                 Text (First, Unit.Last_Token);
   begin
      for L in reverse Footer'Range loop
         if Footer (L) /= WLF then
            Put (S, Footer (Footer'First .. L));
            exit;
         end if;
      end loop;
   end;

   TIO.Close (S);

   return 0;
end Dynamo;