ocarina-analyzer-aadl-semantics.adb 92 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--      O C A R I N A . A N A L Y Z E R . A A D L . S E M A N T I C S       --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 2009-2011, European Space Agency (ESA).           --
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
--                                                                          --
-- 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 Namet;
with Utils;

with Ocarina.AADL_Values;

with Ocarina.Analyzer.Messages;
with Ocarina.Analyzer.AADL.Finder;
with Ocarina.Analyzer.AADL.Queries;

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

with Ocarina.Processor.Properties;

package body Ocarina.Analyzer.AADL.Semantics is

   use Errors;
   use Namet;
   use Utils;
   use Ocarina.AADL_Values;
   use Ocarina.Analyzer.Messages;
   use Ocarina.Analyzer.AADL.Queries;
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Tree.Nutils;
   use Ocarina.ME_AADL.AADL_Tree.Entities;
   use Ocarina.ME_AADL.AADL_Tree.Entities.Properties;
   use Ocarina.Processor.Properties;

   function Check_Cycles_In_Component_Implementation
     (Node         : Node_Id;
      Initial_Node : Node_Id := No_Node)
     return Boolean;

   function Check_Cycles_In_Port_Group_Or_Component_Type
     (Node         : Node_Id;
      Initial_Node : Node_Id := No_Node)
     return Boolean;

   function Check_Cycles_In_Inversions_Of_Port_Groups
     (Node         : Node_Id;
      Initial_Node : Node_Id := No_Node)
     return Boolean;

   function Check_For_A_Unique_Initial_Mode (Node : Node_Id) return Boolean;

   function Check_Cycles_In_Subcomponents
     (Node         : Node_Id;
      Initial_Node : Node_Id := No_Node)
     return Boolean;

   function Check_Connections (Node : Node_Id) return Boolean;

   procedure Reset_Connections (Node : Node_Id);

   function Connection_End_Is_Local (Node : Node_Id) return Boolean;

   function Check_End_Types_Consistency (Node : Node_Id) return Boolean;
   --  Check that the end of the connection have compatible types

   function Check_Connection_Ends_Consistency (Node : Node_Id) return Boolean;
   --  Check if that the source is an in port, the destination is an
   --  out port, etc.

   function Check_Connection_End_Consistency
     (Connection_End      : Node_Id;
      Connection_Category : Connection_Type)
     return Boolean;
   --  Check if the connection end (source or destination) is of a
   --  consistent type regarding the connection type.

   function Check_End_Directions_Consistency (Node : Node_Id) return Boolean;
   --  Check that the connected entities are consistent with the
   --  direction of the connection.

   function Check_Property_Associations
     (Properties : List_Id;
      Container  : Node_Id)
     return Boolean;
   --  Return True if the value type of the property association
   --  Property is consistent with the one specified in the property
   --  name declaration. Container is the entity declaration in which
   --  the property association is declared

   function Check_Applies_To
     (Property  : Node_Id;
      Container : Node_Id)
     return Boolean;
   --  Return True if the property association can be applied to the
   --  container or to the entity designated by the 'applies to'
   --  statement, if any.

   function Check_Values_Of_Property_Association
     (Property_Association : Node_Id)
     return Boolean;
   --  Check wether the values of the property association are
   --  conformant with the type associated with the corresponding
   --  property name.

   function Check_Properties_Of_Component_Type
     (Component : Node_Id)
     return Boolean;

   function Check_Properties_Of_Component_Implementation
     (Component : Node_Id)
     return Boolean;

   function Check_Properties_Of_Port_Group_Type
     (Port_Group : Node_Id)
     return Boolean;

   function Check_Property_Type
     (Property_Type         : Node_Id;
      Display_Error_Message : Boolean := True)
     return Boolean;
   --  Return True if the property type is consistent, else False

   function Compare_Numbers
     (Number_1 : Node_Id;
      Number_2 : Node_Id)
     return Integer;
   --  Return -1 if Number_1 > Number_2, 1 if Number_2 > Number_1, or
   --  0 if Number_1 and Number_2 are equal. Return -2 if there is an
   --  error. If Number_1 and Number_2 are two unit number, the
   --  comparision is done with respect to the units (1sec is greater
   --  that 500ms).

   procedure Homogenize_Unit_Numbers
     (Number_1  :     Node_Id;
      Number_2  :     Node_Id;
      Literal_1 : out Node_Id;
      Literal_2 : out Node_Id);
   --  If Number_1 and Number_2 are to unit literals, convert them to
   --  the first common unit to be able to compare them. For example
   --  if Number_1 is 1sec and Number_2 is 500ms then Literal_1 is set
   --  to 1000 and Literal_2 is set to 500.

   function Convert_Single_Value_To_List
     (Property_Association : Node_Id)
     return Boolean;
   --  Edit the value of the property association in order to create a
   --  list with its single value

   function Test_Property_Type_Equivalence
     (Type_Of_Property_Name :
      Ocarina.Me_AADL.AADL_Tree.Entities.Properties.Property_Type;
      Type_Of_Property_Association :
      Ocarina.Me_AADL.AADL_Tree.Entities.Properties.Property_Type)
     return Boolean;

   function Test_Property_Value_Validity
     (Property_Type  : Node_Id;
      Property_Value : Node_Id)
     return Boolean;

   --------------------------------
   -- Check_Qualified_References --
   --------------------------------

   function Check_Qualified_References
     (Container           : Node_Id;
      Qualified_Reference : Node_Id)
     return Boolean
   is
      use Ocarina.Analyzer.AADL.Finder;

      pragma Assert (Kind (Container) = K_Package_Specification
                       or else Kind (Container) = K_Component_Type
                       or else Kind (Container) = K_Component_Implementation
                       or else Kind (Container) = K_Feature_Group_Type
                       or else Kind (Container) = K_Subcomponent
209
                       or else Kind (Container) = K_Subcomponent_Access
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
                       or else Kind (Container) = K_Port_Spec
                       or else Kind (Container) = K_Parameter
                       or else Kind (Container) = K_Connection);

      pragma Assert (Kind (Qualified_Reference) = K_Entity_Reference
             or else Kind (Qualified_Reference) = K_Identifier);

      Pack_Container : Node_Id  := No_Node;
      Success        : Boolean  := False;
   begin
      if Kind (Container) = K_Component_Type
        or else Kind (Container) = K_Component_Implementation
        or else Kind (Container) = K_Feature_Group_Type
      then
         Pack_Container := Namespace (Container);
      elsif Kind (Container) = K_Subcomponent
        or else Kind (Container) = K_Port_Spec
        or else Kind (Container) = K_Parameter
        or else Kind (Container) = K_Feature_Group_Spec
        or else Kind (Container) = K_Subcomponent_Access
        or else Kind (Container) = K_Connection
      then
         Pack_Container := Namespace (Container_Component (Container));
      elsif Kind (Container) = K_Package_Specification then
         Pack_Container := Container;
      end if;

      if Kind (Qualified_Reference) = K_Entity_Reference
        and then Name (Identifier (Pack_Container)) =
                    Name (Namespace_Identifier (Qualified_Reference))
      then
         Success := True;
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
      elsif Kind (Qualified_Reference) = K_Identifier
        and then Name (Identifier (Pack_Container)) =
                    Name (Qualified_Reference)
      then
         Success := True;
      else
         Success := Find_In_Import_Declaration (Pack_Container,
                                                Qualified_Reference);
      end if;

      if Success = False then
         Display_Analyzer_Error
           (Qualified_Reference,
            "qualified reference name not found in 'with' statements of ",
            Pack_Container);
      end if;

      return Success;
   end Check_Qualified_References;

   ----------------------
   -- Check_Applies_To --
   ----------------------

   function Check_Applies_To
     (Property  : Node_Id;
      Container : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Property) = K_Property_Association);
      pragma Assert (Present (Container));

      Pointed_Node       : Node_Id;
      Entity_Of_Property : Node_Id;
      Success            : Boolean;
   begin
      if Applies_To_Prop (Property) = No_List then
         Entity_Of_Property := Container;
      else
         Pointed_Node := First_Node (List_Items
                                       (First_Node
                                          (Applies_To_Prop (Property))));
         if Kind (Pointed_Node) = K_Array_Selection then
            Entity_Of_Property := Corresponding_Entity
              (Identifier (Pointed_Node));
         else
            Entity_Of_Property := Corresponding_Entity (Pointed_Node);
            --  XXX Here we must make this verification OK for all contained
            --  element path in 'applies' to list
         end if;
      end if;

      if Kind (Entity_Of_Property) = K_Package_Specification then
         Success := True;
      else
         Success := Property_Can_Apply_To_Entity
           (Property, Entity_Of_Property);
      end if;

      if Success then
         return True;
      else
         Display_Property_Not_Applicable (Property, Entity_Of_Property);
         return False;
      end if;
   end Check_Applies_To;

   ----------------------------------------------
   -- Check_Cycles_In_Component_Implementation --
   ----------------------------------------------

   function Check_Cycles_In_Component_Implementation
     (Node         : Node_Id;
      Initial_Node : Node_Id := No_Node)
     return Boolean
   is
      pragma Assert (Kind (Node) = K_Component_Implementation);
      pragma Assert (No (Initial_Node)
                     or else Kind (Initial_Node) = K_Component_Implementation);

      First_Extension_Node : Node_Id;
      Success              : Boolean := True;
   begin
      --  We note the first visited node in each component we
      --  scan. Thus, if we scan a component in which we find the same
      --  node_id, it means there is a cycle.

      if No (Initial_Node) then
         First_Extension_Node := Node;
      else
         First_Extension_Node := Initial_Node;
      end if;

      if First_Visited_Node (Node) = First_Extension_Node then
         Display_Cyclic_Extension (Node);
         Set_First_Visited_Node (Node, No_Node);
         return False;
      else
         Set_First_Visited_Node (Node, First_Extension_Node);
      end if;

      if Parent (Node) /= No_Node
        and then Get_Referenced_Entity (Parent (Node)) /= No_Node
      then
         Success := Check_Cycles_In_Component_Implementation
           (Get_Referenced_Entity (Parent (Node)),
            First_Extension_Node);
      else
         Success := Check_Cycles_In_Port_Group_Or_Component_Type
           (Corresponding_Entity (Component_Type_Identifier (Node)));
      end if;

      Set_First_Visited_Node (Node, No_Node);
      return Success;
   end Check_Cycles_In_Component_Implementation;

   -----------------------------------------------
   -- Check_Cycles_In_Inversions_Of_Port_Groups --
   -----------------------------------------------

   function Check_Cycles_In_Inversions_Of_Port_Groups
     (Node         : Node_Id;
      Initial_Node : Node_Id := No_Node)
     return Boolean
   is
      pragma Assert (Kind (Node) = K_Feature_Group_Type);
      pragma Assert (No (Initial_Node)
                     or else Kind (Initial_Node) = K_Feature_Group_Type);

      First_Inversion_Node : Node_Id;
      Success              : Boolean := True;
   begin
      --  We note the first visited node in each component we
      --  scan. Thus, if we scan a component in which we find the same
      --  node id, it means there is a cycle.

      if No (Initial_Node) then
         First_Inversion_Node := Node;
      else
         First_Inversion_Node := Initial_Node;
      end if;

      if First_Visited_Node (Node) = First_Inversion_Node then
         Display_Cyclic_Inversion (Node);
         Set_First_Visited_Node (Node, No_Node);
         return False;
      else
         Set_First_Visited_Node (Node, First_Inversion_Node);
      end if;

      if Inverse_Of (Node) /= No_Node
        and then Get_Referenced_Entity (Inverse_Of (Node)) /= No_Node
      then
         Success := Check_Cycles_In_Inversions_Of_Port_Groups
           (Get_Referenced_Entity (Inverse_Of (Node)),
            First_Inversion_Node);
      end if;

      Set_First_Visited_Node (Node, No_Node);
      return Success;
   end Check_Cycles_In_Inversions_Of_Port_Groups;

   --------------------------------------------------
   -- Check_Cycles_In_Port_Group_Or_Component_Type --
   --------------------------------------------------

   function Check_Cycles_In_Port_Group_Or_Component_Type
     (Node         : Node_Id;
      Initial_Node : Node_Id := No_Node)
     return Boolean
   is
      pragma Assert (Kind (Node) = K_Component_Type
                     or else Kind (Node) = K_Feature_Group_Type);
      pragma Assert (No (Initial_Node)
                     or else Kind (Initial_Node) = K_Component_Type
                     or else Kind (Initial_Node) = K_Feature_Group_Type);

      First_Extension_Node : Node_Id;
      Success              : Boolean := True;
   begin
      --  We note the first visited node in each component we
      --  scan. Thus, if we scan a component in which we find the same
      --  node id, it means there is a cycle.

      if No (Initial_Node) then
         First_Extension_Node := Node;
      else
         First_Extension_Node := Initial_Node;
      end if;

      if First_Visited_Node (Node) = First_Extension_Node then
         Display_Cyclic_Extension (Node);
         Set_First_Visited_Node (Node, No_Node);
         return False;
      else
         Set_First_Visited_Node (Node, First_Extension_Node);
      end if;

      if Parent (Node) /= No_Node
        and then Get_Referenced_Entity (Parent (Node)) /= No_Node
      then
         Success := Check_Cycles_In_Port_Group_Or_Component_Type
           (Get_Referenced_Entity (Parent (Node)),
            First_Extension_Node);
      end if;

      Set_First_Visited_Node (Node, No_Node);
      return Success;
   end Check_Cycles_In_Port_Group_Or_Component_Type;

   -----------------------------------
   -- Check_Cycles_In_Subcomponents --
   -----------------------------------

   function Check_Cycles_In_Subcomponents
     (Node         : Node_Id;
      Initial_Node : Node_Id := No_Node)
     return Boolean
   is
      pragma Assert (Kind (Node) = K_Component_Implementation
                     or else Kind (Node) = K_Component_Type);

      List_Node           : Node_Id;
      First_Instance_Node : Node_Id;
      Success             : Boolean := True;
   begin
      if Kind (Node) = K_Component_Implementation then
         if No (Initial_Node) then
            First_Instance_Node := Node;
         else
            First_Instance_Node := Initial_Node;
         end if;

         if First_Visited_Node (Node) = First_Instance_Node then
            Display_Cyclic_Subcomponents (Node);
            Set_First_Visited_Node (Node, No_Node);
            return False;
         else
            Set_First_Visited_Node (Node, First_Instance_Node);
         end if;

         if not Is_Empty (Subcomponents (Node)) then
            List_Node := First_Node (Subcomponents (Node));

            while Present (List_Node) loop
               if Entity_Ref (List_Node) /= No_Node
                 and then Get_Referenced_Entity
                 (Entity_Ref (List_Node)) /= No_Node
               then
                  Success := Success
                    and then Check_Cycles_In_Subcomponents
                    (Get_Referenced_Entity (Entity_Ref (List_Node)),
                     First_Instance_Node);
               end if;

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

         Set_First_Visited_Node (Node, No_Node);
      end if;

      return Success;
   end Check_Cycles_In_Subcomponents;

   ----------------------
   -- Check_Connection --
   ----------------------

   function Check_Connection (Node : Node_Id) return Boolean is
      pragma Assert (Kind (Node) = K_Connection);

      Success : Boolean := True;
   begin
      if Is_Refinement (Node) then
         return True;
      end if;

      if not Check_Connection_End_Consistency
        (Connection_End      => Get_Referenced_Entity (Destination (Node)),
         Connection_Category => Connection_Type'Val (Category (Node)))
      then
         DAE (Node1    => Destination (Node),
              Message1 => " points to ",
              Node2    => Get_Referenced_Entity (Destination (Node)),
              Message2 => ", which is not of a proper type");
         Success := False;
      end if;

      if not Check_Connection_End_Consistency
        (Connection_End => Get_Referenced_Entity (Source (Node)),
         Connection_Category => Connection_Type'Val (Category (Node)))
      then
         DAE (Node1 => Source (Node),
              Message1 => " points to ",
              Node2 => Get_Referenced_Entity (Source (Node)),
              Message2 => ", which is not of a proper type");
         Success := False;
      end if;

      if Success then
         Success := Check_End_Types_Consistency (Node)
           and then Check_Connection_Ends_Consistency (Node)
           and then Check_End_Directions_Consistency (Node);
      end if;

      return Success;
   end Check_Connection;

   ---------------------------------------
   -- Check_Connection_Ends_Consistency --
   ---------------------------------------

   function Check_Connection_Ends_Consistency
     (Node : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Node) = K_Connection);

      Connection_Source      : constant Node_Id := Get_Referenced_Entity
        (Source (Node));
      Connection_Destination : constant Node_Id := Get_Referenced_Entity
        (Destination (Node));

      Same_Kind                                : Boolean := False;
      Data_Port_And_Parameter                  : Boolean := False;
      Data_And_Require_Data_Access             : Boolean := False;
      Provide_Data_Access_And_Data             : Boolean := False;
      Provide_Bus_Access_And_Bus               : Boolean := False;
      Bus_And_Require_Bus_Access               : Boolean := False;
      Provide_Subprogram_Access_And_Subprogram : Boolean := False;
      Subprogram_And_Require_Subprogram_Access : Boolean := False;
   begin
      pragma Assert
        (Kind (Connection_Source) = K_Port_Spec
         or else Kind (Connection_Source) = K_Parameter
         or else Kind (Connection_Source) = K_Feature_Group_Spec
         or else Kind (Connection_Source) = K_Subcomponent_Access
         or else Kind (Connection_Source) = K_Subcomponent
         or else Kind (Connection_Destination) = K_Subprogram_Call);

      pragma Assert
        (Kind (Connection_Destination) = K_Port_Spec
         or else Kind (Connection_Destination) = K_Parameter
         or else Kind (Connection_Destination) = K_Feature_Group_Spec
         or else Kind (Connection_Destination) = K_Subcomponent_Access
         or else Kind (Connection_Destination) = K_Subcomponent
         or else Kind (Connection_Destination) = K_Subprogram_Call);

      Same_Kind := Kind (Connection_Source) = Kind (Connection_Destination);

      Data_Port_And_Parameter :=
        (Kind (Connection_Source) = K_Port_Spec
         and then Is_Data (Connection_Source)
         and then Kind (Connection_Destination) = K_Parameter)
        or else (Kind (Connection_Destination) = K_Port_Spec
                 and then Is_Data (Connection_Destination)
                 and then Kind (Connection_Source) = K_Parameter);

      Data_And_Require_Data_Access :=
        Kind (Connection_Source) = K_Subcomponent
        and then Component_Category'Val (Category
                                         (Connection_Source)) = CC_Data
        and then Kind (Connection_Destination) = K_Subcomponent_Access
        and then Component_Category'Val
        (Subcomponent_Category (Connection_Destination)) = CC_Data;

      Provide_Data_Access_And_Data :=
        (Kind (Connection_Destination) = K_Subcomponent_Access
         and then Component_Category'Val
         (Subcomponent_Category (Connection_Destination)) = CC_Data
         and then Is_Provided (Connection_Destination)
         and then Kind (Connection_Source) = K_Subcomponent
         and then Component_Category'Val
         (Category (Connection_Source)) = CC_Data);

      Bus_And_Require_Bus_Access :=
        Kind (Connection_Source) = K_Subcomponent
        and then Component_Category'Val (Category
                                         (Connection_Source)) = CC_Bus
        and then Kind (Connection_Destination) = K_Subcomponent_Access
        and then Component_Category'Val
        (Subcomponent_Category (Connection_Destination)) = CC_Bus;

      Provide_Bus_Access_And_Bus :=
        (Kind (Connection_Destination) = K_Subcomponent_Access
         and then Component_Category'Val
        (Subcomponent_Category (Connection_Destination)) = CC_Bus
         and then Is_Provided (Connection_Destination)
         and then Kind (Connection_Source) = K_Subcomponent
         and then Component_Category'Val
         (Category (Connection_Source)) = CC_Bus);

      Subprogram_And_Require_Subprogram_Access :=
        Kind (Connection_Source) = K_Subcomponent
        and then Component_Category'Val (Category
                                         (Connection_Source)) = CC_Subprogram
        and then Kind (Connection_Destination) = K_Subcomponent_Access
        and then Component_Category'Val
        (Subcomponent_Category (Connection_Destination)) = CC_Subprogram;

      Provide_Subprogram_Access_And_Subprogram :=
        (Kind (Connection_Destination) = K_Subcomponent_Access
         and then Component_Category'Val
        (Subcomponent_Category (Connection_Destination)) = CC_Subprogram
         and then Is_Provided (Connection_Destination)
         and then Kind (Connection_Source) = K_Subcomponent
         and then Component_Category'Val
         (Category (Connection_Source)) = CC_Subprogram);

      --  We assume that the only possibility is to connect a
      --  subcomponent into a component requires. The contrary
      --  (connecting a component requires into a subcomponent) is
      --  forbidden.

      if not (Bus_And_Require_Bus_Access
              or else Provide_Bus_Access_And_Bus
              or else Provide_Data_Access_And_Data
              or else Data_And_Require_Data_Access
              or else Subprogram_And_Require_Subprogram_Access
              or else Provide_Subprogram_Access_And_Subprogram
              or else Data_Port_And_Parameter
              or else Same_Kind)
      then
         DAE (Loc      => Ocarina.Me_AADL.AADL_Tree.Nodes.Loc (Node),
              Node1    => Get_Referenced_Entity (Source (Node)),
              Message1 => " and ",
              Node2    => Get_Referenced_Entity (Destination (Node)),
              Message2 => " are not compatible");
         return False;
      else
         return True;
      end if;
   end Check_Connection_Ends_Consistency;

   --------------------------------------
   -- Check_Connection_End_Consistency --
   --------------------------------------

   function Check_Connection_End_Consistency
     (Connection_End      : Node_Id;
      Connection_Category : Connection_Type)
     return Boolean
   is
      pragma Assert
        (Kind (Connection_End) = K_Port_Spec
         or else Kind (Connection_End) = K_Parameter
         or else Kind (Connection_End) = K_Feature_Group_Spec
         or else Kind (Connection_End) = K_Subcomponent_Access
         or else Kind (Connection_End) = K_Subcomponent
         or else Kind (Connection_End) = K_Subprogram_Call);

      Success : Boolean := True;
   begin
      case Connection_Category is
         when CT_Error =>
            Success := False;

         when CT_Port_Connection
           | CT_Access_Subprogram_Group
703
704
           | CT_Access_Virtual_Bus
           | CT_Access =>
705
706
707
            Success := True;
            --  XXX Incomplete TODO

708
709
         when CT_Data
           | CT_Data_Delayed =>
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
            Success := Kind (Connection_End) = K_Port_Spec
              and then Is_Data (Connection_End)
              and then not Is_Event (Connection_End);

         when CT_Event =>
            Success := Kind (Connection_End) = K_Port_Spec
              and then not Is_Data (Connection_End)
              and then Is_Event (Connection_End);

         when CT_Event_Data =>
            Success := Kind (Connection_End) = K_Port_Spec
              and then Is_Data (Connection_End)
              and then Is_Event (Connection_End);

         when CT_Feature_Group =>
            Success := Kind (Connection_End) = K_Feature_Group_Spec;

         when CT_Parameter =>
            --  Parameter connections can connect (event) data ports
            --  to subprogram parameters, since parameter have the
            --  same semantic as (event) data ports.

            Success :=  Kind (Connection_End) = K_Parameter
              or else (Kind (Connection_End) = K_Port_Spec
                       and then Is_Data (Connection_End));

         when CT_Access_Bus =>
            Success := (Kind (Connection_End) = K_Subcomponent_Access
                         and then Component_Category'Val
                         (Subcomponent_Category (Connection_End)) = CC_Bus)
              or else (Kind (Connection_End) = K_Subcomponent
                       and then Component_Category'Val
                       (Category (Connection_End)) = CC_Bus);

         when CT_Access_Data =>
            Success := (Kind (Connection_End) = K_Subcomponent_Access
                        and then Component_Category'Val
                        (Subcomponent_Category (Connection_End)) = CC_Data)
              or else (Kind (Connection_End) = K_Subcomponent
                       and then Component_Category'Val
                       (Category (Connection_End)) = CC_Data);

         when CT_Access_Subprogram =>
            Success :=
              (Kind (Connection_End) = K_Subcomponent_Access
               and then Component_Category'Val
               (Subcomponent_Category (Connection_End)) = CC_Subprogram)
              or else (Kind (Connection_End) = K_Subcomponent
                       and then Component_Category'Val
                       (Category (Connection_End)) = CC_Subprogram);
      end case;

      return Success;
   end Check_Connection_End_Consistency;

   -----------------------
   -- Check_Connections --
   -----------------------

   function Check_Connections (Node : Node_Id) return Boolean is

      pragma Assert (Kind (Node) = K_Component_Implementation);

      List_Node : Node_Id;
      Success   : Boolean := True;
   begin
      if not Is_Empty (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Node)) then
         List_Node := First_Node
           (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Node));

         while Present (List_Node) loop
            Success := Check_Connection (List_Node) and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      --  Check whether there are duplicate connections

      if Success and then
        not Is_Empty (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Node))
      then
         List_Node := First_Node
           (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Node));

         while Present (List_Node) loop
            declare
               Src    : constant Node_Id := Source (List_Node);
               Dst    : constant Node_Id := Destination (List_Node);
               N      : Node_Id;
               I_Name : Name_Id;
               Info   : Nat;
            begin
               --  We use name_buffer hash codes for efficiency. First
               --  we inster the following string : Node%Path
               --  (Src)%Path (Dst)%dup%cnx%check and then we compute
               --  its byte info. If we find a non-zero info, this
               --  means the connection is duplicate and the info
               --  poits to the first met connection. Finally, we set
               --  the info of the string to point to the current
               --  connection.

               Set_Nat_To_Name_Buffer (Nat (Node));
               Add_Char_To_Name_Buffer ('%');

               N := First_Node (Path (Src));
               while Present (N) loop
                  Add_Nat_To_Name_Buffer
                    (Nat (Corresponding_Entity (Item (N))));
                  Add_Char_To_Name_Buffer ('%');

                  N := Next_Node (N);
               end loop;

               N := First_Node (Path (Dst));
               while Present (N) loop
                  Add_Nat_To_Name_Buffer
                    (Nat (Corresponding_Entity (Item (N))));
                  Add_Char_To_Name_Buffer ('%');

                  N := Next_Node (N);
               end loop;

               Add_Str_To_Name_Buffer ("dup%cnx%check");
               I_Name := Name_Find;
               Info := Get_Name_Table_Info (I_Name);

               if Info /= 0 then
                  --  Check whether the two connections have common
                  --  modes.

                  if Have_Common_Statements (In_Modes (List_Node),
                                             In_Modes (Node_Id (Info)))
                  then
                     Error_Loc (1) := Loc (List_Node);
                     Error_Loc (2) := Loc (Node_Id (Info));

                     DE ("This connection is a duplication of the"
                         & " connection declared!");

                     Success := False;
                  end if;
               else
                  Set_Name_Table_Info (I_Name, Nat (List_Node));
               end if;
            end;

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

      return Success;
   end Check_Connections;

   --------------------------------------
   -- Check_End_Directions_Consistency --
   --------------------------------------

   function Check_End_Directions_Consistency (Node : Node_Id) return Boolean is

      pragma Assert (Kind (Node) = K_Connection);

      Connection_Source      : constant Node_Id := Get_Referenced_Entity
        (Source (Node));
      Source_Is_Local        : constant Boolean := Connection_End_Is_Local
        (Source (Node));
      Connection_Destination : constant Node_Id := Get_Referenced_Entity
        (Destination (Node));
      Destination_Is_Local   : constant Boolean := Connection_End_Is_Local
        (Destination (Node));

      Directions : Boolean := False;
   begin
      pragma Assert
        (Kind (Connection_Source) = K_Port_Spec
         or else Kind (Connection_Source) = K_Parameter
         or else Kind (Connection_Source) = K_Feature_Group_Spec
         or else Kind (Connection_Source) = K_Subcomponent_Access
         or else Kind (Connection_Source) = K_Subcomponent);

      pragma Assert
        (Kind (Connection_Destination) = K_Port_Spec
         or else Kind (Connection_Destination) = K_Parameter
         or else Kind (Connection_Destination) = K_Feature_Group_Spec
         or else Kind (Connection_Destination) = K_Subcomponent_Access
         or else Kind (Connection_Destination) = K_Subcomponent);

      case Kind (Connection_Destination) is
         when K_Port_Spec | K_Parameter =>

            --  We do not check strict correspondence between port
            --  directions: an in port can be connected to an in/out
            --  port. This seems strange, but the examples provided
            --  with Osate accept this situation. After all, it can
            --  make sense: an in/out port could have two different
            --  connections.

            Directions := (not Source_Is_Local
                           and then not Destination_Is_Local
                           and then Is_Out (Connection_Source)
                           and then Is_In (Connection_Destination))
              or else (Source_Is_Local
                       and then not Destination_Is_Local
                       and then Is_In (Connection_Source)
                       and then Is_In (Connection_Destination))
              or else (not Source_Is_Local
                       and then Destination_Is_Local
                       and then Is_Out (Connection_Source)
                       and then Is_Out (Connection_Destination))
              or else (Source_Is_Local
                       and then Destination_Is_Local
                       and then Is_In (Connection_Source)
                       and then Is_Out (Connection_Destination))
              or else (Is_In (Connection_Source)
                       and then Is_Out (Connection_Source)
                       and then Is_Out (Connection_Destination)
                       and then Is_In (Connection_Destination));
            --  XXX The latest test may be redudant with the previous
            --  ones

         when K_Feature_Group_Spec =>
            Directions := True;
            --  There is no direction for a port group

         when K_Subcomponent_Access =>
            Directions :=
              (not Source_Is_Local
               and then not Destination_Is_Local
               and then not Is_Provided (Connection_Destination)
               and then Kind (Connection_Source) = K_Subcomponent_Access
               and then Is_Provided (Connection_Source))
              or else
              (Source_Is_Local
               and then not Destination_Is_Local
               and then not Is_Provided (Connection_Destination)
               and then ((Kind (Connection_Source) = K_Subcomponent_Access
                          and then not Is_Provided (Connection_Source))
                         or else Kind (Connection_Source) = K_Subcomponent))
              or else
              (not Source_Is_Local
               and then Destination_Is_Local
               and then Is_Provided (Connection_Destination)
               and then Kind (Connection_Source) = K_Subcomponent_Access
               and then Is_Provided (Connection_Source))
              or else
              (Source_Is_Local
               and then Destination_Is_Local
               and then Is_Provided (Connection_Destination)
               and then Kind (Connection_Source) = K_Subcomponent);

         when K_Subcomponent =>
            Directions := False;

         when others =>
            Directions := True;
      end case;

      if not Directions then
         DAE (Loc      => Ocarina.Me_AADL.AADL_Tree.Nodes.Loc (Node),
              Node1    => Get_Referenced_Entity (Source (Node)),
              Message1 => " and ",
              Node2    => Get_Referenced_Entity (Destination (Node)),
              Message2 => " do not have compatible directions");
         Directions := False;
      end if;

      return Directions;
   end Check_End_Directions_Consistency;

   ---------------------------------
   -- Check_End_Types_Consistency --
   ---------------------------------

   function Check_End_Types_Consistency (Node : Node_Id) return Boolean is

      pragma Assert (Kind (Node) = K_Connection);

      Source_Node          : constant Node_Id := Get_Referenced_Entity
        (Source (Node));
      Destination_Node     : constant Node_Id := Get_Referenced_Entity
        (Destination (Node));
      Source_Is_Local      : constant Boolean := Connection_End_Is_Local
        (Source (Node));
      Destination_Is_Local : constant Boolean := Connection_End_Is_Local
        (Destination (Node));

      Source_Type      : Node_Id;
      Destination_Type : Node_Id;

      Success : Boolean := True;
      No_Type : Boolean := True;
   begin
      --  For port and parameter connections, the consistency is
      --  ensured if the associated data are of the same type, or if
      --  one is an implementation of the other one. For subcomponent
      --  accesses, the provided subcomponent must be of the same type
      --  or be an implementation of the required one. Same thing for
      --  subprogram as features. Ends of a Port group connection must
      --  be the inverse one of the other.

      if Present (Entity_Ref (Source_Node)) then
         Source_Type := Get_Referenced_Entity (Entity_Ref (Source_Node));
      else
         Source_Type := No_Node;
      end if;

      if Present (Entity_Ref (Destination_Node)) then
         Destination_Type :=
           Get_Referenced_Entity (Entity_Ref (Destination_Node));
      else
         Destination_Type := No_Node;
      end if;

      No_Type := No (Source_Type) or else No (Destination_Type);

      if No_Type then
         return True;

         --  If one of the two ends has no type, there is no use
         --  checking anything.
      end if;

      case Connection_Type'Val (Category (Node)) is
         when CT_Error =>
            Success := False;

         when CT_Port_Connection
           | CT_Access_Subprogram_Group
1037
1038
           | CT_Access_Virtual_Bus
           | CT_Access =>
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
            Success := True;
            --  XXX incomplete TODO

         when CT_Data
           | CT_Data_Delayed
           | CT_Event_Data
           | CT_Parameter =>
            if Source_Type = Destination_Type then
               Success := True;
            else
               DAE (Loc      => Loc (Node),
                    Node1    => Source (Node),
                    Message1 => " and ",
                    Node2    => Destination (Node),
                    Message2 => " do not have compatible types");
               Success := False;
            end if;

         when CT_Event =>
            Success := True;

         when CT_Feature_Group =>
            if Source_Is_Local = Destination_Is_Local then
               Success := (Present (Inverse_Of (Source_Type))
                           and then Get_Referenced_Entity
                           (Inverse_Of (Source_Type)) = Destination_Type)
                 or else (Present (Inverse_Of (Destination_Type))
                          and then Get_Referenced_Entity
                          (Inverse_Of (Destination_Type)) = Source_Type);
            else
               Success := (Source_Type = Destination_Type);
            end if;

            if not Success then
               DAE (Loc      => Loc (Node),
                    Node1    => Source (Node),
                    Message1 => " and ",
                    Node2    => Destination (Node),
                    Message2 => " do not have compatible types");
               Success := False;
            end if;

            --  XXX This comparison is too basic. We should compare
            --  the content of the port groups instead

         when CT_Access_Bus
           | CT_Access_Data
           | CT_Access_Subprogram =>
            if Source_Type = Destination_Type then
               Success := True;
            else
               DAE (Loc      => Loc (Node),
                    Node1    => Source (Node),
                    Message1 => " and ",
                    Node2    => Destination (Node),
                    Message2 => " do not have compatible types");
               Success := False;
            end if;
      end case;

      return Success;
   end Check_End_Types_Consistency;

   -------------------------------------
   -- Check_For_A_Unique_Initial_Mode --
   -------------------------------------

   function Check_For_A_Unique_Initial_Mode (Node : Node_Id) return Boolean is

      pragma Assert (Kind (Node) = K_Component_Implementation);

      First_Initial_Mode : Node_Id := No_Node;
      Number_Of_Modes    : Integer := 0;
      Component          : Node_Id := Node;
      Success            : Boolean := True;
      List_Node          : Node_Id;
   begin
      while Present (Component) loop
         if not Is_Empty (Modes (Component)) then
            List_Node := First_Node (Modes (Component));

            while Present (List_Node) loop
               if Kind (List_Node) = K_Mode then
                  Number_Of_Modes := Number_Of_Modes + 1;

                  if Is_Initial (List_Node) then
                     --  The initial mode may be overridden by the
                     --  component.

                     if No (First_Initial_Mode) then
                        First_Initial_Mode := List_Node;
                     else
                        if Component = Node then
                           Display_Conflicting_Initial_Modes
                             (List_Node, First_Initial_Mode);
                        else
                           Display_Conflicting_Initial_Modes
                             (First_Initial_Mode, List_Node);
                        end if;
                        Success := False;
                     end if;
                  end if;
               end if;

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

         if Present (Parent (Component)) then
            Component := Get_Referenced_Entity (Parent (Component));
         else
            Component := No_Node;
         end if;
      end loop;

      if No (First_Initial_Mode) and then Number_Of_Modes /= 0 then
         DAE (Node1    => Node,
              Message1 => " has no initial mode");
         Success := False;
      end if;

      return Success;
   end Check_For_A_Unique_Initial_Mode;

   --------------------------------------------------
   -- Check_Properties_Of_Component_Implementation --
   --------------------------------------------------

   function Check_Properties_Of_Component_Implementation
     (Component : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);

      Success        : Boolean := True;
      List_Node      : Node_Id;
      Call_List_Node : Node_Id;
   begin
      --  Type refinements

      if Refines_Type (Component) /= No_List then
         List_Node := First_Node (Refines_Type (Component));

         while Present (List_Node) loop
            Success := Check_Property_Associations
              (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
               List_Node)
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      --  Subcomponents

      if Subcomponents (Component) /= No_List then
         List_Node := First_Node (Subcomponents (Component));

         while Present (List_Node) loop
            Success := Check_Property_Associations
              (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
               List_Node)
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      --  Call sequences
      --  Some call sequences are anonymous

      if Calls (Component) /= No_List then
         List_Node := First_Node (Calls (Component));

         while Present (List_Node) loop
            if Subprogram_Calls (List_Node) /= No_List then
               Call_List_Node := First_Node (Subprogram_Calls (List_Node));

               while Present (Call_List_Node) loop
                  Success := Check_Property_Associations
                    (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties
                       (Call_List_Node),
                     Call_List_Node)
                    and then Success;
                  Call_List_Node := Next_Node (Call_List_Node);
               end loop;
            end if;

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

      --  Connections
      --  Some connections are anonymous

      if Ocarina.Me_AADL.AADL_Tree.Nodes.Connections
        (Component) /= No_List then
         List_Node := First_Node
           (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Component));

         while Present (List_Node) loop
            Success := Check_Property_Associations
              (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
               List_Node)
              and then Success;

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

      --  Flows

      if Flows (Component) /= No_List then
         List_Node := First_Node (Flows (Component));

         while Present (List_Node) loop
            Success := Check_Property_Associations
              (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
               List_Node)
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      --  Modes

      if Modes (Component) /= No_List then
         List_Node := First_Node (Modes (Component));

         while Present (List_Node) loop
            if Kind (List_Node) = K_Mode then
               Success := Check_Property_Associations
                 (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
                  List_Node)
                 and then Success;
            end if;

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

      --  Properties

      Success := Check_Property_Associations
        (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (Component), Component)
        and then Success;

      return Success;
   end Check_Properties_Of_Component_Implementation;

   ----------------------------------------
   -- Check_Properties_Of_Component_Type --
   ----------------------------------------

   function Check_Properties_Of_Component_Type
     (Component : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Component) = K_Component_Type);

      Success   : Boolean := True;
      List_Node : Node_Id;
   begin
      --  Features

      if Features (Component) /= No_List then
         List_Node := First_Node (Features (Component));

         while Present (List_Node) loop
            Success := Check_Property_Associations
              (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
               List_Node)
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      --  Flows

      if Flows (Component) /= No_List then
         List_Node := First_Node (Flows (Component));

         while Present (List_Node) loop
            Success := Check_Property_Associations
              (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
               List_Node)
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      --  Properties

      Success := Check_Property_Associations
        (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (Component), Component)
        and then Success;

      return Success;
   end Check_Properties_Of_Component_Type;

   -----------------------------------------
   -- Check_Properties_Of_Port_Group_Type --
   -----------------------------------------

   function Check_Properties_Of_Port_Group_Type
     (Port_Group : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Port_Group) = K_Feature_Group_Type);

      Success   : Boolean := True;
      List_Node : Node_Id;
   begin
      --  Features

      if Features (Port_Group) /= No_List then
         List_Node := First_Node (Features (Port_Group));

         while Present (List_Node) loop
            Success := Check_Property_Associations
              (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
               List_Node)
              and then Success;
            List_Node := Next_Node (List_Node);
         end loop;
      end if;

      --  Properties

      Success := Check_Property_Associations
        (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (Port_Group), Port_Group)
        and then Success;

      return Success;
   end Check_Properties_Of_Port_Group_Type;

   ---------------------------------
   -- Check_Property_Associations --
   ---------------------------------

   function Check_Property_Associations
     (Properties : List_Id;
      Container  : Node_Id)
     return Boolean
   is
      pragma Assert (Present (Container));

      Success   : Boolean := True;
      List_Node : Node_Id;
   begin
      if Properties /= No_List then
         List_Node := First_Node (Properties);

         while Present (List_Node) loop
            pragma Assert (Kind (List_Node) = K_Property_Association);

            Success := Check_Applies_To (List_Node, Container)
              and then Check_Values_Of_Property_Association (List_Node)
              and then Success;

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

      return Success;
   end Check_Property_Associations;

   -------------------------
   -- Check_Property_Type --
   -------------------------

   function Check_Property_Type
     (Property_Type         : Node_Id;
      Display_Error_Message : Boolean := True)
     return Boolean
   is
      pragma Assert
        (Kind (Property_Type) = K_Property_Type_Declaration
         or else Kind (Property_Type) = K_Property_Type
         or else Kind (Property_Type) = K_Integer_Type
         or else Kind (Property_Type) = K_Real_Type
         or else Kind (Property_Type) = K_Enumeration_Type
         or else Kind (Property_Type) = K_Boolean_Type
         or else Kind (Property_Type) = K_String_Type
         or else Kind (Property_Type) = K_Range_Type
         or else Kind (Property_Type) = K_Reference_Type
         or else Kind (Property_Type) = K_Classifier_Type
         or else Kind (Property_Type) = K_Unique_Property_Type_Identifier
         or else Kind (Property_Type) = K_Record_Type);

      Type_Designator : Node_Id;
      Success         : Boolean := True;
   begin
      case Kind (Property_Type) is
         when K_Property_Type_Declaration =>
            Type_Designator := Property_Type_Designator (Property_Type);
         when K_Property_Type =>
            Type_Designator := Expanded_Type_Designator (Property_Type);
         when others =>
            Type_Designator := Property_Type;
      end case;

      if Present (Type_Designator) then
         case Kind (Type_Designator) is
            when K_Integer_Type
              | K_Real_Type =>
               if Present (Type_Range (Type_Designator))
                 and then Present (Lower_Bound (Type_Range (Type_Designator)))
                 and then Kind (Lower_Bound (Type_Range (Type_Designator)))
                 = K_Literal
                 and then Present (Upper_Bound (Type_Range (Type_Designator)))
                 and then Kind (Upper_Bound (Type_Range (Type_Designator)))
                 = K_Literal
               then
                  --  We only check the types that are completely
                  --  defined. Typically, the types that have been
                  --  expanded.

                  Success := Compare_Numbers
                    (Lower_Bound (Type_Range (Type_Designator)),
                     Upper_Bound (Type_Range (Type_Designator))) >= 0;
               end if;

            when K_Range_Type =>
               Success := Check_Property_Type
                 (Number_Type (Type_Designator), False);

            when others =>
               Success := True;
         end case;
      end if;

      if Display_Error_Message and then not Success then
         Display_Inconsistent_Property_Type (Property_Type);
      end if;

      return Success;
   end Check_Property_Type;

   -----------------------
   -- Reset_Connections --
   -----------------------

   procedure Reset_Connections (Node : Node_Id)
   is
      pragma Assert (Kind (Node) = K_Component_Implementation);

      List_Node : Node_Id;
   begin
      --  Reset connections info on name table

      if not Is_Empty (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Node)) then
         List_Node := First_Node
           (Ocarina.Me_AADL.AADL_Tree.Nodes.Connections (Node));

         while Present (List_Node) loop
            declare
               Src    : constant Node_Id := Source (List_Node);
               Dst    : constant Node_Id := Destination (List_Node);
               N      : Node_Id;
               I_Name : Name_Id;
            begin
               --  We use name_buffer hash codes for efficiency. First
               --  we inster the following string : Node%Path
               --  (Src)%Path (Dst)%dup%cnx%check and then we compute
               --  its byte info. If we find a non-zero info, this
               --  means the connection is duplicate and the info
               --  poits to the first met connection. Finally, we set
               --  the info of the string to point to the current
               --  connection.

               Set_Nat_To_Name_Buffer (Nat (Node));
               Add_Char_To_Name_Buffer ('%');

               N := First_Node (Path (Src));
               while Present (N) loop
                  Add_Nat_To_Name_Buffer
                    (Nat (Corresponding_Entity (Item (N))));
                  Add_Char_To_Name_Buffer ('%');

                  N := Next_Node (N);
               end loop;

               N := First_Node (Path (Dst));
               while Present (N) loop
                  Add_Nat_To_Name_Buffer
                    (Nat (Corresponding_Entity (Item (N))));
                  Add_Char_To_Name_Buffer ('%');

                  N := Next_Node (N);
               end loop;

               Add_Str_To_Name_Buffer ("dup%cnx%check");
               I_Name := Name_Find;
               Set_Name_Table_Info (I_Name, 0);
            end;

            List_Node := Next_Node (List_Node);
         end loop;
      end if;
   end Reset_Connections;

   ----------------------
   -- Reset_Connection --
   ----------------------

   procedure Reset_All_Connections (Root : Node_Id) is
      pragma Assert (Kind (Root) = K_AADL_Specification);

      List_Node         : Node_Id;
      Package_List_Node : Node_Id;
   begin
      if not Is_Empty (Declarations (Root)) then
         List_Node := First_Node (Declarations (Root));

         while Present (List_Node) loop
            case Kind (List_Node) is
               when  K_Component_Implementation =>
                  Reset_Connections (List_Node);

               when K_Package_Specification =>
                  if not Is_Empty (Declarations (List_Node)) then
                     Package_List_Node :=
                       First_Node (Declarations (List_Node));

                     while Present (Package_List_Node) loop
                        case Kind (Package_List_Node) is
                           when  K_Component_Implementation =>
                              Reset_Connections (Package_List_Node);

                           when others =>
                              null;
                        end case;

                        Package_List_Node := Next_Node (Package_List_Node);
                     end loop;
                  end if;

               when others =>
                  null;
            end case;

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

   end Reset_All_Connections;

   -----------------------------------
   -- Check_Semantics_In_Components --
   -----------------------------------

   function Check_Semantics_In_Components (Root : Node_Id) return Boolean is

      pragma Assert (Kind (Root) = K_AADL_Specification);

      Success           : Boolean := True;
      List_Node         : Node_Id;
      Package_List_Node : Node_Id;
   begin
      if not Is_Empty (Declarations (Root)) then
         List_Node := First_Node (Declarations (Root));

         while Present (List_Node) loop
            case Kind (List_Node) is
               when  K_Component_Implementation =>
                  Success := Check_For_A_Unique_Initial_Mode (List_Node)
                    and then Check_Cycles_In_Subcomponents (List_Node)
                    and then Check_Connections (List_Node)
                    and then Success;

               when K_Package_Specification =>
                  if not Is_Empty (Declarations (List_Node)) then
                     Package_List_Node :=
                       First_Node (Declarations (List_Node));

                     while Present (Package_List_Node) loop
                        case Kind (Package_List_Node) is
                           when  K_Component_Implementation =>
                              Success :=
                                Check_For_A_Unique_Initial_Mode
                                (Package_List_Node)
                                and then Check_Cycles_In_Subcomponents
                                (Package_List_Node)
                                and then Check_Connections (Package_List_Node)
                                and then Success;

                           when others =>
                              null;
                        end case;

                        Package_List_Node := Next_Node (Package_List_Node);
                     end loop;
                  end if;

               when others =>
                  null;
            end case;

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

      return Success;
   end Check_Semantics_In_Components;

   -----------------------------------
   -- Check_Semantics_In_Namespaces --
   -----------------------------------

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

      Success           : Boolean := True;
      List_Node         : Node_Id;
      Package_List_Node : Node_Id;
   begin
      if not Is_Empty (Declarations (Root)) then
         List_Node := First_Node (Declarations (Root));

         while Present (List_Node) loop
            case Kind (List_Node) is
               when K_Component_Type =>
                  Success := Check_Cycles_In_Port_Group_Or_Component_Type
                    (List_Node)
                    and then Success;

               when  K_Component_Implementation =>
                  Success := Check_Cycles_In_Component_Implementation
                    (List_Node)
                    and then Success;

               when K_Feature_Group_Type =>
                  Success :=
                    (Check_Cycles_In_Port_Group_Or_Component_Type
                     (List_Node)
                     and then Check_Cycles_In_Inversions_Of_Port_Groups
                     (List_Node))
                    and then Success;

               when K_Package_Specification =>
                  if not Is_Empty (Declarations (List_Node)) then
                     Package_List_Node :=
                       First_Node (Declarations (List_Node));

                     while Present (Package_List_Node) loop
                        case Kind (Package_List_Node) is
                           when K_Component_Type =>
                              Success :=
                                Check_Cycles_In_Port_Group_Or_Component_Type
                                (Package_List_Node)
                                and then Success;

                           when  K_Component_Implementation =>
                              Success :=
                                Check_Cycles_In_Component_Implementation
                                (Package_List_Node)
                                and then Success;

                           when K_Feature_Group_Type =>
                              Success :=
                                (Check_Cycles_In_Port_Group_Or_Component_Type
                                 (Package_List_Node)
                                 and then
                                 Check_Cycles_In_Inversions_Of_Port_Groups
                                 (Package_List_Node))
                                and then Success;

                           when others =>
                              null;
                        end case;

                        Package_List_Node := Next_Node (Package_List_Node);
                     end loop;
                  end if;

               when others =>
                  null;
            end case;

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

      return Success;
   end Check_Semantics_In_Namespaces;

   -----------------------------------
   -- Check_Semantics_Of_Properties --
   -----------------------------------

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

      Success           : Boolean;
      List_Node         : Node_Id;
      Package_List_Node : Node_Id;
   begin
      Success := Compute_Property_Values (Root);

      if Success and then Declarations (Root) /= No_List then
         List_Node := First_Node (Declarations (Root));

         while Present (List_Node) loop
            case Kind (List_Node) is
               when  K_Component_Implementation =>
                  Success :=
                    Check_Properties_Of_Component_Implementation (List_Node)
                    and then Success;

               when K_Component_Type =>
                  Success :=
                    Check_Properties_Of_Component_Type (List_Node)
                    and then Success;

               when K_Feature_Group_Type =>
                  Success :=
                    Check_Properties_Of_Port_Group_Type (List_Node)
                    and then Success;

               when K_Package_Specification =>
                  Success := Check_Property_Associations
                    (Ocarina.Me_AADL.AADL_Tree.Nodes.Properties (List_Node),
                     List_Node)
                    and then Success;

                  if Declarations (List_Node) /= No_List then
                     Package_List_Node :=
                       First_Node (Declarations (List_Node));

                     while Present (Package_List_Node) loop
                        case Kind (Package_List_Node) is
                           when  K_Component_Implementation =>
                              Success :=
                                Check_Properties_Of_Component_Implementation
                                (Package_List_Node)
                                and then Success;

                           when K_Component_Type =>
                              Success :=
                                Check_Properties_Of_Component_Type
                                (Package_List_Node)
                                and then Success;

                           when K_Feature_Group_Type =>
                              Success :=
                                Check_Properties_Of_Port_Group_Type
                                (Package_List_Node)
                                and then Success;

                           when others =>
                              null;
                        end case;

                        Package_List_Node := Next_Node (Package_List_Node);
                     end loop;
                  end if;

               when K_Property_Set =>
                  if Declarations (List_Node) /= No_List then
                     Package_List_Node :=
                       First_Node (Declarations (List_Node));

                     while Present (Package_List_Node) loop
                        case Kind (Package_List_Node) is
                           when  K_Property_Type_Declaration =>
                              Success :=
                                Check_Property_Type (Package_List_Node)
                                and then Success;
                           when others =>
                              null;
                        end case;

                        Package_List_Node := Next_Node (Package_List_Node);
                     end loop;
                  end if;

               when others =>
                  null;
            end case;

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

      return Success;
   end Check_Semantics_Of_Properties;

   ------------------------------------------
   -- Check_Values_Of_Property_Association --
   ------------------------------------------

   function Check_Values_Of_Property_Association
     (Property_Association : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Property_Association) = K_Property_Association);

      Property_Name         : constant Node_Id := Get_Referenced_Entity
        (Ocarina.Me_AADL.AADL_Tree.Nodes.Property_Name (Property_Association));
      Type_Of_Property_Name : constant Property_Type := Get_Type_Of_Property
        (Property_Name);

      List_Node            : Node_Id;
      Success              : Boolean := True;
      Types_Are_Compatible : Boolean := True;
   begin
      if Value_Of_Property_Association_Is_Undefined (Property_Association) then
         Success := True;
      else
         --  Extract from the AADL 1.0 standard (paragraph 10.3 page 158):

         --  "If the property declaration for the associated property
         --  name *does not* contain the reserved words *list of*, the
         --  property value must be a single property value. If the
         --  property declaration for the associated property name
         --  contains the reserved words *list of*, the property value
         --  *can be a single property value*, which is interpreted to
         --  be a list of one value."

         --  So list properties can take single values after '=>' and
         --  '+=>'

         --  However we convert these property to their right form
         --  (and keep the old form) to make easier further tree
         --  manipulations.

         if Is_Additive_Association (Property_Association)
           and then Present
           (Expanded_Single_Value
            (Property_Association_Value
             (Property_Association)))
         then
            --  Additive association allowed only for list properties

            if not Type_Of_Property_Is_A_List (Property_Name) then
               Success := Convert_Single_Value_To_List (Property_Association);

               --  Even if the conversion succeded, this is an
               --  error. This is a workaround to get the proper error
               --  message.

               Display_Property_List_Discrepancy
                 (Property_Association => Property_Association,
                  Property_Name        => Property_Name);
               return False;
            else
               Success := Convert_Single_Value_To_List (Property_Association);

               --  We do not return since there are more tests to
               --  perform.
            end if;
         end if;

         --  To avoid endless recursion, we begin by testing list
         --  properties.

         if Expanded_Multi_Value
           (Property_Association_Value
            (Property_Association)) /= No_List
         then
            if Type_Of_Property_Is_A_List (Property_Name) then
               List_Node := First_Node
                 (Expanded_Multi_Value
                  (Property_Association_Value
                   (Property_Association)));

               while Present (List_Node) loop
                  Types_Are_Compatible := Test_Property_Type_Equivalence
                    (Type_Of_Property_Name,
                     Get_Type_Of_Property_Value
                     (List_Node))
                    and then Test_Property_Value_Validity
                    (Property_Name_Type (Property_Name), List_Node);

                  if not Types_Are_Compatible then
                     Display_Incompatible_Property_Types
                       (Property_Association => Property_Association,
                        Property_Value       => List_Node,
                        Property_Name        => Property_Name);
                     Success := False;
                  end if;

                  List_Node := Next_Node (List_Node);
               end loop;

            else
               --  Single value properties cannot have a list as
               --  value.

               Success := False;
               Display_Property_List_Discrepancy
                 (Property_Association => Property_Association,
                  Property_Name        => Property_Name);
            end if;

         elsif Expanded_Single_Value
           (Property_Association_Value (Property_Association)) /= No_Node
         then
            if Type_Of_Property_Is_A_List (Property_Name) then
               --  If the value is a single element while we are
               --  expecting a list, we build a list from the single
               --  element, and display a warning. and keep the single
               --  element untouched.

               Display_Conversion_To_Property_List
                 (Property_Association => Property_Association,
                  Property_Name        => Property_Name);
               Success := Convert_Single_Value_To_List (Property_Association)
                 and then Check_Values_Of_Property_Association
                 (Property_Association);
            else
               Success := Test_Property_Type_Equivalence
                 (Type_Of_Property_Name,
                  Get_Type_Of_Property_Value
                  (Expanded_Single_Value
                   (Property_Association_Value
1954
                      (Property_Association))))
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
                 and then Test_Property_Value_Validity
                 (Property_Name_Type (Property_Name),
                  Expanded_Single_Value
                  (Property_Association_Value
                   (Property_Association)));

               if not Success then
                  Display_Incompatible_Property_Types
                    (Property_Association => Property_Association,
                     Property_Value       => Expanded_Single_Value
                       (Property_Association_Value
                        (Property_Association)),
                     Property_Name        => Property_Name);
               end if;
            end if;

         else
            --  If the property association has no actual value

            Success := True;
         end if;
      end if;

      return Success;
   end Check_Values_Of_Property_Association;

   ---------------------
   -- Compare_Numbers --
   ---------------------

   function Compare_Numbers
     (Number_1 : Node_Id;
      Number_2 : Node_Id)
     return Integer
   is
      pragma Assert (Kind (Number_1) = K_Literal or else
                     Kind (Number_1) = K_Signed_AADLNumber);
      pragma Assert (Kind (Number_2) = K_Literal or else
                     Kind (Number_2) = K_Signed_AADLNumber);

      Literal_1 : Node_Id;
      Literal_2 : Node_Id;
      Result    : Integer;
   begin
      if Kind (Number_1) = K_Literal and then Kind (Number_2) = K_Literal then
         Literal_1 := Number_1;
         Literal_2 := Number_2;
      else
         Homogenize_Unit_Numbers (Number_1, Number_2, Literal_1, Literal_2);
      end if;

      --  Use the routines of the AADL_Values package to compare the
2007
      --  values and wrap them to intercept any comparison error.
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019

      begin
         if Value (Value (Literal_1)) < Value (Value (Literal_2)) then
            Result := 1;
         elsif Value (Value (Literal_2)) < Value (Value (Literal_1)) then
            Result := -1;
         else
            Result := 0;
         end if;
      exception
         when Constraint_Error =>
            Result := -2;
2020
            raise;
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
      end;

      return Result;
   end Compare_Numbers;

   -----------------------------
   -- Connection_End_Is_Local --
   -----------------------------

   function Connection_End_Is_Local (Node : Node_Id) return Boolean is

      pragma Assert (Kind (Node) = K_Entity_Reference);
   begin
      return Next_Node (First_Node (Path (Node))) = No_Node
        or else Kind (Corresponding_Entity
                      (Item (First_Node (Path (Node))))) =
                      K_Feature_Group_Spec;
   end Connection_End_Is_Local;

   ----------------------------------
   -- Convert_Single_Value_To_List --
   ----------------------------------

   function Convert_Single_Value_To_List
     (Property_Association : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Property_Association) = K_Property_Association);
   begin
      Set_Expanded_Multi_Value
        (Property_Association_Value (Property_Association),
         New_List (K_List_Id,
                   Loc (Expanded_Single_Value
                        (Property_Association_Value
                         (Property_Association)))));
      Append_Node_To_List
        (Expanded_Single_Value
         (Property_Association_Value (Property_Association)),
         Expanded_Multi_Value
         (Property_Association_Value (Property_Association)));

      Set_Multi_Value
        (Property_Association_Value (Property_Association),
         New_List (K_List_Id,
                   Loc (Single_Value
                        (Property_Association_Value
                         (Property_Association)))));
      Append_Node_To_List
        (Single_Value (Property_Association_Value (Property_Association)),
         Multi_Value (Property_Association_Value (Property_Association)));

      return True;
   end Convert_Single_Value_To_List;

   -----------------------------
   -- Homogenize_Unit_Numbers --
   -----------------------------

   procedure Homogenize_Unit_Numbers
     (Number_1  :     Node_Id;
      Number_2  :     Node_Id;
      Literal_1 : out Node_Id;
      Literal_2 : out Node_Id)
   is
      function Convert_To_Base (L : Node_Id; U : Node_Id) return Node_Id;
      --  Converts the literal L associated to the unit U into a
      --  literal associated with the base dentifier of the units
      --  type.

      procedure Fetch
        (U       :     Node_Id;
         Fetched : out Node_Id;
         Base    : out Boolean);
      --  Return the defining identifier corresponding to the
      --  multiplier U in the corresponding units type. Base is set to
      --  True if the fetched identifier is the base unit
      --  identifier. If the identifier is not found, return No_Node
      --  and False.

      ---------------------
      -- Convert_To_Base --
      ---------------------

      function Convert_To_Base (L : Node_Id; U : Node_Id) return Node_Id is
         Fetched        : Node_Id;
         N              : Node_Id;
         Base           : Boolean;
         Result         : Value_Type;
         Count          : Natural;
         Max_Iterations : Natural;
         Units_Type     : Node_Id;
      begin
         Fetch (U, Fetched, Base);

         if not Base then
            --  To avoid infinite loops and detect bad formed units
            --  types.

            Units_Type := Corresponding_Entity
              (Unit_Identifier
               (Corresponding_Entity
                (Fetched)));

            Max_Iterations := Length (Unit_Definitions (Units_Type));
         end if;

         Result := Value (Value (L));

         Count := 0;

         while not Base loop
            Result := Result * Value
              (Value
               (Numeric_Literal
                (Corresponding_Entity
                 (Fetched))));

            Fetch
              (Unit_Identifier (Corresponding_Entity (Fetched)),
               Fetched,
               Base);

            Count := Count + 1;

            if Count > Max_Iterations + 1 then
               DAE
                 (Message0 => "Units Type ",
                  Node1    =>  Units_Type,
                  Message1 => " Contains cycles is not correctly defined");
               exit;
            end if;

         end loop;

         N := New_Node (K_Literal, Loc (L));
         Set_Value (N, New_Value (Result));
2157

2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
         return N;
      end Convert_To_Base;

      -----------
      -- Fetch --
      -----------

      procedure Fetch
        (U       :     Node_Id;
         Fetched : out Node_Id;
         Base    : out Boolean)
      is
         Units_Type      : Node_Id;
         Unit_Definition : Node_Id;
      begin
         if Kind (Corresponding_Entity (U)) = K_Units_Type then
            --  We have the base identifier

            Units_Type := Corresponding_Entity (U);
         else
            Units_Type := Corresponding_Entity
              (Unit_Identifier
               (Corresponding_Entity
                (U)));
         end if;

         --  This phase is neccessary because the Unit_Identifier of a
         --  Unit_definition is not linked directly to its
         --  corresponding unit definition.

         Fetched := Base_Identifier (Units_Type);

         if To_Lower (Name (Fetched)) = To_Lower (Name (U)) then
            Base := True;
         else
            Base := False;
            Unit_Definition := First_Node (Unit_Definitions (Units_Type));

            while Present (Unit_Definition) loop
               Fetched := Identifier (Unit_Definition);

               exit when To_Lower (Name (Fetched)) = To_Lower (Name (U));

               Fetched := No_Node;
               Unit_Definition := Next_Node (Unit_Definition);
            end loop;
         end if;
      end Fetch;

      Unit_1 : Node_Id;
      Unit_2 : Node_Id;
   begin
      --  If one of the numbers is a literal (without a unit) this
      --  means that the corresponding unit type contains only one
      --  unit identifier an that the node linker did not detect an
      --  error. So we return the literal without modifying them.

      if Kind (Number_1) = K_Literal then
         Literal_1 := Number_1;

         --  Number_2 is necessarily a K_Signed_AADLNumber

         Literal_2 := Number_Value (Number_2);

         --  Nothing else to do

         return;
      end if;

      if Kind (Number_2) = K_Literal then
         Literal_2 := Number_2;

         --  Number_1 is necessarily a K_Signed_AADLNumber

         Literal_1 := Number_Value (Number_1);

         --  Nothing more to do

         return;
      end if;

      --  At this stage, both numbers are K_Signed_AADLNumber's. But
      --  they may have null Unit identifiers.

      if No (Unit_Identifier (Number_1))
        or else No (Unit_Identifier (Number_2))
      then
         Literal_1 := Number_Value (Number_1);
         Literal_2 := Number_Value (Number_2);

         --  Nothing more to do

         return;
      end if;

      --  At this stage, we have two K_Signed_AADLNumber's with non
      --  null unit identifiers.

      --  Get the corresponding unit identifiers. If the name linker
      --  failed to find the corresponding unit identifier, do not
      --  cause error cascade.

      Unit_1 := Corresponding_Entity (Unit_Identifier (Number_1));
      Unit_2 := Corresponding_Entity (Unit_Identifier (Number_2));

      --  Convert the literals

      if Present (Unit_1) then
         Literal_1 := Convert_To_Base (Number_Value (Number_1), Unit_1);
      else
         Literal_1 := Number_Value (Number_1);
      end if;

      if Present (Unit_2) then
         Literal_2 := Convert_To_Base (Number_Value (Number_2), Unit_2);
      else
         Literal_2 := Number_Value (Number_2);
      end if;
   end Homogenize_Unit_Numbers;

   ------------------------------------
   -- Test_Property_Type_Equivalence --
   ------------------------------------

   function Test_Property_Type_Equivalence
     (Type_Of_Property_Name        :
      Ocarina.Me_AADL.AADL_Tree.Entities.Properties.Property_Type;
      Type_Of_Property_Association :
      Ocarina.Me_AADL.AADL_Tree.Entities.Properties.Property_Type)
     return Boolean
   is
      Success : Boolean;
   begin
      case Type_Of_Property_Name is
         when PT_Boolean =>
            Success := Type_Of_Property_Association = PT_Boolean_Expression
              or else Type_Of_Property_Association = PT_Boolean;

         when PT_Integer =>
            Success := Type_Of_Property_Association = PT_Integer
              or else Type_Of_Property_Association = PT_Unsigned_Integer;

         when PT_Float =>
            Success := Type_Of_Property_Association = PT_Float
              or else Type_Of_Property_Association = PT_Unsigned_Float
              or else Type_Of_Property_Association = PT_Integer
              or else Type_Of_Property_Association = PT_Unsigned_Integer;

         when PT_List =>
            Success := False;

         when PT_Reference =>
            Success := Type_Of_Property_Association = PT_Reference;

         when others =>
            Success := Type_Of_Property_Association = Type_Of_Property_Name;
      end case;

      return Success;
   end Test_Property_Type_Equivalence;

   ----------------------------------
   -- Test_Property_Value_Validity --
   ----------------------------------

   function Test_Property_Value_Validity
     (Property_Type  : Node_Id;
      Property_Value : Node_Id)
     return Boolean
   is
      pragma Assert (Kind (Property_Type) = K_Property_Type);
      pragma Assert (Kind (Property_Value) = K_Component_Classifier_Term
                     or else Kind (Property_Value) = K_Reference_Term
                     or else Kind (Property_Value) = K_Enumeration_Term
                     or else Kind (Property_Value) = K_Number_Range_Term
                     or else Kind (Property_Value) = K_Literal
                     or else Kind (Property_Value) = K_Signed_AADLNumber);

      List_Node       : Node_Id;
      Temp_Node       : Node_Id;
      Type_Designator : Node_Id;
      Is_Integer      : Boolean;
      Actual_Literal  : Node_Id;
      Success         : Boolean := True;
   begin
      Type_Designator := Expanded_Type_Designator (Property_Type);
      Success := Check_Property_Type (Type_Designator);

      if Success then
         case Kind (Type_Designator) is
            when K_Classifier_Type =>
               List_Node :=
                 First_Node (List_Items (Type_Designator));
               Success := False;

               if Kind (Property_Value) = K_Component_Classifier_Term then
                  Temp_Node := Get_Referenced_Entity (Property_Value);

                  if Present (Temp_Node) then
                     case AADL_Version is
                        when AADL_V1 =>
                           while Present (List_Node) loop
                              if Get_Category_Of_Component (Temp_Node) =
                                Component_Category'Val (Category (List_Node))
                              then
                                 Success := True;
                              end if;

                              List_Node := Next_Node (List_Node);
                           end loop;

                        when AADL_V2 =>
                           Success := True;
                     end case;
                  end if;
               end if;

            when K_Reference_Type =>
               if List_Items (Type_Designator) = No_List then
                  List_Node := No_Node;
               else
                  List_Node :=
                    First_Node (List_Items (Type_Designator));
               end if;

               if Present (List_Node) then
                  Success := False;
               else
                  Success := True;

                  --  If no type is indicated, then any reference is
                  --  correct.
               end if;

               if Kind (Property_Value) = K_Reference_Term then
                  Temp_Node := Get_Referenced_Entity
                    (Reference_Term (Property_Value));

                  if Present (Temp_Node) then
                     while Present (List_Node) loop
                        case AADL_Version is
                           when AADL_V1 =>
                              case (Referable_Element_Category'Val
                                      (Category (List_Node)))
                                 is
                                 when REC_Component_Category =>
                                    if Get_Entity_Category (Temp_Node) =
                                      EC_Subcomponent
                                    then
2407
2408
2409
2410
                                       --  If the subcomponent
                                       --  specification is incomplete
                                       --  (see AADL 1.0 standard
                                       --  paragraph 4.5 section
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
                                       --  `semantics'), then there is
                                       --  nothing else to analyze.

                                       if No (Entity_Ref (Temp_Node)) then
                                          Success := True;
                                       elsif Get_Category_Of_Component
                                         (Get_Referenced_Entity
                                            (Entity_Ref (Temp_Node))) =
                                         Component_Category'Val
                                         (Component_Cat (List_Node))
                                       then
                                          Success := True;
                                       end if;
                                    end if;
                                 when REC_Connections =>
                                    if Get_Entity_Category (Temp_Node) =
                                      EC_Connection
                                    then
                                       Success := True;
                                    end if;
                                 when REC_Server_Subprogram =>
                                    if Get_Entity_Category (Temp_Node) =
                                      EC_Feature
                                    then
                                       --  XXX This is incomplete

                                       Success := True;
                                    end if;
                                 when REC_Identifier =>
                                    --  XXX This is incomplete

                                    Success := True;
                                 when REC_Subprogram_Call_Sequence =>
                                    --  XXX This is incomplete

                                    Success := True;
                              end case;
                           when AADL_V2 =>
                              Success := True;
                              --  XXX This incomplete

                        end case;

                        List_Node := Next_Node (List_Node);
                     end loop;
                  end if;
               else
                  Success := False;
               end if;

            when K_Real_Type =>
               if Kind (Property_Value) = K_Literal
                 or else Kind (Property_Value) = K_Signed_AADLNumber
               then
                  Actual_Literal := Property_Value;
               else
                  Actual_Literal := No_Node;
               end if;

               if Present (Actual_Literal) then
                  if No (Type_Range (Type_Designator)) then
                     Success := True;
                  else
                     Success :=
                       (Compare_Numbers
                        (Lower_Bound
                         (Type_Range
                          (Type_Designator)),
                         Actual_Literal) >= 0)
                       and then
                       (Compare_Numbers
                        (Actual_Literal,
                         Upper_Bound
                         (Type_Range
                          (Type_Designator))) >= 0);
                  end if;
               else
                  Success := False;
               end if;

            when K_Integer_Type =>
               if Kind (Property_Value) = K_Literal then
                  Actual_Literal := Property_Value;
                  Is_Integer := Value (Value (Actual_Literal)).T = LT_Integer;
               elsif Kind (Property_Value) = K_Signed_AADLNumber then
                  Actual_Literal := Property_Value;
                  Is_Integer := Value
                    (Value
                     (Number_Value
                      (Actual_Literal))).T =
                    LT_Integer;
               else
                  Actual_Literal := No_Node;
                  Is_Integer := False;
               end if;

               if Is_Integer then
                  if Type_Range (Type_Designator) = No_Node then
                     Success := True;
                  else
                     Success :=
                       Present (Lower_Bound (Type_Range (Type_Designator)))
                       and then
                       (Compare_Numbers
2515
2516
                          (Lower_Bound (Type_Range (Type_Designator)),
                           Actual_Literal) >= 0)
2517
2518
2519
2520
2521
                       and then
                       Present (Upper_Bound (Type_Range (Type_Designator)))
                       and then
                       (Compare_Numbers
                        (Actual_Literal,
2522
                         Upper_Bound (Type_Range (Type_Designator))) >= 0);
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
                  end if;
               else
                  Success := False;
               end if;

            when K_Range_Type =>
               Success := True;

            when K_Boolean_Type =>
               Success := Kind (Property_Value) = K_Literal
                 and then Value (Value (Property_Value)).T = LT_Boolean;

            when K_String_Type =>
               Success := Kind (Property_Value) = K_Literal
                 and then Value (Value (Property_Value)).T = LT_String;

            when K_Enumeration_Type =>
               case AADL_Version is
                  when AADL_V1 =>
                     Success := Kind (Property_Value) = K_Literal
                       and then Value (Value
                                         (Property_Value)).T = LT_Enumeration;

                     if Success then
                        Success := False;
                        List_Node := First_Node (Identifiers
                                                   (Type_Designator));

                        while Present (List_Node) loop
                           Success := Success
                             or else Name (List_Node) =
                             Value (Value (Property_Value)).EVal;

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

                  when AADL_V2 =>
                     Success := Kind (Property_Value) = K_Enumeration_Term;

                     if Success then
                        Success := False;
                        List_Node := First_Node (Identifiers
                                                   (Type_Designator));

                        while Present (List_Node) loop
                           Success := Success
                             or else Name (List_Node) =
                             Name (Identifier (Property_Value));

                           List_Node := Next_Node (List_Node);
                        end loop;
                     end if;
               end case;

            when others =>
               Success := False;
         end case;
      end if;

      return Success;
   end Test_Property_Value_Validity;

end Ocarina.Analyzer.AADL.Semantics;