Skip to content

Commit

Permalink
fix: Add AST for the C++ attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi authored Jun 26, 2023
1 parent 58f7719 commit aedc6a7
Show file tree
Hide file tree
Showing 38 changed files with 2,017 additions and 378 deletions.
257 changes: 208 additions & 49 deletions packages/cxx-frontend/src/AST.ts

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions packages/cxx-frontend/src/ASTKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export enum ASTKind {
ModuleName,
ImportName,
ModulePartition,
AttributeArgumentClause,
Attribute,
AttributeUsingPrefix,

// RequirementAST
SimpleRequirement,
Expand Down Expand Up @@ -238,4 +241,14 @@ export enum ASTKind {
// DeclaratorModifierAST
FunctionDeclarator,
ArrayDeclarator,

// AttributeSpecifierAST
CxxAttribute,
GCCAttribute,
AlignasAttribute,
AsmAttribute,

// AttributeTokenAST
ScopedAttributeToken,
SimpleAttributeToken,
}
13 changes: 13 additions & 0 deletions packages/cxx-frontend/src/ASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export abstract class ASTVisitor<Context, Result> {
abstract visitModuleName(node: ast.ModuleNameAST, context: Context): Result;
abstract visitImportName(node: ast.ImportNameAST, context: Context): Result;
abstract visitModulePartition(node: ast.ModulePartitionAST, context: Context): Result;
abstract visitAttributeArgumentClause(node: ast.AttributeArgumentClauseAST, context: Context): Result;
abstract visitAttribute(node: ast.AttributeAST, context: Context): Result;
abstract visitAttributeUsingPrefix(node: ast.AttributeUsingPrefixAST, context: Context): Result;

// RequirementAST
abstract visitSimpleRequirement(node: ast.SimpleRequirementAST, context: Context): Result;
Expand Down Expand Up @@ -242,5 +245,15 @@ export abstract class ASTVisitor<Context, Result> {
// DeclaratorModifierAST
abstract visitFunctionDeclarator(node: ast.FunctionDeclaratorAST, context: Context): Result;
abstract visitArrayDeclarator(node: ast.ArrayDeclaratorAST, context: Context): Result;

// AttributeSpecifierAST
abstract visitCxxAttribute(node: ast.CxxAttributeAST, context: Context): Result;
abstract visitGCCAttribute(node: ast.GCCAttributeAST, context: Context): Result;
abstract visitAlignasAttribute(node: ast.AlignasAttributeAST, context: Context): Result;
abstract visitAsmAttribute(node: ast.AsmAttributeAST, context: Context): Result;

// AttributeTokenAST
abstract visitScopedAttributeToken(node: ast.ScopedAttributeTokenAST, context: Context): Result;
abstract visitSimpleAttributeToken(node: ast.SimpleAttributeTokenAST, context: Context): Result;
}

34 changes: 34 additions & 0 deletions packages/cxx-frontend/src/RecursiveASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
this.accept(node.getModuleName(), context);
}

visitAttributeArgumentClause(node: ast.AttributeArgumentClauseAST, context: Context): void {
}

visitAttribute(node: ast.AttributeAST, context: Context): void {
this.accept(node.getAttributeToken(), context);
this.accept(node.getAttributeArgumentClause(), context);
}

visitAttributeUsingPrefix(node: ast.AttributeUsingPrefixAST, context: Context): void {
}

visitSimpleRequirement(node: ast.SimpleRequirementAST, context: Context): void {
this.accept(node.getExpression(), context);
}
Expand Down Expand Up @@ -962,5 +973,28 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
this.accept(element, context);
}
}

visitCxxAttribute(node: ast.CxxAttributeAST, context: Context): void {
this.accept(node.getAttributeUsingPrefix(), context);
for (const element of node.getAttributeList()) {
this.accept(element, context);
}
}

visitGCCAttribute(node: ast.GCCAttributeAST, context: Context): void {
}

visitAlignasAttribute(node: ast.AlignasAttributeAST, context: Context): void {
this.accept(node.getExpression(), context);
}

visitAsmAttribute(node: ast.AsmAttributeAST, context: Context): void {
}

visitScopedAttributeToken(node: ast.ScopedAttributeTokenAST, context: Context): void {
}

visitSimpleAttributeToken(node: ast.SimpleAttributeTokenAST, context: Context): void {
}
}

1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_decoder_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function gen_ast_decoder_cc({
const emit = (line = "") => code.push(line);

const by_base = groupNodesByBaseType(ast);
by_base.set("AttributeAST", []);

const makeClassName = (name: string) =>
name != "AST" ? name.slice(0, -3) : name;
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_decoder_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function gen_ast_decoder_h({
const emit = (line = "") => code.push(line);

const by_base = groupNodesByBaseType(ast);
by_base.set("AttributeAST", []);

const makeClassName = (name: string) =>
name != "AST" ? name.slice(0, -3) : name;
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function gen_ast_encoder_cc({
const emit = (line = "") => code.push(line);

const by_base = groupNodesByBaseType(ast);
by_base.set("AttributeAST", []);

const makeClassName = (name: string) =>
name != "AST" ? name.slice(0, -3) : name;
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_encoder_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function gen_ast_encoder_h({
const emit = (line = "") => code.push(line);

const by_base = groupNodesByBaseType(ast);
by_base.set("AttributeAST", []);

const makeClassName = (name: string) =>
name != "AST" ? name.slice(0, -3) : name;
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_fbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export function gen_ast_fbs({ ast, output }: { ast: AST; output: string }) {
const emit = (line = "") => code.push(line);

const by_bases = groupNodesByBaseType(ast);
by_bases.set("AttributeAST", []);

const className = (name: string) =>
name != "AST" ? name.slice(0, -3) : name;
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_fwd_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function gen_ast_fwd_h({ ast, output }: { ast: AST; output: string }) {
const by_base = groupNodesByBaseType(ast);

const baseClassNames = Array.from(by_base.keys()).filter((b) => b !== "AST");
baseClassNames.push("AttributeAST");
baseClassNames.sort();

for (const base of baseClassNames) {
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
const by_bases = groupNodesByBaseType(ast);

const baseClassNames = Array.from(by_bases.keys()).filter((b) => b !== "AST");
baseClassNames.push("AttributeAST");
baseClassNames.sort();

for (const base of baseClassNames) {
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_recursive_visitor_ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export function gen_ast_recursive_visitor_ts({
const by_bases = groupNodesByBaseType(ast);

const baseClassNames = Array.from(by_bases.keys()).filter((b) => b !== "AST");
baseClassNames.push("AttributeAST");
baseClassNames.sort();

emit();
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function gen_ast_ts({ ast, output }: { ast: AST; output: string }) {
const by_bases = groupNodesByBaseType(ast);

const baseClassNames = Array.from(by_bases.keys()).filter((b) => b !== "AST");
baseClassNames.push("AttributeAST");
baseClassNames.sort();

for (const base of baseClassNames) {
Expand Down
1 change: 0 additions & 1 deletion packages/cxx-gen-ast/src/gen_ast_visitor_ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export function gen_ast_visitor_ts({
const by_bases = groupNodesByBaseType(ast);

const baseClassNames = Array.from(by_bases.keys()).filter((b) => b !== "AST");
baseClassNames.push("AttributeAST");
baseClassNames.sort();

emit();
Expand Down
98 changes: 98 additions & 0 deletions src/frontend/cxx/ast_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,39 @@ void ASTPrinter::visit(ModulePartitionAST* ast) {
}
}

void ASTPrinter::visit(AttributeArgumentClauseAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:AttributeArgumentClause");
}

void ASTPrinter::visit(AttributeAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:Attribute");

if (ast->attributeToken) {
if (auto childNode = accept(ast->attributeToken); !childNode.is_null()) {
json_.push_back(std::vector<nlohmann::json>{"attr:attributeToken",
std::move(childNode)});
}
}

if (ast->attributeArgumentClause) {
if (auto childNode = accept(ast->attributeArgumentClause);
!childNode.is_null()) {
json_.push_back(std::vector<nlohmann::json>{
"attr:attributeArgumentClause", std::move(childNode)});
}
}
}

void ASTPrinter::visit(AttributeUsingPrefixAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:AttributeUsingPrefix");
}

void ASTPrinter::visit(SimpleRequirementAST* ast) {
json_ = nlohmann::json::array();

Expand Down Expand Up @@ -3603,4 +3636,69 @@ void ASTPrinter::visit(ArrayDeclaratorAST* ast) {
}
}

void ASTPrinter::visit(CxxAttributeAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:CxxAttribute");

if (ast->attributeUsingPrefix) {
if (auto childNode = accept(ast->attributeUsingPrefix);
!childNode.is_null()) {
json_.push_back(std::vector<nlohmann::json>{"attr:attributeUsingPrefix",
std::move(childNode)});
}
}

if (ast->attributeList) {
auto elements = nlohmann::json::array();
elements.push_back("array");
for (auto it = ast->attributeList; it; it = it->next) {
if (auto childNode = accept(it->value); !childNode.is_null()) {
elements.push_back(std::move(childNode));
}
}
if (elements.size() > 1) {
json_.push_back(
std::vector<nlohmann::json>{"attr:attributeList", elements});
}
}
}

void ASTPrinter::visit(GCCAttributeAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:GCCAttribute");
}

void ASTPrinter::visit(AlignasAttributeAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:AlignasAttribute");

if (ast->expression) {
if (auto childNode = accept(ast->expression); !childNode.is_null()) {
json_.push_back(
std::vector<nlohmann::json>{"attr:expression", std::move(childNode)});
}
}
}

void ASTPrinter::visit(AsmAttributeAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:AsmAttribute");
}

void ASTPrinter::visit(ScopedAttributeTokenAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:ScopedAttributeToken");
}

void ASTPrinter::visit(SimpleAttributeTokenAST* ast) {
json_ = nlohmann::json::array();

json_.push_back("ast:SimpleAttributeToken");
}

} // namespace cxx
11 changes: 11 additions & 0 deletions src/frontend/cxx/ast_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class ASTPrinter : ASTVisitor {
void visit(ModuleNameAST* ast) override;
void visit(ImportNameAST* ast) override;
void visit(ModulePartitionAST* ast) override;
void visit(AttributeArgumentClauseAST* ast) override;
void visit(AttributeAST* ast) override;
void visit(AttributeUsingPrefixAST* ast) override;

void visit(SimpleRequirementAST* ast) override;
void visit(CompoundRequirementAST* ast) override;
Expand Down Expand Up @@ -241,6 +244,14 @@ class ASTPrinter : ASTVisitor {

void visit(FunctionDeclaratorAST* ast) override;
void visit(ArrayDeclaratorAST* ast) override;

void visit(CxxAttributeAST* ast) override;
void visit(GCCAttributeAST* ast) override;
void visit(AlignasAttributeAST* ast) override;
void visit(AsmAttributeAST* ast) override;

void visit(ScopedAttributeTokenAST* ast) override;
void visit(SimpleAttributeTokenAST* ast) override;
};

} // namespace cxx
Loading

0 comments on commit aedc6a7

Please sign in to comment.