Skip to content

Commit

Permalink
[cpp] i64 is now implicitly convertible to any weak pointer (essent…
Browse files Browse the repository at this point in the history
…ially a intptr_t)
  • Loading branch information
harrand committed May 18, 2024
1 parent 2787e47 commit 65a683e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions cpp/src/semal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ namespace semal
d.assert_that(std::holds_alternative<ast::identifier>(payload.rhs_expr->expr), std::format("in a cast, rhs of the cast token \"{}\" must be an identifier, not an expression or anything else.", payload.op.lexeme));
std::string type_name = std::get<ast::identifier>(payload.rhs_expr->expr).iden;
rhs = d.state.get_type_from_name(type_name);
d.assert_that(lhs.is_explicitly_convertible_to(rhs), std::format("cast from \"{}\" to \"{}\" is impossible", lhs.name(), rhs.name()));
d.assert_that(!rhs.is_undefined(), std::format("unknown cast destination type \"{}\"", type_name));
// todo: confirm that lhs can actually be casted to rhs.
}
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ bool type::is_implicitly_convertible_to(const type& rhs) const
if(this->is_weak() || rhs.is_weak())
{
// weak types are implicitly convertible to a bunch of things.
auto plain_this = this->without_qualifiers();
if(plain_this == type::from_primitive(primitive_type::i64) && rhs.is_pointer())
{
// i64 -> any pointer (i.e uintptr_t)
return true;
}
if(this->is_integer_type() && rhs.is_integer_type())
{
// integer promotion.
Expand Down
2 changes: 1 addition & 1 deletion samples/scratchpad.psy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ putchar :: (ch : i8) -> u0 := extern;
dub :: (val : i64) -> i64
{
vvptr : i64&& const;
deref vvptr = (55@i64&);
deref vvptr = (55@u64& weak);
vv : f64 weak := 5;
vv = 50;

Expand Down

0 comments on commit 65a683e

Please sign in to comment.