use crate::{ident::Ident, span::Span};
use super::{
attribute::Annotation,
path::{Path, TypeArguments},
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Type {
Primitive(PrimitiveType),
Reference(ReferenceType),
Void(Span),
}
impl Type {
pub fn span(&self) -> Span {
match self {
Self::Primitive(p) => p.span(),
Self::Reference(r) => r.span(),
Self::Void(s) => *s,
}
}
pub fn is_void(&self) -> bool {
matches!(self, Self::Void(_))
}
pub fn is_primitive(&self) -> bool {
matches!(self, Self::Primitive(_))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PrimitiveType {
Byte,
Short,
Int,
Long,
Char,
Float,
Double,
Boolean,
}
impl PrimitiveType {
pub fn as_str(&self) -> &'static str {
match self {
Self::Byte => "byte",
Self::Short => "short",
Self::Int => "int",
Self::Long => "long",
Self::Char => "char",
Self::Float => "float",
Self::Double => "double",
Self::Boolean => "boolean",
}
}
pub fn span(&self) -> Span {
Span::call_site() }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ReferenceType {
ClassOrInterfaceType(ClassOrInterfaceType),
TypeVar(Ident),
Array(ArrayType),
}
impl ReferenceType {
pub fn span(&self) -> Span {
match self {
Self::ClassOrInterfaceType(t) => t.span(),
Self::TypeVar(i) => i.span(),
Self::Array(a) => a.span,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ClassOrInterfaceType {
pub path: Path,
pub annotations_prefix: Vec<Annotation>, }
impl ClassOrInterfaceType {
pub fn span(&self) -> Span {
self.path.span
}
pub fn name(&self) -> &Ident {
self.path.last_ident()
}
pub fn type_args(&self) -> Option<&TypeArguments> {
self.path.last_segment().args.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ArrayType {
pub elem_type: Box<Type>,
pub dims: Vec<ArrayDim>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ArrayDim {
pub bracket_span: (Span, Span),
pub annotations: Vec<Annotation>,
}