diff --git a/packages/cxx-gen-ast/src/gen_ast_dump_cc.ts b/packages/cxx-gen-ast/src/gen_ast_dump_cc.ts index 1a4969af..ffd3dab2 100644 --- a/packages/cxx-gen-ast/src/gen_ast_dump_cc.ts +++ b/packages/cxx-gen-ast/src/gen_ast_dump_cc.ts @@ -42,8 +42,8 @@ export function gen_ast_dump_cc({ ast, output }: { ast: AST; output: string }) { emit(` ++indent_;`); emit(` out_ << std::format("{:{}}", "", indent_ * 2);`); emit(` out_ << std::format("{}\\n", "${fieldName}");`); - emit(` for (auto it = ast->${member.name}; it; it = it->next) {`); - emit(` accept(it->value);`); + emit(` for (auto node: ListView{ast->${member.name}}) {`); + emit(` accept(node);`); emit(` }`); emit(` --indent_;`); emit(` }`); diff --git a/packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts b/packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts index af46e941..a3c3cf58 100644 --- a/packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts +++ b/packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts @@ -114,9 +114,9 @@ export function gen_ast_encoder_cc({ const className = makeClassName(m.type); emit(` std::vector>`); emit(` ${m.name}Offsets;`); - emit(` for (auto it = ast->${m.name}; it; it = it->next) {`); - emit(` if (!it->value) continue;`); - emit(` ${m.name}Offsets.emplace_back(accept(it->value).o);`); + emit(` for (auto node : ListView{ast->${m.name}}) {`); + emit(` if (!node) continue;`); + emit(` ${m.name}Offsets.emplace_back(accept(node).o);`); emit(` }`); emit(); emit(` auto ${m.name}OffsetsVector = fbb_.CreateVector(`); @@ -130,10 +130,10 @@ export function gen_ast_encoder_cc({ emit(` std::vector>`); emit(` ${m.name}Types;`); emit(); - emit(` for (auto it = ast->${m.name}; it; it = it->next) {`); - emit(` if (!it->value) continue;`); + emit(` for (auto node : ListView{ast->${m.name}}) {`); + emit(` if (!node) continue;`); emit(` const auto [offset, type] = accept${className}(`); - emit(` it->value);`); + emit(` node);`); emit(` ${m.name}Offsets.push_back(offset);`); emit(` ${m.name}Types.push_back(type);`); emit(` }`); diff --git a/packages/cxx-gen-ast/src/gen_ast_h.ts b/packages/cxx-gen-ast/src/gen_ast_h.ts index 40860eac..f414fc67 100644 --- a/packages/cxx-gen-ast/src/gen_ast_h.ts +++ b/packages/cxx-gen-ast/src/gen_ast_h.ts @@ -90,7 +90,7 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) { } emit( - ` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`, + ` void accept(ASTVisitor* visitor) override { visitor->visit(this); }` ); emit(); emit(` auto firstSourceLocation() -> SourceLocation override;`); @@ -110,7 +110,7 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) { emit(` switch (ast->kind()) {`); nodes.forEach(({ name }) => { emit( - ` case ${name}::Kind: return std::invoke(std::forward(visitor), static_cast<${name}*>(ast));`, + ` case ${name}::Kind: return std::invoke(std::forward(visitor), static_cast<${name}*>(ast));` ); }); emit(` default: cxx_runtime_error("unexpected ${variantName}");`); @@ -130,35 +130,76 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) { #include #include #include +#include namespace cxx { template class List final : public Managed { public: - T value; - List* next; + T value; + List* next; - explicit List(const T& value, List* next = nullptr) - : value(value), next(next) {} + explicit List(const T& value, List* next = nullptr) + : value(value), next(next) {} +}; + +template +class ListIterator { + public: + using value_type = T; + using difference_type = std::ptrdiff_t; + + ListIterator() = default; + explicit ListIterator(List* list) : list_(list) {} + + auto operator<=>(const ListIterator&) const = default; + + auto operator*() const -> const T& { return list_->value; } + + auto operator++() -> ListIterator& { + list_ = list_->next; + return *this; + } + + auto operator++(int) -> ListIterator { + auto it = *this; + ++*this; + return it; + } + + private: + List* list_{}; +}; + +template +class ListView : std::ranges::view_interface> { + public: + explicit ListView(List* list) : list_(list) {} + + auto begin() const { return ListIterator(list_); } + auto end() const { return ListIterator(); } + + private: + List* list_; }; class AST : public Managed { public: - explicit AST(ASTKind kind): kind_(kind) {} + explicit AST(ASTKind kind): kind_(kind) {} - virtual ~AST(); + virtual ~AST(); - [[nodiscard]] auto kind() const -> ASTKind { return kind_; } + [[nodiscard]] auto kind() const -> ASTKind { return kind_; } - virtual void accept(ASTVisitor* visitor) = 0; + virtual void accept(ASTVisitor* visitor) = 0; - virtual auto firstSourceLocation() -> SourceLocation = 0; - virtual auto lastSourceLocation() -> SourceLocation = 0; + virtual auto firstSourceLocation() -> SourceLocation = 0; + virtual auto lastSourceLocation() -> SourceLocation = 0; - [[nodiscard]] auto sourceLocationRange() -> SourceLocationRange { - return SourceLocationRange(firstSourceLocation(), lastSourceLocation()); - } + [[nodiscard]] auto sourceLocationRange() -> SourceLocationRange { + return SourceLocationRange(firstSourceLocation(), lastSourceLocation()); + } private: ASTKind kind_; @@ -173,8 +214,8 @@ template template [[nodiscard]] inline auto firstSourceLocation(List* nodes) -> SourceLocation { - for (auto it = nodes; it; it = it->next) { - if (auto loc = firstSourceLocation(it->value)) return loc; + for (auto node : ListView{nodes}) { + if (auto loc = firstSourceLocation(node)) return loc; } return {}; } diff --git a/packages/cxx-gen-ast/src/new_ast_op_cc.ts b/packages/cxx-gen-ast/src/new_ast_op_cc.ts index b483c8aa..5309cbc6 100644 --- a/packages/cxx-gen-ast/src/new_ast_op_cc.ts +++ b/packages/cxx-gen-ast/src/new_ast_op_cc.ts @@ -69,8 +69,8 @@ export function new_ast_op_cc({ } case "node-list": { emit(); - emit(` for (auto it = ast->${m.name}; it; it = it->next) {`); - emit(` auto value = operator()(it->value);`); + emit(` for (auto node : ListView{ast->${m.name}}) {`); + emit(` auto value = operator()(node);`); emit(` }`); emit(); break; @@ -90,7 +90,7 @@ export function new_ast_op_cc({ const resultTy = `${chopAST(base)}Result`; emit(); emit( - `auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${resultTy} {`, + `auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${resultTy} {` ); members.forEach((m) => { switch (m.kind) { @@ -100,8 +100,8 @@ export function new_ast_op_cc({ } case "node-list": { emit(); - emit(` for (auto it = ast->${m.name}; it; it = it->next) {`); - emit(` auto value = accept(it->value);`); + emit(` for (auto node : ListView{ast->${m.name}}) {`); + emit(` auto value = accept(node);`); emit(` }`); emit(); break; diff --git a/packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts b/packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts index 30a42f1b..9d95e8be 100644 --- a/packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts +++ b/packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts @@ -63,7 +63,7 @@ export function new_ast_rewriter_cc({ emit(` copy->${m.name} = ${visitor}(ast->${m.name});`); } else { emit( - ` copy->${m.name} = ast_cast<${m.type}>(${visitor}(ast->${m.name}));`, + ` copy->${m.name} = ast_cast<${m.type}>(${visitor}(ast->${m.name}));` ); } break; @@ -72,8 +72,8 @@ export function new_ast_rewriter_cc({ emit(); emit(` if (auto it = ast->${m.name}) {`); emit(` auto out = ©->${m.name};`); - emit(` for (auto it = ast->${m.name}; it; it = it->next) {`); - emit(` auto value = ${visitor}(it->value);`); + emit(` for (auto node : ListView{ast->${m.name}}) {`); + emit(` auto value = ${visitor}(node);`); if (isBase(m.type)) { emit(`*out = new (arena()) List(value);`); } else { @@ -126,7 +126,7 @@ export function new_ast_rewriter_cc({ nodes.forEach(({ name, members }) => { emit(); emit( - `auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${base}* {`, + `auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${base}* {` ); emit(` auto copy = new (arena()) ${name}{};`); emit(); diff --git a/src/frontend/cxx/ast_printer.cc b/src/frontend/cxx/ast_printer.cc index 89d47974..3c9c7bf0 100644 --- a/src/frontend/cxx/ast_printer.cc +++ b/src/frontend/cxx/ast_printer.cc @@ -62,8 +62,8 @@ void ASTPrinter::visit(TranslationUnitAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declaration-list"); - for (auto it = ast->declarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declarationList}) { + accept(node); } --indent_; } @@ -77,8 +77,8 @@ void ASTPrinter::visit(ModuleUnitAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declaration-list"); - for (auto it = ast->declarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declarationList}) { + accept(node); } --indent_; } @@ -91,8 +91,8 @@ void ASTPrinter::visit(SimpleDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -100,8 +100,8 @@ void ASTPrinter::visit(SimpleDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "decl-specifier-list"); - for (auto it = ast->declSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declSpecifierList}) { + accept(node); } --indent_; } @@ -109,8 +109,8 @@ void ASTPrinter::visit(SimpleDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "init-declarator-list"); - for (auto it = ast->initDeclaratorList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->initDeclaratorList}) { + accept(node); } --indent_; } @@ -129,8 +129,8 @@ void ASTPrinter::visit(AsmDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -138,8 +138,8 @@ void ASTPrinter::visit(AsmDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "asm-qualifier-list"); - for (auto it = ast->asmQualifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->asmQualifierList}) { + accept(node); } --indent_; } @@ -147,8 +147,8 @@ void ASTPrinter::visit(AsmDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "output-operand-list"); - for (auto it = ast->outputOperandList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->outputOperandList}) { + accept(node); } --indent_; } @@ -156,8 +156,8 @@ void ASTPrinter::visit(AsmDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "input-operand-list"); - for (auto it = ast->inputOperandList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->inputOperandList}) { + accept(node); } --indent_; } @@ -165,8 +165,8 @@ void ASTPrinter::visit(AsmDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "clobber-list"); - for (auto it = ast->clobberList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->clobberList}) { + accept(node); } --indent_; } @@ -174,8 +174,8 @@ void ASTPrinter::visit(AsmDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "goto-label-list"); - for (auto it = ast->gotoLabelList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->gotoLabelList}) { + accept(node); } --indent_; } @@ -194,8 +194,8 @@ void ASTPrinter::visit(UsingDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "using-declarator-list"); - for (auto it = ast->usingDeclaratorList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->usingDeclaratorList}) { + accept(node); } --indent_; } @@ -212,8 +212,8 @@ void ASTPrinter::visit(UsingDirectiveAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -239,8 +239,8 @@ void ASTPrinter::visit(AliasDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -248,8 +248,8 @@ void ASTPrinter::visit(AliasDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "gnu-attribute-list"); - for (auto it = ast->gnuAttributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->gnuAttributeList}) { + accept(node); } --indent_; } @@ -262,8 +262,8 @@ void ASTPrinter::visit(OpaqueEnumDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -273,8 +273,8 @@ void ASTPrinter::visit(OpaqueEnumDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "type-specifier-list"); - for (auto it = ast->typeSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + accept(node); } --indent_; } @@ -286,8 +286,8 @@ void ASTPrinter::visit(FunctionDefinitionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -295,8 +295,8 @@ void ASTPrinter::visit(FunctionDefinitionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "decl-specifier-list"); - for (auto it = ast->declSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declSpecifierList}) { + accept(node); } --indent_; } @@ -311,8 +311,8 @@ void ASTPrinter::visit(TemplateDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "template-parameter-list"); - for (auto it = ast->templateParameterList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->templateParameterList}) { + accept(node); } --indent_; } @@ -350,8 +350,8 @@ void ASTPrinter::visit(ExportCompoundDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declaration-list"); - for (auto it = ast->declarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declarationList}) { + accept(node); } --indent_; } @@ -369,8 +369,8 @@ void ASTPrinter::visit(LinkageSpecificationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declaration-list"); - for (auto it = ast->declarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declarationList}) { + accept(node); } --indent_; } @@ -389,8 +389,8 @@ void ASTPrinter::visit(NamespaceDefinitionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -398,8 +398,8 @@ void ASTPrinter::visit(NamespaceDefinitionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "nested-namespace-specifier-list"); - for (auto it = ast->nestedNamespaceSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->nestedNamespaceSpecifierList}) { + accept(node); } --indent_; } @@ -407,8 +407,8 @@ void ASTPrinter::visit(NamespaceDefinitionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "extra-attribute-list"); - for (auto it = ast->extraAttributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->extraAttributeList}) { + accept(node); } --indent_; } @@ -416,8 +416,8 @@ void ASTPrinter::visit(NamespaceDefinitionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declaration-list"); - for (auto it = ast->declarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declarationList}) { + accept(node); } --indent_; } @@ -433,8 +433,8 @@ void ASTPrinter::visit(AttributeDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -447,8 +447,8 @@ void ASTPrinter::visit(ModuleImportDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -473,8 +473,8 @@ void ASTPrinter::visit(ParameterDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -482,8 +482,8 @@ void ASTPrinter::visit(ParameterDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "type-specifier-list"); - for (auto it = ast->typeSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + accept(node); } --indent_; } @@ -512,8 +512,8 @@ void ASTPrinter::visit(StructuredBindingDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -521,8 +521,8 @@ void ASTPrinter::visit(StructuredBindingDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "decl-specifier-list"); - for (auto it = ast->declSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declSpecifierList}) { + accept(node); } --indent_; } @@ -530,8 +530,8 @@ void ASTPrinter::visit(StructuredBindingDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "binding-list"); - for (auto it = ast->bindingList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->bindingList}) { + accept(node); } --indent_; } @@ -601,8 +601,8 @@ void ASTPrinter::visit(CompoundStatementAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "statement-list"); - for (auto it = ast->statementList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->statementList}) { + accept(node); } --indent_; } @@ -698,8 +698,8 @@ void ASTPrinter::visit(TryBlockStatementAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "handler-list"); - for (auto it = ast->handlerList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->handlerList}) { + accept(node); } --indent_; } @@ -814,8 +814,8 @@ void ASTPrinter::visit(LambdaExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "capture-list"); - for (auto it = ast->captureList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->captureList}) { + accept(node); } --indent_; } @@ -823,8 +823,8 @@ void ASTPrinter::visit(LambdaExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "template-parameter-list"); - for (auto it = ast->templateParameterList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->templateParameterList}) { + accept(node); } --indent_; } @@ -834,8 +834,8 @@ void ASTPrinter::visit(LambdaExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "gnu-atribute-list"); - for (auto it = ast->gnuAtributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->gnuAtributeList}) { + accept(node); } --indent_; } @@ -843,8 +843,8 @@ void ASTPrinter::visit(LambdaExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "lambda-specifier-list"); - for (auto it = ast->lambdaSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->lambdaSpecifierList}) { + accept(node); } --indent_; } @@ -853,8 +853,8 @@ void ASTPrinter::visit(LambdaExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -910,8 +910,8 @@ void ASTPrinter::visit(RequiresExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "requirement-list"); - for (auto it = ast->requirementList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->requirementList}) { + accept(node); } --indent_; } @@ -936,8 +936,8 @@ void ASTPrinter::visit(CallExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "expression-list"); - for (auto it = ast->expressionList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->expressionList}) { + accept(node); } --indent_; } @@ -950,8 +950,8 @@ void ASTPrinter::visit(TypeConstructionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "expression-list"); - for (auto it = ast->expressionList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->expressionList}) { + accept(node); } --indent_; } @@ -1112,8 +1112,8 @@ void ASTPrinter::visit(NewExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "type-specifier-list"); - for (auto it = ast->typeSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + accept(node); } --indent_; } @@ -1199,8 +1199,8 @@ void ASTPrinter::visit(TypeTraitExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "type-id-list"); - for (auto it = ast->typeIdList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->typeIdList}) { + accept(node); } --indent_; } @@ -1212,8 +1212,8 @@ void ASTPrinter::visit(ConditionExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1221,8 +1221,8 @@ void ASTPrinter::visit(ConditionExpressionAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "decl-specifier-list"); - for (auto it = ast->declSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declSpecifierList}) { + accept(node); } --indent_; } @@ -1241,8 +1241,8 @@ void ASTPrinter::visit(BracedInitListAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "expression-list"); - for (auto it = ast->expressionList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->expressionList}) { + accept(node); } --indent_; } @@ -1254,8 +1254,8 @@ void ASTPrinter::visit(ParenInitializerAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "expression-list"); - for (auto it = ast->expressionList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->expressionList}) { + accept(node); } --indent_; } @@ -1272,8 +1272,8 @@ void ASTPrinter::visit(GlobalModuleFragmentAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declaration-list"); - for (auto it = ast->declarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declarationList}) { + accept(node); } --indent_; } @@ -1285,8 +1285,8 @@ void ASTPrinter::visit(PrivateModuleFragmentAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declaration-list"); - for (auto it = ast->declarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declarationList}) { + accept(node); } --indent_; } @@ -1300,8 +1300,8 @@ void ASTPrinter::visit(ModuleDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1343,8 +1343,8 @@ void ASTPrinter::visit(DeclaratorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "ptr-op-list"); - for (auto it = ast->ptrOpList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->ptrOpList}) { + accept(node); } --indent_; } @@ -1353,8 +1353,8 @@ void ASTPrinter::visit(DeclaratorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declarator-chunk-list"); - for (auto it = ast->declaratorChunkList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declaratorChunkList}) { + accept(node); } --indent_; } @@ -1379,8 +1379,8 @@ void ASTPrinter::visit(EnumeratorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1393,8 +1393,8 @@ void ASTPrinter::visit(TypeIdAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "type-specifier-list"); - for (auto it = ast->typeSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + accept(node); } --indent_; } @@ -1433,8 +1433,8 @@ void ASTPrinter::visit(BaseSpecifierAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1459,8 +1459,8 @@ void ASTPrinter::visit(ParameterDeclarationClauseAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "parameter-declaration-list"); - for (auto it = ast->parameterDeclarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->parameterDeclarationList}) { + accept(node); } --indent_; } @@ -1489,8 +1489,8 @@ void ASTPrinter::visit(TypeConstraintAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "template-argument-list"); - for (auto it = ast->templateArgumentList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->templateArgumentList}) { + accept(node); } --indent_; } @@ -1516,8 +1516,8 @@ void ASTPrinter::visit(NewPlacementAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "expression-list"); - for (auto it = ast->expressionList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->expressionList}) { + accept(node); } --indent_; } @@ -1555,8 +1555,8 @@ void ASTPrinter::visit(TemplateTypeParameterAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "template-parameter-list"); - for (auto it = ast->templateParameterList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->templateParameterList}) { + accept(node); } --indent_; } @@ -1773,8 +1773,8 @@ void ASTPrinter::visit(ElaboratedTypeSpecifierAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1815,8 +1815,8 @@ void ASTPrinter::visit(EnumSpecifierAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1826,8 +1826,8 @@ void ASTPrinter::visit(EnumSpecifierAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "type-specifier-list"); - for (auto it = ast->typeSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + accept(node); } --indent_; } @@ -1835,8 +1835,8 @@ void ASTPrinter::visit(EnumSpecifierAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "enumerator-list"); - for (auto it = ast->enumeratorList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->enumeratorList}) { + accept(node); } --indent_; } @@ -1860,8 +1860,8 @@ void ASTPrinter::visit(ClassSpecifierAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1871,8 +1871,8 @@ void ASTPrinter::visit(ClassSpecifierAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "base-specifier-list"); - for (auto it = ast->baseSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->baseSpecifierList}) { + accept(node); } --indent_; } @@ -1880,8 +1880,8 @@ void ASTPrinter::visit(ClassSpecifierAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "declaration-list"); - for (auto it = ast->declarationList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->declarationList}) { + accept(node); } --indent_; } @@ -1904,8 +1904,8 @@ void ASTPrinter::visit(PointerOperatorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1913,8 +1913,8 @@ void ASTPrinter::visit(PointerOperatorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "cv-qualifier-list"); - for (auto it = ast->cvQualifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->cvQualifierList}) { + accept(node); } --indent_; } @@ -1932,8 +1932,8 @@ void ASTPrinter::visit(ReferenceOperatorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1946,8 +1946,8 @@ void ASTPrinter::visit(PtrToMemberOperatorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -1955,8 +1955,8 @@ void ASTPrinter::visit(PtrToMemberOperatorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "cv-qualifier-list"); - for (auto it = ast->cvQualifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->cvQualifierList}) { + accept(node); } --indent_; } @@ -1988,8 +1988,8 @@ void ASTPrinter::visit(IdDeclaratorAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -2025,8 +2025,8 @@ void ASTPrinter::visit(FunctionDeclaratorChunkAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "cv-qualifier-list"); - for (auto it = ast->cvQualifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->cvQualifierList}) { + accept(node); } --indent_; } @@ -2035,8 +2035,8 @@ void ASTPrinter::visit(FunctionDeclaratorChunkAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -2050,8 +2050,8 @@ void ASTPrinter::visit(ArrayDeclaratorChunkAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -2105,8 +2105,8 @@ void ASTPrinter::visit(SimpleTemplateIdAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "template-argument-list"); - for (auto it = ast->templateArgumentList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->templateArgumentList}) { + accept(node); } --indent_; } @@ -2119,8 +2119,8 @@ void ASTPrinter::visit(LiteralOperatorTemplateIdAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "template-argument-list"); - for (auto it = ast->templateArgumentList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->templateArgumentList}) { + accept(node); } --indent_; } @@ -2133,8 +2133,8 @@ void ASTPrinter::visit(OperatorFunctionTemplateIdAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "template-argument-list"); - for (auto it = ast->templateArgumentList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->templateArgumentList}) { + accept(node); } --indent_; } @@ -2179,8 +2179,8 @@ void ASTPrinter::visit(CompoundStatementFunctionBodyAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "mem-initializer-list"); - for (auto it = ast->memInitializerList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->memInitializerList}) { + accept(node); } --indent_; } @@ -2193,8 +2193,8 @@ void ASTPrinter::visit(TryStatementFunctionBodyAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "mem-initializer-list"); - for (auto it = ast->memInitializerList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->memInitializerList}) { + accept(node); } --indent_; } @@ -2203,8 +2203,8 @@ void ASTPrinter::visit(TryStatementFunctionBodyAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "handler-list"); - for (auto it = ast->handlerList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->handlerList}) { + accept(node); } --indent_; } @@ -2261,8 +2261,8 @@ void ASTPrinter::visit(NewParenInitializerAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "expression-list"); - for (auto it = ast->expressionList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->expressionList}) { + accept(node); } --indent_; } @@ -2281,8 +2281,8 @@ void ASTPrinter::visit(ParenMemInitializerAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "expression-list"); - for (auto it = ast->expressionList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->expressionList}) { + accept(node); } --indent_; } @@ -2335,8 +2335,8 @@ void ASTPrinter::visit(TypeExceptionDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } @@ -2344,8 +2344,8 @@ void ASTPrinter::visit(TypeExceptionDeclarationAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "type-specifier-list"); - for (auto it = ast->typeSpecifierList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + accept(node); } --indent_; } @@ -2359,8 +2359,8 @@ void ASTPrinter::visit(CxxAttributeAST* ast) { ++indent_; out_ << std::format("{:{}}", "", indent_ * 2); out_ << std::format("{}\n", "attribute-list"); - for (auto it = ast->attributeList; it; it = it->next) { - accept(it->value); + for (auto node : ListView{ast->attributeList}) { + accept(node); } --indent_; } diff --git a/src/frontend/cxx/frontend.cc b/src/frontend/cxx/frontend.cc index fc0f0bf4..c63ca3ff 100644 --- a/src/frontend/cxx/frontend.cc +++ b/src/frontend/cxx/frontend.cc @@ -87,7 +87,11 @@ void dumpTokens(const CLI& cli, TranslationUnit& unit, std::ostream& output) { kind = Lexer::classifyKeyword(tk.spell()); } - output << std::format("{} '{}'{}\n", Token::name(kind), tk.spell(), flags); + output << std::format("{} '{}'{}", Token::name(kind), tk.spell(), flags); + + auto pos = unit.tokenStartPosition(loc); + + output << std::format(" at {}:{}:{}\n", pos.fileName, pos.line, pos.column); if (tk.is(TokenKind::T_EOF_SYMBOL)) break; } diff --git a/src/parser/cxx/ast.h b/src/parser/cxx/ast.h index 36430d00..f7836b8d 100644 --- a/src/parser/cxx/ast.h +++ b/src/parser/cxx/ast.h @@ -30,6 +30,7 @@ #include #include +#include namespace cxx { @@ -43,6 +44,46 @@ class List final : public Managed { : value(value), next(next) {} }; +template +class ListIterator { + public: + using value_type = T; + using difference_type = std::ptrdiff_t; + + ListIterator() = default; + explicit ListIterator(List* list) : list_(list) {} + + auto operator<=>(const ListIterator&) const = default; + + auto operator*() const -> const T& { return list_->value; } + + auto operator++() -> ListIterator& { + list_ = list_->next; + return *this; + } + + auto operator++(int) -> ListIterator { + auto it = *this; + ++*this; + return it; + } + + private: + List* list_{}; +}; + +template +class ListView : std::ranges::view_interface> { + public: + explicit ListView(List* list) : list_(list) {} + + auto begin() const { return ListIterator(list_); } + auto end() const { return ListIterator(); } + + private: + List* list_; +}; + class AST : public Managed { public: explicit AST(ASTKind kind) : kind_(kind) {} @@ -77,8 +118,8 @@ template template [[nodiscard]] inline auto firstSourceLocation(List* nodes) -> SourceLocation { - for (auto it = nodes; it; it = it->next) { - if (auto loc = firstSourceLocation(it->value)) return loc; + for (auto node : ListView{nodes}) { + if (auto loc = firstSourceLocation(node)) return loc; } return {}; } diff --git a/src/parser/cxx/flatbuffers/ast_encoder.cc b/src/parser/cxx/flatbuffers/ast_encoder.cc index 6dfabd11..ae31aeab 100644 --- a/src/parser/cxx/flatbuffers/ast_encoder.cc +++ b/src/parser/cxx/flatbuffers/ast_encoder.cc @@ -401,9 +401,9 @@ void ASTEncoder::visit(TranslationUnitAST* ast) { std::vector> declarationListOffsets; std::vector> declarationListTypes; - for (auto it = ast->declarationList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaration(it->value); + for (auto node : ListView{ast->declarationList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaration(node); declarationListOffsets.push_back(offset); declarationListTypes.push_back(type); } @@ -427,9 +427,9 @@ void ASTEncoder::visit(ModuleUnitAST* ast) { std::vector> declarationListOffsets; std::vector> declarationListTypes; - for (auto it = ast->declarationList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaration(it->value); + for (auto node : ListView{ast->declarationList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaration(node); declarationListOffsets.push_back(offset); declarationListTypes.push_back(type); } @@ -455,9 +455,9 @@ void ASTEncoder::visit(SimpleDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -468,9 +468,9 @@ void ASTEncoder::visit(SimpleDeclarationAST* ast) { std::vector> declSpecifierListOffsets; std::vector> declSpecifierListTypes; - for (auto it = ast->declSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->declSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); declSpecifierListOffsets.push_back(offset); declSpecifierListTypes.push_back(type); } @@ -481,9 +481,9 @@ void ASTEncoder::visit(SimpleDeclarationAST* ast) { std::vector> initDeclaratorListOffsets; - for (auto it = ast->initDeclaratorList; it; it = it->next) { - if (!it->value) continue; - initDeclaratorListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->initDeclaratorList}) { + if (!node) continue; + initDeclaratorListOffsets.emplace_back(accept(node).o); } auto initDeclaratorListOffsetsVector = @@ -511,9 +511,9 @@ void ASTEncoder::visit(AsmDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -522,9 +522,9 @@ void ASTEncoder::visit(AsmDeclarationAST* ast) { auto attributeListTypesVector = fbb_.CreateVector(attributeListTypes); std::vector> asmQualifierListOffsets; - for (auto it = ast->asmQualifierList; it; it = it->next) { - if (!it->value) continue; - asmQualifierListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->asmQualifierList}) { + if (!node) continue; + asmQualifierListOffsets.emplace_back(accept(node).o); } auto asmQualifierListOffsetsVector = @@ -537,35 +537,35 @@ void ASTEncoder::visit(AsmDeclarationAST* ast) { auto literalLoc = encodeSourceLocation(ast->literalLoc); std::vector> outputOperandListOffsets; - for (auto it = ast->outputOperandList; it; it = it->next) { - if (!it->value) continue; - outputOperandListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->outputOperandList}) { + if (!node) continue; + outputOperandListOffsets.emplace_back(accept(node).o); } auto outputOperandListOffsetsVector = fbb_.CreateVector(outputOperandListOffsets); std::vector> inputOperandListOffsets; - for (auto it = ast->inputOperandList; it; it = it->next) { - if (!it->value) continue; - inputOperandListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->inputOperandList}) { + if (!node) continue; + inputOperandListOffsets.emplace_back(accept(node).o); } auto inputOperandListOffsetsVector = fbb_.CreateVector(inputOperandListOffsets); std::vector> clobberListOffsets; - for (auto it = ast->clobberList; it; it = it->next) { - if (!it->value) continue; - clobberListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->clobberList}) { + if (!node) continue; + clobberListOffsets.emplace_back(accept(node).o); } auto clobberListOffsetsVector = fbb_.CreateVector(clobberListOffsets); std::vector> gotoLabelListOffsets; - for (auto it = ast->gotoLabelList; it; it = it->next) { - if (!it->value) continue; - gotoLabelListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->gotoLabelList}) { + if (!node) continue; + gotoLabelListOffsets.emplace_back(accept(node).o); } auto gotoLabelListOffsetsVector = fbb_.CreateVector(gotoLabelListOffsets); @@ -638,9 +638,9 @@ void ASTEncoder::visit(UsingDeclarationAST* ast) { std::vector> usingDeclaratorListOffsets; - for (auto it = ast->usingDeclaratorList; it; it = it->next) { - if (!it->value) continue; - usingDeclaratorListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->usingDeclaratorList}) { + if (!node) continue; + usingDeclaratorListOffsets.emplace_back(accept(node).o); } auto usingDeclaratorListOffsetsVector = @@ -678,9 +678,9 @@ void ASTEncoder::visit(UsingDirectiveAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -752,9 +752,9 @@ void ASTEncoder::visit(AliasDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -768,9 +768,9 @@ void ASTEncoder::visit(AliasDeclarationAST* ast) { std::vector> gnuAttributeListTypes; - for (auto it = ast->gnuAttributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->gnuAttributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); gnuAttributeListOffsets.push_back(offset); gnuAttributeListTypes.push_back(type); } @@ -820,9 +820,9 @@ void ASTEncoder::visit(OpaqueEnumDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -840,9 +840,9 @@ void ASTEncoder::visit(OpaqueEnumDeclarationAST* ast) { std::vector> typeSpecifierListOffsets; std::vector> typeSpecifierListTypes; - for (auto it = ast->typeSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); typeSpecifierListOffsets.push_back(offset); typeSpecifierListTypes.push_back(type); } @@ -876,9 +876,9 @@ void ASTEncoder::visit(FunctionDefinitionAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -889,9 +889,9 @@ void ASTEncoder::visit(FunctionDefinitionAST* ast) { std::vector> declSpecifierListOffsets; std::vector> declSpecifierListTypes; - for (auto it = ast->declSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->declSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); declSpecifierListOffsets.push_back(offset); declSpecifierListTypes.push_back(type); } @@ -931,9 +931,9 @@ void ASTEncoder::visit(TemplateDeclarationAST* ast) { std::vector> templateParameterListTypes; - for (auto it = ast->templateParameterList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptTemplateParameter(it->value); + for (auto node : ListView{ast->templateParameterList}) { + if (!node) continue; + const auto [offset, type] = acceptTemplateParameter(node); templateParameterListOffsets.push_back(offset); templateParameterListTypes.push_back(type); } @@ -1089,9 +1089,9 @@ void ASTEncoder::visit(ExportCompoundDeclarationAST* ast) { std::vector> declarationListOffsets; std::vector> declarationListTypes; - for (auto it = ast->declarationList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaration(it->value); + for (auto node : ListView{ast->declarationList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaration(node); declarationListOffsets.push_back(offset); declarationListTypes.push_back(type); } @@ -1122,9 +1122,9 @@ void ASTEncoder::visit(LinkageSpecificationAST* ast) { std::vector> declarationListOffsets; std::vector> declarationListTypes; - for (auto it = ast->declarationList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaration(it->value); + for (auto node : ListView{ast->declarationList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaration(node); declarationListOffsets.push_back(offset); declarationListTypes.push_back(type); } @@ -1168,9 +1168,9 @@ void ASTEncoder::visit(NamespaceDefinitionAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -1180,9 +1180,9 @@ void ASTEncoder::visit(NamespaceDefinitionAST* ast) { std::vector> nestedNamespaceSpecifierListOffsets; - for (auto it = ast->nestedNamespaceSpecifierList; it; it = it->next) { - if (!it->value) continue; - nestedNamespaceSpecifierListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->nestedNamespaceSpecifierList}) { + if (!node) continue; + nestedNamespaceSpecifierListOffsets.emplace_back(accept(node).o); } auto nestedNamespaceSpecifierListOffsetsVector = @@ -1194,9 +1194,9 @@ void ASTEncoder::visit(NamespaceDefinitionAST* ast) { std::vector> extraAttributeListTypes; - for (auto it = ast->extraAttributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->extraAttributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); extraAttributeListOffsets.push_back(offset); extraAttributeListTypes.push_back(type); } @@ -1211,9 +1211,9 @@ void ASTEncoder::visit(NamespaceDefinitionAST* ast) { std::vector> declarationListOffsets; std::vector> declarationListTypes; - for (auto it = ast->declarationList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaration(it->value); + for (auto node : ListView{ast->declarationList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaration(node); declarationListOffsets.push_back(offset); declarationListTypes.push_back(type); } @@ -1270,9 +1270,9 @@ void ASTEncoder::visit(AttributeDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -1300,9 +1300,9 @@ void ASTEncoder::visit(ModuleImportDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -1328,9 +1328,9 @@ void ASTEncoder::visit(ParameterDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -1343,9 +1343,9 @@ void ASTEncoder::visit(ParameterDeclarationAST* ast) { std::vector> typeSpecifierListOffsets; std::vector> typeSpecifierListTypes; - for (auto it = ast->typeSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); typeSpecifierListOffsets.push_back(offset); typeSpecifierListTypes.push_back(type); } @@ -1415,9 +1415,9 @@ void ASTEncoder::visit(StructuredBindingDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -1428,9 +1428,9 @@ void ASTEncoder::visit(StructuredBindingDeclarationAST* ast) { std::vector> declSpecifierListOffsets; std::vector> declSpecifierListTypes; - for (auto it = ast->declSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->declSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); declSpecifierListOffsets.push_back(offset); declSpecifierListTypes.push_back(type); } @@ -1444,9 +1444,9 @@ void ASTEncoder::visit(StructuredBindingDeclarationAST* ast) { auto lbracketLoc = encodeSourceLocation(ast->lbracketLoc); std::vector> bindingListOffsets; - for (auto it = ast->bindingList; it; it = it->next) { - if (!it->value) continue; - bindingListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->bindingList}) { + if (!node) continue; + bindingListOffsets.emplace_back(accept(node).o); } auto bindingListOffsetsVector = fbb_.CreateVector(bindingListOffsets); @@ -1650,9 +1650,9 @@ void ASTEncoder::visit(CompoundStatementAST* ast) { std::vector> statementListOffsets; std::vector> statementListTypes; - for (auto it = ast->statementList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptStatement(it->value); + for (auto node : ListView{ast->statementList}) { + if (!node) continue; + const auto [offset, type] = acceptStatement(node); statementListOffsets.push_back(offset); statementListTypes.push_back(type); } @@ -2004,9 +2004,9 @@ void ASTEncoder::visit(TryBlockStatementAST* ast) { const auto statement = accept(ast->statement); std::vector> handlerListOffsets; - for (auto it = ast->handlerList; it; it = it->next) { - if (!it->value) continue; - handlerListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->handlerList}) { + if (!node) continue; + handlerListOffsets.emplace_back(accept(node).o); } auto handlerListOffsetsVector = fbb_.CreateVector(handlerListOffsets); @@ -2223,9 +2223,9 @@ void ASTEncoder::visit(LambdaExpressionAST* ast) { std::vector> captureListOffsets; std::vector> captureListTypes; - for (auto it = ast->captureList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptLambdaCapture(it->value); + for (auto node : ListView{ast->captureList}) { + if (!node) continue; + const auto [offset, type] = acceptLambdaCapture(node); captureListOffsets.push_back(offset); captureListTypes.push_back(type); } @@ -2241,9 +2241,9 @@ void ASTEncoder::visit(LambdaExpressionAST* ast) { std::vector> templateParameterListTypes; - for (auto it = ast->templateParameterList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptTemplateParameter(it->value); + for (auto node : ListView{ast->templateParameterList}) { + if (!node) continue; + const auto [offset, type] = acceptTemplateParameter(node); templateParameterListOffsets.push_back(offset); templateParameterListTypes.push_back(type); } @@ -2268,9 +2268,9 @@ void ASTEncoder::visit(LambdaExpressionAST* ast) { std::vector> gnuAtributeListTypes; - for (auto it = ast->gnuAtributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->gnuAtributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); gnuAtributeListOffsets.push_back(offset); gnuAtributeListTypes.push_back(type); } @@ -2280,9 +2280,9 @@ void ASTEncoder::visit(LambdaExpressionAST* ast) { std::vector> lambdaSpecifierListOffsets; - for (auto it = ast->lambdaSpecifierList; it; it = it->next) { - if (!it->value) continue; - lambdaSpecifierListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->lambdaSpecifierList}) { + if (!node) continue; + lambdaSpecifierListOffsets.emplace_back(accept(node).o); } auto lambdaSpecifierListOffsetsVector = @@ -2295,9 +2295,9 @@ void ASTEncoder::visit(LambdaExpressionAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -2441,9 +2441,9 @@ void ASTEncoder::visit(RequiresExpressionAST* ast) { std::vector> requirementListOffsets; std::vector> requirementListTypes; - for (auto it = ast->requirementList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptRequirement(it->value); + for (auto node : ListView{ast->requirementList}) { + if (!node) continue; + const auto [offset, type] = acceptRequirement(node); requirementListOffsets.push_back(offset); requirementListTypes.push_back(type); } @@ -2527,9 +2527,9 @@ void ASTEncoder::visit(CallExpressionAST* ast) { std::vector> expressionListOffsets; std::vector> expressionListTypes; - for (auto it = ast->expressionList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptExpression(it->value); + for (auto node : ListView{ast->expressionList}) { + if (!node) continue; + const auto [offset, type] = acceptExpression(node); expressionListOffsets.push_back(offset); expressionListTypes.push_back(type); } @@ -2561,9 +2561,9 @@ void ASTEncoder::visit(TypeConstructionAST* ast) { std::vector> expressionListOffsets; std::vector> expressionListTypes; - for (auto it = ast->expressionList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptExpression(it->value); + for (auto node : ListView{ast->expressionList}) { + if (!node) continue; + const auto [offset, type] = acceptExpression(node); expressionListOffsets.push_back(offset); expressionListTypes.push_back(type); } @@ -3006,9 +3006,9 @@ void ASTEncoder::visit(NewExpressionAST* ast) { std::vector> typeSpecifierListOffsets; std::vector> typeSpecifierListTypes; - for (auto it = ast->typeSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); typeSpecifierListOffsets.push_back(offset); typeSpecifierListTypes.push_back(type); } @@ -3249,9 +3249,9 @@ void ASTEncoder::visit(TypeTraitExpressionAST* ast) { auto lparenLoc = encodeSourceLocation(ast->lparenLoc); std::vector> typeIdListOffsets; - for (auto it = ast->typeIdList; it; it = it->next) { - if (!it->value) continue; - typeIdListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->typeIdList}) { + if (!node) continue; + typeIdListOffsets.emplace_back(accept(node).o); } auto typeIdListOffsetsVector = fbb_.CreateVector(typeIdListOffsets); @@ -3273,9 +3273,9 @@ void ASTEncoder::visit(ConditionExpressionAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -3286,9 +3286,9 @@ void ASTEncoder::visit(ConditionExpressionAST* ast) { std::vector> declSpecifierListOffsets; std::vector> declSpecifierListTypes; - for (auto it = ast->declSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->declSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); declSpecifierListOffsets.push_back(offset); declSpecifierListTypes.push_back(type); } @@ -3335,9 +3335,9 @@ void ASTEncoder::visit(BracedInitListAST* ast) { std::vector> expressionListOffsets; std::vector> expressionListTypes; - for (auto it = ast->expressionList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptExpression(it->value); + for (auto node : ListView{ast->expressionList}) { + if (!node) continue; + const auto [offset, type] = acceptExpression(node); expressionListOffsets.push_back(offset); expressionListTypes.push_back(type); } @@ -3366,9 +3366,9 @@ void ASTEncoder::visit(ParenInitializerAST* ast) { std::vector> expressionListOffsets; std::vector> expressionListTypes; - for (auto it = ast->expressionList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptExpression(it->value); + for (auto node : ListView{ast->expressionList}) { + if (!node) continue; + const auto [offset, type] = acceptExpression(node); expressionListOffsets.push_back(offset); expressionListTypes.push_back(type); } @@ -3421,9 +3421,9 @@ void ASTEncoder::visit(GlobalModuleFragmentAST* ast) { std::vector> declarationListOffsets; std::vector> declarationListTypes; - for (auto it = ast->declarationList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaration(it->value); + for (auto node : ListView{ast->declarationList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaration(node); declarationListOffsets.push_back(offset); declarationListTypes.push_back(type); } @@ -3452,9 +3452,9 @@ void ASTEncoder::visit(PrivateModuleFragmentAST* ast) { std::vector> declarationListOffsets; std::vector> declarationListTypes; - for (auto it = ast->declarationList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaration(it->value); + for (auto node : ListView{ast->declarationList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaration(node); declarationListOffsets.push_back(offset); declarationListTypes.push_back(type); } @@ -3486,9 +3486,9 @@ void ASTEncoder::visit(ModuleDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -3611,9 +3611,9 @@ void ASTEncoder::visit(DeclaratorAST* ast) { std::vector> ptrOpListOffsets; std::vector> ptrOpListTypes; - for (auto it = ast->ptrOpList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptPtrOperator(it->value); + for (auto node : ListView{ast->ptrOpList}) { + if (!node) continue; + const auto [offset, type] = acceptPtrOperator(node); ptrOpListOffsets.push_back(offset); ptrOpListTypes.push_back(type); } @@ -3628,9 +3628,9 @@ void ASTEncoder::visit(DeclaratorAST* ast) { std::vector> declaratorChunkListTypes; - for (auto it = ast->declaratorChunkList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaratorChunk(it->value); + for (auto node : ListView{ast->declaratorChunkList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaratorChunk(node); declaratorChunkListOffsets.push_back(offset); declaratorChunkListTypes.push_back(type); } @@ -3683,9 +3683,9 @@ void ASTEncoder::visit(EnumeratorAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -3725,9 +3725,9 @@ void ASTEncoder::visit(TypeIdAST* ast) { std::vector> typeSpecifierListOffsets; std::vector> typeSpecifierListTypes; - for (auto it = ast->typeSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); typeSpecifierListOffsets.push_back(offset); typeSpecifierListTypes.push_back(type); } @@ -3775,9 +3775,9 @@ void ASTEncoder::visit(BaseSpecifierAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -3825,9 +3825,9 @@ void ASTEncoder::visit(RequiresClauseAST* ast) { void ASTEncoder::visit(ParameterDeclarationClauseAST* ast) { std::vector> parameterDeclarationListOffsets; - for (auto it = ast->parameterDeclarationList; it; it = it->next) { - if (!it->value) continue; - parameterDeclarationListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->parameterDeclarationList}) { + if (!node) continue; + parameterDeclarationListOffsets.emplace_back(accept(node).o); } auto parameterDeclarationListOffsetsVector = @@ -3879,9 +3879,9 @@ void ASTEncoder::visit(TypeConstraintAST* ast) { std::vector> templateArgumentListTypes; - for (auto it = ast->templateArgumentList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptTemplateArgument(it->value); + for (auto node : ListView{ast->templateArgumentList}) { + if (!node) continue; + const auto [offset, type] = acceptTemplateArgument(node); templateArgumentListOffsets.push_back(offset); templateArgumentListTypes.push_back(type); } @@ -3970,9 +3970,9 @@ void ASTEncoder::visit(NewPlacementAST* ast) { std::vector> expressionListOffsets; std::vector> expressionListTypes; - for (auto it = ast->expressionList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptExpression(it->value); + for (auto node : ListView{ast->expressionList}) { + if (!node) continue; + const auto [offset, type] = acceptExpression(node); expressionListOffsets.push_back(offset); expressionListTypes.push_back(type); } @@ -4028,9 +4028,9 @@ void ASTEncoder::visit(TemplateTypeParameterAST* ast) { std::vector> templateParameterListTypes; - for (auto it = ast->templateParameterList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptTemplateParameter(it->value); + for (auto node : ListView{ast->templateParameterList}) { + if (!node) continue; + const auto [offset, type] = acceptTemplateParameter(node); templateParameterListOffsets.push_back(offset); templateParameterListTypes.push_back(type); } @@ -4466,9 +4466,9 @@ void ASTEncoder::visit(ElaboratedTypeSpecifierAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -4593,9 +4593,9 @@ void ASTEncoder::visit(EnumSpecifierAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -4613,9 +4613,9 @@ void ASTEncoder::visit(EnumSpecifierAST* ast) { std::vector> typeSpecifierListOffsets; std::vector> typeSpecifierListTypes; - for (auto it = ast->typeSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); typeSpecifierListOffsets.push_back(offset); typeSpecifierListTypes.push_back(type); } @@ -4629,9 +4629,9 @@ void ASTEncoder::visit(EnumSpecifierAST* ast) { auto commaLoc = encodeSourceLocation(ast->commaLoc); std::vector> enumeratorListOffsets; - for (auto it = ast->enumeratorList; it; it = it->next) { - if (!it->value) continue; - enumeratorListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->enumeratorList}) { + if (!node) continue; + enumeratorListOffsets.emplace_back(accept(node).o); } auto enumeratorListOffsetsVector = fbb_.CreateVector(enumeratorListOffsets); @@ -4666,9 +4666,9 @@ void ASTEncoder::visit(ClassSpecifierAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -4687,9 +4687,9 @@ void ASTEncoder::visit(ClassSpecifierAST* ast) { auto colonLoc = encodeSourceLocation(ast->colonLoc); std::vector> baseSpecifierListOffsets; - for (auto it = ast->baseSpecifierList; it; it = it->next) { - if (!it->value) continue; - baseSpecifierListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->baseSpecifierList}) { + if (!node) continue; + baseSpecifierListOffsets.emplace_back(accept(node).o); } auto baseSpecifierListOffsetsVector = @@ -4700,9 +4700,9 @@ void ASTEncoder::visit(ClassSpecifierAST* ast) { std::vector> declarationListOffsets; std::vector> declarationListTypes; - for (auto it = ast->declarationList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptDeclaration(it->value); + for (auto node : ListView{ast->declarationList}) { + if (!node) continue; + const auto [offset, type] = acceptDeclaration(node); declarationListOffsets.push_back(offset); declarationListTypes.push_back(type); } @@ -4777,9 +4777,9 @@ void ASTEncoder::visit(PointerOperatorAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -4790,9 +4790,9 @@ void ASTEncoder::visit(PointerOperatorAST* ast) { std::vector> cvQualifierListOffsets; std::vector> cvQualifierListTypes; - for (auto it = ast->cvQualifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->cvQualifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); cvQualifierListOffsets.push_back(offset); cvQualifierListTypes.push_back(type); } @@ -4818,9 +4818,9 @@ void ASTEncoder::visit(ReferenceOperatorAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -4848,9 +4848,9 @@ void ASTEncoder::visit(PtrToMemberOperatorAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -4861,9 +4861,9 @@ void ASTEncoder::visit(PtrToMemberOperatorAST* ast) { std::vector> cvQualifierListOffsets; std::vector> cvQualifierListTypes; - for (auto it = ast->cvQualifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->cvQualifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); cvQualifierListOffsets.push_back(offset); cvQualifierListTypes.push_back(type); } @@ -4933,9 +4933,9 @@ void ASTEncoder::visit(IdDeclaratorAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -4985,9 +4985,9 @@ void ASTEncoder::visit(FunctionDeclaratorChunkAST* ast) { std::vector> cvQualifierListOffsets; std::vector> cvQualifierListTypes; - for (auto it = ast->cvQualifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->cvQualifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); cvQualifierListOffsets.push_back(offset); cvQualifierListTypes.push_back(type); } @@ -5004,9 +5004,9 @@ void ASTEncoder::visit(FunctionDeclaratorChunkAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -5045,9 +5045,9 @@ void ASTEncoder::visit(ArrayDeclaratorChunkAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -5185,9 +5185,9 @@ void ASTEncoder::visit(SimpleTemplateIdAST* ast) { std::vector> templateArgumentListTypes; - for (auto it = ast->templateArgumentList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptTemplateArgument(it->value); + for (auto node : ListView{ast->templateArgumentList}) { + if (!node) continue; + const auto [offset, type] = acceptTemplateArgument(node); templateArgumentListOffsets.push_back(offset); templateArgumentListTypes.push_back(type); } @@ -5232,9 +5232,9 @@ void ASTEncoder::visit(LiteralOperatorTemplateIdAST* ast) { std::vector> templateArgumentListTypes; - for (auto it = ast->templateArgumentList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptTemplateArgument(it->value); + for (auto node : ListView{ast->templateArgumentList}) { + if (!node) continue; + const auto [offset, type] = acceptTemplateArgument(node); templateArgumentListOffsets.push_back(offset); templateArgumentListTypes.push_back(type); } @@ -5266,9 +5266,9 @@ void ASTEncoder::visit(OperatorFunctionTemplateIdAST* ast) { std::vector> templateArgumentListTypes; - for (auto it = ast->templateArgumentList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptTemplateArgument(it->value); + for (auto node : ListView{ast->templateArgumentList}) { + if (!node) continue; + const auto [offset, type] = acceptTemplateArgument(node); templateArgumentListOffsets.push_back(offset); templateArgumentListTypes.push_back(type); } @@ -5397,9 +5397,9 @@ void ASTEncoder::visit(CompoundStatementFunctionBodyAST* ast) { std::vector> memInitializerListTypes; - for (auto it = ast->memInitializerList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptMemInitializer(it->value); + for (auto node : ListView{ast->memInitializerList}) { + if (!node) continue; + const auto [offset, type] = acceptMemInitializer(node); memInitializerListOffsets.push_back(offset); memInitializerListTypes.push_back(type); } @@ -5430,9 +5430,9 @@ void ASTEncoder::visit(TryStatementFunctionBodyAST* ast) { std::vector> memInitializerListTypes; - for (auto it = ast->memInitializerList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptMemInitializer(it->value); + for (auto node : ListView{ast->memInitializerList}) { + if (!node) continue; + const auto [offset, type] = acceptMemInitializer(node); memInitializerListOffsets.push_back(offset); memInitializerListTypes.push_back(type); } @@ -5445,9 +5445,9 @@ void ASTEncoder::visit(TryStatementFunctionBodyAST* ast) { const auto statement = accept(ast->statement); std::vector> handlerListOffsets; - for (auto it = ast->handlerList; it; it = it->next) { - if (!it->value) continue; - handlerListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->handlerList}) { + if (!node) continue; + handlerListOffsets.emplace_back(accept(node).o); } auto handlerListOffsetsVector = fbb_.CreateVector(handlerListOffsets); @@ -5628,9 +5628,9 @@ void ASTEncoder::visit(NewParenInitializerAST* ast) { std::vector> expressionListOffsets; std::vector> expressionListTypes; - for (auto it = ast->expressionList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptExpression(it->value); + for (auto node : ListView{ast->expressionList}) { + if (!node) continue; + const auto [offset, type] = acceptExpression(node); expressionListOffsets.push_back(offset); expressionListTypes.push_back(type); } @@ -5672,9 +5672,9 @@ void ASTEncoder::visit(ParenMemInitializerAST* ast) { std::vector> expressionListOffsets; std::vector> expressionListTypes; - for (auto it = ast->expressionList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptExpression(it->value); + for (auto node : ListView{ast->expressionList}) { + if (!node) continue; + const auto [offset, type] = acceptExpression(node); expressionListOffsets.push_back(offset); expressionListTypes.push_back(type); } @@ -5886,9 +5886,9 @@ void ASTEncoder::visit(TypeExceptionDeclarationAST* ast) { std::vector> attributeListTypes; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptAttributeSpecifier(it->value); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + const auto [offset, type] = acceptAttributeSpecifier(node); attributeListOffsets.push_back(offset); attributeListTypes.push_back(type); } @@ -5899,9 +5899,9 @@ void ASTEncoder::visit(TypeExceptionDeclarationAST* ast) { std::vector> typeSpecifierListOffsets; std::vector> typeSpecifierListTypes; - for (auto it = ast->typeSpecifierList; it; it = it->next) { - if (!it->value) continue; - const auto [offset, type] = acceptSpecifier(it->value); + for (auto node : ListView{ast->typeSpecifierList}) { + if (!node) continue; + const auto [offset, type] = acceptSpecifier(node); typeSpecifierListOffsets.push_back(offset); typeSpecifierListTypes.push_back(type); } @@ -5931,9 +5931,9 @@ void ASTEncoder::visit(CxxAttributeAST* ast) { const auto attributeUsingPrefix = accept(ast->attributeUsingPrefix); std::vector> attributeListOffsets; - for (auto it = ast->attributeList; it; it = it->next) { - if (!it->value) continue; - attributeListOffsets.emplace_back(accept(it->value).o); + for (auto node : ListView{ast->attributeList}) { + if (!node) continue; + attributeListOffsets.emplace_back(accept(node).o); } auto attributeListOffsetsVector = fbb_.CreateVector(attributeListOffsets); diff --git a/src/parser/cxx/preprocessor.cc b/src/parser/cxx/preprocessor.cc index bb19e9c5..f7182bee 100644 --- a/src/parser/cxx/preprocessor.cc +++ b/src/parser/cxx/preprocessor.cc @@ -378,6 +378,12 @@ struct Tok final : Managed { return tk; } + [[nodiscard]] static auto Copy(Arena *pool, const Tok *tok) { + auto tk = new (pool) Tok(); + *tk = *tok; + return tk; + } + [[nodiscard]] static auto Gen(Arena *pool, TokenKind kind, const std::string_view &text, const Hideset *hideset = nullptr) -> Tok * { @@ -417,13 +423,12 @@ struct Tok final : Managed { struct TokList final : Managed { const Tok *tok = nullptr; - const TokList *next = nullptr; + TokList *next = nullptr; - explicit TokList(const Tok *tok, const TokList *next = nullptr) + explicit TokList(const Tok *tok, TokList *next = nullptr) : tok(tok), next(next) {} - [[nodiscard]] static auto isSame(const TokList *ls, const TokList *rs) - -> bool { + [[nodiscard]] static auto isSame(TokList *ls, TokList *rs) -> bool { if (ls == rs) return true; if (!ls || !rs) return false; if (ls->tok->kind != rs->tok->kind) return false; @@ -438,7 +443,7 @@ class TokIterator { using difference_type = std::ptrdiff_t; TokIterator() = default; - explicit TokIterator(const TokList *ts) : ts_(ts) {} + explicit TokIterator(TokList *ts) : ts_(ts) {} auto operator==(const TokIterator &other) const -> bool = default; @@ -457,10 +462,10 @@ class TokIterator { return it; } - [[nodiscard]] auto toTokList() const -> const TokList * { return ts_; } + [[nodiscard]] auto toTokList() const -> TokList * { return ts_; } private: - const TokList *ts_ = nullptr; + TokList *ts_ = nullptr; }; struct EndOfFileSentinel { @@ -522,20 +527,19 @@ inline constexpr LookAt lookat{}; struct ObjectMacro { std::string_view name; - const TokList *body = nullptr; + TokList *body = nullptr; - ObjectMacro(std::string_view name, const TokList *body) - : name(name), body(body) {} + ObjectMacro(std::string_view name, TokList *body) : name(name), body(body) {} }; struct FunctionMacro { std::string_view name; std::vector formals; - const TokList *body = nullptr; + TokList *body = nullptr; bool variadic = false; FunctionMacro(std::string_view name, std::vector formals, - const TokList *body, bool variadic) + TokList *body, bool variadic) : name(name), formals(std::move(formals)), body(body), @@ -543,26 +547,26 @@ struct FunctionMacro { }; struct MacroExpansionContext { - const TokList *ts = nullptr; + TokList *ts = nullptr; }; struct BuiltinObjectMacro { std::string_view name; - std::functionconst TokList *> expand; + std::functionTokList *> expand; BuiltinObjectMacro( std::string_view name, - std::functionconst TokList *> expand) + std::functionTokList *> expand) : name(name), expand(std::move(expand)) {} }; struct BuiltinFunctionMacro { std::string_view name; - std::functionconst TokList *> expand; + std::functionTokList *> expand; BuiltinFunctionMacro( std::string_view name, - std::functionconst TokList *> expand) + std::functionTokList *> expand) : name(name), expand(std::move(expand)) {} }; @@ -592,22 +596,21 @@ using Macro = std::variant const TokList * { +[[nodiscard]] inline auto getMacroBody(const Macro ¯o) -> TokList * { struct { - auto operator()(const ObjectMacro ¯o) const -> const TokList * { + auto operator()(const ObjectMacro ¯o) const -> TokList * { return macro.body; } - auto operator()(const FunctionMacro ¯o) const -> const TokList * { + auto operator()(const FunctionMacro ¯o) const -> TokList * { return macro.body; } - auto operator()(const BuiltinObjectMacro ¯o) const -> const TokList * { + auto operator()(const BuiltinObjectMacro ¯o) const -> TokList * { return nullptr; } - auto operator()(const BuiltinFunctionMacro ¯o) const - -> const TokList * { + auto operator()(const BuiltinFunctionMacro ¯o) const -> TokList * { return nullptr; } } visitor; @@ -645,8 +648,8 @@ struct SourceFile { std::string fileName; std::string source; std::vector lines; - const TokList *tokens = nullptr; - const TokList *headerProtection = nullptr; + TokList *tokens = nullptr; + TokList *headerProtection = nullptr; int headerProtectionLevel = 0; int id = 0; bool pragmaOnceProtected = false; @@ -707,7 +710,7 @@ struct Preprocessor::Private { struct Buffer { SourceFile *source = nullptr; fs::path currentPath; - const TokList *ts = nullptr; + TokList *ts = nullptr; int includeDepth = 0; }; @@ -743,7 +746,7 @@ struct Preprocessor::Private { void initialize(); - void error(const TokList *ts, std::string message) const { + void error(TokList *ts, std::string message) const { if (!ts || !ts->tok) { cxx_runtime_error(std::format("no source location: {}", message)); } else { @@ -752,7 +755,7 @@ struct Preprocessor::Private { } } - void warning(const TokList *ts, std::string message) const { + void warning(TokList *ts, std::string message) const { if (!ts || !ts->tok) { cxx_runtime_error(std::format("no source location: {}", message)); } else { @@ -773,45 +776,44 @@ struct Preprocessor::Private { return std::tuple(skipping_.back(), evaluating_.back()); } - [[nodiscard]] auto clone(const TokList *ts) -> const TokList * { + [[nodiscard]] auto clone(TokList *ts) -> TokList * { if (!ts) return nullptr; return cons(ts->tok, clone(ts->next)); } - [[nodiscard]] auto cons(const Tok *tok, const TokList *next = nullptr) + [[nodiscard]] auto cons(const Tok *tok, TokList *next = nullptr) -> TokList * { return new (&pool_) TokList(tok, next); } - [[nodiscard]] auto snoc(const TokList *first, const TokList *second) - -> const TokList * { + [[nodiscard]] auto snoc(TokList *first, TokList *second) -> TokList * { if (!first) return second; if (!second) return first; - const TokList *tail = first; + TokList *tail = first; while (tail->next) { tail = tail->next; } - const_cast(tail)->next = second; + (tail)->next = second; return first; } template S> - [[nodiscard]] auto toTokList(I first, S last) -> const TokList * { + [[nodiscard]] auto toTokList(I first, S last) -> TokList * { TokList *list = nullptr; TokList **it = &list; for (; first != last; ++first) { *it = cons(&*first); - it = const_cast(&(*it)->next); + it = (&(*it)->next); } return list; } template - [[nodiscard]] auto toTokList(const R &range) -> const TokList * { + [[nodiscard]] auto toTokList(const R &range) -> TokList * { return toTokList(std::ranges::begin(range), std::ranges::end(range)); } @@ -830,6 +832,11 @@ struct Preprocessor::Private { return Tok::Gen(&pool_, kind, text, hideset); } + [[nodiscard]] auto copy(const Tok *tok) -> Tok * { + auto copy = Tok::Copy(&pool_, tok); + return copy; + } + void pushState(std::tuple state) { auto [skipping, evaluating] = state; skipping_.push_back(skipping); @@ -874,15 +881,15 @@ struct Preprocessor::Private { return sourceFile; } - [[nodiscard]] auto bol(const TokList *ts) const -> bool { + [[nodiscard]] auto bol(TokList *ts) const -> bool { return ts && ts->tok->bol; } - [[nodiscard]] auto lookat(const TokList *ts, auto... tokens) const -> bool { + [[nodiscard]] auto lookat(TokList *ts, auto... tokens) const -> bool { return cxx::lookat(TokIterator{ts}, EndOfFileSentinel{}, tokens...); } - [[nodiscard]] auto match(const TokList *&ts, TokenKind k) const -> bool { + [[nodiscard]] auto match(TokList *&ts, TokenKind k) const -> bool { if (lookat(ts, k)) { ts = ts->next; return true; @@ -898,8 +905,8 @@ struct Preprocessor::Private { return true; } - [[nodiscard]] auto matchId(const TokList *&ts, - const std::string_view &s) const -> bool { + [[nodiscard]] auto matchId(TokList *&ts, const std::string_view &s) const + -> bool { if (lookat(ts, TokenKind::T_IDENTIFIER) && ts->tok->text == s) { ts = ts->next; return true; @@ -907,13 +914,13 @@ struct Preprocessor::Private { return false; } - void expect(const TokList *&ts, TokenKind k) const { + void expect(TokList *&ts, TokenKind k) const { if (!match(ts, k)) { error(ts, std::format("expected '{}'", Token::spell(k))); } } - [[nodiscard]] auto expectId(const TokList *&ts) const -> std::string_view { + [[nodiscard]] auto expectId(TokList *&ts) const -> std::string_view { if (lookat(ts, TokenKind::T_IDENTIFIER)) { auto id = ts->tok->text; ts = ts->next; @@ -1030,10 +1037,9 @@ struct Preprocessor::Private { return out.str(); } - [[nodiscard]] auto checkHeaderProtection(const TokList *ts) const - -> const TokList *; + [[nodiscard]] auto checkHeaderProtection(TokList *ts) const -> TokList *; - [[nodiscard]] auto checkPragmaOnceProtected(const TokList *ts) const -> bool; + [[nodiscard]] auto checkPragmaOnceProtected(TokList *ts) const -> bool; [[nodiscard]] auto resolve(const Include &include, bool next) const -> std::optional { @@ -1103,27 +1109,27 @@ struct Preprocessor::Private { return tok->is(TokenKind::T_IDENTIFIER) && isDefined(tok->text); } - void defineMacro(const TokList *ts); + void defineMacro(TokList *ts); void adddBuiltinMacro( std::string_view name, - std::functionconst TokList *> expand) { + std::functionTokList *> expand) { macros_.insert_or_assign(name, BuiltinObjectMacro(name, std::move(expand))); } void adddBuiltinFunctionMacro( std::string_view name, - std::functionconst TokList *> expand) { + std::functionTokList *> expand) { macros_.insert_or_assign(name, BuiltinFunctionMacro(name, std::move(expand))); } [[nodiscard]] auto tokenize(const std::string_view &source, int sourceFile, - bool bol) -> const TokList *; + bool bol) -> TokList *; [[nodiscard]] auto skipLine(TokIterator it, TokIterator last) -> TokIterator; - [[nodiscard]] auto parseMacroDefinition(const TokList *ts) -> Macro; + [[nodiscard]] auto parseMacroDefinition(TokList *ts) -> Macro; [[nodiscard]] auto expand(const std::function &emitToken) -> Status; @@ -1136,75 +1142,72 @@ struct Preprocessor::Private { const std::function &emitToken) -> TokIterator; [[nodiscard]] auto replaceIsDefinedMacro( - const TokList *ts, bool inConditionalExpression, - const std::function &emitToken) -> const TokList *; + TokList *ts, bool inConditionalExpression, + const std::function &emitToken) -> TokList *; - [[nodiscard]] auto expandMacro(const TokList *ts) -> const TokList *; + [[nodiscard]] auto expandMacro(TokList *ts) -> TokList *; - [[nodiscard]] auto expandObjectLikeMacro(const TokList *ts, - const Macro *macro) - -> const TokList *; + [[nodiscard]] auto expandObjectLikeMacro(TokList *ts, const Macro *macro) + -> TokList *; - [[nodiscard]] auto expandFunctionLikeMacro(const TokList *ts, - const Macro *macro) - -> const TokList *; + [[nodiscard]] auto expandFunctionLikeMacro(TokList *ts, const Macro *macro) + -> TokList *; struct ParsedIncludeDirective { Include header; bool includeNext = false; - const TokList *loc = nullptr; + TokList *loc = nullptr; }; - [[nodiscard]] auto parseDirective(SourceFile *source, const TokList *start) + [[nodiscard]] auto parseDirective(SourceFile *source, TokList *start) -> std::optional; - [[nodiscard]] auto parseIncludeDirective(const TokList *directive, - const TokList *ts) + [[nodiscard]] auto parseIncludeDirective(TokList *directive, TokList *ts) -> std::optional; [[nodiscard]] auto resolveIncludeDirective( const ParsedIncludeDirective &directive) -> SourceFile *; - [[nodiscard]] auto parseHeaderName(const TokList *ts) - -> std::tuple>; + [[nodiscard]] auto parseHeaderName(TokList *ts) + -> std::tuple>; - [[nodiscard]] auto substitute(const Macro *macro, + [[nodiscard]] auto substitute(TokList *pointOfSubstitution, + const Macro *macro, const std::vector &actuals, - const Hideset *hideset) -> const TokList *; + const Hideset *hideset) -> TokList *; [[nodiscard]] auto merge(const Tok *left, const Tok *right) -> const Tok *; - [[nodiscard]] auto stringize(const TokList *ts) -> const Tok *; + [[nodiscard]] auto stringize(TokList *ts) -> const Tok *; - [[nodiscard]] auto instantiate(const TokList *ts, const Hideset *hideset) - -> const TokList *; + [[nodiscard]] auto instantiate(TokList *ts, const Hideset *hideset) + -> TokList *; [[nodiscard]] auto lookupMacro(const Tok *tk) const -> const Macro *; - [[nodiscard]] auto lookupMacroArgument(const TokList *&ts, const Macro *macro, + [[nodiscard]] auto lookupMacroArgument(TokList *&ts, const Macro *macro, const std::vector &actuals) -> std::optional; - [[nodiscard]] auto copyTokens(const TokList *ts) -> const TokList *; - [[nodiscard]] auto copyLine(const TokList *ts) -> const TokList *; + [[nodiscard]] auto copyLine(TokList *ts) -> TokList *; - [[nodiscard]] auto constantExpression(const TokList *ts) -> long; - [[nodiscard]] auto conditionalExpression(const TokList *&ts) -> long; - [[nodiscard]] auto binaryExpression(const TokList *&ts) -> long; - [[nodiscard]] auto binaryExpressionHelper(const TokList *&ts, long lhs, - int minPrec) -> long; - [[nodiscard]] auto unaryExpression(const TokList *&ts) -> long; - [[nodiscard]] auto primaryExpression(const TokList *&ts) -> long; + [[nodiscard]] auto constantExpression(TokList *ts) -> long; + [[nodiscard]] auto conditionalExpression(TokList *&ts) -> long; + [[nodiscard]] auto binaryExpression(TokList *&ts) -> long; + [[nodiscard]] auto binaryExpressionHelper(TokList *&ts, long lhs, int minPrec) + -> long; + [[nodiscard]] auto unaryExpression(TokList *&ts) -> long; + [[nodiscard]] auto primaryExpression(TokList *&ts) -> long; - [[nodiscard]] auto parseArguments(const TokList *ts, int formalCount, + [[nodiscard]] auto parseArguments(TokList *ts, int formalCount, bool ignoreComma = false) - -> std::tuple, const TokList *, const Hideset *>; + -> std::tuple, TokList *, const Hideset *>; [[nodiscard]] auto string(std::string s) -> std::string_view; - void print(const TokList *ts, std::ostream &out) const; + void print(TokList *ts, std::ostream &out) const; - void printLine(const TokList *ts, std::ostream &out, bool nl = true) const; + void printLine(TokList *ts, std::ostream &out, bool nl = true) const; void finalizeToken(std::vector &tokens, const Tok *tk); }; @@ -1299,8 +1302,7 @@ void Preprocessor::Private::initialize() { // add built-in object-like macros adddBuiltinMacro( - "__FILE__", - [this](const MacroExpansionContext &context) -> const TokList * { + "__FILE__", [this](const MacroExpansionContext &context) -> TokList * { auto ts = context.ts; auto tk = gen(TokenKind::T_STRING_LITERAL, string(std::format("\"{}\"", currentFileName_))); @@ -1310,8 +1312,7 @@ void Preprocessor::Private::initialize() { }); adddBuiltinMacro( - "__LINE__", - [this](const MacroExpansionContext &context) -> const TokList * { + "__LINE__", [this](const MacroExpansionContext &context) -> TokList * { auto ts = context.ts; const auto start = preprocessor_->tokenStartPosition(ts->tok->token()); auto tk = gen(TokenKind::T_INTEGER_LITERAL, @@ -1321,40 +1322,37 @@ void Preprocessor::Private::initialize() { return cons(tk, ts->next); }); - adddBuiltinMacro( - "__COUNTER__", - [this](const MacroExpansionContext &context) -> const TokList * { - auto tk = gen(TokenKind::T_INTEGER_LITERAL, - string(std::to_string(counter_++))); - tk->sourceFile = context.ts->tok->sourceFile; - tk->space = true; - return cons(tk, context.ts->next); - }); - - adddBuiltinMacro( - "__DATE__", - [this](const MacroExpansionContext &context) -> const TokList * { - auto ts = context.ts; - auto tk = gen(TokenKind::T_STRING_LITERAL, date_); - tk->sourceFile = ts->tok->sourceFile; - tk->space = true; - return cons(tk, ts->next); - }); - - adddBuiltinMacro( - "__TIME__", - [this](const MacroExpansionContext &context) -> const TokList * { - auto ts = context.ts; - auto tk = gen(TokenKind::T_STRING_LITERAL, time_); - tk->sourceFile = ts->tok->sourceFile; - tk->space = true; - return cons(tk, ts->next); - }); + adddBuiltinMacro("__COUNTER__", + [this](const MacroExpansionContext &context) -> TokList * { + auto tk = gen(TokenKind::T_INTEGER_LITERAL, + string(std::to_string(counter_++))); + tk->sourceFile = context.ts->tok->sourceFile; + tk->space = true; + return cons(tk, context.ts->next); + }); + + adddBuiltinMacro("__DATE__", + [this](const MacroExpansionContext &context) -> TokList * { + auto ts = context.ts; + auto tk = gen(TokenKind::T_STRING_LITERAL, date_); + tk->sourceFile = ts->tok->sourceFile; + tk->space = true; + return cons(tk, ts->next); + }); + + adddBuiltinMacro("__TIME__", + [this](const MacroExpansionContext &context) -> TokList * { + auto ts = context.ts; + auto tk = gen(TokenKind::T_STRING_LITERAL, time_); + tk->sourceFile = ts->tok->sourceFile; + tk->space = true; + return cons(tk, ts->next); + }); // add built-in function-like macros auto replaceWithBoolLiteral = [this](const Tok *token, bool value, - const TokList *ts) { + TokList *ts) { auto tk = gen(TokenKind::T_INTEGER_LITERAL, value ? "1" : "0"); tk->sourceFile = token->sourceFile; tk->space = token->space; @@ -1365,7 +1363,7 @@ void Preprocessor::Private::initialize() { adddBuiltinFunctionMacro( "__has_feature", [this, replaceWithBoolLiteral]( - const MacroExpansionContext &context) -> const TokList * { + const MacroExpansionContext &context) -> TokList * { auto ts = context.ts; auto macroId = ts->tok; ts = ts->next; @@ -1379,7 +1377,7 @@ void Preprocessor::Private::initialize() { adddBuiltinFunctionMacro( "__has_builtin", [this, replaceWithBoolLiteral]( - const MacroExpansionContext &context) -> const TokList * { + const MacroExpansionContext &context) -> TokList * { auto ts = context.ts; auto macroId = ts->tok; ts = ts->next; @@ -1393,7 +1391,7 @@ void Preprocessor::Private::initialize() { adddBuiltinFunctionMacro( "__has_extension", [this, replaceWithBoolLiteral]( - const MacroExpansionContext &context) -> const TokList * { + const MacroExpansionContext &context) -> TokList * { auto ts = context.ts; auto macroId = ts->tok; ts = ts->next; @@ -1407,7 +1405,7 @@ void Preprocessor::Private::initialize() { adddBuiltinFunctionMacro( "__has_attribute", [this, replaceWithBoolLiteral]( - const MacroExpansionContext &context) -> const TokList * { + const MacroExpansionContext &context) -> TokList * { auto ts = context.ts; auto macroId = ts->tok; ts = ts->next; @@ -1418,9 +1416,8 @@ void Preprocessor::Private::initialize() { return replaceWithBoolLiteral(macroId, enabled, ts); }); - auto hasInclude = - [this, replaceWithBoolLiteral]( - const MacroExpansionContext &context) -> const TokList * { + auto hasInclude = [this, replaceWithBoolLiteral]( + const MacroExpansionContext &context) -> TokList * { auto ts = context.ts; const auto macroName = ts->tok; @@ -1567,12 +1564,11 @@ void Preprocessor::Private::finalizeToken(std::vector &tokens, }; auto Preprocessor::Private::tokenize(const std::string_view &source, - int sourceFile, bool bol) - -> const TokList * { + int sourceFile, bool bol) -> TokList * { cxx::Lexer lex(source); lex.setKeepComments(true); lex.setPreprocessing(true); - const TokList *ts = nullptr; + TokList *ts = nullptr; auto it = &ts; do { lex(); @@ -1602,7 +1598,7 @@ auto Preprocessor::Private::tokenize(const std::string_view &source, auto tk = fromCurrentToken(lex, sourceFile); if (!lex.tokenIsClean()) tk->text = string(std::move(lex.text())); *it = cons(tk); - it = const_cast(&(*it)->next); + it = (&(*it)->next); } while (lex.tokenKind() != cxx::TokenKind::T_EOF_SYMBOL); return ts; } @@ -1619,7 +1615,7 @@ auto Preprocessor::Private::expandTokens(TokIterator it, TokIterator last, } it = expandOne(it, last, inConditionalExpression, [&](auto tok) { *out = cons(tok); - out = const_cast(&(*out)->next); + out = (&(*out)->next); }); } @@ -1689,8 +1685,7 @@ auto Preprocessor::Private::expand( return CanContinue{}; } -auto Preprocessor::Private::parseDirective(SourceFile *source, - const TokList *start) +auto Preprocessor::Private::parseDirective(SourceFile *source, TokList *start) -> std::optional { auto directive = start->next; @@ -1699,7 +1694,7 @@ auto Preprocessor::Private::parseDirective(SourceFile *source, const auto directiveKind = classifyDirective(directive->tok->text.data(), directive->tok->text.length()); - const TokList *ts = directive->next; + TokList *ts = directive->next; const auto [skipping, evaluating] = state(); @@ -1884,8 +1879,8 @@ auto Preprocessor::Private::parseDirective(SourceFile *source, return std::nullopt; } -auto Preprocessor::Private::parseIncludeDirective(const TokList *directive, - const TokList *ts) +auto Preprocessor::Private::parseIncludeDirective(TokList *directive, + TokList *ts) -> std::optional { if (lookat(ts, TokenKind::T_IDENTIFIER)) { auto eol = skipLine(TokIterator{ts}, TokIterator{}); @@ -1956,8 +1951,8 @@ auto Preprocessor::Private::resolveIncludeDirective( return sourceFile; } -auto Preprocessor::Private::parseHeaderName(const TokList *ts) - -> std::tuple> { +auto Preprocessor::Private::parseHeaderName(TokList *ts) + -> std::tuple> { if (lookat(ts, TokenKind::T_STRING_LITERAL)) { auto file = ts->tok->text.substr(1, ts->tok->text.length() - 2); Include headerFile = QuoteInclude(std::string(file)); @@ -2000,8 +1995,8 @@ auto Preprocessor::Private::expandOne( } auto Preprocessor::Private::replaceIsDefinedMacro( - const TokList *ts, bool inConditionalExpression, - const std::function &emitToken) -> const TokList * { + TokList *ts, bool inConditionalExpression, + const std::function &emitToken) -> TokList * { if (!inConditionalExpression) { return nullptr; } @@ -2032,27 +2027,27 @@ auto Preprocessor::Private::replaceIsDefinedMacro( return ts; } -auto Preprocessor::Private::expandMacro(const TokList *ts) -> const TokList * { +auto Preprocessor::Private::expandMacro(TokList *ts) -> TokList * { struct ExpandMacro { Private &self; - const TokList *ts = nullptr; + TokList *ts = nullptr; const Macro *macro = nullptr; - auto operator()(const ObjectMacro &) -> const TokList * { + auto operator()(const ObjectMacro &) -> TokList * { return self.expandObjectLikeMacro(ts, macro); } - auto operator()(const FunctionMacro &) -> const TokList * { + auto operator()(const FunctionMacro &) -> TokList * { if (!self.lookat(ts->next, TokenKind::T_LPAREN)) return nullptr; return self.expandFunctionLikeMacro(ts, macro); } - auto operator()(const BuiltinObjectMacro ¯o) -> const TokList * { + auto operator()(const BuiltinObjectMacro ¯o) -> TokList * { return macro.expand(MacroExpansionContext{.ts = ts}); } - auto operator()(const BuiltinFunctionMacro ¯o) -> const TokList * { + auto operator()(const BuiltinFunctionMacro ¯o) -> TokList * { if (!self.lookat(ts->next, TokenKind::T_LPAREN)) return nullptr; return macro.expand(MacroExpansionContext{.ts = ts}); @@ -2067,14 +2062,13 @@ auto Preprocessor::Private::expandMacro(const TokList *ts) -> const TokList * { return nullptr; } -auto Preprocessor::Private::expandObjectLikeMacro(const TokList *ts, - const Macro *m) - -> const TokList * { +auto Preprocessor::Private::expandObjectLikeMacro(TokList *ts, const Macro *m) + -> TokList * { auto macro = &std::get(*m); const Tok *tk = ts->tok; const auto hideset = makeUnion(tk->hideset, tk->text); - auto expanded = substitute(m, {}, hideset); + auto expanded = substitute(ts, m, {}, hideset); if (!expanded) { return ts->next; @@ -2087,14 +2081,13 @@ auto Preprocessor::Private::expandObjectLikeMacro(const TokList *ts, auto it = expanded; while (it->next) it = it->next; - const_cast(it)->next = ts->next; + (it)->next = ts->next; return expanded; } -auto Preprocessor::Private::expandFunctionLikeMacro(const TokList *ts, - const Macro *m) - -> const TokList * { +auto Preprocessor::Private::expandFunctionLikeMacro(TokList *ts, const Macro *m) + -> TokList * { auto macro = &std::get(*m); assert(lookat(ts->next, TokenKind::T_LPAREN)); @@ -2106,7 +2099,7 @@ auto Preprocessor::Private::expandFunctionLikeMacro(const TokList *ts, auto hs = makeUnion(makeIntersection(tk->hideset, hideset), tk->text); - auto expanded = substitute(m, args, hs); + auto expanded = substitute(ts, m, args, hs); if (!expanded) { return rest; @@ -2119,30 +2112,53 @@ auto Preprocessor::Private::expandFunctionLikeMacro(const TokList *ts, auto it = expanded; while (it->next) it = it->next; - const_cast(it)->next = rest; + (it)->next = rest; return expanded; } -auto Preprocessor::Private::substitute(const Macro *macro, +auto Preprocessor::Private::substitute(TokList *pointOfSubstitution, + const Macro *macro, const std::vector &actuals, - const Hideset *hideset) - -> const TokList * { - const TokList *os = nullptr; - auto **ip = const_cast(&os); + const Hideset *hideset) -> TokList * { + TokList *os = nullptr; + auto **ip = (&os); - auto appendTokens = [&](const TokList *rs) { + std::unordered_set macroTokens; + + auto appendTokens = [&](TokList *rs) { if (!*ip) { - *ip = const_cast(rs); + *ip = (rs); + } else { + (*ip)->next = (rs); + } + while (*ip && (*ip)->next) ip = (&(*ip)->next); + }; + + auto appendToken = [&](const Tok *tk) { + if (macroTokens.contains(tk)) { + // todo: make a copy of tk and override its location to be the same as + // the point of substitution. + auto copyTk = copy(tk); + copyTk->sourceFile = pointOfSubstitution->tok->sourceFile; + copyTk->offset = pointOfSubstitution->tok->offset; + copyTk->length = pointOfSubstitution->tok->length; + appendTokens(cons(copyTk)); } else { - (*ip)->next = const_cast(rs); + appendTokens(cons(tk)); } - while (*ip && (*ip)->next) ip = const_cast(&(*ip)->next); }; - auto appendToken = [&](const Tok *tk) { appendTokens(cons(tk)); }; + TokList *macroBody = getMacroBody(*macro); - const TokList *ts = getMacroBody(*macro); + for (auto it = macroBody; it; it = it->next) { + macroTokens.insert(it->tok); + } + + // std::cerr << std::format("macro body has {} tokens\n", macroTokens.size()); + + // set the token stream to the macro body + TokList *ts = macroBody; while (ts && !lookat(ts, TokenKind::T_EOF_SYMBOL)) { if (lookat(ts, TokenKind::T_HASH, TokenKind::T_IDENTIFIER)) { @@ -2198,12 +2214,10 @@ auto Preprocessor::Private::substitute(const Macro *macro, } if (auto actual = lookupMacroArgument(ts, macro, actuals)) { - // auto copy = copyTokens(arg); auto [startArg, endArg] = *actual; auto line = expandTokens(startArg, endArg, /*expression*/ false); if (line != TokIterator{}) { - // const_cast(line->tok)->space = true; appendTokens(line.toTokList()); } continue; @@ -2217,7 +2231,7 @@ auto Preprocessor::Private::substitute(const Macro *macro, } auto Preprocessor::Private::lookupMacroArgument( - const TokList *&ts, const Macro *m, const std::vector &actuals) + TokList *&ts, const Macro *m, const std::vector &actuals) -> std::optional { if (!isFunctionLikeMacro(*m)) return std::nullopt; @@ -2270,7 +2284,7 @@ auto Preprocessor::Private::lookupMacroArgument( return std::nullopt; } -auto Preprocessor::Private::checkPragmaOnceProtected(const TokList *ts) const +auto Preprocessor::Private::checkPragmaOnceProtected(TokList *ts) const -> bool { if (!ts) return false; if (!match(ts, TokenKind::T_HASH)) return false; @@ -2279,12 +2293,12 @@ auto Preprocessor::Private::checkPragmaOnceProtected(const TokList *ts) const return true; } -auto Preprocessor::Private::checkHeaderProtection(const TokList *ts) const - -> const TokList * { +auto Preprocessor::Private::checkHeaderProtection(TokList *ts) const + -> TokList * { if (!ts) return nullptr; if (!match(ts, TokenKind::T_HASH)) return nullptr; if (bol(ts) || !matchId(ts, "ifndef")) return nullptr; - const TokList *prot = ts; + TokList *prot = ts; if (bol(ts) || !match(ts, TokenKind::T_IDENTIFIER)) return nullptr; if (!bol(ts) || !match(ts, TokenKind::T_HASH)) return nullptr; if (bol(ts) || !matchId(ts, "define")) return nullptr; @@ -2292,26 +2306,7 @@ auto Preprocessor::Private::checkHeaderProtection(const TokList *ts) const return prot; } -auto Preprocessor::Private::copyTokens(const TokList *ts) -> const TokList * { - if (!ts) return nullptr; - - TokList *line = nullptr; - auto it = &line; - auto lastTok = ts->tok; - while (ts && !lookat(ts, TokenKind::T_EOF_SYMBOL)) { - *it = cons(ts->tok); - lastTok = ts->tok; - it = const_cast(&(*it)->next); - ts = ts->next; - } - auto eol = gen(TokenKind::T_EOF_SYMBOL, std::string_view()); - eol->sourceFile = lastTok->sourceFile; - eol->offset = lastTok->offset + lastTok->length; - *it = cons(eol); - return line; -} - -auto Preprocessor::Private::copyLine(const TokList *ts) -> const TokList * { +auto Preprocessor::Private::copyLine(TokList *ts) -> TokList * { assert(ts); TokList *line = nullptr; auto it = &line; @@ -2320,7 +2315,7 @@ auto Preprocessor::Private::copyLine(const TokList *ts) -> const TokList * { ts = ts->next) { *it = cons(ts->tok); lastTok = ts->tok; - it = const_cast(&(*it)->next); + it = (&(*it)->next); } auto eol = gen(TokenKind::T_EOF_SYMBOL, std::string_view()); eol->sourceFile = lastTok->sourceFile; @@ -2329,7 +2324,7 @@ auto Preprocessor::Private::copyLine(const TokList *ts) -> const TokList * { return line; } -auto Preprocessor::Private::constantExpression(const TokList *ts) -> long { +auto Preprocessor::Private::constantExpression(TokList *ts) -> long { auto line = copyLine(ts); auto it = expandTokens(TokIterator{line}, TokIterator{}, /*inConditionalExpression*/ true); @@ -2337,7 +2332,7 @@ auto Preprocessor::Private::constantExpression(const TokList *ts) -> long { return conditionalExpression(e); } -auto Preprocessor::Private::conditionalExpression(const TokList *&ts) -> long { +auto Preprocessor::Private::conditionalExpression(TokList *&ts) -> long { if (!ts) return 0; const auto value = binaryExpression(ts); if (!match(ts, TokenKind::T_QUESTION)) return value; @@ -2347,7 +2342,7 @@ auto Preprocessor::Private::conditionalExpression(const TokList *&ts) -> long { return value ? iftrue : iffalse; } -static auto prec(const TokList *ts) -> int { +static auto prec(TokList *ts) -> int { enum Prec { kLogicalOr, kLogicalAnd, @@ -2407,12 +2402,12 @@ static auto prec(const TokList *ts) -> int { } // switch } -auto Preprocessor::Private::binaryExpression(const TokList *&ts) -> long { +auto Preprocessor::Private::binaryExpression(TokList *&ts) -> long { auto e = unaryExpression(ts); return binaryExpressionHelper(ts, e, 0); } -auto Preprocessor::Private::binaryExpressionHelper(const TokList *&ts, long lhs, +auto Preprocessor::Private::binaryExpressionHelper(TokList *&ts, long lhs, int minPrec) -> long { while (prec(ts) >= minPrec) { const auto p = prec(ts); @@ -2485,7 +2480,7 @@ auto Preprocessor::Private::binaryExpressionHelper(const TokList *&ts, long lhs, return lhs; } -auto Preprocessor::Private::unaryExpression(const TokList *&ts) -> long { +auto Preprocessor::Private::unaryExpression(TokList *&ts) -> long { if (match(ts, TokenKind::T_MINUS)) { return -unaryExpression(ts); } @@ -2501,7 +2496,7 @@ auto Preprocessor::Private::unaryExpression(const TokList *&ts) -> long { return primaryExpression(ts); } -auto Preprocessor::Private::primaryExpression(const TokList *&ts) -> long { +auto Preprocessor::Private::primaryExpression(TokList *&ts) -> long { const auto tk = ts->tok; if (match(ts, TokenKind::T_INTEGER_LITERAL)) { @@ -2520,20 +2515,19 @@ auto Preprocessor::Private::primaryExpression(const TokList *&ts) -> long { return 0; } -auto Preprocessor::Private::instantiate(const TokList *ts, - const Hideset *hideset) - -> const TokList * { +auto Preprocessor::Private::instantiate(TokList *ts, const Hideset *hideset) + -> TokList * { for (auto ip = ts; ip; ip = ip->next) { if (ip->tok->hideset != hideset) { - const_cast(ip)->tok = withHideset(ip->tok, hideset); + (ip)->tok = withHideset(ip->tok, hideset); } } return ts; } -auto Preprocessor::Private::parseArguments(const TokList *ts, int formalCount, +auto Preprocessor::Private::parseArguments(TokList *ts, int formalCount, bool ignoreComma) - -> std::tuple, const TokList *, const Hideset *> { + -> std::tuple, TokList *, const Hideset *> { assert(lookat(ts, TokenKind::T_IDENTIFIER, TokenKind::T_LPAREN)); auto parsedArgs = ParseArguments{*this}( @@ -2547,7 +2541,7 @@ auto Preprocessor::Private::parseArguments(const TokList *ts, int formalCount, parsedArgs->hideset); } -auto Preprocessor::Private::stringize(const TokList *ts) -> const Tok * { +auto Preprocessor::Private::stringize(TokList *ts) -> const Tok * { std::string s; const auto start = ts; @@ -2584,7 +2578,7 @@ auto Preprocessor::Private::string(std::string s) -> std::string_view { return std::string_view(scratchBuffer_.emplace_front(std::move(s))); } -auto Preprocessor::Private::parseMacroDefinition(const TokList *ts) -> Macro { +auto Preprocessor::Private::parseMacroDefinition(TokList *ts) -> Macro { const auto name = ts->tok->text; ts = ts->next; @@ -2617,7 +2611,7 @@ auto Preprocessor::Private::parseMacroDefinition(const TokList *ts) -> Macro { return ObjectMacro(name, ts); } -void Preprocessor::Private::defineMacro(const TokList *ts) { +void Preprocessor::Private::defineMacro(TokList *ts) { #if 0 std::cout << std::format("*** defining macro: "); printLine(ts, std::cout); @@ -2705,7 +2699,7 @@ static auto needSpace(const Tok *prev, const Tok *current) -> bool { return wantSpace(prev->kind) && wantSpace(current->kind); } -void Preprocessor::Private::print(const TokList *ts, std::ostream &out) const { +void Preprocessor::Private::print(TokList *ts, std::ostream &out) const { bool first = true; for (const Tok *prevTk = nullptr; ts; ts = ts->next) { auto tk = ts->tok; @@ -2721,7 +2715,7 @@ void Preprocessor::Private::print(const TokList *ts, std::ostream &out) const { } } -void Preprocessor::Private::printLine(const TokList *ts, std::ostream &out, +void Preprocessor::Private::printLine(TokList *ts, std::ostream &out, bool nl) const { bool first = true; for (const Tok *prevTk = nullptr; ts; ts = ts->next) { @@ -2833,7 +2827,7 @@ void Preprocessor::preprocess(std::string source, std::string fileName, Private::ParsedIncludeDirective parsedInclude{ .header = header, .includeNext = status.isIncludeNext, - .loc = static_cast(status.loc), + .loc = static_cast(const_cast(status.loc)), }; auto continuation = self.d->resolveIncludeDirective(parsedInclude); diff --git a/tests/unit_tests/preprocessor/define.001.cc b/tests/unit_tests/preprocessor/define.001.cc index 35b4f207..4168ae71 100644 --- a/tests/unit_tests/preprocessor/define.001.cc +++ b/tests/unit_tests/preprocessor/define.001.cc @@ -7,4 +7,4 @@ int main() { return result; } -// CHECK: int result = 0; \ No newline at end of file +// CHECK: int result = 0 ; \ No newline at end of file diff --git a/tests/unit_tests/preprocessor/define.002.cc b/tests/unit_tests/preprocessor/define.002.cc index cd7faa14..6ba14513 100644 --- a/tests/unit_tests/preprocessor/define.002.cc +++ b/tests/unit_tests/preprocessor/define.002.cc @@ -9,4 +9,4 @@ int main() { return result; } -// CHECK: int result = 1; \ No newline at end of file +// CHECK: int result = 1 ; \ No newline at end of file diff --git a/tests/unit_tests/preprocessor/define.003.cc b/tests/unit_tests/preprocessor/define.003.cc index fd7d5207..5f576c2c 100644 --- a/tests/unit_tests/preprocessor/define.003.cc +++ b/tests/unit_tests/preprocessor/define.003.cc @@ -9,4 +9,4 @@ int main() { FOR_EACH(DECLARE_VAR) } -// CHECK: int aa;int bb;int cc; +// CHECK: int aa ; int bb ; int cc ; diff --git a/tests/unit_tests/preprocessor/warning.001.cc b/tests/unit_tests/preprocessor/warning.001.cc index 703641c5..4389d068 100644 --- a/tests/unit_tests/preprocessor/warning.001.cc +++ b/tests/unit_tests/preprocessor/warning.001.cc @@ -10,4 +10,4 @@ const char* platform = PLATFORM; #endif -// CHECK: const char* platform = "linux"; +// CHECK: const char* platform = "linux" ;