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.
13 use std::fmt::{Debug, Formatter, Error};
14 use std::hash::{Hash, Hasher};
17 use self::rustc::middle::def_id::DefId;
18 use self::rustc::middle::infer::InferCtxt;
19 use self::rustc::middle::region::CodeExtent;
20 use self::rustc::middle::subst::{self, Subst, Substs};
21 use self::rustc::middle::ty::{self, Ty};
22 use self::rustc_front::hir;
23 use self::syntax::ast;
24 use self::syntax::codemap::Span;
25 use self::syntax::parse::token::{self, special_idents, InternedString};
28 extern crate rustc_front;
31 #[derive(Copy, Clone)]
32 pub struct Cx<'a,'tcx:'a> {
33 pub tcx: &'a ty::ctxt<'tcx>,
34 pub infcx: &'a InferCtxt<'a,'tcx>,
37 impl<'a,'tcx> Cx<'a,'tcx> {
38 pub fn new(infcx: &'a InferCtxt<'a,'tcx>) -> Cx<'a,'tcx> {
39 Cx { tcx: infcx.tcx, infcx: infcx }
43 pub use self::pattern::PatNode;
45 impl<'a,'tcx:'a> Hair for Cx<'a, 'tcx> {
46 type VarId = ast::NodeId;
48 type AdtDef = ty::AdtDef<'tcx>;
49 type Name = ast::Name;
50 type InternedString = InternedString;
51 type Bytes = Rc<Vec<u8>>;
53 type Projection = ty::ProjectionTy<'tcx>;
54 type Substs = &'tcx subst::Substs<'tcx>;
55 type ClosureSubsts = &'tcx ty::ClosureSubsts<'tcx>;
57 type Region = ty::Region;
58 type CodeExtent = CodeExtent;
59 type Pattern = PatNode<'tcx>;
60 type Expr = &'tcx hir::Expr;
61 type Stmt = &'tcx hir::Stmt;
62 type Block = &'tcx hir::Block;
63 type InlineAsm = &'tcx hir::InlineAsm;
65 fn unit_ty(&mut self) -> Ty<'tcx> {
69 fn usize_ty(&mut self) -> Ty<'tcx> {
73 fn bool_ty(&mut self) -> Ty<'tcx> {
77 fn partial_eq(&mut self, ty: Ty<'tcx>) -> ItemRef<Self> {
78 let eq_def_id = self.tcx.lang_items.eq_trait().unwrap();
79 self.cmp_method_ref(eq_def_id, "eq", ty)
82 fn partial_le(&mut self, ty: Ty<'tcx>) -> ItemRef<Self> {
83 let ord_def_id = self.tcx.lang_items.ord_trait().unwrap();
84 self.cmp_method_ref(ord_def_id, "le", ty)
87 fn num_variants(&mut self, adt_def: ty::AdtDef<'tcx>) -> usize {
88 adt_def.variants.len()
91 fn fields(&mut self, adt_def: ty::AdtDef<'tcx>, variant_index: usize) -> Vec<Field<Self>> {
92 adt_def.variants[variant_index]
96 .map(|(index, field)| {
97 if field.name == special_idents::unnamed_field.name {
100 Field::Named(field.name)
106 fn needs_drop(&mut self, ty: Ty<'tcx>, span: Self::Span) -> bool {
107 if self.infcx.type_moves_by_default(ty, span) {
108 // FIXME(#21859) we should do an add'l check here to determine if
109 // any dtor will execute, but the relevant fn
110 // (`type_needs_drop`) is currently factored into
111 // `librustc_trans`, so we can't easily do so.
114 // if type implements Copy, cannot require drop
119 fn span_bug(&mut self, span: Self::Span, message: &str) -> ! {
120 self.tcx.sess.span_bug(span, message)
124 impl<'a,'tcx:'a> Cx<'a,'tcx> {
125 fn cmp_method_ref(&mut self,
129 -> ItemRef<Cx<'a,'tcx>> {
130 let method_name = token::intern(method_name);
131 let substs = Substs::new_trait(vec![arg_ty], vec![], arg_ty);
132 for trait_item in self.tcx.trait_items(trait_def_id).iter() {
134 ty::ImplOrTraitItem::MethodTraitItem(ref method) => {
135 if method.name == method_name {
136 let method_ty = self.tcx.lookup_item_type(method.def_id);
137 let method_ty = method_ty.ty.subst(self.tcx, &substs);
140 def_id: method.def_id,
141 substs: self.tcx.mk_substs(substs),
145 ty::ImplOrTraitItem::ConstTraitItem(..) |
146 ty::ImplOrTraitItem::TypeTraitItem(..) => {
152 &format!("found no method `{}` in `{:?}`", method_name, trait_def_id));
156 // We only need this impl so that we do deriving for things that are
157 // defined relative to the `Hair` trait. See `Hair` trait for more
159 impl<'a,'tcx> PartialEq for Cx<'a,'tcx> {
160 fn eq(&self, _: &Cx<'a,'tcx>) -> bool {
161 panic!("Cx should never ACTUALLY be compared for equality")
165 impl<'a,'tcx> Eq for Cx<'a,'tcx> { }
167 impl<'a,'tcx> Hash for Cx<'a,'tcx> {
168 fn hash<H: Hasher>(&self, _: &mut H) {
169 panic!("Cx should never ACTUALLY be hashed")
173 impl<'a,'tcx> Debug for Cx<'a,'tcx> {
174 fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {