@@ -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+
258265export 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+
342366export 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