Skip to content

Commit

Permalink
[cpp] method names are now mangled within try_find_function and metho…
Browse files Browse the repository at this point in the history
…d_call, meaning that you can no longer try calling a method as if it were a free function (which would pass checks, but cause an llvm error coz no `this` parameter). this also means methods can have the same name as free functions without causing conflicts.
  • Loading branch information
harrand committed May 24, 2024
1 parent 3fa26d0 commit a697d07
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cpp/src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,11 +975,12 @@ namespace code
value function_call(const data& d, ast::function_call payload)
{
const semal::function_t* func = d.state.try_find_function(payload.function_name);
d.ctx.assert_that(func != nullptr, error_code::codegen, "call to undefined function \"{}\"", payload.function_name);
d.ctx.assert_that(!func->is_method, error_code::codegen, "call to method as \"{}.{}\" if it were a free function.", func->method_owner_struct_name, payload.function_name);
if(func->is_builtin)
{
return codegen_builtin(d, payload, try_find_builtin(payload.function_name));
}
d.ctx.assert_that(func != nullptr, error_code::codegen, "call to undefined function \"{}\"", payload.function_name);
auto* llvm_func = static_cast<llvm::Function*>(func->userdata);
d.ctx.assert_that(llvm_func != nullptr, error_code::ice, "function \"{}\" had a nullptr userdata, implying it has not been codegen'd", payload.function_name);
std::vector<llvm::Value*> llvm_params = {};
Expand Down
11 changes: 10 additions & 1 deletion cpp/src/semal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ namespace semal
{
for(const auto& [mthname, mthdata] : structdata.methods)
{
if(mthname == name)
if(mangle_method_name(mthname) == name)
{
return &mthdata;
}
Expand Down Expand Up @@ -206,6 +206,10 @@ namespace semal
const std::string& function_name = std::get<ast::function_definition>(ancestor.payload).func_name;
const function_t* found_func = this->try_find_function(function_name.c_str());
if(found_func == nullptr)
{
found_func = this->try_find_function(semal::mangle_method_name(function_name));
}
if(found_func == nullptr)
{
diag::ice("at: {}: internal compiler error: found a parent function of an AST node (named \"{}\"), but could not then retrieve the function data from semantic analysis state.", ancestor.meta.to_string(), function_name);
}
Expand Down Expand Up @@ -485,6 +489,11 @@ namespace semal
return ret;
}

std::string mangle_method_name(std::string method_name)
{
return "__method_" + method_name;
}

type output::get_type_from_payload(const ast::node::payload_t& payload, const ast& tree, const ast::path_t& path) const
{
return generic({.state = const_cast<output&>(*this), .path = path, .tree = tree}, payload);
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/semal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace semal
}
}
};

struct local_variable_t
{
type ty;
Expand Down Expand Up @@ -99,6 +100,7 @@ namespace semal

output analyse_predecl(const ast& tree);
output analyse_full(const ast& tree, output predecl = {});
std::string mangle_method_name(std::string method_name);

struct state
{
Expand Down

0 comments on commit a697d07

Please sign in to comment.