taste-backend-skeletons.adb 5.53 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
with Text_IO; use Text_IO;
2
with Ada.Strings.Unbounded,
3
     Ada.Characters.Handling,
Maxime Perrotin's avatar
Maxime Perrotin committed
4
     Ada.Directories;
5

6
use Ada.Characters.Handling,
Maxime Perrotin's avatar
Maxime Perrotin committed
7
    Ada.Directories;
Maxime Perrotin's avatar
Maxime Perrotin committed
8
9
10

package body TASTE.Backend.Skeletons is
   procedure Generate (Model : TASTE_Model) is
Maxime Perrotin's avatar
Maxime Perrotin committed
11
      Template : constant IV_As_Template :=
Maxime Perrotin's avatar
Maxime Perrotin committed
12
        Interface_View_Template (Model.Interface_View);
13
14
15
      Prefix : constant String := Model.Configuration.Binary_Path.all
        & "templates/skeletons/";
      use Ada.Strings.Unbounded;
Maxime Perrotin's avatar
Maxime Perrotin committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
      type Output is (Header, Code);
      function Process_Interfaces (Interfaces : Interface_Vectors.Vector;
                                   Path       : String;
                                   Target     : Output) return Tag
      is
         Interfaces_Tag : Tag;
         Tmplt_Param : constant String :=
           Path & "interface-" & (if Target = Header then "header" else "body")
           & "-parameter.tmplt";
         Tmplt_Sign  : constant String := Path & "interface-signature.tmplt";
         Proceed : constant Boolean := Exists (Path)
           and then Kind (Path) = Directory
           and then Exists (Tmplt_Param) and then Exists (Tmplt_Sign);
      begin
         if not Proceed then
            return Interfaces_Tag;
         end if;
         for Each of Interfaces loop
            declare
               Pool   : Translate_Set := Each.Header;
               Params : Tag;
            begin
               for Param of Each.Params loop
39
                  declare
Maxime Perrotin's avatar
Maxime Perrotin committed
40
                     P : constant String := Parse (Tmplt_Param, Param);
41
                  begin
Maxime Perrotin's avatar
Maxime Perrotin committed
42
                     Params := Params & P;
43
                  end;
Maxime Perrotin's avatar
Maxime Perrotin committed
44
45
               end loop;
               Pool := Pool & Assoc ("Parameters", Params);
Maxime Perrotin's avatar
Maxime Perrotin committed
46
               declare
Maxime Perrotin's avatar
Maxime Perrotin committed
47
                  New_Interface : constant String := Parse (Tmplt_Sign, Pool);
Maxime Perrotin's avatar
Maxime Perrotin committed
48
               begin
Maxime Perrotin's avatar
Maxime Perrotin committed
49
                  Interfaces_Tag := Interfaces_Tag & New_Interface;
Maxime Perrotin's avatar
Maxime Perrotin committed
50
               end;
Maxime Perrotin's avatar
Maxime Perrotin committed
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
            end;
         end loop;
         return Interfaces_Tag;
      end Process_Interfaces;
   begin
      Put_Line ("=== Generate skeletons ===");
      for Each of Model.Interface_View.Flat_Functions loop
         declare
            Language   : constant String := Language_Spelling (Each);
            Path       : constant String := Prefix & To_Lower (Language) & "/";
            Hdr_Tmpl   : constant Translate_Set := +Assoc ("Name", Each.Name);
            Func_Tmpl  : constant Func_As_Template :=
              Template.Funcs.Element (To_String (Each.Name));
            PIs_Header : constant Tag :=
              Process_Interfaces (Func_Tmpl.Provided, Path, Header);
            RIs_Header : constant Tag :=
              Process_Interfaces (Func_Tmpl.Required, Path, Header);
            PIs_Body   : constant Tag :=
              Process_Interfaces (Func_Tmpl.Provided, Path, Code);
            RIs_Body   : constant Tag :=
              Process_Interfaces (Func_Tmpl.Required, Path, Code);
            Func_Hdr   : constant Translate_Set := Func_Tmpl.Header
              & Assoc ("Provided_Interfaces", PIs_Header)
              & Assoc ("Required_Interfaces", RIs_Header);
            Func_Body  : constant Translate_Set := Func_Tmpl.Header
              & Assoc ("Provided_Interfaces", PIs_Body)
              & Assoc ("Required_Interfaces", RIs_Body);
         begin
            if Size (PIs_Header) /= 0 or Size (RIs_Header) /= 0 then
               Put ("***  Generating ");
               Put_Line (Parse (Path & "header-filename.tmplt", Hdr_Tmpl));
               Put_Line (Parse (Path & "header.tmplt", Func_Hdr));
               Put ("***  Generating ");
               Put_Line (Parse (Path & "body-filename.tmplt", Hdr_Tmpl));
               Put_Line (Parse (Path & "body.tmplt", Func_Body));
            else
               Put_Line ("No skeletons for language " & Language & " !");
            end if;
89
90
         end;
      end loop;
Maxime Perrotin's avatar
Maxime Perrotin committed
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
   end Generate;

   function Parameter_Template (Param : ASN1_Parameter) return Translate_Set is
     (+Assoc ("Type", Param.Sort) & Assoc ("Name", Param.Name)
     & Assoc ("Direction", Param.Direction'Img));

   function Interface_Template (TI : Taste_Interface)
                                return Interface_As_Template
   is
      use Template_Vectors;
      Result : Interface_As_Template;
   begin
      Result.Header :=  +Assoc ("Name",             TI.Name)
                        & Assoc ("Parent_Function", TI.Parent_Function);
      for Each of TI.Params loop
         Result.Params := Result.Params & Parameter_Template (Each);
      end loop;
      return Result;
   end Interface_Template;

   function Func_Template (F : Taste_Terminal_Function) return Func_As_Template
   is
      use Interface_Vectors;
      Result : Func_As_Template;
   begin
      Result.Header := +Assoc ("Name", F.Name)
                       & Assoc ("Language", F.Language'Img);
      for Each of F.Provided loop
         Result.Provided := Result.Provided & Interface_Template (Each);
      end loop;
      for Each of F.Required loop
         Result.Required := Result.Required & Interface_Template (Each);
      end loop;
      return Result;
   end Func_Template;

   function Interface_View_Template (IV : Complete_Interface_View)
                                     return IV_As_Template is
129
130
      use Func_Maps;
      use Ada.Strings.Unbounded;
Maxime Perrotin's avatar
Maxime Perrotin committed
131
132
133
      Result : IV_As_Template;
   begin
      for Each of IV.Flat_Functions loop
134
135
         Result.Funcs.Insert (Key      => To_String (Each.Name),
                              New_Item => Func_Template (Each));
Maxime Perrotin's avatar
Maxime Perrotin committed
136
137
138
139
140
      end loop;
      return Result;
   end Interface_View_Template;

end TASTe.Backend.Skeletons;