ocarina-backends-asn1_tree-generator.adb 10 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                  OCARINA.BACKENDS.ASN1_TREE.GENERATOR                    --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--                   Copyright (C) 2010-2015 ESA & ISAE.                    --
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina 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/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27
28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29
30
31
--                                                                          --
------------------------------------------------------------------------------

yoogx's avatar
yoogx committed
32
33
34
35
with GNAT.OS_Lib;    use GNAT.OS_Lib;
with Outfiles;       use Outfiles;
with Ocarina.Namet;  use Ocarina.Namet;
with Ocarina.Output; use Ocarina.Output;
36

37
with Ocarina.Backends.ASN1_Values;
38
39
40
41
42
43
with Ocarina.Backends.ASN1_Tree.Nodes;
with Ocarina.Backends.ASN1_Tree.Nutils;
with Ocarina.Backends.Messages;

package body Ocarina.Backends.ASN1_Tree.Generator is

44
   use Ocarina.Backends.ASN1_Values;
45
46
47
48
49
50
51
   use Ocarina.Backends.ASN1_Tree.Nodes;
   use Ocarina.Backends.ASN1_Tree.Nutils;
   use Ocarina.Backends.Messages;

   procedure Write (T : Token_Type);
   procedure Write_Line (T : Token_Type);

52
53
   pragma Unreferenced (Write_Line);

julien.delange's avatar
julien.delange committed
54
55
   procedure Generate_ASN1_File (N : Node_Id);
   procedure Generate_Module (N : Node_Id);
56
57
58
   procedure Generate_Type_Definition (N : Node_Id);
   procedure Generate_Enumerated (N : Node_Id);
   procedure Generate_Enumerated_Value (N : Node_Id);
59
60
   procedure Generate_Sequence (N : Node_Id);
   procedure Generate_Sequence_Member (N : Node_Id);
61
62
63
   procedure Generate_Choice (N : Node_Id);
   procedure Generate_Choice_Member (N : Node_Id);
   procedure Generate_Defining_Identifier (N : Node_Id);
64
   procedure Generate_Type_Designator (N : Node_Id);
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

   -----------
   -- Write --
   -----------

   procedure Write (T : Token_Type) is
   begin
      Write_Name (Token_Image (T));
   end Write;

   ----------------
   -- Write_Line --
   ----------------

   procedure Write_Line (T : Token_Type) is
   begin
      Write (T);
      Write_Eol;
   end Write_Line;

   --------------
   -- Generate --
   --------------

   procedure Generate (N : Node_Id) is
   begin
      case Kind (N) is
julien.delange's avatar
julien.delange committed
92
93
94
95
96
97
         when K_ASN1_File =>
            Generate_ASN1_File (N);

         when K_ASN1_Module =>
            Generate_Module (N);

98
99
100
101
102
103
104
105
106
         when K_Type_Definition =>
            Generate_Type_Definition (N);

         when K_Enumerated =>
            Generate_Enumerated (N);

         when K_Enumerated_Value =>
            Generate_Enumerated_Value (N);

107
108
109
110
111
112
         when K_Sequence =>
            Generate_Sequence (N);

         when K_Sequence_Member =>
            Generate_Sequence_Member (N);

113
114
115
116
117
118
119
120
121
         when K_Choice =>
            Generate_Choice (N);

         when K_Choice_Member =>
            Generate_Choice_Member (N);

         when K_Defining_Identifier =>
            Generate_Defining_Identifier (N);

122
123
124
         when K_Type_Designator =>
            Generate_Type_Designator (N);

125
126
127
128
129
130
         when others =>
            Display_Error ("other element in generator", Fatal => False);
            null;
      end case;
   end Generate;

julien.delange's avatar
julien.delange committed
131
132
133
134
135
136
137
138
139
140
141
   ------------------------
   -- Generate_ASN1_File --
   ------------------------

   procedure Generate_ASN1_File (N : Node_Id) is
      Fd : File_Descriptor;
   begin
      if No (N) then
         return;
      end if;
      Get_Name_String (Name (Defining_Identifier (N)));
142
      Fd := Create_File (Name_Buffer (1 .. Name_Len) & ".asn", Text);
julien.delange's avatar
julien.delange committed
143
144
145
146
147
148
149
150
151
152
153
154
      Set_Output (Fd);

      Generate (Module_Node (N));

      Release_Output (Fd);
   end Generate_ASN1_File;

   ---------------------
   -- Generate_Module --
   ---------------------

   procedure Generate_Module (N : Node_Id) is
155
      P : Node_Id;
julien.delange's avatar
julien.delange committed
156
   begin
157
158
159
160
      Write_Name (Name (N));
      Write_Space;
      Write_Str ("DEFINITIONS AUTOMATIC TAGS ::= BEGIN");
      Write_Eol;
161
162
      if not Is_Empty (Definitions (N)) then
         P := First_Node (Definitions (N));
163
164
165
166
167
         while Present (P) loop
            Generate (P);
            P := Next_Node (P);
         end loop;
      end if;
168
      Write_Line ("END");
julien.delange's avatar
julien.delange committed
169
170
   end Generate_Module;

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
   ------------------------------
   -- Generate_Type_Definition --
   ------------------------------

   procedure Generate_Type_Definition (N : Node_Id) is
   begin
      Write_Name (Name (N));
      Write_Space;
      Write_Str (" ::= ");
      Generate (Declaration (N));
   end Generate_Type_Definition;

   -------------------------
   -- Generate_Enumerated --
   -------------------------

   procedure Generate_Enumerated (N : Node_Id) is
      P : Node_Id;
   begin
      Write_Str (" ENUMERATED {");
191
192
193
      Write_Eol;
      Increment_Indentation;
      Write_Indentation;
194
195
196
197
198
199
200
      if not Is_Empty (Values (N)) then
         P := First_Node (Values (N));
         while Present (P) loop
            Generate (P);
            P := Next_Node (P);
            if P /= No_Node then
               Write_Char (',');
201
202
               Write_Eol;
               Write_Indentation;
203
204
205
            end if;
         end loop;
      end if;
206
207
      Write_Eol;
      Write_Indentation;
208
      Write_Line ("}");
209
210
      Decrement_Indentation;
      Write_Indentation;
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
   end Generate_Enumerated;

   -------------------------------
   -- Generate_Enumerated_Value --
   -------------------------------

   procedure Generate_Enumerated_Value (N : Node_Id) is
   begin
      Write_Name (Name (N));
      if Value (N) /= No_Value then
         Write_Str (" (");
         Write_Str (Image (Value (N)));
         Write_Char (')');
      end if;
   end Generate_Enumerated_Value;

227
228
229
230
231
232
233
234
   -----------------------
   -- Generate_Sequence --
   -----------------------

   procedure Generate_Sequence (N : Node_Id) is
      P : Node_Id;
   begin
      Write_Line (" SEQUENCE {");
235
      Increment_Indentation;
236
237
238
      if not Is_Empty (Values (N)) then
         P := First_Node (Values (N));
         while Present (P) loop
239
            Write_Indentation;
240
241
242
243
            Generate (P);
            P := Next_Node (P);
            if P /= No_Node then
               Write_Char (',');
244
               Write_Eol;
245
246
247
            end if;
         end loop;
      end if;
248
249
250
      Write_Eol;
      Decrement_Indentation;
      Write_Indentation;
251
252
253
254
255
256
257
258
259
260
261
      Write_Line ("}");
   end Generate_Sequence;

   ------------------------------
   -- Generate_Sequence_Member --
   ------------------------------

   procedure Generate_Sequence_Member (N : Node_Id) is
   begin
      Write_Name (Member_Name (N));
      Write_Space;
262
      Generate (Member_Type (N));
263
264
   end Generate_Sequence_Member;

265
266
267
268
269
270
271
272
273
274
275
276
   ---------------------
   -- Generate_Choice --
   ---------------------

   procedure Generate_Choice (N : Node_Id) is
      P : Node_Id;
   begin
      Write_Line (" CHOICE {");
      Increment_Indentation;
      if not Is_Empty (Values (N)) then
         P := First_Node (Values (N));
         while Present (P) loop
277
            Write_Indentation;
278
279
280
281
282
283
284
285
            Generate (P);
            P := Next_Node (P);
            if P /= No_Node then
               Write_Char (',');
               Write_Eol;
            end if;
         end loop;
      end if;
286
      Write_Eol;
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
      Decrement_Indentation;
      Write_Indentation;
      Write_Line ("}");
   end Generate_Choice;

   ------------------------------
   -- Generate_Choice_Member --
   ------------------------------

   procedure Generate_Choice_Member (N : Node_Id) is
   begin
      Write_Name (Member_Name (N));
      Write_Space;
      Generate (Member_Type (N));
   end Generate_Choice_Member;

   ----------------------------------
   -- Generate_Defining_Identifier --
   ----------------------------------

   procedure Generate_Defining_Identifier (N : Node_Id) is
   begin
      Write_Name (Name (N));
   end Generate_Defining_Identifier;

312
313
314
315
316
317
318
319
320
   ------------------------------
   -- Generate_Type_Designator --
   ------------------------------

   procedure Generate_Type_Designator (N : Node_Id) is
      Cons : constant Node_Id := Constraints (N);
   begin
      Generate (Type_Name (N));
      if Cons /= No_Node then
321
322
323
         if Size_Down (Cons) /= No_Value
           and then Size_Up (Cons) /= No_Value
         then
324
            Write_Str (" (SIZE (");
325
326
327
            Write_Str (Image (Size_Down (Cons)));
            Write_Str (" .. ");
            Write_Str (Image (Size_Up (Cons)));
328
            Write_Str (" ))");
329
330
331
332
         end if;
      end if;
   end Generate_Type_Designator;

333
end Ocarina.Backends.ASN1_Tree.Generator;