Skip to content

Commit 0b03d41

Browse files
committed
More progress
1 parent aa760c0 commit 0b03d41

7 files changed

Lines changed: 66 additions & 44 deletions

File tree

src/builtins.pr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export let ushort_ = register("ushort", get_int_of_size(size_of ushort, true))
131131
export let uint_ = register("uint", get_int_of_size(size_of uint, true))
132132
export let ulong_ = register("ulong", get_int_of_size(size_of ulong, true))
133133

134-
export let size_t_ = register("size_t", create_int_type("size_t", size_of size_t, true))
134+
export let size_t_ = register("size_t", get_int_of_size(size_of size_t, true))
135135

136136
#if not defined WIN32 {
137137
let va_list_ident = parser::make_identifier("__va_list_tag")

src/compiler.pr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,13 @@ def convert_to(loc: &Value, value: Value, tpe: typechecking::TypeId, state: &Sta
21782178
return load_value(value, loc, state)
21792179
}
21802180

2181+
if tpe.kind == typechecking::TypeKind::REFERENCE and
2182+
tpe.tpe.kind == typechecking::TypeKind::INTERFACE and
2183+
value.tpe.kind == typechecking::TypeKind::REFERENCE and
2184+
typechecking::implements(value.tpe, tpe, state.scope) {
2185+
return convert_ref_to_ref(tpe, load_value(value, loc, state), loc, state)
2186+
}
2187+
21812188
if value.tpe.kind != typechecking::TypeKind::NULL {
21822189
let parameter_t = vector::make(NamedParameter)
21832190
parameter_t.push([tpe = value.tpe] !NamedParameter)
@@ -8266,11 +8273,11 @@ def create_dyn_cast_function(function: &Function, cast: toolchain::Cast, state:
82668273
push_label(if_true, state)
82678274

82688275
if cast.dst.is_ref() {
8269-
var res = convert_to(null !&Value, src_value, type_entry.tpe, state)
8276+
var res = convert_to(null !&Value, src_value, type_entry.id, state)
82708277
res = convert_to(null !&Value, res, cast.dst, state)
82718278
state.store(dst_value, res)
82728279
} else {
8273-
var res = convert_to(null !&Value, src_value, type_entry.tpe, state)
8280+
var res = convert_to(null !&Value, src_value, type_entry.id, state)
82748281
res = convert_to(null !&Value, res, cast.dst, state)
82758282
state.store(dst_value, @res.addr)
82768283
}

src/consteval.pr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ export def walk_TypeDecl(node: &parser::Node, state: &typechecking::State) {
534534
let enum_tpe = [
535535
kind = typechecking::TypeKind::ENUM,
536536
id = tpe,
537-
tpe = inner
537+
_tpe = inner
538538
] !&typechecking::Type
539539

540540
enum_tpe.name = parser::identifier_to_str(name)

src/eval.pr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ def eval_Switch(insn: &compiler::Insn, state: &State) {
10801080
}
10811081
}
10821082

1083+
//print("Switch value: ", value.i, " ", debug::type_to_str(value.i), "\n")
10831084
stack_frame.next = stack_frame.jump_table(otherwise.name)
10841085
}
10851086

src/scope.pr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,10 @@ export def get_type(scope: &Scope, id: &parser::Node, error_on_fail: bool = true
15841584
export def get_type_id(scope: &Scope, id: &parser::Node, error_on_fail: bool = true) -> typechecking::TypeId {
15851585
let tpe = get_type(scope, id, error_on_fail)
15861586
if not tpe { return 0 }
1587+
if tpe.tpe and tpe.tpe.kind == typechecking::TypeKind::STUB {
1588+
// If we resolve here again its going to go into an infinite loop
1589+
return tpe.tpe
1590+
}
15871591
return tpe.resolve(scope)
15881592
}
15891593

src/serialize.pr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def serialize_type(fp: File, tpe: &typechecking::Type) {
179179
let kw = tpe.kw !byte
180180
fp.write(*kw)
181181
}
182-
write_type(fp, tpe.tpe)
182+
write_type(fp, tpe._tpe)
183183
} else if typechecking::is_struct(tpe) {
184184
let sz = tpe.fields.length()
185185
fp.write(*sz)
@@ -518,7 +518,7 @@ def deserialize_type(fp: File) {
518518

519519
case typechecking::TypeKind::STATIC_ARRAY
520520
fp.read(*tpe.length)
521-
tpe.tpe = read_type(fp)
521+
tpe._tpe = read_type(fp)
522522

523523
if not has_tc_tpe {
524524
typechecking::type_registry_static_array(
@@ -538,10 +538,10 @@ def deserialize_type(fp: File) {
538538
tpe.kw = kw !parser::VarDecl
539539
}
540540

541-
tpe.tpe = read_type(fp)
541+
tpe._tpe = read_type(fp)
542542

543543
if not has_tc_tpe {
544-
let box = [ id = tpe.tpe, kw = tpe.kw ] !typechecking::Box
544+
let box = [ id = tpe._tpe, kw = tpe.kw ] !typechecking::Box
545545

546546
switch tpekind !int {
547547
case typechecking::TypeKind::TYPE
@@ -670,7 +670,7 @@ def deserialize_type(fp: File) {
670670
}
671671
tpe.members = members
672672
case typechecking::TypeKind::ENUM
673-
tpe.tpe = read_type(fp)
673+
tpe._tpe = read_type(fp)
674674

675675
assert defmodule and defmodule.deserialize
676676

src/typechecking.pr

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export type Type = struct {
188188
// Type both used for array/pointer and enum
189189
// This is also used for type arguments to specify the actual type
190190
// And tagged unions carry the underlying union type
191-
tpe: TypeId
191+
_tpe: TypeId
192192
packed: bool
193193
// Fields for struct, array of StructMember
194194
fields: &Vector(StructMember)
@@ -255,6 +255,13 @@ export type Type = struct {
255255
implementors: &Map(TypeId, &Vector(&scope::Value))
256256
}
257257

258+
export def tpe(tpe: &Type) -> TypeId {
259+
if tpe.kind == TypeKind::WEAK_REF {
260+
return tpe._tpe.tpe
261+
}
262+
return tpe._tpe
263+
}
264+
258265
export def has_variant(this: &Type, tpe: &Type) -> bool {
259266
assert this.kind == TypeKind::TUNION
260267
for var t in this.return_t {
@@ -339,16 +346,28 @@ export def kind(typeref: &TypeRef) -> TypeKind {
339346
}
340347
}
341348

349+
def resolve_stub_types(tpe: &Type, error_on_fail: bool = true) {
350+
if not tpe { return }
351+
if is_box(tpe) {
352+
resolve_stub_types(tpe.tpe, error_on_fail)
353+
} else if tpe.kind == TypeKind::STUB {
354+
assert tpe.defmodule != null
355+
// Lookup stub types on demand
356+
let id = scope::get_type_id(tpe.defmodule.scope, parser::identifier_from_str(tpe.type_name), error_on_fail)
357+
if id == 0 { return }
358+
assert id == tpe.id
359+
} else if tpe.kind == TypeKind::TUPLE or tpe.kind == TypeKind::TUNION {
360+
for var r in tpe.return_t {
361+
resolve_stub_types(r, error_on_fail)
362+
}
363+
}
364+
}
365+
342366
export def resolve(typeref: &TypeRef, scpe: &Scope, error_on_fail: bool = true) -> TypeId {
343367
if not typeref { return 0 }
344368
if typeref.tpe {
345369
let actual = typeref.tpe.resolve()
346-
if actual and actual.kind == TypeKind::STUB {
347-
// Lookup stub types on demand
348-
let id = scope::get_type_id(actual.defmodule.scope, parser::identifier_from_str(actual.type_name), error_on_fail)
349-
assert id == typeref.tpe
350-
return id
351-
}
370+
resolve_stub_types(actual, error_on_fail)
352371
return typeref.tpe
353372
}
354373
if typeref.name {
@@ -1732,7 +1751,7 @@ export def variant(types: &Vector(TypeId)) -> TypeId {
17321751
let union_tpe = make_union_type(fields)
17331752
union_tpe.id = next_type_id()
17341753
register_internal(union_tpe.id, union_tpe)
1735-
variant.tpe = union_tpe.id
1754+
variant._tpe = union_tpe.id
17361755

17371756
type_registry_variant(signature) = variant
17381757
register(variant.id, variant)
@@ -1779,7 +1798,7 @@ export def _type(tpe: TypeId) -> TypeId {
17791798
let tpetpe = [
17801799
kind = TypeKind::TYPE,
17811800
id = next_type_id(),
1782-
tpe = tpe
1801+
_tpe = tpe
17831802
] !&Type
17841803
type_registry_type(tid) = tpetpe
17851804
register(tpetpe.id, tpetpe)
@@ -1795,7 +1814,7 @@ export def pointer(tpe: TypeId, kw: parser::VarDecl) -> TypeId {
17951814
let ptrtpe = [
17961815
kind = TypeKind::POINTER,
17971816
id = next_type_id(),
1798-
tpe = tpe,
1817+
_tpe = tpe,
17991818
kw = kw,
18001819
size = (size_of type *),
18011820
align = (align_of type *)
@@ -1823,7 +1842,7 @@ export def byref(tpe: TypeId) -> TypeId {
18231842
let reftpe = [
18241843
kind = TypeKind::BYREF,
18251844
id = next_type_id(),
1826-
tpe = tpe,
1845+
_tpe = tpe,
18271846
size = (size_of type *),
18281847
align = (align_of type *)
18291848
] !&Type
@@ -1845,7 +1864,7 @@ export def reference(tpe: TypeId, kw: parser::VarDecl) -> TypeId {
18451864
let reftpe = [
18461865
kind = TypeKind::REFERENCE,
18471866
id = next_type_id(),
1848-
tpe = tpe,
1867+
_tpe = tpe,
18491868
kw = kw,
18501869
size = (size_of type &),
18511870
align = (align_of type &)
@@ -1872,7 +1891,7 @@ export def weak_reference(tpe: TypeId, kw: parser::VarDecl) -> TypeId {
18721891
let reftpe = [
18731892
kind = TypeKind::WEAK_REF,
18741893
id = next_type_id(),
1875-
tpe = tpe,
1894+
_tpe = tpe,
18761895
kw = kw,
18771896
size = (size_of type &),
18781897
align = (align_of type &)
@@ -1899,7 +1918,7 @@ export def array(tpe: TypeId, kw: parser::VarDecl) -> TypeId {
18991918
let arrtpe = [
19001919
kind = TypeKind::ARRAY,
19011920
id = next_type_id(),
1902-
tpe = tpe,
1921+
_tpe = tpe,
19031922
kw = kw,
19041923
size = (size_of type [*]),
19051924
align = (align_of type [*])
@@ -1927,7 +1946,7 @@ export def static_array(array_tpe: TypeId, size: size_t, kw: parser::VarDecl) ->
19271946
let arrtpe = [
19281947
kind = TypeKind::STATIC_ARRAY,
19291948
id = next_type_id(),
1930-
tpe = array_tpe,
1949+
_tpe = array_tpe,
19311950
kw = kw,
19321951
length = size,
19331952
size = size * tpe.size,
@@ -3012,7 +3031,7 @@ export def is_polymorph(fdef: FunctionDef) -> bool {
30123031
return is_polymorph(fdef.parameter_t, fdef.context.scope)
30133032
}
30143033

3015-
export def is_polymorph(tpe: &TypeRef, scpe: &scope::Scope, or_name: bool = false) -> bool {
3034+
export def is_polymorph(tpe: &TypeRef, scpe: &scope::Scope) -> bool {
30163035
tpe = tpe.copy() // Don't mutate type
30173036
if not tpe { return false }
30183037
if tpe.tpe {
@@ -3028,19 +3047,19 @@ export def is_polymorph(tpe: &TypeRef, scpe: &scope::Scope, or_name: bool = fals
30283047
return true
30293048
} else if pattern.kind == TypeKind::POINTER or
30303049
pattern.kind == TypeKind::STATIC_ARRAY or pattern.kind == TypeKind::ARRAY {
3031-
return is_polymorph(pattern.tpe, scpe, or_name)
3050+
return is_polymorph(pattern.tpe, scpe)
30323051
} else if pattern.kind == TypeKind::REFERENCE {
30333052
if pattern.tpe and pattern.tpe.kind == TypeKind::INTERFACE {
30343053
return false
30353054
} else {
3036-
return is_polymorph(pattern.tpe, scpe, or_name)
3055+
return is_polymorph(pattern.tpe, scpe)
30373056
}
30383057
} else if pattern.kind == TypeKind::TUPLE or
30393058
pattern.kind == TypeKind::TUNION or
30403059
pattern.kind == TypeKind::TYPE_INSTANCE {
30413060

30423061
for var tpe in pattern.return_t {
3043-
if is_polymorph(tpe, scpe, or_name) { return true }
3062+
if is_polymorph(tpe, scpe) { return true }
30443063
}
30453064
return false
30463065
}
@@ -3049,7 +3068,7 @@ export def is_polymorph(tpe: &TypeRef, scpe: &scope::Scope, or_name: bool = fals
30493068
//if tpe {
30503069
// return is_polymorph(tpe, scpe, or_name)
30513070
//}
3052-
return or_name
3071+
return false
30533072
}
30543073
return false
30553074
}
@@ -3164,8 +3183,10 @@ export def replace_type_defs(
31643183
}*/
31653184

31663185
if replace {
3167-
if is_polymorph(left.tpe, scpe, or_name = true) {
3186+
if is_polymorph(left.tpe, scpe) {
31683187
left.tpe = make_type_ref(rtpe)
3188+
} else {
3189+
left.tpe.resolve(scpe)
31693190
}
31703191
}
31713192
}
@@ -3822,14 +3843,7 @@ def do_type_lookup(node: &parser::Node, state: &State, is_type: bool = false) ->
38223843
node.scope = state.scope
38233844
if node.value.t_parr.tpe { node.value.t_parr.tpe.parent = node }
38243845
var tpe = type_lookup(node.value.t_parr.tpe, state, is_type)
3825-
if not tpe or tpe.kind != TypeKind::REFERENCE {
3826-
errors::errorn(node, "Weak reference must point at a reference, was `" + generic_to_string(tpe) + "`")
3827-
}
3828-
if tpe.pattern {
3829-
return weak_reference(tpe.pattern.tpe, parser::VarDecl::VAR) // FIXME proper kw
3830-
} else {
3831-
return make_type_ref(weak_reference(tpe.tpe.tpe, parser::VarDecl::VAR)) // FIXME proper kw
3832-
}
3846+
return weak_reference(tpe, node.value.t_parr.kw)
38333847
} else if node.kind == parser::NodeKind::STRUCT_T or node.kind == parser::NodeKind::UNION_T {
38343848

38353849
let length = vector::length(node.value.body)
@@ -3908,7 +3922,7 @@ def do_type_lookup(node: &parser::Node, state: &State, is_type: bool = false) ->
39083922
}
39093923
let tpe = [ kind = TypeKind::ENUM ] !&Type
39103924
tpe.id = next_type_id()
3911-
tpe.tpe = enum_tpe
3925+
tpe._tpe = enum_tpe
39123926
tpe.size = enum_tpe.resolve().size
39133927
tpe.align = enum_tpe.resolve().align
39143928
tpe.defmodule = state.module
@@ -4596,11 +4610,7 @@ export def walk_VarDecl(node: &parser::Node, state: &State, set_constant: bool =
45964610

45974611
if tpe_node {
45984612
let tpe = type_lookup(tpe_node, state)
4599-
if not tpe {
4600-
ltypes.push(0)
4601-
} else {
4602-
ltypes.push(tpe.tpe)
4603-
}
4613+
ltypes.push(tpe.resolve(state.scope))
46044614
} else {
46054615
ltypes.push(0)
46064616
}

0 commit comments

Comments
 (0)