buildsupport_utils.adb 27.4 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
--  *************************** buildsupport ****************************  --
Maxime Perrotin's avatar
Maxime Perrotin committed
2
--  (c) 2008-2017 European Space Agency - maxime.perrotin@esa.int
Maxime Perrotin's avatar
Maxime Perrotin committed
3 4
--  LGPL license, see LICENSE file

5 6 7 8 9 10 11 12
with Ada.Text_IO,
     GNAT.OS_Lib,
     Buildsupport_Version,
     Ocarina.Configuration,
     Ocarina.AADL_Values,
     Ocarina.Instances.Queries,
     Ocarina.ME_AADL.AADL_Instances.Nutils,
     Ocarina.ME_AADL.AADL_Instances.Entities,
13
     Ocarina.Backends.Utils,
14
     Ada.Characters.Latin_1;
Maxime Perrotin's avatar
Maxime Perrotin committed
15 16 17

package body Buildsupport_Utils is

18 19 20 21 22 23
   use Ada.Text_IO,
       GNAT.OS_Lib,
       Ocarina.Instances.Queries,
       Ocarina.ME_AADL.AADL_Instances.Nutils,
       Ada.Characters.Latin_1,
       Ocarina.ME_AADL.AADL_Instances.Entities,
24 25
       Ocarina.ME_AADL,
       Ocarina.Backends.Utils;
Maxime Perrotin's avatar
Maxime Perrotin committed
26 27 28 29 30 31 32

   ------------
   -- Banner --
   ------------

   procedure Banner is
      The_Banner : constant String :=
Maxime Perrotin's avatar
Maxime Perrotin committed
33 34 35 36
        "TASTE Buildsupport (Version "
        & Buildsupport_Version.Buildsupport_Release & ") "
        & ASCII.LF & ASCII.CR
        & "Contact: Maxime.Perrotin@esa.int or Thanassis.Tsiodras@esa.int"
Maxime Perrotin's avatar
Maxime Perrotin committed
37
        & ASCII.LF & ASCII.CR
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
38
        & "Based on Ocarina: " & Ocarina.Configuration.Ocarina_Version;
Maxime Perrotin's avatar
Maxime Perrotin committed
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
   begin
      Put_Line (The_Banner);
   end Banner;

   -----------
   -- Usage --
   -----------

   procedure Usage is
   begin
      Put_Line
        ("Usage: buildsupport <options> otherfiles");
      Put_Line
        ("Where <options> are:");
      New_Line;
      Put ("-l, --glue" & HT & HT & HT & HT);
      Put_Line ("Generate glue code");
      Put ("-w, --gw" & HT & HT & HT & HT);
      Put_Line ("Generate code skeletons");
      Put ("-j, --keep-case" & HT & HT & HT & HT);
      Put_Line ("Respect the case for interface names");
      Put ("-o, --output <outputDir>" & HT & HT);
      Put_Line ("Root directory for the output files");
      Put ("-i, --interfaceview <i_view.aadl>" & HT);
      Put_Line ("The interface view in AADL");
      Put ("-c, --deploymentview <d_view.aadl>" & HT);
      Put_Line ("The deployment view in AADL");
      Put ("-d, --dataview <dataview.aadl>" & HT & HT);
      Put_Line ("The data view in AADL");
      Put ("-t, --test" & HT & HT & HT & HT);
      Put_Line ("Dump model information");
      Put ("-g, --debug" & HT & HT & HT & HT);
      Put_Line ("Generate runtime debug output");
      Put ("-s, --stack <stack-value>" & HT & HT);
73 74 75
      Put_Line ("Set the size of the stack per thread in kbytes (default 50)");
      Put ("-x, --timer <timer-resolution in ms>" & HT);
      Put_Line ("Set the timer resolution (default 100 ms)");
Maxime Perrotin's avatar
Maxime Perrotin committed
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
      Put ("-v, --version" & HT & HT & HT & HT);
      Put_Line ("Display buildsupport version number");
      Put ("-p, --polyorb-hi-c" & HT & HT & HT);
      Put_Line ("Interface glue code with PolyORB-HI-C");
      Put ("otherfiles" & HT & HT & HT & HT);
      Put_Line ("Any other aadl file you want to parse");
      New_Line;
      New_Line;
      Put_Line ("For example, this command will generate your application"
       & " skeletons:");
      New_Line;
      Put_Line ("buildsupport -i InterfaceView.aadl -d DataView.aadl"
       & " -o code --gw --keep-case");
      New_Line;

   end Usage;

   -------------------
   -- Exit_On_Error --
   -------------------

   procedure Exit_On_Error (Error : Boolean; Reason : String) is
   begin
      if Error then
         Put_Line (Reason);
         OS_Exit (1);
      end if;
   end Exit_On_Error;

   ----------------------------
   -- Get_RCM_Operation_Kind --
   ----------------------------

   function Get_RCM_Operation_Kind
     (E : Node_Id) return Supported_RCM_Operation_Kind
   is
      RCM_Operation_Kind_N : Name_Id;
Maxime Perrotin's avatar
Maxime Perrotin committed
113 114 115 116 117 118
      RCM_Operation_Kind : constant Name_Id :=
          Get_String_Name ("taste::rcmoperationkind");
      Unprotected_Name   : constant Name_Id := Get_String_Name ("unprotected");
      Protected_Name     : constant Name_Id := Get_String_Name ("protected");
      Cyclic_Name        : constant Name_Id := Get_String_Name ("cyclic");
      Sporadic_Name      : constant Name_Id := Get_String_Name ("sporadic");
119
      Any_Name           : constant Name_Id := Get_String_Name ("any");
Maxime Perrotin's avatar
Maxime Perrotin committed
120 121
   begin
      if Is_Defined_Enumeration_Property (E, RCM_Operation_Kind) then
122 123
         RCM_Operation_Kind_N :=
            Get_Enumeration_Property (E, RCM_Operation_Kind);
Maxime Perrotin's avatar
Maxime Perrotin committed
124 125 126 127 128 129 130 131 132 133 134 135

         if RCM_Operation_Kind_N = Unprotected_Name then
            return Unprotected_Operation;

         elsif RCM_Operation_Kind_N =  Protected_Name then
            return Protected_Operation;

         elsif RCM_Operation_Kind_N =  Cyclic_Name then
            return Cyclic_Operation;

         elsif RCM_Operation_Kind_N =  Sporadic_Name then
            return Sporadic_Operation;
136 137 138

         elsif RCM_Operation_Kind_N =  Any_Name then
            return Any_Operation;
Maxime Perrotin's avatar
Maxime Perrotin committed
139 140
         end if;
      end if;
141 142
      Exit_On_Error (True, "Could not determine interface kind: "
                        & Get_Name_String (RCM_Operation_Kind_N));
Maxime Perrotin's avatar
Maxime Perrotin committed
143
      return Sporadic_Operation;
Maxime Perrotin's avatar
Maxime Perrotin committed
144 145 146 147 148 149 150
   end Get_RCM_Operation_Kind;

   -----------------------
   -- Get_RCM_Operation --
   -----------------------

   function Get_RCM_Operation (E : Node_Id) return Node_Id is
Maxime Perrotin's avatar
Maxime Perrotin committed
151 152
      RCM_Operation : constant Name_Id :=
          Get_String_Name ("taste::rcmoperation");
Maxime Perrotin's avatar
Maxime Perrotin committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
   begin
      if Is_Subprogram_Access (E) then
         return Corresponding_Instance (E);
      else
         if Is_Defined_Property (E, RCM_Operation) then
            return Get_Classifier_Property (E, RCM_Operation);
         else
            return No_Node;
         end if;
      end if;
   end Get_RCM_Operation;

   -----------------------
   -- Get_APLC_Binding --
   -----------------------

   function Get_APLC_Binding (E : Node_Id) return List_Id is
Maxime Perrotin's avatar
Maxime Perrotin committed
170 171
      APLC_Binding : constant Name_Id :=
          Get_String_Name ("taste::aplc_binding");
Maxime Perrotin's avatar
Maxime Perrotin committed
172 173 174 175 176 177 178 179 180 181 182 183 184
   begin
      if Is_Defined_Property (E, APLC_Binding) then
         return Get_List_Property (E, APLC_Binding);
      else
         return No_List;
      end if;
   end Get_APLC_Binding;

   --------------------
   -- Get_RCM_Period --
   --------------------

   function Get_RCM_Period (D : Node_Id) return Unsigned_Long_Long is
Maxime Perrotin's avatar
Maxime Perrotin committed
185
      RCM_Period : constant Name_Id := Get_String_Name ("taste::rcmperiod");
Maxime Perrotin's avatar
Maxime Perrotin committed
186 187 188 189 190 191 192 193 194 195 196 197 198
   begin
      if Is_Defined_Integer_Property (D, RCM_Period) then
         return Get_Integer_Property (D, RCM_Period);
      else
         return 0;
      end if;
   end Get_RCM_Period;

   --------------------------
   -- Get_Ada_Package_Name --
   --------------------------

   function Get_Ada_Package_Name (D : Node_Id) return Name_Id is
Maxime Perrotin's avatar
Maxime Perrotin committed
199 200
      Ada_Package_Name : constant Name_id :=
         Get_String_Name ("taste::ada_package_name");
Maxime Perrotin's avatar
Maxime Perrotin committed
201 202 203 204 205 206 207 208 209
   begin
      return Get_String_Property (D, Ada_Package_Name);
   end Get_Ada_Package_Name;

   -------------------------------
   -- Get_Ellidiss_Tool_Version --
   -------------------------------

   function Get_Ellidiss_Tool_Version (D : Node_Id) return Name_Id is
Maxime Perrotin's avatar
Maxime Perrotin committed
210 211
      Ellidiss_Tool_Version : constant Name_id :=
         Get_String_Name ("taste::version");
Maxime Perrotin's avatar
Maxime Perrotin committed
212 213 214 215
   begin
      return Get_String_Property (D, Ellidiss_Tool_Version);
   end Get_Ellidiss_Tool_Version;

216 217 218 219 220 221 222
   -------------------------------
   -- Get_Instance_Of --
   -------------------------------

   function Get_Instance_Of (Device : Node_Id) return Name_Id is
      Instance_Of : Name_id := No_Name;
   begin
223
      if Is_Defined_Property (Device, "taste_iv_properties::is_instance_of")
224 225 226
      then
         Instance_Of := Get_String_Property
             (Device, Get_String_Name
227
               ("taste_iv_properties::is_instance_of"));
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
      end if;
      return Instance_Of;
   end Get_Instance_Of;

   -------------------------------
   -- Get_Is_Component_Type --
   -------------------------------

   function Get_Is_Component_Type (Device : Node_Id) return Boolean is
      Is_Component_Type  : Boolean := False;
   begin
      if Is_Defined_Property (Device, "taste_iv_properties::is_component_type")
      then
         Is_Component_Type := Get_Boolean_Property
            (Device, Get_String_Name
               ("taste_iv_properties::is_component_type"));
      end if;
      return Is_Component_Type;
   end Get_Is_Component_Type;

Maxime Perrotin's avatar
Maxime Perrotin committed
248 249 250 251 252
   ------------------------
   -- Get_Interface_Name --
   ------------------------

   function Get_Interface_Name (D : Node_Id) return Name_Id is
Maxime Perrotin's avatar
Maxime Perrotin committed
253 254
      Interface_Name : constant Name_id :=
         Get_String_Name ("taste::interfacename");
Maxime Perrotin's avatar
Maxime Perrotin committed
255 256 257 258
   begin
      return Get_String_Property (D, Interface_Name);
   end Get_Interface_Name;

259 260 261 262 263
   ------------------
   -- Get_Env_Vars --
   ------------------

   function Get_Env_Vars (D : Node_Id) return Name_Id is
264
      Env_Vars : constant Name_id :=
265 266
         Get_String_Name ("envvars");
   begin
267
      return Get_String_Property (D, Env_Vars);
268 269
   end Get_Env_Vars;

Maxime Perrotin's avatar
Maxime Perrotin committed
270 271 272 273 274 275
   ---------------------------
   -- Get ASN.1 Module name --
   ---------------------------

   function Get_ASN1_Module_Name (D : Node_Id) return String is
      id : Name_Id := No_Name;
Maxime Perrotin's avatar
Maxime Perrotin committed
276 277
      ASN1_Module : constant Name_id :=
         Get_String_Name ("deployment::asn1_module_name");
Maxime Perrotin's avatar
Maxime Perrotin committed
278 279 280 281 282 283 284 285 286
   begin
      if Is_Defined_String_Property (D, ASN1_Module) then
         id := Get_String_Property (D, ASN1_Module);
         return Get_Name_String (id);
      else
         return Get_Name_String (Get_String_Name ("nomodule"));
      end if;
   end Get_ASN1_Module_Name;

Maxime Perrotin's avatar
Maxime Perrotin committed
287 288 289 290 291
   --------------------------------------------
   -- Get all properties as a Map Key/String --
   -- Input parameter is an AADL instance    --
   --------------------------------------------
   function Get_Properties_Map (D : Node_Id) return Property_Maps.Map is
Maxime Perrotin's avatar
Maxime Perrotin committed
292
      properties : constant List_Id  := AIN.Properties (D);
Maxime Perrotin's avatar
Maxime Perrotin committed
293
      result     : Property_Maps.Map := Property_Maps.Empty_Map;
Maxime Perrotin's avatar
Maxime Perrotin committed
294
      property   : Node_Id           := AIN.First_Node (properties);
295 296
      prop_value : Node_Id;
      single_val : Node_Id;
Maxime Perrotin's avatar
Maxime Perrotin committed
297 298
   begin
      while Present (property) loop
299 300 301 302
         prop_value := AIN.Property_Association_Value (property);
         if Present (ATN.Single_Value (prop_value)) then
            --  Only support single-value properties for now
            single_val := ATN.Single_Value (prop_value);
Maxime Perrotin's avatar
Maxime Perrotin committed
303
            result.Insert (Key => AIN_Case (property),
Maxime Perrotin's avatar
Maxime Perrotin committed
304
                        New_Item =>
305 306 307 308 309
              (case ATN.Kind (single_val) is
                 when ATN.K_Signed_AADLNumber =>
                   Ocarina.AADL_Values.Image
                      (ATN.Value (ATN.Number_Value (single_val))) &
                      (if Present (ATN.Unit_Identifier (single_val)) then " " &
Maxime Perrotin's avatar
Maxime Perrotin committed
310
                      Get_Name_String
311
                          (ATN.Display_Name (ATN.Unit_Identifier (single_val)))
Maxime Perrotin's avatar
Maxime Perrotin committed
312
                      else ""),
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
                 when ATN.K_Literal =>
                    Ocarina.AADL_Values.Image (ATN.Value (single_val),
                                               Quoted => False),
                 when ATN.K_Reference_Term =>
                    Get_Name_String
                       (ATN.Display_Name (ATN.First_Node --  XXX must iterate
                          (ATN.List_Items (ATN.Reference_Term (single_val))))),
                 when ATN.K_Enumeration_Term =>
                    Get_Name_String
                       (ATN.Display_Name (ATN.Identifier (single_val))),
                 when ATN.K_Number_Range_Term =>
                    "RANGE NOT SUPPORTED!",
                 when others => "ERROR! Unsupported kind: "
                                & ATN.Kind (single_val)'Img));
         end if;
Maxime Perrotin's avatar
Maxime Perrotin committed
328
         property := AIN.Next_Node (property);
Maxime Perrotin's avatar
Maxime Perrotin committed
329 330 331 332
      end loop;
      return result;
   end Get_Properties_Map;

Maxime Perrotin's avatar
Maxime Perrotin committed
333 334 335 336 337 338
   -----------------------
   -- Get_ASN1_Encoding --
   -----------------------

   function Get_ASN1_Encoding (E : Node_Id) return Supported_ASN1_Encoding is
      ASN1_Encoding_N : Name_Id;
Maxime Perrotin's avatar
Maxime Perrotin committed
339 340 341 342
      ASN1_Encoding : constant Name_Id := Get_String_Name ("taste::encoding");
      Native_Name   : constant Name_Id := Get_String_Name ("native");
      UPER_Name     : constant Name_Id := Get_String_Name ("uper");
      ACN_Name      : constant Name_Id := Get_String_Name ("acn");
Maxime Perrotin's avatar
Maxime Perrotin committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
   begin
      if Is_Defined_Enumeration_Property (E, ASN1_Encoding) then
         ASN1_Encoding_N := Get_Enumeration_Property (E, ASN1_Encoding);

         if ASN1_Encoding_N = Native_Name then
            return Native;

         elsif ASN1_Encoding_N = UPER_Name then
            return UPER;

         elsif ASN1_Encoding_N = ACN_Name then
            return ACN;
         end if;
      end if;
      Exit_On_Error (True, "ASN1 Encoding not set");
      return Default;
   end Get_ASN1_Encoding;

   -------------------------
   -- Get_ASN1_Basic_Type --
   -------------------------

Maxime Perrotin's avatar
Maxime Perrotin committed
365
   function Get_ASN1_Basic_Type (E : Node_Id) return Supported_ASN1_Basic_Type
Maxime Perrotin's avatar
Maxime Perrotin committed
366
   is
Maxime Perrotin's avatar
Maxime Perrotin committed
367 368 369 370 371 372 373 374 375 376 377 378 379
      ASN1_Basic_Type  : constant Name_Id :=
                               Get_String_Name ("taste::asn1_basic_type");
      Sequence_Name    : constant Name_Id := Get_String_Name ("asequence");
      SequenceOf_Name  : constant Name_Id := Get_String_Name ("asequenceof");
      Enumerated_Name  : constant Name_Id := Get_String_Name ("aenumerated");
      Set_Name         : constant Name_Id := Get_String_Name ("aset");
      SetOf_Name       : constant Name_Id := Get_String_Name ("asetof");
      Integer_Name     : constant Name_Id := Get_String_Name ("ainteger");
      Boolean_Name     : constant Name_Id := Get_String_Name ("aboolean");
      Real_Name        : constant Name_Id := Get_String_Name ("areal");
      OctetString_Name : constant Name_Id := Get_String_Name ("aoctetstring");
      Choice_Name      : constant Name_Id := Get_String_Name ("achoice");
      String_Name      : constant Name_Id := Get_String_Name ("astring");
Maxime Perrotin's avatar
Maxime Perrotin committed
380 381 382 383 384
      ASN1_Basic_Type_N : Name_Id;
   begin
      if Is_Defined_Enumeration_Property (E, ASN1_Basic_Type) then
         ASN1_Basic_Type_N := Get_Enumeration_Property (E, ASN1_Basic_Type);

Maxime Perrotin's avatar
Maxime Perrotin committed
385 386
         if ASN1_Basic_Type_N = Sequence_Name then
            return ASN1_Sequence;
Maxime Perrotin's avatar
Maxime Perrotin committed
387

Maxime Perrotin's avatar
Maxime Perrotin committed
388 389
         elsif ASN1_Basic_Type_N = SequenceOf_Name then
            return ASN1_SequenceOf;
Maxime Perrotin's avatar
Maxime Perrotin committed
390

Maxime Perrotin's avatar
Maxime Perrotin committed
391 392
         elsif ASN1_Basic_Type_N = Enumerated_Name then
            return ASN1_Enumerated;
Maxime Perrotin's avatar
Maxime Perrotin committed
393

Maxime Perrotin's avatar
Maxime Perrotin committed
394 395
         elsif ASN1_Basic_Type_N = Set_Name then
            return ASN1_Set;
Maxime Perrotin's avatar
Maxime Perrotin committed
396

Maxime Perrotin's avatar
Maxime Perrotin committed
397 398
         elsif ASN1_Basic_Type_N = SetOf_Name then
            return ASN1_SetOf;
Maxime Perrotin's avatar
Maxime Perrotin committed
399

Maxime Perrotin's avatar
Maxime Perrotin committed
400 401
         elsif ASN1_Basic_Type_N = Integer_Name then
            return ASN1_Integer;
Maxime Perrotin's avatar
Maxime Perrotin committed
402

Maxime Perrotin's avatar
Maxime Perrotin committed
403 404
         elsif ASN1_Basic_Type_N = Boolean_Name then
            return ASN1_Boolean;
Maxime Perrotin's avatar
Maxime Perrotin committed
405

Maxime Perrotin's avatar
Maxime Perrotin committed
406 407
         elsif ASN1_Basic_Type_N = Real_Name then
            return ASN1_Real;
Maxime Perrotin's avatar
Maxime Perrotin committed
408

Maxime Perrotin's avatar
Maxime Perrotin committed
409 410
         elsif ASN1_Basic_Type_N = OctetString_Name then
            return ASN1_OctetString;
Maxime Perrotin's avatar
Maxime Perrotin committed
411

Maxime Perrotin's avatar
Maxime Perrotin committed
412 413
         elsif ASN1_Basic_Type_N = Choice_Name then
            return ASN1_Choice;
Maxime Perrotin's avatar
Maxime Perrotin committed
414

Maxime Perrotin's avatar
Maxime Perrotin committed
415 416
         elsif ASN1_Basic_Type_N = String_Name then
            return ASN1_String;
Maxime Perrotin's avatar
Maxime Perrotin committed
417

Maxime Perrotin's avatar
Maxime Perrotin committed
418 419 420 421
         else
            raise Program_Error with "Undefined choice "
              & Get_Name_String (ASN1_Basic_Type_N);
         end if;
Maxime Perrotin's avatar
Maxime Perrotin committed
422 423 424 425 426
      end if;
      Exit_On_Error (True, "Error: ASN.1 Basic type undefined!");
      return ASN1_Unknown;
   end Get_ASN1_Basic_Type;

427 428 429 430 431 432 433 434 435 436 437 438 439 440
   ----------------------------------------------------------------
   -- Get Optional Worse Case Execution Time (Upper bound in ms) --
   ----------------------------------------------------------------

   function Get_Upper_WCET (Func : Node_Id) return Optional_Long_Long is
      (if Is_Subprogram_Access (Func) and then Sources (Func) /= No_List
         and then AIN.First_Node (Sources (Func)) /= No_Node
         and then Get_Execution_Time (Corresponding_Instance (AIN.Item
                                           (AIN.First_Node (Sources (Func)))))
                           /= Empty_Time_Array
      then Just (To_Milliseconds (Get_Execution_Time (Corresponding_Instance
                             (AIN.Item (AIN.First_Node (Sources (Func)))))(1)))
         else Nothing);

441 442 443 444
   ---------------------------
   -- AST Builder Functions --
   ---------------------------

Maxime Perrotin's avatar
Maxime Perrotin committed
445
   function AADL_to_Ada_IV (System : Node_Id) return Complete_Interface_View is
446 447
      use type Functions.Vector;
      use type Channels.Vector;
448
      use type Ctxt_Params.Vector;
449
      use type Interfaces.Vector;
450
      use type Parameters.Vector;
Maxime Perrotin's avatar
Maxime Perrotin committed
451
      use type Connection_Maps.Map;
452 453
      Funcs             : Functions.Vector := Functions.Empty_Vector;
      Routes            : Channels.Vector; --  := Channels.Empty_Vector;
Maxime Perrotin's avatar
Maxime Perrotin committed
454
      Routes_Map        : Connection_Maps.Map;
Maxime Perrotin's avatar
Maxime Perrotin committed
455
      Current_Function  : Node_Id;
456 457 458 459 460 461 462 463 464 465 466 467

      --  Parse a connection
      function Parse_Connection (Conn : Node_Id) return Connection is
         Caller  : constant Node_Id := AIN.Item (AIN.First_Node
                                         (AIN.Path (AIN.Destination (Conn))));
         Callee  : constant Node_Id := AIN.Item (AIN.First_Node
                                         (AIN.Path (AIN.Source (Conn))));
         PI_Name : constant Name_Id := Get_Interface_Name
                                   (Get_Referenced_Entity (AIN.Source (Conn)));
         RI_Name : constant Name_Id := Get_Interface_Name
                              (Get_Referenced_Entity (AIN.Destination (Conn)));
      begin
468 469 470 471 472 473 474
         --  Put_Line (AIN.Node_Kind'Image (Kind (Caller)));
         return Connection'(Caller =>
           (if Kind (Caller) = K_Subcomponent_Access_Instance then US ("_env")
            else US (AIN_Case (Caller))),
                            Callee =>
           (if Kind (Callee) = K_Subcomponent_Access_Instance then US ("_env")
            else US (AIN_Case (Callee))),
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
                            PI_Name => US (Get_Name_String (PI_Name)),
                            RI_Name => US (Get_Name_String (RI_Name)));
      end Parse_Connection;

      --  Create a vector of connections for a given system
      --  This vector will then be filtered to connect end-to-end functions
      --  once the system is flattened
      function Parse_System_Connections (System : Node_Id)
         return Channels.Vector
      is
         Conn   : Node_Id;
         Result : Channels.Vector;
      begin
         if Present (AIN.Connections (System)) then
            Conn := AIN.First_Node (AIN.Connections (System));
            while Present (Conn) loop
               Result := Result & Parse_Connection (Conn);
               Conn := AIN.Next_Node (Conn);
            end loop;
         end if;
         return Result;
      end Parse_System_Connections;
497

498 499 500 501 502
      --  Parse an individual context parameter
      function Parse_CP (Subco : Node_Id) return Context_Parameter is
         CP_ASN1 : constant Node_Id    := Corresponding_Instance (Subco);
         NA      : constant Name_Array := Get_Source_Text (CP_ASN1);
      begin
Maxime Perrotin's avatar
Maxime Perrotin committed
503 504 505 506 507 508 509 510 511 512
         return Context_Parameter'(
            Name           => US (AIN_Case (Subco)),
            Sort           => US (Get_Name_String
                                        (Get_Type_Source_Name (CP_ASN1))),
            Default_Value  => US (Get_Name_String (Get_String_Property
                                        (CP_ASN1, "taste::fs_default_value"))),
            ASN1_Module    => US (Get_ASN1_Module_Name (CP_ASN1)),
            ASN1_File_Name => (if NA'Length > 0 then
                               Just (US (Get_Name_String (NA (1))))
                               else Nothing));
513 514
      end Parse_CP;

515 516 517 518 519 520 521 522 523 524 525
      --  Parse a single parameter of an interface
      --  * Name                (Unbounded string)
      --  * Sort                (Unbounded string)
      --  * ASN1_Module         (Unbounded string)
      --  * ASN1_Basic_Type     (Supported_ASN1_Basic_Type)
      --  * ASN1_File_Name      (Unbounded string)
      --  * Encoding            (Supported_ASN1_Encoding)
      --  * Direction           (Parameter_Direction: IN or OUT)
      function Parse_Parameter (Param_I : Node_Id) return ASN1_Parameter is
         Asntype : constant Node_Id := Corresponding_Instance (Param_I);
      begin
Maxime Perrotin's avatar
Maxime Perrotin committed
526
         return ASN1_Parameter'(
527 528 529 530 531 532 533 534 535 536 537 538
             Name => US (AIN_Case (Param_I)),
             Sort => US (Get_Name_String (Get_Type_Source_Name (Asntype))),
             ASN1_Module =>
                 US (Get_Name_String (Get_Ada_Package_Name (Asntype))),
             ASN1_Basic_Type => Get_ASN1_Basic_Type (Asntype),
             ASN1_File_Name =>
                US (Get_Name_String (Get_Source_Text (Asntype)(1))),
             Encoding => Get_ASN1_Encoding (Param_I),
             Direction => (if AIN.Is_In (Param_I)
                           then param_in else param_out));
      end Parse_Parameter;

539
      --  Parse a function interface :
540 541 542 543 544 545 546
      --  * Name                (Unbounded string)
      --  * Params              (Parameters.Vector)
      --  * RCM                 (Supported_RCM_Operation_Kind)
      --  * Period_Or_MIAT      (Unsigned long long)
      --  * WCET_ms             (Optional unsigned long long)
      --  * Queue_Size          (Optional unsigned long long)
      --  * User_Properties     (Property_Maps.Map)
547
      function Parse_Interface (If_I : Node_Id) return Taste_Interface is
548 549 550 551 552
         Name    : constant Name_Id := Get_Interface_Name (If_I);
         CI      : constant Node_Id := Corresponding_Instance (If_I);
         Result  : Taste_Interface;
         Sub_I   : constant Node_Id := Get_RCM_Operation (If_I);
         Param_I : Node_Id;
553
      begin
554
         pragma Assert (Present (Sub_I));
555 556 557 558 559 560 561 562 563 564 565
         --  Keep compatibility with 1.2 models for the interface name
         Result.Name := (if Name = No_Name then US (AIN_Case (If_I)) else
                         US (Get_Name_String (Name)));
         Result.Queue_Size := (if Kind (If_I) = K_Subcomponent_Access_Instance
                               and then Is_Defined_Property
                                   (CI, "taste::associated_queue_size")
                               then Just (Get_Integer_Property
                                   (CI, " taste::associated_queue_size"))
                               else Nothing);
         Result.RCM := Get_RCM_Operation_Kind (If_I);
         Result.Period_Or_MIAT := Get_RCM_Period (If_I);
566
         Result.WCET_ms := Get_Upper_WCET (If_I);
567
         Result.User_Properties := Get_Properties_Map (If_I);
568 569 570 571 572 573 574 575 576 577
         --  Parameters:
         if not Is_Empty (AIN.Features (Sub_I)) then
            Param_I := AIN.First_Node (AIN.Features (Sub_I));
            while Present (Param_I) loop
               if Kind (Param_I) = K_Parameter_Instance then
                  Result.Params := Result.Params & Parse_Parameter (Param_I);
               end if;
               Param_I := AIN.Next_Node (Param_I);
            end loop;
         end if;
578 579 580
         return Result;
      end Parse_Interface;

581 582 583 584 585 586 587 588
      --  Parse the content of a single function :
      --  * Name
      --  * Language
      --  * Zip File
      --  * Context Parameters
      --  * User Properties (from TASTE_IV_Properties.aadl)
      --  * Timers
      --  * Provided and Required Interfaces
589 590 591
      function Parse_Function (Prefix : String;
                               Name   : String;
                               Inst   : Node_Id) return Taste_Terminal_Function
592
      is
Maxime Perrotin's avatar
Maxime Perrotin committed
593 594 595 596
         Result      : Taste_Terminal_Function;
         --  To get the optional zip filename where user code is stored:
         Source_Text : constant Name_Array := Get_Source_Text (Inst);
         Zip_Id      : Name_Id             := No_Name;
597 598
         --  To get the context parameters
         Subco       : Node_Id;
599 600
         --  To get the provided and required interfaces
         PI_Or_RI    : Node_Id;
601
      begin
Maxime Perrotin's avatar
Maxime Perrotin committed
602
         Result.Name     := US (Name);
603 604
         Result.Prefix   := (if Prefix'Length > 0 then Just (US (Prefix))
                             else Nothing);
Maxime Perrotin's avatar
Maxime Perrotin committed
605 606 607 608 609
         Result.Language := Get_Source_Language (Inst);
         if Source_Text'Length /= 0 then
            Zip_Id          := Source_Text (1);
            Result.Zip_File := Just (US (Get_Name_String (Zip_Id)));
         end if;
610 611 612 613 614 615
         --  Parse context parameters
         if Present (AIN.Subcomponents (Inst)) then
            Subco := AIN.First_Node (AIN.Subcomponents (Inst));
            while Present (Subco) loop
               case Get_Category_Of_Component (Subco) is
                  when CC_Data =>
616 617
                     Result.Context_Params := Result.Context_Params
                                              & Parse_CP (Subco);
618 619 620 621 622 623
                  when others =>
                     null;
               end case;
               Subco := AIN.Next_Node (Subco);
            end loop;
         end if;
624 625 626 627
         --  Parse provided and required interfaces
         if Present (AIN.Features (Inst)) then
            PI_Or_RI := AIN.First_Node (AIN.Features (Inst));
            while Present (PI_Or_RI) loop
628 629 630 631 632 633 634
               if AIN.Is_Provided (PI_Or_RI) then
                  Result.Provided := Result.Provided
                                        & Parse_Interface (PI_Or_RI);
               else
                  Result.Required := Result.Required
                                        & Parse_Interface (PI_Or_RI);
               end if;
635 636 637
               PI_Or_RI := AIN.Next_Node (PI_Or_RI);
            end loop;
         end if;
638
         Result.User_Properties := Get_Properties_Map (Inst);
639 640 641
         return Result;
      end Parse_Function;

642
      --  Recursive parsing of a system made of nested functions (TASTE v2)
643 644
      function Rec_Function (Prefix : String := "";
                             Func   : Node_Id) return Functions.Vector is
645
         Inner        : Node_Id;
Maxime Perrotin's avatar
Maxime Perrotin committed
646
         Res          : Functions.Vector := Functions.Empty_Vector;
647
         CI           : constant Node_Id := Corresponding_Instance (Func);
648 649
         Name         : constant String := AIN_Case (Func);
         Next_Prefix  : constant String := Prefix &
Maxime Perrotin's avatar
Maxime Perrotin committed
650
                           (if Prefix'Length > 0 then "." else "") & Name;
651
      begin
652

Maxime Perrotin's avatar
Maxime Perrotin committed
653 654 655 656 657
         case Get_Category_Of_Component (CI) is
            when CC_System =>
               if Present (AIN.Subcomponents (CI)) then
                  Inner := AIN.First_Node (AIN.Subcomponents (CI));
                  while Present (Inner) loop
658 659
                     Res := Res & Rec_Function (Prefix => Next_Prefix,
                                                Func   => Inner);
Maxime Perrotin's avatar
Maxime Perrotin committed
660
                     Inner := AIN.Next_Node (Inner);
Maxime Perrotin's avatar
Maxime Perrotin committed
661 662 663
                  end loop;
               end if;

Maxime Perrotin's avatar
Maxime Perrotin committed
664
               --  Routes := Routes & Parse_System_Connections (CI);
Maxime Perrotin's avatar
Maxime Perrotin committed
665
               Routes_Map.Insert (Key      => Name,
Maxime Perrotin's avatar
Maxime Perrotin committed
666
                                  New_Item => Parse_System_Connections (CI));
667

Maxime Perrotin's avatar
Maxime Perrotin committed
668 669
               if No (AIN.Subcomponents (CI)) or Res = Functions.Empty_Vector
               then
670 671 672
                  Res := Res & Parse_Function (Prefix => Prefix,
                                               Name   => Name,
                                               Inst   => CI);
Maxime Perrotin's avatar
Maxime Perrotin committed
673 674 675 676 677 678
               end if;
            when others =>
               null;
         end case;

         return Res;
679
      end Rec_Function;
Maxime Perrotin's avatar
Maxime Perrotin committed
680
   begin
Maxime Perrotin's avatar
Maxime Perrotin committed
681 682 683
      Exit_On_Error (No (System), "Missing or erroneous interface view");

      Current_Function := AIN.First_Node (AIN.Subcomponents (System));
Maxime Perrotin's avatar
Maxime Perrotin committed
684
      --  Parse functions
Maxime Perrotin's avatar
Maxime Perrotin committed
685
      while Present (Current_Function) loop
686
         Funcs := Funcs & Rec_Function (Func => Current_Function);
Maxime Perrotin's avatar
Maxime Perrotin committed
687 688 689
         Current_Function := AIN.Next_Node (Current_Function);
      end loop;

Maxime Perrotin's avatar
Maxime Perrotin committed
690
      Routes_Map.Insert (Key      => "_Root",
Maxime Perrotin's avatar
Maxime Perrotin committed
691 692 693 694 695 696 697 698 699
                         New_Item => Parse_System_Connections (System));
      for C in Routes_Map.Iterate loop
         Put_Line ("Routes of Function " & Connection_Maps.Key (C));
         for Each of Connection_Maps.Element (C) loop
            Put_Line ("   " & To_String (Each.Caller)
                     & "." & To_String (Each.RI_Name)
                     & " -> " & To_String (Each.Callee) & "." &
                     To_String (Each.PI_Name));
         end loop;
700
      end loop;
Maxime Perrotin's avatar
Maxime Perrotin committed
701

Maxime Perrotin's avatar
Maxime Perrotin committed
702
      return IV_AST : constant Complete_Interface_View :=
Maxime Perrotin's avatar
Maxime Perrotin committed
703 704 705
          (Flat_Functions  => Funcs,
           End_To_End_Conn => Routes,
           Nested_Conn     => Routes_Map);
Maxime Perrotin's avatar
Maxime Perrotin committed
706
   end AADL_to_Ada_IV;
Maxime Perrotin's avatar
Maxime Perrotin committed
707 708

end Buildsupport_Utils;