| ... | ... | @@ -2194,43 +2194,63 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) { |
| 2194 | 2194 | return; |
| 2195 | 2195 | } |
| 2196 | 2196 | |
| 2197 | | ZigList<Buf*> fake_names; |
| 2198 | 2197 | for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) { |
| 2199 | 2198 | AstNode *param_node = proto_node->data.fn_proto.params.at(i); |
| 2200 | 2199 | const ParmVarDecl *param = fn_decl->getParamDecl(i); |
| 2201 | 2200 | const char *name = decl_name(param); |
| 2202 | | fake_names.append(buf_create_from_str(name)); |
| 2203 | | buf_append_char(fake_names.at(i), '_'); |
| 2204 | | if (strlen(name) == 0) { |
| 2205 | | Buf *proto_param_name = param_node->data.param_decl.name; |
| 2201 | Buf *proto_param_name; |
| 2202 | if (strlen(name) != 0) { |
| 2203 | proto_param_name = buf_create_from_str(name); |
| 2204 | } else { |
| 2205 | proto_param_name = param_node->data.param_decl.name; |
| 2206 | 2206 | if (proto_param_name == nullptr) { |
| 2207 | | param_node->data.param_decl.name = buf_sprintf("arg%" ZIG_PRI_usize "", i); |
| 2208 | | } else { |
| 2209 | | param_node->data.param_decl.name = proto_param_name; |
| 2207 | proto_param_name = buf_sprintf("arg%" ZIG_PRI_usize "", i); |
| 2210 | 2208 | } |
| 2211 | | } else { |
| 2212 | | param_node->data.param_decl.name = buf_create_from_str(name); |
| 2213 | 2209 | } |
| 2210 | param_node->data.param_decl.name = proto_param_name; |
| 2214 | 2211 | } |
| 2215 | 2212 | |
| 2216 | | if (fn_decl->hasBody()) { |
| 2217 | | Stmt *body = fn_decl->getBody(); |
| 2213 | if (!fn_decl->hasBody()) { |
| 2214 | // just a prototype |
| 2215 | c->root->data.root.top_level_decls.append(proto_node); |
| 2216 | return; |
| 2217 | } |
| 2218 | 2218 | |
| 2219 | | AstNode *fn_def_node = trans_create_node(c, NodeTypeFnDef); |
| 2220 | | fn_def_node->data.fn_def.fn_proto = proto_node; |
| 2221 | | fn_def_node->data.fn_def.body = trans_stmt(c, nullptr, body); |
| 2222 | | assert(fn_def_node->data.fn_def.body != skip_add_to_block_node); |
| 2223 | | if (fn_def_node->data.fn_def.body == nullptr) { |
| 2224 | | emit_warning(c, fn_decl->getLocation(), "unable to translate function"); |
| 2225 | | return; |
| 2226 | | } |
| 2219 | // actual function definition with body |
| 2227 | 2220 | |
| 2228 | | proto_node->data.fn_proto.fn_def_node = fn_def_node; |
| 2229 | | c->root->data.root.top_level_decls.append(fn_def_node); |
| 2221 | Stmt *body = fn_decl->getBody(); |
| 2222 | AstNode *actual_body_node = trans_stmt(c, nullptr, body); |
| 2223 | assert(actual_body_node != skip_add_to_block_node); |
| 2224 | if (actual_body_node == nullptr) { |
| 2225 | emit_warning(c, fn_decl->getLocation(), "unable to translate function"); |
| 2230 | 2226 | return; |
| 2231 | 2227 | } |
| 2232 | 2228 | |
| 2233 | | c->root->data.root.top_level_decls.append(proto_node); |
| 2229 | // it worked |
| 2230 | |
| 2231 | AstNode *parameter_init_block = trans_create_node(c, NodeTypeBlock); |
| 2232 | |
| 2233 | for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) { |
| 2234 | AstNode *param_node = proto_node->data.fn_proto.params.at(i); |
| 2235 | Buf *good_name = param_node->data.param_decl.name; |
| 2236 | // TODO: avoid name collisions |
| 2237 | Buf *mangled_name = buf_sprintf("_arg_%s", buf_ptr(good_name)); |
| 2238 | param_node->data.param_decl.name = mangled_name; |
| 2239 | |
| 2240 | // var c_name = _mangled_name; |
| 2241 | AstNode *parameter_init = trans_create_node_var_decl_local(c, false, good_name, nullptr, trans_create_node_symbol(c, mangled_name)); |
| 2242 | |
| 2243 | parameter_init_block->data.block.statements.append(parameter_init); |
| 2244 | } |
| 2245 | |
| 2246 | parameter_init_block->data.block.statements.append(actual_body_node); |
| 2247 | |
| 2248 | AstNode *fn_def_node = trans_create_node(c, NodeTypeFnDef); |
| 2249 | fn_def_node->data.fn_def.fn_proto = proto_node; |
| 2250 | fn_def_node->data.fn_def.body = parameter_init_block; |
| 2251 | |
| 2252 | proto_node->data.fn_proto.fn_def_node = fn_def_node; |
| 2253 | c->root->data.root.top_level_decls.append(fn_def_node); |
| 2234 | 2254 | } |
| 2235 | 2255 | |
| 2236 | 2256 | static AstNode *resolve_typdef_as_builtin(Context *c, const TypedefNameDecl *typedef_decl, const char *primitive_name) { |