From 6cc1822ec6f51ec775744839ec6e53905e0c474c Mon Sep 17 00:00:00 2001 From: Janis Date: Thu, 6 Mar 2025 02:18:46 +0100 Subject: [PATCH] trying to fix declrefs to params being places --- src/ast2/mod.rs | 104 +++++++++++++++++++---------------- src/symbol_table.rs | 129 ++++++++++++++++++++------------------------ 2 files changed, 115 insertions(+), 118 deletions(-) diff --git a/src/ast2/mod.rs b/src/ast2/mod.rs index b915613..b086b39 100644 --- a/src/ast2/mod.rs +++ b/src/ast2/mod.rs @@ -5,12 +5,12 @@ use std::{ fmt::{Debug, Display}, }; -use intern::{InternPool, PointerFlags, StructFlags, AMD64_POINTER_TYPE_INFO}; +use intern::{InternPoolWrapper as InternPool, PointerFlags, StructFlags, AMD64_POINTER_TYPE_INFO}; use num_bigint::BigInt; use crate::{ - ast::FloatingType, comptime::ComptimeNumber, lexer::SourceLocation, tokens::Token, - writeln_indented, + ast::FloatingType, comptime::ComptimeNumber, lexer::SourceLocation, + symbol_table::syms2::DeclKind, tokens::Token, writeln_indented, }; pub mod intern; @@ -55,7 +55,7 @@ pub enum Tag { StructDecl, /// `data` is an index to a type, and an intern to a name FieldDecl, - /// `data` is an index to a VarDecl, GlobalDecl or FunctionDecl + /// `data` is an index to a VarDecl, GlobalDecl or FunctionDecl, and an opaque DeclKind DeclRef, /// `data` is an inlined key into the symbol table (scope: index, name: intern) DeclRefUnresolved, @@ -1024,7 +1024,7 @@ impl Ast { fn push_place_to_value_conversion(&mut self, index: Index) -> Index { let loc = self.get_loc(index); let i = self.reserve_node_other(); - self.set_tag_data_source_loc(index, Tag::PlaceToValueConversion, Data::index(index), loc); + self.set_tag_data_source_loc(i, Tag::PlaceToValueConversion, Data::index(index), loc); i } @@ -1033,7 +1033,7 @@ impl Ast { fn push_value_to_place_conversion(&mut self, index: Index) -> Index { let loc = self.get_loc(index); let i = self.reserve_node_other(); - self.set_tag_data_source_loc(index, Tag::ValueToPlaceConversion, Data::index(index), loc); + self.set_tag_data_source_loc(i, Tag::ValueToPlaceConversion, Data::index(index), loc); i } @@ -1109,9 +1109,9 @@ impl Ast { i } - fn resolve_decl_ref(&mut self, i: Index, decl: Index) { + fn resolve_decl_ref(&mut self, i: Index, decl: Index, decl_kind: DeclKind) { self.tags[i] = Tag::DeclRef; - self.datas[i] = Data::index(decl); + self.datas[i] = Data::index_and_opaque(decl, decl_kind as u32); } fn push_struct_decl>( @@ -1333,11 +1333,10 @@ impl Ast { let pointee = self.get_type_of_node(ip, cache, Index::from_u32(self.extra[b - 1]).unwrap()); - ip.try_get_pointer_type( + ip.as_mut().get_pointer_type( pointee, Some(PointerFlags::new(tag == Tag::VarDecl, false, false)), ) - .unwrap() } // these all evaluate to pointers Tag::GlobalDecl => { @@ -1345,8 +1344,8 @@ impl Ast { let pointee = self.get_type_of_node(ip, cache, Index::from_u32(self.extra[a]).unwrap()); - ip.try_get_pointer_type(pointee, Some(PointerFlags::new(true, false, true))) - .unwrap() + ip.as_mut() + .get_pointer_type(pointee, Some(PointerFlags::new(true, false, true))) } Tag::FunctionDecl => self.get_type_of_node(ip, cache, data.as_two_indices().0), Tag::FunctionProto => { @@ -1368,7 +1367,7 @@ impl Ast { }) }; - ip.try_get_function_type(return_type, parameters).unwrap() + ip.as_mut().get_function_type(return_type, parameters) } Tag::BlockTrailingExpr => { let (_a, b) = data.as_extra_range(); @@ -1422,7 +1421,7 @@ impl Ast { } }; - ip.try_get_pointer_type(pointee, None).unwrap() + ip.as_mut().get_pointer_type(pointee, None) } // this evaluates to a pointer Tag::SubscriptExpr => { @@ -1461,11 +1460,13 @@ impl Ast { } Tag::ValueToPlaceConversion => { let ty = self.get_type_of_node(ip, cache, data.as_index()); - ip.try_get_pointer_type(ty, None).unwrap() + ip.as_mut().get_pointer_type(ty, None) } Tag::PlaceToValueConversion => { let ty = self.get_type_of_node(ip, cache, data.as_index()); - ip.try_get_pointee_type(ty).unwrap() + ip.try_get_pointee_type(ty) + .unwrap_or(ip.as_mut().get_pointer_type(ty, None)) + //.expect(&format!("{} is not a pointer type", ip.display_key(ty))) } Tag::Not | Tag::Negate => self.get_type_of_node(ip, cache, data.as_index()), Tag::Or @@ -1498,7 +1499,7 @@ impl Ast { Tag::DeclRef => self.get_type_of_node(ip, cache, data.as_index()), Tag::StructDecl => { let (name, _) = data.as_intern_and_extra_offset(); - ip.try_get_struct_type(name, index).unwrap() + ip.as_mut().get_struct_type(name, index) } Tag::Assign | Tag::Root @@ -1508,6 +1509,8 @@ impl Ast { | Tag::ReturnStmt => void, Tag::FieldDecl => self.get_type_of_node(ip, cache, data.as_index_intern().0), Tag::InternedType => data.as_intern(), + // Tag::PointerType => {} + // Tag::ArrayType => {} Tag::TypeDeclRef | Tag::TypeDeclRefUnresolved | Tag::PointerType | Tag::ArrayType => { ip.get_void_type() } @@ -1969,7 +1972,7 @@ impl Ast { } pub fn comptime_number_to_interned_type_and_value( - ip: &mut InternPool, + ip: &mut intern::InternPool, pointer_bits: u16, comptime: ComptimeNumber, ) -> (intern::Index, intern::Index) { @@ -2044,15 +2047,20 @@ pub fn comptime_number_to_interned_type_and_value( (value, ty) } -pub fn interned_type_and_value_to_comptime_number( - ip: &InternPool, +pub fn interned_type_and_value_to_comptime_number( + ip: IP, pointer_bits: u16, ty: intern::Index, val: intern::Index, -) -> crate::comptime::ComptimeNumber { +) -> crate::comptime::ComptimeNumber +where + IP: AsRef, +{ use crate::ast::IntegralType; use crate::comptime::*; + let ip = ip.as_ref(); + let ty_key = ip.get_key(ty); let signed = ip.is_type_signed(ty, AMD64_POINTER_TYPE_INFO); match ty_key { @@ -2522,7 +2530,7 @@ pub mod ast_gen { #[derive(Debug)] pub struct Parser { pub ast: Ast, - pub intern: intern::InternPool, + pub intern: InternPool, pub syms: crate::symbol_table::syms2::Symbols, scopes: Vec, pub errors: Vec, @@ -2540,7 +2548,7 @@ pub mod ast_gen { pub fn new() -> Parser { Self { ast: Ast::new(), - intern: intern::InternPool::new(), + intern: InternPool::new(), syms: crate::symbol_table::syms2::Symbols::new(), scopes: Vec::new(), errors: Vec::new(), @@ -2778,6 +2786,15 @@ pub mod ast_gen { self.intern .get_function_type(return_type, parameters); } + // Tag::VarDeclAssignment|Tag::MutVarDeclAssignment| + // Tag::VarDecl | Tag::MutVarDecl => { + // let (a,b) = + // data.as_extra_range(); + // let extra = &self.ast.extra[a..b]; + // let typed = Index::from_u32( + // extra.last().unwrap()).unwrap(); + // self.ast.get_type_of_node(&self.intern, cache, index) + // } Tag::StructDecl => { let (name, offset) = data.as_intern_and_extra_offset(); @@ -2823,7 +2840,7 @@ pub mod ast_gen { Tag::TypeDeclRefUnresolved => { let (scope, name) = ast.datas[node].as_index_intern(); // look in my_scope - if let Some(decl) = + if let Some((_, decl)) = self.syms .find_type_symbol(scope, name, ast.source_locs[node]) { @@ -2834,10 +2851,14 @@ pub mod ast_gen { let (scope, name) = ast.datas[node].as_index_intern(); // look in my_scope - if let Some(decl) = + if let Some((key, decl)) = self.syms.find_symbol(scope, name, ast.source_locs[node]) { - ast.resolve_decl_ref(node, decl) + ast.resolve_decl_ref( + node, + decl, + Option::::from(key.kind().unwrap()).unwrap(), + ) }; } _ => {} @@ -3311,22 +3332,7 @@ pub mod ast_gen { /// (let | var) IDENTIFIER (: TYPENAME)? ; /// (let | var) IDENTIFIER (: TYPENAME)? = EXPRESSION ; fn parse_var_decl(&mut self, tokens: &mut TokenIterator) -> ParseResult { - match self.parse_var_decl_inner(tokens) { - Ok(i) => { - _ = tokens.eat_token(Token::Semi).ok_or(ErrorInfo { - error: ParseError::ExpectedToken(Token::Semi), - loc: tokens.current_source_location(), - })?; - Ok(i) - } - Err(err) => { - tokens.advance_past_semi().ok_or(ErrorInfo { - error: ParseError::ExpectedToken(Token::Semi), - loc: tokens.current_source_location(), - })?; - Ok(self.push_error(err.error, err.loc)) - } - } + self.parse_with_trailing_semi(tokens, Self::parse_var_decl_inner) } fn parse_var_decl_inner(&mut self, tokens: &mut TokenIterator) -> ParseResult { @@ -3479,8 +3485,12 @@ pub mod ast_gen { let ty = self.parse_type(tokens)?; let param = self.ast.push_parameter(name, ty, loc); - self.syms - .insert_symbol(self.current_scope(), name, SymbolKind::Local(loc), param); + self.syms.insert_symbol( + self.current_scope(), + name, + SymbolKind::Parameter(loc), + param, + ); return Ok(param); } @@ -4838,7 +4848,9 @@ pub mod ir { Unimplemented, } - use super::{intern::InternPool, visitor::AstExt, AstVisitorTrait, TypeCache}; + use super::{ + intern::InternPoolWrapper as InternPool, visitor::AstExt, AstVisitorTrait, TypeCache, + }; struct IrBuilder { functions: Vec, @@ -5676,7 +5688,7 @@ pub mod irgen { ast: Ast, syms: Symbols, ir: IR, - ip: intern::InternPool, + ip: InternPool, } impl IRGen { diff --git a/src/symbol_table.rs b/src/symbol_table.rs index 7ca8de5..4412496 100644 --- a/src/symbol_table.rs +++ b/src/symbol_table.rs @@ -35,25 +35,13 @@ impl SymbolPath { for node in self.0.iter().skip(1).rev() { match tree.nodes.get_node(node.unwrap()) { Tag::VarDecl { name, .. } => { - _ = write!( - &mut buf, - "V{}::", - tree.get_ident_str(*name).unwrap() - ); + _ = write!(&mut buf, "V{}::", tree.get_ident_str(*name).unwrap()); } Tag::GlobalDecl { name, .. } => { - _ = write!( - &mut buf, - "G{}::", - tree.get_ident_str(*name).unwrap() - ); + _ = write!(&mut buf, "G{}::", tree.get_ident_str(*name).unwrap()); } Tag::FunctionProto { name, .. } => { - _ = write!( - &mut buf, - "F{}::", - tree.get_ident_str(*name).unwrap() - ); + _ = write!(&mut buf, "F{}::", tree.get_ident_str(*name).unwrap()); } _ => {} } @@ -142,12 +130,7 @@ impl Drop for InnerSymbolTable { } impl InnerSymbolTable { - fn insert_symbol( - &mut self, - name: &str, - node: AstNode, - kind: SymbolKind, - ) -> &SymbolRecord { + fn insert_symbol(&mut self, name: &str, node: AstNode, kind: SymbolKind) -> &SymbolRecord { match kind { SymbolKind::Var => { self.ordered_identifiers.push(SymbolRecord { @@ -160,11 +143,7 @@ impl InnerSymbolTable { } } - fn insert_orderless_symbol( - &mut self, - name: &str, - node: AstNode, - ) -> &SymbolRecord { + fn insert_orderless_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord { self.orderless_identifiers.insert( name.to_owned(), SymbolRecord { @@ -175,11 +154,7 @@ impl InnerSymbolTable { self.orderless_identifiers.get(name).unwrap() } - fn find_symbol_or_insert_with<'a, F>( - &'a mut self, - name: &str, - cb: F, - ) -> &'a SymbolRecord + fn find_symbol_or_insert_with<'a, F>(&'a mut self, name: &str, cb: F) -> &'a SymbolRecord where F: FnOnce() -> (AstNode, SymbolKind), { @@ -202,9 +177,7 @@ impl InnerSymbolTable { .find(|(_, v)| v.decl == decl) .map(|(_, v)| v) }) - .or_else(|| { - self.parent_ref().and_then(|p| p.find_symbol_by_decl(decl)) - }) + .or_else(|| self.parent_ref().and_then(|p| p.find_symbol_by_decl(decl))) } fn find_any_symbol(&self, name: &str) -> Option<&SymbolRecord> { @@ -219,9 +192,7 @@ impl InnerSymbolTable { self.ordered_identifiers .iter() .find(|r| r.name.as_str() == name) - .or_else(|| { - self.parent_ref().and_then(|p| p.find_ordered_symbol(name)) - }) + .or_else(|| self.parent_ref().and_then(|p| p.find_ordered_symbol(name))) } fn find_orderless_symbol(&self, name: &str) -> Option<&SymbolRecord> { @@ -315,12 +286,7 @@ impl SymbolTableWrapper { } impl SymbolTableWrapper { - pub fn insert_symbol( - &mut self, - name: &str, - node: AstNode, - kind: SymbolKind, - ) -> &SymbolRecord { + pub fn insert_symbol(&mut self, name: &str, node: AstNode, kind: SymbolKind) -> &SymbolRecord { self.current_mut().insert_symbol(name, node, kind) } @@ -328,27 +294,15 @@ impl SymbolTableWrapper { self.root_mut().find_orderless_symbol(name) } - pub fn insert_root_symbol( - &mut self, - name: &str, - node: AstNode, - ) -> &SymbolRecord { + pub fn insert_root_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord { self.root_mut().insert_orderless_symbol(name, node) } - pub fn insert_orderless_symbol( - &mut self, - name: &str, - node: AstNode, - ) -> &SymbolRecord { + pub fn insert_orderless_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord { self.current_mut().insert_orderless_symbol(name, node) } - pub fn find_symbol_or_insert_with<'a, F>( - &'a mut self, - name: &str, - cb: F, - ) -> &'a SymbolRecord + pub fn find_symbol_or_insert_with<'a, F>(&'a mut self, name: &str, cb: F) -> &'a SymbolRecord where F: FnOnce() -> (AstNode, SymbolKind), { @@ -470,6 +424,40 @@ pub mod syms2 { }, } + impl Key { + pub fn kind(&self) -> Option { + match self { + Key::Symbol { kind, .. } => Some(*kind), + _ => None, + } + } + } + + #[repr(u32)] + pub enum DeclKind { + Local = 1, + Parameter, + } + + impl DeclKind { + pub fn from_u32(v: u32) -> Option { + match v { + 1 => Some(Self::Local), + 2 => Some(Self::Parameter), + _ => None, + } + } + } + impl From for Option { + fn from(value: SymbolKind) -> Self { + match value { + SymbolKind::Parameter(_) => Some(DeclKind::Parameter), + SymbolKind::Local(_) => Some(DeclKind::Local), + _ => None, + } + } + } + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum SymbolKind { __First, @@ -479,6 +467,7 @@ pub mod syms2 { __TypeScope, Scope, ParentScope, + Parameter(SourceLocation), Local(SourceLocation), __Last, } @@ -526,9 +515,7 @@ pub mod syms2 { } let entries = self.inner.iter().map(|(key, val)| { let payload = match key { - Key::ScopeByIndex { .. } => { - ExpandedPayload::Intern(val.as_intern()) - } + Key::ScopeByIndex { .. } => ExpandedPayload::Intern(val.as_intern()), _ => ExpandedPayload::Ast(val.as_ast()), }; @@ -567,7 +554,7 @@ pub mod syms2 { scope: AstIndex, name: InternIndex, loc: SourceLocation, - ) -> Option { + ) -> Option<(Key, AstIndex)> { use SymbolKind::*; let range = self.inner.range( Key::Symbol { @@ -581,8 +568,8 @@ pub mod syms2 { }, ); - if let Some((_, payload)) = range.rev().next() { - Some(payload.as_ast()) + if let Some((key, payload)) = range.rev().next() { + Some((*key, payload.as_ast())) } else { if let Some(parent) = self.inner.get(&Key::Symbol { scope, @@ -601,7 +588,7 @@ pub mod syms2 { scope: AstIndex, name: InternIndex, loc: SourceLocation, - ) -> Option { + ) -> Option<(Key, AstIndex)> { use SymbolKind::*; let range = self.inner.range( Key::Symbol { @@ -615,15 +602,15 @@ pub mod syms2 { }, ); - if let Some((_, payload)) = range.rev().next() { - Some(payload.as_ast()) + if let Some((key, payload)) = range.rev().next() { + Some((*key, payload.as_ast())) } else { if let Some(parent) = self.inner.get(&Key::Symbol { scope, name: InternIndex::invalid(), kind: ParentScope, }) { - self.find_symbol(parent.as_ast(), name, loc) + self.find_type_symbol(parent.as_ast(), name, loc) } else { None } @@ -637,10 +624,8 @@ pub mod syms2 { kind: SymbolKind, ast: AstIndex, ) { - self.inner.insert( - Key::Symbol { scope, name, kind }, - Payload::new_ast(ast), - ); + self.inner + .insert(Key::Symbol { scope, name, kind }, Payload::new_ast(ast)); } } }