1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: unsafe nil

This commit is contained in:
Alexander Medvednikov 2022-07-05 22:59:54 +03:00
parent 9b1a616b13
commit 5f78647137
9 changed files with 34 additions and 3 deletions

View File

@ -42,6 +42,7 @@ pub type Expr = AnonFn
| LockExpr
| MapInit
| MatchExpr
| Nil
| NodeError
| None
| OffsetOf
@ -240,6 +241,11 @@ pub:
pos token.Pos
}
pub struct Nil {
pub:
pos token.Pos
}
pub enum GenericKindField {
unknown
name
@ -1774,7 +1780,7 @@ pub fn (expr Expr) pos() token.Pos {
EnumVal, DumpExpr, FloatLiteral, GoExpr, Ident, IfExpr, IntegerLiteral, IsRefType, Likely,
LockExpr, MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr,
RangeExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral,
StructInit, TypeNode, TypeOf, UnsafeExpr, ComptimeType {
StructInit, TypeNode, TypeOf, UnsafeExpr, ComptimeType, Nil {
return expr.pos
}
IndexExpr {

View File

@ -387,6 +387,9 @@ pub fn (x Expr) str() string {
}
return 'map{ ${pairs.join(' ')} }'
}
Nil {
return 'nil'
}
ParExpr {
return '($x.expr)'
}

View File

@ -442,13 +442,14 @@ pub const (
int_literal_type_idx = 27
thread_type_idx = 28
error_type_idx = 29
nil_type_idx = 30
// u8_type_idx = 30
)
// Note: builtin_type_names must be in the same order as the idx consts above
pub const builtin_type_names = ['void', 'voidptr', 'byteptr', 'charptr', 'i8', 'i16', 'int', 'i64',
'isize', 'u8', 'u16', 'u32', 'u64', 'usize', 'f32', 'f64', 'char', 'bool', 'none', 'string',
'rune', 'array', 'map', 'chan', 'any', 'float_literal', 'int_literal', 'thread', 'Error', 'u8']
'rune', 'array', 'map', 'chan', 'any', 'float_literal', 'int_literal', 'thread', 'Error', 'nil']
pub const builtin_type_names_matcher = build_builtin_type_names_matcher()
@ -509,6 +510,7 @@ pub const (
byteptr_types = new_byteptr_types()
voidptr_types = new_voidptr_types()
cptr_types = merge_types(voidptr_types, byteptr_types, charptr_types)
nil_type = new_type(nil_type_idx)
)
fn new_charptr_types() []Type {
@ -809,7 +811,7 @@ pub fn (mut t Table) register_builtin_type_symbols() {
}
)
t.register_sym(kind: .interface_, name: 'IError', cname: 'IError', mod: 'builtin')
t.register_sym(kind: .u8, name: 'zu8', cname: 'zu8', mod: 'builtin')
t.register_sym(kind: .voidptr, name: 'nil', cname: 'nil', mod: 'builtin')
}
[inline]

View File

@ -2213,6 +2213,12 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
ast.MatchExpr {
return c.match_expr(mut node)
}
ast.Nil {
if !c.inside_unsafe {
c.error('`nil` is only allowed in `unsafe` code', node.pos)
}
return ast.nil_type
}
ast.PostfixExpr {
return c.postfix_expr(mut node)
}

View File

@ -622,6 +622,9 @@ pub fn (mut f Fmt) expr(node_ ast.Expr) {
ast.None {
f.write('none')
}
ast.Nil {
f.write('nil')
}
ast.OffsetOf {
f.offset_of(node)
}

View File

@ -3020,6 +3020,9 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.match_expr(node)
}
ast.NodeError {}
ast.Nil {
g.write('((void*)0)')
}
ast.None {
g.write('_const_none__')
}

View File

@ -353,6 +353,7 @@ fn (mut w Walker) expr(node_ ast.Expr) {
}
}
ast.None {}
ast.Nil{}
ast.ParExpr {
w.expr(node.expr)
}

View File

@ -132,6 +132,10 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr {
.key_select {
node = p.select_expr()
}
.key_nil {
node = ast.Nil{pos:p.tok.pos()}
p.next()
}
.number {
node = p.parse_number_literal()
}

View File

@ -108,6 +108,7 @@ pub enum Kind {
key_match
key_module
key_mut
key_nil
key_shared
key_lock
key_rlock
@ -313,6 +314,7 @@ fn build_token_str() []string {
s[Kind.key_match] = 'match'
s[Kind.key_select] = 'select'
s[Kind.key_none] = 'none'
s[Kind.key_nil] = 'nil'
s[Kind.key_offsetof] = '__offsetof'
s[Kind.key_is] = 'is'
// The following kinds are not for tokens returned by the V scanner.
@ -617,6 +619,7 @@ pub fn kind_to_string(k Kind) string {
.key_unsafe { 'key_unsafe' }
.keyword_end { 'keyword_end' }
._end_ { '_end_' }
.key_nil { 'key_nil' }
}
}