| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 1 | use crate::syntax::Atom::{self, *}; |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 2 | use proc_macro2::{Literal, Span, TokenStream}; |
| 3 | use quote::ToTokens; |
| 4 | use std::collections::HashSet; |
| 5 | use std::fmt::{self, Display}; |
| 6 | use std::str::FromStr; |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 7 | use syn::{Error, Expr, Lit, Result, Token, UnOp}; |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 8 | |
| 9 | pub struct DiscriminantSet { |
| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 10 | repr: Option<Atom>, |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 11 | values: HashSet<Discriminant>, |
| 12 | previous: Option<Discriminant>, |
| 13 | } |
| 14 | |
| 15 | #[derive(Copy, Clone, Hash, Eq, PartialEq)] |
| 16 | pub struct Discriminant { |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 17 | negative: bool, |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 18 | magnitude: u32, |
| 19 | } |
| 20 | |
| 21 | impl DiscriminantSet { |
| 22 | pub fn new() -> Self { |
| 23 | DiscriminantSet { |
| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 24 | repr: None, |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 25 | values: HashSet::new(), |
| 26 | previous: None, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | pub fn insert(&mut self, expr: &Expr) -> Result<Discriminant> { |
| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 31 | let (discriminant, repr) = expr_to_discriminant(expr)?; |
| 32 | self.repr = self.repr.or(repr); |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 33 | insert(self, discriminant) |
| 34 | } |
| 35 | |
| 36 | pub fn insert_next(&mut self) -> Result<Discriminant> { |
| 37 | let discriminant = match self.previous { |
| 38 | None => Discriminant::zero(), |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 39 | Some(mut discriminant) if discriminant.negative => { |
| 40 | discriminant.magnitude -= 1; |
| 41 | if discriminant.magnitude == 0 { |
| 42 | discriminant.negative = false; |
| 43 | } |
| 44 | discriminant |
| 45 | } |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 46 | Some(mut discriminant) => { |
| 47 | if discriminant.magnitude == u32::MAX { |
| 48 | let msg = format!("discriminant overflow on value after {}", u32::MAX); |
| 49 | return Err(Error::new(Span::call_site(), msg)); |
| 50 | } |
| 51 | discriminant.magnitude += 1; |
| 52 | discriminant |
| 53 | } |
| 54 | }; |
| 55 | insert(self, discriminant) |
| 56 | } |
| 57 | } |
| 58 | |
| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 59 | fn expr_to_discriminant(expr: &Expr) -> Result<(Discriminant, Option<Atom>)> { |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 60 | match expr { |
| 61 | Expr::Lit(expr) => { |
| 62 | if let Lit::Int(lit) = &expr.lit { |
| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 63 | let discriminant = lit.base10_parse::<Discriminant>()?; |
| 64 | let repr = parse_int_suffix(lit.suffix())?; |
| 65 | return Ok((discriminant, repr)); |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 66 | } |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 67 | } |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 68 | Expr::Unary(unary) => { |
| 69 | if let UnOp::Neg(_) = unary.op { |
| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 70 | let (mut discriminant, repr) = expr_to_discriminant(&unary.expr)?; |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 71 | discriminant.negative ^= true; |
| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 72 | return Ok((discriminant, repr)); |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | _ => {} |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 76 | } |
| 77 | Err(Error::new_spanned( |
| 78 | expr, |
| 79 | "enums with non-integer literal discriminants are not supported yet", |
| 80 | )) |
| 81 | } |
| 82 | |
| 83 | fn insert(set: &mut DiscriminantSet, discriminant: Discriminant) -> Result<Discriminant> { |
| 84 | if set.values.insert(discriminant) { |
| 85 | set.previous = Some(discriminant); |
| 86 | Ok(discriminant) |
| 87 | } else { |
| 88 | let msg = format!("discriminant value `{}` already exists", discriminant); |
| 89 | Err(Error::new(Span::call_site(), msg)) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl Discriminant { |
| David Tolnay | f1715fa | 2020-05-10 18:58:49 -0700 | [diff] [blame^] | 94 | const fn zero() -> Self { |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 95 | Discriminant { |
| 96 | negative: false, |
| 97 | magnitude: 0, |
| 98 | } |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 99 | } |
| David Tolnay | f1715fa | 2020-05-10 18:58:49 -0700 | [diff] [blame^] | 100 | |
| 101 | const fn pos(u: u32) -> Self { |
| 102 | Discriminant { |
| 103 | negative: false, |
| 104 | magnitude: u, |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | const fn neg(i: i32) -> Self { |
| 109 | Discriminant { |
| 110 | negative: i < 0, |
| 111 | // This is `i.abs() as u32` but without overflow on MIN. Uses the |
| 112 | // fact that MIN.wrapping_abs() wraps back to MIN whose binary |
| 113 | // representation is 1<<31, and thus the `as u32` conversion |
| 114 | // produces 1<<31 too which happens to be the correct unsigned |
| 115 | // magnitude. |
| 116 | magnitude: i.wrapping_abs() as u32, |
| 117 | } |
| 118 | } |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | impl Display for Discriminant { |
| 122 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 123 | if self.negative { |
| 124 | f.write_str("-")?; |
| 125 | } |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 126 | Display::fmt(&self.magnitude, f) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl ToTokens for Discriminant { |
| 131 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 132 | if self.negative { |
| 133 | Token).to_tokens(tokens); |
| 134 | } |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 135 | Literal::u32_unsuffixed(self.magnitude).to_tokens(tokens); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl FromStr for Discriminant { |
| 140 | type Err = Error; |
| 141 | |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 142 | fn from_str(mut s: &str) -> Result<Self> { |
| 143 | let negative = s.starts_with('-'); |
| 144 | if negative { |
| 145 | s = &s[1..]; |
| 146 | } |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 147 | match s.parse::<u32>() { |
| David Tolnay | 69c7960 | 2020-05-10 18:53:31 -0700 | [diff] [blame] | 148 | Ok(magnitude) => Ok(Discriminant { |
| 149 | negative, |
| 150 | magnitude, |
| 151 | }), |
| David Tolnay | 17e137f | 2020-05-08 15:55:28 -0700 | [diff] [blame] | 152 | Err(_) => Err(Error::new( |
| 153 | Span::call_site(), |
| 154 | "discriminant value outside of supported range", |
| 155 | )), |
| 156 | } |
| 157 | } |
| 158 | } |
| David Tolnay | 9bcb4c6 | 2020-05-10 17:42:06 -0700 | [diff] [blame] | 159 | |
| 160 | fn parse_int_suffix(suffix: &str) -> Result<Option<Atom>> { |
| 161 | if suffix.is_empty() { |
| 162 | return Ok(None); |
| 163 | } |
| 164 | if let Some(atom) = Atom::from_str(suffix) { |
| 165 | match atom { |
| 166 | U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize => return Ok(Some(atom)), |
| 167 | _ => {} |
| 168 | } |
| 169 | } |
| 170 | let msg = format!("unrecognized integer suffix: `{}`", suffix); |
| 171 | Err(Error::new(Span::call_site(), msg)) |
| 172 | } |
| David Tolnay | f1715fa | 2020-05-10 18:58:49 -0700 | [diff] [blame^] | 173 | |
| 174 | struct Bounds { |
| 175 | repr: Atom, |
| 176 | min: Discriminant, |
| 177 | max: Discriminant, |
| 178 | } |
| 179 | |
| 180 | const BOUNDS: [Bounds; 6] = [ |
| 181 | Bounds { |
| 182 | repr: U8, |
| 183 | min: Discriminant::zero(), |
| 184 | max: Discriminant::pos(u8::MAX as u32), |
| 185 | }, |
| 186 | Bounds { |
| 187 | repr: I8, |
| 188 | min: Discriminant::neg(i8::MIN as i32), |
| 189 | max: Discriminant::pos(i8::MAX as u32), |
| 190 | }, |
| 191 | Bounds { |
| 192 | repr: U16, |
| 193 | min: Discriminant::zero(), |
| 194 | max: Discriminant::pos(u16::MAX as u32), |
| 195 | }, |
| 196 | Bounds { |
| 197 | repr: I16, |
| 198 | min: Discriminant::neg(i16::MIN as i32), |
| 199 | max: Discriminant::pos(i16::MAX as u32), |
| 200 | }, |
| 201 | Bounds { |
| 202 | repr: U32, |
| 203 | min: Discriminant::zero(), |
| 204 | max: Discriminant::pos(u32::MAX), |
| 205 | }, |
| 206 | Bounds { |
| 207 | repr: I32, |
| 208 | min: Discriminant::neg(i32::MIN), |
| 209 | max: Discriminant::pos(i32::MAX as u32), |
| 210 | }, |
| 211 | ]; |