Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions packages/cxx-frontend/src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6820,20 +6820,51 @@ export class BuiltinOffsetofExpressionAST extends ExpressionAST {
}

/**
* Returns the expression of this node
* Returns the location of the identifier token in this node
*/
getExpression(): ExpressionAST | undefined {
return AST.from<ExpressionAST>(
cxx.getASTSlot(this.getHandle(), 4),
this.parser,
);
getIdentifierToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 4), this.parser);
}

/**
* Returns the designatorList of this node
*/
getDesignatorList(): Iterable<DesignatorAST | undefined> {
let it = cxx.getASTSlot(this.getHandle(), 0);
let value: DesignatorAST | undefined;
let done = false;
const p = this.parser;
function advance() {
done = it === 0;
if (done) return;
const ast = cxx.getListValue(it);
value = AST.from<DesignatorAST>(ast, p);
it = cxx.getListNext(it);
}
function next() {
advance();
return { done, value };
}
return {
[Symbol.iterator]() {
return { next };
},
};
}

/**
* Returns the location of the rparen token in this node
*/
getRparenToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 5), this.parser);
return Token.from(cxx.getASTSlot(this.getHandle(), 6), this.parser);
}

/**
* Returns the identifier attribute of this node
*/
getIdentifier(): string | undefined {
const slot = cxx.getASTSlot(this.getHandle(), 7);
return cxx.getIdentifierValue(slot);
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/cxx-frontend/src/RecursiveASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,9 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
context: Context,
): void {
this.accept(node.getTypeId(), context);
this.accept(node.getExpression(), context);
for (const element of node.getDesignatorList()) {
this.accept(element, context);
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/parser/cxx/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1710,14 +1710,16 @@ auto BuiltinOffsetofExpressionAST::firstSourceLocation() -> SourceLocation {
if (auto loc = cxx::firstSourceLocation(lparenLoc)) return loc;
if (auto loc = cxx::firstSourceLocation(typeId)) return loc;
if (auto loc = cxx::firstSourceLocation(commaLoc)) return loc;
if (auto loc = cxx::firstSourceLocation(expression)) return loc;
if (auto loc = cxx::firstSourceLocation(identifierLoc)) return loc;
if (auto loc = cxx::firstSourceLocation(designatorList)) return loc;
if (auto loc = cxx::firstSourceLocation(rparenLoc)) return loc;
return {};
}

auto BuiltinOffsetofExpressionAST::lastSourceLocation() -> SourceLocation {
if (auto loc = cxx::lastSourceLocation(rparenLoc)) return loc;
if (auto loc = cxx::lastSourceLocation(expression)) return loc;
if (auto loc = cxx::lastSourceLocation(designatorList)) return loc;
if (auto loc = cxx::lastSourceLocation(identifierLoc)) return loc;
if (auto loc = cxx::lastSourceLocation(commaLoc)) return loc;
if (auto loc = cxx::lastSourceLocation(typeId)) return loc;
if (auto loc = cxx::lastSourceLocation(lparenLoc)) return loc;
Expand Down
4 changes: 3 additions & 1 deletion src/parser/cxx/ast.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,10 +1086,12 @@ table BuiltinBitCastExpression /* ExpressionAST */ {

table BuiltinOffsetofExpression /* ExpressionAST */ {
type_id: TypeId;
expression: Expression;
designator_list: [Designator];
identifier: string;
offsetof_loc: uint32;
lparen_loc: uint32;
comma_loc: uint32;
identifier_loc: uint32;
rparen_loc: uint32;
}

Expand Down
4 changes: 3 additions & 1 deletion src/parser/cxx/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -2135,8 +2135,10 @@ class BuiltinOffsetofExpressionAST final : public ExpressionAST {
SourceLocation lparenLoc;
TypeIdAST* typeId = nullptr;
SourceLocation commaLoc;
ExpressionAST* expression = nullptr;
SourceLocation identifierLoc;
List<DesignatorAST*>* designatorList = nullptr;
SourceLocation rparenLoc;
const Identifier* identifier = nullptr;
FieldSymbol* symbol = nullptr;

void accept(ASTVisitor* visitor) override { visitor->visit(this); }
Expand Down
1 change: 0 additions & 1 deletion src/parser/cxx/ast_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,6 @@ auto ASTInterpreter::ExpressionVisitor::operator()(
auto ASTInterpreter::ExpressionVisitor::operator()(
BuiltinOffsetofExpressionAST* ast) -> ExpressionResult {
auto typeIdResult = accept(ast->typeId);
auto expressionResult = accept(ast->expression);

if (ast->symbol) return ast->symbol->offset();

Expand Down
9 changes: 8 additions & 1 deletion src/parser/cxx/ast_pretty_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2751,7 +2751,14 @@ void ASTPrettyPrinter::ExpressionVisitor::operator()(
nospace();
accept.writeToken(ast->commaLoc);
}
accept(ast->expression);
if (ast->identifierLoc) {
accept.writeToken(ast->identifierLoc);
}

for (auto it = ast->designatorList; it; it = it->next) {
accept(it->value);
}

if (ast->rparenLoc) {
nospace();
accept.writeToken(ast->rparenLoc);
Expand Down
11 changes: 10 additions & 1 deletion src/parser/cxx/ast_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1493,8 +1493,17 @@ void ASTPrinter::visit(BuiltinOffsetofExpressionAST* ast) {
to_string(ast->type));
}
out_ << "\n";
accept(ast->identifier, "identifier");
accept(ast->typeId, "type-id");
accept(ast->expression, "expression");
if (ast->designatorList) {
++indent_;
out_ << std::format("{:{}}", "", indent_ * 2);
out_ << std::format("{}\n", "designator-list");
for (auto node : ListView{ast->designatorList}) {
accept(node);
}
--indent_;
}
}

void ASTPrinter::visit(TypeidExpressionAST* ast) {
Expand Down
11 changes: 10 additions & 1 deletion src/parser/cxx/ast_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2922,8 +2922,17 @@ auto ASTRewriter::ExpressionVisitor::operator()(
copy->lparenLoc = ast->lparenLoc;
copy->typeId = rewrite(ast->typeId);
copy->commaLoc = ast->commaLoc;
copy->expression = rewrite(ast->expression);
copy->identifierLoc = ast->identifierLoc;

for (auto designatorList = &copy->designatorList;
auto node : ListView{ast->designatorList}) {
auto value = rewrite(node);
*designatorList = make_list_node(arena(), value);
designatorList = &(*designatorList)->next;
}

copy->rparenLoc = ast->rparenLoc;
copy->identifier = ast->identifier;
copy->symbol = ast->symbol;

return copy;
Expand Down
22 changes: 16 additions & 6 deletions src/parser/cxx/ast_slot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3405,19 +3405,29 @@ void ASTSlot::visit(BuiltinOffsetofExpressionAST* ast) {
slotKind_ = ASTSlotKind::kToken;
slotNameIndex_ = SlotNameIndex{38};
break;
case 4: // expression
value_ = reinterpret_cast<std::intptr_t>(ast->expression);
slotKind_ = ASTSlotKind::kNode;
slotNameIndex_ = SlotNameIndex{79};
case 4: // identifierLoc
value_ = ast->identifierLoc.index();
slotKind_ = ASTSlotKind::kToken;
slotNameIndex_ = SlotNameIndex{102};
break;
case 5: // rparenLoc
case 5: // designatorList
value_ = reinterpret_cast<std::intptr_t>(ast->designatorList);
slotKind_ = ASTSlotKind::kNodeList;
slotNameIndex_ = SlotNameIndex{62};
break;
case 6: // rparenLoc
value_ = ast->rparenLoc.index();
slotKind_ = ASTSlotKind::kToken;
slotNameIndex_ = SlotNameIndex{185};
break;
case 7: // identifier
value_ = reinterpret_cast<std::intptr_t>(ast->identifier);
slotKind_ = ASTSlotKind::kIdentifierAttribute;
slotNameIndex_ = SlotNameIndex{101};
break;
} // switch

slotCount_ = 6;
slotCount_ = 8;
}

void ASTSlot::visit(TypeidExpressionAST* ast) {
Expand Down
4 changes: 3 additions & 1 deletion src/parser/cxx/ast_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,9 @@ void ASTVisitor::visit(BuiltinBitCastExpressionAST* ast) {

void ASTVisitor::visit(BuiltinOffsetofExpressionAST* ast) {
accept(ast->typeId);
accept(ast->expression);
for (auto node : ListView{ast->designatorList}) {
accept(node);
}
}

void ASTVisitor::visit(TypeidExpressionAST* ast) { accept(ast->expression); }
Expand Down
44 changes: 44 additions & 0 deletions src/parser/cxx/binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,28 @@ auto Binder::declareTypeAlias(SourceLocation identifierLoc, TypeIdAST* typeId)
if (typeId) symbol->setType(typeId->type);
symbol->setTemplateParameters(currentTemplateParameters());
declaringScope()->addSymbol(symbol);

if (auto classType = type_cast<ClassType>(symbol->type())) {
auto classSymbol = classType->symbol();
if (!classSymbol->name()) {
classSymbol->setName(symbol->name());
}
}

if (auto enumType = type_cast<EnumType>(symbol->type())) {
auto enumSymbol = enumType->symbol();
if (!enumSymbol->name()) {
enumSymbol->setName(symbol->name());
}
}

if (auto scopedEnumType = type_cast<ScopedEnumType>(symbol->type())) {
auto scopedEnumSymbol = scopedEnumType->symbol();
if (!scopedEnumSymbol->name()) {
scopedEnumSymbol->setName(symbol->name());
}
}

return symbol;
}

Expand Down Expand Up @@ -634,6 +656,28 @@ auto Binder::declareTypedef(DeclaratorAST* declarator, const Decl& decl)
symbol->setName(name);
symbol->setType(type);
scope()->addSymbol(symbol);

if (auto classType = type_cast<ClassType>(symbol->type())) {
auto classSymbol = classType->symbol();
if (!classSymbol->name()) {
classSymbol->setName(symbol->name());
}
}

if (auto enumType = type_cast<EnumType>(symbol->type())) {
auto enumSymbol = enumType->symbol();
if (!enumSymbol->name()) {
enumSymbol->setName(symbol->name());
}
}

if (auto scopedEnumType = type_cast<ScopedEnumType>(symbol->type())) {
auto scopedEnumSymbol = scopedEnumType->symbol();
if (!scopedEnumSymbol->name()) {
scopedEnumSymbol->setName(symbol->name());
}
}

return symbol;
}

Expand Down
16 changes: 14 additions & 2 deletions src/parser/cxx/flatbuffers/ast_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2717,9 +2717,21 @@ auto ASTDecoder::decodeBuiltinOffsetofExpression(
ast->lparenLoc = SourceLocation(node->lparen_loc());
ast->typeId = decodeTypeId(node->type_id());
ast->commaLoc = SourceLocation(node->comma_loc());
ast->expression =
decodeExpression(node->expression(), node->expression_type());
ast->identifierLoc = SourceLocation(node->identifier_loc());
if (node->designator_list()) {
auto* inserter = &ast->designatorList;
for (std::uint32_t i = 0; i < node->designator_list()->size(); ++i) {
*inserter = new (pool_) List(decodeDesignator(
node->designator_list()->Get(i),
io::Designator(node->designator_list_type()->Get(i))));
inserter = &(*inserter)->next;
}
}
ast->rparenLoc = SourceLocation(node->rparen_loc());
if (node->identifier()) {
ast->identifier =
unit_->control()->getIdentifier(node->identifier()->str());
}
return ast;
}

Expand Down
31 changes: 28 additions & 3 deletions src/parser/cxx/flatbuffers/ast_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2955,16 +2955,41 @@ void ASTEncoder::visit(BuiltinBitCastExpressionAST* ast) {
void ASTEncoder::visit(BuiltinOffsetofExpressionAST* ast) {
const auto typeId = accept(ast->typeId);

const auto [expression, expressionType] = acceptExpression(ast->expression);
std::vector<flatbuffers::Offset<>> designatorListOffsets;
std::vector<std::underlying_type_t<io::Designator>> designatorListTypes;

for (auto node : ListView{ast->designatorList}) {
if (!node) continue;
const auto [offset, type] = acceptDesignator(node);
designatorListOffsets.push_back(offset);
designatorListTypes.push_back(type);
}

auto designatorListOffsetsVector = fbb_.CreateVector(designatorListOffsets);
auto designatorListTypesVector = fbb_.CreateVector(designatorListTypes);

flatbuffers::Offset<flatbuffers::String> identifier;
if (ast->identifier) {
if (identifiers_.contains(ast->identifier)) {
identifier = identifiers_.at(ast->identifier);
} else {
identifier = fbb_.CreateString(ast->identifier->value());
identifiers_.emplace(ast->identifier, identifier);
}
}

io::BuiltinOffsetofExpression::Builder builder{fbb_};
builder.add_offsetof_loc(ast->offsetofLoc.index());
builder.add_lparen_loc(ast->lparenLoc.index());
builder.add_type_id(typeId.o);
builder.add_comma_loc(ast->commaLoc.index());
builder.add_expression(expression);
builder.add_expression_type(static_cast<io::Expression>(expressionType));
builder.add_identifier_loc(ast->identifierLoc.index());
builder.add_designator_list(designatorListOffsetsVector);
builder.add_designator_list_type(designatorListTypesVector);
builder.add_rparen_loc(ast->rparenLoc.index());
if (ast->identifier) {
builder.add_identifier(identifier);
}

offset_ = builder.Finish().Union();
type_ = io::Expression_BuiltinOffsetofExpression;
Expand Down
Loading
Loading