ocarina-backends-properties-utils.adb 11 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--    O C A R I N A . B A C K E N D S . P R O P E R T I E S . U T I L S     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--                   Copyright (C) 2014-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 27 28 29 30 31
--                                                                          --
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

yoogx's avatar
yoogx committed
32
with Ocarina.Backends.Messages;
33
with Ocarina.Instances.Queries;
yoogx's avatar
yoogx committed
34 35
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.Namet;
36 37 38 39
with Utils;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Tree.NUtils;
with Ocarina.AADL_Values;
40 41

package body Ocarina.Backends.Properties.Utils is
yoogx's avatar
yoogx committed
42 43

   use Ocarina.Backends.Messages;
44
   use Ocarina.Instances.Queries;
yoogx's avatar
yoogx committed
45 46
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.Namet;
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 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
   use Standard.Utils;

   package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
   package ATNU renames Ocarina.ME_AADL.AADL_Tree.NUtils;
   use type ATN.Node_Kind;

   function SA (S : String) return String_Access is (new String'(S));

   -----------------------------------------
   -- Print_Value_Of_Property_Association --
   -----------------------------------------

   function Print_Value_Of_Property_Association
     (AADL_Property_Value : Node_Id)
     return String;

   function Print_Value_Of_Property_Association
     (AADL_Property_Value : Node_Id)
     return String is
   begin
      if Present (AADL_Property_Value)
        and then ATN.Kind (AADL_Property_Value) = ATN.K_Signed_AADLNumber
      then
         return Ocarina.AADL_Values.Image
           (ATN.Value
              (ATN.Number_Value
                 (AADL_Property_Value)))
           & " "
           & (if Present (ATN.Unit_Identifier (AADL_Property_Value))
                then Get_Name_String
           (ATN.Display_Name
              (ATN.Unit_Identifier (AADL_Property_Value)))
                else "");

      elsif Present (AADL_Property_Value)
        and then ATN.Kind (AADL_Property_Value) = ATN.K_Literal
      then
         --  This property value denotes a literal

         return Ocarina.AADL_Values.Image
           (ATN.Value (AADL_Property_Value), Quoted => False);

      elsif Present (AADL_Property_Value)
        and then ATN.Kind (AADL_Property_Value) = ATN.K_Reference_Term
      then
         --  This property value denotes a reference term

         return Get_Name_String (ATN.Display_Name
                                   (ATN.First_Node --  XXX must iterate
                                      (ATN.List_Items
                                         (ATN.Reference_Term
                                            (AADL_Property_Value)))));

      elsif Present (AADL_Property_Value)
        and then ATN.Kind (AADL_Property_Value) = ATN.K_Enumeration_Term
      then
            --  This property value denotes an enumeration term

            return Get_Name_String (ATN.Display_Name
                                      (ATN.Identifier (AADL_Property_Value)));

      elsif Present (AADL_Property_Value)
        and then ATN.Kind (AADL_Property_Value) = ATN.K_Number_Range_Term
      then
         --  This property value denotes a number range term

         return Ocarina.AADL_Values.Image
           (ATN.Value
              (ATN.Number_Value
                 (ATN.Lower_Bound
                    (AADL_Property_Value))))
           &
           (if Present
              (ATN.Unit_Identifier
                 (ATN.Lower_Bound (AADL_Property_Value)))
              then
           " " & Get_Name_String
           (ATN.Display_Name
              (ATN.Unit_Identifier
                 (ATN.Lower_Bound (AADL_Property_Value))))
              else "")
                & " .. "
                & Ocarina.AADL_Values.Image
                (ATN.Value
                   (ATN.Number_Value
                      (ATN.Upper_Bound
                         (AADL_Property_Value))))
                &
                (if Present
                   (ATN.Unit_Identifier
                      (ATN.Upper_Bound (AADL_Property_Value)))
                   then
                " " & Get_Name_String
                (ATN.Display_Name
                   (ATN.Unit_Identifier
                   (ATN.Upper_Bound (AADL_Property_Value))))
                   else "");
      end if;

      raise Program_Error;
   end Print_Value_Of_Property_Association;
148 149 150 151 152

   ----------------------------
   -- Check_And_Get_Property --
   ----------------------------

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 204 205 206 207 208 209 210 211 212 213 214 215
   function Check_And_Get_Property
     (E : Node_Id;
      Property : Node_Id)
     return String_List
   is
      Prop_Name : constant Name_Id
        := Standard.Utils.To_Lower (Display_Name (Identifier (Property)));
      A : constant String_Access
        := new String'(Get_Name_String (Prop_Name));
      AADL_Property_Value : Node_Id;

   begin
      if Is_Defined_String_Property (E, Prop_Name) then
         return
           A & SA (Get_Name_String (Get_String_Property (E, Prop_Name)));

      elsif (Is_Defined_Integer_Property (E, Prop_Name)
               or else Is_Defined_Float_Property (E, Prop_Name))
      then
         AADL_Property_Value :=
           Get_Value_Of_Property_Association (E, Prop_Name);
         return
           A & SA (Print_Value_Of_Property_Association (AADL_Property_Value));

      elsif Is_Defined_Boolean_Property (E, Prop_Name) then
         return
           A & SA (Boolean'Image (Get_Boolean_Property (E, Prop_Name)));

      elsif Is_Defined_Reference_Property (E, Prop_Name) then
         return A & SA (Get_Reference_Property (E, Prop_Name)'Img);

      elsif Is_Defined_Classifier_Property (E, Prop_Name) then
         return A & SA (Get_Classifier_Property (E, Prop_Name)'Img);

      elsif Is_Defined_Range_Property (E, Prop_Name) then
         AADL_Property_Value := Get_Range_Property (E, Prop_Name);
         return A
           & SA (Print_Value_Of_Property_Association (AADL_Property_Value));

      elsif Is_Defined_List_Property (E, Prop_Name) then
         declare
            It : Node_Id :=
              ATN.First_Node (Get_List_Property (E, Prop_Name));
            B : String_List
              (1 .. ATNU.Length (Get_List_Property (E, Prop_Name)));
            J : Integer := B'First;
         begin
            while Present (It) loop
               B (J) := new String'(Print_Value_Of_Property_Association (It));
               J := J + 1;
               It := ATN.Next_Node (It);
            end loop;
            return A & B;
         end;

      elsif Is_Defined_Enumeration_Property (E, Prop_Name) then
         return A & SA (Get_Name_String
                          (Get_Enumeration_Property (E, Prop_Name)));
      end if;

      return A & SA (" KO");
   end Check_And_Get_Property;

216 217 218 219 220 221 222 223 224 225 226 227 228
   function Check_And_Get_Property
     (E : Node_Id;
      Property_Name : Name_Id;
      Default_Value : Unsigned_Long_Long := 0)
  return Unsigned_Long_Long is
   begin
      if Is_Defined_Integer_Property (E, Property_Name) then
         return Get_Integer_Property (E, Property_Name);
      else
         return Default_Value;
      end if;
   end Check_And_Get_Property;

yoogx's avatar
yoogx committed
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
   function Check_And_Get_Property
     (E : Node_Id;
      Property_Name : Name_Id;
      Default_Value : Name_Id := No_Name)
     return Name_Id is
   begin
      if Is_Defined_String_Property (E, Property_Name) then
         return Get_String_Property (E, Property_Name);
      else
         return Default_Value;
      end if;
   end Check_And_Get_Property;

   function Check_And_Get_Property
     (E : Node_Id;
      Property_Name : Name_Id;
      Default_Value : Node_Id := No_Node)
     return Node_Id is
   begin
      if Is_Defined_Property (E, Property_Name) then
         return Get_Classifier_Property (E, Property_Name);
      else
         return Default_Value;
      end if;
   end Check_And_Get_Property;

yoogx's avatar
yoogx committed
255 256 257 258 259 260 261 262 263 264 265 266 267
   function Check_And_Get_Property
     (E : Node_Id;
      Property_Name : Name_Id;
      Default_Value : List_Id := No_List)
     return List_Id is
   begin
      if Is_Defined_List_Property (E, Property_Name) then
         return Get_List_Property (E, Property_Name);
      else
         return Default_Value;
      end if;
   end Check_And_Get_Property;

yoogx's avatar
yoogx committed
268 269 270 271 272 273 274 275 276 277 278 279 280
   function Check_And_Get_Property
     (E : Node_Id;
      Property_Name : Name_Id;
      Default_Value : Boolean := False)
     return Boolean is
   begin
      if Is_Defined_Boolean_Property (E, Property_Name) then
         return Get_Boolean_Property (E, Property_Name);
      else
         return Default_Value;
      end if;
   end Check_And_Get_Property;

yoogx's avatar
yoogx committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
   function Check_And_Get_Property
     (E : Node_Id;
      Property_Name : Name_Id;
      Names : Name_Array;
      Default_Value : Int := Int'First)
     return Int
   is
      P_Name : Name_Id;
   begin
      if not Is_Defined_Enumeration_Property (E, Property_Name) then
         return Default_Value;
      end if;

      P_Name := Get_Enumeration_Property (E, Property_Name);
      for J in Names'Range loop
         if P_Name = Names (J) then
            return J;
         end if;
      end loop;

      Display_Located_Error
        (Loc (E),
         "Unknown enumerator " & Get_Name_String (P_Name),
         Fatal => True);
      return Default_Value;
   end Check_And_Get_Property;

308
end Ocarina.Backends.Properties.Utils;