use crate::{
CppSyntaxNode,
kind::{CppKind, CppSyntaxKind, CppTokenKind},
syntax::traits::{CppAstChildren, CppAstNode, CppAstToken},
};
use super::{CppKeywordToken, CppNameToken, CppPunctuationToken, CppStat};
mod expr;
mod modules;
mod stat;
pub use expr::*;
pub use modules::*;
pub use stat::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppTranslationUnit {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppTranslationUnit {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::TranslationUnit
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppTranslationUnit {
pub fn get_declarations(&self) -> CppAstChildren<CppDeclaration> {
self.children()
}
pub fn get_namespaces(&self) -> CppAstChildren<CppNamespaceDecl> {
self.children()
}
pub fn get_module_decl(&self) -> Option<CppModuleDecl> {
self.child()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppDeclaration {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppDeclaration {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::Declaration
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
matches!(
CppSyntaxKind::from(syntax.kind()),
CppSyntaxKind::Declaration | CppSyntaxKind::UsingDecl
)
.then_some(Self { syntax })
}
}
impl CppDeclaration {
pub fn get_decl_specifiers(&self) -> Option<CppDeclSpecifierSeq> {
self.child()
}
pub fn get_init_declarators(&self) -> CppAstChildren<CppInitDeclarator> {
self.children()
}
pub fn get_init_declarator(&self) -> Option<CppInitDeclarator> {
self.child()
}
pub fn get_name(&self) -> Option<CppNameToken> {
if CppSyntaxKind::from(self.syntax.kind()) == CppSyntaxKind::UsingDecl {
return crate::syntax::node::traits::first_child_of_kind(
self.syntax(),
&[CppSyntaxKind::NameExpr],
)
.and_then(CppNameExpr::cast)
.and_then(|name| name.get_name_token());
}
if let Some(class) = self.get_class_def()
&& let Some(name) = class.get_name()
{
return Some(name);
}
if let Some(enum_def) = self.get_enum_def()
&& let Some(name) = enum_def.get_name()
{
return Some(name);
}
if let Some(declarator) = self.get_init_declarator()
&& let Some(name) = declarator.get_name()
{
return Some(name);
}
if let Some(declarator) = self.child::<CppDeclarator>()
&& let Some(name) = declarator.get_name()
&& !name.get_text().is_empty()
{
return Some(name);
}
if let Some(wrapped) = self.child::<CppDeclaration>()
&& let Some(name) = wrapped.get_name()
&& !name.get_text().is_empty()
{
return Some(name);
}
self.elaborated_type_name()
}
fn elaborated_type_name(&self) -> Option<CppNameToken> {
let specifiers = self.get_decl_specifiers()?;
let builtin = specifiers.get_builtin_type();
let has_class_keyword = builtin
.as_ref()
.is_some_and(|builtin| builtin.is_class_like());
if !has_class_keyword {
return None;
}
specifiers
.get_type_name()
.and_then(|name| name.get_name_token())
}
pub fn get_name_text(&self) -> Option<String> {
self.get_name().map(|it| it.get_name_text().to_string())
}
pub fn get_declarator(&self) -> Option<CppDeclarator> {
self.get_init_declarator()?.get_declarator()
}
pub fn get_initializer(&self) -> Option<CppInitializer> {
self.get_init_declarator()?.get_initializer()
}
pub fn get_body(&self) -> Option<CppCompoundStat> {
self.child()
}
pub fn get_class_def(&self) -> Option<CppClassDef> {
self.get_decl_specifiers()?.get_class_def()
}
pub fn get_enum_def(&self) -> Option<CppEnumDef> {
self.get_decl_specifiers()?
.syntax()
.descendants()
.filter_map(CppEnumDef::cast)
.find(|it| it.get_body().is_some())
}
pub fn get_namespace_decl(&self) -> Option<CppNamespaceDecl> {
self.child()
}
pub fn get_template_decl(&self) -> Option<CppTemplateDecl> {
self.child()
}
pub fn is_function_def(&self) -> bool {
self.get_body().is_some() && self.get_declarator().is_some_and(|it| it.is_function())
}
pub fn is_exported(&self) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| token.kind() == CppKind::Token(CppTokenKind::ExportKeyword))
}
pub fn kind_name(&self) -> &'static str {
if CppSyntaxKind::from(self.syntax.kind()) == CppSyntaxKind::UsingDecl {
"alias"
} else if self.get_enum_def().is_some() {
"enum"
} else if self.get_class_def().is_some() {
"class"
} else if self.get_declarator().is_some_and(|it| it.is_function()) {
"function"
} else if self.get_decl_specifiers().is_some_and(|it| it.is_alias()) {
"alias"
} else {
"variable"
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppDeclSpecifierSeq {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppDeclSpecifierSeq {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::DeclSpecifierSeq
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppDeclSpecifierSeq {
pub fn get_builtin_type(&self) -> Option<CppBuiltinType> {
self.child()
}
pub fn get_type_name(&self) -> Option<CppNameExpr> {
if let Some(name) = self.child::<CppNameExpr>() {
return Some(name);
}
let wrapped = crate::syntax::node::traits::first_child_of_kind(
self.syntax(),
&[CppSyntaxKind::TemplateType, CppSyntaxKind::NamedType],
)?;
wrapped.children().find_map(CppNameExpr::cast)
}
pub fn get_class_def(&self) -> Option<CppClassDef> {
self.child()
}
pub fn get_enum_def(&self) -> Option<CppEnumDef> {
self.child()
}
pub fn is_alias(&self) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| token.kind() == CppKind::Token(CppTokenKind::Assign))
}
pub fn has_keyword(&self, kind: CppTokenKind) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| token.kind() == kind.into())
}
pub fn is_const(&self) -> bool {
self.has_keyword(CppTokenKind::ConstKeyword)
}
pub fn is_static(&self) -> bool {
self.has_keyword(CppTokenKind::StaticKeyword)
}
pub fn is_virtual(&self) -> bool {
self.has_keyword(CppTokenKind::VirtualKeyword)
}
pub fn get_type_text(&self) -> String {
crate::syntax::node::traits::subtree_text_spaced(self.syntax())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppBuiltinType {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppBuiltinType {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::BuiltinType
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppBuiltinType {
pub fn get_keyword(&self) -> Option<CppKeywordToken> {
self.token()
}
pub fn get_type_text(&self) -> String {
crate::syntax::node::traits::subtree_text_spaced(self.syntax())
}
pub fn is_class_like(&self) -> bool {
matches!(
self.syntax()
.children_with_tokens()
.next()
.and_then(|element| element.into_token())
.map(|token| token.kind()),
Some(CppKind::Token(CppTokenKind::ClassKeyword))
| Some(CppKind::Token(CppTokenKind::StructKeyword))
| Some(CppKind::Token(CppTokenKind::UnionKeyword))
| Some(CppKind::Token(CppTokenKind::EnumKeyword))
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppTypeId {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppTypeId {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
matches!(
kind,
CppSyntaxKind::TypeId | CppSyntaxKind::TrailingReturnType
)
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppTypeId {
pub fn get_decl_specifiers(&self) -> Option<CppDeclSpecifierSeq> {
self.child()
}
pub fn get_type_text(&self) -> String {
let text = self.syntax().text().to_string();
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppDeclarator {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppDeclarator {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::Declarator
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppDeclarator {
pub fn get_name(&self) -> Option<CppNameToken> {
self.token().or_else(|| {
self.child::<CppNameExpr>()
.and_then(|it| it.get_name_token())
})
}
pub fn get_name_text(&self) -> Option<String> {
self.get_name().map(|it| it.get_name_text().to_string())
}
pub fn get_declarator_text(&self) -> String {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.filter(|token| !crate::syntax::node::traits::is_trivia(token.kind().into()))
.map(|token| token.text().to_string())
.collect::<Vec<_>>()
.join("")
.chars()
.filter(|it| !it.is_whitespace())
.collect()
}
pub fn get_param_list(&self) -> Option<CppParamList> {
self.child()
}
pub fn get_trailing_return_type(&self) -> Option<CppTypeId> {
let wrapper = crate::syntax::node::traits::first_child_of_kind(
self.syntax(),
&[CppSyntaxKind::TrailingReturnType],
);
match wrapper {
Some(wrapper) => wrapper.children().find_map(CppTypeId::cast),
None => crate::syntax::node::traits::first_child_of_kind(
self.syntax(),
&[CppSyntaxKind::TypeId],
)
.and_then(CppTypeId::cast),
}
}
pub fn get_pointer_type(&self) -> Option<CppPointerType> {
self.child()
}
pub fn is_function(&self) -> bool {
self.get_param_list().is_some() || self.get_trailing_return_type().is_some()
}
pub fn is_array(&self) -> bool {
self.get_array_type().is_some()
}
pub fn get_array_type(&self) -> Option<CppArrayType> {
self.child()
}
pub fn get_structured_binding(&self) -> Option<CppStructuredBinding> {
if let Some(binding) = self.child::<CppStructuredBinding>() {
return Some(binding);
}
self.syntax()
.parent()
.and_then(|parent| {
crate::syntax::node::traits::first_child_of_kind(
&parent,
&[CppSyntaxKind::StructuredBinding],
)
})
.and_then(CppStructuredBinding::cast)
}
pub fn is_structured_binding(&self) -> bool {
self.get_structured_binding().is_some()
}
pub fn get_binding_names(&self) -> Vec<CppNameToken> {
self.get_structured_binding()
.map(|binding| binding.get_names().collect())
.unwrap_or_default()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppStructuredBinding {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppStructuredBinding {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::StructuredBinding
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppStructuredBinding {
pub fn get_names(&self) -> impl Iterator<Item = CppNameToken> {
self.syntax()
.descendants()
.filter_map(CppNameExpr::cast)
.filter_map(|name| name.get_name_token())
}
pub fn get_name_texts(&self) -> Vec<String> {
self.get_names()
.map(|name| name.get_name_text().to_string())
.collect()
}
pub fn len(&self) -> usize {
self.get_names().count()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn is_pack_expansion(&self) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| token.kind() == CppKind::Token(CppTokenKind::Ellipsis))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppInitDeclarator {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppInitDeclarator {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::InitDeclarator
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppInitDeclarator {
pub fn get_declarator(&self) -> Option<CppDeclarator> {
self.child()
}
pub fn get_name(&self) -> Option<CppNameToken> {
if let Some(binding) = self.get_structured_binding() {
return binding.get_names().next();
}
self.get_declarator()?.get_name()
}
pub fn get_initializer(&self) -> Option<CppInitializer> {
self.child()
}
pub fn get_structured_binding(&self) -> Option<CppStructuredBinding> {
if let Some(binding) = self.child::<CppStructuredBinding>() {
return Some(binding);
}
self.get_declarator()?.get_structured_binding()
}
pub fn is_structured_binding(&self) -> bool {
self.get_structured_binding().is_some()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppInitializer {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppInitializer {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::Initializer
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppInitializer {
pub fn get_expr(&self) -> Option<CppExpr> {
self.child()
}
pub fn get_init_list(&self) -> Option<CppInitListExpr> {
self.child()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppPointerType {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppPointerType {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
matches!(
kind,
CppSyntaxKind::PointerType
| CppSyntaxKind::ReferenceType
| CppSyntaxKind::RValueReferenceType
)
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppPointerType {
pub fn is_rvalue_reference(&self) -> bool {
CppSyntaxKind::from(self.syntax().kind()) == CppSyntaxKind::RValueReferenceType
}
pub fn is_reference(&self) -> bool {
matches!(
CppSyntaxKind::from(self.syntax().kind()),
CppSyntaxKind::ReferenceType | CppSyntaxKind::RValueReferenceType
)
}
pub fn get_operator_token(&self) -> Option<CppPunctuationToken> {
self.token()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppArrayType {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppArrayType {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::ArrayType
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppArrayType {
pub fn get_size_expr(&self) -> Option<CppExpr> {
self.child()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppParamList {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppParamList {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::ParameterList
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppParamList {
pub fn get_params(&self) -> CppAstChildren<CppParam> {
self.children()
}
pub fn is_variadic(&self) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| token.kind() == CppKind::Token(CppTokenKind::Ellipsis))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppParam {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppParam {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::Parameter
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppParam {
pub fn get_decl_specifiers(&self) -> Option<CppDeclSpecifierSeq> {
self.child()
}
pub fn get_declarator(&self) -> Option<CppDeclarator> {
self.child()
}
pub fn get_name(&self) -> Option<CppNameToken> {
self.get_declarator()?.get_name()
}
pub fn get_default_value(&self) -> Option<CppInitializer> {
self.child()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppClassDef {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppClassDef {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
matches!(
kind,
CppSyntaxKind::ClassDef | CppSyntaxKind::StructDef | CppSyntaxKind::UnionDef
)
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppClassDef {
pub fn get_name(&self) -> Option<CppNameToken> {
self.child::<CppNameExpr>()
.and_then(|it| it.get_name_token())
}
pub fn get_name_text(&self) -> Option<String> {
self.get_name().map(|it| it.get_name_text().to_string())
}
pub fn is_struct(&self) -> bool {
CppSyntaxKind::from(self.syntax().kind()) == CppSyntaxKind::StructDef
}
pub fn is_union(&self) -> bool {
CppSyntaxKind::from(self.syntax().kind()) == CppSyntaxKind::UnionDef
}
pub fn get_body(&self) -> Option<CppClassBody> {
self.child()
}
pub fn get_base_specifiers(&self) -> CppAstChildren<CppBaseSpecifier> {
self.children()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppClassBody {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppClassBody {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::ClassBody
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppClassBody {
pub fn get_members(&self) -> CppAstChildren<CppDeclaration> {
self.children()
}
pub fn get_access_specifiers(&self) -> Vec<CppAccessSpecifier> {
self.syntax()
.children()
.filter_map(CppAccessSpecifier::cast)
.collect()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppAccessSpecifier {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppAccessSpecifier {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
matches!(
kind,
CppSyntaxKind::PublicAccess
| CppSyntaxKind::PrivateAccess
| CppSyntaxKind::ProtectedAccess
)
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppAccessSpecifier {
pub fn get_keyword(&self) -> Option<CppKeywordToken> {
self.token()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppBaseSpecifier {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppBaseSpecifier {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::BaseSpecifier
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppBaseSpecifier {
pub fn get_name(&self) -> Option<CppNameExpr> {
self.child()
}
pub fn is_virtual(&self) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| token.kind() == CppKind::Token(CppTokenKind::VirtualKeyword))
}
pub fn get_access(&self) -> Option<CppAccessSpecifier> {
self.child()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppEnumDef {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppEnumDef {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
matches!(
kind,
CppSyntaxKind::EnumDef | CppSyntaxKind::EnumClassDef | CppSyntaxKind::BuiltinType
)
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppEnumDef {
pub fn get_body(&self) -> Option<CppSyntaxNode> {
self.syntax()
.children()
.find(|it| CppSyntaxKind::from(it.kind()) == CppSyntaxKind::CompoundStat)
}
pub fn get_enum_keyword(&self) -> Option<CppKeywordToken> {
self.syntax()
.descendants_with_tokens()
.filter_map(|it| it.into_token())
.filter_map(CppKeywordToken::cast)
.find(|it| it.get_keyword_kind() == CppKind::Token(CppTokenKind::EnumKeyword))
}
pub fn get_name(&self) -> Option<CppNameToken> {
self.get_enum_keyword()?;
self.syntax()
.descendants()
.find_map(CppNameExpr::cast)
.and_then(|it| it.get_name_token())
}
pub fn is_scoped(&self) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| {
matches!(
token.kind(),
CppKind::Token(CppTokenKind::ClassKeyword)
| CppKind::Token(CppTokenKind::StructKeyword)
)
})
}
pub fn get_enumerators(&self) -> impl Iterator<Item = CppEnumerator> {
crate::syntax::node::traits::first_child_of_kind(
self.syntax(),
&[CppSyntaxKind::CompoundStat],
)
.into_iter()
.flat_map(|body| CppAstChildren::<CppEnumerator>::new(&body))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppEnumerator {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppEnumerator {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::EnumeratorDecl
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppEnumerator {
pub fn get_name(&self) -> Option<CppNameToken> {
self.token()
}
pub fn get_value_expr(&self) -> Option<CppExpr> {
self.child()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppNamespaceDecl {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppNamespaceDecl {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::NamespaceDecl
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppNamespaceDecl {
pub fn get_name(&self) -> Option<CppNameExpr> {
self.child()
}
pub fn get_declarations(&self) -> impl Iterator<Item = CppStat> {
self.get_body()
.into_iter()
.flat_map(|body| body.get_stats())
}
pub fn get_body(&self) -> Option<CppCompoundStat> {
self.child()
}
pub fn is_alias(&self) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| token.kind() == CppKind::Token(CppTokenKind::Assign))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppTemplateDecl {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppTemplateDecl {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::TemplateDecl
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppTemplateDecl {
pub fn get_param_list(&self) -> Option<CppTemplateParamList> {
self.child()
}
pub fn is_explicit_specialization(&self) -> bool {
self.get_param_list().is_none()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppTemplateParamList {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppTemplateParamList {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::TemplateParameterList
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppTemplateParamList {
pub fn get_params(&self) -> CppAstChildren<CppTemplateParam> {
self.children()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppTemplateParam {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppTemplateParam {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::TemplateParameter
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppTemplateParam {
pub fn get_decl_specifiers(&self) -> Option<CppDeclSpecifierSeq> {
self.child()
}
pub fn get_name(&self) -> Option<CppNameToken> {
self.get_declarator()?.get_name()
}
pub fn get_declarator(&self) -> Option<CppDeclarator> {
self.child()
}
pub fn is_type_param(&self) -> bool {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.any(|token| {
matches!(
token.kind(),
CppKind::Token(CppTokenKind::TypenameKeyword)
| CppKind::Token(CppTokenKind::ClassKeyword)
)
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppTemplateArgList {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppTemplateArgList {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::TemplateArgumentList
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppTemplateArgList {
pub fn get_args(&self) -> CppAstChildren<CppTemplateArg> {
self.children()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppTemplateArg {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppTemplateArg {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::TemplateArgument
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppTemplateArg {
pub fn get_type_id(&self) -> Option<CppTypeId> {
self.child()
}
pub fn get_expr(&self) -> Option<CppExpr> {
self.child()
}
}