Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/mun_codegen/src/ir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ impl<'db, 'ink, 't> BodyIrGenerator<'db, 'ink, 't> {
}
ValueNs::StructId(_) => self.gen_unit_struct_lit(expr),
ValueNs::FunctionId(_) => panic!("unable to generate path expression from a function"),
ValueNs::ConstId(_const_id) => todo!(),
}
}

Expand Down Expand Up @@ -624,7 +625,7 @@ impl<'db, 'ink, 't> BodyIrGenerator<'db, 'ink, 't> {
.pat_to_local
.get(&pat)
.expect("unresolved local binding"),
ValueNs::FunctionId(_) | ValueNs::StructId(_) => {
ValueNs::FunctionId(_) | ValueNs::StructId(_) | ValueNs::ConstId(_) => {
panic!("no support for module definitions")
}
}
Expand Down
9 changes: 6 additions & 3 deletions crates/mun_codegen/src/ir/file_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ pub(crate) fn gen_file_group_ir<'ink>(
);
}
}
// TODO: Extern types for functions?
ModuleDef::Module(_)
| ModuleDef::Struct(_)
| ModuleDef::PrimitiveType(_)
| ModuleDef::TypeAlias(_)
| ModuleDef::Function(_) => (),
| ModuleDef::Function(_)
| ModuleDef::Const(_) => (),
}
}

Expand Down Expand Up @@ -140,7 +140,10 @@ pub(crate) fn gen_file_group_ir<'ink>(
ModuleDef::Function(f) => {
type_table_builder.collect_fn(f);
}
ModuleDef::PrimitiveType(_) | ModuleDef::TypeAlias(_) | ModuleDef::Module(_) => (),
ModuleDef::PrimitiveType(_)
| ModuleDef::TypeAlias(_)
| ModuleDef::Module(_)
| ModuleDef::Const(_) => (),
}
}

Expand Down
6 changes: 6 additions & 0 deletions crates/mun_hir/src/code_model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod r#const;
mod function;
mod r#impl;
mod module;
Expand All @@ -9,6 +10,8 @@ mod type_alias;

use std::sync::Arc;

use r#const::Const;

pub use self::{
function::{Function, FunctionData},
module::{Module, ModuleDef},
Expand All @@ -25,19 +28,22 @@ use crate::{expr::BodySourceMap, HirDatabase, Name};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DefWithBody {
Function(Function),
Const(Const),
}
impl_froms!(DefWithBody: Function);

impl DefWithBody {
pub fn module(self, db: &dyn HirDatabase) -> Module {
match self {
DefWithBody::Function(f) => f.module(db),
DefWithBody::Const(_) => todo!(),
}
}

pub fn body_source_map(self, db: &dyn HirDatabase) -> Arc<BodySourceMap> {
match self {
DefWithBody::Function(f) => f.body_source_map(db),
DefWithBody::Const(_) => todo!(),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/mun_hir/src/code_model/const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::{ids::ConstId, DiagnosticSink, HirDatabase};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Const {
pub(crate) id: ConstId,
}
24 changes: 23 additions & 1 deletion crates/mun_hir/src/code_model/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use mun_hir_input::{FileId, ModuleId};

use super::{r#impl::Impl, AssocItem, Function, Package, PrimitiveType, Struct, TypeAlias};
use super::{
r#const::Const, r#impl::Impl, AssocItem, DefWithBody, Function, Package, PrimitiveType, Struct,
TypeAlias,
};
use crate::{ids::ItemDefinitionId, DiagnosticSink, HirDatabase};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
Expand Down Expand Up @@ -97,6 +100,7 @@ impl Module {
ModuleDef::Function(f) => f.diagnostics(db, sink),
ModuleDef::Struct(s) => s.diagnostics(db, sink),
ModuleDef::TypeAlias(t) => t.diagnostics(db, sink),
ModuleDef::Const(_) => todo!(),
_ => (),
}
}
Expand Down Expand Up @@ -161,11 +165,22 @@ impl Module {
pub enum ModuleDef {
Module(Module),
Function(Function),
Const(Const),
PrimitiveType(PrimitiveType),
Struct(Struct),
TypeAlias(TypeAlias),
}

impl ModuleDef {
pub fn as_def_with_body(self) -> Option<DefWithBody> {
match self {
ModuleDef::Function(f) => Some(DefWithBody::Function(f)),
ModuleDef::Const(c) => Some(DefWithBody::Const(c)),
_ => None,
}
}
}

impl From<Function> for ModuleDef {
fn from(t: Function) -> Self {
ModuleDef::Function(t)
Expand All @@ -184,6 +199,12 @@ impl From<Struct> for ModuleDef {
}
}

impl From<Const> for ModuleDef {
fn from(t: Const) -> Self {
ModuleDef::Const(t)
}
}

impl From<TypeAlias> for ModuleDef {
fn from(t: TypeAlias) -> Self {
ModuleDef::TypeAlias(t)
Expand All @@ -202,6 +223,7 @@ impl From<ItemDefinitionId> for ModuleDef {
ItemDefinitionId::ModuleId(id) => Module { id }.into(),
ItemDefinitionId::FunctionId(id) => Function { id }.into(),
ItemDefinitionId::StructId(id) => Struct { id }.into(),
ItemDefinitionId::ConstId(id) => Const { id }.into(),
ItemDefinitionId::TypeAliasId(id) => TypeAlias { id }.into(),
ItemDefinitionId::PrimitiveType(ty) => PrimitiveType { inner: ty }.into(),
}
Expand Down
2 changes: 2 additions & 0 deletions crates/mun_hir/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub trait InternDatabase: SourceDatabase {
#[salsa::interned]
fn intern_struct(&self, loc: ids::StructLoc) -> ids::StructId;
#[salsa::interned]
fn intern_const(&self, loc: ids::ConstLoc) -> ids::ConstId;
#[salsa::interned]
fn intern_type_alias(&self, loc: ids::TypeAliasLoc) -> ids::TypeAliasId;
#[salsa::interned]
fn intern_impl(self, loc: ids::ImplLoc) -> ids::ImplId;
Expand Down
1 change: 1 addition & 0 deletions crates/mun_hir/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Body {
collector = ExprCollector::new(def, src.file_id, db);
collector.collect_fn_body(&src.value);
}
DefWithBodyId::ConstId(_const_id) => todo!(),
}

let (body, source_map) = collector.finish();
Expand Down
16 changes: 15 additions & 1 deletion crates/mun_hir/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher};
use mun_hir_input::ModuleId;

use crate::{
item_tree::{Function, Impl, ItemTreeId, ItemTreeNode, Struct, TypeAlias},
item_tree::{Const, Function, Impl, ItemTreeId, ItemTreeNode, Struct, TypeAlias},
primitive_type::PrimitiveType,
DefDatabase,
};
Expand Down Expand Up @@ -123,6 +123,12 @@ pub struct StructId(salsa::InternId);
pub(crate) type StructLoc = ItemLoc<Struct>;
impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ConstId(salsa::InternId);

pub(crate) type ConstLoc = AssocItemLoc<Const>;
impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TypeAliasId(salsa::InternId);

Expand All @@ -149,6 +155,7 @@ pub enum ItemDefinitionId {
ModuleId(ModuleId),
FunctionId(FunctionId),
StructId(StructId),
ConstId(ConstId),
TypeAliasId(TypeAliasId),
PrimitiveType(PrimitiveType),
}
Expand All @@ -171,6 +178,12 @@ impl From<StructId> for ItemDefinitionId {
}
}

impl From<ConstId> for ItemDefinitionId {
fn from(id: ConstId) -> Self {
ItemDefinitionId::ConstId(id)
}
}

impl From<TypeAliasId> for ItemDefinitionId {
fn from(id: TypeAliasId) -> Self {
ItemDefinitionId::TypeAliasId(id)
Expand All @@ -193,6 +206,7 @@ pub enum AssocItemId {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DefWithBodyId {
FunctionId(FunctionId),
ConstId(ConstId),
}

impl From<FunctionId> for DefWithBodyId {
Expand Down
1 change: 1 addition & 0 deletions crates/mun_hir/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl PerNs<(ItemDefinitionId, Visibility)> {
PerNs::types((def, vis))
}
}
ItemDefinitionId::ConstId(_) => PerNs::values((def, vis)),
ItemDefinitionId::TypeAliasId(_) | ItemDefinitionId::PrimitiveType(_) => {
PerNs::types((def, vis))
}
Expand Down
10 changes: 10 additions & 0 deletions crates/mun_hir/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct ItemTreeData {
fields: Arena<Field>,
type_aliases: Arena<TypeAlias>,
impls: Arena<Impl>,
consts: Arena<Const>,

visibilities: ItemVisibilities,
}
Expand Down Expand Up @@ -230,6 +231,7 @@ mod_items! {
TypeAlias in type_aliases -> ast::TypeAliasDef,
Import in imports -> ast::Use,
Impl in impls -> ast::Impl,
Const in consts -> ast::Const,
}

macro_rules! impl_index {
Expand Down Expand Up @@ -336,6 +338,13 @@ impl FunctionFlags {
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Const {
pub name: Name,
pub visibility: RawVisibilityId,
pub ast_id: FileAstId<ast::Const>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Param {
pub type_ref: LocalTypeRefId,
Expand Down Expand Up @@ -530,6 +539,7 @@ mod diagnostics {
)
}
ModItem::Impl(_) => unreachable!("impls cannot be duplicated"),
ModItem::Const(_item) => todo!(),
}
}

Expand Down
25 changes: 22 additions & 3 deletions crates/mun_hir/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use mun_syntax::ast::{
use smallvec::SmallVec;

use super::{
diagnostics, AssociatedItem, Field, Fields, Function, FunctionFlags, IdRange, Impl, ItemTree,
ItemTreeData, ItemTreeNode, ItemVisibilities, LocalItemTreeId, ModItem, Param, ParamAstId,
RawVisibilityId, Struct, TypeAlias,
diagnostics, AssociatedItem, Const, Field, Fields, Function, FunctionFlags, IdRange, Impl,
ItemTree, ItemTreeData, ItemTreeNode, ItemVisibilities, LocalItemTreeId, ModItem, Param,
ParamAstId, RawVisibilityId, Struct, TypeAlias,
};
use crate::{
item_tree::Import,
Expand Down Expand Up @@ -88,6 +88,7 @@ impl Context {
.map_or_else(|| import.path.last_segment(), |alias| alias.as_name())
}
}
ModItem::Const(item) => Some(&self.data.consts[item.index].name),
ModItem::Impl(_) => None,
};
if let Some(name) = name {
Expand Down Expand Up @@ -122,9 +123,27 @@ impl Context {
self.lower_use(&ast).into_iter().map(Into::into).collect(),
)),
ast::ModuleItemKind::Impl(ast) => self.lower_impl(&ast).map(Into::into),
ast::ModuleItemKind::Const(ast) => self.lower_const(ast).map(Into::into),
}
}

/// Lowers a const
fn lower_const(&mut self, ast: ast::Const) -> Option<LocalItemTreeId<Const>> {
let name = ast.name()?.as_name();
let visibility = lower_visibility(&ast);

Some(
self.data
.consts
.alloc(Const {
name,
visibility,
ast_id: self.source_ast_id_map.ast_id(&ast),
})
.into(),
)
}

/// Lowers a `use` statement
fn lower_use(&mut self, use_item: &ast::Use) -> Vec<LocalItemTreeId<Import>> {
let visibility = lower_visibility(use_item);
Expand Down
1 change: 1 addition & 0 deletions crates/mun_hir/src/item_tree/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl Printer<'_> {
ModItem::TypeAlias(it) => self.print_type_alias(it),
ModItem::Import(it) => self.print_use(it),
ModItem::Impl(it) => self.print_impl(it),
ModItem::Const(_it) => todo!(),
}
}

Expand Down
22 changes: 21 additions & 1 deletion crates/mun_hir/src/package_defs/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use rustc_hash::FxHashMap;
use super::PackageDefs;
use crate::{
ids::{
FunctionLoc, ImplLoc, Intern, ItemContainerId, ItemDefinitionId, StructLoc, TypeAliasLoc,
ConstLoc, FunctionLoc, ImplLoc, Intern, ItemContainerId, ItemDefinitionId, StructLoc,
TypeAliasLoc,
},
item_scope::{ImportType, ItemScope, PerNsGlobImports},
item_tree::{
Expand Down Expand Up @@ -525,6 +526,7 @@ impl<'a> ModCollectorContext<'a, '_> {
self.collect_impl(id);
continue;
}
ModItem::Const(id) => self.collect_const(id),
};

self.def_collector.package_defs.modules[self.module_id].add_definition(id);
Expand Down Expand Up @@ -603,6 +605,24 @@ impl<'a> ModCollectorContext<'a, '_> {
}
}

fn collect_const(&self, id: LocalItemTreeId<item_tree::Const>) -> DefData<'a> {
let func = &self.item_tree[id];
DefData {
id: ConstLoc {
container: ItemContainerId::ModuleId(ModuleId {
package: self.def_collector.package_id,
local_id: self.module_id,
}),
id: ItemTreeId::new(self.file_id, id),
}
.intern(self.def_collector.db)
.into(),
name: &func.name,
visibility: &self.item_tree[func.visibility],
has_constructor: false,
}
}

/// Collects the definition data from a `TypeAlias`
fn collect_type_alias(&self, id: LocalItemTreeId<TypeAlias>) -> DefData<'a> {
let type_alias = &self.item_tree[id];
Expand Down
Loading