ocarina-instances-components-features.adb 16.8 KB
Newer Older
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
32
33
34
35
36
37
38
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
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
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                  OCARINA.INSTANCES.COMPONENTS.FEATURES                   --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--               Copyright (C) 2005-2009, GET-Telecom Paris.                --
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. 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. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with Locations;

with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Entities;
with Ocarina.ME_AADL.AADL_Tree.Entities;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.ME_AADL.AADL_Tree.Nutils;

with Ocarina.Annotations;
with Ocarina.Instances.Messages;
with Ocarina.Instances.Namespaces;

package body Ocarina.Instances.Components.Features is

   use Locations;
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.ME_AADL.AADL_Instances.Entities;
   use Ocarina.ME_AADL.AADL_Instances.Nutils;
   use Ocarina.Annotations;
   use Ocarina.Instances.Messages;
   use Ocarina.Instances.Namespaces;

   package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;
   package ATE renames Ocarina.ME_AADL.AADL_Tree.Entities;
   package ATNU renames Ocarina.ME_AADL.AADL_Tree.Nutils;

   function Instantiate_Corresponding_Entity
     (Instance_Root : Node_Id;
      Feature       : Node_Id;
      Container     : Node_Id)
     return Node_Id;

   function Instantiate_Port_Group_Type
     (Instance_Root : Node_Id;
      Port_Group    : Node_Id;
      Feature_List  : List_Id;
      Container     : Node_Id;
      Inverse       : Boolean := False)
     return Boolean;
   --  Recursively instantiate a port group type, with extensions, features
   --  and inverses. Return True if everything was OK, else False

   -------------------------
   -- Instantiate_Feature --
   -------------------------

   function Instantiate_Feature
     (Instance_Root : Node_Id;
      Feature       : Node_Id;
      Inverse       : Boolean := False)
     return Node_Id
   is
      pragma Assert (Kind (Instance_Root) = K_Architecture_Instance);
      pragma Assert (Kind (Feature) = K_Subcomponent_Access
                     or else Kind (Feature) = K_Feature_Group_Spec
                     or else Kind (Feature) = K_Subprogram_Spec
                     or else Kind (Feature) = K_Parameter
                     or else Kind (Feature) = K_Port_Spec);

      New_Instance    : Node_Id := No_Node;
      New_Subinstance : Node_Id := No_Node;
      Container       : constant Node_Id := Container_Component (Feature);
      Port_Group      : Node_Id := No_Node;
      Success         : Boolean := True;
   begin
      case ATN.Kind (Feature) is
         when K_Port_Spec =>
            New_Instance := Ocarina.ME_AADL.AADL_Instances.Nutils.New_Node
              (K_Port_Spec_Instance, ATN.Loc (Feature));
            AIN.Set_Is_In (New_Instance, ATN.Is_In (Feature));
            AIN.Set_Is_Out (New_Instance, ATN.Is_Out (Feature));
            AIN.Set_Is_Event (New_Instance, ATN.Is_Event (Feature));
            AIN.Set_Is_Data (New_Instance, ATN.Is_Data (Feature));
            AIN.Set_Identifier (New_Instance,
                            Duplicate_Identifier (ATN.Identifier (Feature)));
            AIN.Set_Sources (New_Instance, New_List (K_List_Id, No_Location));
            AIN.Set_Destinations (New_Instance,
                                  New_List (K_List_Id, No_Location));

            if ATN.Is_Data (Feature) then
               --  We instantiate the corresponding Data component for data
               --  and event data ports.

               New_Subinstance := Instantiate_Corresponding_Entity
                 (Instance_Root,
                  Feature,
                  Container);

               if Present (New_Subinstance) then
                  Set_Corresponding_Instance (New_Instance, New_Subinstance);
               else
                  Success := False;
               end if;
            end if;

         when K_Feature_Group_Spec =>
            if Entity_Ref (Feature) /= No_Node
              and then ATE.Get_Referenced_Entity
              (Entity_Ref (Feature)) /= No_Node
            then
               New_Instance := New_Node (K_Feature_Group_Spec_Instance,
                                         ATN.Loc (Feature));
               AIN.Set_Features (New_Instance,
                             New_List (K_List_Id, ATN.Loc (Feature)));
               AIN.Set_Identifier (New_Instance,
                              Duplicate_Identifier (ATN.Identifier (Feature)));
               Port_Group := ATE.Get_Referenced_Entity (Entity_Ref (Feature));
               AIN.Set_Sources
                  (New_Instance, New_List (K_List_Id, No_Location));
               AIN.Set_Destinations (New_Instance,
                                  New_List (K_List_Id, No_Location));

               Success := Instantiate_Port_Group_Type
                 (Instance_Root,
                  Port_Group,
                  Ocarina.ME_AADL.AADL_Instances.Nodes.Features (New_Instance),
                  Container,
                  Inverse);
            else
               Display_No_Entity_Ref (Feature);
               Success := False;
            end if;

         when K_Parameter =>
            New_Instance := New_Node (K_Parameter_Instance, ATN.Loc (Feature));
            AIN.Set_Is_In (New_Instance, ATN.Is_In (Feature));
            AIN.Set_Is_Out (New_Instance, ATN.Is_Out (Feature));
            AIN.Set_Identifier (New_Instance,
                            Duplicate_Identifier (ATN.Identifier (Feature)));
            AIN.Set_Sources (New_Instance, New_List (K_List_Id, No_Location));
            AIN.Set_Destinations (New_Instance,
                                  New_List (K_List_Id, No_Location));

            New_Subinstance := Instantiate_Corresponding_Entity
173
              (Instance_Root, Feature, Container);
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

            if Present (New_Subinstance) then
               Set_Corresponding_Instance (New_Instance, New_Subinstance);
            else
               Success := False;
            end if;

         when K_Subprogram_Spec =>
            New_Instance := New_Node (K_Subprogram_Spec_Instance,
                                      ATN.Loc (Feature));
            AIN.Set_Is_Server (New_Instance, ATN.Is_Server (Feature));
            AIN.Set_Identifier (New_Instance,
                            Duplicate_Identifier (ATN.Identifier (Feature)));
            Set_Sources (New_Instance, New_List (K_List_Id, No_Location));
            Set_Destinations (New_Instance, New_List (K_List_Id, No_Location));

            --  Instantiate the corresponding subprogram component

            New_Subinstance := Instantiate_Corresponding_Entity
193
              (Instance_Root, Feature, Container);
194
195
196
197
198
199
200
201
202
203
204
205
206
207

            if Present (New_Subinstance) then
               Set_Corresponding_Instance (New_Instance, New_Subinstance);
            else
               Success := False;
            end if;

         when K_Subcomponent_Access =>
            New_Instance := New_Node (K_Subcomponent_Access_Instance,
                                      ATN.Loc (Feature));
            AIN.Set_Is_Provided (New_Instance, ATN.Is_Provided (Feature));
            AIN.Set_Is_Data (New_Instance,
                         Component_Category'Val
                         (Subcomponent_Category (Feature)) = CC_Data);
208
209
210
211

            AIN.Set_Identifier
              (New_Instance,
               Duplicate_Identifier (ATN.Identifier (Feature)));
212
213
214
215
216
217
218
219
220
221
222
223
224
            Set_Sources (New_Instance, New_List (K_List_Id, No_Location));
            Set_Destinations (New_Instance, New_List (K_List_Id, No_Location));

            --  Instantiate the corresponding component

            --  FIXME: This is definitely a WRONG design. We MUST:

            --  1 - NOT instantiate the component corresponding to an
            --  access.

            --  2 - POSTPONE the resolution of this at the connection
            --  instantiation.

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
            if Component_Category'Val
              (Subcomponent_Category (Feature)) = CC_Data
              and then Present (Get_Instance (ATE.Get_Referenced_Entity
                                                (Entity_Ref (Feature))))
            then
               --  XXX If the component type denotes a data type, we
               --  recycle an existing instance. this is done to avoid
               --  infinite recursion, may lead to incorrect
               --  properties being set. We should definitely
               --  implement the recommandation above.  Yet, this
               --  removes a dirtier hack in Instantiate_Component

               Set_Corresponding_Instance
                 (New_Instance,
                  Get_Instance (ATE.Get_Referenced_Entity
                                  (Entity_Ref (Feature))));
241
242

            else
243
244
245
246
247
248
249
250
               New_Subinstance := Instantiate_Corresponding_Entity
                 (Instance_Root, Feature, Container);

               if Present (New_Subinstance) then
                  Set_Corresponding_Instance (New_Instance, New_Subinstance);
               else
                  Success := False;
               end if;
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
            end if;

         when others =>
            raise Program_Error with "Unknown feature kind "
              & ATN.Kind (Feature)'Img;
      end case;

      if Success then
         Set_Corresponding_Declaration (New_Instance, Feature);
         return New_Instance;
      else
         return No_Node;
      end if;
   end Instantiate_Feature;

   --------------------------------------
   -- Instantiate_Corresponding_Entity --
   --------------------------------------

   function Instantiate_Corresponding_Entity
     (Instance_Root : Node_Id;
      Feature       : Node_Id;
      Container     : Node_Id)
     return Node_Id
   is
      pragma Assert (Kind (Instance_Root) = K_Architecture_Instance);
      pragma Assert (Kind (Feature) = K_Subcomponent_Access
                     or else Kind (Feature) = K_Feature_Group_Spec
                     or else Kind (Feature) = K_Subprogram_Spec
                     or else Kind (Feature) = K_Parameter
                     or else Kind (Feature) = K_Port_Spec);

      Namespace_Model    : Node_Id;
      Namespace_Instance : Node_Id;
      C                  : Node_Id;
      New_Subinstance    : Node_Id := No_Node;
   begin
      if No (Entity_Ref (Feature)) then
         --  Abort the instantiation of the corresponding entity if there
         --  is no such corresponding entity.

         return No_Node;
      else
         C := ATE.Get_Referenced_Entity (Entity_Ref (Feature));

         --  Annotate the component with container

         Annotate (C, Container);

         --  Getting the component namespace

         Namespace_Model := ATN.Namespace (C);
         Namespace_Instance := Instantiate_Namespace
           (Instance_Root, Namespace_Model);

         --  Verify whether the component has been instantiateed or not

         New_Subinstance := Get_First_Contained_Homonym
           (AIN.Declarations (Namespace_Instance),
            ATE.Get_Referenced_Entity (Entity_Ref (Feature)));

         --  If the component is already instantiateed, return it

         if Present (New_Subinstance) then
            return New_Subinstance;
         end if;

318
         --  If the component isn't instantiated yet, instantiate it...
319
320
321
322

         New_Subinstance := Instantiate_Component (Instance_Root, C);

         if Present (New_Subinstance) then
323
            --  Instantiation is successful, append the component to the
324
325
326
327
328
329
330
            --  declarations of its namespace. If the component has
            --  subcomponents, they will be added recursivly.

            --  The namespace declaration list is a node container
            --  list because we cannot append the same node in two
            --  different lists.

331
332
            Append_To_Namespace_Instance (Instance_Root, New_Subinstance);

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
         else
            --  Something went wrong, propagate the information by
            --  returning No_Node.

            return No_Node;
         end if;
      end if;

      return New_Subinstance;
   end Instantiate_Corresponding_Entity;

   ---------------------------------
   -- Instantiate_Port_Group_Type --
   ---------------------------------

   function Instantiate_Port_Group_Type
     (Instance_Root : Node_Id;
      Port_Group    : Node_Id;
      Feature_List  : List_Id;
      Container     : Node_Id;
      Inverse       : Boolean := False)
     return Boolean
   is
      pragma Assert (Feature_List /= No_List);
      pragma Assert (AIN.Kind (Instance_Root) = K_Architecture_Instance);
      pragma Assert (ATN.Kind (Port_Group) = K_Feature_Group_Type);

      Success         : Boolean := True;
      List_Node       : Node_Id := No_Node;
      Instantiateable_Node : Node_Id := No_Node;
      New_Subinstance : Node_Id := No_Node;
   begin
      --  Annotate the parent port group with the container node

      Annotate (Port_Group, Container);

      --  Parent port group

      if Present (ATN.Parent (Port_Group))
        and then Present (ATE.Get_Referenced_Entity
                            (ATN.Parent (Port_Group)))
      then
         --  Instantiate the parent port group

         Success := Instantiate_Port_Group_Type
           (Instance_Root,
            ATE.Get_Referenced_Entity
              (ATN.Parent (Port_Group)),
            Feature_List,
            Container,
            Inverse);
      end if;

      --  Features

      if not ATNU.Is_Empty (ATN.Features (Port_Group)) then
         List_Node := ATN.First_Node (ATN.Features (Port_Group));

         while Present (List_Node) loop
            if not Is_Implicit_Inverse (List_Node) then
               if Inverse then
                  Instantiateable_Node := Inversed_Entity (List_Node);
               else
                  Instantiateable_Node := List_Node;
               end if;

               New_Subinstance := Instantiate_Feature
                 (Instance_Root,
                  Instantiateable_Node,
                  Inverse);

               if Present (New_Subinstance) then
                  Append_Node_To_List (New_Subinstance, Feature_List);
               else
                  Success := False;
               end if;
            end if;

            List_Node := ATN.Next_Node (List_Node);
         end loop;
      end if;

      --  Inverse Of

      if Present (Inverse_Of (Port_Group))
        and then Present (ATE.Get_Referenced_Entity (Inverse_Of (Port_Group)))
      then
         Success := Instantiate_Port_Group_Type
           (Instance_Root,
            ATE.Get_Referenced_Entity (Inverse_Of (Port_Group)),
            Feature_List,
            Container,
            not Inverse)
           and then Success;
      end if;

      return Success;
   end Instantiate_Port_Group_Type;

end Ocarina.Instances.Components.Features;