ocarina-instances.adb 15.7 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
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
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                    O C A R I N A . I N S T A N C E S                     --
--                                                                          --
--                                 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 Errors;
with Locations;
with Namet;
with Output;

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

with Ocarina.Options;
with Ocarina.Analyzer.AADL.Finder;
with Ocarina.Processor.Properties;
with Ocarina.Instances.Components;
with Ocarina.Instances.Namespaces;
with Ocarina.Instances.Processor.Properties;

package body Ocarina.Instances is

   use Errors;
   use Locations;
   use Namet;
   use Output;

   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.ME_AADL.Printers;

   use Ocarina.Options;
   use Ocarina.Analyzer.AADL.Finder;
   use Ocarina.Processor.Properties;
   use Ocarina.Instances.Components;
   use Ocarina.Instances.Namespaces;
   use Ocarina.Instances.Processor.Properties;

   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;

   function Build_Instance_Internal_Name (N : Node_Id) return Name_Id;
   --  Factorize code between (Get/Set)_Instance

   ----------------------------------
   -- Append_To_Namespace_Instance --
   ----------------------------------

   procedure Append_To_Namespace_Instance
     (Instance_Root   : Node_Id;
      Entity_Instance : Node_Id)
   is
      pragma Assert (Kind (Instance_Root) = K_Architecture_Instance);
      pragma Assert (Kind (Entity_Instance) = K_Component_Instance
                     or else Kind (Entity_Instance) = K_Subcomponent_Instance);

      Namespace_Model       : Node_Id;
      Namespace_Instance    : Node_Id;
      Component_Instance    : Node_Id;
      Subcomponent_Instance : Node_Id;

   begin
      --  Set namespaces

      if Kind (Entity_Instance) = K_Component_Instance then
         Component_Instance := Entity_Instance;
      elsif Kind (Entity_Instance) = K_Subcomponent_Instance then
         Component_Instance := Corresponding_Instance (Entity_Instance);
      else
         raise Program_Error;
      end if;

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

      --  Append the subcomponents before the component itself since
      --  the AADL model may be converted into a language that
      --  requires entities to be declared in order to be used.

      if not Is_Empty (AIN.Subcomponents (Component_Instance)) then
         Subcomponent_Instance := AIN.First_Node
           (AIN.Subcomponents (Component_Instance));

         while Present (Subcomponent_Instance) loop
            Append_To_Namespace_Instance
              (Instance_Root,
               Subcomponent_Instance);
            Subcomponent_Instance := AIN.Next_Node (Subcomponent_Instance);
         end loop;
      end if;

      --  Append the component itself

      if No (Get_First_Contained_Homonym_Instance
             (AIN.Declarations (Namespace_Instance),
              Component_Instance))
      then
         Append_Node_To_List
           (Make_Node_Container (Component_Instance),
            AIN.Declarations (Namespace_Instance));
      end if;
   end Append_To_Namespace_Instance;

   -----------------------
   -- Instantiate_Model --
   -----------------------

   function Instantiate_Model (Root : Node_Id) return Node_Id is
      pragma Assert (Kind (Root) = K_AADL_Specification);

      Root_Systems  : Node_List;
      Instance_Root : Node_Id;
      Root_System   : Node_Id := No_Node;
      List_Node     : Node_Id;

   begin
      Errors.Initialize;
      N_Errors   := 0;
      N_Warnings := 0;

      --  Find all the root systems, i.e. AADL system implementations
      --  not connected to any other AADL entity.

      Root_Systems := Find_All_Root_Systems (Root);

      if Root_Systems.First /= Root_Systems.Last then

         --  If the user provided a particular root system to
         --  instantiate, find this system in the root system
         --  list. Otherwise, display an error message indicating that
         --  there are too many systems eligible for instantiation.

         if Root_System_Name /= No_Name then
            List_Node := Root_Systems.First;
            while Present (List_Node) loop
               exit when
                 ATE.Get_Name_Of_Entity (List_Node, False) = Root_System_Name;
               List_Node := ATN.Next_Entity (List_Node);
            end loop;

            if Present (List_Node) then
               Root_System := List_Node;
            end if;

         else
            Error_Loc (1) := No_Location;
            DE ("Cannot select a root system among several ");
            List_Node := Root_Systems.First;
            while Present (List_Node) loop
               Error_Loc (1)  := ATN.Loc (List_Node);
               Error_Name (1) := ATE.Get_Name_Of_Entity (List_Node);
               DE ("%");
               List_Node := ATN.Next_Entity (List_Node);
            end loop;
         end if;

      else
         --  If there is only one root system, choose it

         Root_System := Root_Systems.First;
         if No (Root_System) then
            Error_Loc (1) := No_Location;
            DE ("Cannot find a root system");
         end if;

         if Root_System_Name /= No_Name
           and then ATE.Get_Name_Of_Entity (Root_System, False) /=
           Root_System_Name
         then
            Root_System := No_Node;
         end if;
      end if;

      --  If the chosen top level system does not correspond to the
      --  system name given by the user, complain by displaying an
      --  error and aborting instantiation.

      if No (Root_System) then
         Instance_Root := No_Node;

      else
         --  The first step of the instantiation consist of propagate the
         --  properties declared in the AADL packages to the AADL
         --  component they can apply to.

         Diffuse_Package_Properties_To_Entities (Root);

         Instance_Root := New_Node (K_Architecture_Instance, No_Location);
         Set_Namespaces (Instance_Root, New_List (K_List_Id, No_Location));
         Set_Unnamed_Namespace (Instance_Root, No_Node);

         --  Begin the instantiation

         Set_Root_System
           (Instance_Root, Instantiate_Component (Instance_Root, Root_System));
         Compute_Property_Instance_Values (Instance_Root);
      end if;

      if N_Errors /= 0 or else N_Warnings /= 0 then
         Error_Loc (1) := No_Location;
         Error_Int (1) := N_Errors;
         Error_Int (2) := N_Warnings;

         Set_Str_To_Name_Buffer ("error");

         if N_Errors > 1 then
            Add_Char_To_Name_Buffer ('s');
         end if;

         Error_Name (1) := Name_Find;

         Set_Str_To_Name_Buffer ("warning");

         if N_Warnings > 1 then
            Add_Char_To_Name_Buffer ('s');
         end if;

         Error_Name (2) := Name_Find;
         DE ("Total: $%and $%");
      end if;

      if No (Instance_Root) then
         Set_Standard_Error;
         Write_Line ("Cannot instantiate AADL models");

      elsif Debug_Mode
        and then Get_Current_Action = Instantiate_Model
      then
         Print_AADL_Tree
           (Instance_Root,
            Ocarina.ME_AADL.AADL_Instances.Debug.W_Node_Id'Access);
      end if;

      return Instance_Root;
   end Instantiate_Model;

   -----------------------
   -- Get_First_Homonym --
   -----------------------

   function Get_First_Homonym
     (Declaration_List    : List_Id;
      Name_Of_Declaration : Name_Id)
     return Node_Id
   is
      List_Node : Node_Id;
   begin
      if Name_Of_Declaration = No_Name
        or else Declaration_List = No_List
      then
         return No_Node;
      end if;

      List_Node := ATN.First_Node (Declaration_List);

      while Present (List_Node) loop
         exit when ATE.Get_Name_Of_Entity
           (List_Node, False) = Name_Of_Declaration;
         List_Node := ATN.Next_Node (List_Node);
      end loop;

      return List_Node;
   end Get_First_Homonym;

   --------------------------------
   -- Get_First_Homonym_Instance --
   --------------------------------

   function Get_First_Homonym_Instance
     (Declaration_List    : List_Id;
      Name_Of_Declaration : Name_Id)
     return Node_Id
   is
      List_Node : Node_Id;
   begin
      if Name_Of_Declaration = No_Name
        or else Declaration_List = No_List
      then
         return No_Node;
      end if;

      List_Node := AIN.First_Node (Declaration_List);

      while Present (List_Node) loop
         exit when Get_Name_Of_Entity (List_Node, False) = Name_Of_Declaration;
         List_Node := AIN.Next_Node (List_Node);
      end loop;

      return List_Node;
   end Get_First_Homonym_Instance;

   ---------------------------------
   -- Get_First_Contained_Homonym --
   ---------------------------------

   function Get_First_Contained_Homonym
     (Declaration_List    : List_Id;
      Name_Of_Declaration : Name_Id)
     return Node_Id
   is
      List_Node : Node_Id;
   begin
      if Name_Of_Declaration = No_Name
        or else Declaration_List = No_List
      then
         return No_Node;
      end if;

      List_Node := AIN.First_Node (Declaration_List);

      while Present (List_Node) loop
         exit when Get_Name_Of_Entity (AIN.Item (List_Node), False) =
           Name_Of_Declaration;
         List_Node := AIN.Next_Node (List_Node);
      end loop;

      if No (List_Node) then
         return List_Node;
      else
         return AIN.Item (List_Node);
      end if;
   end Get_First_Contained_Homonym;

   ------------------------------------------
   -- Get_First_Contained_Homonym_Instance --
   ------------------------------------------

   function Get_First_Contained_Homonym_Instance
     (Declaration_List    : List_Id;
      Name_Of_Declaration : Name_Id)
     return Node_Id
   is
      List_Node : Node_Id;
   begin
      if Name_Of_Declaration = No_Name
        or else Declaration_List = No_List
      then
         return No_Node;
      end if;

      List_Node := AIN.First_Node (Declaration_List);

      while Present (List_Node) loop
         exit when Get_Name_Of_Entity (AIN.Item (List_Node), False) =
           Name_Of_Declaration;
         List_Node := AIN.Next_Node (List_Node);
      end loop;

      if No (List_Node) then
         return List_Node;
      else
         return AIN.Item (List_Node);
      end if;
   end Get_First_Contained_Homonym_Instance;

   -----------------------
   -- Get_First_Homonym --
   -----------------------

   function Get_First_Homonym
     (Declaration_List : List_Id;
      Declaration      : Node_Id)
     return Node_Id
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Homonym
        (Declaration_List,
         Get_Name_Of_Entity (Declaration, False));
   end Get_First_Homonym;

   --------------------------------
   -- Get_First_Homonym_Instance --
   --------------------------------

   function Get_First_Homonym_Instance
     (Declaration_List : List_Id;
      Declaration      : Node_Id)
     return Node_Id
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Homonym_Instance
        (Declaration_List,
         ATE.Get_Name_Of_Entity (Declaration, False));
   end Get_First_Homonym_Instance;

   ---------------------------------
   -- Get_First_Contained_Homonym --
   ---------------------------------

   function Get_First_Contained_Homonym
     (Declaration_List : List_Id;
      Declaration      : Node_Id)
     return Node_Id
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Contained_Homonym
        (Declaration_List,
         ATE.Get_Name_Of_Entity (Declaration, False));
   end Get_First_Contained_Homonym;

   ------------------------------------------
   -- Get_First_Contained_Homonym_Instance --
   ------------------------------------------

   function Get_First_Contained_Homonym_Instance
     (Declaration_List : List_Id;
      Declaration      : Node_Id)
     return Node_Id
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Contained_Homonym_Instance
        (Declaration_List,
         Get_Name_Of_Entity (Declaration, False));
   end Get_First_Contained_Homonym_Instance;

   ----------------------------------
   -- Build_Instance_Internal_Name --
   ----------------------------------

   function Build_Instance_Internal_Name (N : Node_Id) return Name_Id is
   begin
      Set_Str_To_Name_Buffer ("%Instance%");
      Add_Nat_To_Name_Buffer (Nat (N));
      return Name_Find;
   end Build_Instance_Internal_Name;

   ------------------
   -- Get_Instance --
   ------------------

   function Get_Instance (N : Node_Id) return Node_Id is
      I_Name : constant Name_Id := Build_Instance_Internal_Name (N);
   begin
      return Node_Id (Get_Name_Table_Info (I_Name));
   end Get_Instance;

   ------------------
   -- Set_Instance --
   ------------------

   procedure Set_Instance (N : Node_Id; E : Node_Id) is
      I_Name : constant Name_Id := Build_Instance_Internal_Name (N);
   begin
      Set_Name_Table_Info (I_Name, Int (E));
   end Set_Instance;

end Ocarina.Instances;