ocarina-instances.adb 16.2 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
178
179
180
181
               exit when
                 (ATE.Get_Name_Of_Entity (List_Node, False) = Root_System_Name
                    or else
                    ATE.Get_Name_Of_Entity (List_Node, False, True)
                    = Root_System_Name);

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

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

      List_Node := ATN.First_Node (Declaration_List);

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

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

   function Get_First_Homonym_Instance
     (Declaration_List : List_Id;
415
      Declaration      : Node_Id) return Node_Id
416
417
418
419
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Homonym_Instance
420
421
          (Declaration_List,
           ATE.Get_Name_Of_Entity (Declaration, False));
422
423
424
425
426
427
428
429
   end Get_First_Homonym_Instance;

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

   function Get_First_Contained_Homonym
     (Declaration_List : List_Id;
430
      Declaration      : Node_Id) return Node_Id
431
432
433
434
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Contained_Homonym
435
436
          (Declaration_List,
           ATE.Get_Name_Of_Entity (Declaration, False));
437
438
439
440
441
442
443
444
   end Get_First_Contained_Homonym;

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

   function Get_First_Contained_Homonym_Instance
     (Declaration_List : List_Id;
445
      Declaration      : Node_Id) return Node_Id
446
447
448
449
   is
      pragma Assert (Present (Declaration));
   begin
      return Get_First_Contained_Homonym_Instance
450
451
          (Declaration_List,
           Get_Name_Of_Entity (Declaration, False));
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
   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;