Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
afb8cba5
R
Rust
项目概览
int
/
Rust
接近 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
afb8cba5
编写于
11月 28, 2018
作者:
O
Oliver Scherer
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move hir::Lit -> ty::Const conversion into its own file
上级
40412dc0
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
106 addition
and
101 deletion
+106
-101
src/librustc_mir/hair/constant.rs
src/librustc_mir/hair/constant.rs
+102
-0
src/librustc_mir/hair/cx/mod.rs
src/librustc_mir/hair/cx/mod.rs
+1
-1
src/librustc_mir/hair/mod.rs
src/librustc_mir/hair/mod.rs
+1
-0
src/librustc_mir/hair/pattern/mod.rs
src/librustc_mir/hair/pattern/mod.rs
+2
-100
未找到文件。
src/librustc_mir/hair/constant.rs
0 → 100644
浏览文件 @
afb8cba5
use
syntax
::
ast
;
use
rustc
::
ty
::{
self
,
Ty
,
TyCtxt
,
ParamEnv
};
use
syntax_pos
::
symbol
::
Symbol
;
use
rustc
::
mir
::
interpret
::{
ConstValue
,
Scalar
};
#[derive(PartialEq)]
crate
enum
LitToConstError
{
UnparseableFloat
,
Reported
,
}
crate
fn
lit_to_const
<
'a
,
'gcx
,
'tcx
>
(
lit
:
&
'tcx
ast
::
LitKind
,
tcx
:
TyCtxt
<
'a
,
'gcx
,
'tcx
>
,
ty
:
Ty
<
'tcx
>
,
neg
:
bool
,
)
->
Result
<&
'tcx
ty
::
Const
<
'tcx
>
,
LitToConstError
>
{
use
syntax
::
ast
::
*
;
let
trunc
=
|
n
|
{
let
param_ty
=
ParamEnv
::
reveal_all
()
.and
(
tcx
.lift_to_global
(
&
ty
)
.unwrap
());
let
width
=
tcx
.layout_of
(
param_ty
)
.map_err
(|
_
|
LitToConstError
::
Reported
)
?
.size
;
trace!
(
"trunc {} with size {} and shift {}"
,
n
,
width
.bits
(),
128
-
width
.bits
());
let
shift
=
128
-
width
.bits
();
let
result
=
(
n
<<
shift
)
>>
shift
;
trace!
(
"trunc result: {}"
,
result
);
Ok
(
ConstValue
::
Scalar
(
Scalar
::
Bits
{
bits
:
result
,
size
:
width
.bytes
()
as
u8
,
}))
};
use
rustc
::
mir
::
interpret
::
*
;
let
lit
=
match
*
lit
{
LitKind
::
Str
(
ref
s
,
_
)
=>
{
let
s
=
s
.as_str
();
let
id
=
tcx
.allocate_bytes
(
s
.as_bytes
());
ConstValue
::
new_slice
(
Scalar
::
Ptr
(
id
.into
()),
s
.len
()
as
u64
,
&
tcx
)
},
LitKind
::
ByteStr
(
ref
data
)
=>
{
let
id
=
tcx
.allocate_bytes
(
data
);
ConstValue
::
Scalar
(
Scalar
::
Ptr
(
id
.into
()))
},
LitKind
::
Byte
(
n
)
=>
ConstValue
::
Scalar
(
Scalar
::
Bits
{
bits
:
n
as
u128
,
size
:
1
,
}),
LitKind
::
Int
(
n
,
_
)
if
neg
=>
{
let
n
=
n
as
i128
;
let
n
=
n
.overflowing_neg
()
.0
;
trunc
(
n
as
u128
)
?
},
LitKind
::
Int
(
n
,
_
)
=>
trunc
(
n
)
?
,
LitKind
::
Float
(
n
,
fty
)
=>
{
parse_float
(
n
,
fty
,
neg
)
.map_err
(|
_
|
LitToConstError
::
UnparseableFloat
)
?
}
LitKind
::
FloatUnsuffixed
(
n
)
=>
{
let
fty
=
match
ty
.sty
{
ty
::
Float
(
fty
)
=>
fty
,
_
=>
bug!
()
};
parse_float
(
n
,
fty
,
neg
)
.map_err
(|
_
|
LitToConstError
::
UnparseableFloat
)
?
}
LitKind
::
Bool
(
b
)
=>
ConstValue
::
Scalar
(
Scalar
::
from_bool
(
b
)),
LitKind
::
Char
(
c
)
=>
ConstValue
::
Scalar
(
Scalar
::
from_char
(
c
)),
};
Ok
(
ty
::
Const
::
from_const_value
(
tcx
,
lit
,
ty
))
}
fn
parse_float
<
'tcx
>
(
num
:
Symbol
,
fty
:
ast
::
FloatTy
,
neg
:
bool
,
)
->
Result
<
ConstValue
<
'tcx
>
,
()
>
{
let
num
=
num
.as_str
();
use
rustc_apfloat
::
ieee
::{
Single
,
Double
};
use
rustc_apfloat
::
Float
;
let
(
bits
,
size
)
=
match
fty
{
ast
::
FloatTy
::
F32
=>
{
num
.parse
::
<
f32
>
()
.map_err
(|
_
|
())
?
;
let
mut
f
=
num
.parse
::
<
Single
>
()
.unwrap_or_else
(|
e
|
{
panic!
(
"apfloat::ieee::Single failed to parse `{}`: {:?}"
,
num
,
e
)
});
if
neg
{
f
=
-
f
;
}
(
f
.to_bits
(),
4
)
}
ast
::
FloatTy
::
F64
=>
{
num
.parse
::
<
f64
>
()
.map_err
(|
_
|
())
?
;
let
mut
f
=
num
.parse
::
<
Double
>
()
.unwrap_or_else
(|
e
|
{
panic!
(
"apfloat::ieee::Single failed to parse `{}`: {:?}"
,
num
,
e
)
});
if
neg
{
f
=
-
f
;
}
(
f
.to_bits
(),
8
)
}
};
Ok
(
ConstValue
::
Scalar
(
Scalar
::
Bits
{
bits
,
size
}))
}
src/librustc_mir/hair/cx/mod.rs
浏览文件 @
afb8cba5
...
...
@@ -31,7 +31,7 @@
use
syntax
::
symbol
::
Symbol
;
use
rustc
::
hir
;
use
rustc_data_structures
::
sync
::
Lrc
;
use
hair
::
pattern
::{
lit_to_const
,
LitToConstError
};
use
hair
::
constant
::{
lit_to_const
,
LitToConstError
};
#[derive(Clone)]
pub
struct
Cx
<
'a
,
'gcx
:
'a
+
'tcx
,
'tcx
:
'a
>
{
...
...
src/librustc_mir/hair/mod.rs
浏览文件 @
afb8cba5
...
...
@@ -26,6 +26,7 @@
use
self
::
cx
::
Cx
;
pub
mod
cx
;
mod
constant
;
pub
mod
pattern
;
pub
use
self
::
pattern
::{
BindingMode
,
Pattern
,
PatternKind
,
FieldPattern
};
...
...
src/librustc_mir/hair/pattern/mod.rs
浏览文件 @
afb8cba5
...
...
@@ -19,11 +19,12 @@
use
const_eval
::{
const_field
,
const_variant_index
};
use
hair
::
util
::
UserAnnotatedTyHelpers
;
use
hair
::
constant
::
*
;
use
rustc
::
mir
::{
fmt_const_val
,
Field
,
BorrowKind
,
Mutability
};
use
rustc
::
mir
::{
ProjectionElem
,
UserTypeAnnotation
,
UserTypeProjection
,
UserTypeProjections
};
use
rustc
::
mir
::
interpret
::{
Scalar
,
GlobalId
,
ConstValue
,
sign_extend
};
use
rustc
::
ty
::{
self
,
Region
,
TyCtxt
,
AdtDef
,
Ty
,
ParamEnv
};
use
rustc
::
ty
::{
self
,
Region
,
TyCtxt
,
AdtDef
,
Ty
};
use
rustc
::
ty
::
subst
::{
Substs
,
Kind
};
use
rustc
::
ty
::
layout
::
VariantIdx
;
use
rustc
::
hir
::{
self
,
PatKind
,
RangeEnd
};
...
...
@@ -37,7 +38,6 @@
use
syntax
::
ast
;
use
syntax
::
ptr
::
P
;
use
syntax_pos
::
Span
;
use
syntax_pos
::
symbol
::
Symbol
;
#[derive(Clone,
Debug)]
pub
enum
PatternError
{
...
...
@@ -1292,101 +1292,3 @@ pub fn compare_const_vals<'a, 'tcx>(
fallback
()
}
#[derive(PartialEq)]
pub
enum
LitToConstError
{
UnparseableFloat
,
Reported
,
}
pub
fn
lit_to_const
<
'a
,
'gcx
,
'tcx
>
(
lit
:
&
'tcx
ast
::
LitKind
,
tcx
:
TyCtxt
<
'a
,
'gcx
,
'tcx
>
,
ty
:
Ty
<
'tcx
>
,
neg
:
bool
,
)
->
Result
<&
'tcx
ty
::
Const
<
'tcx
>
,
LitToConstError
>
{
use
syntax
::
ast
::
*
;
let
trunc
=
|
n
|
{
let
param_ty
=
ParamEnv
::
reveal_all
()
.and
(
tcx
.lift_to_global
(
&
ty
)
.unwrap
());
let
width
=
tcx
.layout_of
(
param_ty
)
.map_err
(|
_
|
LitToConstError
::
Reported
)
?
.size
;
trace!
(
"trunc {} with size {} and shift {}"
,
n
,
width
.bits
(),
128
-
width
.bits
());
let
shift
=
128
-
width
.bits
();
let
result
=
(
n
<<
shift
)
>>
shift
;
trace!
(
"trunc result: {}"
,
result
);
Ok
(
ConstValue
::
Scalar
(
Scalar
::
Bits
{
bits
:
result
,
size
:
width
.bytes
()
as
u8
,
}))
};
use
rustc
::
mir
::
interpret
::
*
;
let
lit
=
match
*
lit
{
LitKind
::
Str
(
ref
s
,
_
)
=>
{
let
s
=
s
.as_str
();
let
id
=
tcx
.allocate_bytes
(
s
.as_bytes
());
ConstValue
::
new_slice
(
Scalar
::
Ptr
(
id
.into
()),
s
.len
()
as
u64
,
&
tcx
)
},
LitKind
::
ByteStr
(
ref
data
)
=>
{
let
id
=
tcx
.allocate_bytes
(
data
);
ConstValue
::
Scalar
(
Scalar
::
Ptr
(
id
.into
()))
},
LitKind
::
Byte
(
n
)
=>
ConstValue
::
Scalar
(
Scalar
::
Bits
{
bits
:
n
as
u128
,
size
:
1
,
}),
LitKind
::
Int
(
n
,
_
)
if
neg
=>
{
let
n
=
n
as
i128
;
let
n
=
n
.overflowing_neg
()
.0
;
trunc
(
n
as
u128
)
?
},
LitKind
::
Int
(
n
,
_
)
=>
trunc
(
n
)
?
,
LitKind
::
Float
(
n
,
fty
)
=>
{
parse_float
(
n
,
fty
,
neg
)
.map_err
(|
_
|
LitToConstError
::
UnparseableFloat
)
?
}
LitKind
::
FloatUnsuffixed
(
n
)
=>
{
let
fty
=
match
ty
.sty
{
ty
::
Float
(
fty
)
=>
fty
,
_
=>
bug!
()
};
parse_float
(
n
,
fty
,
neg
)
.map_err
(|
_
|
LitToConstError
::
UnparseableFloat
)
?
}
LitKind
::
Bool
(
b
)
=>
ConstValue
::
Scalar
(
Scalar
::
from_bool
(
b
)),
LitKind
::
Char
(
c
)
=>
ConstValue
::
Scalar
(
Scalar
::
from_char
(
c
)),
};
Ok
(
ty
::
Const
::
from_const_value
(
tcx
,
lit
,
ty
))
}
pub
fn
parse_float
<
'tcx
>
(
num
:
Symbol
,
fty
:
ast
::
FloatTy
,
neg
:
bool
,
)
->
Result
<
ConstValue
<
'tcx
>
,
()
>
{
let
num
=
num
.as_str
();
use
rustc_apfloat
::
ieee
::{
Single
,
Double
};
use
rustc_apfloat
::
Float
;
let
(
bits
,
size
)
=
match
fty
{
ast
::
FloatTy
::
F32
=>
{
num
.parse
::
<
f32
>
()
.map_err
(|
_
|
())
?
;
let
mut
f
=
num
.parse
::
<
Single
>
()
.unwrap_or_else
(|
e
|
{
panic!
(
"apfloat::ieee::Single failed to parse `{}`: {:?}"
,
num
,
e
)
});
if
neg
{
f
=
-
f
;
}
(
f
.to_bits
(),
4
)
}
ast
::
FloatTy
::
F64
=>
{
num
.parse
::
<
f64
>
()
.map_err
(|
_
|
())
?
;
let
mut
f
=
num
.parse
::
<
Double
>
()
.unwrap_or_else
(|
e
|
{
panic!
(
"apfloat::ieee::Single failed to parse `{}`: {:?}"
,
num
,
e
)
});
if
neg
{
f
=
-
f
;
}
(
f
.to_bits
(),
8
)
}
};
Ok
(
ConstValue
::
Scalar
(
Scalar
::
Bits
{
bits
,
size
}))
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录