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
13 changes: 12 additions & 1 deletion packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function new_ast_rewriter_cc({
`auto ${m.name}Decl = Decl{${specsAttr}Ctx, copy->${m.name}};`
);
emit(
`auto ${m.name}Type = getDeclaratorType(translationUnit(), copy->${m.name}, ${specsAttr}Ctx.getType());`
`auto ${m.name}Type = getDeclaratorType(translationUnit(), copy->${m.name}, ${specsAttr}Ctx.type());`
);

typeAttr = members.find(
Expand Down Expand Up @@ -197,6 +197,17 @@ export function new_ast_rewriter_cc({
} // switch

emit(` }`);

// update the context if needed
switch (m.type) {
case "SpecifierAST":
emit(`${m.name}Ctx.finish();`);
break;

default:
break;
} // switch

emit();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/cxx/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class MemInitializerAST : public AST {
class NestedNameSpecifierAST : public AST {
public:
using AST::AST;
Symbol* symbol = nullptr;
ScopedSymbol* symbol = nullptr;
};

class NewInitializerAST : public AST {
Expand Down
28 changes: 20 additions & 8 deletions src/parser/cxx/ast_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1059,8 +1059,8 @@ auto ASTRewriter::operator()(InitDeclaratorAST* ast, const DeclSpecs& declSpecs)

auto decl = Decl{declSpecs, copy->declarator};

auto type = getDeclaratorType(translationUnit(), copy->declarator,
declSpecs.getType());
auto type =
getDeclaratorType(translationUnit(), copy->declarator, declSpecs.type());

// ### fix scope
if (binder_.scope() && binder_.scope()->isClassScope()) {
Expand Down Expand Up @@ -1152,12 +1152,13 @@ auto ASTRewriter::operator()(TypeIdAST* ast) -> TypeIdAST* {
typeSpecifierList = &(*typeSpecifierList)->next;
typeSpecifierListCtx.accept(value);
}
typeSpecifierListCtx.finish();

copy->declarator = operator()(ast->declarator);

auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
typeSpecifierListCtx.getType());
typeSpecifierListCtx.type());
copy->type = declaratorType;

return copy;
Expand Down Expand Up @@ -1194,8 +1195,10 @@ auto ASTRewriter::operator()(BaseSpecifierAST* ast) -> BaseSpecifierAST* {
copy->nestedNameSpecifier = operator()(ast->nestedNameSpecifier);
copy->templateLoc = ast->templateLoc;
copy->unqualifiedId = operator()(ast->unqualifiedId);
copy->ellipsisLoc = ast->ellipsisLoc;
copy->isTemplateIntroduced = ast->isTemplateIntroduced;
copy->isVirtual = ast->isVirtual;
copy->isVariadic = ast->isVariadic;
copy->accessSpecifier = ast->accessSpecifier;
copy->symbol = ast->symbol;

Expand Down Expand Up @@ -1403,6 +1406,7 @@ auto ASTRewriter::DeclarationVisitor::operator()(SimpleDeclarationAST* ast)
declSpecifierList = &(*declSpecifierList)->next;
declSpecifierListCtx.accept(value);
}
declSpecifierListCtx.finish();

for (auto initDeclaratorList = &copy->initDeclaratorList;
auto node : ListView{ast->initDeclaratorList}) {
Expand Down Expand Up @@ -1614,6 +1618,7 @@ auto ASTRewriter::DeclarationVisitor::operator()(OpaqueEnumDeclarationAST* ast)
typeSpecifierList = &(*typeSpecifierList)->next;
typeSpecifierListCtx.accept(value);
}
typeSpecifierListCtx.finish();

copy->emicolonLoc = ast->emicolonLoc;

Expand All @@ -1639,12 +1644,13 @@ auto ASTRewriter::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
declSpecifierList = &(*declSpecifierList)->next;
declSpecifierListCtx.accept(value);
}
declSpecifierListCtx.finish();

copy->declarator = rewrite(ast->declarator);

auto declaratorDecl = Decl{declSpecifierListCtx, copy->declarator};
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
declSpecifierListCtx.getType());
declSpecifierListCtx.type());
copy->requiresClause = rewrite(ast->requiresClause);
copy->functionBody = rewrite(ast->functionBody);
copy->symbol = ast->symbol;
Expand Down Expand Up @@ -1878,12 +1884,13 @@ auto ASTRewriter::DeclarationVisitor::operator()(ParameterDeclarationAST* ast)
typeSpecifierList = &(*typeSpecifierList)->next;
typeSpecifierListCtx.accept(value);
}
typeSpecifierListCtx.finish();

copy->declarator = rewrite(ast->declarator);

auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
typeSpecifierListCtx.getType());
typeSpecifierListCtx.type());
copy->type = declaratorType;
copy->equalLoc = ast->equalLoc;
copy->expression = rewrite(ast->expression);
Expand Down Expand Up @@ -1931,6 +1938,7 @@ auto ASTRewriter::DeclarationVisitor::operator()(
declSpecifierList = &(*declSpecifierList)->next;
declSpecifierListCtx.accept(value);
}
declSpecifierListCtx.finish();

copy->refQualifierLoc = ast->refQualifierLoc;
copy->lbracketLoc = ast->lbracketLoc;
Expand Down Expand Up @@ -3034,12 +3042,13 @@ auto ASTRewriter::ExpressionVisitor::operator()(NewExpressionAST* ast)
typeSpecifierList = &(*typeSpecifierList)->next;
typeSpecifierListCtx.accept(value);
}
typeSpecifierListCtx.finish();

copy->declarator = rewrite(ast->declarator);

auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
typeSpecifierListCtx.getType());
typeSpecifierListCtx.type());
copy->rparenLoc = ast->rparenLoc;
copy->newInitalizer = rewrite(ast->newInitalizer);

Expand Down Expand Up @@ -3224,12 +3233,13 @@ auto ASTRewriter::ExpressionVisitor::operator()(ConditionExpressionAST* ast)
declSpecifierList = &(*declSpecifierList)->next;
declSpecifierListCtx.accept(value);
}
declSpecifierListCtx.finish();

copy->declarator = rewrite(ast->declarator);

auto declaratorDecl = Decl{declSpecifierListCtx, copy->declarator};
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
declSpecifierListCtx.getType());
declSpecifierListCtx.type());
copy->initializer = rewrite(ast->initializer);
copy->symbol = ast->symbol;

Expand Down Expand Up @@ -3734,6 +3744,7 @@ auto ASTRewriter::SpecifierVisitor::operator()(EnumSpecifierAST* ast)
typeSpecifierList = &(*typeSpecifierList)->next;
typeSpecifierListCtx.accept(value);
}
typeSpecifierListCtx.finish();

copy->lbraceLoc = ast->lbraceLoc;

Expand Down Expand Up @@ -4498,12 +4509,13 @@ auto ASTRewriter::ExceptionDeclarationVisitor::operator()(
typeSpecifierList = &(*typeSpecifierList)->next;
typeSpecifierListCtx.accept(value);
}
typeSpecifierListCtx.finish();

copy->declarator = rewrite(ast->declarator);

auto declaratorDecl = Decl{typeSpecifierListCtx, copy->declarator};
auto declaratorType = getDeclaratorType(translationUnit(), copy->declarator,
typeSpecifierListCtx.getType());
typeSpecifierListCtx.type());

return copy;
}
Expand Down
54 changes: 42 additions & 12 deletions src/parser/cxx/binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ auto Binder::enterBlock(SourceLocation loc) -> BlockSymbol* {
}

void Binder::bind(EnumSpecifierAST* ast, const DeclSpecs& underlyingTypeSpecs) {
const auto underlyingType = underlyingTypeSpecs.getType();
const Type* underlyingType = control()->getIntType();

if (underlyingTypeSpecs.hasTypeOrSizeSpecifier())
underlyingType = underlyingTypeSpecs.type();

const auto location = ast->unqualifiedId
? ast->unqualifiedId->firstSourceLocation()
Expand Down Expand Up @@ -133,12 +136,13 @@ void Binder::bind(EnumSpecifierAST* ast, const DeclSpecs& underlyingTypeSpecs) {
}
}

void Binder::bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs) {
void Binder::bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs,
bool isDeclaration) {
const auto _ = ScopeGuard{this};

auto className = get_name(control(), ast->unqualifiedId);
const auto location = ast->unqualifiedId->firstSourceLocation();

const auto _ = ScopeGuard{this};

if (ast->nestedNameSpecifier) {
auto parent = ast->nestedNameSpecifier->symbol;

Expand Down Expand Up @@ -171,8 +175,8 @@ void Binder::bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs) {

ast->symbol = classSymbol;

declSpecs.type = ast->symbol->type();
declSpecs.setTypeSpecifier(ast);
declSpecs.setType(ast->symbol->type());
}

void Binder::bind(ClassSpecifierAST* ast, DeclSpecs& declSpecs) {
Expand Down Expand Up @@ -250,7 +254,7 @@ void Binder::bind(ClassSpecifierAST* ast, DeclSpecs& declSpecs) {
ast->symbol = classSymbol;

declSpecs.setTypeSpecifier(ast);
declSpecs.type = classSymbol->type();
declSpecs.setType(classSymbol->type());
}

void Binder::complete(ClassSpecifierAST* ast) {
Expand All @@ -266,7 +270,7 @@ void Binder::complete(ClassSpecifierAST* ast) {

void Binder::bind(ParameterDeclarationAST* ast, const Decl& decl,
bool inTemplateParameters) {
ast->type = getDeclaratorType(unit_, ast->declarator, decl.specs.getType());
ast->type = getDeclaratorType(unit_, ast->declarator, decl.specs.type());

if (auto declId = decl.declaratorId; declId && declId->unqualifiedId) {
auto paramName = get_name(control(), declId->unqualifiedId);
Expand Down Expand Up @@ -503,13 +507,13 @@ void Binder::bind(UsingDirectiveAST* ast) {
}

void Binder::bind(TypeIdAST* ast, const Decl& decl) {
ast->type = getDeclaratorType(unit_, ast->declarator, decl.specs.getType());
ast->type = getDeclaratorType(unit_, ast->declarator, decl.specs.type());
}

auto Binder::declareTypedef(DeclaratorAST* declarator, const Decl& decl)
-> TypeAliasSymbol* {
auto name = decl.getName();
auto type = getDeclaratorType(unit_, declarator, decl.specs.getType());
auto type = getDeclaratorType(unit_, declarator, decl.specs.type());
auto symbol = control()->newTypeAliasSymbol(scope(), decl.location());
symbol->setName(name);
symbol->setType(type);
Expand All @@ -520,7 +524,7 @@ auto Binder::declareTypedef(DeclaratorAST* declarator, const Decl& decl)
auto Binder::declareFunction(DeclaratorAST* declarator, const Decl& decl)
-> FunctionSymbol* {
auto name = decl.getName();
auto type = getDeclaratorType(unit_, declarator, decl.specs.getType());
auto type = getDeclaratorType(unit_, declarator, decl.specs.type());

auto parentScope = scope();

Expand Down Expand Up @@ -573,7 +577,7 @@ auto Binder::declareFunction(DeclaratorAST* declarator, const Decl& decl)
auto Binder::declareField(DeclaratorAST* declarator, const Decl& decl)
-> FieldSymbol* {
auto name = decl.getName();
auto type = getDeclaratorType(unit_, declarator, decl.specs.getType());
auto type = getDeclaratorType(unit_, declarator, decl.specs.type());
auto fieldSymbol = control()->newFieldSymbol(scope(), decl.location());
applySpecifiers(fieldSymbol, decl.specs);
fieldSymbol->setName(name);
Expand All @@ -590,7 +594,7 @@ auto Binder::declareVariable(DeclaratorAST* declarator, const Decl& decl)
-> VariableSymbol* {
auto name = decl.getName();
auto symbol = control()->newVariableSymbol(scope(), decl.location());
auto type = getDeclaratorType(unit_, declarator, decl.specs.getType());
auto type = getDeclaratorType(unit_, declarator, decl.specs.type());
applySpecifiers(symbol, decl.specs);
symbol->setName(name);
symbol->setType(type);
Expand Down Expand Up @@ -648,6 +652,32 @@ auto Binder::isConstructor(Symbol* symbol) const -> bool {
return true;
}

auto Binder::resolveNestedNameSpecifier(Symbol* symbol) -> ScopedSymbol* {
if (auto classSymbol = symbol_cast<ClassSymbol>(symbol)) return classSymbol;

if (auto namespaceSymbol = symbol_cast<NamespaceSymbol>(symbol))
return namespaceSymbol;

if (auto enumSymbol = symbol_cast<EnumSymbol>(symbol)) return enumSymbol;

if (auto scopedEnumSymbol = symbol_cast<ScopedEnumSymbol>(symbol))
return scopedEnumSymbol;

if (auto typeAliasSymbol = symbol_cast<TypeAliasSymbol>(symbol)) {
if (auto classType = type_cast<ClassType>(typeAliasSymbol->type()))
return classType->symbol();

if (auto enumType = type_cast<EnumType>(typeAliasSymbol->type()))
return enumType->symbol();

if (auto scopedEnumType =
type_cast<ScopedEnumType>(typeAliasSymbol->type()))
return scopedEnumType->symbol();
}

return nullptr;
}

auto Binder::resolve(NestedNameSpecifierAST* nestedNameSpecifier,
UnqualifiedIdAST* unqualifiedId, bool canInstantiate)
-> Symbol* {
Expand Down
6 changes: 5 additions & 1 deletion src/parser/cxx/binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class Binder {

void bind(EnumSpecifierAST* ast, const DeclSpecs& underlyingTypeSpec);

void bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs);
void bind(ElaboratedTypeSpecifierAST* ast, DeclSpecs& declSpecs,
bool isDeclaration);

void bind(ClassSpecifierAST* ast, DeclSpecs& declSpecs);

Expand Down Expand Up @@ -135,6 +136,9 @@ class Binder {
UnqualifiedIdAST* unqualifiedId,
bool canInstantiate) -> Symbol*;

[[nodiscard]] auto resolveNestedNameSpecifier(Symbol* symbol)
-> ScopedSymbol*;

class ScopeGuard {
public:
Binder* p = nullptr;
Expand Down
Loading