This commit is contained in:
Edgar 2024-02-17 12:37:28 +01:00
parent e27c30a9f2
commit 6d31a9ea6f
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
5 changed files with 46 additions and 4 deletions

View file

@ -62,6 +62,7 @@ pub struct Ident {
pub struct Type { pub struct Type {
pub name: Ident, pub name: Ident,
pub generics: Vec<Type>, pub generics: Vec<Type>,
pub is_ref: Option<RefType>,
pub span: Span, pub span: Span,
} }
@ -72,6 +73,12 @@ pub struct FnParam {
pub span: Span, pub span: Span,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RefType {
Not,
Mut,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Block { pub struct Block {
pub body: Vec<Statement>, pub body: Vec<Statement>,

View file

@ -18,7 +18,7 @@ use inkwell::{
AddressSpace, AddressSpace,
}; };
use ir::{LocalKind, ModuleBody, ProgramBody, TypeInfo, ValueTree}; use ir::{LocalKind, ModuleBody, ProgramBody, TypeInfo, ValueTree};
use llvm_sys::debuginfo::LLVMDIFlagPublic; use llvm_sys::debuginfo::{LLVMDIFlagLValueReference, LLVMDIFlagPublic};
use tracing::{info, trace}; use tracing::{info, trace};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -1083,6 +1083,12 @@ fn compile_basic_type<'ctx>(
.ptr_sized_int_type(&ctx.target_data, None) .ptr_sized_int_type(&ctx.target_data, None)
.ptr_type(AddressSpace::default()) .ptr_type(AddressSpace::default())
.as_basic_type_enum(), .as_basic_type_enum(),
ir::TypeKind::Ref(_, _) => ctx
.ctx
.context
.ptr_sized_int_type(&ctx.target_data, None)
.ptr_type(AddressSpace::default())
.as_basic_type_enum(),
} }
} }
@ -1093,6 +1099,8 @@ fn compile_debug_type<'ctx>(ctx: &ModuleCompileCtx<'ctx, '_>, ty: &ir::TypeInfo)
// 5 = signed // 5 = signed
// 11 = numeric string // 11 = numeric string
// https://dwarfstd.org/doc/DWARF5.pdf#section.7.8 // https://dwarfstd.org/doc/DWARF5.pdf#section.7.8
// https://github.com/GaloisInc/dwarf-tools/blob/master/src/DWARF/DW/TAG.hs
match &ty.kind { match &ty.kind {
ir::TypeKind::Unit => todo!(), ir::TypeKind::Unit => todo!(),
ir::TypeKind::Bool => ctx ir::TypeKind::Bool => ctx
@ -1194,5 +1202,9 @@ fn compile_debug_type<'ctx>(ctx: &ModuleCompileCtx<'ctx, '_>, ty: &ir::TypeInfo)
AddressSpace::default(), AddressSpace::default(),
) )
.as_type(), .as_type(),
ir::TypeKind::Ref(_, inner) => ctx
.di_builder
.create_reference_type(compile_debug_type(ctx, inner), 0x10)
.as_type(),
} }
} }

View file

@ -204,6 +204,7 @@ pub enum TypeKind {
Float(FloatTy), Float(FloatTy),
FnDef(DefId, Vec<TypeInfo>), // The vec are generic types, not arg types FnDef(DefId, Vec<TypeInfo>), // The vec are generic types, not arg types
Ptr(Box<TypeInfo>), Ptr(Box<TypeInfo>),
Ref(bool, Box<TypeInfo>),
} }
impl TypeKind { impl TypeKind {
@ -235,6 +236,7 @@ impl TypeKind {
TypeKind::Unit => unreachable!(), TypeKind::Unit => unreachable!(),
TypeKind::FnDef(_, _) => unreachable!(), TypeKind::FnDef(_, _) => unreachable!(),
TypeKind::Ptr(_pointee) => todo!(), TypeKind::Ptr(_pointee) => todo!(),
TypeKind::Ref(_, inner) => inner.kind.get_falsy_value(),
} }
} }
} }

View file

@ -802,7 +802,7 @@ fn lower_path(builder: &mut BodyBuilder, info: &ast::PathExpr) -> (ir::Place, Ty
#[allow(clippy::only_used_in_recursion)] #[allow(clippy::only_used_in_recursion)]
pub fn lower_type(ctx: &BuildCtx, t: &ast::Type) -> ir::TypeInfo { pub fn lower_type(ctx: &BuildCtx, t: &ast::Type) -> ir::TypeInfo {
match t.name.name.as_str() { let inner_ty = match t.name.name.as_str() {
"()" => ir::TypeInfo { "()" => ir::TypeInfo {
span: Some(t.span), span: Some(t.span),
kind: ir::TypeKind::Unit, kind: ir::TypeKind::Unit,
@ -860,5 +860,19 @@ pub fn lower_type(ctx: &BuildCtx, t: &ast::Type) -> ir::TypeInfo {
kind: ir::TypeKind::Ptr(Box::new(lower_type(ctx, t.generics.first().unwrap()))), kind: ir::TypeKind::Ptr(Box::new(lower_type(ctx, t.generics.first().unwrap()))),
}, },
x => todo!("{:?}", x), x => todo!("{:?}", x),
};
match t.is_ref {
Some(x) => ir::TypeInfo {
span: Some(t.span),
kind: TypeKind::Ref(
match x {
ast::RefType::Not => false,
ast::RefType::Mut => true,
},
Box::new(inner_ty),
),
},
None => inner_ty,
} }
} }

View file

@ -141,14 +141,16 @@ pub(crate) Ident: ast::Ident = {
} }
pub(crate) Type: ast::Type = { pub(crate) Type: ast::Type = {
<lo:@L> <name:Ident> <hi:@R> => ast::Type { <lo:@L> <is_ref:RefType?> <name:Ident> <hi:@R> => ast::Type {
name, name,
generics: vec![], generics: vec![],
is_ref,
span: ast::Span::new(lo, hi), span: ast::Span::new(lo, hi),
}, },
<lo:@L> <name:Ident> "<" <generics:Comma<Type>> ">" <hi:@R> => ast::Type { <lo:@L> <is_ref:RefType?> <name:Ident> "<" <generics:Comma<Type>> ">" <hi:@R> => ast::Type {
name, name,
generics, generics,
is_ref,
span: ast::Span::new(lo, hi), span: ast::Span::new(lo, hi),
}, },
} }
@ -346,6 +348,11 @@ pub(crate) ValueExpr: ast::ValueExpr = {
<PathExpr> => ast::ValueExpr::Path(<>), <PathExpr> => ast::ValueExpr::Path(<>),
} }
pub(crate) RefType: ast::RefType = {
"&" => ast::RefType::Not,
"&" "mut" => ast::RefType::Mut,
}
pub(crate) FnParam: ast::FnParam = { pub(crate) FnParam: ast::FnParam = {
<lo:@L> <name:Ident> ":" <arg_type:Type> <hi:@R> => ast::FnParam { <lo:@L> <name:Ident> ":" <arg_type:Type> <hi:@R> => ast::FnParam {
name, name,