authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-28 21:38:01-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-28 21:38:01-07:00
logc3516b80044732e18eedcd8cd49c424fec497bb3
treed629ea5cf6440c2f98434bffcd706f93799da4ae
parenta5c2de5fee67e35c8173b7051675d49648086cbb

parseh handles self referential structs and fn pointers


3 files changed, 80 insertions(+), 19 deletions(-)

src/ast_render.cpp+6-4
......@@ -509,7 +509,8 @@ static void render_node(AstRender *ar, AstNode *node) {
509509
510510 if (child->type == NodeTypeImport ||
511511 child->type == NodeTypeVariableDeclaration ||
512 child->type == NodeTypeErrorValueDecl)
512 child->type == NodeTypeErrorValueDecl ||
513 child->type == NodeTypeFnProto)
513514 {
514515 fprintf(ar->f, ";");
515516 }
......@@ -530,8 +531,10 @@ static void render_node(AstRender *ar, AstNode *node) {
530531 AstNode *param_decl = node->data.fn_proto.params.at(arg_i);
531532 assert(param_decl->type == NodeTypeParamDecl);
532533 const char *arg_name = buf_ptr(&param_decl->data.param_decl.name);
533 const char *noalias_str = param_decl->data.param_decl.is_noalias ? "noalias " : "";
534 fprintf(ar->f, "%s%s: ", noalias_str, arg_name);
534 if (buf_len(&param_decl->data.param_decl.name) > 0) {
535 const char *noalias_str = param_decl->data.param_decl.is_noalias ? "noalias " : "";
536 fprintf(ar->f, "%s%s: ", noalias_str, arg_name);
537 }
535538 render_node(ar, param_decl->data.param_decl.type);
536539
537540 if (arg_i + 1 < arg_count || is_var_args) {
......@@ -548,7 +551,6 @@ static void render_node(AstRender *ar, AstNode *node) {
548551 fprintf(ar->f, " -> ");
549552 render_node(ar, return_type_node);
550553 }
551 fprintf(ar->f, ";");
552554 break;
553555 }
554556 case NodeTypeFnDef:
src/parseh.cpp+65-15
......@@ -121,6 +121,17 @@ static AstNode *create_struct_field_node(Context *c, const char *name, AstNode *
121121 return node;
122122}
123123
124static AstNode *create_param_decl_node(Context *c, const char *name, AstNode *type_node, bool is_noalias) {
125 assert(type_node);
126 AstNode *node = create_node(c, NodeTypeParamDecl);
127 buf_init_from_str(&node->data.param_decl.name, name);
128 node->data.param_decl.type = type_node;
129 node->data.param_decl.is_noalias = is_noalias;
130
131 normalize_parent_ptrs(node);
132 return node;
133}
134
124135static AstNode *create_num_lit_unsigned(Context *c, uint64_t x) {
125136 AstNode *node = create_node(c, NodeTypeNumberLiteral);
126137 node->data.number_literal.kind = NumLitUInt;
......@@ -255,6 +266,12 @@ static AstNode *make_type_node(Context *c, const Type *ty, const Decl *decl,
255266 const PointerType *pointer_ty = static_cast<const PointerType*>(ty);
256267 QualType child_qt = pointer_ty->getPointeeType();
257268 AstNode *type_node = make_qual_type_node(c, child_qt, decl);
269 if (child_qt.getTypePtr()->getTypeClass() == Type::Paren) {
270 const ParenType *paren_type = static_cast<const ParenType *>(child_qt.getTypePtr());
271 if (paren_type->getInnerType()->getTypeClass() == Type::FunctionProto) {
272 return create_prefix_node(c, PrefixOpMaybe, type_node);
273 }
274 }
258275 return pointer_to_type(c, type_node, child_qt.isConstQualified());
259276 }
260277 case Type::Typedef:
......@@ -311,8 +328,32 @@ static AstNode *make_type_node(Context *c, const Type *ty, const Decl *decl,
311328 }
312329 }
313330 case Type::FunctionProto:
314 emit_warning(c, decl, "ignoring function type");
315 return nullptr;
331 {
332 const FunctionProtoType *fn_proto_ty = static_cast<const FunctionProtoType*>(ty);
333 AstNode *node = create_node(c, NodeTypeFnProto);
334 buf_resize(&node->data.fn_proto.name, 0);
335 node->data.fn_proto.is_extern = true;
336 node->data.fn_proto.is_var_args = fn_proto_ty->isVariadic();
337 node->data.fn_proto.return_type = make_qual_type_node(c, fn_proto_ty->getReturnType(), decl);
338
339 if (!node->data.fn_proto.return_type) {
340 return nullptr;
341 }
342
343 int arg_count = fn_proto_ty->getNumParams();
344 for (int i = 0; i < arg_count; i += 1) {
345 QualType qt = fn_proto_ty->getParamType(i);
346 bool is_noalias = qt.isRestrictQualified();
347 AstNode *type_node = make_qual_type_node(c, qt, decl);
348 if (!type_node) {
349 return nullptr;
350 }
351 node->data.fn_proto.params.append(create_param_decl_node(c, "", type_node, is_noalias));
352 }
353
354 normalize_parent_ptrs(node);
355 return node;
356 }
316357 case Type::Record:
317358 {
318359 const RecordType *record_ty = static_cast<const RecordType*>(ty);
......@@ -356,6 +397,11 @@ static AstNode *make_type_node(Context *c, const Type *ty, const Decl *decl,
356397 uint64_t size = const_arr_ty->getSize().getLimitedValue();
357398 return create_array_type_node(c, child_type_node, size, false);
358399 }
400 case Type::Paren:
401 {
402 const ParenType *paren_ty = static_cast<const ParenType *>(ty);
403 return make_qual_type_node(c, paren_ty->getInnerType(), decl);
404 }
359405 case Type::BlockPointer:
360406 case Type::LValueReference:
361407 case Type::RValueReference:
......@@ -368,7 +414,6 @@ static AstNode *make_type_node(Context *c, const Type *ty, const Decl *decl,
368414 case Type::ExtVector:
369415 case Type::FunctionNoProto:
370416 case Type::UnresolvedUsing:
371 case Type::Paren:
372417 case Type::Adjusted:
373418 case Type::Decayed:
374419 case Type::TypeOfExpr:
......@@ -423,23 +468,19 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
423468 int arg_count = fn_decl->getNumParams();
424469 for (int i = 0; i < arg_count; i += 1) {
425470 const ParmVarDecl *param = fn_decl->getParamDecl(i);
426 AstNode *param_decl_node = create_node(c, NodeTypeParamDecl);
427471 const char *name = decl_name(param);
428472 if (strlen(name) == 0) {
429473 name = buf_ptr(buf_sprintf("arg%d", i));
430474 }
431 buf_init_from_str(&param_decl_node->data.param_decl.name, name);
432475 QualType qt = param->getOriginalType();
433 param_decl_node->data.param_decl.is_noalias = qt.isRestrictQualified();
434 param_decl_node->data.param_decl.type = make_qual_type_node(c, qt, fn_decl);
435 if (!param_decl_node->data.param_decl.type) {
436 emit_warning(c, param, "skipping function %s, unresolved param type\n",
437 buf_ptr(&node->data.fn_proto.name));
476 bool is_noalias = qt.isRestrictQualified();
477 AstNode *type_node = make_qual_type_node(c, qt, fn_decl);
478 if (!type_node) {
479 emit_warning(c, param, "skipping function %s, unresolved param type\n", name);
438480 return;
439481 }
440482
441 normalize_parent_ptrs(param_decl_node);
442 node->data.fn_proto.params.append(param_decl_node);
483 node->data.fn_proto.params.append(create_param_decl_node(c, name, type_node, is_noalias));
443484 }
444485
445486 if (fn_decl->isNoReturn()) {
......@@ -512,6 +553,11 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {
512553 node->data.struct_decl.visib_mod = VisibModExport;
513554 node->data.struct_decl.directives = create_empty_directives(c);
514555
556 // eagerly put the name in the table, but we need to remember to remove it if it fails
557 // boy it would be nice to have defer here wouldn't it
558 c->enum_type_table.put(bare_name, true);
559
560
515561 ZigList<AstNode *> var_decls = {0};
516562 int i = 0;
517563 for (auto it = enum_def->enumerator_begin(),
......@@ -520,6 +566,7 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {
520566 {
521567 const EnumConstantDecl *enum_const = *it;
522568 if (enum_const->getInitExpr()) {
569 c->enum_type_table.remove(bare_name);
523570 emit_warning(c, enum_const, "skipping enum %s - has init expression\n", buf_ptr(bare_name));
524571 return;
525572 }
......@@ -549,8 +596,6 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {
549596 var_decls.append(var_node);
550597 }
551598
552 c->enum_type_table.put(bare_name, true);
553
554599 normalize_parent_ptrs(node);
555600 c->root->data.root.top_level_decls.append(node);
556601
......@@ -594,6 +639,10 @@ static void visit_record_decl(Context *c, const RecordDecl *record_decl) {
594639 node->data.struct_decl.visib_mod = VisibModExport;
595640 node->data.struct_decl.directives = create_empty_directives(c);
596641
642 // eagerly put the name in the table, but we need to remember to remove it if it fails
643 // boy it would be nice to have defer here wouldn't it
644 c->struct_type_table.put(bare_name, true);
645
597646 for (auto it = record_def->field_begin(),
598647 it_end = record_def->field_end();
599648 it != it_end; ++it)
......@@ -601,12 +650,14 @@ static void visit_record_decl(Context *c, const RecordDecl *record_decl) {
601650 const FieldDecl *field_decl = *it;
602651
603652 if (field_decl->isBitField()) {
653 c->struct_type_table.remove(bare_name);
604654 emit_warning(c, field_decl, "skipping struct %s - has bitfield\n", buf_ptr(bare_name));
605655 return;
606656 }
607657
608658 AstNode *type_node = make_qual_type_node(c, field_decl->getType(), field_decl);
609659 if (!type_node) {
660 c->struct_type_table.remove(bare_name);
610661 emit_warning(c, field_decl, "skipping struct %s - unhandled type\n", buf_ptr(bare_name));
611662 return;
612663 }
......@@ -615,7 +666,6 @@ static void visit_record_decl(Context *c, const RecordDecl *record_decl) {
615666 node->data.struct_decl.fields.append(field_node);
616667 }
617668
618 c->struct_type_table.put(bare_name, true);
619669 normalize_parent_ptrs(node);
620670 c->root->data.root.top_level_decls.append(node);
621671
test/run_tests.cpp+9
......@@ -1961,6 +1961,15 @@ pub const Bar = enum_Bar;)OUTPUT");
19611961 add_parseh_case("constant size array", R"SOURCE(
19621962void func(int array[20]);
19631963 )SOURCE", R"OUTPUT(pub extern fn func(array: [20]c_int);)OUTPUT");
1964
1965 add_parseh_case("self referential struct with function pointer", R"SOURCE(
1966struct Foo {
1967 void (*derp)(struct Foo *foo);
1968};
1969 )SOURCE", R"OUTPUT(export struct struct_Foo {
1970 derp: ?extern fn (?&struct_Foo),
1971}
1972pub const Foo = struct_Foo;)OUTPUT");
19641973}
19651974
19661975static void print_compiler_invocation(TestCase *test_case) {