authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-09-11 20:39:42-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-09-11 20:39:42-07:00
log99cb6e955a4a044a19f98abd1d486560ee9c9729
tree4f11d0100b2ce8d227d543e96f7915688fce82ec
parente52418097d908f6f0b96513395752405329caf4e

oh, actually it was


1 files changed, 43 insertions(+), 23 deletions(-)

src/parsec.cpp+43-23
......@@ -2194,43 +2194,63 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
21942194 return;
21952195 }
21962196
2197 ZigList<Buf*> fake_names;
21982197 for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) {
21992198 AstNode *param_node = proto_node->data.fn_proto.params.at(i);
22002199 const ParmVarDecl *param = fn_decl->getParamDecl(i);
22012200 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;
22062206 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);
22102208 }
2211 } else {
2212 param_node->data.param_decl.name = buf_create_from_str(name);
22132209 }
2210 param_node->data.param_decl.name = proto_param_name;
22142211 }
22152212
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 }
22182218
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
22272220
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");
22302226 return;
22312227 }
22322228
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);
22342254}
22352255
22362256static AstNode *resolve_typdef_as_builtin(Context *c, const TypedefNameDecl *typedef_decl, const char *primitive_name) {