ocarina-backends-python-nutils.adb 17.7 KB
Newer Older
yoogx's avatar
yoogx committed
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
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--       O C A R I N A . B A C K E N D S . P Y T H O N . N U T I L S        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2016 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/>.                                          --
--                                                                          --
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

32
33
34
35
36
37
38
39
40
41
42
43
pragma Warnings (Off);

with GNAT.Table;

with Charset;       use Charset;
with Locations;     use Locations;
with Ocarina.Namet; use Ocarina.Namet;
with Utils;         use Utils;

with Ocarina.Backends.Utils;
with Ocarina.ME_AADL.AADL_Tree.Nodes; use Ocarina.ME_AADL.AADL_Tree.Nodes;
use Ocarina.Backends.Utils;
yoogx's avatar
yoogx committed
44
with Ocarina.Backends.Python_Values; use Ocarina.Backends.Python_Values;
45

yoogx's avatar
yoogx committed
46
package body Ocarina.Backends.Python.Nutils is
47
48

   package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
yoogx's avatar
yoogx committed
49
   package XTN renames Ocarina.Backends.Python.Nodes;
50

yoogx's avatar
yoogx committed
51
52
   Keyword_Suffix : constant String := "%Python";
   --  Used to mark Python keywords and avoid collision with other languages
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

   type Entity_Stack_Entry is record
      Current_File   : Node_Id;
      Current_Entity : Node_Id;
   end record;

   No_Depth : constant Int := -1;
   package Entity_Stack is new GNAT.Table
     (Entity_Stack_Entry,
      Int,
      No_Depth + 1,
      10,
      10);

   use Entity_Stack;

   ------------------------
   -- Add_Prefix_To_Name --
   ------------------------

   function Add_Prefix_To_Name
     (Prefix : String;
      Name   : Name_Id) return Name_Id
   is
   begin
      Set_Str_To_Name_Buffer (Prefix);
      Get_Name_String_And_Append (Name);
      return Name_Find;
   end Add_Prefix_To_Name;

   ------------------------
   -- Add_Suffix_To_Name --
   ------------------------

   function Add_Suffix_To_Name
     (Suffix : String;
      Name   : Name_Id) return Name_Id
   is
   begin
      Get_Name_String (Name);
      Add_Str_To_Name_Buffer (Suffix);
      return Name_Find;
   end Add_Suffix_To_Name;

   -----------------------------
   -- Remove_Suffix_From_Name --
   -----------------------------

   function Remove_Suffix_From_Name
     (Suffix : String;
      Name   : Name_Id) return Name_Id
   is
      Length   : Natural;
      Temp_Str : String (1 .. Suffix'Length);
   begin
      Set_Str_To_Name_Buffer (Suffix);
      Length := Name_Len;
      Get_Name_String (Name);
      if Name_Len > Length then
         Temp_Str := Name_Buffer (Name_Len - Length + 1 .. Name_Len);
         if Suffix = Temp_Str then
            Set_Str_To_Name_Buffer (Name_Buffer (1 .. Name_Len - Length));
            return Name_Find;
         end if;
      end if;
      return Name;
   end Remove_Suffix_From_Name;

   -------------------------
   -- Append_Node_To_List --
   -------------------------

   procedure Append_Node_To_List (E : Node_Id; L : List_Id) is
      Last : Node_Id;

   begin
      Last := XTN.Last_Node (L);
      if No (Last) then
         XTN.Set_First_Node (L, E);
      else
         XTN.Set_Next_Node (Last, E);
      end if;
      Last := E;
      while Present (Last) loop
         XTN.Set_Last_Node (L, Last);
         Last := XTN.Next_Node (Last);
      end loop;
   end Append_Node_To_List;

   -----------------------
   -- Insert_After_Node --
   -----------------------

   procedure Insert_After_Node (E : Node_Id; N : Node_Id) is
      Next : constant Node_Id := XTN.Next_Node (N);
   begin
      XTN.Set_Next_Node (N, E);
      XTN.Set_Next_Node (E, Next);
   end Insert_After_Node;

   ------------------------
   -- Insert_Before_Node --
   ------------------------

   procedure Insert_Before_Node (E : Node_Id; N : Node_Id; L : List_Id) is
      Entity : Node_Id;
   begin
      Entity := XTN.First_Node (L);
      if Entity = N then
         XTN.Set_Next_Node (E, Entity);
         XTN.Set_First_Node (L, E);
      else
         while Present (Entity) loop
            exit when XTN.Next_Node (Entity) = N;
            Entity := XTN.Next_Node (Entity);
         end loop;

         Insert_After_Node (E, Entity);
      end if;
   end Insert_Before_Node;

   ---------------
   -- Copy_Node --
   ---------------

   function Copy_Node (N : Node_Id) return Node_Id is
      C : Node_Id;
   begin
      case XTN.Kind (N) is
         when K_Defining_Identifier =>
            C := New_Node (K_Defining_Identifier);
            XTN.Set_Name (C, XTN.Name (N));
            XTN.Set_Corresponding_Node (C, XTN.Corresponding_Node (N));

         when K_Literal =>
            C := New_Node (K_Literal);
            XTN.Set_Value (C, XTN.Value (N));

         when others =>
            raise Program_Error;
      end case;
      return C;
   end Copy_Node;

   ---------------------
   -- Message_Comment --
   ---------------------

   function Message_Comment (M : Name_Id) return Node_Id is
      C : Node_Id;
   begin
yoogx's avatar
yoogx committed
204
      C := Make_Python_Comment (M);
205
206
207
208
209
210
211
212
213
214
215
      return C;
   end Message_Comment;

   ---------------------
   -- Message_Comment --
   ---------------------

   function Message_Comment (M : String) return Node_Id is
      C : Node_Id;
   begin
      Set_Str_To_Name_Buffer (M);
yoogx's avatar
yoogx committed
216
      C := Make_Python_Comment (Name_Find);
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
      return C;
   end Message_Comment;

   -----------
   -- Image --
   -----------

   function Image (T : Token_Type) return String is
      S : String := Token_Type'Image (T);
   begin
      To_Lower (S);
      return S (5 .. S'Last);
   end Image;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
   begin
      --  Initialize Nutils only once

      if Initialized then
         return;
      end if;

      Initialized := True;

      --  Keywords.
      for I in Keyword_Type loop
         New_Token (I);
      end loop;

      --  Graphic Characters
      New_Token (Tok_Slash, "/");
      New_Token (Tok_Less, "<");
      New_Token (Tok_Equal, "=");
      New_Token (Tok_Greater, ">");

   end Initialize;

   --------------
   -- Is_Empty --
   --------------

   function Is_Empty (L : List_Id) return Boolean is
   begin
      return L = No_List or else No (XTN.First_Node (L));
   end Is_Empty;

   ------------
   -- Length --
   ------------

   function Length (L : List_Id) return Natural is
      N : Node_Id;
      C : Natural := 0;
   begin
      if not Is_Empty (L) then
         N := XTN.First_Node (L);

         while Present (N) loop
            C := C + 1;
            N := XTN.Next_Node (N);
         end loop;
      end if;

      return C;
   end Length;

   --------------------
   -- Make_C_Comment --
   --------------------

yoogx's avatar
yoogx committed
291
   function Make_Python_Comment (N : Name_Id) return Node_Id is
292
293
      C : Node_Id;
   begin
yoogx's avatar
yoogx committed
294
      C := New_Node (K_Python_Comment);
295
296
297
      Set_Defining_Identifier (C, New_Node (K_Defining_Identifier));
      XTN.Set_Name (Defining_Identifier (C), N);
      return C;
yoogx's avatar
yoogx committed
298
   end Make_Python_Comment;
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

   ------------------------------
   -- Make_Defining_Identifier --
   ------------------------------

   function Make_Defining_Identifier (Name : Name_Id) return Node_Id is
      N : Node_Id;

   begin
      N := New_Node (K_Defining_Identifier);
      XTN.Set_Name (N, Name);
      return N;
   end Make_Defining_Identifier;

   ------------------
   -- Make_List_Id --
   ------------------

   function Make_List_Id
     (N1 : Node_Id;
      N2 : Node_Id := No_Node;
      N3 : Node_Id := No_Node) return List_Id
   is
      L : List_Id;
   begin
      L := New_List (K_List_Id);
      Append_Node_To_List (N1, L);
      if Present (N2) then
         Append_Node_To_List (N2, L);

         if Present (N3) then
            Append_Node_To_List (N3, L);
         end if;
      end if;
      return L;
   end Make_List_Id;

   -----------------
   -- Next_N_Node --
   -----------------

   function Next_N_Node (N : Node_Id; Num : Natural) return Node_Id is
      Result : Node_Id := N;
   begin
      for I in 1 .. Num loop
         Result := XTN.Next_Node (Result);
      end loop;

      return Result;
   end Next_N_Node;

   --------------
   -- New_List --
   --------------

   function New_List
     (Kind : XTN.Node_Kind;
      From : Node_Id := No_Node) return List_Id
   is
      N : Node_Id;

   begin
      XTN.Entries.Increment_Last;
      N                     := XTN.Entries.Last;
      XTN.Entries.Table (N) := XTN.Default_Node;
      Set_Kind (N, Kind);
      if Present (From) then
         XTN.Set_Loc (N, XTN.Loc (From));
      else
         XTN.Set_Loc (N, No_Location);
      end if;
      return List_Id (N);
   end New_List;

   --------------
   -- New_Node --
   --------------

   function New_Node
     (Kind : XTN.Node_Kind;
      From : Node_Id := No_Node) return Node_Id
   is
      N : Node_Id;
   begin
      XTN.Entries.Increment_Last;
      N                     := XTN.Entries.Last;
      XTN.Entries.Table (N) := XTN.Default_Node;
      XTN.Set_Kind (N, Kind);

      if Present (From) then
         XTN.Set_Loc (N, ATN.Loc (From));
      else
         XTN.Set_Loc (N, No_Location);
      end if;

      return N;
   end New_Node;

   ---------------
   -- New_Token --
   ---------------

   procedure New_Token (T : Token_Type; I : String := "") is
      Name : Name_Id;
   begin
      if T in Keyword_Type then
         --  Marking the token image as a keyword for fas searching
         --  purpose, we add the prefix to avoir collision with other
         --  languages keywords

         Set_Str_To_Name_Buffer (Image (T));
         Name := Name_Find;
         Name := Add_Suffix_To_Name (Keyword_Suffix, Name);
         Set_Name_Table_Byte
           (Name,
            Ocarina.Types.Byte (Token_Type'Pos (T) + 1));

         Set_Str_To_Name_Buffer (Image (T));
      else
         Set_Str_To_Name_Buffer (I);
      end if;
      Token_Image (T) := Name_Find;
   end New_Token;

   ----------------
   -- Pop_Entity --
   ----------------

   procedure Pop_Entity is
   begin
      if Last > No_Depth then
         Decrement_Last;
      end if;
   end Pop_Entity;

   -----------------
   -- Push_Entity --
   -----------------

   procedure Push_Entity (E : Node_Id) is
   begin
      Increment_Last;
      Table (Last).Current_Entity := E;
   end Push_Entity;

   ---------------------------
   -- Remove_Node_From_List --
   ---------------------------

   procedure Remove_Node_From_List (E : Node_Id; L : List_Id) is
      C : Node_Id;

   begin
      C := XTN.First_Node (L);
      if C = E then
         XTN.Set_First_Node (L, XTN.Next_Node (E));
         if XTN.Last_Node (L) = E then
            XTN.Set_Last_Node (L, No_Node);
         end if;
      else
         while Present (C) loop
            if XTN.Next_Node (C) = E then
               XTN.Set_Next_Node (C, XTN.Next_Node (E));
               if XTN.Last_Node (L) = E then
                  XTN.Set_Last_Node (L, C);
               end if;
               exit;
            end if;
            C := XTN.Next_Node (C);
         end loop;
      end if;
   end Remove_Node_From_List;

   ---------------
   -- To_C_Name --
   ---------------

yoogx's avatar
yoogx committed
476
   function To_Python_Name (N : Name_Id) return Name_Id is
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
      First     : Natural := 1;
      Name      : Name_Id;
      Test_Name : Name_Id;
      V         : Ocarina.Types.Byte;
   begin
      Get_Name_String (Normalize_Name (N));
      while First <= Name_Len and then Name_Buffer (First) = '_' loop
         First := First + 1;
      end loop;

      for I in First .. Name_Len loop
         if Name_Buffer (I) = '_'
           and then I < Name_Len
           and then Name_Buffer (I + 1) = '_'
         then
            Name_Buffer (I + 1) := 'U';
         end if;
      end loop;

      if Name_Buffer (Name_Len) = '_' then
         Add_Char_To_Name_Buffer ('U');
      end if;
      Name := Name_Find;

      --  If the identifier collides with an Ada reserved word insert
      --  "AADL_" string before the identifier.

      Test_Name := Add_Suffix_To_Name (Keyword_Suffix, Name);
      V         := Get_Name_Table_Byte (Test_Name);
      if V > 0 then
         Set_Str_To_Name_Buffer ("AADL_");
         Get_Name_String_And_Append (Name);
         Name := Name_Find;
      end if;

      return To_Lower (Name);
yoogx's avatar
yoogx committed
513
   end To_Python_Name;
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556

   ----------------------------
   -- Conventional_Base_Name --
   ----------------------------

   function Conventional_Base_Name (N : Name_Id) return Name_Id is
   begin
      Get_Name_String (N);

      for Index in 1 .. Name_Len loop
         Name_Buffer (Index) := To_Lower (Name_Buffer (Index));
      end loop;

      return Name_Find;
   end Conventional_Base_Name;

   --------------------
   -- Current_Entity --
   --------------------

   function Current_Entity return Node_Id is
   begin
      if Last = No_Depth then
         return No_Node;
      else
         return Table (Last).Current_Entity;
      end if;
   end Current_Entity;

   ------------------
   -- Current_File --
   ------------------

   function Current_File return Node_Id is
   begin
      if Last = No_Depth then
         return No_Node;
      else
         return Table (Last).Current_File;
      end if;
   end Current_File;

   -------------------
yoogx's avatar
yoogx committed
557
   -- Make_Python_File --
558
559
   -------------------

yoogx's avatar
yoogx committed
560
   function Make_Python_File
561
562
563
564
     (Identifier : Node_Id;
      DTD        : Node_Id := No_Node) return Node_Id
   is
      File            : Node_Id;
yoogx's avatar
yoogx committed
565
      The_Python_Node : Node_Id;
566
567

   begin
yoogx's avatar
yoogx committed
568
569
      File            := New_Node (K_Python_File);
      The_Python_Node := New_Node (XTN.K_Python_Node);
570
571
572
573
574

      Set_Defining_Identifier (File, Identifier);
      Set_Corresponding_Node (Identifier, File);

      XTN.Set_Is_HTML (File, False);
yoogx's avatar
yoogx committed
575
      XTN.Set_Root_Node (File, The_Python_Node);
576
      if Present (DTD) then
yoogx's avatar
yoogx committed
577
         XTN.Set_Python_DTD (File, DTD);
578
579
580
      end if;

      return File;
yoogx's avatar
yoogx committed
581
   end Make_Python_File;
582
583
584
585
586
587
588
589
590
591
592
593
594
595

   ------------------
   -- Make_Literal --
   ------------------

   function Make_Literal (Value : Value_Id) return Node_Id is
      N : Node_Id;
   begin
      N := New_Node (K_Literal);
      XTN.Set_Value (N, Value);
      return N;
   end Make_Literal;

   -------------------
yoogx's avatar
yoogx committed
596
   -- Make_Python_Node --
597
598
   -------------------

yoogx's avatar
yoogx committed
599
   function Make_Python_Node
600
601
     (Name_String : String               := "";
      Name_Nameid : Name_Id              := No_Name;
yoogx's avatar
yoogx committed
602
      Kind        : Python_New_Node_Kind := K_String) return Node_Id
603
604
605
606
607
   is
      N : Node_Id;
      L : List_Id;
      K : List_Id;
   begin
yoogx's avatar
yoogx committed
608
      N := New_Node (K_Python_Node);
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

      if Kind = K_String then
         Set_Str_To_Name_Buffer (Name_String);
         XTN.Set_Defining_Identifier (N, Make_Defining_Identifier (Name_Find));
      else
         XTN.Set_Defining_Identifier
           (N,
            Make_Defining_Identifier (Name_Nameid));
      end if;

      L := New_List (XTN.K_List_Id);
      XTN.Set_Items (N, L);

      K := New_List (XTN.K_List_Id);
      XTN.Set_Subitems (N, K);

      return N;
yoogx's avatar
yoogx committed
626
   end Make_Python_Node;
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675

   ----------------------
   -- Make_Assignement --
   ----------------------

   function Make_Assignement
     (Left  : Node_Id;
      Right : Node_Id) return Node_Id
   is
      N : Node_Id;
   begin
      N := New_Node (XTN.K_Assignement);
      XTN.Set_Left_Expression (N, Left);
      XTN.Set_Right_Expression (N, Right);
      return N;
   end Make_Assignement;

   --------------------
   -- Make_Container --
   --------------------

   function Make_Container (Content : Node_Id) return Node_Id is
      N : Node_Id;
   begin
      N := New_Node (XTN.K_Container);
      XTN.Set_Content (N, Content);
      return N;
   end Make_Container;

   -------------------
   -- Add_Attribute --
   -------------------

   procedure Add_Attribute (Key : String; Value : String; N : Node_Id) is
      R : Node_Id;
      Q : Node_Id;
   begin
      Set_Str_To_Name_Buffer (Key);
      R := Make_Defining_Identifier (Name_Find);
      Set_Str_To_Name_Buffer (Value);
      Q := Make_Defining_Identifier (Name_Find);
      Append_Node_To_List (Make_Assignement (R, Q), XTN.Items (N));
   end Add_Attribute;

   procedure Add_Attribute (Key : String; Value : Value_Id; N : Node_Id) is
   begin
      Add_Attribute (Key, Image (Value), N);
   end Add_Attribute;

yoogx's avatar
yoogx committed
676
end Ocarina.Backends.Python.Nutils;