feat(ast): add AstKind::ty method (#8521)

Add `AstKind::ty` method to get `AstType` from an `AstKind`.

Works by setting the enum discriminants of `AstKind` and `AstType` to be the same, so one can be converted to the other at zero cost.
This commit is contained in:
overlookmotel 2025-01-16 00:32:36 +00:00
parent 06ccb51fae
commit a6d71f8f27
2 changed files with 393 additions and 337 deletions

View file

@ -4,347 +4,377 @@
#![allow(missing_docs)]
// FIXME (in ast_tools/src/generators/ast_kind.rs)
use std::ptr;
use oxc_span::{GetSpan, Span};
use crate::ast::*;
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum AstType {
BooleanLiteral,
NullLiteral,
NumericLiteral,
StringLiteral,
BigIntLiteral,
RegExpLiteral,
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,
AssignmentTargetPattern,
ArrayAssignmentTarget,
ObjectAssignmentTarget,
AssignmentTargetWithDefault,
SequenceExpression,
Super,
AwaitExpression,
ChainExpression,
ParenthesizedExpression,
Directive,
Hashbang,
BlockStatement,
VariableDeclaration,
VariableDeclarator,
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,
TSConditionalType,
TSUnionType,
TSIntersectionType,
TSParenthesizedType,
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,
TSConstructSignatureDeclaration,
TSInterfaceHeritage,
TSModuleDeclaration,
TSModuleBlock,
TSTypeLiteral,
TSInferType,
TSTypeQuery,
TSImportType,
TSMappedType,
TSTemplateLiteralType,
TSAsExpression,
TSSatisfiesExpression,
TSTypeAssertion,
TSImportEqualsDeclaration,
TSModuleReference,
TSExternalModuleReference,
TSNonNullExpression,
Decorator,
TSExportAssignment,
TSInstantiationExpression,
JSXElement,
JSXOpeningElement,
JSXClosingElement,
JSXFragment,
JSXElementName,
JSXNamespacedName,
JSXMemberExpression,
JSXMemberExpressionObject,
JSXExpressionContainer,
JSXAttributeItem,
JSXSpreadAttribute,
JSXIdentifier,
JSXText,
BooleanLiteral = 0,
NullLiteral = 1,
NumericLiteral = 2,
StringLiteral = 3,
BigIntLiteral = 4,
RegExpLiteral = 5,
Program = 6,
IdentifierName = 7,
IdentifierReference = 8,
BindingIdentifier = 9,
LabelIdentifier = 10,
ThisExpression = 11,
ArrayExpression = 12,
ArrayExpressionElement = 13,
Elision = 14,
ObjectExpression = 15,
ObjectProperty = 16,
PropertyKey = 17,
TemplateLiteral = 18,
TaggedTemplateExpression = 19,
MemberExpression = 20,
CallExpression = 21,
NewExpression = 22,
MetaProperty = 23,
SpreadElement = 24,
Argument = 25,
UpdateExpression = 26,
UnaryExpression = 27,
BinaryExpression = 28,
PrivateInExpression = 29,
LogicalExpression = 30,
ConditionalExpression = 31,
AssignmentExpression = 32,
AssignmentTarget = 33,
SimpleAssignmentTarget = 34,
AssignmentTargetPattern = 35,
ArrayAssignmentTarget = 36,
ObjectAssignmentTarget = 37,
AssignmentTargetWithDefault = 38,
SequenceExpression = 39,
Super = 40,
AwaitExpression = 41,
ChainExpression = 42,
ParenthesizedExpression = 43,
Directive = 44,
Hashbang = 45,
BlockStatement = 46,
VariableDeclaration = 47,
VariableDeclarator = 48,
EmptyStatement = 49,
ExpressionStatement = 50,
IfStatement = 51,
DoWhileStatement = 52,
WhileStatement = 53,
ForStatement = 54,
ForStatementInit = 55,
ForInStatement = 56,
ForOfStatement = 57,
ContinueStatement = 58,
BreakStatement = 59,
ReturnStatement = 60,
WithStatement = 61,
SwitchStatement = 62,
SwitchCase = 63,
LabeledStatement = 64,
ThrowStatement = 65,
TryStatement = 66,
CatchClause = 67,
CatchParameter = 68,
DebuggerStatement = 69,
AssignmentPattern = 70,
ObjectPattern = 71,
ArrayPattern = 72,
BindingRestElement = 73,
Function = 74,
FormalParameters = 75,
FormalParameter = 76,
FunctionBody = 77,
ArrowFunctionExpression = 78,
YieldExpression = 79,
Class = 80,
ClassBody = 81,
MethodDefinition = 82,
PropertyDefinition = 83,
PrivateIdentifier = 84,
StaticBlock = 85,
ModuleDeclaration = 86,
ImportExpression = 87,
ImportDeclaration = 88,
ImportSpecifier = 89,
ImportDefaultSpecifier = 90,
ImportNamespaceSpecifier = 91,
ExportNamedDeclaration = 92,
ExportDefaultDeclaration = 93,
ExportAllDeclaration = 94,
ExportSpecifier = 95,
TSThisParameter = 96,
TSEnumDeclaration = 97,
TSEnumMember = 98,
TSTypeAnnotation = 99,
TSLiteralType = 100,
TSConditionalType = 101,
TSUnionType = 102,
TSIntersectionType = 103,
TSParenthesizedType = 104,
TSIndexedAccessType = 105,
TSNamedTupleMember = 106,
TSAnyKeyword = 107,
TSStringKeyword = 108,
TSBooleanKeyword = 109,
TSNumberKeyword = 110,
TSNeverKeyword = 111,
TSIntrinsicKeyword = 112,
TSUnknownKeyword = 113,
TSNullKeyword = 114,
TSUndefinedKeyword = 115,
TSVoidKeyword = 116,
TSSymbolKeyword = 117,
TSThisType = 118,
TSObjectKeyword = 119,
TSBigIntKeyword = 120,
TSTypeReference = 121,
TSTypeName = 122,
TSQualifiedName = 123,
TSTypeParameterInstantiation = 124,
TSTypeParameter = 125,
TSTypeParameterDeclaration = 126,
TSTypeAliasDeclaration = 127,
TSClassImplements = 128,
TSInterfaceDeclaration = 129,
TSPropertySignature = 130,
TSMethodSignature = 131,
TSConstructSignatureDeclaration = 132,
TSInterfaceHeritage = 133,
TSModuleDeclaration = 134,
TSModuleBlock = 135,
TSTypeLiteral = 136,
TSInferType = 137,
TSTypeQuery = 138,
TSImportType = 139,
TSMappedType = 140,
TSTemplateLiteralType = 141,
TSAsExpression = 142,
TSSatisfiesExpression = 143,
TSTypeAssertion = 144,
TSImportEqualsDeclaration = 145,
TSModuleReference = 146,
TSExternalModuleReference = 147,
TSNonNullExpression = 148,
Decorator = 149,
TSExportAssignment = 150,
TSInstantiationExpression = 151,
JSXElement = 152,
JSXOpeningElement = 153,
JSXClosingElement = 154,
JSXFragment = 155,
JSXElementName = 156,
JSXNamespacedName = 157,
JSXMemberExpression = 158,
JSXMemberExpressionObject = 159,
JSXExpressionContainer = 160,
JSXAttributeItem = 161,
JSXSpreadAttribute = 162,
JSXIdentifier = 163,
JSXText = 164,
}
/// Untyped AST Node Kind
#[derive(Debug, Clone, Copy)]
#[repr(C, u8)]
pub enum AstKind<'a> {
BooleanLiteral(&'a BooleanLiteral),
NullLiteral(&'a NullLiteral),
NumericLiteral(&'a NumericLiteral<'a>),
StringLiteral(&'a StringLiteral<'a>),
BigIntLiteral(&'a BigIntLiteral<'a>),
RegExpLiteral(&'a RegExpLiteral<'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>),
AssignmentTargetPattern(&'a AssignmentTargetPattern<'a>),
ArrayAssignmentTarget(&'a ArrayAssignmentTarget<'a>),
ObjectAssignmentTarget(&'a ObjectAssignmentTarget<'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>),
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>),
TSConditionalType(&'a TSConditionalType<'a>),
TSUnionType(&'a TSUnionType<'a>),
TSIntersectionType(&'a TSIntersectionType<'a>),
TSParenthesizedType(&'a TSParenthesizedType<'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>),
TSConstructSignatureDeclaration(&'a TSConstructSignatureDeclaration<'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>),
TSMappedType(&'a TSMappedType<'a>),
TSTemplateLiteralType(&'a TSTemplateLiteralType<'a>),
TSAsExpression(&'a TSAsExpression<'a>),
TSSatisfiesExpression(&'a TSSatisfiesExpression<'a>),
TSTypeAssertion(&'a TSTypeAssertion<'a>),
TSImportEqualsDeclaration(&'a TSImportEqualsDeclaration<'a>),
TSModuleReference(&'a TSModuleReference<'a>),
TSExternalModuleReference(&'a TSExternalModuleReference<'a>),
TSNonNullExpression(&'a TSNonNullExpression<'a>),
Decorator(&'a Decorator<'a>),
TSExportAssignment(&'a TSExportAssignment<'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>),
BooleanLiteral(&'a BooleanLiteral) = AstType::BooleanLiteral as u8,
NullLiteral(&'a NullLiteral) = AstType::NullLiteral as u8,
NumericLiteral(&'a NumericLiteral<'a>) = AstType::NumericLiteral as u8,
StringLiteral(&'a StringLiteral<'a>) = AstType::StringLiteral as u8,
BigIntLiteral(&'a BigIntLiteral<'a>) = AstType::BigIntLiteral as u8,
RegExpLiteral(&'a RegExpLiteral<'a>) = AstType::RegExpLiteral as u8,
Program(&'a Program<'a>) = AstType::Program as u8,
IdentifierName(&'a IdentifierName<'a>) = AstType::IdentifierName as u8,
IdentifierReference(&'a IdentifierReference<'a>) = AstType::IdentifierReference as u8,
BindingIdentifier(&'a BindingIdentifier<'a>) = AstType::BindingIdentifier as u8,
LabelIdentifier(&'a LabelIdentifier<'a>) = AstType::LabelIdentifier as u8,
ThisExpression(&'a ThisExpression) = AstType::ThisExpression as u8,
ArrayExpression(&'a ArrayExpression<'a>) = AstType::ArrayExpression as u8,
ArrayExpressionElement(&'a ArrayExpressionElement<'a>) = AstType::ArrayExpressionElement as u8,
Elision(&'a Elision) = AstType::Elision as u8,
ObjectExpression(&'a ObjectExpression<'a>) = AstType::ObjectExpression as u8,
ObjectProperty(&'a ObjectProperty<'a>) = AstType::ObjectProperty as u8,
PropertyKey(&'a PropertyKey<'a>) = AstType::PropertyKey as u8,
TemplateLiteral(&'a TemplateLiteral<'a>) = AstType::TemplateLiteral as u8,
TaggedTemplateExpression(&'a TaggedTemplateExpression<'a>) =
AstType::TaggedTemplateExpression as u8,
MemberExpression(&'a MemberExpression<'a>) = AstType::MemberExpression as u8,
CallExpression(&'a CallExpression<'a>) = AstType::CallExpression as u8,
NewExpression(&'a NewExpression<'a>) = AstType::NewExpression as u8,
MetaProperty(&'a MetaProperty<'a>) = AstType::MetaProperty as u8,
SpreadElement(&'a SpreadElement<'a>) = AstType::SpreadElement as u8,
Argument(&'a Argument<'a>) = AstType::Argument as u8,
UpdateExpression(&'a UpdateExpression<'a>) = AstType::UpdateExpression as u8,
UnaryExpression(&'a UnaryExpression<'a>) = AstType::UnaryExpression as u8,
BinaryExpression(&'a BinaryExpression<'a>) = AstType::BinaryExpression as u8,
PrivateInExpression(&'a PrivateInExpression<'a>) = AstType::PrivateInExpression as u8,
LogicalExpression(&'a LogicalExpression<'a>) = AstType::LogicalExpression as u8,
ConditionalExpression(&'a ConditionalExpression<'a>) = AstType::ConditionalExpression as u8,
AssignmentExpression(&'a AssignmentExpression<'a>) = AstType::AssignmentExpression as u8,
AssignmentTarget(&'a AssignmentTarget<'a>) = AstType::AssignmentTarget as u8,
SimpleAssignmentTarget(&'a SimpleAssignmentTarget<'a>) = AstType::SimpleAssignmentTarget as u8,
AssignmentTargetPattern(&'a AssignmentTargetPattern<'a>) =
AstType::AssignmentTargetPattern as u8,
ArrayAssignmentTarget(&'a ArrayAssignmentTarget<'a>) = AstType::ArrayAssignmentTarget as u8,
ObjectAssignmentTarget(&'a ObjectAssignmentTarget<'a>) = AstType::ObjectAssignmentTarget as u8,
AssignmentTargetWithDefault(&'a AssignmentTargetWithDefault<'a>) =
AstType::AssignmentTargetWithDefault as u8,
SequenceExpression(&'a SequenceExpression<'a>) = AstType::SequenceExpression as u8,
Super(&'a Super) = AstType::Super as u8,
AwaitExpression(&'a AwaitExpression<'a>) = AstType::AwaitExpression as u8,
ChainExpression(&'a ChainExpression<'a>) = AstType::ChainExpression as u8,
ParenthesizedExpression(&'a ParenthesizedExpression<'a>) =
AstType::ParenthesizedExpression as u8,
Directive(&'a Directive<'a>) = AstType::Directive as u8,
Hashbang(&'a Hashbang<'a>) = AstType::Hashbang as u8,
BlockStatement(&'a BlockStatement<'a>) = AstType::BlockStatement as u8,
VariableDeclaration(&'a VariableDeclaration<'a>) = AstType::VariableDeclaration as u8,
VariableDeclarator(&'a VariableDeclarator<'a>) = AstType::VariableDeclarator as u8,
EmptyStatement(&'a EmptyStatement) = AstType::EmptyStatement as u8,
ExpressionStatement(&'a ExpressionStatement<'a>) = AstType::ExpressionStatement as u8,
IfStatement(&'a IfStatement<'a>) = AstType::IfStatement as u8,
DoWhileStatement(&'a DoWhileStatement<'a>) = AstType::DoWhileStatement as u8,
WhileStatement(&'a WhileStatement<'a>) = AstType::WhileStatement as u8,
ForStatement(&'a ForStatement<'a>) = AstType::ForStatement as u8,
ForStatementInit(&'a ForStatementInit<'a>) = AstType::ForStatementInit as u8,
ForInStatement(&'a ForInStatement<'a>) = AstType::ForInStatement as u8,
ForOfStatement(&'a ForOfStatement<'a>) = AstType::ForOfStatement as u8,
ContinueStatement(&'a ContinueStatement<'a>) = AstType::ContinueStatement as u8,
BreakStatement(&'a BreakStatement<'a>) = AstType::BreakStatement as u8,
ReturnStatement(&'a ReturnStatement<'a>) = AstType::ReturnStatement as u8,
WithStatement(&'a WithStatement<'a>) = AstType::WithStatement as u8,
SwitchStatement(&'a SwitchStatement<'a>) = AstType::SwitchStatement as u8,
SwitchCase(&'a SwitchCase<'a>) = AstType::SwitchCase as u8,
LabeledStatement(&'a LabeledStatement<'a>) = AstType::LabeledStatement as u8,
ThrowStatement(&'a ThrowStatement<'a>) = AstType::ThrowStatement as u8,
TryStatement(&'a TryStatement<'a>) = AstType::TryStatement as u8,
CatchClause(&'a CatchClause<'a>) = AstType::CatchClause as u8,
CatchParameter(&'a CatchParameter<'a>) = AstType::CatchParameter as u8,
DebuggerStatement(&'a DebuggerStatement) = AstType::DebuggerStatement as u8,
AssignmentPattern(&'a AssignmentPattern<'a>) = AstType::AssignmentPattern as u8,
ObjectPattern(&'a ObjectPattern<'a>) = AstType::ObjectPattern as u8,
ArrayPattern(&'a ArrayPattern<'a>) = AstType::ArrayPattern as u8,
BindingRestElement(&'a BindingRestElement<'a>) = AstType::BindingRestElement as u8,
Function(&'a Function<'a>) = AstType::Function as u8,
FormalParameters(&'a FormalParameters<'a>) = AstType::FormalParameters as u8,
FormalParameter(&'a FormalParameter<'a>) = AstType::FormalParameter as u8,
FunctionBody(&'a FunctionBody<'a>) = AstType::FunctionBody as u8,
ArrowFunctionExpression(&'a ArrowFunctionExpression<'a>) =
AstType::ArrowFunctionExpression as u8,
YieldExpression(&'a YieldExpression<'a>) = AstType::YieldExpression as u8,
Class(&'a Class<'a>) = AstType::Class as u8,
ClassBody(&'a ClassBody<'a>) = AstType::ClassBody as u8,
MethodDefinition(&'a MethodDefinition<'a>) = AstType::MethodDefinition as u8,
PropertyDefinition(&'a PropertyDefinition<'a>) = AstType::PropertyDefinition as u8,
PrivateIdentifier(&'a PrivateIdentifier<'a>) = AstType::PrivateIdentifier as u8,
StaticBlock(&'a StaticBlock<'a>) = AstType::StaticBlock as u8,
ModuleDeclaration(&'a ModuleDeclaration<'a>) = AstType::ModuleDeclaration as u8,
ImportExpression(&'a ImportExpression<'a>) = AstType::ImportExpression as u8,
ImportDeclaration(&'a ImportDeclaration<'a>) = AstType::ImportDeclaration as u8,
ImportSpecifier(&'a ImportSpecifier<'a>) = AstType::ImportSpecifier as u8,
ImportDefaultSpecifier(&'a ImportDefaultSpecifier<'a>) = AstType::ImportDefaultSpecifier as u8,
ImportNamespaceSpecifier(&'a ImportNamespaceSpecifier<'a>) =
AstType::ImportNamespaceSpecifier as u8,
ExportNamedDeclaration(&'a ExportNamedDeclaration<'a>) = AstType::ExportNamedDeclaration as u8,
ExportDefaultDeclaration(&'a ExportDefaultDeclaration<'a>) =
AstType::ExportDefaultDeclaration as u8,
ExportAllDeclaration(&'a ExportAllDeclaration<'a>) = AstType::ExportAllDeclaration as u8,
ExportSpecifier(&'a ExportSpecifier<'a>) = AstType::ExportSpecifier as u8,
TSThisParameter(&'a TSThisParameter<'a>) = AstType::TSThisParameter as u8,
TSEnumDeclaration(&'a TSEnumDeclaration<'a>) = AstType::TSEnumDeclaration as u8,
TSEnumMember(&'a TSEnumMember<'a>) = AstType::TSEnumMember as u8,
TSTypeAnnotation(&'a TSTypeAnnotation<'a>) = AstType::TSTypeAnnotation as u8,
TSLiteralType(&'a TSLiteralType<'a>) = AstType::TSLiteralType as u8,
TSConditionalType(&'a TSConditionalType<'a>) = AstType::TSConditionalType as u8,
TSUnionType(&'a TSUnionType<'a>) = AstType::TSUnionType as u8,
TSIntersectionType(&'a TSIntersectionType<'a>) = AstType::TSIntersectionType as u8,
TSParenthesizedType(&'a TSParenthesizedType<'a>) = AstType::TSParenthesizedType as u8,
TSIndexedAccessType(&'a TSIndexedAccessType<'a>) = AstType::TSIndexedAccessType as u8,
TSNamedTupleMember(&'a TSNamedTupleMember<'a>) = AstType::TSNamedTupleMember as u8,
TSAnyKeyword(&'a TSAnyKeyword) = AstType::TSAnyKeyword as u8,
TSStringKeyword(&'a TSStringKeyword) = AstType::TSStringKeyword as u8,
TSBooleanKeyword(&'a TSBooleanKeyword) = AstType::TSBooleanKeyword as u8,
TSNumberKeyword(&'a TSNumberKeyword) = AstType::TSNumberKeyword as u8,
TSNeverKeyword(&'a TSNeverKeyword) = AstType::TSNeverKeyword as u8,
TSIntrinsicKeyword(&'a TSIntrinsicKeyword) = AstType::TSIntrinsicKeyword as u8,
TSUnknownKeyword(&'a TSUnknownKeyword) = AstType::TSUnknownKeyword as u8,
TSNullKeyword(&'a TSNullKeyword) = AstType::TSNullKeyword as u8,
TSUndefinedKeyword(&'a TSUndefinedKeyword) = AstType::TSUndefinedKeyword as u8,
TSVoidKeyword(&'a TSVoidKeyword) = AstType::TSVoidKeyword as u8,
TSSymbolKeyword(&'a TSSymbolKeyword) = AstType::TSSymbolKeyword as u8,
TSThisType(&'a TSThisType) = AstType::TSThisType as u8,
TSObjectKeyword(&'a TSObjectKeyword) = AstType::TSObjectKeyword as u8,
TSBigIntKeyword(&'a TSBigIntKeyword) = AstType::TSBigIntKeyword as u8,
TSTypeReference(&'a TSTypeReference<'a>) = AstType::TSTypeReference as u8,
TSTypeName(&'a TSTypeName<'a>) = AstType::TSTypeName as u8,
TSQualifiedName(&'a TSQualifiedName<'a>) = AstType::TSQualifiedName as u8,
TSTypeParameterInstantiation(&'a TSTypeParameterInstantiation<'a>) =
AstType::TSTypeParameterInstantiation as u8,
TSTypeParameter(&'a TSTypeParameter<'a>) = AstType::TSTypeParameter as u8,
TSTypeParameterDeclaration(&'a TSTypeParameterDeclaration<'a>) =
AstType::TSTypeParameterDeclaration as u8,
TSTypeAliasDeclaration(&'a TSTypeAliasDeclaration<'a>) = AstType::TSTypeAliasDeclaration as u8,
TSClassImplements(&'a TSClassImplements<'a>) = AstType::TSClassImplements as u8,
TSInterfaceDeclaration(&'a TSInterfaceDeclaration<'a>) = AstType::TSInterfaceDeclaration as u8,
TSPropertySignature(&'a TSPropertySignature<'a>) = AstType::TSPropertySignature as u8,
TSMethodSignature(&'a TSMethodSignature<'a>) = AstType::TSMethodSignature as u8,
TSConstructSignatureDeclaration(&'a TSConstructSignatureDeclaration<'a>) =
AstType::TSConstructSignatureDeclaration as u8,
TSInterfaceHeritage(&'a TSInterfaceHeritage<'a>) = AstType::TSInterfaceHeritage as u8,
TSModuleDeclaration(&'a TSModuleDeclaration<'a>) = AstType::TSModuleDeclaration as u8,
TSModuleBlock(&'a TSModuleBlock<'a>) = AstType::TSModuleBlock as u8,
TSTypeLiteral(&'a TSTypeLiteral<'a>) = AstType::TSTypeLiteral as u8,
TSInferType(&'a TSInferType<'a>) = AstType::TSInferType as u8,
TSTypeQuery(&'a TSTypeQuery<'a>) = AstType::TSTypeQuery as u8,
TSImportType(&'a TSImportType<'a>) = AstType::TSImportType as u8,
TSMappedType(&'a TSMappedType<'a>) = AstType::TSMappedType as u8,
TSTemplateLiteralType(&'a TSTemplateLiteralType<'a>) = AstType::TSTemplateLiteralType as u8,
TSAsExpression(&'a TSAsExpression<'a>) = AstType::TSAsExpression as u8,
TSSatisfiesExpression(&'a TSSatisfiesExpression<'a>) = AstType::TSSatisfiesExpression as u8,
TSTypeAssertion(&'a TSTypeAssertion<'a>) = AstType::TSTypeAssertion as u8,
TSImportEqualsDeclaration(&'a TSImportEqualsDeclaration<'a>) =
AstType::TSImportEqualsDeclaration as u8,
TSModuleReference(&'a TSModuleReference<'a>) = AstType::TSModuleReference as u8,
TSExternalModuleReference(&'a TSExternalModuleReference<'a>) =
AstType::TSExternalModuleReference as u8,
TSNonNullExpression(&'a TSNonNullExpression<'a>) = AstType::TSNonNullExpression as u8,
Decorator(&'a Decorator<'a>) = AstType::Decorator as u8,
TSExportAssignment(&'a TSExportAssignment<'a>) = AstType::TSExportAssignment as u8,
TSInstantiationExpression(&'a TSInstantiationExpression<'a>) =
AstType::TSInstantiationExpression as u8,
JSXElement(&'a JSXElement<'a>) = AstType::JSXElement as u8,
JSXOpeningElement(&'a JSXOpeningElement<'a>) = AstType::JSXOpeningElement as u8,
JSXClosingElement(&'a JSXClosingElement<'a>) = AstType::JSXClosingElement as u8,
JSXFragment(&'a JSXFragment<'a>) = AstType::JSXFragment as u8,
JSXElementName(&'a JSXElementName<'a>) = AstType::JSXElementName as u8,
JSXNamespacedName(&'a JSXNamespacedName<'a>) = AstType::JSXNamespacedName as u8,
JSXMemberExpression(&'a JSXMemberExpression<'a>) = AstType::JSXMemberExpression as u8,
JSXMemberExpressionObject(&'a JSXMemberExpressionObject<'a>) =
AstType::JSXMemberExpressionObject as u8,
JSXExpressionContainer(&'a JSXExpressionContainer<'a>) = AstType::JSXExpressionContainer as u8,
JSXAttributeItem(&'a JSXAttributeItem<'a>) = AstType::JSXAttributeItem as u8,
JSXSpreadAttribute(&'a JSXSpreadAttribute<'a>) = AstType::JSXSpreadAttribute as u8,
JSXIdentifier(&'a JSXIdentifier<'a>) = AstType::JSXIdentifier as u8,
JSXText(&'a JSXText<'a>) = AstType::JSXText as u8,
}
impl AstKind<'_> {
/// Get the [`AstType`] of an [`AstKind`].
#[inline]
pub fn ty(&self) -> AstType {
// SAFETY: `AstKind` is `#[repr(C, u8)]`, so discriminant is stored in first byte,
// and it's valid to read it.
// `AstType` is also `#[repr(u8)]` and `AstKind` and `AstType` both have the same
// discriminants, so it's valid to read `AstKind`'s discriminant as `AstType`.
unsafe { *ptr::from_ref(self).cast::<AstType>().as_ref().unwrap_unchecked() }
}
}
impl GetSpan for AstKind<'_> {

View file

@ -1,7 +1,8 @@
use convert_case::{Case, Casing};
use itertools::Itertools;
use proc_macro2::Span;
use quote::{format_ident, quote};
use syn::{parse_quote, Arm, ImplItemFn, Variant};
use syn::{parse_quote, Arm, ImplItemFn, LitInt};
use crate::{
output::{output_path, Output},
@ -97,10 +98,17 @@ impl Generator for AstKindGenerator {
})
.collect_vec();
let types = have_kinds.iter().map(|(ident, _)| ident).collect_vec();
let kinds: Vec<Variant> =
have_kinds.iter().map(|(ident, typ)| parse_quote!(#ident(&'a #typ))).collect_vec();
let (types, kinds): (Vec<_>, Vec<_>) = have_kinds
.iter()
.enumerate()
.map(|(index, (ident, typ))| {
let index = u8::try_from(index).unwrap();
let index = LitInt::new(&index.to_string(), Span::call_site());
let type_variant = quote!( #ident = #index );
let kind_variant = quote!( #ident(&'a #typ) = AstType::#ident as u8 );
(type_variant, kind_variant)
})
.unzip();
let span_matches: Vec<Arm> = have_kinds
.iter()
@ -131,6 +139,9 @@ impl Generator for AstKindGenerator {
tokens: quote! {
#![allow(missing_docs)] ///@ FIXME (in ast_tools/src/generators/ast_kind.rs)
///@@line_break
use std::ptr;
///@@line_break
use oxc_span::{GetSpan, Span};
@ -138,7 +149,8 @@ impl Generator for AstKindGenerator {
use crate::ast::*;
///@@line_break
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum AstType {
#(#types),*,
}
@ -146,10 +158,24 @@ impl Generator for AstKindGenerator {
///@@line_break
/// Untyped AST Node Kind
#[derive(Debug, Clone, Copy)]
#[repr(C, u8)]
pub enum AstKind<'a> {
#(#kinds),*,
}
///@@line_break
impl AstKind<'_> {
/// Get the [`AstType`] of an [`AstKind`].
#[inline]
pub fn ty(&self) -> AstType {
///@ SAFETY: `AstKind` is `#[repr(C, u8)]`, so discriminant is stored in first byte,
///@ and it's valid to read it.
///@ `AstType` is also `#[repr(u8)]` and `AstKind` and `AstType` both have the same
///@ discriminants, so it's valid to read `AstKind`'s discriminant as `AstType`.
unsafe { *ptr::from_ref(self).cast::<AstType>().as_ref().unwrap_unchecked() }
}
}
///@@line_break
impl GetSpan for AstKind<'_> {
#[allow(clippy::match_same_arms)]