deriving: factor out discriminant_value construction
This commit is contained in:
parent
dd5972ee35
commit
1e67d8a570
3 changed files with 33 additions and 37 deletions
|
@ -209,6 +209,8 @@ use syntax::ptr::P;
|
||||||
|
|
||||||
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};
|
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};
|
||||||
|
|
||||||
|
use deriving;
|
||||||
|
|
||||||
pub mod ty;
|
pub mod ty;
|
||||||
|
|
||||||
pub struct TraitDef<'a> {
|
pub struct TraitDef<'a> {
|
||||||
|
@ -381,22 +383,6 @@ fn find_type_parameters(ty: &ast::Ty, ty_param_names: &[ast::Name]) -> Vec<P<ast
|
||||||
visitor.types
|
visitor.types
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replacement for expr_unreachable which generates intrinsics::unreachable()
|
|
||||||
/// instead of unreachable!()
|
|
||||||
fn expr_unreachable_intrinsic(cx: &ExtCtxt, sp: Span) -> P<Expr> {
|
|
||||||
let path = cx.std_path(&["intrinsics", "unreachable"]);
|
|
||||||
let call = cx.expr_call_global(
|
|
||||||
sp, path, vec![]);
|
|
||||||
let unreachable = cx.expr_block(P(ast::Block {
|
|
||||||
stmts: vec![],
|
|
||||||
expr: Some(call),
|
|
||||||
id: ast::DUMMY_NODE_ID,
|
|
||||||
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
|
|
||||||
span: sp }));
|
|
||||||
|
|
||||||
unreachable
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> TraitDef<'a> {
|
impl<'a> TraitDef<'a> {
|
||||||
pub fn expand(&self,
|
pub fn expand(&self,
|
||||||
cx: &mut ExtCtxt,
|
cx: &mut ExtCtxt,
|
||||||
|
@ -1279,15 +1265,11 @@ impl<'a> MethodDef<'a> {
|
||||||
|
|
||||||
let mut first_ident = None;
|
let mut first_ident = None;
|
||||||
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {
|
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {
|
||||||
let path = cx.std_path(&["intrinsics", "discriminant_value"]);
|
let self_addr = cx.expr_addr_of(sp, self_arg.clone());
|
||||||
let call = cx.expr_call_global(
|
let variant_value = deriving::call_intrinsic(cx,
|
||||||
sp, path, vec![cx.expr_addr_of(sp, self_arg.clone())]);
|
sp,
|
||||||
let variant_value = cx.expr_block(P(ast::Block {
|
"discriminant_value",
|
||||||
stmts: vec![],
|
vec![self_addr]);
|
||||||
expr: Some(call),
|
|
||||||
id: ast::DUMMY_NODE_ID,
|
|
||||||
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
|
|
||||||
span: sp }));
|
|
||||||
|
|
||||||
let target_ty = cx.ty_ident(sp, cx.ident_of(target_type_name));
|
let target_ty = cx.ty_ident(sp, cx.ident_of(target_type_name));
|
||||||
let variant_disr = cx.expr_cast(sp, variant_value, target_ty);
|
let variant_disr = cx.expr_cast(sp, variant_value, target_ty);
|
||||||
|
@ -1315,7 +1297,9 @@ impl<'a> MethodDef<'a> {
|
||||||
//Since we know that all the arguments will match if we reach the match expression we
|
//Since we know that all the arguments will match if we reach the match expression we
|
||||||
//add the unreachable intrinsics as the result of the catch all which should help llvm
|
//add the unreachable intrinsics as the result of the catch all which should help llvm
|
||||||
//in optimizing it
|
//in optimizing it
|
||||||
match_arms.push(cx.arm(sp, vec![cx.pat_wild(sp)], expr_unreachable_intrinsic(cx, sp)));
|
match_arms.push(cx.arm(sp,
|
||||||
|
vec![cx.pat_wild(sp)],
|
||||||
|
deriving::call_intrinsic(cx, sp, "unreachable", vec![])));
|
||||||
|
|
||||||
// Final wrinkle: the self_args are expressions that deref
|
// Final wrinkle: the self_args are expressions that deref
|
||||||
// down to desired l-values, but we cannot actually deref
|
// down to desired l-values, but we cannot actually deref
|
||||||
|
@ -1391,7 +1375,7 @@ impl<'a> MethodDef<'a> {
|
||||||
// derive Debug on such a type could here generate code
|
// derive Debug on such a type could here generate code
|
||||||
// that needs the feature gate enabled.)
|
// that needs the feature gate enabled.)
|
||||||
|
|
||||||
expr_unreachable_intrinsic(cx, sp)
|
deriving::call_intrinsic(cx, sp, "unreachable", vec![])
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ use deriving;
|
||||||
use deriving::generic::*;
|
use deriving::generic::*;
|
||||||
use deriving::generic::ty::*;
|
use deriving::generic::ty::*;
|
||||||
|
|
||||||
use syntax::ast::{self, MetaItem, Expr, Mutability};
|
use syntax::ast::{MetaItem, Expr, Mutability};
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::{ExtCtxt, Annotatable};
|
use syntax::ext::base::{ExtCtxt, Annotatable};
|
||||||
use syntax::ext::build::AstBuilder;
|
use syntax::ext::build::AstBuilder;
|
||||||
|
@ -82,15 +82,10 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
|
||||||
let fields = match *substr.fields {
|
let fields = match *substr.fields {
|
||||||
Struct(_, ref fs) => fs,
|
Struct(_, ref fs) => fs,
|
||||||
EnumMatching(_, _, ref fs) => {
|
EnumMatching(_, _, ref fs) => {
|
||||||
let path = cx.std_path(&["intrinsics", "discriminant_value"]);
|
let variant_value = deriving::call_intrinsic(cx,
|
||||||
let call = cx.expr_call_global(
|
trait_span,
|
||||||
trait_span, path, vec![cx.expr_self(trait_span)]);
|
"discriminant_value",
|
||||||
let variant_value = cx.expr_block(P(ast::Block {
|
vec![cx.expr_self(trait_span)]);
|
||||||
stmts: vec![],
|
|
||||||
expr: Some(call),
|
|
||||||
id: ast::DUMMY_NODE_ID,
|
|
||||||
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
|
|
||||||
span: trait_span }));
|
|
||||||
|
|
||||||
stmts.push(call_hash(trait_span, variant_value));
|
stmts.push(call_hash(trait_span, variant_value));
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ use syntax::ext::build::AstBuilder;
|
||||||
use syntax::feature_gate;
|
use syntax::feature_gate;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::parse::token::{intern, intern_and_get_ident};
|
use syntax::parse::token::{intern, intern_and_get_ident};
|
||||||
|
use syntax::ptr::P;
|
||||||
|
|
||||||
macro_rules! pathvec {
|
macro_rules! pathvec {
|
||||||
($($x:ident)::+) => (
|
($($x:ident)::+) => (
|
||||||
|
@ -271,3 +272,19 @@ fn hygienic_type_parameter(item: &Annotatable, base: &str) -> String {
|
||||||
typaram
|
typaram
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructs an expression that calls an intrinsic
|
||||||
|
fn call_intrinsic(cx: &ExtCtxt,
|
||||||
|
span: Span,
|
||||||
|
intrinsic: &str,
|
||||||
|
args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
||||||
|
let path = cx.std_path(&["intrinsics", intrinsic]);
|
||||||
|
let call = cx.expr_call_global(span, path, args);
|
||||||
|
|
||||||
|
cx.expr_block(P(ast::Block {
|
||||||
|
stmts: vec![],
|
||||||
|
expr: Some(call),
|
||||||
|
id: ast::DUMMY_NODE_ID,
|
||||||
|
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
|
||||||
|
span: span }))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue