| ... | @@ -668,11 +668,19 @@ static bool c_is_float(Context *c, QualType qt) { | ... | @@ -668,11 +668,19 @@ static bool c_is_float(Context *c, QualType qt) { |
| 668 | } | 668 | } |
| 669 | | 669 | |
| 670 | static AstNode * trans_stmt(Context *c, AstNode *block, Stmt *stmt); | 670 | static AstNode * trans_stmt(Context *c, AstNode *block, Stmt *stmt); |
| | 671 | static AstNode * trans_create_node(Context *c, NodeType id); |
| | 672 | static AstNode * trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc); |
| 671 | | 673 | |
| 672 | static AstNode * trans_expr(Context *c, AstNode *block, Expr *expr) { | 674 | static AstNode * trans_expr(Context *c, AstNode *block, Expr *expr) { |
| 673 | return trans_stmt(c, block, expr); | 675 | return trans_stmt(c, block, expr); |
| 674 | } | 676 | } |
| 675 | | 677 | |
| | 678 | static AstNode *trans_create_symbol_node(Context *c, const char * name) { |
| | 679 | AstNode *node = trans_create_node(c, NodeTypeSymbol); |
| | 680 | node->data.symbol_expr.symbol = buf_create_from_str(name); |
| | 681 | return node; |
| | 682 | } |
| | 683 | |
| 676 | static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLocation &source_loc, | 684 | static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLocation &source_loc, |
| 677 | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> *type_table) | 685 | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> *type_table) |
| 678 | { | 686 | { |
| ... | @@ -682,43 +690,43 @@ static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLo | ... | @@ -682,43 +690,43 @@ static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLo |
| 682 | const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty); | 690 | const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty); |
| 683 | switch (builtin_ty->getKind()) { | 691 | switch (builtin_ty->getKind()) { |
| 684 | case BuiltinType::Void: | 692 | case BuiltinType::Void: |
| 685 | zig_panic("TODO void type"); | 693 | return trans_create_symbol_node(c, "c_void"); |
| 686 | case BuiltinType::Bool: | 694 | case BuiltinType::Bool: |
| 687 | zig_panic("TODO bool type"); | 695 | return trans_create_symbol_node(c, "bool"); |
| 688 | case BuiltinType::Char_U: | 696 | case BuiltinType::Char_U: |
| 689 | case BuiltinType::UChar: | 697 | case BuiltinType::UChar: |
| 690 | case BuiltinType::Char_S: | 698 | case BuiltinType::Char_S: |
| 691 | zig_panic("TODO u8 type"); | 699 | return trans_create_symbol_node(c, "u8"); |
| 692 | case BuiltinType::SChar: | 700 | case BuiltinType::SChar: |
| 693 | zig_panic("TODO i8 type"); | 701 | return trans_create_symbol_node(c, "i8"); |
| 694 | case BuiltinType::UShort: | 702 | case BuiltinType::UShort: |
| 695 | zig_panic("TODO c_ushort type"); | 703 | return trans_create_symbol_node(c, "c_ushort"); |
| 696 | case BuiltinType::UInt: | 704 | case BuiltinType::UInt: |
| 697 | zig_panic("TODO c_uint type"); | 705 | return trans_create_symbol_node(c, "c_uint"); |
| 698 | case BuiltinType::ULong: | 706 | case BuiltinType::ULong: |
| 699 | zig_panic("TODO c_ulong type"); | 707 | return trans_create_symbol_node(c, "c_ulong"); |
| 700 | case BuiltinType::ULongLong: | 708 | case BuiltinType::ULongLong: |
| 701 | zig_panic("TODO c_ulonglong type"); | 709 | return trans_create_symbol_node(c, "c_ulonglong"); |
| 702 | case BuiltinType::Short: | 710 | case BuiltinType::Short: |
| 703 | zig_panic("TODO c_short type"); | 711 | return trans_create_symbol_node(c, "c_short"); |
| 704 | case BuiltinType::Int: | 712 | case BuiltinType::Int: |
| 705 | zig_panic("TODO c_int type"); | 713 | return trans_create_symbol_node(c, "c_int"); |
| 706 | case BuiltinType::Long: | 714 | case BuiltinType::Long: |
| 707 | zig_panic("TODO c_long type"); | 715 | return trans_create_symbol_node(c, "c_long"); |
| 708 | case BuiltinType::LongLong: | 716 | case BuiltinType::LongLong: |
| 709 | zig_panic("TODO c_longlong type"); | 717 | return trans_create_symbol_node(c, "c_longlong"); |
| 710 | case BuiltinType::UInt128: | 718 | case BuiltinType::UInt128: |
| 711 | zig_panic("TODO u128 type"); | 719 | return trans_create_symbol_node(c, "u128"); |
| 712 | case BuiltinType::Int128: | 720 | case BuiltinType::Int128: |
| 713 | zig_panic("TODO i128 type"); | 721 | return trans_create_symbol_node(c, "i128"); |
| 714 | case BuiltinType::Float: | 722 | case BuiltinType::Float: |
| 715 | zig_panic("TODO f32 type"); | 723 | return trans_create_symbol_node(c, "f32"); |
| 716 | case BuiltinType::Double: | 724 | case BuiltinType::Double: |
| 717 | zig_panic("TODO f64 type"); | 725 | return trans_create_symbol_node(c, "f64"); |
| 718 | case BuiltinType::Float128: | 726 | case BuiltinType::Float128: |
| 719 | zig_panic("TODO f128 type"); | 727 | return trans_create_symbol_node(c, "f128"); |
| 720 | case BuiltinType::LongDouble: | 728 | case BuiltinType::LongDouble: |
| 721 | zig_panic("TODO c_longdouble type"); | 729 | return trans_create_symbol_node(c, "c_longdouble"); |
| 722 | case BuiltinType::WChar_U: | 730 | case BuiltinType::WChar_U: |
| 723 | case BuiltinType::Char16: | 731 | case BuiltinType::Char16: |
| 724 | case BuiltinType::Char32: | 732 | case BuiltinType::Char32: |
| ... | @@ -783,7 +791,26 @@ static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLo | ... | @@ -783,7 +791,26 @@ static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLo |
| 783 | break; | 791 | break; |
| 784 | } | 792 | } |
| 785 | case Type::Pointer: | 793 | case Type::Pointer: |
| 786 | zig_panic("TODO pointer"); | 794 | { |
| | 795 | const PointerType *pointer_ty = static_cast<const PointerType*>(ty); |
| | 796 | QualType child_qt = pointer_ty->getPointeeType(); |
| | 797 | AstNode *child_node = trans_qual_type(c, child_qt, source_loc); |
| | 798 | if (child_node == nullptr) return nullptr; |
| | 799 | |
| | 800 | if (qual_type_child_is_fn_proto(child_qt)) { |
| | 801 | zig_panic("TODO pointer to function proto"); |
| | 802 | } |
| | 803 | |
| | 804 | AstNode *pointer_node = trans_create_node(c, NodeTypeAddrOfExpr); |
| | 805 | pointer_node->data.addr_of_expr.is_const = child_qt.isConstQualified(); |
| | 806 | pointer_node->data.addr_of_expr.is_volatile = child_qt.isVolatileQualified(); |
| | 807 | pointer_node->data.addr_of_expr.op_expr = child_node; |
| | 808 | |
| | 809 | AstNode *maybe_node = trans_create_node(c, NodeTypePrefixOpExpr); |
| | 810 | maybe_node->data.prefix_op_expr.prefix_op = PrefixOpMaybe; |
| | 811 | maybe_node->data.prefix_op_expr.primary_expr = pointer_node; |
| | 812 | return maybe_node; |
| | 813 | } |
| 787 | case Type::Typedef: | 814 | case Type::Typedef: |
| 788 | zig_panic("TODO typedef"); | 815 | zig_panic("TODO typedef"); |
| 789 | case Type::Elaborated: | 816 | case Type::Elaborated: |
| ... | @@ -851,7 +878,7 @@ static AstNode * trans_qual_type(Context *c, QualType qt, const SourceLocation & | ... | @@ -851,7 +878,7 @@ static AstNode * trans_qual_type(Context *c, QualType qt, const SourceLocation & |
| 851 | return trans_qual_type_with_table(c, qt, source_loc, &c->global_type_table2); | 878 | return trans_qual_type_with_table(c, qt, source_loc, &c->global_type_table2); |
| 852 | } | 879 | } |
| 853 | | 880 | |
| 854 | static AstNode * trans_create_node(Context *c, Stmt *stmt, NodeType id) { | 881 | static AstNode * trans_create_node(Context *c, NodeType id) { |
| 855 | AstNode *node = allocate<AstNode>(1); | 882 | AstNode *node = allocate<AstNode>(1); |
| 856 | node->type = id; | 883 | node->type = id; |
| 857 | node->owner = c->import; | 884 | node->owner = c->import; |
| ... | @@ -860,7 +887,7 @@ static AstNode * trans_create_node(Context *c, Stmt *stmt, NodeType id) { | ... | @@ -860,7 +887,7 @@ static AstNode * trans_create_node(Context *c, Stmt *stmt, NodeType id) { |
| 860 | } | 887 | } |
| 861 | | 888 | |
| 862 | static AstNode * trans_compound_stmt(Context *c, AstNode *parent, CompoundStmt *stmt) { | 889 | static AstNode * trans_compound_stmt(Context *c, AstNode *parent, CompoundStmt *stmt) { |
| 863 | AstNode *child_block = trans_create_node(c, stmt, NodeTypeBlock); | 890 | AstNode *child_block = trans_create_node(c, NodeTypeBlock); |
| 864 | for (CompoundStmt::body_iterator it = stmt->body_begin(), end_it = stmt->body_end(); it != end_it; ++it) { | 891 | for (CompoundStmt::body_iterator it = stmt->body_begin(), end_it = stmt->body_end(); it != end_it; ++it) { |
| 865 | AstNode *child_node = trans_stmt(c, child_block, *it); | 892 | AstNode *child_node = trans_stmt(c, child_block, *it); |
| 866 | if (child_node != nullptr) | 893 | if (child_node != nullptr) |
| ... | @@ -874,7 +901,7 @@ static AstNode *trans_return_stmt(Context *c, AstNode *block, ReturnStmt *stmt) | ... | @@ -874,7 +901,7 @@ static AstNode *trans_return_stmt(Context *c, AstNode *block, ReturnStmt *stmt) |
| 874 | if (value_expr == nullptr) { | 901 | if (value_expr == nullptr) { |
| 875 | zig_panic("TODO handle C return void"); | 902 | zig_panic("TODO handle C return void"); |
| 876 | } else { | 903 | } else { |
| 877 | AstNode *return_node = trans_create_node(c, stmt, NodeTypeReturnExpr); | 904 | AstNode *return_node = trans_create_node(c, NodeTypeReturnExpr); |
| 878 | return_node->data.return_expr.expr = trans_expr(c, block, value_expr); | 905 | return_node->data.return_expr.expr = trans_expr(c, block, value_expr); |
| 879 | return return_node; | 906 | return return_node; |
| 880 | } | 907 | } |
| ... | @@ -898,7 +925,7 @@ static void aps_int_to_bigint(Context *c, const llvm::APSInt &aps_int, BigInt *b | ... | @@ -898,7 +925,7 @@ static void aps_int_to_bigint(Context *c, const llvm::APSInt &aps_int, BigInt *b |
| 898 | } | 925 | } |
| 899 | | 926 | |
| 900 | static AstNode * trans_integer_literal(Context *c, IntegerLiteral *stmt) { | 927 | static AstNode * trans_integer_literal(Context *c, IntegerLiteral *stmt) { |
| 901 | AstNode *node = trans_create_node(c, stmt, NodeTypeIntLiteral); | 928 | AstNode *node = trans_create_node(c, NodeTypeIntLiteral); |
| 902 | llvm::APSInt result; | 929 | llvm::APSInt result; |
| 903 | if (!stmt->EvaluateAsInt(result, *c->ctx)) { | 930 | if (!stmt->EvaluateAsInt(result, *c->ctx)) { |
| 904 | fprintf(stderr, "TODO unable to convert integer literal to zig\n"); | 931 | fprintf(stderr, "TODO unable to convert integer literal to zig\n"); |
| ... | @@ -909,7 +936,7 @@ static AstNode * trans_integer_literal(Context *c, IntegerLiteral *stmt) { | ... | @@ -909,7 +936,7 @@ static AstNode * trans_integer_literal(Context *c, IntegerLiteral *stmt) { |
| 909 | } | 936 | } |
| 910 | | 937 | |
| 911 | static AstNode * trans_conditional_operator(Context *c, AstNode *block, ConditionalOperator *stmt) { | 938 | static AstNode * trans_conditional_operator(Context *c, AstNode *block, ConditionalOperator *stmt) { |
| 912 | AstNode *node = trans_create_node(c, stmt, NodeTypeIfBoolExpr); | 939 | AstNode *node = trans_create_node(c, NodeTypeIfBoolExpr); |
| 913 | | 940 | |
| 914 | Expr *cond_expr = stmt->getCond(); | 941 | Expr *cond_expr = stmt->getCond(); |
| 915 | Expr *true_expr = stmt->getTrueExpr(); | 942 | Expr *true_expr = stmt->getTrueExpr(); |
| ... | @@ -944,7 +971,7 @@ static AstNode * trans_binary_operator(Context *c, AstNode *block, BinaryOperato | ... | @@ -944,7 +971,7 @@ static AstNode * trans_binary_operator(Context *c, AstNode *block, BinaryOperato |
| 944 | zig_panic("TODO handle more C binary operators: BO_Shr"); | 971 | zig_panic("TODO handle more C binary operators: BO_Shr"); |
| 945 | case BO_LT: | 972 | case BO_LT: |
| 946 | { | 973 | { |
| 947 | AstNode *node = trans_create_node(c, stmt, NodeTypeBinOpExpr); | 974 | AstNode *node = trans_create_node(c, NodeTypeBinOpExpr); |
| 948 | node->data.bin_op_expr.bin_op = BinOpTypeCmpLessThan; | 975 | node->data.bin_op_expr.bin_op = BinOpTypeCmpLessThan; |
| 949 | node->data.bin_op_expr.op1 = trans_expr(c, block, stmt->getLHS()); | 976 | node->data.bin_op_expr.op1 = trans_expr(c, block, stmt->getLHS()); |
| 950 | node->data.bin_op_expr.op2 = trans_expr(c, block, stmt->getRHS()); | 977 | node->data.bin_op_expr.op2 = trans_expr(c, block, stmt->getRHS()); |
| ... | @@ -1125,13 +1152,13 @@ static AstNode * trans_decl_ref_expr(Context *c, DeclRefExpr *stmt) { | ... | @@ -1125,13 +1152,13 @@ static AstNode * trans_decl_ref_expr(Context *c, DeclRefExpr *stmt) { |
| 1125 | ValueDecl *value_decl = stmt->getDecl(); | 1152 | ValueDecl *value_decl = stmt->getDecl(); |
| 1126 | const char *name = decl_name(value_decl); | 1153 | const char *name = decl_name(value_decl); |
| 1127 | | 1154 | |
| 1128 | AstNode *node = trans_create_node(c, stmt, NodeTypeSymbol); | 1155 | AstNode *node = trans_create_node(c, NodeTypeSymbol); |
| 1129 | node->data.symbol_expr.symbol = buf_create_from_str(name); | 1156 | node->data.symbol_expr.symbol = buf_create_from_str(name); |
| 1130 | return node; | 1157 | return node; |
| 1131 | } | 1158 | } |
| 1132 | | 1159 | |
| 1133 | static AstNode * trans_create_num_lit_node_unsigned(Context *c, Stmt *stmt, uint64_t x) { | 1160 | static AstNode * trans_create_num_lit_node_unsigned(Context *c, Stmt *stmt, uint64_t x) { |
| 1134 | AstNode *node = trans_create_node(c, stmt, NodeTypeIntLiteral); | 1161 | AstNode *node = trans_create_node(c, NodeTypeIntLiteral); |
| 1135 | node->data.int_literal.bigint = allocate<BigInt>(1); | 1162 | node->data.int_literal.bigint = allocate<BigInt>(1); |
| 1136 | bigint_init_unsigned(node->data.int_literal.bigint, x); | 1163 | bigint_init_unsigned(node->data.int_literal.bigint, x); |
| 1137 | return node; | 1164 | return node; |
| ... | @@ -1157,13 +1184,13 @@ static AstNode * trans_unary_operator(Context *c, AstNode *block, UnaryOperator | ... | @@ -1157,13 +1184,13 @@ static AstNode * trans_unary_operator(Context *c, AstNode *block, UnaryOperator |
| 1157 | { | 1184 | { |
| 1158 | Expr *op_expr = stmt->getSubExpr(); | 1185 | Expr *op_expr = stmt->getSubExpr(); |
| 1159 | if (c_is_signed_integer(c, op_expr->getType()) || c_is_float(c, op_expr->getType())) { | 1186 | if (c_is_signed_integer(c, op_expr->getType()) || c_is_float(c, op_expr->getType())) { |
| 1160 | AstNode *node = trans_create_node(c, stmt, NodeTypePrefixOpExpr); | 1187 | AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr); |
| 1161 | node->data.prefix_op_expr.prefix_op = PrefixOpNegation; | 1188 | node->data.prefix_op_expr.prefix_op = PrefixOpNegation; |
| 1162 | node->data.prefix_op_expr.primary_expr = trans_expr(c, block, op_expr); | 1189 | node->data.prefix_op_expr.primary_expr = trans_expr(c, block, op_expr); |
| 1163 | return node; | 1190 | return node; |
| 1164 | } else if (c_is_unsigned_integer(c, op_expr->getType())) { | 1191 | } else if (c_is_unsigned_integer(c, op_expr->getType())) { |
| 1165 | // we gotta emit 0 -% x | 1192 | // we gotta emit 0 -% x |
| 1166 | AstNode *node = trans_create_node(c, stmt, NodeTypeBinOpExpr); | 1193 | AstNode *node = trans_create_node(c, NodeTypeBinOpExpr); |
| 1167 | node->data.bin_op_expr.op1 = trans_create_num_lit_node_unsigned(c, stmt, 0); | 1194 | node->data.bin_op_expr.op1 = trans_create_num_lit_node_unsigned(c, stmt, 0); |
| 1168 | node->data.bin_op_expr.op2 = trans_expr(c, block, op_expr); | 1195 | node->data.bin_op_expr.op2 = trans_expr(c, block, op_expr); |
| 1169 | node->data.bin_op_expr.bin_op = BinOpTypeSubWrap; | 1196 | node->data.bin_op_expr.bin_op = BinOpTypeSubWrap; |
| ... | @@ -1194,7 +1221,7 @@ static AstNode * trans_local_declaration(Context *c, AstNode *block, DeclStmt *s | ... | @@ -1194,7 +1221,7 @@ static AstNode * trans_local_declaration(Context *c, AstNode *block, DeclStmt *s |
| 1194 | switch (decl->getKind()) { | 1221 | switch (decl->getKind()) { |
| 1195 | case Decl::Var: { | 1222 | case Decl::Var: { |
| 1196 | VarDecl *var_decl = (VarDecl *)decl; | 1223 | VarDecl *var_decl = (VarDecl *)decl; |
| 1197 | AstNode *node = trans_create_node(c, stmt, NodeTypeVariableDeclaration); | 1224 | AstNode *node = trans_create_node(c, NodeTypeVariableDeclaration); |
| 1198 | node->data.variable_declaration.symbol = buf_create_from_str(decl_name(var_decl)); | 1225 | node->data.variable_declaration.symbol = buf_create_from_str(decl_name(var_decl)); |
| 1199 | QualType qual_type = var_decl->getTypeSourceInfo()->getType(); | 1226 | QualType qual_type = var_decl->getTypeSourceInfo()->getType(); |
| 1200 | node->data.variable_declaration.is_const = qual_type.isConstQualified(); | 1227 | node->data.variable_declaration.is_const = qual_type.isConstQualified(); |