ocarina-instances-finder.adb 19.5 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--             O C A R I N A . I N S T A N C E S . F I N D E R              --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
hugues.jerome's avatar
hugues.jerome committed
9
--               Copyright (C) 2005-2010, GET-Telecom Paris.                --
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
--                                                                          --
-- 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 Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Tree.Nutils;
with Ocarina.ME_AADL.AADL_Instances.Entities;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.Instances.Components.Subprogram_Calls;
with Errors;
with Locations;

package body Ocarina.Instances.Finder is

   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.ME_AADL.AADL_Instances.Entities;
   use Locations;
   use Errors;

   use Ocarina.Instances.Components.Subprogram_Calls;

   package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;

   procedure Select_Nodes
     (Decl_List  :        List_Id;
      Kinds      :        Node_Kind_Array;
      First_Node : in out Node_Id;
      Last_Node  : in out Node_Id);
   --  Build a list (chained using the accessor Next_Entity) from
   --  Decl_List and appends it to Last_Node. This list will contain
   --  the nodes whose kinds correspond to Kinds.

   procedure Select_Single_Node
     (Node       :        Node_Id;
      Kinds      :        Node_Kind_Array;
      First_Node : in out Node_Id;
      Last_Node  : in out Node_Id);
   --  Same as previous select_nodes, yet only appends a single node if
   --  its kind belong to the kinds array

   -------------------
   -- Find_Instance --
   -------------------

   function Find_Instance
     (Instance_Root      : Node_Id;
      Reference_Instance : Node_Id;
      Path               : List_Id)
     return Node_Id
   is
      pragma Assert (Kind (Instance_Root) = K_Architecture_Instance);
      pragma Assert (Present (Reference_Instance));

      List_Node        : Node_Id;
      Actual_List_Node : Node_Id;
      Pointed_Instance : Node_Id;
88
89
      Use_Contained_Element_Path : Boolean := False;

90
91
92
93
94
95
96
   begin
      --  Path may be the path of an entity reference; in this case it
      --  is a list of node_container that contain identifiers. Else
      --  it is a list of identifiers.

      if Path = No_List then
         Pointed_Instance := Reference_Instance;
97

98
99
100
101
102
      else
         Pointed_Instance := Reference_Instance;
         List_Node := ATN.First_Node (Path);

         while Present (List_Node) loop
103
104
105
            if not Use_Contained_Element_Path then
               if ATN.Kind (List_Node) = K_Node_Container then
                  Actual_List_Node := ATN.Item (List_Node);
106

107
108
109
110
111
112
113
114
115
116
               elsif ATN.Kind (List_Node) = K_Identifier then
                  Actual_List_Node := List_Node;

               else
                  --  XXX check whether AADLv2 always implies
                  --  Use_Contained_Element_Path to be true.

                  Actual_List_Node := ATN.First_Node (List_Items (List_Node));
                  Use_Contained_Element_Path := True;
               end if;
117
118
119
120
121
            end if;

            pragma Assert (ATN.Kind (Actual_List_Node) = K_Identifier);

            Pointed_Instance := Find_Local_Instance
122
              (Pointed_Instance, Actual_List_Node);
123
124
125

            if No (Pointed_Instance) then
               exit;
126

127
128
129
130
131
            elsif Kind (Pointed_Instance) = K_Call_Instance then
               Pointed_Instance := Duplicate_Subprogram_Call_Instance
                 (Instance_Root,
                  Pointed_Instance);
               Pointed_Instance := Corresponding_Instance (Pointed_Instance);
132

133
134
            elsif Kind (Pointed_Instance) = K_Subcomponent_Instance then
               Pointed_Instance := Corresponding_Instance (Pointed_Instance);
135

136
137
            end if;

138
139
140
141
142
143
144
145
            if not Use_Contained_Element_Path then
               List_Node := ATN.Next_Node (List_Node);
            else
               Actual_List_Node := ATN.Next_Node (Actual_List_Node);
               if No (Actual_List_Node) then
                  exit;
               end if;
            end if;
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
         end loop;
      end if;

      return Pointed_Instance;
   end Find_Instance;

   --------------------
   -- Find_All_Flows --
   --------------------

   procedure Find_All_Flows
     (Instance_Root :        Node_Id;
      First_Node    : in out Node_Id;
      Last_Node     : in out Node_Id)
   is
      use Ocarina.ME_AADL.AADL_Instances.Nutils;

      pragma Assert (AIN.Kind (Instance_Root) = K_Architecture_Instance);

      R : constant Node_Id := AIN.Root_System (Instance_Root);
      N : Node_Id;
   begin
hugues.jerome's avatar
hugues.jerome committed
168
169
170
      if Ocarina.ME_AADL.AADL_Tree.Nutils.Is_Empty (AIN.Flows (R))
        and then Present (First_Node) and then Present (Last_Node)
      then
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
216
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
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
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
476
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
513
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
         First_Node := No_Node;
         Last_Node := No_Node;
         return;
      end if;

      N := ATN.First_Node (AIN.Flows (R));
      while Present (N) loop
         if ATN.Kind (N) = K_End_To_End_Flow_Spec then
            if No (First_Node) then
               First_Node := N;
               Last_Node := N;
               ATN.Set_Next_Entity (N, No_Node);
            else
               ATN.Set_Next_Entity (N, No_Node);
               ATN.Set_Next_Entity (Last_Node, N);
               Last_Node := N;
            end if;
         end if;

         N := ATN.Next_Node (N);
      end loop;
   end Find_All_Flows;

   ------------------------
   -- Find_All_Instances --
   ------------------------

   procedure Find_All_Instances
     (Instance_Root :        Node_Id;
      Kinds         :        Node_Kind_Array;
      First_Node    : in out Node_Id;
      Last_Node     : in out Node_Id)
   is
      use Ocarina.ME_AADL.AADL_Instances.Nutils;

      pragma Assert (Kind (Instance_Root) = K_Architecture_Instance or else
                     Kind (Instance_Root) = K_Component_Instance or else
                     Kind (Instance_Root) = K_Subcomponent_Instance or else
                     Kind (Instance_Root) = K_Call_Sequence_Instance or else
                     Kind (Instance_Root) = K_Call_Instance);

      R         : Node_Id;
      List_Node : List_Id;
      N         : Node_Id;
   begin
      case AIN.Kind (Instance_Root) is
         when AIN.K_Architecture_Instance =>
            R := AIN.Root_System (Instance_Root);
            Select_Single_Node
              (R,
               Kinds,
               First_Node,
               Last_Node);

            --  We first get the declarations of the unnamed namespace

            if Present (AIN.Unnamed_Namespace (Instance_Root)) and then
              not Is_Empty (AIN.Declarations
                            (AIN.Unnamed_Namespace
                             (Instance_Root))) then
               List_Node := AIN.Declarations
                 (AIN.Unnamed_Namespace (Instance_Root));
               Select_Nodes
                 (List_Node,
                  Kinds,
                  First_Node,
                  Last_Node);
            end if;

         when AIN.K_Subcomponent_Instance =>
            R := AIN.Corresponding_Instance (Instance_Root);
            Select_Single_Node
              (R,
               Kinds,
               First_Node,
               Last_Node);

         when AIN.K_Component_Instance =>
            R := Instance_Root;
            Select_Single_Node
              (Instance_Root,
               Kinds,
               First_Node,
               Last_Node);

         when AIN.K_Call_Sequence_Instance =>
            --  Special case :
            --  we parse recursively subprogram calls

            R := Instance_Root;
            Select_Nodes (AIN.Subprogram_Calls (R),
                          Kinds,
                          First_Node,
                          Last_Node);
            if not Is_Empty (AIN.Subprogram_calls (R)) then
               N := AIN.First_Node (AIN.Subprogram_calls (R));
               while Present (N) loop
                  Find_All_Instances
                    (N, Kinds, First_Node, Last_Node);
                  N := AIN.Next_Node (N);
               end loop;
            end if;
            return;

         when AIN.K_Call_Instance =>
            R := AIN.Corresponding_Instance (Instance_Root);
            Select_Single_Node (R,
                                Kinds,
                                First_Node,
                                Last_Node);

         when others =>
            DE (Locations.Image (AIN.Loc (Instance_Root))  &
                " : unexpected instance kind");
            return;
      end case;

      Select_Nodes (AIN.Calls (R),
                    Kinds,
                    First_Node,
                    Last_Node);
      Select_Nodes (AIN.Connections (R),
                    Kinds,
                    First_Node,
                    Last_Node);

      --  Recursively search in call sequences and subcomponents instances

      if not Is_Empty (AIN.Subcomponents (R)) then
         N := AIN.First_Node (AIN.Subcomponents (R));
         while Present (N) loop
            Find_All_Instances
              (N, Kinds, First_Node, Last_Node);
            N := AIN.Next_Node (N);
         end loop;
      end if;

      if not Is_Empty (AIN.Calls (R)) then
         N := AIN.First_Node (AIN.Calls (R));
         while Present (N) loop
            Find_All_Instances
              (N, Kinds, First_Node, Last_Node);
            N := AIN.Next_Node (N);
         end loop;
      end if;
   end Find_All_Instances;

   -------------------------------
   -- Find_Instance_In_Instance --
   -------------------------------

   function Find_Instance_In_Instance
     (Instance_Root      : Node_Id;
      Reference_Instance : Node_Id;
      Path               : List_Id)
     return Node_Id
   is
      pragma Assert (Kind (Instance_Root) = K_Architecture_Instance);
      pragma Assert (Present (Reference_Instance));

      List_Node        : Node_Id;
      Actual_List_Node : Node_Id;
      Pointed_Instance : Node_Id;
   begin
      --  Path may be the path of an entity reference; in this case it
      --  is a list of node_container that contain identifiers. Else
      --  it is a list of identifiers.

      if Path = No_List then
         Pointed_Instance := Reference_Instance;
      else
         Pointed_Instance := Reference_Instance;
         List_Node := AIN.First_Node (Path);

         while Present (List_Node) loop
            if ATN.Kind (List_Node) = K_Node_Container then
               Actual_List_Node := ATN.Item (List_Node);
            elsif ATN.Kind (List_Node) = K_Identifier then
               Actual_List_Node := List_Node;
            else
               Actual_List_Node := ATN.First_Node (List_Items (List_Node));
            end if;

            pragma Assert (ATN.Kind (Actual_List_Node) = K_Identifier);

            Pointed_Instance := Find_Local_Instance
              (Pointed_Instance,
               Actual_List_Node);

            if No (Pointed_Instance) then
               exit;
            elsif Kind (Pointed_Instance) = K_Call_Instance then
               Pointed_Instance := Duplicate_Subprogram_Call_Instance
                 (Instance_Root,
                  Pointed_Instance);
               Pointed_Instance := Corresponding_Instance (Pointed_Instance);
            elsif Kind (Pointed_Instance) = K_Subcomponent_Instance then
               Pointed_Instance := Corresponding_Instance (Pointed_Instance);
            end if;

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

      return Pointed_Instance;
   end Find_Instance_In_Instance;

   -------------------------
   -- Find_Local_Instance --
   -------------------------

   function Find_Local_Instance
     (Reference_Instance  : Node_Id;
      Instance_Identifier : Node_Id)
     return Node_Id
   is
      pragma Assert (AIN.Kind (Reference_Instance) = K_Component_Instance
                       or else
                       AIN.Kind (Reference_Instance) = K_Connection_Instance);
      pragma Assert (ATN.Kind (Instance_Identifier) = K_Identifier);

      List_Node          : Node_Id;
      Sequence_List_Node : Node_Id;
      Instance_Name      : constant Name_Id := ATN.Name (Instance_Identifier);
      Component_Instance : Node_Id;
   begin
      if Kind (Reference_Instance) = K_Component_Instance then
         Component_Instance := Reference_Instance;
      else
         Component_Instance := Parent_Component (Reference_Instance);
      end if;

      --  We compare lower case names. So, when we use
      --  Get_Name_Of_Entity, we set the Get_Display_Name flag to
      --  'False'.

      if AIN.Features (Component_Instance) /= No_List then
         List_Node := AIN.First_Node (AIN.Features (Component_Instance));

         while Present (List_Node) loop
            if Get_Name_Of_Entity (List_Node, False) = Instance_Name then
               return List_Node;
            end if;

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

      if AIN.Subcomponents (Component_Instance) /= No_List then
         List_Node := AIN.First_Node
           (AIN.Subcomponents (Component_Instance));

         while Present (List_Node) loop
            if Get_Name_Of_Entity (List_Node, False) = Instance_Name then
               return List_Node;
            end if;

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

      if AIN.Modes (Component_Instance) /= No_List then
         List_Node := AIN.First_Node (AIN.Modes (Component_Instance));

         while Present (List_Node) loop
            if Get_Name_Of_Entity (List_Node, False) = Instance_Name then
               return List_Node;
            end if;

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

      if AIN.Connections (Component_Instance) /= No_List then
         List_Node := AIN.First_Node
           (AIN.Connections (Component_Instance));

         while Present (List_Node) loop
            if Get_Name_Of_Entity (List_Node, False) = Instance_Name then
               return List_Node;
            end if;

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

      if AIN.Calls (Component_Instance) /= No_List then
         List_Node := AIN.First_Node (AIN.Calls (Component_Instance));

         while Present (List_Node) loop
            if Get_Name_Of_Entity (List_Node, False) = Instance_Name then
               return List_Node;

            elsif AIN.Subprogram_Calls (List_Node) /= No_List then
               Sequence_List_Node := AIN.First_Node
                 (AIN.Subprogram_Calls (List_Node));

               while Present (Sequence_List_Node) loop
                  if Get_Name_Of_Entity (Sequence_List_Node, False)
                    = Instance_Name
                  then
                     return Sequence_List_Node;
                  end if;

                  Sequence_List_Node := AIN.Next_Node (Sequence_List_Node);
               end loop;
               List_Node := AIN.Next_Node (List_Node);
            end if;
         end loop;
      end if;

      if AIN.Flows (Component_Instance) /= No_List then
         List_Node := AIN.First_Node (AIN.Flows (Component_Instance));

         while Present (List_Node) loop
            if Get_Name_Of_Entity (List_Node, False) = Instance_Name then
               return List_Node;
            end if;

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

      return No_Node;
   end Find_Local_Instance;

   ------------------
   -- Select_Nodes --
   ------------------

   procedure Select_Nodes
     (Decl_List  :        List_Id;
      Kinds      :        Node_Kind_Array;
      First_Node : in out Node_Id;
      Last_Node  : in out Node_Id)
   is
      use Ocarina.ME_AADL.AADL_Instances.Nutils;

      Success         : Boolean;
      Local_List_Node : Node_Id;
   begin
      if not Is_Empty (Decl_List) then
         Local_List_Node :=
           AIN.First_Node (Decl_List);

         while Present (Local_List_Node) loop
            Success := False;

            for K in Kinds'Range loop
               Success := Success
                 or else (AIN.Kind (Local_List_Node) = Kinds (K));
            end loop;

            if Success then
               if No (First_Node) then
                  First_Node := Local_List_Node;
                  Last_Node := Local_List_Node;
               else
                  AIN.Set_Next_Entity (Last_Node, Local_List_Node);
                  AIN.Set_Next_Entity (Local_List_Node, No_Node);
                  Last_Node := Local_List_Node;
               end if;
            end if;

            Local_List_Node := AIN.Next_Node (Local_List_Node);
         end loop;
      end if;
   end Select_Nodes;

   ------------------------
   -- Select_Single_Node --
   ------------------------

   procedure Select_Single_Node
     (Node       :        Node_Id;
      Kinds      :        Node_Kind_Array;
      First_Node : in out Node_Id;
      Last_Node  : in out Node_Id)
   is
      use Ocarina.ME_AADL.AADL_Instances.Nutils;

      Success         : Boolean;
   begin
      if Present (Node) then
         Success := False;

         for K in Kinds'Range loop
            Success := Success
              or else (AIN.Kind (Node) = Kinds (K));
         end loop;

         if Success then
            if No (First_Node) then
               First_Node := Node;
               Last_Node := Node;
            else
               AIN.Set_Next_Entity (Last_Node, Node);
               AIN.Set_Next_Entity (Node, No_Node);
               Last_Node := Node;
            end if;
         end if;
      end if;
   end Select_Single_Node;

end Ocarina.Instances.Finder;