Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use the name of the type given to look up a method in the namespace. #372

Merged
merged 2 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use crate::build_config::BuildConfig;
use crate::control_flow_analysis::ControlFlowGraph;
use crate::parse_tree::MethodName;
use crate::parser::{HllParser, Rule};

use pest::Parser;
Expand Down Expand Up @@ -37,9 +38,24 @@ pub(crate) fn type_check_method_application<'sc>(
));
}

let ty = match method_name {
MethodName::FromType { ref type_name, .. } => type_name
.as_ref()
.map(|x| insert_type(x.clone()))
.unwrap_or_else(|| {
args_buf
.get(0)
.map(|x| x.return_type)
.unwrap_or_else(|| insert_type(TypeInfo::Unknown))
}),
_ => args_buf
.get(0)
.map(|x| x.return_type)
.unwrap_or_else(|| insert_type(TypeInfo::Unknown)),
};

let method = check!(
namespace
.find_method_for_type(args_buf[0].return_type, &method_name, self_type, &args_buf,),
namespace.find_method_for_type(ty, &method_name, self_type, &args_buf),
return err(warnings, errors),
warnings,
errors
Expand Down
9 changes: 4 additions & 5 deletions core_lang/src/semantic_analysis/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'sc> Namespace<'sc> {
///
///
/// If a self type is given and anything on this ref chain refers to self, update the chain.
pub(crate) fn resolve_type_with_self(&mut self, ty: TypeInfo, self_type: TypeId) -> TypeId {
pub(crate) fn resolve_type_with_self(&self, ty: TypeInfo, self_type: TypeId) -> TypeId {
let ty = ty.clone();
match ty {
TypeInfo::Custom { name } => match self.get_symbol_by_str(&name) {
Expand Down Expand Up @@ -553,6 +553,7 @@ impl<'sc> Namespace<'sc> {

// This is a hack and I don't think it should be used. We check the local namespace first,
// but if nothing turns up then we try the namespace where the type itself is declared.
let r#type = namespace.resolve_type_with_self(look_up_type_id(r#type), self_type);
let methods = self.get_methods_for_type(r#type);
let methods = match methods[..] {
[] => namespace.get_methods_for_type(r#type),
Expand All @@ -565,14 +566,12 @@ impl<'sc> Namespace<'sc> {
{
Some(o) => ok(o, warnings, errors),
None => {
if args_buf
.get(0)
.map(|x| crate::type_engine::look_up_type_id(x.return_type))
if args_buf.get(0).map(|x| look_up_type_id(x.return_type))
!= Some(TypeInfo::ErrorRecovery)
{
errors.push(CompileError::MethodNotFound {
method_name: method_name.primary_name.to_string(),
type_name: look_up_type_id(args_buf[0].return_type).friendly_type_str(),
type_name: r#type.friendly_type_str(),
span: method_name.span.clone(),
});
}
Expand Down
8 changes: 8 additions & 0 deletions stdlib/src/ops.sw
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ impl b256 {
r4: bool
}
}
fn neq(self, other: Self) -> bool {
// Both self and other are addresses of the values, so we can use MEQ.
not(asm(r1: self, r2: other, r3, r4) {
addi r3 zero i32;
meq r4 r1 r2 r3;
r4: bool
})
}
}


Expand Down
3 changes: 2 additions & 1 deletion test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ pub fn run(filter_regex: Option<regex::Regex>) {
("const_decl", ProgramState::Return(100)),
("const_decl_in_library", ProgramState::Return(1)), // true
("aliased_imports", ProgramState::Return(42)),
("b512_struct_alignment", ProgramState::Return(1)), // true
("empty_method_initializer", ProgramState::Return(1)), // true
("b512_struct_alignment", ProgramState::Return(1)), // true
];

project_names.into_iter().for_each(|(name, res)| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
author = "Nick Furfaro"
license = "MIT"
name = "b512_test"
entry = "main.sw"

[dependencies]
std = { path = "../../../../../stdlib" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
script;

// Stores two b256s in contiguous memory.
// Guaranteed to be contiguous for things like ECR.
struct B512 {
hi: b256,
lo: b256,
}

impl B512 {
// Initializes a blank B512
fn new() -> B512 {
let hi = asm(rhi) {
move rhi sp;
cfei i32;
rhi: b256
};

let lo = asm(rlo) {
move rlo sp;
cfei i32;
rlo: b256
};

B512 {
hi: hi,
lo: lo
}
}

fn from_b256(hi: b256, lo: b256) -> B512 {
// copy the two given b256s into contiguous stack memory
// this involves grabbing the stack pointer, extending the stack by 256 bits,
// using MCP to copy hi into first ptr
// repeat w/ second ptr

let hi = asm(r1: hi, rhi) {
move rhi sp; // move stack pointer to rhi
cfei i32; // extend call frame by 32 bytes to allocate more memory. now $rhi is pointing to blank, uninitialized (but allocated) memory
mcpi rhi r1 i32; // refactor to use mcpi when implemented!
rhi: b256
};

let lo = asm(r1: lo, rlo) {
move rlo sp;
cfei i32;
// now $rlo is pointing to blank memory that we can use
mcpi rlo r1 i32; // refactor to use mcpi when implemented!
rlo: b256
};

B512 {
hi: hi,
lo: lo
}
}
}

fn main() -> bool {
let hi_bits: b256 = 0x7777777777777777777777777777777777777777777777777777777777777777;
let lo_bits: b256 = 0x0000000000000000000000000000000000000000000000000000000000000000;


let b = ~B512::from_b256(hi_bits, lo_bits);
let other_b = ~B512::new();
(b.hi != other_b.hi) && (b.lo == other_b.lo)
}




Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
library types;

// Stores two b256s in contiguous memory.
// Guaranteed to be contiguous for things like ECR.
struct B512 {
hi: b256,
lo: b256,
}

// temp
pub fn build_from_b256s(hi: b256, lo: b256) -> B512 {
let hi = asm(r1: hi, rhi, r2: 32) {
move rhi sp; // move stack pointer to rhi
cfei i32; // extend call frame by 32 bytes to allocate more memory. now $rhi is pointing to blank, uninitialized (but allocated) memory
// addi r5 zero i32;
mcp rhi r1 r2;
rhi: b256
};

let lo = asm(r1: lo, rlo, r2: 32) {
move rlo sp;
cfei i32;
// now $rlo is pointing to blank memory that we can use
mcp rlo r1 r2;
rlo: b256
};

B512 {
hi: hi,
lo: lo
}
}

impl B512 {
// Initializes a blank B512
fn new() -> B512 {
let hi = asm(rhi) {
move rhi sp;
cfei i32;
rhi: b256
};

let lo = asm(rlo) {
move rlo sp;
cfei i32;
rlo: b256
};

B512 {
hi: hi,
lo: lo
}
}

fn from_b256(hi: b256, lo: b256) -> B512 {
// copy the two given b256s into contiguous stack memory
// this involves grabbing the stack pointer, extending the stack by 256 bits,
// using MCP to copy hi into first ptr
// repeat w/ second ptr

let hi = asm(r1: hi, rhi, r2: 32) {
move rhi sp; // move stack pointer to rhi
cfei i32; // extend call frame by 32 bytes to allocate more memory. now $rhi is pointing to blank, uninitialized (but allocated) memory
mcp rhi r1 r2; // refactor to use mcpi when implemented!
rhi: b256
};

let lo = asm(r1: lo, rlo, r2) {
move rlo sp;
cfei i32;
// now $rlo is pointing to blank memory that we can use
mcp rlo r1 r2; // refactor to use mcpi when implemented!
rlo: b256
};

B512 {
hi: hi,
lo: lo
}
}
}