Skip to content

Commit

Permalink
ast: add missing docstrings for vlib/v/ast/comptime_const_values.v fu…
Browse files Browse the repository at this point in the history
…nctions (#21219)
  • Loading branch information
dotdot0 committed Apr 9, 2024
1 parent 82f283f commit 294f7e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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 `ComptTimeConstValue` as `i8` type.
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 `ComptTimeConstValue` 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 `ComptTimeConstValue` 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 `ComptTimeConstValue` 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 `ComptTimeConstValue` 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 `ComptTimeConstValue` 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 `ComptTimeConstValue` 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 `ComptTimeConstValue` 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 `ComptTimeConstValue` 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 `ComptTimeConstValue` 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 `ComptTimeConstValue` as `f32` type.
pub fn (val ComptTimeConstValue) f32() ?f32 {
x := val.f64()?
return f32(x)
}

// f64 tries to return a `ComptTimeConstValue` 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 `ComptTimeConstValue` as `string` type.
pub fn (val ComptTimeConstValue) string() ?string {
match val {
i8 {
Expand Down
3 changes: 2 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

0 comments on commit 294f7e4

Please sign in to comment.