') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Implement @src by Vexu · Pull Request #5628 · ziglang/zig · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/std/builtin.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -131,6 +131,15 @@ pub const CallingConvention = enum {
AAPCSVFP,
};

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const SourceLocation = struct {
file: []const u8,
fn_name: []const u8,
line: u32,
column: u32,
};

pub const TypeId = @TagType(TypeInfo);

/// This data structure is used by the Zig language code generation and
Expand Down
6 changes: 6 additions & 0 deletions src/all_types.hpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -1827,6 +1827,7 @@ enum BuiltinFnId {
BuiltinFnIdBitSizeof,
BuiltinFnIdWasmMemorySize,
BuiltinFnIdWasmMemoryGrow,
BuiltinFnIdSrc,
};

struct BuiltinFnEntry {
Expand DownExpand Up@@ -2754,6 +2755,7 @@ enum IrInstSrcId {
IrInstSrcIdSpillEnd,
IrInstSrcIdWasmMemorySize,
IrInstSrcIdWasmMemoryGrow,
IrInstSrcIdSrc,
};

// ir_render_* functions in codegen.cpp consume Gen instructions and produce LLVM IR.
Expand DownExpand Up@@ -3761,6 +3763,10 @@ struct IrInstGenWasmMemoryGrow {
IrInstGen *delta;
};

struct IrInstSrcSrc {
IrInstSrc base;
};

struct IrInstSrcSlice {
IrInstSrc base;

Expand Down
1 change: 1 addition & 0 deletions src/codegen.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8714,6 +8714,7 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdBitSizeof, "bitSizeOf", 1);
create_builtin_fn(g, BuiltinFnIdWasmMemorySize, "wasmMemorySize", 1);
create_builtin_fn(g, BuiltinFnIdWasmMemoryGrow, "wasmMemoryGrow", 2);
create_builtin_fn(g, BuiltinFnIdSrc, "src", 0);
}

static const char *bool_to_str(bool b) {
Expand Down
76 changes: 76 additions & 0 deletions src/ir.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -561,6 +561,8 @@ static void destroy_instruction_src(IrInstSrc *inst) {
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemorySize *>(inst));
case IrInstSrcIdWasmMemoryGrow:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemoryGrow *>(inst));
case IrInstSrcIdSrc:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSrc *>(inst));
}
zig_unreachable();
}
Expand DownExpand Up@@ -1627,6 +1629,9 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcWasmMemoryGrow *) {
return IrInstSrcIdWasmMemoryGrow;
}

static constexpr IrInstSrcId ir_inst_id(IrInstSrcSrc *) {
return IrInstSrcIdSrc;
}

static constexpr IrInstGenId ir_inst_id(IrInstGenDeclVar *) {
return IrInstGenIdDeclVar;
Expand DownExpand Up@@ -5030,6 +5035,11 @@ static IrInstGen *ir_build_wasm_memory_grow_gen(IrAnalyze *ira, IrInst *source_i
return &instruction->base;
}

static IrInstSrc *ir_build_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) {
IrInstSrcSrc *instruction = ir_build_instruction<IrInstSrcSrc>(irb, scope, source_node);

return &instruction->base;
}

static void ir_count_defers(IrBuilderSrc *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
results[ReturnKindUnconditional] = 0;
Expand DownExpand Up@@ -7450,6 +7460,11 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
return ir_gen_union_init_expr(irb, scope, node, union_type_inst, name_inst, init_node,
lval, result_loc);
}
case BuiltinFnIdSrc:
{
IrInstSrc *src_inst = ir_build_src(irb, scope, node);
return ir_lval_wrap(irb, scope, src_inst, lval, result_loc);
}
}
zig_unreachable();
}
Expand DownExpand Up@@ -30859,6 +30874,64 @@ static IrInstGen *ir_analyze_instruction_spill_end(IrAnalyze *ira, IrInstSrcSpil
return ir_build_spill_end_gen(ira, &instruction->base.base, begin, operand->value->type);
}

static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instruction) {
ZigFn *fn_entry = scope_fn_entry(instruction->base.base.scope);
if (fn_entry == nullptr) {
ir_add_error(ira, &instruction->base.base, buf_sprintf("@src outside function"));
return ira->codegen->invalid_inst_gen;
}

ZigType *u8_ptr = get_pointer_to_type_extra(
ira->codegen, ira->codegen->builtin_types.entry_u8,
true, false, PtrLenUnknown,
0, 0, 0, false);
ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);

ZigType *source_location_type = get_builtin_type(ira->codegen, "SourceLocation");
if (type_resolve(ira->codegen, source_location_type, ResolveStatusSizeKnown)) {
zig_unreachable();
}

ZigValue *result = ira->codegen->pass1_arena->create<ZigValue>();
result->special = ConstValSpecialStatic;
result->type = source_location_type;

ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4);
result->data.x_struct.fields = fields;

// file: []const u8
ensure_field_index(source_location_type, "file", 0);
fields[0]->special = ConstValSpecialStatic;
fields[0]->type = u8_slice;

ZigType *import = instruction->base.base.source_node->owner;
Buf *path = import->data.structure.root_struct->path;
ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee;
init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true);

// fn_name: []const u8
ensure_field_index(source_location_type, "fn_name", 1);
fields[1]->special = ConstValSpecialStatic;
fields[1]->type = u8_slice;

ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true);

// line: u32
ensure_field_index(source_location_type, "line", 2);
fields[2]->special = ConstValSpecialStatic;
fields[2]->type = ira->codegen->builtin_types.entry_u32;
bigint_init_unsigned(&fields[2]->data.x_bigint, instruction->base.base.source_node->line + 1);

// column: u32
ensure_field_index(source_location_type, "column", 3);
fields[3]->special = ConstValSpecialStatic;
fields[3]->type = ira->codegen->builtin_types.entry_u32;
bigint_init_unsigned(&fields[3]->data.x_bigint, instruction->base.base.source_node->column + 1);

return ir_const_move(ira, &instruction->base.base, result);
}

static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruction) {
switch (instruction->id) {
case IrInstSrcIdInvalid:
Expand DownExpand Up@@ -31130,6 +31203,8 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
return ir_analyze_instruction_wasm_memory_size(ira, (IrInstSrcWasmMemorySize *)instruction);
case IrInstSrcIdWasmMemoryGrow:
return ir_analyze_instruction_wasm_memory_grow(ira, (IrInstSrcWasmMemoryGrow *)instruction);
case IrInstSrcIdSrc:
return ir_analyze_instruction_src(ira, (IrInstSrcSrc *)instruction);
}
zig_unreachable();
}
Expand DownExpand Up@@ -31525,6 +31600,7 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {
case IrInstSrcIdAlloca:
case IrInstSrcIdSpillEnd:
case IrInstSrcIdWasmMemorySize:
case IrInstSrcIdSrc:
return false;

case IrInstSrcIdAsm:
Expand Down
9 changes: 9 additions & 0 deletions src/ir_print.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,6 +325,8 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {
return "SrcWasmMemorySize";
case IrInstSrcIdWasmMemoryGrow:
return "SrcWasmMemoryGrow";
case IrInstSrcIdSrc:
return "SrcSrc";
}
zig_unreachable();
}
Expand DownExpand Up@@ -1744,6 +1746,10 @@ static void ir_print_wasm_memory_grow(IrPrintGen *irp, IrInstGenWasmMemoryGrow *
fprintf(irp->f, ")");
}

static void ir_print_builtin_src(IrPrintSrc *irp, IrInstSrcSrc *instruction) {
fprintf(irp->f, "@src()");
}

static void ir_print_memset(IrPrintSrc *irp, IrInstSrcMemset *instruction) {
fprintf(irp->f, "@memset(");
ir_print_other_inst_src(irp, instruction->dest_ptr);
Expand DownExpand Up@@ -2994,6 +3000,9 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai
case IrInstSrcIdWasmMemoryGrow:
ir_print_wasm_memory_grow(irp, (IrInstSrcWasmMemoryGrow *)instruction);
break;
case IrInstSrcIdSrc:
ir_print_builtin_src(irp, (IrInstSrcSrc *)instruction);
break;
}
fprintf(irp->f, "\n");
}
Expand Down
8 changes: 8 additions & 0 deletions test/compile_errors.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
const std = @import("std");

pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add("@src outside function",
\\comptime {
\\ @src();
\\}
, &[_][]const u8{
"tmp.zig:2:5: error: @src outside function",
});

cases.add("call assigned to constant",
\\const Foo = struct {
\\ x: i32,
Expand Down
1 change: 1 addition & 0 deletions test/stage1/behavior.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -131,4 +131,5 @@ comptime {
}
_ = @import("behavior/while.zig");
_ = @import("behavior/widening.zig");
_ = @import("behavior/src.zig");
}
15 changes: 15 additions & 0 deletions test/stage1/behavior/src.zig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
const std = @import("std");
const expect = std.testing.expect;

test "@src" {
doTheTest();
}

fn doTheTest() void {
const src = @src();

expect(src.line == 9);
expect(src.column == 17);
expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
expect(std.mem.endsWith(u8, src.file, "src.zig"));
}