David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 1 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 2 | use delimited::Delimited; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 3 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 4 | ast_enum_of_structs! { |
| 5 | pub enum ConstExpr { |
| 6 | /// A function call |
| 7 | pub Call(ConstCall { |
| 8 | /// The function being called |
| 9 | pub func: Box<ConstExpr>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 10 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 11 | /// The arguments to the function being called |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 12 | pub args: Delimited<ConstExpr, tokens::Comma>, |
| 13 | |
| 14 | pub paren_token: tokens::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 15 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 16 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 17 | /// A binary operation (For example: `a + b`, `a * b`) |
| 18 | pub Binary(ConstBinary { |
| 19 | /// The binary operation this represents |
| 20 | pub op: BinOp, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 21 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 22 | /// The left-hand-side of the constant binary op |
| 23 | pub left: Box<ConstExpr>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 24 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 25 | /// The right-hand-side of the constant binary op |
| 26 | pub right: Box<ConstExpr>, |
| 27 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 28 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 29 | /// A unary operation (For example: `!x`, `*x`) |
| 30 | pub Unary(ConstUnary { |
| 31 | /// Operation being performed |
| 32 | pub op: UnOp, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 33 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 34 | /// Expression acted on |
| 35 | pub expr: Box<ConstExpr>, |
| 36 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 37 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 38 | /// A literal (For example: `1`, `"foo"`) |
| 39 | pub Lit(Lit), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 40 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 41 | /// A cast (`foo as f64`) |
| 42 | pub Cast(ConstCast { |
| 43 | /// Value being casted |
| 44 | pub expr: Box<ConstExpr>, |
| 45 | |
| 46 | /// Type casted to |
| 47 | pub ty: Box<Ty>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 48 | |
| 49 | pub as_token: tokens::As, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 50 | }), |
| 51 | |
| 52 | /// Variable reference, possibly containing `::` and/or type |
| 53 | /// parameters, e.g. foo::bar::<baz>. |
| 54 | pub Path(Path), |
| 55 | |
| 56 | /// An indexing operation (`foo[2]`) |
| 57 | pub Index(ConstIndex { |
| 58 | /// Value that is being indexed |
| 59 | pub expr: Box<ConstExpr>, |
| 60 | |
| 61 | /// Index expression |
| 62 | pub index: Box<ConstExpr>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 63 | |
| 64 | pub bracket_token: tokens::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 65 | }), |
| 66 | |
| 67 | /// No-op: used solely so we can pretty-print faithfully |
| 68 | pub Paren(ConstParen { |
| 69 | /// Expression that's parenthesized |
| 70 | pub expr: Box<ConstExpr>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 71 | pub paren_token: tokens::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 72 | }), |
| 73 | |
| 74 | /// If compiling with full support for expression syntax, any expression is |
| 75 | /// allowed |
| 76 | pub Other(Other), |
| 77 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 78 | } |
| 79 | |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 80 | #[cfg(not(feature = "full"))] |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 81 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 82 | pub struct Other { |
| 83 | _private: (), |
| 84 | } |
| 85 | |
| 86 | #[cfg(feature = "full")] |
| 87 | pub type Other = Expr; |
| 88 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 89 | #[cfg(feature = "parsing")] |
| 90 | pub mod parsing { |
| 91 | use super::*; |
| 92 | use {BinOp, Ty}; |
| 93 | use lit::parsing::lit; |
| 94 | use op::parsing::{binop, unop}; |
| 95 | use ty::parsing::{path, ty}; |
| 96 | |
| 97 | named!(pub const_expr -> ConstExpr, do_parse!( |
| 98 | mut e: alt!( |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 99 | expr_unary |
| 100 | | |
David Tolnay | f43ba80 | 2016-10-30 10:20:47 -0700 | [diff] [blame] | 101 | expr_lit |
| 102 | | |
| 103 | expr_path |
| 104 | | |
| 105 | expr_paren |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 106 | // Cannot handle ConstExpr::Other here because for example |
| 107 | // `[u32; n!()]` would end up successfully parsing `n` as |
| 108 | // ConstExpr::Path and then fail to parse `!()`. Instead, callers |
| 109 | // are required to handle Other. See ty::parsing::array_len and |
| 110 | // data::parsing::discriminant. |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 111 | ) >> |
| 112 | many0!(alt!( |
| 113 | tap!(args: and_call => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 114 | let (args, paren) = args; |
| 115 | e = ConstCall { |
| 116 | func: Box::new(e), |
| 117 | args: args, |
| 118 | paren_token: paren, |
| 119 | }.into(); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 120 | }) |
| 121 | | |
| 122 | tap!(more: and_binary => { |
| 123 | let (op, other) = more; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 124 | e = ConstBinary { op: op, left: Box::new(e), right: Box::new(other) }.into(); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 125 | }) |
| 126 | | |
| 127 | tap!(ty: and_cast => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 128 | let (ty, token) = ty; |
| 129 | e = ConstCast { |
| 130 | expr: Box::new(e), |
| 131 | ty: Box::new(ty), |
| 132 | as_token: token, |
| 133 | }.into(); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 134 | }) |
David Tolnay | 6758875 | 2016-10-30 12:23:10 -0700 | [diff] [blame] | 135 | | |
| 136 | tap!(i: and_index => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 137 | let (i, bracket) = i; |
| 138 | e = ConstIndex { |
| 139 | expr: Box::new(e), |
| 140 | index: Box::new(i), |
| 141 | bracket_token: bracket, |
| 142 | }.into(); |
David Tolnay | 6758875 | 2016-10-30 12:23:10 -0700 | [diff] [blame] | 143 | }) |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 144 | )) >> |
| 145 | (e) |
| 146 | )); |
| 147 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 148 | named!(and_call -> (Delimited<ConstExpr, tokens::Comma>, tokens::Paren), do_parse!( |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 149 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 150 | args: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 151 | const_expr) >> |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 152 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 153 | (args, tokens::Paren::default()) |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 154 | )); |
| 155 | |
| 156 | named!(and_binary -> (BinOp, ConstExpr), tuple!(binop, const_expr)); |
| 157 | |
| 158 | named!(expr_unary -> ConstExpr, do_parse!( |
| 159 | operator: unop >> |
| 160 | operand: const_expr >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 161 | (ConstUnary { op: operator, expr: Box::new(operand) }.into()) |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 162 | )); |
| 163 | |
| 164 | named!(expr_lit -> ConstExpr, map!(lit, ConstExpr::Lit)); |
| 165 | |
David Tolnay | f43ba80 | 2016-10-30 10:20:47 -0700 | [diff] [blame] | 166 | named!(expr_path -> ConstExpr, map!(path, ConstExpr::Path)); |
| 167 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 168 | named!(and_index -> (ConstExpr, tokens::Bracket), do_parse!( |
| 169 | punct!("[") >> |
| 170 | expr: const_expr >> |
| 171 | punct!("]") >> |
| 172 | (expr, tokens::Bracket::default()) |
| 173 | )); |
David Tolnay | 6758875 | 2016-10-30 12:23:10 -0700 | [diff] [blame] | 174 | |
David Tolnay | f43ba80 | 2016-10-30 10:20:47 -0700 | [diff] [blame] | 175 | named!(expr_paren -> ConstExpr, do_parse!( |
| 176 | punct!("(") >> |
| 177 | e: const_expr >> |
| 178 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 179 | (ConstParen { |
| 180 | expr: Box::new(e), |
| 181 | paren_token: tokens::Paren::default(), |
| 182 | }.into()) |
David Tolnay | f43ba80 | 2016-10-30 10:20:47 -0700 | [diff] [blame] | 183 | )); |
| 184 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 185 | named!(and_cast -> (Ty, tokens::As), do_parse!( |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 186 | keyword!("as") >> |
| 187 | ty: ty >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 188 | (ty, tokens::As::default()) |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 189 | )); |
| 190 | } |
| 191 | |
| 192 | #[cfg(feature = "printing")] |
| 193 | mod printing { |
| 194 | use super::*; |
| 195 | use quote::{Tokens, ToTokens}; |
| 196 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 197 | impl ToTokens for ConstCall { |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 198 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 199 | self.func.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 200 | self.paren_token.surround(tokens, |tokens| { |
| 201 | self.args.to_tokens(tokens); |
| 202 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | impl ToTokens for ConstBinary { |
| 207 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 208 | self.left.to_tokens(tokens); |
| 209 | self.op.to_tokens(tokens); |
| 210 | self.right.to_tokens(tokens); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | impl ToTokens for ConstUnary { |
| 215 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 216 | self.op.to_tokens(tokens); |
| 217 | self.expr.to_tokens(tokens); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | impl ToTokens for ConstCast { |
| 222 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 223 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 224 | self.as_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 225 | self.ty.to_tokens(tokens); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | impl ToTokens for ConstIndex { |
| 230 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 231 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 232 | self.bracket_token.surround(tokens, |tokens| { |
| 233 | self.index.to_tokens(tokens); |
| 234 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | impl ToTokens for ConstParen { |
| 239 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 240 | self.paren_token.surround(tokens, |tokens| { |
| 241 | self.expr.to_tokens(tokens); |
| 242 | }) |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 245 | |
| 246 | #[cfg(not(feature = "full"))] |
| 247 | impl ToTokens for Other { |
| 248 | fn to_tokens(&self, _tokens: &mut Tokens) { |
| 249 | unreachable!() |
| 250 | } |
| 251 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 252 | } |