fix codegen for implicit maybe wrap

This commit is contained in:
Andrew Kelley 2016-01-08 02:52:27 -07:00
parent 9aea99a999
commit e1f498212c
5 changed files with 17 additions and 24 deletions

View File

@ -17,7 +17,7 @@ compromises backward compatibility.
* Completely compatible with C libraries with no wrapper necessary. * Completely compatible with C libraries with no wrapper necessary.
* In addition to creating executables, creating a C library is a primary use * In addition to creating executables, creating a C library is a primary use
case. You can export an auto-generated .h file. case. You can export an auto-generated .h file.
* Do not depend on libc unless explicitly imported. * Do not depend on libc unless explicitly linked.
* Provide standard library which competes with the C standard library and is * Provide standard library which competes with the C standard library and is
always compiled against statically in source form. always compiled against statically in source form.
* Generics so that one can write efficient data structures that work for any * Generics so that one can write efficient data structures that work for any
@ -32,6 +32,8 @@ compromises backward compatibility.
* Eliminate the need for header files (when using zig internally). * Eliminate the need for header files (when using zig internally).
* Tagged union enum type. * Tagged union enum type.
* Resilient to parsing errors to make IDE integration work well. * Resilient to parsing errors to make IDE integration work well.
* Eliminate the preprocessor, but have a plan for how to do things you would
want to use the preprocessor for such as conditional compilation.
* Ability to mark functions as test and automatically run them in test mode. * Ability to mark functions as test and automatically run them in test mode.
This mode should automatically provide test coverage. This mode should automatically provide test coverage.
* Friendly toward package maintainers. * Friendly toward package maintainers.

View File

@ -1207,7 +1207,7 @@ static TypeTableEntry *resolve_type_compatibility(CodeGen *g, BlockContext *cont
actual_type->data.pointer.child_type); actual_type->data.pointer.child_type);
} }
add_node_error(g, node, add_node_error(g, first_executing_node(node),
buf_sprintf("expected type '%s', got '%s'", buf_sprintf("expected type '%s', got '%s'",
buf_ptr(&expected_type->name), buf_ptr(&expected_type->name),
buf_ptr(&actual_type->name))); buf_ptr(&actual_type->name)));

View File

@ -495,6 +495,7 @@ static LLVMValueRef gen_bare_cast(CodeGen *g, AstNode *node, LLVMValueRef expr_v
{ {
assert(cast_node->ptr); assert(cast_node->ptr);
assert(wanted_type->id == TypeTableEntryIdMaybe); assert(wanted_type->id == TypeTableEntryIdMaybe);
assert(actual_type);
add_debug_source_node(g, node); add_debug_source_node(g, node);
LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 0, ""); LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 0, "");
@ -1600,12 +1601,6 @@ static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
zig_unreachable(); zig_unreachable();
} }
static LLVMValueRef gen_cast_node(CodeGen *g, AstNode *node, LLVMValueRef val, TypeTableEntry *before_type,
CastNode *cast_node)
{
return cast_node->after_type ? gen_bare_cast(g, node, val, before_type, cast_node->after_type, cast_node) : val;
}
static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) { static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
LLVMValueRef val = gen_expr_no_cast(g, node); LLVMValueRef val = gen_expr_no_cast(g, node);
@ -1615,17 +1610,19 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
assert(node->codegen_node); assert(node->codegen_node);
{
TypeTableEntry *before_type = node->codegen_node->expr_node.type_entry; TypeTableEntry *before_type = node->codegen_node->expr_node.type_entry;
if (before_type && before_type->id == TypeTableEntryIdUnreachable) { if (before_type && before_type->id == TypeTableEntryIdUnreachable) {
return val; return val;
} }
val = gen_cast_node(g, node, val, before_type, &node->codegen_node->expr_node.implicit_cast); CastNode *cast_node = &node->codegen_node->expr_node.implicit_cast;
if (cast_node->after_type) {
val = gen_bare_cast(g, node, val, before_type, cast_node->after_type, cast_node);
before_type = cast_node->after_type;
} }
{ cast_node = &node->codegen_node->expr_node.implicit_maybe_cast;
TypeTableEntry *before_type = node->codegen_node->expr_node.implicit_cast.after_type; if (cast_node->after_type) {
val = gen_cast_node(g, node, val, before_type, &node->codegen_node->expr_node.implicit_maybe_cast); val = gen_bare_cast(g, node, val, before_type, cast_node->after_type, cast_node);
} }
return val; return val;

View File

@ -1,12 +1,6 @@
// These functions are provided when not linking against libc because LLVM // These functions are provided when not linking against libc because LLVM
// sometimes generates code that calls them. // sometimes generates code that calls them.
// In the future we may put these functions in separate compile units, make them .o files,
// and then use
// ar rcs foo.a foo.o memcpy.o memset.o
// ld -o foo foo.a
// This will omit the machine code if the function is unused.
export fn memset(dest: &u8, c: u8, n: usize) -> &u8 { export fn memset(dest: &u8, c: u8, n: usize) -> &u8 {
var index : #typeof(n) = 0; var index : #typeof(n) = 0;
while (index != n) { while (index != n) {

View File

@ -42,7 +42,7 @@ pub fn print_i64(x: i64) -> isize {
/* /*
// TODO error handling // TODO error handling
pub fn readline(buf: []u8) -> ?[]u8 { pub fn readline(buf: []u8) -> ?[]u8 {
var index = 0; var index : usize = 0;
while (index < buf.len) { while (index < buf.len) {
// TODO unknown size array indexing operator // TODO unknown size array indexing operator
const err = read(stdin_fileno, &buf.ptr[index], 1); const err = read(stdin_fileno, &buf.ptr[index], 1);