File: gps-scripts-entities.adb

package info (click to toggle)
gnat-gps 18-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,716 kB
  • sloc: ada: 362,679; python: 31,031; xml: 9,597; makefile: 1,030; ansic: 917; sh: 264; java: 17
file content (636 lines) | stat: -rw-r--r-- 22,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
------------------------------------------------------------------------------
--                                  G P S                                   --
--                                                                          --
--                        Copyright (C) 2013-2018, AdaCore                  --
--                                                                          --
-- This is free software;  you can redistribute it  and/or modify it  under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  This software is distributed in the hope  that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY 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  this  software;   see  file --
-- COPYING3.  If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license.                                                          --
------------------------------------------------------------------------------

with Basic_Types;                      use Basic_Types;
with GNATCOLL.Projects;
with GNATCOLL.VFS;
with GNATCOLL.Xref;                    use GNATCOLL.Xref;
with GPS.Core_Kernels;                 use GPS.Core_Kernels;
with GPS.Intl;                         use GPS.Intl;
with GPS.Scripts.File_Locations;
with GPS.Scripts.Files;

package body GPS.Scripts.Entities is

   type Entity_Properties_Record is new Instance_Property_Record with record
      H : Root_Entity_Ref;
   end record;

   Entity_Class_Name        : constant String := "Entity";
   --  Name of the class for shell commands associated with this package

   procedure Entity_Command_Handler
     (Data : in out Callback_Data'Class; Command : String);
   --  Handler for the "Entity" commands

   -------------------
   -- Create_Entity --
   -------------------

   function Create_Entity
     (Script : access Scripting_Language_Record'Class;
      Entity : Xref.Root_Entity'Class)
      return Class_Instance
   is
      Instance : Class_Instance;
   begin
      if Entity = No_Root_Entity then
         return No_Class_Instance;
      else
         Instance := New_Instance
           (Script, New_Class (Get_Repository (Script), Entity_Class_Name));
         Set_Data (Instance, Entity);
         return Instance;
      end if;
   end Create_Entity;

   ----------------------------
   -- Entity_Command_Handler --
   ----------------------------

   procedure Entity_Command_Handler
     (Data : in out Callback_Data'Class; Command : String)
   is
      Kernel    : constant Core_Kernel := Get_Kernel (Data);
      Entity    : constant Root_Entity'Class := Get_Data (Data, 1);
   begin
      if Command = Constructor_Method then
         declare
            Name   : constant String  := Nth_Arg (Data, 2);
            File   : constant Class_Instance  :=
              Nth_Arg (Data, 3,
                       GPS.Scripts.Files.Get_File_Class (Kernel),
                       Default    => No_Class_Instance,
                       Allow_Null => True);
            Loc    : General_Location;
            Approx_Search : constant Boolean := Nth_Arg (Data, 6, True);

         begin
            if File = No_Class_Instance then
               --  Looking for a predefined entity
               Loc := No_Location;
            else
               Loc :=
                 (File    => GPS.Scripts.Files.Get_Data (File),
                  Project_Path => GNATCOLL.VFS.No_File,  --  ??? unknown
                  Line    => Nth_Arg (Data, 4, Default => -1),
                  Column  => Visible_Column_Type
                    (Nth_Arg (Data, 5, Default => -1)));
            end if;

            declare
               Ref : Root_Entity_Reference_Ref;
               Entity : constant Root_Entity'Class :=
                 Kernel.Databases.Get_Entity
                   (Loc               => Loc,
                    Name              => Name,
                    Closest_Ref       => Ref,
                    Approximate_Search_Fallback => Approx_Search);
            begin
               if Entity = No_Root_Entity then
                  Set_Error_Msg (Data, -"Entity not found");
               else
                  declare
                     Instance : constant Class_Instance :=
                       Nth_Arg (Data, 1, Get_Entity_Class (Kernel));
                  begin
                     Set_Data (Instance, Entity);
                  end;
               end if;
            end;
         end;

      elsif Command = "full_name" then
         Set_Return_Value (Data, Entity.Qualified_Name);

      elsif Command = "name" then
         Set_Return_Value (Data, Entity.Get_Name);

      elsif Command = "category" then
         Set_Return_Value (Data, Entity.Get_Display_Kind);

      elsif Command = "attributes" then
         --  ??? Should be made obsolete and replaced by separate functions.
         declare
            Subp   : constant Root_Entity'Class := Entity.Is_Parameter_Of;
         begin
            Set_Return_Value (Data, Entity.Is_Global);
            Set_Return_Value_Key (Data, "global");

            Set_Return_Value (Data, Entity.Is_Static_Local);
            Set_Return_Value_Key (Data, "static");

            if Subp /= No_Root_Entity then
               declare
                  Params : Parameter_Array := Subp.Parameters;
               begin
                  for P in Params'Range loop
                     if Params (P).Parameter.all = Entity then
                        case Params (P).Kind is
                           when In_Parameter =>
                              Set_Return_Value (Data, True);
                              Set_Return_Value_Key (Data, "in");

                           when Out_Parameter =>
                              Set_Return_Value (Data, True);
                              Set_Return_Value_Key (Data, "out");

                           when In_Out_Parameter =>
                              Set_Return_Value (Data, True);
                              Set_Return_Value_Key (Data, "inout");

                           when Access_Parameter =>
                              Set_Return_Value (Data, True);
                              Set_Return_Value_Key (Data, "access");
                        end case;
                        exit;
                     end if;
                  end loop;

                  Free (Params);
               end;
            end if;
         end;

      elsif Command = "is_subprogram" then
         Set_Return_Value (Data, Entity.Is_Subprogram);

      elsif Command = "is_generic" then
         Set_Return_Value (Data, Entity.Is_Generic);

      elsif Command = "is_global" then
         Set_Return_Value (Data, Entity.Is_Global);

      elsif Command = "is_access" then
         Set_Return_Value (Data, Entity.Is_Access);

      elsif Command = "is_array" then
         Set_Return_Value (Data, Entity.Is_Array);

      elsif Command = "is_type" then
         Set_Return_Value (Data, Entity.Is_Type);

      elsif Command = "overrides" then
         Set_Return_Value
           (Data, Create_Entity (Get_Script (Data), Entity.Overrides));

      elsif Command = "is_container" then
         Set_Return_Value (Data, Entity.Is_Container);

      elsif Command = "declaration" then
         declare
            Location : constant General_Location :=
              Entity.Get_Declaration.Loc;
         begin
            Set_Return_Value
              (Data, GPS.Scripts.File_Locations.Create_File_Location
                 (Get_Script (Data),
                  File   => GPS.Scripts.Files.Create_File
                              (Get_Script (Data), Location.File),
                  Line   => Location.Line,
                  Column => Location.Column));
         end;

      elsif Command = "body" then
         declare
            Location     : General_Location := No_Location;
            Cur_Location : General_Location := No_Location;
            Count        : Integer := Nth_Arg (Data, 2, 1);
         begin
            while Count > 0 loop
               Location := Entity.Get_Body (After => Cur_Location);
               Count := Count - 1;
               Cur_Location := Location;
            end loop;

            if Location /= No_Location then
               Set_Return_Value
                 (Data, GPS.Scripts.File_Locations.Create_File_Location
                    (Get_Script (Data),
                     File   => GPS.Scripts.Files.Create_File
                                 (Get_Script (Data), Location.File),
                     Line   => Location.Line,
                     Column => Location.Column));

            else
               Set_Error_Msg (Data, -"Body not found for the entity");
            end if;
         end;

      elsif Command = "end_of_scope" then
         declare
            Location : General_Location := No_Location;
         begin
            Location := Entity.End_Of_Scope;
            if Location /= No_Location then
               Set_Return_Value
                 (Data, GPS.Scripts.File_Locations.Create_File_Location
                    (Get_Script (Data),
                     File   => GPS.Scripts.Files.Create_File
                                 (Get_Script (Data), Location.File),
                     Line   => Location.Line,
                     Column => Location.Column));
            else
               Set_Error_Msg (Data, -"end-of-scope not found for the entity");
            end if;
         end;

      elsif Command = "discriminants" then
         declare
            Discrs : Xref.Entity_Array := Discriminants (Entity);
         begin
            Set_Return_Value_As_List (Data);

            for D in Discrs'Range loop
               Set_Return_Value
                 (Data, Create_Entity (Get_Script (Data), Discrs (D).all));
            end loop;

            Free (Discrs);
         end;

      elsif Command = "parameters" then
         declare
            Params : Parameter_Array := Parameters (Entity);
         begin
            Set_Return_Value_As_List (Data);
            for P in Params'Range loop
               Set_Return_Value
                 (Data, Create_Entity
                    (Get_Script (Data), Params (P).Parameter.all));
            end loop;
            Free (Params);
         end;

      elsif Command = "methods" then
         declare
            Methods : Xref.Entity_Array :=
              Entity.Methods
                (Include_Inherited => Nth_Arg (Data, 2, False));
         begin
            Set_Return_Value_As_List (Data);

            for M in Methods'Range loop
               Set_Return_Value
                 (Data, Create_Entity (Get_Script (Data), Methods (M).all));
            end loop;

            Free (Methods);
         end;

      elsif Command = "return_type" then
         Set_Return_Value
           (Data, Create_Entity (Get_Script (Data), Entity.Returned_Type));

      elsif Command = "primitive_of" then
         declare
            Arr : Xref.Entity_Array := Entity.Is_Primitive_Of;
         begin
            Set_Return_Value_As_List (Data);
            for A in Arr'Range loop
               Set_Return_Value
                 (Data, Create_Entity (Get_Script (Data), Arr (A).all));
            end loop;

            Free (Arr);
         end;

      elsif Command = "pointed_type" then
         declare
            Result : constant Root_Entity'Class := Entity.Pointed_Type;
         begin
            if Result = No_Root_Entity then
               declare
                  Res_2 : constant Root_Entity'Class :=
                    Entity.Get_Type_Of.Pointed_Type;
               begin
                  Set_Return_Value
                    (Data, Create_Entity (Get_Script (Data),
                     Res_2));
               end;
            else
               Set_Return_Value
                 (Data, Create_Entity (Get_Script (Data),
                  Result));
            end if;
         end;

      elsif Command = "type" then
         Set_Return_Value
           (Data, Create_Entity (Get_Script (Data), Entity.Get_Type_Of));

      elsif Command = "instance_of" then
         Set_Return_Value
           (Data, Create_Entity (Get_Script (Data), Entity.Instance_Of));

      elsif Command = "is_predefined" then
         Set_Return_Value
           (Data, Entity.Is_Predefined_Entity);

      elsif Command = "fields" then
         declare
            F : Xref.Entity_Array := Entity.Fields;
         begin
            Set_Return_Value_As_List (Data);

            for F2 in F'Range loop
               Set_Return_Value
                 (Data, Create_Entity (Get_Script (Data), F (F2).all));
            end loop;

            Free (F);
         end;

      elsif Command = "literals" then
         declare
            F : Xref.Entity_Array := Entity.Literals;
         begin
            Set_Return_Value_As_List (Data);

            for F2 in F'Range loop
               Set_Return_Value
                 (Data, Create_Entity (Get_Script (Data), F (F2).all));
            end loop;

            Free (F);
         end;

      elsif Command = "derived_types" then
         declare
            Children : Xref.Entity_Array :=
              Entity.Child_Types (Recursive => False);
         begin
            Set_Return_Value_As_List (Data);

            for C in Children'Range loop
               Set_Return_Value
                 (Data, Create_Entity
                    (Get_Script (Data), Children (C).all));
            end loop;

            Free (Children);
         end;

      elsif Command = "get_called_entities" then
         declare
            Called_Entities : Abstract_Entities_Cursor'Class :=
              Entity.Get_All_Called_Entities;
         begin
            Set_Return_Value_As_List (Data);
            while not Called_Entities.At_End loop
               Set_Return_Value
                 (Data,
                  Create_Entity (Get_Script (Data), Called_Entities.Get));
               Called_Entities.Next;
            end loop;

            Called_Entities.Destroy;
         end;

      elsif Command = "parent_types" then
         declare
            Parents : Xref.Entity_Array :=
              Entity.Parent_Types (Recursive => Nth_Arg (Data, 2, False));
         begin
            Set_Return_Value_As_List (Data);

            for C in Parents'Range loop
               Set_Return_Value
                 (Data, Create_Entity (Get_Script (Data), Parents (C).all));
            end loop;

            Free (Parents);
         end;
      elsif Command = "child_types" then
         declare
            Parents : Xref.Entity_Array :=
              Entity.Child_Types (Recursive => Nth_Arg (Data, 2, False));
         begin
            Set_Return_Value_As_List (Data);

            for C in Parents'Range loop
               Set_Return_Value
                 (Data, Create_Entity (Get_Script (Data), Parents (C).all));
            end loop;

            Free (Parents);
         end;

      end if;
   end Entity_Command_Handler;

   --------------
   -- Get_Data --
   --------------

   function Get_Data
     (Data : Callback_Data'Class;
      N : Positive)
      return Xref.Root_Entity'Class
   is
      Kernel : constant Core_Kernel := Get_Kernel (Data);
      Class : constant Class_Type := Get_Entity_Class (Kernel);
      Inst  : constant Class_Instance := Nth_Arg
        (Data, N, Class, Allow_Null => True);
      Props : Instance_Property;
   begin
      if Inst = No_Class_Instance then
         return No_General_Entity;
      end if;

      Props := Get_Data (Inst, Entity_Class_Name);
      if Props = null then
         return No_General_Entity;
      else
         return Entity_Properties_Record (Props.all).H.Element;
      end if;
   end Get_Data;

   ----------------------
   -- Get_Entity_Class --
   ----------------------

   function Get_Entity_Class
     (Kernel : access GPS.Core_Kernels.Core_Kernel_Record'Class)
      return Class_Type is
   begin
      return New_Class (Kernel.Scripts, Entity_Class_Name);
   end Get_Entity_Class;

   -----------------------
   -- Register_Commands --
   -----------------------

   procedure Register_Commands
     (Kernel : access GPS.Core_Kernels.Core_Kernel_Record'Class)
   is
      C : constant Class_Type := Get_Entity_Class (Kernel);
   begin
      Kernel.Scripts.Register_Command
        (Constructor_Method,
         Class        => C,
         Params       => (Param ("name"),
                          Param ("file",   Optional => True),
                          Param ("line",   Optional => True),
                          Param ("column", Optional => True),
                          Param ("approximate_search_fallback", True)),
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("name",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("full_name",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("attributes",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("is_subprogram",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("is_generic",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("is_global",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("is_access",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("is_array",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("is_type",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("overrides",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("is_container",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("declaration",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("body",
         Class        => C,
         Params       => (1 => Param ("nth", Optional => True)),
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("end_of_scope",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("category",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("discriminants",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("fields",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("literals",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("is_predefined",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("parameters",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("methods",
         Class        => C,
         Params       => (2 => Param ("include_inherited", Optional => True)),
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("return_type",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("pointed_type",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("derived_types",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("parent_types",
         Class        => C,
         Params       => (2 => Param ("recursive", Optional => True)),
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("child_types",
         Class        => C,
         Params       => (2 => Param ("recursive", Optional => True)),
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("get_called_entities",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("instance_of",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("primitive_of",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
      Kernel.Scripts.Register_Command
        ("type",
         Class        => C,
         Handler      => Entity_Command_Handler'Access);
   end Register_Commands;

   --------------
   -- Set_Data --
   --------------

   procedure Set_Data
     (Instance : Class_Instance;
      Entity : Xref.Root_Entity'Class)
   is
      R : Entity_Properties_Record;
   begin
      if not Is_Subclass (Instance, Entity_Class_Name) then
         raise Invalid_Data;
      end if;

      R.H.Replace_Element (Entity);
      Set_Data (Instance, Entity_Class_Name, R);
   end Set_Data;

end GPS.Scripts.Entities;