| ... | ... | @@ -122,6 +122,7 @@ const Scope = struct { |
| 122 | 122 | base: Scope, |
| 123 | 123 | sym_table: SymbolTable, |
| 124 | 124 | macro_table: SymbolTable, |
| 125 | context: *Context, |
| 125 | 126 | |
| 126 | 127 | fn init(c: *Context) Root { |
| 127 | 128 | return .{ |
| ... | ... | @@ -131,14 +132,20 @@ const Scope = struct { |
| 131 | 132 | }, |
| 132 | 133 | .sym_table = SymbolTable.init(c.a()), |
| 133 | 134 | .macro_table = SymbolTable.init(c.a()), |
| 135 | .context = c, |
| 134 | 136 | }; |
| 135 | 137 | } |
| 136 | 138 | |
| 137 | | fn contains(scope: *Root, name: []const u8) bool { |
| 138 | | return isZigPrimitiveType(name) or |
| 139 | | scope.sym_table.contains(name) or |
| 139 | fn localContains(scope: *Root, name: []const u8) bool { |
| 140 | return scope.sym_table.contains(name) or |
| 140 | 141 | scope.macro_table.contains(name); |
| 141 | 142 | } |
| 143 | |
| 144 | fn contains(scope: *Root, name: []const u8) bool { |
| 145 | return scope.localContains(name) or |
| 146 | isZigPrimitiveType(name) or |
| 147 | scope.context.global_names.contains(name); |
| 148 | } |
| 142 | 149 | }; |
| 143 | 150 | |
| 144 | 151 | fn findBlockScope(inner: *Scope, c: *Context) !*Scope.Block { |
| ... | ... | @@ -207,6 +214,12 @@ pub const Context = struct { |
| 207 | 214 | clang_context: *ZigClangASTContext, |
| 208 | 215 | mangle_count: u32 = 0, |
| 209 | 216 | |
| 217 | /// This one is different than the root scope's name table. This contains |
| 218 | /// a list of names that we found by visiting all the top level decls without |
| 219 | /// translating them. The other maps are updated as we translate; this one is updated |
| 220 | /// up front in a pre-processing step. |
| 221 | global_names: std.StringHashMap(void), |
| 222 | |
| 210 | 223 | fn getMangle(c: *Context) u32 { |
| 211 | 224 | c.mangle_count += 1; |
| 212 | 225 | return c.mangle_count; |
| ... | ... | @@ -291,9 +304,14 @@ pub fn translate( |
| 291 | 304 | .alias_list = AliasList.init(arena), |
| 292 | 305 | .global_scope = try arena.create(Scope.Root), |
| 293 | 306 | .clang_context = ZigClangASTUnit_getASTContext(ast_unit).?, |
| 307 | .global_names = std.StringHashMap(void).init(arena), |
| 294 | 308 | }; |
| 295 | 309 | context.global_scope.* = Scope.Root.init(&context); |
| 296 | 310 | |
| 311 | if (!ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, &context, declVisitorNamesOnlyC)) { |
| 312 | return context.err; |
| 313 | } |
| 314 | |
| 297 | 315 | if (!ZigClangASTUnit_visitLocalTopLevelDecls(ast_unit, &context, declVisitorC)) { |
| 298 | 316 | return context.err; |
| 299 | 317 | } |
| ... | ... | @@ -321,6 +339,15 @@ pub fn translate( |
| 321 | 339 | return tree; |
| 322 | 340 | } |
| 323 | 341 | |
| 342 | extern fn declVisitorNamesOnlyC(context: ?*c_void, decl: *const ZigClangDecl) bool { |
| 343 | const c = @ptrCast(*Context, @alignCast(@alignOf(Context), context)); |
| 344 | declVisitorNamesOnly(c, decl) catch |err| { |
| 345 | c.err = err; |
| 346 | return false; |
| 347 | }; |
| 348 | return true; |
| 349 | } |
| 350 | |
| 324 | 351 | extern fn declVisitorC(context: ?*c_void, decl: *const ZigClangDecl) bool { |
| 325 | 352 | const c = @ptrCast(*Context, @alignCast(@alignOf(Context), context)); |
| 326 | 353 | declVisitor(c, decl) catch |err| { |
| ... | ... | @@ -330,6 +357,11 @@ extern fn declVisitorC(context: ?*c_void, decl: *const ZigClangDecl) bool { |
| 330 | 357 | return true; |
| 331 | 358 | } |
| 332 | 359 | |
| 360 | fn declVisitorNamesOnly(c: *Context, decl: *const ZigClangDecl) Error!void { |
| 361 | const decl_name = try c.str(ZigClangDecl_getName_bytes_begin(decl)); |
| 362 | _ = try c.global_names.put(decl_name, {}); |
| 363 | } |
| 364 | |
| 333 | 365 | fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { |
| 334 | 366 | switch (ZigClangDecl_getKind(decl)) { |
| 335 | 367 | .Function => { |