Skip to content
Merged
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
38 changes: 2 additions & 36 deletions src/ast/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,22 +562,13 @@ pub struct Field {
pub pltype: PLType,
pub name: String,
pub range: Range,
pub is_ref: bool,
pub refs: Rc<RefCell<Vec<Location>>>,
}

impl Field {
pub fn get_di_type<'a, 'ctx>(&self, ctx: &Ctx<'a, 'ctx>, offset: u64) -> (DIType<'ctx>, u64) {
let di_type = self.pltype.get_ditype(ctx);
let debug_type = if self.is_ref {
self.pltype
.clone()
.get_di_ref_type(ctx, di_type.clone())
.unwrap()
.as_type()
} else {
di_type.unwrap()
};
let debug_type = di_type.unwrap();
(
ctx.dibuilder
.create_member_type(
Expand All @@ -604,7 +595,6 @@ pub struct FNType {
pub ret_pltype: Box<PLType>,
pub range: Range,
pub refs: Rc<RefCell<Vec<Location>>>,
pub is_ref: bool,
pub doc: Vec<Box<NodeEnum>>,
}

Expand Down Expand Up @@ -659,17 +649,7 @@ impl STType {
.ordered_fields
.clone()
.into_iter()
.map(|order_field| {
if order_field.is_ref {
order_field
.pltype
.get_basic_type(&ctx)
.ptr_type(inkwell::AddressSpace::Generic)
.as_basic_type_enum()
} else {
order_field.pltype.get_basic_type(&ctx)
}
})
.map(|order_field| order_field.pltype.get_basic_type(&ctx))
.collect::<Vec<_>>(),
false,
);
Expand Down Expand Up @@ -1003,20 +983,6 @@ impl<'a, 'ctx> Ctx<'a, 'ctx> {
_ => v,
}
}
// load type** and type* to type*
pub fn try_load2ptr(&mut self, v: Value<'ctx>) -> Value<'ctx> {
match v.as_basic_value_enum() {
BasicValueEnum::PointerValue(ptr2value) => {
if ptr2value.get_type().get_element_type().is_pointer_type() {
let value = self.builder.build_load(ptr2value, "loadtmp");
Value::VarValue(value.into_pointer_value())
} else {
Value::VarValue(ptr2value)
}
}
_ => v,
}
}

pub fn if_completion(&mut self, c: impl FnOnce(&mut Ctx, &(Pos, Option<String>))) {
if let Some(tp) = self.action {
Expand Down
75 changes: 11 additions & 64 deletions src/ast/node/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,15 @@ impl Node for FuncDefNode {
para_pltypes.push(pltype.clone());
let di_type = pltype.get_ditype(ctx);
let di_type = di_type.unwrap();
let di_ref_type = pltype.clone().get_di_ref_type(ctx, Some(di_type)).unwrap();
param_ditypes.push(if para.tp.is_ref {
di_ref_type.as_type()
} else {
di_type
});
param_ditypes.push(di_type);
}
}
}
ctx.push_semantic_token(self.typenode.ret.range, SemanticTokenType::TYPE, 0);
if let Some(body) = self.body.as_mut() {
let subroutine_type = ctx.dibuilder.create_subroutine_type(
ctx.diunit.get_file(),
{
let pltype = self.typenode.ret.get_type(ctx)?;
let di_type = pltype.get_ditype(ctx);
let di_ref_type = pltype.clone().get_di_ref_type(ctx, di_type);
if self.typenode.ret.is_ref {
di_ref_type.and_then(|v| Some(v.as_type()))
} else {
di_type
}
},
self.typenode.ret.get_type(ctx)?.get_ditype(ctx),
&param_ditypes,
DIFlags::PUBLIC,
);
Expand Down Expand Up @@ -153,13 +139,6 @@ impl Node for FuncDefNode {
}
op.unwrap()
};
let ret_type = if self.typenode.ret.is_ref {
ret_type
.ptr_type(inkwell::AddressSpace::Generic)
.as_basic_type_enum()
} else {
ret_type
};
Some(alloc(&mut ctx, ret_type, "retvalue"))
} else {
None
Expand Down Expand Up @@ -259,11 +238,7 @@ impl Node for FuncCallNode {
for (i, para) in self.paralist.iter_mut().enumerate() {
let pararange = para.range();
let (value, _, _, _) = para.emit(ctx)?;
let load_op = if let Value::RefValue(ptr) = value {
Some(ptr.as_basic_value_enum())
} else {
ctx.try_load2var(value).as_basic_value_enum_op()
};
let load_op = ctx.try_load2var(value).as_basic_value_enum_op();
if load_op.is_none() {
return Ok((Value::None, None, TerminatorEnum::NONE, false));
}
Expand All @@ -282,23 +257,12 @@ impl Node for FuncCallNode {
if let PLType::FN(fv) = &fntp {
ctx.save_if_comment_doc_hover(self.range, Some(fv.doc.clone()));
let o = match ret.try_as_basic_value().left() {
Some(v) => {
if v.is_pointer_value() {
Ok((
Value::RefValue(v.into_pointer_value()),
Some(*fv.ret_pltype.clone()),
TerminatorEnum::NONE,
false,
))
} else {
Ok((
Value::LoadValue(v),
Some(*fv.ret_pltype.clone()),
TerminatorEnum::NONE,
false,
))
}
}
Some(v) => Ok((
Value::LoadValue(v),
Some(*fv.ret_pltype.clone()),
TerminatorEnum::NONE,
false,
)),
None => Ok((
Value::None,
Some(*fv.ret_pltype.clone()),
Expand Down Expand Up @@ -335,30 +299,14 @@ impl FuncTypeNode {
for para in self.paralist.iter() {
let paramtype = para.tp.get_type(ctx)?;
param_pltypes.push(paramtype.clone());
para_types.push(if para.tp.is_ref {
paramtype
.get_basic_type(&ctx)
.ptr_type(inkwell::AddressSpace::Generic)
.into()
} else {
paramtype.get_basic_type(&ctx).into()
});
para_types.push(paramtype.get_basic_type(&ctx).into());
}
let pltype = self.ret.get_type(ctx)?;
let func_type = if pltype.is_void() {
// void type
ctx.context.void_type().fn_type(&para_types, false)
} else {
// is ref
if self.ret.is_ref {
pltype
.get_basic_type(&ctx)
.ptr_type(inkwell::AddressSpace::Generic)
.as_basic_type_enum()
.fn_type(&para_types, false)
} else {
pltype.get_basic_type(&ctx).fn_type(&para_types, false)
}
pltype.get_basic_type(&ctx).fn_type(&para_types, false)
};
let mut name = self.id.clone();
if !self.declare {
Expand All @@ -373,7 +321,6 @@ impl FuncTypeNode {
param_pltypes,
range: self.range,
refs: Rc::new(RefCell::new(refs)),
is_ref: self.ret.is_ref,
doc: self.doc.clone(),
});
ctx.set_if_refs_tp(&ftp, self.range);
Expand Down
20 changes: 3 additions & 17 deletions src/ast/node/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,7 @@ impl GlobalNode {
}
let pltype = pltype_opt.unwrap();
let ditype = pltype.get_ditype(ctx);
let (base_value, debug_type) = if let Value::RefValue(ref_value) = value {
(
ref_value.as_basic_value_enum(),
Some(
pltype
.clone()
.get_di_ref_type(ctx, ditype.clone())
.unwrap()
.as_type(),
),
)
} else {
let ditype = ditype.clone();
(ctx.try_load2var(value).as_basic_value_enum(), ditype)
};
let base_value = ctx.try_load2var(value).as_basic_value_enum();
let base_type = base_value.get_type();
let globalptr = ctx.module.add_global(base_type, None, &self.var.name);
globalptr.set_initializer(&base_type.const_zero());
Expand All @@ -65,11 +51,11 @@ impl GlobalNode {
"",
ctx.diunit.get_file(),
self.var.range.start.line as u32,
debug_type.unwrap(),
ditype.unwrap(),
false,
None,
None,
debug_type.unwrap().get_align_in_bits(),
ditype.unwrap().get_align_in_bits(),
);
globalptr.set_metadata(exp.as_metadata_value(ctx.context), 0);
ctx.add_symbol(
Expand Down
2 changes: 0 additions & 2 deletions src/ast/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ pub enum Value<'a> {
FloatValue(FloatValue<'a>),
VarValue(PointerValue<'a>),
LoadValue(BasicValueEnum<'a>),
RefValue(PointerValue<'a>),
StructFieldValue((String, BasicValueEnum<'a>)),
FnValue(FunctionValue<'a>),
STValue(StructType<'a>),
Expand All @@ -87,7 +86,6 @@ impl<'a> Value<'a> {
Value::IntValue(v) => Some(v.as_basic_value_enum()),
Value::FloatValue(v) => Some(v.as_basic_value_enum()),
Value::VarValue(v) => Some(v.as_basic_value_enum()),
Value::RefValue(v) => Some(v.as_basic_value_enum()),
Value::BoolValue(v) => Some(v.as_basic_value_enum()),
Value::LoadValue(v) => Some(*v),
Value::StructFieldValue((_, v)) => Some(*v),
Expand Down
12 changes: 0 additions & 12 deletions src/ast/node/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ impl Node for UnaryOpNode {
}
fn emit<'a, 'ctx>(&'a mut self, ctx: &mut Ctx<'a, 'ctx>) -> NodeResult<'ctx> {
let (exp, pltype, _, is_const) = self.exp.emit(ctx)?;
if let (&Value::VarValue(_), TokenType::REF) = (&exp, self.op) {
if is_const {
return Err(ctx.add_err(self.range, ErrorCode::REF_CONST));
}
if let Value::VarValue(exp) = ctx.try_load2ptr(exp) {
return Ok((Value::RefValue(exp), pltype, TerminatorEnum::NONE, false));
}
todo!()
}
let exp = ctx.try_load2var(exp);
return Ok(match (exp, self.op) {
(Value::IntValue(exp), TokenType::MINUS) => (
Expand Down Expand Up @@ -227,9 +218,6 @@ impl Node for TakeOpNode {
return Err(ctx.add_err(self.range, ErrorCode::INVALID_GET_FIELD));
}
let mut pltype = pltype.unwrap();
if self.ids.len() != 0 {
res = ctx.try_load2ptr(res);
}
for id in &self.ids {
res = match res.as_basic_value_enum() {
BasicValueEnum::PointerValue(s) => {
Expand Down
6 changes: 1 addition & 5 deletions src/ast/node/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ impl Node for RetNode {
return Err(err);
}
let (ret, _, _, _) = ret.emit(ctx)?;
let ret = if rettp.unwrap().is_pointer_type() {
ctx.try_load2ptr(ret)
} else {
ctx.try_load2var(ret)
};
let ret = ctx.try_load2var(ret);
if ret.as_basic_value_enum().get_type() != rettp.unwrap() {
let err = ctx.add_err(self.range, ErrorCode::RETURN_TYPE_MISMATCH);
return Err(err);
Expand Down
14 changes: 1 addition & 13 deletions src/ast/node/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,7 @@ impl Node for DefNode {
}
let pltype = pltype_opt.unwrap();
let ditype = pltype.get_ditype(ctx);
let (base_value, debug_type) = if let Value::RefValue(ref_value) = value {
(
ref_value.as_basic_value_enum(),
Some(
pltype
.clone()
.get_di_ref_type(ctx, ditype.clone())
.unwrap()
.as_type(),
),
)
} else {
let (base_value, debug_type) = {
let ditype = ditype.clone();
let loadv = ctx.try_load2var(value);
if let Value::None = loadv {
Expand Down Expand Up @@ -103,7 +92,6 @@ impl Node for AssignNode {
return Err(ctx.add_err(vrange, ErrorCode::ASSIGN_CONST));
}
let (value, _, _, _) = self.exp.emit(ctx)?;
let ptr = ctx.try_load2ptr(ptr);
if let Value::VarValue(ptr) = ptr {
let load = ctx.try_load2var(value);
if ptr.get_type().get_element_type()
Expand Down
18 changes: 2 additions & 16 deletions src/ast/node/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use rustc_hash::FxHashMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeNameNode {
pub id: Option<ExternIDNode>,
pub is_ref: bool,
}

impl TypeNameNode {
Expand Down Expand Up @@ -133,7 +132,6 @@ impl StructDefNode {
pltype: tp.clone(),
name: field.id.name.clone(),
range: field.range,
is_ref: field.tp.is_ref,
refs: Rc::new(RefCell::new(vec![])),
};
ctx.send_if_go_to_def(f.range, f.range, ctx.plmod.path.clone());
Expand All @@ -151,17 +149,7 @@ impl StructDefNode {
st.set_body(
&order_fields
.into_iter()
.map(|order_field| {
if order_field.is_ref {
order_field
.pltype
.get_basic_type(&ctx)
.ptr_type(inkwell::AddressSpace::Generic)
.as_basic_type_enum()
} else {
order_field.pltype.get_basic_type(&ctx)
}
})
.map(|order_field| order_field.pltype.get_basic_type(&ctx))
.collect::<Vec<_>>(),
false,
);
Expand Down Expand Up @@ -204,9 +192,7 @@ impl Node for StructInitFieldNode {
if !self.has_comma {
return Err(ctx.add_err(self.range, ErrorCode::COMPLETION));
}
let value = if let Value::RefValue(_) = v {
v.as_basic_value_enum()
} else {
let value = {
let v = ctx.try_load2var(v);
let vop = v.as_basic_value_enum_op();
if vop.is_none() {
Expand Down
1 change: 0 additions & 1 deletion src/ast/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ define_tokens!(
RETURN = "return",
DOT = ".",
SEMI = ";",
REF = "&",
CONST = "const",
USE = "use",
DOUBLE_COLON = "::"
Expand Down
Loading