v2 / vlib / builtin / builtin.v
58 lines · 51 sloc · 1.32 KB · a75b9622292f36a85c5da0926c1f8a1e1623e4ae
Raw
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]
5module builtin
6
7// isnil returns true if an object is nil (only for C objects).
8@[inline]
9pub fn isnil(v voidptr) bool {
10 return v == 0
11}
12
13struct 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.
22pub struct SliceIndex {
23pub:
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]
33fn __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]
51fn __v2_flag_has_int(receiver int, flag int) bool {
52 return (receiver & flag) != 0
53}
54
55@[inline]
56fn __v2_flag_all_int(receiver int, flags int) bool {
57 return (receiver & flags) == flags
58}
59