Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed Apr 27, 2024
1 parent e599657 commit 46be6d7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
16 changes: 14 additions & 2 deletions compiler/lsp/finder.oc
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,20 @@ def Finder::find_in_call_args(&this, node: &AST, args: &Vector<&Argument>): bool

for let i = 0; i < args.size; i += 1 {
let arg = args.at(i)
if arg.label_span.contains_loc(.loc) {
if verbose then println("TODO: set usage for Finder::find_in_call_args label span")
if arg.label? and arg.label_span.contains_loc(.loc) {
// Find the parameter with the same label
let callee_type = node.u.call.callee.etype
if callee_type? {
let found: &Variable = null
for param in callee_type.sym.u.func.params.iter() {
if param.sym.name == arg.label {
found = param
break
}
}
.set_usage(found.sym, node)
}

return true
}
if .find_in_expression(arg.expr) return true
Expand Down
4 changes: 4 additions & 0 deletions compiler/parser.oc
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,10 @@ def Parser::parse_namespace_until(&this, end_type: TokenType) {
let type = .parse_type()
.consume_end_of_statement()

let sym = Symbol::new(TypeDef, .ns, name.text, name.text, name.text, name.span)
sym.u.type_def = type
type.sym = sym

if type? {
.ns.typedefs.insert(name.text, type)
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/passes/typechecker.oc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def TypeChecker::resolve_type(
let p_r = resolve_templates

let resolved = old
if not old? return null

match old.base {
Pointer => {
let ptr = .resolve_type(old.u.ptr, p_a, p_e, p_r)
Expand Down Expand Up @@ -130,7 +132,7 @@ def TypeChecker::resolve_type(

if res? {
match res.type {
SymbolType::TypeDef => resolved = res.u.type_def
SymbolType::TypeDef => resolved = .resolve_type(res.u.type_def, p_a, p_e, p_r)
SymbolType::Structure => {
let struc = res.u.struc
if res.is_templated() and not allow_incomplete {
Expand Down
14 changes: 13 additions & 1 deletion compiler/types.oc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,19 @@ def Type::can_have_methods(&this): bool => match .base {
else => false
}

def Type::is_resolved(&this): bool => .base != BaseType::Unresolved
def Type::is_resolved(&this): bool => match .base {
Unresolved => false
Alias => .u.ptr.is_resolved()
Pointer => .u.ptr.is_resolved()
Function => {
let resolved = .u.func.return_type.is_resolved()
for param in .u.func.params.iter() {
resolved = resolved and param.type.is_resolved()
}
yield resolved
}
else => true
}

def Type::eq(&this, other: &Type, strict: bool = false): bool {
if (this == null and other == null) return true
Expand Down
5 changes: 2 additions & 3 deletions std/buffer.oc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ def Buffer::resize_if_necessary(&this, new_size: u32) {
// NOTE: To make a buffer trivially usable as a C-style string,
// we always allocate one extra byte for the null terminator, and
// ensure that any unused bytes are zeroed out.
let new_size_nt = new_size + 1
if new_size_nt >= .capacity {
let new_capacity = u32::max(.capacity, new_size_nt)
if new_size + 1 >= .capacity {
let new_capacity = u32::max(.capacity * 3 / 2, new_size + 1)
.data = mem::realloc<u8>(.data, .capacity, new_capacity)
// Zero out the new capacity
memset(.data + .capacity, 0, new_capacity - .capacity)
Expand Down
4 changes: 4 additions & 0 deletions std/span.oc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def Location::default(): Location {
}

def Location::hash(this): u32 => pair_hash(.filename.hash(), .index.hash())

[operator "=="]
def Location::eq(this, other: Location): bool => .filename.eq(other.filename) and .index == other.index
def Location::str(&this): str { return `{.filename}:{.line}:{.col}`}
Expand Down Expand Up @@ -57,6 +59,8 @@ def Span::default(): Span {
}

def Span::hash(this): u32 => pair_hash(.start.hash(), .end.index.hash())

[operator "=="]
def Span::eq(this, other: Span): bool => .start.eq(other.start) and .end.eq(other.end)

def Span::is_valid(this): bool {
Expand Down
13 changes: 13 additions & 0 deletions std/vector.oc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ def Vector::back(&this, i: u32 = 0): T {
return .data[.size - i - 1]
}

//* Returns a pointer to the last element of the vector
def Vector::back_ptr(&this, i: u32 = 0): &T {
assert .size > 0, "Empty vector in Vector::back"
assert i < .size, "Out of bounds in Vector::back"
return &.data[.size - i - 1]
}

//* Returns a pointer to the last element of the vector
def Vector::at_ptr(&this, i: u32 = 0): &T {
assert i < .size, "Out of bounds in Vector::at"
return &.data[i]
}

[operator "[]"]
//* Returns the element at the given index
def Vector::at(&this, i: u32): T {
Expand Down

0 comments on commit 46be6d7

Please sign in to comment.