authorgravatar for jonathan.haehne@hotmail.comTau <jonathan.haehne@hotmail.com> 2024-06-20 19:10:26+02:00
committergravatar for jonathan.haehne@hotmail.comTau <jonathan.haehne@hotmail.com> 2024-07-19 17:46:34+02:00
log876258abe46273aeac7c9791c7ec69bceb29f5ae
treec21713bd3200a847d65aea06c64a9a5b408c6f62
parentebd9efa85052fc19d8296e8e0f4da079da9cab45

llvm: set precise scopes on namespace types and variables

This will allow accessing non-local declarations from debuggers, which, AFAICT, was impossible before. Getting scopes right already works for type declarations and functions, but will need some fiddling for variables: For those, I tried imitating what Clang does for static member variables, but LLDB tries to re-mangle those and then fails at lookup, while GDB outright crashes. Hopefully I can find some other dwarven incantation to do the right thing.

3 files changed, 313 insertions(+), 229 deletions(-)

src/codegen/llvm.zig+223-196
......@@ -1140,18 +1140,7 @@ pub const Object = struct {
11401140 try self.genModuleLevelAssembly();
11411141
11421142 if (!self.builder.strip) {
1143 {
1144 var i: usize = 0;
1145 while (i < self.debug_unresolved_namespace_scopes.count()) : (i += 1) {
1146 const namespace_index = self.debug_unresolved_namespace_scopes.keys()[i];
1147 const fwd_ref = self.debug_unresolved_namespace_scopes.values()[i];
1148
1149 const namespace = zcu.namespacePtr(namespace_index);
1150 const debug_type = try self.lowerDebugType(namespace.getType(zcu));
1151
1152 self.builder.debugForwardReferenceSetType(fwd_ref, debug_type);
1153 }
1154 }
1143 try self.genNamespaces();
11551144
11561145 self.builder.debugForwardReferenceSetType(
11571146 self.debug_enums_fwd_ref,
......@@ -1641,6 +1630,7 @@ pub const Object = struct {
16411630
16421631 const file, const subprogram = if (!wip.strip) debug_info: {
16431632 const file = try o.getDebugFile(file_scope);
1633 const scope = try o.namespaceToDebugScope(decl.src_namespace);
16441634
16451635 const line_number = decl.navSrcLine(zcu) + 1;
16461636 const is_internal_linkage = decl.val.getExternFunc(zcu) == null;
......@@ -1648,6 +1638,7 @@ pub const Object = struct {
16481638
16491639 const subprogram = try o.builder.debugSubprogram(
16501640 file,
1641 scope,
16511642 try o.builder.metadataString(decl.name.toSlice(ip)),
16521643 try o.builder.metadataStringFromStrtabString(function_index.name(&o.builder)),
16531644 line_number,
......@@ -1924,6 +1915,7 @@ pub const Object = struct {
19241915
19251916 if (o.debug_type_map.get(ty)) |debug_type| return debug_type;
19261917
1918
19271919 switch (ty.zigTypeTag(zcu)) {
19281920 .Void,
19291921 .NoReturn,
......@@ -1938,9 +1930,9 @@ pub const Object = struct {
19381930 .Int => {
19391931 const info = ty.intInfo(zcu);
19401932 assert(info.bits != 0);
1941 const name = try o.allocTypeName(ty);
1942 defer gpa.free(name);
1943 const builder_name = try o.builder.metadataString(name);
1933 const int_name = try o.allocTypeName(ty);
1934 defer gpa.free(int_name);
1935 const builder_name = try o.builder.metadataString(int_name);
19441936 const debug_bits = ty.abiSize(pt) * 8; // lldb cannot handle non-byte sized types
19451937 const debug_int_type = switch (info.signedness) {
19461938 .signed => try o.builder.debugSignedType(builder_name, debug_bits),
......@@ -1949,68 +1941,12 @@ pub const Object = struct {
19491941 try o.debug_type_map.put(gpa, ty, debug_int_type);
19501942 return debug_int_type;
19511943 },
1952 .Enum => {
1953 const owner_decl_index = ty.getOwnerDecl(zcu);
1954 const owner_decl = zcu.declPtr(owner_decl_index);
1955
1956 if (!ty.hasRuntimeBitsIgnoreComptime(pt)) {
1957 const debug_enum_type = try o.makeEmptyNamespaceDebugType(owner_decl_index);
1958 try o.debug_type_map.put(gpa, ty, debug_enum_type);
1959 return debug_enum_type;
1960 }
1961
1962 const enum_type = ip.loadEnumType(ty.toIntern());
1963
1964 const enumerators = try gpa.alloc(Builder.Metadata, enum_type.names.len);
1965 defer gpa.free(enumerators);
1966
1967 const int_ty = Type.fromInterned(enum_type.tag_ty);
1968 const int_info = ty.intInfo(zcu);
1969 assert(int_info.bits != 0);
1970
1971 for (enum_type.names.get(ip), 0..) |field_name_ip, i| {
1972 var bigint_space: Value.BigIntSpace = undefined;
1973 const bigint = if (enum_type.values.len != 0)
1974 Value.fromInterned(enum_type.values.get(ip)[i]).toBigInt(&bigint_space, pt)
1975 else
1976 std.math.big.int.Mutable.init(&bigint_space.limbs, i).toConst();
1977
1978 enumerators[i] = try o.builder.debugEnumerator(
1979 try o.builder.metadataString(field_name_ip.toSlice(ip)),
1980 int_info.signedness == .unsigned,
1981 int_info.bits,
1982 bigint,
1983 );
1984 }
1985
1986 const file_scope = zcu.namespacePtr(owner_decl.src_namespace).fileScope(zcu);
1987 const file = try o.getDebugFile(file_scope);
1988 const scope = try o.namespaceToDebugScope(owner_decl.src_namespace);
1989
1990 const name = try o.allocTypeName(ty);
1991 defer gpa.free(name);
1992
1993 const debug_enum_type = try o.builder.debugEnumerationType(
1994 try o.builder.metadataString(name),
1995 file,
1996 scope,
1997 owner_decl.typeSrcLine(zcu) + 1, // Line
1998 try o.lowerDebugType(int_ty),
1999 ty.abiSize(pt) * 8,
2000 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
2001 try o.builder.debugTuple(enumerators),
2002 );
2003
2004 try o.debug_type_map.put(gpa, ty, debug_enum_type);
2005 try o.debug_enums.append(gpa, debug_enum_type);
2006 return debug_enum_type;
2007 },
20081944 .Float => {
20091945 const bits = ty.floatBits(target);
2010 const name = try o.allocTypeName(ty);
2011 defer gpa.free(name);
1946 const float_name = try o.allocTypeName(ty);
1947 defer gpa.free(float_name);
20121948 const debug_float_type = try o.builder.debugFloatType(
2013 try o.builder.metadataString(name),
1949 try o.builder.metadataString(float_name),
20141950 bits,
20151951 );
20161952 try o.debug_type_map.put(gpa, ty, debug_float_type);
......@@ -2068,6 +2004,7 @@ pub const Object = struct {
20682004
20692005 const name = try o.allocTypeName(ty);
20702006 defer gpa.free(name);
2007
20712008 const line = 0;
20722009
20732010 const ptr_size = ptr_ty.abiSize(pt);
......@@ -2146,34 +2083,6 @@ pub const Object = struct {
21462083
21472084 return debug_ptr_type;
21482085 },
2149 .Opaque => {
2150 if (ty.toIntern() == .anyopaque_type) {
2151 const debug_opaque_type = try o.builder.debugSignedType(
2152 try o.builder.metadataString("anyopaque"),
2153 0,
2154 );
2155 try o.debug_type_map.put(gpa, ty, debug_opaque_type);
2156 return debug_opaque_type;
2157 }
2158
2159 const name = try o.allocTypeName(ty);
2160 defer gpa.free(name);
2161 const owner_decl_index = ty.getOwnerDecl(zcu);
2162 const owner_decl = zcu.declPtr(owner_decl_index);
2163 const file_scope = zcu.namespacePtr(owner_decl.src_namespace).fileScope(zcu);
2164 const debug_opaque_type = try o.builder.debugStructType(
2165 try o.builder.metadataString(name),
2166 try o.getDebugFile(file_scope),
2167 try o.namespaceToDebugScope(owner_decl.src_namespace),
2168 owner_decl.typeSrcLine(zcu) + 1, // Line
2169 .none, // Underlying type
2170 0, // Size
2171 0, // Align
2172 .none, // Fields
2173 );
2174 try o.debug_type_map.put(gpa, ty, debug_opaque_type);
2175 return debug_opaque_type;
2176 },
21772086 .Array => {
21782087 const debug_array_type = try o.builder.debugArrayType(
21792088 .none, // Name
......@@ -2203,9 +2112,9 @@ pub const Object = struct {
22032112 .Int => blk: {
22042113 const info = elem_ty.intInfo(zcu);
22052114 assert(info.bits != 0);
2206 const name = try o.allocTypeName(ty);
2207 defer gpa.free(name);
2208 const builder_name = try o.builder.metadataString(name);
2115 const vec_name = try o.allocTypeName(ty);
2116 defer gpa.free(vec_name);
2117 const builder_name = try o.builder.metadataString(vec_name);
22092118 break :blk switch (info.signedness) {
22102119 .signed => try o.builder.debugSignedType(builder_name, info.bits),
22112120 .unsigned => try o.builder.debugUnsignedType(builder_name, info.bits),
......@@ -2240,6 +2149,7 @@ pub const Object = struct {
22402149 .Optional => {
22412150 const name = try o.allocTypeName(ty);
22422151 defer gpa.free(name);
2152
22432153 const child_ty = ty.optionalChild(zcu);
22442154 if (!child_ty.hasRuntimeBitsIgnoreComptime(pt)) {
22452155 const debug_bool_type = try o.builder.debugBoolType(
......@@ -2399,10 +2309,156 @@ pub const Object = struct {
23992309 try o.debug_type_map.put(gpa, ty, debug_error_set);
24002310 return debug_error_set;
24012311 },
2402 .Struct => {
2403 const name = try o.allocTypeName(ty);
2404 defer gpa.free(name);
2312 .Fn => {
2313 const fn_info = zcu.typeToFunc(ty).?;
2314
2315 var debug_param_types = std.ArrayList(Builder.Metadata).init(gpa);
2316 defer debug_param_types.deinit();
2317
2318 try debug_param_types.ensureUnusedCapacity(3 + fn_info.param_types.len);
2319
2320 // Return type goes first.
2321 if (Type.fromInterned(fn_info.return_type).hasRuntimeBitsIgnoreComptime(pt)) {
2322 const sret = firstParamSRet(fn_info, pt, target);
2323 const ret_ty = if (sret) Type.void else Type.fromInterned(fn_info.return_type);
2324 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ret_ty));
2325
2326 if (sret) {
2327 const ptr_ty = try pt.singleMutPtrType(Type.fromInterned(fn_info.return_type));
2328 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty));
2329 }
2330 } else {
2331 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(Type.void));
2332 }
2333
2334 if (Type.fromInterned(fn_info.return_type).isError(zcu) and
2335 zcu.comp.config.any_error_tracing)
2336 {
2337 const ptr_ty = try pt.singleMutPtrType(try o.getStackTraceType());
2338 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty));
2339 }
2340
2341 for (0..fn_info.param_types.len) |i| {
2342 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[i]);
2343 if (!param_ty.hasRuntimeBitsIgnoreComptime(pt)) continue;
2344
2345 if (isByRef(param_ty, pt)) {
2346 const ptr_ty = try pt.singleMutPtrType(param_ty);
2347 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty));
2348 } else {
2349 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(param_ty));
2350 }
2351 }
2352
2353 const debug_function_type = try o.builder.debugSubroutineType(
2354 try o.builder.debugTuple(debug_param_types.items),
2355 );
2356
2357 try o.debug_type_map.put(gpa, ty, debug_function_type);
2358 return debug_function_type;
2359 },
2360 .ComptimeInt => unreachable,
2361 .ComptimeFloat => unreachable,
2362 .Type => unreachable,
2363 .Undefined => unreachable,
2364 .Null => unreachable,
2365 .EnumLiteral => unreachable,
24052366
2367 .Frame => @panic("TODO implement lowerDebugType for Frame types"),
2368 .AnyFrame => @panic("TODO implement lowerDebugType for AnyFrame types"),
2369 // These are the types that need a correct scope.
2370 .Enum,
2371 .Struct,
2372 .Union,
2373 .Opaque => {}
2374 }
2375
2376
2377 const owner_decl_index = ty.getOwnerDeclOrNull(zcu);
2378 const owner_decl: ?*Zcu.Decl =
2379 if (owner_decl_index) |owner| zcu.declPtr(owner) else null;
2380
2381 const file = if (owner_decl) |owner|
2382 try o.getDebugFile(zcu.namespacePtr(owner.src_namespace).fileScope(zcu)) else .none;
2383 const scope = if (owner_decl) |owner|
2384 try o.namespaceToDebugScope(owner.src_namespace) else o.debug_compile_unit;
2385 const line = if (owner_decl) |owner| owner.typeSrcLine(zcu) + 1 else 0;
2386
2387
2388 const name = if (owner_decl) |owner| owner.name.toSlice(ip) else try o.allocTypeName(ty);
2389 defer if (owner_decl == null) gpa.free(name);
2390
2391 switch (ty.zigTypeTag(zcu)) {
2392 .Enum => {
2393 if (!ty.hasRuntimeBitsIgnoreComptime(pt)) {
2394 const debug_enum_type = try o.makeEmptyNamespaceDebugType(owner_decl_index.?);
2395 try o.debug_type_map.put(gpa, ty, debug_enum_type);
2396 return debug_enum_type;
2397 }
2398
2399 const enum_type = ip.loadEnumType(ty.toIntern());
2400
2401 const enumerators = try gpa.alloc(Builder.Metadata, enum_type.names.len);
2402 defer gpa.free(enumerators);
2403
2404 const int_ty = Type.fromInterned(enum_type.tag_ty);
2405 const int_info = ty.intInfo(zcu);
2406 assert(int_info.bits != 0);
2407
2408 for (enum_type.names.get(ip), 0..) |field_name_ip, i| {
2409 var bigint_space: Value.BigIntSpace = undefined;
2410 const bigint = if (enum_type.values.len != 0)
2411 Value.fromInterned(enum_type.values.get(ip)[i]).toBigInt(&bigint_space, pt)
2412 else
2413 std.math.big.int.Mutable.init(&bigint_space.limbs, i).toConst();
2414
2415 enumerators[i] = try o.builder.debugEnumerator(
2416 try o.builder.metadataString(field_name_ip.toSlice(ip)),
2417 int_info.signedness == .unsigned,
2418 int_info.bits,
2419 bigint,
2420 );
2421 }
2422
2423 const debug_enum_type = try o.builder.debugEnumerationType(
2424 try o.builder.metadataString(name),
2425 file,
2426 scope,
2427 line,
2428 try o.lowerDebugType(int_ty),
2429 ty.abiSize(pt) * 8,
2430 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
2431 try o.builder.debugTuple(enumerators),
2432 );
2433
2434 try o.debug_type_map.put(gpa, ty, debug_enum_type);
2435 try o.debug_enums.append(gpa, debug_enum_type);
2436 return debug_enum_type;
2437 },
2438 .Opaque => {
2439 if (ty.toIntern() == .anyopaque_type) {
2440 const debug_opaque_type = try o.builder.debugSignedType(
2441 try o.builder.metadataString("anyopaque"),
2442 0,
2443 );
2444 try o.debug_type_map.put(gpa, ty, debug_opaque_type);
2445 return debug_opaque_type;
2446 }
2447
2448 const debug_opaque_type = try o.builder.debugStructType(
2449 try o.builder.metadataString(name),
2450 file,
2451 scope,
2452 line,
2453 .none, // Underlying type
2454 0, // Size
2455 0, // Align
2456 .none, // Fields
2457 );
2458 try o.debug_type_map.put(gpa, ty, debug_opaque_type);
2459 return debug_opaque_type;
2460 },
2461 .Struct => {
24062462 if (zcu.typeToPackedStruct(ty)) |struct_type| {
24072463 const backing_int_ty = struct_type.backingIntTypeUnordered(ip);
24082464 if (backing_int_ty != .none) {
......@@ -2457,8 +2513,8 @@ pub const Object = struct {
24572513
24582514 const debug_struct_type = try o.builder.debugStructType(
24592515 try o.builder.metadataString(name),
2460 .none, // File
2461 o.debug_compile_unit, // Scope
2516 file,
2517 scope,
24622518 0, // Line
24632519 .none, // Underlying type
24642520 ty.abiSize(pt) * 8,
......@@ -2480,8 +2536,7 @@ pub const Object = struct {
24802536 // into. Therefore we can satisfy this by making an empty namespace,
24812537 // rather than changing the frontend to unnecessarily resolve the
24822538 // struct field types.
2483 const owner_decl_index = ty.getOwnerDecl(zcu);
2484 const debug_struct_type = try o.makeEmptyNamespaceDebugType(owner_decl_index);
2539 const debug_struct_type = try o.makeEmptyNamespaceDebugType(owner_decl_index.?);
24852540 try o.debug_type_map.put(gpa, ty, debug_struct_type);
24862541 return debug_struct_type;
24872542 }
......@@ -2490,8 +2545,7 @@ pub const Object = struct {
24902545 }
24912546
24922547 if (!ty.hasRuntimeBitsIgnoreComptime(pt)) {
2493 const owner_decl_index = ty.getOwnerDecl(zcu);
2494 const debug_struct_type = try o.makeEmptyNamespaceDebugType(owner_decl_index);
2548 const debug_struct_type = try o.makeEmptyNamespaceDebugType(owner_decl_index.?);
24952549 try o.debug_type_map.put(gpa, ty, debug_struct_type);
24962550 return debug_struct_type;
24972551 }
......@@ -2526,7 +2580,7 @@ pub const Object = struct {
25262580
25272581 fields.appendAssumeCapacity(try o.builder.debugMemberType(
25282582 try o.builder.metadataString(field_name.toSlice(ip)),
2529 .none, // File
2583 file,
25302584 debug_fwd_ref,
25312585 0, // Line
25322586 try o.lowerDebugType(field_ty),
......@@ -2538,9 +2592,9 @@ pub const Object = struct {
25382592
25392593 const debug_struct_type = try o.builder.debugStructType(
25402594 try o.builder.metadataString(name),
2541 .none, // File
2542 o.debug_compile_unit, // Scope
2543 0, // Line
2595 file,
2596 scope,
2597 line,
25442598 .none, // Underlying type
25452599 ty.abiSize(pt) * 8,
25462600 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
......@@ -2556,17 +2610,12 @@ pub const Object = struct {
25562610 return debug_struct_type;
25572611 },
25582612 .Union => {
2559 const owner_decl_index = ty.getOwnerDecl(zcu);
2560
2561 const name = try o.allocTypeName(ty);
2562 defer gpa.free(name);
2563
25642613 const union_type = ip.loadUnionType(ty.toIntern());
25652614 if (!union_type.haveFieldTypes(ip) or
25662615 !ty.hasRuntimeBitsIgnoreComptime(pt) or
25672616 !union_type.haveLayout(ip))
25682617 {
2569 const debug_union_type = try o.makeEmptyNamespaceDebugType(owner_decl_index);
2618 const debug_union_type = try o.makeEmptyNamespaceDebugType(owner_decl_index.?);
25702619 try o.debug_type_map.put(gpa, ty, debug_union_type);
25712620 return debug_union_type;
25722621 }
......@@ -2581,8 +2630,8 @@ pub const Object = struct {
25812630 if (layout.payload_size == 0) {
25822631 const debug_union_type = try o.builder.debugStructType(
25832632 try o.builder.metadataString(name),
2584 .none, // File
2585 o.debug_compile_unit, // Scope
2633 file,
2634 scope,
25862635 0, // Line
25872636 .none, // Underlying type
25882637 ty.abiSize(pt) * 8,
......@@ -2624,7 +2673,7 @@ pub const Object = struct {
26242673 const field_name = tag_type.names.get(ip)[field_index];
26252674 fields.appendAssumeCapacity(try o.builder.debugMemberType(
26262675 try o.builder.metadataString(field_name.toSlice(ip)),
2627 .none, // File
2676 file,
26282677 debug_union_fwd_ref,
26292678 0, // Line
26302679 try o.lowerDebugType(Type.fromInterned(field_ty)),
......@@ -2643,9 +2692,9 @@ pub const Object = struct {
26432692
26442693 const debug_union_type = try o.builder.debugUnionType(
26452694 try o.builder.metadataString(union_name),
2646 .none, // File
2647 o.debug_compile_unit, // Scope
2648 0, // Line
2695 file,
2696 scope,
2697 line,
26492698 .none, // Underlying type
26502699 ty.abiSize(pt) * 8,
26512700 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
......@@ -2674,7 +2723,7 @@ pub const Object = struct {
26742723
26752724 const debug_tag_type = try o.builder.debugMemberType(
26762725 try o.builder.metadataString("tag"),
2677 .none, // File
2726 file, // File
26782727 debug_fwd_ref,
26792728 0, // Line
26802729 try o.lowerDebugType(Type.fromInterned(union_type.enum_tag_ty)),
......@@ -2685,7 +2734,7 @@ pub const Object = struct {
26852734
26862735 const debug_payload_type = try o.builder.debugMemberType(
26872736 try o.builder.metadataString("payload"),
2688 .none, // File
2737 file,
26892738 debug_fwd_ref,
26902739 0, // Line
26912740 debug_union_type,
......@@ -2702,9 +2751,9 @@ pub const Object = struct {
27022751
27032752 const debug_tagged_union_type = try o.builder.debugStructType(
27042753 try o.builder.metadataString(name),
2705 .none, // File
2706 o.debug_compile_unit, // Scope
2707 0, // Line
2754 file, // File
2755 scope,
2756 line,
27082757 .none, // Underlying type
27092758 ty.abiSize(pt) * 8,
27102759 (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8,
......@@ -2719,63 +2768,20 @@ pub const Object = struct {
27192768
27202769 return debug_tagged_union_type;
27212770 },
2722 .Fn => {
2723 const fn_info = zcu.typeToFunc(ty).?;
2724
2725 var debug_param_types = std.ArrayList(Builder.Metadata).init(gpa);
2726 defer debug_param_types.deinit();
2727
2728 try debug_param_types.ensureUnusedCapacity(3 + fn_info.param_types.len);
2729
2730 // Return type goes first.
2731 if (Type.fromInterned(fn_info.return_type).hasRuntimeBitsIgnoreComptime(pt)) {
2732 const sret = firstParamSRet(fn_info, pt, target);
2733 const ret_ty = if (sret) Type.void else Type.fromInterned(fn_info.return_type);
2734 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ret_ty));
2735
2736 if (sret) {
2737 const ptr_ty = try pt.singleMutPtrType(Type.fromInterned(fn_info.return_type));
2738 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty));
2739 }
2740 } else {
2741 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(Type.void));
2742 }
2743
2744 if (Type.fromInterned(fn_info.return_type).isError(zcu) and
2745 zcu.comp.config.any_error_tracing)
2746 {
2747 const ptr_ty = try pt.singleMutPtrType(try o.getStackTraceType());
2748 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty));
2749 }
2750
2751 for (0..fn_info.param_types.len) |i| {
2752 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[i]);
2753 if (!param_ty.hasRuntimeBitsIgnoreComptime(pt)) continue;
2754
2755 if (isByRef(param_ty, pt)) {
2756 const ptr_ty = try pt.singleMutPtrType(param_ty);
2757 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty));
2758 } else {
2759 debug_param_types.appendAssumeCapacity(try o.lowerDebugType(param_ty));
2760 }
2761 }
2771 else => unreachable, // Handled above.
2772 }
2773 }
27622774
2763 const debug_function_type = try o.builder.debugSubroutineType(
2764 try o.builder.debugTuple(debug_param_types.items),
2765 );
2775 fn genNamespaces(o: *Object) !void {
2776 var i: usize = 0;
2777 while (i < o.debug_unresolved_namespace_scopes.count()) : (i += 1) {
2778 const namespace_index = o.debug_unresolved_namespace_scopes.keys()[i];
2779 const fwd_ref = o.debug_unresolved_namespace_scopes.values()[i];
27662780
2767 try o.debug_type_map.put(gpa, ty, debug_function_type);
2768 return debug_function_type;
2769 },
2770 .ComptimeInt => unreachable,
2771 .ComptimeFloat => unreachable,
2772 .Type => unreachable,
2773 .Undefined => unreachable,
2774 .Null => unreachable,
2775 .EnumLiteral => unreachable,
2781 const namespace = o.pt.zcu.namespacePtr(namespace_index);
2782 const debug_type = try o.lowerDebugType(namespace.getType(o.pt.zcu));
27762783
2777 .Frame => @panic("TODO implement lowerDebugType for Frame types"),
2778 .AnyFrame => @panic("TODO implement lowerDebugType for AnyFrame types"),
2784 o.builder.debugForwardReferenceSetType(fwd_ref, debug_type);
27792785 }
27802786 }
27812787
......@@ -4718,17 +4724,37 @@ pub const DeclGen = struct {
47184724 if (!owner_mod.strip) {
47194725 const debug_file = try o.getDebugFile(file_scope);
47204726
4721 const debug_global_var = try o.builder.debugGlobalVar(
4722 try o.builder.metadataString(decl.name.toSlice(ip)), // Name
4723 try o.builder.metadataStringFromStrtabString(variable_index.name(&o.builder)), // Linkage name
4724 debug_file, // File
4725 debug_file, // Scope
4727 const linkage_name = try o.builder.metadataStringFromStrtabString(variable_index.name(&o.builder));
4728
4729 const debug_global_var = if (!decl.isExtern(zcu)) blk: {
4730 // Imitate a C++ static member variable since neither
4731 // GDB or LLDB can really cope with regular variables
4732 // directly inside a struct type.
4733 const ty = try o.lowerDebugType(decl.typeOf(zcu));
4734 const name = try o.builder.metadataString(decl.name.toSlice(ip));
4735
4736 break :blk try o.builder.debugGlobalVar(
4737 name,
4738 linkage_name,
4739 debug_file,
4740 debug_file,
4741 line_number,
4742 ty,
4743 variable_index,
4744 .none,
4745 .internal,
4746 );
4747 } else try o.builder.debugGlobalVar(
4748 linkage_name,
4749 linkage_name,
4750 debug_file,
4751 debug_file,
47264752 line_number,
47274753 try o.lowerDebugType(decl.typeOf(zcu)),
47284754 variable_index,
4729 .{ .local = !decl.isExtern(zcu) },
4755 .none,
4756 .external,
47304757 );
4731
47324758 const debug_expression = try o.builder.debugExpression(&.{});
47334759
47344760 const debug_global_var_expression = try o.builder.debugGlobalVarExpression(
......@@ -5178,6 +5204,7 @@ pub const FuncGen = struct {
51785204
51795205 self.scope = try o.builder.debugSubprogram(
51805206 self.file,
5207 self.file, // TODO Get the correct scope into here—self.scope is the function's *inner* scope.
51815208 try o.builder.metadataString(decl.name.toSlice(&zcu.intern_pool)),
51825209 try o.builder.metadataString(decl.fqn.toSlice(&zcu.intern_pool)),
51835210 line_number,
src/codegen/llvm/Builder.zig+83-29
......@@ -7651,6 +7651,7 @@ pub const Metadata = enum(u32) {
76517651 composite_vector_type,
76527652 derived_pointer_type,
76537653 derived_member_type,
7654 derived_static_member_type,
76547655 subroutine_type,
76557656 enumerator_unsigned,
76567657 enumerator_signed_positive,
......@@ -7663,6 +7664,7 @@ pub const Metadata = enum(u32) {
76637664 parameter,
76647665 global_var,
76657666 @"global_var local",
7667 @"global_var decl",
76667668 global_var_expression,
76677669 constant,
76687670
......@@ -7696,6 +7698,7 @@ pub const Metadata = enum(u32) {
76967698 .composite_vector_type,
76977699 .derived_pointer_type,
76987700 .derived_member_type,
7701 .derived_static_member_type,
76997702 .subroutine_type,
77007703 .enumerator_unsigned,
77017704 .enumerator_signed_positive,
......@@ -7707,6 +7710,7 @@ pub const Metadata = enum(u32) {
77077710 .parameter,
77087711 .global_var,
77097712 .@"global_var local",
7713 .@"global_var decl",
77107714 .global_var_expression,
77117715 => false,
77127716 };
......@@ -7860,6 +7864,7 @@ pub const Metadata = enum(u32) {
78607864 }
78617865 };
78627866
7867 scope: Metadata,
78637868 file: Metadata,
78647869 name: MetadataString,
78657870 linkage_name: MetadataString,
......@@ -7990,8 +7995,10 @@ pub const Metadata = enum(u32) {
79907995 };
79917996
79927997 pub const GlobalVar = struct {
7993 pub const Options = struct {
7994 local: bool,
7998 pub const Options = enum {
7999 internal,
8000 internal_decl,
8001 external,
79958002 };
79968003
79978004 name: MetadataString,
......@@ -8001,6 +8008,7 @@ pub const Metadata = enum(u32) {
80018008 line: u32,
80028009 ty: Metadata,
80038010 variable: Variable.Index,
8011 declaration: Metadata,
80048012 };
80058013
80068014 pub const GlobalVarExpression = struct {
......@@ -9985,7 +9993,7 @@ pub fn printUnbuffered(
99859993 try metadata_formatter.specialized(.@"distinct !", .DISubprogram, .{
99869994 .name = extra.name,
99879995 .linkageName = extra.linkage_name,
9988 .scope = extra.file,
9996 .scope = extra.scope,
99899997 .file = extra.file,
99909998 .line = extra.line,
99919999 .type = extra.ty,
......@@ -10079,8 +10087,8 @@ pub fn printUnbuffered(
1007910087 else => extra.name,
1008010088 },
1008110089 .scope = extra.scope,
10082 .file = null,
10083 .line = null,
10090 .file = extra.file,
10091 .line = extra.line,
1008410092 .baseType = extra.underlying_type,
1008510093 .size = extra.bitSize(),
1008610094 .@"align" = extra.bitAlign(),
......@@ -10101,6 +10109,7 @@ pub fn printUnbuffered(
1010110109 },
1010210110 .derived_pointer_type,
1010310111 .derived_member_type,
10112 .derived_static_member_type,
1010410113 => |kind| {
1010510114 const extra = self.metadataExtraData(Metadata.DerivedType, metadata_item.data);
1010610115 try metadata_formatter.specialized(.@"!", .DIDerivedType, .{
......@@ -10109,7 +10118,8 @@ pub fn printUnbuffered(
1010910118 DW_TAG_member,
1011010119 }, switch (kind) {
1011110120 .derived_pointer_type => .DW_TAG_pointer_type,
10112 .derived_member_type => .DW_TAG_member,
10121 .derived_member_type,
10122 .derived_static_member_type => .DW_TAG_member,
1011310123 else => unreachable,
1011410124 }),
1011510125 .name = switch (extra.name) {
......@@ -10126,7 +10136,7 @@ pub fn printUnbuffered(
1012610136 0 => null,
1012710137 else => |bit_offset| bit_offset,
1012810138 },
10129 .flags = null,
10139 .flags = null, // TODO staticness
1013010140 .extraData = null,
1013110141 .dwarfAddressSpace = null,
1013210142 .annotations = null,
......@@ -10246,6 +10256,7 @@ pub fn printUnbuffered(
1024610256 },
1024710257 .global_var,
1024810258 .@"global_var local",
10259 .@"global_var decl",
1024910260 => |kind| {
1025010261 const extra = self.metadataExtraData(Metadata.GlobalVar, metadata_item.data);
1025110262 try metadata_formatter.specialized(.@"distinct !", .DIGlobalVariable, .{
......@@ -10255,12 +10266,8 @@ pub fn printUnbuffered(
1025510266 .file = extra.file,
1025610267 .line = extra.line,
1025710268 .type = extra.ty,
10258 .isLocal = switch (kind) {
10259 .global_var => false,
10260 .@"global_var local" => true,
10261 else => unreachable,
10262 },
10263 .isDefinition = true,
10269 .isLocal = kind != .global_var,
10270 .isDefinition = kind != .@"global_var decl",
1026410271 .declaration = null,
1026510272 .templateParams = null,
1026610273 .@"align" = null,
......@@ -11749,6 +11756,7 @@ pub fn debugCompileUnit(
1174911756pub fn debugSubprogram(
1175011757 self: *Builder,
1175111758 file: Metadata,
11759 scope: Metadata,
1175211760 name: MetadataString,
1175311761 linkage_name: MetadataString,
1175411762 line: u32,
......@@ -11760,6 +11768,7 @@ pub fn debugSubprogram(
1176011768 try self.ensureUnusedMetadataCapacity(1, Metadata.Subprogram, 0);
1176111769 return self.debugSubprogramAssumeCapacity(
1176211770 file,
11771 scope,
1176311772 name,
1176411773 linkage_name,
1176511774 line,
......@@ -11949,6 +11958,28 @@ pub fn debugPointerType(
1194911958 );
1195011959}
1195111960
11961pub fn debugStaticMemberType(
11962 self: *Builder,
11963 name: MetadataString,
11964 file: Metadata,
11965 scope: Metadata,
11966 line: u32,
11967 underlying_type: Metadata,
11968) Allocator.Error!Metadata {
11969 try self.ensureUnusedMetadataCapacity(1, Metadata.DerivedType, 0);
11970 return self.debugMemberTypeAssumeCapacity(
11971 name,
11972 file,
11973 scope,
11974 line,
11975 underlying_type,
11976 0,
11977 0,
11978 0,
11979 true,
11980 );
11981}
11982
1195211983pub fn debugMemberType(
1195311984 self: *Builder,
1195411985 name: MetadataString,
......@@ -11970,6 +12001,7 @@ pub fn debugMemberType(
1197012001 size_in_bits,
1197112002 align_in_bits,
1197212003 offset_in_bits,
12004 false,
1197312005 );
1197412006}
1197512007
......@@ -12063,6 +12095,7 @@ pub fn debugGlobalVar(
1206312095 line: u32,
1206412096 ty: Metadata,
1206512097 variable: Variable.Index,
12098 declaration: Metadata,
1206612099 options: Metadata.GlobalVar.Options,
1206712100) Allocator.Error!Metadata {
1206812101 try self.ensureUnusedMetadataCapacity(1, Metadata.GlobalVar, 0);
......@@ -12074,6 +12107,7 @@ pub fn debugGlobalVar(
1207412107 line,
1207512108 ty,
1207612109 variable,
12110 declaration,
1207712111 options,
1207812112 );
1207912113}
......@@ -12224,6 +12258,7 @@ pub fn debugCompileUnitAssumeCapacity(
1222412258fn debugSubprogramAssumeCapacity(
1222512259 self: *Builder,
1222612260 file: Metadata,
12261 scope: Metadata,
1222712262 name: MetadataString,
1222812263 linkage_name: MetadataString,
1222912264 line: u32,
......@@ -12237,6 +12272,7 @@ fn debugSubprogramAssumeCapacity(
1223712272 @as(u3, @truncate(@as(u32, @bitCast(options.sp_flags)) >> 2)));
1223812273 return self.metadataDistinctAssumeCapacity(tag, Metadata.Subprogram{
1223912274 .file = file,
12275 .scope = scope,
1224012276 .name = name,
1224112277 .linkage_name = linkage_name,
1224212278 .line = line,
......@@ -12499,21 +12535,25 @@ fn debugMemberTypeAssumeCapacity(
1249912535 size_in_bits: u64,
1250012536 align_in_bits: u64,
1250112537 offset_in_bits: u64,
12538 static: bool,
1250212539) Metadata {
1250312540 assert(!self.strip);
12504 return self.metadataSimpleAssumeCapacity(.derived_member_type, Metadata.DerivedType{
12505 .name = name,
12506 .file = file,
12507 .scope = scope,
12508 .line = line,
12509 .underlying_type = underlying_type,
12510 .size_in_bits_lo = @truncate(size_in_bits),
12511 .size_in_bits_hi = @truncate(size_in_bits >> 32),
12512 .align_in_bits_lo = @truncate(align_in_bits),
12513 .align_in_bits_hi = @truncate(align_in_bits >> 32),
12514 .offset_in_bits_lo = @truncate(offset_in_bits),
12515 .offset_in_bits_hi = @truncate(offset_in_bits >> 32),
12516 });
12541 return self.metadataSimpleAssumeCapacity(
12542 if (static) .derived_static_member_type else .derived_member_type,
12543 Metadata.DerivedType{
12544 .name = name,
12545 .file = file,
12546 .scope = scope,
12547 .line = line,
12548 .underlying_type = underlying_type,
12549 .size_in_bits_lo = @truncate(size_in_bits),
12550 .size_in_bits_hi = @truncate(size_in_bits >> 32),
12551 .align_in_bits_lo = @truncate(align_in_bits),
12552 .align_in_bits_hi = @truncate(align_in_bits >> 32),
12553 .offset_in_bits_lo = @truncate(offset_in_bits),
12554 .offset_in_bits_hi = @truncate(offset_in_bits >> 32),
12555 }
12556 );
1251712557}
1251812558
1251912559fn debugSubroutineTypeAssumeCapacity(
......@@ -12769,11 +12809,16 @@ fn debugGlobalVarAssumeCapacity(
1276912809 line: u32,
1277012810 ty: Metadata,
1277112811 variable: Variable.Index,
12812 declaration: Metadata,
1277212813 options: Metadata.GlobalVar.Options,
1277312814) Metadata {
1277412815 assert(!self.strip);
1277512816 return self.metadataDistinctAssumeCapacity(
12776 if (options.local) .@"global_var local" else .global_var,
12817 switch (options) {
12818 .internal => .@"global_var local",
12819 .internal_decl => .@"global_var decl",
12820 .external => .global_var,
12821 },
1277712822 Metadata.GlobalVar{
1277812823 .name = name,
1277912824 .linkage_name = linkage_name,
......@@ -12782,6 +12827,7 @@ fn debugGlobalVarAssumeCapacity(
1278212827 .line = line,
1278312828 .ty = ty,
1278412829 .variable = variable,
12830 .declaration = declaration,
1278512831 },
1278612832 );
1278712833}
......@@ -13818,7 +13864,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1381813864 const extra = self.metadataExtraData(Metadata.Subprogram, data);
1381913865
1382013866 try metadata_block.writeAbbrevAdapted(MetadataBlock.Subprogram{
13821 .scope = extra.file,
13867 .scope = extra.scope,
1382213868 .name = extra.name,
1382313869 .linkage_name = extra.linkage_name,
1382413870 .file = extra.file,
......@@ -13898,12 +13944,14 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1389813944 },
1389913945 .derived_pointer_type,
1390013946 .derived_member_type,
13947 .derived_static_member_type,
1390113948 => |kind| {
1390213949 const extra = self.metadataExtraData(Metadata.DerivedType, data);
1390313950 try metadata_block.writeAbbrevAdapted(MetadataBlock.DerivedType{
1390413951 .tag = switch (kind) {
1390513952 .derived_pointer_type => DW.TAG.pointer_type,
13906 .derived_member_type => DW.TAG.member,
13953 .derived_member_type,
13954 .derived_static_member_type => DW.TAG.member,
1390713955 else => unreachable,
1390813956 },
1390913957 .name = extra.name,
......@@ -13914,6 +13962,9 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1391413962 .size_in_bits = extra.bitSize(),
1391513963 .align_in_bits = extra.bitAlign(),
1391613964 .offset_in_bits = extra.bitOffset(),
13965 .flags = .{
13966 .StaticMember = kind == .derived_static_member_type,
13967 },
1391713968 }, metadata_adapter);
1391813969 },
1391913970 .subroutine_type => {
......@@ -14041,6 +14092,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1404114092 },
1404214093 .global_var,
1404314094 .@"global_var local",
14095 .@"global_var decl",
1404414096 => |kind| {
1404514097 const extra = self.metadataExtraData(Metadata.GlobalVar, data);
1404614098 try metadata_block.writeAbbrevAdapted(MetadataBlock.GlobalVar{
......@@ -14051,6 +14103,8 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1405114103 .line = extra.line,
1405214104 .ty = extra.ty,
1405314105 .local = kind == .@"global_var local",
14106 .defined = kind != .@"global_var decl",
14107 .declaration = extra.declaration,
1405414108 }, metadata_adapter);
1405514109 },
1405614110 .global_var_expression => {
src/codegen/llvm/ir.zig+7-4
......@@ -694,7 +694,7 @@ pub const MetadataBlock = struct {
694694 pub const ops = [_]AbbrevOp{
695695 .{ .literal = 20 },
696696 .{ .literal = 1 }, // is distinct
697 .{ .literal = std.dwarf.LANG.C99 }, // source language
697 .{ .literal = std.dwarf.LANG.C_plus_plus_11 }, // source language
698698 MetadataAbbrev, // file
699699 MetadataAbbrev, // producer
700700 .{ .fixed = 1 }, // isOptimized
......@@ -863,7 +863,7 @@ pub const MetadataBlock = struct {
863863 .{ .vbr = 6 }, // size in bits
864864 .{ .vbr = 6 }, // align in bits
865865 .{ .vbr = 6 }, // offset in bits
866 .{ .literal = 0 }, // flags
866 .{ .fixed = 32 }, // flags
867867 .{ .literal = 0 }, // extra data
868868 };
869869
......@@ -876,6 +876,7 @@ pub const MetadataBlock = struct {
876876 size_in_bits: u64,
877877 align_in_bits: u64,
878878 offset_in_bits: u64,
879 flags: Builder.Metadata.DIFlags,
879880 };
880881
881882 pub const SubroutineType = struct {
......@@ -1002,8 +1003,8 @@ pub const MetadataBlock = struct {
10021003 LineAbbrev, // line
10031004 MetadataAbbrev, // type
10041005 .{ .fixed = 1 }, // local
1005 .{ .literal = 1 }, // defined
1006 .{ .literal = 0 }, // static data members declaration
1006 .{ .fixed = 1 }, // defined
1007 MetadataAbbrev, // static data members declaration
10071008 .{ .literal = 0 }, // template params
10081009 .{ .literal = 0 }, // align in bits
10091010 .{ .literal = 0 }, // annotations
......@@ -1016,6 +1017,8 @@ pub const MetadataBlock = struct {
10161017 line: u32,
10171018 ty: Builder.Metadata,
10181019 local: bool,
1020 defined: bool,
1021 declaration: Builder.Metadata,
10191022 };
10201023
10211024 pub const GlobalVarExpression = struct {