1
------------------------------------------------------------------------------
2
--                                                                          --
3
--                           OCARINA COMPONENTS                             --
4
--                                                                          --
5
--                  OCARINA.BACKENDS.ASN1_TREE.GENERATOR                    --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--         Copyright (C) 2010-2019 ESA & ISAE, 2019-2020 OpenAADL           --
10
--                                                                          --
11
-- Ocarina  is free software; you can redistribute it and/or modify under   --
12
-- terms of the  GNU General Public License as published  by the Free Soft- --
13
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
15
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
16
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
17
--                                                                          --
18
-- As a special exception under Section 7 of GPL version 3, you are granted --
19
-- additional permissions described in the GCC Runtime Library Exception,   --
20
-- version 3.1, as published by the Free Software Foundation.               --
21
--                                                                          --
22
-- You should have received a copy of the GNU General Public License and    --
23
-- a copy of the GCC Runtime Library Exception along with this program;     --
24
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
27
--                    Ocarina is maintained by OpenAADL team                --
28
--                              (info@openaadl.org)                         --
29
--                                                                          --
30
------------------------------------------------------------------------------
31

32
with GNAT.OS_Lib;    use GNAT.OS_Lib;
33
with Outfiles;       use Outfiles;
34
with Ocarina.Namet;  use Ocarina.Namet;
35
with Ocarina.Output; use Ocarina.Output;
36

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

42
package body Ocarina.Backends.ASN1_Tree.Generator is
43

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

49
   procedure Write (T : Token_Type);
50
   procedure Write_Line (T : Token_Type);
51

52
   pragma Unreferenced (Write_Line);
53

54
   procedure Generate_ASN1_File (N : Node_Id);
55
   procedure Generate_Module (N : Node_Id);
56
   procedure Generate_Type_Definition (N : Node_Id);
57
   procedure Generate_Enumerated (N : Node_Id);
58
   procedure Generate_Enumerated_Value (N : Node_Id);
59
   procedure Generate_Sequence (N : Node_Id);
60
   procedure Generate_Sequence_Member (N : Node_Id);
61
   procedure Generate_Choice (N : Node_Id);
62
   procedure Generate_Choice_Member (N : Node_Id);
63
   procedure Generate_Defining_Identifier (N : Node_Id);
64
   procedure Generate_Type_Designator (N : Node_Id);
65

66
   -----------
67
   -- Write --
68
   -----------
69

70 0
   procedure Write (T : Token_Type) is
71
   begin
72 0
      Write_Name (Token_Image (T));
73 0
   end Write;
74

75
   ----------------
76
   -- Write_Line --
77
   ----------------
78

79 0
   procedure Write_Line (T : Token_Type) is
80
   begin
81 0
      Write (T);
82 0
      Write_Eol;
83 0
   end Write_Line;
84

85
   --------------
86
   -- Generate --
87
   --------------
88

89 1
   procedure Generate (N : Node_Id) is
90
   begin
91
      case Kind (N) is
92 1
         when K_ASN1_File =>
93 1
            Generate_ASN1_File (N);
94

95 1
         when K_ASN1_Module =>
96 1
            Generate_Module (N);
97

98 1
         when K_Type_Definition =>
99 1
            Generate_Type_Definition (N);
100

101 1
         when K_Enumerated =>
102 1
            Generate_Enumerated (N);
103

104 1
         when K_Enumerated_Value =>
105 1
            Generate_Enumerated_Value (N);
106

107 1
         when K_Sequence =>
108 1
            Generate_Sequence (N);
109

110 1
         when K_Sequence_Member =>
111 1
            Generate_Sequence_Member (N);
112

113 1
         when K_Choice =>
114 1
            Generate_Choice (N);
115

116 1
         when K_Choice_Member =>
117 1
            Generate_Choice_Member (N);
118

119 1
         when K_Defining_Identifier =>
120 1
            Generate_Defining_Identifier (N);
121

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

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

131
   ------------------------
132
   -- Generate_ASN1_File --
133
   ------------------------
134

135 1
   procedure Generate_ASN1_File (N : Node_Id) is
136 1
      Fd : File_Descriptor;
137
   begin
138 1
      if No (N) then
139 0
         return;
140
      end if;
141 1
      Get_Name_String (Name (Defining_Identifier (N)));
142
      Fd := Create_File (Name_Buffer (1 .. Name_Len) & ".asn", Text);
143 1
      Set_Output (Fd);
144

145 1
      Generate (Module_Node (N));
146

147 1
      Release_Output (Fd);
148
   end Generate_ASN1_File;
149

150
   ---------------------
151
   -- Generate_Module --
152
   ---------------------
153

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

171
   ------------------------------
172
   -- Generate_Type_Definition --
173
   ------------------------------
174

175 1
   procedure Generate_Type_Definition (N : Node_Id) is
176
   begin
177 1
      Write_Name (Name (N));
178 1
      Write_Space;
179 1
      Write_Str (" ::= ");
180 1
      Generate (Declaration (N));
181 1
   end Generate_Type_Definition;
182

183
   -------------------------
184
   -- Generate_Enumerated --
185
   -------------------------
186

187 1
   procedure Generate_Enumerated (N : Node_Id) is
188 1
      P : Node_Id;
189
   begin
190 1
      Write_Str (" ENUMERATED {");
191 1
      Write_Eol;
192 1
      Increment_Indentation;
193 1
      Write_Indentation;
194 1
      if not Is_Empty (Values (N)) then
195 1
         P := First_Node (Values (N));
196 1
         while Present (P) loop
197 1
            Generate (P);
198 1
            P := Next_Node (P);
199 1
            if P /= No_Node then
200 1
               Write_Char (',');
201 1
               Write_Eol;
202 1
               Write_Indentation;
203
            end if;
204 1
         end loop;
205
      end if;
206 1
      Write_Eol;
207 1
      Write_Indentation;
208 1
      Write_Line ("}");
209 1
      Decrement_Indentation;
210 1
      Write_Indentation;
211 1
   end Generate_Enumerated;
212

213
   -------------------------------
214
   -- Generate_Enumerated_Value --
215
   -------------------------------
216

217 1
   procedure Generate_Enumerated_Value (N : Node_Id) is
218
   begin
219 1
      Write_Name (Name (N));
220 1
      if Value (N) /= No_Value then
221 1
         Write_Str (" (");
222 1
         Write_Str (Image (Value (N)));
223 1
         Write_Char (')');
224
      end if;
225 1
   end Generate_Enumerated_Value;
226

227
   -----------------------
228
   -- Generate_Sequence --
229
   -----------------------
230

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

254
   ------------------------------
255
   -- Generate_Sequence_Member --
256
   ------------------------------
257

258 1
   procedure Generate_Sequence_Member (N : Node_Id) is
259
   begin
260 1
      Write_Name (Member_Name (N));
261 1
      Write_Space;
262 1
      Generate (Member_Type (N));
263 1
   end Generate_Sequence_Member;
264

265
   ---------------------
266
   -- Generate_Choice --
267
   ---------------------
268

269 1
   procedure Generate_Choice (N : Node_Id) is
270 1
      P : Node_Id;
271
   begin
272 1
      Write_Line (" CHOICE {");
273 1
      Increment_Indentation;
274 1
      if not Is_Empty (Values (N)) then
275 1
         P := First_Node (Values (N));
276 1
         while Present (P) loop
277 1
            Write_Indentation;
278 1
            Generate (P);
279 1
            P := Next_Node (P);
280 1
            if P /= No_Node then
281 1
               Write_Char (',');
282 1
               Write_Eol;
283
            end if;
284 1
         end loop;
285
      end if;
286 1
      Write_Eol;
287 1
      Decrement_Indentation;
288 1
      Write_Indentation;
289 1
      Write_Line ("}");
290 1
   end Generate_Choice;
291

292
   ------------------------------
293
   -- Generate_Choice_Member --
294
   ------------------------------
295

296 1
   procedure Generate_Choice_Member (N : Node_Id) is
297
   begin
298 1
      Write_Name (Member_Name (N));
299 1
      Write_Space;
300 1
      Generate (Member_Type (N));
301 1
   end Generate_Choice_Member;
302

303
   ----------------------------------
304
   -- Generate_Defining_Identifier --
305
   ----------------------------------
306

307 1
   procedure Generate_Defining_Identifier (N : Node_Id) is
308
   begin
309 1
      Write_Name (Name (N));
310 1
   end Generate_Defining_Identifier;
311

312
   ------------------------------
313
   -- Generate_Type_Designator --
314
   ------------------------------
315

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

333
end Ocarina.Backends.ASN1_Tree.Generator;

Read our documentation on viewing source code .

Loading