Skip to content

Commit

Permalink
new shared T()
Browse files Browse the repository at this point in the history
  • Loading branch information
momentarylapse committed Sep 30, 2024
1 parent d6d481d commit f74c55c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
8 changes: 8 additions & 0 deletions doc/language/pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ There are several pointer types:
* `shared![X]` - shared ownership, not null
* `xfer[X]` - ownership transfer

## New

🔥 Objects created with the `new` operator have ownership:
```kaba
var a = new X() # type: owned![X]
var b = new shared X() # type: shared![X]
```

## Pointers as containers

Pointers that can be null (`X*`, `owned[X]`, `shared[X]`) need additional unwrapping when using the "contained" value (i.e. the object pointed to by the pointer) (see [error handling](error.md)):
Expand Down
2 changes: 1 addition & 1 deletion src/lib/kaba/kaba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace kaba {

string Version = "0.20.7.0";
string Version = "0.20.7.1";

//#define ScriptDebug

Expand Down
25 changes: 18 additions & 7 deletions src/lib/kaba/parser/Concretifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,19 @@ shared<Node> Concretifier::concretify_statement_new(shared<Node> node, Block *bl
auto tt = ff->name_space;
//do_error("NEW " + tt->long_name());

//node->type = tree->get_pointer(tt, -1);
node->type = tree->request_implicit_class_xfer(tt, -1);
// return xfer[T]
node->type = tree->request_implicit_class_xfer(tt, node->token_id);

// new shared T() ?
if (flags_has(node->flags, Flags::Shared)) {
// return shared![T]
auto t = tree->request_implicit_class_shared_not_null(tt, node->token_id);
CastingDataSingle cd{};
if (!type_match_with_cast(node, false, t, cd))
do_error("can not convert to shared![T]...", node);
return apply_type_cast(cd, node, t);
}

return node;
}

Expand Down Expand Up @@ -1520,13 +1531,13 @@ shared<Node> Concretifier::concretify_operator(shared<Node> node, Block *block,
}
}

const Class *type_ownify_xfer(SyntaxTree *tree, const Class *t) {
const Class *type_ownify_xfer(SyntaxTree *tree, const Class *t, int token_id) {
if (t->is_pointer_xfer_not_null())
return tree->request_implicit_class_owned_not_null(t->param[0], -1);
return tree->request_implicit_class_owned_not_null(t->param[0], token_id);
if (t->is_list()) {
auto tt = type_ownify_xfer(tree, t->param[0]);
auto tt = type_ownify_xfer(tree, t->param[0], token_id);
if (tt != t->param[0])
return tree->request_implicit_class_list(tt, -1);
return tree->request_implicit_class_list(tt, token_id);
}
return t;
}
Expand Down Expand Up @@ -1578,7 +1589,7 @@ shared<Node> Concretifier::concretify_var_declaration(shared<Node> node, Block *
auto rhs = force_concrete_type(concretify_node(node->params[2]->params[1], block, ns));
node->params[2]->params[1] = rhs;
// don't create xfer[X] variables!
type = type_ownify_xfer(tree, rhs->type);
type = type_ownify_xfer(tree, rhs->type, node->token_id);
}

//as_const
Expand Down
4 changes: 3 additions & 1 deletion src/lib/kaba/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,9 +1197,11 @@ shared<Node> Parser::parse_abstract_statement_pass(Block *block) {
// type: class
// p[0]: call to constructor (optional)
shared<Node> Parser::parse_abstract_statement_new(Block *block) {
int token0 = Exp.consume_token(); // "new"
const int token0 = Exp.consume_token(); // "new"
const auto flags = parse_flags();
auto cmd = add_node_statement(StatementID::New, token0, TypeUnknown);
cmd->set_param(0, parse_abstract_operand(block));
flags_set(cmd->flags, flags);
return cmd;
}

Expand Down
8 changes: 8 additions & 0 deletions test/regression/shared/new-shared.kaba
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class S as shared
i: i32
s: string

func main()
#var s: shared![S] = new S()
var s = new shared S()
print(typeof(s))
1 change: 1 addition & 0 deletions test/regression/shared/new-shared.reg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shared![S]

0 comments on commit f74c55c

Please sign in to comment.