@@ -48,7 +48,6 @@ pub use self::PathParameters::*;
48
48
use attr:: ThinAttributes ;
49
49
use codemap:: { Span , Spanned , DUMMY_SP , ExpnId } ;
50
50
use abi:: Abi ;
51
- use ast_util;
52
51
use ext:: base;
53
52
use ext:: tt:: macro_parser;
54
53
use owned_slice:: OwnedSlice ;
@@ -427,6 +426,19 @@ impl Generics {
427
426
}
428
427
}
429
428
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
+
430
442
/// A `where` clause in a definition
431
443
#[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
432
444
pub struct WhereClause {
@@ -657,6 +669,57 @@ pub enum BinOp_ {
657
669
BiGt ,
658
670
}
659
671
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
+
660
723
pub type BinOp = Spanned < BinOp_ > ;
661
724
662
725
#[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug , Copy ) ]
@@ -669,13 +732,31 @@ pub enum UnOp {
669
732
UnNeg
670
733
}
671
734
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
+
672
753
/// A statement
673
754
pub type Stmt = Spanned < Stmt_ > ;
674
755
675
756
impl fmt:: Debug for Stmt {
676
757
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
677
758
write ! ( f, "stmt({}: {})" ,
678
- ast_util :: stmt_id ( self )
759
+ self . node . id ( )
679
760
. map_or( Cow :: Borrowed ( "<macro>" ) , |id|Cow :: Owned ( id. to_string( ) ) ) ,
680
761
pprust:: stmt_to_string( self ) )
681
762
}
@@ -697,6 +778,15 @@ pub enum Stmt_ {
697
778
}
698
779
699
780
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
+
700
790
pub fn attrs ( & self ) -> & [ Attribute ] {
701
791
match * self {
702
792
StmtDecl ( ref d, _) => d. attrs ( ) ,
@@ -1226,6 +1316,16 @@ pub enum Lit_ {
1226
1316
LitBool ( bool ) ,
1227
1317
}
1228
1318
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
+
1229
1329
// NB: If you change this, you'll probably want to change the corresponding
1230
1330
// type structure in middle/ty.rs as well.
1231
1331
#[ derive( Clone , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug ) ]
@@ -1301,11 +1401,37 @@ impl fmt::Debug for IntTy {
1301
1401
1302
1402
impl fmt:: Display for IntTy {
1303
1403
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 ( ) )
1305
1405
}
1306
1406
}
1307
1407
1308
1408
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
+
1309
1435
pub fn bit_width ( & self ) -> Option < usize > {
1310
1436
Some ( match * self {
1311
1437
TyIs => return None ,
@@ -1327,6 +1453,29 @@ pub enum UintTy {
1327
1453
}
1328
1454
1329
1455
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
+
1330
1479
pub fn bit_width ( & self ) -> Option < usize > {
1331
1480
Some ( match * self {
1332
1481
TyUs => return None ,
@@ -1346,7 +1495,7 @@ impl fmt::Debug for UintTy {
1346
1495
1347
1496
impl fmt:: Display for UintTy {
1348
1497
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 ( ) )
1350
1499
}
1351
1500
}
1352
1501
@@ -1364,11 +1513,18 @@ impl fmt::Debug for FloatTy {
1364
1513
1365
1514
impl fmt:: Display for FloatTy {
1366
1515
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 ( ) )
1368
1517
}
1369
1518
}
1370
1519
1371
1520
impl FloatTy {
1521
+ pub fn ty_to_string ( & self ) -> & ' static str {
1522
+ match * self {
1523
+ TyF32 => "f32" ,
1524
+ TyF64 => "f64" ,
1525
+ }
1526
+ }
1527
+
1372
1528
pub fn bit_width ( & self ) -> usize {
1373
1529
match * self {
1374
1530
TyF32 => 32 ,
0 commit comments