-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathast.h
2645 lines (2145 loc) · 103 KB
/
ast.h
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
/*++
Copyright (c) 2006 Microsoft Corporation
Module Name:
ast.h
Abstract:
Expression DAG
Author:
Leonardo de Moura (leonardo) 2006-09-18.
Revision History:
--*/
#pragma once
#include "util/vector.h"
#include "util/hashtable.h"
#include "util/buffer.h"
#include "util/zstring.h"
#include "util/symbol.h"
#include "util/rational.h"
#include "util/hash.h"
#include "util/optional.h"
#include "util/trace.h"
#include "util/bit_vector.h"
#include "util/symbol_table.h"
#include "util/tptr.h"
#include "util/memory_manager.h"
#include "util/small_object_allocator.h"
#include "util/obj_ref.h"
#include "util/ref_vector.h"
#include "util/ref_pair_vector.h"
#include "util/ref_buffer.h"
#include "util/obj_mark.h"
#include "util/obj_hashtable.h"
#include "util/id_gen.h"
#include "util/map.h"
#include "util/parray.h"
#include "util/dictionary.h"
#include "util/chashtable.h"
#include "util/z3_exception.h"
#include "util/dependency.h"
#include "util/rlimit.h"
#include <variant>
#define RECYCLE_FREE_AST_INDICES
#ifdef _MSC_VER
#pragma warning(disable : 4200)
#pragma warning(disable : 4355)
#endif
#ifdef _MSC_VER
# define Z3_NORETURN __declspec(noreturn)
#else
# define Z3_NORETURN [[noreturn]]
#endif
class ast;
class ast_manager;
/**
\brief Generic exception for AST related errors.
We used to use fatal_error_msg to report errors inside plugins.
*/
class ast_exception : public default_exception {
public:
ast_exception(std::string && msg) : default_exception(std::move(msg)) {}
};
typedef int family_id;
const family_id null_family_id = -1;
const family_id basic_family_id = 0;
const family_id label_family_id = 1;
const family_id pattern_family_id = 2;
const family_id model_value_family_id = 3;
const family_id user_sort_family_id = 4;
const family_id last_builtin_family_id = 4;
const family_id arith_family_id = 5;
// -----------------------------------
//
// parameter
//
// -----------------------------------
/**
\brief Interpreted function declarations and sorts may have parameters that are used
to encode extra information associated with them.
*/
class parameter {
public:
// NOTE: these must be in the same order as the entries in the variant below
enum kind_t {
PARAM_INT,
PARAM_AST,
PARAM_SYMBOL,
PARAM_ZSTRING,
PARAM_RATIONAL,
PARAM_DOUBLE,
// PARAM_EXTERNAL is used for handling decl_plugin specific parameters.
// For example, it is used for handling mpf numbers in float_decl_plugin,
// and irrational algebraic numbers in arith_decl_plugin.
// PARAM_EXTERNAL is not supported by z3 low level input format. This format is legacy, so
// this is not a big problem.
// Remark: PARAM_EXTERNAL can only be used to decorate theory decls.
PARAM_EXTERNAL
};
private:
// It is not possible to use tag pointers, since symbols are already tagged.
std::variant<
int, // for PARAM_INT
ast*, // for PARAM_AST
symbol, // for PARAM_SYMBOL
zstring*, // for PARAM_ZSTRING
rational*, // for PARAM_RATIONAL
double, // for PARAM_DOUBLE (remark: this is not used in float_decl_plugin)
unsigned // for PARAM_EXTERNAL
> m_val;
public:
parameter() : m_val(0) {}
explicit parameter(int val): m_val(val) {}
explicit parameter(unsigned val): m_val((int)val) {}
explicit parameter(ast * p): m_val(p) {}
explicit parameter(symbol const & s): m_val(s) {}
explicit parameter(rational const & r): m_val(alloc(rational, r)) {}
explicit parameter(rational && r) : m_val(alloc(rational, std::move(r))) {}
explicit parameter(zstring const& s): m_val(alloc(zstring, s)) {}
explicit parameter(zstring && s): m_val(alloc(zstring, std::move(s))) {}
explicit parameter(double d): m_val(d) {}
explicit parameter(const char *s): m_val(symbol(s)) {}
explicit parameter(const std::string &s): m_val(symbol(s)) {}
explicit parameter(unsigned ext_id, bool): m_val(ext_id) {}
parameter(parameter const& other) { *this = other; }
parameter(parameter && other) noexcept : m_val(std::move(other.m_val)) {
other.m_val = 0;
}
~parameter();
parameter& operator=(parameter const& other);
kind_t get_kind() const { return static_cast<kind_t>(m_val.index()); }
bool is_int() const { return get_kind() == PARAM_INT; }
bool is_ast() const { return get_kind() == PARAM_AST; }
bool is_symbol() const { return get_kind() == PARAM_SYMBOL; }
bool is_rational() const { return get_kind() == PARAM_RATIONAL; }
bool is_double() const { return get_kind() == PARAM_DOUBLE; }
bool is_external() const { return get_kind() == PARAM_EXTERNAL; }
bool is_zstring() const { return get_kind() == PARAM_ZSTRING; }
bool is_int(int & i) const { return is_int() && (i = get_int(), true); }
bool is_ast(ast * & a) const { return is_ast() && (a = get_ast(), true); }
bool is_symbol(symbol & s) const { return is_symbol() && (s = get_symbol(), true); }
bool is_rational(rational & r) const { return is_rational() && (r = get_rational(), true); }
bool is_double(double & d) const { return is_double() && (d = get_double(), true); }
bool is_external(unsigned & id) const { return is_external() && (id = get_ext_id(), true); }
bool is_zstring(zstring& s) const { return is_zstring() && (s = get_zstring(), true); }
/**
\brief This method is invoked when the parameter is
attached to a function declaration or sort.
*/
void init_eh(ast_manager & m);
/**
\brief This method is invoked before the function
declaration or sort associated with the parameter is
deleted.
*/
void del_eh(ast_manager & m, family_id fid);
int get_int() const { return std::get<int>(m_val); }
ast * get_ast() const { return std::get<ast*>(m_val); }
symbol get_symbol() const { return std::get<symbol>(m_val); }
rational const & get_rational() const { return *std::get<rational*>(m_val); }
zstring const& get_zstring() const { return *std::get<zstring*>(m_val); }
double get_double() const { return std::get<double>(m_val); }
unsigned get_ext_id() const { return std::get<unsigned>(m_val); }
bool operator==(parameter const & p) const;
bool operator!=(parameter const & p) const { return !operator==(p); }
unsigned hash() const;
std::ostream& display(std::ostream& out) const;
};
inline std::ostream& operator<<(std::ostream& out, parameter const & p) {
return p.display(out);
}
void display_parameters(std::ostream & out, unsigned n, parameter const * p);
// -----------------------------------
//
// family_manager
//
// -----------------------------------
/**
\brief Interpreted functions and sorts are grouped in families.
Each family has an unique ID. This class models the mapping
between symbols (family names) and the unique IDs.
*/
class family_manager {
family_id m_next_id = 0;
symbol_table<family_id> m_families;
svector<symbol> m_names;
public:
/**
\brief Return the family_id for s, a new id is created if !has_family(s)
If has_family(s), then this method is equivalent to get_family_id(s)
*/
family_id mk_family_id(symbol const & s);
/**
\brief Return the family_id for s, return null_family_id if s was not registered in the manager.
*/
family_id get_family_id(symbol const & s) const;
bool has_family(symbol const & s) const;
void get_dom(svector<symbol>& dom) const { m_families.get_dom(dom); }
void get_range(svector<family_id> & range) const { m_families.get_range(range); }
symbol const & get_name(family_id fid) const { return fid >= 0 && fid < static_cast<int>(m_names.size()) ? m_names[fid] : symbol::null; }
bool has_family(family_id fid) const { return fid >= 0 && fid < static_cast<int>(m_names.size()); }
};
// -----------------------------------
//
// decl_info
//
// -----------------------------------
/**
\brief Each interpreted function declaration or sort has a kind.
Kinds are used to identify interpreted functions and sorts in a family.
*/
typedef int decl_kind;
const decl_kind null_decl_kind = -1;
/**
\brief Interpreted function declarations and sorts are associated with
a family id, kind, and parameters.
*/
class decl_info {
family_id m_family_id;
decl_kind m_kind;
vector<parameter> m_parameters;
public:
bool m_private_parameters;
decl_info(family_id family_id = null_family_id, decl_kind k = null_decl_kind,
unsigned num_parameters = 0, parameter const * parameters = nullptr, bool private_params = false);
void init_eh(ast_manager & m);
void del_eh(ast_manager & m);
family_id get_family_id() const { return m_family_id; }
decl_kind get_decl_kind() const { return m_kind; }
bool is_decl_of(family_id fid, decl_kind k) const { return m_family_id == fid && k == m_kind; }
unsigned get_num_parameters() const { return m_parameters.size(); }
parameter const & get_parameter(unsigned idx) const { return m_parameters[idx]; }
parameter const * get_parameters() const { return m_parameters.begin(); }
bool private_parameters() const { return m_private_parameters; }
struct iterator {
decl_info const& d;
iterator(decl_info const& d) : d(d) {}
parameter const* begin() const { return d.get_parameters(); }
parameter const* end() const { return begin() + d.get_num_parameters(); }
};
iterator parameters() const { return iterator(*this); }
unsigned hash() const;
bool operator==(decl_info const & info) const;
};
std::ostream & operator<<(std::ostream & out, decl_info const & info);
// -----------------------------------
//
// sort_size
//
// -----------------------------------
/**
\brief Models the number of elements of a sort.
*/
class sort_size {
enum kind_t {
SS_FINITE,
// For some sorts it may be too expensive to compute the
// number of elements precisely (e.g., arrays). In this
// cases, we mark the sort as too big. That is, the number
// of elements is at least bigger than 2^64.
SS_FINITE_VERY_BIG,
SS_INFINITE
} m_kind;
uint64_t m_size; // It is only meaningful if m_kind == SS_FINITE
sort_size(kind_t k, uint64_t r):m_kind(k), m_size(r) {}
public:
sort_size():m_kind(SS_INFINITE), m_size(0) {}
sort_size(uint64_t const & sz):m_kind(SS_FINITE), m_size(sz) {}
explicit sort_size(rational const& r) {
if (r.is_uint64()) {
m_kind = SS_FINITE;
m_size = r.get_uint64();
}
else {
m_kind = SS_FINITE_VERY_BIG;
m_size = 0;
}
}
static sort_size mk_infinite() { return sort_size(SS_INFINITE, 0); }
static sort_size mk_very_big() { return sort_size(SS_FINITE_VERY_BIG, 0); }
static sort_size mk_finite(uint64_t r) { return sort_size(SS_FINITE, r); }
bool is_infinite() const { return m_kind == SS_INFINITE; }
bool is_very_big() const { return m_kind == SS_FINITE_VERY_BIG; }
bool is_finite() const { return m_kind == SS_FINITE; }
static bool is_very_big_base2(unsigned power) { return power >= 64; }
uint64_t size() const { SASSERT(is_finite()); return m_size; }
};
std::ostream& operator<<(std::ostream& out, sort_size const & ss);
// -----------------------------------
//
// sort_info
//
// -----------------------------------
/**
\brief Extra information that may be attached to interpreted sorts.
*/
class sort_info : public decl_info {
sort_size m_num_elements;
public:
sort_info(family_id family_id = null_family_id, decl_kind k = null_decl_kind,
unsigned num_parameters = 0, parameter const * parameters = nullptr, bool private_parameters = false):
decl_info(family_id, k, num_parameters, parameters, private_parameters) {
}
sort_info(family_id family_id, decl_kind k, uint64_t num_elements,
unsigned num_parameters = 0, parameter const * parameters = nullptr, bool private_parameters = false):
decl_info(family_id, k, num_parameters, parameters, private_parameters), m_num_elements(num_elements) {
}
sort_info(family_id family_id, decl_kind k, sort_size const& num_elements,
unsigned num_parameters = 0, parameter const * parameters = nullptr, bool private_parameters = false):
decl_info(family_id, k, num_parameters, parameters, private_parameters), m_num_elements(num_elements) {
}
sort_info(decl_info && di, sort_size const& num_elements) :
decl_info(std::move(di)), m_num_elements(num_elements) {}
bool is_infinite() const { return m_num_elements.is_infinite(); }
bool is_very_big() const { return m_num_elements.is_very_big(); }
sort_size const & get_num_elements() const { return m_num_elements; }
void set_num_elements(sort_size const& s) { m_num_elements = s; }
};
std::ostream & operator<<(std::ostream & out, sort_info const & info);
// -----------------------------------
//
// func_decl_info
//
// -----------------------------------
/**
\brief Extra information that may be attached to interpreted function decls.
*/
struct func_decl_info : public decl_info {
bool m_left_assoc:1;
bool m_right_assoc:1;
bool m_flat_associative:1;
bool m_commutative:1;
bool m_chainable:1;
bool m_pairwise:1;
bool m_injective:1;
bool m_idempotent:1;
bool m_skolem:1;
bool m_lambda:1;
func_decl_info(family_id family_id = null_family_id, decl_kind k = null_decl_kind, unsigned num_parameters = 0, parameter const * parameters = nullptr);
bool is_associative() const { return m_left_assoc && m_right_assoc; }
bool is_left_associative() const { return m_left_assoc; }
bool is_right_associative() const { return m_right_assoc; }
bool is_flat_associative() const { return m_flat_associative; }
bool is_commutative() const { return m_commutative; }
bool is_chainable() const { return m_chainable; }
bool is_pairwise() const { return m_pairwise; }
bool is_injective() const { return m_injective; }
bool is_idempotent() const { return m_idempotent; }
bool is_skolem() const { return m_skolem; }
bool is_lambda() const { return m_lambda; }
void set_associative(bool flag = true) { m_left_assoc = flag; m_right_assoc = flag; }
void set_left_associative(bool flag = true) { m_left_assoc = flag; }
void set_right_associative(bool flag = true) { m_right_assoc = flag; }
void set_flat_associative(bool flag = true) { m_flat_associative = flag; }
void set_commutative(bool flag = true) { m_commutative = flag; }
void set_chainable(bool flag = true) { m_chainable = flag; }
void set_pairwise(bool flag = true) { m_pairwise = flag; }
void set_injective(bool flag = true) { m_injective = flag; }
void set_idempotent(bool flag = true) { m_idempotent = flag; }
void set_skolem(bool flag = true) { m_skolem = flag; }
void set_lambda(bool flag = true) { m_lambda = flag; }
bool operator==(func_decl_info const & info) const;
// Return true if the func_decl_info is equivalent to the null one (i.e., it does not contain any useful info).
bool is_null() const {
return
get_family_id() == null_family_id && !is_left_associative() && !is_right_associative() && !is_commutative() &&
!is_chainable() && !is_pairwise() && !is_injective() && !is_idempotent() && !is_skolem();
}
};
std::ostream & operator<<(std::ostream & out, func_decl_info const & info);
// -----------------------------------
//
// ast
//
// -----------------------------------
typedef enum { AST_APP, AST_VAR, AST_QUANTIFIER, AST_SORT, AST_FUNC_DECL } ast_kind;
char const * get_ast_kind_name(ast_kind k);
class shared_occs_mark;
class ast {
protected:
friend class ast_manager;
unsigned m_id;
unsigned m_kind:16;
// Warning: the marks should be used carefully, since they are shared.
unsigned m_mark1:1;
unsigned m_mark2:1;
// Private mark used by shared_occs functor
// Motivation for this field:
// - A mark cannot be used by more than one owner.
// So, it is only safe to use mark by "self-contained" code.
// They should be viewed as temporary information.
// - The functor shared_occs is used by some AST pretty printers.
// - So, a code that uses marks could not use the pretty printer if
// shared_occs used one of the public marks.
// - This was a constant source of assertion violations.
unsigned m_mark_shared_occs:1;
friend class shared_occs_mark;
void mark_so(bool flag) { m_mark_shared_occs = flag; }
void reset_mark_so() { m_mark_shared_occs = false; }
bool is_marked_so() const { return m_mark_shared_occs; }
unsigned m_ref_count;
unsigned m_hash;
#ifdef Z3DEBUG
// In debug mode, we store who is the owner of the mark.
void * m_mark1_owner;
void * m_mark2_owner;
#endif
void inc_ref() {
SASSERT(m_ref_count < UINT_MAX);
m_ref_count ++;
}
void dec_ref() {
SASSERT(m_ref_count > 0);
--m_ref_count;
}
ast(ast_kind k):m_id(UINT_MAX), m_kind(k), m_mark1(false), m_mark2(false), m_mark_shared_occs(false), m_ref_count(0) {
DEBUG_CODE({
m_mark1_owner = 0;
m_mark2_owner = 0;
});
}
public:
unsigned get_id() const { return m_id; }
unsigned get_ref_count() const { return m_ref_count; }
ast_kind get_kind() const { return static_cast<ast_kind>(m_kind); }
unsigned hash() const { return m_hash; }
#ifdef Z3DEBUG
void mark1(bool flag, void * owner) { SASSERT(m_mark1_owner == 0 || m_mark1_owner == owner); m_mark1 = flag; m_mark1_owner = owner; }
void mark2(bool flag, void * owner) { SASSERT(m_mark2_owner == 0 || m_mark2_owner == owner); m_mark2 = flag; m_mark2_owner = owner; }
void reset_mark1(void * owner) { SASSERT(m_mark1_owner == 0 || m_mark1_owner == owner); m_mark1 = false; m_mark1_owner = 0; }
void reset_mark2(void * owner) { SASSERT(m_mark2_owner == 0 || m_mark2_owner == owner); m_mark2 = false; m_mark2_owner = 0; }
bool is_marked1(void * owner) const { SASSERT(m_mark1_owner == 0 || m_mark1_owner == owner); return m_mark1; }
bool is_marked2(void * owner) const { SASSERT(m_mark2_owner == 0 || m_mark2_owner == owner); return m_mark2; }
#define AST_MARK1(A,F,O) A->mark1(F, O)
#define AST_MARK2(A,F,O) A->mark2(F, O)
#define AST_RESET_MARK1(A,O) A->reset_mark1(O)
#define AST_RESET_MARK2(A,O) A->reset_mark2(O)
#define AST_IS_MARKED1(A,O) A->is_marked1(O)
#define AST_IS_MARKED2(A,O) A->is_marked2(O)
#else
void mark1(bool flag) { m_mark1 = flag; }
void mark2(bool flag) { m_mark2 = flag; }
void reset_mark1() { m_mark1 = false; }
void reset_mark2() { m_mark2 = false; }
bool is_marked1() const { return m_mark1; }
bool is_marked2() const { return m_mark2; }
#define AST_MARK1(A,F,O) A->mark1(F)
#define AST_MARK2(A,F,O) A->mark2(F)
#define AST_RESET_MARK1(A,O) A->reset_mark1()
#define AST_RESET_MARK2(A,O) A->reset_mark2()
#define AST_IS_MARKED1(A,O) A->is_marked1()
#define AST_IS_MARKED2(A,O) A->is_marked2()
#endif
};
#define MATCH_TERNARY(_MATCHER_) \
bool _MATCHER_(expr const* n, expr*& a1, expr*& a2, expr *& a3) const { \
if (_MATCHER_(n) && to_app(n)->get_num_args() == 3) { \
a1 = to_app(n)->get_arg(0); a2 = to_app(n)->get_arg(1); a3 = to_app(n)->get_arg(2); return true; } \
return false; \
}
#define MATCH_BINARY(_MATCHER_) \
bool _MATCHER_(expr const* n, expr*& s, expr*& t) const { \
if (_MATCHER_(n) && to_app(n)->get_num_args() == 2) { s = to_app(n)->get_arg(0); t = to_app(n)->get_arg(1); return true; } \
return false; \
}
#define MATCH_UNARY(_MATCHER_) \
bool _MATCHER_(expr const* n, expr*& s) const { \
if (_MATCHER_(n) && to_app(n)->get_num_args() == 1) { s = to_app(n)->get_arg(0); return true; } \
return false; \
}
// -----------------------------------
//
// decl
//
// -----------------------------------
/**
The ids of expressions and declarations are in different ranges.
*/
const unsigned c_first_decl_id = (1u << 31u);
/**
\brief Superclass for function declarations and sorts.
*/
class decl : public ast {
protected:
friend class ast_manager;
symbol m_name;
decl_info * m_info;
decl(ast_kind k, symbol const & name, decl_info * info):ast(k), m_name(name), m_info(info) {}
public:
unsigned get_small_id() const { SASSERT(get_id() >= c_first_decl_id); return get_id() - c_first_decl_id; }
symbol const & get_name() const { return m_name; }
decl_info * get_info() const { return m_info; }
family_id get_family_id() const { return m_info == nullptr ? null_family_id : m_info->get_family_id(); }
decl_kind get_decl_kind() const { return m_info == nullptr ? null_decl_kind : m_info->get_decl_kind(); }
bool is_decl_of(family_id fid, decl_kind k) const { return m_info && m_info->is_decl_of(fid, k); }
unsigned get_num_parameters() const { return m_info == nullptr ? 0 : m_info->get_num_parameters(); }
parameter const & get_parameter(unsigned idx) const { return m_info->get_parameter(idx); }
parameter const * get_parameters() const { return m_info == nullptr ? nullptr : m_info->get_parameters(); }
bool private_parameters() const { return m_info != nullptr && m_info->private_parameters(); }
struct iterator {
decl const& d;
iterator(decl const& d) : d(d) {}
parameter const* begin() const { return d.get_parameters(); }
parameter const* end() const { return begin() + d.get_num_parameters(); }
};
iterator parameters() const { return iterator(*this); }
};
// -----------------------------------
//
// sort
//
// -----------------------------------
class sort : public decl {
friend class ast_manager;
static unsigned get_obj_size() { return sizeof(sort); }
sort(symbol const & name, sort_info * info):decl(AST_SORT, name, info) {}
public:
sort_info * get_info() const { return static_cast<sort_info*>(m_info); }
bool is_infinite() const { return get_info() == nullptr || get_info()->is_infinite(); }
bool is_very_big() const { return get_info() == nullptr || get_info()->is_very_big(); }
bool is_sort_of(family_id fid, decl_kind k) const { return get_family_id() == fid && get_decl_kind() == k; }
sort_size const & get_num_elements() const { return get_info()->get_num_elements(); }
void set_num_elements(sort_size const& s) { get_info()->set_num_elements(s); }
unsigned get_size() const { return get_obj_size(); }
};
// -----------------------------------
//
// func_decl
//
// -----------------------------------
class func_decl : public decl {
friend class ast_manager;
unsigned m_arity;
sort * m_range;
sort * m_domain[0];
static unsigned get_obj_size(unsigned arity) { return sizeof(func_decl) + arity * sizeof(sort *); }
func_decl(symbol const & name, unsigned arity, sort * const * domain, sort * range, func_decl_info * info);
public:
func_decl_info * get_info() const { return static_cast<func_decl_info*>(m_info); }
bool is_associative() const { return get_info() != nullptr && get_info()->is_associative(); }
bool is_left_associative() const { return get_info() != nullptr && get_info()->is_left_associative(); }
bool is_right_associative() const { return get_info() != nullptr && get_info()->is_right_associative(); }
bool is_flat_associative() const { return get_info() != nullptr && get_info()->is_flat_associative(); }
bool is_commutative() const { return get_info() != nullptr && get_info()->is_commutative(); }
bool is_chainable() const { return get_info() != nullptr && get_info()->is_chainable(); }
bool is_pairwise() const { return get_info() != nullptr && get_info()->is_pairwise(); }
bool is_injective() const { return get_info() != nullptr && get_info()->is_injective(); }
bool is_skolem() const { return get_info() != nullptr && get_info()->is_skolem(); }
bool is_lambda() const { return get_info() != nullptr && get_info()->is_lambda(); }
bool is_idempotent() const { return get_info() != nullptr && get_info()->is_idempotent(); }
unsigned get_arity() const { return m_arity; }
sort * get_domain(unsigned idx) const { SASSERT(idx < get_arity()); return m_domain[idx]; }
sort * const * get_domain() const { return m_domain; }
sort * get_range() const { return m_range; }
unsigned get_size() const { return get_obj_size(m_arity); }
sort * const * begin() const { return get_domain(); }
sort * const * end() const { return get_domain() + get_arity(); }
};
// -----------------------------------
//
// expression
//
// -----------------------------------
/**
\brief Superclass for applications, variables and quantifiers.
*/
class expr : public ast {
protected:
friend class ast_manager;
expr(ast_kind k):ast(k) {}
public:
sort* get_sort() const;
unsigned get_small_id() const { return get_id(); }
};
// -----------------------------------
//
// application
//
// -----------------------------------
#define APP_DEPTH_NUM_BITS 16
const unsigned c_max_depth = ((1 << APP_DEPTH_NUM_BITS) - 1);
struct app_flags {
unsigned m_depth:APP_DEPTH_NUM_BITS; // if app is to deep, it doesn't matter.
unsigned m_ground:1; // application does not have free variables or nested quantifiers.
unsigned m_has_quantifiers:1; // application has nested quantifiers.
unsigned m_has_labels:1; // application has nested labels.
};
class app : public expr {
friend class ast_manager;
func_decl * m_decl;
unsigned m_num_args;
expr * m_args[0];
static app_flags g_constant_flags;
// remark: store term depth in the end of the app. the depth is only stored if the num_args > 0
static unsigned get_obj_size(unsigned num_args) {
return num_args == 0 ? sizeof(app) : sizeof(app) + num_args * sizeof(expr *) + sizeof(app_flags);
}
friend class tmp_app;
app_flags * flags() const { return m_num_args == 0 ? &g_constant_flags : reinterpret_cast<app_flags*>(const_cast<expr**>(m_args + m_num_args)); }
app(func_decl * decl, unsigned num_args, expr * const * args);
public:
func_decl * get_decl() const { return m_decl; }
family_id get_family_id() const { return get_decl()->get_family_id(); }
decl_kind get_decl_kind() const { return get_decl()->get_decl_kind(); }
symbol const& get_name() const { return get_decl()->get_name(); }
unsigned get_num_parameters() const { return get_decl()->get_num_parameters(); }
parameter const& get_parameter(unsigned idx) const { return get_decl()->get_parameter(idx); }
parameter const* get_parameters() const { return get_decl()->get_parameters(); }
bool is_app_of(family_id fid, decl_kind k) const { return m_decl->is_decl_of(fid, k); }
unsigned get_num_args() const { return m_num_args; }
expr * get_arg(unsigned idx) const { SASSERT(idx < m_num_args); return m_args[idx]; }
expr * const * get_args() const { return m_args; }
unsigned get_size() const { return get_obj_size(get_num_args()); }
expr * const * begin() const { return m_args; }
expr * const * end() const { return m_args + m_num_args; }
sort * _get_sort() const { return get_decl()->get_range(); }
unsigned get_depth() const { return flags()->m_depth; }
bool is_ground() const { return flags()->m_ground; }
bool has_quantifiers() const { return flags()->m_has_quantifiers; }
bool has_labels() const { return flags()->m_has_labels; }
};
// -----------------------------------
//
// temporary application: little hack to avoid
// the creation of temporary expressions to just
// check the presence of the expression in
// some container/index.
//
// -----------------------------------
class tmp_app {
unsigned m_num_args;
char * m_data;
public:
tmp_app(unsigned num_args):
m_num_args(num_args) {
unsigned sz = app::get_obj_size(num_args);
m_data = alloc_svect(char, sz);
memset(m_data, 0, sz);
get_app()->m_num_args = m_num_args;
}
~tmp_app() {
dealloc_svect(m_data);
}
app * get_app() {
return reinterpret_cast<app*>(m_data);
}
expr ** get_args() {
return get_app()->m_args;
}
void set_decl(func_decl * d) {
get_app()->m_decl = d;
}
void set_num_args(unsigned num_args) {
get_app()->m_num_args = num_args;
}
void set_arg(unsigned idx, expr * arg) {
get_args()[idx] = arg;
SASSERT(get_app()->get_arg(idx) == arg);
}
void copy(app * source) {
SASSERT(source->get_num_args() <= m_num_args);
new (m_data) app(source->get_decl(), source->get_num_args(), source->get_args());
SASSERT(get_app()->get_decl() == source->get_decl());
SASSERT(get_app()->get_arg(0) == source->get_arg(0));
SASSERT(get_app()->get_arg(1) == source->get_arg(1));
}
void copy_swapping_args(app * source) {
SASSERT(source->get_num_args() == 2 && m_num_args >= 2);
expr * args[2] = { source->get_arg(1), source->get_arg(0) };
new (m_data) app(source->get_decl(), 2, args);
SASSERT(get_app()->get_decl() == source->get_decl());
SASSERT(get_app()->get_arg(0) == source->get_arg(1));
SASSERT(get_app()->get_arg(1) == source->get_arg(0));
}
};
// -----------------------------------
//
// variables
//
// -----------------------------------
class var : public expr {
friend class ast_manager;
unsigned m_idx;
sort * m_sort;
static unsigned get_obj_size() { return sizeof(var); }
var(unsigned idx, sort * s):expr(AST_VAR), m_idx(idx), m_sort(s) {}
public:
unsigned get_idx() const { return m_idx; }
sort * _get_sort() const { return m_sort; }
unsigned get_size() const { return get_obj_size(); }
};
// -----------------------------------
//
// quantifier
//
// -----------------------------------
enum quantifier_kind {
forall_k,
exists_k,
lambda_k
};
class quantifier : public expr {
friend class ast_manager;
quantifier_kind m_kind;
unsigned m_num_decls;
expr * m_expr;
sort * m_sort;
unsigned m_depth;
// extra fields
int m_weight;
bool m_has_unused_vars;
bool m_has_labels;
symbol m_qid;
symbol m_skid;
unsigned m_num_patterns;
unsigned m_num_no_patterns;
char m_patterns_decls[0];
static unsigned get_obj_size(unsigned num_decls, unsigned num_patterns, unsigned num_no_patterns) {
return (unsigned)(sizeof(quantifier) + num_decls * (sizeof(sort *) + sizeof(symbol)) + (num_patterns + num_no_patterns) * sizeof(expr*));
}
quantifier(quantifier_kind k, unsigned num_decls, sort * const * decl_sorts, symbol const * decl_names, expr * body, sort* s,
int weight, symbol const & qid, symbol const & skid, unsigned num_patterns, expr * const * patterns,
unsigned num_no_patterns, expr * const * no_patterns);
quantifier(unsigned num_decls, sort * const * decl_sorts, symbol const * decl_names, expr * body, sort* sort);
public:
quantifier_kind get_kind() const { return m_kind; }
// bool is_forall() const { return m_kind == forall_k; }
// bool is_exists() const { return m_kind == exists_k; }
// bool is_lambda() const { return m_kind == lambda_k; }
unsigned get_num_decls() const { return m_num_decls; }
sort * const * get_decl_sorts() const { return reinterpret_cast<sort * const *>(m_patterns_decls); }
symbol const * get_decl_names() const { return reinterpret_cast<symbol const *>(get_decl_sorts() + m_num_decls); }
sort * get_decl_sort(unsigned idx) const { return get_decl_sorts()[idx]; }
symbol const & get_decl_name(unsigned idx) const { return get_decl_names()[idx]; }
expr * get_expr() const { return m_expr; }
sort * _get_sort() const { return m_sort; }
unsigned get_depth() const { return m_depth; }
int get_weight() const { return m_weight; }
symbol const & get_qid() const { return m_qid; }
symbol const & get_skid() const { return m_skid; }
unsigned get_num_patterns() const { return m_num_patterns; }
expr * const * get_patterns() const { return reinterpret_cast<expr * const *>(get_decl_names() + m_num_decls); }
expr * get_pattern(unsigned idx) const { return get_patterns()[idx]; }
unsigned get_num_no_patterns() const { return m_num_no_patterns; }
expr * const * get_no_patterns() const { return reinterpret_cast<expr * const *>(get_decl_names() + m_num_decls); }
expr * get_no_pattern(unsigned idx) const { return get_no_patterns()[idx]; }
bool has_patterns() const { return m_num_patterns > 0 || m_num_no_patterns > 0; }
unsigned get_size() const { return get_obj_size(m_num_decls, m_num_patterns, m_num_no_patterns); }
bool may_have_unused_vars() const { return m_has_unused_vars; }
void set_no_unused_vars() { m_has_unused_vars = false; }
bool has_labels() const { return m_has_labels; }
unsigned get_num_children() const { return 1 + get_num_patterns() + get_num_no_patterns(); }
expr * get_child(unsigned idx) const {
SASSERT(idx < get_num_children());
if (idx == 0)
return get_expr();
else if (idx <= get_num_patterns())
return get_pattern(idx - 1);
else
return get_no_pattern(idx - get_num_patterns() - 1);
}
};
// -----------------------------------
//
// AST recognisers
//
// -----------------------------------
inline bool is_decl(ast const * n) { ast_kind k = n->get_kind(); return k == AST_FUNC_DECL || k == AST_SORT; }
inline bool is_sort(ast const * n) { return n->get_kind() == AST_SORT; }
inline bool is_func_decl(ast const * n) { return n->get_kind() == AST_FUNC_DECL; }
inline bool is_expr(ast const * n) { return !is_decl(n); }
inline bool is_app(ast const * n) { return n->get_kind() == AST_APP; }
inline bool is_var(ast const * n) { return n->get_kind() == AST_VAR; }
inline bool is_var(ast const * n, unsigned& idx) { return is_var(n) && (idx = static_cast<var const*>(n)->get_idx(), true); }
inline bool is_quantifier(ast const * n) { return n->get_kind() == AST_QUANTIFIER; }
inline bool is_forall(ast const * n) { return is_quantifier(n) && static_cast<quantifier const *>(n)->get_kind() == forall_k; }
inline bool is_exists(ast const * n) { return is_quantifier(n) && static_cast<quantifier const *>(n)->get_kind() == exists_k; }
inline bool is_lambda(ast const * n) { return is_quantifier(n) && static_cast<quantifier const *>(n)->get_kind() == lambda_k; }
// -----------------------------------
//
// AST coercions
//
// -----------------------------------
inline decl * to_decl(ast * n) { SASSERT(is_decl(n)); return static_cast<decl *>(n); }
inline sort * to_sort(ast * n) { SASSERT(is_sort(n)); return static_cast<sort *>(n); }
inline func_decl * to_func_decl(ast * n) { SASSERT(is_func_decl(n)); return static_cast<func_decl *>(n); }
inline expr * to_expr(ast * n) { SASSERT(is_expr(n)); return static_cast<expr *>(n); }
inline app * to_app(ast * n) { SASSERT(is_app(n)); return static_cast<app *>(n); }
inline var * to_var(ast * n) { SASSERT(is_var(n)); return static_cast<var *>(n); }
inline quantifier * to_quantifier(ast * n) { SASSERT(is_quantifier(n)); return static_cast<quantifier *>(n); }
inline decl const * to_decl(ast const * n) { SASSERT(is_decl(n)); return static_cast<decl const *>(n); }
inline sort const * to_sort(ast const * n) { SASSERT(is_sort(n)); return static_cast<sort const *>(n); }
inline func_decl const * to_func_decl(ast const * n) { SASSERT(is_func_decl(n)); return static_cast<func_decl const *>(n); }
inline expr const * to_expr(ast const * n) { SASSERT(is_expr(n)); return static_cast<expr const *>(n); }
inline app const * to_app(ast const * n) { SASSERT(is_app(n)); return static_cast<app const *>(n); }
inline var const * to_var(ast const * n) { SASSERT(is_var(n)); return static_cast<var const *>(n); }
inline quantifier const * to_quantifier(ast const * n) { SASSERT(is_quantifier(n)); return static_cast<quantifier const *>(n); }
// -----------------------------------
//
// AST hash-consing
//
// -----------------------------------
unsigned get_node_hash(ast const * n);
bool compare_nodes(ast const * n1, ast const * n2);
unsigned get_node_size(ast const * n);
unsigned get_asts_hash(unsigned sz, ast * const* ns, unsigned init);
unsigned get_apps_hash(unsigned sz, app * const* ns, unsigned init);
unsigned get_exprs_hash(unsigned sz, expr * const* ns, unsigned init);
unsigned get_sorts_hash(unsigned sz, sort * const* ns, unsigned init);
unsigned get_decl_hash(unsigned sz, func_decl* const* ns, unsigned init);
// This is the internal comparison functor for hash-consing AST nodes.
struct ast_eq_proc {
bool operator()(ast const * n1, ast const * n2) const {
return n1->hash() == n2->hash() && compare_nodes(n1, n2);
}
};
class ast_translation;
class ast_table : public chashtable<ast*, obj_ptr_hash<ast>, ast_eq_proc> {
public:
ast_table() : chashtable({}, {}, 512 * 1024, 8 * 1024) {}
void push_erase(ast * n);
ast* pop_erase();
};
// -----------------------------------
//
// decl_plugin
//
// -----------------------------------
/**
\brief Auxiliary data-structure used to initialize the parser symbol tables.
*/
struct builtin_name {
decl_kind m_kind;
symbol m_name;
builtin_name(char const * name, decl_kind k) : m_kind(k), m_name(name) {}
builtin_name(const std::string &name, decl_kind k) : m_kind(k), m_name(name) {}
};
/**
\brief Each family of interpreted function declarations and sorts must provide a plugin
to build sorts and decls of the family.
*/
class decl_plugin {