authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-13 00:13:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-13 00:13:54-04:00
log41144a8566a6fbd779403f6b69424bb640c94a7f
treeb4c5807dddca6d3eb66047aebccf6cc9d3fdd90c
parentf043e0e85cac9330cf809bef177784c3cd133348

ability to inline at function callsite

closes #306

11 files changed, 213 insertions(+), 48 deletions(-)

doc/langref.md+6-3
......@@ -81,9 +81,9 @@ SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbo
8181
8282SwitchItem = Expression | (Expression "..." Expression)
8383
84WhileExpression(body) = option("inline") "while" "(" Expression option(";" Expression) ")" body
84WhileExpression(body) = "while" "(" Expression option(";" Expression) ")" body
8585
86ForExpression(body) = option("inline") "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body
86ForExpression(body) = "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body
8787
8888BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpression
8989
......@@ -127,7 +127,9 @@ MultiplyOperator = "*" | "/" | "%" | "**" | "*%"
127127
128128PrefixOpExpression = PrefixOp PrefixOpExpression | SuffixOpExpression
129129
130SuffixOpExpression = PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
130SuffixOpExpression = InlineExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
131
132InlineExpression = option("inline") PrimaryExpression
131133
132134FieldAccessExpression = "." Symbol
133135
......@@ -161,6 +163,7 @@ ContainerDecl = option("extern" | "packed") ("struct" | "enum" | "union") "{" ma
161163## Operator Precedence
162164
163165```
166inline x
164167x() x[] x.y
165168!x -x -%x ~x *x &x ?x %x %%x ??x
166169x{}
src/all_types.hpp+21-1
......@@ -167,6 +167,7 @@ struct ConstErrValue {
167167struct ConstBoundFnValue {
168168 FnTableEntry *fn;
169169 IrInstruction *first_arg;
170 bool is_inline;
170171};
171172
172173struct ConstArgTuple {
......@@ -192,6 +193,11 @@ enum RuntimeHintMaybe {
192193 RuntimeHintMaybeNonNull,
193194};
194195
196struct ConstFn {
197 FnTableEntry *fn_entry;
198 bool is_inline;
199};
200
195201struct ConstExprValue {
196202 TypeTableEntry *type;
197203 ConstValSpecial special;
......@@ -202,7 +208,7 @@ struct ConstExprValue {
202208 // populated if special == ConstValSpecialStatic
203209 BigNum x_bignum;
204210 bool x_bool;
205 FnTableEntry *x_fn;
211 ConstFn x_fn;
206212 ConstBoundFnValue x_bound_fn;
207213 TypeTableEntry *x_type;
208214 ConstExprValue *x_maybe;
......@@ -366,6 +372,7 @@ enum NodeType {
366372 NodeTypeTypeLiteral,
367373 NodeTypeVarLiteral,
368374 NodeTypeTryExpr,
375 NodeTypeInlineExpr,
369376};
370377
371378struct AstNodeRoot {
......@@ -789,6 +796,10 @@ struct AstNodeTypeLiteral {
789796struct AstNodeVarLiteral {
790797};
791798
799struct AstNodeInlineExpr {
800 AstNode *body;
801};
802
792803struct AstNode {
793804 enum NodeType type;
794805 size_t line;
......@@ -847,6 +858,7 @@ struct AstNode {
847858 AstNodeErrorType error_type;
848859 AstNodeTypeLiteral type_literal;
849860 AstNodeVarLiteral var_literal;
861 AstNodeInlineExpr inline_expr;
850862 } data;
851863};
852864
......@@ -1748,6 +1760,7 @@ enum IrInstructionId {
17481760 IrInstructionIdDeclRef,
17491761 IrInstructionIdPanic,
17501762 IrInstructionIdEnumTagName,
1763 IrInstructionIdSetFnRefInline,
17511764};
17521765
17531766struct IrInstruction {
......@@ -1943,6 +1956,7 @@ struct IrInstructionCall {
19431956 IrInstruction **args;
19441957 bool is_comptime;
19451958 LLVMValueRef tmp_ptr;
1959 bool is_inline;
19461960};
19471961
19481962struct IrInstructionConst {
......@@ -2493,6 +2507,12 @@ struct IrInstructionEnumTagName {
24932507 IrInstruction *target;
24942508};
24952509
2510struct IrInstructionSetFnRefInline {
2511 IrInstruction base;
2512
2513 IrInstruction *fn_ref;
2514};
2515
24962516static const size_t slice_ptr_index = 0;
24972517static const size_t slice_len_index = 1;
24982518
src/analyze.cpp+10-5
......@@ -2129,6 +2129,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
21292129 case NodeTypeTypeLiteral:
21302130 case NodeTypeVarLiteral:
21312131 case NodeTypeTryExpr:
2132 case NodeTypeInlineExpr:
21322133 zig_unreachable();
21332134 }
21342135}
......@@ -3251,7 +3252,8 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
32513252 // TODO better hashing algorithm
32523253 return 31643936;
32533254 case TypeTableEntryIdFn:
3254 return hash_ptr(const_val->data.x_fn);
3255 return hash_ptr(const_val->data.x_fn.fn_entry) +
3256 (const_val->data.x_fn.is_inline ? 4133894920 : 3983484790);
32553257 case TypeTableEntryIdTypeDecl:
32563258 return hash_ptr(const_val->data.x_type);
32573259 case TypeTableEntryIdNamespace:
......@@ -3697,7 +3699,8 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
36973699 case TypeTableEntryIdPureError:
36983700 return a->data.x_pure_err == b->data.x_pure_err;
36993701 case TypeTableEntryIdFn:
3700 return a->data.x_fn == b->data.x_fn;
3702 return a->data.x_fn.fn_entry == b->data.x_fn.fn_entry &&
3703 a->data.x_fn.is_inline == b->data.x_fn.is_inline;
37013704 case TypeTableEntryIdBool:
37023705 return a->data.x_bool == b->data.x_bool;
37033706 case TypeTableEntryIdInt:
......@@ -3933,8 +3936,9 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
39333936 zig_unreachable();
39343937 case TypeTableEntryIdFn:
39353938 {
3936 FnTableEntry *fn_entry = const_val->data.x_fn;
3937 buf_appendf(buf, "%s", buf_ptr(&fn_entry->symbol_name));
3939 FnTableEntry *fn_entry = const_val->data.x_fn.fn_entry;
3940 const char *inline_str = const_val->data.x_fn.is_inline ? "inline " : "";
3941 buf_appendf(buf, "%s%s", inline_str, buf_ptr(&fn_entry->symbol_name));
39383942 return;
39393943 }
39403944 case TypeTableEntryIdBlock:
......@@ -4011,7 +4015,8 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
40114015 case TypeTableEntryIdBoundFn:
40124016 {
40134017 FnTableEntry *fn_entry = const_val->data.x_bound_fn.fn;
4014 buf_appendf(buf, "(bound fn %s)", buf_ptr(&fn_entry->symbol_name));
4018 const char *inline_str = const_val->data.x_bound_fn.is_inline ? "inline " : "";
4019 buf_appendf(buf, "(%sbound fn %s)", inline_str, buf_ptr(&fn_entry->symbol_name));
40154020 return;
40164021 }
40174022 case TypeTableEntryIdStruct:
src/ast_render.cpp+8
......@@ -240,6 +240,8 @@ static const char *node_type_str(NodeType node_type) {
240240 return "VarLiteral";
241241 case NodeTypeTryExpr:
242242 return "TryExpr";
243 case NodeTypeInlineExpr:
244 return "InlineExpr";
243245 }
244246 zig_unreachable();
245247}
......@@ -921,6 +923,12 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
921923 render_node_ungrouped(ar, node->data.unwrap_err_expr.op2);
922924 break;
923925 }
926 case NodeTypeInlineExpr:
927 {
928 fprintf(ar->f, "inline ");
929 render_node_grouped(ar, node->data.inline_expr.body);
930 break;
931 }
924932 case NodeTypeFnDecl:
925933 case NodeTypeParamDecl:
926934 case NodeTypeErrorValueDecl:
src/codegen.cpp+8-3
......@@ -608,7 +608,7 @@ static void gen_panic(CodeGen *g, LLVMValueRef msg_arg) {
608608 LLVMBuildLoad(g->builder, ptr_ptr, ""),
609609 LLVMBuildLoad(g->builder, len_ptr, ""),
610610 };
611 ZigLLVMBuildCall(g->builder, fn_val, args, 2, panic_fn->type_entry->data.fn.calling_convention, "");
611 ZigLLVMBuildCall(g->builder, fn_val, args, 2, panic_fn->type_entry->data.fn.calling_convention, false, "");
612612 LLVMBuildUnreachable(g->builder);
613613}
614614
......@@ -1773,8 +1773,12 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
17731773 }
17741774 }
17751775
1776 bool want_always_inline = (instruction->fn_entry != nullptr &&
1777 instruction->fn_entry->fn_inline == FnInlineAlways) || instruction->is_inline;
1778
17761779 LLVMValueRef result = ZigLLVMBuildCall(g->builder, fn_val,
1777 gen_param_values, (unsigned)gen_param_index, fn_type->data.fn.calling_convention, "");
1780 gen_param_values, (unsigned)gen_param_index, fn_type->data.fn.calling_convention,
1781 want_always_inline, "");
17781782
17791783 for (size_t param_i = 0; param_i < fn_type_id->param_count; param_i += 1) {
17801784 FnGenParamInfo *gen_info = &fn_type->data.fn.gen_param_info[param_i];
......@@ -2749,6 +2753,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
27492753 case IrInstructionIdSetGlobalLinkage:
27502754 case IrInstructionIdDeclRef:
27512755 case IrInstructionIdSwitchVar:
2756 case IrInstructionIdSetFnRefInline:
27522757 zig_unreachable();
27532758 case IrInstructionIdReturn:
27542759 return ir_render_return(g, executable, (IrInstructionReturn *)instruction);
......@@ -3183,7 +3188,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
31833188 }
31843189 }
31853190 case TypeTableEntryIdFn:
3186 return fn_llvm_value(g, const_val->data.x_fn);
3191 return fn_llvm_value(g, const_val->data.x_fn.fn_entry);
31873192 case TypeTableEntryIdPointer:
31883193 {
31893194 render_const_val_global(g, const_val, "");
src/ir.cpp+102-22
......@@ -545,6 +545,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionEnumTagName *) {
545545 return IrInstructionIdEnumTagName;
546546}
547547
548static constexpr IrInstructionId ir_instruction_id(IrInstructionSetFnRefInline *) {
549 return IrInstructionIdSetFnRefInline;
550}
551
548552template<typename T>
549553static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
550554 T *special_instruction = allocate<T>(1);
......@@ -701,7 +705,7 @@ static IrInstruction *ir_create_const_fn(IrBuilder *irb, Scope *scope, AstNode *
701705 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb, scope, source_node);
702706 const_instruction->base.value.type = fn_entry->type_entry;
703707 const_instruction->base.value.special = ConstValSpecialStatic;
704 const_instruction->base.value.data.x_fn = fn_entry;
708 const_instruction->base.value.data.x_fn.fn_entry = fn_entry;
705709 return &const_instruction->base;
706710}
707711
......@@ -882,12 +886,13 @@ static IrInstruction *ir_build_enum_field_ptr_from(IrBuilder *irb, IrInstruction
882886
883887static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *source_node,
884888 FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args,
885 bool is_comptime)
889 bool is_comptime, bool is_inline)
886890{
887891 IrInstructionCall *call_instruction = ir_build_instruction<IrInstructionCall>(irb, scope, source_node);
888892 call_instruction->fn_entry = fn_entry;
889893 call_instruction->fn_ref = fn_ref;
890894 call_instruction->is_comptime = is_comptime;
895 call_instruction->is_inline = is_inline;
891896 call_instruction->args = args;
892897 call_instruction->arg_count = arg_count;
893898
......@@ -901,10 +906,10 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc
901906
902907static IrInstruction *ir_build_call_from(IrBuilder *irb, IrInstruction *old_instruction,
903908 FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args,
904 bool is_comptime)
909 bool is_comptime, bool is_inline)
905910{
906911 IrInstruction *new_instruction = ir_build_call(irb, old_instruction->scope,
907 old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime);
912 old_instruction->source_node, fn_entry, fn_ref, arg_count, args, is_comptime, is_inline);
908913 ir_link_new_instruction(new_instruction, old_instruction);
909914 return new_instruction;
910915}
......@@ -2145,6 +2150,18 @@ static IrInstruction *ir_build_enum_tag_name(IrBuilder *irb, Scope *scope, AstNo
21452150 return &instruction->base;
21462151}
21472152
2153static IrInstruction *ir_build_set_fn_ref_inline(IrBuilder *irb, Scope *scope, AstNode *source_node,
2154 IrInstruction *fn_ref)
2155{
2156 IrInstructionSetFnRefInline *instruction = ir_build_instruction<IrInstructionSetFnRefInline>(
2157 irb, scope, source_node);
2158 instruction->fn_ref = fn_ref;
2159
2160 ir_ref_instruction(fn_ref, irb->current_basic_block);
2161
2162 return &instruction->base;
2163}
2164
21482165static IrInstruction *ir_instruction_br_get_dep(IrInstructionBr *instruction, size_t index) {
21492166 return nullptr;
21502167}
......@@ -2809,6 +2826,14 @@ static IrInstruction *ir_instruction_enumtagname_get_dep(IrInstructionEnumTagNam
28092826 }
28102827}
28112828
2829static IrInstruction *ir_instruction_setfnrefinline_get_dep(IrInstructionSetFnRefInline *instruction, size_t index) {
2830 switch (index) {
2831 case 0: return instruction->fn_ref;
2832 default: return nullptr;
2833 }
2834}
2835
2836
28122837static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t index) {
28132838 switch (instruction->id) {
28142839 case IrInstructionIdInvalid:
......@@ -2999,6 +3024,8 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
29993024 return ir_instruction_panic_get_dep((IrInstructionPanic *) instruction, index);
30003025 case IrInstructionIdEnumTagName:
30013026 return ir_instruction_enumtagname_get_dep((IrInstructionEnumTagName *) instruction, index);
3027 case IrInstructionIdSetFnRefInline:
3028 return ir_instruction_setfnrefinline_get_dep((IrInstructionSetFnRefInline *) instruction, index);
30023029 }
30033030 zig_unreachable();
30043031}
......@@ -4297,7 +4324,7 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
42974324 return args[i];
42984325 }
42994326
4300 return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false);
4327 return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, false);
43014328}
43024329
43034330static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
......@@ -5490,6 +5517,19 @@ static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNo
54905517 return ir_build_fn_proto(irb, parent_scope, node, param_types, return_type);
54915518}
54925519
5520static IrInstruction *ir_gen_inline_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
5521 assert(node->type == NodeTypeInlineExpr);
5522
5523 AstNode *body_node = node->data.inline_expr.body;
5524
5525 IrInstruction *fn_ptr = ir_gen_node(irb, body_node, parent_scope);
5526 if (fn_ptr == irb->codegen->invalid_instruction)
5527 return irb->codegen->invalid_instruction;
5528
5529 return ir_build_set_fn_ref_inline(irb, parent_scope, node, fn_ptr);
5530}
5531
5532
54935533static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scope,
54945534 LVal lval)
54955535{
......@@ -5580,6 +5620,8 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
55805620 return ir_lval_wrap(irb, scope, ir_gen_container_decl(irb, scope, node), lval);
55815621 case NodeTypeFnProto:
55825622 return ir_lval_wrap(irb, scope, ir_gen_fn_proto(irb, scope, node), lval);
5623 case NodeTypeInlineExpr:
5624 return ir_lval_wrap(irb, scope, ir_gen_inline_expr(irb, scope, node), lval);
55835625 case NodeTypeFnDef:
55845626 zig_panic("TODO IR gen NodeTypeFnDef");
55855627 case NodeTypeFnDecl:
......@@ -6435,7 +6477,7 @@ static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value
64356477 return const_val->data.x_type;
64366478}
64376479
6438static FnTableEntry *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {
6480static FnTableEntry *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value, bool *is_inline) {
64396481 if (fn_value == ira->codegen->invalid_instruction)
64406482 return nullptr;
64416483
......@@ -6452,7 +6494,8 @@ static FnTableEntry *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {
64526494 if (!const_val)
64536495 return nullptr;
64546496
6455 return const_val->data.x_fn;
6497 *is_inline = const_val->data.x_fn.is_inline;
6498 return const_val->data.x_fn.fn_entry;
64566499}
64576500
64586501static IrInstruction *ir_analyze_maybe_wrap(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, TypeTableEntry *wanted_type) {
......@@ -8245,7 +8288,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
82458288
82468289static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call_instruction,
82478290 FnTableEntry *fn_entry, TypeTableEntry *fn_type, IrInstruction *fn_ref,
8248 IrInstruction *first_arg_ptr, bool inline_fn_call)
8291 IrInstruction *first_arg_ptr, bool comptime_fn_call, bool inline_fn_call)
82498292{
82508293 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
82518294 size_t first_arg_1_or_0 = first_arg_ptr ? 1 : 0;
......@@ -8276,7 +8319,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
82768319 return ira->codegen->builtin_types.entry_invalid;
82778320 }
82788321
8279 if (inline_fn_call) {
8322 if (comptime_fn_call) {
82808323 // No special handling is needed for compile time evaluation of generic functions.
82818324 if (!fn_entry || fn_entry->type_entry->data.fn.fn_type_id.is_extern) {
82828325 ir_add_error(ira, fn_ref, buf_sprintf("unable to evaluate constant expression"));
......@@ -8472,8 +8515,8 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
84728515 inst_fn_type_id.return_type = return_type;
84738516
84748517 if (type_requires_comptime(return_type)) {
8475 // Throw out our work and call the function as if it were inline.
8476 return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr, true);
8518 // Throw out our work and call the function as if it were comptime.
8519 return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr, true, false);
84778520 }
84788521 }
84798522
......@@ -8498,7 +8541,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
84988541
84998542 size_t impl_param_count = impl_fn->type_entry->data.fn.fn_type_id.param_count;
85008543 IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base,
8501 impl_fn, nullptr, impl_param_count, casted_args, false);
8544 impl_fn, nullptr, impl_param_count, casted_args, false, inline_fn_call);
85028545
85038546 TypeTableEntry *return_type = impl_fn->type_entry->data.fn.fn_type_id.return_type;
85048547 ir_add_alloca(ira, new_call_instruction, return_type);
......@@ -8557,7 +8600,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
85578600 return ira->codegen->builtin_types.entry_invalid;
85588601
85598602 IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base,
8560 fn_entry, fn_ref, call_param_count, casted_args, false);
8603 fn_entry, fn_ref, call_param_count, casted_args, false, inline_fn_call);
85618604
85628605 ir_add_alloca(ira, new_call_instruction, return_type);
85638606 return ir_finish_anal(ira, return_type);
......@@ -8568,10 +8611,10 @@ static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstruction
85688611 if (type_is_invalid(fn_ref->value.type))
85698612 return ira->codegen->builtin_types.entry_invalid;
85708613
8571 bool is_inline = call_instruction->is_comptime ||
8614 bool is_comptime = call_instruction->is_comptime ||
85728615 ir_should_inline(ira->new_irb.exec, call_instruction->base.scope);
85738616
8574 if (is_inline || instr_is_comptime(fn_ref)) {
8617 if (is_comptime || instr_is_comptime(fn_ref)) {
85758618 if (fn_ref->value.type->id == TypeTableEntryIdMetaType) {
85768619 TypeTableEntry *dest_type = ir_resolve_type(ira, fn_ref);
85778620 if (type_is_invalid(dest_type))
......@@ -8594,15 +8637,17 @@ static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstruction
85948637 ir_link_new_instruction(cast_instruction, &call_instruction->base);
85958638 return ir_finish_anal(ira, cast_instruction->value.type);
85968639 } else if (fn_ref->value.type->id == TypeTableEntryIdFn) {
8597 FnTableEntry *fn_table_entry = ir_resolve_fn(ira, fn_ref);
8640 bool is_inline;
8641 FnTableEntry *fn_table_entry = ir_resolve_fn(ira, fn_ref, &is_inline);
85988642 return ir_analyze_fn_call(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,
8599 fn_ref, nullptr, is_inline);
8643 fn_ref, nullptr, is_comptime, is_inline);
86008644 } else if (fn_ref->value.type->id == TypeTableEntryIdBoundFn) {
86018645 assert(fn_ref->value.special == ConstValSpecialStatic);
86028646 FnTableEntry *fn_table_entry = fn_ref->value.data.x_bound_fn.fn;
86038647 IrInstruction *first_arg_ptr = fn_ref->value.data.x_bound_fn.first_arg;
8648 bool is_inline = fn_ref->value.data.x_bound_fn.is_inline;
86048649 return ir_analyze_fn_call(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,
8605 nullptr, first_arg_ptr, is_inline);
8650 nullptr, first_arg_ptr, is_comptime, is_inline);
86068651 } else {
86078652 ir_add_error_node(ira, fn_ref->source_node,
86088653 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value.type->name)));
......@@ -8612,7 +8657,7 @@ static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstruction
86128657
86138658 if (fn_ref->value.type->id == TypeTableEntryIdFn) {
86148659 return ir_analyze_fn_call(ira, call_instruction, nullptr, fn_ref->value.type,
8615 fn_ref, nullptr, false);
8660 fn_ref, nullptr, false, false);
86168661 } else {
86178662 ir_add_error_node(ira, fn_ref->source_node,
86188663 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->value.type->name)));
......@@ -9354,7 +9399,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
93549399 ConstExprValue *const_val = allocate<ConstExprValue>(1);
93559400 const_val->special = ConstValSpecialStatic;
93569401 const_val->type = fn_entry->type_entry;
9357 const_val->data.x_fn = fn_entry;
9402 const_val->data.x_fn.fn_entry = fn_entry;
93589403
93599404 bool ptr_is_const = true;
93609405 bool ptr_is_volatile = false;
......@@ -9891,7 +9936,7 @@ static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira,
98919936 safety_off_ptr = &block_scope->safety_off;
98929937 safety_set_node_ptr = &block_scope->safety_set_node;
98939938 } else if (target_type->id == TypeTableEntryIdFn) {
9894 FnTableEntry *target_fn = target_val->data.x_fn;
9939 FnTableEntry *target_fn = target_val->data.x_fn.fn_entry;
98959940 assert(target_fn->def_scope);
98969941 safety_off_ptr = &target_fn->def_scope->safety_off;
98979942 safety_set_node_ptr = &target_fn->def_scope->safety_set_node;
......@@ -11164,6 +11209,38 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIn
1116411209 return result->value.type;
1116511210}
1116611211
11212static TypeTableEntry *ir_analyze_instruction_set_fn_ref_inline(IrAnalyze *ira,
11213 IrInstructionSetFnRefInline *instruction)
11214{
11215 IrInstruction *fn_ref = instruction->fn_ref->other;
11216 if (type_is_invalid(fn_ref->value.type))
11217 return ira->codegen->builtin_types.entry_invalid;
11218
11219 if (fn_ref->value.type->id == TypeTableEntryIdFn) {
11220 ConstExprValue *fn_ref_val = ir_resolve_const(ira, fn_ref, UndefBad);
11221 if (!fn_ref_val)
11222 return ira->codegen->builtin_types.entry_invalid;
11223
11224 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
11225 *out_val = *fn_ref_val;
11226 out_val->data.x_fn.is_inline = true;
11227 return out_val->type;
11228 } else if (fn_ref->value.type->id == TypeTableEntryIdBoundFn) {
11229 ConstExprValue *fn_ref_val = ir_resolve_const(ira, fn_ref, UndefBad);
11230 if (!fn_ref_val)
11231 return ira->codegen->builtin_types.entry_invalid;
11232
11233 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
11234 *out_val = *fn_ref_val;
11235 out_val->data.x_bound_fn.is_inline = true;
11236 return out_val->type;
11237 } else {
11238 ir_add_error(ira, &instruction->base,
11239 buf_sprintf("expected function type, found '%s'", buf_ptr(&fn_ref->value.type->name)));
11240 return ira->codegen->builtin_types.entry_invalid;
11241 }
11242}
11243
1116711244static TypeTableEntry *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstructionTypeName *instruction) {
1116811245 IrInstruction *type_value = instruction->type_value->other;
1116911246 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
......@@ -12710,6 +12787,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1271012787 return ir_analyze_instruction_int_to_ptr(ira, (IrInstructionIntToPtr *)instruction);
1271112788 case IrInstructionIdEnumTagName:
1271212789 return ir_analyze_instruction_enum_tag_name(ira, (IrInstructionEnumTagName *)instruction);
12790 case IrInstructionIdSetFnRefInline:
12791 return ir_analyze_instruction_set_fn_ref_inline(ira, (IrInstructionSetFnRefInline *)instruction);
1271312792 case IrInstructionIdMaybeWrap:
1271412793 case IrInstructionIdErrWrapCode:
1271512794 case IrInstructionIdErrWrapPayload:
......@@ -12892,6 +12971,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
1289212971 case IrInstructionIdErrName:
1289312972 case IrInstructionIdTypeName:
1289412973 case IrInstructionIdEnumTagName:
12974 case IrInstructionIdSetFnRefInline:
1289512975 return false;
1289612976 case IrInstructionIdAsm:
1289712977 {
......@@ -12953,7 +13033,7 @@ FnTableEntry *ir_create_inline_fn(CodeGen *codegen, Buf *fn_name, VariableTableE
1295313033 }
1295413034
1295513035 IrInstruction *call_instruction = ir_build_call(irb, scope, source_node, nullptr, fn_ref_instruction,
12956 arg_count, args, false);
13036 arg_count, args, false, false);
1295713037 ir_build_return(irb, scope, source_node, call_instruction);
1295813038
1295913039 if (codegen->verbose) {
src/ir_print.cpp+7
......@@ -870,6 +870,10 @@ static void ir_print_panic(IrPrint *irp, IrInstructionPanic *instruction) {
870870 fprintf(irp->f, ")");
871871}
872872
873static void ir_print_set_fn_ref_inline(IrPrint *irp, IrInstructionSetFnRefInline *instruction) {
874 fprintf(irp->f, "inline ");
875 ir_print_other_instruction(irp, instruction->fn_ref);
876}
873877
874878static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
875879 ir_print_prefix(irp, instruction);
......@@ -1155,6 +1159,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
11551159 case IrInstructionIdPanic:
11561160 ir_print_panic(irp, (IrInstructionPanic *)instruction);
11571161 break;
1162 case IrInstructionIdSetFnRefInline:
1163 ir_print_set_fn_ref_inline(irp, (IrInstructionSetFnRefInline *)instruction);
1164 break;
11581165 }
11591166 fprintf(irp->f, "\n");
11601167}
src/parser.cpp+39-12
......@@ -927,7 +927,32 @@ static AstNode *ast_parse_curly_suffix_expr(ParseContext *pc, size_t *token_inde
927927}
928928
929929/*
930SuffixOpExpression = PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
930InlineExpression = option("inline") PrimaryExpression
931*/
932static AstNode *ast_parse_inline_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
933 Token *token = &pc->tokens->at(*token_index);
934
935 if (token->id == TokenIdKeywordInline) {
936 *token_index += 1;
937 AstNode *primary_expr_node = ast_parse_primary_expr(pc, token_index, true);
938 if (primary_expr_node->type == NodeTypeWhileExpr) {
939 primary_expr_node->data.while_expr.is_inline = true;
940 return primary_expr_node;
941 } else if (primary_expr_node->type == NodeTypeForExpr) {
942 primary_expr_node->data.for_expr.is_inline = true;
943 return primary_expr_node;
944 } else {
945 AstNode *node = ast_create_node(pc, NodeTypeInlineExpr, token);
946 node->data.inline_expr.body = primary_expr_node;
947 return node;
948 }
949 } else {
950 return ast_parse_primary_expr(pc, token_index, mandatory);
951 }
952}
953
954/*
955SuffixOpExpression = InlineExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
931956FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)
932957ArrayAccessExpression : token(LBracket) Expression token(RBracket)
933958SliceExpression : token(LBracket) Expression token(Ellipsis) option(Expression) token(RBracket) option(token(Const))
......@@ -935,8 +960,8 @@ FieldAccessExpression : token(Dot) token(Symbol)
935960StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression
936961*/
937962static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
938 AstNode *primary_expr = ast_parse_primary_expr(pc, token_index, mandatory);
939 if (!primary_expr)
963 AstNode *inline_expr = ast_parse_inline_expr(pc, token_index, mandatory);
964 if (!inline_expr)
940965 return nullptr;
941966
942967 while (true) {
......@@ -945,10 +970,10 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
945970 *token_index += 1;
946971
947972 AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, first_token);
948 node->data.fn_call_expr.fn_ref_expr = primary_expr;
973 node->data.fn_call_expr.fn_ref_expr = inline_expr;
949974 ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params);
950975
951 primary_expr = node;
976 inline_expr = node;
952977 } else if (first_token->id == TokenIdLBracket) {
953978 *token_index += 1;
954979
......@@ -960,7 +985,7 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
960985 *token_index += 1;
961986
962987 AstNode *node = ast_create_node(pc, NodeTypeSliceExpr, first_token);
963 node->data.slice_expr.array_ref_expr = primary_expr;
988 node->data.slice_expr.array_ref_expr = inline_expr;
964989 node->data.slice_expr.start = expr_node;
965990 node->data.slice_expr.end = ast_parse_expression(pc, token_index, false);
966991
......@@ -972,15 +997,15 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
972997 node->data.slice_expr.is_const = true;
973998 }
974999
975 primary_expr = node;
1000 inline_expr = node;
9761001 } else if (ellipsis_or_r_bracket->id == TokenIdRBracket) {
9771002 *token_index += 1;
9781003
9791004 AstNode *node = ast_create_node(pc, NodeTypeArrayAccessExpr, first_token);
980 node->data.array_access_expr.array_ref_expr = primary_expr;
1005 node->data.array_access_expr.array_ref_expr = inline_expr;
9811006 node->data.array_access_expr.subscript = expr_node;
9821007
983 primary_expr = node;
1008 inline_expr = node;
9841009 } else {
9851010 ast_invalid_token_error(pc, first_token);
9861011 }
......@@ -990,12 +1015,12 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
9901015 Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol);
9911016
9921017 AstNode *node = ast_create_node(pc, NodeTypeFieldAccessExpr, first_token);
993 node->data.field_access_expr.struct_expr = primary_expr;
1018 node->data.field_access_expr.struct_expr = inline_expr;
9941019 node->data.field_access_expr.field_name = token_buf(name_token);
9951020
996 primary_expr = node;
1021 inline_expr = node;
9971022 } else {
998 return primary_expr;
1023 return inline_expr;
9991024 }
10001025 }
10011026}
......@@ -2807,5 +2832,7 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
28072832 case NodeTypeVarLiteral:
28082833 // none
28092834 break;
2835 case NodeTypeInlineExpr:
2836 visit_field(&node->data.inline_expr.body, visit, context);
28102837 }
28112838}
src/zig_llvm.cpp+4-1
......@@ -176,10 +176,13 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
176176
177177
178178LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
179 unsigned NumArgs, unsigned CC, const char *Name)
179 unsigned NumArgs, unsigned CC, bool always_inline, const char *Name)
180180{
181181 CallInst *call_inst = CallInst::Create(unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs), Name);
182182 call_inst->setCallingConv(CC);
183 if (always_inline) {
184 call_inst->addAttribute(AttributeSet::FunctionIndex, Attribute::AlwaysInline);
185 }
183186 return wrap(unwrap(B)->Insert(call_inst));
184187}
185188
src/zig_llvm.hpp+1-1
......@@ -38,7 +38,7 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
3838 const char *filename, LLVMCodeGenFileType file_type, char **error_message, bool is_debug);
3939
4040LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
41 unsigned NumArgs, unsigned CC, const char *Name);
41 unsigned NumArgs, unsigned CC, bool always_inline, const char *Name);
4242
4343LLVMValueRef ZigLLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
4444 const char *Constraints, bool HasSideEffects, bool IsAlignStack, bool is_x86);
test/cases/fn.zig+7
......@@ -87,3 +87,10 @@ fn fn1() -> u32 {5}
8787fn fn2() -> u32 {6}
8888fn fn3() -> u32 {7}
8989fn fn4() -> u32 {8}
90
91
92test "inline function call" {
93 assert((inline add(3, 9)) == 12);
94}
95
96fn add(a: i32, b: i32) -> i32 { a + b }