forked from AdaCore/langkit-query-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlkql-primitives.adb
1685 lines (1451 loc) · 50.2 KB
/
lkql-primitives.adb
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
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
703
704
705
706
707
708
709
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
------------------------------------------------------------------------------
-- --
-- LKQL --
-- --
-- Copyright (C) 2019-2022, AdaCore --
-- --
-- LKQL 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 3, or (at your option) any later --
-- version. This software 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. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are --
-- granted additional permissions described in the GCC Runtime Library --
-- Exception, version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and a --
-- copy of the GCC Runtime Library Exception along with this program; see --
-- the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
------------------------------------------------------------------------------
with Ada.Assertions; use Ada.Assertions;
with Ada.Containers; use type Ada.Containers.Count_Type;
with Ada.Containers.Generic_Array_Sort;
with Ada.Directories;
with Ada.Strings.Wide_Wide_Unbounded; use Ada.Strings.Wide_Wide_Unbounded;
with Ada.Strings.Wide_Wide_Unbounded.Wide_Wide_Text_IO;
use Ada.Strings.Wide_Wide_Unbounded.Wide_Wide_Text_IO;
with Ada.Wide_Wide_Text_IO;
with Ada.Strings.Hash;
with LKQL.Eval_Contexts; use LKQL.Eval_Contexts;
with LKQL.Evaluation;
with LKQL.Error_Handling; use LKQL.Error_Handling;
with LKQL.Errors; use LKQL.Errors;
with Langkit_Support.Hashes; use Langkit_Support.Hashes;
with Langkit_Support.Names; use Langkit_Support.Names;
with GNAT.Case_Util;
with LKQL.Depth_Nodes;
package body LKQL.Primitives is
function Int_Image (Value : Adaptive_Integer) return Unbounded_Text_Type;
-- Wraps the Integer'Wide_Wide_Image function, removing the leading space
function Bool_Image (Value : Boolean) return Unbounded_Text_Type;
-- Return a String representation of the given Boolean value
function Iterator_Image
(Value : Primitive) return Unbounded_Text_Type;
function Selector_List_Image
(Value : Selector_List) return Unbounded_Text_Type;
-- Return a String representation of the given Selector_List
function List_Image (Value : Primitive_List;
Open : Text_Type := "[";
Close : Text_Type := "]") return Unbounded_Text_Type;
-- Return a String representation of the given Primitive_List value
function Object_Image (Value : Primitive_Assocs) return Unbounded_Text_Type;
-- Given a ``Primitive_Assocs``, return a textual representation as ``{key:
-- <val>, ...}``.
function Selector_List_Data (Value : Selector_List;
Member_Name : Text_Type;
Pool : Primitive_Pool) return Primitive;
-- Return the value of the property named 'Member_Name' of the given
-- Primitive Selector_List.
-- Raise an Unsupported_Error if there is no property named
-- 'Member_Name'.
function List_Data (Value : Primitive_List_Access;
Member_Name : Text_Type;
Pool : Primitive_Pool) return Primitive;
-- Return the value of the property named 'Member_Name' of the given
-- Primitive List.
-- Raise an Unsupported_Error if there is no property named
-- 'Member_Name'.
function Str_Data
(Value : Text_Type;
Member_Name : Text_Type;
Pool : Primitive_Pool) return Primitive;
-- Return the value of the property named 'Member_Name' of the given
-- Str value.
-- Raise an Unsupported_Error if there is no property named
-- 'Member_Name'.
procedure Raise_Unsupported_Operation
(Left, Right : Primitive; Name : String)
with No_Return;
-- Raise an Unsupported_Operation exception mentionning the kind of the
-- operands as well as the name of the operation.
function Create return Primitive_Pool;
-- Create a new primitive pool
procedure Destroy (Pool : in out Primitive_Pool);
-- Destroy the pool (free all the objects, and free the pool)
---------------
-- Int_Image --
---------------
function Int_Image (Value : Adaptive_Integer) return Unbounded_Text_Type is
Image : constant Text_Type := To_Text (Adaptive_Integers.Image (Value));
begin
if Image (1) = ' ' then
return To_Unbounded_Text (Image (2 .. Image'Last));
else
return To_Unbounded_Text (Image);
end if;
end Int_Image;
-----------------
-- Bool_Image --
-----------------
function Bool_Image (Value : Boolean) return Unbounded_Text_Type is
use GNAT.Case_Util;
Image : String := Boolean'Image (Value);
begin
To_Lower (Image);
return To_Unbounded_Text (To_Text (Image));
end Bool_Image;
-------------------------
-- Selector_List_Image --
-------------------------
function Selector_List_Image
(Value : Selector_List) return Unbounded_Text_Type
is
use LKQL.Depth_Nodes;
Image : Unbounded_Text_Type;
Nodes : constant Depth_Node_Vector := Value.Depth_Nodes;
Length : constant Count_Type := Nodes.Length;
Counter : Count_Type := 1;
begin
Append (Image, "[");
for D of Nodes loop
Counter := Counter + 1;
Append (Image, To_Text (D.Node.Image));
if Counter <= Length then
Append (Image, ", ");
end if;
end loop;
Append (Image, "]");
return Image;
end Selector_List_Image;
----------------
-- List_Image --
----------------
function List_Image (Value : Primitive_List;
Open : Text_Type := "[";
Close : Text_Type := "]") return Unbounded_Text_Type
is
Image : Unbounded_Text_Type;
begin
Append (Image, Open);
for I in Value.Elements.First_Index .. Value.Elements.Last_Index loop
Append (Image, To_Unbounded_Text (Value.Elements (I)));
if I < Value.Elements.Last_Index then
Append (Image, ", ");
end if;
end loop;
Append (Image, Close);
return Image;
end List_Image;
------------------
-- Object_Image --
------------------
function Object_Image (Value : Primitive_Assocs) return Unbounded_Text_Type
is
Image : Unbounded_Text_Type;
use Primitive_Maps;
begin
Append (Image, "{");
declare
type Cursor_Array
is array (Positive range <>) of Primitive_Maps.Cursor;
Cursors : Cursor_Array (1 .. Natural (Value.Elements.Length));
function "<" (L, R : Primitive_Maps.Cursor) return Boolean
is
(Key (L).all < Key (R).all);
procedure Cursor_Sort
is new Ada.Containers.Generic_Array_Sort
(Positive, Primitive_Maps.Cursor, Cursor_Array, "<");
I : Positive := 1;
begin
for Cur in Value.Elements.Iterate loop
Cursors (I) := Cur;
I := I + 1;
end loop;
Cursor_Sort (Cursors);
I := 1;
for Cur of Cursors loop
Append (Image, """" & Key (Cur).all & """");
Append (Image, ": ");
Append (Image, To_Unbounded_Text (Primitive_Maps.Element (Cur)));
if I < Positive (Value.Elements.Length) then
Append (Image, ", ");
end if;
I := I + 1;
end loop;
end;
Append (Image, "}");
return Image;
end Object_Image;
--------------------
-- Iterator_Image --
--------------------
function Iterator_Image
(Value : Primitive) return Unbounded_Text_Type
is
begin
Consume (Value);
return List_Image (Value.Iter_Cache.all);
end Iterator_Image;
----------------
-- Check_Kind --
----------------
procedure Check_Kind
(Expected_Kind : Valid_Primitive_Kind; Value : Primitive) is
begin
if Kind (Value) /= Expected_Kind then
raise Unsupported_Error
with "Type error: expected " &
To_String (Expected_Kind) & " but got " &
Kind_Name (Value);
end if;
end Check_Kind;
---------------------------------
-- Raise_Unsupported_Operation --
---------------------------------
procedure Raise_Unsupported_Operation
(Left, Right : Primitive; Name : String)
is
Message : constant String :=
"Unsupported operation: " & Kind_Name (Left) & ' ' & Name &
Kind_Name (Right);
begin
raise Unsupported_Error with Message;
end Raise_Unsupported_Operation;
-------------
-- Release --
-------------
procedure Release (Data : in out Primitive_Data) is
procedure Free_Regex is new Ada.Unchecked_Deallocation
(GNAT.Regpat.Pattern_Matcher, Regex_Access);
begin
case Data.Kind is
when Kind_List =>
Free_Primitive_List (Data.List_Val);
when Kind_Iterator =>
Data.Iter_Val.Iter.Release;
Primitive_Iters.Free_Iterator (Data.Iter_Val.Iter);
Free_Iterator_Primitive (Data.Iter_Val);
Free_Primitive_List (Data.Iter_Cache);
when Kind_Function | Kind_Selector =>
LKQL.Eval_Contexts.Dec_Ref
(LKQL.Eval_Contexts.Environment_Access (Data.Frame));
when Kind_Str =>
Free (Data.Str_Val);
when Kind_Namespace =>
LKQL.Eval_Contexts.Dec_Ref
(LKQL.Eval_Contexts.Environment_Access (Data.Namespace));
when Kind_Object =>
Free_Primitive_Assocs (Data.Obj_Assocs);
when Kind_Builtin_Function =>
-- We don't ever free built-in functions, since their data is
-- freed directly in the builtin functions package.
null;
when Kind_Regex =>
Free_Regex (Data.Regex_Val);
when others =>
null;
end case;
end Release;
--------------
-- Get_Iter --
--------------
function Get_Iter (Value : Iterator_Primitive) return Primitive_Iter_Access
is
begin
return new Primitive_Iters.Iterator_Interface'Class'
(Primitive_Iters.Iterator_Interface'Class (Value.Iter.Clone));
end Get_Iter;
-------------
-- Consume --
-------------
procedure Consume (Iter : Primitive; Num_Elements : Integer := -1)
is
Element : Primitive;
Consumed : Natural := 0;
begin
while (Num_Elements = -1 or else Consumed < Num_Elements)
and then Iter.Iter_Val.Iter.Next (Element)
loop
Iter.Iter_Cache.Elements.Append (Element);
Consumed := Consumed + 1;
end loop;
end Consume;
-----------------
-- To_Iterator --
-----------------
function To_Iterator
(Value : Primitive; Pool : Primitive_Pool) return Primitive_Iter'Class
is
(case Value.Kind is
when Kind_Iterator =>
Primitive_Iter'Class (Iter_Val (Value).Iter.Clone),
when Kind_List =>
Primitive_Vec_Iters.To_Iterator (Elements (Value).all),
when Kind_Selector_List =>
To_Iterator (To_List (Selector_List_Val (Value), Pool), Pool),
when others =>
raise Assertion_Error with
"Cannot get an iterator from a value of kind : " &
Kind_Name (Value));
-------------
-- To_List --
-------------
function To_List
(Value : Selector_List; Pool : Primitive_Pool) return Primitive
is
begin
return Result : constant Primitive := Make_Empty_List (Pool) do
for N of Value.Nodes loop
Append (Result, To_Primitive (N, Pool));
end loop;
end return;
end To_List;
----------
-- Kind --
----------
function Kind (Value : Primitive) return Valid_Primitive_Kind is
(Value.Kind);
-------------
-- Int_Val --
-------------
function Int_Val (Value : Primitive) return Adaptive_Integer is
(Value.Int_Val);
-------------
-- Str_Val --
-------------
function Str_Val (Value : Primitive) return Text_Type is
(Value.Str_Val.all);
--------------
-- Bool_Val --
--------------
function Bool_Val (Value : Primitive) return Boolean is
(Value.Bool_Val);
--------------
-- Node_Val --
--------------
function Node_Val (Value : Primitive) return LK.Lk_Node is
(Value.Node_Val);
--------------
-- List_Val --
--------------
function List_Val (Value : Primitive) return Primitive_List_Access is
(Value.List_Val);
-----------------------
-- Selector_List_Val --
-----------------------
function Selector_List_Val (Value : Primitive) return Selector_List is
(Value.Selector_List_Val);
--------------
-- Iter_Val --
--------------
function Iter_Val (Value : Primitive) return Iterator_Primitive_Access is
(Value.Iter_Val);
--------------
-- Elements --
--------------
function Elements
(Value : Primitive) return not null Primitive_Vector_Access is
begin
case Value.Kind is
when Kind_List =>
return Value.List_Val.Elements'Access;
when Kind_Iterator =>
Consume (Value);
return Value.Iter_Cache.Elements'Access;
when others =>
raise Unsupported_Error with "Invalid kind for elements";
end case;
end Elements;
------------------------
-- Selector_List_Data --
------------------------
function Selector_List_Data
(Value : Selector_List;
Member_Name : Text_Type;
Pool : Primitive_Pool) return Primitive
is
begin
if Member_Name = "max_depth" then
return To_Primitive (Value.Max_Depth, Pool);
elsif Member_Name = "nodes" then
return To_List (Value, Pool);
else
return List_Data
(List_Val (To_List (Value, Pool)), Member_Name, Pool);
end if;
exception
when Unsupported_Error =>
raise Unsupported_Error with
"No property named " & To_UTF8 (Member_Name) &
" on values of kind " & To_String (Kind_Selector_List);
end Selector_List_Data;
---------------
-- List_Data --
---------------
function List_Data (Value : Primitive_List_Access;
Member_Name : Text_Type;
Pool : Primitive_Pool) return Primitive
is
begin
if Member_Name = "length" then
return To_Primitive (Integer (Value.Elements.Length), Pool);
else
raise Unsupported_Error with
"No property named " & To_UTF8 (Member_Name) &
" on values of kind " & To_String (Kind_List);
end if;
end List_Data;
------------------
-- Str_Property --
------------------
function Str_Data
(Value : Text_Type;
Member_Name : Text_Type;
Pool : Primitive_Pool) return Primitive
is
begin
if Member_Name = "length" then
return To_Primitive (Value'Length, Pool);
else
raise Unsupported_Error with
"No property named " & To_UTF8 (Member_Name) &
" on values of kind " & To_String (Kind_Str);
end if;
end Str_Data;
--------------
-- Property --
--------------
function Data
(Value : Primitive;
Member_Name : Text_Type;
Pool : Primitive_Pool) return Primitive
is
begin
case Kind (Value) is
when Kind_Selector_List =>
return Selector_List_Data
(Selector_List_Val (Value), Member_Name, Pool);
when Kind_List =>
return List_Data (List_Val (Value), Member_Name, Pool);
when Kind_Str =>
return Str_Data (Str_Val (Value), Member_Name, Pool);
when Kind_Iterator =>
Consume (Value);
return List_Data (Value.Iter_Cache, Member_Name, Pool);
when others =>
raise Unsupported_Error with
"Cannot get property on value of kind "
& Kind_Name (Value);
end case;
end Data;
----------------
-- Is_Nullish --
----------------
function Is_Nullish (Value : Primitive) return Boolean
is
((Kind (Value) = Kind_Node
and then Value.Node_Val.Is_Null)
or else Kind (Value) = Kind_Unit);
----------------
-- Booleanize --
----------------
function Booleanize (Value : Primitive) return Boolean is
begin
return (if (Value.Kind = Kind_Bool
and then not Value.Bool_Val)
or else Value.Kind = Kind_Unit
or else (Value.Kind = Kind_Node
and then Value.Node_Val.Is_Null)
then False
else True);
end Booleanize;
------------
-- Truthy --
------------
function Truthy (Value : Primitive; Has_Truthy : out Boolean) return Boolean
is
begin
Has_Truthy := True;
case Kind (Value) is
when Kind_Node | Kind_Unit =>
return Booleanize (Value);
when Kind_Bool =>
return Bool_Val (Value);
when Kind_Iterator =>
declare
Iterator_Clone : Primitive_Iters.Iterator_Interface'Class
:= Primitive_Iters.Clone (Iter_Val (Value).Iter.all);
Dummy_Element : Primitive;
begin
return Primitive_Iters.Next (Iterator_Clone, Dummy_Element);
end;
when Kind_List =>
return Primitives.Length (Value) /= 0;
when others =>
Has_Truthy := False;
return False;
end case;
end Truthy;
------------------
-- To_Primitive --
------------------
function To_Primitive
(Val : Integer; Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive
((Kind => Kind_Int, Int_Val => Create (Val), Pool => Pool));
end To_Primitive;
------------------
-- To_Primitive --
------------------
--
function To_Primitive
(Val : Adaptive_Integer; Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive
((Kind => Kind_Int, Int_Val => Val, Pool => Pool));
end To_Primitive;
------------------
-- To_Primitive --
------------------
function To_Primitive
(Val : Text_Type; Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive
((Kind => Kind_Str, Str_Val => new Text_Type'(Val), Pool => Pool));
end To_Primitive;
------------------
-- To_Primitive --
------------------
function To_Primitive
(Node : LK.Lk_Node; Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive ((Kind_Node, Pool, Node));
end To_Primitive;
------------------
-- To_Primitive --
------------------
function To_Primitive
(Token : LK.Lk_Token; Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive ((Kind_Token, Pool, Token));
end To_Primitive;
------------------
-- To_Primitive --
------------------
function To_Primitive
(Unit : LK.Lk_Unit; Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive ((Kind_Analysis_Unit, Pool, Unit));
end To_Primitive;
------------------
-- To_Primitive --
------------------
function To_Primitive
(Val : Primitive_Iter'Class; Pool : Primitive_Pool) return Primitive
is
Val_Copy : constant Primitive_Iter_Access :=
new Primitive_Iter'Class'(Primitive_Iter'Class (Val.Clone));
Iter_Primitive : constant Iterator_Primitive_Access :=
new Iterator_Primitive'(Iter => Val_Copy);
List : constant Primitive_List_Access :=
new Primitive_List'(Elements => Primitive_Vectors.Empty_Vector);
begin
return Create_Primitive
((Kind_Iterator, Pool, Iter_Primitive, Iter_Cache => List));
end To_Primitive;
------------------
-- To_Primitive --
------------------
function To_Primitive
(Val : Selector_List; Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive ((Kind_Selector_List, Pool, Val));
end To_Primitive;
-----------------------
-- Make_Empty_Object --
-----------------------
function Make_Empty_Object (Pool : Primitive_Pool) return Primitive is
Map : constant Primitive_Assocs_Access :=
new Primitive_Assocs'(Elements => Primitive_Maps.Empty_Map);
begin
return Create_Primitive
((Kind => Kind_Object, Obj_Assocs => Map, Pool => Pool));
end Make_Empty_Object;
---------------------
-- Make_Empty_List --
---------------------
function Make_Empty_List (Pool : Primitive_Pool) return Primitive is
List : constant Primitive_List_Access :=
new Primitive_List'(Elements => Primitive_Vectors.Empty_Vector);
begin
return Create_Primitive
((Kind => Kind_List, List_Val => List, Pool => Pool));
end Make_Empty_List;
----------------------
-- Make_Empty_Tuple --
----------------------
function Make_Empty_Tuple (Pool : Primitive_Pool) return Primitive is
List : constant Primitive_List_Access :=
new Primitive_List'(Elements => Primitive_Vectors.Empty_Vector);
begin
return Create_Primitive
((Kind => Kind_Tuple, List_Val => List, Pool => Pool));
end Make_Empty_Tuple;
--------------------
-- Make_Namespace --
--------------------
function Make_Namespace
(N : Environment_Access;
Module : L.Lkql_Node;
Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive
((Kind => Kind_Namespace,
Namespace => N,
Module => Module,
Pool => Pool));
end Make_Namespace;
----------------
-- Make_Regex --
----------------
function Make_Regex
(Regex : GNAT.Regpat.Pattern_Matcher;
Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive
((Kind => Kind_Regex,
Regex_Val => new GNAT.Regpat.Pattern_Matcher'(Regex),
Pool => Pool));
end Make_Regex;
-------------------
-- Make_Function --
-------------------
function Make_Function
(Node : L.Base_Function;
Env : Environment_Access;
Pool : Primitive_Pool;
With_Call_Cache : Boolean := False) return Primitive is
begin
return Create_Primitive
((Kind => Kind_Function,
Fun_Node => Node,
Frame => Env,
Pool => Pool,
Call_Cache => (if With_Call_Cache
then Callable_Caches.Create (Pool)
else Callable_Caches.No_Cache)));
end Make_Function;
-----------------------------
-- Make_Property_Reference --
-----------------------------
function Make_Property_Reference
(Node_Val : LK.Lk_Node;
Property_Ref : LKI.Struct_Member_Ref;
Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive
((Kind => Kind_Property_Reference,
Ref => Property_Ref,
Property_Node => Node_Val,
Pool => Pool));
end Make_Property_Reference;
-------------
-- Profile --
-------------
function Profile (Obj : Primitive) return Text_Type is
Profile : Unbounded_Text_Type;
begin
case Obj.Kind is
when Kind_Function =>
Profile := To_Unbounded_Text
(Obj.Fun_Node.P_Profile);
when Kind_Selector =>
Profile := To_Unbounded_Text
("selector " & Obj.Sel_Node.F_Name.Text);
when Kind_Builtin_Function =>
declare
P : constant Builtin_Function := Obj.Builtin_Fn;
package U renames Ada.Strings.Wide_Wide_Unbounded;
begin
U.Append (Profile, "@builtin fun ");
U.Append (Profile, P.Name);
U.Append (Profile, "(");
for I in P.Params'Range loop
U.Append (Profile, P.Params (I).Name);
if Primitive_Options.Is_Some (P.Params (I).Default_Value)
then
U.Append
(Profile,
"=" & To_Unbounded_Text
(Primitive_Options.Extract
(P.Params (I).Default_Value)));
end if;
if I < P.Params'Last then
U.Append (Profile, ", ");
end if;
end loop;
U.Append (Profile, ")");
end;
when others =>
null;
end case;
return To_Text (Profile);
end Profile;
---------------------------
-- Make_Builtin_Function --
---------------------------
function Make_Builtin_Function
(Fn : Builtin_Function; Pool : Primitive_Pool) return Primitive
is
begin
return Create_Primitive ((Kind_Builtin_Function, Pool, Fn));
end Make_Builtin_Function;
-------------------
-- Make_Selector --
-------------------
function Make_Selector
(Node : L.Selector_Decl;
Env : Environment_Access;
Pool : Primitive_Pool;
With_Call_Cache : Boolean := False) return Primitive
is
begin
return Create_Primitive
((Kind => Kind_Selector,
Sel_Node => Node,
Frame => Env,
Pool => Pool,
Sel_Cache => (if With_Call_Cache
then new Node_To_Nodes.Map
else null)));
end Make_Selector;
------------
-- Append --
------------
procedure Append (List, Element : Primitive) is
List_Elements : constant Primitive_Vector_Access :=
Elements (List);
begin
Check_Kind (Kind_List, List);
List_Elements.Append (Element);
end Append;
procedure Extend_With_List (List : Primitive_List_Access;
New_values : Primitive_List_Access);
procedure Extend_With_Iter (List : Primitive_List_Access;
Iter : Iterator_Primitive_Access);
------------
-- Extend --
------------
procedure Extend (List, New_Value : Primitive) is
begin
Check_Kind (Kind_List, List);
case Kind (New_Value) is
when Kind_List =>
Extend_With_List (List_Val (List), List_Val (New_Value));
when Kind_Iterator =>
Extend_With_Iter (List_Val (List), Iter_Val (New_Value));
when others =>
Append (List, New_Value);
end case;
end Extend;
----------------------
-- Extend_With_List --
----------------------
procedure Extend_With_List (List : Primitive_List_Access;
New_values : Primitive_List_Access)
is
begin
for E of New_values.Elements loop
List.Elements.Append (E);
end loop;
end Extend_With_List;
----------------------
-- Extend_With_Iter --
----------------------
procedure Extend_With_Iter (List : Primitive_List_Access;
Iter : Iterator_Primitive_Access)
is
Iter_Copy : Primitive_Iter'Class :=
Iter.Iter.Clone;
Current_Element : Primitive;
begin
while Iter_Copy.Next (Current_Element) loop
List.Elements.Append (Current_Element);
end loop;
Iter_Copy.Release;
end Extend_With_Iter;
--------------
-- Contains --
--------------
function Contains
(List, Value : Primitive) return Boolean
is
begin
Check_Kind (Kind_List, List);
-- Since we're using smart pointers, the "=" function used by
-- Vector.Contains checks referencial equality instead of structural
-- equality. So the iteration "has" to be done manually.
for Elem of List.List_Val.Elements loop
if Deep_Equals (Elem, Value) then
return True;
end if;
end loop;
return False;
end Contains;
---------
-- Get --
---------
function Get
(List : Primitive; Index : Integer;
Raise_If_OOB : Boolean := True) return Primitive
is
begin
return Get (List.List_Val, Index, Raise_If_OOB);
end Get;
function Get
(List : Primitive_List_Access; Index : Integer;
Raise_If_OOB : Boolean := True) return Primitive
is
begin
if Index not in List.Elements.First_Index .. List.Elements.Last_Index
then
if Raise_If_OOB then
raise Unsupported_Error
with "Invalid index: " & Integer'Image (Index);
else
return Make_Unit_Primitive;
end if;
end if;
return List.Elements.Element (Positive (Index));
end Get;
------------