Add -disallow-rtti

This commit is contained in:
gingerBill
2022-02-28 13:35:29 +00:00
parent a7ae197a55
commit 01162e08b5
3 changed files with 172 additions and 141 deletions
+2
View File
@@ -508,6 +508,7 @@ __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check
return &type_table[n] return &type_table[n]
} }
when !ODIN_DISALLOW_RTTI {
typeid_base :: proc "contextless" (id: typeid) -> typeid { typeid_base :: proc "contextless" (id: typeid) -> typeid {
ti := type_info_of(id) ti := type_info_of(id)
ti = type_info_base(ti) ti = type_info_base(ti)
@@ -518,6 +519,7 @@ typeid_core :: proc "contextless" (id: typeid) -> typeid {
return ti.id return ti.id
} }
typeid_base_without_enum :: typeid_core typeid_base_without_enum :: typeid_core
}
+56 -35
View File
@@ -17,41 +17,11 @@ type_assertion_trap :: proc "contextless" () -> ! {
} }
bounds_check_error_loc :: #force_inline proc "contextless" (using loc := #caller_location, index, count: int) {
bounds_check_error(file_path, line, column, index, count)
}
slice_expr_error_hi_loc :: #force_inline proc "contextless" (using loc := #caller_location, hi: int, len: int) {
slice_expr_error_hi(file_path, line, column, hi, len)
}
slice_expr_error_lo_hi_loc :: #force_inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) {
slice_expr_error_lo_hi(file_path, line, column, lo, hi, len)
}
dynamic_array_expr_error_loc :: #force_inline proc "contextless" (using loc := #caller_location, low, high, max: int) {
dynamic_array_expr_error(file_path, line, column, low, high, max)
}
when ODIN_FOREIGN_ERROR_PROCEDURES {
foreign {
bounds_check_error :: proc "contextless" (file: string, line, column: i32, index, count: int) ---
slice_handle_error :: proc "contextless" (file: string, line, column: i32, lo, hi: int, len: int) -> ! ---
multi_pointer_slice_handle_error :: proc "contextless" (file: string, line, column: i32, lo, hi: int) -> ! ---
multi_pointer_slice_expr_error :: proc "contextless" (file: string, line, column: i32, lo, hi: int) ---
slice_expr_error_hi :: proc "contextless" (file: string, line, column: i32, hi: int, len: int) ---
slice_expr_error_lo_hi :: proc "contextless" (file: string, line, column: i32, lo, hi: int, len: int) ---
dynamic_array_expr_error :: proc "contextless" (file: string, line, column: i32, low, high, max: int) ---
matrix_bounds_check_error :: proc "contextless" (file: string, line, column: i32, row_index, column_index, row_count, column_count: int) ---
type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: i32, from, to: typeid) ---
type_assertion_check2 :: proc "contextless" (ok: bool, file: string, line, column: i32, from, to: typeid, from_data: rawptr) ---
}
} else {
bounds_check_error :: proc "contextless" (file: string, line, column: i32, index, count: int) { bounds_check_error :: proc "contextless" (file: string, line, column: i32, index, count: int) {
if 0 <= index && index < count { if 0 <= index && index < count {
return return
} }
@(cold)
handle_error :: proc "contextless" (file: string, line, column: i32, index, count: int) { handle_error :: proc "contextless" (file: string, line, column: i32, index, count: int) {
print_caller_location(Source_Code_Location{file, line, column, ""}) print_caller_location(Source_Code_Location{file, line, column, ""})
print_string(" Index ") print_string(" Index ")
@@ -112,6 +82,7 @@ when ODIN_FOREIGN_ERROR_PROCEDURES {
if 0 <= low && low <= high && high <= max { if 0 <= low && low <= high && high <= max {
return return
} }
@(cold)
handle_error :: proc "contextless" (file: string, line, column: i32, low, high, max: int) { handle_error :: proc "contextless" (file: string, line, column: i32, low, high, max: int) {
print_caller_location(Source_Code_Location{file, line, column, ""}) print_caller_location(Source_Code_Location{file, line, column, ""})
print_string(" Invalid dynamic array indices ") print_string(" Invalid dynamic array indices ")
@@ -132,6 +103,7 @@ when ODIN_FOREIGN_ERROR_PROCEDURES {
0 <= column_index && column_index < column_count { 0 <= column_index && column_index < column_count {
return return
} }
@(cold)
handle_error :: proc "contextless" (file: string, line, column: i32, row_index, column_index, row_count, column_count: int) { handle_error :: proc "contextless" (file: string, line, column: i32, row_index, column_index, row_count, column_count: int) {
print_caller_location(Source_Code_Location{file, line, column, ""}) print_caller_location(Source_Code_Location{file, line, column, ""})
print_string(" Matrix indices [") print_string(" Matrix indices [")
@@ -150,10 +122,38 @@ when ODIN_FOREIGN_ERROR_PROCEDURES {
} }
when ODIN_DISALLOW_RTTI {
type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: i32) {
if ok {
return
}
@(cold)
handle_error :: proc "contextless" (file: string, line, column: i32) {
print_caller_location(Source_Code_Location{file, line, column, ""})
print_string(" Invalid type assertion\n")
type_assertion_trap()
}
handle_error(file, line, column)
}
type_assertion_check2 :: proc "contextless" (ok: bool, file: string, line, column: i32) {
if ok {
return
}
@(cold)
handle_error :: proc "contextless" (file: string, line, column: i32) {
print_caller_location(Source_Code_Location{file, line, column, ""})
print_string(" Invalid type assertion\n")
type_assertion_trap()
}
handle_error(file, line, column)
}
} else {
type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: i32, from, to: typeid) { type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: i32, from, to: typeid) {
if ok { if ok {
return return
} }
@(cold)
handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid) { handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid) {
print_caller_location(Source_Code_Location{file, line, column, ""}) print_caller_location(Source_Code_Location{file, line, column, ""})
print_string(" Invalid type assertion from ") print_string(" Invalid type assertion from ")
@@ -198,6 +198,7 @@ when ODIN_FOREIGN_ERROR_PROCEDURES {
return id return id
} }
@(cold)
handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid, from_data: rawptr) { handle_error :: proc "contextless" (file: string, line, column: i32, from, to: typeid, from_data: rawptr) {
actual := variant_type(from, from_data) actual := variant_type(from, from_data)
@@ -219,13 +220,11 @@ when ODIN_FOREIGN_ERROR_PROCEDURES {
} }
// `make` related procedures below
make_slice_error_loc :: #force_inline proc "contextless" (loc := #caller_location, len: int) { make_slice_error_loc :: #force_inline proc "contextless" (loc := #caller_location, len: int) {
if 0 <= len { if 0 <= len {
return return
} }
@(cold)
handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) { handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) {
print_caller_location(loc) print_caller_location(loc)
print_string(" Invalid slice length for make: ") print_string(" Invalid slice length for make: ")
@@ -236,10 +235,11 @@ make_slice_error_loc :: #force_inline proc "contextless" (loc := #caller_locatio
handle_error(loc, len) handle_error(loc, len)
} }
make_dynamic_array_error_loc :: #force_inline proc "contextless" (loc := #caller_location, len, cap: int) { make_dynamic_array_error_loc :: #force_inline proc "contextless" (using loc := #caller_location, len, cap: int) {
if 0 <= len && len <= cap { if 0 <= len && len <= cap {
return return
} }
@(cold)
handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) { handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) {
print_caller_location(loc) print_caller_location(loc)
print_string(" Invalid dynamic array parameters for make: ") print_string(" Invalid dynamic array parameters for make: ")
@@ -256,6 +256,7 @@ make_map_expr_error_loc :: #force_inline proc "contextless" (loc := #caller_loca
if 0 <= cap { if 0 <= cap {
return return
} }
@(cold)
handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) { handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) {
print_caller_location(loc) print_caller_location(loc)
print_string(" Invalid map capacity for make: ") print_string(" Invalid map capacity for make: ")
@@ -265,3 +266,23 @@ make_map_expr_error_loc :: #force_inline proc "contextless" (loc := #caller_loca
} }
handle_error(loc, cap) handle_error(loc, cap)
} }
bounds_check_error_loc :: #force_inline proc "contextless" (using loc := #caller_location, index, count: int) {
bounds_check_error(file_path, line, column, index, count)
}
slice_expr_error_hi_loc :: #force_inline proc "contextless" (using loc := #caller_location, hi: int, len: int) {
slice_expr_error_hi(file_path, line, column, hi, len)
}
slice_expr_error_lo_hi_loc :: #force_inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) {
slice_expr_error_lo_hi(file_path, line, column, lo, hi, len)
}
dynamic_array_expr_error_loc :: #force_inline proc "contextless" (using loc := #caller_location, low, high, max: int) {
dynamic_array_expr_error(file_path, line, column, low, high, max)
}
+8
View File
@@ -160,6 +160,13 @@ print_caller_location :: proc "contextless" (using loc: Source_Code_Location) {
} }
} }
print_typeid :: proc "contextless" (id: typeid) { print_typeid :: proc "contextless" (id: typeid) {
when ODIN_DISALLOW_RTTI {
if id == nil {
print_string("nil")
} else {
print_string("<unknown type>")
}
} else {
if id == nil { if id == nil {
print_string("nil") print_string("nil")
} else { } else {
@@ -167,6 +174,7 @@ print_typeid :: proc "contextless" (id: typeid) {
print_type(ti) print_type(ti)
} }
} }
}
print_type :: proc "contextless" (ti: ^Type_Info) { print_type :: proc "contextless" (ti: ^Type_Info) {
if ti == nil { if ti == nil {
print_string("nil") print_string("nil")