authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-03 19:38:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-03 19:38:36-07:00
loge64c0941f9cff66311c969b85be5fe5575c4ce45
treea3417dd85241d55362433f41e00b72d4d2cba435
parentfa6e3eec464227a2c20ca949e2fd12f3ab649960

implement #sizeof()

closes #8

6 files changed, 177 insertions(+), 56 deletions(-)

doc/langref.md+5-3
......@@ -60,9 +60,11 @@ ParamDeclList : token(LParen) list(ParamDecl, token(Comma)) token(RParen)
6060
6161ParamDecl : token(Symbol) token(Colon) Type | token(Ellipsis)
6262
63Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompileTimeFnCall
63Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompilerFnExpr
6464
65CompileTimeFnCall : token(NumberSign) token(Symbol) token(LParen) Expression token(RParen)
65CompilerFnExpr : token(NumberSign) token(Symbol) token(LParen) Expression token(RParen)
66
67CompilerFnType : token(NumberSign) token(Symbol) token(LParen) Type token(RParen)
6668
6769PointerType : token(Ampersand) option(token(Const)) Type
6870
......@@ -152,7 +154,7 @@ ArrayAccessExpression : token(LBracket) Expression token(RBracket)
152154
153155PrefixOp : token(Not) | token(Dash) | token(Tilde) | (token(Ampersand) option(token(Const)))
154156
155PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | Goto | token(Break) | token(Continue) | BlockExpression | token(Symbol) | StructValueExpression
157PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | Goto | token(Break) | token(Continue) | BlockExpression | token(Symbol) | StructValueExpression | CompilerFnType
156158
157159StructValueExpression : token(Type) token(LBrace) list(StructValueExpressionField, token(Comma)) token(RBrace)
158160
src/analyze.cpp+55-9
......@@ -59,7 +59,8 @@ static AstNode *first_executing_node(AstNode *node) {
5959 case NodeTypeStructValueExpr:
6060 case NodeTypeStructValueField:
6161 case NodeTypeWhileExpr:
62 case NodeTypeCompilerFnCall:
62 case NodeTypeCompilerFnExpr:
63 case NodeTypeCompilerFnType:
6364 return node;
6465 }
6566 zig_panic("unreachable");
......@@ -109,6 +110,20 @@ TypeTableEntry *new_type_table_entry(TypeTableEntryId id) {
109110 return entry;
110111}
111112
113static TypeTableEntry *get_number_literal_type_unsigned(CodeGen *g, uint64_t x) {
114 NumLit kind;
115 if (x <= UINT8_MAX) {
116 kind = NumLitU8;
117 } else if (x <= UINT16_MAX) {
118 kind = NumLitU16;
119 } else if (x <= UINT32_MAX) {
120 kind = NumLitU32;
121 } else {
122 kind = NumLitU64;
123 }
124 return g->num_lit_types[kind];
125}
126
112127TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const) {
113128 TypeTableEntry **parent_pointer = is_const ?
114129 &child_type->pointer_const_parent :
......@@ -279,15 +294,16 @@ static TypeTableEntry *resolve_type(CodeGen *g, AstNode *node, ImportTableEntry
279294 case AstNodeTypeTypeCompilerExpr:
280295 {
281296 AstNode *compiler_expr_node = node->data.type.compiler_expr;
282 Buf *fn_name = &compiler_expr_node->data.compiler_fn_call.name;
297 Buf *fn_name = &compiler_expr_node->data.compiler_fn_expr.name;
283298 if (buf_eql_str(fn_name, "typeof")) {
284 return analyze_expression(g, import, context, nullptr,
285 compiler_expr_node->data.compiler_fn_call.expr);
299 type_node->entry = analyze_expression(g, import, context, nullptr,
300 compiler_expr_node->data.compiler_fn_expr.expr);
286301 } else {
287302 add_node_error(g, node,
288303 buf_sprintf("invalid compiler function: '%s'", buf_ptr(fn_name)));
289 return g->builtin_types.entry_invalid;
304 type_node->entry = g->builtin_types.entry_invalid;
290305 }
306 return type_node->entry;
291307 }
292308 }
293309 zig_unreachable();
......@@ -625,7 +641,8 @@ static void preview_function_declarations(CodeGen *g, ImportTableEntry *import,
625641 case NodeTypeStructField:
626642 case NodeTypeStructValueExpr:
627643 case NodeTypeStructValueField:
628 case NodeTypeCompilerFnCall:
644 case NodeTypeCompilerFnExpr:
645 case NodeTypeCompilerFnType:
629646 zig_unreachable();
630647 }
631648}
......@@ -698,7 +715,8 @@ static void preview_types(CodeGen *g, ImportTableEntry *import, AstNode *node) {
698715 case NodeTypeStructField:
699716 case NodeTypeStructValueExpr:
700717 case NodeTypeStructValueField:
701 case NodeTypeCompilerFnCall:
718 case NodeTypeCompilerFnExpr:
719 case NodeTypeCompilerFnType:
702720 zig_unreachable();
703721 }
704722}
......@@ -1580,6 +1598,30 @@ static TypeTableEntry *analyze_if_var_expr(CodeGen *g, ImportTableEntry *import,
15801598 node->data.if_var_expr.then_block, node->data.if_var_expr.else_node, node);
15811599}
15821600
1601static TypeTableEntry *analyze_compiler_fn_type(CodeGen *g, ImportTableEntry *import, BlockContext *context,
1602 TypeTableEntry *expected_type, AstNode *node)
1603{
1604 assert(node->type == NodeTypeCompilerFnType);
1605
1606 Buf *name = &node->data.compiler_fn_type.name;
1607 if (buf_eql_str(name, "sizeof")) {
1608 TypeTableEntry *type_entry = resolve_type(g, node->data.compiler_fn_type.type, import, context);
1609 uint64_t size_in_bytes = type_entry->size_in_bits / 8;
1610
1611 TypeTableEntry *num_lit_type = get_number_literal_type_unsigned(g, size_in_bytes);
1612
1613 NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
1614 assert(!codegen_num_lit->resolved_type);
1615 codegen_num_lit->resolved_type = resolve_type_compatibility(g, context, node, expected_type, num_lit_type);
1616
1617 return num_lit_type;
1618 } else {
1619 add_node_error(g, node,
1620 buf_sprintf("invalid compiler function: '%s'", buf_ptr(name)));
1621 return g->builtin_types.entry_invalid;
1622 }
1623}
1624
15831625static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, BlockContext *context,
15841626 TypeTableEntry *expected_type, AstNode *node)
15851627{
......@@ -1852,6 +1894,9 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
18521894 case NodeTypeStructValueExpr:
18531895 return_type = analyze_struct_val_expr(g, import, context, expected_type, node);
18541896 break;
1897 case NodeTypeCompilerFnType:
1898 return_type = analyze_compiler_fn_type(g, import, context, expected_type, node);
1899 break;
18551900 case NodeTypeDirective:
18561901 case NodeTypeFnDecl:
18571902 case NodeTypeFnProto:
......@@ -1866,7 +1911,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
18661911 case NodeTypeStructDecl:
18671912 case NodeTypeStructField:
18681913 case NodeTypeStructValueField:
1869 case NodeTypeCompilerFnCall:
1914 case NodeTypeCompilerFnExpr:
18701915 zig_unreachable();
18711916 }
18721917 assert(return_type);
......@@ -2015,7 +2060,8 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import,
20152060 case NodeTypeStructField:
20162061 case NodeTypeStructValueExpr:
20172062 case NodeTypeStructValueField:
2018 case NodeTypeCompilerFnCall:
2063 case NodeTypeCompilerFnExpr:
2064 case NodeTypeCompilerFnType:
20192065 zig_unreachable();
20202066 }
20212067}
src/codegen.cpp+56-26
......@@ -1191,6 +1191,58 @@ static LLVMValueRef gen_var_decl_expr(CodeGen *g, AstNode *node) {
11911191 node->codegen_node->expr_node.block_context, false, &init_val);
11921192}
11931193
1194static LLVMValueRef gen_number_literal_raw(CodeGen *g, AstNode *source_node,
1195 NumberLiteralNode *codegen_num_lit, AstNodeNumberLiteral *num_lit_node)
1196{
1197 TypeTableEntry *type_entry = codegen_num_lit->resolved_type;
1198 assert(type_entry);
1199
1200 // override the expression type for number literals
1201 source_node->codegen_node->expr_node.type_entry = type_entry;
1202
1203 if (type_entry->id == TypeTableEntryIdInt) {
1204 // here the union has int64_t and uint64_t and we purposefully read
1205 // the uint64_t value in either case, because we want the twos
1206 // complement representation
1207
1208 return LLVMConstInt(type_entry->type_ref,
1209 num_lit_node->data.x_uint,
1210 type_entry->data.integral.is_signed);
1211 } else if (type_entry->id == TypeTableEntryIdFloat) {
1212
1213 return LLVMConstReal(type_entry->type_ref,
1214 num_lit_node->data.x_float);
1215 } else {
1216 zig_panic("bad number literal type");
1217 }
1218}
1219
1220static LLVMValueRef gen_compiler_fn_type(CodeGen *g, AstNode *node) {
1221 assert(node->type == NodeTypeCompilerFnType);
1222
1223 Buf *name = &node->data.compiler_fn_type.name;
1224 if (buf_eql_str(name, "sizeof")) {
1225 TypeTableEntry *type_entry = get_type_for_type_node(g, node->data.compiler_fn_type.type);
1226 NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
1227 AstNodeNumberLiteral num_lit_node;
1228 num_lit_node.kind = type_entry->data.num_lit.kind;
1229 num_lit_node.overflow = false;
1230 num_lit_node.data.x_uint = type_entry->size_in_bits / 8;
1231 return gen_number_literal_raw(g, node, codegen_num_lit, &num_lit_node);
1232 } else {
1233 zig_unreachable();
1234 }
1235}
1236
1237static LLVMValueRef gen_number_literal(CodeGen *g, AstNode *node) {
1238 assert(node->type == NodeTypeNumberLiteral);
1239
1240 NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
1241 assert(codegen_num_lit);
1242
1243 return gen_number_literal_raw(g, node, codegen_num_lit, &node->data.number_literal);
1244}
1245
11941246static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
11951247 switch (node->type) {
11961248 case NodeTypeBinOpExpr:
......@@ -1228,31 +1280,7 @@ static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
12281280 case NodeTypeAsmExpr:
12291281 return gen_asm_expr(g, node);
12301282 case NodeTypeNumberLiteral:
1231 {
1232 NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
1233 assert(codegen_num_lit);
1234 TypeTableEntry *type_entry = codegen_num_lit->resolved_type;
1235 assert(type_entry);
1236
1237 // override the expression type for number literals
1238 node->codegen_node->expr_node.type_entry = type_entry;
1239
1240 if (type_entry->id == TypeTableEntryIdInt) {
1241 // here the union has int64_t and uint64_t and we purposefully read
1242 // the uint64_t value in either case, because we want the twos
1243 // complement representation
1244
1245 return LLVMConstInt(type_entry->type_ref,
1246 node->data.number_literal.data.x_uint,
1247 type_entry->data.integral.is_signed);
1248 } else if (type_entry->id == TypeTableEntryIdFloat) {
1249
1250 return LLVMConstReal(type_entry->type_ref,
1251 node->data.number_literal.data.x_float);
1252 } else {
1253 zig_panic("bad number literal type");
1254 }
1255 }
1283 return gen_number_literal(g, node);
12561284 case NodeTypeStringLiteral:
12571285 {
12581286 Buf *str = &node->data.string_literal.buf;
......@@ -1313,6 +1341,8 @@ static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
13131341 }
13141342 case NodeTypeStructValueExpr:
13151343 return gen_struct_val_expr(g, node);
1344 case NodeTypeCompilerFnType:
1345 return gen_compiler_fn_type(g, node);
13161346 case NodeTypeRoot:
13171347 case NodeTypeRootExportDecl:
13181348 case NodeTypeFnProto:
......@@ -1326,7 +1356,7 @@ static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
13261356 case NodeTypeStructDecl:
13271357 case NodeTypeStructField:
13281358 case NodeTypeStructValueField:
1329 case NodeTypeCompilerFnCall:
1359 case NodeTypeCompilerFnExpr:
13301360 zig_unreachable();
13311361 }
13321362 zig_unreachable();
src/parser.cpp+47-12
......@@ -142,8 +142,10 @@ const char *node_type_str(NodeType node_type) {
142142 return "StructValueExpr";
143143 case NodeTypeStructValueField:
144144 return "StructValueField";
145 case NodeTypeCompilerFnCall:
146 return "CompilerFnCall";
145 case NodeTypeCompilerFnExpr:
146 return "CompilerFnExpr";
147 case NodeTypeCompilerFnType:
148 return "CompilerFnType";
147149 }
148150 zig_unreachable();
149151}
......@@ -410,7 +412,10 @@ void ast_print(AstNode *node, int indent) {
410412 fprintf(stderr, "%s '%s'\n", node_type_str(node->type), buf_ptr(&node->data.struct_val_field.name));
411413 ast_print(node->data.struct_val_field.expr, indent + 2);
412414 break;
413 case NodeTypeCompilerFnCall:
415 case NodeTypeCompilerFnExpr:
416 fprintf(stderr, "%s\n", node_type_str(node->type));
417 break;
418 case NodeTypeCompilerFnType:
414419 fprintf(stderr, "%s\n", node_type_str(node->type));
415420 break;
416421 }
......@@ -996,7 +1001,32 @@ static void ast_parse_type_assume_amp(ParseContext *pc, int *token_index, AstNod
9961001}
9971002
9981003/*
999CompileTimeFnCall : token(NumberSign) token(Symbol) token(LParen) Expression token(RParen)
1004CompilerFnType : token(NumberSign) token(Symbol) token(LParen) Expression token(RParen)
1005*/
1006static AstNode *ast_parse_compiler_fn_type(ParseContext *pc, int *token_index, bool mandatory) {
1007 Token *token = &pc->tokens->at(*token_index);
1008
1009 if (token->id == TokenIdNumberSign) {
1010 *token_index += 1;
1011 } else if (mandatory) {
1012 ast_invalid_token_error(pc, token);
1013 } else {
1014 return nullptr;
1015 }
1016
1017 Token *name_symbol = ast_eat_token(pc, token_index, TokenIdSymbol);
1018 ast_eat_token(pc, token_index, TokenIdLParen);
1019
1020 AstNode *node = ast_create_node(pc, NodeTypeCompilerFnType, token);
1021 ast_buf_from_token(pc, name_symbol, &node->data.compiler_fn_type.name);
1022 node->data.compiler_fn_type.type = ast_parse_type(pc, token_index);
1023
1024 ast_eat_token(pc, token_index, TokenIdRParen);
1025 return node;
1026}
1027
1028/*
1029CompilerFnExpr : token(NumberSign) token(Symbol) token(LParen) Expression token(RParen)
10001030*/
10011031static AstNode *ast_parse_compiler_fn_call(ParseContext *pc, int *token_index, bool mandatory) {
10021032 Token *token = &pc->tokens->at(*token_index);
......@@ -1012,16 +1042,16 @@ static AstNode *ast_parse_compiler_fn_call(ParseContext *pc, int *token_index, b
10121042 Token *name_symbol = ast_eat_token(pc, token_index, TokenIdSymbol);
10131043 ast_eat_token(pc, token_index, TokenIdLParen);
10141044
1015 AstNode *node = ast_create_node(pc, NodeTypeCompilerFnCall, token);
1016 ast_buf_from_token(pc, name_symbol, &node->data.compiler_fn_call.name);
1017 node->data.compiler_fn_call.expr = ast_parse_expression(pc, token_index, true);
1045 AstNode *node = ast_create_node(pc, NodeTypeCompilerFnExpr, token);
1046 ast_buf_from_token(pc, name_symbol, &node->data.compiler_fn_expr.name);
1047 node->data.compiler_fn_expr.expr = ast_parse_expression(pc, token_index, true);
10181048
10191049 ast_eat_token(pc, token_index, TokenIdRParen);
10201050 return node;
10211051}
10221052
10231053/*
1024Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompileTimeFnCall
1054Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompilerFnExpr
10251055PointerType : token(Ampersand) option(token(Const)) Type
10261056ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket)
10271057*/
......@@ -1029,10 +1059,10 @@ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
10291059 Token *token = &pc->tokens->at(*token_index);
10301060 AstNode *node = ast_create_node(pc, NodeTypeType, token);
10311061
1032 AstNode *compiler_fn_call = ast_parse_compiler_fn_call(pc, token_index, false);
1033 if (compiler_fn_call) {
1062 AstNode *compiler_fn_expr = ast_parse_compiler_fn_call(pc, token_index, false);
1063 if (compiler_fn_expr) {
10341064 node->data.type.type = AstNodeTypeTypeCompilerExpr;
1035 node->data.type.compiler_expr = compiler_fn_call;
1065 node->data.type.compiler_expr = compiler_fn_expr;
10361066 return node;
10371067 }
10381068
......@@ -1238,7 +1268,7 @@ static AstNode *ast_parse_struct_val_expr(ParseContext *pc, int *token_index) {
12381268}
12391269
12401270/*
1241PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | Goto | token(Break) | token(Continue) | BlockExpression | token(Symbol) | StructValueExpression
1271PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | Goto | token(Break) | token(Continue) | BlockExpression | token(Symbol) | StructValueExpression | CompilerFnType
12421272*/
12431273static AstNode *ast_parse_primary_expr(ParseContext *pc, int *token_index, bool mandatory) {
12441274 Token *token = &pc->tokens->at(*token_index);
......@@ -1317,6 +1347,11 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, int *token_index, bool
13171347 return block_expr_node;
13181348 }
13191349
1350 AstNode *compiler_fn_type = ast_parse_compiler_fn_type(pc, token_index, false);
1351 if (compiler_fn_type) {
1352 return compiler_fn_type;
1353 }
1354
13201355 if (!mandatory)
13211356 return nullptr;
13221357
src/parser.hpp+10-3
......@@ -57,7 +57,8 @@ enum NodeType {
5757 NodeTypeStructField,
5858 NodeTypeStructValueExpr,
5959 NodeTypeStructValueField,
60 NodeTypeCompilerFnCall,
60 NodeTypeCompilerFnExpr,
61 NodeTypeCompilerFnType,
6162};
6263
6364struct AstNodeRoot {
......@@ -332,11 +333,16 @@ struct AstNodeStructValueExpr {
332333 ZigList<AstNode *> fields;
333334};
334335
335struct AstNodeCompilerFnCall {
336struct AstNodeCompilerFnExpr {
336337 Buf name;
337338 AstNode *expr;
338339};
339340
341struct AstNodeCompilerFnType {
342 Buf name;
343 AstNode *type;
344};
345
340346struct AstNode {
341347 enum NodeType type;
342348 int line;
......@@ -376,7 +382,8 @@ struct AstNode {
376382 AstNodeNumberLiteral number_literal;
377383 AstNodeStructValueExpr struct_val_expr;
378384 AstNodeStructValueField struct_val_field;
379 AstNodeCompilerFnCall compiler_fn_call;
385 AstNodeCompilerFnExpr compiler_fn_expr;
386 AstNodeCompilerFnType compiler_fn_type;
380387 Buf symbol;
381388 bool bool_literal;
382389 } data;
test/run_tests.cpp+4-3
......@@ -698,16 +698,17 @@ fn outer() -> isize {
698698}
699699 )SOURCE", "OK\n");
700700
701 add_simple_case("#typeof()", R"SOURCE(
701 add_simple_case("#sizeof() and #typeof()", R"SOURCE(
702702use "std.zig";
703703const x: u16 = 13;
704704const z: #typeof(x) = 19;
705705pub fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
706706 const y: #typeof(x) = 120;
707 print_str("OK\n");
707 print_u64(#sizeof(#typeof(y)));
708 print_str("\n");
708709 return 0;
709710}
710 )SOURCE", "OK\n");
711 )SOURCE", "2\n");
711712}
712713
713714////////////////////////////////////////////////////////////////////////////////////