File: functionselectors.cpp

package info (click to toggle)
cupt 2.10.4%2Bnmu2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,144 kB
  • sloc: cpp: 23,642; perl: 1,599; sh: 40; makefile: 19
file content (1293 lines) | stat: -rw-r--r-- 36,830 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
/**************************************************************************
*   Copyright (C) 2012 by Eugene V. Lyubimkin                             *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License                  *
*   (version 3 or above) as published by the Free Software Foundation.    *
*                                                                         *
*   This program 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 GPL                        *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA               *
**************************************************************************/
#include <common/regex.hpp>

#include <cupt/cache/releaseinfo.hpp>
#include <cupt/cache/binarypackage.hpp>
#include <cupt/cache/sourcepackage.hpp>
#include <cupt/cache/binaryversion.hpp>
#include <cupt/cache/sourceversion.hpp>
#include <cupt/system/state.hpp>

#include "functionselectors.hpp"

FunctionalSelector::Query::Query()
{}

FunctionalSelector::Query::~Query()
{}

namespace {

typedef FunctionalSelector::Query FS; // rename to FSQ
typedef const Version* SPCV; // former shared_ptr< const Version >
typedef list< SPCV > FSResult;
typedef BinaryVersion::RelationTypes BRT;
typedef SourceVersion::RelationTypes SRT;

const RelationLine& getRelationLine(const Version* v, BRT::Type relationType)
{
	return static_cast< const BinaryVersion* >(v)->relations[relationType];
}
RelationLine getRelationLine(const Version* v, SRT::Type relationType)
{
	return static_cast< const SourceVersion* >(v)->relations[relationType].toRelationLine("");
}

template < typename RelationType >
struct IsBinary
{};
template <>
struct IsBinary< BRT::Type >
{
	static const bool value = true;
};
template <>
struct IsBinary< SRT::Type >
{
	static const bool value = false;
};


bool __spcv_less(const Cache& cache, const SPCV& left, const SPCV& right)
{
	if (left->packageName > right->packageName)
	{
		return true;
	}
	if (left->packageName < right->packageName)
	{
		return false;
	}
	auto leftPin = cache.getPin(left);
	auto rightPin = cache.getPin(right);
	if (leftPin < rightPin)
	{
		return true;
	}
	if (leftPin > rightPin)
	{
		return false;
	}
	return left->versionString < right->versionString;
}
struct SpcvGreater
{
 private:
	const Cache& __cache;
 public:
	SpcvGreater(const Cache& cache) : __cache(cache) {}
	bool operator()(const SPCV& left, const SPCV& right)
	{
		return __spcv_less(__cache, right, left);
	}
};

class VersionSetGetter
{
	bool __binary; // source if false
	const Cache& __cache;
	mutable FSResult* __cached_all_versions;

	static vector< string > __sort(vector< string >&& input)
	{
		std::sort(input.begin(), input.end());
		return input;
	}
	Range< Cache::PackageNameIterator > __get_package_names() const
	{
		return __binary ? __cache.getBinaryPackageNames() : __cache.getSourcePackageNames();
	}
	const Package* __get_package(const string& packageName) const
	{
		return __binary ? (const Package*)__cache.getBinaryPackage(packageName)
				: (const Package*)__cache.getSourcePackage(packageName);
	}
	void __add_package_to_result(const string& packageName, FSResult* result) const
	{
		// we call getSortedPinnedVersions() to place versions of the same package in the preference order
		for (auto&& pinnedVersion: __cache.getSortedVersionsWithPriorities(__get_package(packageName)))
		{
			result->emplace_back(std::move(pinnedVersion.version));
		}
	}
 public:
	explicit VersionSetGetter(const Cache& cache, bool binary)
		: __binary(binary), __cache(cache), __cached_all_versions(NULL)
	{}
	const FSResult& getAll() const
	{
		if (!__cached_all_versions)
		{
			__cached_all_versions = new FSResult;
			for (const string& packageName: __sort(__get_package_names().asVector()))
			{
				__add_package_to_result(packageName, __cached_all_versions);
			}
		}
		return *__cached_all_versions;
	}
	FSResult get(const sregex& regex) const
	{
		FSResult result;

		smatch m;
		vector< string > matchedPackageNames;
		for (const string& packageName: __get_package_names())
		{
			if (regex_match(packageName, m, regex))
			{
				matchedPackageNames.push_back(packageName);
			}
		}
		for (const string& packageName: __sort(std::move(matchedPackageNames)))
		{
			__add_package_to_result(packageName, &result);
		}

		return result;
	}
	~VersionSetGetter()
	{
		delete __cached_all_versions;
	}
};

class VersionSet
{
	const VersionSetGetter* __binary_getter;
	const VersionSetGetter* __source_getter;
	const VersionSetGetter* __current_getter;
	bool __filtered;
	FSResult __versions;
	std::map< string, FSResult > __variables;

	VersionSet(const VersionSet& from, FSResult&& versions)
		: __binary_getter(from.__binary_getter)
		, __source_getter(from.__source_getter)
		, __current_getter(from.__current_getter)
		, __filtered(true), __versions(std::move(versions)), __variables(from.__variables)
	{}
 public:
	explicit VersionSet(const VersionSetGetter* binaryGetter, const VersionSetGetter* sourceGetter)
		: __binary_getter(binaryGetter), __source_getter(sourceGetter), __filtered(false)
	{}
	VersionSet generate(FSResult&& versions) const
	{
		return VersionSet(*this, std::move(versions));
	}
	void selectGetterType(bool binary)
	{
		__current_getter = binary ? __binary_getter : __source_getter;
	}
	const FSResult& get() const
	{
		if (__filtered)
		{
			return __versions;
		}
		else
		{
			return __current_getter->getAll();
		}
	}
	FSResult get(const sregex& regex) const
	{
		if (__filtered)
		{
			smatch m;
			FSResult result(__versions);
			result.remove_if([&regex, &m](const SPCV& version)
			{
				return !regex_match(version->packageName, m, regex);
			});
			return result;
		}
		else
		{
			return __current_getter->get(regex);
		}
	}
	void setVariable(const string& name, FSResult&& versions)
	{
		__variables[name] = std::move(versions);
	}
	const FSResult& getFromVariable(const string& name) const
	{
		auto it = __variables.find(name);
		if (it == __variables.end())
		{
			fatal2("the variable '%s' is not defined", name);
		}
		return it->second;
	}
	VersionSet getUnfiltered() const
	{
		VersionSet result(__binary_getter, __source_getter);
		result.__variables = this->__variables;
		result.__current_getter = this->__current_getter;
		return result;
	}
	bool isFiltered() const
	{
		return __filtered;
	}
};

struct Context
{
	const Cache& cache;
	ReverseDependsIndex< BinaryVersion > reverseIndex;
	ReverseDependsIndex< SourceVersion > reverseBuildIndex;

	Context(const Cache& cache_)
		: cache(cache_), reverseIndex(cache), reverseBuildIndex(cache)
	{}
	SpcvGreater getSorter() const
	{
		return SpcvGreater(cache);
	}
	void mergeFsResults(FSResult* main, FSResult&& other)
	{
		main->merge(std::move(other), getSorter());
		main->unique();
	}
	FSResult filterThrough(const FSResult& versions, const VersionSet& allowedSet)
	{
		if (allowedSet.isFiltered())
		{
			const auto& allowedVersions = allowedSet.get();
			FSResult result;
			std::set_intersection(allowedVersions.begin(), allowedVersions.end(),
					versions.begin(), versions.end(),
					std::back_inserter(result), getSorter());
			return result;
		}
		else
		{
			return versions;
		}
	}
};

class CommonFS: public FS
{
 public:
	typedef vector< string > Arguments;
	virtual FSResult select(Context&, const VersionSet& from) const = 0;
};

unique_ptr< CommonFS > internalParseFunctionQuery(const string& query, bool binary);

void __require_n_arguments(const CommonFS::Arguments& arguments, size_t n)
{
	if (arguments.size() != n)
	{
		fatal2(__("the function requires exactly %zu arguments"), n);
	}
}

class BestFS: public CommonFS
{
	unique_ptr< CommonFS > __leaf_fs;
 public:
	BestFS(bool binary, const Arguments& arguments)
	{
		__require_n_arguments(arguments, 1);
		__leaf_fs = internalParseFunctionQuery(arguments[0], binary);
	}
	FSResult select(Context& context, const VersionSet& from) const
	{
		auto result = __leaf_fs->select(context, from);
		result.unique([](const SPCV& left, const SPCV& right) { return left->packageName == right->packageName; });
		return result;
	}
};

class DefineVariableFS: public CommonFS
{
	string __name;
	unique_ptr< CommonFS > __value_fs;
	unique_ptr< CommonFS > __leaf_fs;
 public:
	DefineVariableFS(bool binary, const Arguments& arguments)
	{
		__require_n_arguments(arguments, 3);
		__name = arguments[0];
		__value_fs = internalParseFunctionQuery(arguments[1], binary);
		__leaf_fs = internalParseFunctionQuery(arguments[2], binary);
	}
	FSResult select(Context& context, const VersionSet& from) const
	{
		VersionSet modifiedFrom(from);
		modifiedFrom.setVariable(__name,
				__value_fs->select(context, from.getUnfiltered()));
		return __leaf_fs->select(context, modifiedFrom);
	}
};

class ExtractVariableFS: public CommonFS
{
	const string __name;
 public:
	ExtractVariableFS(const string& name, const Arguments& arguments)
		: __name(name)
	{
		__require_n_arguments(arguments, 0);
	}
	FSResult select(Context& context, const VersionSet& from) const
	{
		return context.filterThrough(from.getFromVariable(__name), from);
	}
};

class AlgeFS: public CommonFS
{
 protected:
	list< unique_ptr< CommonFS > > _leaves;
 public:
	// postcondition: _leaves are not empty
	AlgeFS(bool binary, const Arguments& arguments)
	{
		if (arguments.empty())
		{
			fatal2(__("the function should have at least one argument"));
		}
		for (const auto& argument: arguments)
		{
			_leaves.push_back(internalParseFunctionQuery(argument, binary));
		}
	}
};

class AndFS: public AlgeFS
{
 public:
	AndFS(bool binary, const Arguments& arguments)
		: AlgeFS(binary, arguments)
	{}
	FSResult select(Context& context, const VersionSet& from) const
	{
		auto result = _leaves.front()->select(context, from);
		for (auto it = ++_leaves.begin(); it != _leaves.end(); ++it)
		{
			result = (*it)->select(context, from.generate(std::move(result)));
		}
		return result;
	}
};

class NotFS: public AlgeFS
{
	static const Arguments& __check_and_return_arguments(const Arguments& arguments)
	{
		__require_n_arguments(arguments, 1);
		return arguments;
	}
 public:
	NotFS(bool binary, const Arguments& arguments)
		: AlgeFS(binary, __check_and_return_arguments(arguments))
	{}
	FSResult select(Context& context, const VersionSet& from) const
	{
		const auto& fromVersions = from.get();
		auto notVersions = _leaves.front()->select(context, from);
		FSResult result;
		std::set_difference(fromVersions.begin(), fromVersions.end(),
				notVersions.begin(), notVersions.end(),
				std::back_inserter(result), context.getSorter());
		return result;
	}
};

class XorFS: public AlgeFS
{
	static const Arguments& __check_and_return_arguments(const Arguments& arguments)
	{
		__require_n_arguments(arguments, 2);
		return arguments;
	}
 public:
	XorFS(bool binary, const Arguments& arguments)
		: AlgeFS(binary, __check_and_return_arguments(arguments))
	{}
	FSResult select(Context& context, const VersionSet& from) const
	{
		auto leftVersions = _leaves.front()->select(context, from);
		auto rightVersions = _leaves.back()->select(context, from);
		FSResult result;
		std::set_symmetric_difference(leftVersions.begin(), leftVersions.end(),
				rightVersions.begin(), rightVersions.end(),
				std::back_inserter(result), context.getSorter());
		return result;
	}
};

// for determining, is function selector binary or source
class BinaryTagDummyFS: public CommonFS
{
	unique_ptr< CommonFS > __real_fs;
 public:
	BinaryTagDummyFS(unique_ptr< CommonFS >&& realFS)
		: __real_fs(std::move(realFS))
	{}
	FSResult select(Context& context, const VersionSet& from) const
	{
		return __real_fs->select(context, from);
	}
};

class OrFS: public AlgeFS
{
 public:
	OrFS(bool binary, const Arguments& arguments)
		: AlgeFS(binary, arguments)
	{}
	FSResult select(Context& context, const VersionSet& from) const
	{
		auto result = _leaves.front()->select(context, from);
		for (auto it = ++_leaves.begin(); it != _leaves.end(); ++it)
		{
			auto part = (*it)->select(context, from);
			context.mergeFsResults(&result, std::move(part));
		}
		return result;
	}
};

class PredicateFS: public CommonFS
{
 protected:
	virtual bool _match(const Cache&, const SPCV&) const = 0;
 public:
	FSResult select(Context& context, const VersionSet& from) const
	{
		FSResult result = from.get();
		result.remove_if([this, &context](const SPCV& version) { return !this->_match(context.cache, version); });
		return result;
	}
};

sregex __parse_regex(const string& input)
{
	try
	{
		return sregex::compile(input, regex_constants::optimize);
	}
	catch (regex_error&)
	{
		fatal2(__("regular expression '%s' is not valid"), input);
		__builtin_unreachable();
	}
}

sregex __get_regex_from_arguments(const CommonFS::Arguments& arguments)
{
	__require_n_arguments(arguments, 1);
	return __parse_regex(arguments[0]);
}

class RegexMatcher
{
	sregex __regex;
	mutable smatch __m;

 public:
	RegexMatcher(const CommonFS::Arguments& arguments)
		: __regex(__get_regex_from_arguments(arguments))
	{}
	bool match(const string& input) const
	{
		return regex_match(input, __m, __regex);
	}
};

class PackageNameFS: public CommonFS
{
	sregex __regex;
 public:
	PackageNameFS(const Arguments& arguments)
		: __regex(__get_regex_from_arguments(arguments))
	{}
	FSResult select(Context&, const VersionSet& from) const
	{
		return from.get(__regex);
	}
};

class RegexMatchBaseFS: public PredicateFS
{
 protected:
	RegexMatcher _matcher;
 public:
	RegexMatchBaseFS(const Arguments& arguments)
		: _matcher(arguments)
	{}
};

class RegexMatchFS: public RegexMatchBaseFS
{
	std::function< string (const Cache&, const SPCV&) > __get_attribute;
 public:
	RegexMatchFS(decltype(__get_attribute) getAttribute, const Arguments& arguments)
		: RegexMatchBaseFS(arguments), __get_attribute(getAttribute)
	{}
 protected:
	bool _match(const Cache& cache, const SPCV& version) const
	{
		return _matcher.match(__get_attribute(cache, version));
	}
};

class SourceRegexMatchFS: public RegexMatchBaseFS
{
	std::function< string (const Version::Source&) > __get_source_attribute;
 public:
	SourceRegexMatchFS(decltype(__get_source_attribute) getter, const Arguments& arguments)
		: RegexMatchBaseFS(arguments), __get_source_attribute(getter)
	{}
 protected:
	bool _match(const Cache&, const SPCV& version) const
	{
		for (const auto& source: version->sources)
		{
			if (_matcher.match(__get_source_attribute(source)))
			{
				return true;
			}
		}
		return false;
	}
};

class ProvidesFS: public RegexMatchBaseFS
{
 public:
	ProvidesFS(const Arguments& arguments)
		: RegexMatchBaseFS(arguments)
	{}
 protected:
	bool _match(const Cache&, const SPCV& v) const
	{
		auto version = static_cast< const BinaryVersion* >(v);
		for (const auto& elem: version->provides)
		{
			if (_matcher.match(elem.packageName))
			{
				return true;
			}
		}
		return false;
	}
};

class UploadersFS: public RegexMatchBaseFS
{
 public:
	UploadersFS(const Arguments& arguments)
		: RegexMatchBaseFS(arguments)
	{}
 protected:
	bool _match(const Cache&, const SPCV& v) const
	{
		auto version = static_cast< const SourceVersion* >(v);
		for (const string& uploader: version->uploaders)
		{
			if (_matcher.match(uploader)) return true;
		}
		return false;
	}
};

class BoolMatchFS: public PredicateFS
{
	std::function< bool (const Cache&, const SPCV&) > __get_attribute;
 public:
	BoolMatchFS(decltype(__get_attribute) getAttribute, const Arguments& arguments)
		: __get_attribute(getAttribute)
	{
		__require_n_arguments(arguments, 0);
	}
	bool _match(const Cache& cache, const SPCV& version) const
	{
		return __get_attribute(cache, version);
	}
};

class OtherFieldRegexMatchFS: public RegexMatchFS
{
	string __field_name;
	static string __extract_second_argument(const Arguments& arguments)
	{
		__require_n_arguments(arguments, 2);
		return arguments[1];
	}
 public:
	OtherFieldRegexMatchFS(const Arguments& arguments)
		: RegexMatchFS([this](const Cache&, const SPCV& version) -> string
				{
					if (!version->others)
					{
						return string();
					}
					auto it = version->others->find(this->__field_name);
					if (it != version->others->end())
					{
						return it->second;
					}
					else
					{
						return string();
					}
				}, { __extract_second_argument(arguments) }),
		__field_name(arguments[0])
	{}
};

class TransformFS: public CommonFS
{
	unique_ptr< CommonFS > __leaf;
	bool __binary;
 protected:
	virtual FSResult _transform(Context&, const SPCV& version) const = 0;
 public:
	TransformFS(bool binary, const Arguments& arguments)
		: __binary(binary)
	{
		__require_n_arguments(arguments, 1);
		__leaf = internalParseFunctionQuery(arguments[0], binary);
	}
	FSResult select(Context& context, const VersionSet& from) const
	{
		FSResult allTransformed;
		auto newVersionSet = from.getUnfiltered();
		newVersionSet.selectGetterType(__binary);
		for (const auto& version: __leaf->select(context, newVersionSet))
		{
			auto transformedList = _transform(context, version);
			context.mergeFsResults(&allTransformed, std::move(transformedList));
		}
		return context.filterThrough(allTransformed, from);
	}
};

class RecursiveFS: public CommonFS
{
	string __variable_name;
	unique_ptr< CommonFS > __initial_variable_value_fs;
	unique_ptr< CommonFS > __iterating_fs;

 public:
	RecursiveFS(bool binary, const Arguments& arguments)
	{
		__require_n_arguments(arguments, 3);
		__variable_name = arguments[0];
		__initial_variable_value_fs = internalParseFunctionQuery(arguments[1], binary);
		__iterating_fs = internalParseFunctionQuery(arguments[2], binary);
	}
	FSResult select(Context& context, const VersionSet& from) const
	{
		FSResult result;

		FSResult variableValue = __initial_variable_value_fs->select(context, from.getUnfiltered());
		size_t previousResultSize;
		do
		{
			previousResultSize = result.size();

			VersionSet iterationSet = from.getUnfiltered();
			iterationSet.setVariable(__variable_name, std::move(variableValue));
			context.mergeFsResults(&result, __iterating_fs->select(context, iterationSet));

			variableValue = result;
		}
		while (result.size() != previousResultSize);

		return context.filterThrough(result, from);
	}
};

template < typename RelationType >
class DependencyFS: public TransformFS
{
	const RelationType __relation_type;
 public:
	DependencyFS(RelationType relationType, const Arguments& arguments)
		: TransformFS(IsBinary<RelationType>::value, arguments), __relation_type(relationType)
	{}
 protected:
	FSResult _transform(Context& context, const SPCV& version) const
	{
		auto sorter = context.getSorter();
		FSResult result;

		for (const auto& relationExpression: getRelationLine(version, __relation_type))
		{
			auto satisfyingVersions = context.cache.getSatisfyingVersions(relationExpression);
			std::sort(satisfyingVersions.begin(), satisfyingVersions.end(), sorter);
			list< SPCV > sortedList;
			std::move(satisfyingVersions.begin(), satisfyingVersions.end(),
					std::back_inserter(sortedList));
			context.mergeFsResults(&result, std::move(sortedList));
		}
		return result;
	}
};

class ReverseDependencyFS: public TransformFS
{
	BRT::Type __relation_type;
 public:
	ReverseDependencyFS(BRT::Type relationType, const Arguments& arguments)
		: TransformFS(true, arguments), __relation_type(relationType)
	{}
 protected:
	FSResult _transform(Context& context, const SPCV& version) const
	{
		context.reverseIndex.add(__relation_type);

		FSResult result;

		auto binaryVersion = static_cast< const BinaryVersion* >(version);
		context.reverseIndex.foreachReverseDependency(binaryVersion, __relation_type,
				[&context, &result](const BinaryVersion* reverseVersion, const RelationExpression&)
				{
					context.mergeFsResults(&result, { reverseVersion });
				});

		return result;
	}
};

class ReverseBuildDependencyFS: public TransformFS
{
	SRT::Type __relationType;
 public:
	ReverseBuildDependencyFS(SRT::Type relationType, const Arguments& arguments)
		: TransformFS(true, arguments), __relationType(relationType)
	{}
 protected:
	FSResult _transform(Context& context, const SPCV& version) const
	{
		context.reverseBuildIndex.add(__relationType);

		FSResult result;

		auto binaryVersion = static_cast< const BinaryVersion* >(version);
		context.reverseBuildIndex.foreachReverseDependency(binaryVersion, __relationType,
				[&context, &result](const SourceVersion* reverseVersion, const RelationExpression&)
				{
					context.mergeFsResults(&result, { reverseVersion });
				});
		return result;
	}
};

class BinaryToSourceFS: public TransformFS
{
 public:
	BinaryToSourceFS(const Arguments& arguments)
		: TransformFS(true, arguments)
	{}
 protected:
	FSResult _transform(Context& context, const SPCV& version) const
	{
		auto binaryVersion = static_cast< const BinaryVersion* >(version);
		if (auto sourcePackage = context.cache.getSourcePackage(binaryVersion->sourcePackageName))
		{
			for (auto sourceVersion: *sourcePackage)
			{
				if (sourceVersion->versionString == binaryVersion->sourceVersionString)
				{
					return { sourceVersion };
				}
			}
		}
		return {};
	}
};

class SourceToBinaryFS: public TransformFS
{
 public:
	SourceToBinaryFS(const Arguments& arguments)
		: TransformFS(false, arguments)
	{}
 protected:
	FSResult _transform(Context& context, const SPCV& version) const
	{
		auto sourceVersion = static_cast< const SourceVersion* >(version);

		FSResult result;

		for (const auto& packageName: sourceVersion->binaryPackageNames)
		{
			if (auto binaryPackage = context.cache.getBinaryPackage(packageName))
			{
				for (auto binaryVersion: *binaryPackage)
				{
					if (binaryVersion->sourceVersionString == sourceVersion->versionString)
					{
						context.mergeFsResults(&result, { binaryVersion });
					}
				}
			}
		}
		return result;
	}
};

class PackageIsInstalledFS: public PredicateFS
{
 protected:
	bool _match(const Cache& cache, const SPCV& version) const
	{
		return isPackageInstalled(cache, version->packageName);
	}
 public:
	PackageIsInstalledFS(const Arguments& arguments)
	{
		__require_n_arguments(arguments, 0);
	}
};

class PackageIsAutoInstalledFS: public PredicateFS
{
 protected:
	bool _match(const Cache& cache, const SPCV& version) const
	{
		return cache.isAutomaticallyInstalled(version->packageName);
	}
 public:
	PackageIsAutoInstalledFS(const Arguments& arguments)
	{
		__require_n_arguments(arguments, 0);
	}
};

///

namespace attr
{
	string priority(const Cache&, const SPCV& version)
	{
		return Version::Priorities::strings[version->priority];
	}
}

CommonFS* constructFSByName(const string& functionName, const CommonFS::Arguments& arguments, bool binary)
{
	#define VERSION_MEMBER(member) [](const Cache&, const SPCV& version) { return version-> member; }
	#define VERSION_RELEASE_MEMBER(member) [](const Version::Source& source) { return source.release-> member; }
	#define BINARY_VERSION_MEMBER(member) [](const Cache&, const SPCV& version) \
			{ return static_cast< const BinaryVersion* >(version)-> member; }
	#define CONSTRUCT_FS(name, code) if (functionName == name) { return new code; }
	#define CONSTRUCT_RELEASE_MEMBER_FS(name, member) \
			CONSTRUCT_FS(name, SourceRegexMatchFS(VERSION_RELEASE_MEMBER(member), arguments))

	// variables
	if (functionName.front() == '_')
	{
		return new ExtractVariableFS(functionName, arguments);
	}
	CONSTRUCT_FS("with", DefineVariableFS(binary, arguments))
	CONSTRUCT_FS("recursive", RecursiveFS(binary, arguments))
	// logic
	CONSTRUCT_FS("and", AndFS(binary, arguments))
	CONSTRUCT_FS("or", OrFS(binary, arguments))
	CONSTRUCT_FS("not", NotFS(binary, arguments))
	CONSTRUCT_FS("xor", XorFS(binary, arguments))
	// narrowing/widening
	CONSTRUCT_FS("best", BestFS(binary, arguments))
	// common
	CONSTRUCT_FS("package:name", PackageNameFS(arguments))
	CONSTRUCT_FS("version", RegexMatchFS(VERSION_MEMBER(versionString), arguments))
	CONSTRUCT_FS("maintainer", RegexMatchFS(VERSION_MEMBER(maintainer), arguments))
	CONSTRUCT_FS("priority", RegexMatchFS(attr::priority, arguments))
	CONSTRUCT_FS("section", RegexMatchFS(VERSION_MEMBER(section), arguments))
	CONSTRUCT_FS("trusted", BoolMatchFS(VERSION_MEMBER(isVerified()), arguments))
	CONSTRUCT_FS("field", OtherFieldRegexMatchFS(arguments))
	CONSTRUCT_RELEASE_MEMBER_FS("release:archive", archive)
	CONSTRUCT_RELEASE_MEMBER_FS("release:codename", codename)
	CONSTRUCT_RELEASE_MEMBER_FS("release:component", component)
	CONSTRUCT_RELEASE_MEMBER_FS("release:version", version)
	CONSTRUCT_RELEASE_MEMBER_FS("release:vendor", vendor)
	CONSTRUCT_RELEASE_MEMBER_FS("release:origin", baseUri)
	if (binary)
	{
		CONSTRUCT_FS("source-package", RegexMatchFS(BINARY_VERSION_MEMBER(sourcePackageName), arguments))
		CONSTRUCT_FS("source-version", RegexMatchFS(BINARY_VERSION_MEMBER(sourceVersionString), arguments))
		CONSTRUCT_FS("essential", BoolMatchFS(BINARY_VERSION_MEMBER(essential), arguments))
		CONSTRUCT_FS("important", BoolMatchFS(BINARY_VERSION_MEMBER(important), arguments))
		CONSTRUCT_FS("installed", BoolMatchFS(BINARY_VERSION_MEMBER(isInstalled()), arguments))
		CONSTRUCT_FS("description", RegexMatchFS(BINARY_VERSION_MEMBER(description), arguments))
		CONSTRUCT_FS("package:installed", PackageIsInstalledFS(arguments))
		CONSTRUCT_FS("package:automatically-installed", PackageIsAutoInstalledFS(arguments))
		// relations
		CONSTRUCT_FS("pre-depends", DependencyFS<BRT::Type>(BRT::PreDepends, arguments))
		CONSTRUCT_FS("depends", DependencyFS<BRT::Type>(BRT::Depends, arguments))
		CONSTRUCT_FS("recommends", DependencyFS<BRT::Type>(BRT::Recommends, arguments))
		CONSTRUCT_FS("suggests", DependencyFS<BRT::Type>(BRT::Suggests, arguments))
		CONSTRUCT_FS("conflicts", DependencyFS<BRT::Type>(BRT::Conflicts, arguments))
		CONSTRUCT_FS("breaks", DependencyFS<BRT::Type>(BRT::Breaks, arguments))
		CONSTRUCT_FS("replaces", DependencyFS<BRT::Type>(BRT::Replaces, arguments))
		CONSTRUCT_FS("enhances", DependencyFS<BRT::Type>(BRT::Enhances, arguments))
		CONSTRUCT_FS("reverse-pre-depends", ReverseDependencyFS(BRT::PreDepends, arguments))
		CONSTRUCT_FS("reverse-depends", ReverseDependencyFS(BRT::Depends, arguments))
		CONSTRUCT_FS("reverse-recommends", ReverseDependencyFS(BRT::Recommends, arguments))
		CONSTRUCT_FS("reverse-suggests", ReverseDependencyFS(BRT::Suggests, arguments))
		CONSTRUCT_FS("reverse-conflicts", ReverseDependencyFS(BRT::Conflicts, arguments))
		CONSTRUCT_FS("reverse-breaks", ReverseDependencyFS(BRT::Breaks, arguments))
		CONSTRUCT_FS("reverse-enhances", ReverseDependencyFS(BRT::Enhances, arguments))
		CONSTRUCT_FS("reverse-replaces", ReverseDependencyFS(BRT::Replaces, arguments))

		CONSTRUCT_FS("provides", ProvidesFS(arguments))

		CONSTRUCT_FS("build-depends", DependencyFS<SRT::Type>(SRT::BuildDepends, arguments))
		CONSTRUCT_FS("build-depends-indep", DependencyFS<SRT::Type>(SRT::BuildDependsIndep, arguments))
		CONSTRUCT_FS("build-depends-arch", DependencyFS<SRT::Type>(SRT::BuildDependsArch, arguments))
		CONSTRUCT_FS("build-conflicts", DependencyFS<SRT::Type>(SRT::BuildConflicts, arguments))
		CONSTRUCT_FS("build-conflicts-indep", DependencyFS<SRT::Type>(SRT::BuildConflictsIndep, arguments))
		CONSTRUCT_FS("build-conflicts-arch", DependencyFS<SRT::Type>(SRT::BuildConflictsArch, arguments))

		CONSTRUCT_FS("source-to-binary", SourceToBinaryFS(arguments))
	}
	else
	{
		CONSTRUCT_FS("uploaders", UploadersFS(arguments))

		CONSTRUCT_FS("reverse-build-depends", ReverseBuildDependencyFS(SRT::BuildDepends, arguments))
		CONSTRUCT_FS("reverse-build-depends-indep", ReverseBuildDependencyFS(SRT::BuildDependsIndep, arguments))
		CONSTRUCT_FS("reverse-build-depends-arch", ReverseBuildDependencyFS(SRT::BuildDependsArch, arguments))
		CONSTRUCT_FS("reverse-build-conflicts", ReverseBuildDependencyFS(SRT::BuildConflicts, arguments))
		CONSTRUCT_FS("reverse-build-conflicts-indep", ReverseBuildDependencyFS(SRT::BuildConflictsIndep, arguments))
		CONSTRUCT_FS("reverse-build-conflicts-arch", ReverseBuildDependencyFS(SRT::BuildConflictsArch, arguments))

		CONSTRUCT_FS("binary-to-source", BinaryToSourceFS(arguments))
	}

	fatal2(__("unknown %s selector function '%s'"), binary ? __("binary") : __("source"), functionName);
	__builtin_unreachable();
}

vector< string > split(const string& input, const char delimiter = ',')
{
	if (input.empty())
	{
		return {};
	}
	vector< string > result;
	size_t argumentStartPosition = 0;
	size_t position = 0;
	size_t level = 0;

	char delimiters[] = "d()/";
	delimiters[0] = delimiter;

	while (position = input.find_first_of(delimiters, position), position != string::npos)
	{
		if (input[position] == delimiter && level == 0)
		{
			result.push_back(input.substr(argumentStartPosition, position - argumentStartPosition));
			argumentStartPosition = position+1;
		}
		else switch (input[position])
		{
			case '(':
				++level;
				break;
			case ')':
				if (level == 0)
				{
					fatal2(__("unexpected closing bracket ')' after '%s'"), input.substr(0, position));
				}
				--level;
				break;
			case '/': // quoting
				position = input.find('/', position+1);
				if (position == string::npos)
				{
					fatal2(__("unable to find closing quoting character '/'"));
				}
		}
		++position;
	}
	if (level != 0)
	{
		fatal2(__("too few closing brackets"));
	}
	result.push_back(input.substr(argumentStartPosition, input.size() - argumentStartPosition));
	return result;
}

void trim(string& s)
{
	const char* trimmedCharacters = " \t\n";
	if (s.size() > 0)
	{
		auto firstValuablePosition = s.find_first_not_of(trimmedCharacters);
		auto lastValuablePosition = s.find_last_not_of(trimmedCharacters);
		s = s.substr(firstValuablePosition, lastValuablePosition - firstValuablePosition + 1);
	}
}

void stripArgumentQuotes(string& argument)
{
	if (argument.size() >= 2)
	{
		if (argument.front() == '/' && argument.back() == '/')
		{
			argument = argument.substr(1, argument.size()-2);
		}
	}
}

void processNonTrivialAliases(string* functionNamePtr, vector< string >* argumentsPtr)
{
	auto getUniqueVariableName = []()
	{
		static size_t anonymousVariableId = 0;
		return format2("__anon%zu", anonymousVariableId++);
	};

	if (*functionNamePtr == "package-with-installed-dependencies")
	{
		__require_n_arguments(*argumentsPtr, 1);
		*functionNamePtr = "recursive";
		auto variableName = getUniqueVariableName();
		auto recursiveExpression = format2(
				"best( fmap(%s, Ypd,Yd,Yr) & Pi )", variableName);
		*argumentsPtr = { variableName, argumentsPtr->front(), recursiveExpression };
	}
	else if (*functionNamePtr == "fmap")
	{
		if (argumentsPtr->size() < 2)
		{
			fatal2("the function '%s' requires at least %zu arguments", "fmap", 2);
		}
		auto argument = argumentsPtr->front();
		argumentsPtr->erase(argumentsPtr->begin());

		auto variableName = getUniqueVariableName();
		auto bracedVariableName = format2("(%s)", variableName);

		vector< string > mappedCalls;
		for (const auto& e: *argumentsPtr) { mappedCalls.push_back(e + bracedVariableName); }
		string expression = format2("or(%s)", join(",", mappedCalls));

		*functionNamePtr = "with";
		*argumentsPtr = { variableName, argument, expression };
	}
}

void processAliases(string* functionNamePtr, vector< string >* argumentsPtr)
{
	{ // simple aliases
		static map< string, string > aliases = {
			{ "Pn", "package:name" },
			{ "Pi", "package:installed" },
			{ "Pai", "package:automatically-installed" },

			{ "v", "version" },
			{ "m", "maintainer" },
			{ "p", "priority" },
			{ "s", "section" },
			{ "t", "trusted" },
			{ "f", "field" },
			{ "sp", "source-package" },
			{ "sv", "source-version" },
			{ "e", "essential" },
			{ "i", "installed" },
			{ "d", "description" },
			{ "o", "provides" },
			{ "u", "uploaders" },

			{ "Ypd", "pre-depends" },
			{ "Yd", "depends" },
			{ "Yr", "recommends" },
			{ "Ys", "suggests" },
			{ "Ye", "enhances" },
			{ "Yc", "conflicts" },
			{ "Yb", "breaks" },
			{ "Yrp", "replaces" },

			{ "YRpd", "reverse-pre-depends" },
			{ "YRd", "reverse-depends" },
			{ "YRr", "reverse-recommends" },
			{ "YRs", "reverse-suggests" },
			{ "YRe", "reverse-enhances" },
			{ "YRc", "reverse-conflicts" },
			{ "YRb", "reverse-breaks" },
			{ "YRrp", "reverse-replaces" },

			{ "Zbd", "build-depends" },
			{ "Zbdi", "build-depends-indep" },
			{ "Zbda", "build-depends-arch" },
			{ "Zbc", "build-conflicts" },
			{ "Zbci", "build-conflicts-indep" },
			{ "Zbca", "build-conflicts-arch" },

			{ "ZRbd", "reverse-build-depends" },
			{ "ZRbdi", "reverse-build-depends-indep" },
			{ "ZRbda", "reverse-build-depends-arch" },
			{ "ZRbc", "reverse-build-conflicts" },
			{ "ZRbci", "reverse-build-conflicts-indep" },
			{ "ZRbca", "reverse-build-conflicts-arch" },

			{ "Ra", "release:archive" },
			{ "Rn", "release:codename" },
			{ "Rc", "release:component" },
			{ "Rv", "release:version" },
			{ "Ro", "release:vendor" },
			{ "Ru", "release:origin" },
		};
		auto it = aliases.find(*functionNamePtr);
		if (it != aliases.end())
		{
			*functionNamePtr = it->second;
			return;
		}
	}

	processNonTrivialAliases(functionNamePtr, argumentsPtr);
}

void parseFunctionNameAndArguments(const string& query,
		string* functionNamePtr, vector< string >* argumentsPtr)
{
	vector< string > possibleArguments;
	possibleArguments = split(query, '|');
	if (possibleArguments.size() > 1)
	{
		*functionNamePtr = "or";
		*argumentsPtr = std::move(possibleArguments);
		return;
	}
	possibleArguments = split(query, '&');
	if (possibleArguments.size() > 1)
	{
		*functionNamePtr = "and";
		*argumentsPtr = std::move(possibleArguments);
		return;
	}

	auto argumentsPosition = query.find_first_of("()");
	if (argumentsPosition == string::npos)
	{
		*functionNamePtr = query; // assume that the function takes no parameters
	}
	else
	{
		if (query[argumentsPosition] == ')')
		{
			fatal2(__("closing bracket ')' doesn't have a corresponding opening bracket '('"));
		}
		// now we know it's surely '('
		if (query.back() != ')')
		{
			fatal2(__("the last query character is not a closing bracket ')'"));
		}
		*functionNamePtr = query.substr(0, argumentsPosition);
		*argumentsPtr = split(query.substr(argumentsPosition + 1, query.size() - argumentsPosition - 2));
	}
}

unique_ptr< CommonFS > internalParseFunctionQuery(const string& query, bool binary)
{
	try
	{
		if (query.empty())
		{
			fatal2(__("query cannot be empty"));
		}

		string functionName;
		vector< string > arguments;
		parseFunctionNameAndArguments(query, &functionName, &arguments);

		for (string& argument: arguments)
		{
			trim(argument);
			stripArgumentQuotes(argument);
		}
		processAliases(&functionName, &arguments);
		return unique_ptr< CommonFS >(constructFSByName(functionName, arguments, binary));
	}
	catch (Exception&)
	{
		fatal2(__("unable to parse the query '%s'"), query);
		__builtin_unreachable();
	}
}

}

struct FunctionalSelector::Data
{
	Context context;
	VersionSetGetter binaryGetter;
	VersionSetGetter sourceGetter;

	Data(const Cache& cache_)
		: context(cache_), binaryGetter(cache_, true), sourceGetter(cache_, false)
	{}
};
FunctionalSelector::FunctionalSelector(const Cache& cache)
{
	__data = new Data(cache);
}
FunctionalSelector::~FunctionalSelector()
{
	delete __data;
}

unique_ptr< FS > FunctionalSelector::parseQuery(const string& query, bool binary)
{
	Cache::memoize = true;

	auto trimmedQuery = query;
	trim(trimmedQuery);
	auto result = internalParseFunctionQuery(trimmedQuery, binary);

	if (binary)
	{
		unique_ptr< CommonFS > newResult(new BinaryTagDummyFS(std::move(result)));
		result.swap(newResult);
	}
	return std::move(result);
}

list< SPCV > FunctionalSelector::selectAllVersions(const FS& functionSelector)
{
	const CommonFS* commonFS = dynamic_cast< const CommonFS* >(&functionSelector);
	if (!commonFS)
	{
		fatal2i("selectVersion: functionSelector is not an ancestor of CommonFS");
	}
	bool binary = (dynamic_cast< const BinaryTagDummyFS* >(commonFS));
	VersionSet versionSet(&__data->binaryGetter, &__data->sourceGetter);
	versionSet.selectGetterType(binary);
	return commonFS->select(__data->context, versionSet);
}

list< SPCV > FunctionalSelector::selectBestVersions(const FS& functionSelector)
{
	auto result = selectAllVersions(functionSelector);
	result.unique([](const SPCV& left, const SPCV& right) { return left->packageName == right->packageName; });
	return result;
}