Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some ast_util functions into methods #30105

Merged
merged 1 commit into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use middle::ty::{self, TypeAndMut, Ty, HasTypeFlags};
use middle::ty::fold::TypeFoldable;

use std::fmt;
use syntax::{abi, ast_util};
use syntax::{abi};
use syntax::parse::token;
use syntax::ast::CRATE_NODE_ID;
use rustc_front::hir;
Expand Down Expand Up @@ -778,9 +778,9 @@ impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
match *self {
TyBool => write!(f, "bool"),
TyChar => write!(f, "char"),
TyInt(t) => write!(f, "{}", ast_util::int_ty_to_string(t)),
TyUint(t) => write!(f, "{}", ast_util::uint_ty_to_string(t)),
TyFloat(t) => write!(f, "{}", ast_util::float_ty_to_string(t)),
TyInt(t) => write!(f, "{}", t.ty_to_string()),
TyUint(t) => write!(f, "{}", t.ty_to_string()),
TyFloat(t) => write!(f, "{}", t.ty_to_string()),
TyBox(typ) => write!(f, "Box<{}>", typ),
TyRawPtr(ref tm) => {
write!(f, "*{} {}", match tm.mutbl {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_trans/trans/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::rc::Rc;
use syntax;
use syntax::util::interner::Interner;
use syntax::codemap::Span;
use syntax::{ast, ast_util, codemap};
use syntax::{ast, codemap};
use syntax::parse::token;


Expand Down Expand Up @@ -936,13 +936,13 @@ fn basic_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
ty::TyBool => ("bool", DW_ATE_boolean),
ty::TyChar => ("char", DW_ATE_unsigned_char),
ty::TyInt(int_ty) => {
(ast_util::int_ty_to_string(int_ty), DW_ATE_signed)
(int_ty.ty_to_string(), DW_ATE_signed)
},
ty::TyUint(uint_ty) => {
(ast_util::uint_ty_to_string(uint_ty), DW_ATE_unsigned)
(uint_ty.ty_to_string(), DW_ATE_unsigned)
},
ty::TyFloat(float_ty) => {
(ast_util::float_ty_to_string(float_ty), DW_ATE_float)
(float_ty.ty_to_string(), DW_ATE_float)
},
_ => cx.sess().bug("debuginfo::basic_type_metadata - t is invalid type")
};
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_trans/trans/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use middle::subst::{self, Substs};
use middle::ty::{self, Ty};

use rustc_front::hir;
use syntax::ast_util;

// Compute the name of the type as it should be stored in debuginfo. Does not do
// any caching, i.e. calling the function twice with the same type will also do
Expand All @@ -44,9 +43,9 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
ty::TyBool => output.push_str("bool"),
ty::TyChar => output.push_str("char"),
ty::TyStr => output.push_str("str"),
ty::TyInt(int_ty) => output.push_str(ast_util::int_ty_to_string(int_ty)),
ty::TyUint(uint_ty) => output.push_str(ast_util::uint_ty_to_string(uint_ty)),
ty::TyFloat(float_ty) => output.push_str(ast_util::float_ty_to_string(float_ty)),
ty::TyInt(int_ty) => output.push_str(int_ty.ty_to_string()),
ty::TyUint(uint_ty) => output.push_str(uint_ty.ty_to_string()),
ty::TyFloat(float_ty) => output.push_str(float_ty.ty_to_string()),
ty::TyStruct(def, substs) |
ty::TyEnum(def, substs) => {
push_item_name(cx, def.did, qualified, output);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/trans/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use trans::type_::Type;
use rustc_front;
use rustc_front::hir;

use syntax::{ast, ast_util, codemap};
use syntax::{ast, codemap};
use syntax::parse::token::InternedString;
use syntax::ptr::P;
use syntax::parse::token;
Expand Down Expand Up @@ -2622,7 +2622,7 @@ fn expr_kind(tcx: &ty::ctxt, expr: &hir::Expr) -> ExprKind {
ExprKind::RvalueDps
}

hir::ExprLit(ref lit) if ast_util::lit_is_str(&**lit) => {
hir::ExprLit(ref lit) if lit.node.is_str() => {
ExprKind::RvalueDps
}

Expand Down
166 changes: 161 additions & 5 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub use self::PathParameters::*;
use attr::ThinAttributes;
use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
use abi::Abi;
use ast_util;
use ext::base;
use ext::tt::macro_parser;
use owned_slice::OwnedSlice;
Expand Down Expand Up @@ -427,6 +426,19 @@ impl Generics {
}
}

impl Default for Generics {
fn default() -> Generics {
Generics {
lifetimes: Vec::new(),
ty_params: OwnedSlice::empty(),
where_clause: WhereClause {
id: DUMMY_NODE_ID,
predicates: Vec::new(),
}
}
}
}

/// A `where` clause in a definition
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct WhereClause {
Expand Down Expand Up @@ -657,6 +669,57 @@ pub enum BinOp_ {
BiGt,
}

impl BinOp_ {
pub fn to_string(&self) -> &'static str {
match *self {
BiAdd => "+",
BiSub => "-",
BiMul => "*",
BiDiv => "/",
BiRem => "%",
BiAnd => "&&",
BiOr => "||",
BiBitXor => "^",
BiBitAnd => "&",
BiBitOr => "|",
BiShl => "<<",
BiShr => ">>",
BiEq => "==",
BiLt => "<",
BiLe => "<=",
BiNe => "!=",
BiGe => ">=",
BiGt => ">"
}
}
pub fn lazy(&self) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline between functions please (and elsewhere)

match *self {
BiAnd | BiOr => true,
_ => false
}
}

pub fn is_shift(&self) -> bool {
match *self {
BiShl | BiShr => true,
_ => false
}
}
pub fn is_comparison(&self) -> bool {
match *self {
BiEq | BiLt | BiLe | BiNe | BiGt | BiGe =>
true,
BiAnd | BiOr | BiAdd | BiSub | BiMul | BiDiv | BiRem |
BiBitXor | BiBitAnd | BiBitOr | BiShl | BiShr =>
false,
}
}
/// Returns `true` if the binary operator takes its arguments by value
pub fn is_by_value(&self) -> bool {
!BinOp_::is_comparison(self)
}
}

pub type BinOp = Spanned<BinOp_>;

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
Expand All @@ -669,13 +732,31 @@ pub enum UnOp {
UnNeg
}

impl UnOp {
/// Returns `true` if the unary operator takes its argument by value
pub fn is_by_value(u: UnOp) -> bool {
match u {
UnNeg | UnNot => true,
_ => false,
}
}

pub fn to_string(op: UnOp) -> &'static str {
match op {
UnDeref => "*",
UnNot => "!",
UnNeg => "-",
}
}
}

/// A statement
pub type Stmt = Spanned<Stmt_>;

impl fmt::Debug for Stmt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "stmt({}: {})",
ast_util::stmt_id(self)
self.node.id()
.map_or(Cow::Borrowed("<macro>"),|id|Cow::Owned(id.to_string())),
pprust::stmt_to_string(self))
}
Expand All @@ -697,6 +778,15 @@ pub enum Stmt_ {
}

impl Stmt_ {
pub fn id(&self) -> Option<NodeId> {
match *self {
StmtDecl(_, id) => Some(id),
StmtExpr(_, id) => Some(id),
StmtSemi(_, id) => Some(id),
StmtMac(..) => None,
}
}

pub fn attrs(&self) -> &[Attribute] {
match *self {
StmtDecl(ref d, _) => d.attrs(),
Expand Down Expand Up @@ -1226,6 +1316,16 @@ pub enum Lit_ {
LitBool(bool),
}

impl Lit_ {
/// Returns true if this literal is a string and false otherwise.
pub fn is_str(&self) -> bool {
match *self {
LitStr(..) => true,
_ => false,
}
}
}

// NB: If you change this, you'll probably want to change the corresponding
// type structure in middle/ty.rs as well.
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
Expand Down Expand Up @@ -1301,11 +1401,37 @@ impl fmt::Debug for IntTy {

impl fmt::Display for IntTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ast_util::int_ty_to_string(*self))
write!(f, "{}", self.ty_to_string())
}
}

impl IntTy {
pub fn ty_to_string(&self) -> &'static str {
match *self {
TyIs => "isize",
TyI8 => "i8",
TyI16 => "i16",
TyI32 => "i32",
TyI64 => "i64"
}
}

pub fn val_to_string(&self, val: i64) -> String {
// cast to a u64 so we can correctly print INT64_MIN. All integral types
// are parsed as u64, so we wouldn't want to print an extra negative
// sign.
format!("{}{}", val as u64, self.ty_to_string())
}

pub fn ty_max(&self) -> u64 {
match *self {
TyI8 => 0x80,
TyI16 => 0x8000,
TyIs | TyI32 => 0x80000000, // actually ni about TyIs
TyI64 => 0x8000000000000000
}
}

pub fn bit_width(&self) -> Option<usize> {
Some(match *self {
TyIs => return None,
Expand All @@ -1327,6 +1453,29 @@ pub enum UintTy {
}

impl UintTy {
pub fn ty_to_string(&self) -> &'static str {
match *self {
TyUs => "usize",
TyU8 => "u8",
TyU16 => "u16",
TyU32 => "u32",
TyU64 => "u64"
}
}

pub fn val_to_string(&self, val: u64) -> String {
format!("{}{}", val, self.ty_to_string())
}

pub fn ty_max(&self) -> u64 {
match *self {
TyU8 => 0xff,
TyU16 => 0xffff,
TyUs | TyU32 => 0xffffffff, // actually ni about TyUs
TyU64 => 0xffffffffffffffff
}
}

pub fn bit_width(&self) -> Option<usize> {
Some(match *self {
TyUs => return None,
Expand All @@ -1346,7 +1495,7 @@ impl fmt::Debug for UintTy {

impl fmt::Display for UintTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ast_util::uint_ty_to_string(*self))
write!(f, "{}", self.ty_to_string())
}
}

Expand All @@ -1364,11 +1513,18 @@ impl fmt::Debug for FloatTy {

impl fmt::Display for FloatTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ast_util::float_ty_to_string(*self))
write!(f, "{}", self.ty_to_string())
}
}

impl FloatTy {
pub fn ty_to_string(&self) -> &'static str {
match *self {
TyF32 => "f32",
TyF64 => "f64",
}
}

pub fn bit_width(&self) -> usize {
match *self {
TyF32 => 32,
Expand Down
Loading