| 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | |
| 5 | use crate::bindgen::ir::Path; |
| 6 | use std::collections::hash_map::Entry; |
| 7 | use std::collections::HashMap; |
| 8 | |
| 9 | impl DeclarationType { |
| 10 | pub fn to_str(self) -> &'static str { |
| 11 | match self { |
| 12 | DeclarationType::Struct => "struct" , |
| 13 | DeclarationType::Enum => "enum" , |
| 14 | DeclarationType::Union => "union" , |
| 15 | } |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | #[derive (Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] |
| 20 | pub enum DeclarationType { |
| 21 | Struct, |
| 22 | Enum, |
| 23 | Union, |
| 24 | } |
| 25 | |
| 26 | #[derive (Default)] |
| 27 | pub struct DeclarationTypeResolver { |
| 28 | types: HashMap<Path, Option<DeclarationType>>, |
| 29 | } |
| 30 | |
| 31 | impl DeclarationTypeResolver { |
| 32 | fn insert(&mut self, path: &Path, ty: Option<DeclarationType>) { |
| 33 | if let Entry::Vacant(vacant_entry) = self.types.entry(path.clone()) { |
| 34 | vacant_entry.insert(ty); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub fn add_enum(&mut self, path: &Path) { |
| 39 | self.insert(path, Some(DeclarationType::Enum)); |
| 40 | } |
| 41 | |
| 42 | pub fn add_struct(&mut self, path: &Path) { |
| 43 | self.insert(path, Some(DeclarationType::Struct)); |
| 44 | } |
| 45 | |
| 46 | pub fn add_union(&mut self, path: &Path) { |
| 47 | self.insert(path, Some(DeclarationType::Union)); |
| 48 | } |
| 49 | |
| 50 | pub fn add_none(&mut self, path: &Path) { |
| 51 | self.insert(path, None); |
| 52 | } |
| 53 | |
| 54 | pub fn type_for(&self, path: &Path) -> Option<DeclarationType> { |
| 55 | *self.types.get(path)? |
| 56 | } |
| 57 | } |
| 58 | |