@@ -48,7 +48,6 @@ pub use self::PathParameters::*;
4848use attr:: ThinAttributes ;
4949use codemap:: { Span , Spanned , DUMMY_SP , ExpnId } ;
5050use abi:: Abi ;
51- use ast_util;
5251use ext:: base;
5352use ext:: tt:: macro_parser;
5453use 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 ) ]
432444pub 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+
660723pub 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
673754pub type Stmt = Spanned < Stmt_ > ;
674755
675756impl 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
699780impl 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
13021402impl 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
13081408impl 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
13291455impl 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
13471496impl 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
13651514impl 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
13711520impl 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