2014-01-30 19:29:35 +01:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
2011-07-05 16:23:07 -07:00
|
|
|
// The Rust abstract syntax tree.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::TyParamBound::*;
|
|
|
|
pub use self::UnsafeSource::*;
|
|
|
|
pub use self::PathParameters::*;
|
2017-03-17 04:04:41 +00:00
|
|
|
pub use symbol::{Ident, Symbol as Name};
|
2016-06-18 04:01:57 +00:00
|
|
|
pub use util::ThinVec;
|
2018-01-15 15:09:39 -08:00
|
|
|
pub use util::parser::ExprPrecedence;
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2017-03-15 00:22:48 +00:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2016-06-21 18:08:13 -04:00
|
|
|
use codemap::{respan, Spanned};
|
2014-04-02 01:19:41 -07:00
|
|
|
use abi::Abi;
|
2017-03-16 10:23:33 +00:00
|
|
|
use ext::hygiene::{Mark, SyntaxContext};
|
2015-06-17 09:56:27 +03:00
|
|
|
use print::pprust;
|
2014-05-18 01:13:20 +03:00
|
|
|
use ptr::P;
|
2017-03-14 15:50:04 +01:00
|
|
|
use rustc_data_structures::indexed_vec;
|
2016-11-16 10:52:37 +00:00
|
|
|
use symbol::{Symbol, keywords};
|
2017-02-21 05:05:59 +00:00
|
|
|
use tokenstream::{ThinTokenStream, TokenStream};
|
2012-05-21 10:45:56 -07:00
|
|
|
|
2017-03-17 04:04:41 +00:00
|
|
|
use serialize::{self, Encoder, Decoder};
|
2016-11-15 08:54:27 +00:00
|
|
|
use std::collections::HashSet;
|
2014-02-19 18:56:33 -08:00
|
|
|
use std::fmt;
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2016-08-31 14:00:29 +03:00
|
|
|
use std::u32;
|
|
|
|
|
2018-01-16 01:44:32 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
|
|
|
|
pub struct Label {
|
|
|
|
pub ident: Ident,
|
|
|
|
pub span: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Label {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "label({:?})", self.ident)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
|
2013-02-11 16:33:31 -08:00
|
|
|
pub struct Lifetime {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
|
|
|
pub span: Span,
|
2017-03-25 21:14:18 +00:00
|
|
|
pub ident: Ident,
|
2013-02-11 16:33:31 -08:00
|
|
|
}
|
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
impl fmt::Debug for Lifetime {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "lifetime({}: {})", self.id, pprust::lifetime_to_string(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A lifetime definition, e.g. `'a: 'b+'c+'d`
|
2015-03-18 18:06:10 +05:30
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-08-05 22:59:24 -04:00
|
|
|
pub struct LifetimeDef {
|
2016-05-17 18:51:45 +02:00
|
|
|
pub attrs: ThinVec<Attribute>,
|
2014-08-05 22:59:24 -04:00
|
|
|
pub lifetime: Lifetime,
|
|
|
|
pub bounds: Vec<Lifetime>
|
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A "Path" is essentially Rust's notion of a name.
|
|
|
|
///
|
|
|
|
/// It's represented as a sequence of identifiers,
|
2014-06-09 13:12:30 -07:00
|
|
|
/// along with a bunch of supporting information.
|
2016-06-24 13:14:34 +02:00
|
|
|
///
|
|
|
|
/// E.g. `std::cmp::PartialEq`
|
2015-06-17 09:56:27 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
|
2013-03-26 17:00:35 -07:00
|
|
|
pub struct Path {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub span: Span,
|
2013-08-07 09:47:28 -07:00
|
|
|
/// The segments in the path: the things separated by `::`.
|
2016-12-05 03:51:11 +00:00
|
|
|
/// Global paths begin with `keywords::CrateRoot`.
|
2014-11-03 21:52:52 -05:00
|
|
|
pub segments: Vec<PathSegment>,
|
2013-08-07 09:47:28 -07:00
|
|
|
}
|
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
impl<'a> PartialEq<&'a str> for Path {
|
|
|
|
fn eq(&self, string: &&'a str) -> bool {
|
|
|
|
self.segments.len() == 1 && self.segments[0].identifier.name == *string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
impl fmt::Debug for Path {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "path({})", pprust::path_to_string(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Path {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", pprust::path_to_string(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 12:12:01 +03:00
|
|
|
impl Path {
|
|
|
|
// convert a span and an identifier to the corresponding
|
|
|
|
// 1-segment path
|
|
|
|
pub fn from_ident(s: Span, identifier: Ident) -> Path {
|
|
|
|
Path {
|
|
|
|
span: s,
|
2017-03-08 20:30:06 +03:00
|
|
|
segments: vec![PathSegment::from_ident(identifier, s)],
|
2016-03-29 12:12:01 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 03:51:11 +00:00
|
|
|
|
2018-03-10 02:02:39 +03:00
|
|
|
// Make a "crate root" segment for this path unless it already has it
|
|
|
|
// or starts with something like `self`/`super`/`$crate`/etc.
|
|
|
|
pub fn make_root(&self) -> Option<PathSegment> {
|
|
|
|
if let Some(ident) = self.segments.get(0).map(|seg| seg.identifier) {
|
2018-03-09 23:56:40 -06:00
|
|
|
if ::parse::token::is_path_segment_keyword(ident) &&
|
2018-03-10 02:02:39 +03:00
|
|
|
ident.name != keywords::Crate.name() {
|
|
|
|
return None;
|
2017-11-04 23:56:45 +03:00
|
|
|
}
|
2016-12-05 03:51:11 +00:00
|
|
|
}
|
2018-03-10 17:45:47 +03:00
|
|
|
Some(PathSegment::crate_root(self.span.shrink_to_lo()))
|
2016-12-05 03:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_global(&self) -> bool {
|
|
|
|
!self.segments.is_empty() && self.segments[0].identifier.name == keywords::CrateRoot.name()
|
|
|
|
}
|
2016-03-29 12:12:01 +03:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A segment of a path: an identifier, an optional lifetime, and a set of types.
|
|
|
|
///
|
|
|
|
/// E.g. `std`, `String` or `Box<T>`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-08-07 09:47:28 -07:00
|
|
|
pub struct PathSegment {
|
|
|
|
/// The identifier portion of this path segment.
|
2014-03-27 15:39:48 -07:00
|
|
|
pub identifier: Ident,
|
2017-03-08 20:30:06 +03:00
|
|
|
/// Span of the segment identifier.
|
|
|
|
pub span: Span,
|
2014-11-03 21:52:52 -05:00
|
|
|
|
|
|
|
/// Type/lifetime parameters attached to this path. They come in
|
2017-07-23 19:32:36 +03:00
|
|
|
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`.
|
|
|
|
/// `None` means that no parameter list is supplied (`Path`),
|
|
|
|
/// `Some` means that parameter list is supplied (`Path<X, Y>`)
|
|
|
|
/// but it can be empty (`Path<>`).
|
|
|
|
/// `P` is used as a size optimization for the common case with no parameters.
|
2016-12-10 06:45:58 +00:00
|
|
|
pub parameters: Option<P<PathParameters>>,
|
|
|
|
}
|
|
|
|
|
2016-12-05 03:51:11 +00:00
|
|
|
impl PathSegment {
|
2017-03-08 20:30:06 +03:00
|
|
|
pub fn from_ident(ident: Ident, span: Span) -> Self {
|
|
|
|
PathSegment { identifier: ident, span: span, parameters: None }
|
|
|
|
}
|
2017-03-17 23:41:09 +00:00
|
|
|
pub fn crate_root(span: Span) -> Self {
|
2016-12-05 03:51:11 +00:00
|
|
|
PathSegment {
|
2018-03-18 02:57:23 +03:00
|
|
|
identifier: Ident::new(keywords::CrateRoot.name(), span),
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-12-05 03:51:11 +00:00
|
|
|
parameters: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Parameters of a path segment.
|
|
|
|
///
|
|
|
|
/// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-03 21:52:52 -05:00
|
|
|
pub enum PathParameters {
|
2015-03-17 04:02:29 +05:30
|
|
|
/// The `<'a, A,B,C>` in `foo::bar::baz::<'a, A,B,C>`
|
2015-12-22 16:56:13 +01:00
|
|
|
AngleBracketed(AngleBracketedParameterData),
|
2015-03-17 04:02:29 +05:30
|
|
|
/// The `(A,B)` and `C` in `Foo(A,B) -> C`
|
2015-12-22 16:56:13 +01:00
|
|
|
Parenthesized(ParenthesizedParameterData),
|
2014-11-03 21:52:52 -05:00
|
|
|
}
|
|
|
|
|
2017-07-20 02:39:34 +03:00
|
|
|
impl PathParameters {
|
2017-07-23 20:50:56 +03:00
|
|
|
pub fn span(&self) -> Span {
|
2017-07-20 02:39:34 +03:00
|
|
|
match *self {
|
2017-07-23 20:50:56 +03:00
|
|
|
AngleBracketed(ref data) => data.span,
|
|
|
|
Parenthesized(ref data) => data.span,
|
2017-07-20 02:39:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 21:52:52 -05:00
|
|
|
/// A path like `Foo<'a, T>`
|
2016-12-10 06:45:58 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Default)]
|
2014-11-03 21:52:52 -05:00
|
|
|
pub struct AngleBracketedParameterData {
|
2017-07-23 20:50:56 +03:00
|
|
|
/// Overall span
|
|
|
|
pub span: Span,
|
2013-10-29 06:03:32 -04:00
|
|
|
/// The lifetime parameters for this path segment.
|
2014-03-27 15:39:48 -07:00
|
|
|
pub lifetimes: Vec<Lifetime>,
|
2013-08-07 09:47:28 -07:00
|
|
|
/// The type parameters for this path segment, if present.
|
2017-01-17 01:54:59 +03:00
|
|
|
pub types: Vec<P<Ty>>,
|
2014-11-29 17:08:30 +13:00
|
|
|
/// Bindings (equality constraints) on associated types, if present.
|
2016-06-24 13:14:34 +02:00
|
|
|
///
|
|
|
|
/// E.g., `Foo<A=Bar>`.
|
2017-01-17 01:54:59 +03:00
|
|
|
pub bindings: Vec<TypeBinding>,
|
2013-01-13 10:48:09 -08:00
|
|
|
}
|
2010-10-04 17:25:52 -07:00
|
|
|
|
2016-12-10 06:45:58 +00:00
|
|
|
impl Into<Option<P<PathParameters>>> for AngleBracketedParameterData {
|
|
|
|
fn into(self) -> Option<P<PathParameters>> {
|
2017-07-23 19:32:36 +03:00
|
|
|
Some(P(PathParameters::AngleBracketed(self)))
|
2014-11-03 21:52:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:39:34 +03:00
|
|
|
impl Into<Option<P<PathParameters>>> for ParenthesizedParameterData {
|
|
|
|
fn into(self) -> Option<P<PathParameters>> {
|
|
|
|
Some(P(PathParameters::Parenthesized(self)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 21:52:52 -05:00
|
|
|
/// A path like `Foo(A,B) -> C`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-03 21:52:52 -05:00
|
|
|
pub struct ParenthesizedParameterData {
|
2015-01-10 11:54:15 -05:00
|
|
|
/// Overall span
|
|
|
|
pub span: Span,
|
|
|
|
|
2014-11-03 21:52:52 -05:00
|
|
|
/// `(A,B)`
|
|
|
|
pub inputs: Vec<P<Ty>>,
|
|
|
|
|
|
|
|
/// `C`
|
|
|
|
pub output: Option<P<Ty>>,
|
|
|
|
}
|
|
|
|
|
2016-09-17 23:31:03 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
|
2016-08-31 14:00:29 +03:00
|
|
|
pub struct NodeId(u32);
|
|
|
|
|
|
|
|
impl NodeId {
|
|
|
|
pub fn new(x: usize) -> NodeId {
|
|
|
|
assert!(x < (u32::MAX as usize));
|
|
|
|
NodeId(x as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_u32(x: u32) -> NodeId {
|
|
|
|
NodeId(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_usize(&self) -> usize {
|
|
|
|
self.0 as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_u32(&self) -> u32 {
|
|
|
|
self.0
|
|
|
|
}
|
2017-03-16 10:23:33 +00:00
|
|
|
|
|
|
|
pub fn placeholder_from_mark(mark: Mark) -> Self {
|
|
|
|
NodeId(mark.as_u32())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn placeholder_to_mark(self) -> Mark {
|
|
|
|
Mark::from_u32(self.0)
|
|
|
|
}
|
2016-08-31 14:00:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for NodeId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
2012-03-14 17:18:53 -04:00
|
|
|
|
2016-09-17 23:31:03 +03:00
|
|
|
impl serialize::UseSpecializedEncodable for NodeId {
|
|
|
|
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
|
|
|
s.emit_u32(self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl serialize::UseSpecializedDecodable for NodeId {
|
|
|
|
fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> {
|
|
|
|
d.read_u32().map(NodeId)
|
|
|
|
}
|
|
|
|
}
|
2012-03-14 17:18:53 -04:00
|
|
|
|
2017-03-14 15:50:04 +01:00
|
|
|
impl indexed_vec::Idx for NodeId {
|
|
|
|
fn new(idx: usize) -> Self {
|
|
|
|
NodeId::new(idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index(self) -> usize {
|
|
|
|
self.as_usize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:31:58 -04:00
|
|
|
/// Node id used to represent the root of the crate.
|
2016-08-31 14:00:29 +03:00
|
|
|
pub const CRATE_NODE_ID: NodeId = NodeId(0);
|
2010-10-18 16:15:25 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// When parsing and doing expansions, we initially give all AST nodes this AST
|
|
|
|
/// node value. Then later, in the renumber pass, we renumber them to have
|
|
|
|
/// small, positive ids.
|
2016-08-31 14:00:29 +03:00
|
|
|
pub const DUMMY_NODE_ID: NodeId = NodeId(!0);
|
2013-09-06 22:11:55 -04:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// The AST represents all type param bounds as types.
|
|
|
|
/// typeck::collect::compute_bounds matches these against
|
|
|
|
/// the "special" built-in traits (see middle::lang_items) and
|
2014-08-05 16:40:04 -07:00
|
|
|
/// detects Copy, Send and Sync.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-02-14 21:50:03 -08:00
|
|
|
pub enum TyParamBound {
|
2014-12-24 19:38:10 +13:00
|
|
|
TraitTyParamBound(PolyTraitRef, TraitBoundModifier),
|
2014-08-27 21:46:52 -04:00
|
|
|
RegionTyParamBound(Lifetime)
|
2013-01-10 11:16:54 -08:00
|
|
|
}
|
2011-12-28 17:50:12 +01:00
|
|
|
|
2018-02-18 19:40:35 +01:00
|
|
|
impl TyParamBound {
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match self {
|
|
|
|
&TraitTyParamBound(ref t, ..) => t.span,
|
|
|
|
&RegionTyParamBound(ref l) => l.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 19:38:10 +13:00
|
|
|
/// A modifier on a bound, currently this is only used for `?Sized`, where the
|
|
|
|
/// modifier is `Maybe`. Negative bounds should also be handled here.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-12-24 19:38:10 +13:00
|
|
|
pub enum TraitBoundModifier {
|
|
|
|
None,
|
|
|
|
Maybe,
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:54:59 +03:00
|
|
|
pub type TyParamBounds = Vec<TyParamBound>;
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-02-14 21:50:03 -08:00
|
|
|
pub struct TyParam {
|
2016-05-17 18:51:45 +02:00
|
|
|
pub attrs: ThinVec<Attribute>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ident: Ident,
|
|
|
|
pub id: NodeId,
|
2014-08-27 21:46:52 -04:00
|
|
|
pub bounds: TyParamBounds,
|
2014-04-03 13:53:57 +13:00
|
|
|
pub default: Option<P<Ty>>,
|
2016-08-10 19:39:12 +02:00
|
|
|
pub span: Span,
|
2013-02-14 21:50:03 -08:00
|
|
|
}
|
|
|
|
|
2017-10-16 21:07:26 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum GenericParam {
|
|
|
|
Lifetime(LifetimeDef),
|
|
|
|
Type(TyParam),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericParam {
|
|
|
|
pub fn is_lifetime_param(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
GenericParam::Lifetime(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_type_param(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
GenericParam::Type(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents lifetime, type and const parameters attached to a declaration of
|
|
|
|
/// a function, enum, trait, etc.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-02-14 21:50:03 -08:00
|
|
|
pub struct Generics {
|
2017-10-16 21:07:26 +02:00
|
|
|
pub params: Vec<GenericParam>,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub where_clause: WhereClause,
|
2016-08-10 19:39:12 +02:00
|
|
|
pub span: Span,
|
2013-02-14 21:50:03 -08:00
|
|
|
}
|
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
impl Generics {
|
|
|
|
pub fn is_lt_parameterized(&self) -> bool {
|
2017-10-16 21:07:26 +02:00
|
|
|
self.params.iter().any(|param| param.is_lifetime_param())
|
2013-03-06 12:27:23 -05:00
|
|
|
}
|
2017-10-16 21:07:26 +02:00
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn is_type_parameterized(&self) -> bool {
|
2017-10-16 21:07:26 +02:00
|
|
|
self.params.iter().any(|param| param.is_type_param())
|
2015-03-24 16:54:09 -07:00
|
|
|
}
|
2017-10-16 21:07:26 +02:00
|
|
|
|
2015-03-24 16:54:09 -07:00
|
|
|
pub fn is_parameterized(&self) -> bool {
|
2017-10-16 21:07:26 +02:00
|
|
|
!self.params.is_empty()
|
2013-02-14 21:50:03 -08:00
|
|
|
}
|
2017-10-16 21:07:26 +02:00
|
|
|
|
2016-10-26 20:51:49 -07:00
|
|
|
pub fn span_for_name(&self, name: &str) -> Option<Span> {
|
2017-10-16 21:07:26 +02:00
|
|
|
for param in &self.params {
|
|
|
|
if let GenericParam::Type(ref t) = *param {
|
|
|
|
if t.ident.name == name {
|
|
|
|
return Some(t.span);
|
|
|
|
}
|
2016-10-26 20:51:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2013-01-13 11:15:14 -08:00
|
|
|
}
|
2010-11-24 18:01:20 -08:00
|
|
|
|
2015-11-28 19:02:07 +00:00
|
|
|
impl Default for Generics {
|
2016-09-11 17:00:09 +05:30
|
|
|
/// Creates an instance of `Generics`.
|
2015-11-28 19:02:07 +00:00
|
|
|
fn default() -> Generics {
|
|
|
|
Generics {
|
2017-10-16 21:07:26 +02:00
|
|
|
params: Vec::new(),
|
2015-11-28 19:02:07 +00:00
|
|
|
where_clause: WhereClause {
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
predicates: Vec::new(),
|
2017-07-27 13:37:35 +09:00
|
|
|
span: DUMMY_SP,
|
2016-08-10 19:39:12 +02:00
|
|
|
},
|
|
|
|
span: DUMMY_SP,
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:02:29 +05:30
|
|
|
/// A `where` clause in a definition
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-08-11 09:32:26 -07:00
|
|
|
pub struct WhereClause {
|
|
|
|
pub id: NodeId,
|
|
|
|
pub predicates: Vec<WherePredicate>,
|
2017-07-27 13:37:35 +09:00
|
|
|
pub span: Span,
|
2014-08-11 09:32:26 -07:00
|
|
|
}
|
|
|
|
|
2015-03-17 04:02:29 +05:30
|
|
|
/// A single predicate in a `where` clause
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-29 17:08:30 +13:00
|
|
|
pub enum WherePredicate {
|
2016-01-02 01:26:22 -06:00
|
|
|
/// A type binding, e.g. `for<'c> Foo: Send+Clone+'c`
|
2014-11-29 17:08:30 +13:00
|
|
|
BoundPredicate(WhereBoundPredicate),
|
2015-03-17 04:02:29 +05:30
|
|
|
/// A lifetime predicate, e.g. `'a: 'b+'c`
|
2014-12-20 02:29:19 -08:00
|
|
|
RegionPredicate(WhereRegionPredicate),
|
2015-03-17 04:02:29 +05:30
|
|
|
/// An equality predicate (unsupported)
|
2015-08-07 09:30:19 -04:00
|
|
|
EqPredicate(WhereEqPredicate),
|
2014-11-29 17:08:30 +13:00
|
|
|
}
|
|
|
|
|
2018-02-18 19:40:35 +01:00
|
|
|
impl WherePredicate {
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match self {
|
|
|
|
&WherePredicate::BoundPredicate(ref p) => p.span,
|
|
|
|
&WherePredicate::RegionPredicate(ref p) => p.span,
|
|
|
|
&WherePredicate::EqPredicate(ref p) => p.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A type bound.
|
|
|
|
///
|
|
|
|
/// E.g. `for<'c> Foo: Send+Clone+'c`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-29 17:08:30 +13:00
|
|
|
pub struct WhereBoundPredicate {
|
2014-08-11 09:32:26 -07:00
|
|
|
pub span: Span,
|
2017-10-16 21:07:26 +02:00
|
|
|
/// Any generics from a `for` binding
|
|
|
|
pub bound_generic_params: Vec<GenericParam>,
|
2015-03-17 04:02:29 +05:30
|
|
|
/// The type being bounded
|
2014-12-20 02:29:19 -08:00
|
|
|
pub bounded_ty: P<Ty>,
|
2015-03-17 04:02:29 +05:30
|
|
|
/// Trait and lifetime bounds (`Clone+Send+'static`)
|
2015-12-16 21:44:33 +03:00
|
|
|
pub bounds: TyParamBounds,
|
2014-08-11 09:32:26 -07:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A lifetime predicate.
|
|
|
|
///
|
|
|
|
/// E.g. `'a: 'b+'c`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-12-20 02:29:19 -08:00
|
|
|
pub struct WhereRegionPredicate {
|
|
|
|
pub span: Span,
|
|
|
|
pub lifetime: Lifetime,
|
2014-12-20 02:48:43 -08:00
|
|
|
pub bounds: Vec<Lifetime>,
|
2014-12-20 02:29:19 -08:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// An equality predicate (unsupported).
|
|
|
|
///
|
|
|
|
/// E.g. `T=int`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-29 17:08:30 +13:00
|
|
|
pub struct WhereEqPredicate {
|
|
|
|
pub id: NodeId,
|
|
|
|
pub span: Span,
|
2017-01-16 21:32:13 +03:00
|
|
|
pub lhs_ty: P<Ty>,
|
|
|
|
pub rhs_ty: P<Ty>,
|
2014-11-29 17:08:30 +13:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// The set of MetaItems that define the compilation environment of the crate,
|
|
|
|
/// used to drive conditional compilation
|
2016-11-16 10:52:37 +00:00
|
|
|
pub type CrateConfig = HashSet<(Name, Option<Symbol>)>;
|
2010-08-18 09:00:10 -07:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-07-19 07:38:55 +02:00
|
|
|
pub struct Crate {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub module: Mod,
|
|
|
|
pub attrs: Vec<Attribute>,
|
|
|
|
pub span: Span,
|
2013-01-14 19:06:59 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
/// A spanned compile-time attribute list item.
|
|
|
|
pub type NestedMetaItem = Spanned<NestedMetaItemKind>;
|
|
|
|
|
|
|
|
/// Possible values inside of compile-time attribute lists.
|
|
|
|
///
|
|
|
|
/// E.g. the '..' in `#[name(..)]`.
|
|
|
|
#[derive(Clone, Eq, RustcEncodable, RustcDecodable, Hash, Debug, PartialEq)]
|
|
|
|
pub enum NestedMetaItemKind {
|
|
|
|
/// A full MetaItem, for recursive meta items.
|
2016-11-15 10:17:24 +00:00
|
|
|
MetaItem(MetaItem),
|
2016-08-19 18:58:14 -07:00
|
|
|
/// A literal.
|
|
|
|
///
|
|
|
|
/// E.g. "foo", 64, true
|
|
|
|
Literal(Lit),
|
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A spanned compile-time attribute item.
|
|
|
|
///
|
|
|
|
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
|
2016-11-15 07:37:10 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub struct MetaItem {
|
|
|
|
pub name: Name,
|
|
|
|
pub node: MetaItemKind,
|
|
|
|
pub span: Span,
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A compile-time attribute item.
|
|
|
|
///
|
|
|
|
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
|
2016-11-15 08:54:27 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-02-09 12:05:20 +01:00
|
|
|
pub enum MetaItemKind {
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Word meta item.
|
|
|
|
///
|
|
|
|
/// E.g. `test` as in `#[test]`
|
2016-11-15 07:37:10 +00:00
|
|
|
Word,
|
2016-06-24 13:14:34 +02:00
|
|
|
/// List meta item.
|
|
|
|
///
|
|
|
|
/// E.g. `derive(..)` as in `#[derive(..)]`
|
2016-11-15 07:37:10 +00:00
|
|
|
List(Vec<NestedMetaItem>),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Name value meta item.
|
|
|
|
///
|
|
|
|
/// E.g. `feature = "foo"` as in `#[feature = "foo"]`
|
2016-11-15 07:37:10 +00:00
|
|
|
NameValue(Lit)
|
2013-07-19 21:51:37 +10:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A Block (`{ .. }`).
|
|
|
|
///
|
|
|
|
/// E.g. `{ .. }` as in `fn foo() { .. }`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-07-19 07:38:55 +02:00
|
|
|
pub struct Block {
|
2015-03-17 04:19:27 +05:30
|
|
|
/// Statements in a block
|
2016-02-11 23:33:09 +03:00
|
|
|
pub stmts: Vec<Stmt>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
2015-03-18 18:06:10 +05:30
|
|
|
/// Distinguishes between `unsafe { ... }` and `{ ... }`
|
2014-03-27 15:39:48 -07:00
|
|
|
pub rules: BlockCheckMode,
|
|
|
|
pub span: Span,
|
2017-12-13 23:05:49 -08:00
|
|
|
pub recovered: bool,
|
2013-01-14 19:35:08 -08:00
|
|
|
}
|
2010-08-18 09:00:10 -07:00
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
|
2013-09-02 03:45:37 +02:00
|
|
|
pub struct Pat {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
2016-02-11 21:16:33 +03:00
|
|
|
pub node: PatKind,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub span: Span,
|
2013-01-14 20:52:28 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
impl fmt::Debug for Pat {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "pat({}: {})", self.id, pprust::pat_to_string(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-24 03:26:10 +00:00
|
|
|
impl Pat {
|
2017-12-18 23:26:59 +03:00
|
|
|
pub(super) fn to_ty(&self) -> Option<P<Ty>> {
|
|
|
|
let node = match &self.node {
|
|
|
|
PatKind::Wild => TyKind::Infer,
|
|
|
|
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None) =>
|
|
|
|
TyKind::Path(None, Path::from_ident(ident.span, ident.node)),
|
|
|
|
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
|
|
|
|
PatKind::Mac(mac) => TyKind::Mac(mac.clone()),
|
|
|
|
PatKind::Ref(pat, mutbl) =>
|
|
|
|
pat.to_ty().map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?,
|
|
|
|
PatKind::Slice(pats, None, _) if pats.len() == 1 =>
|
|
|
|
pats[0].to_ty().map(TyKind::Slice)?,
|
|
|
|
PatKind::Tuple(pats, None) => {
|
|
|
|
let mut tys = Vec::new();
|
|
|
|
for pat in pats {
|
|
|
|
tys.push(pat.to_ty()?);
|
|
|
|
}
|
|
|
|
TyKind::Tup(tys)
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(P(Ty { node, id: self.id, span: self.span }))
|
|
|
|
}
|
|
|
|
|
2016-04-24 03:26:10 +00:00
|
|
|
pub fn walk<F>(&self, it: &mut F) -> bool
|
|
|
|
where F: FnMut(&Pat) -> bool
|
|
|
|
{
|
|
|
|
if !it(self) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.node {
|
|
|
|
PatKind::Ident(_, _, Some(ref p)) => p.walk(it),
|
|
|
|
PatKind::Struct(_, ref fields, _) => {
|
|
|
|
fields.iter().all(|field| field.node.pat.walk(it))
|
|
|
|
}
|
2016-03-06 15:54:44 +03:00
|
|
|
PatKind::TupleStruct(_, ref s, _) | PatKind::Tuple(ref s, _) => {
|
2016-04-24 03:26:10 +00:00
|
|
|
s.iter().all(|p| p.walk(it))
|
|
|
|
}
|
2018-02-24 15:27:06 +03:00
|
|
|
PatKind::Box(ref s) | PatKind::Ref(ref s, _) | PatKind::Paren(ref s) => {
|
2016-04-24 03:26:10 +00:00
|
|
|
s.walk(it)
|
|
|
|
}
|
2016-09-20 16:54:24 +02:00
|
|
|
PatKind::Slice(ref before, ref slice, ref after) => {
|
2016-04-24 03:26:10 +00:00
|
|
|
before.iter().all(|p| p.walk(it)) &&
|
|
|
|
slice.iter().all(|p| p.walk(it)) &&
|
|
|
|
after.iter().all(|p| p.walk(it))
|
|
|
|
}
|
|
|
|
PatKind::Wild |
|
|
|
|
PatKind::Lit(_) |
|
2016-08-26 19:23:42 +03:00
|
|
|
PatKind::Range(..) |
|
2016-08-26 19:23:42 +03:00
|
|
|
PatKind::Ident(..) |
|
2016-04-24 03:26:10 +00:00
|
|
|
PatKind::Path(..) |
|
|
|
|
PatKind::Mac(_) => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:19:27 +05:30
|
|
|
/// A single field in a struct pattern
|
|
|
|
///
|
2015-03-18 18:06:10 +05:30
|
|
|
/// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
|
|
|
|
/// are treated the same as` x: x, y: ref y, z: ref mut z`,
|
|
|
|
/// except is_shorthand is true
|
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-09-02 03:45:37 +02:00
|
|
|
pub struct FieldPat {
|
2015-03-17 04:19:27 +05:30
|
|
|
/// The identifier for the field
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ident: Ident,
|
2015-03-17 04:19:27 +05:30
|
|
|
/// The pattern the field is destructured to
|
2014-05-18 01:13:20 +03:00
|
|
|
pub pat: P<Pat>,
|
2014-10-06 13:36:53 +13:00
|
|
|
pub is_shorthand: bool,
|
2017-01-03 19:13:01 -08:00
|
|
|
pub attrs: ThinVec<Attribute>,
|
2013-01-14 20:52:28 -08:00
|
|
|
}
|
2011-07-11 14:13:20 +02:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2013-09-02 03:45:37 +02:00
|
|
|
pub enum BindingMode {
|
2015-12-18 14:23:01 +01:00
|
|
|
ByRef(Mutability),
|
|
|
|
ByValue(Mutability),
|
2012-07-31 19:25:24 -07:00
|
|
|
}
|
|
|
|
|
2017-01-10 22:13:53 +01:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum RangeEnd {
|
2017-09-21 12:13:26 +02:00
|
|
|
Included(RangeSyntax),
|
2017-01-10 22:13:53 +01:00
|
|
|
Excluded,
|
|
|
|
}
|
|
|
|
|
2017-09-21 12:13:26 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum RangeSyntax {
|
|
|
|
DotDotDot,
|
|
|
|
DotDotEq,
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-02-11 21:16:33 +03:00
|
|
|
pub enum PatKind {
|
2015-10-31 03:44:43 +03:00
|
|
|
/// Represents a wildcard pattern (`_`)
|
2016-02-11 21:16:33 +03:00
|
|
|
Wild,
|
2014-08-06 17:04:44 +02:00
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
/// A `PatKind::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
|
|
|
|
/// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
|
|
|
|
/// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
|
|
|
|
/// during name resolution.
|
2016-02-11 21:16:33 +03:00
|
|
|
Ident(BindingMode, SpannedIdent, Option<P<Pat>>),
|
2014-07-27 15:23:01 -07:00
|
|
|
|
2016-02-13 15:51:27 +03:00
|
|
|
/// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
|
|
|
|
/// The `bool` is `true` in the presence of a `..`.
|
|
|
|
Struct(Path, Vec<Spanned<FieldPat>>, bool),
|
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
/// A tuple struct/variant pattern `Variant(x, y, .., z)`.
|
2016-03-06 15:54:44 +03:00
|
|
|
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
|
2016-03-06 15:54:44 +03:00
|
|
|
/// 0 <= position <= subpats.len()
|
|
|
|
TupleStruct(Path, Vec<P<Pat>>, Option<usize>),
|
2016-02-13 15:51:27 +03:00
|
|
|
|
2016-06-11 18:47:47 +03:00
|
|
|
/// A possibly qualified path pattern.
|
2017-08-11 20:34:14 +02:00
|
|
|
/// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
|
|
|
|
/// or associated constants. Qualified path patterns `<A>::B::C`/`<A as Trait>::B::C` can
|
2016-06-11 18:47:47 +03:00
|
|
|
/// only legally refer to associated constants.
|
|
|
|
Path(Option<QSelf>, Path),
|
2015-03-25 10:53:28 -06:00
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
/// A tuple pattern `(a, b)`.
|
2016-03-06 15:54:44 +03:00
|
|
|
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
|
2016-03-06 15:54:44 +03:00
|
|
|
/// 0 <= position <= subpats.len()
|
|
|
|
Tuple(Vec<P<Pat>>, Option<usize>),
|
2015-03-17 04:19:27 +05:30
|
|
|
/// A `box` pattern
|
2016-02-11 21:16:33 +03:00
|
|
|
Box(P<Pat>),
|
2015-03-17 04:19:27 +05:30
|
|
|
/// A reference pattern, e.g. `&mut (a, b)`
|
2016-02-11 21:16:33 +03:00
|
|
|
Ref(P<Pat>, Mutability),
|
2015-03-17 04:19:27 +05:30
|
|
|
/// A literal
|
2016-02-11 21:16:33 +03:00
|
|
|
Lit(P<Expr>),
|
2017-09-21 12:13:26 +02:00
|
|
|
/// A range pattern, e.g. `1...2`, `1..=2` or `1..2`
|
2017-01-10 22:13:53 +01:00
|
|
|
Range(P<Expr>, P<Expr>, RangeEnd),
|
2015-11-01 16:37:12 +05:30
|
|
|
/// `[a, b, ..i, y, z]` is represented as:
|
2016-09-20 16:54:24 +02:00
|
|
|
/// `PatKind::Slice(box [a, b], Some(i), box [y, z])`
|
|
|
|
Slice(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
|
2018-02-24 15:27:06 +03:00
|
|
|
/// Parentheses in patters used for grouping, i.e. `(PAT)`.
|
|
|
|
Paren(P<Pat>),
|
2015-03-17 04:19:27 +05:30
|
|
|
/// A macro pattern; pre-expansion
|
2016-02-11 21:16:33 +03:00
|
|
|
Mac(Mac),
|
2010-11-24 14:42:01 -08:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2013-09-02 03:45:37 +02:00
|
|
|
pub enum Mutability {
|
2016-02-09 17:44:47 +01:00
|
|
|
Mutable,
|
|
|
|
Immutable,
|
2013-08-02 21:41:06 -07:00
|
|
|
}
|
2010-11-29 14:18:26 -08:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2016-02-08 13:16:12 +01:00
|
|
|
pub enum BinOpKind {
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `+` operator (addition)
|
2016-02-08 13:16:12 +01:00
|
|
|
Add,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `-` operator (subtraction)
|
2016-02-08 13:16:12 +01:00
|
|
|
Sub,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `*` operator (multiplication)
|
2016-02-08 13:16:12 +01:00
|
|
|
Mul,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `/` operator (division)
|
2016-02-08 13:16:12 +01:00
|
|
|
Div,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `%` operator (modulus)
|
2016-02-08 13:16:12 +01:00
|
|
|
Rem,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `&&` operator (logical and)
|
2016-02-08 13:16:12 +01:00
|
|
|
And,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `||` operator (logical or)
|
2016-02-08 13:16:12 +01:00
|
|
|
Or,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `^` operator (bitwise xor)
|
2016-02-08 13:16:12 +01:00
|
|
|
BitXor,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `&` operator (bitwise and)
|
2016-02-08 13:16:12 +01:00
|
|
|
BitAnd,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `|` operator (bitwise or)
|
2016-02-08 13:16:12 +01:00
|
|
|
BitOr,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `<<` operator (shift left)
|
2016-02-08 13:16:12 +01:00
|
|
|
Shl,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `>>` operator (shift right)
|
2016-02-08 13:16:12 +01:00
|
|
|
Shr,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `==` operator (equality)
|
2016-02-08 13:16:12 +01:00
|
|
|
Eq,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `<` operator (less than)
|
2016-02-08 13:16:12 +01:00
|
|
|
Lt,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `<=` operator (less than or equal to)
|
2016-02-08 13:16:12 +01:00
|
|
|
Le,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `!=` operator (not equal to)
|
2016-02-08 13:16:12 +01:00
|
|
|
Ne,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `>=` operator (greater than or equal to)
|
2016-02-08 13:16:12 +01:00
|
|
|
Ge,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `>` operator (greater than)
|
2016-02-08 13:16:12 +01:00
|
|
|
Gt,
|
2010-09-27 18:25:02 -07:00
|
|
|
}
|
|
|
|
|
2016-02-08 13:16:12 +01:00
|
|
|
impl BinOpKind {
|
2015-11-28 19:02:07 +00:00
|
|
|
pub fn to_string(&self) -> &'static str {
|
2016-02-08 13:16:12 +01:00
|
|
|
use self::BinOpKind::*;
|
2015-11-28 19:02:07 +00:00
|
|
|
match *self {
|
2016-02-08 13:16:12 +01:00
|
|
|
Add => "+",
|
|
|
|
Sub => "-",
|
|
|
|
Mul => "*",
|
|
|
|
Div => "/",
|
|
|
|
Rem => "%",
|
|
|
|
And => "&&",
|
|
|
|
Or => "||",
|
|
|
|
BitXor => "^",
|
|
|
|
BitAnd => "&",
|
|
|
|
BitOr => "|",
|
|
|
|
Shl => "<<",
|
|
|
|
Shr => ">>",
|
|
|
|
Eq => "==",
|
|
|
|
Lt => "<",
|
|
|
|
Le => "<=",
|
|
|
|
Ne => "!=",
|
|
|
|
Ge => ">=",
|
|
|
|
Gt => ">",
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn lazy(&self) -> bool {
|
|
|
|
match *self {
|
2016-02-08 13:16:12 +01:00
|
|
|
BinOpKind::And | BinOpKind::Or => true,
|
2015-11-28 19:02:07 +00:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_shift(&self) -> bool {
|
|
|
|
match *self {
|
2016-02-08 13:16:12 +01:00
|
|
|
BinOpKind::Shl | BinOpKind::Shr => true,
|
2015-11-28 19:02:07 +00:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 11:40:16 -08:00
|
|
|
|
2015-11-28 19:02:07 +00:00
|
|
|
pub fn is_comparison(&self) -> bool {
|
2016-02-08 13:16:12 +01:00
|
|
|
use self::BinOpKind::*;
|
2015-11-28 19:02:07 +00:00
|
|
|
match *self {
|
2016-02-08 13:16:12 +01:00
|
|
|
Eq | Lt | Le | Ne | Gt | Ge =>
|
2015-11-28 19:02:07 +00:00
|
|
|
true,
|
2016-02-08 13:16:12 +01:00
|
|
|
And | Or | Add | Sub | Mul | Div | Rem |
|
|
|
|
BitXor | BitAnd | BitOr | Shl | Shr =>
|
2015-11-28 19:02:07 +00:00
|
|
|
false,
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 11:40:16 -08:00
|
|
|
|
2015-11-28 19:02:07 +00:00
|
|
|
/// Returns `true` if the binary operator takes its arguments by value
|
|
|
|
pub fn is_by_value(&self) -> bool {
|
2016-02-08 13:16:12 +01:00
|
|
|
!self.is_comparison()
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 13:16:12 +01:00
|
|
|
pub type BinOp = Spanned<BinOpKind>;
|
2015-01-13 14:24:37 +11:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2013-09-02 03:45:37 +02:00
|
|
|
pub enum UnOp {
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `*` operator for dereferencing
|
2016-02-08 13:21:29 +01:00
|
|
|
Deref,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `!` operator for logical inversion
|
2016-02-08 13:21:29 +01:00
|
|
|
Not,
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The `-` operator for negation
|
2016-02-08 13:21:29 +01:00
|
|
|
Neg,
|
2012-09-07 18:53:14 -07:00
|
|
|
}
|
|
|
|
|
2015-11-28 19:02:07 +00:00
|
|
|
impl UnOp {
|
|
|
|
/// Returns `true` if the unary operator takes its argument by value
|
|
|
|
pub fn is_by_value(u: UnOp) -> bool {
|
|
|
|
match u {
|
2016-02-08 13:21:29 +01:00
|
|
|
UnOp::Neg | UnOp::Not => true,
|
2015-11-28 19:02:07 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string(op: UnOp) -> &'static str {
|
|
|
|
match op {
|
2016-02-08 13:21:29 +01:00
|
|
|
UnOp::Deref => "*",
|
|
|
|
UnOp::Not => "!",
|
|
|
|
UnOp::Neg => "-",
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A statement
|
2016-06-17 02:27:16 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
|
|
|
|
pub struct Stmt {
|
|
|
|
pub id: NodeId,
|
|
|
|
pub node: StmtKind,
|
|
|
|
pub span: Span,
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2016-07-02 09:01:21 +00:00
|
|
|
impl Stmt {
|
|
|
|
pub fn add_trailing_semicolon(mut self) -> Self {
|
|
|
|
self.node = match self.node {
|
|
|
|
StmtKind::Expr(expr) => StmtKind::Semi(expr),
|
|
|
|
StmtKind::Mac(mac) => StmtKind::Mac(mac.map(|(mac, _style, attrs)| {
|
|
|
|
(mac, MacStmtStyle::Semicolon, attrs)
|
|
|
|
})),
|
2017-05-12 20:05:39 +02:00
|
|
|
node => node,
|
2016-07-02 09:01:21 +00:00
|
|
|
};
|
|
|
|
self
|
|
|
|
}
|
2017-07-16 00:17:35 +02:00
|
|
|
|
|
|
|
pub fn is_item(&self) -> bool {
|
|
|
|
match self.node {
|
|
|
|
StmtKind::Local(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 23:20:56 -07:00
|
|
|
|
|
|
|
pub fn is_expr(&self) -> bool {
|
|
|
|
match self.node {
|
|
|
|
StmtKind::Expr(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2016-07-02 09:01:21 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
impl fmt::Debug for Stmt {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-06-17 02:30:01 +00:00
|
|
|
write!(f, "stmt({}: {})", self.id.to_string(), pprust::stmt_to_string(self))
|
2015-06-17 09:56:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
|
2016-02-08 17:23:13 +01:00
|
|
|
pub enum StmtKind {
|
2016-06-23 01:04:08 +00:00
|
|
|
/// A local (let) binding.
|
2016-06-17 02:27:16 +00:00
|
|
|
Local(P<Local>),
|
2012-07-03 17:30:25 -07:00
|
|
|
|
2016-06-23 01:04:08 +00:00
|
|
|
/// An item definition.
|
2016-06-17 02:27:16 +00:00
|
|
|
Item(P<Item>),
|
2012-07-03 17:30:25 -07:00
|
|
|
|
2016-06-26 02:19:34 +00:00
|
|
|
/// Expr without trailing semi-colon.
|
2016-06-17 02:27:16 +00:00
|
|
|
Expr(P<Expr>),
|
2017-09-03 12:27:31 -07:00
|
|
|
/// Expr with a trailing semi-colon.
|
2016-06-17 02:27:16 +00:00
|
|
|
Semi(P<Expr>),
|
2017-09-05 18:46:21 -07:00
|
|
|
/// Macro.
|
2016-06-26 02:18:04 +00:00
|
|
|
Mac(P<(Mac, MacStmtStyle, ThinVec<Attribute>)>),
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-14 09:18:10 -08:00
|
|
|
pub enum MacStmtStyle {
|
|
|
|
/// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
|
|
|
|
/// `foo!(...);`, `foo![...];`
|
2016-02-09 11:56:59 +01:00
|
|
|
Semicolon,
|
2014-11-14 09:18:10 -08:00
|
|
|
/// The macro statement had braces; e.g. foo! { ... }
|
2016-02-09 11:56:59 +01:00
|
|
|
Braces,
|
2014-11-14 09:18:10 -08:00
|
|
|
/// The macro statement had parentheses or brackets and no semicolon; e.g.
|
|
|
|
/// `foo!(...)`. All of these will end up being converted into macro
|
|
|
|
/// expressions.
|
2016-02-09 11:56:59 +01:00
|
|
|
NoBraces,
|
2010-09-09 15:59:29 -07:00
|
|
|
}
|
|
|
|
|
2013-11-24 18:38:41 -05:00
|
|
|
/// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-07-19 07:38:55 +02:00
|
|
|
pub struct Local {
|
2014-05-18 01:13:20 +03:00
|
|
|
pub pat: P<Pat>,
|
2015-01-02 20:55:31 +09:00
|
|
|
pub ty: Option<P<Ty>>,
|
2015-03-17 04:32:58 +05:30
|
|
|
/// Initializer expression to set the value, if any
|
2014-05-18 01:13:20 +03:00
|
|
|
pub init: Option<P<Expr>>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
|
|
|
pub span: Span,
|
2016-06-18 04:01:57 +00:00
|
|
|
pub attrs: ThinVec<Attribute>,
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// An arm of a 'match'.
|
|
|
|
///
|
|
|
|
/// E.g. `0...10 => { println!("match!") }` as in
|
|
|
|
///
|
2017-06-20 15:15:16 +08:00
|
|
|
/// ```
|
|
|
|
/// match 123 {
|
2016-06-24 13:14:34 +02:00
|
|
|
/// 0...10 => { println!("match!") },
|
2017-06-20 15:15:16 +08:00
|
|
|
/// _ => { println!("no match!") },
|
2016-06-24 13:14:34 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-09-02 03:45:37 +02:00
|
|
|
pub struct Arm {
|
2014-04-22 21:54:48 -07:00
|
|
|
pub attrs: Vec<Attribute>,
|
2014-05-18 01:13:20 +03:00
|
|
|
pub pats: Vec<P<Pat>>,
|
|
|
|
pub guard: Option<P<Expr>>,
|
|
|
|
pub body: P<Expr>,
|
2013-01-14 20:52:28 -08:00
|
|
|
}
|
2010-11-24 15:45:59 -08:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2013-07-19 16:24:22 +02:00
|
|
|
pub struct Field {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ident: SpannedIdent,
|
2014-05-18 01:13:20 +03:00
|
|
|
pub expr: P<Expr>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub span: Span,
|
2016-10-27 03:15:13 +03:00
|
|
|
pub is_shorthand: bool,
|
2017-01-03 19:13:01 -08:00
|
|
|
pub attrs: ThinVec<Attribute>,
|
2013-01-14 21:36:27 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2013-10-28 19:22:42 -07:00
|
|
|
pub type SpannedIdent = Spanned<Ident>;
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2013-07-27 10:25:59 +02:00
|
|
|
pub enum BlockCheckMode {
|
2016-02-08 12:44:45 +01:00
|
|
|
Default,
|
|
|
|
Unsafe(UnsafeSource),
|
2013-09-05 20:50:10 -07:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2013-09-05 20:50:10 -07:00
|
|
|
pub enum UnsafeSource {
|
|
|
|
CompilerGenerated,
|
|
|
|
UserProvided,
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
2011-10-06 16:42:27 -07:00
|
|
|
|
2015-03-17 04:32:58 +05:30
|
|
|
/// An expression
|
2015-06-17 09:56:27 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash,)]
|
2013-09-02 03:45:37 +02:00
|
|
|
pub struct Expr {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
2016-02-08 16:05:05 +01:00
|
|
|
pub node: ExprKind,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub span: Span,
|
2016-06-18 04:01:57 +00:00
|
|
|
pub attrs: ThinVec<Attribute>
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
|
2017-08-17 16:51:52 -07:00
|
|
|
impl Expr {
|
2018-02-16 15:56:50 +01:00
|
|
|
/// Whether this expression would be valid somewhere that expects a value, for example, an `if`
|
2017-08-17 16:51:52 -07:00
|
|
|
/// condition.
|
|
|
|
pub fn returns(&self) -> bool {
|
|
|
|
if let ExprKind::Block(ref block) = self.node {
|
|
|
|
match block.stmts.last().map(|last_stmt| &last_stmt.node) {
|
|
|
|
// implicit return
|
|
|
|
Some(&StmtKind::Expr(_)) => true,
|
|
|
|
Some(&StmtKind::Semi(ref expr)) => {
|
|
|
|
if let ExprKind::Ret(_) = expr.node {
|
|
|
|
// last statement is explicit return
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This is a block that doesn't end in either an implicit or explicit return
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is not a block, it is a value
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2017-12-17 01:53:11 +03:00
|
|
|
|
|
|
|
fn to_bound(&self) -> Option<TyParamBound> {
|
|
|
|
match &self.node {
|
|
|
|
ExprKind::Path(None, path) =>
|
|
|
|
Some(TraitTyParamBound(PolyTraitRef::new(Vec::new(), path.clone(), self.span),
|
|
|
|
TraitBoundModifier::None)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:26:59 +03:00
|
|
|
pub(super) fn to_ty(&self) -> Option<P<Ty>> {
|
2017-12-17 01:53:11 +03:00
|
|
|
let node = match &self.node {
|
|
|
|
ExprKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
|
|
|
|
ExprKind::Mac(mac) => TyKind::Mac(mac.clone()),
|
|
|
|
ExprKind::Paren(expr) => expr.to_ty().map(TyKind::Paren)?,
|
|
|
|
ExprKind::AddrOf(mutbl, expr) =>
|
|
|
|
expr.to_ty().map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?,
|
|
|
|
ExprKind::Repeat(expr, expr_len) =>
|
|
|
|
expr.to_ty().map(|ty| TyKind::Array(ty, expr_len.clone()))?,
|
|
|
|
ExprKind::Array(exprs) if exprs.len() == 1 =>
|
|
|
|
exprs[0].to_ty().map(TyKind::Slice)?,
|
|
|
|
ExprKind::Tup(exprs) => {
|
|
|
|
let mut tys = Vec::new();
|
|
|
|
for expr in exprs {
|
|
|
|
tys.push(expr.to_ty()?);
|
|
|
|
}
|
|
|
|
TyKind::Tup(tys)
|
|
|
|
}
|
|
|
|
ExprKind::Binary(binop, lhs, rhs) if binop.node == BinOpKind::Add =>
|
|
|
|
if let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) {
|
|
|
|
TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(P(Ty { node, id: self.id, span: self.span }))
|
|
|
|
}
|
2018-01-10 11:40:16 -08:00
|
|
|
|
|
|
|
pub fn precedence(&self) -> ExprPrecedence {
|
|
|
|
match self.node {
|
|
|
|
ExprKind::Box(_) => ExprPrecedence::Box,
|
|
|
|
ExprKind::Array(_) => ExprPrecedence::Array,
|
|
|
|
ExprKind::Call(..) => ExprPrecedence::Call,
|
|
|
|
ExprKind::MethodCall(..) => ExprPrecedence::MethodCall,
|
|
|
|
ExprKind::Tup(_) => ExprPrecedence::Tup,
|
|
|
|
ExprKind::Binary(op, ..) => ExprPrecedence::Binary(op.node),
|
|
|
|
ExprKind::Unary(..) => ExprPrecedence::Unary,
|
|
|
|
ExprKind::Lit(_) => ExprPrecedence::Lit,
|
|
|
|
ExprKind::Type(..) | ExprKind::Cast(..) => ExprPrecedence::Cast,
|
|
|
|
ExprKind::If(..) => ExprPrecedence::If,
|
|
|
|
ExprKind::IfLet(..) => ExprPrecedence::IfLet,
|
|
|
|
ExprKind::While(..) => ExprPrecedence::While,
|
|
|
|
ExprKind::WhileLet(..) => ExprPrecedence::WhileLet,
|
|
|
|
ExprKind::ForLoop(..) => ExprPrecedence::ForLoop,
|
|
|
|
ExprKind::Loop(..) => ExprPrecedence::Loop,
|
|
|
|
ExprKind::Match(..) => ExprPrecedence::Match,
|
|
|
|
ExprKind::Closure(..) => ExprPrecedence::Closure,
|
|
|
|
ExprKind::Block(..) => ExprPrecedence::Block,
|
|
|
|
ExprKind::Catch(..) => ExprPrecedence::Catch,
|
|
|
|
ExprKind::Assign(..) => ExprPrecedence::Assign,
|
|
|
|
ExprKind::AssignOp(..) => ExprPrecedence::AssignOp,
|
|
|
|
ExprKind::Field(..) => ExprPrecedence::Field,
|
|
|
|
ExprKind::TupField(..) => ExprPrecedence::TupField,
|
|
|
|
ExprKind::Index(..) => ExprPrecedence::Index,
|
|
|
|
ExprKind::Range(..) => ExprPrecedence::Range,
|
|
|
|
ExprKind::Path(..) => ExprPrecedence::Path,
|
|
|
|
ExprKind::AddrOf(..) => ExprPrecedence::AddrOf,
|
|
|
|
ExprKind::Break(..) => ExprPrecedence::Break,
|
|
|
|
ExprKind::Continue(..) => ExprPrecedence::Continue,
|
|
|
|
ExprKind::Ret(..) => ExprPrecedence::Ret,
|
|
|
|
ExprKind::InlineAsm(..) => ExprPrecedence::InlineAsm,
|
|
|
|
ExprKind::Mac(..) => ExprPrecedence::Mac,
|
|
|
|
ExprKind::Struct(..) => ExprPrecedence::Struct,
|
|
|
|
ExprKind::Repeat(..) => ExprPrecedence::Repeat,
|
|
|
|
ExprKind::Paren(..) => ExprPrecedence::Paren,
|
|
|
|
ExprKind::Try(..) => ExprPrecedence::Try,
|
|
|
|
ExprKind::Yield(..) => ExprPrecedence::Yield,
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 16:51:52 -07:00
|
|
|
}
|
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
impl fmt::Debug for Expr {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 01:23:31 -05:00
|
|
|
/// Limit types of a range (inclusive or exclusive)
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum RangeLimits {
|
|
|
|
/// Inclusive at the beginning, exclusive at the end
|
|
|
|
HalfOpen,
|
|
|
|
/// Inclusive at the beginning and end
|
|
|
|
Closed,
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-02-08 16:05:05 +01:00
|
|
|
pub enum ExprKind {
|
2015-09-24 18:00:08 +03:00
|
|
|
/// A `box x` expression.
|
2016-02-08 16:05:05 +01:00
|
|
|
Box(P<Expr>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// An array (`[a, b, c, d]`)
|
2017-01-15 23:36:10 -08:00
|
|
|
Array(Vec<P<Expr>>),
|
2015-03-17 17:42:20 +05:30
|
|
|
/// A function call
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-17 17:42:20 +05:30
|
|
|
/// The first field resolves to the function itself,
|
2017-10-27 16:27:59 -02:00
|
|
|
/// and the second field is the list of arguments.
|
|
|
|
/// This also represents calling the constructor of
|
|
|
|
/// tuple-like ADTs such as tuple structs and enum variants.
|
2016-02-08 16:05:05 +01:00
|
|
|
Call(P<Expr>, Vec<P<Expr>>),
|
2017-07-07 02:39:55 +03:00
|
|
|
/// A method call (`x.foo::<'static, Bar, Baz>(a, b, c, d)`)
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2017-07-07 02:39:55 +03:00
|
|
|
/// The `PathSegment` represents the method name and its generic arguments
|
2015-03-19 00:56:37 +05:30
|
|
|
/// (within the angle brackets).
|
2015-03-17 03:41:23 +05:30
|
|
|
/// The first element of the vector of `Expr`s is the expression that evaluates
|
2015-03-17 17:42:20 +05:30
|
|
|
/// to the object on which the method is being called on (the receiver),
|
|
|
|
/// and the remaining elements are the rest of the arguments.
|
|
|
|
/// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
|
2017-07-07 02:39:55 +03:00
|
|
|
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d])`.
|
|
|
|
MethodCall(PathSegment, Vec<P<Expr>>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A tuple (`(a, b, c ,d)`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Tup(Vec<P<Expr>>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A binary operation (For example: `a + b`, `a * b`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Binary(BinOp, P<Expr>, P<Expr>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A unary operation (For example: `!x`, `*x`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Unary(UnOp, P<Expr>),
|
2016-03-11 00:05:53 +05:30
|
|
|
/// A literal (For example: `1`, `"foo"`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Lit(P<Lit>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A cast (`foo as f64`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Cast(P<Expr>, P<Ty>),
|
|
|
|
Type(P<Expr>, P<Ty>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// An `if` block, with an optional else block
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-17 03:41:23 +05:30
|
|
|
/// `if expr { block } else { expr }`
|
2016-02-08 16:05:05 +01:00
|
|
|
If(P<Expr>, P<Block>, Option<P<Expr>>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// An `if let` expression with an optional else block
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-17 03:41:23 +05:30
|
|
|
/// `if let pat = expr { block } else { expr }`
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
|
|
|
/// This is desugared to a `match` expression.
|
2018-02-24 03:12:35 +03:00
|
|
|
IfLet(Vec<P<Pat>>, P<Expr>, P<Block>, Option<P<Expr>>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A while loop, with an optional label
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-18 18:06:10 +05:30
|
|
|
/// `'label: while expr { block }`
|
2018-01-16 01:44:32 +03:00
|
|
|
While(P<Expr>, P<Block>, Option<Label>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A while-let loop, with an optional label
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-18 18:06:10 +05:30
|
|
|
/// `'label: while let pat = expr { block }`
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
|
|
|
/// This is desugared to a combination of `loop` and `match` expressions.
|
2018-02-24 03:12:35 +03:00
|
|
|
WhileLet(Vec<P<Pat>>, P<Expr>, P<Block>, Option<Label>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A for loop, with an optional label
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-18 18:06:10 +05:30
|
|
|
/// `'label: for pat in expr { block }`
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
|
|
|
/// This is desugared to a combination of `loop` and `match` expressions.
|
2018-01-16 01:44:32 +03:00
|
|
|
ForLoop(P<Pat>, P<Expr>, P<Block>, Option<Label>),
|
2015-03-18 18:06:10 +05:30
|
|
|
/// Conditionless loop (can be exited with break, continue, or return)
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-18 18:06:10 +05:30
|
|
|
/// `'label: loop { block }`
|
2018-01-16 01:44:32 +03:00
|
|
|
Loop(P<Block>, Option<Label>),
|
2015-09-30 17:18:09 +13:00
|
|
|
/// A `match` block.
|
2016-02-08 16:05:05 +01:00
|
|
|
Match(P<Expr>, Vec<Arm>),
|
2016-10-26 02:17:29 +03:00
|
|
|
/// A closure (for example, `move |a, b, c| a + b + c`)
|
2016-04-20 14:44:07 -04:00
|
|
|
///
|
|
|
|
/// The final span is the span of the argument block `|...|`
|
2017-10-07 16:36:28 +02:00
|
|
|
Closure(CaptureBy, Movability, P<FnDecl>, P<Expr>, Span),
|
2015-03-18 18:06:10 +05:30
|
|
|
/// A block (`{ ... }`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Block(P<Block>),
|
2017-02-17 15:12:47 -08:00
|
|
|
/// A catch block (`catch { ... }`)
|
|
|
|
Catch(P<Block>),
|
2013-09-02 03:45:37 +02:00
|
|
|
|
2015-03-17 03:41:23 +05:30
|
|
|
/// An assignment (`a = foo()`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Assign(P<Expr>, P<Expr>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// An assignment with an operator
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
|
|
|
/// For example, `a += 1`.
|
2016-02-08 16:05:05 +01:00
|
|
|
AssignOp(BinOp, P<Expr>, P<Expr>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// Access of a named struct field (`obj.foo`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Field(P<Expr>, SpannedIdent),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// Access of an unnamed field of a struct or tuple-struct
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
|
|
|
/// For example, `foo.0`.
|
2016-02-08 16:05:05 +01:00
|
|
|
TupField(P<Expr>, Spanned<usize>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// An indexing operation (`foo[2]`)
|
2016-02-08 16:05:05 +01:00
|
|
|
Index(P<Expr>, P<Expr>),
|
2016-01-13 01:23:31 -05:00
|
|
|
/// A range (`1..2`, `1..`, `..2`, `1...2`, `1...`, `...2`)
|
|
|
|
Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits),
|
2013-11-24 18:38:41 -05:00
|
|
|
|
2015-02-17 19:29:13 +02:00
|
|
|
/// Variable reference, possibly containing `::` and/or type
|
2015-03-19 00:56:37 +05:30
|
|
|
/// parameters, e.g. foo::bar::<baz>.
|
|
|
|
///
|
|
|
|
/// Optionally "qualified",
|
2016-06-24 13:14:34 +02:00
|
|
|
/// E.g. `<Vec<T> as SomeTrait>::SomeType`.
|
2016-02-08 16:05:05 +01:00
|
|
|
Path(Option<QSelf>, Path),
|
2013-05-10 15:15:06 -07:00
|
|
|
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A referencing operation (`&a` or `&mut a`)
|
2016-02-08 16:05:05 +01:00
|
|
|
AddrOf(Mutability, P<Expr>),
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 15:15:06 -07:00
|
|
|
/// A `break`, with an optional label to break, and an optional expression
|
2018-01-16 01:44:32 +03:00
|
|
|
Break(Option<Label>, Option<P<Expr>>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A `continue`, with an optional label
|
2018-01-16 01:44:32 +03:00
|
|
|
Continue(Option<Label>),
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A `return`, with an optional value to be returned
|
2016-02-08 16:05:05 +01:00
|
|
|
Ret(Option<P<Expr>>),
|
2013-08-27 23:12:05 -07:00
|
|
|
|
2015-03-17 03:41:23 +05:30
|
|
|
/// Output of the `asm!()` macro
|
2016-11-03 08:38:17 +00:00
|
|
|
InlineAsm(P<InlineAsm>),
|
2011-08-19 15:16:48 -07:00
|
|
|
|
2015-03-17 03:41:23 +05:30
|
|
|
/// A macro invocation; pre-expansion
|
2016-02-08 16:05:05 +01:00
|
|
|
Mac(Mac),
|
2012-07-23 16:39:18 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// A struct literal expression.
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-18 18:06:10 +05:30
|
|
|
/// For example, `Foo {x: 1, y: 2}`, or
|
2015-03-19 00:56:37 +05:30
|
|
|
/// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
|
2016-02-08 16:05:05 +01:00
|
|
|
Struct(Path, Vec<Field>, Option<P<Expr>>),
|
2012-08-03 18:01:30 -07:00
|
|
|
|
2015-08-11 17:35:22 +02:00
|
|
|
/// An array literal constructed from one repeated element.
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2016-03-11 00:05:53 +05:30
|
|
|
/// For example, `[1; 5]`. The first expression is the element
|
2015-03-19 00:56:37 +05:30
|
|
|
/// to be repeated; the second is the number of times to repeat it.
|
2016-02-08 16:05:05 +01:00
|
|
|
Repeat(P<Expr>, P<Expr>),
|
2012-10-27 17:14:09 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// No-op: used solely so we can pretty-print faithfully
|
2016-02-08 16:05:05 +01:00
|
|
|
Paren(P<Expr>),
|
2016-02-28 17:38:48 -05:00
|
|
|
|
|
|
|
/// `expr?`
|
|
|
|
Try(P<Expr>),
|
2016-12-26 14:34:03 +01:00
|
|
|
|
|
|
|
/// A `yield`, with an optional value to be yielded
|
|
|
|
Yield(Option<P<Expr>>),
|
2011-07-08 16:35:09 -07:00
|
|
|
}
|
|
|
|
|
2015-02-17 19:29:13 +02:00
|
|
|
/// The explicit Self type in a "qualified path". The actual
|
|
|
|
/// path, including the trait and the associated item, is stored
|
2015-03-19 00:48:08 -04:00
|
|
|
/// separately. `position` represents the index of the associated
|
2015-02-17 19:29:13 +02:00
|
|
|
/// item qualified with this Self type.
|
2014-08-05 19:44:21 -07:00
|
|
|
///
|
2017-06-20 15:15:16 +08:00
|
|
|
/// ```ignore (only-for-syntax-highlight)
|
2015-11-03 16:34:11 +00:00
|
|
|
/// <Vec<T> as a::b::Trait>::AssociatedItem
|
|
|
|
/// ^~~~~ ~~~~~~~~~~~~~~^
|
|
|
|
/// ty position = 3
|
2015-02-17 19:29:13 +02:00
|
|
|
///
|
2015-11-03 16:34:11 +00:00
|
|
|
/// <Vec<T>>::AssociatedItem
|
|
|
|
/// ^~~~~ ^
|
|
|
|
/// ty position = 0
|
|
|
|
/// ```
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2015-02-17 19:29:13 +02:00
|
|
|
pub struct QSelf {
|
|
|
|
pub ty: P<Ty>,
|
|
|
|
pub position: usize
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
|
|
|
|
2016-02-08 15:27:08 +01:00
|
|
|
/// A capture clause
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2016-02-08 15:27:08 +01:00
|
|
|
pub enum CaptureBy {
|
|
|
|
Value,
|
|
|
|
Ref,
|
2014-07-23 12:43:29 -07:00
|
|
|
}
|
|
|
|
|
2017-10-07 16:36:28 +02:00
|
|
|
/// The movability of a generator / closure literal
|
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
|
|
|
pub enum Movability {
|
|
|
|
Static,
|
|
|
|
Movable,
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub type Mac = Spanned<Mac_>;
|
2011-07-08 16:35:09 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Represents a macro invocation. The Path indicates which macro
|
|
|
|
/// is being invoked, and the vector of token-trees contains the source
|
|
|
|
/// of the macro invocation.
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-09-20 16:15:37 -04:00
|
|
|
/// NB: the additional ident for a macro_rules-style macro is actually
|
|
|
|
/// stored in the enclosing item. Oog.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2015-09-20 16:15:37 -04:00
|
|
|
pub struct Mac_ {
|
|
|
|
pub path: Path,
|
2017-02-21 05:05:59 +00:00
|
|
|
pub tts: ThinTokenStream,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mac_ {
|
|
|
|
pub fn stream(&self) -> TokenStream {
|
|
|
|
self.tts.clone().into()
|
|
|
|
}
|
2010-09-09 15:59:29 -07:00
|
|
|
}
|
|
|
|
|
2017-03-17 21:58:48 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub struct MacroDef {
|
|
|
|
pub tokens: ThinTokenStream,
|
2017-03-18 01:55:51 +00:00
|
|
|
pub legacy: bool,
|
2017-03-17 21:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MacroDef {
|
|
|
|
pub fn stream(&self) -> TokenStream {
|
|
|
|
self.tokens.clone().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2013-10-08 02:49:10 +02:00
|
|
|
pub enum StrStyle {
|
2015-03-17 17:42:20 +05:30
|
|
|
/// A regular string, like `"foo"`
|
2016-02-09 18:01:08 +01:00
|
|
|
Cooked,
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A raw string, like `r##"foo"##`
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2015-03-17 04:32:58 +05:30
|
|
|
/// The uint is the number of `#` symbols used
|
2016-02-09 18:01:08 +01:00
|
|
|
Raw(usize)
|
2013-10-08 02:49:10 +02:00
|
|
|
}
|
|
|
|
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A literal
|
2016-02-08 17:06:20 +01:00
|
|
|
pub type Lit = Spanned<LitKind>;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2014-08-05 09:59:03 +02:00
|
|
|
pub enum LitIntType {
|
2016-02-08 17:16:23 +01:00
|
|
|
Signed(IntTy),
|
|
|
|
Unsigned(UintTy),
|
|
|
|
Unsuffixed,
|
2014-08-05 09:59:03 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Literal kind.
|
|
|
|
///
|
|
|
|
/// E.g. `"foo"`, `42`, `12.34` or `bool`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-02-08 17:06:20 +01:00
|
|
|
pub enum LitKind {
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A string literal (`"foo"`)
|
2016-11-16 10:52:37 +00:00
|
|
|
Str(Symbol, StrStyle),
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A byte string (`b"foo"`)
|
2018-02-27 17:11:14 +01:00
|
|
|
ByteStr(Lrc<Vec<u8>>),
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A byte char (`b'f'`)
|
2016-02-08 17:06:20 +01:00
|
|
|
Byte(u8),
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A character literal (`'a'`)
|
2016-02-08 17:06:20 +01:00
|
|
|
Char(char),
|
2016-03-11 00:05:53 +05:30
|
|
|
/// An integer literal (`1`)
|
2016-08-23 03:56:52 +03:00
|
|
|
Int(u128, LitIntType),
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A float literal (`1f64` or `1E10f64`)
|
2016-11-16 10:52:37 +00:00
|
|
|
Float(Symbol, FloatTy),
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A float literal without a suffix (`1.0 or 1.0E10`)
|
2016-11-16 10:52:37 +00:00
|
|
|
FloatUnsuffixed(Symbol),
|
2015-03-17 04:32:58 +05:30
|
|
|
/// A boolean literal
|
2016-02-08 17:06:20 +01:00
|
|
|
Bool(bool),
|
2010-09-09 15:59:29 -07:00
|
|
|
}
|
|
|
|
|
2016-02-08 17:06:20 +01:00
|
|
|
impl LitKind {
|
2015-11-28 19:02:07 +00:00
|
|
|
/// Returns true if this literal is a string and false otherwise.
|
|
|
|
pub fn is_str(&self) -> bool {
|
|
|
|
match *self {
|
2016-02-08 17:06:20 +01:00
|
|
|
LitKind::Str(..) => true,
|
2015-11-28 19:02:07 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
|
|
|
|
/// Returns true if this literal has no suffix. Note: this will return true
|
|
|
|
/// for literals with prefixes such as raw strings and byte strings.
|
|
|
|
pub fn is_unsuffixed(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
// unsuffixed variants
|
2017-05-12 20:05:39 +02:00
|
|
|
LitKind::Str(..) |
|
|
|
|
LitKind::ByteStr(..) |
|
|
|
|
LitKind::Byte(..) |
|
|
|
|
LitKind::Char(..) |
|
|
|
|
LitKind::Int(_, LitIntType::Unsuffixed) |
|
|
|
|
LitKind::FloatUnsuffixed(..) |
|
2016-08-19 18:58:14 -07:00
|
|
|
LitKind::Bool(..) => true,
|
|
|
|
// suffixed variants
|
2017-05-12 20:05:39 +02:00
|
|
|
LitKind::Int(_, LitIntType::Signed(..)) |
|
|
|
|
LitKind::Int(_, LitIntType::Unsigned(..)) |
|
2016-08-19 18:58:14 -07:00
|
|
|
LitKind::Float(..) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if this literal has a suffix.
|
|
|
|
pub fn is_suffixed(&self) -> bool {
|
|
|
|
!self.is_unsuffixed()
|
|
|
|
}
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
|
2010-11-03 16:43:12 -07:00
|
|
|
// NB: If you change this, you'll probably want to change the corresponding
|
2010-12-21 12:13:51 -08:00
|
|
|
// type structure in middle/ty.rs as well.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct MutTy {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ty: P<Ty>,
|
|
|
|
pub mutbl: Mutability,
|
2013-01-14 21:36:27 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2015-03-11 08:38:27 +02:00
|
|
|
/// Represents a method's signature in a trait declaration,
|
|
|
|
/// or in an implementation.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2015-03-11 08:38:27 +02:00
|
|
|
pub struct MethodSig {
|
2014-12-09 10:36:46 -05:00
|
|
|
pub unsafety: Unsafety,
|
2016-08-10 16:20:12 -07:00
|
|
|
pub constness: Spanned<Constness>,
|
2014-05-28 22:26:56 -07:00
|
|
|
pub abi: Abi,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub decl: P<FnDecl>,
|
2013-01-15 15:03:49 -08:00
|
|
|
}
|
2011-06-03 15:26:03 -07:00
|
|
|
|
2015-12-18 14:38:28 -08:00
|
|
|
/// Represents an item declaration within a trait declaration,
|
|
|
|
/// possibly including a default implementation. A trait item is
|
|
|
|
/// either required (meaning it doesn't have an implementation, just a
|
|
|
|
/// signature) or provided (meaning it has a default implementation).
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2015-03-10 12:28:44 +02:00
|
|
|
pub struct TraitItem {
|
|
|
|
pub id: NodeId,
|
|
|
|
pub ident: Ident,
|
|
|
|
pub attrs: Vec<Attribute>,
|
2017-09-21 22:18:47 -04:00
|
|
|
pub generics: Generics,
|
2016-02-09 17:54:11 +01:00
|
|
|
pub node: TraitItemKind,
|
2015-03-10 12:28:44 +02:00
|
|
|
pub span: Span,
|
2017-07-12 09:50:05 -07:00
|
|
|
/// See `Item::tokens` for what this is
|
|
|
|
pub tokens: Option<TokenStream>,
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-02-09 17:54:11 +01:00
|
|
|
pub enum TraitItemKind {
|
|
|
|
Const(P<Ty>, Option<P<Expr>>),
|
|
|
|
Method(MethodSig, Option<P<Block>>),
|
|
|
|
Type(TyParamBounds, Option<P<Ty>>),
|
2016-06-11 02:00:07 +01:00
|
|
|
Macro(Mac),
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2015-03-10 12:28:44 +02:00
|
|
|
pub struct ImplItem {
|
2014-08-05 19:44:21 -07:00
|
|
|
pub id: NodeId,
|
|
|
|
pub ident: Ident,
|
|
|
|
pub vis: Visibility,
|
2015-12-18 14:38:28 -08:00
|
|
|
pub defaultness: Defaultness,
|
2014-08-05 19:44:21 -07:00
|
|
|
pub attrs: Vec<Attribute>,
|
2017-09-21 22:18:47 -04:00
|
|
|
pub generics: Generics,
|
2015-11-13 14:15:04 +01:00
|
|
|
pub node: ImplItemKind,
|
2015-03-10 12:28:44 +02:00
|
|
|
pub span: Span,
|
2017-07-12 09:50:05 -07:00
|
|
|
/// See `Item::tokens` for what this is
|
|
|
|
pub tokens: Option<TokenStream>,
|
2015-03-10 12:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2015-11-13 14:15:04 +01:00
|
|
|
pub enum ImplItemKind {
|
|
|
|
Const(P<Ty>, P<Expr>),
|
|
|
|
Method(MethodSig, P<Block>),
|
|
|
|
Type(P<Ty>),
|
|
|
|
Macro(Mac),
|
2012-07-10 13:44:20 -07:00
|
|
|
}
|
|
|
|
|
2017-08-14 18:19:42 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy,
|
|
|
|
PartialOrd, Ord)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum IntTy {
|
2018-01-04 03:12:04 +02:00
|
|
|
Isize,
|
2016-02-08 16:20:57 +01:00
|
|
|
I8,
|
|
|
|
I16,
|
|
|
|
I32,
|
|
|
|
I64,
|
2016-08-23 03:56:52 +03:00
|
|
|
I128,
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
2011-12-07 21:06:12 +01:00
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Debug for IntTy {
|
2014-12-20 00:09:35 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-20 15:45:07 -08:00
|
|
|
fmt::Display::fmt(self, f)
|
2014-12-20 00:09:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Display for IntTy {
|
2014-02-19 18:56:33 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-11-28 19:02:07 +00:00
|
|
|
write!(f, "{}", self.ty_to_string())
|
2013-01-22 07:01:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 10:44:20 -07:00
|
|
|
impl IntTy {
|
2015-11-28 19:02:07 +00:00
|
|
|
pub fn ty_to_string(&self) -> &'static str {
|
|
|
|
match *self {
|
2018-01-04 03:12:04 +02:00
|
|
|
IntTy::Isize => "isize",
|
2016-02-08 16:20:57 +01:00
|
|
|
IntTy::I8 => "i8",
|
|
|
|
IntTy::I16 => "i16",
|
|
|
|
IntTy::I32 => "i32",
|
2016-08-23 03:56:52 +03:00
|
|
|
IntTy::I64 => "i64",
|
|
|
|
IntTy::I128 => "i128",
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:56:52 +03:00
|
|
|
pub fn val_to_string(&self, val: i128) -> String {
|
2016-08-24 15:58:40 +03:00
|
|
|
// cast to a u128 so we can correctly print INT128_MIN. All integral types
|
|
|
|
// are parsed as u128, so we wouldn't want to print an extra negative
|
2015-11-28 19:02:07 +00:00
|
|
|
// sign.
|
2016-08-23 03:56:52 +03:00
|
|
|
format!("{}{}", val as u128, self.ty_to_string())
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 15:46:51 -07:00
|
|
|
pub fn bit_width(&self) -> Option<usize> {
|
|
|
|
Some(match *self {
|
2018-01-04 03:12:04 +02:00
|
|
|
IntTy::Isize => return None,
|
2016-02-08 16:20:57 +01:00
|
|
|
IntTy::I8 => 8,
|
|
|
|
IntTy::I16 => 16,
|
|
|
|
IntTy::I32 => 32,
|
|
|
|
IntTy::I64 => 64,
|
2016-08-23 03:56:52 +03:00
|
|
|
IntTy::I128 => 128,
|
2015-08-14 15:46:51 -07:00
|
|
|
})
|
|
|
|
}
|
2014-06-18 10:44:20 -07:00
|
|
|
}
|
|
|
|
|
2017-08-14 18:19:42 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy,
|
|
|
|
PartialOrd, Ord)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum UintTy {
|
2018-01-04 03:12:04 +02:00
|
|
|
Usize,
|
2016-02-08 16:20:57 +01:00
|
|
|
U8,
|
|
|
|
U16,
|
|
|
|
U32,
|
|
|
|
U64,
|
2016-08-23 03:56:52 +03:00
|
|
|
U128,
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
2011-12-07 21:06:12 +01:00
|
|
|
|
2014-06-18 10:44:20 -07:00
|
|
|
impl UintTy {
|
2015-11-28 19:02:07 +00:00
|
|
|
pub fn ty_to_string(&self) -> &'static str {
|
|
|
|
match *self {
|
2018-01-04 03:12:04 +02:00
|
|
|
UintTy::Usize => "usize",
|
2016-02-08 16:20:57 +01:00
|
|
|
UintTy::U8 => "u8",
|
|
|
|
UintTy::U16 => "u16",
|
|
|
|
UintTy::U32 => "u32",
|
2016-08-23 03:56:52 +03:00
|
|
|
UintTy::U64 => "u64",
|
|
|
|
UintTy::U128 => "u128",
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:56:52 +03:00
|
|
|
pub fn val_to_string(&self, val: u128) -> String {
|
2015-11-28 19:02:07 +00:00
|
|
|
format!("{}{}", val, self.ty_to_string())
|
|
|
|
}
|
|
|
|
|
2015-08-14 15:46:51 -07:00
|
|
|
pub fn bit_width(&self) -> Option<usize> {
|
|
|
|
Some(match *self {
|
2018-01-04 03:12:04 +02:00
|
|
|
UintTy::Usize => return None,
|
2016-02-08 16:20:57 +01:00
|
|
|
UintTy::U8 => 8,
|
|
|
|
UintTy::U16 => 16,
|
|
|
|
UintTy::U32 => 32,
|
|
|
|
UintTy::U64 => 64,
|
2016-08-23 03:56:52 +03:00
|
|
|
UintTy::U128 => 128,
|
2015-08-14 15:46:51 -07:00
|
|
|
})
|
|
|
|
}
|
2014-06-18 10:44:20 -07:00
|
|
|
}
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Debug for UintTy {
|
2014-12-20 00:09:35 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-20 15:45:07 -08:00
|
|
|
fmt::Display::fmt(self, f)
|
2014-12-20 00:09:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Display for UintTy {
|
2014-02-19 18:56:33 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-11-28 19:02:07 +00:00
|
|
|
write!(f, "{}", self.ty_to_string())
|
2013-01-22 07:01:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 18:19:42 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy,
|
|
|
|
PartialOrd, Ord)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum FloatTy {
|
2016-02-08 16:09:01 +01:00
|
|
|
F32,
|
|
|
|
F64,
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
2011-07-05 11:48:19 +02:00
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Debug for FloatTy {
|
2014-12-20 00:09:35 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-20 15:45:07 -08:00
|
|
|
fmt::Display::fmt(self, f)
|
2014-12-20 00:09:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Display for FloatTy {
|
2014-02-19 18:56:33 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-11-28 19:02:07 +00:00
|
|
|
write!(f, "{}", self.ty_to_string())
|
2013-01-22 07:01:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 10:44:20 -07:00
|
|
|
impl FloatTy {
|
2015-11-28 19:02:07 +00:00
|
|
|
pub fn ty_to_string(&self) -> &'static str {
|
|
|
|
match *self {
|
2016-02-08 16:09:01 +01:00
|
|
|
FloatTy::F32 => "f32",
|
|
|
|
FloatTy::F64 => "f64",
|
2015-11-28 19:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 15:46:51 -07:00
|
|
|
pub fn bit_width(&self) -> usize {
|
|
|
|
match *self {
|
2016-02-08 16:09:01 +01:00
|
|
|
FloatTy::F32 => 32,
|
|
|
|
FloatTy::F64 => 64,
|
2015-08-14 15:46:51 -07:00
|
|
|
}
|
|
|
|
}
|
2014-06-18 10:44:20 -07:00
|
|
|
}
|
|
|
|
|
2014-11-29 17:08:30 +13:00
|
|
|
// Bind a type to an associated type: `A=Foo`.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-29 17:08:30 +13:00
|
|
|
pub struct TypeBinding {
|
|
|
|
pub id: NodeId,
|
|
|
|
pub ident: Ident,
|
|
|
|
pub ty: P<Ty>,
|
|
|
|
pub span: Span,
|
|
|
|
}
|
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
|
2013-01-29 13:54:06 -08:00
|
|
|
pub struct Ty {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
2016-02-08 16:53:21 +01:00
|
|
|
pub node: TyKind,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub span: Span,
|
2013-01-15 14:59:39 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2015-06-17 09:56:27 +03:00
|
|
|
impl fmt::Debug for Ty {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "type({})", pprust::ty_to_string(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct BareFnTy {
|
2014-12-09 10:36:46 -05:00
|
|
|
pub unsafety: Unsafety,
|
2014-04-02 01:19:41 -07:00
|
|
|
pub abi: Abi,
|
2017-10-16 21:07:26 +02:00
|
|
|
pub generic_params: Vec<GenericParam>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub decl: P<FnDecl>
|
2012-11-04 20:41:00 -08:00
|
|
|
}
|
|
|
|
|
2014-11-10 19:11:27 +05:30
|
|
|
/// The different kinds of types recognized by the compiler
|
2016-06-24 13:14:34 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-02-08 16:53:21 +01:00
|
|
|
pub enum TyKind {
|
2016-09-20 16:54:24 +02:00
|
|
|
/// A variable-length slice (`[T]`)
|
|
|
|
Slice(P<Ty>),
|
2015-02-23 13:06:20 -08:00
|
|
|
/// A fixed length array (`[T; n]`)
|
2016-09-20 16:54:24 +02:00
|
|
|
Array(P<Ty>, P<Expr>),
|
2014-11-10 19:11:27 +05:30
|
|
|
/// A raw pointer (`*const T` or `*mut T`)
|
2016-02-08 16:53:21 +01:00
|
|
|
Ptr(MutTy),
|
2014-11-10 19:11:27 +05:30
|
|
|
/// A reference (`&'a T` or `&'a mut T`)
|
2016-02-08 16:53:21 +01:00
|
|
|
Rptr(Option<Lifetime>, MutTy),
|
2015-01-17 23:33:05 +00:00
|
|
|
/// A bare function (e.g. `fn(usize) -> bool`)
|
2016-02-08 16:53:21 +01:00
|
|
|
BareFn(P<BareFnTy>),
|
2016-08-02 15:56:20 +08:00
|
|
|
/// The never type (`!`)
|
|
|
|
Never,
|
2014-11-10 19:11:27 +05:30
|
|
|
/// A tuple (`(A, B, C, D,...)`)
|
2016-02-08 16:53:21 +01:00
|
|
|
Tup(Vec<P<Ty>> ),
|
2015-02-17 19:29:13 +02:00
|
|
|
/// A path (`module::module::...::Type`), optionally
|
|
|
|
/// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
|
2014-11-10 19:11:27 +05:30
|
|
|
///
|
|
|
|
/// Type parameters are stored in the Path itself
|
2016-02-08 16:53:21 +01:00
|
|
|
Path(Option<QSelf>, Path),
|
2017-01-16 23:33:45 +03:00
|
|
|
/// A trait object type `Bound1 + Bound2 + Bound3`
|
|
|
|
/// where `Bound` is a trait or a lifetime.
|
2017-10-10 17:33:19 +03:00
|
|
|
TraitObject(TyParamBounds, TraitObjectSyntax),
|
2017-01-16 23:33:45 +03:00
|
|
|
/// An `impl Bound1 + Bound2 + Bound3` type
|
|
|
|
/// where `Bound` is a trait or a lifetime.
|
2016-08-01 04:25:32 +03:00
|
|
|
ImplTrait(TyParamBounds),
|
2014-06-09 13:12:30 -07:00
|
|
|
/// No-op; kept solely so that we can pretty-print faithfully
|
2016-02-08 16:53:21 +01:00
|
|
|
Paren(P<Ty>),
|
2014-11-10 19:11:27 +05:30
|
|
|
/// Unused for now
|
2016-02-08 16:53:21 +01:00
|
|
|
Typeof(P<Expr>),
|
|
|
|
/// TyKind::Infer means the type should be inferred instead of it having been
|
2014-06-09 13:12:30 -07:00
|
|
|
/// specified. This can appear anywhere in a type.
|
2016-02-08 16:53:21 +01:00
|
|
|
Infer,
|
2016-03-06 15:54:44 +03:00
|
|
|
/// Inferred type of a `self` or `&self` argument in a method.
|
|
|
|
ImplicitSelf,
|
2015-07-25 21:30:35 -07:00
|
|
|
// A macro in the type position.
|
2016-02-08 16:53:21 +01:00
|
|
|
Mac(Mac),
|
2017-03-28 18:56:29 -07:00
|
|
|
/// Placeholder for a kind that has failed to be defined.
|
|
|
|
Err,
|
2010-09-09 15:59:29 -07:00
|
|
|
}
|
2017-10-10 17:33:19 +03:00
|
|
|
|
|
|
|
/// Syntax used to declare a trait object.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum TraitObjectSyntax {
|
|
|
|
Dyn,
|
|
|
|
None,
|
|
|
|
}
|
2010-09-09 15:59:29 -07:00
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Inline assembly dialect.
|
|
|
|
///
|
2017-12-31 17:17:01 +01:00
|
|
|
/// E.g. `"intel"` as in `asm!("mov eax, 2" : "={eax}"(result) : : : "intel")`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum AsmDialect {
|
2015-09-21 11:45:04 +02:00
|
|
|
Att,
|
|
|
|
Intel,
|
2013-03-27 14:42:19 -07:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Inline assembly.
|
|
|
|
///
|
2017-12-31 17:17:01 +01:00
|
|
|
/// E.g. `"={eax}"(result)` as in `asm!("mov eax, 2" : "={eax}"(result) : : : "intel")`
|
2015-12-05 08:18:24 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub struct InlineAsmOutput {
|
2016-11-16 10:52:37 +00:00
|
|
|
pub constraint: Symbol,
|
2015-12-05 08:18:24 +00:00
|
|
|
pub expr: P<Expr>,
|
|
|
|
pub is_rw: bool,
|
|
|
|
pub is_indirect: bool,
|
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Inline assembly.
|
|
|
|
///
|
|
|
|
/// E.g. `asm!("NOP");`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct InlineAsm {
|
2016-11-16 10:52:37 +00:00
|
|
|
pub asm: Symbol,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub asm_str_style: StrStyle,
|
2015-12-05 08:18:24 +00:00
|
|
|
pub outputs: Vec<InlineAsmOutput>,
|
2016-11-16 10:52:37 +00:00
|
|
|
pub inputs: Vec<(Symbol, P<Expr>)>,
|
|
|
|
pub clobbers: Vec<Symbol>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub volatile: bool,
|
|
|
|
pub alignstack: bool,
|
2014-09-27 01:33:36 -07:00
|
|
|
pub dialect: AsmDialect,
|
2017-03-17 04:04:41 +00:00
|
|
|
pub ctxt: SyntaxContext,
|
2013-03-27 13:42:21 -07:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// An argument in a function header.
|
|
|
|
///
|
|
|
|
/// E.g. `bar: usize` as in `fn foo(bar: usize)`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Arg {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ty: P<Ty>,
|
2014-05-18 01:13:20 +03:00
|
|
|
pub pat: P<Pat>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
2013-01-15 16:05:20 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
/// Alternative representation for `Arg`s describing `self` parameter of methods.
|
2016-06-24 13:14:34 +02:00
|
|
|
///
|
|
|
|
/// E.g. `&mut self` as in `fn foo(&mut self)`
|
2016-05-08 21:18:21 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum SelfKind {
|
|
|
|
/// `self`, `mut self`
|
2016-03-06 15:54:44 +03:00
|
|
|
Value(Mutability),
|
2016-05-08 21:18:21 +03:00
|
|
|
/// `&'lt self`, `&'lt mut self`
|
2016-03-06 15:54:44 +03:00
|
|
|
Region(Option<Lifetime>, Mutability),
|
2016-05-08 21:18:21 +03:00
|
|
|
/// `self: TYPE`, `mut self: TYPE`
|
2016-03-06 15:54:44 +03:00
|
|
|
Explicit(P<Ty>, Mutability),
|
2016-05-08 21:18:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type ExplicitSelf = Spanned<SelfKind>;
|
|
|
|
|
2014-01-27 14:18:36 +02:00
|
|
|
impl Arg {
|
2016-05-08 21:18:21 +03:00
|
|
|
pub fn to_self(&self) -> Option<ExplicitSelf> {
|
2016-03-06 15:54:44 +03:00
|
|
|
if let PatKind::Ident(BindingMode::ByValue(mutbl), ident, _) = self.pat.node {
|
2016-05-08 21:18:21 +03:00
|
|
|
if ident.node.name == keywords::SelfValue.name() {
|
|
|
|
return match self.ty.node {
|
2016-03-06 15:54:44 +03:00
|
|
|
TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
|
|
|
|
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::ImplicitSelf => {
|
2016-03-06 15:54:44 +03:00
|
|
|
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
|
2016-05-08 21:18:21 +03:00
|
|
|
}
|
2017-03-15 00:22:48 +00:00
|
|
|
_ => Some(respan(self.pat.span.to(self.ty.span),
|
2016-03-06 15:54:44 +03:00
|
|
|
SelfKind::Explicit(self.ty.clone(), mutbl))),
|
2016-05-08 21:18:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
pub fn is_self(&self) -> bool {
|
|
|
|
if let PatKind::Ident(_, ident, _) = self.pat.node {
|
|
|
|
ident.node.name == keywords::SelfValue.name()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_self(eself: ExplicitSelf, eself_ident: SpannedIdent) -> Arg {
|
2017-03-15 00:22:48 +00:00
|
|
|
let span = eself.span.to(eself_ident.span);
|
2016-05-08 21:18:21 +03:00
|
|
|
let infer_ty = P(Ty {
|
|
|
|
id: DUMMY_NODE_ID,
|
2016-03-06 15:54:44 +03:00
|
|
|
node: TyKind::ImplicitSelf,
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-05-08 21:18:21 +03:00
|
|
|
});
|
2016-12-04 03:18:11 +02:00
|
|
|
let arg = |mutbl, ty| Arg {
|
2016-03-06 15:54:44 +03:00
|
|
|
pat: P(Pat {
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
node: PatKind::Ident(BindingMode::ByValue(mutbl), eself_ident, None),
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-03-06 15:54:44 +03:00
|
|
|
}),
|
2017-08-06 22:54:09 -07:00
|
|
|
ty,
|
2016-05-08 21:18:21 +03:00
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
};
|
|
|
|
match eself.node {
|
2016-12-04 03:18:11 +02:00
|
|
|
SelfKind::Explicit(ty, mutbl) => arg(mutbl, ty),
|
|
|
|
SelfKind::Value(mutbl) => arg(mutbl, infer_ty),
|
2016-03-06 15:54:44 +03:00
|
|
|
SelfKind::Region(lt, mutbl) => arg(Mutability::Immutable, P(Ty {
|
2016-05-08 21:18:21 +03:00
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
node: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl: mutbl }),
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-12-04 03:18:11 +02:00
|
|
|
})),
|
2016-05-08 21:18:21 +03:00
|
|
|
}
|
|
|
|
}
|
2014-01-27 14:18:36 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Header (not the body) of a function declaration.
|
|
|
|
///
|
|
|
|
/// E.g. `fn foo(bar: baz)`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct FnDecl {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub inputs: Vec<Arg>,
|
2014-11-09 16:14:15 +01:00
|
|
|
pub output: FunctionRetTy,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub variadic: bool
|
2013-01-15 16:05:20 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
impl FnDecl {
|
|
|
|
pub fn get_self(&self) -> Option<ExplicitSelf> {
|
|
|
|
self.inputs.get(0).and_then(Arg::to_self)
|
|
|
|
}
|
|
|
|
pub fn has_self(&self) -> bool {
|
|
|
|
self.inputs.get(0).map(Arg::is_self).unwrap_or(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 09:51:31 -03:00
|
|
|
/// Is the trait definition an auto trait?
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum IsAuto {
|
|
|
|
Yes,
|
|
|
|
No
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-12-09 10:36:46 -05:00
|
|
|
pub enum Unsafety {
|
|
|
|
Unsafe,
|
|
|
|
Normal,
|
2011-05-04 11:28:13 -07:00
|
|
|
}
|
|
|
|
|
2015-02-25 22:05:07 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum Constness {
|
|
|
|
Const,
|
|
|
|
NotConst,
|
|
|
|
}
|
|
|
|
|
2015-12-18 14:38:28 -08:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum Defaultness {
|
|
|
|
Default,
|
|
|
|
Final,
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Display for Unsafety {
|
2014-02-19 18:56:33 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-20 15:45:07 -08:00
|
|
|
fmt::Display::fmt(match *self {
|
2014-12-20 00:09:35 -08:00
|
|
|
Unsafety::Normal => "normal",
|
|
|
|
Unsafety::Unsafe => "unsafe",
|
|
|
|
}, f)
|
2013-01-08 14:00:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-28 23:33:18 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
|
|
|
|
pub enum ImplPolarity {
|
2015-03-17 05:06:13 +05:30
|
|
|
/// `impl Trait for Type`
|
2014-12-28 23:33:18 +01:00
|
|
|
Positive,
|
2015-03-17 05:06:13 +05:30
|
|
|
/// `impl !Trait for Type`
|
2014-12-28 23:33:18 +01:00
|
|
|
Negative,
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Debug for ImplPolarity {
|
2014-12-28 23:33:18 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
ImplPolarity::Positive => "positive".fmt(f),
|
|
|
|
ImplPolarity::Negative => "negative".fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-09 16:14:15 +01:00
|
|
|
pub enum FunctionRetTy {
|
2015-03-19 00:56:37 +05:30
|
|
|
/// Return type is not specified.
|
|
|
|
///
|
|
|
|
/// Functions default to `()` and
|
2015-01-18 22:49:19 +09:00
|
|
|
/// closures default to inference. Span points to where return
|
|
|
|
/// type would be inserted.
|
2016-02-08 15:04:11 +01:00
|
|
|
Default(Span),
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Everything else
|
2016-02-08 15:04:11 +01:00
|
|
|
Ty(P<Ty>),
|
2014-11-09 16:14:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionRetTy {
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match *self {
|
2016-02-08 15:04:11 +01:00
|
|
|
FunctionRetTy::Default(span) => span,
|
|
|
|
FunctionRetTy::Ty(ref ty) => ty.span,
|
2014-11-09 16:14:15 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-14 19:02:30 -07:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Module declaration.
|
|
|
|
///
|
|
|
|
/// E.g. `mod foo;` or `mod foo { .. }`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Mod {
|
2014-04-27 05:05:45 +09:00
|
|
|
/// A span from the first token past `{` to the last token until `}`.
|
|
|
|
/// For `mod foo;`, the inner span ranges from the first token
|
|
|
|
/// to the last token in the external file.
|
|
|
|
pub inner: Span,
|
2014-05-18 01:13:20 +03:00
|
|
|
pub items: Vec<P<Item>>,
|
2013-01-15 16:05:20 -08:00
|
|
|
}
|
2010-08-18 09:00:10 -07:00
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Foreign module declaration.
|
|
|
|
///
|
|
|
|
/// E.g. `extern { .. }` or `extern C { .. }`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct ForeignMod {
|
2014-04-02 01:19:41 -07:00
|
|
|
pub abi: Abi,
|
2016-02-11 23:33:09 +03:00
|
|
|
pub items: Vec<ForeignItem>,
|
2013-01-15 16:05:20 -08:00
|
|
|
}
|
2011-02-01 13:40:04 -05:00
|
|
|
|
2017-03-15 21:27:40 -05:00
|
|
|
/// Global inline assembly
|
|
|
|
///
|
|
|
|
/// aka module-level assembly or file-scoped assembly
|
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
|
|
|
pub struct GlobalAsm {
|
|
|
|
pub asm: Symbol,
|
|
|
|
pub ctxt: SyntaxContext,
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct EnumDef {
|
2016-02-11 23:33:09 +03:00
|
|
|
pub variants: Vec<Variant>,
|
2013-01-15 16:05:20 -08:00
|
|
|
}
|
2012-10-07 10:31:34 -07:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Variant_ {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub name: Ident,
|
|
|
|
pub attrs: Vec<Attribute>,
|
2015-10-25 18:33:51 +03:00
|
|
|
pub data: VariantData,
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Explicit discriminant, e.g. `Foo = 1`
|
2014-05-18 01:13:20 +03:00
|
|
|
pub disr_expr: Option<P<Expr>>,
|
2013-01-15 16:05:20 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub type Variant = Spanned<Variant_>;
|
2010-11-24 11:36:35 -08:00
|
|
|
|
2018-03-12 23:16:09 +03:00
|
|
|
/// Part of `use` item to the right of its prefix.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2017-09-26 23:04:00 +02:00
|
|
|
pub enum UseTreeKind {
|
2018-03-12 23:16:09 +03:00
|
|
|
/// `use prefix` or `use prefix as rename`
|
2018-03-09 18:58:44 +03:00
|
|
|
Simple(Option<Ident>),
|
2018-03-12 23:16:09 +03:00
|
|
|
/// `use prefix::{...}`
|
2017-09-26 23:04:00 +02:00
|
|
|
Nested(Vec<(UseTree, NodeId)>),
|
2018-03-12 23:16:09 +03:00
|
|
|
/// `use prefix::*`
|
2018-03-09 18:58:44 +03:00
|
|
|
Glob,
|
2011-01-01 12:55:18 -05:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:16:09 +03:00
|
|
|
/// A tree of paths sharing common prefixes.
|
|
|
|
/// Used in `use` items both at top-level and inside of braces in import groups.
|
2017-09-26 23:04:00 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub struct UseTree {
|
|
|
|
pub prefix: Path,
|
2018-03-09 18:58:44 +03:00
|
|
|
pub kind: UseTreeKind,
|
2017-09-26 23:04:00 +02:00
|
|
|
pub span: Span,
|
2016-05-22 18:07:28 +03:00
|
|
|
}
|
|
|
|
|
2018-03-09 18:58:44 +03:00
|
|
|
impl UseTree {
|
|
|
|
pub fn ident(&self) -> Ident {
|
|
|
|
match self.kind {
|
|
|
|
UseTreeKind::Simple(Some(rename)) => rename,
|
|
|
|
UseTreeKind::Simple(None) =>
|
|
|
|
self.prefix.segments.last().expect("empty prefix in a simple import").identifier,
|
|
|
|
_ => panic!("`UseTree::ident` can only be used on a simple import"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Distinguishes between Attributes that decorate items and Attributes that
|
|
|
|
/// are contained as statements within items. These two cases need to be
|
|
|
|
/// distinguished for pretty-printing.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2013-07-19 21:51:37 +10:00
|
|
|
pub enum AttrStyle {
|
2015-10-01 18:03:34 +02:00
|
|
|
Outer,
|
|
|
|
Inner,
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
2011-06-14 16:13:19 -07:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
|
2015-01-17 23:33:05 +00:00
|
|
|
pub struct AttrId(pub usize);
|
2014-05-20 00:07:24 -07:00
|
|
|
|
2016-11-14 12:00:25 +00:00
|
|
|
/// Meta-data associated with an item
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Doc-comments are promoted to attributes that have is_sugared_doc = true
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-11-14 12:00:25 +00:00
|
|
|
pub struct Attribute {
|
2014-05-20 00:07:24 -07:00
|
|
|
pub id: AttrId,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub style: AttrStyle,
|
2017-03-03 09:23:59 +00:00
|
|
|
pub path: Path,
|
|
|
|
pub tokens: TokenStream,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub is_sugared_doc: bool,
|
2016-11-14 12:00:25 +00:00
|
|
|
pub span: Span,
|
2013-01-13 16:51:48 -08:00
|
|
|
}
|
2011-06-14 16:13:19 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// TraitRef's appear in impls.
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2014-06-09 13:12:30 -07:00
|
|
|
/// resolve maps each TraitRef's ref_id to its defining trait; that's all
|
|
|
|
/// that the ref_id is for. The impl_id maps to the "self type" of this impl.
|
2016-02-09 11:36:51 +01:00
|
|
|
/// If this impl is an ItemKind::Impl, the impl_id is redundant (it could be the
|
2014-06-09 13:12:30 -07:00
|
|
|
/// same as the impl's node id).
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct TraitRef {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub path: Path,
|
|
|
|
pub ref_id: NodeId,
|
2014-11-07 06:53:45 -05:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-11-07 06:53:45 -05:00
|
|
|
pub struct PolyTraitRef {
|
|
|
|
/// The `'a` in `<'a> Foo<&'a T>`
|
2017-10-16 21:07:26 +02:00
|
|
|
pub bound_generic_params: Vec<GenericParam>,
|
2014-11-07 06:53:45 -05:00
|
|
|
|
|
|
|
/// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
|
2014-12-24 19:38:10 +13:00
|
|
|
pub trait_ref: TraitRef,
|
2015-02-05 21:46:01 +13:00
|
|
|
|
|
|
|
pub span: Span,
|
2013-01-15 16:05:20 -08:00
|
|
|
}
|
2012-04-11 16:18:00 -07:00
|
|
|
|
2017-03-17 00:47:32 +03:00
|
|
|
impl PolyTraitRef {
|
2017-10-16 21:07:26 +02:00
|
|
|
pub fn new(generic_params: Vec<GenericParam>, path: Path, span: Span) -> Self {
|
2017-03-17 00:47:32 +03:00
|
|
|
PolyTraitRef {
|
2017-10-16 21:07:26 +02:00
|
|
|
bound_generic_params: generic_params,
|
2017-03-17 00:47:32 +03:00
|
|
|
trait_ref: TraitRef { path: path, ref_id: DUMMY_NODE_ID },
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2017-03-17 00:47:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 14:43:47 -07:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum CrateSugar {
|
|
|
|
/// Source is `pub(crate)`
|
|
|
|
PubCrate,
|
|
|
|
|
|
|
|
/// Source is (just) `crate`
|
|
|
|
JustCrate,
|
|
|
|
}
|
|
|
|
|
2018-01-29 14:12:09 +09:00
|
|
|
pub type Visibility = Spanned<VisibilityKind>;
|
|
|
|
|
2016-03-23 10:17:34 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2018-01-29 14:12:09 +09:00
|
|
|
pub enum VisibilityKind {
|
2014-01-09 15:05:33 +02:00
|
|
|
Public,
|
2018-01-29 14:12:09 +09:00
|
|
|
Crate(CrateSugar),
|
2016-03-31 19:10:38 +00:00
|
|
|
Restricted { path: P<Path>, id: NodeId },
|
2014-01-09 15:05:33 +02:00
|
|
|
Inherited,
|
2013-07-02 12:47:32 -07:00
|
|
|
}
|
2012-05-08 16:06:24 +02:00
|
|
|
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Field of a struct.
|
|
|
|
///
|
|
|
|
/// E.g. `bar: usize` as in `struct Foo { bar: usize }`
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-04-06 11:19:10 +03:00
|
|
|
pub struct StructField {
|
|
|
|
pub span: Span,
|
2016-04-02 16:47:53 +03:00
|
|
|
pub ident: Option<Ident>,
|
|
|
|
pub vis: Visibility,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
|
|
|
pub ty: P<Ty>,
|
|
|
|
pub attrs: Vec<Attribute>,
|
2013-01-13 15:28:49 -08:00
|
|
|
}
|
2012-08-15 15:53:58 -07:00
|
|
|
|
2015-10-13 16:18:33 +03:00
|
|
|
/// Fields and Ids of enum variants and structs
|
|
|
|
///
|
|
|
|
/// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
|
|
|
|
/// variant kinds) and an Id of the variant's constructor (not relevant for `Struct`-variants).
|
|
|
|
/// One shared Id can be successfully used for these two purposes.
|
|
|
|
/// Id of the whole enum lives in `Item`.
|
|
|
|
///
|
|
|
|
/// For structs: `NodeId` represents an Id of the structure's constructor, so it is not actually
|
|
|
|
/// used for `Struct`-structs (but still presents). Structures don't have an analogue of "Id of
|
|
|
|
/// the variant itself" from enum variants.
|
|
|
|
/// Id of the whole struct lives in `Item`.
|
2015-10-08 23:45:46 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2015-10-10 03:28:40 +03:00
|
|
|
pub enum VariantData {
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Struct variant.
|
|
|
|
///
|
|
|
|
/// E.g. `Bar { .. }` as in `enum Foo { Bar { .. } }`
|
2015-10-10 03:28:40 +03:00
|
|
|
Struct(Vec<StructField>, NodeId),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Tuple variant.
|
|
|
|
///
|
|
|
|
/// E.g. `Bar(..)` as in `enum Foo { Bar(..) }`
|
2015-10-10 03:28:40 +03:00
|
|
|
Tuple(Vec<StructField>, NodeId),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// Unit variant.
|
|
|
|
///
|
|
|
|
/// E.g. `Bar = ..` as in `enum Foo { Bar = .. }`
|
2015-10-10 03:28:40 +03:00
|
|
|
Unit(NodeId),
|
2015-10-08 23:45:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VariantData {
|
2015-10-25 18:33:51 +03:00
|
|
|
pub fn fields(&self) -> &[StructField] {
|
2015-10-10 03:28:40 +03:00
|
|
|
match *self {
|
2015-10-25 18:33:51 +03:00
|
|
|
VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => fields,
|
|
|
|
_ => &[],
|
|
|
|
}
|
2015-10-08 23:45:46 +03:00
|
|
|
}
|
2015-10-10 03:28:40 +03:00
|
|
|
pub fn id(&self) -> NodeId {
|
|
|
|
match *self {
|
|
|
|
VariantData::Struct(_, id) | VariantData::Tuple(_, id) | VariantData::Unit(id) => id
|
|
|
|
}
|
|
|
|
}
|
2015-10-08 23:45:46 +03:00
|
|
|
pub fn is_struct(&self) -> bool {
|
2015-10-10 03:28:40 +03:00
|
|
|
if let VariantData::Struct(..) = *self { true } else { false }
|
2015-10-08 23:45:46 +03:00
|
|
|
}
|
|
|
|
pub fn is_tuple(&self) -> bool {
|
2015-10-10 03:28:40 +03:00
|
|
|
if let VariantData::Tuple(..) = *self { true } else { false }
|
2015-10-08 23:45:46 +03:00
|
|
|
}
|
|
|
|
pub fn is_unit(&self) -> bool {
|
2015-10-10 03:28:40 +03:00
|
|
|
if let VariantData::Unit(..) = *self { true } else { false }
|
2015-10-08 23:45:46 +03:00
|
|
|
}
|
2013-01-13 13:45:57 -08:00
|
|
|
}
|
2012-08-07 15:34:07 -07:00
|
|
|
|
2015-03-17 05:06:13 +05:30
|
|
|
/// An item
|
|
|
|
///
|
|
|
|
/// The name might be a dummy name in case of anonymous items
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Item {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ident: Ident,
|
|
|
|
pub attrs: Vec<Attribute>,
|
|
|
|
pub id: NodeId,
|
2016-02-09 11:36:51 +01:00
|
|
|
pub node: ItemKind,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub vis: Visibility,
|
|
|
|
pub span: Span,
|
2017-07-10 17:44:46 -07:00
|
|
|
|
|
|
|
/// Original tokens this item was parsed from. This isn't necessarily
|
|
|
|
/// available for all items, although over time more and more items should
|
|
|
|
/// have this be `Some`. Right now this is primarily used for procedural
|
|
|
|
/// macros, notably custom attributes.
|
2017-07-12 09:50:05 -07:00
|
|
|
///
|
|
|
|
/// Note that the tokens here do not include the outer attributes, but will
|
|
|
|
/// include inner attributes.
|
2017-07-10 17:44:46 -07:00
|
|
|
pub tokens: Option<TokenStream>,
|
2013-01-13 13:13:41 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-02-09 11:36:51 +01:00
|
|
|
pub enum ItemKind {
|
2018-03-09 18:51:48 +03:00
|
|
|
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.
|
2015-03-19 00:56:37 +05:30
|
|
|
///
|
2016-06-24 13:14:34 +02:00
|
|
|
/// E.g. `extern crate foo` or `extern crate foo_bar as foo`
|
2016-02-09 11:36:51 +01:00
|
|
|
ExternCrate(Option<Name>),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A use declaration (`use` or `pub use`) item.
|
|
|
|
///
|
|
|
|
/// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
|
2017-09-26 23:04:00 +02:00
|
|
|
Use(P<UseTree>),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A static item (`static` or `pub static`).
|
|
|
|
///
|
|
|
|
/// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
|
2016-02-09 11:36:51 +01:00
|
|
|
Static(P<Ty>, Mutability, P<Expr>),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A constant item (`const` or `pub const`).
|
|
|
|
///
|
|
|
|
/// E.g. `const FOO: i32 = 42;`
|
2016-02-09 11:36:51 +01:00
|
|
|
Const(P<Ty>, P<Expr>),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A function declaration (`fn` or `pub fn`).
|
|
|
|
///
|
|
|
|
/// E.g. `fn foo(bar: usize) -> usize { .. }`
|
2016-08-10 16:20:12 -07:00
|
|
|
Fn(P<FnDecl>, Unsafety, Spanned<Constness>, Abi, Generics, P<Block>),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A module declaration (`mod` or `pub mod`).
|
|
|
|
///
|
|
|
|
/// E.g. `mod foo;` or `mod foo { .. }`
|
2016-02-09 11:36:51 +01:00
|
|
|
Mod(Mod),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// An external module (`extern` or `pub extern`).
|
|
|
|
///
|
|
|
|
/// E.g. `extern {}` or `extern "C" {}`
|
2016-02-09 11:36:51 +01:00
|
|
|
ForeignMod(ForeignMod),
|
2017-03-15 21:27:40 -05:00
|
|
|
/// Module-level inline assembly (from `global_asm!()`)
|
|
|
|
GlobalAsm(P<GlobalAsm>),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A type alias (`type` or `pub type`).
|
|
|
|
///
|
|
|
|
/// E.g. `type Foo = Bar<u8>;`
|
2016-02-09 11:36:51 +01:00
|
|
|
Ty(P<Ty>, Generics),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// An enum definition (`enum` or `pub enum`).
|
|
|
|
///
|
|
|
|
/// E.g. `enum Foo<A, B> { C<A>, D<B> }`
|
2016-02-09 11:36:51 +01:00
|
|
|
Enum(EnumDef, Generics),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A struct definition (`struct` or `pub struct`).
|
|
|
|
///
|
|
|
|
/// E.g. `struct Foo<A> { x: A }`
|
2016-02-09 11:36:51 +01:00
|
|
|
Struct(VariantData, Generics),
|
2016-08-29 05:04:31 +00:00
|
|
|
/// A union definition (`union` or `pub union`).
|
|
|
|
///
|
|
|
|
/// E.g. `union Foo<A, B> { x: A, y: B }`
|
2016-07-17 00:15:15 +03:00
|
|
|
Union(VariantData, Generics),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// A Trait declaration (`trait` or `pub trait`).
|
|
|
|
///
|
2017-10-12 09:51:31 -03:00
|
|
|
/// E.g. `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`
|
|
|
|
Trait(IsAuto, Unsafety, Generics, TyParamBounds, Vec<TraitItem>),
|
2017-10-02 12:27:45 +00:00
|
|
|
/// Trait alias
|
|
|
|
///
|
|
|
|
/// E.g. `trait Foo = Bar + Quux;`
|
|
|
|
TraitAlias(Generics, TyParamBounds),
|
2016-06-24 13:14:34 +02:00
|
|
|
/// An implementation.
|
|
|
|
///
|
|
|
|
/// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
|
2016-02-09 11:36:51 +01:00
|
|
|
Impl(Unsafety,
|
2014-12-28 23:33:18 +01:00
|
|
|
ImplPolarity,
|
2016-11-18 17:14:42 +01:00
|
|
|
Defaultness,
|
2014-12-10 06:15:06 -05:00
|
|
|
Generics,
|
2014-01-09 15:05:33 +02:00
|
|
|
Option<TraitRef>, // (optional) trait this impl implements
|
|
|
|
P<Ty>, // self
|
2016-02-11 23:33:09 +03:00
|
|
|
Vec<ImplItem>),
|
2017-03-05 05:15:58 +00:00
|
|
|
/// A macro invocation.
|
2016-06-24 13:14:34 +02:00
|
|
|
///
|
|
|
|
/// E.g. `macro_rules! foo { .. }` or `foo!(..)`
|
2016-02-09 11:36:51 +01:00
|
|
|
Mac(Mac),
|
2017-03-05 05:15:58 +00:00
|
|
|
|
|
|
|
/// A macro definition.
|
2017-03-17 21:58:48 +00:00
|
|
|
MacroDef(MacroDef),
|
2011-05-11 15:10:24 +02:00
|
|
|
}
|
|
|
|
|
2016-02-09 11:36:51 +01:00
|
|
|
impl ItemKind {
|
2014-09-20 14:08:10 +02:00
|
|
|
pub fn descriptive_variant(&self) -> &str {
|
|
|
|
match *self {
|
2016-02-09 11:36:51 +01:00
|
|
|
ItemKind::ExternCrate(..) => "extern crate",
|
|
|
|
ItemKind::Use(..) => "use",
|
|
|
|
ItemKind::Static(..) => "static item",
|
|
|
|
ItemKind::Const(..) => "constant item",
|
|
|
|
ItemKind::Fn(..) => "function",
|
|
|
|
ItemKind::Mod(..) => "module",
|
|
|
|
ItemKind::ForeignMod(..) => "foreign module",
|
2017-03-15 21:27:40 -05:00
|
|
|
ItemKind::GlobalAsm(..) => "global asm",
|
2016-02-09 11:36:51 +01:00
|
|
|
ItemKind::Ty(..) => "type alias",
|
|
|
|
ItemKind::Enum(..) => "enum",
|
|
|
|
ItemKind::Struct(..) => "struct",
|
2016-08-29 05:04:31 +00:00
|
|
|
ItemKind::Union(..) => "union",
|
2016-02-09 11:36:51 +01:00
|
|
|
ItemKind::Trait(..) => "trait",
|
2017-10-02 12:27:45 +00:00
|
|
|
ItemKind::TraitAlias(..) => "trait alias",
|
2016-02-09 11:36:51 +01:00
|
|
|
ItemKind::Mac(..) |
|
2017-03-05 05:15:58 +00:00
|
|
|
ItemKind::MacroDef(..) |
|
2017-12-01 10:01:23 -02:00
|
|
|
ItemKind::Impl(..) => "item"
|
2014-09-20 14:08:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct ForeignItem {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ident: Ident,
|
|
|
|
pub attrs: Vec<Attribute>,
|
2016-02-09 11:31:19 +01:00
|
|
|
pub node: ForeignItemKind,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub id: NodeId,
|
|
|
|
pub span: Span,
|
|
|
|
pub vis: Visibility,
|
2013-01-13 12:02:16 -08:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2015-03-17 05:06:13 +05:30
|
|
|
/// An item within an `extern` block
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
2016-02-09 11:31:19 +01:00
|
|
|
pub enum ForeignItemKind {
|
2015-03-17 05:06:13 +05:30
|
|
|
/// A foreign function
|
2016-02-09 11:31:19 +01:00
|
|
|
Fn(P<FnDecl>, Generics),
|
2015-03-17 05:06:13 +05:30
|
|
|
/// A foreign static item (`static ext: u8`), with optional mutability
|
2015-03-18 18:06:10 +05:30
|
|
|
/// (the boolean is true when mutable)
|
2016-02-09 11:31:19 +01:00
|
|
|
Static(P<Ty>, bool),
|
2017-09-03 19:53:58 +01:00
|
|
|
/// A foreign type
|
|
|
|
Ty,
|
2018-03-10 18:16:26 -08:00
|
|
|
/// A macro invocation
|
|
|
|
Macro(Mac),
|
2011-02-02 10:43:57 -05:00
|
|
|
}
|
|
|
|
|
2016-02-09 11:31:19 +01:00
|
|
|
impl ForeignItemKind {
|
2014-09-20 14:08:10 +02:00
|
|
|
pub fn descriptive_variant(&self) -> &str {
|
|
|
|
match *self {
|
2016-02-09 11:31:19 +01:00
|
|
|
ForeignItemKind::Fn(..) => "foreign function",
|
2017-09-03 19:53:58 +01:00
|
|
|
ForeignItemKind::Static(..) => "foreign static item",
|
|
|
|
ForeignItemKind::Ty => "foreign type",
|
2018-03-10 18:16:26 -08:00
|
|
|
ForeignItemKind::Macro(..) => "macro in foreign module",
|
2014-09-20 14:08:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 17:48:58 +02:00
|
|
|
#[cfg(test)]
|
2015-04-24 17:30:41 +02:00
|
|
|
mod tests {
|
2014-02-05 08:52:54 -08:00
|
|
|
use serialize;
|
2013-11-27 17:48:58 +02:00
|
|
|
use super::*;
|
|
|
|
|
2013-04-03 10:28:14 -07:00
|
|
|
// are ASTs encodable?
|
2014-03-18 10:58:26 -07:00
|
|
|
#[test]
|
|
|
|
fn check_asts_encodable() {
|
2015-01-03 22:24:50 -08:00
|
|
|
fn assert_encodable<T: serialize::Encodable>() {}
|
|
|
|
assert_encodable::<Crate>();
|
2014-03-18 10:58:26 -07:00
|
|
|
}
|
2013-02-13 12:49:45 -08:00
|
|
|
}
|