Source

z3 / src / ast / trail.h

Full commit
  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
/*++
Copyright (c) 2006 Microsoft Corporation

Module Name:

    trail.h

Abstract:

    <abstract>

Author:

    Leonardo de Moura (leonardo) 2008-06-02.

Revision History:

--*/
#ifndef _TRAIL_H_
#define _TRAIL_H_

#include"ast.h"
#include"obj_hashtable.h"
#include"region.h"

template<typename Ctx>
class trail {
public:
    virtual ~trail() {
    }
    virtual void undo(Ctx & ctx) = 0;
};

template<typename Ctx, typename T>
class value_trail : public trail<Ctx> {
    T & m_value;
    T   m_old_value;
    
public:
    value_trail(T & value):
        m_value(value),
        m_old_value(value) {
    }
    
    virtual ~value_trail() {
    }
    
    virtual void undo(Ctx & ctx) {
        m_value = m_old_value;
    }
};

template<typename Ctx>
class reset_flag_trail : public trail<Ctx> {
    bool & m_value;
public:
    reset_flag_trail(bool & value):
        m_value(value) {
    }
    
    virtual ~reset_flag_trail() {
    }
    
    virtual void undo(Ctx & ctx) {
        m_value = false;
    }
};

template<typename Ctx, typename T>
class set_ptr_trail : public trail<Ctx> {
    T * & m_ptr;
public:
    set_ptr_trail(T * & ptr):
        m_ptr(ptr) {
        SASSERT(m_ptr == 0);
    }
    
    virtual void undo(Ctx & ctx) {
        m_ptr = 0;
    }
};

template<typename Ctx, typename T, bool CallDestructors=true>
class restore_size_trail : public trail<Ctx> {
    vector<T, CallDestructors> & m_vector;
    unsigned                     m_old_size;
public:
    restore_size_trail(vector<T, CallDestructors> & v, unsigned sz):
        m_vector(v),
        m_old_size(sz) {
    }
    restore_size_trail(vector<T, CallDestructors> & v):
        m_vector(v),
        m_old_size(v.size()) {
    }
    virtual ~restore_size_trail() {
    }
    virtual void undo(Ctx & ctx) {
        m_vector.shrink(m_old_size);
    }
};    
        
template<typename Ctx, typename T, bool CallDestructors=true>
class vector_value_trail : public trail<Ctx> {
    vector<T, CallDestructors> & m_vector;
    unsigned                     m_idx;
    T                            m_old_value;
public:
    vector_value_trail(vector<T, CallDestructors> & v, unsigned idx):
        m_vector(v),
        m_idx(idx),
        m_old_value(v[idx]) {
    }
    
    virtual ~vector_value_trail() {
    }
    
    virtual void undo(Ctx & ctx) {
        m_vector[m_idx] = m_old_value;
    }
};


template<typename Ctx, typename D, typename R>
class insert_obj_map : public trail<Ctx> {
    obj_map<D,R>&     m_map;
    D*                m_obj;
public:
    insert_obj_map(obj_map<D,R>& t, D* o) : m_map(t), m_obj(o) {}
    virtual ~insert_obj_map() {}
    virtual void undo(Ctx & ctx) { m_map.remove(m_obj); }
};

template<typename Ctx, typename M, typename D>
class insert_map : public trail<Ctx> {
    M&            m_map;
    D             m_obj;
public:
    insert_map(M& t, D o) : m_map(t), m_obj(o) {}
    virtual ~insert_map() {}
    virtual void undo(Ctx & ctx) { m_map.remove(m_obj); }
};



template<typename Ctx, typename V>
class push_back_vector : public trail<Ctx> {
    V & m_vector;
public:
    push_back_vector(V & v):
        m_vector(v) {
    }
    
    virtual void undo(Ctx & ctx) {
        m_vector.pop_back();
    }
};

template<typename Ctx, typename T>
class set_vector_idx_trail : public trail<Ctx> {
    ptr_vector<T> & m_vector;
    unsigned                         m_idx;
public:
    set_vector_idx_trail(ptr_vector<T> & v, unsigned idx):
        m_vector(v),
        m_idx(idx) {
    }
    
    virtual ~set_vector_idx_trail() {
    }
    
    virtual void undo(Ctx & ctx) {
        m_vector[m_idx] = 0;
    }
};
    
template<typename Ctx, typename T, bool CallDestructors=true>
class pop_back_trail : public trail<Ctx> {
    vector<T, CallDestructors> & m_vector;
    T m_value;
public:
    pop_back_trail(vector<T, CallDestructors> & v):
    m_vector(v),
    m_value(m_vector.back()) {
    }
    
    virtual void undo(Ctx & ctx) {
        m_vector.push_back(m_value);
    }
};

template<typename Ctx, typename T, bool CallDestructors=true>
class pop_back2_trail : public trail<Ctx> {
    vector<T, CallDestructors> & m_vector;
    typedef vector<vector<T, CallDestructors>, true> vector_t;
    unsigned m_index;
    T m_value;
public:
    pop_back2_trail(vector<T, CallDestructors> & v, unsigned index):
    m_vector(v),
    m_index(index),
    m_value(m_vector[index].back()) {
    }
    
    virtual void undo(Ctx & ctx) {
        m_vector[m_index].push_back(m_value);
    }
};


template<typename S, typename T>
class ast2ast_trailmap {
    ref_vector<S, ast_manager> m_domain;
    ref_vector<T, ast_manager> m_range;
    obj_map<S, T*>             m_map;
public:
    ast2ast_trailmap(ast_manager& m):
        m_domain(m),
        m_range(m), 
        m_map()
    {}

    bool find(S* s, T*& t) {
        return m_map.find(s,t);
    }
    
    void insert(S* s, T* t) {
        SASSERT(!m_map.contains(s));
        m_domain.push_back(s);
        m_range.push_back(t);
        m_map.insert(s,t);
    }
    
    void pop() {
        SASSERT(!m_domain.empty());
        m_map.remove(m_domain.back());
        m_domain.pop_back();
        m_range.pop_back();
    }
};

template<typename Ctx, typename S, typename T>
class ast2ast_trail : public trail<Ctx> {
    ast2ast_trailmap<S,T>& m_map;
public:
    ast2ast_trail(ast2ast_trailmap<S,T>& m, S* s, T* t) : 
        m_map(m) {
        m.insert(s,t);
    }

    virtual void undo(Ctx& ctx) {
        m_map.pop();
    }    
};

template<typename Ctx, typename T, bool CallDestructors=true>
class push_back_trail : public trail<Ctx> {
    vector<T, CallDestructors> & m_vector;
public:
    push_back_trail(vector<T, CallDestructors> & v):
        m_vector(v) {
    }
    
    virtual void undo(Ctx & ctx) {
        m_vector.pop_back();
    }
};

template<typename Ctx, typename T, bool CallDestructors=true>
class push_back2_trail : public trail<Ctx> {
    typedef vector<vector<T, CallDestructors>, true> vector_t;
    vector_t & m_vector;
    unsigned   m_index;
public:
    push_back2_trail(vector_t & v, unsigned index) : 
    m_vector(v),
    m_index(index) {
    }
    
    virtual void undo(Ctx & ctx) {
        m_vector[m_index].pop_back();
    }
};

template<typename Ctx>
class set_bitvector_trail : public trail<Ctx> {
    svector<bool> & m_vector;
    unsigned        m_idx;
public:
    set_bitvector_trail(svector<bool> & v, unsigned idx):
        m_vector(v),
        m_idx(idx) {
        SASSERT(m_vector[m_idx] == false);
        m_vector[m_idx] = true;
    }
    
    virtual void undo(Ctx & ctx) {
        m_vector[m_idx] = false;
    }
};
    
template<typename Ctx, typename T>
class new_obj_trail : public trail<Ctx> {
    T * m_obj;
public:
    new_obj_trail(T * obj):
        m_obj(obj) {
    }
    
    virtual void undo(Ctx & ctx) {
        dealloc(m_obj);
    }
};

template<typename Ctx, typename M, typename T>
class obj_ref_trail : public trail<Ctx> {
    obj_ref<T,M> m_obj;
public:
    obj_ref_trail(obj_ref<T,M>& obj):
        m_obj(obj) {
    }
    
    virtual void undo(Ctx & ctx) {
        m_obj.reset();
    }
};

template<typename Ctx, typename T>
class insert_obj_trail : public trail<Ctx> {
    obj_hashtable<T>& m_table;
    T*                m_obj;
public:
    insert_obj_trail(obj_hashtable<T>& t, T* o) : m_table(t), m_obj(o) {}
    virtual ~insert_obj_trail() {}
    virtual void undo(Ctx & ctx) { m_table.remove(m_obj); }
};



template<typename Ctx, typename T>
class remove_obj_trail : public trail<Ctx> {
    obj_hashtable<T>& m_table;
    T*                m_obj;
public:
    remove_obj_trail(obj_hashtable<T>& t, T* o) : m_table(t), m_obj(o) {}
    virtual ~remove_obj_trail() {}
    virtual void undo(Ctx & ctx) { m_table.insert(m_obj); } 
};


template<typename Ctx>
void undo_trail_stack(Ctx & ctx, ptr_vector<trail<Ctx> > & s, unsigned old_size) {
    SASSERT(old_size <= s.size());
    typename ptr_vector<trail<Ctx> >::iterator begin = s.begin() + old_size;
    typename ptr_vector<trail<Ctx> >::iterator it    = s.end();
    while (it != begin) {
        --it;
        (*it)->undo(ctx);
    }
    s.shrink(old_size);
}

template<typename Ctx>
class trail_stack {
    Ctx &                   m_ctx;
    ptr_vector<trail<Ctx> > m_trail_stack;
    unsigned_vector         m_scopes;
    region                  m_region;
public:
    trail_stack(Ctx & c):m_ctx(c) {}

    ~trail_stack() {}
    
    region & get_region() { return m_region; }
    
    void reset() { 
        pop_scope(m_scopes.size()); 
        // Undo trail objects stored at lvl 0 (avoid memory leaks if lvl 0 contains new_obj_trail objects).
        undo_trail_stack(m_ctx, m_trail_stack, 0); 
    }

    void push_ptr(trail<Ctx> * t) { m_trail_stack.push_back(t); }

    template<typename TrailObject>
    void push(TrailObject const & obj) { m_trail_stack.push_back(new (m_region) TrailObject(obj)); }

    unsigned get_num_scopes() const { return m_scopes.size(); }

    void push_scope() { m_region.push_scope(); m_scopes.push_back(m_trail_stack.size()); }

    void pop_scope(unsigned num_scopes) {
        if (num_scopes == 0) return;
        unsigned lvl      = m_scopes.size();
        SASSERT(num_scopes <= lvl);
        unsigned new_lvl  = lvl - num_scopes;
        unsigned old_size = m_scopes[new_lvl];
        undo_trail_stack(m_ctx, m_trail_stack, old_size);
        m_scopes.shrink(new_lvl);
        m_region.pop_scope(num_scopes);
    }
};

#endif /* _TRAIL_H_ */