diff options
Diffstat (limited to 'src/parser/clang/parsing.rs')
-rw-r--r-- | src/parser/clang/parsing.rs | 86 |
1 files changed, 84 insertions, 2 deletions
diff --git a/src/parser/clang/parsing.rs b/src/parser/clang/parsing.rs index 1f1691f..251aefa 100644 --- a/src/parser/clang/parsing.rs +++ b/src/parser/clang/parsing.rs | |||
@@ -20,6 +20,7 @@ struct TopLevel { | |||
20 | structs: BTreeMap<Usr, Described<Struct>>, | 20 | structs: BTreeMap<Usr, Described<Struct>>, |
21 | functions: BTreeMap<Usr, Described<Function>>, | 21 | functions: BTreeMap<Usr, Described<Function>>, |
22 | typedefs: BTreeMap<Usr, Described<Typedef>>, | 22 | typedefs: BTreeMap<Usr, Described<Typedef>>, |
23 | enums: BTreeMap<Usr, Described<Enum>>, | ||
23 | } | 24 | } |
24 | 25 | ||
25 | /* | 26 | /* |
@@ -88,6 +89,10 @@ impl TopLevel { | |||
88 | ClangEntityKind::Typedef => { | 89 | ClangEntityKind::Typedef => { |
89 | &mut parent.get_member_typedefs()?.get_mut(&usr)?.entity | 90 | &mut parent.get_member_typedefs()?.get_mut(&usr)?.entity |
90 | } | 91 | } |
92 | ClangEntityKind::Enum => &mut parent.get_member_enums()?.get_mut(&usr)?.entity, | ||
93 | ClangEntityKind::EnumConstant => { | ||
94 | panic!("enum variant getting"); | ||
95 | } | ||
91 | }) | 96 | }) |
92 | } else { | 97 | } else { |
93 | Some(match path.get_kind().try_into().ok()? { | 98 | Some(match path.get_kind().try_into().ok()? { |
@@ -96,6 +101,8 @@ impl TopLevel { | |||
96 | ClangEntityKind::Struct(_) => &mut self.structs.get_mut(&usr)?.entity, | 101 | ClangEntityKind::Struct(_) => &mut self.structs.get_mut(&usr)?.entity, |
97 | ClangEntityKind::Function(_) => &mut self.functions.get_mut(&usr)?.entity, | 102 | ClangEntityKind::Function(_) => &mut self.functions.get_mut(&usr)?.entity, |
98 | ClangEntityKind::Typedef => &mut self.typedefs.get_mut(&usr)?.entity, | 103 | ClangEntityKind::Typedef => &mut self.typedefs.get_mut(&usr)?.entity, |
104 | ClangEntityKind::Enum => &mut self.enums.get_mut(&usr)?.entity, | ||
105 | ClangEntityKind::EnumConstant => panic!("enum variant getting"), | ||
99 | }) | 106 | }) |
100 | } | 107 | } |
101 | } | 108 | } |
@@ -191,6 +198,12 @@ impl TopLevelManipulation<Typedef> for TopLevel { | |||
191 | } | 198 | } |
192 | } | 199 | } |
193 | 200 | ||
201 | impl TopLevelManipulation<Enum> for TopLevel { | ||
202 | fn insert_toplevel(&mut self, usr: Usr, entity: Described<Enum>) { | ||
203 | self.enums.insert(usr, entity); | ||
204 | } | ||
205 | } | ||
206 | |||
194 | /* | 207 | /* |
195 | trait FromTopLevel: ClangEntity + Sized { | 208 | trait FromTopLevel: ClangEntity + Sized { |
196 | fn from_toplevel<'a>(toplevel: &'a mut TopLevel, usr: &Usr) -> Option<&'a mut Described<Self>>; | 209 | fn from_toplevel<'a>(toplevel: &'a mut TopLevel, usr: &Usr) -> Option<&'a mut Described<Self>>; |
@@ -526,7 +539,10 @@ fn add_entity( | |||
526 | ClangEntityKind::Function(_) => Described::<Function>::try_from(libclang_entity) | 539 | ClangEntityKind::Function(_) => Described::<Function>::try_from(libclang_entity) |
527 | .and_then(|function| toplevel.insert(libclang_entity, function)), | 540 | .and_then(|function| toplevel.insert(libclang_entity, function)), |
528 | ClangEntityKind::Typedef => Described::<Typedef>::try_from(libclang_entity) | 541 | ClangEntityKind::Typedef => Described::<Typedef>::try_from(libclang_entity) |
529 | .and_then(|function| toplevel.insert(libclang_entity, function)), | 542 | .and_then(|typedef| toplevel.insert(libclang_entity, typedef)), |
543 | ClangEntityKind::Enum => Described::<Enum>::try_from(libclang_entity) | ||
544 | .and_then(|r#enum| toplevel.insert(libclang_entity, r#enum)), | ||
545 | ClangEntityKind::EnumConstant => panic!("Enum variant encountered not within an enum"), | ||
530 | }; | 546 | }; |
531 | // TODO: check result | 547 | // TODO: check result |
532 | 548 | ||
@@ -611,6 +627,7 @@ impl<'a> TryFrom<clang::Entity<'a>> for Namespace { | |||
611 | member_structs: Default::default(), | 627 | member_structs: Default::default(), |
612 | member_functions: Default::default(), | 628 | member_functions: Default::default(), |
613 | member_typedefs: Default::default(), | 629 | member_typedefs: Default::default(), |
630 | member_enums: Default::default(), | ||
614 | }) | 631 | }) |
615 | } | 632 | } |
616 | } | 633 | } |
@@ -655,6 +672,7 @@ impl<'a> TryFrom<clang::Entity<'a>> for Struct { | |||
655 | let mut member_structs = BTreeMap::new(); | 672 | let mut member_structs = BTreeMap::new(); |
656 | let mut member_functions = BTreeMap::new(); | 673 | let mut member_functions = BTreeMap::new(); |
657 | let mut member_typedefs = BTreeMap::new(); | 674 | let mut member_typedefs = BTreeMap::new(); |
675 | let mut member_enums = BTreeMap::new(); | ||
658 | 676 | ||
659 | for child in entity.get_children() { | 677 | for child in entity.get_children() { |
660 | trace!("Struct has child: {:?}", child); | 678 | trace!("Struct has child: {:?}", child); |
@@ -694,6 +712,12 @@ impl<'a> TryFrom<clang::Entity<'a>> for Struct { | |||
694 | .ok_or_else(|| anyhow!("no usr for: {:?}", child))?; | 712 | .ok_or_else(|| anyhow!("no usr for: {:?}", child))?; |
695 | member_typedefs.insert(child_usr, Described::<Typedef>::try_from(child)?); | 713 | member_typedefs.insert(child_usr, Described::<Typedef>::try_from(child)?); |
696 | } | 714 | } |
715 | Ok(ClangEntityKind::Enum) => { | ||
716 | let child_usr = child | ||
717 | .get_usr() | ||
718 | .ok_or_else(|| anyhow!("no usr for: {:?}", child))?; | ||
719 | member_enums.insert(child_usr, Described::<Enum>::try_from(child)?); | ||
720 | } | ||
697 | Ok(other) => warn!("Unsupported child of struct {:?}: {:?}", other, child), | 721 | Ok(other) => warn!("Unsupported child of struct {:?}: {:?}", other, child), |
698 | Err(err) => info!("Error while parsing entity {:?}: {}", child, err), | 722 | Err(err) => info!("Error while parsing entity {:?}: {}", child, err), |
699 | } | 723 | } |
@@ -715,6 +739,7 @@ impl<'a> TryFrom<clang::Entity<'a>> for Struct { | |||
715 | member_structs, | 739 | member_structs, |
716 | member_variables, | 740 | member_variables, |
717 | member_typedefs, | 741 | member_typedefs, |
742 | member_enums, | ||
718 | }) | 743 | }) |
719 | } | 744 | } |
720 | } | 745 | } |
@@ -768,7 +793,6 @@ impl<'a> TryFrom<clang::Entity<'a>> for Typedef { | |||
768 | } | 793 | } |
769 | debug!("Parsing typedef: {:?}", entity); | 794 | debug!("Parsing typedef: {:?}", entity); |
770 | 795 | ||
771 | // TODO: unwrap (and unwrap in other similar places too) | ||
772 | let referee = entity | 796 | let referee = entity |
773 | .get_typedef_underlying_type() | 797 | .get_typedef_underlying_type() |
774 | .ok_or_else(|| anyhow!("No underlying type"))? | 798 | .ok_or_else(|| anyhow!("No underlying type"))? |
@@ -778,6 +802,64 @@ impl<'a> TryFrom<clang::Entity<'a>> for Typedef { | |||
778 | } | 802 | } |
779 | } | 803 | } |
780 | 804 | ||
805 | impl<'a> TryFrom<clang::Entity<'a>> for Enum { | ||
806 | type Error = Error; | ||
807 | |||
808 | fn try_from(entity: clang::Entity) -> Result<Self, Error> { | ||
809 | match entity.get_kind().try_into() { | ||
810 | Ok(ClangEntityKind::Enum) => {} | ||
811 | _ => panic!("Trying to parse a non-enum into a enum"), | ||
812 | } | ||
813 | debug!("Parsing enum: {:?}", entity); | ||
814 | |||
815 | let underlying_type = entity | ||
816 | .get_enum_underlying_type() | ||
817 | .ok_or_else(|| anyhow!("No enum underlying type"))? | ||
818 | .get_display_name(); | ||
819 | |||
820 | let mut constants = BTreeMap::new(); | ||
821 | for child in entity.get_children() { | ||
822 | trace!("Enum has child: {:?}", child); | ||
823 | match child.get_kind().try_into() { | ||
824 | Ok(ClangEntityKind::EnumConstant) => { | ||
825 | let child_usr = child | ||
826 | .get_usr() | ||
827 | .ok_or_else(|| anyhow!("no usr for: {:?}", child))?; | ||
828 | constants.insert(child_usr, Described::<EnumConstant>::try_from(child)?); | ||
829 | } | ||
830 | Ok(other) => warn!("Unsupported child of enum {:?}: {:?}", other, child), | ||
831 | Err(err) => info!("Error while parsing entity {:?}: {}", child, err), | ||
832 | } | ||
833 | } | ||
834 | |||
835 | Ok(Enum { | ||
836 | underlying_type, | ||
837 | constants, | ||
838 | }) | ||
839 | } | ||
840 | } | ||
841 | |||
842 | impl<'a> TryFrom<clang::Entity<'a>> for EnumConstant { | ||
843 | type Error = Error; | ||
844 | |||
845 | fn try_from(entity: clang::Entity) -> Result<Self, Error> { | ||
846 | match entity.get_kind().try_into() { | ||
847 | Ok(ClangEntityKind::EnumConstant) => {} | ||
848 | _ => panic!("Trying to parse a non-enum constant into a enum constant"), | ||
849 | } | ||
850 | debug!("Parsing enum: {:?}", entity); | ||
851 | |||
852 | let value = entity | ||
853 | .get_enum_constant_value() | ||
854 | .ok_or_else(|| anyhow!("No enum constant value"))?; | ||
855 | dbg!(value); | ||
856 | |||
857 | Ok(EnumConstant { | ||
858 | value: String::from(""), | ||
859 | }) | ||
860 | } | ||
861 | } | ||
862 | |||
781 | fn get_description(entity: clang::Entity) -> Result<Description> { | 863 | fn get_description(entity: clang::Entity) -> Result<Description> { |
782 | let name = entity | 864 | let name = entity |
783 | .get_display_name() | 865 | .get_display_name() |