1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! Methods for lower the HIR to types.

pub(crate) use self::diagnostics::LowerDiagnostic;
use crate::resolve::{HasResolver, TypeNs};
use crate::ty::{Substitution, TyKind};
use crate::{
    arena::map::ArenaMap,
    code_model::StructKind,
    diagnostics::DiagnosticSink,
    name_resolution::Namespace,
    primitive_type::PrimitiveType,
    resolve::Resolver,
    ty::{FnSig, Ty},
    type_ref::{LocalTypeRefId, TypeRef, TypeRefMap, TypeRefSourceMap},
    FileId, Function, HirDatabase, ModuleDef, Path, Struct, TypeAlias,
};
use crate::{HasVisibility, Visibility};
use std::{ops::Index, sync::Arc};

/// A struct which holds resolved type references to `Ty`s.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LowerTyMap {
    pub(crate) type_ref_to_type: ArenaMap<LocalTypeRefId, Ty>,
    pub(crate) diagnostics: Vec<LowerDiagnostic>,

    unknown_ty: Ty,
}

impl Default for LowerTyMap {
    fn default() -> Self {
        LowerTyMap {
            type_ref_to_type: Default::default(),
            diagnostics: vec![],
            unknown_ty: TyKind::Unknown.intern(),
        }
    }
}

impl Index<LocalTypeRefId> for LowerTyMap {
    type Output = Ty;
    fn index(&self, expr: LocalTypeRefId) -> &Ty {
        self.type_ref_to_type.get(expr).unwrap_or(&self.unknown_ty)
    }
}

impl LowerTyMap {
    /// Adds all the `LowerDiagnostic`s of the result to the `DiagnosticSink`.
    pub(crate) fn add_diagnostics(
        &self,
        db: &dyn HirDatabase,
        file_id: FileId,
        source_map: &TypeRefSourceMap,
        sink: &mut DiagnosticSink,
    ) {
        self.diagnostics
            .iter()
            .for_each(|it| it.add_to(db, file_id, source_map, sink))
    }
}

impl Ty {
    /// Tries to lower a HIR type reference to an actual resolved type. Besides the type also
    /// returns an diagnostics that where encountered along the way.
    pub(crate) fn from_hir(
        db: &dyn HirDatabase,
        resolver: &Resolver,
        type_ref_map: &TypeRefMap,
        type_ref: LocalTypeRefId,
    ) -> (Ty, Vec<diagnostics::LowerDiagnostic>) {
        let mut diagnostics = Vec::new();
        let ty =
            Ty::from_hir_with_diagnostics(db, resolver, type_ref_map, &mut diagnostics, type_ref);
        (ty, diagnostics)
    }

    /// Tries to lower a HIR type reference to an actual resolved type. Takes a mutable reference
    /// to a `Vec` which will hold any diagnostics encountered a long the way.
    fn from_hir_with_diagnostics(
        db: &dyn HirDatabase,
        resolver: &Resolver,
        type_ref_map: &TypeRefMap,
        diagnostics: &mut Vec<LowerDiagnostic>,
        type_ref: LocalTypeRefId,
    ) -> Ty {
        let res = match &type_ref_map[type_ref] {
            TypeRef::Path(path) => Ty::from_path(db, resolver, type_ref, path, diagnostics),
            TypeRef::Error => Some((TyKind::Unknown.intern(), false)),
            TypeRef::Tuple(inner) => {
                let inner_tys = inner.iter().map(|tr| {
                    Self::from_hir_with_diagnostics(db, resolver, type_ref_map, diagnostics, *tr)
                });
                Some((
                    TyKind::Tuple(inner_tys.len(), inner_tys.collect()).intern(),
                    false,
                ))
            }
            TypeRef::Never => Some((TyKind::Never.intern(), false)),
            TypeRef::Array(inner) => {
                let inner = Self::from_hir_with_diagnostics(
                    db,
                    resolver,
                    type_ref_map,
                    diagnostics,
                    *inner,
                );
                Some((TyKind::Array(inner).intern(), false))
            }
        };
        if let Some((ty, is_cyclic)) = res {
            if is_cyclic {
                diagnostics.push(LowerDiagnostic::CyclicType { id: type_ref })
            }
            ty
        } else {
            diagnostics.push(LowerDiagnostic::UnresolvedType { id: type_ref });
            TyKind::Unknown.intern()
        }
    }

    /// Constructs a `Ty` from a path.
    fn from_path(
        db: &dyn HirDatabase,
        resolver: &Resolver,
        type_ref: LocalTypeRefId,
        path: &Path,
        diagnostics: &mut Vec<LowerDiagnostic>,
    ) -> Option<(Self, bool)> {
        // Find the type
        let (ty, vis) = resolver.resolve_path_as_type_fully(db.upcast(), path)?;

        // Get the definition and visibility
        let def = match ty {
            TypeNs::StructId(id) => TypableDef::Struct(id.into()),
            TypeNs::TypeAliasId(id) => TypableDef::TypeAlias(id.into()),
            TypeNs::PrimitiveType(id) => TypableDef::PrimitiveType(id),
        };

        // Get the current module and see if the type is visible from here
        if let Some(module) = resolver.module() {
            if !vis.is_visible_from(db, module) {
                diagnostics.push(LowerDiagnostic::TypeIsPrivate { id: type_ref })
            }
        }

        Some(db.type_for_def(def, Namespace::Types))
    }
}

/// Resolves all types in the specified `TypeRefMap`.
pub fn types_from_hir(
    db: &dyn HirDatabase,
    resolver: &Resolver,
    type_ref_map: &TypeRefMap,
) -> Arc<LowerTyMap> {
    let mut result = LowerTyMap::default();
    for (id, _) in type_ref_map.iter() {
        let ty =
            Ty::from_hir_with_diagnostics(db, resolver, type_ref_map, &mut result.diagnostics, id);
        result.type_ref_to_type.insert(id, ty);
    }
    Arc::new(result)
}

pub fn lower_struct_query(db: &dyn HirDatabase, s: Struct) -> Arc<LowerTyMap> {
    let data = s.data(db.upcast());
    types_from_hir(db, &s.id.resolver(db.upcast()), data.type_ref_map())
}

pub fn lower_type_alias_query(db: &dyn HirDatabase, t: TypeAlias) -> Arc<LowerTyMap> {
    let data = t.data(db.upcast());
    types_from_hir(db, &t.id.resolver(db.upcast()), data.type_ref_map())
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TypableDef {
    Function(Function),
    PrimitiveType(PrimitiveType),
    Struct(Struct),
    TypeAlias(TypeAlias),
}

impl From<Function> for TypableDef {
    fn from(f: Function) -> Self {
        TypableDef::Function(f)
    }
}

impl From<PrimitiveType> for TypableDef {
    fn from(f: PrimitiveType) -> Self {
        TypableDef::PrimitiveType(f)
    }
}

impl From<Struct> for TypableDef {
    fn from(f: Struct) -> Self {
        TypableDef::Struct(f)
    }
}

impl From<ModuleDef> for Option<TypableDef> {
    fn from(d: ModuleDef) -> Self {
        match d {
            ModuleDef::Function(f) => Some(TypableDef::Function(f)),
            ModuleDef::PrimitiveType(t) => Some(TypableDef::PrimitiveType(t)),
            ModuleDef::Struct(t) => Some(TypableDef::Struct(t)),
            ModuleDef::TypeAlias(t) => Some(TypableDef::TypeAlias(t)),
            ModuleDef::Module(_) => None,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CallableDef {
    Function(Function),
    Struct(Struct),
}
impl_froms!(CallableDef: Function, Struct);

impl CallableDef {
    pub fn is_function(self) -> bool {
        matches!(self, CallableDef::Function(_))
    }

    pub fn is_struct(self) -> bool {
        matches!(self, CallableDef::Struct(_))
    }
}

impl HasVisibility for CallableDef {
    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
        match self {
            CallableDef::Struct(strukt) => strukt.visibility(db),
            CallableDef::Function(function) => function.visibility(db),
        }
    }
}

/// Build the declared type of an item. This depends on the namespace; e.g. for
/// `struct Foo(usize)`, we have two types: The type of the struct itself, and
/// the constructor function `(usize) -> Foo` which lives in the values
/// namespace.
pub(crate) fn type_for_def(db: &dyn HirDatabase, def: TypableDef, ns: Namespace) -> (Ty, bool) {
    let ty = match (def, ns) {
        (TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f),
        (TypableDef::PrimitiveType(t), Namespace::Types) => type_for_primitive(t),
        (TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s),
        (TypableDef::Struct(s), Namespace::Types) => type_for_struct(db, s),
        (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t),

        // 'error' cases:
        (TypableDef::Function(_), Namespace::Types) => TyKind::Unknown.intern(),
        (TypableDef::PrimitiveType(_), Namespace::Values) => TyKind::Unknown.intern(),
        (TypableDef::TypeAlias(_), Namespace::Values) => TyKind::Unknown.intern(),
    };
    (ty, false)
}

/// Recover with an unknown type when a cycle is detected in the salsa database.
pub(crate) fn type_for_cycle_recover(
    _db: &dyn HirDatabase,
    _cycle: &[String],
    _def: &TypableDef,
    _ns: &Namespace,
) -> (Ty, bool) {
    (TyKind::Unknown.intern(), true)
}

/// Build the declared type of a static.
fn type_for_primitive(def: PrimitiveType) -> Ty {
    match def {
        PrimitiveType::Float(f) => TyKind::Float(f.into()),
        PrimitiveType::Int(i) => TyKind::Int(i.into()),
        PrimitiveType::Bool => TyKind::Bool,
    }
    .intern()
}

/// Build the declared type of a function. This should not need to look at the
/// function body.
fn type_for_fn(_db: &dyn HirDatabase, def: Function) -> Ty {
    TyKind::FnDef(def.into(), Substitution::empty()).intern()
}

pub(crate) fn callable_item_sig(db: &dyn HirDatabase, def: CallableDef) -> FnSig {
    match def {
        CallableDef::Function(f) => fn_sig_for_fn(db, f),
        CallableDef::Struct(s) => fn_sig_for_struct_constructor(db, s),
    }
}

pub(crate) fn fn_sig_for_fn(db: &dyn HirDatabase, def: Function) -> FnSig {
    let data = def.data(db.upcast());
    let resolver = def.id.resolver(db.upcast());
    let params = data
        .params()
        .iter()
        .map(|tr| Ty::from_hir(db, &resolver, data.type_ref_map(), *tr).0)
        .collect::<Vec<_>>();
    let ret = Ty::from_hir(db, &resolver, data.type_ref_map(), *data.ret_type()).0;
    FnSig::from_params_and_return(params, ret)
}

pub(crate) fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: Struct) -> FnSig {
    let data = def.data(db.upcast());
    let resolver = def.id.resolver(db.upcast());
    let params = data
        .fields
        .iter()
        .map(|(_, field)| Ty::from_hir(db, &resolver, data.type_ref_map(), field.type_ref).0)
        .collect::<Vec<_>>();
    let ret = type_for_struct(db, def);
    FnSig::from_params_and_return(params, ret)
}

/// Build the type of a struct constructor.
fn type_for_struct_constructor(db: &dyn HirDatabase, def: Struct) -> Ty {
    let struct_data = db.struct_data(def.id);
    if struct_data.kind == StructKind::Tuple {
        TyKind::FnDef(def.into(), Substitution::empty()).intern()
    } else {
        type_for_struct(db, def)
    }
}

fn type_for_struct(_db: &dyn HirDatabase, def: Struct) -> Ty {
    TyKind::Struct(def).intern()
}

fn type_for_type_alias(db: &dyn HirDatabase, def: TypeAlias) -> Ty {
    let data = def.data(db.upcast());
    let resolver = def.id.resolver(db.upcast());
    let type_ref = def.type_ref(db);
    Ty::from_hir(db, &resolver, data.type_ref_map(), type_ref).0
}

pub mod diagnostics {
    use crate::diagnostics::{CyclicType, PrivateAccess, UnresolvedType};
    use crate::{
        diagnostics::DiagnosticSink,
        type_ref::{LocalTypeRefId, TypeRefSourceMap},
        FileId, HirDatabase,
    };

    #[derive(Debug, PartialEq, Eq, Clone)]
    pub(crate) enum LowerDiagnostic {
        UnresolvedType { id: LocalTypeRefId },
        TypeIsPrivate { id: LocalTypeRefId },
        CyclicType { id: LocalTypeRefId },
    }

    impl LowerDiagnostic {
        pub(crate) fn add_to(
            &self,
            _db: &dyn HirDatabase,
            file_id: FileId,
            source_map: &TypeRefSourceMap,
            sink: &mut DiagnosticSink,
        ) {
            match self {
                LowerDiagnostic::UnresolvedType { id } => sink.push(UnresolvedType {
                    file: file_id,
                    type_ref: source_map.type_ref_syntax(*id).unwrap(),
                }),
                LowerDiagnostic::CyclicType { id } => sink.push(CyclicType {
                    file: file_id,
                    type_ref: source_map.type_ref_syntax(*id).unwrap(),
                }),
                LowerDiagnostic::TypeIsPrivate { id } => sink.push(PrivateAccess {
                    file: file_id,
                    expr: source_map.type_ref_syntax(*id).unwrap().syntax_node_ptr(),
                }),
            }
        }
    }
}