Skip to main content

xur_bytecode/
types.rs

1//! Naming written types at runtime, and the runtime checks for them.
2
3use xur_ast::{Ty, TyKind};
4use xur_resolve::{DefId, DefKind, PrimTy, Res, Resolutions};
5
6use crate::ir::RtType;
7
8/// The runtime check for a written type, or `None` when nothing about it can
9/// be checked (an inferred or generic type).
10pub 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
42                .iter()
43                .map(|ty| rt_type_or_any(resolutions, ty))
44                .collect(),
45            ret: Box::new(match ret {
46                Some(ty) => rt_type_or_any(resolutions, ty),
47                None => RtType::Unit,
48            }),
49        },
50        TyKind::Record => RtType::Record,
51        TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => RtType::Any,
52    }
53}
54
55pub(crate) fn prim_rt_type(prim: PrimTy) -> RtType {
56    let int = |name, min, max| RtType::Int { name, min, max };
57    match prim {
58        PrimTy::Bool => RtType::Bool,
59        PrimTy::Str => RtType::Str,
60        PrimTy::Bytes => RtType::Bytes,
61        PrimTy::F32 => RtType::Float { name: "f32" },
62        PrimTy::F64 => RtType::Float { name: "f64" },
63        PrimTy::U8 => int("u8", 0, u8::MAX as i128),
64        PrimTy::U16 => int("u16", 0, u16::MAX as i128),
65        PrimTy::U32 => int("u32", 0, u32::MAX as i128),
66        PrimTy::U64 => int("u64", 0, u64::MAX as i128),
67        PrimTy::Usize => int("usize", 0, u64::MAX as i128),
68        PrimTy::I8 => int("i8", i8::MIN as i128, i8::MAX as i128),
69        PrimTy::I16 => int("i16", i16::MIN as i128, i16::MAX as i128),
70        PrimTy::I32 => int("i32", i32::MIN as i128, i32::MAX as i128),
71        PrimTy::I64 => int("i64", i64::MIN as i128, i64::MAX as i128),
72        PrimTy::Isize => int("isize", i64::MIN as i128, i64::MAX as i128),
73        PrimTy::Map => RtType::Map,
74        PrimTy::Record => RtType::Record,
75        PrimTy::Type => RtType::Type,
76    }
77}
78
79/// How a written type is named at runtime, e.g. by `type_name`: `u32`,
80/// `Point`, `[u8]`, or `_` for a type left to be inferred.
81pub fn type_name(resolutions: &Resolutions, ty: &Ty) -> String {
82    match &ty.kind {
83        TyKind::Path(path) => match resolutions.res.get(&path.id) {
84            Some(Res::Def(_, def)) => resolutions.defs.get(*def).name.to_string(),
85            Some(Res::PrimTy(prim)) => prim.name().to_owned(),
86            _ => "_".to_owned(),
87        },
88        TyKind::Tuple(tys) => {
89            let names: Vec<_> = tys.iter().map(|ty| type_name(resolutions, ty)).collect();
90            format!("({})", names.join(", "))
91        }
92        TyKind::Paren(ty) => type_name(resolutions, ty),
93        TyKind::Ptr(ty) => format!("*{}", type_name(resolutions, ty)),
94        TyKind::List(ty) => format!("[{}]", type_name(resolutions, ty)),
95        TyKind::Fn { params, ret } => {
96            let params: Vec<_> = params.iter().map(|ty| type_name(resolutions, ty)).collect();
97            let ret = match ret {
98                Some(ty) => type_name(resolutions, ty),
99                None => "()".to_owned(),
100            };
101            format!("fn({}) -> {}", params.join(", "), ret)
102        }
103        TyKind::Record => "@".to_owned(),
104        TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => "_".to_owned(),
105    }
106}
107
108/// The struct, enum or native type a written type names, if any.
109pub fn type_def(resolutions: &Resolutions, ty: &Ty) -> Option<DefId> {
110    match &ty.kind {
111        TyKind::Path(path) => match resolutions.res.get(&path.id)? {
112            &Res::Def(DefKind::Struct | DefKind::Enum | DefKind::NativeType(_), def) => Some(def),
113            _ => None,
114        },
115        TyKind::Paren(ty) => type_def(resolutions, ty),
116        _ => None,
117    }
118}