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

compiler: implement typeof(x)

This commit is contained in:
Delyan Angelov
2020-02-24 22:45:47 +02:00
committed by GitHub
parent 794ee6fc9d
commit b17ade1257
7 changed files with 124 additions and 6 deletions

View File

@ -627,7 +627,21 @@ fn (p mut Parser) cast(typ string) {
if expr_typ == 'bool' {
p.error('cannot cast `bool` to `$typ`')
}
p.cgen.set_placeholder(pos, '($typ)(')
if typ != expr_typ && typ in p.table.sum_types {
T := p.table.find_type(typ)
if expr_typ in T.ctype_names {
// There is no need for a cast here, since it was already done
// in p.bool_expression, SUM TYPE CAST2 . Besides, doubling the
// cast here causes MSVC to complain with:
// error C2440: 'type cast': cannot convert from 'ExprType' to 'ExprType'
p.cgen.set_placeholder(pos, '(')
}else{
p.warn('only $T.ctype_names can be casted to `$typ`')
p.error('cannot cast `$expr_typ` to `$typ`')
}
}else{
p.cgen.set_placeholder(pos, '($typ)(')
}
}
p.check(.rpar)
p.gen(')')