trying to fix declrefs to params being places

This commit is contained in:
Janis 2025-03-06 02:18:46 +01:00
parent 8c95a2ba3d
commit 6cc1822ec6
2 changed files with 115 additions and 118 deletions

View file

@ -5,12 +5,12 @@ use std::{
fmt::{Debug, Display}, 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 num_bigint::BigInt;
use crate::{ use crate::{
ast::FloatingType, comptime::ComptimeNumber, lexer::SourceLocation, tokens::Token, ast::FloatingType, comptime::ComptimeNumber, lexer::SourceLocation,
writeln_indented, symbol_table::syms2::DeclKind, tokens::Token, writeln_indented,
}; };
pub mod intern; pub mod intern;
@ -55,7 +55,7 @@ pub enum Tag {
StructDecl, StructDecl,
/// `data` is an index to a type, and an intern to a name /// `data` is an index to a type, and an intern to a name
FieldDecl, 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, DeclRef,
/// `data` is an inlined key into the symbol table (scope: index, name: intern) /// `data` is an inlined key into the symbol table (scope: index, name: intern)
DeclRefUnresolved, DeclRefUnresolved,
@ -1024,7 +1024,7 @@ impl Ast {
fn push_place_to_value_conversion(&mut self, index: Index) -> Index { fn push_place_to_value_conversion(&mut self, index: Index) -> Index {
let loc = self.get_loc(index); let loc = self.get_loc(index);
let i = self.reserve_node_other(); 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 i
} }
@ -1033,7 +1033,7 @@ impl Ast {
fn push_value_to_place_conversion(&mut self, index: Index) -> Index { fn push_value_to_place_conversion(&mut self, index: Index) -> Index {
let loc = self.get_loc(index); let loc = self.get_loc(index);
let i = self.reserve_node_other(); 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 i
} }
@ -1109,9 +1109,9 @@ impl Ast {
i 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.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<I: IntoIterator<Item = (intern::Index, Index)>>( fn push_struct_decl<I: IntoIterator<Item = (intern::Index, Index)>>(
@ -1333,11 +1333,10 @@ impl Ast {
let pointee = let pointee =
self.get_type_of_node(ip, cache, Index::from_u32(self.extra[b - 1]).unwrap()); 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, pointee,
Some(PointerFlags::new(tag == Tag::VarDecl, false, false)), Some(PointerFlags::new(tag == Tag::VarDecl, false, false)),
) )
.unwrap()
} }
// these all evaluate to pointers // these all evaluate to pointers
Tag::GlobalDecl => { Tag::GlobalDecl => {
@ -1345,8 +1344,8 @@ impl Ast {
let pointee = let pointee =
self.get_type_of_node(ip, cache, Index::from_u32(self.extra[a]).unwrap()); 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))) ip.as_mut()
.unwrap() .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::FunctionDecl => self.get_type_of_node(ip, cache, data.as_two_indices().0),
Tag::FunctionProto => { 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 => { Tag::BlockTrailingExpr => {
let (_a, b) = data.as_extra_range(); 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 // this evaluates to a pointer
Tag::SubscriptExpr => { Tag::SubscriptExpr => {
@ -1461,11 +1460,13 @@ impl Ast {
} }
Tag::ValueToPlaceConversion => { Tag::ValueToPlaceConversion => {
let ty = self.get_type_of_node(ip, cache, data.as_index()); 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 => { Tag::PlaceToValueConversion => {
let ty = self.get_type_of_node(ip, cache, data.as_index()); 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::Not | Tag::Negate => self.get_type_of_node(ip, cache, data.as_index()),
Tag::Or Tag::Or
@ -1498,7 +1499,7 @@ impl Ast {
Tag::DeclRef => self.get_type_of_node(ip, cache, data.as_index()), Tag::DeclRef => self.get_type_of_node(ip, cache, data.as_index()),
Tag::StructDecl => { Tag::StructDecl => {
let (name, _) = data.as_intern_and_extra_offset(); 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::Assign
| Tag::Root | Tag::Root
@ -1508,6 +1509,8 @@ impl Ast {
| Tag::ReturnStmt => void, | Tag::ReturnStmt => void,
Tag::FieldDecl => self.get_type_of_node(ip, cache, data.as_index_intern().0), Tag::FieldDecl => self.get_type_of_node(ip, cache, data.as_index_intern().0),
Tag::InternedType => data.as_intern(), Tag::InternedType => data.as_intern(),
// Tag::PointerType => {}
// Tag::ArrayType => {}
Tag::TypeDeclRef | Tag::TypeDeclRefUnresolved | Tag::PointerType | Tag::ArrayType => { Tag::TypeDeclRef | Tag::TypeDeclRefUnresolved | Tag::PointerType | Tag::ArrayType => {
ip.get_void_type() ip.get_void_type()
} }
@ -1969,7 +1972,7 @@ impl Ast {
} }
pub fn comptime_number_to_interned_type_and_value( pub fn comptime_number_to_interned_type_and_value(
ip: &mut InternPool, ip: &mut intern::InternPool,
pointer_bits: u16, pointer_bits: u16,
comptime: ComptimeNumber, comptime: ComptimeNumber,
) -> (intern::Index, intern::Index) { ) -> (intern::Index, intern::Index) {
@ -2044,15 +2047,20 @@ pub fn comptime_number_to_interned_type_and_value(
(value, ty) (value, ty)
} }
pub fn interned_type_and_value_to_comptime_number( pub fn interned_type_and_value_to_comptime_number<IP>(
ip: &InternPool, ip: IP,
pointer_bits: u16, pointer_bits: u16,
ty: intern::Index, ty: intern::Index,
val: intern::Index, val: intern::Index,
) -> crate::comptime::ComptimeNumber { ) -> crate::comptime::ComptimeNumber
where
IP: AsRef<intern::InternPool>,
{
use crate::ast::IntegralType; use crate::ast::IntegralType;
use crate::comptime::*; use crate::comptime::*;
let ip = ip.as_ref();
let ty_key = ip.get_key(ty); let ty_key = ip.get_key(ty);
let signed = ip.is_type_signed(ty, AMD64_POINTER_TYPE_INFO); let signed = ip.is_type_signed(ty, AMD64_POINTER_TYPE_INFO);
match ty_key { match ty_key {
@ -2522,7 +2530,7 @@ pub mod ast_gen {
#[derive(Debug)] #[derive(Debug)]
pub struct Parser { pub struct Parser {
pub ast: Ast, pub ast: Ast,
pub intern: intern::InternPool, pub intern: InternPool,
pub syms: crate::symbol_table::syms2::Symbols, pub syms: crate::symbol_table::syms2::Symbols,
scopes: Vec<Index>, scopes: Vec<Index>,
pub errors: Vec<ErrorInfo>, pub errors: Vec<ErrorInfo>,
@ -2540,7 +2548,7 @@ pub mod ast_gen {
pub fn new() -> Parser { pub fn new() -> Parser {
Self { Self {
ast: Ast::new(), ast: Ast::new(),
intern: intern::InternPool::new(), intern: InternPool::new(),
syms: crate::symbol_table::syms2::Symbols::new(), syms: crate::symbol_table::syms2::Symbols::new(),
scopes: Vec::new(), scopes: Vec::new(),
errors: Vec::new(), errors: Vec::new(),
@ -2778,6 +2786,15 @@ pub mod ast_gen {
self.intern self.intern
.get_function_type(return_type, parameters); .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 => { Tag::StructDecl => {
let (name, offset) = let (name, offset) =
data.as_intern_and_extra_offset(); data.as_intern_and_extra_offset();
@ -2823,7 +2840,7 @@ pub mod ast_gen {
Tag::TypeDeclRefUnresolved => { Tag::TypeDeclRefUnresolved => {
let (scope, name) = ast.datas[node].as_index_intern(); let (scope, name) = ast.datas[node].as_index_intern();
// look in my_scope // look in my_scope
if let Some(decl) = if let Some((_, decl)) =
self.syms self.syms
.find_type_symbol(scope, name, ast.source_locs[node]) .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(); let (scope, name) = ast.datas[node].as_index_intern();
// look in my_scope // look in my_scope
if let Some(decl) = if let Some((key, decl)) =
self.syms.find_symbol(scope, name, ast.source_locs[node]) self.syms.find_symbol(scope, name, ast.source_locs[node])
{ {
ast.resolve_decl_ref(node, decl) ast.resolve_decl_ref(
node,
decl,
Option::<DeclKind>::from(key.kind().unwrap()).unwrap(),
)
}; };
} }
_ => {} _ => {}
@ -3311,22 +3332,7 @@ pub mod ast_gen {
/// (let | var) IDENTIFIER (: TYPENAME)? ; /// (let | var) IDENTIFIER (: TYPENAME)? ;
/// (let | var) IDENTIFIER (: TYPENAME)? = EXPRESSION ; /// (let | var) IDENTIFIER (: TYPENAME)? = EXPRESSION ;
fn parse_var_decl(&mut self, tokens: &mut TokenIterator) -> ParseResult<Index> { fn parse_var_decl(&mut self, tokens: &mut TokenIterator) -> ParseResult<Index> {
match self.parse_var_decl_inner(tokens) { self.parse_with_trailing_semi(tokens, Self::parse_var_decl_inner)
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))
}
}
} }
fn parse_var_decl_inner(&mut self, tokens: &mut TokenIterator) -> ParseResult<Index> { fn parse_var_decl_inner(&mut self, tokens: &mut TokenIterator) -> ParseResult<Index> {
@ -3479,8 +3485,12 @@ pub mod ast_gen {
let ty = self.parse_type(tokens)?; let ty = self.parse_type(tokens)?;
let param = self.ast.push_parameter(name, ty, loc); let param = self.ast.push_parameter(name, ty, loc);
self.syms self.syms.insert_symbol(
.insert_symbol(self.current_scope(), name, SymbolKind::Local(loc), param); self.current_scope(),
name,
SymbolKind::Parameter(loc),
param,
);
return Ok(param); return Ok(param);
} }
@ -4838,7 +4848,9 @@ pub mod ir {
Unimplemented, Unimplemented,
} }
use super::{intern::InternPool, visitor::AstExt, AstVisitorTrait, TypeCache}; use super::{
intern::InternPoolWrapper as InternPool, visitor::AstExt, AstVisitorTrait, TypeCache,
};
struct IrBuilder { struct IrBuilder {
functions: Vec<Function>, functions: Vec<Function>,
@ -5676,7 +5688,7 @@ pub mod irgen {
ast: Ast, ast: Ast,
syms: Symbols, syms: Symbols,
ir: IR, ir: IR,
ip: intern::InternPool, ip: InternPool,
} }
impl IRGen { impl IRGen {

View file

@ -35,25 +35,13 @@ impl SymbolPath {
for node in self.0.iter().skip(1).rev() { for node in self.0.iter().skip(1).rev() {
match tree.nodes.get_node(node.unwrap()) { match tree.nodes.get_node(node.unwrap()) {
Tag::VarDecl { name, .. } => { Tag::VarDecl { name, .. } => {
_ = write!( _ = write!(&mut buf, "V{}::", tree.get_ident_str(*name).unwrap());
&mut buf,
"V{}::",
tree.get_ident_str(*name).unwrap()
);
} }
Tag::GlobalDecl { name, .. } => { Tag::GlobalDecl { name, .. } => {
_ = write!( _ = write!(&mut buf, "G{}::", tree.get_ident_str(*name).unwrap());
&mut buf,
"G{}::",
tree.get_ident_str(*name).unwrap()
);
} }
Tag::FunctionProto { name, .. } => { Tag::FunctionProto { name, .. } => {
_ = write!( _ = write!(&mut buf, "F{}::", tree.get_ident_str(*name).unwrap());
&mut buf,
"F{}::",
tree.get_ident_str(*name).unwrap()
);
} }
_ => {} _ => {}
} }
@ -142,12 +130,7 @@ impl Drop for InnerSymbolTable {
} }
impl InnerSymbolTable { impl InnerSymbolTable {
fn insert_symbol( fn insert_symbol(&mut self, name: &str, node: AstNode, kind: SymbolKind) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
kind: SymbolKind,
) -> &SymbolRecord {
match kind { match kind {
SymbolKind::Var => { SymbolKind::Var => {
self.ordered_identifiers.push(SymbolRecord { self.ordered_identifiers.push(SymbolRecord {
@ -160,11 +143,7 @@ impl InnerSymbolTable {
} }
} }
fn insert_orderless_symbol( fn insert_orderless_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
) -> &SymbolRecord {
self.orderless_identifiers.insert( self.orderless_identifiers.insert(
name.to_owned(), name.to_owned(),
SymbolRecord { SymbolRecord {
@ -175,11 +154,7 @@ impl InnerSymbolTable {
self.orderless_identifiers.get(name).unwrap() self.orderless_identifiers.get(name).unwrap()
} }
fn find_symbol_or_insert_with<'a, F>( fn find_symbol_or_insert_with<'a, F>(&'a mut self, name: &str, cb: F) -> &'a SymbolRecord
&'a mut self,
name: &str,
cb: F,
) -> &'a SymbolRecord
where where
F: FnOnce() -> (AstNode, SymbolKind), F: FnOnce() -> (AstNode, SymbolKind),
{ {
@ -202,9 +177,7 @@ impl InnerSymbolTable {
.find(|(_, v)| v.decl == decl) .find(|(_, v)| v.decl == decl)
.map(|(_, v)| v) .map(|(_, v)| v)
}) })
.or_else(|| { .or_else(|| self.parent_ref().and_then(|p| p.find_symbol_by_decl(decl)))
self.parent_ref().and_then(|p| p.find_symbol_by_decl(decl))
})
} }
fn find_any_symbol(&self, name: &str) -> Option<&SymbolRecord> { fn find_any_symbol(&self, name: &str) -> Option<&SymbolRecord> {
@ -219,9 +192,7 @@ impl InnerSymbolTable {
self.ordered_identifiers self.ordered_identifiers
.iter() .iter()
.find(|r| r.name.as_str() == name) .find(|r| r.name.as_str() == name)
.or_else(|| { .or_else(|| self.parent_ref().and_then(|p| p.find_ordered_symbol(name)))
self.parent_ref().and_then(|p| p.find_ordered_symbol(name))
})
} }
fn find_orderless_symbol(&self, name: &str) -> Option<&SymbolRecord> { fn find_orderless_symbol(&self, name: &str) -> Option<&SymbolRecord> {
@ -315,12 +286,7 @@ impl SymbolTableWrapper {
} }
impl SymbolTableWrapper { impl SymbolTableWrapper {
pub fn insert_symbol( pub fn insert_symbol(&mut self, name: &str, node: AstNode, kind: SymbolKind) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
kind: SymbolKind,
) -> &SymbolRecord {
self.current_mut().insert_symbol(name, node, kind) self.current_mut().insert_symbol(name, node, kind)
} }
@ -328,27 +294,15 @@ impl SymbolTableWrapper {
self.root_mut().find_orderless_symbol(name) self.root_mut().find_orderless_symbol(name)
} }
pub fn insert_root_symbol( pub fn insert_root_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
) -> &SymbolRecord {
self.root_mut().insert_orderless_symbol(name, node) self.root_mut().insert_orderless_symbol(name, node)
} }
pub fn insert_orderless_symbol( pub fn insert_orderless_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
) -> &SymbolRecord {
self.current_mut().insert_orderless_symbol(name, node) self.current_mut().insert_orderless_symbol(name, node)
} }
pub fn find_symbol_or_insert_with<'a, F>( pub fn find_symbol_or_insert_with<'a, F>(&'a mut self, name: &str, cb: F) -> &'a SymbolRecord
&'a mut self,
name: &str,
cb: F,
) -> &'a SymbolRecord
where where
F: FnOnce() -> (AstNode, SymbolKind), F: FnOnce() -> (AstNode, SymbolKind),
{ {
@ -470,6 +424,40 @@ pub mod syms2 {
}, },
} }
impl Key {
pub fn kind(&self) -> Option<SymbolKind> {
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<Self> {
match v {
1 => Some(Self::Local),
2 => Some(Self::Parameter),
_ => None,
}
}
}
impl From<SymbolKind> for Option<DeclKind> {
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)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum SymbolKind { pub enum SymbolKind {
__First, __First,
@ -479,6 +467,7 @@ pub mod syms2 {
__TypeScope, __TypeScope,
Scope, Scope,
ParentScope, ParentScope,
Parameter(SourceLocation),
Local(SourceLocation), Local(SourceLocation),
__Last, __Last,
} }
@ -526,9 +515,7 @@ pub mod syms2 {
} }
let entries = self.inner.iter().map(|(key, val)| { let entries = self.inner.iter().map(|(key, val)| {
let payload = match key { let payload = match key {
Key::ScopeByIndex { .. } => { Key::ScopeByIndex { .. } => ExpandedPayload::Intern(val.as_intern()),
ExpandedPayload::Intern(val.as_intern())
}
_ => ExpandedPayload::Ast(val.as_ast()), _ => ExpandedPayload::Ast(val.as_ast()),
}; };
@ -567,7 +554,7 @@ pub mod syms2 {
scope: AstIndex, scope: AstIndex,
name: InternIndex, name: InternIndex,
loc: SourceLocation, loc: SourceLocation,
) -> Option<AstIndex> { ) -> Option<(Key, AstIndex)> {
use SymbolKind::*; use SymbolKind::*;
let range = self.inner.range( let range = self.inner.range(
Key::Symbol { Key::Symbol {
@ -581,8 +568,8 @@ pub mod syms2 {
}, },
); );
if let Some((_, payload)) = range.rev().next() { if let Some((key, payload)) = range.rev().next() {
Some(payload.as_ast()) Some((*key, payload.as_ast()))
} else { } else {
if let Some(parent) = self.inner.get(&Key::Symbol { if let Some(parent) = self.inner.get(&Key::Symbol {
scope, scope,
@ -601,7 +588,7 @@ pub mod syms2 {
scope: AstIndex, scope: AstIndex,
name: InternIndex, name: InternIndex,
loc: SourceLocation, loc: SourceLocation,
) -> Option<AstIndex> { ) -> Option<(Key, AstIndex)> {
use SymbolKind::*; use SymbolKind::*;
let range = self.inner.range( let range = self.inner.range(
Key::Symbol { Key::Symbol {
@ -615,15 +602,15 @@ pub mod syms2 {
}, },
); );
if let Some((_, payload)) = range.rev().next() { if let Some((key, payload)) = range.rev().next() {
Some(payload.as_ast()) Some((*key, payload.as_ast()))
} else { } else {
if let Some(parent) = self.inner.get(&Key::Symbol { if let Some(parent) = self.inner.get(&Key::Symbol {
scope, scope,
name: InternIndex::invalid(), name: InternIndex::invalid(),
kind: ParentScope, kind: ParentScope,
}) { }) {
self.find_symbol(parent.as_ast(), name, loc) self.find_type_symbol(parent.as_ast(), name, loc)
} else { } else {
None None
} }
@ -637,10 +624,8 @@ pub mod syms2 {
kind: SymbolKind, kind: SymbolKind,
ast: AstIndex, ast: AstIndex,
) { ) {
self.inner.insert( self.inner
Key::Symbol { scope, name, kind }, .insert(Key::Symbol { scope, name, kind }, Payload::new_ast(ast));
Payload::new_ast(ast),
);
} }
} }
} }