diff options
Diffstat (limited to 'src/parser.rs')
-rw-r--r-- | src/parser.rs | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/src/parser.rs b/src/parser.rs index 1acf1c60..3004eb40 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -26,6 +26,7 @@ pub struct ClangParserOptions { pub ignore_functions: bool, pub enable_cxx_namespaces: bool, pub class_constants: bool, + pub namespaced_constants: bool, pub override_enum_ty: Option<il::IKind>, pub clang_args: Vec<String>, pub opaque_types: Vec<String>, @@ -38,6 +39,9 @@ struct ClangParserCtx<'a> { builtin_defs: Vec<Cursor>, module_map: ModuleMap, current_module_id: ModuleId, + /// This member is used to track down if we're in a namespace if the + /// enable_cxx_namespaces option is disabled. + namespace_depth: usize, current_translation_unit: TranslationUnit, logger: &'a (Logger+'a), err_count: i32, @@ -53,6 +57,10 @@ impl<'a> ClangParserCtx<'a> { self.module(&self.current_module_id) } + fn in_root_namespace(&self) -> bool { + self.namespace_depth == 0 + } + fn current_module_mut(&mut self) -> &mut Module { self.module_map.get_mut(&self.current_module_id).expect("Module not found!") } @@ -1229,6 +1237,12 @@ fn visit_top(cursor: &Cursor, CXChildVisit_Continue } CXCursor_VarDecl => { + // TODO: At some point we might want to mangle them instead? + // We already have a bunch of that logic. + if !ctx.in_root_namespace() && !ctx.options.namespaced_constants { + return CXChildVisit_Continue; + } + let linkage = cursor.linkage(); if linkage != CXLinkage_External && linkage != CXLinkage_UniqueExternal { return CXChildVisit_Continue; @@ -1287,7 +1301,10 @@ fn visit_top(cursor: &Cursor, } CXCursor_Namespace => { if !ctx.options.enable_cxx_namespaces { - return CXChildVisit_Recurse; + ctx.namespace_depth += 1; + cursor.visit(|cur, _: &Cursor| visit_top(cur, &mut ctx)); + ctx.namespace_depth -= 1; + return CXChildVisit_Continue; } let namespace_name = match ctx.current_translation_unit.tokens(cursor) { @@ -1327,27 +1344,29 @@ fn visit_top(cursor: &Cursor, let previous_id = ctx.current_module_id; ctx.current_module_id = mod_id; + ctx.namespace_depth += 1; cursor.visit(|cur, _: &Cursor| visit_top(cur, &mut ctx)); + ctx.namespace_depth -= 1; ctx.current_module_id = previous_id; return CXChildVisit_Continue; } CXCursor_MacroDefinition => { let val = parse_int_literal_tokens(cursor, &ctx.current_translation_unit, 1); - if val.is_none() { - // Not an integer literal. - return CXChildVisit_Continue; - } + let val = match val { + None => return CXChildVisit_Continue, // Not an integer literal. + Some(v) => v, + }; let var = decl_name(ctx, cursor); let vi = var.varinfo(); let mut vi = vi.borrow_mut(); - vi.ty = match val { - None => TVoid, - Some(v) if v.abs() > u32::max_value() as i64 => TInt(IULongLong, Layout::new(8, 8)), - _ => TInt(IUInt, Layout::new(4, 4)), + vi.ty = if val.abs() > u32::max_value() as i64 { + TInt(IULongLong, Layout::new(8, 8)) + } else { + TInt(IUInt, Layout::new(4, 4)) }; vi.is_const = true; - vi.val = val; + vi.val = Some(val); ctx.current_module_mut().globals.push(var); return CXChildVisit_Continue; @@ -1386,6 +1405,7 @@ pub fn parse(options: ClangParserOptions, logger: &Logger) -> Result<ModuleMap, name: HashMap::new(), builtin_defs: vec!(), module_map: ModuleMap::new(), + namespace_depth: 0, current_module_id: ROOT_MODULE_ID, current_translation_unit: unit, logger: logger, |