Skip to content

Commit d06f944

Browse files
committed
chore: Add support for __builtin_offsetof and compound literals
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 50da036 commit d06f944

33 files changed

+699
-390
lines changed

packages/cxx-frontend/src/AST.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5153,6 +5153,72 @@ export class BuiltinBitCastExpressionAST extends ExpressionAST {
51535153
}
51545154
}
51555155

5156+
/**
5157+
* BuiltinOffsetofExpressionAST node.
5158+
*/
5159+
export class BuiltinOffsetofExpressionAST extends ExpressionAST {
5160+
/**
5161+
* Traverse this node using the given visitor.
5162+
* @param visitor the visitor.
5163+
* @param context the context.
5164+
* @returns the result of the visit.
5165+
*/
5166+
accept<Context, Result>(
5167+
visitor: ASTVisitor<Context, Result>,
5168+
context: Context,
5169+
): Result {
5170+
return visitor.visitBuiltinOffsetofExpression(this, context);
5171+
}
5172+
5173+
/**
5174+
* Returns the location of the offsetof token in this node
5175+
*/
5176+
getOffsetofToken(): Token | undefined {
5177+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
5178+
}
5179+
5180+
/**
5181+
* Returns the location of the lparen token in this node
5182+
*/
5183+
getLparenToken(): Token | undefined {
5184+
return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser);
5185+
}
5186+
5187+
/**
5188+
* Returns the typeId of this node
5189+
*/
5190+
getTypeId(): TypeIdAST | undefined {
5191+
return AST.from<TypeIdAST>(
5192+
cxx.getASTSlot(this.getHandle(), 2),
5193+
this.parser,
5194+
);
5195+
}
5196+
5197+
/**
5198+
* Returns the location of the comma token in this node
5199+
*/
5200+
getCommaToken(): Token | undefined {
5201+
return Token.from(cxx.getASTSlot(this.getHandle(), 3), this.parser);
5202+
}
5203+
5204+
/**
5205+
* Returns the expression of this node
5206+
*/
5207+
getExpression(): ExpressionAST | undefined {
5208+
return AST.from<ExpressionAST>(
5209+
cxx.getASTSlot(this.getHandle(), 4),
5210+
this.parser,
5211+
);
5212+
}
5213+
5214+
/**
5215+
* Returns the location of the rparen token in this node
5216+
*/
5217+
getRparenToken(): Token | undefined {
5218+
return Token.from(cxx.getASTSlot(this.getHandle(), 5), this.parser);
5219+
}
5220+
}
5221+
51565222
/**
51575223
* TypeidExpressionAST node.
51585224
*/
@@ -12703,6 +12769,7 @@ const AST_CONSTRUCTORS: Array<
1270312769
PostIncrExpressionAST,
1270412770
CppCastExpressionAST,
1270512771
BuiltinBitCastExpressionAST,
12772+
BuiltinOffsetofExpressionAST,
1270612773
TypeidExpressionAST,
1270712774
TypeidOfTypeExpressionAST,
1270812775
SpliceExpressionAST,

packages/cxx-frontend/src/ASTKind.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export enum ASTKind {
102102
PostIncrExpression,
103103
CppCastExpression,
104104
BuiltinBitCastExpression,
105+
BuiltinOffsetofExpression,
105106
TypeidExpression,
106107
TypeidOfTypeExpression,
107108
SpliceExpression,

packages/cxx-frontend/src/ASTSlot.ts

Lines changed: 81 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -165,84 +165,85 @@ export enum ASTSlot {
165165
newLoc = 143,
166166
newPlacement = 144,
167167
noexceptLoc = 145,
168-
op = 146,
169-
opLoc = 147,
170-
openLoc = 148,
171-
operatorFunctionId = 149,
172-
operatorLoc = 150,
173-
outputOperandList = 151,
174-
parameterDeclarationClause = 152,
175-
parameterDeclarationList = 153,
176-
privateLoc = 154,
177-
privateModuleFragment = 155,
178-
ptrOpList = 156,
179-
qualifier = 157,
180-
qualifierLoc = 158,
181-
questionLoc = 159,
182-
rangeDeclaration = 160,
183-
rangeInitializer = 161,
184-
rbraceLoc = 162,
185-
rbracket2Loc = 163,
186-
rbracketLoc = 164,
187-
refLoc = 165,
188-
refOp = 166,
189-
refQualifierLoc = 167,
190-
requirementList = 168,
191-
requiresClause = 169,
192-
requiresLoc = 170,
193-
restrictLoc = 171,
194-
returnLoc = 172,
195-
rightExpression = 173,
196-
rparen2Loc = 174,
197-
rparenLoc = 175,
198-
scopeLoc = 176,
199-
secondColonLoc = 177,
200-
semicolonLoc = 178,
201-
sizeExpression = 179,
202-
sizeofLoc = 180,
203-
specifier = 181,
204-
specifierLoc = 182,
205-
splicer = 183,
206-
starLoc = 184,
207-
statement = 185,
208-
statementList = 186,
209-
staticAssertLoc = 187,
210-
staticLoc = 188,
211-
stringLiteral = 189,
212-
stringliteralLoc = 190,
213-
switchLoc = 191,
214-
symbolicName = 192,
215-
symbolicNameLoc = 193,
216-
templateArgumentList = 194,
217-
templateId = 195,
218-
templateLoc = 196,
219-
templateParameterList = 197,
220-
templateRequiresClause = 198,
221-
thisLoc = 199,
222-
threadLoc = 200,
223-
threadLocalLoc = 201,
224-
throwLoc = 202,
225-
tildeLoc = 203,
226-
trailingReturnType = 204,
227-
tryLoc = 205,
228-
typeConstraint = 206,
229-
typeId = 207,
230-
typeIdList = 208,
231-
typeLoc = 209,
232-
typeSpecifier = 210,
233-
typeSpecifierList = 211,
234-
typeTraitLoc = 212,
235-
typedefLoc = 213,
236-
typeidLoc = 214,
237-
typenameLoc = 215,
238-
underlyingTypeLoc = 216,
239-
unqualifiedId = 217,
240-
usingDeclaratorList = 218,
241-
usingLoc = 219,
242-
vaArgLoc = 220,
243-
virtualLoc = 221,
244-
voidLoc = 222,
245-
volatileLoc = 223,
246-
whileLoc = 224,
247-
yieldLoc = 225,
168+
offsetofLoc = 146,
169+
op = 147,
170+
opLoc = 148,
171+
openLoc = 149,
172+
operatorFunctionId = 150,
173+
operatorLoc = 151,
174+
outputOperandList = 152,
175+
parameterDeclarationClause = 153,
176+
parameterDeclarationList = 154,
177+
privateLoc = 155,
178+
privateModuleFragment = 156,
179+
ptrOpList = 157,
180+
qualifier = 158,
181+
qualifierLoc = 159,
182+
questionLoc = 160,
183+
rangeDeclaration = 161,
184+
rangeInitializer = 162,
185+
rbraceLoc = 163,
186+
rbracket2Loc = 164,
187+
rbracketLoc = 165,
188+
refLoc = 166,
189+
refOp = 167,
190+
refQualifierLoc = 168,
191+
requirementList = 169,
192+
requiresClause = 170,
193+
requiresLoc = 171,
194+
restrictLoc = 172,
195+
returnLoc = 173,
196+
rightExpression = 174,
197+
rparen2Loc = 175,
198+
rparenLoc = 176,
199+
scopeLoc = 177,
200+
secondColonLoc = 178,
201+
semicolonLoc = 179,
202+
sizeExpression = 180,
203+
sizeofLoc = 181,
204+
specifier = 182,
205+
specifierLoc = 183,
206+
splicer = 184,
207+
starLoc = 185,
208+
statement = 186,
209+
statementList = 187,
210+
staticAssertLoc = 188,
211+
staticLoc = 189,
212+
stringLiteral = 190,
213+
stringliteralLoc = 191,
214+
switchLoc = 192,
215+
symbolicName = 193,
216+
symbolicNameLoc = 194,
217+
templateArgumentList = 195,
218+
templateId = 196,
219+
templateLoc = 197,
220+
templateParameterList = 198,
221+
templateRequiresClause = 199,
222+
thisLoc = 200,
223+
threadLoc = 201,
224+
threadLocalLoc = 202,
225+
throwLoc = 203,
226+
tildeLoc = 204,
227+
trailingReturnType = 205,
228+
tryLoc = 206,
229+
typeConstraint = 207,
230+
typeId = 208,
231+
typeIdList = 209,
232+
typeLoc = 210,
233+
typeSpecifier = 211,
234+
typeSpecifierList = 212,
235+
typeTraitLoc = 213,
236+
typedefLoc = 214,
237+
typeidLoc = 215,
238+
typenameLoc = 216,
239+
underlyingTypeLoc = 217,
240+
unqualifiedId = 218,
241+
usingDeclaratorList = 219,
242+
usingLoc = 220,
243+
vaArgLoc = 221,
244+
virtualLoc = 222,
245+
voidLoc = 223,
246+
volatileLoc = 224,
247+
whileLoc = 225,
248+
yieldLoc = 226,
248249
}

packages/cxx-frontend/src/ASTVisitor.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,18 @@ export abstract class ASTVisitor<Context, Result> {
925925
context: Context,
926926
): Result;
927927

928+
/**
929+
* Visit BuiltinOffsetofExpression node.
930+
*
931+
* @param node The node to visit.
932+
* @param context The context.
933+
* @returns The result of the visit.
934+
*/
935+
abstract visitBuiltinOffsetofExpression(
936+
node: ast.BuiltinOffsetofExpressionAST,
937+
context: Context,
938+
): Result;
939+
928940
/**
929941
* Visit TypeidExpression node.
930942
*

packages/cxx-frontend/src/RecursiveASTVisitor.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,20 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
10661066
this.accept(node.getExpression(), context);
10671067
}
10681068

1069+
/**
1070+
* Visit a BuiltinOffsetofExpression node.
1071+
*
1072+
* @param node The node to visit.
1073+
* @param context The context.
1074+
*/
1075+
visitBuiltinOffsetofExpression(
1076+
node: ast.BuiltinOffsetofExpressionAST,
1077+
context: Context,
1078+
): void {
1079+
this.accept(node.getTypeId(), context);
1080+
this.accept(node.getExpression(), context);
1081+
}
1082+
10691083
/**
10701084
* Visit a TypeidExpression node.
10711085
*

packages/cxx-frontend/src/TokenKind.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ export enum TokenKind {
172172
WHILE,
173173
__ATTRIBUTE__,
174174
__BUILTIN_BIT_CAST,
175+
__BUILTIN_OFFSETOF,
175176
__BUILTIN_VA_ARG,
176177
__BUILTIN_VA_LIST,
177178
__COMPLEX__,

packages/cxx-gen-ast/src/tokens.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export const KEYWORDS: string[] = [
178178
"while",
179179
"__attribute__",
180180
"__builtin_bit_cast",
181+
"__builtin_offsetof",
181182
"__builtin_va_arg",
182183
"__builtin_va_list",
183184
"__complex__",

src/frontend/cxx/ast_printer.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,12 @@ void ASTPrinter::visit(BuiltinBitCastExpressionAST* ast) {
10251025
accept(ast->expression, "expression");
10261026
}
10271027

1028+
void ASTPrinter::visit(BuiltinOffsetofExpressionAST* ast) {
1029+
out_ << std::format("{}\n", "builtin-offsetof-expression");
1030+
accept(ast->typeId, "type-id");
1031+
accept(ast->expression, "expression");
1032+
}
1033+
10281034
void ASTPrinter::visit(TypeidExpressionAST* ast) {
10291035
out_ << std::format("{}\n", "typeid-expression");
10301036
accept(ast->expression, "expression");

src/frontend/cxx/ast_printer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class ASTPrinter : ASTVisitor {
121121
void visit(PostIncrExpressionAST* ast) override;
122122
void visit(CppCastExpressionAST* ast) override;
123123
void visit(BuiltinBitCastExpressionAST* ast) override;
124+
void visit(BuiltinOffsetofExpressionAST* ast) override;
124125
void visit(TypeidExpressionAST* ast) override;
125126
void visit(TypeidOfTypeExpressionAST* ast) override;
126127
void visit(SpliceExpressionAST* ast) override;

src/lib/cxx/include/stddef.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ typedef int wint_t;
5252
#endif
5353

5454
#endif /* __need_wint_t */
55+
56+
#if !defined(offsetof)
57+
#define offsetof(t, d) __builtin_offsetof(t, d)
58+
#endif

0 commit comments

Comments
 (0)