diff --git a/core/mem/mem.odin b/core/mem/mem.odin index a56702ae8..fe2be4cf6 100644 --- a/core/mem/mem.odin +++ b/core/mem/mem.odin @@ -2,7 +2,7 @@ package mem import "core:runtime" -set :: proc "contextless" (data: rawptr, value: byte, len: int) -> rawptr { +set :: proc(data: rawptr, value: byte, len: int) -> rawptr { foreign _ { when ODIN_USE_LLVM_API { when size_of(rawptr) == 8 { @@ -26,26 +26,26 @@ set :: proc "contextless" (data: rawptr, value: byte, len: int) -> rawptr { memset(data, value, len); return data; } -zero :: inline proc "contextless" (data: rawptr, len: int) -> rawptr { +zero :: inline proc(data: rawptr, len: int) -> rawptr { return set(data, 0, len); } -zero_item :: inline proc "contextless" (item: $P/^$T) { +zero_item :: inline proc(item: $P/^$T) { set(item, 0, size_of(T)); } -zero_slice :: proc "contextless" (data: $T/[]$E) { +zero_slice :: proc(data: $T/[]$E) { if n := len(data); n > 0 { zero(&data[0], size_of(E)*n); } } -copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr { +copy :: proc(dst, src: rawptr, len: int) -> rawptr { return runtime.mem_copy(dst, src, len); } -copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr { +copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr { return runtime.mem_copy_non_overlapping(dst, src, len); } -compare :: inline proc "contextless" (a, b: []byte) -> int { +compare :: inline proc(a, b: []byte) -> int { // NOTE(tetra): no-abc is okay here because if the slices are empty, `&a[0]` is just nil+0 == nil, which // compare_byte_ptrs handles fine when the passed length is also zero. res := #no_bounds_check compare_byte_ptrs(&a[0], &b[0], min(len(a), len(b))); @@ -57,7 +57,7 @@ compare :: inline proc "contextless" (a, b: []byte) -> int { return res; } -compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int #no_bounds_check { +compare_byte_ptrs :: proc(a, b: ^byte, n: int) -> int #no_bounds_check { x := slice_ptr(a, n); y := slice_ptr(b, n); @@ -91,36 +91,36 @@ compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int #no_bounds_ return 0; } -compare_ptrs :: inline proc "contextless" (a, b: rawptr, n: int) -> int { +compare_ptrs :: inline proc(a, b: rawptr, n: int) -> int { return compare_byte_ptrs((^byte)(a), (^byte)(b), n); } -ptr_offset :: inline proc "contextless" (ptr: $P/^$T, n: int) -> P { +ptr_offset :: inline proc(ptr: $P/^$T, n: int) -> P { new := int(uintptr(ptr)) + size_of(T)*n; return P(uintptr(new)); } -ptr_sub :: inline proc "contextless" (a, b: $P/^$T) -> int { +ptr_sub :: inline proc(a, b: $P/^$T) -> int { return (int(uintptr(a)) - int(uintptr(b)))/size_of(T); } -slice_ptr :: inline proc "contextless" (ptr: ^$T, len: int) -> []T { +slice_ptr :: inline proc(ptr: ^$T, len: int) -> []T { assert(len >= 0); return transmute([]T)Raw_Slice{data = ptr, len = len}; } -slice_ptr_to_bytes :: proc "contextless" (ptr: rawptr, len: int) -> []byte { +slice_ptr_to_bytes :: proc(ptr: rawptr, len: int) -> []byte { assert(len >= 0); return transmute([]byte)Raw_Slice{data = ptr, len = len}; } -slice_to_bytes :: inline proc "contextless" (slice: $E/[]$T) -> []byte { +slice_to_bytes :: inline proc(slice: $E/[]$T) -> []byte { s := transmute(Raw_Slice)slice; s.len *= size_of(T); return transmute([]byte)s; } -slice_data_cast :: inline proc "contextless" ($T: typeid/[]$A, slice: $S/[]$B) -> T { +slice_data_cast :: inline proc($T: typeid/[]$A, slice: $S/[]$B) -> T { when size_of(A) == 0 || size_of(B) == 0 { return nil; } else { @@ -130,7 +130,7 @@ slice_data_cast :: inline proc "contextless" ($T: typeid/[]$A, slice: $S/[]$B) - } } -slice_to_components :: proc "contextless" (slice: $E/[]$T) -> (data: ^T, len: int) { +slice_to_components :: proc(slice: $E/[]$T) -> (data: ^T, len: int) { s := transmute(Raw_Slice)slice; return s.data, s.len; } @@ -145,22 +145,22 @@ buffer_from_slice :: inline proc(backing: $T/[]$E) -> [dynamic]E { }; } -ptr_to_bytes :: inline proc "contextless" (ptr: ^$T, len := 1) -> []byte { +ptr_to_bytes :: inline proc(ptr: ^$T, len := 1) -> []byte { assert(len >= 0); return transmute([]byte)Raw_Slice{ptr, len*size_of(T)}; } -any_to_bytes :: inline proc "contextless" (val: any) -> []byte { +any_to_bytes :: inline proc(val: any) -> []byte { ti := type_info_of(val.id); size := ti != nil ? ti.size : 0; return transmute([]byte)Raw_Slice{val.data, size}; } -kilobytes :: inline proc "contextless" (x: int) -> int do return (x) * 1024; -megabytes :: inline proc "contextless" (x: int) -> int do return kilobytes(x) * 1024; -gigabytes :: inline proc "contextless" (x: int) -> int do return megabytes(x) * 1024; -terabytes :: inline proc "contextless" (x: int) -> int do return gigabytes(x) * 1024; +kilobytes :: inline proc(x: int) -> int do return (x) * 1024; +megabytes :: inline proc(x: int) -> int do return kilobytes(x) * 1024; +gigabytes :: inline proc(x: int) -> int do return megabytes(x) * 1024; +terabytes :: inline proc(x: int) -> int do return gigabytes(x) * 1024; is_power_of_two :: inline proc(x: uintptr) -> bool { if x <= 0 do return false; diff --git a/core/runtime/core.odin b/core/runtime/core.odin index c1ecc4b9c..a40e6d005 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -518,7 +518,7 @@ copy :: proc{copy_slice, copy_from_string}; @builtin -pop :: proc "contextless" (array: ^$T/[dynamic]$E) -> E { +pop :: proc(array: ^$T/[dynamic]$E) -> E { if array == nil do return E{}; assert(len(array) > 0); res := #no_bounds_check array[len(array)-1]; @@ -1270,7 +1270,7 @@ __get_map_key :: proc "contextless" (k: $K) -> Map_Key { return map_key; } -_fnv64a :: proc(data: []byte, seed: u64 = 0xcbf29ce484222325) -> u64 { +_fnv64a :: proc "contextless" (data: []byte, seed: u64 = 0xcbf29ce484222325) -> u64 { h: u64 = seed; for b in data { h = (h ~ u64(b)) * 0x100000001b3; @@ -1279,10 +1279,10 @@ _fnv64a :: proc(data: []byte, seed: u64 = 0xcbf29ce484222325) -> u64 { } -default_hash :: proc(data: []byte) -> u64 { +default_hash :: proc "contextless" (data: []byte) -> u64 { return _fnv64a(data); } -default_hash_string :: proc(s: string) -> u64 do return default_hash(transmute([]byte)(s)); +default_hash_string :: proc "contextless" (s: string) -> u64 do return default_hash(transmute([]byte)(s)); source_code_location_hash :: proc(s: Source_Code_Location) -> u64 { diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index 9788c0633..a89c0580d 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -583,6 +583,7 @@ quaternion256_ne :: inline proc "contextless" (a, b: quaternion256) -> bool { re bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) { if 0 <= index && index < count do return; handle_error :: proc "contextless" (file: string, line, column: int, index, count: int) { + context = default_context(); fd := os.stderr; print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); os.write_string(fd, " Index "); @@ -596,6 +597,7 @@ bounds_check_error :: proc "contextless" (file: string, line, column: int, index } slice_handle_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) { + context = default_context(); fd := os.stderr; print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); os.write_string(fd, " Invalid slice indices: "); @@ -621,6 +623,7 @@ slice_expr_error_lo_hi :: proc "contextless" (file: string, line, column: int, l dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) { if 0 <= low && low <= high && high <= max do return; handle_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) { + context = default_context(); fd := os.stderr; print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); os.write_string(fd, " Invalid dynamic array values: "); @@ -639,6 +642,7 @@ dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: int, from, to: typeid) { if ok do return; handle_error :: proc "contextless" (file: string, line, column: int, from, to: typeid) { + context = default_context(); fd := os.stderr; print_caller_location(fd, Source_Code_Location{file, line, column, "", 0}); os.write_string(fd, " Invalid type assertion from "); @@ -751,6 +755,7 @@ dynamic_array_expr_error_loc :: inline proc "contextless" (using loc := #caller_ make_slice_error_loc :: inline proc "contextless" (loc := #caller_location, len: int) { if 0 <= len do return; handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) { + context = default_context(); fd := os.stderr; print_caller_location(fd, loc); os.write_string(fd, " Invalid slice length for make: "); @@ -764,6 +769,7 @@ make_slice_error_loc :: inline proc "contextless" (loc := #caller_location, len: make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_location, len, cap: int) { if 0 <= len && len <= cap do return; handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) { + context = default_context(); fd := os.stderr; print_caller_location(fd, loc); os.write_string(fd, " Invalid dynamic array parameters for make: "); @@ -779,6 +785,7 @@ make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_ make_map_expr_error_loc :: inline proc "contextless" (loc := #caller_location, cap: int) { if 0 <= cap do return; handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) { + context = default_context(); fd := os.stderr; print_caller_location(fd, loc); os.write_string(fd, " Invalid map capacity for make: "); diff --git a/core/sync/atomic.odin b/core/sync/atomic.odin index 996c14f47..ba324e96d 100644 --- a/core/sync/atomic.odin +++ b/core/sync/atomic.odin @@ -10,7 +10,7 @@ Ordering :: enum { Sequentially_Consistent, } -strongest_failure_ordering :: inline proc "contextless" (order: Ordering) -> Ordering { +strongest_failure_ordering :: inline proc(order: Ordering) -> Ordering { switch order { case .Relaxed: return .Relaxed; case .Release: return .Relaxed; @@ -21,7 +21,7 @@ strongest_failure_ordering :: inline proc "contextless" (order: Ordering) -> Ord return .Relaxed; } -fence :: inline proc "contextless" ($order: Ordering) { +fence :: inline proc($order: Ordering) { switch order { case .Relaxed: panic("there is no such thing as a relaxed fence"); case .Release: intrinsics.atomic_fence_rel(); @@ -33,7 +33,7 @@ fence :: inline proc "contextless" ($order: Ordering) { } -atomic_store :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) { +atomic_store :: inline proc(dst: ^$T, val: T, $order: Ordering) { switch order { case .Relaxed: intrinsics.atomic_store_relaxed(dst, val); case .Release: intrinsics.atomic_store_rel(dst, val); @@ -44,7 +44,7 @@ atomic_store :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) { } } -atomic_load :: inline proc "contextless" (dst: ^$T, $order: Ordering) -> T { +atomic_load :: inline proc(dst: ^$T, $order: Ordering) -> T { switch order { case .Relaxed: return intrinsics.atomic_load_relaxed(dst); case .Acquire: return intrinsics.atomic_load_acq(dst); @@ -56,7 +56,7 @@ atomic_load :: inline proc "contextless" (dst: ^$T, $order: Ordering) -> T { return T{}; } -atomic_swap :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> T { +atomic_swap :: inline proc(dst: ^$T, val: T, $order: Ordering) -> T { switch order { case .Relaxed: return intrinsics.atomic_xchg_relaxed(dst, val); case .Release: return intrinsics.atomic_xchg_rel(dst, val); @@ -68,7 +68,7 @@ atomic_swap :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> return T{}; } -atomic_compare_exchange :: inline proc "contextless" (dst: ^$T, old, new: T, $success, $failure: Ordering) -> (val: T, ok: bool) { +atomic_compare_exchange :: inline proc(dst: ^$T, old, new: T, $success, $failure: Ordering) -> (val: T, ok: bool) { switch failure { case .Relaxed: switch success { @@ -102,7 +102,7 @@ atomic_compare_exchange :: inline proc "contextless" (dst: ^$T, old, new: T, $su } -atomic_compare_exchange_weak :: inline proc "contextless" (dst: ^$T, old, new: T, $success, $failure: Ordering) -> (val: T, ok: bool) { +atomic_compare_exchange_weak :: inline proc(dst: ^$T, old, new: T, $success, $failure: Ordering) -> (val: T, ok: bool) { switch failure { case .Relaxed: switch success { @@ -137,7 +137,7 @@ atomic_compare_exchange_weak :: inline proc "contextless" (dst: ^$T, old, new: T } -atomic_add :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> T { +atomic_add :: inline proc(dst: ^$T, val: T, $order: Ordering) -> T { switch order { case .Relaxed: return intrinsics.atomic_add_relaxed(dst, val); case .Release: return intrinsics.atomic_add_rel(dst, val); @@ -149,7 +149,7 @@ atomic_add :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> return T{}; } -atomic_sub :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> T { +atomic_sub :: inline proc(dst: ^$T, val: T, $order: Ordering) -> T { switch order { case .Relaxed: return intrinsics.atomic_sub_relaxed(dst, val); case .Release: return intrinsics.atomic_sub_rel(dst, val); @@ -161,7 +161,7 @@ atomic_sub :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> return T{}; } -atomic_and :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> T { +atomic_and :: inline proc(dst: ^$T, val: T, $order: Ordering) -> T { switch order { case .Relaxed: return intrinsics.atomic_and_relaxed(dst, val); case .Release: return intrinsics.atomic_and_rel(dst, val); @@ -173,7 +173,7 @@ atomic_and :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> return T{}; } -atomic_nand :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> T { +atomic_nand :: inline proc(dst: ^$T, val: T, $order: Ordering) -> T { switch order { case .Relaxed: return intrinsics.atomic_nand_relaxed(dst, val); case .Release: return intrinsics.atomic_nand_rel(dst, val); @@ -185,7 +185,7 @@ atomic_nand :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> return T{}; } -atomic_or :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> T { +atomic_or :: inline proc(dst: ^$T, val: T, $order: Ordering) -> T { switch order { case .Relaxed: return intrinsics.atomic_or_relaxed(dst, val); case .Release: return intrinsics.atomic_or_rel(dst, val); @@ -197,7 +197,7 @@ atomic_or :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> T return T{}; } -atomic_xor :: inline proc "contextless" (dst: ^$T, val: T, $order: Ordering) -> T { +atomic_xor :: inline proc(dst: ^$T, val: T, $order: Ordering) -> T { switch order { case .Relaxed: return intrinsics.atomic_xor_relaxed(dst, val); case .Release: return intrinsics.atomic_xor_rel(dst, val); diff --git a/core/thread/thread_windows.odin b/core/thread/thread_windows.odin index a558e1b0a..430997441 100644 --- a/core/thread/thread_windows.odin +++ b/core/thread/thread_windows.odin @@ -28,7 +28,7 @@ create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^T win32_thread_id: u32; __windows_thread_entry_proc :: proc "c" (t: ^Thread) -> i32 { - c := context; + c := runtime.default_context(); if t.use_init_context { c = t.init_context; } diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin index 4ef9b2a8c..4f425b698 100644 --- a/examples/demo/demo.odin +++ b/examples/demo/demo.odin @@ -6,6 +6,7 @@ import "core:os" import "core:thread" import "core:time" import "core:reflect" +import "core:runtime" import "intrinsics" @@ -1931,6 +1932,16 @@ union_maybe :: proc() { fmt.println(z, z_ok); } +dummy_procedure :: proc() { + fmt.println("dummy_procedure"); +} + +explicit_context_definition :: proc "c" () { + // Try commenting the following statement out below + context = runtime.default_context(); + dummy_procedure(); +} + main :: proc() { when true { the_basics(); @@ -1961,5 +1972,6 @@ main :: proc() { soa_struct_layout(); constant_literal_expressions(); union_maybe(); + explicit_context_definition(); } } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 4d3268f44..e6812b81d 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7411,6 +7411,12 @@ ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call, Type *t Type *pt = base_type(proc_type); + if (pt->kind == Type_Proc && pt->Proc.calling_convention == ProcCC_Odin) { + if ((c->scope->flags & ScopeFlag_ContextDefined) == 0) { + error(call, "'context' has not been defined within this scope, but is required for this procedure call"); + } + } + #if 0 if (pt->kind == Type_Proc && pt->Proc.calling_convention == ProcCC_Odin) { init_core_context(c->checker); diff --git a/src/check_type.cpp b/src/check_type.cpp index 8d8bae1a7..8aa24a752 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2512,6 +2512,8 @@ bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node, GB_ASSERT(cc > 0); if (cc == ProcCC_Odin) { c->scope->flags |= ScopeFlag_ContextDefined; + } else { + c->scope->flags &= ~ScopeFlag_ContextDefined; } bool variadic = false; diff --git a/src/checker.cpp b/src/checker.cpp index 2bf79134d..bb54fd903 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -281,6 +281,7 @@ Scope *create_scope_from_package(CheckerContext *c, AstPackage *pkg) { if (s->flags & (ScopeFlag_Init|ScopeFlag_Global)) { s->flags |= ScopeFlag_HasBeenImported; } + s->flags |= ScopeFlag_ContextDefined; return s; }