protobuf_1.0.0_43962766/source/compiler/compiler-enum_descriptors.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
--  MIT License
--
--  Copyright (c) 2020 Max Reznik
--
--  Permission is hereby granted, free of charge, to any person obtaining a
--  copy of this software and associated documentation files (the "Software"),
--  to deal in the Software without restriction, including without limitation
--  the rights to use, copy, modify, merge, publish, distribute, sublicense,
--  and/or sell copies of the Software, and to permit persons to whom the
--  Software is furnished to do so, subject to the following conditions:
--
--  The above copyright notice and this permission notice shall be included in
--  all copies or substantial portions of the Software.
--
--  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
--  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
--  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
--  DEALINGS IN THE SOFTWARE.

with Ada.Containers.Ordered_Sets;

package body Compiler.Enum_Descriptors is

   F : Ada_Pretty.Factory renames Compiler.Context.Factory;

   function "+" (Text : Wide_Wide_String)
     return League.Strings.Universal_String
       renames League.Strings.To_Universal_String;

   function Type_Name
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto)
      return League.Strings.Universal_String;
   --  Return Ada type (simple) name

   function Default
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto)
      return League.Strings.Universal_String;
   --  Return default value for given enum type as string

   function Literal_Name
     (Self  : Google.Protobuf.Descriptor.Enum_Descriptor_Proto;
      Index : Positive)
      return League.Strings.Universal_String;
   --  Return default value for given enum type as string

   function Max_Value
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto) return Integer;
   --  Maximum representation value

   function Min_Value
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto) return Integer;
   --  Minimum representation value

   -------------
   -- Default --
   -------------

   function Default
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto)
      return League.Strings.Universal_String is
   begin
      return Literal_Name (Self, 1);
   end Default;

   -----------------
   -- Get_Literal --
   -----------------

   function Get_Literal (Value : Integer) return Ada_Pretty.Node_Access is
      Result : Ada_Pretty.Node_Access := F.New_Literal (abs Value);
   begin
      if Value < 0 then
         Result := F.New_Infix (+"-", Result);
      end if;

      return Result;
   end Get_Literal;

   ------------------
   -- Literal_Name --
   ------------------

   function Literal_Name
     (Self  : Google.Protobuf.Descriptor.Enum_Descriptor_Proto;
      Index : Positive)
      return League.Strings.Universal_String
   is
      use type League.Strings.Universal_String;

      Name  : constant League.Strings.Universal_String := Type_Name (Self);
      Literal : League.Strings.Universal_String :=
        Self.Value (Index).Name.Value;
   begin
      if Literal.To_Lowercase = Name.To_Lowercase then
         Literal.Prepend ("PB_");
      end if;

      return Literal;
   end Literal_Name;

   ---------------
   -- Max_Value --
   ---------------

   function Max_Value
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto) return Integer
   is
      Result : Integer := Integer (Self.Value (1).Number.Value);
      Next   : Integer;
   begin
      for J in 2 .. Self.Value.Length loop
         Next := Integer (Self.Value (J).Number.Value);
         Result := Integer'Max (Result, Next);
      end loop;

      return Result;
   end Max_Value;

   ---------------
   -- Min_Value --
   ---------------

   function Min_Value
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto) return Integer
   is
      Result : Integer := Integer (Self.Value (1).Number.Value);
      Next   : Integer;
   begin
      for J in 2 .. Self.Value.Length loop
         Next := Integer (Self.Value (J).Number.Value);
         Result := Integer'Min (Result, Next);
      end loop;

      return Result;
   end Min_Value;


   --------------------------
   -- Populate_Named_Types --
   --------------------------

   procedure Populate_Named_Types
     (Self        :        Google.Protobuf.Descriptor.Enum_Descriptor_Proto;
      PB_Prefix   :        League.Strings.Universal_String;
      Ada_Package :        League.Strings.Universal_String;
      Map         : in out Compiler.Context.Named_Type_Maps.Map)
   is
      Name  : constant League.Strings.Universal_String := Type_Name (Self);
      Key   : League.Strings.Universal_String := PB_Prefix;
      Value : constant Compiler.Context.Named_Type :=
        (Is_Enumeration => True,
         Ada_Type       =>
           (Package_Name => Ada_Package,
            Type_Name    => Name),
         Enum           =>
           (Min => Min_Value (Self),
            Max => Max_Value (Self),
            Default => Default (Self)));
   begin
      Key.Append (".");

      if Self.Name.Is_Set then
         Key.Append (Self.Name.Value);
      end if;

      Map.Insert (Key, Value);
   end Populate_Named_Types;

   -----------------
   -- Public_Spec --
   -----------------

   function Public_Spec
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto)
      return Ada_Pretty.Node_Access
   is
      use type League.Strings.Universal_String;

      type Enum is record
         Value : Integer;
         Name  : League.Strings.Universal_String;
      end record;

      function Less (Left, Right : Enum) return Boolean;

      function Less (Left, Right : Enum) return Boolean is
      begin
         return Left.Value < Right.Value
           or else (Left.Value = Right.Value and then Left.Name < Right.Name);
      end Less;

      package Enum_Sets is new Ada.Containers.Ordered_Sets (Enum, Less);

      Name    : constant League.Strings.Universal_String := Type_Name (Self);
      Result  : Ada_Pretty.Node_Access;
      Clause  : Ada_Pretty.Node_Access;
      Item    : Ada_Pretty.Node_Access;
      Aliases : Ada_Pretty.Node_Access;
      Enums   : Enum_Sets.Set;
      Done    : Compiler.Context.String_Sets.Set;
      First   : Boolean := True;
      Prev    : Enum;
   begin
      for J in 1 .. Self.Value.Length loop
         Enums.Include
           ((Value => Integer (Self.Value (J).Number.Value),
             Name  => Literal_Name (Self, J)));
      end loop;

      Prev := Enums.Last_Element;

      for J of Enums loop
         if Done.Contains (J.Name.To_Lowercase) then
            null;  --  Skip name that differs only in character case
         elsif not First and Prev.Value = J.Value then
            Done.Insert (J.Name.To_Lowercase);

            Aliases := F.New_List
              (Aliases,
               F.New_Subprogram_Declaration
                (Specification =>
                  F.New_Subprogram_Specification
                   (Name          => F.New_Name (J.Name),
                    Result        => F.New_Name (Name)),
                 Expression    =>
                   F.New_Name (Prev.Name)));
         else
            Done.Insert (J.Name.To_Lowercase);
            Item := F.New_Argument_Association (F.New_Name (J.Name));
            Result := F.New_List (Result, Item);

            Clause := F.New_List
              (Clause,
               F.New_Argument_Association
                (Choice => F.New_Name (J.Name),
                 Value  => Get_Literal (J.Value)));
         end if;

         Prev := J;
         First := False;
      end loop;

      Result := F.New_Type
        (Name       => F.New_Name (Name),
         Definition => F.New_Parentheses (Result));

      Clause := F.New_Statement
        (F.New_Apply
          (F.New_Name ("for " & Name & " use"),
           Clause));

      Item := F.New_List
        (Aliases,
         F.New_Package_Instantiation
          (Name        => F.New_Name (Name & "_Vectors"),
           Template    => F.New_Selected_Name (+"PB_Support.Vectors"),
           Actual_Part => F.New_Name (Name)));

      Result := F.New_List ((Result, Clause, Item));

      return Result;
   end Public_Spec;

   ---------------
   -- Type_Name --
   ---------------

   function Type_Name
     (Self : Google.Protobuf.Descriptor.Enum_Descriptor_Proto)
      return League.Strings.Universal_String is
   begin
      if Self.Name.Is_Set then
         return Compiler.Context.To_Ada_Name (Self.Name.Value);
      else
         return +"Enum";
      end if;
   end Type_Name;

end Compiler.Enum_Descriptors;