rev: ebafc720a90fa0562f276f82113a43e8e7c3ee88 scopes/src/qualifier/unique_qualifiers.cpp -rw-r--r-- 6.8 KiB View raw Log this file
ebafc720a90f — Leonard Ritter * merged default branch 7 days ago
                                                                                
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
/*
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.
*/

#include "unique_qualifiers.hpp"
#include "../error.hpp"
#include "../dyn_cast.inc"
#include "../hash.hpp"
#include "../qualifier.inc"
#include "../styled_stream.hpp"

#include <algorithm>

namespace scopes {

//------------------------------------------------------------------------------

namespace ViewSet {
struct Hash {
    std::size_t operator()(const ViewQualifier *s) const {
        return s->prehash;
    }
};

struct KeyEqual {
    bool operator()( const ViewQualifier *lhs, const ViewQualifier *rhs ) const {
        return lhs->ids == rhs->ids;
    }
};
} // namespace ViewSet

static std::unordered_set<const ViewQualifier *, ViewSet::Hash, ViewSet::KeyEqual> views;

//------------------------------------------------------------------------------

namespace UniqueSet {
struct Hash {
    std::size_t operator()(const UniqueQualifier *s) const {
        return std::hash<int>{}(s->id);
    }
};

struct KeyEqual {
    bool operator()( const UniqueQualifier *lhs, const UniqueQualifier *rhs ) const {
        return lhs->id == rhs->id;
    }
};
} // namespace ViewSet

static std::unordered_set<const UniqueQualifier *, UniqueSet::Hash, UniqueSet::KeyEqual> uniques;

//------------------------------------------------------------------------------

MutateQualifier::MutateQualifier()
    : Qualifier((QualifierKind)Kind) {}

void MutateQualifier::stream_prefix(StyledStream &ss) const {
}

void MutateQualifier::stream_postfix(StyledStream &ss) const {
    ss << "!";
}

//------------------------------------------------------------------------------

void stream_id(StyledStream &ss, int id) {
    if (id >= 0) {
        ss << id;
    } else if (id < LastUniqueOutput) {
        ss << "E" << -id+LastUniqueOutput;
    } else if (id < 0) {
        ss << "R" << -id;
    }
}

//------------------------------------------------------------------------------

void map_unique_id(ID2SetMap &idmap, int fromid, int toid) {
    auto result = idmap.insert({fromid, { toid }});
    if (!result.second) {
        result.first->second.insert(toid);
    }
}

void dump_idmap(const ID2SetMap &idmap) {
    StyledStream ss;
    for (auto && entry : idmap) {
        ss << entry.first << ": ";
        for (auto && id : entry.second) {
            ss << id << " ";
        }
        ss << std::endl;
    }
    ss << idmap.size() << " entries" << std::endl;
}

IDSet difference_idset(const IDSet &a, const IDSet &b) {
    IDSet c;
    c.reserve(a.size());
    for (auto id : a) {
        assert(id);
        if (!b.count(id))
            c.insert(id);
    }
    return c;
}

IDSet intersect_idset(const IDSet &a, const IDSet &b) {
    IDSet c;
    c.reserve(std::min(a.size(), b.size()));
    for (auto id : a) {
        assert(id);
        if (b.count(id))
            c.insert(id);
    }
    return c;
}

IDSet union_idset(const IDSet &a, const IDSet &b) {
    IDSet c;
    c.reserve(std::max(a.size(), b.size()));
    for (auto id : a) {
        assert(id);
        c.insert(id);
    }
    for (auto id : b) {
        assert(id);
        c.insert(id);
    }
    return c;
}

void dump_idset(const IDSet &a) {
    StyledStream ss;
    for (auto id : a) {
        ss << id << " ";
    }
    ss << std::endl;
}

ViewQualifier::ViewQualifier(const IDSet &_ids)
    : Qualifier((QualifierKind)Kind), ids(_ids) {
    for (auto entry : ids) {
        sorted_ids.push_back(entry);
    }
    std::sort(sorted_ids.begin(), sorted_ids.end());
    std::size_t h = 0;
    for (auto &&entry : sorted_ids) {
        h = hash2(h, std::hash<int>{}(entry));
    }
    prehash = h;
}

void ViewQualifier::stream_prefix(StyledStream &ss) const {
    ss << "%";
    if (sorted_ids.empty()) {
        ss << "?";
    } else {
        for (int i = 0; i < sorted_ids.size(); ++i) {
            if (i > 0) ss << "|";
            stream_id(ss, sorted_ids[i]);
        }
    }
    ss << ":";
}

void ViewQualifier::stream_postfix(StyledStream &ss) const {
}

//------------------------------------------------------------------------------

void UniqueQualifier::stream_prefix(StyledStream &ss) const {
    stream_id(ss, id);
    ss << ":";
}

void UniqueQualifier::stream_postfix(StyledStream &ss) const {
}

UniqueQualifier::UniqueQualifier(int _id)
    : Qualifier((QualifierKind)Kind), id(_id) {
}

//------------------------------------------------------------------------------

bool is_view(const Type *T) {
    return has_qualifier<ViewQualifier>(T);
}

const Type *strip_view(const Type *T) {
    return strip_qualifier<ViewQualifier>(T);
}

const ViewQualifier *get_view(const Type *T) {
    return get_qualifier<ViewQualifier>(T);
}

const ViewQualifier *try_view(const Type *T) {
    return try_qualifier<ViewQualifier>(T);
}

//------------------------------------------------------------------------------

bool is_unique(const Type *T) {
    return has_qualifier<UniqueQualifier>(T);
}

bool is_movable(const Type *T) {
    return is_unique(T) || is_plain(T);
}

const Type *strip_unique(const Type *T) {
    return strip_qualifier<UniqueQualifier>(T);
}

const UniqueQualifier *get_unique(const Type *T) {
    return get_qualifier<UniqueQualifier>(T);
}

const UniqueQualifier *try_unique(const Type *T) {
    return try_qualifier<UniqueQualifier>(T);
}

//------------------------------------------------------------------------------

const Type *strip_lifetime(const Type *T) {
    return strip_qualifiers(T, QM_UniquenessTags);
}

//------------------------------------------------------------------------------

static const MutateQualifier *_mutate_qualifier = nullptr;
const Type *mutate_type(const Type *type) {
    if (has_qualifier<MutateQualifier>(type))
        return type;
    if (!_mutate_qualifier) {
        _mutate_qualifier = new MutateQualifier();
    }
    return qualify(type, { _mutate_qualifier });
}

const Type * view_type(const Type *type, IDSet ids) {
    auto ut = try_qualifier<UniqueQualifier>(type);
    if (ut) {
        ids.insert(ut->id);
        type = strip_qualifier<UniqueQualifier>(type);
    }
    auto vt = try_qualifier<ViewQualifier>(type);
    if (vt) {
        for (auto entry : vt->ids) {
            ids.insert(entry);
        }
    }
    const ViewQualifier *result = nullptr;
    ViewQualifier key(ids);
    auto it = views.find(&key);
    if (it != views.end()) {
        result = *it;
    } else {
        result = new ViewQualifier(ids);
        views.insert(result);
    }
    return qualify(type, { result });
}

const Type * unique_type(const Type *type, int id) {
    type = strip_qualifier<ViewQualifier>(type);
    const UniqueQualifier *result = nullptr;
    UniqueQualifier key(id);
    auto it = uniques.find(&key);
    if (it != uniques.end()) {
        result = *it;
    } else {
        result = new UniqueQualifier(id);
        uniques.insert(result);
    }
    return qualify(type, { result });
}

} // namespace scopes