Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vlib: add missing docstrings for vlib/v/ast/comptime_const_values.v functions #21219

Merged
merged 11 commits into from
Apr 9, 2024
14 changes: 14 additions & 0 deletions vlib/v/ast/comptime_const_values.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ pub type ComptTimeConstValue = EmptyExpr

//| int

// empty_comptime_const_expr return an EmptyExpr
pub fn empty_comptime_const_expr() ComptTimeConstValue {
return EmptyExpr(0)
}

// i8 tries to return a `CompTimeConstValue` as `i8` type
dotdot0 marked this conversation as resolved.
Show resolved Hide resolved
pub fn (val ComptTimeConstValue) i8() ?i8 {
x := val.i64()?
if x > -129 && x < 128 {
Expand All @@ -30,6 +32,7 @@ pub fn (val ComptTimeConstValue) i8() ?i8 {
return none
}

// i16 tries to return a `CompTimeConstValue` as `i16` type
pub fn (val ComptTimeConstValue) i16() ?i16 {
x := val.i64()?
if x > -32769 && x < 32768 {
Expand All @@ -38,6 +41,7 @@ pub fn (val ComptTimeConstValue) i16() ?i16 {
return none
}

// int tries to return a `CompTimeConstValue` as `int` type
pub fn (val ComptTimeConstValue) int() ?int {
x := val.i64()?
if x > -2147483649 && x < 2147483648 {
Expand All @@ -46,6 +50,7 @@ pub fn (val ComptTimeConstValue) int() ?int {
return none
}

// i32 tries to return a `CompTimeConstValue` as `i32` type
pub fn (val ComptTimeConstValue) i32() ?i32 {
x := val.i64()?
if x > -2147483649 && x < 2147483648 {
Expand All @@ -54,6 +59,7 @@ pub fn (val ComptTimeConstValue) i32() ?i32 {
return none
}

// voidptr tries to return a `CompTimeConstValue` as `voidptr` type
pub fn (val ComptTimeConstValue) voidptr() ?voidptr {
match val {
i8, i16, i32, i64, int { return voidptr(i64(val)) }
Expand All @@ -65,6 +71,7 @@ pub fn (val ComptTimeConstValue) voidptr() ?voidptr {
return none
}

// i64 tries to return a `CompTimeConstValue` as i64 type
pub fn (val ComptTimeConstValue) i64() ?i64 {
match val {
i8 {
Expand Down Expand Up @@ -119,6 +126,7 @@ pub fn (val ComptTimeConstValue) i64() ?i64 {
return none
}

// u8 tries to return a `CompTimeConstValue` as `u8` type
pub fn (val ComptTimeConstValue) u8() ?u8 {
x := val.u64()?
if x < 256 {
Expand All @@ -127,6 +135,7 @@ pub fn (val ComptTimeConstValue) u8() ?u8 {
return none
}

// u16 tries to return a `CompTimeConstValue` as `u16` type
pub fn (val ComptTimeConstValue) u16() ?u16 {
x := val.u64()?
if x < 65536 {
Expand All @@ -135,6 +144,7 @@ pub fn (val ComptTimeConstValue) u16() ?u16 {
return none
}

// u32 tries to return a `CompTimeConstValue` as `u32` type
pub fn (val ComptTimeConstValue) u32() ?u32 {
x := val.u64()?
if x < 4294967296 {
Expand All @@ -143,6 +153,7 @@ pub fn (val ComptTimeConstValue) u32() ?u32 {
return none
}

// u64 tries to return a `CompTimeConstValue` as `u64` type
pub fn (val ComptTimeConstValue) u64() ?u64 {
match val {
i8 {
Expand Down Expand Up @@ -204,11 +215,13 @@ pub fn (val ComptTimeConstValue) u64() ?u64 {
return none
}

// f32 tries to return a `CompTimeConstValue` as `f32` type
pub fn (val ComptTimeConstValue) f32() ?f32 {
x := val.f64()?
return f32(x)
}

// f64 tries to return a `CompTimeConstValue` as `f64` type
pub fn (val ComptTimeConstValue) f64() ?f64 {
match val {
i8 {
Expand Down Expand Up @@ -254,6 +267,7 @@ pub fn (val ComptTimeConstValue) f64() ?f64 {
return none
}

// string tries to return a `CompTimeConstValue` as `string` type
pub fn (val ComptTimeConstValue) string() ?string {
match val {
i8 {
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/ast/types.v
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ fn (ts TypeSymbol) dbg_common(mut res []string) {
res << 'language: ${ts.language}'
}

// str returns a string representation of the type.
pub fn (t Type) str() string {
return 'ast.Type(0x${t.hex()} = ${u32(t)})'
}
Expand Down Expand Up @@ -717,7 +718,7 @@ pub fn mktyp(typ Type) Type {
}
}

// returns TypeSymbol kind only if there are no type modifiers
// type_kind returns the kind of the given type symbol.
pub fn (t &Table) type_kind(typ Type) Kind {
if typ.nr_muls() > 0 || typ.has_option_or_result() {
return Kind.placeholder
Expand Down Expand Up @@ -1009,6 +1010,7 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_sym(kind: .voidptr, name: 'nil', cname: 'voidptr', mod: 'builtin') // 31
}

t
dotdot0 marked this conversation as resolved.
Show resolved Hide resolved
@[inline]
pub fn (t &TypeSymbol) is_pointer() bool {
return t.kind in [.byteptr, .charptr, .voidptr]
Expand Down
Loading