6
6
use std:: rc:: Rc ;
7
7
use termcolor:: StandardStreamLock ;
8
8
9
+ use super :: casts:: { ConstCast , DynamicCast , FooCastParser , ReinterpretCast , StaticCast } ;
9
10
use super :: list:: { ListInitialization , ListInitializationParser } ;
10
11
use super :: operator:: { BinaryOp , Conditional , Operator , UnaryOp } ;
11
12
use super :: params:: { Parameters , ParametersParser } ;
@@ -108,6 +109,10 @@ pub enum ExprNode {
108
109
Nullptr ( Box < Nullptr > ) ,
109
110
This ( Box < This > ) ,
110
111
Type ( Box < Type > ) ,
112
+ StaticCast ( Box < StaticCast > ) ,
113
+ DynamicCast ( Box < DynamicCast > ) ,
114
+ ConstCast ( Box < ConstCast > ) ,
115
+ ReinterpretCast ( Box < ReinterpretCast > ) ,
111
116
}
112
117
113
118
impl Dump for ExprNode {
@@ -134,6 +139,10 @@ impl Dump for ExprNode {
134
139
Self :: Nullptr ( x) => dump ! ( x) ,
135
140
Self :: This ( x) => dump ! ( x) ,
136
141
Self :: Type ( x) => dump ! ( x) ,
142
+ Self :: StaticCast ( x) => dump ! ( x) ,
143
+ Self :: DynamicCast ( x) => dump ! ( x) ,
144
+ Self :: ConstCast ( x) => dump ! ( x) ,
145
+ Self :: ReinterpretCast ( x) => dump ! ( x) ,
137
146
}
138
147
}
139
148
}
@@ -995,6 +1004,15 @@ impl<'a, L: TLexer> ExpressionParser<'a, L> {
995
1004
. push ( ExprNode :: Bool ( Box :: new ( Bool { value : false } ) ) ) ;
996
1005
self . last = LastKind :: Operand ;
997
1006
}
1007
+ Token :: StaticCast
1008
+ | Token :: DynamicCast
1009
+ | Token :: ConstCast
1010
+ | Token :: ReinterpretCast => {
1011
+ let fcp = FooCastParser :: new ( self . lexer ) ;
1012
+ let ( _, node) = fcp. parse ( tok, context) ?;
1013
+
1014
+ self . push_operand ( node. unwrap ( ) ) ;
1015
+ }
998
1016
_ => {
999
1017
let dsp = DeclSpecifierParser :: new ( self . lexer ) ;
1000
1018
let ( tk, ( _, typ, _, _) ) = dsp. parse ( Some ( tok) , None , context) ?;
@@ -1458,4 +1476,80 @@ mod tests {
1458
1476
1459
1477
assert_eq ! ( node, expected) ;
1460
1478
}
1479
+
1480
+ #[ test]
1481
+ fn test_static_cast ( ) {
1482
+ let mut lexer = Lexer :: < DefaultContext > :: new ( b"static_cast<const int>(x)" ) ;
1483
+ let mut parser = ExpressionParser :: new ( & mut lexer, Token :: Eof ) ;
1484
+ let mut context = Context :: default ( ) ;
1485
+ let node = parser. parse ( None , & mut context) . unwrap ( ) . 1 . unwrap ( ) ;
1486
+
1487
+ let expected = node ! ( StaticCast {
1488
+ typ: Type {
1489
+ base: BaseType :: Primitive ( Primitive :: Int ) ,
1490
+ cv: CVQualifier :: CONST ,
1491
+ pointers: None ,
1492
+ } ,
1493
+ arg: ExprNode :: Variable ( Box :: new( mk_var!( "x" ) ) ) ,
1494
+ } ) ;
1495
+
1496
+ assert_eq ! ( node, expected) ;
1497
+ }
1498
+
1499
+ #[ test]
1500
+ fn test_dynamic_cast ( ) {
1501
+ let mut lexer = Lexer :: < DefaultContext > :: new ( b"dynamic_cast<const int>(x)" ) ;
1502
+ let mut parser = ExpressionParser :: new ( & mut lexer, Token :: Eof ) ;
1503
+ let mut context = Context :: default ( ) ;
1504
+ let node = parser. parse ( None , & mut context) . unwrap ( ) . 1 . unwrap ( ) ;
1505
+
1506
+ let expected = node ! ( DynamicCast {
1507
+ typ: Type {
1508
+ base: BaseType :: Primitive ( Primitive :: Int ) ,
1509
+ cv: CVQualifier :: CONST ,
1510
+ pointers: None ,
1511
+ } ,
1512
+ arg: ExprNode :: Variable ( Box :: new( mk_var!( "x" ) ) ) ,
1513
+ } ) ;
1514
+
1515
+ assert_eq ! ( node, expected) ;
1516
+ }
1517
+
1518
+ #[ test]
1519
+ fn test_const_cast ( ) {
1520
+ let mut lexer = Lexer :: < DefaultContext > :: new ( b"const_cast<const int>(x)" ) ;
1521
+ let mut parser = ExpressionParser :: new ( & mut lexer, Token :: Eof ) ;
1522
+ let mut context = Context :: default ( ) ;
1523
+ let node = parser. parse ( None , & mut context) . unwrap ( ) . 1 . unwrap ( ) ;
1524
+
1525
+ let expected = node ! ( ConstCast {
1526
+ typ: Type {
1527
+ base: BaseType :: Primitive ( Primitive :: Int ) ,
1528
+ cv: CVQualifier :: CONST ,
1529
+ pointers: None ,
1530
+ } ,
1531
+ arg: ExprNode :: Variable ( Box :: new( mk_var!( "x" ) ) ) ,
1532
+ } ) ;
1533
+
1534
+ assert_eq ! ( node, expected) ;
1535
+ }
1536
+
1537
+ #[ test]
1538
+ fn test_reinterpret_cast ( ) {
1539
+ let mut lexer = Lexer :: < DefaultContext > :: new ( b"reinterpret_cast<const int>(x)" ) ;
1540
+ let mut parser = ExpressionParser :: new ( & mut lexer, Token :: Eof ) ;
1541
+ let mut context = Context :: default ( ) ;
1542
+ let node = parser. parse ( None , & mut context) . unwrap ( ) . 1 . unwrap ( ) ;
1543
+
1544
+ let expected = node ! ( ReinterpretCast {
1545
+ typ: Type {
1546
+ base: BaseType :: Primitive ( Primitive :: Int ) ,
1547
+ cv: CVQualifier :: CONST ,
1548
+ pointers: None ,
1549
+ } ,
1550
+ arg: ExprNode :: Variable ( Box :: new( mk_var!( "x" ) ) ) ,
1551
+ } ) ;
1552
+
1553
+ assert_eq ! ( node, expected) ;
1554
+ }
1461
1555
}
0 commit comments