1use crate::{ident::Ident, span::Span};
4
5use super::{
6 attribute::Annotation,
7 path::{Path, TypeArguments},
8};
9
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub enum Type {
13 Primitive(PrimitiveType),
15 Reference(ReferenceType),
17 Void(Span),
19}
20
21impl Type {
22 pub fn span(&self) -> Span {
23 match self {
24 Self::Primitive(p) => p.span(),
25 Self::Reference(r) => r.span(),
26 Self::Void(s) => *s,
27 }
28 }
29
30 pub fn is_void(&self) -> bool {
32 matches!(self, Self::Void(_))
33 }
34
35 pub fn is_primitive(&self) -> bool {
37 matches!(self, Self::Primitive(_))
38 }
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
43pub enum PrimitiveType {
44 Byte,
45 Short,
46 Int,
47 Long,
48 Char,
49 Float,
50 Double,
51 Boolean,
52}
53
54impl PrimitiveType {
55 pub fn as_str(&self) -> &'static str {
56 match self {
57 Self::Byte => "byte",
58 Self::Short => "short",
59 Self::Int => "int",
60 Self::Long => "long",
61 Self::Char => "char",
62 Self::Float => "float",
63 Self::Double => "double",
64 Self::Boolean => "boolean",
65 }
66 }
67
68 pub fn span(&self) -> Span {
69 Span::call_site() }
71}
72
73#[derive(Debug, Clone, PartialEq, Eq, Hash)]
75pub enum ReferenceType {
76 ClassOrInterfaceType(ClassOrInterfaceType),
78 TypeVar(Ident),
80 Array(ArrayType),
82}
83
84impl ReferenceType {
85 pub fn span(&self) -> Span {
86 match self {
87 Self::ClassOrInterfaceType(t) => t.span(),
88 Self::TypeVar(i) => i.span(),
89 Self::Array(a) => a.span,
90 }
91 }
92}
93
94#[derive(Debug, Clone, PartialEq, Eq, Hash)]
96pub struct ClassOrInterfaceType {
97 pub path: Path,
100 pub annotations_prefix: Vec<Annotation>, }
102
103impl ClassOrInterfaceType {
104 pub fn span(&self) -> Span {
105 self.path.span
106 }
107
108 pub fn name(&self) -> &Ident {
109 self.path.last_ident()
110 }
111
112 pub fn type_args(&self) -> Option<&TypeArguments> {
113 self.path.last_segment().args.as_ref()
114 }
115}
116
117#[derive(Debug, Clone, PartialEq, Eq, Hash)]
119pub struct ArrayType {
120 pub elem_type: Box<Type>,
121 pub dims: Vec<ArrayDim>,
122 pub span: Span,
123}
124
125#[derive(Debug, Clone, PartialEq, Eq, Hash)]
127pub struct ArrayDim {
128 pub bracket_span: (Span, Span),
129 pub annotations: Vec<Annotation>,
130}