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::*; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame^] | 92 | use synom::Synom; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 93 | use synom::tokens::*; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 94 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 95 | impl Synom for ConstExpr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame^] | 96 | named!(parse -> Self, do_parse!( |
| 97 | mut e: alt!( |
| 98 | map!(syn!(ConstUnary), |e: ConstUnary| e.into()) |
| 99 | | |
| 100 | map!(syn!(Lit), |e: Lit| e.into()) |
| 101 | | |
| 102 | map!(syn!(Path), |e: Path| e.into()) |
| 103 | | |
| 104 | map!(syn!(ConstParen), |e: ConstParen| e.into()) |
| 105 | // Cannot handle ConstExpr::Other here because for example |
| 106 | // `[u32; n!()]` would end up successfully parsing `n` as |
| 107 | // ConstExpr::Path and then fail to parse `!()`. Instead, callers |
| 108 | // are required to handle Other. See ty::parsing::array_len and |
| 109 | // data::parsing::discriminant. |
| 110 | ) >> |
| 111 | many0!(alt!( |
| 112 | tap!(args: and_call => { |
| 113 | let (args, paren) = args; |
| 114 | e = ConstCall { |
| 115 | func: Box::new(e), |
| 116 | args: args, |
| 117 | paren_token: paren, |
| 118 | }.into(); |
| 119 | }) |
| 120 | | |
| 121 | tap!(more: and_binary => { |
| 122 | let (op, other) = more; |
| 123 | e = ConstBinary { op: op, left: Box::new(e), right: Box::new(other) }.into(); |
| 124 | }) |
| 125 | | |
| 126 | tap!(ty: and_cast => { |
| 127 | let (ty, token) = ty; |
| 128 | e = ConstCast { |
| 129 | expr: Box::new(e), |
| 130 | ty: Box::new(ty), |
| 131 | as_token: token, |
| 132 | }.into(); |
| 133 | }) |
| 134 | | |
| 135 | tap!(i: and_index => { |
| 136 | let (i, bracket) = i; |
| 137 | e = ConstIndex { |
| 138 | expr: Box::new(e), |
| 139 | index: Box::new(i), |
| 140 | bracket_token: bracket, |
| 141 | }.into(); |
| 142 | }) |
| 143 | )) >> |
| 144 | (e) |
| 145 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 146 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 147 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 148 | named!(and_call -> (Delimited<ConstExpr, tokens::Comma>, tokens::Paren), |
| 149 | parens!(call!(Delimited::parse_terminated))); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 150 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 151 | named!(and_binary -> (BinOp, ConstExpr), |
| 152 | tuple!(call!(BinOp::parse_binop), syn!(ConstExpr))); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 153 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 154 | impl Synom for ConstUnary { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame^] | 155 | named!(parse -> Self, do_parse!( |
| 156 | operator: syn!(UnOp) >> |
| 157 | operand: syn!(ConstExpr) >> |
| 158 | (ConstUnary { op: operator, expr: Box::new(operand) }) |
| 159 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 160 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 161 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 162 | named!(and_index -> (ConstExpr, tokens::Bracket), |
| 163 | brackets!(syn!(ConstExpr))); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 164 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 165 | impl Synom for ConstParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame^] | 166 | named!(parse -> Self, do_parse!( |
| 167 | parens: parens!(syn!(ConstExpr)) >> |
| 168 | (ConstParen { |
| 169 | expr: Box::new(parens.0), |
| 170 | paren_token: parens.1, |
| 171 | }) |
| 172 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 173 | } |
David Tolnay | f43ba80 | 2016-10-30 10:20:47 -0700 | [diff] [blame] | 174 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 175 | named!(and_cast -> (Ty, tokens::As), do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 176 | as_tok: syn!(As) >> |
| 177 | ty: syn!(Ty) >> |
| 178 | (ty, as_tok) |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 179 | )); |
| 180 | } |
| 181 | |
| 182 | #[cfg(feature = "printing")] |
| 183 | mod printing { |
| 184 | use super::*; |
| 185 | use quote::{Tokens, ToTokens}; |
| 186 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 187 | impl ToTokens for ConstCall { |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 188 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 189 | self.func.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 190 | self.paren_token.surround(tokens, |tokens| { |
| 191 | self.args.to_tokens(tokens); |
| 192 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | impl ToTokens for ConstBinary { |
| 197 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 198 | self.left.to_tokens(tokens); |
| 199 | self.op.to_tokens(tokens); |
| 200 | self.right.to_tokens(tokens); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | impl ToTokens for ConstUnary { |
| 205 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 206 | self.op.to_tokens(tokens); |
| 207 | self.expr.to_tokens(tokens); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | impl ToTokens for ConstCast { |
| 212 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 213 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 214 | self.as_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 215 | self.ty.to_tokens(tokens); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | impl ToTokens for ConstIndex { |
| 220 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 221 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 222 | self.bracket_token.surround(tokens, |tokens| { |
| 223 | self.index.to_tokens(tokens); |
| 224 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | impl ToTokens for ConstParen { |
| 229 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 230 | self.paren_token.surround(tokens, |tokens| { |
| 231 | self.expr.to_tokens(tokens); |
| 232 | }) |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 235 | |
| 236 | #[cfg(not(feature = "full"))] |
| 237 | impl ToTokens for Other { |
| 238 | fn to_tokens(&self, _tokens: &mut Tokens) { |
| 239 | unreachable!() |
| 240 | } |
| 241 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 242 | } |