@@ -70,7 +70,7 @@ struct string_view_ref : public string_range
70
70
}
71
71
};
72
72
73
- template <typename Iter>
73
+ template <typename Iter, typename OutputType = string_view_ref >
74
74
struct string_view_deref_range {
75
75
std::string & _str_ref;
76
76
Iter _begin, _end;
@@ -80,7 +80,7 @@ struct string_view_deref_range {
80
80
Iter _itr;
81
81
82
82
using iterator_category = typename Iter::iterator_category;
83
- using value_type = string_view_ref ;
83
+ using value_type = OutputType ;
84
84
using difference_type = typename Iter::difference_type;
85
85
using pointer = value_type *;
86
86
using reference = value_type &;
@@ -121,17 +121,28 @@ struct xt_inst_file
121
121
{
122
122
std::string contents;
123
123
// views into the contents:
124
- string_range impl_project { 0 , 0 };
125
- std::vector<string_range> exported_symbols;
126
- std::vector<string_range> instantiated_symbols;
127
124
128
- auto get_impl_project () {
129
- return impl_project.get (contents);
130
- }
125
+ string_range version { 0 , 0 };
126
+
127
+ struct exp_symbol { string_range flags, name; };
128
+
129
+ struct exp_symbol_ref : public exp_symbol {
130
+ std::string & str_ref;
131
+ std::string_view get_flags () { return flags.get (str_ref); }
132
+ bool has_flag (const char c) { return get_flags ().find (c) != std::string_view::npos; }
133
+ bool has_flags (std::string_view cs) {
134
+ for (const char c : cs) if (!has_flag (c)) return false ;
135
+ return true ;
136
+ }
137
+ std::string_view get_name () { return name.get (str_ref); }
138
+ };
139
+
140
+ std::vector<exp_symbol> exported_symbols;
141
+ std::vector<string_range> instantiated_symbols;
131
142
132
143
// return a range of string_view_refs
133
144
auto get_exported_symbols () {
134
- return string_view_deref_range<decltype (exported_symbols)::iterator> {
145
+ return string_view_deref_range<decltype (exported_symbols)::iterator, exp_symbol_ref > {
135
146
contents, exported_symbols.begin (), exported_symbols.end ()
136
147
};
137
148
}
@@ -143,38 +154,40 @@ struct xt_inst_file
143
154
};
144
155
}
145
156
146
- string_range append_enclosed_string (
147
- std::string_view before,
148
- std::string_view str,
149
- std::string_view after)
150
- {
151
- contents += before;
157
+ string_range append_string (std::string_view str) {
152
158
std::size_t pos = contents.size ();
153
159
contents += str;
154
- contents += after;
155
160
return { pos, str.size () };
156
161
}
157
162
158
163
static constexpr std::string_view
159
- project_prefix = " // proj " ,
164
+ version_prefix = " // ver " ,
160
165
export_prefix = " // exp " ,
161
166
instantiation_prefix = " template " ;
162
167
163
- void append_impl_project (std::string_view project) {
164
- impl_project =
165
- append_enclosed_string (project_prefix, project, " \n " );
168
+ static constexpr std::string_view
169
+ current_version = " 1" ;
170
+
171
+ void append_version () {
172
+ contents += version_prefix;
173
+ contents += current_version;
174
+ contents += ' \n ' ;
166
175
}
167
176
168
- void append_exported_symbol (std::string_view symbol) {
169
- exported_symbols.emplace_back (
170
- append_enclosed_string (export_prefix, symbol, " \n " )
171
- );
177
+ void append_exported_symbol (std::string_view symbol, std::string_view flags) {
178
+ contents += export_prefix;
179
+ auto flags_range = append_string (flags);
180
+ contents += ' ' ;
181
+ auto name_range = append_string (symbol);
182
+ contents += ' \n ' ;
183
+ exported_symbols.emplace_back (exp_symbol { flags_range, name_range });
172
184
}
173
185
174
186
void append_instantiated_symbol (std::string_view symbol) {
175
- instantiated_symbols.emplace_back (
176
- append_enclosed_string (instantiation_prefix, symbol, " ;\n " )
177
- );
187
+ contents += instantiation_prefix;
188
+ auto symbol_range = append_string (symbol);
189
+ contents += " ;\n " ;
190
+ instantiated_symbols.emplace_back (symbol_range);
178
191
}
179
192
};
180
193
@@ -202,6 +215,8 @@ struct xt_inst_file_reader_writer
202
215
ret.exported_symbols .reserve (nr_lines);
203
216
ret.instantiated_symbols .reserve (nr_lines);
204
217
218
+ std::string_view version;
219
+
205
220
auto it = ret.contents .begin ();
206
221
while (it != ret.contents .end ()) {
207
222
auto it_end = std::find (it, ret.contents .end (), ' \n ' );
@@ -217,10 +232,15 @@ struct xt_inst_file_reader_writer
217
232
return false ;
218
233
};
219
234
220
- if (check_and_remove (xt_inst_file::project_prefix )) {
221
- ret. impl_project . set_view (ret. contents , line) ;
235
+ if (check_and_remove (xt_inst_file::version_prefix )) {
236
+ version = line;
222
237
} else if (check_and_remove (xt_inst_file::export_prefix)) {
223
- ret.exported_symbols .emplace_back (ret.contents , line);
238
+ std::string_view flags = line.substr (0 , line.find (' ' ));
239
+ std::string_view name = line.substr (flags.size () + 1 );
240
+ ret.exported_symbols .emplace_back (xt_inst_file::exp_symbol {
241
+ string_range { ret.contents , flags },
242
+ string_range { ret.contents , name }
243
+ });
224
244
} else if (check_and_remove (xt_inst_file::instantiation_prefix)) {
225
245
line.remove_suffix (1 ); // for the ; at the end
226
246
ret.instantiated_symbols .emplace_back (ret.contents , line);
@@ -230,6 +250,12 @@ struct xt_inst_file_reader_writer
230
250
if (it_end == ret.contents .end ()) break ;
231
251
it = it_end + 1 ;
232
252
}
253
+
254
+ if (version != xt_inst_file::current_version) {
255
+ fmt::print (" note: version mismatch, ignoring file contents\n " );
256
+ ret = {};
257
+ }
258
+
233
259
return ret;
234
260
}
235
261
0 commit comments