| 1 | // Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | @[has_globals] |
| 5 | module builtin |
| 6 | |
| 7 | // isnil returns true if an object is nil (only for C objects). |
| 8 | @[inline] |
| 9 | pub fn isnil(v voidptr) bool { |
| 10 | return v == 0 |
| 11 | } |
| 12 | |
| 13 | struct VCastTypeIndexName { |
| 14 | tindex int |
| 15 | tname string |
| 16 | } |
| 17 | |
| 18 | // will be filled in cgen |
| 19 | __global as_cast_type_indexes []VCastTypeIndexName |
| 20 | |
| 21 | // SliceIndex describes one overloaded `[]` index or slice part. |
| 22 | pub struct SliceIndex { |
| 23 | pub: |
| 24 | is_range bool |
| 25 | value int |
| 26 | low int |
| 27 | high int |
| 28 | has_low bool |
| 29 | has_high bool |
| 30 | } |
| 31 | |
| 32 | @[direct_array_access] |
| 33 | fn __as_cast(obj voidptr, obj_type int, expected_type int) voidptr { |
| 34 | if obj_type != expected_type { |
| 35 | mut obj_name := as_cast_type_indexes[0].tname.clone() |
| 36 | mut expected_name := as_cast_type_indexes[0].tname.clone() |
| 37 | for x in as_cast_type_indexes { |
| 38 | if x.tindex == obj_type { |
| 39 | obj_name = x.tname.clone() |
| 40 | } |
| 41 | if x.tindex == expected_type { |
| 42 | expected_name = x.tname.clone() |
| 43 | } |
| 44 | } |
| 45 | panic('as cast: cannot cast `' + obj_name + '` to `' + expected_name + '`') |
| 46 | } |
| 47 | return obj |
| 48 | } |
| 49 | |
| 50 | @[inline] |
| 51 | fn __v2_flag_has_int(receiver int, flag int) bool { |
| 52 | return (receiver & flag) != 0 |
| 53 | } |
| 54 | |
| 55 | @[inline] |
| 56 | fn __v2_flag_all_int(receiver int, flags int) bool { |
| 57 | return (receiver & flags) == flags |
| 58 | } |
| 59 | |