• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rieske / trans / 28461496314

30 Jun 2026 04:55PM UTC coverage: 90.13% (-0.4%) from 90.53%
28461496314

Pull #44

github

rieske
Support multi-word struct pass/return and expand struct tests

Pass multi-word structs on the stack with correct SP adjustments when
pushing words; copy aggregates word-wise on assign. Return up to two
words in RAX/RDX and restore them on retrieve. Use the real function
return type from declaration specs (fixes void calls and struct
returns). Expand StructsTest for by-value/by-address args in different
positions, mutation, return by value/pointer, assign, and chains.
Pull Request #44: Implement C structs with member access

329 of 404 new or added lines in 13 files covered. (81.44%)

5415 of 6008 relevant lines covered (90.13%)

240586.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.28
/src/ast/ContextualSyntaxNodeBuilder.cpp
1
#include "ContextualSyntaxNodeBuilder.h"
2
#include "MemberAccess.h"
3

4
#include <algorithm>
5
#include <sstream>
6

7
#include "ArithmeticExpression.h"
8
#include "ArrayAccess.h"
9
#include "ArrayDeclarator.h"
10
#include "AssignmentExpression.h"
11
#include "BitwiseExpression.h"
12
#include "Block.h"
13
#include "ComparisonExpression.h"
14
#include "ConstantExpression.h"
15
#include "ExpressionList.h"
16
#include "ForLoopHeader.h"
17
#include "FunctionCall.h"
18
#include "FunctionDefinition.h"
19
#include "Identifier.h"
20
#include "IdentifierExpression.h"
21
#include "IfElseStatement.h"
22
#include "IfStatement.h"
23
#include "JumpStatement.h"
24
#include "LogicalAndExpression.h"
25
#include "LogicalOrExpression.h"
26
#include "LoopStatement.h"
27
#include "Operator.h"
28
#include "PostfixExpression.h"
29
#include "PrefixExpression.h"
30
#include "ReturnStatement.h"
31
#include "VoidReturnStatement.h"
32
#include "ShiftExpression.h"
33
#include "TypeCast.h"
34
#include "UnaryExpression.h"
35
#include "WhileLoopHeader.h"
36

37
#include "ast/StringLiteralExpression.h"
38
#include "types/Type.h"
39

40
namespace ast {
41

42
void doNothing(AbstractSyntaxTreeBuilderContext&) {
75,434 ✔
43
}
75,434 ✔
44

45
void shortType(AbstractSyntaxTreeBuilderContext& context) {
×
46
    throw std::runtime_error { "short type is not implemented yet" };
×
47
}
48

49
void integerType(AbstractSyntaxTreeBuilderContext& context) {
1,212 ✔
50
    context.pushTypeSpecifier( { type::signedInteger(), context.popTerminal().value });
1,212 ✔
51
}
1,212 ✔
52

53
void longType(AbstractSyntaxTreeBuilderContext& context) {
×
54
    throw std::runtime_error { "long type is not implemented yet" };
×
55
}
56

57
void characterType(AbstractSyntaxTreeBuilderContext& context) {
6 ✔
58
    context.pushTypeSpecifier( { type::signedCharacter(), context.popTerminal().value });
6 ✔
59
}
6 ✔
60

61
void voidType(AbstractSyntaxTreeBuilderContext& context) {
46 ✔
62
    context.pushTypeSpecifier( { type::voidType(), context.popTerminal().value });
46 ✔
63
}
46 ✔
64

65
void floatType(AbstractSyntaxTreeBuilderContext& context) {
×
66
    context.pushTypeSpecifier( { type::floating(), context.popTerminal().value });
×
67
}
×
68

69
void doubleType(AbstractSyntaxTreeBuilderContext& context) {
×
70
    throw std::runtime_error { "double type is not implemented yet" };
×
71
}
72

73
void signedType(AbstractSyntaxTreeBuilderContext& context) {
×
74
    throw std::runtime_error { "signed type is not implemented yet" };
×
75
}
76

77
void unsignedType(AbstractSyntaxTreeBuilderContext& context) {
×
78
    throw std::runtime_error { "unsigned type is not implemented yet" };
×
79
}
80

81
void typedefName(AbstractSyntaxTreeBuilderContext& context) {
×
82
    throw std::runtime_error { "typedefName type is not implemented yet" };
×
83
}
84

85
void structOrUnionType(AbstractSyntaxTreeBuilderContext& context) {
106 ✔
86
    // type_spec -> struct_or_union_spec: TypeSpecifier already pushed.
87
}
106 ✔
88

89
void enumType(AbstractSyntaxTreeBuilderContext& context) {
×
90
    throw std::runtime_error { "enumType type is not implemented yet" };
×
91
}
92

93
void constQualifier(AbstractSyntaxTreeBuilderContext& context) {
×
94
    context.pushTypeQualifier(type::Qualifier::CONST);
×
95
}
×
96

97
void volatileQualifier(AbstractSyntaxTreeBuilderContext& context) {
×
98
    context.pushTypeQualifier(type::Qualifier::VOLATILE);
×
99
}
×
100

101
void typeQualifierList(AbstractSyntaxTreeBuilderContext& context) {
×
102
    context.newTypeQualifierList(context.popTypeQualifier());
×
103
}
×
104

105
void addTypeQualifierToList(AbstractSyntaxTreeBuilderContext& context) {
×
106
    context.addToTypeQualifierList(context.popTypeQualifier());
×
107
}
×
108

109
void parenthesizedExpression(AbstractSyntaxTreeBuilderContext& context) {
52 ✔
110
    context.popTerminal();
52 ✔
111
    context.popTerminal();
52 ✔
112
}
52 ✔
113

114
void declarationTypeSpecifier(AbstractSyntaxTreeBuilderContext& context) {
1,300 ✔
115
    context.pushDeclarationSpecifiers( { context.popTypeSpecifier() });
1,300 ✔
116
}
1,300 ✔
117

118
void addDeclarationTypeSpecifier(AbstractSyntaxTreeBuilderContext& context) {
×
119
    auto declarationSpecifiers = context.popDeclarationSpecifiers();
×
120
    auto typeSpecifier = context.popTypeSpecifier();
×
121
    context.pushDeclarationSpecifiers( { typeSpecifier, declarationSpecifiers });
×
122
}
×
123

124
void declarationStorageClassSpecifier(AbstractSyntaxTreeBuilderContext& context) {
×
125
    context.pushDeclarationSpecifiers( { context.popStorageSpecifier() });
×
126
}
×
127

128
void addDeclarationStorageClassSpecifier(AbstractSyntaxTreeBuilderContext& context) {
×
129
    auto declarationSpecifiers = context.popDeclarationSpecifiers();
×
130
    auto storageSpecifier = context.popStorageSpecifier();
×
131
    context.pushDeclarationSpecifiers( { storageSpecifier, declarationSpecifiers });
×
132
}
×
133

134
void declarationTypeQualifier(AbstractSyntaxTreeBuilderContext& context) {
×
135
    context.pushDeclarationSpecifiers( { context.popTypeQualifier() });
×
136
}
×
137

138
void addDeclarationTypeQualifier(AbstractSyntaxTreeBuilderContext& context) {
×
139
    auto declarationSpecifiers = context.popDeclarationSpecifiers();
×
140
    auto typeQualifier = context.popTypeQualifier();
×
141
    context.pushDeclarationSpecifiers( { typeQualifier, declarationSpecifiers });
×
142
}
×
143

144
void identifierDeclarator(AbstractSyntaxTreeBuilderContext& context) {
1,452 ✔
145
    context.pushDirectDeclarator(std::make_unique<Identifier>(context.popTerminal()));
1,452 ✔
146
}
1,452 ✔
147

148
void arrayDeclarator(AbstractSyntaxTreeBuilderContext& context) {
×
149
    context.popTerminal();
×
150
    context.popTerminal();
×
151
    context.pushDirectDeclarator(std::make_unique<ArrayDeclarator>(context.popDirectDeclarator(), context.popExpression()));
×
152
}
×
153

154
void abstractArrayDeclarator(AbstractSyntaxTreeBuilderContext& context) {
×
155
    context.popTerminal();
×
156
    context.popTerminal();
×
157
    //context.pushDeclarator(std::make_unique<ArrayDeclarator>(context.popDirectDeclarator()));
158
    throw std::runtime_error { "abstract array declarator is not implemented yet" };
×
159
}
160

161
void functionDeclarator(AbstractSyntaxTreeBuilderContext& context) {
90 ✔
162
    context.popTerminal();
90 ✔
163
    context.popTerminal();
90 ✔
164
    auto arguments = context.popArgumentsDeclaration().first;
90 ✔
165
    // `(void)` is an empty parameter list, not a single void parameter.
166
    if (arguments.size() == 1 && arguments.front().isVoid()) {
90 ✔
167
        arguments.clear();
6 ✔
168
    }
169
    context.pushDirectDeclarator(std::make_unique<FunctionDeclarator>(context.popDirectDeclarator(), std::move(arguments)));
90 ✔
170
}
90 ✔
171

172
void noargFunctionDeclarator(AbstractSyntaxTreeBuilderContext& context) {
434 ✔
173
    context.popTerminal();
434 ✔
174
    context.popTerminal();
434 ✔
175
    context.pushDirectDeclarator(std::make_unique<FunctionDeclarator>(context.popDirectDeclarator()));
434 ✔
176
}
434 ✔
177

178
void pointerToDeclarator(AbstractSyntaxTreeBuilderContext& context) {
84 ✔
179
    context.pushDeclarator(std::make_unique<Declarator>(context.popDirectDeclarator(), context.popPointers()));
84 ✔
180
}
84 ✔
181

182
void declarator(AbstractSyntaxTreeBuilderContext& context) {
1,368 ✔
183
    context.pushDeclarator(std::make_unique<Declarator>(context.popDirectDeclarator()));
1,368 ✔
184
}
1,368 ✔
185

186
void parameterDeclaration(AbstractSyntaxTreeBuilderContext& context) {
196 ✔
187
    context.pushFormalArgument(FormalArgument { context.popDeclarationSpecifiers(), context.popDeclarator() });
196 ✔
188
}
196 ✔
189

190
void abstractParameterDeclaration(AbstractSyntaxTreeBuilderContext& context) {
×
191
    throw std::runtime_error { "abstractParameterDeclaration is not implemented yet" };
×
192
    //context.pushFormalArgument(std::make_unique<FormalArgument>(context.popDeclarationSpecifiers(), context.popAbstractDeclarator()));
193
}
194

195
void parameterBaseTypeDeclaration(AbstractSyntaxTreeBuilderContext& context) {
6 ✔
196
    context.pushFormalArgument(FormalArgument { context.popDeclarationSpecifiers() });
6 ✔
197
}
6 ✔
198

199
void formalArguments(AbstractSyntaxTreeBuilderContext& context) {
90 ✔
200
    FormalArguments formalArguments;
90 ✔
201
    formalArguments.push_back(context.popFormalArgument());
90 ✔
202
    context.pushFormalArguments(std::move(formalArguments));
90 ✔
203
}
90 ✔
204

205
void addFormalArgument(AbstractSyntaxTreeBuilderContext& context) {
112 ✔
206
    context.popTerminal();
112 ✔
207
    auto formalArguments = context.popFormalArguments();
112 ✔
208
    formalArguments.push_back(context.popFormalArgument());
112 ✔
209
    context.pushFormalArguments(std::move(formalArguments));
112 ✔
210
}
112 ✔
211

212
void formalArgumentsDeclaration(AbstractSyntaxTreeBuilderContext& context) {
90 ✔
213
    context.pushArgumentsDeclaration(std::make_pair(context.popFormalArguments(), false));
90 ✔
214
}
90 ✔
215

216
void formalArgumentsWithVararg(AbstractSyntaxTreeBuilderContext& context) {
×
217
    context.popTerminal();
×
218
    context.popTerminal();
×
219
    //context.pushArgumentsDeclaration(std::make_pair(context.popFormalArguments(), true));
220
    throw std::runtime_error { "formalArgumentsWithVararg is not implemented yet" };
×
221
}
222

223
void integerConstant(AbstractSyntaxTreeBuilderContext& context) {
1,360 ✔
224
    auto constant = context.popTerminal();
1,360 ✔
225
    context.pushConstant( { constant.value, type::signedInteger(), constant.context });
1,360 ✔
226
}
1,360 ✔
227

228
void characterConstant(AbstractSyntaxTreeBuilderContext& context) {
14 ✔
229
    auto constant = context.popTerminal();
14 ✔
230
    context.pushConstant( { constant.value, type::signedCharacter(), constant.context });
14 ✔
231
}
14 ✔
232

233
void floatConstant(AbstractSyntaxTreeBuilderContext& context) {
×
234
    auto constant = context.popTerminal();
×
235
    throw std::runtime_error { "floating constants not implemented yet" };
×
236
    // context.pushConstant( { constant.value, type::floating(), constant.context });
237
}
×
238

239
void enumerationConstant(AbstractSyntaxTreeBuilderContext& context) {
×
240
    auto constant = context.popTerminal();
×
241
    throw std::runtime_error { "enumerationConstant is not implemented yet" };
×
242
}
×
243

244
void identifierExpression(AbstractSyntaxTreeBuilderContext& context) {
2,944 ✔
245
    auto identifier = context.popTerminal();
2,944 ✔
246
    context.pushExpression(std::make_unique<IdentifierExpression>(identifier.value, identifier.context));
2,944 ✔
247
}
2,944 ✔
248

249
void constantExpression(AbstractSyntaxTreeBuilderContext& context) {
1,374 ✔
250
    context.pushExpression(std::make_unique<ConstantExpression>(context.popConstant()));
1,374 ✔
251
}
1,374 ✔
252

253
void stringLiteralExpression(AbstractSyntaxTreeBuilderContext& context) {
830 ✔
254
    auto literal = context.popTerminal();
830 ✔
255
    context.pushExpression(std::make_unique<StringLiteralExpression>(literal.value, literal.context));
830 ✔
256
}
830 ✔
257

258
void arrayAccess(AbstractSyntaxTreeBuilderContext& context) {
×
259
    context.popTerminal(); // ]
×
260
    context.popTerminal(); // [
×
261
    auto subscriptExpression = context.popExpression();
×
262
    auto postfixExpression = context.popExpression();
×
263
    context.pushExpression(std::make_unique<ArrayAccess>(std::move(postfixExpression), std::move(subscriptExpression)));
×
264
}
×
265

266
void functionCall(AbstractSyntaxTreeBuilderContext& context) {
932 ✔
267
    context.popTerminal(); // )
932 ✔
268
    context.popTerminal(); // (
932 ✔
269
    context.pushExpression(std::make_unique<FunctionCall>(context.popExpression(), context.popActualArgumentsList()));
932 ✔
270
}
932 ✔
271

272
void noargFunctionCall(AbstractSyntaxTreeBuilderContext& context) {
28 ✔
273
    context.popTerminal(); // )
28 ✔
274
    context.popTerminal(); // (
28 ✔
275
    context.pushExpression(std::make_unique<FunctionCall>(context.popExpression()));
28 ✔
276
}
28 ✔
277

278
void directMemberAccess(AbstractSyntaxTreeBuilderContext& context) {
142 ✔
279
    auto member = context.popTerminal();
142 ✔
280
    context.popTerminal();
142 ✔
281
    auto base = context.popExpression();
142 ✔
282
    context.pushExpression(std::make_unique<MemberAccess>(std::move(base), member.value, false, member.context));
142 ✔
283
}
142 ✔
284

285
void pointeeMemberAccess(AbstractSyntaxTreeBuilderContext& context) {
28 ✔
286
    auto member = context.popTerminal();
28 ✔
287
    context.popTerminal();
28 ✔
288
    auto base = context.popExpression();
28 ✔
289
    context.pushExpression(std::make_unique<MemberAccess>(std::move(base), member.value, true, member.context));
28 ✔
290
}
28 ✔
291

292
void postfixIncrementDecrement(AbstractSyntaxTreeBuilderContext& context) {
24 ✔
293
    context.pushExpression(std::make_unique<PostfixExpression>(context.popExpression(), std::make_unique<Operator>(context.popTerminal().type)));
24 ✔
294
}
24 ✔
295

296
void prefixIncrementDecrement(AbstractSyntaxTreeBuilderContext& context) {
28 ✔
297
    context.pushExpression(std::make_unique<PrefixExpression>(std::make_unique<Operator>(context.popTerminal().value), context.popExpression()));
28 ✔
298
}
28 ✔
299

300
void unaryExpression(AbstractSyntaxTreeBuilderContext& context) {
558 ✔
301
    context.pushExpression(std::make_unique<UnaryExpression>(std::make_unique<Operator>(context.popTerminal().value), context.popExpression()));
558 ✔
302
}
558 ✔
303

304
void sizeofExpression(AbstractSyntaxTreeBuilderContext& context) {
×
305
    throw std::runtime_error { "sizeofExpression is not implemented yet" };
×
306
}
307

308
void sizeofTypeExpression(AbstractSyntaxTreeBuilderContext& context) {
×
309
    throw std::runtime_error { "sizeofTypeExpression is not implemented yet" };
×
310
}
311

312
void typeCast(AbstractSyntaxTreeBuilderContext& context) {
×
313
    context.popTerminal(); // )
×
314
    //context.pushExpression(std::make_unique<TypeCast>(context.popTypeName(), context.popExpression()));
315
    context.popTerminal(); // (
×
316
    throw std::runtime_error { "typeCast is not implemented yet" };
×
317
}
318

319
void arithmeticExpression(AbstractSyntaxTreeBuilderContext& context) {
140 ✔
320
    auto rightHandSide = context.popExpression();
140 ✔
321
    auto leftHandSide = context.popExpression();
140 ✔
322
    auto arithmeticOperator = std::make_unique<Operator>(context.popTerminal().value);
140 ✔
323
    context.pushExpression(std::make_unique<ArithmeticExpression>(std::move(leftHandSide), std::move(arithmeticOperator), std::move(rightHandSide)));
140 ✔
324
}
140 ✔
325

326
void shiftExpression(AbstractSyntaxTreeBuilderContext& context) {
20 ✔
327
    auto additionExpression = context.popExpression();
20 ✔
328
    auto shiftExpression = context.popExpression();
20 ✔
329
    auto shiftOperator = std::make_unique<Operator>(context.popTerminal().value);
20 ✔
330
    context.pushExpression(std::make_unique<ShiftExpression>(std::move(shiftExpression), std::move(shiftOperator), std::move(additionExpression)));
20 ✔
331
}
20 ✔
332

333
void relationalExpression(AbstractSyntaxTreeBuilderContext& context) {
180 ✔
334
    auto rightHandSide = context.popExpression();
180 ✔
335
    auto leftHandSide = context.popExpression();
180 ✔
336
    auto comparisonOperator = std::make_unique<Operator>(context.popTerminal().value);
180 ✔
337
    context.pushExpression(std::make_unique<ComparisonExpression>(std::move(leftHandSide), std::move(comparisonOperator), std::move(rightHandSide)));
180 ✔
338
}
180 ✔
339

340
void bitwiseExpression(AbstractSyntaxTreeBuilderContext& context) {
16 ✔
341
    auto rightHandSide = context.popExpression();
16 ✔
342
    auto leftHandSide = context.popExpression();
16 ✔
343
    auto bitwiseOperator = std::make_unique<Operator>(context.popTerminal().value);
16 ✔
344
    context.pushExpression(std::make_unique<BitwiseExpression>(std::move(leftHandSide), std::move(bitwiseOperator), std::move(rightHandSide)));
16 ✔
345
}
16 ✔
346

347
void logicalAndExpression(AbstractSyntaxTreeBuilderContext& context) {
12 ✔
348
    context.popTerminal();
12 ✔
349
    auto rightHandSide = context.popExpression();
12 ✔
350
    auto leftHandSide = context.popExpression();
12 ✔
351
    context.pushExpression(std::make_unique<LogicalAndExpression>(std::move(leftHandSide), std::move(rightHandSide)));
12 ✔
352
}
12 ✔
353

354
void logicalOrExpression(AbstractSyntaxTreeBuilderContext& context) {
12 ✔
355
    context.popTerminal();
12 ✔
356
    auto rightHandSide = context.popExpression();
12 ✔
357
    auto leftHandSide = context.popExpression();
12 ✔
358
    context.pushExpression(std::make_unique<LogicalOrExpression>(std::move(leftHandSide), std::move(rightHandSide)));
12 ✔
359
}
12 ✔
360

361
void conditionalExpression(AbstractSyntaxTreeBuilderContext& context) {
×
362
    throw std::runtime_error { "conditionalExpression is not implemented yet" };
×
363
}
364

365
void assignmentExpression(AbstractSyntaxTreeBuilderContext& context) {
562 ✔
366
    auto rightHandSide = context.popExpression();
562 ✔
367
    auto leftHandSide = context.popExpression();
562 ✔
368
    auto assignmentOperator = std::make_unique<Operator>(context.popTerminal().value);
562 ✔
369
    context.pushExpression(
562 ✔
370
            std::make_unique<AssignmentExpression>(std::move(leftHandSide), std::move(assignmentOperator), std::move(rightHandSide)));
1,124 ✔
371
}
562 ✔
372

373
void initializer(AbstractSyntaxTreeBuilderContext& context) {
×
374
    throw std::runtime_error { "initializer is not implemented yet" };
×
375
}
376

377
void initializedDeclarator(AbstractSyntaxTreeBuilderContext& context) {
596 ✔
378
    auto declarator = context.popDeclarator();
596 ✔
379
    context.pushInitializedDeclarator(std::make_unique<InitializedDeclarator>(std::move(declarator)));
596 ✔
380
}
596 ✔
381

382
void initializedDeclaratorWithInitializer(AbstractSyntaxTreeBuilderContext& context) {
66 ✔
383
    auto declarator = context.popDeclarator();
66 ✔
384
    auto initializerExpression = context.popExpression();
66 ✔
385
    context.pushInitializedDeclarator(std::make_unique<InitializedDeclarator>(std::move(declarator), std::move(initializerExpression)));
66 ✔
386
}
66 ✔
387

388
void initializedDeclaratorList(AbstractSyntaxTreeBuilderContext& context) {
540 ✔
389
    std::vector<std::unique_ptr<InitializedDeclarator>> declarators;
540 ✔
390
    declarators.push_back(context.popInitializedDeclarator());
540 ✔
391
    context.pushInitializedDeclarators(std::move(declarators));
540 ✔
392
}
540 ✔
393

394
void addToInitializedDeclaratorList(AbstractSyntaxTreeBuilderContext& context) {
122 ✔
395
    context.popTerminal();
122 ✔
396
    auto initializedDeclarators = context.popInitializedDeclarators();
122 ✔
397
    initializedDeclarators.push_back(context.popInitializedDeclarator());
122 ✔
398
    context.pushInitializedDeclarators(std::move(initializedDeclarators));
122 ✔
399
}
122 ✔
400

401
void initializedDeclaration(AbstractSyntaxTreeBuilderContext& context) {
540 ✔
402
    context.popTerminal();
540 ✔
403
    auto declarationSpecifiers = context.popDeclarationSpecifiers();
540 ✔
404
    auto initializedDeclarators = context.popInitializedDeclarators();
540 ✔
405
    context.pushDeclaration(std::make_unique<Declaration>(declarationSpecifiers, std::move(initializedDeclarators)));
540 ✔
406
}
540 ✔
407

408
void declaration(AbstractSyntaxTreeBuilderContext& context) {
34 ✔
409
    context.popTerminal();
34 ✔
410
    auto declarationSpecifiers = context.popDeclarationSpecifiers();
34 ✔
411
    context.pushDeclaration(std::make_unique<Declaration>(declarationSpecifiers));
34 ✔
412
}
34 ✔
413

414
void declarationList(AbstractSyntaxTreeBuilderContext& context) {
310 ✔
415
    std::vector<std::unique_ptr<Declaration>> declarations;
310 ✔
416
    declarations.push_back(context.popDeclaration());
310 ✔
417
    context.pushDeclarationList(std::move(declarations));
310 ✔
418
}
310 ✔
419

420
void addDeclarationToList(AbstractSyntaxTreeBuilderContext& context) {
112 ✔
421
    auto declarations = context.popDeclarationList();
112 ✔
422
    declarations.push_back(context.popDeclaration());
112 ✔
423
    context.pushDeclarationList(std::move(declarations));
112 ✔
424
}
112 ✔
425

426
void expressionList(AbstractSyntaxTreeBuilderContext& context) {
2 ✔
427
    context.popTerminal();
2 ✔
428
    auto rightHandSide = context.popExpression();
2 ✔
429
    auto leftHandSide = context.popExpression();
2 ✔
430
    context.pushExpression(std::make_unique<ExpressionList>(std::move(leftHandSide), std::move(rightHandSide)));
2 ✔
431
}
2 ✔
432

433
void pointer(AbstractSyntaxTreeBuilderContext& context) {
84 ✔
434
    context.popTerminal();
84 ✔
435
    context.newPointer(Pointer { });
84 ✔
436
}
84 ✔
437

438
void pointerToPointer(AbstractSyntaxTreeBuilderContext& context) {
×
439
    context.popTerminal();
×
440
    context.pointerToPointer(Pointer { });
×
441
}
×
442

443
void qualifiedPointer(AbstractSyntaxTreeBuilderContext& context) {
×
444
    context.popTerminal();
×
445
    context.newPointer(context.popTypeQualifierList());
×
446
}
×
447

448
void qualifiedPointerToPointer(AbstractSyntaxTreeBuilderContext& context) {
×
449
    context.popTerminal();
×
450
    context.pointerToPointer( { context.popTypeQualifierList() });
×
451
}
×
452

453
void ifStatement(AbstractSyntaxTreeBuilderContext& context) {
20 ✔
454
    context.popTerminal();
20 ✔
455
    context.popTerminal();
20 ✔
456
    context.popTerminal();
20 ✔
457
    context.pushStatement(std::make_unique<IfStatement>(context.popExpression(), context.popStatement()));
20 ✔
458
}
20 ✔
459

460
void ifElseStatement(AbstractSyntaxTreeBuilderContext& context) {
16 ✔
461
    context.popTerminal();
16 ✔
462
    context.popTerminal();
16 ✔
463
    context.popTerminal();
16 ✔
464
    context.popTerminal();
16 ✔
465
    auto falsyStatement = context.popStatement();
16 ✔
466
    auto truthyStatement = context.popStatement();
16 ✔
467
    context.pushStatement(std::make_unique<IfElseStatement>(context.popExpression(), std::move(truthyStatement), std::move(falsyStatement)));
16 ✔
468
}
16 ✔
469

470
void whileLoopStatement(AbstractSyntaxTreeBuilderContext& context) {
20 ✔
471
    context.popTerminal();
20 ✔
472
    context.popTerminal();
20 ✔
473
    context.popTerminal();
20 ✔
474
    auto loopHeader = std::make_unique<WhileLoopHeader>(context.popExpression());
20 ✔
475
    auto body = context.popStatement();
20 ✔
476
    context.pushStatement(std::make_unique<LoopStatement>(std::move(loopHeader), std::move(body)));
20 ✔
477
}
20 ✔
478

479
void statementList(AbstractSyntaxTreeBuilderContext& context) {
622 ✔
480
    context.newStatementList(context.popStatement());
622 ✔
481
}
622 ✔
482

483
void addToStatementList(AbstractSyntaxTreeBuilderContext& context) {
1,436 ✔
484
    context.addToStatementList(context.popStatement());
1,436 ✔
485
}
1,436 ✔
486

487
void returnExpressionStatement(AbstractSyntaxTreeBuilderContext& context) {
500 ✔
488
    context.popTerminal();
500 ✔
489
    context.popTerminal();
500 ✔
490
    context.pushStatement(std::make_unique<ReturnStatement>(context.popExpression()));
500 ✔
491
}
500 ✔
492

493
void returnVoidStatement(AbstractSyntaxTreeBuilderContext& context) {
26 ✔
494
    context.popTerminal();
26 ✔
495
    context.popTerminal();
26 ✔
496
    context.pushStatement(std::make_unique<VoidReturnStatement>());
26 ✔
497
}
26 ✔
498

499
void createActualArgumentsList(AbstractSyntaxTreeBuilderContext& context) {
932 ✔
500
    context.newActualArgumentsList(context.popExpression());
932 ✔
501
}
932 ✔
502

503
void addToActualArgumentsList(AbstractSyntaxTreeBuilderContext& context) {
1,148 ✔
504
    context.popTerminal();
1,148 ✔
505
    context.addToActualArgumentsList(context.popExpression());
1,148 ✔
506
}
1,148 ✔
507

508
void declarationCompound(AbstractSyntaxTreeBuilderContext& context) {
×
509
    context.popTerminal();
×
510
    context.popTerminal();
×
511
    context.pushStatement(std::make_unique<Block>(context.popDeclarationList(), std::vector<std::unique_ptr<AbstractSyntaxTreeNode>> { }));
×
512
}
×
513

514
void statementCompound(AbstractSyntaxTreeBuilderContext& context) {
312 ✔
515
    context.popTerminal();
312 ✔
516
    context.popTerminal();
312 ✔
517
    context.pushStatement(std::make_unique<Block>(std::vector<std::unique_ptr<Declaration>> { }, context.popStatementList()));
312 ✔
518
}
312 ✔
519

520
void fullCompound(AbstractSyntaxTreeBuilderContext& context) {
310 ✔
521
    context.popTerminal();
310 ✔
522
    context.popTerminal();
310 ✔
523
    auto declarations = context.popDeclarationList();
310 ✔
524
    auto statements = context.popStatementList();
310 ✔
525
    context.pushStatement(std::make_unique<Block>(std::move(declarations), std::move(statements)));
310 ✔
526
}
310 ✔
527

528
void emptyCompound(AbstractSyntaxTreeBuilderContext& context) {
12 ✔
529
    context.popTerminal();
12 ✔
530
    context.popTerminal();
12 ✔
531
    context.pushStatement(std::make_unique<Block>(
12 ✔
532
                std::vector<std::unique_ptr<Declaration>> { },
12 ✔
533
                std::vector<std::unique_ptr<AbstractSyntaxTreeNode>>{}));
12 ✔
534
}
12 ✔
535

536
void expressionStatement(AbstractSyntaxTreeBuilderContext& context) {
1,436 ✔
537
    context.popTerminal();
1,436 ✔
538
    context.pushStatement(context.popExpression());
1,436 ✔
539
}
1,436 ✔
540

541
void emptyStatement(AbstractSyntaxTreeBuilderContext& context) {
×
542
    context.popTerminal();
×
543
}
×
544

545
void functionDefinition(AbstractSyntaxTreeBuilderContext& context) {
522 ✔
546
    auto declarationSpecifiers = context.popDeclarationSpecifiers();
522 ✔
547
    auto declarator = context.popDeclarator();
522 ✔
548
    auto statement = context.popStatement();
522 ✔
549
    context.pushStatement(std::make_unique<FunctionDefinition>(std::move(declarationSpecifiers), std::move(declarator), std::move(statement)));
522 ✔
550
}
522 ✔
551

552
void defaultReturnTypeFunctionDefinition(AbstractSyntaxTreeBuilderContext& context) {
×
553
    DeclarationSpecifiers defaultReturnTypeSpecifiers { TypeSpecifier { type::signedInteger(), "int" } };
×
554
    context.pushStatement(std::make_unique<FunctionDefinition>(defaultReturnTypeSpecifiers, context.popDeclarator(), context.popStatement()));
×
555
}
×
556

557
void externalFunctionDefinition(AbstractSyntaxTreeBuilderContext& context) {
522 ✔
558
    context.pushExternalDeclaration(context.popStatement());
522 ✔
559
}
522 ✔
560

561
void externalDeclaration(AbstractSyntaxTreeBuilderContext& context) {
152 ✔
562
    context.pushExternalDeclaration(context.popDeclaration());
152 ✔
563
}
152 ✔
564

565
void translationUnit(AbstractSyntaxTreeBuilderContext& context) {
410 ✔
566
    context.addToTranslationUnit(context.popExternalDeclaration());
410 ✔
567
}
410 ✔
568

569
void addToTranslationUnit(AbstractSyntaxTreeBuilderContext& context) {
264 ✔
570
    context.addToTranslationUnit(context.popExternalDeclaration());
264 ✔
571
}
264 ✔
572

573
ContextualSyntaxNodeBuilder::ContextualSyntaxNodeBuilder(const parser::Grammar& grammar) {
412 ✔
574
    this->grammar = &grammar;
412 ✔
575

576
    int s_type_specifier = grammar.symbolId("<type_spec>");
412 ✔
577
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("short") }] = shortType;
1,648 ✔
578
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("int") }] = integerType;
1,648 ✔
579
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("long") }] = longType;
1,648 ✔
580
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("char") }] = characterType;
1,648 ✔
581
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("void") }] = voidType;
1,648 ✔
582
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("float") }] = floatType;
1,648 ✔
583
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("double") }] = doubleType;
1,648 ✔
584
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("signed") }] = signedType;
1,648 ✔
585
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("unsigned") }] = unsignedType;
1,648 ✔
586
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("typedef_name") }] = typedefName;
1,648 ✔
587
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("<struct_or_union_spec>") }] = structOrUnionType;
1,648 ✔
588
    nodeCreatorRegistry[s_type_specifier][{ grammar.symbolId("<enum_spec>") }] = enumType;
2,060 ✔
589

590
    int s_type_qualifier = grammar.symbolId("<type_qualifier>");
412 ✔
591
    nodeCreatorRegistry[s_type_qualifier][{ grammar.symbolId("const") }] = constQualifier;
1,648 ✔
592
    nodeCreatorRegistry[s_type_qualifier][{ grammar.symbolId("volatile") }] = volatileQualifier;
2,060 ✔
593

594
    int s_decl_specs = grammar.symbolId("<decl_specs>");
412 ✔
595
    nodeCreatorRegistry[s_decl_specs][{ s_type_specifier }] = declarationTypeSpecifier;
824 ✔
596
    nodeCreatorRegistry[s_decl_specs][{ s_type_specifier, s_decl_specs }] = addDeclarationTypeSpecifier;
824 ✔
597
    nodeCreatorRegistry[s_decl_specs][{ grammar.symbolId("<storage_class_spec>") }] = declarationStorageClassSpecifier;
1,648 ✔
598
    nodeCreatorRegistry[s_decl_specs][{ grammar.symbolId("<storage_class_spec>"), s_decl_specs }] = addDeclarationStorageClassSpecifier;
1,648 ✔
599
    nodeCreatorRegistry[s_decl_specs][{ s_type_qualifier }] = declarationTypeQualifier;
824 ✔
600
    nodeCreatorRegistry[s_decl_specs][{ s_type_qualifier, s_decl_specs }] = addDeclarationTypeQualifier;
1,236 ✔
601

602
    int s_direct_declarator = grammar.symbolId("<direct_declarator>");
824 ✔
603
    int s_declarator = grammar.symbolId("<declarator>");
824 ✔
604
    int s_param_type_list = grammar.symbolId("<param_type_list>");
824 ✔
605
    int s_identifier = grammar.symbolId("id");
824 ✔
606
    int s_open_paren = grammar.symbolId("(");
824 ✔
607
    int s_close_paren = grammar.symbolId(")");
824 ✔
608
    int s_open_bracket = grammar.symbolId("[");
824 ✔
609
    int s_close_bracket = grammar.symbolId("]");
412 ✔
610

611
    nodeCreatorRegistry[s_direct_declarator][{ s_identifier }] = identifierDeclarator;
824 ✔
612
    nodeCreatorRegistry[s_direct_declarator][{ s_open_paren, s_declarator, s_close_paren }] = parenthesizedExpression;
824 ✔
613
    nodeCreatorRegistry[s_direct_declarator][{ s_direct_declarator, s_open_bracket, grammar.symbolId("<const_exp>"), s_close_bracket }] = arrayDeclarator;
1,648 ✔
614
    nodeCreatorRegistry[s_direct_declarator][{ s_direct_declarator, s_open_bracket, s_close_bracket }] = abstractArrayDeclarator;
824 ✔
615
    nodeCreatorRegistry[s_direct_declarator][{ s_direct_declarator, s_open_paren, s_param_type_list, s_close_paren }] = functionDeclarator;
824 ✔
616
    nodeCreatorRegistry[s_direct_declarator][{ s_direct_declarator, s_open_paren, s_close_paren }] = noargFunctionDeclarator;
1,236 ✔
617

618
    int s_pointer = grammar.symbolId("<pointer>" );
412 ✔
619
    nodeCreatorRegistry[s_declarator][{ s_pointer, s_direct_declarator }] = pointerToDeclarator;
824 ✔
620
    nodeCreatorRegistry[s_declarator][{ s_direct_declarator }] = declarator;
1,236 ✔
621

622
    int s_param_decl = grammar.symbolId("<param_decl>");
412 ✔
623
    nodeCreatorRegistry[s_param_decl][{ s_decl_specs, s_declarator }] = parameterDeclaration;
824 ✔
624
    nodeCreatorRegistry[s_param_decl][{ s_decl_specs, grammar.symbolId("<abstract_declarator>") }] = abstractParameterDeclaration;
1,648 ✔
625
    nodeCreatorRegistry[s_param_decl][{ s_decl_specs }] = parameterBaseTypeDeclaration;
1,236 ✔
626

627
    int s_param_list = grammar.symbolId("<param_list>");
824 ✔
628
    int s_comma = grammar.symbolId(",");
412 ✔
629
    nodeCreatorRegistry[s_param_list][{ s_param_decl }] = formalArguments;
824 ✔
630
    nodeCreatorRegistry[s_param_list][{ s_param_list, s_comma, s_param_decl }] = addFormalArgument;
824 ✔
631

632
    nodeCreatorRegistry[s_param_type_list][{ s_param_list }] = formalArgumentsDeclaration;
824 ✔
633
    nodeCreatorRegistry[s_param_type_list][{ s_param_list, s_comma, grammar.symbolId("...") }] = formalArgumentsWithVararg;
2,060 ✔
634

635
    int s_constant = grammar.symbolId("<const>");
412 ✔
636
    nodeCreatorRegistry[s_constant][{ grammar.symbolId("int_const") }] = integerConstant;
1,648 ✔
637
    nodeCreatorRegistry[s_constant][{ grammar.symbolId("char_const") }] = characterConstant;
1,648 ✔
638
    nodeCreatorRegistry[s_constant][{ grammar.symbolId("float_const") }] = floatConstant;
1,648 ✔
639
    nodeCreatorRegistry[s_constant][{ grammar.symbolId("enumeration_const") }] = enumerationConstant;
2,060 ✔
640

641
    int s_exp = grammar.symbolId("<exp>");
824 ✔
642
    int s_primary_exp = grammar.symbolId("<primary_exp>");
412 ✔
643
    nodeCreatorRegistry[s_primary_exp][{ s_identifier }] = identifierExpression;
824 ✔
644
    nodeCreatorRegistry[s_primary_exp][{ s_constant }] = constantExpression;
824 ✔
645
    nodeCreatorRegistry[s_primary_exp][{ grammar.symbolId("string") }] = stringLiteralExpression;
1,648 ✔
646
    nodeCreatorRegistry[s_primary_exp][{ s_open_paren, s_exp, s_close_paren }] = parenthesizedExpression;
1,236 ✔
647

648
    int s_argument_exp_list = grammar.symbolId("<argument_exp_list>");
824 ✔
649
    int s_postfix_exp = grammar.symbolId("<postfix_exp>");
412 ✔
650
    nodeCreatorRegistry[s_postfix_exp][{ s_primary_exp }] = doNothing;
824 ✔
651
    nodeCreatorRegistry[s_postfix_exp][{ s_postfix_exp, s_open_bracket, s_exp, s_close_bracket }] = arrayAccess;
824 ✔
652
    nodeCreatorRegistry[s_postfix_exp][{ s_postfix_exp, s_open_paren, s_argument_exp_list, s_close_paren }] = functionCall;
824 ✔
653
    nodeCreatorRegistry[s_postfix_exp][{ s_postfix_exp, s_open_paren, s_close_paren }] = noargFunctionCall;
824 ✔
654
    nodeCreatorRegistry[s_postfix_exp][{ s_postfix_exp, grammar.symbolId("."), s_identifier }] = directMemberAccess;
1,648 ✔
655
    nodeCreatorRegistry[s_postfix_exp][{ s_postfix_exp, grammar.symbolId("->"), s_identifier }] = pointeeMemberAccess;
1,648 ✔
656
    nodeCreatorRegistry[s_postfix_exp][{ s_postfix_exp, grammar.symbolId("++") }] = postfixIncrementDecrement;
1,648 ✔
657
    nodeCreatorRegistry[s_postfix_exp][{ s_postfix_exp, grammar.symbolId("--") }] = postfixIncrementDecrement;
2,060 ✔
658

659
    int s_cast_exp = grammar.symbolId("<cast_exp>");
824 ✔
660
    int s_unary_exp = grammar.symbolId("<unary_exp>");
824 ✔
661
    int s_unary_operator = grammar.symbolId("<unary_operator>");
412 ✔
662
    nodeCreatorRegistry[s_unary_exp][{ s_postfix_exp }] = doNothing;
824 ✔
663
    nodeCreatorRegistry[s_unary_exp][{ grammar.symbolId("++"), s_unary_exp }] = prefixIncrementDecrement;
1,648 ✔
664
    nodeCreatorRegistry[s_unary_exp][{ grammar.symbolId("--"), s_unary_exp }] = prefixIncrementDecrement;
1,648 ✔
665
    nodeCreatorRegistry[s_unary_exp][{ s_unary_operator, s_cast_exp }] = unaryExpression;
824 ✔
666
    nodeCreatorRegistry[s_unary_exp][{ grammar.symbolId("sizeof"), s_unary_exp }] = sizeofExpression;
1,648 ✔
667
    nodeCreatorRegistry[s_unary_exp][{ grammar.symbolId("sizeof"), s_open_paren, grammar.symbolId("<type_name>"), s_close_paren }] = sizeofTypeExpression;
2,472 ✔
668

669
    nodeCreatorRegistry[s_cast_exp][{ s_unary_exp }] = doNothing;
824 ✔
670
    nodeCreatorRegistry[s_cast_exp][{ s_open_paren, grammar.symbolId("<type_name>"), s_close_paren, s_cast_exp }] = typeCast;
2,060 ✔
671

672
    int s_mult_exp = grammar.symbolId("<mult_exp>");
412 ✔
673
    nodeCreatorRegistry[s_mult_exp][{ s_cast_exp }] = doNothing;
824 ✔
674
    nodeCreatorRegistry[s_mult_exp][{ s_mult_exp, grammar.symbolId("*"), s_cast_exp }] = arithmeticExpression;
1,648 ✔
675
    nodeCreatorRegistry[s_mult_exp][{ s_mult_exp, grammar.symbolId("/"), s_cast_exp }] = arithmeticExpression;
1,648 ✔
676
    nodeCreatorRegistry[s_mult_exp][{ s_mult_exp, grammar.symbolId("%"), s_cast_exp }] = arithmeticExpression;
2,060 ✔
677

678
    int s_additive_exp = grammar.symbolId("<additive_exp>");
412 ✔
679
    nodeCreatorRegistry[s_additive_exp][{ s_mult_exp }] = doNothing;
824 ✔
680
    nodeCreatorRegistry[s_additive_exp][{ s_additive_exp, grammar.symbolId("+"), s_mult_exp }] = arithmeticExpression;
1,648 ✔
681
    nodeCreatorRegistry[s_additive_exp][{ s_additive_exp, grammar.symbolId("-"), s_mult_exp }] = arithmeticExpression;
2,060 ✔
682

683
    int s_shift_exp = grammar.symbolId("<shift_expression>");
412 ✔
684
    nodeCreatorRegistry[s_shift_exp][{ s_additive_exp }] = doNothing;
824 ✔
685
    nodeCreatorRegistry[s_shift_exp][{ s_shift_exp, grammar.symbolId("<<"), s_additive_exp }] = shiftExpression;
1,648 ✔
686
    nodeCreatorRegistry[s_shift_exp][{ s_shift_exp, grammar.symbolId(">>"), s_additive_exp }] = shiftExpression;
2,060 ✔
687

688
    int s_relational_exp = grammar.symbolId("<relational_exp>");
412 ✔
689
    nodeCreatorRegistry[s_relational_exp][{ s_shift_exp }] = doNothing;
824 ✔
690
    nodeCreatorRegistry[s_relational_exp][{ s_relational_exp, grammar.symbolId("<"), s_shift_exp }] = relationalExpression;
1,648 ✔
691
    nodeCreatorRegistry[s_relational_exp][{ s_relational_exp, grammar.symbolId(">"), s_shift_exp }] = relationalExpression;
1,648 ✔
692
    nodeCreatorRegistry[s_relational_exp][{ s_relational_exp, grammar.symbolId("<="), s_shift_exp }] = relationalExpression;
1,648 ✔
693
    nodeCreatorRegistry[s_relational_exp][{ s_relational_exp, grammar.symbolId(">="), s_shift_exp }] = relationalExpression;
2,060 ✔
694

695
    int s_equality_exp = grammar.symbolId("<equality_exp>");
412 ✔
696
    nodeCreatorRegistry[s_equality_exp][{ s_relational_exp }] = doNothing;
824 ✔
697
    nodeCreatorRegistry[s_equality_exp][{ s_equality_exp, grammar.symbolId("=="), s_relational_exp }] = relationalExpression;
1,648 ✔
698
    nodeCreatorRegistry[s_equality_exp][{ s_equality_exp, grammar.symbolId("!="), s_relational_exp }] = relationalExpression;
2,060 ✔
699

700
    int s_and_exp = grammar.symbolId("<and_exp>");
412 ✔
701
    nodeCreatorRegistry[s_and_exp][{ s_equality_exp }] = doNothing;
824 ✔
702
    nodeCreatorRegistry[s_and_exp][{ s_and_exp, grammar.symbolId("&"), s_equality_exp }] = bitwiseExpression;
2,060 ✔
703

704
    int s_exclusive_or_exp = grammar.symbolId("<exclusive_or_exp>");
412 ✔
705
    nodeCreatorRegistry[s_exclusive_or_exp][{ s_and_exp }] = doNothing;
824 ✔
706
    nodeCreatorRegistry[s_exclusive_or_exp][{ s_exclusive_or_exp, grammar.symbolId("^"), s_and_exp }] = bitwiseExpression;
2,060 ✔
707

708
    int s_inclusive_or_exp = grammar.symbolId("<inclusive_or_exp>");
412 ✔
709
    nodeCreatorRegistry[s_inclusive_or_exp][{ s_exclusive_or_exp }] = doNothing;
824 ✔
710
    nodeCreatorRegistry[s_inclusive_or_exp][{ s_inclusive_or_exp, grammar.symbolId("|"), s_exclusive_or_exp }] = bitwiseExpression;
2,060 ✔
711

712
    int s_logical_and_exp = grammar.symbolId("<logical_and_exp>");
412 ✔
713
    nodeCreatorRegistry[s_logical_and_exp][{ s_inclusive_or_exp }] = doNothing;
824 ✔
714
    nodeCreatorRegistry[s_logical_and_exp][{ s_logical_and_exp, grammar.symbolId("&&"), s_inclusive_or_exp }] = logicalAndExpression;
2,060 ✔
715

716
    int s_logical_or_exp = grammar.symbolId("<logical_or_exp>");
412 ✔
717
    nodeCreatorRegistry[s_logical_or_exp][{ s_logical_and_exp }] = doNothing;
824 ✔
718
    nodeCreatorRegistry[s_logical_or_exp][{ s_logical_or_exp, grammar.symbolId("||"), s_logical_and_exp }] = logicalOrExpression;
2,060 ✔
719

720
    int s_conditional_exp = grammar.symbolId("<conditional_exp>");
412 ✔
721
    nodeCreatorRegistry[s_conditional_exp][{ s_logical_or_exp }] = doNothing;
824 ✔
722
    nodeCreatorRegistry[s_conditional_exp][{ s_logical_or_exp, grammar.symbolId("?"), s_exp, grammar.symbolId(":"), s_conditional_exp }] = conditionalExpression;
2,884 ✔
723

724
    int s_assignment = grammar.symbolId("<assignment_exp>");
824 ✔
725
    int s_assignment_operator = grammar.symbolId("<assignment_operator>");
412 ✔
726
    nodeCreatorRegistry[s_assignment][{ s_conditional_exp }] = doNothing;
824 ✔
727
    nodeCreatorRegistry[s_assignment][{ s_unary_exp, s_assignment_operator, s_assignment }] = assignmentExpression;
1,236 ✔
728

729
    int s_initializer = grammar.symbolId("<initializer>");
824 ✔
730
    int s_open_brace = grammar.symbolId("{");
824 ✔
731
    int s_close_brace = grammar.symbolId("}");
412 ✔
732
    nodeCreatorRegistry[s_initializer][{ s_assignment }] = doNothing;
824 ✔
733
    nodeCreatorRegistry[s_initializer][{ s_open_brace, grammar.symbolId("<initializer_list>"), s_close_brace }] = initializer;
2,060 ✔
734

735
    int s_init_declarator = grammar.symbolId("<init_declarator>");
412 ✔
736
    nodeCreatorRegistry[s_init_declarator][{ s_declarator }] = initializedDeclarator;
824 ✔
737
    nodeCreatorRegistry[s_init_declarator][{ s_declarator, grammar.symbolId("="), s_initializer }] = initializedDeclaratorWithInitializer;
2,060 ✔
738

739
    int s_init_declarator_list = grammar.symbolId("<init_declarator_list>");
412 ✔
740
    nodeCreatorRegistry[s_init_declarator_list][{ s_init_declarator }] = initializedDeclaratorList;
824 ✔
741
    nodeCreatorRegistry[s_init_declarator_list][{ s_init_declarator_list, s_comma, s_init_declarator }] = addToInitializedDeclaratorList;
1,236 ✔
742

743
    int s_decl = grammar.symbolId("<decl>");
824 ✔
744
    int s_semicolon = grammar.symbolId(";");
412 ✔
745
    nodeCreatorRegistry[s_decl][{ s_decl_specs, s_init_declarator_list, s_semicolon }] = initializedDeclaration;
824 ✔
746
    nodeCreatorRegistry[s_decl][{ s_decl_specs, s_semicolon }] = declaration;
1,236 ✔
747

748
    int s_decl_list = grammar.symbolId("<decl_list>");
412 ✔
749
    nodeCreatorRegistry[s_decl_list][{ s_decl }] = declarationList;
824 ✔
750
    nodeCreatorRegistry[s_decl_list][{ s_decl_list, s_decl }] = addDeclarationToList;
824 ✔
751

752
    nodeCreatorRegistry[s_exp][{ s_assignment }] = doNothing;
824 ✔
753
    nodeCreatorRegistry[s_exp][{ s_exp, s_comma, s_assignment }] = expressionList;
1,236 ✔
754

755
    int s_type_qualifier_list = grammar.symbolId("<type_qualifier_list>");
412 ✔
756
    nodeCreatorRegistry[s_type_qualifier_list][{ s_type_qualifier }] = typeQualifierList;
824 ✔
757
    nodeCreatorRegistry[s_type_qualifier_list][{ s_type_qualifier_list, s_type_qualifier }] = addTypeQualifierToList;
824 ✔
758

759
    nodeCreatorRegistry[s_pointer][{ grammar.symbolId("*"), s_type_qualifier_list }] = qualifiedPointer;
1,648 ✔
760
    nodeCreatorRegistry[s_pointer][{ grammar.symbolId("*") }] = pointer;
1,648 ✔
761
    nodeCreatorRegistry[s_pointer][{ grammar.symbolId("*"), s_type_qualifier_list, s_pointer }] = qualifiedPointerToPointer;
1,648 ✔
762
    nodeCreatorRegistry[s_pointer][{ grammar.symbolId("*"), s_pointer }] = pointerToPointer;
1,648 ✔
763

764
    nodeCreatorRegistry[s_unary_operator][{ grammar.symbolId("&") }] = doNothing;
1,648 ✔
765
    nodeCreatorRegistry[s_unary_operator][{ grammar.symbolId("*") }] = doNothing;
1,648 ✔
766
    nodeCreatorRegistry[s_unary_operator][{ grammar.symbolId("+") }] = doNothing;
1,648 ✔
767
    nodeCreatorRegistry[s_unary_operator][{ grammar.symbolId("-") }] = doNothing;
1,648 ✔
768
    nodeCreatorRegistry[s_unary_operator][{ grammar.symbolId("~") }] = doNothing;
1,648 ✔
769
    nodeCreatorRegistry[s_unary_operator][{ grammar.symbolId("!") }] = doNothing;
1,648 ✔
770

771
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("=") }] = doNothing;
1,648 ✔
772
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("*=") }] = doNothing;
1,648 ✔
773
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("/=") }] = doNothing;
1,648 ✔
774
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("%=") }] = doNothing;
1,648 ✔
775
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("+=") }] = doNothing;
1,648 ✔
776
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("-=") }] = doNothing;
1,648 ✔
777
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("<<=") }] = doNothing;
1,648 ✔
778
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId(">>=") }] = doNothing;
1,648 ✔
779
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("&=") }] = doNothing;
1,648 ✔
780
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("^=") }] = doNothing;
1,648 ✔
781
    nodeCreatorRegistry[s_assignment_operator][{ grammar.symbolId("|=") }] = doNothing;
2,060 ✔
782

783
    int s_exp_stat = grammar.symbolId("<exp_stat>");
412 ✔
784
    nodeCreatorRegistry[s_exp_stat][{ s_exp, s_semicolon }] = expressionStatement;
824 ✔
785
    nodeCreatorRegistry[s_exp_stat][{ s_semicolon }] = emptyStatement;
1,236 ✔
786

787
    int s_matched = grammar.symbolId("<matched>");
824 ✔
788
    int s_unmatched = grammar.symbolId("<unmatched>");
824 ✔
789
    int s_stat = grammar.symbolId("<stat>");
824 ✔
790
    int s_compound_stat = grammar.symbolId("<compound_stat>");
824 ✔
791
    int s_jump_stat = grammar.symbolId("<jump_stat>");
824 ✔
792
    int s_if = grammar.symbolId("if");
412 ✔
793
    nodeCreatorRegistry[s_matched][{s_if, s_open_paren, s_exp, s_close_paren, s_matched, grammar.symbolId("else"), s_matched }] = ifElseStatement;
1,648 ✔
794
    nodeCreatorRegistry[s_unmatched][{s_if, s_open_paren, s_exp, s_close_paren, s_stat }] = ifStatement;
824 ✔
795
    //nodeCreatorRegistry[s_matched][{ grammar.symbolId("switch"), s_open_paren, s_exp, s_close_paren, s_matched }] = switchStatement;
796
    //nodeCreatorRegistry[s_matched][{ grammar.symbolId("<labeled_stat_matched>") }] = labeledStatement; // ??
797
    nodeCreatorRegistry[s_matched][{ s_exp_stat }] = doNothing;
824 ✔
798
    nodeCreatorRegistry[s_matched][{ s_compound_stat }] = doNothing;
824 ✔
799
    nodeCreatorRegistry[s_matched][{ s_jump_stat }] = doNothing;
824 ✔
800

801
    nodeCreatorRegistry[s_stat][{ s_matched }] = doNothing;
824 ✔
802
    nodeCreatorRegistry[s_stat][{ s_unmatched }] = doNothing;
1,236 ✔
803

804
    int s_stat_list = grammar.symbolId("<stat_list>");
412 ✔
805
    nodeCreatorRegistry[s_stat_list][{ s_stat }] = statementList;
824 ✔
806
    nodeCreatorRegistry[s_stat_list][{ s_stat_list, s_stat }] = addToStatementList;
1,236 ✔
807

808
    int s_return = grammar.symbolId("return");
412 ✔
809
    //nodeCreatorRegistry[s_jump][{ grammar.symbolId("goto"), s_identifier, s_semicolon }] = gotoStatement;
810
    //nodeCreatorRegistry[s_jump][{ grammar.symbolId("continue"), s_semicolon }] = continueStatement;
811
    //nodeCreatorRegistry[s_jump][{ grammar.symbolId("break"), s_semicolon }] = breakStatement;
812
    nodeCreatorRegistry[s_jump_stat][{ s_return, s_exp, s_semicolon }] = returnExpressionStatement;
824 ✔
813
    nodeCreatorRegistry[s_jump_stat][{ s_return, s_semicolon }] = returnVoidStatement;
1,236 ✔
814

815

816
    int s_struct_or_union = grammar.symbolId("<struct_or_union>");
824 ✔
817
    int s_struct_or_union_spec = grammar.symbolId("<struct_or_union_spec>");
824 ✔
818
    int s_struct_decl_list = grammar.symbolId("<struct_decl_list>");
824 ✔
819
    int s_struct_decl = grammar.symbolId("<struct_decl>");
824 ✔
820
    int s_struct_declarator_list = grammar.symbolId("<struct_declarator_list>");
824 ✔
821
    int s_struct_declarator = grammar.symbolId("<struct_declarator>");
824 ✔
822
    int s_spec_qualifier_list = grammar.symbolId("<spec_qualifier_list>");
412 ✔
823

824
    nodeCreatorRegistry[s_struct_or_union][{ grammar.symbolId("struct") }] = [](AbstractSyntaxTreeBuilderContext& context) { context.popTerminal(); };
1,754 ✔
825
    nodeCreatorRegistry[s_struct_or_union][{ grammar.symbolId("union") }] = [](AbstractSyntaxTreeBuilderContext& context) {
2,060 ✔
NEW
826
        context.popTerminal();
×
NEW
827
        throw std::runtime_error { "union is not implemented" };
×
828
    };
412 ✔
829

830
    nodeCreatorRegistry[s_struct_or_union_spec][{ s_struct_or_union, s_identifier, grammar.symbolId("{"), s_struct_decl_list, grammar.symbolId("}") }] =
2,884 ✔
NEW
831
        [](AbstractSyntaxTreeBuilderContext& context) {
×
832
            context.popTerminal();
34 ✔
833
            context.popTerminal();
34 ✔
834
            auto tag = context.popTerminal();
34 ✔
835
            auto members = context.popStructMemberList();
34 ✔
836
            // Same Type instance as any earlier incomplete references (e.g. self-pointers).
837
            type::Type structType = context.ensureStructTag(tag.value);
34 ✔
838
            type::completeStructure(structType, std::move(members));
34 ✔
839
            context.pushTypeSpecifier(TypeSpecifier { structType, tag.value });
34 ✔
840
        };
446 ✔
841
    nodeCreatorRegistry[s_struct_or_union_spec][{ s_struct_or_union, grammar.symbolId("{"), s_struct_decl_list, grammar.symbolId("}") }] =
2,884 ✔
NEW
842
        [](AbstractSyntaxTreeBuilderContext& context) {
×
NEW
843
            context.popTerminal();
×
NEW
844
            context.popTerminal();
×
NEW
845
            auto members = context.popStructMemberList();
×
NEW
846
            context.pushTypeSpecifier(TypeSpecifier { type::structure(std::move(members)), "" });
×
847
        };
412 ✔
848
    nodeCreatorRegistry[s_struct_or_union_spec][{ s_struct_or_union, s_identifier }] =
1,236 ✔
NEW
849
        [](AbstractSyntaxTreeBuilderContext& context) {
×
850
            auto tag = context.popTerminal();
72 ✔
851
            // Incomplete until a defining `struct Tag { ... }` completes the shared body.
852
            context.pushTypeSpecifier(TypeSpecifier { context.ensureStructTag(tag.value), tag.value });
72 ✔
853
        };
484 ✔
854

855
    nodeCreatorRegistry[s_spec_qualifier_list][{ s_type_specifier }] = doNothing;
824 ✔
856
    nodeCreatorRegistry[s_spec_qualifier_list][{ s_type_specifier, s_spec_qualifier_list }] = doNothing;
824 ✔
857
    nodeCreatorRegistry[s_spec_qualifier_list][{ s_type_qualifier }] = [](AbstractSyntaxTreeBuilderContext& context) { context.popTypeQualifier(); };
824 ✔
858
    nodeCreatorRegistry[s_spec_qualifier_list][{ s_type_qualifier, s_spec_qualifier_list }] = [](AbstractSyntaxTreeBuilderContext& context) { context.popTypeQualifier(); };
824 ✔
859

860
    nodeCreatorRegistry[s_struct_declarator][{ s_declarator }] = [](AbstractSyntaxTreeBuilderContext& context) {
1,236 ✔
861
        context.addStructDeclarator(context.popDeclarator());
70 ✔
862
    };
482 ✔
863
    nodeCreatorRegistry[s_struct_declarator_list][{ s_struct_declarator }] = doNothing;
824 ✔
864
    nodeCreatorRegistry[s_struct_declarator_list][{ s_struct_declarator_list, s_comma, s_struct_declarator }] =
1,236 ✔
865
        [](AbstractSyntaxTreeBuilderContext& context) { context.popTerminal(); };
412 ✔
866

867
    nodeCreatorRegistry[s_struct_decl][{ s_spec_qualifier_list, s_struct_declarator_list, s_semicolon }] =
1,236 ✔
NEW
868
        [](AbstractSyntaxTreeBuilderContext& context) {
×
869
            context.popTerminal();
70 ✔
870
            auto declarators = context.popStructDeclarators();
70 ✔
871
            auto typeSpec = context.popTypeSpecifier();
70 ✔
872
            auto baseType = typeSpec.getType();
70 ✔
873
            for (auto& declarator : declarators) {
140 ✔
874
                context.addStructMember(declarator->getName(), declarator->getFundamentalType(baseType));
70 ✔
875
            }
876
        };
482 ✔
877
    nodeCreatorRegistry[s_struct_decl_list][{ s_struct_decl }] = doNothing;
824 ✔
878
    nodeCreatorRegistry[s_struct_decl_list][{ s_struct_decl_list, s_struct_decl }] = doNothing;
824 ✔
879

880

881
    nodeCreatorRegistry[s_argument_exp_list][{ s_assignment }] = createActualArgumentsList;
824 ✔
882
    nodeCreatorRegistry[s_argument_exp_list][{ s_argument_exp_list, s_comma, s_assignment }] = addToActualArgumentsList;
824 ✔
883

884
    nodeCreatorRegistry[s_compound_stat][{ s_open_brace, s_decl_list, s_stat_list, s_close_brace}] = fullCompound;
824 ✔
885
    nodeCreatorRegistry[s_compound_stat][{ s_open_brace, s_stat_list, s_close_brace}] = statementCompound;
824 ✔
886
    nodeCreatorRegistry[s_compound_stat][{ s_open_brace, s_decl_list, s_close_brace}] = declarationCompound;
824 ✔
887
    nodeCreatorRegistry[s_compound_stat][{ s_open_brace, s_close_brace}] = emptyCompound;
1,236 ✔
888

889
    int s_function_definition = grammar.symbolId("<function_definition>");
412 ✔
890
    nodeCreatorRegistry[s_function_definition][{ s_decl_specs, s_declarator, s_compound_stat }] = functionDefinition;
824 ✔
891
    nodeCreatorRegistry[s_function_definition][{ s_declarator, s_compound_stat }] = defaultReturnTypeFunctionDefinition;
1,236 ✔
892

893
    int s_external_decl = grammar.symbolId("<external_decl>");
412 ✔
894
    nodeCreatorRegistry[s_external_decl][{ s_function_definition }] = externalFunctionDefinition;
824 ✔
895
    nodeCreatorRegistry[s_external_decl][{ s_decl }] = externalDeclaration;
1,236 ✔
896

897
    int s_translation_unit = grammar.symbolId("<translation_unit>");
412 ✔
898
    nodeCreatorRegistry[s_translation_unit][{ s_external_decl }] = translationUnit;
824 ✔
899
    nodeCreatorRegistry[s_translation_unit][{ s_translation_unit, s_external_decl }] = addToTranslationUnit;
1,236 ✔
900

901
    int s_iteration_stat_matched = grammar.symbolId("<iteration_stat_matched>");
824 ✔
902
    int s_iteration_stat_unmatched = grammar.symbolId("<iteration_stat_unmatched>");
412 ✔
903
    nodeCreatorRegistry[s_matched][{ s_iteration_stat_matched }] = doNothing;
824 ✔
904
    nodeCreatorRegistry[s_unmatched][{ s_iteration_stat_unmatched }] = doNothing;
1,236 ✔
905

906
    int s_while = grammar.symbolId("while");
824 ✔
907
    int s_for = grammar.symbolId("for");
412 ✔
908
    nodeCreatorRegistry[s_iteration_stat_matched][{ s_while, s_open_paren, s_exp, s_close_paren, s_matched }] = whileLoopStatement;
824 ✔
909
    nodeCreatorRegistry[s_iteration_stat_unmatched][{ s_while, s_open_paren, s_exp, s_close_paren, s_unmatched }] = whileLoopStatement;
824 ✔
910

911
    auto forLoop = [](bool hasInit, bool hasClause, bool hasIncrement) {
3,296 ✔
912
        return [=](AbstractSyntaxTreeBuilderContext& context) {
30 ✔
913
            for (int i = 0; i < 5; ++i) {
180 ✔
914
                context.popTerminal();  // for ( ; ; ) punctuation
150 ✔
915
            }
916
            auto increment = hasIncrement ? context.popExpression() : nullptr;
30 ✔
917
            auto clause = hasClause ? context.popExpression() : nullptr;
30 ✔
918
            auto initialization = hasInit ? context.popExpression() : nullptr;
30 ✔
919
            auto loopHeader = std::make_unique<ForLoopHeader>(std::move(initialization), std::move(clause), std::move(increment));
30 ✔
920
            auto body = context.popStatement();
30 ✔
921
            context.pushStatement(std::make_unique<LoopStatement>(std::move(loopHeader), std::move(body)));
30 ✔
922
        };
3,326 ✔
923
    };
924
    auto registerFor = [&](const std::vector<int>& prod, std::function<void(AbstractSyntaxTreeBuilderContext&)> creator) {
3,296 ✔
925
        nodeCreatorRegistry[s_iteration_stat_matched][prod] = creator;
3,296 ✔
926
        auto unmatchedProd = prod;
3,296 ✔
927
        unmatchedProd.back() = s_unmatched;
3,296 ✔
928
        nodeCreatorRegistry[s_iteration_stat_unmatched][unmatchedProd] = creator;
3,296 ✔
929
    };
3,296 ✔
930
    registerFor({ s_for, s_open_paren, s_exp, s_semicolon, s_exp, s_semicolon, s_exp, s_close_paren, s_matched }, forLoop(true,  true,  true));
1,236 ✔
931
    registerFor({ s_for, s_open_paren, s_exp, s_semicolon, s_exp, s_semicolon, s_close_paren, s_matched }, forLoop(true,  true,  false));
1,236 ✔
932
    registerFor({ s_for, s_open_paren, s_exp, s_semicolon, s_semicolon, s_exp, s_close_paren, s_matched }, forLoop(true,  false, true));
1,236 ✔
933
    registerFor({ s_for, s_open_paren, s_exp, s_semicolon, s_semicolon, s_close_paren, s_matched }, forLoop(true,  false, false));
1,236 ✔
934
    registerFor({ s_for, s_open_paren, s_semicolon, s_exp, s_semicolon, s_exp, s_close_paren, s_matched }, forLoop(false, true,  true));
1,236 ✔
935
    registerFor({ s_for, s_open_paren, s_semicolon, s_exp, s_semicolon, s_close_paren, s_matched }, forLoop(false, true,  false));
1,236 ✔
936
    registerFor({ s_for, s_open_paren, s_semicolon, s_semicolon, s_exp, s_close_paren, s_matched }, forLoop(false, false, true));
1,236 ✔
937
    registerFor({ s_for, s_open_paren, s_semicolon, s_semicolon, s_close_paren, s_matched }, forLoop(false, false, false));
1,236 ✔
938
}
412 ✔
939

940
ContextualSyntaxNodeBuilder::~ContextualSyntaxNodeBuilder() = default;
412 ✔
941

942
void ContextualSyntaxNodeBuilder::updateContext(const parser::Production& production, AbstractSyntaxTreeBuilderContext& context) const {
102,730 ✔
943
    try {
944
        nodeCreatorRegistry.at(production.getDefiningSymbol()).at(production.producedSequence())(context);
102,730 ✔
945
    } catch (std::out_of_range& exception) {
×
946
        noCreatorDefined(production);
×
947
    }
×
948
}
102,730 ✔
949

950
void ContextualSyntaxNodeBuilder::noCreatorDefined(const parser::Production& production) const {
×
951
    throw std::runtime_error { "no AST creator defined for production `" + grammar->str(production) + "`" };
×
952
}
953

954
void ContextualSyntaxNodeBuilder::loopJumpStatement(AbstractSyntaxTreeBuilderContext& context) {
×
955
    context.pushStatement(std::make_unique<JumpStatement>(context.popTerminal()));
×
956
    context.popTerminal();
×
957
}
×
958

959
} /* namespace ast */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc