fe_mir/db/queries/
constant.rs1use std::rc::Rc;
2
3use fe_analyzer::namespace::items as analyzer_items;
4
5use crate::{
6 db::MirDb,
7 ir::{Constant, ConstantId, SourceInfo, TypeId},
8};
9
10pub fn mir_lowered_constant(
11 db: &dyn MirDb,
12 analyzer_const: analyzer_items::ModuleConstantId,
13) -> ConstantId {
14 let name = analyzer_const.name(db.upcast());
15 let value = analyzer_const.constant_value(db.upcast()).unwrap();
16 let ty = analyzer_const.typ(db.upcast()).unwrap();
17 let module_id = analyzer_const.module(db.upcast());
18 let span = analyzer_const.span(db.upcast());
19 let id = analyzer_const.node_id(db.upcast());
20
21 let ty = db.mir_lowered_type(ty);
22 let source = SourceInfo { span, id };
23
24 let constant = Constant {
25 name,
26 value: value.into(),
27 ty,
28 module_id,
29 source,
30 };
31
32 db.mir_intern_const(constant.into())
33}
34
35impl ConstantId {
36 pub fn data(self, db: &dyn MirDb) -> Rc<Constant> {
37 db.lookup_mir_intern_const(self)
38 }
39
40 pub fn ty(self, db: &dyn MirDb) -> TypeId {
41 self.data(db).ty
42 }
43}