use reinterpret_cast as pointer cast

This commit is contained in:
ValKmjolnir 2023-11-05 00:10:26 +08:00
parent c946e9debd
commit 8a160dc7f2
6 changed files with 86 additions and 58 deletions

View File

@ -71,7 +71,7 @@ var nas_bind(var* args, usize size, gc* ngc) {
server.sin_port = htons(args[2].num()); server.sin_port = htons(args[2].num());
return var::num(static_cast<double>(bind( return var::num(static_cast<double>(bind(
args[0].num(), args[0].num(),
(sockaddr*)&server, reinterpret_cast<sockaddr*>(&server),
sizeof(server) sizeof(server)
))); )));
} }
@ -110,9 +110,17 @@ var nas_accept(var* args, usize size, gc* ngc) {
sockaddr_in client; sockaddr_in client;
int socklen = sizeof(sockaddr_in); int socklen = sizeof(sockaddr_in);
#ifdef _WIN32 #ifdef _WIN32
int client_sd = accept(args[0].num(), (sockaddr*)&client, &socklen); int client_sd = accept(
args[0].num(),
reinterpret_cast<sockaddr*>(&client),
&socklen
);
#else #else
int client_sd = accept(args[0].num(), (sockaddr*)&client, (socklen_t*)&socklen); int client_sd = accept(
args[0].num(),
reinterpret_cast<sockaddr*>(&client),
reinterpret_cast<socklen_t*>(&socklen)
);
#endif #endif
var res = ngc->temp = ngc->alloc(vm_hash); var res = ngc->temp = ngc->alloc(vm_hash);
auto& hash = res.hash().elems; auto& hash = res.hash().elems;
@ -159,7 +167,7 @@ var nas_sendto(var* args, usize size, gc* ngc) {
args[3].str().c_str(), args[3].str().c_str(),
args[3].str().length(), args[3].str().length(),
args[4].num(), args[4].num(),
(sockaddr*)&addr, reinterpret_cast<sockaddr*>(&addr),
sizeof(sockaddr_in) sizeof(sockaddr_in)
))); )));
} }
@ -205,7 +213,7 @@ var nas_recvfrom(var* args, usize size, gc* ngc) {
buf, buf,
args[1].num(), args[1].num(),
args[2].num(), args[2].num(),
(sockaddr*)&addr, reinterpret_cast<sockaddr*>(&addr),
&socklen &socklen
); );
#else #else
@ -214,8 +222,8 @@ var nas_recvfrom(var* args, usize size, gc* ngc) {
buf, buf,
args[1].num(), args[1].num(),
args[2].num(), args[2].num(),
(sockaddr*)&addr, reinterpret_cast<sockaddr*>(&addr),
(socklen_t*)&socklen reinterpret_cast<socklen_t*>(&socklen)
); );
#endif #endif
hash["size"] = var::num(static_cast<double>(recvsize)); hash["size"] = var::num(static_cast<double>(recvsize));

View File

@ -45,7 +45,7 @@ var builtin_cocreate(context* ctx, gc* ngc) {
coroutine.ctx.top++; coroutine.ctx.top++;
// store old localr on stack // store old localr on stack
coroutine.ctx.top[0] = var::addr((var*)nullptr); coroutine.ctx.top[0] = var::addr(nullptr);
coroutine.ctx.top++; coroutine.ctx.top++;
// store old pc on stack // store old pc on stack

View File

@ -53,9 +53,9 @@ var builtin_dlopen(context* ctx, gc* ngc) {
// get "get" function, to get the register table // get "get" function, to get the register table
#ifdef _WIN32 #ifdef _WIN32
void* register_table_get_function = (void*)GetProcAddress( void* register_table_get_function = reinterpret_cast<void*>(GetProcAddress(
static_cast<HMODULE>(library_object.ghost().pointer), "get" static_cast<HMODULE>(library_object.ghost().pointer), "get"
); ));
#else #else
void* register_table_get_function = dlsym( void* register_table_get_function = dlsym(
library_object.ghost().pointer, "get" library_object.ghost().pointer, "get"

View File

@ -113,11 +113,11 @@ bool linker::import_check(expr* node) {
if (call_node->get_calls().size()!=1) { if (call_node->get_calls().size()!=1) {
return false; return false;
} }
auto maybe_func_call = call_node->get_calls()[0];
if (call_node->get_calls()[0]->get_type()!=expr_type::ast_callf) { if (maybe_func_call->get_type()!=expr_type::ast_callf) {
return false; return false;
} }
auto func_call = (call_function*)call_node->get_calls()[0]; auto func_call = reinterpret_cast<call_function*>(maybe_func_call);
if (func_call->get_argument().size()!=1) { if (func_call->get_argument().size()!=1) {
return false; return false;
} }

View File

@ -132,9 +132,13 @@ bool parse::check_func_end(expr* node) {
if (type==expr_type::ast_func) { if (type==expr_type::ast_func) {
return true; return true;
} else if (type==expr_type::ast_def) { } else if (type==expr_type::ast_def) {
return check_func_end(((definition_expr*)node)->get_value()); return check_func_end(
reinterpret_cast<definition_expr*>(node)->get_value()
);
} else if (type==expr_type::ast_assign) { } else if (type==expr_type::ast_assign) {
return check_func_end(((assignment_expr*)node)->get_right()); return check_func_end(
reinterpret_cast<assignment_expr*>(node)->get_right()
);
} }
return false; return false;
} }

View File

@ -12,7 +12,8 @@ void optimizer::const_string(
const auto& left = left_node->get_content(); const auto& left = left_node->get_content();
const auto& right = right_node->get_content(); const auto& right = right_node->get_content();
node->set_optimized_string( node->set_optimized_string(
new string_literal(node->get_location(), left+right)); new string_literal(node->get_location(), left+right)
);
} }
void optimizer::const_number( void optimizer::const_number(
@ -43,7 +44,8 @@ void optimizer::const_number(
return; return;
} }
node->set_optimized_number( node->set_optimized_number(
new number_literal(node->get_location(), res)); new number_literal(node->get_location(), res)
);
} }
void optimizer::const_number( void optimizer::const_number(
@ -62,46 +64,56 @@ void optimizer::const_number(
return; return;
} }
node->set_optimized_number( node->set_optimized_number(
new number_literal(node->get_location(), res)); new number_literal(node->get_location(), res)
);
} }
bool optimizer::visit_binary_operator(binary_operator* node) { bool optimizer::visit_binary_operator(binary_operator* node) {
node->get_left()->accept(this); auto left_node = node->get_left();
node->get_right()->accept(this); auto right_node = node->get_right();
left_node->accept(this);
right_node->accept(this);
number_literal* left_num_node = nullptr; number_literal* left_num_node = nullptr;
number_literal* right_num_node = nullptr; number_literal* right_num_node = nullptr;
string_literal* left_str_node = nullptr; string_literal* left_str_node = nullptr;
string_literal* right_str_node = nullptr; string_literal* right_str_node = nullptr;
if (node->get_left()->get_type()==expr_type::ast_num) { if (left_node->get_type()==expr_type::ast_num) {
left_num_node = (number_literal*)node->get_left(); left_num_node = reinterpret_cast<number_literal*>(left_node);
} else if (node->get_left()->get_type()==expr_type::ast_binary && } else if (left_node->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_left())->get_optimized_number()) { reinterpret_cast<binary_operator*>(left_node)->get_optimized_number()) {
left_num_node = ((binary_operator*)node->get_left())->get_optimized_number(); auto optimized = reinterpret_cast<binary_operator*>(left_node);
} else if (node->get_left()->get_type()==expr_type::ast_unary && left_num_node = optimized->get_optimized_number();
((unary_operator*)node->get_left())->get_optimized_number()) { } else if (left_node->get_type()==expr_type::ast_unary &&
left_num_node = ((unary_operator*)node->get_left())->get_optimized_number(); reinterpret_cast<unary_operator*>(left_node)->get_optimized_number()) {
auto optimized = reinterpret_cast<unary_operator*>(left_node);
left_num_node = optimized->get_optimized_number();
} }
if (node->get_right()->get_type()==expr_type::ast_num) { if (right_node->get_type()==expr_type::ast_num) {
right_num_node = (number_literal*)node->get_right(); right_num_node = reinterpret_cast<number_literal*>(right_node);
} else if (node->get_right()->get_type()==expr_type::ast_binary && } else if (right_node->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_right())->get_optimized_number()) { reinterpret_cast<binary_operator*>(right_node)->get_optimized_number()) {
right_num_node = ((binary_operator*)node->get_right())->get_optimized_number(); auto optimized = reinterpret_cast<binary_operator*>(right_node);
} else if (node->get_right()->get_type()==expr_type::ast_unary && right_num_node = optimized->get_optimized_number();
((unary_operator*)node->get_right())->get_optimized_number()) { } else if (right_node->get_type()==expr_type::ast_unary &&
right_num_node = ((unary_operator*)node->get_right())->get_optimized_number(); reinterpret_cast<unary_operator*>(right_node)->get_optimized_number()) {
auto optimized = reinterpret_cast<unary_operator*>(right_node);
right_num_node = optimized->get_optimized_number();
} }
if (node->get_left()->get_type()==expr_type::ast_str) { if (left_node->get_type()==expr_type::ast_str) {
left_str_node = (string_literal*)node->get_left(); left_str_node = reinterpret_cast<string_literal*>(left_node);
} else if (node->get_left()->get_type()==expr_type::ast_binary && } else if (left_node->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_left())->get_optimized_string()) { reinterpret_cast<binary_operator*>(left_node)->get_optimized_string()) {
left_str_node = ((binary_operator*)node->get_left())->get_optimized_string(); auto optimized = reinterpret_cast<binary_operator*>(left_node);
left_str_node = optimized->get_optimized_string();
} }
if (node->get_right()->get_type()==expr_type::ast_str) { if (right_node->get_type()==expr_type::ast_str) {
right_str_node = (string_literal*)node->get_right(); right_str_node = reinterpret_cast<string_literal*>(right_node);
} else if (node->get_right()->get_type()==expr_type::ast_binary && } else if (right_node->get_type()==expr_type::ast_binary &&
((binary_operator*)node->get_right())->get_optimized_string()) { reinterpret_cast<binary_operator*>(right_node)->get_optimized_string()) {
right_str_node = ((binary_operator*)node->get_right())->get_optimized_string(); auto optimized = reinterpret_cast<binary_operator*>(right_node);
right_str_node = optimized->get_optimized_string();
} }
if (left_num_node && right_num_node) { if (left_num_node && right_num_node) {
const_number(node, left_num_node, right_num_node); const_number(node, left_num_node, right_num_node);
@ -115,19 +127,23 @@ bool optimizer::visit_binary_operator(binary_operator* node) {
} }
bool optimizer::visit_unary_operator(unary_operator* node) { bool optimizer::visit_unary_operator(unary_operator* node) {
node->get_value()->accept(this); auto value = node->get_value();
number_literal* value_node = nullptr; value->accept(this);
if (node->get_value()->get_type()==expr_type::ast_num) {
value_node = (number_literal*)node->get_value(); number_literal* num_node = nullptr;
} else if (node->get_value()->get_type()==expr_type::ast_binary && if (value->get_type()==expr_type::ast_num) {
((binary_operator*)node->get_value())->get_optimized_number()) { num_node = reinterpret_cast<number_literal*>(value);
value_node = ((binary_operator*)node->get_value())->get_optimized_number(); } else if (value->get_type()==expr_type::ast_binary &&
} else if (node->get_value()->get_type()==expr_type::ast_unary && reinterpret_cast<binary_operator*>(value)->get_optimized_number()) {
((unary_operator*)node->get_value())->get_optimized_number()) { auto optimized = reinterpret_cast<binary_operator*>(value);
value_node = ((unary_operator*)node->get_value())->get_optimized_number(); num_node = optimized->get_optimized_number();
} else if (value->get_type()==expr_type::ast_unary &&
reinterpret_cast<unary_operator*>(value)->get_optimized_number()) {
auto optimized = reinterpret_cast<unary_operator*>(value);
num_node = optimized->get_optimized_number();
} }
if (value_node) { if (num_node) {
const_number(node, value_node); const_number(node, num_node);
} }
return true; return true;
} }