ocarina-instances.adb 16 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                     --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2005-2009 Telecom ParisTech, 2010-2015 ESA & ISAE.      --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--                                                                          --
-- 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.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29
30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31
32
33
34
35
--                                                                          --
------------------------------------------------------------------------------

with Errors;
with Locations;
36
37
with Ocarina.Namet;
with Ocarina.Output;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

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;
58
59
   use Ocarina.Namet;
   use Ocarina.Output;
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

   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);
90
91
92
      pragma Assert
        (Kind (Entity_Instance) = K_Component_Instance
         or else Kind (Entity_Instance) = K_Subcomponent_Instance);
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

      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;

110
111
112
113
      Namespace_Model :=
        ATN.Namespace (Corresponding_Declaration (Component_Instance));
      Namespace_Instance :=
        Instantiate_Namespace (Instance_Root, Namespace_Model);
114
115
116
117
118
119

      --  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
120
121
         Subcomponent_Instance :=
           AIN.First_Node (AIN.Subcomponents (Component_Instance));
122
123
124
125
126
127
128
129
130
131
132

         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

133
134
      if No
          (Get_First_Contained_Homonym_Instance
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
             (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
176
177
               exit when ATE.Get_Name_Of_Entity (List_Node, False) =
                 Root_System_Name;
178
179
180
181
182
183
184
185
186
               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;
187
            DE ("Please select a root system among ");
188
189
190
191
192
193
194
            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;
195
            Exit_On_Error (True, "Cannot instantiate AADL model");
196
197
198
199
200
201
202
203
204
205
206
207
         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
208
209
210
           and then
             ATE.Get_Name_Of_Entity (Root_System, False) /=
             Root_System_Name
211
         then
212
            Error_Name (1) := Root_System_Name;
213
            Error_Name (2) := ATE.Get_Name_Of_Entity (Root_System);
214
            DE ("system % is not a root system, use %");
215
            Root_System := No_Node;
216
            Exit_On_Error (True, "Cannot instantiate AADL model");
217
218
219
220
221
222
223
224
225
         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;
226
         Exit_On_Error (True, "Cannot instantiate full model, exit now");
227
228
229
230
231
232
233
234
235
236
237
238
239
240
      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
241
242
           (Instance_Root,
            Instantiate_Component (Instance_Root, Root_System));
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
         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");

273
      elsif Debug_Mode and then Get_Current_Action = Instantiate_Model then
274
275
276
277
278
279
280
281
282
283
284
285
286
287
         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;
288
      Name_Of_Declaration : Name_Id) return Node_Id
289
290
291
   is
      List_Node : Node_Id;
   begin
292
      if Name_Of_Declaration = No_Name or else Declaration_List = No_List then
293
294
295
296
297
298
         return No_Node;
      end if;

      List_Node := ATN.First_Node (Declaration_List);

      while Present (List_Node) loop
299
300
         exit when ATE.Get_Name_Of_Entity (List_Node, False) =
           Name_Of_Declaration;
301
302
303
304
305
306
307
308
309
310
311
312
         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;
313
      Name_Of_Declaration : Name_Id) return Node_Id
314
315
316
   is
      List_Node : Node_Id;
   begin
317
      if Name_Of_Declaration = No_Name or else Declaration_List = No_List then
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
         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;
337
      Name_Of_Declaration : Name_Id) return Node_Id
338
339
340
   is
      List_Node : Node_Id;
   begin
341
      if Name_Of_Declaration = No_Name or else Declaration_List = No_List then
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
         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;
366
      Name_Of_Declaration : Name_Id) return Node_Id
367
368
369
   is
      List_Node : Node_Id;
   begin
370
      if Name_Of_Declaration = No_Name or else Declaration_List = No_List then
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
         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;
395
      Declaration      : Node_Id) return Node_Id
396
397
398
399
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Homonym
400
401
          (Declaration_List,
           Get_Name_Of_Entity (Declaration, False));
402
403
404
405
406
407
408
409
   end Get_First_Homonym;

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

   function Get_First_Homonym_Instance
     (Declaration_List : List_Id;
410
      Declaration      : Node_Id) return Node_Id
411
412
413
414
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Homonym_Instance
415
416
          (Declaration_List,
           ATE.Get_Name_Of_Entity (Declaration, False));
417
418
419
420
421
422
423
424
   end Get_First_Homonym_Instance;

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

   function Get_First_Contained_Homonym
     (Declaration_List : List_Id;
425
      Declaration      : Node_Id) return Node_Id
426
427
428
429
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Contained_Homonym
430
431
          (Declaration_List,
           ATE.Get_Name_Of_Entity (Declaration, False));
432
433
434
435
436
437
438
439
   end Get_First_Contained_Homonym;

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

   function Get_First_Contained_Homonym_Instance
     (Declaration_List : List_Id;
440
      Declaration      : Node_Id) return Node_Id
441
442
443
444
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Contained_Homonym_Instance
445
446
          (Declaration_List,
           Get_Name_Of_Entity (Declaration, False));
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
   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;