1use crate::helpers::NodeExt;
7use crate::raw::{Node, NodeKind};
8
9#[derive(Clone, Copy)]
14pub struct TypeRef<'ctx> {
15 raw: Node<'ctx>,
16}
17
18impl<'ctx> TypeRef<'ctx> {
19 pub fn new(raw: Node<'ctx>) -> Self {
21 Self { raw }
22 }
23
24 pub fn raw(&self) -> Node<'ctx> {
26 self.raw
27 }
28
29 pub fn kind(&self) -> TypeKind<'ctx> {
31 self.classify()
32 }
33
34 pub fn display(&self) -> String {
36 self.raw.to_string()
37 }
38
39 pub fn generic_args(&self) -> Vec<TypeRef<'ctx>> {
43 if let TypeKind::Named(named) = self.kind() {
44 named.generic_args()
45 } else {
46 Vec::new()
47 }
48 }
49
50 pub fn is_generic(&self) -> bool {
52 if let TypeKind::Named(named) = self.kind() {
53 named.is_generic()
54 } else {
55 false
56 }
57 }
58
59 fn wrap_child<F>(&self, wrapper: F) -> TypeKind<'ctx>
61 where
62 F: FnOnce(Box<TypeRef<'ctx>>) -> TypeKind<'ctx>,
63 {
64 self.raw
65 .child(0)
66 .map(|inner| wrapper(Box::new(TypeRef::new(inner))))
67 .unwrap_or(TypeKind::Other(self.raw))
68 }
69
70 fn wrap_nested_child<F>(&self, wrapper: F) -> TypeKind<'ctx>
72 where
73 F: FnOnce(Box<TypeRef<'ctx>>) -> TypeKind<'ctx>,
74 {
75 self.raw
76 .child(0)
77 .and_then(|t| t.child(0))
78 .map(|inner| wrapper(Box::new(TypeRef::new(inner))))
79 .unwrap_or(TypeKind::Other(self.raw))
80 }
81
82 fn classify(&self) -> TypeKind<'ctx> {
83 match self.raw.kind() {
84 NodeKind::Class
86 | NodeKind::Structure
87 | NodeKind::Enum
88 | NodeKind::Protocol
89 | NodeKind::TypeAlias
90 | NodeKind::OtherNominalType => TypeKind::Named(NamedType { raw: self.raw }),
91
92 NodeKind::BoundGenericClass
94 | NodeKind::BoundGenericStructure
95 | NodeKind::BoundGenericEnum
96 | NodeKind::BoundGenericProtocol
97 | NodeKind::BoundGenericTypeAlias
98 | NodeKind::BoundGenericOtherNominalType => {
99 TypeKind::Named(NamedType { raw: self.raw })
100 }
101
102 NodeKind::FunctionType
104 | NodeKind::NoEscapeFunctionType
105 | NodeKind::CFunctionPointer
106 | NodeKind::ThinFunctionType
107 | NodeKind::AutoClosureType
108 | NodeKind::EscapingAutoClosureType
109 | NodeKind::ObjCBlock
110 | NodeKind::EscapingObjCBlock
111 | NodeKind::ConcurrentFunctionType
112 | NodeKind::GlobalActorFunctionType
113 | NodeKind::DifferentiableFunctionType
114 | NodeKind::IsolatedAnyFunctionType
115 | NodeKind::NonIsolatedCallerFunctionType
116 | NodeKind::SendingResultFunctionType
117 | NodeKind::UncurriedFunctionType => TypeKind::Function(FunctionType { raw: self.raw }),
118
119 NodeKind::ImplFunctionType => TypeKind::ImplFunction(ImplFunctionType::new(self.raw)),
121
122 NodeKind::Tuple => {
124 let elements = self
125 .raw
126 .children()
127 .filter(|c| c.kind() == NodeKind::TupleElement)
128 .map(TupleElement::new)
129 .collect();
130 TypeKind::Tuple(elements)
131 }
132
133 NodeKind::SugaredOptional => self.wrap_child(TypeKind::Optional),
135
136 NodeKind::SugaredArray => self.wrap_child(TypeKind::Array),
138
139 NodeKind::SugaredDictionary => {
141 if let (Some(key), Some(value)) = (self.raw.child(0), self.raw.child(1)) {
142 TypeKind::Dictionary {
143 key: Box::new(TypeRef::new(key)),
144 value: Box::new(TypeRef::new(value)),
145 }
146 } else {
147 TypeKind::Other(self.raw)
148 }
149 }
150
151 NodeKind::DependentGenericParamType => {
153 let depth = self.raw.child(0).and_then(|c| c.index()).unwrap_or(0);
154 let index = self.raw.child(1).and_then(|c| c.index()).unwrap_or(0);
155 TypeKind::GenericParam { depth, index }
156 }
157
158 NodeKind::Metatype | NodeKind::ExistentialMetatype => {
160 if let Some(inner) = self.raw.child(0) {
161 let type_node = if inner.kind() == NodeKind::MetatypeRepresentation {
163 self.raw.child(1)
164 } else {
165 Some(inner)
166 };
167 if let Some(t) = type_node {
168 TypeKind::Metatype(Box::new(TypeRef::new(t)))
169 } else {
170 TypeKind::Other(self.raw)
171 }
172 } else {
173 TypeKind::Other(self.raw)
174 }
175 }
176
177 NodeKind::ProtocolList
179 | NodeKind::ProtocolListWithClass
180 | NodeKind::ProtocolListWithAnyObject => {
181 let protocols: Vec<_> = self
182 .raw
183 .descendants()
184 .filter(|n| n.kind() == NodeKind::Protocol || n.kind() == NodeKind::Type)
185 .map(TypeRef::new)
186 .collect();
187 if protocols.is_empty() {
188 TypeKind::Any
190 } else {
191 TypeKind::Existential(protocols)
192 }
193 }
194
195 NodeKind::BuiltinTypeName => {
197 let name = self.raw.text().unwrap_or("");
198 TypeKind::Builtin(name)
199 }
200
201 NodeKind::BuiltinFixedArray => {
203 let mut children = self.raw.children();
205 let size = children
206 .next()
207 .and_then(|t| t.child(0))
208 .and_then(|n| n.index())
209 .map(|i| i as i64);
210 let element = children
211 .next()
212 .and_then(|t| t.child(0))
213 .map(|n| Box::new(TypeRef::new(n)));
214 TypeKind::BuiltinFixedArray { size, element }
215 }
216
217 NodeKind::InOut => self.wrap_child(TypeKind::InOut),
219
220 NodeKind::Shared => self.wrap_child(TypeKind::Shared),
222 NodeKind::Owned => self.wrap_child(TypeKind::Owned),
223 NodeKind::Weak => self.wrap_nested_child(TypeKind::Weak),
224 NodeKind::Unowned => self.wrap_nested_child(TypeKind::Unowned),
225 NodeKind::Sending => self.wrap_child(TypeKind::Sending),
226 NodeKind::Isolated => self.wrap_child(TypeKind::Isolated),
227
228 NodeKind::NoDerivative => self.wrap_child(TypeKind::NoDerivative),
230
231 NodeKind::Pack => {
233 let elements: Vec<_> = self
234 .raw
235 .children()
236 .filter(|c| c.kind() == NodeKind::Type)
237 .filter_map(|t| t.child(0))
238 .map(TypeRef::new)
239 .collect();
240 TypeKind::Pack(elements)
241 }
242
243 NodeKind::Integer => {
245 let value = self.raw.index().map(|i| i as i64).unwrap_or(0);
246 TypeKind::ValueGeneric(value)
247 }
248
249 NodeKind::CompileTimeLiteral => self.wrap_child(TypeKind::CompileTimeLiteral),
251
252 NodeKind::ErrorType => TypeKind::Error,
254
255 NodeKind::Type => {
257 if let Some(inner) = self.raw.child(0) {
258 TypeRef::new(inner).classify()
259 } else {
260 TypeKind::Other(self.raw)
261 }
262 }
263
264 NodeKind::DynamicSelf => self.wrap_child(TypeKind::DynamicSelf),
266
267 NodeKind::ConstrainedExistential => self.wrap_child(TypeKind::ConstrainedExistential),
269
270 NodeKind::Module => TypeKind::Named(NamedType { raw: self.raw }),
272
273 NodeKind::DependentGenericType => {
275 let signature = self
277 .raw
278 .child_of_kind(NodeKind::DependentGenericSignature)
279 .map(GenericSignature::new);
280
281 let inner = self
282 .raw
283 .child_of_kind(NodeKind::Type)
284 .and_then(|t| t.child(0));
285
286 match (signature, inner) {
287 (Some(sig), Some(inner_node)) => TypeKind::Generic {
288 signature: sig,
289 inner: Box::new(TypeRef::new(inner_node)),
290 },
291 (None, Some(inner_node)) => TypeRef::new(inner_node).kind(),
292 _ => TypeKind::Other(self.raw),
293 }
294 }
295
296 NodeKind::DependentMemberType => {
298 let base = self
300 .raw
301 .child_of_kind(NodeKind::Type)
302 .and_then(|t| t.child(0))
303 .map(|inner| Box::new(TypeRef::new(inner)));
304 let name = self
305 .raw
306 .child_of_kind(NodeKind::DependentAssociatedTypeRef)
307 .and_then(|r| r.child_of_kind(NodeKind::Identifier))
308 .and_then(|id| id.text());
309 if let Some(base) = base {
310 TypeKind::AssociatedType { base, name }
311 } else {
312 TypeKind::Other(self.raw)
313 }
314 }
315
316 NodeKind::OpaqueType => {
318 let source = self
320 .raw
321 .child_of_kind(NodeKind::OpaqueReturnTypeOf)
322 .map(OpaqueSource::from_opaque_return_type_of);
323 let index = self
324 .raw
325 .child_of_kind(NodeKind::Index)
326 .and_then(|i| i.index());
327 TypeKind::Opaque { source, index }
328 }
329
330 NodeKind::OpaqueReturnType => {
331 let source = self
334 .raw
335 .child_of_kind(NodeKind::OpaqueReturnTypeParent)
336 .map(OpaqueSource::from_opaque_return_type_parent);
337 TypeKind::Opaque {
338 source,
339 index: None,
340 }
341 }
342
343 NodeKind::SILBoxTypeWithLayout | NodeKind::SILBoxType => {
345 let mut fields = Vec::new();
348 let mut substitutions = Vec::new();
349
350 for child in self.raw.children() {
351 match child.kind() {
352 NodeKind::SILBoxLayout => {
353 for field_node in child.children() {
354 let is_mutable = field_node.kind() == NodeKind::SILBoxMutableField;
355 if let Some(type_node) = field_node.child(0) {
356 let inner = if type_node.kind() == NodeKind::Type {
357 type_node.child(0).unwrap_or(type_node)
358 } else {
359 type_node
360 };
361 fields.push(SILBoxField {
362 type_ref: TypeRef::new(inner),
363 is_mutable,
364 });
365 }
366 }
367 }
368 NodeKind::TypeList => {
369 for type_node in child.children() {
370 if let Some(inner) = type_node.unwrap_if_kind(NodeKind::Type) {
371 substitutions.push(TypeRef::new(inner));
372 }
373 }
374 }
375 _ => {}
376 }
377 }
378
379 TypeKind::SILBox {
380 fields,
381 substitutions,
382 }
383 }
384
385 _ => TypeKind::Other(self.raw),
387 }
388 }
389}
390
391impl std::fmt::Debug for TypeRef<'_> {
392 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
393 self.kind().fmt(f)
395 }
396}
397
398impl std::fmt::Display for TypeRef<'_> {
399 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
400 write!(f, "{}", self.display())
401 }
402}
403
404#[derive(Debug)]
406pub enum TypeKind<'ctx> {
407 Named(NamedType<'ctx>),
409 Function(FunctionType<'ctx>),
411 ImplFunction(ImplFunctionType<'ctx>),
413 Tuple(Vec<TupleElement<'ctx>>),
415 Optional(Box<TypeRef<'ctx>>),
417 Array(Box<TypeRef<'ctx>>),
419 Dictionary {
421 key: Box<TypeRef<'ctx>>,
422 value: Box<TypeRef<'ctx>>,
423 },
424 GenericParam { depth: u64, index: u64 },
426 Metatype(Box<TypeRef<'ctx>>),
428 Existential(Vec<TypeRef<'ctx>>),
430 Any,
432 Builtin(&'ctx str),
434 BuiltinFixedArray {
436 size: Option<i64>,
437 element: Option<Box<TypeRef<'ctx>>>,
438 },
439 InOut(Box<TypeRef<'ctx>>),
441 Shared(Box<TypeRef<'ctx>>),
443 Owned(Box<TypeRef<'ctx>>),
445 Weak(Box<TypeRef<'ctx>>),
447 Unowned(Box<TypeRef<'ctx>>),
449 Sending(Box<TypeRef<'ctx>>),
451 Isolated(Box<TypeRef<'ctx>>),
453 NoDerivative(Box<TypeRef<'ctx>>),
455 Pack(Vec<TypeRef<'ctx>>),
457 ValueGeneric(i64),
459 CompileTimeLiteral(Box<TypeRef<'ctx>>),
461 DynamicSelf(Box<TypeRef<'ctx>>),
463 ConstrainedExistential(Box<TypeRef<'ctx>>),
465 AssociatedType {
467 base: Box<TypeRef<'ctx>>,
468 name: Option<&'ctx str>,
469 },
470 Opaque {
472 source: Option<OpaqueSource<'ctx>>,
474 index: Option<u64>,
476 },
477 Generic {
479 signature: GenericSignature<'ctx>,
481 inner: Box<TypeRef<'ctx>>,
483 },
484 Error,
486 SILBox {
488 fields: Vec<SILBoxField<'ctx>>,
490 substitutions: Vec<TypeRef<'ctx>>,
492 },
493 Other(Node<'ctx>),
495}
496
497#[derive(Debug)]
499pub struct SILBoxField<'ctx> {
500 pub type_ref: TypeRef<'ctx>,
502 pub is_mutable: bool,
504}
505
506#[derive(Debug, Clone, Copy)]
510pub struct OpaqueSource<'ctx> {
511 pub module: Option<&'ctx str>,
513 pub containing_type: Option<&'ctx str>,
515 pub name: Option<&'ctx str>,
517 pub mangled_name: Option<&'ctx str>,
519}
520
521impl<'ctx> OpaqueSource<'ctx> {
522 fn from_opaque_return_type_of(node: Node<'ctx>) -> Self {
524 let func_node = node.child_of_kind(NodeKind::Function);
526
527 let (module, containing_type, name) = if let Some(func) = func_node {
528 let module = func
529 .descendants()
530 .find(|d| d.kind() == NodeKind::Module)
531 .and_then(|m| m.text());
532
533 let containing_type = func
534 .children()
535 .find(|c| {
536 matches!(
537 c.kind(),
538 NodeKind::Structure | NodeKind::Class | NodeKind::Enum | NodeKind::Protocol
539 )
540 })
541 .and_then(|t| {
542 t.child_of_kind(NodeKind::Identifier)
543 .and_then(|id| id.text())
544 });
545
546 let name = func
547 .child_of_kind(NodeKind::Identifier)
548 .and_then(|id| id.text());
549
550 (module, containing_type, name)
551 } else {
552 (None, None, None)
553 };
554
555 Self {
556 module,
557 containing_type,
558 name,
559 mangled_name: None,
560 }
561 }
562
563 fn from_opaque_return_type_parent(node: Node<'ctx>) -> Self {
565 Self {
567 module: None,
568 containing_type: None,
569 name: None,
570 mangled_name: node.text(),
571 }
572 }
573}
574
575#[derive(Clone, Copy)]
577pub struct NamedType<'ctx> {
578 raw: Node<'ctx>,
579}
580
581impl<'ctx> NamedType<'ctx> {
582 pub fn raw(&self) -> Node<'ctx> {
584 self.raw
585 }
586
587 pub fn name(&self) -> Option<&'ctx str> {
589 match self.raw.kind() {
591 NodeKind::BoundGenericClass
592 | NodeKind::BoundGenericStructure
593 | NodeKind::BoundGenericEnum
594 | NodeKind::BoundGenericProtocol
595 | NodeKind::BoundGenericTypeAlias
596 | NodeKind::BoundGenericOtherNominalType => {
597 self.raw.child(0).and_then(Self::extract_name)
598 }
599 _ => Self::extract_name(self.raw),
600 }
601 }
602
603 fn extract_name(node: Node<'ctx>) -> Option<&'ctx str> {
604 for child in node.children() {
606 if child.kind() == NodeKind::Identifier {
607 return child.text();
608 }
609 }
610 if let Some(inner) = node.unwrap_if_kind(NodeKind::Type) {
612 return Self::extract_name(inner);
613 }
614 node.text()
616 }
617
618 pub fn module(&self) -> Option<&'ctx str> {
620 Self::find_module_in_node(self.raw)
621 }
622
623 fn find_module_in_node(node: Node<'ctx>) -> Option<&'ctx str> {
624 let search_node = match node.kind() {
626 NodeKind::BoundGenericClass
627 | NodeKind::BoundGenericStructure
628 | NodeKind::BoundGenericEnum
629 | NodeKind::BoundGenericProtocol
630 | NodeKind::BoundGenericTypeAlias
631 | NodeKind::BoundGenericOtherNominalType => {
632 node.child(0).unwrap_or(node)
634 }
635 NodeKind::Type => {
636 return node.child(0).and_then(Self::find_module_in_node);
638 }
639 _ => node,
640 };
641
642 for child in search_node.children() {
643 if child.kind() == NodeKind::Module {
644 return child.text();
645 }
646 match child.kind() {
648 NodeKind::Type
649 | NodeKind::Class
650 | NodeKind::Structure
651 | NodeKind::Enum
652 | NodeKind::Protocol
653 | NodeKind::TypeAlias => {
654 if let Some(module) = Self::find_module_in_node(child) {
655 return Some(module);
656 }
657 }
658 _ => {}
659 }
660 }
661 None
662 }
663
664 pub fn full_name(&self) -> String {
666 self.raw.to_string()
667 }
668
669 pub fn generic_args(&self) -> Vec<TypeRef<'ctx>> {
671 match self.raw.kind() {
672 NodeKind::BoundGenericClass
673 | NodeKind::BoundGenericStructure
674 | NodeKind::BoundGenericEnum
675 | NodeKind::BoundGenericProtocol
676 | NodeKind::BoundGenericTypeAlias
677 | NodeKind::BoundGenericOtherNominalType => {
678 self.raw
681 .child(1)
682 .map(|type_list| {
683 type_list
684 .children()
685 .filter(|c| c.kind() == NodeKind::Type)
686 .map(|c| TypeRef::new(c.child(0).unwrap_or(c)))
687 .collect()
688 })
689 .unwrap_or_default()
690 }
691 _ => Vec::new(),
692 }
693 }
694
695 pub fn is_class(&self) -> bool {
697 matches!(
698 self.raw.kind(),
699 NodeKind::Class | NodeKind::BoundGenericClass
700 )
701 }
702
703 pub fn is_generic(&self) -> bool {
705 matches!(
706 self.raw.kind(),
707 NodeKind::BoundGenericClass
708 | NodeKind::BoundGenericStructure
709 | NodeKind::BoundGenericEnum
710 | NodeKind::BoundGenericProtocol
711 | NodeKind::BoundGenericTypeAlias
712 | NodeKind::BoundGenericOtherNominalType
713 )
714 }
715}
716
717impl std::fmt::Debug for NamedType<'_> {
718 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
719 let mut s = f.debug_struct("NamedType");
720 s.field("module", &self.module());
721 s.field("name", &self.name());
722 let args = self.generic_args();
723 if !args.is_empty() {
724 s.field("generic_args", &args);
725 }
726 s.finish()
727 }
728}
729
730#[derive(Clone, Copy)]
732pub struct FunctionType<'ctx> {
733 raw: Node<'ctx>,
734}
735
736impl<'ctx> FunctionType<'ctx> {
737 pub fn new(raw: Node<'ctx>) -> Self {
739 Self { raw }
740 }
741
742 pub fn raw(&self) -> Node<'ctx> {
744 self.raw
745 }
746
747 pub fn parameters(&self) -> Vec<FunctionParam<'ctx>> {
749 for child in self.raw.children() {
752 if child.kind() == NodeKind::ArgumentTuple {
753 return self.extract_params_from_argument_tuple(child);
754 }
755 if child.kind() == NodeKind::ImplParameter {
757 return self.extract_impl_params();
758 }
759 }
760 Vec::new()
761 }
762
763 fn extract_params_from_argument_tuple(
764 &self,
765 arg_tuple: Node<'ctx>,
766 ) -> Vec<FunctionParam<'ctx>> {
767 let type_node = match arg_tuple.child(0) {
769 Some(n) if n.kind() == NodeKind::Type => n.child(0).unwrap_or(n),
770 Some(n) => n,
771 None => return Vec::new(),
772 };
773
774 if type_node.kind() == NodeKind::Tuple {
775 type_node
776 .children()
777 .filter(|c| c.kind() == NodeKind::TupleElement)
778 .map(FunctionParam::from_tuple_element)
779 .collect()
780 } else {
781 if type_node.num_children() == 0 && type_node.text().is_none() {
784 Vec::new()
785 } else {
786 vec![FunctionParam {
787 label: None,
788 type_ref: TypeRef::new(type_node),
789 is_variadic: false,
790 }]
791 }
792 }
793 }
794
795 fn extract_impl_params(&self) -> Vec<FunctionParam<'ctx>> {
796 self.raw
797 .children()
798 .filter(|c| c.kind() == NodeKind::ImplParameter)
799 .map(|c| {
800 let type_node = c
801 .child_of_kind(NodeKind::Type)
802 .and_then(|t| t.child(0))
803 .unwrap_or(c);
804 FunctionParam {
805 label: None,
806 type_ref: TypeRef::new(type_node),
807 is_variadic: false,
808 }
809 })
810 .collect()
811 }
812
813 pub fn return_type(&self) -> Option<TypeRef<'ctx>> {
815 for child in self.raw.children() {
816 if child.kind() == NodeKind::ReturnType {
817 return child
819 .child(0)
820 .map(|t| TypeRef::new(t.child(0).unwrap_or(t)));
821 }
822 if child.kind() == NodeKind::ImplResult {
824 return child
825 .child_of_kind(NodeKind::Type)
826 .map(|t| TypeRef::new(t.child(0).unwrap_or(t)));
827 }
828 }
829 None
830 }
831
832 pub fn is_async(&self) -> bool {
834 self.raw
835 .descendants()
836 .any(|n| n.kind() == NodeKind::AsyncAnnotation)
837 }
838
839 pub fn is_throwing(&self) -> bool {
841 self.raw.descendants().any(|n| {
842 n.kind() == NodeKind::ThrowsAnnotation || n.kind() == NodeKind::TypedThrowsAnnotation
843 })
844 }
845
846 pub fn thrown_error_type(&self) -> Option<TypeRef<'ctx>> {
848 self.raw
849 .descendants()
850 .find(|n| n.kind() == NodeKind::TypedThrowsAnnotation)
851 .and_then(|n| n.child(0))
852 .map(TypeRef::new)
853 }
854
855 pub fn convention(&self) -> FunctionConvention {
857 match self.raw.kind() {
858 NodeKind::CFunctionPointer => FunctionConvention::C,
859 NodeKind::ObjCBlock | NodeKind::EscapingObjCBlock => FunctionConvention::Block,
860 NodeKind::ThinFunctionType => FunctionConvention::Thin,
861 _ => {
862 for child in self.raw.children() {
864 if matches!(
865 child.kind(),
866 NodeKind::ImplFunctionConvention | NodeKind::ImplFunctionConventionName
867 ) && let Some(text) = child.text()
868 {
869 return match text {
870 "c" => FunctionConvention::C,
871 "block" => FunctionConvention::Block,
872 "thin" => FunctionConvention::Thin,
873 _ => FunctionConvention::Swift,
874 };
875 }
876 }
877 FunctionConvention::Swift
878 }
879 }
880 }
881
882 pub fn is_escaping(&self) -> bool {
884 !matches!(
885 self.raw.kind(),
886 NodeKind::NoEscapeFunctionType | NodeKind::AutoClosureType
887 )
888 }
889
890 pub fn is_autoclosure(&self) -> bool {
892 matches!(
893 self.raw.kind(),
894 NodeKind::AutoClosureType | NodeKind::EscapingAutoClosureType
895 )
896 }
897
898 pub fn has_sending_result(&self) -> bool {
900 self.raw
901 .children()
902 .any(|c| c.kind() == NodeKind::SendingResultFunctionType)
903 }
904}
905
906impl std::fmt::Debug for FunctionType<'_> {
907 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
908 let mut s = f.debug_struct("FunctionType");
909 s.field("parameters", &self.parameters());
910 s.field("return_type", &self.return_type());
911 s.field("is_async", &self.is_async());
912 s.field("is_throwing", &self.is_throwing());
913 if self.has_sending_result() {
914 s.field("has_sending_result", &true);
915 }
916 s.field("convention", &self.convention());
917 s.finish()
918 }
919}
920
921#[derive(Clone, Copy)]
926pub struct ImplFunctionType<'ctx> {
927 raw: Node<'ctx>,
928}
929
930impl<'ctx> ImplFunctionType<'ctx> {
931 pub fn new(raw: Node<'ctx>) -> Self {
933 Self { raw }
934 }
935
936 pub fn raw(&self) -> Node<'ctx> {
938 self.raw
939 }
940
941 pub fn callee_convention(&self) -> Option<&'ctx str> {
943 self.raw
944 .child_of_kind(NodeKind::ImplConvention)
945 .and_then(|c| c.text())
946 }
947
948 pub fn parameters(&self) -> Vec<ImplParam<'ctx>> {
950 self.raw
951 .children()
952 .filter(|c| c.kind() == NodeKind::ImplParameter)
953 .map(ImplParam::new)
954 .collect()
955 }
956
957 pub fn results(&self) -> Vec<ImplResult<'ctx>> {
959 self.raw
960 .children()
961 .filter(|c| c.kind() == NodeKind::ImplResult)
962 .map(ImplResult::new)
963 .collect()
964 }
965
966 pub fn error_result(&self) -> Option<ImplResult<'ctx>> {
968 self.raw
969 .child_of_kind(NodeKind::ImplErrorResult)
970 .map(ImplResult::new)
971 }
972
973 pub fn generic_signature(&self) -> Option<GenericSignature<'ctx>> {
975 self.raw
976 .child_of_kind(NodeKind::DependentGenericSignature)
977 .map(GenericSignature::new)
978 }
979
980 pub fn substitutions(&self) -> Vec<TypeRef<'ctx>> {
982 for child in self.raw.children() {
984 if child.kind() == NodeKind::ImplPatternSubstitutions
985 || child.kind() == NodeKind::ImplInvocationSubstitutions
986 {
987 return child
988 .children()
989 .filter(|c| c.kind() == NodeKind::Type)
990 .filter_map(|t| t.child(0))
991 .map(TypeRef::new)
992 .collect();
993 }
994 }
995 Vec::new()
996 }
997
998 pub fn is_escaping(&self) -> bool {
1000 self.raw
1001 .children()
1002 .any(|c| c.kind() == NodeKind::ImplEscaping)
1003 }
1004
1005 pub fn has_sending_result(&self) -> bool {
1007 self.raw
1008 .children()
1009 .any(|c| c.kind() == NodeKind::ImplSendingResult)
1010 }
1011}
1012
1013impl std::fmt::Debug for ImplFunctionType<'_> {
1014 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1015 let mut s = f.debug_struct("ImplFunctionType");
1016 s.field("callee_convention", &self.callee_convention());
1017 s.field("is_escaping", &self.is_escaping());
1018 if self.has_sending_result() {
1019 s.field("has_sending_result", &true);
1020 }
1021 s.field("parameters", &self.parameters());
1022 s.field("results", &self.results());
1023 if let Some(err) = self.error_result() {
1024 s.field("error_result", &err);
1025 }
1026 if let Some(sig) = self.generic_signature() {
1027 s.field("generic_signature", &sig);
1028 }
1029 let subs = self.substitutions();
1030 if !subs.is_empty() {
1031 s.field("substitutions", &subs);
1032 }
1033 s.finish()
1034 }
1035}
1036
1037#[derive(Clone, Copy)]
1039pub struct ImplParam<'ctx> {
1040 raw: Node<'ctx>,
1041}
1042
1043impl<'ctx> ImplParam<'ctx> {
1044 fn new(raw: Node<'ctx>) -> Self {
1045 Self { raw }
1046 }
1047
1048 pub fn convention(&self) -> Option<&'ctx str> {
1050 self.raw
1051 .child_of_kind(NodeKind::ImplConvention)
1052 .and_then(|c| c.text())
1053 }
1054
1055 pub fn type_ref(&self) -> Option<TypeRef<'ctx>> {
1057 self.raw
1058 .child_of_kind(NodeKind::Type)
1059 .and_then(|t| t.child(0))
1060 .map(TypeRef::new)
1061 }
1062
1063 pub fn is_sending(&self) -> bool {
1065 self.raw
1066 .children()
1067 .any(|c| c.kind() == NodeKind::ImplParameterSending)
1068 }
1069}
1070
1071impl std::fmt::Debug for ImplParam<'_> {
1072 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1073 let mut s = f.debug_struct("ImplParam");
1074 s.field("convention", &self.convention());
1075 if self.is_sending() {
1076 s.field("is_sending", &true);
1077 }
1078 s.field("type_ref", &self.type_ref());
1079 s.finish()
1080 }
1081}
1082
1083#[derive(Clone, Copy)]
1085pub struct ImplResult<'ctx> {
1086 raw: Node<'ctx>,
1087}
1088
1089impl<'ctx> ImplResult<'ctx> {
1090 fn new(raw: Node<'ctx>) -> Self {
1091 Self { raw }
1092 }
1093
1094 pub fn convention(&self) -> Option<&'ctx str> {
1096 self.raw
1097 .child_of_kind(NodeKind::ImplConvention)
1098 .and_then(|c| c.text())
1099 }
1100
1101 pub fn type_ref(&self) -> Option<TypeRef<'ctx>> {
1103 self.raw
1104 .child_of_kind(NodeKind::Type)
1105 .and_then(|t| t.child(0))
1106 .map(TypeRef::new)
1107 }
1108}
1109
1110impl std::fmt::Debug for ImplResult<'_> {
1111 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1112 f.debug_struct("ImplResult")
1113 .field("convention", &self.convention())
1114 .field("type_ref", &self.type_ref())
1115 .finish()
1116 }
1117}
1118
1119#[derive(Debug)]
1121pub struct FunctionParam<'ctx> {
1122 pub label: Option<&'ctx str>,
1124 pub type_ref: TypeRef<'ctx>,
1126 pub is_variadic: bool,
1128}
1129
1130impl<'ctx> FunctionParam<'ctx> {
1131 fn from_tuple_element(node: Node<'ctx>) -> Self {
1132 let mut label = None;
1133 let mut type_node = None;
1134 let mut is_variadic = false;
1135
1136 for child in node.children() {
1137 match child.kind() {
1138 NodeKind::TupleElementName => {
1139 label = child.text();
1140 }
1141 NodeKind::Type => {
1142 type_node = child.child(0).or(Some(child));
1143 }
1144 NodeKind::VariadicMarker => {
1145 is_variadic = true;
1146 }
1147 _ => {
1148 if type_node.is_none() {
1149 type_node = Some(child);
1150 }
1151 }
1152 }
1153 }
1154
1155 FunctionParam {
1156 label,
1157 type_ref: TypeRef::new(type_node.unwrap_or(node)),
1158 is_variadic,
1159 }
1160 }
1161}
1162
1163#[derive(Clone, Copy)]
1165pub struct TupleElement<'ctx> {
1166 raw: Node<'ctx>,
1167}
1168
1169impl<'ctx> TupleElement<'ctx> {
1170 pub fn new(raw: Node<'ctx>) -> Self {
1172 Self { raw }
1173 }
1174
1175 pub fn raw(&self) -> Node<'ctx> {
1177 self.raw
1178 }
1179
1180 pub fn label(&self) -> Option<&'ctx str> {
1182 self.raw
1183 .child_of_kind(NodeKind::TupleElementName)
1184 .and_then(|c| c.text())
1185 }
1186
1187 pub fn type_ref(&self) -> TypeRef<'ctx> {
1189 let type_node = self
1190 .raw
1191 .child_of_kind(NodeKind::Type)
1192 .and_then(|c| c.child(0))
1193 .unwrap_or(self.raw);
1194 TypeRef::new(type_node)
1195 }
1196
1197 pub fn is_variadic(&self) -> bool {
1199 self.raw
1200 .children()
1201 .any(|c| c.kind() == NodeKind::VariadicMarker)
1202 }
1203}
1204
1205impl std::fmt::Debug for TupleElement<'_> {
1206 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1207 f.debug_struct("TupleElement")
1208 .field("label", &self.label())
1209 .field("type", &self.type_ref())
1210 .field("is_variadic", &self.is_variadic())
1211 .finish()
1212 }
1213}
1214
1215#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1217pub enum FunctionConvention {
1218 Swift,
1220 C,
1222 Block,
1224 Thin,
1226}
1227
1228#[derive(Clone, Copy)]
1230pub struct GenericSignature<'ctx> {
1231 raw: Node<'ctx>,
1232}
1233
1234impl<'ctx> GenericSignature<'ctx> {
1235 pub fn new(raw: Node<'ctx>) -> Self {
1237 Self { raw }
1238 }
1239
1240 pub fn raw(&self) -> Node<'ctx> {
1242 self.raw
1243 }
1244
1245 pub fn param_counts(&self) -> Vec<u64> {
1249 self.raw
1250 .children()
1251 .filter(|c| c.kind() == NodeKind::DependentGenericParamCount)
1252 .filter_map(|c| c.index())
1253 .collect()
1254 }
1255
1256 pub fn requirements(&self) -> Vec<GenericRequirement<'ctx>> {
1258 self.raw
1259 .children()
1260 .filter_map(|c| match c.kind() {
1261 NodeKind::DependentGenericConformanceRequirement => {
1262 Some(GenericRequirement::from_conformance_node(c))
1263 }
1264 NodeKind::DependentGenericSameTypeRequirement => {
1265 Some(GenericRequirement::from_same_type_node(c))
1266 }
1267 NodeKind::DependentGenericLayoutRequirement => {
1268 Some(GenericRequirement::from_layout_node(c))
1269 }
1270 _ => None,
1271 })
1272 .collect()
1273 }
1274}
1275
1276impl std::fmt::Debug for GenericSignature<'_> {
1277 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1278 f.debug_struct("GenericSignature")
1279 .field("param_counts", &self.param_counts())
1280 .field("requirements", &self.requirements())
1281 .finish()
1282 }
1283}
1284
1285#[derive(Clone, Copy)]
1287pub struct GenericRequirement<'ctx> {
1288 kind: GenericRequirementKind<'ctx>,
1289}
1290
1291impl<'ctx> GenericRequirement<'ctx> {
1292 fn from_conformance_node(node: Node<'ctx>) -> Self {
1293 let mut children = node.children();
1295 let subject = children
1296 .next()
1297 .filter(|c| c.kind() == NodeKind::Type)
1298 .and_then(|c| c.child(0))
1299 .map(TypeRef::new);
1300 let constraint = children
1301 .next()
1302 .filter(|c| c.kind() == NodeKind::Type)
1303 .and_then(|c| c.child(0))
1304 .map(TypeRef::new);
1305
1306 Self {
1307 kind: GenericRequirementKind::Conformance {
1308 subject,
1309 constraint,
1310 },
1311 }
1312 }
1313
1314 fn from_same_type_node(node: Node<'ctx>) -> Self {
1315 let mut children = node.children();
1317 let first = children
1318 .next()
1319 .filter(|c| c.kind() == NodeKind::Type)
1320 .and_then(|c| c.child(0))
1321 .map(TypeRef::new);
1322 let second = children
1323 .next()
1324 .filter(|c| c.kind() == NodeKind::Type)
1325 .and_then(|c| c.child(0))
1326 .map(TypeRef::new);
1327
1328 Self {
1329 kind: GenericRequirementKind::SameType { first, second },
1330 }
1331 }
1332
1333 fn from_layout_node(node: Node<'ctx>) -> Self {
1334 let subject = node
1336 .child_of_kind(NodeKind::Type)
1337 .and_then(|c| c.child(0))
1338 .map(TypeRef::new);
1339 let layout = node
1340 .child_of_kind(NodeKind::Identifier)
1341 .and_then(|c| c.text());
1342
1343 Self {
1344 kind: GenericRequirementKind::Layout { subject, layout },
1345 }
1346 }
1347
1348 pub fn kind(&self) -> &GenericRequirementKind<'ctx> {
1350 &self.kind
1351 }
1352
1353 pub fn display(&self) -> String {
1355 match &self.kind {
1356 GenericRequirementKind::Conformance {
1357 subject,
1358 constraint,
1359 } => {
1360 let subj = subject.map(|s| s.display()).unwrap_or_else(|| "?".into());
1361 let cons = constraint
1362 .map(|c| c.display())
1363 .unwrap_or_else(|| "?".into());
1364 format!("{subj}: {cons}")
1365 }
1366 GenericRequirementKind::SameType { first, second } => {
1367 let f = first.map(|s| s.display()).unwrap_or_else(|| "?".into());
1368 let s = second.map(|s| s.display()).unwrap_or_else(|| "?".into());
1369 format!("{f} == {s}")
1370 }
1371 GenericRequirementKind::Layout { subject, layout } => {
1372 let subj = subject.map(|s| s.display()).unwrap_or_else(|| "?".into());
1373 let lay = layout.unwrap_or("?");
1374 format!("{subj}: {lay}")
1375 }
1376 }
1377 }
1378}
1379
1380impl std::fmt::Debug for GenericRequirement<'_> {
1381 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1382 match &self.kind {
1383 GenericRequirementKind::Conformance {
1384 subject,
1385 constraint,
1386 } => f
1387 .debug_struct("Conformance")
1388 .field("subject", subject)
1389 .field("constraint", constraint)
1390 .finish(),
1391 GenericRequirementKind::SameType { first, second } => f
1392 .debug_struct("SameType")
1393 .field("first", first)
1394 .field("second", second)
1395 .finish(),
1396 GenericRequirementKind::Layout { subject, layout } => f
1397 .debug_struct("Layout")
1398 .field("subject", subject)
1399 .field("layout", layout)
1400 .finish(),
1401 }
1402 }
1403}
1404
1405impl std::fmt::Display for GenericRequirement<'_> {
1406 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1407 write!(f, "{}", self.display())
1408 }
1409}
1410
1411#[derive(Clone, Copy)]
1413pub enum GenericRequirementKind<'ctx> {
1414 Conformance {
1416 subject: Option<TypeRef<'ctx>>,
1417 constraint: Option<TypeRef<'ctx>>,
1418 },
1419 SameType {
1421 first: Option<TypeRef<'ctx>>,
1422 second: Option<TypeRef<'ctx>>,
1423 },
1424 Layout {
1426 subject: Option<TypeRef<'ctx>>,
1427 layout: Option<&'ctx str>,
1428 },
1429}
1430
1431#[cfg(test)]
1432mod tests {
1433 use super::*;
1434 use crate::raw::Context;
1435
1436 fn parse_type<'ctx>(ctx: &'ctx Context, mangled: &str) -> Option<TypeRef<'ctx>> {
1439 let root = Node::parse(ctx, mangled)?;
1440 root.descendants()
1442 .find(|n| n.kind() == NodeKind::Type)
1443 .and_then(|n| n.child(0))
1444 .map(TypeRef::new)
1445 }
1446
1447 #[test]
1448 fn test_simple_type() {
1449 let ctx = Context::new();
1450 let root = Node::parse(&ctx, "$sSi").unwrap();
1452 let type_ref = TypeRef::new(root.child(0).unwrap());
1453 assert!(matches!(type_ref.kind(), TypeKind::Named(_)));
1454 }
1455
1456 #[test]
1457 fn test_function_type() {
1458 let ctx = Context::new();
1459 let type_ref = parse_type(&ctx, "_TtFSiSS").expect("Should parse");
1461 if let TypeKind::Function(func) = type_ref.kind() {
1462 assert!(!func.is_async());
1463 assert!(!func.is_throwing());
1464 assert_eq!(func.convention(), FunctionConvention::Swift);
1465 } else {
1466 panic!("Expected function type, got {:?}", type_ref.kind());
1467 }
1468 }
1469
1470 #[test]
1471 fn test_optional_type() {
1472 let ctx = Context::new();
1473 let type_ref = parse_type(&ctx, "_TtGSqSS_").expect("Should parse");
1475 if let TypeKind::Named(named) = type_ref.kind() {
1478 assert_eq!(named.name(), Some("Optional"));
1479 assert!(named.is_generic());
1480 } else {
1481 panic!(
1482 "Expected named type for Optional, got {:?}",
1483 type_ref.kind()
1484 );
1485 }
1486 }
1487
1488 #[test]
1489 fn test_array_type() {
1490 let ctx = Context::new();
1491 let type_ref = parse_type(&ctx, "_TtGSaSS_").expect("Should parse");
1493 if let TypeKind::Named(named) = type_ref.kind() {
1495 assert_eq!(named.name(), Some("Array"));
1496 assert!(named.is_generic());
1497 } else {
1498 panic!("Expected named type for Array, got {:?}", type_ref.kind());
1499 }
1500 }
1501
1502 #[test]
1503 fn test_dictionary_type() {
1504 let ctx = Context::new();
1505 let type_ref = parse_type(&ctx, "_TtGVs10DictionarySSSi_").expect("Should parse");
1507 if let TypeKind::Named(named) = type_ref.kind() {
1509 assert_eq!(named.name(), Some("Dictionary"));
1510 assert!(named.is_generic());
1511 let args = named.generic_args();
1512 assert_eq!(args.len(), 2);
1513 } else {
1514 panic!(
1515 "Expected named type for Dictionary, got {:?}",
1516 type_ref.kind()
1517 );
1518 }
1519 }
1520
1521 #[test]
1522 fn test_c_function_pointer() {
1523 let ctx = Context::new();
1524 let type_ref = parse_type(&ctx, "_TtcSiSu").expect("Should parse");
1526 if let TypeKind::Function(func) = type_ref.kind() {
1527 assert_eq!(func.convention(), FunctionConvention::C);
1528 } else {
1529 panic!("Expected function type, got {:?}", type_ref.kind());
1530 }
1531 }
1532
1533 #[test]
1534 fn test_block_type() {
1535 let ctx = Context::new();
1536 let type_ref = parse_type(&ctx, "_TtbSiSu").expect("Should parse");
1538 if let TypeKind::Function(func) = type_ref.kind() {
1539 assert_eq!(func.convention(), FunctionConvention::Block);
1540 } else {
1541 panic!("Expected function type, got {:?}", type_ref.kind());
1542 }
1543 }
1544
1545 #[test]
1546 fn test_tuple_type() {
1547 let ctx = Context::new();
1548 let type_ref = parse_type(&ctx, "_TtTSiSu_").expect("Should parse");
1550 if let TypeKind::Tuple(elements) = type_ref.kind() {
1551 assert_eq!(elements.len(), 2);
1552 } else {
1553 panic!("Expected tuple type, got {:?}", type_ref.kind());
1554 }
1555 }
1556
1557 #[test]
1558 fn test_labeled_tuple() {
1559 let ctx = Context::new();
1560 let type_ref = parse_type(&ctx, "_TtT3fooSi3barSu_").expect("Should parse");
1562 if let TypeKind::Tuple(elements) = type_ref.kind() {
1563 assert_eq!(elements.len(), 2);
1564 assert_eq!(elements[0].label(), Some("foo"));
1565 assert_eq!(elements[1].label(), Some("bar"));
1566 } else {
1567 panic!("Expected tuple type, got {:?}", type_ref.kind());
1568 }
1569 }
1570
1571 #[test]
1572 fn test_metatype() {
1573 let ctx = Context::new();
1574 let type_ref = parse_type(&ctx, "_TtMSi").expect("Should parse");
1576 assert!(
1577 matches!(type_ref.kind(), TypeKind::Metatype(_)),
1578 "Expected metatype, got {:?}",
1579 type_ref.kind()
1580 );
1581 }
1582
1583 #[test]
1584 fn test_inout_type() {
1585 let ctx = Context::new();
1586 let type_ref = parse_type(&ctx, "_TtRSi").expect("Should parse");
1588 assert!(
1589 matches!(type_ref.kind(), TypeKind::InOut(_)),
1590 "Expected inout type, got {:?}",
1591 type_ref.kind()
1592 );
1593 }
1594
1595 #[test]
1596 fn test_named_type_generic_args() {
1597 let ctx = Context::new();
1598 let type_ref = parse_type(&ctx, "_TtGSqSS_").expect("Should parse");
1600 if let TypeKind::Named(named) = type_ref.kind() {
1601 let args = named.generic_args();
1602 assert_eq!(args.len(), 1);
1603 assert!(args[0].display().contains("String"));
1605 } else {
1606 panic!("Expected named type");
1607 }
1608 }
1609}