diff --git a/Cargo.lock b/Cargo.lock index 09ecee4..a3bb61f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.18" @@ -22,6 +33,7 @@ name = "analysis" version = "0.1.0" dependencies = [ "bumpalo", + "lasso", "parser", "rustc-hash", ] @@ -148,6 +160,26 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash", +] + [[package]] name = "hashbrown" version = "0.12.1" @@ -161,7 +193,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.1", ] [[package]] @@ -184,6 +216,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" +[[package]] +name = "lasso" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aeb7b21a526375c5ca55f1a6dfd4e1fad9fa4edd750f530252a718a44b2608f0" +dependencies = [ + "hashbrown 0.11.2", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -534,6 +575,12 @@ dependencies = [ "parser", ] +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "walkdir" version = "2.3.2" @@ -545,6 +592,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" diff --git a/analysis/Cargo.toml b/analysis/Cargo.toml index bfa6ca9..5efaec8 100644 --- a/analysis/Cargo.toml +++ b/analysis/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" [dependencies] bumpalo = "3.10.0" +lasso = "0.6.0" parser = { path = "../parser" } rustc-hash = "1.1.0" diff --git a/analysis/src/hir.rs b/analysis/src/hir.rs index 1b9cf7b..e11f235 100644 --- a/analysis/src/hir.rs +++ b/analysis/src/hir.rs @@ -1,3 +1,10 @@ +use lasso::Spur; +use parser::Spanned; + +pub type Symbol = Spur; + +pub type Ident = Spanned; + pub struct Hir<'hir> { x: &'hir (), } diff --git a/analysis/src/lower.rs b/analysis/src/lower.rs index ef562c0..4e9f47c 100644 --- a/analysis/src/lower.rs +++ b/analysis/src/lower.rs @@ -1,31 +1,46 @@ -use parser::{ - ast::{self, Ident}, - Span, -}; +use parser::{ast, Span}; use rustc_hash::FxHashMap; -use crate::hir; +use crate::hir::{self, Symbol}; pub struct LowerCtx<'hir> { + hir_symbol_intern: lasso::Rodeo, hir_arena: &'hir bumpalo::Bump, - global_symbols: FxHashMap, + global_symbols: FxHashMap, } impl<'hir> LowerCtx<'hir> { pub fn lower_translation_unit(&mut self, unit: &ast::TranslationUnit) -> hir::Hir<'hir> { - for decl in unit {} + for _decl in unit {} todo!() } - fn lower_decl(&mut self, decl: &ast::ExternalDecl, span: Span) -> hir::ExternalDecl { + fn lower_decl(&mut self, decl: &ast::ExternalDecl, _span: Span) -> hir::ExternalDecl { match decl { ast::ExternalDecl::Decl(_) => todo!(), - ast::ExternalDecl::FunctionDef(func) => todo!(), + ast::ExternalDecl::FunctionDef(_func) => todo!(), } } - fn lower_function_def(&mut self, def: &ast::FunctionDef, span: Span) -> hir::FunctionDef { + fn lower_function_def(&mut self, def: &ast::FunctionDef, _span: Span) -> hir::FunctionDef { + let decl = def.decl.uwnrap_normal(); + let (init_declarator, _declarator_span) = decl + .init_declarators + .get(0) + .expect("single init declarator in function definition"); + let declarator = &init_declarator.declarator; + + let ((name_ident, _name_span), _param_decls) = declarator.decl.unwrap_with_params(); + + let name_sym = self.hir_symbol_intern.get_or_intern(name_ident); + + + if self.global_symbols.contains_key(&name_sym) { + panic!("function declarated twice! return this error properly! lol!") + } + + todo!() } } diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 73e9e42..f8e07f6 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -262,3 +262,25 @@ pub enum ExternalDecl { } pub type TranslationUnit = Vec>; + +impl Decl { + pub fn uwnrap_normal(&self) -> &NormalDecl { + match self { + Decl::Normal(decl) => decl, + Decl::StaticAssert => { + panic!("Expected normal declaration, found static assert declaration") + } + } + } +} + +impl DirectDeclarator { + pub fn unwrap_with_params(&self) -> (&Ident, &Vec) { + match self { + DirectDeclarator::Ident(_) => { + panic!("Expected declarator with parameters, found single identifier declarator1") + } + DirectDeclarator::WithParams { ident, params } => (ident, params), + } + } +} diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 9e93108..1429b19 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -5,7 +5,7 @@ use crate::{ ast::{ Decl, DeclAttr, DeclSpec, Declarator, DirectDeclarator, ExternalDecl, FunctionDef, FunctionParamDecl, Ident, InitDecl, IntTy, IntTyKind, IntTySignedness, NormalDecl, Stmt, - TypeSpecifier, TranslationUnit, + TranslationUnit, TypeSpecifier, }, pre::Punctuator as P, token::{Keyword as Kw, Token as Tok}, @@ -307,7 +307,7 @@ where kind: IntTyKind::Int, }), Tok::Kw(Kw::Long) => { - if let Some(_) = eat!(self, Tok::Kw(Kw::Long)) { + if eat!(self, Tok::Kw(Kw::Long)).is_some() { eat!(self, Tok::Kw(Kw::Int)); TypeSpecifier::Integer(IntTy { sign: signedness.unwrap_or_default(),