Skip to content

Commit

Permalink
Gutted out the old type resolution system, and started pluging in the…
Browse files Browse the repository at this point in the history
… new one
  • Loading branch information
IsaacShelton committed Oct 12, 2024
1 parent 26c6ba5 commit 4ec6cc7
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 440 deletions.
41 changes: 23 additions & 18 deletions src/ast/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ pub struct AstWorkspace<'a> {
impl<'a> AstWorkspace<'a> {
pub const DEFAULT_SETTINGS_ID: SettingsId = SettingsId(0);

pub fn get_owning_module(&self, fs_node_id: FsNodeId) -> Option<FsNodeId> {
let mut fs_node_id = fs_node_id;

loop {
if self.module_folders.contains_key(&fs_node_id) {
return Some(fs_node_id);
}

if let Some(parent) = self.fs.get(fs_node_id).parent {
fs_node_id = parent;
} else {
break;
}
}

None
}

pub fn new(
fs: Fs,
files: IndexMap<FsNodeId, AstFile>,
Expand Down Expand Up @@ -70,6 +52,29 @@ impl<'a> AstWorkspace<'a> {
workspace
}

pub fn get_owning_module(&self, fs_node_id: FsNodeId) -> Option<FsNodeId> {
let mut fs_node_id = fs_node_id;

loop {
if self.module_folders.contains_key(&fs_node_id) {
return Some(fs_node_id);
}

if let Some(parent) = self.fs.get(fs_node_id).parent {
fs_node_id = parent;
} else {
break;
}
}

None
}

pub fn get_settings_for_module(&self, module_node_id: FsNodeId) -> &Settings {
let file = self.files.get(&module_node_id).expect("valid module id");
&self.settings[file.settings.unwrap_or_default().0]
}

pub fn get_mut(&mut self, id: FsNodeId) -> Option<&mut AstFile> {
self.files.get_mut(&id)
}
Expand Down
25 changes: 18 additions & 7 deletions src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn lower_global(
let mangled_name = if global.is_foreign {
global.name.plain().to_string()
} else {
global.name.display(resolved_ast.fs).to_string()
global.name.display(&resolved_ast.workspace.fs).to_string()
};

ir_module.globals.insert(
Expand Down Expand Up @@ -167,7 +167,10 @@ fn lower_function(
} else {
return Err(LowerErrorKind::MustReturnValueOfTypeBeforeExitingFunction {
return_type: function.return_type.to_string(),
function: function.name.display(resolved_ast.fs).to_string(),
function: function
.name
.display(&resolved_ast.workspace.fs)
.to_string(),
}
.at(function.source));
}
Expand Down Expand Up @@ -200,7 +203,10 @@ fn lower_function(
} else if function.is_foreign {
function.name.plain().to_string()
} else {
function.name.display(resolved_ast.fs).to_string()
function
.name
.display(&resolved_ast.workspace.fs)
.to_string()
};

let is_main = mangled_name == "main";
Expand Down Expand Up @@ -341,6 +347,8 @@ fn lower_type(
use resolved::{IntegerBits as Bits, IntegerSign as Sign};

match &resolved_type.kind {
resolved::TypeKind::Unresolved => panic!(),
resolved::TypeKind::TypeAlias(_, _) => todo!("lower type ref"),
resolved::TypeKind::Boolean => Ok(ir::Type::Boolean),
resolved::TypeKind::Integer(bits, sign) => Ok(match (bits, sign) {
(Bits::Bits8, Sign::Signed) => ir::Type::S8,
Expand Down Expand Up @@ -395,13 +403,16 @@ fn lower_type(
})))
}
resolved::TypeKind::FunctionPointer(_function_pointer) => Ok(ir::Type::FunctionPointer),
resolved::TypeKind::Enum(enum_name) => {
resolved::TypeKind::Enum(_, _enum_name) => {
todo!("lower enum type");
/*
let enum_definition = resolved_ast
.enums
.get(enum_name)
.expect("referenced enum to exist");
lower_type(target, &enum_definition.resolved_type, resolved_ast)
*/
}
}
}
Expand Down Expand Up @@ -898,7 +909,7 @@ fn lower_expr(
LowerErrorKind::NoSuchEnumMember {
enum_name: enum_member_literal
.enum_name
.display(resolved_ast.fs)
.display(&resolved_ast.workspace.fs)
.to_string(),
variant_name: enum_member_literal.variant_name.clone(),
}
Expand All @@ -918,7 +929,7 @@ fn lower_expr(
value: value.to_string(),
expected_type: enum_member_literal
.enum_name
.display(resolved_ast.fs)
.display(&resolved_ast.workspace.fs)
.to_string(),
}
.at(enum_definition.source)
Expand Down Expand Up @@ -953,7 +964,7 @@ fn lower_expr(
return Err(LowerErrorKind::EnumBackingTypeMustBeInteger {
enum_name: enum_member_literal
.enum_name
.display(resolved_ast.fs)
.display(&resolved_ast.workspace.fs)
.to_string(),
}
.at(enum_definition.source))
Expand Down
10 changes: 7 additions & 3 deletions src/resolve/core_structure_info.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use super::error::{ResolveError, ResolveErrorKind};
use super::error::ResolveError;
use crate::{
name::ResolvedName,
resolved::{self, StructureRef},
source_files::Source,
};

pub fn get_core_structure_info(
resolved_type: &resolved::Type,
source: Source,
_resolved_type: &resolved::Type,
_source: Source,
) -> Result<(&ResolvedName, StructureRef), ResolveError> {
todo!("get_core_structure_info");

/*
match &resolved_type.kind {
resolved::TypeKind::Structure(name, structure_ref) => Ok((name, *structure_ref)),
_ => Err(ResolveErrorKind::CannotCreateStructLiteralForNonStructure {
bad_type: resolved_type.to_string(),
}
.at(source)),
}
*/
}
18 changes: 14 additions & 4 deletions src/resolve/expr/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub fn resolve_call_expr(
return Err(ResolveErrorKind::BadTypeForArgumentToFunction {
expected: preferred_type.to_string(),
got: argument.resolved_type.to_string(),
name: function.name.display(ctx.resolved_ast.fs).to_string(),
name: function
.name
.display(&ctx.resolved_ast.workspace.fs)
.to_string(),
i,
}
.at(source));
Expand All @@ -88,13 +91,20 @@ pub fn resolve_call_expr(
}

if let Some(required_ty) = &call.expected_to_return {
let resolved_required_ty =
resolve_type(ctx.type_search_ctx, required_ty, &mut Default::default())?;
let resolved_required_ty = resolve_type(
ctx.resolved_ast,
ctx.module_fs_node_id,
required_ty,
&mut Default::default(),
)?;

if resolved_required_ty != return_type {
return Err(ResolveErrorKind::FunctionMustReturnType {
of: required_ty.to_string(),
function_name: function.name.display(ctx.resolved_ast.fs).to_string(),
function_name: function
.name
.display(&ctx.resolved_ast.workspace.fs)
.to_string(),
}
.at(function.return_type.source));
}
Expand Down
23 changes: 15 additions & 8 deletions src/resolve/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use super::{
error::ResolveError,
function_search_ctx::FunctionSearchCtx,
global_search_ctx::GlobalSearchCtx,
type_search_ctx::TypeSearchCtx,
variable_search_ctx::VariableSearchCtx,
Initialized,
};
Expand Down Expand Up @@ -52,7 +51,6 @@ use std::collections::HashMap;
pub struct ResolveExprCtx<'a, 'b> {
pub resolved_ast: &'b mut resolved::Ast<'a>,
pub function_search_ctx: &'b FunctionSearchCtx,
pub type_search_ctx: &'b TypeSearchCtx<'a>,
pub global_search_ctx: &'b GlobalSearchCtx,
pub variable_search_ctx: VariableSearchCtx,
pub resolved_function_ref: resolved::FunctionRef,
Expand Down Expand Up @@ -351,15 +349,18 @@ pub fn resolve_expr(
resolved::TypeKind::Boolean.at(source),
resolved::Expr::new(resolved::ExprKind::BooleanLiteral(*value), source),
)),
ast::ExprKind::EnumMemberLiteral(enum_member_literal) => {
ast::ExprKind::EnumMemberLiteral(_enum_member_literal) => {
todo!("resolve enum member literal");

/*
let resolved_type = resolve_type(
ctx.type_search_ctx,
&ast::TypeKind::Named(enum_member_literal.enum_name.clone())
.at(enum_member_literal.source),
&mut Default::default(),
)?;
let TypeKind::Enum(resolved_name) = &resolved_type.kind else {
let TypeKind::Enum(_, _) = &resolved_type.kind else {
return Err(ResolveErrorKind::StaticMemberOfTypeDoesNotExist {
ty: enum_member_literal.enum_name.to_string(),
member: enum_member_literal.variant_name.clone(),
Expand All @@ -379,7 +380,8 @@ pub fn resolve_expr(
})),
source,
),
))
))
*/
}
ast::ExprKind::InterpreterSyscall(info) => {
let ast::InterpreterSyscall {
Expand All @@ -388,14 +390,19 @@ pub fn resolve_expr(
result_type,
} = &**info;

let resolved_type =
resolve_type(ctx.type_search_ctx, result_type, &mut Default::default())?;
let resolved_type = resolve_type(
ctx.resolved_ast,
ctx.module_fs_node_id,
result_type,
&mut Default::default(),
)?;

let mut resolved_args = Vec::with_capacity(args.len());

for (expected_arg_type, arg) in args {
let preferred_type = resolve_type(
ctx.type_search_ctx,
ctx.resolved_ast,
ctx.module_fs_node_id,
expected_arg_type,
&mut Default::default(),
)?;
Expand Down
29 changes: 13 additions & 16 deletions src/resolve/expr/struct_literal.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
use super::{resolve_expr, PreferredType, ResolveExprCtx};
use super::ResolveExprCtx;
use crate::{
ast::{self, ConformBehavior, FieldInitializer, FillBehavior},
resolve::{
conform::{conform_expr, ConformMode, Perform},
core_structure_info::get_core_structure_info,
error::{ResolveError, ResolveErrorKind},
resolve_type, Initialized,
},
resolved::{self, StructLiteral, StructureRef, TypedExpr},
resolve::error::ResolveError,
resolved::{self, StructureRef, TypedExpr},
source_files::Source,
};
use indexmap::IndexMap;
use itertools::Itertools;

fn get_field_info<'a>(
ctx: &'a ResolveExprCtx,
Expand All @@ -30,13 +23,16 @@ fn get_field_info<'a>(
}

pub fn resolve_struct_literal_expr(
ctx: &mut ResolveExprCtx,
ast_type: &ast::Type,
fields: &[FieldInitializer],
fill_behavior: FillBehavior,
conform_behavior: ConformBehavior,
source: Source,
_ctx: &mut ResolveExprCtx,
_ast_type: &ast::Type,
_fields: &[FieldInitializer],
_fill_behavior: FillBehavior,
_conform_behavior: ConformBehavior,
_source: Source,
) -> Result<TypedExpr, ResolveError> {
todo!("resolve_struct_literal_expr");

/*
let resolved_type = resolve_type(ctx.type_search_ctx, ast_type, &mut Default::default())?;
let (struct_name, structure_ref) = get_core_structure_info(&resolved_type, source)?;
Expand Down Expand Up @@ -170,4 +166,5 @@ pub fn resolve_struct_literal_expr(
ast_type.source,
),
))
*/
}
Loading

0 comments on commit 4ec6cc7

Please sign in to comment.