Skip to content

Commit

Permalink
refs
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 17, 2024
1 parent e27c30a commit 6d31a9e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/edlang_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct Ident {
pub struct Type {
pub name: Ident,
pub generics: Vec<Type>,
pub is_ref: Option<RefType>,
pub span: Span,
}

Expand All @@ -72,6 +73,12 @@ pub struct FnParam {
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)]
pub struct Block {
pub body: Vec<Statement>,
Expand Down
14 changes: 13 additions & 1 deletion lib/edlang_codegen_llvm/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use inkwell::{
AddressSpace,
};
use ir::{LocalKind, ModuleBody, ProgramBody, TypeInfo, ValueTree};
use llvm_sys::debuginfo::LLVMDIFlagPublic;
use llvm_sys::debuginfo::{LLVMDIFlagLValueReference, LLVMDIFlagPublic};
use tracing::{info, trace};

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -1083,6 +1083,12 @@ fn compile_basic_type<'ctx>(
.ptr_sized_int_type(&ctx.target_data, None)
.ptr_type(AddressSpace::default())
.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(),
}
}

Expand All @@ -1093,6 +1099,8 @@ fn compile_debug_type<'ctx>(ctx: &ModuleCompileCtx<'ctx, '_>, ty: &ir::TypeInfo)
// 5 = signed
// 11 = numeric string
// 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 {
ir::TypeKind::Unit => todo!(),
ir::TypeKind::Bool => ctx
Expand Down Expand Up @@ -1194,5 +1202,9 @@ fn compile_debug_type<'ctx>(ctx: &ModuleCompileCtx<'ctx, '_>, ty: &ir::TypeInfo)
AddressSpace::default(),
)
.as_type(),
ir::TypeKind::Ref(_, inner) => ctx
.di_builder
.create_reference_type(compile_debug_type(ctx, inner), 0x10)
.as_type(),
}
}
2 changes: 2 additions & 0 deletions lib/edlang_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub enum TypeKind {
Float(FloatTy),
FnDef(DefId, Vec<TypeInfo>), // The vec are generic types, not arg types
Ptr(Box<TypeInfo>),
Ref(bool, Box<TypeInfo>),
}

impl TypeKind {
Expand Down Expand Up @@ -235,6 +236,7 @@ impl TypeKind {
TypeKind::Unit => unreachable!(),
TypeKind::FnDef(_, _) => unreachable!(),
TypeKind::Ptr(_pointee) => todo!(),
TypeKind::Ref(_, inner) => inner.kind.get_falsy_value(),
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion lib/edlang_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ fn lower_path(builder: &mut BodyBuilder, info: &ast::PathExpr) -> (ir::Place, Ty

#[allow(clippy::only_used_in_recursion)]
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 {
span: Some(t.span),
kind: ir::TypeKind::Unit,
Expand Down Expand Up @@ -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()))),
},
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,
}
}
11 changes: 9 additions & 2 deletions lib/edlang_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ pub(crate) Ident: ast::Ident = {
}

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,
generics: vec![],
is_ref,
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,
generics,
is_ref,
span: ast::Span::new(lo, hi),
},
}
Expand Down Expand Up @@ -346,6 +348,11 @@ pub(crate) ValueExpr: ast::ValueExpr = {
<PathExpr> => ast::ValueExpr::Path(<>),
}

pub(crate) RefType: ast::RefType = {
"&" => ast::RefType::Not,
"&" "mut" => ast::RefType::Mut,
}

pub(crate) FnParam: ast::FnParam = {
<lo:@L> <name:Ident> ":" <arg_type:Type> <hi:@R> => ast::FnParam {
name,
Expand Down

0 comments on commit 6d31a9e

Please sign in to comment.