Skip to content

Commit

Permalink
Unused warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Apr 17, 2024
1 parent e4987f6 commit bec2891
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 86 deletions.
18 changes: 16 additions & 2 deletions crates/rue-compiler/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::HashMap;

use id_arena::{Arena, Id};
use rue_parser::SyntaxToken;

use crate::{hir::Hir, lir::Lir, scope::Scope, symbol::Symbol, ty::Type};

Expand All @@ -24,15 +27,22 @@ pub struct Database {
types: Arena<Type>,
hir: Arena<Hir>,
lir: Arena<Lir>,
symbol_tokens: HashMap<SymbolId, SyntaxToken>,
}

impl Database {
pub(crate) fn alloc_scope(&mut self, scope: Scope) -> ScopeId {
ScopeId(self.scopes.alloc(scope))
}

pub(crate) fn alloc_symbol(&mut self, symbol: Symbol) -> SymbolId {
SymbolId(self.symbols.alloc(symbol))
pub(crate) fn alloc_symbol(&mut self, symbol: Symbol, token: Option<SyntaxToken>) -> SymbolId {
let id = SymbolId(self.symbols.alloc(symbol));

if let Some(token) = token {
self.symbol_tokens.insert(id, token);
}

id
}

pub(crate) fn alloc_type(&mut self, ty: Type) -> TypeId {
Expand All @@ -55,6 +65,10 @@ impl Database {
&self.symbols[id.0]
}

pub fn symbol_token(&self, id: SymbolId) -> Option<SyntaxToken> {
self.symbol_tokens.get(&id).cloned()
}

pub fn ty_raw(&self, id: TypeId) -> &Type {
&self.types[id.0]
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rue-compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub enum WarningKind {

#[error("redundant check against same type `{0}`")]
RedundantTypeGuard(String),

#[error("unused symbol `{0}`")]
UnusedSymbol(String),
}

#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
Expand Down
49 changes: 17 additions & 32 deletions crates/rue-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,55 +36,40 @@ impl Output {
}

pub fn analyze(root: Root) -> Vec<Diagnostic> {
let mut db = Database::default();
let scope_id = db.alloc_scope(Scope::default());
let mut lowerer = Lowerer::new(&mut db);
let mut database = Database::default();
precompile(&mut database, root).0
}

fn precompile(database: &mut Database, root: Root) -> (Vec<Diagnostic>, Option<LirId>) {
let scope_id = database.alloc_scope(Scope::default());

let mut lowerer = Lowerer::new(database);
lowerer.compile_root(root, scope_id);
let mut diagnostics = lowerer.finish();

let Some(main_id) = db.scope_mut(scope_id).symbol("main") else {
let Some(main_id) = database.scope_mut(scope_id).symbol("main") else {
diagnostics.push(Diagnostic::new(
DiagnosticKind::Error(ErrorKind::MissingMain),
0..0,
));

return diagnostics;
return (diagnostics, None);
};

let mut optimizer = Optimizer::new(&mut db);
optimizer.opt_main(main_id);
let mut optimizer = Optimizer::new(database);
let lir_id = optimizer.opt_main(scope_id, main_id);
diagnostics.extend(optimizer.finish());

diagnostics
(diagnostics, Some(lir_id))
}

pub fn compile(allocator: &mut Allocator, root: Root, parsing_succeeded: bool) -> Output {
let mut db = Database::default();
let scope_id = db.alloc_scope(Scope::default());

let mut lowerer = Lowerer::new(&mut db);
lowerer.compile_root(root, scope_id);
let mut diagnostics = lowerer.finish();

let Some(main_id) = db.scope_mut(scope_id).symbol("main") else {
diagnostics.push(Diagnostic::new(
DiagnosticKind::Error(ErrorKind::MissingMain),
0..0,
));

return Output {
diagnostics,
node_ptr: NodePtr::NIL,
};
};

let mut optimizer = Optimizer::new(&mut db);
let lir_id = optimizer.opt_main(main_id);
diagnostics.extend(optimizer.finish());
let mut database = Database::default();
let (diagnostics, lir_id) = precompile(&mut database, root);

let node_ptr = if !diagnostics.iter().any(Diagnostic::is_error) && parsing_succeeded {
let mut codegen = Codegen::new(&mut db, allocator);
codegen.gen_lir(lir_id)
let mut codegen = Codegen::new(&mut database, allocator);
codegen.gen_lir(lir_id.unwrap())
} else {
NodePtr::NIL
};
Expand Down
91 changes: 58 additions & 33 deletions crates/rue-compiler/src/lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,54 @@ impl<'a> Lowerer<'a> {
// Define the `sha256` function, since it's not an operator or keyword.
{
let mut scope = Scope::default();
let param = db.alloc_symbol(Symbol::Parameter {
type_id: bytes_type,
});
let param = db.alloc_symbol(
Symbol::Parameter {
type_id: bytes_type,
},
None,
);
scope.define_symbol("bytes".to_string(), param);
let param_ref = db.alloc_hir(Hir::Reference(param));
let hir_id = db.alloc_hir(Hir::Sha256(param_ref));
let scope_id = db.alloc_scope(scope);

builtins.define_symbol(
"sha256".to_string(),
db.alloc_symbol(Symbol::Function {
scope_id,
hir_id,
ty: FunctionType::new(vec![bytes_type], bytes32_type, false),
}),
db.alloc_symbol(
Symbol::Function {
scope_id,
hir_id,
ty: FunctionType::new(vec![bytes_type], bytes32_type, false),
},
None,
),
);
}

// Define the `pubkey_for_exp` function, since it's not an operator or keyword.
{
let mut scope = Scope::default();
let param = db.alloc_symbol(Symbol::Parameter {
type_id: bytes32_type,
});
let param = db.alloc_symbol(
Symbol::Parameter {
type_id: bytes32_type,
},
None,
);
scope.define_symbol("exponent".to_string(), param);
let param_ref = db.alloc_hir(Hir::Reference(param));
let hir_id = db.alloc_hir(Hir::PubkeyForExp(param_ref));
let scope_id = db.alloc_scope(scope);

builtins.define_symbol(
"pubkey_for_exp".to_string(),
db.alloc_symbol(Symbol::Function {
scope_id,
hir_id,
ty: FunctionType::new(vec![bytes32_type], pk_type, false),
}),
db.alloc_symbol(
Symbol::Function {
scope_id,
hir_id,
ty: FunctionType::new(vec![bytes32_type], pk_type, false),
},
None,
),
);
}

Expand Down Expand Up @@ -216,7 +228,9 @@ impl<'a> Lowerer<'a> {

parameter_types.push(type_id);

let symbol_id = self.db.alloc_symbol(Symbol::Parameter { type_id });
let symbol_id = self
.db
.alloc_symbol(Symbol::Parameter { type_id }, param.name());

if let Some(name) = param.name() {
scope.define_symbol(name.to_string(), symbol_id);
Expand All @@ -236,11 +250,14 @@ impl<'a> Lowerer<'a> {

let ty = FunctionType::new(parameter_types, return_type, varargs);

let symbol_id = self.db.alloc_symbol(Symbol::Function {
scope_id,
hir_id,
ty,
});
let symbol_id = self.db.alloc_symbol(
Symbol::Function {
scope_id,
hir_id,
ty,
},
function_item.name(),
);

if let Some(name) = function_item.name() {
self.scope_mut().define_symbol(name.to_string(), symbol_id);
Expand All @@ -260,7 +277,7 @@ impl<'a> Lowerer<'a> {

let symbol_id = self
.db
.alloc_symbol(Symbol::ConstBinding { type_id, hir_id });
.alloc_symbol(Symbol::ConstBinding { type_id, hir_id }, const_item.name());

if let Some(name) = const_item.name() {
self.scope_mut().define_symbol(name.to_string(), symbol_id);
Expand Down Expand Up @@ -470,10 +487,13 @@ impl<'a> Lowerer<'a> {
return None;
};

let symbol_id = self.db.alloc_symbol(Symbol::LetBinding {
type_id: expected_type.unwrap_or(value.ty()),
hir_id: value.hir(),
});
let symbol_id = self.db.alloc_symbol(
Symbol::LetBinding {
type_id: expected_type.unwrap_or(value.ty()),
hir_id: value.hir(),
},
Some(name.clone()),
);

// Every let binding is a new scope for now, to simplify the code generation process later.
// This is not the most efficient way to do it, but it's the easiest with the current codebase.
Expand Down Expand Up @@ -1414,7 +1434,9 @@ impl<'a> Lowerer<'a> {
parameter_types.push(type_id);

if let Some(name) = param.name() {
let symbol_id = self.db.alloc_symbol(Symbol::Parameter { type_id });
let symbol_id = self
.db
.alloc_symbol(Symbol::Parameter { type_id }, Some(name.clone()));
scope.define_symbol(name.to_string(), symbol_id);
};

Expand Down Expand Up @@ -1452,11 +1474,14 @@ impl<'a> Lowerer<'a> {

let ty = FunctionType::new(parameter_types.clone(), return_type, varargs);

let symbol_id = self.db.alloc_symbol(Symbol::Function {
scope_id,
hir_id: body.hir(),
ty: ty.clone(),
});
let symbol_id = self.db.alloc_symbol(
Symbol::Function {
scope_id,
hir_id: body.hir(),
ty: ty.clone(),
},
None,
);

Value::typed(
self.db.alloc_hir(Hir::Reference(symbol_id)),
Expand Down
Loading

0 comments on commit bec2891

Please sign in to comment.