| 1 | module js |
| 2 | |
| 3 | import v.ast |
| 4 | import arrays |
| 5 | |
| 6 | struct Type { |
| 7 | // typ is the original type |
| 8 | typ ast.Type @[required] |
| 9 | sym &ast.TypeSymbol = unsafe { nil } @[required] |
| 10 | // unaliased is `typ` once aliased have been resolved |
| 11 | // it may not contain information such as flags and nr_muls |
| 12 | unaliased ast.Type @[required] |
| 13 | unaliased_sym &ast.TypeSymbol = unsafe { nil } @[required] |
| 14 | } |
| 15 | |
| 16 | fn (a Type) == (b Type) bool { |
| 17 | return a.unaliased == b.unaliased |
| 18 | } |
| 19 | |
| 20 | fn (a Type) < (b Type) bool { |
| 21 | return a.unaliased_sym.name < b.unaliased_sym.name |
| 22 | } |
| 23 | |
| 24 | // unwrap returns the following variants of a type: |
| 25 | // * generics unwrapped |
| 26 | // * alias unwrapped |
| 27 | fn (mut g JsGen) unwrap(typ ast.Type) Type { |
| 28 | no_generic := g.unwrap_generic(typ) |
| 29 | no_generic_sym := g.table.sym(no_generic) |
| 30 | if no_generic_sym.kind != .alias { |
| 31 | return Type{ |
| 32 | typ: no_generic |
| 33 | sym: no_generic_sym |
| 34 | unaliased: no_generic |
| 35 | unaliased_sym: no_generic_sym |
| 36 | } |
| 37 | } |
| 38 | no_generic_unaliased := g.table.unaliased_type(no_generic) |
| 39 | return Type{ |
| 40 | typ: no_generic |
| 41 | sym: no_generic_sym |
| 42 | unaliased: no_generic_unaliased |
| 43 | unaliased_sym: g.table.sym(no_generic_unaliased) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn (mut g JsGen) unwrap_sum_type(typ ast.Type) []Type { |
| 48 | mut types := []Type{} |
| 49 | sym := g.table.sym(typ) |
| 50 | if sym.info is ast.SumType { |
| 51 | for v in sym.info.variants { |
| 52 | types << g.unwrap_sum_type(v) |
| 53 | } |
| 54 | } else { |
| 55 | types << g.unwrap(typ) |
| 56 | } |
| 57 | return arrays.distinct(types) |
| 58 | } |
| 59 | |