Skip to content

Commit

Permalink
fix: Reimplemented the support for nested name specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi committed Sep 16, 2023
1 parent 3c38cab commit a9f60ed
Show file tree
Hide file tree
Showing 26 changed files with 908 additions and 342 deletions.
119 changes: 95 additions & 24 deletions packages/cxx-frontend/src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export abstract class FunctionBodyAST extends AST {}
export abstract class LambdaCaptureAST extends AST {}
export abstract class MemInitializerAST extends AST {}
export abstract class NameAST extends AST {}
export abstract class NestedNameSpecifierAST extends AST {}
export abstract class NewInitializerAST extends AST {}
export abstract class PtrOperatorAST extends AST {}
export abstract class RequirementAST extends AST {}
Expand Down Expand Up @@ -127,27 +128,6 @@ export class TypeIdAST extends AST {
}
}

export class NestedNameSpecifierAST extends AST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
context: Context,
): Result {
return visitor.visitNestedNameSpecifier(this, context);
}
getScopeToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
}
*getNameList(): Generator<NameAST | undefined> {
for (
let it = cxx.getASTSlot(this.getHandle(), 1);
it;
it = cxx.getListNext(it)
) {
yield AST.from<NameAST>(cxx.getListValue(it), this.parser);
}
}
}

export class UsingDeclaratorAST extends AST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
Expand Down Expand Up @@ -977,6 +957,94 @@ export class NewPlacementAST extends AST {
}
}

export class GlobalNestedNameSpecifierAST extends NestedNameSpecifierAST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
context: Context,
): Result {
return visitor.visitGlobalNestedNameSpecifier(this, context);
}
getScopeToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
}
}

export class SimpleNestedNameSpecifierAST extends NestedNameSpecifierAST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
context: Context,
): Result {
return visitor.visitSimpleNestedNameSpecifier(this, context);
}
getNestedNameSpecifier(): NestedNameSpecifierAST | undefined {
return AST.from<NestedNameSpecifierAST>(
cxx.getASTSlot(this.getHandle(), 0),
this.parser,
);
}
getIdentifierToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser);
}
getIdentifier(): string | undefined {
const slot = cxx.getASTSlot(this.getHandle(), 2);
return cxx.getIdentifierValue(slot);
}
getScopeToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 3), this.parser);
}
}

export class DecltypeNestedNameSpecifierAST extends NestedNameSpecifierAST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
context: Context,
): Result {
return visitor.visitDecltypeNestedNameSpecifier(this, context);
}
getNestedNameSpecifier(): NestedNameSpecifierAST | undefined {
return AST.from<NestedNameSpecifierAST>(
cxx.getASTSlot(this.getHandle(), 0),
this.parser,
);
}
getDecltypeSpecifier(): DecltypeSpecifierAST | undefined {
return AST.from<DecltypeSpecifierAST>(
cxx.getASTSlot(this.getHandle(), 1),
this.parser,
);
}
getScopeToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser);
}
}

export class TemplateNestedNameSpecifierAST extends NestedNameSpecifierAST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
context: Context,
): Result {
return visitor.visitTemplateNestedNameSpecifier(this, context);
}
getNestedNameSpecifier(): NestedNameSpecifierAST | undefined {
return AST.from<NestedNameSpecifierAST>(
cxx.getASTSlot(this.getHandle(), 0),
this.parser,
);
}
getTemplateToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser);
}
getTemplateName(): TemplateNameAST | undefined {
return AST.from<TemplateNameAST>(
cxx.getASTSlot(this.getHandle(), 2),
this.parser,
);
}
getScopeToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 3), this.parser);
}
}

export class ThrowExceptionSpecifierAST extends ExceptionSpecifierAST {
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
Expand Down Expand Up @@ -4206,8 +4274,8 @@ export class DecltypeNameAST extends NameAST {
): Result {
return visitor.visitDecltypeName(this, context);
}
getDecltypeSpecifier(): SpecifierAST | undefined {
return AST.from<SpecifierAST>(
getDecltypeSpecifier(): DecltypeSpecifierAST | undefined {
return AST.from<DecltypeSpecifierAST>(
cxx.getASTSlot(this.getHandle(), 0),
this.parser,
);
Expand Down Expand Up @@ -5293,7 +5361,6 @@ const AST_CONSTRUCTORS: Array<
new (handle: number, kind: ASTKind, parser: TranslationUnitLike) => AST
> = [
TypeIdAST,
NestedNameSpecifierAST,
UsingDeclaratorAST,
HandlerAST,
EnumBaseAST,
Expand Down Expand Up @@ -5325,6 +5392,10 @@ const AST_CONSTRUCTORS: Array<
AttributeUsingPrefixAST,
DesignatorAST,
NewPlacementAST,
GlobalNestedNameSpecifierAST,
SimpleNestedNameSpecifierAST,
DecltypeNestedNameSpecifierAST,
TemplateNestedNameSpecifierAST,
ThrowExceptionSpecifierAST,
NoexceptSpecifierAST,
PackExpansionExpressionAST,
Expand Down
7 changes: 6 additions & 1 deletion packages/cxx-frontend/src/ASTKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
export enum ASTKind {
// AST
TypeId,
NestedNameSpecifier,
UsingDeclarator,
Handler,
EnumBase,
Expand Down Expand Up @@ -54,6 +53,12 @@ export enum ASTKind {
Designator,
NewPlacement,

// NestedNameSpecifierAST
GlobalNestedNameSpecifier,
SimpleNestedNameSpecifier,
DecltypeNestedNameSpecifier,
TemplateNestedNameSpecifier,

// ExceptionSpecifierAST
ThrowExceptionSpecifier,
NoexceptSpecifier,
Expand Down
22 changes: 18 additions & 4 deletions packages/cxx-frontend/src/ASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export abstract class ASTVisitor<Context, Result> {

// AST
abstract visitTypeId(node: ast.TypeIdAST, context: Context): Result;
abstract visitNestedNameSpecifier(
node: ast.NestedNameSpecifierAST,
context: Context,
): Result;
abstract visitUsingDeclarator(
node: ast.UsingDeclaratorAST,
context: Context,
Expand Down Expand Up @@ -124,6 +120,24 @@ export abstract class ASTVisitor<Context, Result> {
context: Context,
): Result;

// NestedNameSpecifierAST
abstract visitGlobalNestedNameSpecifier(
node: ast.GlobalNestedNameSpecifierAST,
context: Context,
): Result;
abstract visitSimpleNestedNameSpecifier(
node: ast.SimpleNestedNameSpecifierAST,
context: Context,
): Result;
abstract visitDecltypeNestedNameSpecifier(
node: ast.DecltypeNestedNameSpecifierAST,
context: Context,
): Result;
abstract visitTemplateNestedNameSpecifier(
node: ast.TemplateNestedNameSpecifierAST,
context: Context,
): Result;

// ExceptionSpecifierAST
abstract visitThrowExceptionSpecifier(
node: ast.ThrowExceptionSpecifierAST,
Expand Down
37 changes: 28 additions & 9 deletions packages/cxx-frontend/src/RecursiveASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
this.accept(node.getDeclarator(), context);
}

visitNestedNameSpecifier(
node: ast.NestedNameSpecifierAST,
context: Context,
): void {
for (const element of node.getNameList()) {
this.accept(element, context);
}
}

visitUsingDeclarator(node: ast.UsingDeclaratorAST, context: Context): void {
this.accept(node.getNestedNameSpecifier(), context);
this.accept(node.getName(), context);
Expand Down Expand Up @@ -255,6 +246,34 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
}
}

visitGlobalNestedNameSpecifier(
node: ast.GlobalNestedNameSpecifierAST,
context: Context,
): void {}

visitSimpleNestedNameSpecifier(
node: ast.SimpleNestedNameSpecifierAST,
context: Context,
): void {
this.accept(node.getNestedNameSpecifier(), context);
}

visitDecltypeNestedNameSpecifier(
node: ast.DecltypeNestedNameSpecifierAST,
context: Context,
): void {
this.accept(node.getNestedNameSpecifier(), context);
this.accept(node.getDecltypeSpecifier(), context);
}

visitTemplateNestedNameSpecifier(
node: ast.TemplateNestedNameSpecifierAST,
context: Context,
): void {
this.accept(node.getNestedNameSpecifier(), context);
this.accept(node.getTemplateName(), context);
}

visitThrowExceptionSpecifier(
node: ast.ThrowExceptionSpecifierAST,
context: Context,
Expand Down
35 changes: 22 additions & 13 deletions src/frontend/cxx/ast_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,6 @@ void ASTPrinter::visit(TypeIdAST* ast) {
accept(ast->declarator, "declarator");
}

void ASTPrinter::visit(NestedNameSpecifierAST* ast) {
fmt::print(out_, "{}\n", "nested-name-specifier");
if (ast->nameList) {
++indent_;
fmt::print(out_, "{:{}}", "", indent_ * 2);
fmt::print(out_, "{}\n", "name-list");
for (auto it = ast->nameList; it; it = it->next) {
accept(it->value);
}
--indent_;
}
}

void ASTPrinter::visit(UsingDeclaratorAST* ast) {
fmt::print(out_, "{}\n", "using-declarator");
accept(ast->nestedNameSpecifier, "nested-name-specifier");
Expand Down Expand Up @@ -457,6 +444,28 @@ void ASTPrinter::visit(NewPlacementAST* ast) {
}
}

void ASTPrinter::visit(GlobalNestedNameSpecifierAST* ast) {
fmt::print(out_, "{}\n", "global-nested-name-specifier");
}

void ASTPrinter::visit(SimpleNestedNameSpecifierAST* ast) {
fmt::print(out_, "{}\n", "simple-nested-name-specifier");
accept(ast->identifier, "identifier");
accept(ast->nestedNameSpecifier, "nested-name-specifier");
}

void ASTPrinter::visit(DecltypeNestedNameSpecifierAST* ast) {
fmt::print(out_, "{}\n", "decltype-nested-name-specifier");
accept(ast->nestedNameSpecifier, "nested-name-specifier");
accept(ast->decltypeSpecifier, "decltype-specifier");
}

void ASTPrinter::visit(TemplateNestedNameSpecifierAST* ast) {
fmt::print(out_, "{}\n", "template-nested-name-specifier");
accept(ast->nestedNameSpecifier, "nested-name-specifier");
accept(ast->templateName, "template-name");
}

void ASTPrinter::visit(ThrowExceptionSpecifierAST* ast) {
fmt::print(out_, "{}\n", "throw-exception-specifier");
}
Expand Down
6 changes: 5 additions & 1 deletion src/frontend/cxx/ast_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class ASTPrinter : ASTVisitor {
void accept(const Identifier* id, std::string_view field = {});

void visit(TypeIdAST* ast) override;
void visit(NestedNameSpecifierAST* ast) override;
void visit(UsingDeclaratorAST* ast) override;
void visit(HandlerAST* ast) override;
void visit(EnumBaseAST* ast) override;
Expand Down Expand Up @@ -76,6 +75,11 @@ class ASTPrinter : ASTVisitor {
void visit(DesignatorAST* ast) override;
void visit(NewPlacementAST* ast) override;

void visit(GlobalNestedNameSpecifierAST* ast) override;
void visit(SimpleNestedNameSpecifierAST* ast) override;
void visit(DecltypeNestedNameSpecifierAST* ast) override;
void visit(TemplateNestedNameSpecifierAST* ast) override;

void visit(ThrowExceptionSpecifierAST* ast) override;
void visit(NoexceptSpecifierAST* ast) override;

Expand Down
Loading

0 comments on commit a9f60ed

Please sign in to comment.