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