| ... | ... | @@ -664,10 +664,10 @@ static bool c_is_float(Context *c, QualType qt) { |
| 664 | 664 | } |
| 665 | 665 | } |
| 666 | 666 | |
| 667 | | static AstNode * trans_stmt(Context *c, Stmt *stmt); |
| 667 | static AstNode * trans_stmt(Context *c, AstNode *block, Stmt *stmt); |
| 668 | 668 | |
| 669 | | static AstNode * trans_expr(Context *c, Expr *expr) { |
| 670 | | return trans_stmt(c, expr); |
| 669 | static AstNode * trans_expr(Context *c, AstNode *block, Expr *expr) { |
| 670 | return trans_stmt(c, block, expr); |
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | static AstNode * trans_create_node(Context *c, Stmt *stmt, NodeType id) { |
| ... | ... | @@ -678,22 +678,23 @@ static AstNode * trans_create_node(Context *c, Stmt *stmt, NodeType id) { |
| 678 | 678 | return node; |
| 679 | 679 | } |
| 680 | 680 | |
| 681 | | static AstNode * trans_compound_stmt(Context *c, CompoundStmt *stmt) { |
| 682 | | AstNode *block_node = trans_create_node(c, stmt, NodeTypeBlock); |
| 681 | static AstNode * trans_compound_stmt(Context *c, AstNode *parent, CompoundStmt *stmt) { |
| 682 | AstNode *child_block = trans_create_node(c, stmt, NodeTypeBlock); |
| 683 | 683 | for (CompoundStmt::body_iterator it = stmt->body_begin(), end_it = stmt->body_end(); it != end_it; ++it) { |
| 684 | | AstNode *child_node = trans_stmt(c, *it); |
| 685 | | block_node->data.block.statements.append(child_node); |
| 684 | AstNode *child_node = trans_stmt(c, child_block, *it); |
| 685 | if (child_node != nullptr) |
| 686 | child_block->data.block.statements.append(child_node); |
| 686 | 687 | } |
| 687 | | return block_node; |
| 688 | return child_block; |
| 688 | 689 | } |
| 689 | 690 | |
| 690 | | static AstNode *trans_return_stmt(Context *c, ReturnStmt *stmt) { |
| 691 | static AstNode *trans_return_stmt(Context *c, AstNode *block, ReturnStmt *stmt) { |
| 691 | 692 | Expr *value_expr = stmt->getRetValue(); |
| 692 | 693 | if (value_expr == nullptr) { |
| 693 | 694 | zig_panic("TODO handle C return void"); |
| 694 | 695 | } else { |
| 695 | 696 | AstNode *return_node = trans_create_node(c, stmt, NodeTypeReturnExpr); |
| 696 | | return_node->data.return_expr.expr = trans_expr(c, value_expr); |
| 697 | return_node->data.return_expr.expr = trans_expr(c, block, value_expr); |
| 697 | 698 | return return_node; |
| 698 | 699 | } |
| 699 | 700 | } |
| ... | ... | @@ -726,21 +727,21 @@ static AstNode * trans_integer_literal(Context *c, IntegerLiteral *stmt) { |
| 726 | 727 | return node; |
| 727 | 728 | } |
| 728 | 729 | |
| 729 | | static AstNode * trans_conditional_operator(Context *c, ConditionalOperator *stmt) { |
| 730 | static AstNode * trans_conditional_operator(Context *c, AstNode *block, ConditionalOperator *stmt) { |
| 730 | 731 | AstNode *node = trans_create_node(c, stmt, NodeTypeIfBoolExpr); |
| 731 | 732 | |
| 732 | 733 | Expr *cond_expr = stmt->getCond(); |
| 733 | 734 | Expr *true_expr = stmt->getTrueExpr(); |
| 734 | 735 | Expr *false_expr = stmt->getFalseExpr(); |
| 735 | 736 | |
| 736 | | node->data.if_bool_expr.condition = trans_expr(c, cond_expr); |
| 737 | | node->data.if_bool_expr.then_block = trans_expr(c, true_expr); |
| 738 | | node->data.if_bool_expr.else_node = trans_expr(c, false_expr); |
| 737 | node->data.if_bool_expr.condition = trans_expr(c, block, cond_expr); |
| 738 | node->data.if_bool_expr.then_block = trans_expr(c, block, true_expr); |
| 739 | node->data.if_bool_expr.else_node = trans_expr(c, block, false_expr); |
| 739 | 740 | |
| 740 | 741 | return node; |
| 741 | 742 | } |
| 742 | 743 | |
| 743 | | static AstNode * trans_binary_operator(Context *c, BinaryOperator *stmt) { |
| 744 | static AstNode * trans_binary_operator(Context *c, AstNode *block, BinaryOperator *stmt) { |
| 744 | 745 | switch (stmt->getOpcode()) { |
| 745 | 746 | case BO_PtrMemD: |
| 746 | 747 | zig_panic("TODO handle more C binary operators: BO_PtrMemD"); |
| ... | ... | @@ -764,8 +765,8 @@ static AstNode * trans_binary_operator(Context *c, BinaryOperator *stmt) { |
| 764 | 765 | { |
| 765 | 766 | AstNode *node = trans_create_node(c, stmt, NodeTypeBinOpExpr); |
| 766 | 767 | node->data.bin_op_expr.bin_op = BinOpTypeCmpLessThan; |
| 767 | | node->data.bin_op_expr.op1 = trans_expr(c, stmt->getLHS()); |
| 768 | | node->data.bin_op_expr.op2 = trans_expr(c, stmt->getRHS()); |
| 768 | node->data.bin_op_expr.op1 = trans_expr(c, block, stmt->getLHS()); |
| 769 | node->data.bin_op_expr.op2 = trans_expr(c, block, stmt->getRHS()); |
| 769 | 770 | return node; |
| 770 | 771 | } |
| 771 | 772 | case BO_GT: |
| ... | ... | @@ -817,10 +818,10 @@ static AstNode * trans_binary_operator(Context *c, BinaryOperator *stmt) { |
| 817 | 818 | zig_unreachable(); |
| 818 | 819 | } |
| 819 | 820 | |
| 820 | | static AstNode * trans_implicit_cast_expr(Context *c, ImplicitCastExpr *stmt) { |
| 821 | static AstNode * trans_implicit_cast_expr(Context *c, AstNode *block, ImplicitCastExpr *stmt) { |
| 821 | 822 | switch (stmt->getCastKind()) { |
| 822 | 823 | case CK_LValueToRValue: |
| 823 | | return trans_expr(c, stmt->getSubExpr()); |
| 824 | return trans_expr(c, block, stmt->getSubExpr()); |
| 824 | 825 | case CK_IntegralCast: |
| 825 | 826 | zig_panic("TODO handle C translation cast CK_IntegralCast"); |
| 826 | 827 | case CK_Dependent: |
| ... | ... | @@ -955,7 +956,7 @@ static AstNode * trans_create_num_lit_node_unsigned(Context *c, Stmt *stmt, uint |
| 955 | 956 | return node; |
| 956 | 957 | } |
| 957 | 958 | |
| 958 | | static AstNode * trans_unary_operator(Context *c, UnaryOperator *stmt) { |
| 959 | static AstNode * trans_unary_operator(Context *c, AstNode *block, UnaryOperator *stmt) { |
| 959 | 960 | switch (stmt->getOpcode()) { |
| 960 | 961 | case UO_PostInc: |
| 961 | 962 | zig_panic("TODO handle C translation UO_PostInc"); |
| ... | ... | @@ -977,13 +978,13 @@ static AstNode * trans_unary_operator(Context *c, UnaryOperator *stmt) { |
| 977 | 978 | if (c_is_signed_integer(c, op_expr->getType()) || c_is_float(c, op_expr->getType())) { |
| 978 | 979 | AstNode *node = trans_create_node(c, stmt, NodeTypePrefixOpExpr); |
| 979 | 980 | node->data.prefix_op_expr.prefix_op = PrefixOpNegation; |
| 980 | | node->data.prefix_op_expr.primary_expr = trans_expr(c, op_expr); |
| 981 | node->data.prefix_op_expr.primary_expr = trans_expr(c, block, op_expr); |
| 981 | 982 | return node; |
| 982 | 983 | } else if (c_is_unsigned_integer(c, op_expr->getType())) { |
| 983 | 984 | // we gotta emit 0 -% x |
| 984 | 985 | AstNode *node = trans_create_node(c, stmt, NodeTypeBinOpExpr); |
| 985 | 986 | node->data.bin_op_expr.op1 = trans_create_num_lit_node_unsigned(c, stmt, 0); |
| 986 | | node->data.bin_op_expr.op2 = trans_expr(c, op_expr); |
| 987 | node->data.bin_op_expr.op2 = trans_expr(c, block, op_expr); |
| 987 | 988 | node->data.bin_op_expr.bin_op = BinOpTypeSubWrap; |
| 988 | 989 | return node; |
| 989 | 990 | } else { |
| ... | ... | @@ -1006,25 +1007,200 @@ static AstNode * trans_unary_operator(Context *c, UnaryOperator *stmt) { |
| 1006 | 1007 | zig_unreachable(); |
| 1007 | 1008 | } |
| 1008 | 1009 | |
| 1009 | | static AstNode *trans_stmt(Context *c, Stmt *stmt) { |
| 1010 | static AstNode * trans_local_declaration(Context *c, AstNode *block, DeclStmt *stmt) { |
| 1011 | for (auto iter = stmt->decl_begin(); iter != stmt->decl_end(); iter++) { |
| 1012 | Decl *decl = *iter; |
| 1013 | switch (decl->getKind()) { |
| 1014 | case Decl::Var: { |
| 1015 | VarDecl *var_decl = (VarDecl *)decl; |
| 1016 | AstNode *node = trans_create_node(c, stmt, NodeTypeVariableDeclaration); |
| 1017 | node->data.variable_declaration.symbol = buf_create_from_str(decl_name(var_decl)); |
| 1018 | QualType qual_type = var_decl->getTypeSourceInfo()->getType(); |
| 1019 | node->data.variable_declaration.is_const = qual_type.isConstQualified(); |
| 1020 | node->data.variable_declaration.type = trans_qual_type(c, block, qual_type); |
| 1021 | if (var_decl->hasInit()) { |
| 1022 | node->data.variable_declaration.expr = trans_expr(c, block, var_decl->getInit()); |
| 1023 | } |
| 1024 | |
| 1025 | //emit_warning(c, decl->getLocation(), "asdf"); |
| 1026 | |
| 1027 | block->data.block.statements.append(node); |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | case Decl::AccessSpec: |
| 1032 | zig_panic("TODO handle decl kind AccessSpec"); |
| 1033 | case Decl::Block: |
| 1034 | zig_panic("TODO handle decl kind Block"); |
| 1035 | case Decl::Captured: |
| 1036 | zig_panic("TODO handle decl kind Captured"); |
| 1037 | case Decl::ClassScopeFunctionSpecialization: |
| 1038 | zig_panic("TODO handle decl kind ClassScopeFunctionSpecialization"); |
| 1039 | case Decl::Empty: |
| 1040 | zig_panic("TODO handle decl kind Empty"); |
| 1041 | case Decl::Export: |
| 1042 | zig_panic("TODO handle decl kind Export"); |
| 1043 | case Decl::ExternCContext: |
| 1044 | zig_panic("TODO handle decl kind ExternCContext"); |
| 1045 | case Decl::FileScopeAsm: |
| 1046 | zig_panic("TODO handle decl kind FileScopeAsm"); |
| 1047 | case Decl::Friend: |
| 1048 | zig_panic("TODO handle decl kind Friend"); |
| 1049 | case Decl::FriendTemplate: |
| 1050 | zig_panic("TODO handle decl kind FriendTemplate"); |
| 1051 | case Decl::Import: |
| 1052 | zig_panic("TODO handle decl kind Import"); |
| 1053 | case Decl::LinkageSpec: |
| 1054 | zig_panic("TODO handle decl kind LinkageSpec"); |
| 1055 | case Decl::Label: |
| 1056 | zig_panic("TODO handle decl kind Label"); |
| 1057 | case Decl::Namespace: |
| 1058 | zig_panic("TODO handle decl kind Namespace"); |
| 1059 | case Decl::NamespaceAlias: |
| 1060 | zig_panic("TODO handle decl kind NamespaceAlias"); |
| 1061 | case Decl::ObjCCompatibleAlias: |
| 1062 | zig_panic("TODO handle decl kind ObjCCompatibleAlias"); |
| 1063 | case Decl::ObjCCategory: |
| 1064 | zig_panic("TODO handle decl kind ObjCCategory"); |
| 1065 | case Decl::ObjCCategoryImpl: |
| 1066 | zig_panic("TODO handle decl kind ObjCCategoryImpl"); |
| 1067 | case Decl::ObjCImplementation: |
| 1068 | zig_panic("TODO handle decl kind ObjCImplementation"); |
| 1069 | case Decl::ObjCInterface: |
| 1070 | zig_panic("TODO handle decl kind ObjCInterface"); |
| 1071 | case Decl::ObjCProtocol: |
| 1072 | zig_panic("TODO handle decl kind ObjCProtocol"); |
| 1073 | case Decl::ObjCMethod: |
| 1074 | zig_panic("TODO handle decl kind ObjCMethod"); |
| 1075 | case Decl::ObjCProperty: |
| 1076 | zig_panic("TODO handle decl kind ObjCProperty"); |
| 1077 | case Decl::BuiltinTemplate: |
| 1078 | zig_panic("TODO handle decl kind BuiltinTemplate"); |
| 1079 | case Decl::ClassTemplate: |
| 1080 | zig_panic("TODO handle decl kind ClassTemplate"); |
| 1081 | case Decl::FunctionTemplate: |
| 1082 | zig_panic("TODO handle decl kind FunctionTemplate"); |
| 1083 | case Decl::TypeAliasTemplate: |
| 1084 | zig_panic("TODO handle decl kind TypeAliasTemplate"); |
| 1085 | case Decl::VarTemplate: |
| 1086 | zig_panic("TODO handle decl kind VarTemplate"); |
| 1087 | case Decl::TemplateTemplateParm: |
| 1088 | zig_panic("TODO handle decl kind TemplateTemplateParm"); |
| 1089 | case Decl::Enum: |
| 1090 | zig_panic("TODO handle decl kind Enum"); |
| 1091 | case Decl::Record: |
| 1092 | zig_panic("TODO handle decl kind Record"); |
| 1093 | case Decl::CXXRecord: |
| 1094 | zig_panic("TODO handle decl kind CXXRecord"); |
| 1095 | case Decl::ClassTemplateSpecialization: |
| 1096 | zig_panic("TODO handle decl kind ClassTemplateSpecialization"); |
| 1097 | case Decl::ClassTemplatePartialSpecialization: |
| 1098 | zig_panic("TODO handle decl kind ClassTemplatePartialSpecialization"); |
| 1099 | case Decl::TemplateTypeParm: |
| 1100 | zig_panic("TODO handle decl kind TemplateTypeParm"); |
| 1101 | case Decl::ObjCTypeParam: |
| 1102 | zig_panic("TODO handle decl kind ObjCTypeParam"); |
| 1103 | case Decl::TypeAlias: |
| 1104 | zig_panic("TODO handle decl kind TypeAlias"); |
| 1105 | case Decl::Typedef: |
| 1106 | zig_panic("TODO handle decl kind Typedef"); |
| 1107 | case Decl::UnresolvedUsingTypename: |
| 1108 | zig_panic("TODO handle decl kind UnresolvedUsingTypename"); |
| 1109 | case Decl::Using: |
| 1110 | zig_panic("TODO handle decl kind Using"); |
| 1111 | case Decl::UsingDirective: |
| 1112 | zig_panic("TODO handle decl kind UsingDirective"); |
| 1113 | case Decl::UsingPack: |
| 1114 | zig_panic("TODO handle decl kind UsingPack"); |
| 1115 | case Decl::UsingShadow: |
| 1116 | zig_panic("TODO handle decl kind UsingShadow"); |
| 1117 | case Decl::ConstructorUsingShadow: |
| 1118 | zig_panic("TODO handle decl kind ConstructorUsingShadow"); |
| 1119 | case Decl::Binding: |
| 1120 | zig_panic("TODO handle decl kind Binding"); |
| 1121 | case Decl::Field: |
| 1122 | zig_panic("TODO handle decl kind Field"); |
| 1123 | case Decl::ObjCAtDefsField: |
| 1124 | zig_panic("TODO handle decl kind ObjCAtDefsField"); |
| 1125 | case Decl::ObjCIvar: |
| 1126 | zig_panic("TODO handle decl kind ObjCIvar"); |
| 1127 | case Decl::Function: |
| 1128 | zig_panic("TODO handle decl kind Function"); |
| 1129 | case Decl::CXXDeductionGuide: |
| 1130 | zig_panic("TODO handle decl kind CXXDeductionGuide"); |
| 1131 | case Decl::CXXMethod: |
| 1132 | zig_panic("TODO handle decl kind CXXMethod"); |
| 1133 | case Decl::CXXConstructor: |
| 1134 | zig_panic("TODO handle decl kind CXXConstructor"); |
| 1135 | case Decl::CXXConversion: |
| 1136 | zig_panic("TODO handle decl kind CXXConversion"); |
| 1137 | case Decl::CXXDestructor: |
| 1138 | zig_panic("TODO handle decl kind CXXDestructor"); |
| 1139 | case Decl::MSProperty: |
| 1140 | zig_panic("TODO handle decl kind MSProperty"); |
| 1141 | case Decl::NonTypeTemplateParm: |
| 1142 | zig_panic("TODO handle decl kind NonTypeTemplateParm"); |
| 1143 | case Decl::Decomposition: |
| 1144 | zig_panic("TODO handle decl kind Decomposition"); |
| 1145 | case Decl::ImplicitParam: |
| 1146 | zig_panic("TODO handle decl kind ImplicitParam"); |
| 1147 | case Decl::OMPCapturedExpr: |
| 1148 | zig_panic("TODO handle decl kind OMPCapturedExpr"); |
| 1149 | case Decl::ParmVar: |
| 1150 | zig_panic("TODO handle decl kind ParmVar"); |
| 1151 | case Decl::VarTemplateSpecialization: |
| 1152 | zig_panic("TODO handle decl kind VarTemplateSpecialization"); |
| 1153 | case Decl::VarTemplatePartialSpecialization: |
| 1154 | zig_panic("TODO handle decl kind VarTemplatePartialSpecialization"); |
| 1155 | case Decl::EnumConstant: |
| 1156 | zig_panic("TODO handle decl kind EnumConstant"); |
| 1157 | case Decl::IndirectField: |
| 1158 | zig_panic("TODO handle decl kind IndirectField"); |
| 1159 | case Decl::OMPDeclareReduction: |
| 1160 | zig_panic("TODO handle decl kind OMPDeclareReduction"); |
| 1161 | case Decl::UnresolvedUsingValue: |
| 1162 | zig_panic("TODO handle decl kind UnresolvedUsingValue"); |
| 1163 | case Decl::OMPThreadPrivate: |
| 1164 | zig_panic("TODO handle decl kind OMPThreadPrivate"); |
| 1165 | case Decl::ObjCPropertyImpl: |
| 1166 | zig_panic("TODO handle decl kind ObjCPropertyImpl"); |
| 1167 | case Decl::PragmaComment: |
| 1168 | zig_panic("TODO handle decl kind PragmaComment"); |
| 1169 | case Decl::PragmaDetectMismatch: |
| 1170 | zig_panic("TODO handle decl kind PragmaDetectMismatch"); |
| 1171 | case Decl::StaticAssert: |
| 1172 | zig_panic("TODO handle decl kind StaticAssert"); |
| 1173 | case Decl::TranslationUnit: |
| 1174 | zig_panic("TODO handle decl kind TranslationUnit"); |
| 1175 | } |
| 1176 | zig_unreachable(); |
| 1177 | } |
| 1178 | |
| 1179 | // declarations were already added |
| 1180 | return nullptr; |
| 1181 | } |
| 1182 | |
| 1183 | static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) { |
| 1010 | 1184 | Stmt::StmtClass sc = stmt->getStmtClass(); |
| 1011 | 1185 | switch (sc) { |
| 1012 | 1186 | case Stmt::ReturnStmtClass: |
| 1013 | | return trans_return_stmt(c, (ReturnStmt *)stmt); |
| 1187 | return trans_return_stmt(c, block, (ReturnStmt *)stmt); |
| 1014 | 1188 | case Stmt::CompoundStmtClass: |
| 1015 | | return trans_compound_stmt(c, (CompoundStmt *)stmt); |
| 1189 | return trans_compound_stmt(c, block, (CompoundStmt *)stmt); |
| 1016 | 1190 | case Stmt::IntegerLiteralClass: |
| 1017 | 1191 | return trans_integer_literal(c, (IntegerLiteral *)stmt); |
| 1018 | 1192 | case Stmt::ConditionalOperatorClass: |
| 1019 | | return trans_conditional_operator(c, (ConditionalOperator *)stmt); |
| 1193 | return trans_conditional_operator(c, block, (ConditionalOperator *)stmt); |
| 1020 | 1194 | case Stmt::BinaryOperatorClass: |
| 1021 | | return trans_binary_operator(c, (BinaryOperator *)stmt); |
| 1195 | return trans_binary_operator(c, block, (BinaryOperator *)stmt); |
| 1022 | 1196 | case Stmt::ImplicitCastExprClass: |
| 1023 | | return trans_implicit_cast_expr(c, (ImplicitCastExpr *)stmt); |
| 1197 | return trans_implicit_cast_expr(c, block, (ImplicitCastExpr *)stmt); |
| 1024 | 1198 | case Stmt::DeclRefExprClass: |
| 1025 | 1199 | return trans_decl_ref_expr(c, (DeclRefExpr *)stmt); |
| 1026 | 1200 | case Stmt::UnaryOperatorClass: |
| 1027 | | return trans_unary_operator(c, (UnaryOperator *)stmt); |
| 1201 | return trans_unary_operator(c, block, (UnaryOperator *)stmt); |
| 1202 | case Stmt::DeclStmtClass: |
| 1203 | return trans_local_declaration(c, block, (DeclStmt *)stmt); |
| 1028 | 1204 | case Stmt::CaseStmtClass: |
| 1029 | 1205 | zig_panic("TODO handle C CaseStmtClass"); |
| 1030 | 1206 | case Stmt::DefaultStmtClass: |
| ... | ... | @@ -1057,8 +1233,6 @@ static AstNode *trans_stmt(Context *c, Stmt *stmt) { |
| 1057 | 1233 | zig_panic("TODO handle C CoreturnStmtClass"); |
| 1058 | 1234 | case Stmt::CoroutineBodyStmtClass: |
| 1059 | 1235 | zig_panic("TODO handle C CoroutineBodyStmtClass"); |
| 1060 | | case Stmt::DeclStmtClass: |
| 1061 | | zig_panic("TODO handle C DeclStmtClass"); |
| 1062 | 1236 | case Stmt::DoStmtClass: |
| 1063 | 1237 | zig_panic("TODO handle C DoStmtClass"); |
| 1064 | 1238 | case Stmt::BinaryConditionalOperatorClass: |
| ... | ... | @@ -1409,7 +1583,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) { |
| 1409 | 1583 | if (fn_decl->hasBody()) { |
| 1410 | 1584 | fprintf(stderr, "fn %s\n", buf_ptr(fn_name)); |
| 1411 | 1585 | Stmt *body = fn_decl->getBody(); |
| 1412 | | AstNode *body_node = trans_stmt(c, body); |
| 1586 | AstNode *body_node = trans_stmt(c, nullptr, body); |
| 1413 | 1587 | ast_render(c->codegen, stderr, body_node, 4); |
| 1414 | 1588 | fprintf(stderr, "\n"); |
| 1415 | 1589 | } |