@@ -104,54 +104,45 @@ export function gen_ast_encoder_cc({
104
104
} ) ;
105
105
} else if ( m . kind === "node" && by_base . has ( m . type ) ) {
106
106
const className = makeClassName ( m . type ) ;
107
- emit (
108
- `const auto [${ m . name } , ${ m . name } Type] = accept${ className } (ast->${ m . name } );`
109
- ) ;
107
+ emit ( `const auto [${ m . name } , ${ m . name } Type] =` ) ;
108
+ emit ( ` accept${ className } (ast->${ m . name } );` ) ;
110
109
finalizers . push ( ( ) => {
111
110
emit ( ` builder.add_${ fieldName } (${ m . name } );` ) ;
112
- emit (
113
- ` builder.add_${ fieldName } _type(static_cast<io::${ className } >(${ m . name } Type));`
114
- ) ;
111
+ emit ( ` builder.add_${ fieldName } _type(` ) ;
112
+ emit ( ` static_cast<io::${ className } >(${ m . name } Type));` ) ;
115
113
} ) ;
116
114
} else if ( m . kind === "node-list" && ! by_base . has ( m . type ) ) {
117
115
const className = makeClassName ( m . type ) ;
118
- emit (
119
- ` std::vector<flatbuffers::Offset<io::${ className } >> ${ m . name } Offsets;`
120
- ) ;
116
+ emit ( ` std::vector<flatbuffers::Offset<io::${ className } >>` ) ;
117
+ emit ( ` ${ m . name } Offsets;` ) ;
121
118
emit ( ` for (auto it = ast->${ m . name } ; it; it = it->next) {` ) ;
122
119
emit ( ` if (!it->value) continue;` ) ;
123
120
emit ( ` ${ m . name } Offsets.emplace_back(accept(it->value).o);` ) ;
124
121
emit ( ` }` ) ;
125
122
emit ( ) ;
126
- emit (
127
- ` auto ${ m . name } OffsetsVector = fbb_.CreateVector(${ m . name } Offsets);`
128
- ) ;
123
+ emit ( ` auto ${ m . name } OffsetsVector = fbb_.CreateVector(` ) ;
124
+ emit ( ` ${ m . name } Offsets);` ) ;
129
125
finalizers . push ( ( ) => {
130
126
emit ( ` builder.add_${ fieldName } (${ m . name } OffsetsVector);` ) ;
131
127
} ) ;
132
128
} else if ( m . kind === "node-list" && by_base . has ( m . type ) ) {
133
129
const className = makeClassName ( m . type ) ;
134
130
emit ( ` std::vector<flatbuffers::Offset<>> ${ m . name } Offsets;` ) ;
135
- emit (
136
- ` std::vector<std::underlying_type_t<io::${ className } >> ${ m . name } Types;`
137
- ) ;
131
+ emit ( ` std::vector<std::underlying_type_t<io::${ className } >>` ) ;
132
+ emit ( ` ${ m . name } Types;` ) ;
138
133
emit ( ) ;
139
- emit ( ` for (auto it = ast->${ m . name } ; it; it = it->next) {` ) ;
140
- emit ( ` if (!it->value) continue;` ) ;
141
- emit (
142
- ` const auto [offset, type] = accept${ className } (it->value);`
143
- ) ;
144
- emit ( ` ${ m . name } Offsets.push_back(offset);` ) ;
145
- emit ( ` ${ m . name } Types.push_back(type);` ) ;
146
- emit ( ` }` ) ;
147
-
134
+ emit ( ` for (auto it = ast->${ m . name } ; it; it = it->next) {` ) ;
135
+ emit ( ` if (!it->value) continue;` ) ;
136
+ emit ( ` const auto [offset, type] = accept${ className } (` ) ;
137
+ emit ( ` it->value);` ) ;
138
+ emit ( ` ${ m . name } Offsets.push_back(offset);` ) ;
139
+ emit ( ` ${ m . name } Types.push_back(type);` ) ;
140
+ emit ( ` }` ) ;
148
141
emit ( ) ;
149
- emit (
150
- ` auto ${ m . name } OffsetsVector = fbb_.CreateVector(${ m . name } Offsets);`
151
- ) ;
152
- emit (
153
- ` auto ${ m . name } TypesVector = fbb_.CreateVector(${ m . name } Types);`
154
- ) ;
142
+ emit ( ` auto ${ m . name } OffsetsVector = fbb_.CreateVector(` ) ;
143
+ emit ( ` ${ m . name } Offsets);` ) ;
144
+ emit ( ` auto ${ m . name } TypesVector = fbb_.CreateVector(` ) ;
145
+ emit ( ` ${ m . name } Types);` ) ;
155
146
156
147
finalizers . push ( ( ) => {
157
148
emit ( ` builder.add_${ fieldName } (${ m . name } OffsetsVector);` ) ;
@@ -178,9 +169,13 @@ export function gen_ast_encoder_cc({
178
169
emitLiteral ( m , "identifiers_" , finalizers ) ;
179
170
} else if ( m . kind === "attribute" && m . type === "TokenKind" ) {
180
171
finalizers . push ( ( ) => {
181
- emit (
182
- ` builder.add_${ fieldName } (static_cast<std::uint32_t>(ast->${ m . name } ));`
183
- ) ;
172
+ emit ( ` builder.add_${ fieldName } (` ) ;
173
+ emit ( ` static_cast<std::uint32_t>(ast->${ m . name } ));` ) ;
174
+ } ) ;
175
+ } else if ( m . kind == "token" ) {
176
+ emit ( ` auto ${ m . name } = encodeSourceLocation(ast->${ m . name } );` ) ;
177
+ finalizers . push ( ( ) => {
178
+ emit ( ` builder.add_${ fieldName } (${ m . name } .o);` ) ;
184
179
} ) ;
185
180
}
186
181
} ) ;
@@ -207,6 +202,7 @@ export function gen_ast_encoder_cc({
207
202
#include <cxx/literals.h>
208
203
#include <cxx/names.h>
209
204
#include <cxx/translation_unit.h>
205
+ #include <cxx/private/format.h>
210
206
211
207
#include <algorithm>
212
208
@@ -219,13 +215,17 @@ auto ASTEncoder::operator()(TranslationUnit* unit) -> std::span<const std::uint8
219
215
Table<StringLiteral> stringLiterals;
220
216
Table<IntegerLiteral> integerLiterals;
221
217
Table<FloatLiteral> floatLiterals;
218
+ SourceFiles sourceFiles;
219
+ SourceLines sourceLines;
222
220
223
221
std::swap(unit_, unit);
224
222
std::swap(identifiers_, identifiers);
225
223
std::swap(charLiterals_, charLiterals);
226
224
std::swap(stringLiterals_, stringLiterals);
227
225
std::swap(integerLiterals_, integerLiterals);
228
226
std::swap(floatLiterals_, floatLiterals);
227
+ std::swap(sourceFiles_, sourceFiles);
228
+ std::swap(sourceLines_, sourceLines);
229
229
230
230
auto [unitOffset, unitType] = acceptUnit(unit_->ast());
231
231
@@ -242,6 +242,8 @@ auto ASTEncoder::operator()(TranslationUnit* unit) -> std::span<const std::uint8
242
242
std::swap(stringLiterals_, stringLiterals);
243
243
std::swap(integerLiterals_, integerLiterals);
244
244
std::swap(floatLiterals_, floatLiterals);
245
+ std::swap(sourceFiles_, sourceFiles);
246
+ std::swap(sourceLines_, sourceLines);
245
247
246
248
fbb_.Finish(builder.Finish(), io::SerializedUnitIdentifier());
247
249
@@ -257,6 +259,49 @@ auto ASTEncoder::accept(AST* ast) -> flatbuffers::Offset<> {
257
259
return offset;
258
260
}
259
261
262
+ auto ASTEncoder::encodeSourceLocation(const SourceLocation& loc)
263
+ -> flatbuffers::Offset<> {
264
+ if (!loc) {
265
+ return {};
266
+ }
267
+
268
+ std::string_view fileName;
269
+ uint32_t line = 0, column = 0;
270
+ unit_->getTokenStartPosition(loc, &line, &column, &fileName);
271
+
272
+ flatbuffers::Offset<io::SourceLine> sourceLineOffset;
273
+
274
+ auto key = std::tuple(fileName, line);
275
+
276
+ if (sourceLines_.contains(key)) {
277
+ sourceLineOffset = sourceLines_.at(key).o;
278
+ } else {
279
+ flatbuffers::Offset<flatbuffers::String> fileNameOffset;
280
+
281
+ if (sourceFiles_.contains(fileName)) {
282
+ fileNameOffset = sourceFiles_.at(fileName);
283
+ } else {
284
+ fileNameOffset = fbb_.CreateString(fileName);
285
+ sourceFiles_.emplace(fileName, fileNameOffset.o);
286
+ }
287
+
288
+ io::SourceLineBuilder sourceLineBuilder{fbb_};
289
+ sourceLineBuilder.add_file_name(fileNameOffset);
290
+ sourceLineBuilder.add_line(line);
291
+ sourceLineOffset = sourceLineBuilder.Finish();
292
+ sourceLines_.emplace(std::move(key), sourceLineOffset.o);
293
+ }
294
+
295
+ io::SourceLocationBuilder sourceLocationBuilder{fbb_};
296
+ sourceLocationBuilder.add_source_line(sourceLineOffset);
297
+ sourceLocationBuilder.add_column(column);
298
+
299
+ auto offset = sourceLocationBuilder.Finish();
300
+
301
+ return offset.Union();
302
+ }
303
+
304
+
260
305
${ code . join ( "\n" ) }
261
306
262
307
} // namespace cxx
0 commit comments