-
Notifications
You must be signed in to change notification settings - Fork 546
/
Copy pathobject_ref.h
225 lines (201 loc) · 8.07 KB
/
object_ref.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
/*
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#pragma once
#include <algorithm>
#include <string>
#include "runtime/object.h"
#include "runtime/optional.h"
namespace lean {
/* Smart point for Lean objects. It is useful for writing C++ code that manipulates Lean objects. */
class object_ref {
protected:
object * m_obj;
public:
object_ref():m_obj(box(0)) {}
explicit object_ref(obj_arg o):m_obj(o) {}
object_ref(b_obj_arg o, bool):m_obj(o) { inc(o); }
object_ref(object_ref const & s):m_obj(s.m_obj) { inc(m_obj); }
object_ref(object_ref && s):m_obj(s.m_obj) { s.m_obj = box(0); }
~object_ref() { dec(m_obj); }
object_ref & operator=(object_ref const & s) {
inc(s.m_obj);
object * new_obj = s.m_obj;
dec(m_obj);
m_obj = new_obj;
return *this;
}
object_ref & operator=(object_ref && s) {
dec(m_obj);
m_obj = s.m_obj;
s.m_obj = box(0);
return *this;
}
void set_box(object * o) {
lean_assert(is_scalar(m_obj));
m_obj = o;
}
object * raw() const { return m_obj; }
object * steal() { object * r = m_obj; m_obj = box(0); return r; }
object * to_obj_arg() const { inc(m_obj); return m_obj; }
static void swap(object_ref & a, object_ref & b) { std::swap(a.m_obj, b.m_obj); }
};
/* Remark: this function doesn't increase the reference counter of objs */
LEAN_EXPORT object_ref mk_cnstr(unsigned tag, unsigned num_objs, object ** objs, unsigned scalar_sz = 0);
inline object_ref mk_cnstr(unsigned tag, object * o, unsigned scalar_sz = 0) { return mk_cnstr(tag, 1, &o, scalar_sz); }
inline object_ref mk_cnstr(unsigned tag, object * o1, object * o2, unsigned scalar_sz = 0) {
object * os[2] = { o1, o2 };
return mk_cnstr(tag, 2, os, scalar_sz);
}
inline object_ref mk_cnstr(unsigned tag, object * o1, object * o2, object * o3, unsigned scalar_sz = 0) {
object * os[3] = { o1, o2, o3 };
return mk_cnstr(tag, 3, os, scalar_sz);
}
inline object_ref mk_cnstr(unsigned tag, object * o1, object * o2, object * o3, object * o4, unsigned scalar_sz = 0) {
object * os[4] = { o1, o2, o3, o4 };
return mk_cnstr(tag, 4, os, scalar_sz);
}
inline object_ref mk_cnstr(unsigned tag, object * o1, object * o2, object * o3, object * o4, object * o5, unsigned scalar_sz = 0) {
object * os[5] = { o1, o2, o3, o4, o5 };
return mk_cnstr(tag, 5, os, scalar_sz);
}
inline object_ref mk_cnstr(unsigned tag, object_ref const & o, unsigned scalar_sz = 0) {
object * r = alloc_cnstr(tag, 1, scalar_sz);
cnstr_set(r, 0, o.to_obj_arg());
return object_ref(r);
}
inline object_ref mk_cnstr(unsigned tag, object_ref const & o1, object_ref const & o2, unsigned scalar_sz = 0) {
object * r = alloc_cnstr(tag, 2, scalar_sz);
cnstr_set(r, 0, o1.to_obj_arg());
cnstr_set(r, 1, o2.to_obj_arg());
return object_ref(r);
}
inline object_ref mk_cnstr(unsigned tag, object_ref const & o1, object_ref const & o2, object_ref const & o3, unsigned scalar_sz = 0) {
object * r = alloc_cnstr(tag, 3, scalar_sz);
cnstr_set(r, 0, o1.to_obj_arg());
cnstr_set(r, 1, o2.to_obj_arg());
cnstr_set(r, 2, o3.to_obj_arg());
return object_ref(r);
}
inline object_ref mk_cnstr(unsigned tag, object_ref const & o1, object_ref const & o2, object_ref const & o3, object_ref const & o4, unsigned scalar_sz = 0) {
object * r = alloc_cnstr(tag, 4, scalar_sz);
cnstr_set(r, 0, o1.to_obj_arg());
cnstr_set(r, 1, o2.to_obj_arg());
cnstr_set(r, 2, o3.to_obj_arg());
cnstr_set(r, 3, o4.to_obj_arg());
return object_ref(r);
}
inline object_ref mk_cnstr(unsigned tag, object_ref const & o1, object_ref const & o2, object_ref const & o3, object_ref const & o4, object_ref const & o5, unsigned scalar_sz = 0) {
object * r = alloc_cnstr(tag, 5, scalar_sz);
cnstr_set(r, 0, o1.to_obj_arg());
cnstr_set(r, 1, o2.to_obj_arg());
cnstr_set(r, 2, o3.to_obj_arg());
cnstr_set(r, 3, o4.to_obj_arg());
cnstr_set(r, 4, o5.to_obj_arg());
return object_ref(r);
}
inline object_ref mk_cnstr(unsigned tag, object_ref const & o1, object_ref const & o2, object_ref const & o3, object_ref const & o4, object_ref const & o5, object_ref const & o6, unsigned scalar_sz = 0) {
object * r = alloc_cnstr(tag, 6, scalar_sz);
cnstr_set(r, 0, o1.to_obj_arg());
cnstr_set(r, 1, o2.to_obj_arg());
cnstr_set(r, 2, o3.to_obj_arg());
cnstr_set(r, 3, o4.to_obj_arg());
cnstr_set(r, 4, o5.to_obj_arg());
cnstr_set(r, 5, o6.to_obj_arg());
return object_ref(r);
}
inline object_ref mk_cnstr(unsigned tag, object_ref const & o1, object_ref const & o2, object_ref const & o3, object_ref const & o4, object_ref const & o5, object_ref const & o6, object_ref const & o7, unsigned scalar_sz = 0) {
object * r = alloc_cnstr(tag, 7, scalar_sz);
cnstr_set(r, 0, o1.to_obj_arg());
cnstr_set(r, 1, o2.to_obj_arg());
cnstr_set(r, 2, o3.to_obj_arg());
cnstr_set(r, 3, o4.to_obj_arg());
cnstr_set(r, 4, o5.to_obj_arg());
cnstr_set(r, 5, o6.to_obj_arg());
cnstr_set(r, 6, o7.to_obj_arg());
return object_ref(r);
}
/* The following definition is a low level hack that relies on the fact that sizeof(object_ref) == sizeof(object *). */
inline object_ref const & cnstr_get_ref(object * o, unsigned i) {
static_assert(sizeof(object_ref) == sizeof(object *), "unexpected object_ref size"); // NOLINT
lean_assert(is_cnstr(o));
lean_assert(i < lean_ctor_num_objs(o));
return reinterpret_cast<object_ref const *>(lean_to_ctor(o)->m_objs)[i];
}
inline object_ref const & cnstr_get_ref(object_ref const & ref, unsigned i) {
return cnstr_get_ref(ref.raw(), i);
}
template<class T>
inline T const & cnstr_get_ref_t(object_ref const & o, unsigned i) {
static_assert(sizeof(T) == sizeof(object_ref), "unexpected object wrapper size");
return static_cast<T const &>(cnstr_get_ref(o.raw(), i));
}
/* Given `T` which is a subclass of object_ref that wraps a Lean value of type `Ty`,
convert a value `o` of `Option Ty` into `optional<T>` */
template<typename T> optional<T> to_optional(obj_arg o) {
if (is_scalar(o)) return optional<T>();
T r(cnstr_get(o, 0), true);
dec(o);
return optional<T>(r);
}
/* "Borrow" version of `to_optional` */
template<typename T> optional<T> to_optional(b_obj_arg o, bool) {
if (is_scalar(o)) return optional<T>();
T r(cnstr_get(o, 0), true);
return optional<T>(r);
}
/* Given `T` which is a scalar type that wraps a Lean scalar value of type `Ty`,
convert a value `o` of `Option Ty` into `optional<T>` */
template<typename T> optional<T> to_optional_scalar(obj_arg o) {
if (is_scalar(o)) return optional<T>();
T r = static_cast<T>(unbox(cnstr_get(o, 0)));
dec(o);
return optional<T>(r);
}
template<typename T> obj_res to_object(optional<T> const & o) {
if (o) {
obj_res r = alloc_cnstr(1, 1, 0);
cnstr_set(r, 0, o->to_obj_arg());
return r;
} else {
return box(0);
}
}
/*
inductive Except (ε : Type u) (α : Type v)
| error {} : ε → Except
| ok {} : α → Except
*/
template<typename T> object * mk_except_ok(T const & val) {
obj_res r = alloc_cnstr(1, 1, 0);
cnstr_set(r, 0, val.to_obj_arg());
return r;
}
template<typename T> object * mk_except_error(T const & err) {
obj_res r = alloc_cnstr(0, 1, 0);
cnstr_set(r, 0, err.to_obj_arg());
return r;
}
inline object * mk_except_error_string(char const * err) {
obj_res r = alloc_cnstr(0, 1, 0);
cnstr_set(r, 0, mk_string(err));
return r;
}
/* Given `o` representing a Lean value of type `Except String A`, return `T` an smart pointer
that encapsulates `A` values or throw an exception */
template<typename T> T get_except_value(obj_arg o) {
if (cnstr_tag(o) == 1) {
T result(cnstr_get(o, 0), true);
dec(o);
return result;
} else {
std::string err = string_to_std(cnstr_get(o, 0));
dec(o);
throw exception(err);
}
}
// Remark: `T` must be an `object_ref`.
#define TO_REF(T, o) reinterpret_cast<T const &>(o)
}