implement undefined literal

master
Andrew Kelley 2016-01-22 23:12:33 -07:00
parent 0e51c16ef5
commit e269caae02
3 changed files with 90 additions and 70 deletions

View File

@ -62,6 +62,7 @@ struct ConstPtrValue {
struct ConstExprValue {
bool ok; // true if constant expression evalution worked
bool depends_on_compile_var;
bool undef;
union {
BigNum x_bignum;
@ -808,6 +809,7 @@ enum TypeTableEntryId {
TypeTableEntryIdStruct,
TypeTableEntryIdNumLitFloat,
TypeTableEntryIdNumLitInt,
TypeTableEntryIdUndefLit,
TypeTableEntryIdMaybe,
TypeTableEntryIdError,
TypeTableEntryIdEnum,
@ -949,6 +951,7 @@ struct CodeGen {
TypeTableEntry *entry_invalid;
TypeTableEntry *entry_num_lit_int;
TypeTableEntry *entry_num_lit_float;
TypeTableEntry *entry_undef;
} builtin_types;
LLVMTargetDataRef target_data_ref;

View File

@ -111,6 +111,7 @@ TypeTableEntry *new_type_table_entry(TypeTableEntryId id) {
case TypeTableEntryIdMaybe:
case TypeTableEntryIdFn:
case TypeTableEntryIdError:
case TypeTableEntryIdUndefLit:
// nothing to init
break;
case TypeTableEntryIdStruct:
@ -1063,6 +1064,7 @@ static bool type_has_codegen_value(TypeTableEntryId id) {
case TypeTableEntryIdUnreachable:
case TypeTableEntryIdNumLitFloat:
case TypeTableEntryIdNumLitInt:
case TypeTableEntryIdUndefLit:
return false;
case TypeTableEntryIdBool:
@ -2481,10 +2483,9 @@ static TypeTableEntry *analyze_undefined_literal_expr(CodeGen *g, ImportTableEnt
ConstExprValue *const_val = &expr->const_val;
const_val->ok = true;
const_val->undef = true;
zig_panic("TODO");
return expected_type;
return expected_type ? expected_type : g->builtin_types.entry_undef;
}

View File

@ -1725,8 +1725,16 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
}
if (variable->type->size_in_bits == 0) {
return nullptr;
} else {
}
bool have_init_expr = false;
if (var_decl->expr) {
ConstExprValue *const_val = &get_resolved_expr(var_decl->expr)->const_val;
if (!const_val->ok || !const_val->undef) {
have_init_expr = true;
}
}
if (have_init_expr) {
TypeTableEntry *expr_type = get_expr_type(var_decl->expr);
LLVMValueRef value;
if (unwrap_maybe) {
@ -1802,7 +1810,6 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
LLVMZigInsertDeclareAtEnd(g->dbuilder, variable->value_ref, variable->di_loc_var, debug_loc,
LLVMGetInsertBlock(g->builder));
return nullptr;
}
}
static LLVMValueRef gen_var_decl_expr(CodeGen *g, AstNode *node) {
@ -2035,6 +2042,10 @@ static void build_label_blocks(CodeGen *g, AstNode *block_node) {
static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val) {
assert(const_val->ok);
if (const_val->undef) {
return LLVMConstNull(type_entry->type_ref);
}
if (type_entry->id == TypeTableEntryIdInt) {
return LLVMConstInt(type_entry->type_ref, bignum_to_twos_complement(&const_val->data.x_bignum), false);
} else if (type_entry->id == TypeTableEntryIdFloat) {
@ -2389,6 +2400,11 @@ static void define_builtin_types(CodeGen *g) {
buf_init_from_str(&entry->name, "(integer literal)");
g->builtin_types.entry_num_lit_int = entry;
}
{
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdUndefLit);
buf_init_from_str(&entry->name, "(undefined)");
g->builtin_types.entry_undef = entry;
}
for (int i = 0; i < array_length(int_sizes_in_bits); i += 1) {
int size_in_bits = int_sizes_in_bits[i];