This commit is contained in:
nora 2022-07-24 18:38:19 +02:00
parent 475a520de3
commit 683f11c997
6 changed files with 111 additions and 13 deletions

55
Cargo.lock generated
View file

@ -8,6 +8,17 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 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]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.18" version = "0.7.18"
@ -22,6 +33,7 @@ name = "analysis"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"lasso",
"parser", "parser",
"rustc-hash", "rustc-hash",
] ]
@ -148,6 +160,26 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 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]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.1" version = "0.12.1"
@ -161,7 +193,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"hashbrown", "hashbrown 0.12.1",
] ]
[[package]] [[package]]
@ -184,6 +216,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" 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]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -534,6 +575,12 @@ dependencies = [
"parser", "parser",
] ]
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.3.2" version = "2.3.2"
@ -545,6 +592,12 @@ dependencies = [
"winapi-util", "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]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View file

@ -7,5 +7,6 @@ edition = "2021"
[dependencies] [dependencies]
bumpalo = "3.10.0" bumpalo = "3.10.0"
lasso = "0.6.0"
parser = { path = "../parser" } parser = { path = "../parser" }
rustc-hash = "1.1.0" rustc-hash = "1.1.0"

View file

@ -1,3 +1,10 @@
use lasso::Spur;
use parser::Spanned;
pub type Symbol = Spur;
pub type Ident = Spanned<Symbol>;
pub struct Hir<'hir> { pub struct Hir<'hir> {
x: &'hir (), x: &'hir (),
} }

View file

@ -1,31 +1,46 @@
use parser::{ use parser::{ast, Span};
ast::{self, Ident},
Span,
};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use crate::hir; use crate::hir::{self, Symbol};
pub struct LowerCtx<'hir> { pub struct LowerCtx<'hir> {
hir_symbol_intern: lasso::Rodeo,
hir_arena: &'hir bumpalo::Bump, hir_arena: &'hir bumpalo::Bump,
global_symbols: FxHashMap<Ident, &'hir hir::ExternalDecl>, global_symbols: FxHashMap<Symbol, &'hir hir::ExternalDecl>,
} }
impl<'hir> LowerCtx<'hir> { impl<'hir> LowerCtx<'hir> {
pub fn lower_translation_unit(&mut self, unit: &ast::TranslationUnit) -> hir::Hir<'hir> { pub fn lower_translation_unit(&mut self, unit: &ast::TranslationUnit) -> hir::Hir<'hir> {
for decl in unit {} for _decl in unit {}
todo!() 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 { match decl {
ast::ExternalDecl::Decl(_) => todo!(), 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!() todo!()
} }
} }

View file

@ -262,3 +262,25 @@ pub enum ExternalDecl {
} }
pub type TranslationUnit = Vec<Spanned<ExternalDecl>>; pub type TranslationUnit = Vec<Spanned<ExternalDecl>>;
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<FunctionParamDecl>) {
match self {
DirectDeclarator::Ident(_) => {
panic!("Expected declarator with parameters, found single identifier declarator1")
}
DirectDeclarator::WithParams { ident, params } => (ident, params),
}
}
}

View file

@ -5,7 +5,7 @@ use crate::{
ast::{ ast::{
Decl, DeclAttr, DeclSpec, Declarator, DirectDeclarator, ExternalDecl, FunctionDef, Decl, DeclAttr, DeclSpec, Declarator, DirectDeclarator, ExternalDecl, FunctionDef,
FunctionParamDecl, Ident, InitDecl, IntTy, IntTyKind, IntTySignedness, NormalDecl, Stmt, FunctionParamDecl, Ident, InitDecl, IntTy, IntTyKind, IntTySignedness, NormalDecl, Stmt,
TypeSpecifier, TranslationUnit, TranslationUnit, TypeSpecifier,
}, },
pre::Punctuator as P, pre::Punctuator as P,
token::{Keyword as Kw, Token as Tok}, token::{Keyword as Kw, Token as Tok},
@ -307,7 +307,7 @@ where
kind: IntTyKind::Int, kind: IntTyKind::Int,
}), }),
Tok::Kw(Kw::Long) => { 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)); eat!(self, Tok::Kw(Kw::Int));
TypeSpecifier::Integer(IntTy { TypeSpecifier::Integer(IntTy {
sign: signedness.unwrap_or_default(), sign: signedness.unwrap_or_default(),