Skip to content

Commit ec8ea22

Browse files
committed
[breaking-change] move ast_util functions to methods
1 parent 9ea4b4f commit ec8ea22

File tree

10 files changed

+191
-192
lines changed

10 files changed

+191
-192
lines changed

src/librustc/util/ppaux.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use middle::ty::{self, TypeAndMut, Ty, HasTypeFlags};
2424
use middle::ty::fold::TypeFoldable;
2525

2626
use std::fmt;
27-
use syntax::{abi, ast_util};
27+
use syntax::{abi};
2828
use syntax::parse::token;
2929
use syntax::ast::CRATE_NODE_ID;
3030
use rustc_front::hir;
@@ -778,9 +778,9 @@ impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
778778
match *self {
779779
TyBool => write!(f, "bool"),
780780
TyChar => write!(f, "char"),
781-
TyInt(t) => write!(f, "{}", ast_util::int_ty_to_string(t)),
782-
TyUint(t) => write!(f, "{}", ast_util::uint_ty_to_string(t)),
783-
TyFloat(t) => write!(f, "{}", ast_util::float_ty_to_string(t)),
781+
TyInt(t) => write!(f, "{}", t.ty_to_string()),
782+
TyUint(t) => write!(f, "{}", t.ty_to_string()),
783+
TyFloat(t) => write!(f, "{}", t.ty_to_string()),
784784
TyBox(typ) => write!(f, "Box<{}>", typ),
785785
TyRawPtr(ref tm) => {
786786
write!(f, "*{} {}", match tm.mutbl {

src/librustc_trans/trans/debuginfo/metadata.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use std::rc::Rc;
4646
use syntax;
4747
use syntax::util::interner::Interner;
4848
use syntax::codemap::Span;
49-
use syntax::{ast, ast_util, codemap};
49+
use syntax::{ast, codemap};
5050
use syntax::parse::token;
5151

5252

@@ -936,13 +936,13 @@ fn basic_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
936936
ty::TyBool => ("bool", DW_ATE_boolean),
937937
ty::TyChar => ("char", DW_ATE_unsigned_char),
938938
ty::TyInt(int_ty) => {
939-
(ast_util::int_ty_to_string(int_ty), DW_ATE_signed)
939+
(int_ty.ty_to_string(), DW_ATE_signed)
940940
},
941941
ty::TyUint(uint_ty) => {
942-
(ast_util::uint_ty_to_string(uint_ty), DW_ATE_unsigned)
942+
(uint_ty.ty_to_string(), DW_ATE_unsigned)
943943
},
944944
ty::TyFloat(float_ty) => {
945-
(ast_util::float_ty_to_string(float_ty), DW_ATE_float)
945+
(float_ty.ty_to_string(), DW_ATE_float)
946946
},
947947
_ => cx.sess().bug("debuginfo::basic_type_metadata - t is invalid type")
948948
};

src/librustc_trans/trans/debuginfo/type_names.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use middle::subst::{self, Substs};
1919
use middle::ty::{self, Ty};
2020

2121
use rustc_front::hir;
22-
use syntax::ast_util;
2322

2423
// Compute the name of the type as it should be stored in debuginfo. Does not do
2524
// any caching, i.e. calling the function twice with the same type will also do
@@ -44,9 +43,9 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
4443
ty::TyBool => output.push_str("bool"),
4544
ty::TyChar => output.push_str("char"),
4645
ty::TyStr => output.push_str("str"),
47-
ty::TyInt(int_ty) => output.push_str(ast_util::int_ty_to_string(int_ty)),
48-
ty::TyUint(uint_ty) => output.push_str(ast_util::uint_ty_to_string(uint_ty)),
49-
ty::TyFloat(float_ty) => output.push_str(ast_util::float_ty_to_string(float_ty)),
46+
ty::TyInt(int_ty) => output.push_str(int_ty.ty_to_string()),
47+
ty::TyUint(uint_ty) => output.push_str(uint_ty.ty_to_string()),
48+
ty::TyFloat(float_ty) => output.push_str(float_ty.ty_to_string()),
5049
ty::TyStruct(def, substs) |
5150
ty::TyEnum(def, substs) => {
5251
push_item_name(cx, def.did, qualified, output);

src/librustc_trans/trans/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ use trans::type_::Type;
8383
use rustc_front;
8484
use rustc_front::hir;
8585

86-
use syntax::{ast, ast_util, codemap};
86+
use syntax::{ast, codemap};
8787
use syntax::parse::token::InternedString;
8888
use syntax::ptr::P;
8989
use syntax::parse::token;
@@ -2622,7 +2622,7 @@ fn expr_kind(tcx: &ty::ctxt, expr: &hir::Expr) -> ExprKind {
26222622
ExprKind::RvalueDps
26232623
}
26242624

2625-
hir::ExprLit(ref lit) if ast_util::lit_is_str(&**lit) => {
2625+
hir::ExprLit(ref lit) if lit.node.is_str() => {
26262626
ExprKind::RvalueDps
26272627
}
26282628

src/libsyntax/ast.rs

+161-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ pub use self::PathParameters::*;
4848
use attr::ThinAttributes;
4949
use codemap::{Span, Spanned, DUMMY_SP, ExpnId};
5050
use abi::Abi;
51-
use ast_util;
5251
use ext::base;
5352
use ext::tt::macro_parser;
5453
use owned_slice::OwnedSlice;
@@ -427,6 +426,19 @@ impl Generics {
427426
}
428427
}
429428

429+
impl Default for Generics {
430+
fn default() -> Generics {
431+
Generics {
432+
lifetimes: Vec::new(),
433+
ty_params: OwnedSlice::empty(),
434+
where_clause: WhereClause {
435+
id: DUMMY_NODE_ID,
436+
predicates: Vec::new(),
437+
}
438+
}
439+
}
440+
}
441+
430442
/// A `where` clause in a definition
431443
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
432444
pub struct WhereClause {
@@ -657,6 +669,57 @@ pub enum BinOp_ {
657669
BiGt,
658670
}
659671

672+
impl BinOp_ {
673+
pub fn to_string(&self) -> &'static str {
674+
match *self {
675+
BiAdd => "+",
676+
BiSub => "-",
677+
BiMul => "*",
678+
BiDiv => "/",
679+
BiRem => "%",
680+
BiAnd => "&&",
681+
BiOr => "||",
682+
BiBitXor => "^",
683+
BiBitAnd => "&",
684+
BiBitOr => "|",
685+
BiShl => "<<",
686+
BiShr => ">>",
687+
BiEq => "==",
688+
BiLt => "<",
689+
BiLe => "<=",
690+
BiNe => "!=",
691+
BiGe => ">=",
692+
BiGt => ">"
693+
}
694+
}
695+
pub fn lazy(&self) -> bool {
696+
match *self {
697+
BiAnd | BiOr => true,
698+
_ => false
699+
}
700+
}
701+
702+
pub fn is_shift(&self) -> bool {
703+
match *self {
704+
BiShl | BiShr => true,
705+
_ => false
706+
}
707+
}
708+
pub fn is_comparison(&self) -> bool {
709+
match *self {
710+
BiEq | BiLt | BiLe | BiNe | BiGt | BiGe =>
711+
true,
712+
BiAnd | BiOr | BiAdd | BiSub | BiMul | BiDiv | BiRem |
713+
BiBitXor | BiBitAnd | BiBitOr | BiShl | BiShr =>
714+
false,
715+
}
716+
}
717+
/// Returns `true` if the binary operator takes its arguments by value
718+
pub fn is_by_value(&self) -> bool {
719+
!BinOp_::is_comparison(self)
720+
}
721+
}
722+
660723
pub type BinOp = Spanned<BinOp_>;
661724

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

735+
impl UnOp {
736+
/// Returns `true` if the unary operator takes its argument by value
737+
pub fn is_by_value(u: UnOp) -> bool {
738+
match u {
739+
UnNeg | UnNot => true,
740+
_ => false,
741+
}
742+
}
743+
744+
pub fn to_string(op: UnOp) -> &'static str {
745+
match op {
746+
UnDeref => "*",
747+
UnNot => "!",
748+
UnNeg => "-",
749+
}
750+
}
751+
}
752+
672753
/// A statement
673754
pub type Stmt = Spanned<Stmt_>;
674755

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

699780
impl Stmt_ {
781+
pub fn id(&self) -> Option<NodeId> {
782+
match *self {
783+
StmtDecl(_, id) => Some(id),
784+
StmtExpr(_, id) => Some(id),
785+
StmtSemi(_, id) => Some(id),
786+
StmtMac(..) => None,
787+
}
788+
}
789+
700790
pub fn attrs(&self) -> &[Attribute] {
701791
match *self {
702792
StmtDecl(ref d, _) => d.attrs(),
@@ -1226,6 +1316,16 @@ pub enum Lit_ {
12261316
LitBool(bool),
12271317
}
12281318

1319+
impl Lit_ {
1320+
/// Returns true if this literal is a string and false otherwise.
1321+
pub fn is_str(&self) -> bool {
1322+
match *self {
1323+
LitStr(..) => true,
1324+
_ => false,
1325+
}
1326+
}
1327+
}
1328+
12291329
// NB: If you change this, you'll probably want to change the corresponding
12301330
// type structure in middle/ty.rs as well.
12311331
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
@@ -1301,11 +1401,37 @@ impl fmt::Debug for IntTy {
13011401

13021402
impl fmt::Display for IntTy {
13031403
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1304-
write!(f, "{}", ast_util::int_ty_to_string(*self))
1404+
write!(f, "{}", self.ty_to_string())
13051405
}
13061406
}
13071407

13081408
impl IntTy {
1409+
pub fn ty_to_string(&self) -> &'static str {
1410+
match *self {
1411+
TyIs => "isize",
1412+
TyI8 => "i8",
1413+
TyI16 => "i16",
1414+
TyI32 => "i32",
1415+
TyI64 => "i64"
1416+
}
1417+
}
1418+
1419+
pub fn val_to_string(&self, val: i64) -> String {
1420+
// cast to a u64 so we can correctly print INT64_MIN. All integral types
1421+
// are parsed as u64, so we wouldn't want to print an extra negative
1422+
// sign.
1423+
format!("{}{}", val as u64, self.ty_to_string())
1424+
}
1425+
1426+
pub fn ty_max(&self) -> u64 {
1427+
match *self {
1428+
TyI8 => 0x80,
1429+
TyI16 => 0x8000,
1430+
TyIs | TyI32 => 0x80000000, // actually ni about TyIs
1431+
TyI64 => 0x8000000000000000
1432+
}
1433+
}
1434+
13091435
pub fn bit_width(&self) -> Option<usize> {
13101436
Some(match *self {
13111437
TyIs => return None,
@@ -1327,6 +1453,29 @@ pub enum UintTy {
13271453
}
13281454

13291455
impl UintTy {
1456+
pub fn ty_to_string(&self) -> &'static str {
1457+
match *self {
1458+
TyUs => "usize",
1459+
TyU8 => "u8",
1460+
TyU16 => "u16",
1461+
TyU32 => "u32",
1462+
TyU64 => "u64"
1463+
}
1464+
}
1465+
1466+
pub fn val_to_string(&self, val: u64) -> String {
1467+
format!("{}{}", val, self.ty_to_string())
1468+
}
1469+
1470+
pub fn ty_max(&self) -> u64 {
1471+
match *self {
1472+
TyU8 => 0xff,
1473+
TyU16 => 0xffff,
1474+
TyUs | TyU32 => 0xffffffff, // actually ni about TyUs
1475+
TyU64 => 0xffffffffffffffff
1476+
}
1477+
}
1478+
13301479
pub fn bit_width(&self) -> Option<usize> {
13311480
Some(match *self {
13321481
TyUs => return None,
@@ -1346,7 +1495,7 @@ impl fmt::Debug for UintTy {
13461495

13471496
impl fmt::Display for UintTy {
13481497
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1349-
write!(f, "{}", ast_util::uint_ty_to_string(*self))
1498+
write!(f, "{}", self.ty_to_string())
13501499
}
13511500
}
13521501

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

13651514
impl fmt::Display for FloatTy {
13661515
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1367-
write!(f, "{}", ast_util::float_ty_to_string(*self))
1516+
write!(f, "{}", self.ty_to_string())
13681517
}
13691518
}
13701519

13711520
impl FloatTy {
1521+
pub fn ty_to_string(&self) -> &'static str {
1522+
match *self {
1523+
TyF32 => "f32",
1524+
TyF64 => "f64",
1525+
}
1526+
}
1527+
13721528
pub fn bit_width(&self) -> usize {
13731529
match *self {
13741530
TyF32 => 32,

0 commit comments

Comments
 (0)