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.
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.
12 * This module contains the code to convert from the wacky tcx data
13 * structures into the hair. The `builder` is generally ignorant of
14 * the tcx etc, and instead goes through the `Cx` for most of its
19 use rustc
::mir
::repr
::*;
21 use rustc
::middle
::const_eval
::{self, ConstVal}
;
22 use rustc
::middle
::infer
::InferCtxt
;
23 use rustc
::middle
::ty
::{self, Ty}
;
24 use syntax
::codemap
::Span
;
25 use syntax
::parse
::token
;
28 #[derive(Copy, Clone)]
29 pub struct Cx
<'a
, 'tcx
: 'a
> {
30 tcx
: &'a ty
::ctxt
<'tcx
>,
31 infcx
: &'a InferCtxt
<'a
, 'tcx
>,
34 impl<'a
,'tcx
> Cx
<'a
,'tcx
> {
35 pub fn new(infcx
: &'a InferCtxt
<'a
, 'tcx
>) -> Cx
<'a
, 'tcx
> {
43 impl<'a
,'tcx
:'a
> Cx
<'a
, 'tcx
> {
44 /// Normalizes `ast` into the appropriate `mirror` type.
45 pub fn mirror
<M
: Mirror
<'tcx
>>(&mut self, ast
: M
) -> M
::Output
{
49 pub fn usize_ty(&mut self) -> Ty
<'tcx
> {
53 pub fn usize_literal(&mut self, value
: usize) -> Literal
<'tcx
> {
54 Literal
::Value { value: ConstVal::Uint(value as u64) }
57 pub fn bool_ty(&mut self) -> Ty
<'tcx
> {
61 pub fn str_literal(&mut self, value
: token
::InternedString
) -> Literal
<'tcx
> {
62 Literal
::Value { value: ConstVal::Str(value) }
65 pub fn true_literal(&mut self) -> Literal
<'tcx
> {
66 Literal
::Value { value: ConstVal::Bool(true) }
69 pub fn false_literal(&mut self) -> Literal
<'tcx
> {
70 Literal
::Value { value: ConstVal::Bool(false) }
73 pub fn const_eval_literal(&mut self, e
: &hir
::Expr
) -> Literal
<'tcx
> {
74 Literal
::Value { value: const_eval::eval_const_expr(self.tcx, e) }
77 pub fn try_const_eval_literal(&mut self, e
: &hir
::Expr
) -> Option
<Literal
<'tcx
>> {
78 let hint
= const_eval
::EvalHint
::ExprTypeChecked
;
79 const_eval
::eval_const_expr_partial(self.tcx
, e
, hint
, None
)
81 .map(|v
| Literal
::Value { value: v }
)
84 pub fn num_variants(&mut self, adt_def
: ty
::AdtDef
<'tcx
>) -> usize {
85 adt_def
.variants
.len()
88 pub fn all_fields(&mut self, adt_def
: ty
::AdtDef
<'tcx
>, variant_index
: usize) -> Vec
<Field
> {
89 (0..adt_def
.variants
[variant_index
].fields
.len())
94 pub fn needs_drop(&mut self, ty
: Ty
<'tcx
>) -> bool
{
95 self.tcx
.type_needs_drop_given_env(ty
, &self.infcx
.parameter_environment
)
98 pub fn span_bug(&mut self, span
: Span
, message
: &str) -> ! {
99 self.tcx
.sess
.span_bug(span
, message
)
102 pub fn tcx(&self) -> &'a ty
::ctxt
<'tcx
> {