Skip to content

Commit

Permalink
feat(ir): span for all ast nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniMay committed Feb 23, 2024
1 parent 8d8e570 commit 213a4b5
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 173 deletions.
211 changes: 112 additions & 99 deletions src/ir/frontend/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,35 @@ pub type AstNodeBox = Box<AstNode>;
/// An abstract syntax tree node.
#[derive(Debug)]
pub struct AstNode {
pub(super) span: Option<Span>,
pub(super) span: Span,
pub(super) kind: AstNodeKind,
}

impl AstNode {
pub fn new_boxed(kind: AstNodeKind) -> AstNodeBox {
Box::new(AstNode { span: None, kind })
pub fn new_boxed(kind: AstNodeKind, span: Span) -> AstNodeBox {
Box::new(AstNode { span, kind })
}
}

impl AstNode {
pub fn new_boxed_global_ident(name: String, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::GlobalIdent(name));
node.span = Some(span);
node
AstNode::new_boxed(AstNodeKind::GlobalIdent(name), span)
}

pub fn new_boxed_local_ident(name: String, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::LocalIdent(name));
node.span = Some(span);
node
AstNode::new_boxed(AstNodeKind::LocalIdent(name), span)
}

pub fn new_boxed_bytes(bytes: Vec<u8>, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Bytes(bytes));
node.span = Some(span);
node
AstNode::new_boxed(AstNodeKind::Bytes(bytes), span)
}

pub fn new_boxed_zero(span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Zero);
node.span = Some(span);
node
AstNode::new_boxed(AstNodeKind::Zero, span)
}

pub fn new_boxed_undef(span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Undef);
node.span = Some(span);
node
AstNode::new_boxed(AstNodeKind::Undef, span)
}
}

Expand Down Expand Up @@ -138,9 +128,7 @@ pub struct Callee {

impl Callee {
pub fn new_boxed(name: String, args: Vec<AstNodeBox>, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Callee(Callee { name, args }));
node.span = Some(span);
node
AstNode::new_boxed(AstNodeKind::Callee(Callee { name, args }), span)
}
}

Expand All @@ -156,9 +144,7 @@ pub struct Operand {

impl Operand {
pub fn new_boxed(ty: Option<Type>, value: AstNodeBox, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Operand(Operand { ty, value }));
node.span = Some(span);
node
AstNode::new_boxed(AstNodeKind::Operand(Operand { ty, value }), span)
}
}
#[derive(Debug)]
Expand All @@ -171,8 +157,8 @@ pub struct TypeDef {
}

impl TypeDef {
pub fn new_boxed(name: String, ty: Type) -> AstNodeBox {
AstNode::new_boxed(AstNodeKind::TypeDef(TypeDef { name, ty }))
pub fn new_boxed(name: String, ty: Type, span: Span) -> AstNodeBox {
AstNode::new_boxed(AstNodeKind::TypeDef(TypeDef { name, ty }), span)
}
}

Expand All @@ -192,13 +178,22 @@ pub struct GlobalDef {
}

impl GlobalDef {
pub fn new_boxed(mutable: bool, name: String, ty: Type, init: AstNodeBox) -> AstNodeBox {
AstNode::new_boxed(AstNodeKind::GlobalDef(GlobalDef {
mutable,
name,
ty,
init,
}))
pub fn new_boxed(
mutable: bool,
name: String,
ty: Type,
init: AstNodeBox,
span: Span,
) -> AstNodeBox {
AstNode::new_boxed(
AstNodeKind::GlobalDef(GlobalDef {
mutable,
name,
ty,
init,
}),
span,
)
}
}

Expand All @@ -213,9 +208,7 @@ pub struct FunctionDecl {

impl FunctionDecl {
pub fn new_boxed(name: String, ty: Type, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::FunctionDecl(FunctionDecl { name, ty }));
node.span = Some(span);
node
AstNode::new_boxed(AstNodeKind::FunctionDecl(FunctionDecl { name, ty }), span)
}
}

Expand All @@ -233,10 +226,10 @@ pub struct FunctionDef {

impl FunctionDef {
pub fn new_boxed(name: String, ty: Type, blocks: Vec<AstNodeBox>, span: Span) -> AstNodeBox {
let mut node =
AstNode::new_boxed(AstNodeKind::FunctionDef(FunctionDef { name, ty, blocks }));
node.span = Some(span);
node
AstNode::new_boxed(
AstNodeKind::FunctionDef(FunctionDef { name, ty, blocks }),
span,
)
}
}

Expand All @@ -257,12 +250,16 @@ impl Block {
name: String,
params: Vec<(Type, String)>,
insts: Vec<AstNodeBox>,
span: Span,
) -> AstNodeBox {
AstNode::new_boxed(AstNodeKind::Block(Block {
name,
params,
insts,
}))
AstNode::new_boxed(
AstNodeKind::Block(Block {
name,
params,
insts,
}),
span,
)
}
}

Expand All @@ -288,36 +285,44 @@ impl Inst {
operands: Vec<AstNodeBox>,
span: Span,
) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Inst(Inst {
kind,
dest,
operands,
ty: None,
}));
node.span = Some(span);
node
AstNode::new_boxed(
AstNodeKind::Inst(Inst {
kind,
dest,
operands,
ty: None,
}),
span,
)
}

pub(super) fn new_boxed_alloc(dest: String, ty: Type, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Inst(Inst {
kind: InstKind::Alloc,
dest: Some(dest),
operands: Vec::new(),
ty: Some(ty),
}));
node.span = Some(span);
node
AstNode::new_boxed(
AstNodeKind::Inst(Inst {
kind: InstKind::Alloc,
dest: Some(dest),
operands: Vec::new(),
ty: Some(ty),
}),
span,
)
}

pub(super) fn new_boxed_load(dest: String, ty: Type, ptr: AstNodeBox, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Inst(Inst {
kind: InstKind::Load,
dest: Some(dest),
operands: vec![ptr],
ty: Some(ty),
}));
node.span = Some(span);
node
pub(super) fn new_boxed_load(
dest: String,
ty: Type,
ptr: AstNodeBox,
span: Span,
) -> AstNodeBox {
AstNode::new_boxed(
AstNodeKind::Inst(Inst {
kind: InstKind::Load,
dest: Some(dest),
operands: vec![ptr],
ty: Some(ty),
}),
span,
)
}

pub(super) fn new_boxed_cast(
Expand All @@ -327,25 +332,32 @@ impl Inst {
val: AstNodeBox,
span: Span,
) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Inst(Inst {
kind: InstKind::Cast(op),
dest: Some(dest),
operands: vec![val],
ty: Some(ty),
}));
node.span = Some(span);
node
AstNode::new_boxed(
AstNodeKind::Inst(Inst {
kind: InstKind::Cast(op),
dest: Some(dest),
operands: vec![val],
ty: Some(ty),
}),
span,
)
}

pub(super) fn new_boxed_call(dest: Option<String>, ty: Type, callee: AstNodeBox, span: Span) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Inst(Inst {
kind: InstKind::Call,
dest,
operands: vec![callee],
ty: Some(ty),
}));
node.span = Some(span);
node
pub(super) fn new_boxed_call(
dest: Option<String>,
ty: Type,
callee: AstNodeBox,
span: Span,
) -> AstNodeBox {
AstNode::new_boxed(
AstNodeKind::Inst(Inst {
kind: InstKind::Call,
dest,
operands: vec![callee],
ty: Some(ty),
}),
span,
)
}

pub(super) fn new_boxed_getelemptr(
Expand All @@ -354,14 +366,15 @@ impl Inst {
operands: Vec<AstNodeBox>,
span: Span,
) -> AstNodeBox {
let mut node = AstNode::new_boxed(AstNodeKind::Inst(Inst {
kind: InstKind::GetElemPtr,
dest: Some(dest),
operands,
ty: Some(ty),
}));
node.span = Some(span);
node
AstNode::new_boxed(
AstNodeKind::Inst(Inst {
kind: InstKind::GetElemPtr,
dest: Some(dest),
operands,
ty: Some(ty),
}),
span,
)
}
}

Expand All @@ -371,8 +384,8 @@ pub struct Array {
}

impl Array {
pub fn new_boxed(elems: Vec<AstNodeBox>) -> AstNodeBox {
AstNode::new_boxed(AstNodeKind::Array(Array { elems }))
pub fn new_boxed(elems: Vec<AstNodeBox>, span: Span) -> AstNodeBox {
AstNode::new_boxed(AstNodeKind::Array(Array { elems }), span)
}
}

Expand All @@ -382,7 +395,7 @@ pub struct Struct {
}

impl Struct {
pub fn new_boxed(fields: Vec<AstNodeBox>) -> AstNodeBox {
AstNode::new_boxed(AstNodeKind::Struct(Struct { fields }))
pub fn new_boxed(fields: Vec<AstNodeBox>, span: Span) -> AstNodeBox {
AstNode::new_boxed(AstNodeKind::Struct(Struct { fields }), span)
}
}
Loading

0 comments on commit 213a4b5

Please sign in to comment.