diff --git a/crates/oxc_ast/src/ast_kind.rs b/crates/oxc_ast/src/ast_kind_impl.rs similarity index 53% rename from crates/oxc_ast/src/ast_kind.rs rename to crates/oxc_ast/src/ast_kind_impl.rs index 84ae388d207ef..c02deb6f1bcd3 100644 --- a/crates/oxc_ast/src/ast_kind.rs +++ b/crates/oxc_ast/src/ast_kind_impl.rs @@ -1,224 +1,6 @@ -use oxc_span::{Atom, GetSpan, Span}; +use oxc_span::Atom; -#[allow(clippy::wildcard_imports)] -use crate::ast::*; - -macro_rules! ast_kinds { - { $($ident:ident($type:ty),)* } => ( - #[derive(Debug, Clone, Copy)] - pub enum AstType { - $($ident,)* - } - - /// Untyped AST Node Kind - #[derive(Debug, Clone, Copy)] - pub enum AstKind<'a> { - $($ident($type),)* - } - ) -} - -ast_kinds! { - Program(&'a Program<'a>), - Directive(&'a Directive<'a>), - Hashbang(&'a Hashbang<'a>), - - BlockStatement(&'a BlockStatement<'a>), - BreakStatement(&'a BreakStatement<'a>), - ContinueStatement(&'a ContinueStatement<'a>), - DebuggerStatement(&'a DebuggerStatement), - DoWhileStatement(&'a DoWhileStatement<'a>), - EmptyStatement(&'a EmptyStatement), - ExpressionStatement(&'a ExpressionStatement<'a>), - ForInStatement(&'a ForInStatement<'a>), - ForOfStatement(&'a ForOfStatement<'a>), - ForStatement(&'a ForStatement<'a>), - ForStatementInit(&'a ForStatementInit<'a>), - IfStatement(&'a IfStatement<'a>), - LabeledStatement(&'a LabeledStatement<'a>), - ReturnStatement(&'a ReturnStatement<'a>), - SwitchStatement(&'a SwitchStatement<'a>), - ThrowStatement(&'a ThrowStatement<'a>), - TryStatement(&'a TryStatement<'a>), - WhileStatement(&'a WhileStatement<'a>), - WithStatement(&'a WithStatement<'a>), - - SwitchCase(&'a SwitchCase<'a>), - CatchClause(&'a CatchClause<'a>), - FinallyClause(&'a BlockStatement<'a>), - - VariableDeclaration(&'a VariableDeclaration<'a>), - VariableDeclarator(&'a VariableDeclarator<'a>), - - UsingDeclaration(&'a UsingDeclaration<'a>), - - IdentifierName(&'a IdentifierName<'a>), - IdentifierReference(&'a IdentifierReference<'a>), - BindingIdentifier(&'a BindingIdentifier<'a>), - LabelIdentifier(&'a LabelIdentifier<'a>), - PrivateIdentifier(&'a PrivateIdentifier<'a>), - - NumericLiteral(&'a NumericLiteral<'a>), - StringLiteral(&'a StringLiteral<'a>), - BooleanLiteral(&'a BooleanLiteral), - NullLiteral(&'a NullLiteral), - BigIntLiteral(&'a BigIntLiteral<'a>), - RegExpLiteral(&'a RegExpLiteral<'a>), - TemplateLiteral(&'a TemplateLiteral<'a>), - - MetaProperty(&'a MetaProperty<'a>), - Super(&'a Super), - - ArrayExpression(&'a ArrayExpression<'a>), - ArrowFunctionExpression(&'a ArrowFunctionExpression<'a>), - AssignmentExpression(&'a AssignmentExpression<'a>), - AwaitExpression(&'a AwaitExpression<'a>), - BinaryExpression(&'a BinaryExpression<'a>), - CallExpression(&'a CallExpression<'a>), - ChainExpression(&'a ChainExpression<'a>), - ConditionalExpression(&'a ConditionalExpression<'a>), - LogicalExpression(&'a LogicalExpression<'a>), - MemberExpression(&'a MemberExpression<'a>), - NewExpression(&'a NewExpression<'a>), - ObjectExpression(&'a ObjectExpression<'a>), - ParenthesizedExpression(&'a ParenthesizedExpression<'a>), - SequenceExpression(&'a SequenceExpression<'a>), - TaggedTemplateExpression(&'a TaggedTemplateExpression<'a>), - ThisExpression(&'a ThisExpression), - UnaryExpression(&'a UnaryExpression<'a>), - UpdateExpression(&'a UpdateExpression<'a>), - YieldExpression(&'a YieldExpression<'a>), - ImportExpression(&'a ImportExpression<'a>), - PrivateInExpression(&'a PrivateInExpression<'a>), - - ObjectProperty(&'a ObjectProperty<'a>), - PropertyKey(&'a PropertyKey<'a>), - Argument(&'a Argument<'a>), - AssignmentTarget(&'a AssignmentTarget<'a>), - SimpleAssignmentTarget(&'a SimpleAssignmentTarget<'a>), - AssignmentTargetWithDefault(&'a AssignmentTargetWithDefault<'a>), - ArrayExpressionElement(&'a ArrayExpressionElement<'a>), - Elision(&'a Elision), - ExpressionArrayElement(&'a Expression<'a>), - SpreadElement(&'a SpreadElement<'a>), - BindingRestElement(&'a BindingRestElement<'a>), - - Function(&'a Function<'a>), - FunctionBody(&'a FunctionBody<'a>), - FormalParameters(&'a FormalParameters<'a>), - FormalParameter(&'a FormalParameter<'a>), - CatchParameter(&'a CatchParameter<'a>), - - Class(&'a Class<'a>), - ClassBody(&'a ClassBody<'a>), - ClassHeritage(&'a Expression<'a>), - TSClassImplements(&'a TSClassImplements<'a>), - StaticBlock(&'a StaticBlock<'a>), - PropertyDefinition(&'a PropertyDefinition<'a>), - MethodDefinition(&'a MethodDefinition<'a>), - - ArrayPattern(&'a ArrayPattern<'a>), - ObjectPattern(&'a ObjectPattern<'a>), - AssignmentPattern(&'a AssignmentPattern<'a>), - - Decorator(&'a Decorator<'a>), - - ModuleDeclaration(&'a ModuleDeclaration<'a>), - ImportDeclaration(&'a ImportDeclaration<'a>), - ImportSpecifier(&'a ImportSpecifier<'a>), - ExportSpecifier(&'a ExportSpecifier<'a>), - ImportDefaultSpecifier(&'a ImportDefaultSpecifier<'a>), - ImportNamespaceSpecifier(&'a ImportNamespaceSpecifier<'a>), - ExportDefaultDeclaration(&'a ExportDefaultDeclaration<'a>), - ExportNamedDeclaration(&'a ExportNamedDeclaration<'a>), - ExportAllDeclaration(&'a ExportAllDeclaration<'a>), - - // JSX - // Please make sure to add these to `is_jsx` below. - JSXElement(&'a JSXElement<'a>), - JSXFragment(&'a JSXFragment<'a>), - JSXOpeningElement(&'a JSXOpeningElement<'a>), - JSXClosingElement(&'a JSXClosingElement<'a>), - JSXElementName(&'a JSXElementName<'a>), - JSXExpressionContainer(&'a JSXExpressionContainer<'a>), - JSXAttributeItem(&'a JSXAttributeItem<'a>), - JSXSpreadAttribute(&'a JSXSpreadAttribute<'a>), - JSXText(&'a JSXText<'a>), - JSXIdentifier(&'a JSXIdentifier<'a>), - JSXMemberExpression(&'a JSXMemberExpression<'a>), - JSXMemberExpressionObject(&'a JSXMemberExpressionObject<'a>), - JSXNamespacedName(&'a JSXNamespacedName<'a>), - - // TypeScript - TSModuleBlock(&'a TSModuleBlock<'a>), - - // NOTE: make sure add these to AstKind::is_type below - TSAnyKeyword(&'a TSAnyKeyword), - TSBigIntKeyword(&'a TSBigIntKeyword), - TSBooleanKeyword(&'a TSBooleanKeyword), - TSIntrinsicKeyword(&'a TSIntrinsicKeyword), - TSNeverKeyword(&'a TSNeverKeyword), - TSNullKeyword(&'a TSNullKeyword), - TSNumberKeyword(&'a TSNumberKeyword), - TSObjectKeyword(&'a TSObjectKeyword), - TSStringKeyword(&'a TSStringKeyword), - TSSymbolKeyword(&'a TSSymbolKeyword), - TSUndefinedKeyword(&'a TSUndefinedKeyword), - TSUnknownKeyword(&'a TSUnknownKeyword), - TSVoidKeyword(&'a TSVoidKeyword), - - // NOTE: make sure add these to AstKind::is_type below - TSIndexedAccessType(&'a TSIndexedAccessType<'a>), - TSInferType(&'a TSInferType<'a>), - TSIntersectionType(&'a TSIntersectionType<'a>), - TSLiteralType(&'a TSLiteralType<'a>), - TSMethodSignature(&'a TSMethodSignature<'a>), - TSTemplateLiteralType(&'a TSTemplateLiteralType<'a>), - TSThisType(&'a TSThisType), - TSTypeLiteral(&'a TSTypeLiteral<'a>), - TSTypeReference(&'a TSTypeReference<'a>), - TSUnionType(&'a TSUnionType<'a>), - - TSAsExpression(&'a TSAsExpression<'a>), - TSSatisfiesExpression(&'a TSSatisfiesExpression<'a>), - TSNonNullExpression(&'a TSNonNullExpression<'a>), - TSInstantiationExpression(&'a TSInstantiationExpression<'a>), - - TSEnumDeclaration(&'a TSEnumDeclaration<'a>), - TSEnumMember(&'a TSEnumMember<'a>), - - TSImportEqualsDeclaration(&'a TSImportEqualsDeclaration<'a>), - TSTypeName(&'a TSTypeName<'a>), - TSExternalModuleReference(&'a TSExternalModuleReference<'a>), - TSQualifiedName(&'a TSQualifiedName<'a>), - - TSInterfaceDeclaration(&'a TSInterfaceDeclaration<'a>), - TSInterfaceHeritage(&'a TSInterfaceHeritage<'a>), - TSModuleDeclaration(&'a TSModuleDeclaration<'a>), - TSTypeAliasDeclaration(&'a TSTypeAliasDeclaration<'a>), - TSTypeAnnotation(&'a TSTypeAnnotation<'a>), - TSTypeQuery(&'a TSTypeQuery<'a>), - TSTypeAssertion(&'a TSTypeAssertion<'a>), - TSThisParameter(&'a TSThisParameter<'a>), - TSTypeParameter(&'a TSTypeParameter<'a>), - TSTypeParameterDeclaration(&'a TSTypeParameterDeclaration<'a>), - TSTypeParameterInstantiation(&'a TSTypeParameterInstantiation<'a>), - TSImportType(&'a TSImportType<'a>), - TSNamedTupleMember(&'a TSNamedTupleMember<'a>), - - TSPropertySignature(&'a TSPropertySignature<'a>), -} - -#[allow(unsafe_code)] -// SAFETY: -// The AST is part of the bump allocator, -// it is our responsibility to never simultaneously mutate across threads. -unsafe impl<'a> Send for AstKind<'a> {} -#[allow(unsafe_code)] -// SAFETY: -// The AST is part of the bump allocator, -// it is our responsibility to never simultaneously mutate across threads. -unsafe impl<'a> Sync for AstKind<'a> {} +use super::{ast::*, AstKind}; impl<'a> AstKind<'a> { #[rustfmt::skip] @@ -359,196 +141,6 @@ impl<'a> AstKind<'a> { } } -impl<'a> GetSpan for AstKind<'a> { - #[allow(clippy::match_same_arms)] - fn span(&self) -> Span { - match self { - Self::Program(x) => x.span, - Self::Directive(x) => x.span, - Self::Hashbang(x) => x.span, - - Self::BlockStatement(x) => x.span, - Self::BreakStatement(x) => x.span, - Self::ContinueStatement(x) => x.span, - Self::DebuggerStatement(x) => x.span, - Self::DoWhileStatement(x) => x.span, - Self::EmptyStatement(x) => x.span, - Self::ExpressionStatement(x) => x.span, - Self::ForInStatement(x) => x.span, - Self::ForOfStatement(x) => x.span, - Self::ForStatement(x) => x.span, - Self::ForStatementInit(x) => x.span(), - Self::IfStatement(x) => x.span, - Self::LabeledStatement(x) => x.span, - Self::ReturnStatement(x) => x.span, - Self::SwitchStatement(x) => x.span, - Self::ThrowStatement(x) => x.span, - Self::TryStatement(x) => x.span, - Self::WhileStatement(x) => x.span, - Self::WithStatement(x) => x.span, - - Self::SwitchCase(x) => x.span, - Self::CatchClause(x) => x.span, - Self::FinallyClause(x) => x.span, - - Self::VariableDeclaration(x) => x.span, - Self::VariableDeclarator(x) => x.span, - - Self::UsingDeclaration(x) => x.span, - - Self::IdentifierName(x) => x.span, - Self::IdentifierReference(x) => x.span, - Self::BindingIdentifier(x) => x.span, - Self::LabelIdentifier(x) => x.span, - Self::PrivateIdentifier(x) => x.span, - - Self::NumericLiteral(x) => x.span, - Self::StringLiteral(x) => x.span, - Self::BooleanLiteral(x) => x.span, - Self::NullLiteral(x) => x.span, - Self::BigIntLiteral(x) => x.span, - Self::RegExpLiteral(x) => x.span, - Self::TemplateLiteral(x) => x.span, - - Self::MetaProperty(x) => x.span, - Self::Super(x) => x.span, - - Self::ArrayExpression(x) => x.span, - Self::ArrowFunctionExpression(x) => x.span, - Self::AssignmentExpression(x) => x.span, - Self::AwaitExpression(x) => x.span, - Self::BinaryExpression(x) => x.span, - Self::CallExpression(x) => x.span, - Self::ChainExpression(x) => x.span, - Self::ConditionalExpression(x) => x.span, - Self::LogicalExpression(x) => x.span, - Self::MemberExpression(x) => x.span(), - Self::NewExpression(x) => x.span, - Self::ObjectExpression(x) => x.span, - Self::ParenthesizedExpression(x) => x.span, - Self::SequenceExpression(x) => x.span, - Self::TaggedTemplateExpression(x) => x.span, - Self::ThisExpression(x) => x.span, - Self::UnaryExpression(x) => x.span, - Self::UpdateExpression(x) => x.span, - Self::YieldExpression(x) => x.span, - Self::ImportExpression(x) => x.span, - Self::PrivateInExpression(x) => x.span, - - Self::ObjectProperty(x) => x.span, - Self::PropertyKey(x) => x.span(), - Self::Argument(x) => x.span(), - Self::ArrayExpressionElement(x) => x.span(), - Self::AssignmentTarget(x) => x.span(), - Self::SimpleAssignmentTarget(x) => x.span(), - Self::AssignmentTargetWithDefault(x) => x.span, - Self::SpreadElement(x) => x.span, - Self::Elision(x) => x.span, - Self::ExpressionArrayElement(x) => x.span(), - Self::BindingRestElement(x) => x.span, - - Self::Function(x) => x.span, - Self::FunctionBody(x) => x.span, - Self::FormalParameters(x) => x.span, - Self::FormalParameter(x) => x.span, - Self::CatchParameter(x) => x.span, - - Self::Class(x) => x.span, - Self::ClassBody(x) => x.span, - Self::ClassHeritage(x) => x.span(), - Self::TSClassImplements(x) => x.span, - Self::StaticBlock(x) => x.span, - Self::PropertyDefinition(x) => x.span, - Self::MethodDefinition(x) => x.span, - - Self::ArrayPattern(x) => x.span, - Self::ObjectPattern(x) => x.span, - Self::AssignmentPattern(x) => x.span, - - Self::Decorator(x) => x.span, - - Self::ModuleDeclaration(x) => x.span(), - Self::ImportDeclaration(x) => x.span, - Self::ImportSpecifier(x) => x.span, - Self::ExportSpecifier(x) => x.span, - Self::ImportDefaultSpecifier(x) => x.span, - Self::ImportNamespaceSpecifier(x) => x.span, - Self::ExportDefaultDeclaration(x) => x.span, - Self::ExportNamedDeclaration(x) => x.span, - Self::ExportAllDeclaration(x) => x.span, - - Self::JSXOpeningElement(x) => x.span, - Self::JSXClosingElement(x) => x.span, - Self::JSXElementName(x) => x.span(), - Self::JSXElement(x) => x.span, - Self::JSXFragment(x) => x.span, - Self::JSXAttributeItem(x) => x.span(), - Self::JSXSpreadAttribute(x) => x.span, - Self::JSXText(x) => x.span, - Self::JSXExpressionContainer(x) => x.span, - Self::JSXIdentifier(x) => x.span, - Self::JSXMemberExpression(x) => x.span, - Self::JSXMemberExpressionObject(x) => x.span(), - Self::JSXNamespacedName(x) => x.span, - - Self::TSModuleBlock(x) => x.span, - - Self::TSAnyKeyword(x) => x.span, - Self::TSIntersectionType(x) => x.span, - Self::TSLiteralType(x) => x.span, - Self::TSMethodSignature(x) => x.span, - Self::TSNullKeyword(x) => x.span, - Self::TSTypeLiteral(x) => x.span, - Self::TSTypeReference(x) => x.span, - Self::TSUnionType(x) => x.span, - Self::TSVoidKeyword(x) => x.span, - Self::TSBigIntKeyword(x) => x.span, - Self::TSBooleanKeyword(x) => x.span, - Self::TSIntrinsicKeyword(x) => x.span, - Self::TSNeverKeyword(x) => x.span, - Self::TSNumberKeyword(x) => x.span, - Self::TSObjectKeyword(x) => x.span, - Self::TSStringKeyword(x) => x.span, - Self::TSSymbolKeyword(x) => x.span, - Self::TSThisType(x) => x.span, - Self::TSUndefinedKeyword(x) => x.span, - Self::TSUnknownKeyword(x) => x.span, - Self::TSInferType(x) => x.span, - Self::TSTemplateLiteralType(x) => x.span, - - Self::TSIndexedAccessType(x) => x.span, - - Self::TSAsExpression(x) => x.span, - Self::TSSatisfiesExpression(x) => x.span, - Self::TSNonNullExpression(x) => x.span, - Self::TSInstantiationExpression(x) => x.span, - - Self::TSEnumDeclaration(x) => x.span, - Self::TSEnumMember(x) => x.span, - - Self::TSImportEqualsDeclaration(x) => x.span, - Self::TSTypeName(x) => x.span(), - Self::TSExternalModuleReference(x) => x.span, - Self::TSQualifiedName(x) => x.span, - Self::TSInterfaceDeclaration(x) => x.span, - Self::TSInterfaceHeritage(x) => x.span, - Self::TSModuleDeclaration(x) => x.span, - Self::TSTypeAliasDeclaration(x) => x.span, - Self::TSTypeAnnotation(x) => x.span, - Self::TSTypeQuery(x) => x.span, - Self::TSTypeAssertion(x) => x.span, - Self::TSThisParameter(x) => x.span, - Self::TSTypeParameter(x) => x.span, - Self::TSTypeParameterDeclaration(x) => x.span, - Self::TSTypeParameterInstantiation(x) => x.span, - Self::TSImportType(x) => x.span, - Self::TSNamedTupleMember(x) => x.span, - - Self::TSPropertySignature(x) => x.span, - } - } -} - impl<'a> AstKind<'a> { #[allow(clippy::match_same_arms)] /// Get the AST kind name with minimal details. Particularly useful for diff --git a/crates/oxc_ast/src/generated/ast_kind.rs b/crates/oxc_ast/src/generated/ast_kind.rs new file mode 100644 index 0000000000000..0d8d9b1e3920e --- /dev/null +++ b/crates/oxc_ast/src/generated/ast_kind.rs @@ -0,0 +1,502 @@ +// Auto-generated code, DO NOT EDIT DIRECTLY! +// To edit this generated file you have to edit `tasks/ast_codegen/src/generators/ast_kind.rs` + +use crate::ast::*; +use oxc_span::{GetSpan, Span}; + +#[derive(Debug, Clone, Copy)] +pub enum AstType { + BooleanLiteral, + NullLiteral, + NumericLiteral, + BigIntLiteral, + RegExpLiteral, + StringLiteral, + Program, + IdentifierName, + IdentifierReference, + BindingIdentifier, + LabelIdentifier, + ThisExpression, + ArrayExpression, + ArrayExpressionElement, + Elision, + ObjectExpression, + ObjectProperty, + PropertyKey, + TemplateLiteral, + TaggedTemplateExpression, + MemberExpression, + CallExpression, + NewExpression, + MetaProperty, + SpreadElement, + Argument, + UpdateExpression, + UnaryExpression, + BinaryExpression, + PrivateInExpression, + LogicalExpression, + ConditionalExpression, + AssignmentExpression, + AssignmentTarget, + SimpleAssignmentTarget, + AssignmentTargetWithDefault, + SequenceExpression, + Super, + AwaitExpression, + ChainExpression, + ParenthesizedExpression, + Directive, + Hashbang, + BlockStatement, + VariableDeclaration, + VariableDeclarator, + UsingDeclaration, + EmptyStatement, + ExpressionStatement, + IfStatement, + DoWhileStatement, + WhileStatement, + ForStatement, + ForStatementInit, + ForInStatement, + ForOfStatement, + ContinueStatement, + BreakStatement, + ReturnStatement, + WithStatement, + SwitchStatement, + SwitchCase, + LabeledStatement, + ThrowStatement, + TryStatement, + CatchClause, + CatchParameter, + DebuggerStatement, + AssignmentPattern, + ObjectPattern, + ArrayPattern, + BindingRestElement, + Function, + FormalParameters, + FormalParameter, + FunctionBody, + ArrowFunctionExpression, + YieldExpression, + Class, + ClassBody, + MethodDefinition, + PropertyDefinition, + PrivateIdentifier, + StaticBlock, + ModuleDeclaration, + ImportExpression, + ImportDeclaration, + ImportSpecifier, + ImportDefaultSpecifier, + ImportNamespaceSpecifier, + ExportNamedDeclaration, + ExportDefaultDeclaration, + ExportAllDeclaration, + ExportSpecifier, + TSThisParameter, + TSEnumDeclaration, + TSEnumMember, + TSTypeAnnotation, + TSLiteralType, + TSUnionType, + TSIntersectionType, + TSIndexedAccessType, + TSNamedTupleMember, + TSAnyKeyword, + TSStringKeyword, + TSBooleanKeyword, + TSNumberKeyword, + TSNeverKeyword, + TSIntrinsicKeyword, + TSUnknownKeyword, + TSNullKeyword, + TSUndefinedKeyword, + TSVoidKeyword, + TSSymbolKeyword, + TSThisType, + TSObjectKeyword, + TSBigIntKeyword, + TSTypeReference, + TSTypeName, + TSQualifiedName, + TSTypeParameterInstantiation, + TSTypeParameter, + TSTypeParameterDeclaration, + TSTypeAliasDeclaration, + TSClassImplements, + TSInterfaceDeclaration, + TSPropertySignature, + TSMethodSignature, + TSInterfaceHeritage, + TSModuleDeclaration, + TSModuleBlock, + TSTypeLiteral, + TSInferType, + TSTypeQuery, + TSImportType, + TSTemplateLiteralType, + TSAsExpression, + TSSatisfiesExpression, + TSTypeAssertion, + TSImportEqualsDeclaration, + TSExternalModuleReference, + TSNonNullExpression, + Decorator, + TSInstantiationExpression, + JSXElement, + JSXOpeningElement, + JSXClosingElement, + JSXFragment, + JSXElementName, + JSXNamespacedName, + JSXMemberExpression, + JSXMemberExpressionObject, + JSXExpressionContainer, + JSXAttributeItem, + JSXSpreadAttribute, + JSXIdentifier, + JSXText, + FinallyClause, + ClassHeritage, + ExpressionArrayElement, +} + +/// Untyped AST Node Kind +#[derive(Debug, Clone, Copy)] +pub enum AstKind<'a> { + BooleanLiteral(&'a BooleanLiteral), + NullLiteral(&'a NullLiteral), + NumericLiteral(&'a NumericLiteral<'a>), + BigIntLiteral(&'a BigIntLiteral<'a>), + RegExpLiteral(&'a RegExpLiteral<'a>), + StringLiteral(&'a StringLiteral<'a>), + Program(&'a Program<'a>), + IdentifierName(&'a IdentifierName<'a>), + IdentifierReference(&'a IdentifierReference<'a>), + BindingIdentifier(&'a BindingIdentifier<'a>), + LabelIdentifier(&'a LabelIdentifier<'a>), + ThisExpression(&'a ThisExpression), + ArrayExpression(&'a ArrayExpression<'a>), + ArrayExpressionElement(&'a ArrayExpressionElement<'a>), + Elision(&'a Elision), + ObjectExpression(&'a ObjectExpression<'a>), + ObjectProperty(&'a ObjectProperty<'a>), + PropertyKey(&'a PropertyKey<'a>), + TemplateLiteral(&'a TemplateLiteral<'a>), + TaggedTemplateExpression(&'a TaggedTemplateExpression<'a>), + MemberExpression(&'a MemberExpression<'a>), + CallExpression(&'a CallExpression<'a>), + NewExpression(&'a NewExpression<'a>), + MetaProperty(&'a MetaProperty<'a>), + SpreadElement(&'a SpreadElement<'a>), + Argument(&'a Argument<'a>), + UpdateExpression(&'a UpdateExpression<'a>), + UnaryExpression(&'a UnaryExpression<'a>), + BinaryExpression(&'a BinaryExpression<'a>), + PrivateInExpression(&'a PrivateInExpression<'a>), + LogicalExpression(&'a LogicalExpression<'a>), + ConditionalExpression(&'a ConditionalExpression<'a>), + AssignmentExpression(&'a AssignmentExpression<'a>), + AssignmentTarget(&'a AssignmentTarget<'a>), + SimpleAssignmentTarget(&'a SimpleAssignmentTarget<'a>), + AssignmentTargetWithDefault(&'a AssignmentTargetWithDefault<'a>), + SequenceExpression(&'a SequenceExpression<'a>), + Super(&'a Super), + AwaitExpression(&'a AwaitExpression<'a>), + ChainExpression(&'a ChainExpression<'a>), + ParenthesizedExpression(&'a ParenthesizedExpression<'a>), + Directive(&'a Directive<'a>), + Hashbang(&'a Hashbang<'a>), + BlockStatement(&'a BlockStatement<'a>), + VariableDeclaration(&'a VariableDeclaration<'a>), + VariableDeclarator(&'a VariableDeclarator<'a>), + UsingDeclaration(&'a UsingDeclaration<'a>), + EmptyStatement(&'a EmptyStatement), + ExpressionStatement(&'a ExpressionStatement<'a>), + IfStatement(&'a IfStatement<'a>), + DoWhileStatement(&'a DoWhileStatement<'a>), + WhileStatement(&'a WhileStatement<'a>), + ForStatement(&'a ForStatement<'a>), + ForStatementInit(&'a ForStatementInit<'a>), + ForInStatement(&'a ForInStatement<'a>), + ForOfStatement(&'a ForOfStatement<'a>), + ContinueStatement(&'a ContinueStatement<'a>), + BreakStatement(&'a BreakStatement<'a>), + ReturnStatement(&'a ReturnStatement<'a>), + WithStatement(&'a WithStatement<'a>), + SwitchStatement(&'a SwitchStatement<'a>), + SwitchCase(&'a SwitchCase<'a>), + LabeledStatement(&'a LabeledStatement<'a>), + ThrowStatement(&'a ThrowStatement<'a>), + TryStatement(&'a TryStatement<'a>), + CatchClause(&'a CatchClause<'a>), + CatchParameter(&'a CatchParameter<'a>), + DebuggerStatement(&'a DebuggerStatement), + AssignmentPattern(&'a AssignmentPattern<'a>), + ObjectPattern(&'a ObjectPattern<'a>), + ArrayPattern(&'a ArrayPattern<'a>), + BindingRestElement(&'a BindingRestElement<'a>), + Function(&'a Function<'a>), + FormalParameters(&'a FormalParameters<'a>), + FormalParameter(&'a FormalParameter<'a>), + FunctionBody(&'a FunctionBody<'a>), + ArrowFunctionExpression(&'a ArrowFunctionExpression<'a>), + YieldExpression(&'a YieldExpression<'a>), + Class(&'a Class<'a>), + ClassBody(&'a ClassBody<'a>), + MethodDefinition(&'a MethodDefinition<'a>), + PropertyDefinition(&'a PropertyDefinition<'a>), + PrivateIdentifier(&'a PrivateIdentifier<'a>), + StaticBlock(&'a StaticBlock<'a>), + ModuleDeclaration(&'a ModuleDeclaration<'a>), + ImportExpression(&'a ImportExpression<'a>), + ImportDeclaration(&'a ImportDeclaration<'a>), + ImportSpecifier(&'a ImportSpecifier<'a>), + ImportDefaultSpecifier(&'a ImportDefaultSpecifier<'a>), + ImportNamespaceSpecifier(&'a ImportNamespaceSpecifier<'a>), + ExportNamedDeclaration(&'a ExportNamedDeclaration<'a>), + ExportDefaultDeclaration(&'a ExportDefaultDeclaration<'a>), + ExportAllDeclaration(&'a ExportAllDeclaration<'a>), + ExportSpecifier(&'a ExportSpecifier<'a>), + TSThisParameter(&'a TSThisParameter<'a>), + TSEnumDeclaration(&'a TSEnumDeclaration<'a>), + TSEnumMember(&'a TSEnumMember<'a>), + TSTypeAnnotation(&'a TSTypeAnnotation<'a>), + TSLiteralType(&'a TSLiteralType<'a>), + TSUnionType(&'a TSUnionType<'a>), + TSIntersectionType(&'a TSIntersectionType<'a>), + TSIndexedAccessType(&'a TSIndexedAccessType<'a>), + TSNamedTupleMember(&'a TSNamedTupleMember<'a>), + TSAnyKeyword(&'a TSAnyKeyword), + TSStringKeyword(&'a TSStringKeyword), + TSBooleanKeyword(&'a TSBooleanKeyword), + TSNumberKeyword(&'a TSNumberKeyword), + TSNeverKeyword(&'a TSNeverKeyword), + TSIntrinsicKeyword(&'a TSIntrinsicKeyword), + TSUnknownKeyword(&'a TSUnknownKeyword), + TSNullKeyword(&'a TSNullKeyword), + TSUndefinedKeyword(&'a TSUndefinedKeyword), + TSVoidKeyword(&'a TSVoidKeyword), + TSSymbolKeyword(&'a TSSymbolKeyword), + TSThisType(&'a TSThisType), + TSObjectKeyword(&'a TSObjectKeyword), + TSBigIntKeyword(&'a TSBigIntKeyword), + TSTypeReference(&'a TSTypeReference<'a>), + TSTypeName(&'a TSTypeName<'a>), + TSQualifiedName(&'a TSQualifiedName<'a>), + TSTypeParameterInstantiation(&'a TSTypeParameterInstantiation<'a>), + TSTypeParameter(&'a TSTypeParameter<'a>), + TSTypeParameterDeclaration(&'a TSTypeParameterDeclaration<'a>), + TSTypeAliasDeclaration(&'a TSTypeAliasDeclaration<'a>), + TSClassImplements(&'a TSClassImplements<'a>), + TSInterfaceDeclaration(&'a TSInterfaceDeclaration<'a>), + TSPropertySignature(&'a TSPropertySignature<'a>), + TSMethodSignature(&'a TSMethodSignature<'a>), + TSInterfaceHeritage(&'a TSInterfaceHeritage<'a>), + TSModuleDeclaration(&'a TSModuleDeclaration<'a>), + TSModuleBlock(&'a TSModuleBlock<'a>), + TSTypeLiteral(&'a TSTypeLiteral<'a>), + TSInferType(&'a TSInferType<'a>), + TSTypeQuery(&'a TSTypeQuery<'a>), + TSImportType(&'a TSImportType<'a>), + TSTemplateLiteralType(&'a TSTemplateLiteralType<'a>), + TSAsExpression(&'a TSAsExpression<'a>), + TSSatisfiesExpression(&'a TSSatisfiesExpression<'a>), + TSTypeAssertion(&'a TSTypeAssertion<'a>), + TSImportEqualsDeclaration(&'a TSImportEqualsDeclaration<'a>), + TSExternalModuleReference(&'a TSExternalModuleReference<'a>), + TSNonNullExpression(&'a TSNonNullExpression<'a>), + Decorator(&'a Decorator<'a>), + TSInstantiationExpression(&'a TSInstantiationExpression<'a>), + JSXElement(&'a JSXElement<'a>), + JSXOpeningElement(&'a JSXOpeningElement<'a>), + JSXClosingElement(&'a JSXClosingElement<'a>), + JSXFragment(&'a JSXFragment<'a>), + JSXElementName(&'a JSXElementName<'a>), + JSXNamespacedName(&'a JSXNamespacedName<'a>), + JSXMemberExpression(&'a JSXMemberExpression<'a>), + JSXMemberExpressionObject(&'a JSXMemberExpressionObject<'a>), + JSXExpressionContainer(&'a JSXExpressionContainer<'a>), + JSXAttributeItem(&'a JSXAttributeItem<'a>), + JSXSpreadAttribute(&'a JSXSpreadAttribute<'a>), + JSXIdentifier(&'a JSXIdentifier<'a>), + JSXText(&'a JSXText<'a>), + FinallyClause(&'a BlockStatement<'a>), + ClassHeritage(&'a Expression<'a>), + ExpressionArrayElement(&'a Expression<'a>), +} + +impl<'a> GetSpan for AstKind<'a> { + #[allow(clippy::match_same_arms)] + fn span(&self) -> Span { + match self { + Self::BooleanLiteral(it) => it.span(), + Self::NullLiteral(it) => it.span(), + Self::NumericLiteral(it) => it.span(), + Self::BigIntLiteral(it) => it.span(), + Self::RegExpLiteral(it) => it.span(), + Self::StringLiteral(it) => it.span(), + Self::Program(it) => it.span(), + Self::IdentifierName(it) => it.span(), + Self::IdentifierReference(it) => it.span(), + Self::BindingIdentifier(it) => it.span(), + Self::LabelIdentifier(it) => it.span(), + Self::ThisExpression(it) => it.span(), + Self::ArrayExpression(it) => it.span(), + Self::ArrayExpressionElement(it) => it.span(), + Self::Elision(it) => it.span(), + Self::ObjectExpression(it) => it.span(), + Self::ObjectProperty(it) => it.span(), + Self::PropertyKey(it) => it.span(), + Self::TemplateLiteral(it) => it.span(), + Self::TaggedTemplateExpression(it) => it.span(), + Self::MemberExpression(it) => it.span(), + Self::CallExpression(it) => it.span(), + Self::NewExpression(it) => it.span(), + Self::MetaProperty(it) => it.span(), + Self::SpreadElement(it) => it.span(), + Self::Argument(it) => it.span(), + Self::UpdateExpression(it) => it.span(), + Self::UnaryExpression(it) => it.span(), + Self::BinaryExpression(it) => it.span(), + Self::PrivateInExpression(it) => it.span(), + Self::LogicalExpression(it) => it.span(), + Self::ConditionalExpression(it) => it.span(), + Self::AssignmentExpression(it) => it.span(), + Self::AssignmentTarget(it) => it.span(), + Self::SimpleAssignmentTarget(it) => it.span(), + Self::AssignmentTargetWithDefault(it) => it.span(), + Self::SequenceExpression(it) => it.span(), + Self::Super(it) => it.span(), + Self::AwaitExpression(it) => it.span(), + Self::ChainExpression(it) => it.span(), + Self::ParenthesizedExpression(it) => it.span(), + Self::Directive(it) => it.span(), + Self::Hashbang(it) => it.span(), + Self::BlockStatement(it) => it.span(), + Self::VariableDeclaration(it) => it.span(), + Self::VariableDeclarator(it) => it.span(), + Self::UsingDeclaration(it) => it.span(), + Self::EmptyStatement(it) => it.span(), + Self::ExpressionStatement(it) => it.span(), + Self::IfStatement(it) => it.span(), + Self::DoWhileStatement(it) => it.span(), + Self::WhileStatement(it) => it.span(), + Self::ForStatement(it) => it.span(), + Self::ForStatementInit(it) => it.span(), + Self::ForInStatement(it) => it.span(), + Self::ForOfStatement(it) => it.span(), + Self::ContinueStatement(it) => it.span(), + Self::BreakStatement(it) => it.span(), + Self::ReturnStatement(it) => it.span(), + Self::WithStatement(it) => it.span(), + Self::SwitchStatement(it) => it.span(), + Self::SwitchCase(it) => it.span(), + Self::LabeledStatement(it) => it.span(), + Self::ThrowStatement(it) => it.span(), + Self::TryStatement(it) => it.span(), + Self::CatchClause(it) => it.span(), + Self::CatchParameter(it) => it.span(), + Self::DebuggerStatement(it) => it.span(), + Self::AssignmentPattern(it) => it.span(), + Self::ObjectPattern(it) => it.span(), + Self::ArrayPattern(it) => it.span(), + Self::BindingRestElement(it) => it.span(), + Self::Function(it) => it.span(), + Self::FormalParameters(it) => it.span(), + Self::FormalParameter(it) => it.span(), + Self::FunctionBody(it) => it.span(), + Self::ArrowFunctionExpression(it) => it.span(), + Self::YieldExpression(it) => it.span(), + Self::Class(it) => it.span(), + Self::ClassBody(it) => it.span(), + Self::MethodDefinition(it) => it.span(), + Self::PropertyDefinition(it) => it.span(), + Self::PrivateIdentifier(it) => it.span(), + Self::StaticBlock(it) => it.span(), + Self::ModuleDeclaration(it) => it.span(), + Self::ImportExpression(it) => it.span(), + Self::ImportDeclaration(it) => it.span(), + Self::ImportSpecifier(it) => it.span(), + Self::ImportDefaultSpecifier(it) => it.span(), + Self::ImportNamespaceSpecifier(it) => it.span(), + Self::ExportNamedDeclaration(it) => it.span(), + Self::ExportDefaultDeclaration(it) => it.span(), + Self::ExportAllDeclaration(it) => it.span(), + Self::ExportSpecifier(it) => it.span(), + Self::TSThisParameter(it) => it.span(), + Self::TSEnumDeclaration(it) => it.span(), + Self::TSEnumMember(it) => it.span(), + Self::TSTypeAnnotation(it) => it.span(), + Self::TSLiteralType(it) => it.span(), + Self::TSUnionType(it) => it.span(), + Self::TSIntersectionType(it) => it.span(), + Self::TSIndexedAccessType(it) => it.span(), + Self::TSNamedTupleMember(it) => it.span(), + Self::TSAnyKeyword(it) => it.span(), + Self::TSStringKeyword(it) => it.span(), + Self::TSBooleanKeyword(it) => it.span(), + Self::TSNumberKeyword(it) => it.span(), + Self::TSNeverKeyword(it) => it.span(), + Self::TSIntrinsicKeyword(it) => it.span(), + Self::TSUnknownKeyword(it) => it.span(), + Self::TSNullKeyword(it) => it.span(), + Self::TSUndefinedKeyword(it) => it.span(), + Self::TSVoidKeyword(it) => it.span(), + Self::TSSymbolKeyword(it) => it.span(), + Self::TSThisType(it) => it.span(), + Self::TSObjectKeyword(it) => it.span(), + Self::TSBigIntKeyword(it) => it.span(), + Self::TSTypeReference(it) => it.span(), + Self::TSTypeName(it) => it.span(), + Self::TSQualifiedName(it) => it.span(), + Self::TSTypeParameterInstantiation(it) => it.span(), + Self::TSTypeParameter(it) => it.span(), + Self::TSTypeParameterDeclaration(it) => it.span(), + Self::TSTypeAliasDeclaration(it) => it.span(), + Self::TSClassImplements(it) => it.span(), + Self::TSInterfaceDeclaration(it) => it.span(), + Self::TSPropertySignature(it) => it.span(), + Self::TSMethodSignature(it) => it.span(), + Self::TSInterfaceHeritage(it) => it.span(), + Self::TSModuleDeclaration(it) => it.span(), + Self::TSModuleBlock(it) => it.span(), + Self::TSTypeLiteral(it) => it.span(), + Self::TSInferType(it) => it.span(), + Self::TSTypeQuery(it) => it.span(), + Self::TSImportType(it) => it.span(), + Self::TSTemplateLiteralType(it) => it.span(), + Self::TSAsExpression(it) => it.span(), + Self::TSSatisfiesExpression(it) => it.span(), + Self::TSTypeAssertion(it) => it.span(), + Self::TSImportEqualsDeclaration(it) => it.span(), + Self::TSExternalModuleReference(it) => it.span(), + Self::TSNonNullExpression(it) => it.span(), + Self::Decorator(it) => it.span(), + Self::TSInstantiationExpression(it) => it.span(), + Self::JSXElement(it) => it.span(), + Self::JSXOpeningElement(it) => it.span(), + Self::JSXClosingElement(it) => it.span(), + Self::JSXFragment(it) => it.span(), + Self::JSXElementName(it) => it.span(), + Self::JSXNamespacedName(it) => it.span(), + Self::JSXMemberExpression(it) => it.span(), + Self::JSXMemberExpressionObject(it) => it.span(), + Self::JSXExpressionContainer(it) => it.span(), + Self::JSXAttributeItem(it) => it.span(), + Self::JSXSpreadAttribute(it) => it.span(), + Self::JSXIdentifier(it) => it.span(), + Self::JSXText(it) => it.span(), + Self::FinallyClause(it) => it.span(), + Self::ClassHeritage(it) => it.span(), + Self::ExpressionArrayElement(it) => it.span(), + } + } +} diff --git a/crates/oxc_ast/src/lib.rs b/crates/oxc_ast/src/lib.rs index ca9db3b327afd..95f905f77fa98 100644 --- a/crates/oxc_ast/src/lib.rs +++ b/crates/oxc_ast/src/lib.rs @@ -15,16 +15,19 @@ mod serialize; pub mod ast; mod ast_builder; mod ast_impl; -mod ast_kind; +mod ast_kind_impl; pub mod precedence; pub mod syntax_directed_operations; mod trivia; pub mod visit; mod generated { + pub mod ast_kind; pub mod span; } +pub use generated::ast_kind; + pub use num_bigint::BigUint; pub use crate::{ diff --git a/tasks/ast_codegen/src/generators/ast_kind.rs b/tasks/ast_codegen/src/generators/ast_kind.rs index 228500f80e9a3..c1e05afbe4679 100644 --- a/tasks/ast_codegen/src/generators/ast_kind.rs +++ b/tasks/ast_codegen/src/generators/ast_kind.rs @@ -1,32 +1,161 @@ use itertools::Itertools; -use syn::{parse_quote, Variant}; +use quote::quote; +use syn::{parse_quote, Arm, Ident, Type, Variant}; use crate::{schema::RType, CodegenCtx, Generator, GeneratorOutput}; +use super::generated_header; + pub struct AstKindGenerator; +const BLACK_LIST: [&str; 68] = [ + "Expression", + "ObjectPropertyKind", + "TemplateElement", + "ComputedMemberExpression", + "StaticMemberExpression", + "PrivateFieldExpression", + "AssignmentTargetPattern", + "ArrayAssignmentTarget", + "ObjectAssignmentTarget", + "AssignmentTargetRest", + "AssignmentTargetMaybeDefault", + "AssignmentTargetProperty", + "AssignmentTargetPropertyIdentifier", + "AssignmentTargetPropertyProperty", + "ChainElement", + "Statement", + "Declaration", + "ForStatementLeft", + "BindingPattern", + "BindingPatternKind", + "BindingProperty", + "ClassElement", + "AccessorProperty", + "ImportDeclarationSpecifier", + "WithClause", + "ImportAttribute", + "ImportAttributeKey", + "ExportDefaultDeclarationKind", + "ModuleExportName", + "TSEnumMemberName", + "TSLiteral", + "TSType", + "TSConditionalType", + "TSTypeOperator", + "TSArrayType", + "TSTupleType", + "TSOptionalType", + "TSRestType", + "TSTupleElement", + "TSInterfaceBody", + "TSSignature", + "TSIndexSignature", + "TSCallSignatureDeclaration", + "TSConstructSignatureDeclaration", + "TSIndexSignatureName", + "TSTypePredicate", + "TSTypePredicateName", + "TSModuleDeclarationName", + "TSModuleDeclarationBody", + "TSTypeQueryExprName", + "TSImportAttribute", + "TSImportAttributes", + "TSImportAttributeName", + "TSFunctionType", + "TSConstructorType", + "TSMappedType", + "TSModuleReference", + "TSExportAssignment", + "TSNamespaceExportDeclaration", + "JSDocNullableType", + "JSDocUnknownType", + "JSXExpression", + "JSXEmptyExpression", + "JSXAttribute", + "JSXAttributeName", + "JSXAttributeValue", + "JSXChild", + "JSXSpreadChild", +]; + +pub fn blacklist((ident, _): &(Ident, Type)) -> bool { + !BLACK_LIST.contains(&ident.to_string().as_str()) +} + +pub fn aliased_nodes() -> [(Ident, Type); 3] { + use syn::parse_quote as pq; + [ + (pq!(FinallyClause), pq!(BlockStatement<'a>)), + (pq!(ClassHeritage), pq!(Expression<'a>)), + (pq!(ExpressionArrayElement), pq!(Expression<'a>)), + ] +} + impl Generator for AstKindGenerator { fn name(&self) -> &'static str { "AstKindGenerator" } fn generate(&mut self, ctx: &CodegenCtx) -> GeneratorOutput { - let kinds: Vec = ctx + let have_kinds: Vec<(Ident, Type)> = ctx .ty_table .iter() .filter_map(|maybe_kind| match &*maybe_kind.borrow() { - kind @ (RType::Enum(_) | RType::Struct(_)) => { - let ident = kind.ident(); - let typ = kind.as_type(); - Some(parse_quote!(#ident(#typ))) + kind @ (RType::Enum(_) | RType::Struct(_)) if kind.visitable() => { + let ident = kind.ident().unwrap().clone(); + let typ = kind.as_type().unwrap(); + Some((ident, typ)) } _ => None, }) + .filter(blacklist) + .chain(aliased_nodes()) + .collect(); + + let types: Vec = + have_kinds.iter().map(|(ident, _)| parse_quote!(#ident)).collect_vec(); + + let kinds: Vec = + have_kinds.iter().map(|(ident, typ)| parse_quote!(#ident(&'a #typ))).collect_vec(); + + let span_matches: Vec = have_kinds + .iter() + .map(|(ident, _)| parse_quote!(Self :: #ident(it) => it.span())) .collect_vec(); - GeneratorOutput::One(parse_quote! { + let header = generated_header!(); + + GeneratorOutput::One(quote! { + #header + + use crate::ast::*; + use oxc_span::{GetSpan, Span}; + + endl!(); + + #[derive(Debug, Clone, Copy)] + pub enum AstType { + #(#types),*, + } + + endl!(); + + /// Untyped AST Node Kind + #[derive(Debug, Clone, Copy)] pub enum AstKind<'a> { - #(#kinds),* + #(#kinds),*, + } + + endl!(); + + impl<'a> GetSpan for AstKind<'a> { + #[allow(clippy::match_same_arms)] + fn span(&self) -> Span { + match self { + #(#span_matches),*, + } + } } }) } diff --git a/tasks/ast_codegen/src/generators/mod.rs b/tasks/ast_codegen/src/generators/mod.rs index 0e32617f6f6b0..f8eca3f124d3a 100644 --- a/tasks/ast_codegen/src/generators/mod.rs +++ b/tasks/ast_codegen/src/generators/mod.rs @@ -27,7 +27,7 @@ macro_rules! generated_header { "// To edit this generated file you have to edit `{file}`" )); // TODO add generation date, AST source hash, etc here. - quote! { + quote::quote! { insert!("// Auto-generated code, DO NOT EDIT DIRECTLY!"); #edit_comment endl!(); diff --git a/tasks/ast_codegen/src/main.rs b/tasks/ast_codegen/src/main.rs index 818c604b39e53..6754fa22dc1a1 100644 --- a/tasks/ast_codegen/src/main.rs +++ b/tasks/ast_codegen/src/main.rs @@ -197,12 +197,25 @@ fn main() -> std::result::Result<(), Box> { let outputs: HashMap<_, _> = outputs.into_iter().collect(); { - let span_path = format!("{output_dir}/span.rs"); - let mut span_file = fs::File::create(span_path)?; + // write `span.rs` file let output = outputs[ImplGetSpanGenerator.name()].as_one(); let span_content = pprint(output); - span_file.write_all(span_content.as_bytes())?; + let path = format!("{output_dir}/span.rs"); + let mut file = fs::File::create(path)?; + + file.write_all(span_content.as_bytes())?; + } + + { + // write `ast_kind.rs` file + let output = outputs[AstKindGenerator.name()].as_one(); + let span_content = pprint(output); + + let path = format!("{output_dir}/ast_kind.rs"); + let mut file = fs::File::create(path)?; + + file.write_all(span_content.as_bytes())?; } // NOTE: Print AstKind