Skip to content

Commit

Permalink
Implement automatic tuple expansion in function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSmith33 committed Aug 19, 2023
1 parent f53089e commit d9caed5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions source/tests/passing.d
Original file line number Diff line number Diff line change
Expand Up @@ -6496,3 +6496,26 @@ void tester300(ref TestContext ctx) {
auto run1 = ctx.getFunctionPtr!(size_t, size_t, uint)("run1");
assert(run1(42, 10) == 42 - 10);
}


@TestInfo(&tester301)
immutable test301 = q{--- test301
// Variadic args tuple forward into call
u32 run(u32 a, u32 b, u32 c) {
return caller(a, b, c);
}
u32 caller[Args...](Args... args) {
return sum(args);
}
u32 sum[Args...](Args... args) {
u32 res = 0;
#foreach(i, arg; args) {
res += arg;
}
return res;
}
};
void tester301(ref TestContext ctx) {
auto run = ctx.getFunctionPtr!(uint, uint, uint, uint)("run");
assert(run(100, 10, 1) == 111);
}
16 changes: 16 additions & 0 deletions source/vox/fe/ast/expr/call.d
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,25 @@ void name_resolve_call(CallExprNode* node, ref NameResolveState state) {
node.state = AstNodeState.name_resolve;
require_name_resolve(node.callee, state);
require_name_resolve(node.args, state);
expandTuples(node.args, state.context);
node.state = AstNodeState.name_resolve_done;
}

void expandTuples(ref AstNodes nodes, CompilationContext* c) {
uint length = nodes.length;
uint index;
while (index < length) {
auto node = nodes[index].get_node(c);
if (node.astType == AstType.decl_alias_array) {
auto aliasArray = node.as!AliasArrayDeclNode(c);
nodes.replaceAt(c.arrayArena, index, 1, aliasArray.items[]);
length = length - 1 + aliasArray.items.length;
continue;
}
++index;
}
}

// Get type from function declaration
void type_check_call(ref AstIndex callIndex, CallExprNode* node, ref TypeCheckState state)
{
Expand Down
2 changes: 2 additions & 0 deletions todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
- only simple types
- ifti of member functions needs to account for this parameter, because ifti is done before providing this arg
+ variadic template arguments (requires inference)
- alias tuple auto-expansion / manual expansion.
+ auto tuple expansion in function call
- a way to perform IFTI from user code (pass template args with names and runtime types and get)
- #parameter(#type type, string name, type default_value, #alias[] attributes)
- #function(string name, #type return_type, #parameter[] parameters, #alias[] attributes)
Expand Down

0 comments on commit d9caed5

Please sign in to comment.