|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! See docs in build/expr/mod.rs |
| 12 | +
|
| 13 | +use rustc_data_structures::fnv::FnvHashMap; |
| 14 | + |
| 15 | +use build::{Builder}; |
| 16 | +use hair::*; |
| 17 | +use repr::*; |
| 18 | + |
| 19 | +impl<H:Hair> Builder<H> { |
| 20 | + /// Compile `expr`, yielding a compile-time constant. Assumes that |
| 21 | + /// `expr` is a valid compile-time constant! |
| 22 | + pub fn as_constant<M>(&mut self, expr: M) -> Constant<H> |
| 23 | + where M: Mirror<H, Output=Expr<H>> |
| 24 | + { |
| 25 | + let expr = self.hir.mirror(expr); |
| 26 | + self.expr_as_constant(expr) |
| 27 | + } |
| 28 | + |
| 29 | + fn expr_as_constant(&mut self, expr: Expr<H>) -> Constant<H> { |
| 30 | + let this = self; |
| 31 | + let Expr { ty: _, temp_lifetime: _, span, kind } = expr; |
| 32 | + let kind = match kind { |
| 33 | + ExprKind::Scope { extent: _, value } => { |
| 34 | + return this.as_constant(value); |
| 35 | + } |
| 36 | + ExprKind::Paren { arg } => { |
| 37 | + return this.as_constant(arg); |
| 38 | + } |
| 39 | + ExprKind::Literal { literal } => { |
| 40 | + ConstantKind::Literal(literal) |
| 41 | + } |
| 42 | + ExprKind::Vec { fields } => { |
| 43 | + let fields = this.as_constants(fields); |
| 44 | + ConstantKind::Aggregate(AggregateKind::Vec, fields) |
| 45 | + } |
| 46 | + ExprKind::Tuple { fields } => { |
| 47 | + let fields = this.as_constants(fields); |
| 48 | + ConstantKind::Aggregate(AggregateKind::Tuple, fields) |
| 49 | + } |
| 50 | + ExprKind::Adt { adt_def, variant_index, substs, fields, base: None } => { |
| 51 | + let field_names = this.hir.fields(adt_def, variant_index); |
| 52 | + let fields = this.named_field_constants(field_names, fields); |
| 53 | + ConstantKind::Aggregate(AggregateKind::Adt(adt_def, variant_index, substs), fields) |
| 54 | + } |
| 55 | + ExprKind::Repeat { value, count } => { |
| 56 | + let value = Box::new(this.as_constant(value)); |
| 57 | + let count = Box::new(this.as_constant(count)); |
| 58 | + ConstantKind::Repeat(value, count) |
| 59 | + } |
| 60 | + ExprKind::Binary { op, lhs, rhs } => { |
| 61 | + let lhs = Box::new(this.as_constant(lhs)); |
| 62 | + let rhs = Box::new(this.as_constant(rhs)); |
| 63 | + ConstantKind::BinaryOp(op, lhs, rhs) |
| 64 | + } |
| 65 | + ExprKind::Unary { op, arg } => { |
| 66 | + let arg = Box::new(this.as_constant(arg)); |
| 67 | + ConstantKind::UnaryOp(op, arg) |
| 68 | + } |
| 69 | + ExprKind::Field { lhs, name } => { |
| 70 | + let lhs = this.as_constant(lhs); |
| 71 | + ConstantKind::Projection( |
| 72 | + Box::new(ConstantProjection { |
| 73 | + base: lhs, |
| 74 | + elem: ProjectionElem::Field(name), |
| 75 | + })) |
| 76 | + } |
| 77 | + ExprKind::Deref { arg } => { |
| 78 | + let arg = this.as_constant(arg); |
| 79 | + ConstantKind::Projection( |
| 80 | + Box::new(ConstantProjection { |
| 81 | + base: arg, |
| 82 | + elem: ProjectionElem::Deref, |
| 83 | + })) |
| 84 | + } |
| 85 | + ExprKind::Call { fun, args } => { |
| 86 | + let fun = this.as_constant(fun); |
| 87 | + let args = this.as_constants(args); |
| 88 | + ConstantKind::Call(Box::new(fun), args) |
| 89 | + } |
| 90 | + _ => { |
| 91 | + this.hir.span_bug( |
| 92 | + span, |
| 93 | + &format!("expression is not a valid constant {:?}", kind)); |
| 94 | + } |
| 95 | + }; |
| 96 | + Constant { span: span, kind: kind } |
| 97 | + } |
| 98 | + |
| 99 | + fn as_constants(&mut self, |
| 100 | + exprs: Vec<ExprRef<H>>) |
| 101 | + -> Vec<Constant<H>> |
| 102 | + { |
| 103 | + exprs.into_iter().map(|expr| self.as_constant(expr)).collect() |
| 104 | + } |
| 105 | + |
| 106 | + fn named_field_constants(&mut self, |
| 107 | + field_names: Vec<Field<H>>, |
| 108 | + field_exprs: Vec<FieldExprRef<H>>) |
| 109 | + -> Vec<Constant<H>> |
| 110 | + { |
| 111 | + let fields_map: FnvHashMap<_, _> = |
| 112 | + field_exprs.into_iter() |
| 113 | + .map(|f| (f.name, self.as_constant(f.expr))) |
| 114 | + .collect(); |
| 115 | + |
| 116 | + let fields: Vec<_> = |
| 117 | + field_names.into_iter() |
| 118 | + .map(|n| fields_map[&n].clone()) |
| 119 | + .collect(); |
| 120 | + |
| 121 | + fields |
| 122 | + } |
| 123 | +} |
0 commit comments