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 | CppSyntaxKind::TypedefDecl
)
.then_some(Self { syntax })
}
}
impl CppDeclaration {
pub fn get_decl_specifiers(&self) -> Option<CppDeclSpecifierSeq> {
self.child()
}
pub fn is_a_typedef(&self) -> bool {
CppSyntaxKind::from(self.syntax().kind()) == CppSyntaxKind::TypedefDecl
|| self
.get_decl_specifiers()
.is_some_and(|specifiers| specifiers.has_keyword(CppTokenKind::TypedefKeyword))
}
pub fn get_init_declarators(&self) -> CppAstChildren<CppInitDeclarator> {
self.children()
}
pub fn get_init_declarator(&self) -> Option<CppInitDeclarator> {
self.child()
}
pub fn get_type_text_of(&self, name: &str) -> Option<String> {
let base = self
.get_decl_specifiers()
.map(|specifiers| specifiers.get_type_text())
.unwrap_or_default();
for declarator in self.get_init_declarators() {
let declares_it = declarator
.get_name()
.is_some_and(|written| written.get_name_text() == name);
if !declares_it {
continue;
}
let mut the_type = base.clone();
if let Some(declarator) = declarator.get_declarator() {
the_type.push_str(&declarator.get_type_suffix());
}
let the_type = the_type.trim().to_string();
if !the_type.is_empty() {
return Some(the_type);
}
}
None
}
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);
}
if self.get_param_list().is_some()
&& let Some(specifiers) = self.get_decl_specifiers()
&& specifiers.get_builtin_type().is_none()
&& let Some(name) = specifiers
.descendants::<CppNameExpr>()
.last()
.and_then(|name| name.get_name_token())
{
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())
}
fn without_template_arguments(written: &str) -> &str {
match written.find('<') {
Some(at) => written[..at].trim_end(),
None => written,
}
}
pub fn promises_no_return(&self) -> bool {
let tilde = self
.syntax()
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.take_while(|token| CppTokenKind::from(token.kind()) != CppTokenKind::LeftParen)
.any(|token| CppTokenKind::from(token.kind()) == CppTokenKind::Tilde);
if tilde {
return true;
}
let written = self
.get_declarator()
.or_else(|| self.child::<CppDeclarator>())
.and_then(|declarator| declarator.child::<CppNameExpr>())
.map(|name| name.get_qualified_name())
.filter(|name| !name.trim().is_empty())
.or_else(|| self.get_name_text());
let Some(written) = written else {
return false;
};
let written = written.trim();
let segments: Vec<&str> = written
.split("::")
.map(str::trim)
.filter(|part| !part.is_empty())
.collect();
let last = segments
.last()
.map(|part| Self::without_template_arguments(part));
let before = segments
.len()
.checked_sub(2)
.map(|at| Self::without_template_arguments(segments[at]));
if let (Some(last), Some(before)) = (last, before)
&& last == before
{
return true;
}
let enclosing = crate::syntax::node::traits::typed_ancestors::<CppClassDef>(self.syntax())
.next()
.and_then(|class| class.get_name_text());
match (enclosing, last) {
(Some(class), Some(last)) => Self::without_template_arguments(class.trim()) == last,
_ => false,
}
}
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_param_list(&self) -> Option<CppParamList> {
self.get_init_declarator()
.and_then(|init| init.get_declarator())
.and_then(|declarator| declarator.get_param_list())
.or_else(|| {
self.children::<CppDeclarator>()
.find_map(|declarator| declarator.get_param_list())
})
}
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 is_void(&self) -> bool {
self.has_keyword(CppTokenKind::VoidKeyword)
}
pub fn is_deduced_return_type(&self) -> bool {
self.has_keyword(CppTokenKind::AutoKeyword)
|| self.has_keyword(CppTokenKind::DecltypeKeyword)
}
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 get_attribute_lists(&self) -> CppAstChildren<CppAttributeList> {
self.children()
}
pub fn has_attribute(&self, name: &str) -> bool {
self.get_attribute_lists().any(|list| list.has_a_name(name))
}
pub fn has_keyword(&self, kind: CppTokenKind) -> bool {
self.syntax()
.descendants_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 is_void(&self) -> bool {
self.get_decl_specifiers()
.is_some_and(|specifiers| specifiers.has_keyword(CppTokenKind::VoidKeyword))
}
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_pointer_types(&self) -> Vec<CppPointerType> {
let mut found = Vec::new();
let mut current = Some(self.clone());
while let Some(declarator) = current {
for child in declarator.syntax().children() {
if let Some(pointer) = CppPointerType::cast(child) {
found.push(pointer);
}
}
current = declarator.child::<CppDeclarator>();
}
found
}
pub fn is_a_reference(&self) -> bool {
self.get_pointer_types()
.iter()
.any(|pointer| pointer.is_reference())
}
pub fn is_a_pointer(&self) -> bool {
self.get_pointer_types()
.iter()
.any(|pointer| !pointer.is_reference())
}
pub fn get_type_suffix(&self) -> String {
let mut out = String::new();
for element in self.syntax().children_with_tokens() {
match element {
rowan::NodeOrToken::Node(child) => match CppSyntaxKind::from(child.kind()) {
CppSyntaxKind::PointerType
| CppSyntaxKind::ReferenceType
| CppSyntaxKind::RValueReferenceType => {
if let Some(pointer) = CppPointerType::cast(child) {
out.push_str(&pointer.get_operator_text());
}
}
CppSyntaxKind::ArrayType => {
if let Some(array) = CppArrayType::cast(child) {
out.push_str(&array.get_type_text());
}
}
CppSyntaxKind::Declarator => {
if let Some(inner) = CppDeclarator::cast(child) {
out.push_str(&inner.get_type_suffix());
}
}
_ => {}
},
rowan::NodeOrToken::Token(token) => match token.kind() {
CppKind::Token(CppTokenKind::Star)
| CppKind::Token(CppTokenKind::Ampersand)
| CppKind::Token(CppTokenKind::LogicalAnd) => {
out.push_str(token.text());
}
_ => {}
},
}
}
out
}
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_requires_clause(&self) -> Option<CppRequiresClause> {
self.child()
}
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_text(&self) -> String {
for kind in [
CppTokenKind::Star,
CppTokenKind::Ampersand,
CppTokenKind::LogicalAnd,
] {
if let Some(token) = self.token_by_kind(kind) {
return token.get_text().to_string();
}
}
String::new()
}
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()
}
pub fn get_type_text(&self) -> String {
crate::syntax::node::traits::subtree_text(self.syntax())
}
}
fn push_a_spelling(out: &mut String, spelling: &str) {
let word_like = |character: char| character.is_alphanumeric() || character == '_';
if out.chars().next_back().is_some_and(word_like)
&& spelling.chars().next().is_some_and(word_like)
{
out.push(' ');
}
out.push_str(spelling);
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppTrailingReturnType {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppTrailingReturnType {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::TrailingReturnType
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppTrailingReturnType {
pub fn get_type_id(&self) -> Option<CppTypeId> {
self.child()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppFriendDecl {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppFriendDecl {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::FriendDecl
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppFriendDecl {
pub fn get_declaration(&self) -> Option<CppDeclaration> {
self.child()
}
pub fn get_name_text(&self) -> Option<String> {
self.get_declaration()?.get_name_text()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppMemberInitializer {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppMemberInitializer {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::MemberInitializer
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppMemberInitializer {
pub fn get_name(&self) -> Option<CppNameToken> {
self.child::<CppNameExpr>()?.get_name_token()
}
pub fn get_name_text(&self) -> Option<String> {
self.get_name().map(|name| name.get_name_text().to_string())
}
pub fn get_argument(&self) -> Option<CppExpr> {
if let Some(list) = self.child::<CppArgList>() {
return list.get_args().next();
}
if let Some(braced) = self.child::<CppInitListExpr>() {
return braced.get_elements().next();
}
self.children::<CppExpr>().next()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppRequiresExpr {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppRequiresExpr {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::RequiresExpr
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppRequiresClause {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppRequiresClause {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::RequiresClause
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppRequiresClause {
pub fn get_clause_text(&self) -> String {
crate::syntax::node::traits::subtree_text_spaced(self.syntax())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CppAttributeList {
syntax: CppSyntaxNode,
}
impl CppAstNode for CppAttributeList {
fn syntax(&self) -> &CppSyntaxNode {
&self.syntax
}
fn can_cast(kind: CppSyntaxKind) -> bool {
kind == CppSyntaxKind::AttributeList
}
fn cast(syntax: CppSyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind().into()).then_some(Self { syntax })
}
}
impl CppAttributeList {
pub fn get_names(&self) -> Vec<String> {
self.syntax()
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.filter(|token| token.kind() == CppKind::Token(CppTokenKind::Identifier))
.map(|token| token.text().to_string())
.collect()
}
pub fn has_a_name(&self, name: &str) -> bool {
self.get_names().iter().any(|known| known == name)
}
}
#[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_name_text(&self) -> Option<String> {
self.get_name().map(|name| name.get_name_text().to_string())
}
pub fn is_auto(&self) -> bool {
self.get_decl_specifiers()
.and_then(|specifiers| specifiers.get_builtin_type())
.and_then(|builtin| builtin.get_keyword())
.is_some_and(|keyword| {
keyword.get_keyword_kind() == CppKind::Token(CppTokenKind::AutoKeyword)
})
}
pub fn get_type_text(&self) -> String {
let mut the_type = String::new();
for element in self.syntax().children_with_tokens() {
match element {
rowan::NodeOrToken::Node(child) => {
if let Some(declarator) = CppDeclarator::cast(child.clone()) {
the_type.push_str(&declarator.get_type_suffix());
break;
}
if let Some(specifiers) = CppDeclSpecifierSeq::cast(child) {
push_a_spelling(&mut the_type, &specifiers.get_type_text());
}
}
rowan::NodeOrToken::Token(token) => {
if !crate::syntax::node::traits::is_trivia(token.kind().into()) {
push_a_spelling(&mut the_type, token.text());
}
}
}
}
the_type.trim().to_string()
}
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_name_text(&self) -> Option<String> {
let name = self.get_name()?;
Some(name.syntax().text().to_string().trim().to_string())
}
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()
.and_then(|declarator| declarator.get_name())
.or_else(|| {
self.syntax()
.descendants()
.find_map(CppNameExpr::cast)
.and_then(|name| name.get_name_token())
})
}
pub fn get_declarator(&self) -> Option<CppDeclarator> {
self.child()
}
pub fn is_type_param(&self) -> bool {
self.syntax()
.descendants_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()
}
}