ocarina-analyzer-aadl-finder.adb 45.3 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 . F I N D E R          --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--       Copyright (C) 2009 Telecom ParisTech, 2010-2015 ESA & ISAE.        --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29
30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31
32
33
--                                                                          --
------------------------------------------------------------------------------

34
with Ocarina.Namet;
35
36
37
38
39
40
41
42
43
44
45
46
47

with Ocarina.Analyzer.AADL.Naming_Rules;
with Ocarina.Analyzer.Messages;

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

with Ocarina.Property_Sets;

package body Ocarina.Analyzer.AADL.Finder is

48
   use Ocarina.Namet;
49
50
51
52
53
54
55
56
57
58
59
60
61
   use Ocarina.Analyzer.AADL.Naming_Rules;
   use Ocarina.Analyzer.Messages;
   use Ocarina.ME_AADL;
   use Ocarina.ME_AADL.AADL_Tree.Entities;
   use Ocarina.ME_AADL.AADL_Tree.Nodes;
   use Ocarina.ME_AADL.AADL_Tree.Nutils;
   use Ocarina.ME_AADL.AADL_Tree.Entities.Properties;
   use Ocarina.Property_Sets;

   function Find_AADL_Declaration_Classifier
     (Root                   : Node_Id;
      Package_Identifier     : Node_Id;
      Declaration_Identifier : Node_Id;
62
      Declaration_Kinds      : Node_Kind_Array) return Node_Id;
63
64
65
66

   function Find_Subclause_Declaration_Classifier
     (Component              : Node_Id;
      Declaration_Identifier : Node_Id;
67
      Subclause_Kinds        : Node_Kind_Array) return Node_Id;
68
69
70

   function Filter_Declarations_According_To_Modes
     (Declaration_Node : Node_Id;
71
      In_Modes         : Node_Id) return Node_Id;
72
73
74
75
76
77
78
   --  Given a chained list of homonyms 'Declaration_Node',
   --
   --  * if In_Modes is not null, return the node coprresponding to
   --  the declaration that matches these modes or else the
   --  declaration that has no "in modes" clause or else No_Node.
   --  * if In_Modes is nul, return the node coprresponding to the
   --  declaration with no "in modes" clause or else No_Node.
79
80
81
82
83
84
85

   --------------------------------------------
   -- Filter_Declarations_According_To_Modes --
   --------------------------------------------

   function Filter_Declarations_According_To_Modes
     (Declaration_Node : Node_Id;
86
      In_Modes         : Node_Id) return Node_Id
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
   is
      Pointed_Node       : Node_Id := Declaration_Node;
      Homonym_Node       : Node_Id;
      Homonym_Identifier : Node_Id;
      Success            : Boolean;
      Was_First_Homonym  : Boolean;
      Required_Mode      : Node_Id;
      Present_Mode       : Node_Id;
      Name_Id_1          : Name_Id;
      Name_Id_2          : Name_Id;
      Name_Id_1b         : Name_Id;
      Name_Id_2b         : Name_Id;
   begin
      --  If In_Modes is (none), return No_Node

      if Present (In_Modes) and then not Have_Modes (In_Modes) then
         return No_Node;
      end if;

      Homonym_Node := Pointed_Node;

      while Present (Homonym_Node) loop
109
         Success           := True;
110
111
112
113
         Was_First_Homonym := (Homonym_Node = Pointed_Node);

         if Have_Modes (In_Modes) then
            if Present
114
115
116
                (Ocarina.ME_AADL.AADL_Tree.Nodes.In_Modes (Homonym_Node))
              and then not Have_Modes
                (Ocarina.ME_AADL.AADL_Tree.Nodes.In_Modes (Homonym_Node))
117
118
119
120
121
122
            then
               --  This means that the declarator has an 'in modes
               --  (none)' clause. This is not good for us.

               Success := False;
            elsif Have_Modes
123
124
                (Ocarina.ME_AADL.AADL_Tree.Nodes.In_Modes (Homonym_Node))
            then
125
126
127
               --  All the modes of

               Required_Mode := First_Node (Modes (In_Modes));
128
               Success       := False;
129
130
131
132
133

               --  For each required mode, we look for it in the
               --  in_modes statement.

               while Present (Required_Mode) loop
134
135
136
137
138
                  Present_Mode :=
                    First_Node
                      (Modes
                         (Ocarina.ME_AADL.AADL_Tree.Nodes.In_Modes
                            (Homonym_Node)));
139
140
141
142

                  while Present (Present_Mode) loop
                     if Kind (Present_Mode) = Kind (Required_Mode) then
                        if Kind (Required_Mode) = K_Entity_Reference then
143
144
145
146
147
                           Name_Id_1 :=
                             Get_Name_Of_Entity_Reference (Present_Mode);
                           Name_Id_2 :=
                             Get_Name_Of_Entity_Reference (Required_Mode);
                           Success := (Name_Id_1 = Name_Id_2) or else Success;
148
149
150

                        elsif Kind (Required_Mode) =
                          K_Pair_Of_Entity_References
151
152
153
                          and then
                            (Second_Reference (Required_Mode) /= No_Node) =
                            (Second_Reference (Present_Mode) /= No_Node)
154
155
                        then
                           if Second_Reference (Required_Mode) = No_Node then
156
157
158
159
160
161
162
163
                              Name_Id_1 :=
                                Get_Name_Of_Entity_Reference
                                  (First_Reference (Present_Mode));
                              Name_Id_2 :=
                                Get_Name_Of_Entity_Reference
                                  (First_Reference (Required_Mode));
                              Success :=
                                (Name_Id_1 = Name_Id_2) or else Success;
164
165

                           else
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                              Name_Id_1 :=
                                Get_Name_Of_Entity_Reference
                                  (First_Reference (Present_Mode));
                              Name_Id_2 :=
                                Get_Name_Of_Entity_Reference
                                  (First_Reference (Required_Mode));
                              Name_Id_1b :=
                                Get_Name_Of_Entity_Reference
                                  (Second_Reference (Present_Mode));
                              Name_Id_2b :=
                                Get_Name_Of_Entity_Reference
                                  (Second_Reference (Required_Mode));

                              Success :=
                                ((Name_Id_1 = Name_Id_2)
                                 and then (Name_Id_1b = Name_Id_2b))
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
                                or else Success;
                           end if;
                        end if;
                     end if;

                     exit when Success;
                     Present_Mode := Next_Node (Present_Mode);
                  end loop;

                  exit when not Success;
                  Required_Mode := Next_Node (Required_Mode);
               end loop;
            end if;
         end if;

         Homonym_Identifier := Homonym (Identifier (Homonym_Node));

         if Present (Homonym_Identifier) then
            Homonym_Node := Corresponding_Entity (Homonym_Identifier);
         else
            Homonym_Node := No_Node;
         end if;

         if Was_First_Homonym and then not Success then
            Pointed_Node := Homonym_Node;
         end if;
      end loop;

      return Pointed_Node;
   end Filter_Declarations_According_To_Modes;

   --------------------------------------
   -- Find_AADL_Declaration_Classifier --
   --------------------------------------

   function Find_AADL_Declaration_Classifier
     (Root                   : Node_Id;
      Package_Identifier     : Node_Id;
      Declaration_Identifier : Node_Id;
221
      Declaration_Kinds      : Node_Kind_Array) return Node_Id
222
223
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
224
225
226
      pragma Assert
        (No (Package_Identifier)
         or else Kind (Package_Identifier) = K_Identifier);
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
      pragma Assert (Kind (Declaration_Identifier) = K_Identifier);
      pragma Assert (Declaration_Kinds'Length > 0);

      Pack               : Node_Id;
      Pointed_Node       : Node_Id := No_Node;
      Homonym_Node       : Node_Id;
      Homonym_Identifier : Node_Id;
      Success            : Boolean;
      Was_First_Homonym  : Boolean;
   begin
      if Present (Package_Identifier) then
         Pack := Node_In_Scope (Package_Identifier, Entity_Scope (Root));
         Pointed_Node := No_Node;

         --  Node_In_Scope returns a node with all its homonyms. We
         --  have to look for a package in this list. Naming rules
         --  ensure there is at most one package in the list.

245
         while Present (Pack) and then Kind (Pack) /= K_Package_Specification
246
247
248
249
250
251
252
253
254
255
256
257
258
         loop
            Homonym_Identifier := Homonym (Identifier (Pack));

            if Present (Homonym_Identifier) then
               Pack := Corresponding_Entity (Homonym_Identifier);
            else
               Pack := No_Node;
            end if;
         end loop;

         --  If the package has been found, we look for the declaration

         if Present (Pack) then
259
260
            Pointed_Node :=
              Node_In_Scope (Declaration_Identifier, Entity_Scope (Pack));
261
262
263
264
265
266
267
268
269

            if Current_Scope /= Entity_Scope (Pack) then
               --  If the search is not done from the local package,
               --  then we must ignore the private declarations

               Homonym_Node := Pointed_Node;

               while Present (Homonym_Node) loop
                  Was_First_Homonym := (Homonym_Node = Pointed_Node);
270
                  Success           := not Is_Private (Homonym_Node);
271
272

                  if not Success then
273
274
275
276
277
278
                     Homonym_Identifier :=
                       Remove_From_Homonyms
                         (Identifier (Pointed_Node),
                          Identifier (Homonym_Node));
                  --  Beware: Remove_From_Homonyms only handles
                  --  identifiers.
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
                  else
                     Homonym_Identifier := Homonym (Identifier (Homonym_Node));
                  end if;

                  if Present (Homonym_Identifier) then
                     Homonym_Node := Corresponding_Entity (Homonym_Identifier);
                  else
                     Homonym_Node := No_Node;
                  end if;

                  if Was_First_Homonym and then not Success then
                     Pointed_Node := Homonym_Node;
                  end if;
               end loop;
            end if;
         end if;
      else
         Pointed_Node := Node_In_Scope (Declaration_Identifier, Current_Scope);
         --  Current_Scope is supposed to be the one of the package
      end if;

      --  We then filter out the node kinds we do not seek

      Homonym_Node := Pointed_Node;

      while Present (Homonym_Node) loop
305
         Success           := False;
306
307
308
         Was_First_Homonym := (Homonym_Node = Pointed_Node);

         for K in Declaration_Kinds'Range loop
309
310
            Success :=
              (Kind (Pointed_Node) = Declaration_Kinds (K)) or else Success;
311
312
313
         end loop;

         if not Success then
314
315
316
317
318
            Homonym_Identifier :=
              Remove_From_Homonyms
                (Identifier (Pointed_Node),
                 Identifier (Homonym_Node));
         --  Beware: Remove_From_Homonyms only handles identifiers.
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
         else
            Homonym_Identifier := Homonym (Identifier (Homonym_Node));
         end if;

         if Present (Homonym_Identifier) then
            Homonym_Node := Corresponding_Entity (Homonym_Identifier);
         else
            Homonym_Node := No_Node;
         end if;

         if Was_First_Homonym and then not Success then
            Pointed_Node := Homonym_Node;
         end if;
      end loop;

      return Pointed_Node;
   end Find_AADL_Declaration_Classifier;

   ------------------------------
   -- Find_All_Component_Types --
   ------------------------------

   function Find_All_Component_Types
     (Root      : Node_Id;
343
      Namespace : Node_Id := No_Node) return Node_List
344
345
346
347
348
349
350
351
352
353
354
355
   is
   begin
      return Find_All_Declarations (Root, (1 => K_Component_Type), Namespace);
   end Find_All_Component_Types;

   ---------------------------
   -- Find_All_Declarations --
   ---------------------------

   function Find_All_Declarations
     (Root      : Node_Id;
      Kinds     : Node_Kind_Array;
356
      Namespace : Node_Id := No_Node) return Node_List
357
358
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
359
360
361
362
      pragma Assert
        (No (Namespace)
         or else Kind (Namespace) = K_AADL_Specification
         or else Kind (Namespace) = K_Package_Specification);
363
364
365
366
367

      EL        : Node_List;
      List_Node : Node_Id;
   begin
      if No (Namespace) then
368
369
370
371
372
         Select_Nodes
           (Ocarina.ME_AADL.AADL_Tree.Nodes.Declarations (Root),
            Kinds,
            EL.First,
            EL.Last);
373
374
375
376

         --  We first get the declarations of the unnamed namespace

         if not Is_Empty
377
378
379
380
381
             (Ocarina.ME_AADL.AADL_Tree.Nodes.Declarations (Root))
         then
            List_Node :=
              Ocarina.ME_AADL.AADL_Tree.Nodes.First_Node
                (Ocarina.ME_AADL.AADL_Tree.Nodes.Declarations (Root));
382
383
384
385
386
387

            while Present (List_Node) loop
               if Kind (List_Node) = K_Package_Specification then
                  --  Then those of the packages

                  Select_Nodes
388
                    (Ocarina.ME_AADL.AADL_Tree.Nodes.Declarations (List_Node),
389
390
391
392
393
394
395
396
397
                     Kinds,
                     EL.First,
                     EL.Last);
               end if;

               List_Node := Next_Node (List_Node);
            end loop;
         end if;
      else
398
399
400
401
402
         Select_Nodes
           (Ocarina.ME_AADL.AADL_Tree.Nodes.Declarations (Namespace),
            Kinds,
            EL.First,
            EL.Last);
403
404
405
406
407
408
409
410
411
      end if;

      return EL;
   end Find_All_Declarations;

   -----------------------
   -- Find_All_Features --
   -----------------------

412
   function Find_All_Features (AADL_Declaration : Node_Id) return Node_List is
413
414
   begin
      return Find_All_Subclauses
415
416
417
418
419
          (AADL_Declaration,
           (K_Port_Spec,
            K_Parameter,
            K_Feature_Group_Spec,
            K_Subcomponent_Access));
420
421
422
423
424
425
426
   end Find_All_Features;

   ------------------------------------
   -- Find_All_Property_Associations --
   ------------------------------------

   function Find_All_Property_Associations
427
     (AADL_Declaration : Node_Id) return Node_List
428
429
430
   is
   begin
      return Find_All_Subclauses
431
432
          (AADL_Declaration,
           (1 => K_Property_Association));
433
434
435
436
437
438
439
440
441
442
443
   end Find_All_Property_Associations;

   -------------------------------
   -- Find_Property_Enumeration --
   -------------------------------

   function Find_Property_Enumeration
     (Root               : Node_Id;
      Container          : Node_Id;
      Property_Container : Node_Id;
      Default_Value      : Node_Id;
444
      Designator         : Node_Id) return Node_Id
445
446
447
448
   is
      pragma Assert (Present (Root));
      pragma Assert (Present (Container));
      pragma Assert
449
450
451
452
        (Kind (Property_Container) = K_Property_Association
         or else Kind (Property_Container) = K_Constant_Property_Declaration
         or else Kind (Property_Container) = K_Property_Type
         or else Kind (Property_Container) = K_Property_Definition_Declaration
453
454
         or else Kind (Property_Container) = K_Property_Type_Declaration
         or else Kind (Property_Container) = K_Record_Term_Element);
455

456
      List_Node     : Node_Id := No_Node;
457
458
459
460
461
462
      Property_Type : Node_Id;
      Pointed_Node  : Node_Id;

   begin
      case Kind (Designator) is
         when K_Unique_Property_Type_Identifier =>
463
464
465
466
467
468
469
            Pointed_Node :=
              Find_Property_Entity
                (Root,
                 Identifier
                   (Corresponding_Entity
                      (Scope_Entity (Identifier (Entity (Designator))))),
                 Identifier (Designator));
470
471
472
473

            if Present (Pointed_Node) then
               if Kind (Pointed_Node) = K_Property_Type_Declaration then
                  Property_Type := Property_Type_Designator (Pointed_Node);
474
               elsif Kind (Pointed_Node) =
475
476
477
478
479
                 K_Property_Definition_Declaration
               then
                  Property_Type := Property_Name_Type (Pointed_Node);
               end if;

480
481
482
               if Kind (Property_Type) /= K_Enumeration_Type
                 and then Kind (Property_Type) /= K_Record_Type
               then
483
484
485
                  return No_Node;
               end if;

486
487
488
489
               if Kind (Property_Type) = K_Enumeration_Type
                 and then not Is_Empty (Identifiers (Property_Type))
               then
                  List_Node := First_Node (Identifiers (Property_Type));
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
                  while Present (List_Node) loop
                     if Ocarina.ME_AADL.AADL_Tree.Nodes.Name (List_Node) =
                       Name (Identifier (Default_Value))
                     then
                        Resolve_Term_In_Property
                          (Property_Container,
                           Default_Value,
                           K_Enumeration_Term);
                        return Pointed_Node;
                     end if;

                     List_Node := Next_Node (List_Node);
                  end loop;

               elsif Kind (Property_Type) = K_Record_Type then

                  --  When processing a Record_Type, we iterate over
                  --  the property container that holds the record
                  --  term element (i.e. foo => bar) and check that
                  --

                  List_Node := First_Node (List_Items (Property_Type));
                  while Present (List_Node) loop
                     --  A property type is a list of record_type element
                     --  XXX should use case insensitive match ?
                     if Ocarina.ME_AADL.AADL_Tree.Nodes.Display_Name
                       (Identifier (List_Node)) =
                       Display_Name (Identifier (Property_Container))
                     then
                        Resolve_Term_In_Property
                          (List_Node, --  Property_Container,
                           Default_Value,
                           K_Enumeration_Term);

                        return Pointed_Node;
                     end if;

                     List_Node := Next_Node (List_Node);
                  end loop;

               end if;
532
533
534
535
536
537
538
            end if;

         when K_Enumeration_Type =>
            if not Is_Empty (Identifiers (Designator)) then
               List_Node := First_Node (Identifiers (Designator));

               while Present (List_Node) loop
539
                  if Ocarina.ME_AADL.AADL_Tree.Nodes.Name (List_Node) =
540
541
                    Name (Identifier (Default_Value))
                  then
542
543
544
545
                     Resolve_Term_In_Property
                       (Property_Container,
                        Default_Value,
                        K_Enumeration_Term);
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565

                     return Property_Container;
                  end if;

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

         when others =>
            return No_Node;
      end case;

      return No_Node;
   end Find_Property_Enumeration;

   -------------------------------------------------------
   -- Find_All_Subclause_Declarations_Except_Properties --
   -------------------------------------------------------

   function Find_All_Subclause_Declarations_Except_Properties
566
     (AADL_Declaration : Node_Id) return Node_List
567
568
569
   is
   begin
      return Find_All_Subclauses
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
          (AADL_Declaration,
           (K_Annex_Subclause,
            K_Port_Spec,
            K_Parameter,
            K_Feature_Group_Spec,
            K_Subcomponent_Access,
            K_Flow_Spec,
            K_Flow_Implementation,
            K_End_To_End_Flow_Spec,
            K_Flow_Implementation_Refinement,
            K_End_To_End_Flow_Refinement,
            K_Mode,
            K_Connection,
            K_Subprogram_Call,
            K_Subprogram_Call_Sequence));
585
586
587
588
589
590
591
592
   end Find_All_Subclause_Declarations_Except_Properties;

   -------------------------
   -- Find_All_Subclauses --
   -------------------------

   function Find_All_Subclauses
     (AADL_Declaration : Node_Id;
593
      Kinds            : Node_Kind_Array) return Node_List
594
   is
595
596
597
598
      pragma Assert
        (Kind (AADL_Declaration) = K_Component_Implementation
         or else Kind (AADL_Declaration) = K_Component_Type
         or else Kind (AADL_Declaration) = K_Feature_Group_Type);
599
600
601
602
603
604
605
606
607
608
609
610

      EL               : Node_List;
      List_Node        : Node_Id;
      Declaration_Node : Node_Id;
   begin
      case Kind (AADL_Declaration) is
         when K_Component_Type =>
            Declaration_Node := AADL_Declaration;

            while Present (Declaration_Node)
              and then Kind (Declaration_Node) = K_Component_Type
            loop
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
               Select_Nodes
                 (Annexes (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Features (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Flows (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties
                    (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
632
633

               if Present (Parent (Declaration_Node)) then
634
635
636
                  Declaration_Node :=
                    Corresponding_Entity
                      (Identifier (Parent (Declaration_Node)));
637
638
639
640
641
642
643
644
645
646
647
               else
                  Declaration_Node := No_Node;
               end if;
            end loop;

         when K_Component_Implementation =>

            --  We first scan the corresponding component type and its
            --  parents

            if Component_Type_Identifier (AADL_Declaration) /= No_Node then
648
649
650
               Declaration_Node :=
                 Corresponding_Entity
                   (Component_Type_Identifier (AADL_Declaration));
651
652
653
654

               while Present (Declaration_Node)
                 and then Kind (Declaration_Node) = K_Component_Type
               loop
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
                  Select_Nodes
                    (Annexes (Declaration_Node),
                     Kinds,
                     EL.First,
                     EL.Last);
                  Select_Nodes
                    (Features (Declaration_Node),
                     Kinds,
                     EL.First,
                     EL.Last);
                  Select_Nodes
                    (Flows (Declaration_Node),
                     Kinds,
                     EL.First,
                     EL.Last);
                  Select_Nodes
                    (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties
                       (Declaration_Node),
                     Kinds,
                     EL.First,
                     EL.Last);
676
677

                  if Present (Parent (Declaration_Node)) then
678
679
680
                     Declaration_Node :=
                       Corresponding_Entity
                         (Identifier (Parent (Declaration_Node)));
681
682
683
684
685
686
687
688
689
690
691
692
693
694
                  else
                     Declaration_Node := No_Node;
                  end if;
               end loop;
            end if;

            --  Then we scan the component implementation and its
            --  parents

            Declaration_Node := AADL_Declaration;

            while Present (Declaration_Node)
              and then Kind (Declaration_Node) = K_Component_Implementation
            loop
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
               Select_Nodes
                 (Refines_Type (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Subcomponents (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Calls (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
710
711

               if not Is_Empty (Calls (Declaration_Node)) then
712
713
714
                  List_Node :=
                    Ocarina.ME_AADL.AADL_Tree.Nodes.First_Node
                      (Calls (Declaration_Node));
715
716

                  while Present (List_Node) loop
717
718
719
720
721
                     Select_Nodes
                       (Subprogram_Calls (List_Node),
                        Kinds,
                        EL.First,
                        EL.Last);
722
723
724
725
                     List_Node := Next_Node (List_Node);
                  end loop;
               end if;

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
               Select_Nodes
                 (Annexes (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Connections (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Flows (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Modes (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties
                    (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
752
753

               if Present (Parent (Declaration_Node)) then
754
755
756
                  Declaration_Node :=
                    Corresponding_Entity
                      (Identifier (Parent (Declaration_Node)));
757
758
759
760
761
762
763
764
765
766
767
               else
                  Declaration_Node := No_Node;
               end if;
            end loop;

         when K_Feature_Group_Type =>
            Declaration_Node := AADL_Declaration;

            while Present (Declaration_Node)
              and then Kind (Declaration_Node) = K_Feature_Group_Type
            loop
768
769
770
771
772
773
774
775
776
777
778
               Select_Nodes
                 (Features (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
               Select_Nodes
                 (Ocarina.ME_AADL.AADL_Tree.Nodes.Properties
                    (Declaration_Node),
                  Kinds,
                  EL.First,
                  EL.Last);
779
780

               if Present (Parent (Declaration_Node)) then
781
782
783
                  Declaration_Node :=
                    Corresponding_Entity
                      (Identifier (Parent (Declaration_Node)));
784
785
786
787
788
789
               else
                  Declaration_Node := No_Node;
               end if;
            end loop;

         when others =>
790
791
792
            DAE
              (Node1    => AADL_Declaration,
               Message1 => " is not an adequate AADL declaration");
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
            return (No_Node, No_Node);
      end case;

      return EL;
   end Find_All_Subclauses;

   ---------------------------
   -- Find_All_Root_Systems --
   ---------------------------

   function Find_All_Root_Systems (Root : Node_Id) return Node_List is

      pragma Assert (Kind (Root) = K_AADL_Specification);

      System_List       : Node_List;
      Top_Level_Systems : Node_List;
      List_Node         : Node_Id;
      Kept_Node         : Node_Id;
   begin
812
813
814
815
816
      System_List :=
        Find_All_Declarations
          (Root,
           (1 => K_Component_Implementation),
           No_Node);
817
818
819
820
821
822
823

      --  First, we only retrieve the component implementations

      List_Node := System_List.First;

      while Present (List_Node) loop
         if Component_Category'Val (Category (List_Node)) = CC_System
824
825
826
           and then Is_Empty
             (Features
                (Corresponding_Entity (Component_Type_Identifier (List_Node))))
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
         then
            --  If the system implementation corresponds to a type
            --  that does not have any feature, we keep it.

            Kept_Node := List_Node;
         else
            Kept_Node := No_Node;
         end if;

         List_Node := Next_Entity (List_Node);

         if Present (Kept_Node) then
            Set_Next_Entity (Kept_Node, Top_Level_Systems.First);

            if Top_Level_Systems.Last = No_Node then
               Top_Level_Systems.Last := Kept_Node;
            end if;

            Top_Level_Systems.First := Kept_Node;
         end if;
      end loop;

      return Top_Level_Systems;
   end Find_All_Root_Systems;

   -------------------------------
   -- Find_Component_Classifier --
   -------------------------------

   function Find_Component_Classifier
     (Root                 : Node_Id;
      Package_Identifier   : Node_Id;
859
      Component_Identifier : Node_Id) return Node_Id
860
861
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
862
863
864
      pragma Assert
        (No (Package_Identifier)
         or else Kind (Package_Identifier) = K_Identifier);
865
      pragma Assert (Kind (Component_Identifier) = K_Identifier);
866
867
868

      Pointed_Node : Node_Id;

869
   begin
870
871
872
873
874
875
876
877
      Pointed_Node :=
        Find_AADL_Declaration_Classifier
          (Root,
           Package_Identifier,
           Component_Identifier,
           (K_Component_Type,
            K_Component_Implementation,
            K_Alias_Declaration));
878
879
880
881
882
883
884
885
886
887

      --  In case the classifier is an alias, return the renamed entity

      if Present (Pointed_Node)
        and then Kind (Pointed_Node) = K_Alias_Declaration
      then
         Pointed_Node := Renamed_Entity (Pointed_Node);
      end if;

      return Pointed_Node;
888
889
890
891
892
893
894
895
896
   end Find_Component_Classifier;

   ---------------------
   -- Find_Connection --
   ---------------------

   function Find_Connection
     (Component             : Node_Id;
      Connection_Identifier : Node_Id;
897
      In_Modes              : Node_Id := No_Node) return Node_Id
898
899
900
901
902
903
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Connection_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
904
905
906
907
908
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Connection_Identifier,
           (1 => K_Connection));
909

910
911
      Pointed_Node :=
        Filter_Declarations_According_To_Modes (Pointed_Node, In_Modes);
912
913
914
915
916
917
918
919
920
921

      return Pointed_Node;
   end Find_Connection;

   ------------------
   -- Find_Feature --
   ------------------

   function Find_Feature
     (Component          : Node_Id;
922
      Feature_Identifier : Node_Id) return Node_Id
923
   is
924
925
926
927
928
      pragma Assert
        (Kind (Component) = K_Component_Implementation
         or else Kind (Component) = K_Component_Type
         or else Kind (Component) = K_Subcomponent_Access
         or else Kind (Component) = K_Feature_Group_Type);
929
930
931
932
      pragma Assert (Kind (Feature_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
933
934
935
936
937
938
939
940
941
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Feature_Identifier,
           (K_Port_Spec,
            K_Parameter,
            K_Feature_Group_Spec,
            K_Subcomponent_Access,
            K_Subprogram_Spec));
942
943
944
945
946

      if No (Pointed_Node)
        and then Kind (Component) = K_Feature_Group_Type
        and then Present (Inverse_Of (Component))
      then
947
948
949
950
         Pointed_Node :=
           Find_Feature
             (Get_Referenced_Entity (Inverse_Of (Component)),
              Feature_Identifier);
951
952
953
954
955
956
957
958
959
960
961
962
963

         Pointed_Node := Inversed_Entity (Pointed_Node);
      end if;

      return Pointed_Node;
   end Find_Feature;

   --------------------
   -- Find_Flow_Spec --
   --------------------

   function Find_Flow_Spec
     (Component       : Node_Id;
964
      Flow_Identifier : Node_Id) return Node_Id
965
   is
966
967
968
      pragma Assert
        (Kind (Component) = K_Component_Implementation
         or else Kind (Component) = K_Component_Type);
969
970
971
      pragma Assert (Kind (Flow_Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
972
973
974
          (Component,
           Flow_Identifier,
           (1 => K_Flow_Spec));
975
976
977
978
979
980
981
982
   end Find_Flow_Spec;

   ---------------
   -- Find_Mode --
   ---------------

   function Find_Mode
     (Component       : Node_Id;
983
      Mode_Identifier : Node_Id) return Node_Id
984
   is
985
986
987
988
      pragma Assert
        (Kind (Component) = K_Component_Implementation
         or else Kind (Component) = K_Component_Type
         or else Kind (Component) = K_Feature_Group_Type);
989
990
991
      pragma Assert (Kind (Mode_Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
992
993
994
          (Component,
           Mode_Identifier,
           (1 => K_Mode));
995
996
997
998
999
1000
1001
1002
1003
   end Find_Mode;

   --------------------------------
   -- Find_Port_Group_Classifier --
   --------------------------------

   function Find_Port_Group_Classifier
     (Root                  : Node_Id;
      Package_Identifier    : Node_Id;
1004
      Port_Group_Identifier : Node_Id) return Node_Id
1005
1006
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
1007
1008
1009
      pragma Assert
        (No (Package_Identifier)
         or else Kind (Package_Identifier) = K_Identifier);
1010
1011
1012
      pragma Assert (Kind (Port_Group_Identifier) = K_Identifier);
   begin
      return Find_AADL_Declaration_Classifier
1013
1014
1015
1016
          (Root,
           Package_Identifier,
           Port_Group_Identifier,
           (1 => K_Feature_Group_Type));
1017
1018
1019
1020
1021
1022
1023
1024
   end Find_Port_Group_Classifier;

   -------------------------------
   -- Find_Property_Association --
   -------------------------------

   function Find_Property_Association
     (AADL_Declaration          : Node_Id;
1025
      Property_Association_Name : Name_Id) return Node_Id
1026
1027
1028
   is
      pragma Assert (Present (AADL_Declaration));

1029
1030
1031
      All_Properties : constant Node_List :=
        Find_All_Property_Associations (AADL_Declaration);
      List_Node : Node_Id;
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
   begin
      if All_Properties.First /= No_Node then
         List_Node := All_Properties.First;

         while Present (List_Node) loop
            if Name (Identifier (List_Node)) = Property_Association_Name then
               return List_Node;
            end if;

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

      return No_Node;
   end Find_Property_Association;

   --------------------------
   -- Find_Property_Entity --
   --------------------------

   function Find_Property_Entity
     (Root                    : Node_Id;
      Property_Set_Identifier : Node_Id;
1055
      Property_Identifier     : Node_Id) return Node_Id
1056
1057
   is
      pragma Assert (Kind (Root) = K_AADL_Specification);
1058
1059
1060
      pragma Assert
        (No (Property_Set_Identifier)
         or else Kind (Property_Set_Identifier) = K_Identifier);
1061
1062
1063
1064
1065
      pragma Assert (Kind (Property_Identifier) = K_Identifier);

      Property_Set               : Node_Id;
      Found_Property_Declaration : Node_Id := No_Node;
   begin
1066
1067
1068
1069
      --  Per strict conformance to AADL legality rules, all property
      --  entities should be either fully qualified, or part of
      --  pre-declared property sets.

1070
      if Present (Property_Set_Identifier) then
1071
1072
         Property_Set :=
           Node_In_Scope (Property_Set_Identifier, Entity_Scope (Root));
1073

1074
1075
         --  If we found the corresponding property set, then we look
         --  for the property in it.
1076
1077

         if Present (Property_Set) then
1078
1079
            Found_Property_Declaration :=
              Node_In_Scope (Property_Identifier, Entity_Scope (Property_Set));
1080
1081
1082
1083
1084
1085
1086
1087
         else
            Found_Property_Declaration := No_Node;
         end if;

      else
         --  If we did not find anything so far, we try the implicit
         --  property sets.

1088
1089
1090
         for S in Standard_Property_Set_Type'Range loop
            Set_Str_To_Name_Buffer (Image (S));
            Property_Set := Node_In_Scope (Name_Find, Entity_Scope (Root));
1091

1092
            if Present (Property_Set) then
1093
1094
1095
1096
               Found_Property_Declaration :=
                 Node_In_Scope
                   (Property_Identifier,
                    Entity_Scope (Property_Set));
1097
            end if;
1098

1099
1100
            exit when Present (Found_Property_Declaration);
         end loop;
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
      end if;

      return Found_Property_Declaration;
   end Find_Property_Entity;

   --------------------
   -- Find_Subclause --
   --------------------

   function Find_Subclause
     (Component  : Node_Id;
1112
      Identifier : Node_Id) return Node_Id
1113
   is
1114
1115
      pragma Assert
        (Kind (Component) = K_Component_Implementation
1116
1117
         or else Kind (Component) = K_Component_Type
         or else Kind (Component) = K_Port_Spec);
1118
1119
1120
      pragma Assert (Kind (Identifier) = K_Identifier);
   begin
      return Find_Subclause_Declaration_Classifier
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
          (Component,
           Identifier,
           (K_Flow_Spec,
            K_Flow_Implementation,
            K_Flow_Implementation_Refinement,
            K_End_To_End_Flow_Spec,
            K_End_To_End_Flow_Refinement,
            K_Connection,
            K_Subcomponent,
            K_Port_Spec,
            K_Parameter,
            K_Feature_Group_Spec,
            K_Subcomponent_Access,
            K_Subprogram_Spec,
            K_Mode,
            K_Subprogram_Call));
1137
1138
1139
1140
1141
1142
1143
1144
1145
   end Find_Subclause;

   -------------------------------------------
   -- Find_Subclause_Declaration_Classifier --
   -------------------------------------------

   function Find_Subclause_Declaration_Classifier
     (Component              : Node_Id;
      Declaration_Identifier : Node_Id;
1146
      Subclause_Kinds        : Node_Kind_Array) return Node_Id
1147
   is
1148
1149
1150
1151
      pragma Assert
        (Kind (Component) = K_Component_Implementation
         or else Kind (Component) = K_Component_Type
         or else Kind (Component) = K_Feature_Group_Type
1152
         or else Kind (Component) = K_Port_spec
1153
         or else Kind (Component) = K_Subcomponent_Access);
1154
1155
1156
1157
1158
1159
      pragma Assert (Kind (Declaration_Identifier) = K_Identifier);
      pragma Assert (Subclause_Kinds'Length > 0);

      Pointed_Node : Node_Id := No_Node;
   begin
      if Kind (Component) = K_Subcomponent_Access
1160
1161
1162
        and then
          Component_Category'Val (Subcomponent_Category (Component)) =
          CC_Subprogram
1163
      then
1164
1165
1166
1167
         Pointed_Node :=
           Node_In_Scope
             (Declaration_Identifier,
              Entity_Scope (Get_Referenced_Entity (Entity_Ref (Component))));
1168
      else
1169
1170
         Pointed_Node :=
           Node_In_Scope (Declaration_Identifier, Entity_Scope (Component));
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
      end if;

      return Pointed_Node;
   end Find_Subclause_Declaration_Classifier;

   -----------------------
   -- Find_Subcomponent --
   -----------------------

   function Find_Subcomponent
     (Component               : Node_Id;
      Subcomponent_Identifier : Node_Id;
1183
      In_Modes                : Node_Id := No_Node) return Node_Id
1184
1185
1186
1187
1188
1189
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Subcomponent_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
1190
1191
1192
1193
1194
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Subcomponent_Identifier,
           (1 => K_Subcomponent));
1195

1196
1197
      Pointed_Node :=
        Filter_Declarations_According_To_Modes (Pointed_Node, In_Modes);
1198
1199
1200
1201

      return Pointed_Node;
   end Find_Subcomponent;

1202
1203
1204
1205
1206
1207
   --------------------
   -- Find_Prototype --
   --------------------

   function Find_Prototype
     (Component            : Node_Id;
1208
      Prototype_Identifier : Node_Id) return Node_Id
1209
1210
1211
1212
1213
1214
   is
      pragma Assert (Kind (Component) = K_Component_Type);
      pragma Assert (Kind (Prototype_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
1215
1216
1217
1218
1219
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Prototype_Identifier,
           (1 => K_Prototype));
1220
1221
1222
1223

      return Pointed_Node;
   end Find_Prototype;

1224
1225
1226
1227
1228
1229
1230
   --------------------------
   -- Find_Subprogram_Call --
   --------------------------

   function Find_Subprogram_Call
     (Component       : Node_Id;
      Call_Identifier : Node_Id;
1231
      In_Modes        : Node_Id := No_Node) return Node_Id
1232
1233
1234
1235
1236
1237
   is
      pragma Assert (Kind (Component) = K_Component_Implementation);
      pragma Assert (Kind (Call_Identifier) = K_Identifier);

      Pointed_Node : Node_Id;
   begin
1238
1239
1240
1241
1242
      Pointed_Node :=
        Find_Subclause_Declaration_Classifier
          (Component,
           Call_Identifier,
           (1 => K_Subprogram_Call));
1243

1244
1245
      Pointed_Node :=
        Filter_Declarations_According_To_Modes (Pointed_Node, In_Modes);
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255

      return Pointed_Node;
   end Find_Subprogram_Call;

   --------------------------------
   -- Find_In_Import_Declaration --
   --------------------------------

   function Find_In_Import_Declaration
     (Package_Container : Node_Id;
1256
      Node              : Node_Id) return Boolean
1257
   is
1258
      Identifier           : Node_Id := No_Node;
1259
1260
1261
1262
      Pack_Identifier      : Node_Id;
      Import_Node          : Node_Id;
      List_Node            : Node_Id;
      Name_Visibility_Node : Node_Id;
1263
      Success              : Boolean := False;
1264
1265
1266
1267
1268
1269
1270
1271
1272
   begin
      if Kind (Node) = K_Identifier then
         Identifier := Node;
      elsif Kind (Node) = K_Entity_Reference then
         Identifier := Namespace_Identifier (Node);
      end if;

      if Present (Package_Container)
        and then not Is_Empty (Declarations (Package_Container))
1273
1274
1275
        and then
          (Kind (First_Node (Declarations (Package_Container)))) =
          K_Name_Visibility_Declaration
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
      then
         Name_Visibility_Node := First_Node (Declarations (Package_Container));

         if not Is_Empty (List_Items (Name_Visibility_Node)) then
            List_Node := First_Node (List_Items (Name_Visibility_Node));
         end if;

         --  XXX fixme : when pack2 'with' pack1, pack1 identifier must
         --  be add to the scope of pack2 ?

         while Present (List_Node) loop
            if Kind (List_Node) = K_Import_Declaration
              and then not Is_Empty (List_Items (List_Node))
            then
               Import_Node := First_Node (List_Items (List_Node));
            end if;

            while Present (Import_Node) loop
               if Kind (Import_Node) = K_Package_Name then
                  Pack_Identifier := Build_Package_Identifier (Import_Node);

                  if Name (Pack_Identifier) = Name (Identifier) then
                     Success := True;
                  end if;
               else
                  if Name (Import_Node) = Name (Identifier) then
                     Success := True;
                  end if;
               end if;

               Import_Node := Next_Node (Import_Node);
            end loop;

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

      return Success;
   end Find_In_Import_Declaration;

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

   procedure Select_Nodes
     (Decl_List  :        List_Id;
      Kinds      :        Node_Kind_Array;
      First_Node : in out Node_Id;
      Last_Node  : in out Node_Id)
   is
      Success         : Boolean;
      Local_List_Node : Node_Id;
   begin
      if not Is_Empty (Decl_List) then
         Local_List_Node :=
1331
           Ocarina.ME_AADL.AADL_Tree.Nodes.First_Node (Decl_List);
1332
1333
1334
1335
1336

         while Present (Local_List_Node) loop
            Success := False;

            for K in Kinds'Range loop
1337
               Success := Success or else (Kind (Local_List_Node) = Kinds (K));
1338
1339
1340
1341
1342
            end loop;

            if Success then
               if No (First_Node) then
                  First_Node := Local_List_Node;
1343
                  Last_Node  := Local_List_Node;
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
               else
                  Set_Next_Entity (Last_Node, Local_List_Node);
                  Set_Next_Entity (Local_List_Node, No_Node);
                  Last_Node := Local_List_Node;
               end if;
            end if;

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

end Ocarina.Analyzer.AADL.Finder;