rust/src/libsyntax/ext/build.rs

1219 lines
45 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use abi::Abi;
2016-02-11 18:16:33 +00:00
use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
2014-05-23 15:39:26 +00:00
use attr;
use syntax_pos::{Span, DUMMY_SP, Pos};
2016-08-10 23:20:12 +00:00
use codemap::{dummy_spanned, respan, Spanned};
use ext::base::ExtCtxt;
2014-09-13 16:06:01 +00:00
use ptr::P;
use symbol::{intern_and_get_ident, keywords, InternedString};
// Transitional reexports so qquote can find the paths it is looking for
mod syntax {
pub use ext;
pub use parse;
}
pub trait AstBuilder {
// paths
fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
2013-09-02 00:50:59 +00:00
fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
fn path_all(&self, sp: Span,
global: bool,
idents: Vec<ast::Ident> ,
lifetimes: Vec<ast::Lifetime>,
types: Vec<P<ast::Ty>>,
bindings: Vec<ast::TypeBinding> )
2013-07-05 10:15:21 +00:00
-> ast::Path;
fn qpath(&self, self_type: P<ast::Ty>,
trait_path: ast::Path,
ident: ast::Ident)
-> (ast::QSelf, ast::Path);
fn qpath_all(&self, self_type: P<ast::Ty>,
trait_path: ast::Path,
ident: ast::Ident,
lifetimes: Vec<ast::Lifetime>,
types: Vec<P<ast::Ty>>,
bindings: Vec<ast::TypeBinding>)
-> (ast::QSelf, ast::Path);
// types
fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
fn ty_path(&self, ast::Path) -> P<ast::Ty>;
fn ty_sum(&self, ast::Path, ast::TyParamBounds) -> P<ast::Ty>;
fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
fn ty_rptr(&self, span: Span,
ty: P<ast::Ty>,
2013-07-05 12:33:52 +00:00
lifetime: Option<ast::Lifetime>,
mutbl: ast::Mutability) -> P<ast::Ty>;
fn ty_ptr(&self, span: Span,
ty: P<ast::Ty>,
mutbl: ast::Mutability) -> P<ast::Ty>;
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> ;
fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> ;
fn typaram(&self,
2014-04-03 00:53:57 +00:00
span: Span,
id: ast::Ident,
attrs: Vec<ast::Attribute>,
bounds: ast::TyParamBounds,
default: Option<P<ast::Ty>>) -> ast::TyParam;
fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
2013-07-05 10:15:21 +00:00
fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
fn lifetime_def(&self,
span: Span,
name: ast::Name,
attrs: Vec<ast::Attribute>,
bounds: Vec<ast::Lifetime>)
-> ast::LifetimeDef;
// statements
fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt;
fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt;
fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> ast::Stmt;
fn stmt_let_typed(&self,
sp: Span,
mutbl: bool,
2013-09-02 00:50:59 +00:00
ident: ast::Ident,
typ: P<ast::Ty>,
2014-09-13 16:06:01 +00:00
ex: P<ast::Expr>)
-> ast::Stmt;
2016-08-26 16:23:42 +00:00
fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt;
fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt;
// blocks
2016-06-23 09:51:18 +00:00
fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block>;
2014-09-13 16:06:01 +00:00
fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
// expressions
fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr>;
2014-09-13 16:06:01 +00:00
fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
2014-09-13 16:06:01 +00:00
fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
2014-09-13 16:06:01 +00:00
fn expr_self(&self, span: Span) -> P<ast::Expr>;
fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
2014-09-13 16:06:01 +00:00
lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
2015-01-17 23:33:05 +00:00
idx: usize) -> P<ast::Expr>;
2014-09-13 16:06:01 +00:00
fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
fn expr_method_call(&self, span: Span,
2014-09-13 16:06:01 +00:00
expr: P<ast::Expr>, ident: ast::Ident,
args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
fn expr_struct_ident(&self, span: Span, id: ast::Ident,
2014-09-13 16:06:01 +00:00
fields: Vec<ast::Field>) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
2014-09-13 16:06:01 +00:00
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
2015-02-21 11:56:46 +00:00
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
2014-09-13 16:06:01 +00:00
fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
2014-09-13 16:06:01 +00:00
fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
2014-09-13 16:06:01 +00:00
fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
fn expr_none(&self, sp: Span) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
fn expr_break(&self, sp: Span) -> P<ast::Expr>;
2014-09-13 16:06:01 +00:00
fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
2014-09-13 16:06:01 +00:00
fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
2014-09-13 16:06:01 +00:00
fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
2016-02-11 18:16:33 +00:00
fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat>;
2014-09-13 16:06:01 +00:00
fn pat_wild(&self, span: Span) -> P<ast::Pat>;
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
fn pat_ident_binding_mode(&self,
span: Span,
2013-09-02 00:50:59 +00:00
ident: ast::Ident,
2014-09-13 16:06:01 +00:00
bm: ast::BindingMode) -> P<ast::Pat>;
fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat>;
fn pat_tuple_struct(&self, span: Span, path: ast::Path,
subpats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
fn pat_struct(&self, span: Span, path: ast::Path,
field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat>;
2014-09-13 16:06:01 +00:00
fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
2014-09-13 16:06:01 +00:00
fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
fn pat_none(&self, span: Span) -> P<ast::Pat>;
2014-09-13 16:06:01 +00:00
fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
2014-09-13 16:06:01 +00:00
fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
fn arm_unreachable(&self, span: Span) -> ast::Arm;
2014-09-13 16:06:01 +00:00
fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
fn expr_if(&self, span: Span,
2014-09-13 16:06:01 +00:00
cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
fn lambda_fn_decl(&self,
span: Span,
fn_decl: P<ast::FnDecl>,
body: P<ast::Expr>,
fn_decl_span: Span)
-> P<ast::Expr>;
fn lambda(&self, span: Span, ids: Vec<ast::Ident>, body: P<ast::Expr>) -> P<ast::Expr>;
fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr>;
fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
2014-05-16 07:16:13 +00:00
fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
blk: Vec<ast::Stmt>) -> P<ast::Expr>;
fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr>;
fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
2014-09-13 16:06:01 +00:00
ident: ast::Ident) -> P<ast::Expr>;
// items
fn item(&self, span: Span,
name: Ident, attrs: Vec<ast::Attribute> , node: ast::ItemKind) -> P<ast::Item>;
fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
// FIXME unused self
fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
fn item_fn_poly(&self,
span: Span,
2013-09-02 00:50:59 +00:00
name: Ident,
inputs: Vec<ast::Arg> ,
output: P<ast::Ty>,
generics: Generics,
2014-09-13 16:06:01 +00:00
body: P<ast::Block>) -> P<ast::Item>;
fn item_fn(&self,
span: Span,
2013-09-02 00:50:59 +00:00
name: Ident,
inputs: Vec<ast::Arg> ,
output: P<ast::Ty>,
2014-09-13 16:06:01 +00:00
body: P<ast::Block>) -> P<ast::Item>;
fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
fn item_enum_poly(&self,
span: Span,
2013-09-02 00:50:59 +00:00
name: Ident,
enum_definition: ast::EnumDef,
2014-09-13 16:06:01 +00:00
generics: Generics) -> P<ast::Item>;
fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
fn item_struct_poly(&self,
span: Span,
2013-09-02 00:50:59 +00:00
name: Ident,
struct_def: ast::VariantData,
2014-09-13 16:06:01 +00:00
generics: Generics) -> P<ast::Item>;
fn item_struct(&self, span: Span, name: Ident, struct_def: ast::VariantData) -> P<ast::Item>;
fn item_mod(&self, span: Span, inner_span: Span,
2014-05-16 07:16:13 +00:00
name: Ident, attrs: Vec<ast::Attribute>,
items: Vec<P<ast::Item>>) -> P<ast::Item>;
fn item_static(&self,
span: Span,
name: Ident,
ty: P<ast::Ty>,
mutbl: ast::Mutability,
2014-09-13 16:06:01 +00:00
expr: P<ast::Expr>)
-> P<ast::Item>;
rustc: Add `const` globals to the language This change is an implementation of [RFC 69][rfc] which adds a third kind of global to the language, `const`. This global is most similar to what the old `static` was, and if you're unsure about what to use then you should use a `const`. The semantics of these three kinds of globals are: * A `const` does not represent a memory location, but only a value. Constants are translated as rvalues, which means that their values are directly inlined at usage location (similar to a #define in C/C++). Constant values are, well, constant, and can not be modified. Any "modification" is actually a modification to a local value on the stack rather than the actual constant itself. Almost all values are allowed inside constants, whether they have interior mutability or not. There are a few minor restrictions listed in the RFC, but they should in general not come up too often. * A `static` now always represents a memory location (unconditionally). Any references to the same `static` are actually a reference to the same memory location. Only values whose types ascribe to `Sync` are allowed in a `static`. This restriction is in place because many threads may access a `static` concurrently. Lifting this restriction (and allowing unsafe access) is a future extension not implemented at this time. * A `static mut` continues to always represent a memory location. All references to a `static mut` continue to be `unsafe`. This is a large breaking change, and many programs will need to be updated accordingly. A summary of the breaking changes is: * Statics may no longer be used in patterns. Statics now always represent a memory location, which can sometimes be modified. To fix code, repurpose the matched-on-`static` to a `const`. static FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } change this code to: const FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } * Statics may no longer refer to other statics by value. Due to statics being able to change at runtime, allowing them to reference one another could possibly lead to confusing semantics. If you are in this situation, use a constant initializer instead. Note, however, that statics may reference other statics by address, however. * Statics may no longer be used in constant expressions, such as array lengths. This is due to the same restrictions as listed above. Use a `const` instead. [breaking-change] [rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 15:17:01 +00:00
fn item_const(&self,
span: Span,
name: Ident,
ty: P<ast::Ty>,
expr: P<ast::Expr>)
-> P<ast::Item>;
fn item_ty_poly(&self,
span: Span,
2013-09-02 00:50:59 +00:00
name: Ident,
ty: P<ast::Ty>,
2014-09-13 16:06:01 +00:00
generics: Generics) -> P<ast::Item>;
fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute;
fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem;
fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem;
fn meta_list(&self,
sp: Span,
name: ast::Name,
mis: Vec<ast::NestedMetaItem> )
-> ast::MetaItem;
fn meta_name_value(&self,
sp: Span,
name: ast::Name,
value: ast::LitKind)
-> ast::MetaItem;
fn item_use(&self, sp: Span,
vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item>;
fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
fn item_use_list(&self, sp: Span, vis: ast::Visibility,
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
fn item_use_glob(&self, sp: Span,
vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
}
2013-12-25 18:10:33 +00:00
impl<'a> AstBuilder for ExtCtxt<'a> {
fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
self.path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
}
2013-09-02 00:50:59 +00:00
fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
self.path(span, vec![id])
}
fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
}
fn path_all(&self,
sp: Span,
global: bool,
mut idents: Vec<ast::Ident> ,
lifetimes: Vec<ast::Lifetime>,
types: Vec<P<ast::Ty>>,
bindings: Vec<ast::TypeBinding> )
-> ast::Path {
let last_identifier = idents.pop().unwrap();
2014-09-15 03:27:36 +00:00
let mut segments: Vec<ast::PathSegment> = idents.into_iter()
.map(|ident| {
ast::PathSegment {
identifier: ident,
parameters: ast::PathParameters::none(),
}
}).collect();
segments.push(ast::PathSegment {
identifier: last_identifier,
parameters: ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData {
lifetimes: lifetimes,
types: P::from_vec(types),
bindings: P::from_vec(bindings),
})
});
2013-07-05 10:15:21 +00:00
ast::Path {
span: sp,
global: global,
segments: segments,
}
}
/// Constructs a qualified path.
///
/// Constructs a path like `<self_type as trait_path>::ident`.
fn qpath(&self,
self_type: P<ast::Ty>,
trait_path: ast::Path,
ident: ast::Ident)
-> (ast::QSelf, ast::Path) {
self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
}
/// Constructs a qualified path.
///
/// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
fn qpath_all(&self,
self_type: P<ast::Ty>,
trait_path: ast::Path,
ident: ast::Ident,
lifetimes: Vec<ast::Lifetime>,
types: Vec<P<ast::Ty>>,
bindings: Vec<ast::TypeBinding>)
-> (ast::QSelf, ast::Path) {
let mut path = trait_path;
path.segments.push(ast::PathSegment {
identifier: ident,
parameters: ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData {
lifetimes: lifetimes,
types: P::from_vec(types),
bindings: P::from_vec(bindings),
})
});
(ast::QSelf {
ty: self_type,
position: path.segments.len() - 1
}, path)
}
fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
ast::MutTy {
ty: ty,
mutbl: mutbl
}
}
fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
P(ast::Ty {
id: ast::DUMMY_NODE_ID,
span: span,
node: ty
})
}
fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
self.ty(path.span, ast::TyKind::Path(None, path))
}
fn ty_sum(&self, path: ast::Path, bounds: ast::TyParamBounds) -> P<ast::Ty> {
self.ty(path.span,
ast::TyKind::ObjectSum(self.ty_path(path),
bounds))
}
// Might need to take bounds as an argument in the future, if you ever want
// to generate a bounded existential trait type.
2013-09-02 00:50:59 +00:00
fn ty_ident(&self, span: Span, ident: ast::Ident)
-> P<ast::Ty> {
self.ty_path(self.path_ident(span, ident))
}
fn ty_rptr(&self,
span: Span,
ty: P<ast::Ty>,
2013-07-05 12:33:52 +00:00
lifetime: Option<ast::Lifetime>,
mutbl: ast::Mutability)
-> P<ast::Ty> {
self.ty(span,
ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
}
fn ty_ptr(&self,
span: Span,
ty: P<ast::Ty>,
mutbl: ast::Mutability)
-> P<ast::Ty> {
self.ty(span,
ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
}
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
self.ty_path(
self.path_all(DUMMY_SP,
true,
self.std_path(&["option", "Option"]),
Vec::new(),
vec![ ty ],
Vec::new()))
}
fn ty_infer(&self, span: Span) -> P<ast::Ty> {
self.ty(span, ast::TyKind::Infer)
}
fn typaram(&self,
2014-04-03 00:53:57 +00:00
span: Span,
id: ast::Ident,
attrs: Vec<ast::Attribute>,
bounds: ast::TyParamBounds,
default: Option<P<ast::Ty>>) -> ast::TyParam {
ast::TyParam {
ident: id,
id: ast::DUMMY_NODE_ID,
attrs: attrs.into(),
bounds: bounds,
2014-04-03 00:53:57 +00:00
default: default,
span: span
}
}
// these are strange, and probably shouldn't be used outside of
// pipes. Specifically, the global version possible generates
// incorrect code.
fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> {
ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
}
fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> {
ty_params
.iter()
.map(|p| self.ty_path(self.path_global(DUMMY_SP, vec![p.ident])))
.collect()
}
fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
ast::TraitRef {
path: path,
ref_id: ast::DUMMY_NODE_ID,
2014-11-07 11:53:45 +00:00
}
}
fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
2014-11-07 11:53:45 +00:00
ast::PolyTraitRef {
bound_lifetimes: Vec::new(),
trait_ref: self.trait_ref(path),
span: span,
}
}
2013-07-05 10:15:21 +00:00
fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None)
}
fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
}
fn lifetime_def(&self,
span: Span,
name: ast::Name,
attrs: Vec<ast::Attribute>,
bounds: Vec<ast::Lifetime>)
-> ast::LifetimeDef {
ast::LifetimeDef {
attrs: attrs.into(),
lifetime: self.lifetime(span, name),
bounds: bounds
}
}
fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
ast::Stmt {
id: ast::DUMMY_NODE_ID,
span: expr.span,
node: ast::StmtKind::Expr(expr),
}
}
fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt {
2016-06-17 02:30:01 +00:00
ast::Stmt {
id: ast::DUMMY_NODE_ID,
span: expr.span,
node: ast::StmtKind::Semi(expr),
}
}
2014-05-16 07:16:13 +00:00
fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
ex: P<ast::Expr>) -> ast::Stmt {
let pat = if mutbl {
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
self.pat_ident_binding_mode(sp, ident, binding_mode)
} else {
self.pat_ident(sp, ident)
};
2014-09-13 16:06:01 +00:00
let local = P(ast::Local {
pat: pat,
2015-01-02 11:55:31 +00:00
ty: None,
init: Some(ex),
id: ast::DUMMY_NODE_ID,
span: sp,
attrs: ast::ThinVec::new(),
2014-09-13 16:06:01 +00:00
});
2016-06-17 02:30:01 +00:00
ast::Stmt {
id: ast::DUMMY_NODE_ID,
node: ast::StmtKind::Local(local),
span: sp,
}
}
fn stmt_let_typed(&self,
sp: Span,
mutbl: bool,
2013-09-02 00:50:59 +00:00
ident: ast::Ident,
typ: P<ast::Ty>,
2014-09-13 16:06:01 +00:00
ex: P<ast::Expr>)
-> ast::Stmt {
let pat = if mutbl {
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
self.pat_ident_binding_mode(sp, ident, binding_mode)
} else {
self.pat_ident(sp, ident)
};
2014-09-13 16:06:01 +00:00
let local = P(ast::Local {
pat: pat,
2015-01-02 11:55:31 +00:00
ty: Some(typ),
init: Some(ex),
id: ast::DUMMY_NODE_ID,
span: sp,
attrs: ast::ThinVec::new(),
2014-09-13 16:06:01 +00:00
});
ast::Stmt {
2016-06-17 02:30:01 +00:00
id: ast::DUMMY_NODE_ID,
node: ast::StmtKind::Local(local),
span: sp,
}
}
2016-08-26 16:23:42 +00:00
// Generate `let _: Type;`, usually used for type assertions.
fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
let local = P(ast::Local {
pat: self.pat_wild(span),
ty: Some(ty),
init: None,
id: ast::DUMMY_NODE_ID,
span: span,
attrs: ast::ThinVec::new(),
});
ast::Stmt {
id: ast::DUMMY_NODE_ID,
node: ast::StmtKind::Local(local),
span: span,
}
}
fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
2016-06-17 02:30:01 +00:00
ast::Stmt {
id: ast::DUMMY_NODE_ID,
node: ast::StmtKind::Item(item),
span: sp,
}
}
2014-09-13 16:06:01 +00:00
fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
self.block(expr.span, vec![ast::Stmt {
id: ast::DUMMY_NODE_ID,
2016-06-23 09:51:18 +00:00
span: expr.span,
node: ast::StmtKind::Expr(expr),
2016-06-23 09:51:18 +00:00
}])
}
fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
2016-06-23 09:51:18 +00:00
P(ast::Block {
stmts: stmts,
id: ast::DUMMY_NODE_ID,
rules: BlockCheckMode::Default,
span: span,
})
}
fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr> {
2014-09-13 16:06:01 +00:00
P(ast::Expr {
id: ast::DUMMY_NODE_ID,
node: node,
span: span,
attrs: ast::ThinVec::new(),
2014-09-13 16:06:01 +00:00
})
}
2014-09-13 16:06:01 +00:00
fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
self.expr(path.span, ast::ExprKind::Path(None, path))
}
/// Constructs a QPath expression.
fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
self.expr(span, ast::ExprKind::Path(Some(qself), path))
}
2014-09-13 16:06:01 +00:00
fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
self.expr_path(self.path_ident(span, id))
}
2014-09-13 16:06:01 +00:00
fn expr_self(&self, span: Span) -> P<ast::Expr> {
self.expr_ident(span, keywords::SelfValue.ident())
}
fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
2014-09-13 16:06:01 +00:00
lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
}
2014-09-13 16:06:01 +00:00
fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
self.expr_unary(sp, UnOp::Deref, e)
}
2014-09-13 16:06:01 +00:00
fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Unary(op, e))
}
2014-09-13 16:06:01 +00:00
fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
let field_span = Span {
lo: sp.lo - Pos::from_usize(ident.name.as_str().len()),
hi: sp.hi,
expn_id: sp.expn_id,
};
let id = Spanned { node: ident, span: field_span };
self.expr(sp, ast::ExprKind::Field(expr, id))
}
2015-01-17 23:33:05 +00:00
fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
let field_span = Span {
lo: sp.lo - Pos::from_usize(idx.to_string().len()),
hi: sp.hi,
expn_id: sp.expn_id,
};
let id = Spanned { node: idx, span: field_span };
self.expr(sp, ast::ExprKind::TupField(expr, id))
}
2014-09-13 16:06:01 +00:00
fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
}
2014-09-13 16:06:01 +00:00
fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
}
2014-09-13 16:06:01 +00:00
fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
self.expr(span, ast::ExprKind::Call(expr, args))
}
2014-05-16 07:16:13 +00:00
fn expr_call_ident(&self, span: Span, id: ast::Ident,
2014-09-13 16:06:01 +00:00
args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
}
fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
2014-09-13 16:06:01 +00:00
args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
let pathexpr = self.expr_path(self.path_global(sp, fn_path));
self.expr_call(sp, pathexpr, args)
}
fn expr_method_call(&self, span: Span,
2014-09-13 16:06:01 +00:00
expr: P<ast::Expr>,
2013-09-02 00:50:59 +00:00
ident: ast::Ident,
2014-09-13 16:06:01 +00:00
mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
let id = Spanned { node: ident, span: span };
args.insert(0, expr);
self.expr(span, ast::ExprKind::MethodCall(id, Vec::new(), args))
}
2014-09-13 16:06:01 +00:00
fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
self.expr(b.span, ast::ExprKind::Block(b))
}
2014-09-13 16:06:01 +00:00
fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
ast::Field { ident: respan(span, name), expr: e, span: span, is_shorthand: false }
}
2014-09-13 16:06:01 +00:00
fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
self.expr(span, ast::ExprKind::Struct(path, fields, None))
}
fn expr_struct_ident(&self, span: Span,
2014-09-13 16:06:01 +00:00
id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
self.expr_struct(span, self.path_ident(span, id), fields)
}
fn expr_lit(&self, sp: Span, lit: ast::LitKind) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Lit(P(respan(sp, lit))))
}
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
self.expr_lit(span, ast::LitKind::Int(i as u64, ast::LitIntType::Unsigned(ast::UintTy::Us)))
}
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
if i < 0 {
let i = (-i) as u64;
let lit_ty = ast::LitIntType::Signed(ast::IntTy::Is);
let lit = self.expr_lit(sp, ast::LitKind::Int(i, lit_ty));
self.expr_unary(sp, ast::UnOp::Neg, lit)
} else {
self.expr_lit(sp, ast::LitKind::Int(i as u64, ast::LitIntType::Signed(ast::IntTy::Is)))
}
}
2015-02-23 03:07:38 +00:00
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
self.expr_lit(sp, ast::LitKind::Int(u as u64, ast::LitIntType::Unsigned(ast::UintTy::U32)))
2015-02-23 03:07:38 +00:00
}
2014-09-13 16:06:01 +00:00
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
self.expr_lit(sp, ast::LitKind::Int(u as u64, ast::LitIntType::Unsigned(ast::UintTy::U8)))
}
2014-09-13 16:06:01 +00:00
fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
self.expr_lit(sp, ast::LitKind::Bool(value))
}
2014-09-13 16:06:01 +00:00
fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Vec(exprs))
}
2014-09-13 16:06:01 +00:00
fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
Vec::new())
}
2014-09-13 16:06:01 +00:00
fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
DST coercions and DST structs [breaking-change] 1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code. 2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible. 3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 12:20:11 +00:00
self.expr_addr_of(sp, self.expr_vec(sp, exprs))
}
2014-09-13 16:06:01 +00:00
fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
}
2014-09-13 16:06:01 +00:00
fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Cast(expr, ty))
}
2014-09-13 16:06:01 +00:00
fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
let some = self.std_path(&["option", "Option", "Some"]);
self.expr_call_global(sp, some, vec![expr])
}
2014-09-13 16:06:01 +00:00
fn expr_none(&self, sp: Span) -> P<ast::Expr> {
let none = self.std_path(&["option", "Option", "None"]);
let none = self.path_global(sp, none);
self.expr_path(none)
}
fn expr_break(&self, sp: Span) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Break(None))
}
2014-09-13 16:06:01 +00:00
fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Tup(exprs))
}
2014-09-13 16:06:01 +00:00
fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
let loc = self.codemap().lookup_char_pos(span.lo);
let expr_file = self.expr_str(span, intern_and_get_ident(&loc.file.name));
let expr_line = self.expr_u32(span, loc.line as u32);
let expr_file_line_tuple = self.expr_tuple(span, vec![expr_file, expr_line]);
let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
self.expr_call_global(
span,
rustc: Implement custom panic runtimes This commit is an implementation of [RFC 1513] which allows applications to alter the behavior of panics at compile time. A new compiler flag, `-C panic`, is added and accepts the values `unwind` or `panic`, with the default being `unwind`. This model affects how code is generated for the local crate, skipping generation of landing pads with `-C panic=abort`. [RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md Panic implementations are then provided by crates tagged with `#![panic_runtime]` and lazily required by crates with `#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic runtime must match the final product, and if the panic strategy is not `abort` then the entire DAG must have the same panic strategy. With the `-C panic=abort` strategy, users can expect a stable method to disable generation of landing pads, improving optimization in niche scenarios, decreasing compile time, and decreasing output binary size. With the `-C panic=unwind` strategy users can expect the existing ability to isolate failure in Rust code from the outside world. Organizationally, this commit dismantles the `sys_common::unwind` module in favor of some bits moving part of it to `libpanic_unwind` and the rest into the `panicking` module in libstd. The custom panic runtime support is pretty similar to the custom allocator support with the only major difference being how the panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 23:18:40 +00:00
self.std_path(&["rt", "begin_panic"]),
vec![
self.expr_str(span, msg),
expr_file_line_ptr])
}
2014-09-13 16:06:01 +00:00
fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
self.expr_fail(span,
InternedString::new(
"internal error: entered unreachable code"))
}
2014-09-13 16:06:01 +00:00
fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
let ok = self.std_path(&["result", "Result", "Ok"]);
self.expr_call_global(sp, ok, vec![expr])
}
2014-09-13 16:06:01 +00:00
fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
let err = self.std_path(&["result", "Result", "Err"]);
self.expr_call_global(sp, err, vec![expr])
}
2014-09-13 16:06:01 +00:00
fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
let ok = self.std_path(&["result", "Result", "Ok"]);
let ok_path = self.path_global(sp, ok);
let err = self.std_path(&["result", "Result", "Err"]);
let err_path = self.path_global(sp, err);
let binding_variable = self.ident_of("__try_var");
let binding_pat = self.pat_ident(sp, binding_variable);
let binding_expr = self.expr_ident(sp, binding_variable);
// Ok(__try_var) pattern
let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
// Err(__try_var) (pattern and expression resp.)
let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
vec![binding_expr.clone()]);
// return Err(__try_var)
let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
// Ok(__try_var) => __try_var
let ok_arm = self.arm(sp, vec![ok_pat], binding_expr);
// Err(__try_var) => return Err(__try_var)
let err_arm = self.arm(sp, vec![err_pat], err_expr);
// match head { Ok() => ..., Err() => ... }
self.expr_match(sp, head, vec![ok_arm, err_arm])
}
2016-02-11 18:16:33 +00:00
fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
2014-09-13 16:06:01 +00:00
P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
}
2014-09-13 16:06:01 +00:00
fn pat_wild(&self, span: Span) -> P<ast::Pat> {
2016-02-11 18:16:33 +00:00
self.pat(span, PatKind::Wild)
}
2014-09-13 16:06:01 +00:00
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
2016-02-11 18:16:33 +00:00
self.pat(span, PatKind::Lit(expr))
}
2014-09-13 16:06:01 +00:00
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
self.pat_ident_binding_mode(span, ident, binding_mode)
}
fn pat_ident_binding_mode(&self,
span: Span,
2013-09-02 00:50:59 +00:00
ident: ast::Ident,
2014-09-13 16:06:01 +00:00
bm: ast::BindingMode) -> P<ast::Pat> {
2016-02-11 18:16:33 +00:00
let pat = PatKind::Ident(bm, Spanned{span: span, node: ident}, None);
self.pat(span, pat)
}
fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
self.pat(span, PatKind::Path(None, path))
}
fn pat_tuple_struct(&self, span: Span, path: ast::Path,
subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
self.pat(span, PatKind::TupleStruct(path, subpats, None))
}
fn pat_struct(&self, span: Span, path: ast::Path,
field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
self.pat(span, PatKind::Struct(path, field_pats, false))
}
2014-09-13 16:06:01 +00:00
fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
self.pat(span, PatKind::Tuple(pats, None))
}
2014-09-13 16:06:01 +00:00
fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
let some = self.std_path(&["option", "Option", "Some"]);
let path = self.path_global(span, some);
self.pat_tuple_struct(span, path, vec![pat])
}
2014-09-13 16:06:01 +00:00
fn pat_none(&self, span: Span) -> P<ast::Pat> {
let some = self.std_path(&["option", "Option", "None"]);
let path = self.path_global(span, some);
self.pat_path(span, path)
}
2014-09-13 16:06:01 +00:00
fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
let some = self.std_path(&["result", "Result", "Ok"]);
let path = self.path_global(span, some);
self.pat_tuple_struct(span, path, vec![pat])
}
2014-09-13 16:06:01 +00:00
fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
let some = self.std_path(&["result", "Result", "Err"]);
let path = self.path_global(span, some);
self.pat_tuple_struct(span, path, vec![pat])
}
2014-09-13 16:06:01 +00:00
fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
ast::Arm {
attrs: vec![],
pats: pats,
guard: None,
body: expr
}
}
fn arm_unreachable(&self, span: Span) -> ast::Arm {
self.arm(span, vec![self.pat_wild(span)], self.expr_unreachable(span))
}
2014-09-13 16:06:01 +00:00
fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
self.expr(span, ast::ExprKind::Match(arg, arms))
}
2014-09-13 16:06:01 +00:00
fn expr_if(&self, span: Span, cond: P<ast::Expr>,
then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
let els = els.map(|x| self.expr_block(self.block_expr(x)));
self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
}
2014-09-13 16:06:01 +00:00
fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
self.expr(span, ast::ExprKind::Loop(block, None))
}
fn lambda_fn_decl(&self,
span: Span,
fn_decl: P<ast::FnDecl>,
body: P<ast::Expr>,
fn_decl_span: Span) // span of the `|...|` part
-> P<ast::Expr> {
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
fn_decl,
body,
fn_decl_span))
}
fn lambda(&self,
span: Span,
ids: Vec<ast::Ident>,
body: P<ast::Expr>)
-> P<ast::Expr> {
let fn_decl = self.fn_decl(
ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
self.ty_infer(span));
// FIXME -- We are using `span` as the span of the `|...|`
// part of the lambda, but it probably (maybe?) corresponds to
// the entire lambda body. Probably we should extend the API
// here, but that's not entirely clear.
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, body, span))
}
fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
self.lambda(span, Vec::new(), body)
}
fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
self.lambda(span, vec![ident], body)
}
fn lambda_stmts(&self,
span: Span,
ids: Vec<ast::Ident>,
stmts: Vec<ast::Stmt>)
2014-09-13 16:06:01 +00:00
-> P<ast::Expr> {
self.lambda(span, ids, self.expr_block(self.block(span, stmts)))
}
fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
self.lambda0(span, self.expr_block(self.block(span, stmts)))
}
fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
2014-09-13 16:06:01 +00:00
ident: ast::Ident) -> P<ast::Expr> {
self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
}
fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
let arg_pat = self.pat_ident(span, ident);
ast::Arg {
ty: ty,
pat: arg_pat,
id: ast::DUMMY_NODE_ID
}
}
// FIXME unused self
fn fn_decl(&self, inputs: Vec<ast::Arg>, output: P<ast::Ty>) -> P<ast::FnDecl> {
P(ast::FnDecl {
inputs: inputs,
output: ast::FunctionRetTy::Ty(output),
variadic: false
})
}
2014-09-13 16:06:01 +00:00
fn item(&self, span: Span, name: Ident,
attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
// FIXME: Would be nice if our generated code didn't violate
// Rust coding conventions
2014-09-13 16:06:01 +00:00
P(ast::Item {
ident: name,
attrs: attrs,
id: ast::DUMMY_NODE_ID,
node: node,
vis: ast::Visibility::Inherited,
2014-09-13 16:06:01 +00:00
span: span
})
}
fn item_fn_poly(&self,
span: Span,
2013-09-02 00:50:59 +00:00
name: Ident,
inputs: Vec<ast::Arg> ,
output: P<ast::Ty>,
generics: Generics,
2014-09-13 16:06:01 +00:00
body: P<ast::Block>) -> P<ast::Item> {
self.item(span,
name,
Vec::new(),
ast::ItemKind::Fn(self.fn_decl(inputs, output),
2014-12-09 15:36:46 +00:00
ast::Unsafety::Normal,
2016-08-10 23:20:12 +00:00
dummy_spanned(ast::Constness::NotConst),
Abi::Rust,
generics,
body))
}
fn item_fn(&self,
span: Span,
2013-09-02 00:50:59 +00:00
name: Ident,
inputs: Vec<ast::Arg> ,
output: P<ast::Ty>,
body: P<ast::Block>
2014-09-13 16:06:01 +00:00
) -> P<ast::Item> {
self.item_fn_poly(
span,
name,
inputs,
output,
Generics::default(),
body)
}
fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
2015-10-02 00:53:28 +00:00
let fields: Vec<_> = tys.into_iter().map(|ty| {
2016-04-06 08:19:10 +00:00
ast::StructField {
span: ty.span,
ty: ty,
2016-04-02 13:47:53 +00:00
ident: None,
vis: ast::Visibility::Inherited,
attrs: Vec::new(),
id: ast::DUMMY_NODE_ID,
2016-04-06 08:19:10 +00:00
}
2013-07-06 04:57:11 +00:00
}).collect();
2015-10-10 00:28:40 +00:00
let vdata = if fields.is_empty() {
ast::VariantData::Unit(ast::DUMMY_NODE_ID)
2015-10-08 20:45:46 +00:00
} else {
2015-10-10 00:28:40 +00:00
ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
2015-10-08 20:45:46 +00:00
};
2015-10-02 00:53:28 +00:00
respan(span,
ast::Variant_ {
name: name,
attrs: Vec::new(),
data: vdata,
disr_expr: None,
})
}
2013-09-02 00:50:59 +00:00
fn item_enum_poly(&self, span: Span, name: Ident,
enum_definition: ast::EnumDef,
2014-09-13 16:06:01 +00:00
generics: Generics) -> P<ast::Item> {
self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
}
2013-09-02 00:50:59 +00:00
fn item_enum(&self, span: Span, name: Ident,
2014-09-13 16:06:01 +00:00
enum_definition: ast::EnumDef) -> P<ast::Item> {
self.item_enum_poly(span, name, enum_definition,
Generics::default())
}
fn item_struct(&self, span: Span, name: Ident,
struct_def: ast::VariantData) -> P<ast::Item> {
self.item_struct_poly(
span,
name,
struct_def,
Generics::default()
)
}
fn item_struct_poly(&self, span: Span, name: Ident,
struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
}
fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
attrs: Vec<ast::Attribute>,
items: Vec<P<ast::Item>>) -> P<ast::Item> {
self.item(
span,
name,
attrs,
ast::ItemKind::Mod(ast::Mod {
inner: inner_span,
items: items,
})
)
}
fn item_static(&self,
span: Span,
name: Ident,
ty: P<ast::Ty>,
mutbl: ast::Mutability,
2014-09-13 16:06:01 +00:00
expr: P<ast::Expr>)
-> P<ast::Item> {
self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
}
rustc: Add `const` globals to the language This change is an implementation of [RFC 69][rfc] which adds a third kind of global to the language, `const`. This global is most similar to what the old `static` was, and if you're unsure about what to use then you should use a `const`. The semantics of these three kinds of globals are: * A `const` does not represent a memory location, but only a value. Constants are translated as rvalues, which means that their values are directly inlined at usage location (similar to a #define in C/C++). Constant values are, well, constant, and can not be modified. Any "modification" is actually a modification to a local value on the stack rather than the actual constant itself. Almost all values are allowed inside constants, whether they have interior mutability or not. There are a few minor restrictions listed in the RFC, but they should in general not come up too often. * A `static` now always represents a memory location (unconditionally). Any references to the same `static` are actually a reference to the same memory location. Only values whose types ascribe to `Sync` are allowed in a `static`. This restriction is in place because many threads may access a `static` concurrently. Lifting this restriction (and allowing unsafe access) is a future extension not implemented at this time. * A `static mut` continues to always represent a memory location. All references to a `static mut` continue to be `unsafe`. This is a large breaking change, and many programs will need to be updated accordingly. A summary of the breaking changes is: * Statics may no longer be used in patterns. Statics now always represent a memory location, which can sometimes be modified. To fix code, repurpose the matched-on-`static` to a `const`. static FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } change this code to: const FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } * Statics may no longer refer to other statics by value. Due to statics being able to change at runtime, allowing them to reference one another could possibly lead to confusing semantics. If you are in this situation, use a constant initializer instead. Note, however, that statics may reference other statics by address, however. * Statics may no longer be used in constant expressions, such as array lengths. This is due to the same restrictions as listed above. Use a `const` instead. [breaking-change] [rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 15:17:01 +00:00
fn item_const(&self,
span: Span,
name: Ident,
ty: P<ast::Ty>,
expr: P<ast::Expr>)
-> P<ast::Item> {
self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
rustc: Add `const` globals to the language This change is an implementation of [RFC 69][rfc] which adds a third kind of global to the language, `const`. This global is most similar to what the old `static` was, and if you're unsure about what to use then you should use a `const`. The semantics of these three kinds of globals are: * A `const` does not represent a memory location, but only a value. Constants are translated as rvalues, which means that their values are directly inlined at usage location (similar to a #define in C/C++). Constant values are, well, constant, and can not be modified. Any "modification" is actually a modification to a local value on the stack rather than the actual constant itself. Almost all values are allowed inside constants, whether they have interior mutability or not. There are a few minor restrictions listed in the RFC, but they should in general not come up too often. * A `static` now always represents a memory location (unconditionally). Any references to the same `static` are actually a reference to the same memory location. Only values whose types ascribe to `Sync` are allowed in a `static`. This restriction is in place because many threads may access a `static` concurrently. Lifting this restriction (and allowing unsafe access) is a future extension not implemented at this time. * A `static mut` continues to always represent a memory location. All references to a `static mut` continue to be `unsafe`. This is a large breaking change, and many programs will need to be updated accordingly. A summary of the breaking changes is: * Statics may no longer be used in patterns. Statics now always represent a memory location, which can sometimes be modified. To fix code, repurpose the matched-on-`static` to a `const`. static FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } change this code to: const FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } * Statics may no longer refer to other statics by value. Due to statics being able to change at runtime, allowing them to reference one another could possibly lead to confusing semantics. If you are in this situation, use a constant initializer instead. Note, however, that statics may reference other statics by address, however. * Statics may no longer be used in constant expressions, such as array lengths. This is due to the same restrictions as listed above. Use a `const` instead. [breaking-change] [rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 15:17:01 +00:00
}
fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
2014-09-13 16:06:01 +00:00
generics: Generics) -> P<ast::Item> {
self.item(span, name, Vec::new(), ast::ItemKind::Ty(ty, generics))
}
2014-09-13 16:06:01 +00:00
fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
self.item_ty_poly(span, name, ty, Generics::default())
}
fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute {
attr::mk_spanned_attr_outer(sp, attr::mk_attr_id(), mi)
}
fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
attr::mk_spanned_word_item(sp, w)
}
fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem {
respan(sp, ast::NestedMetaItemKind::MetaItem(attr::mk_spanned_word_item(sp, w)))
}
fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec<ast::NestedMetaItem>)
-> ast::MetaItem {
attr::mk_spanned_list_item(sp, name, mis)
}
fn meta_name_value(&self, sp: Span, name: ast::Name, value: ast::LitKind)
-> ast::MetaItem {
attr::mk_spanned_name_value_item(sp, name, respan(sp, value))
}
fn item_use(&self, sp: Span,
vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
P(ast::Item {
id: ast::DUMMY_NODE_ID,
ident: keywords::Invalid.ident(),
attrs: vec![],
node: ast::ItemKind::Use(vp),
vis: vis,
span: sp
})
}
fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
let last = path.segments.last().unwrap().identifier;
self.item_use_simple_(sp, vis, last, path)
}
fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
self.item_use(sp, vis,
2014-09-13 16:06:01 +00:00
P(respan(sp,
ast::ViewPathSimple(ident,
path))))
}
fn item_use_list(&self, sp: Span, vis: ast::Visibility,
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
let imports = imports.iter().map(|id| {
let item = ast::PathListItem_ {
name: *id,
rename: None,
id: ast::DUMMY_NODE_ID,
};
respan(sp, item)
}).collect();
self.item_use(sp, vis,
2014-09-13 16:06:01 +00:00
P(respan(sp,
ast::ViewPathList(self.path(sp, path),
imports))))
}
fn item_use_glob(&self, sp: Span,
vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
self.item_use(sp, vis,
2014-09-13 16:06:01 +00:00
P(respan(sp,
ast::ViewPathGlob(self.path(sp, path)))))
}
}