1use xur_ast::{Ty, TyKind};
4use xur_resolve::{DefId, DefKind, PrimTy, Res, Resolutions};
5
6use crate::ir::RtType;
7
8pub fn rt_type(resolutions: &Resolutions, ty: &Ty) -> Option<RtType> {
11 match rt_type_or_any(resolutions, ty) {
12 RtType::Any => None,
13 ty => Some(ty),
14 }
15}
16
17fn rt_type_or_any(resolutions: &Resolutions, ty: &Ty) -> RtType {
18 match &ty.kind {
19 TyKind::Path(path) => match resolutions.res.get(&path.id) {
20 Some(Res::PrimTy(prim)) => prim_rt_type(*prim),
21 Some(&Res::Def(DefKind::Struct | DefKind::Enum, def)) => RtType::Adt {
22 def,
23 name: resolutions.def_path(def),
24 },
25 Some(&Res::Def(DefKind::NativeType(id), def)) => RtType::Native {
26 id,
27 name: resolutions.def_path(def),
28 },
29 _ => RtType::Any,
30 },
31 TyKind::Tuple(tys) if tys.is_empty() => RtType::Unit,
32 TyKind::Tuple(tys) => RtType::Tuple(
33 tys.iter()
34 .map(|ty| rt_type_or_any(resolutions, ty))
35 .collect(),
36 ),
37 TyKind::Paren(ty) => rt_type_or_any(resolutions, ty),
38 TyKind::Ptr(_) => RtType::Ptr,
39 TyKind::List(_) => RtType::List,
40 TyKind::Fn { params, ret } => RtType::Fn {
41 params: params.iter().map(|ty| rt_type_or_any(resolutions, ty)).collect(),
42 ret: Box::new(match ret {
43 Some(ty) => rt_type_or_any(resolutions, ty),
44 None => RtType::Unit,
45 }),
46 },
47 TyKind::Record | TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => RtType::Any,
48 }
49}
50
51pub(crate) fn prim_rt_type(prim: PrimTy) -> RtType {
52 let int = |name, min, max| RtType::Int { name, min, max };
53 match prim {
54 PrimTy::Bool => RtType::Bool,
55 PrimTy::Str => RtType::Str,
56 PrimTy::Bytes => RtType::Bytes,
57 PrimTy::F32 => RtType::Float { name: "f32" },
58 PrimTy::F64 => RtType::Float { name: "f64" },
59 PrimTy::U8 => int("u8", 0, u8::MAX.into()),
60 PrimTy::U16 => int("u16", 0, u16::MAX.into()),
61 PrimTy::U32 => int("u32", 0, u32::MAX.into()),
62 PrimTy::U64 => int("u64", 0, i64::MAX),
63 PrimTy::Usize => int("usize", 0, i64::MAX),
64 PrimTy::I8 => int("i8", i8::MIN.into(), i8::MAX.into()),
65 PrimTy::I16 => int("i16", i16::MIN.into(), i16::MAX.into()),
66 PrimTy::I32 => int("i32", i32::MIN.into(), i32::MAX.into()),
67 PrimTy::I64 => int("i64", i64::MIN, i64::MAX),
68 PrimTy::Isize => int("isize", i64::MIN, i64::MAX),
69 PrimTy::Type => RtType::Type,
70 }
71}
72
73pub fn type_name(resolutions: &Resolutions, ty: &Ty) -> String {
76 match &ty.kind {
77 TyKind::Path(path) => match resolutions.res.get(&path.id) {
78 Some(Res::Def(_, def)) => resolutions.defs.get(*def).name.to_string(),
79 Some(Res::PrimTy(prim)) => prim.name().to_owned(),
80 _ => "_".to_owned(),
81 },
82 TyKind::Tuple(tys) => {
83 let names: Vec<_> = tys.iter().map(|ty| type_name(resolutions, ty)).collect();
84 format!("({})", names.join(", "))
85 }
86 TyKind::Paren(ty) => type_name(resolutions, ty),
87 TyKind::Ptr(ty) => format!("*{}", type_name(resolutions, ty)),
88 TyKind::List(ty) => format!("[{}]", type_name(resolutions, ty)),
89 TyKind::Fn { params, ret } => {
90 let params: Vec<_> = params.iter().map(|ty| type_name(resolutions, ty)).collect();
91 let ret = match ret {
92 Some(ty) => type_name(resolutions, ty),
93 None => "()".to_owned(),
94 };
95 format!("fn({}) -> {}", params.join(", "), ret)
96 }
97 TyKind::Record => "@".to_owned(),
98 TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => "_".to_owned(),
99 }
100}
101
102pub fn type_def(resolutions: &Resolutions, ty: &Ty) -> Option<DefId> {
104 match &ty.kind {
105 TyKind::Path(path) => match resolutions.res.get(&path.id)? {
106 &Res::Def(DefKind::Struct | DefKind::Enum | DefKind::NativeType(_), def) => Some(def),
107 _ => None,
108 },
109 TyKind::Paren(ty) => type_def(resolutions, ty),
110 _ => None,
111 }
112}