diff options
| author | Ginger Bill <bill@gingerbill.org> | 2017-08-12 20:04:58 +0100 |
|---|---|---|
| committer | Ginger Bill <bill@gingerbill.org> | 2017-08-12 20:04:58 +0100 |
| commit | 33aad3a8cec6aec8b6041c20ee661b4b8a451333 (patch) | |
| tree | 30f99a912c34b9f06c62daf19a9d0f4e499a683f /src | |
| parent | 4262c125c5c56db61c57e090ea76db61c16bcebc (diff) | |
| parent | a09d5959efba59f1b8c0278ad8b36d8840a13069 (diff) | |
Merge branch 'master' of https://github.com/gingerBill/Odin
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_expr.cpp | 141 | ||||
| -rw-r--r-- | src/gb/gb.h | 2 |
2 files changed, 47 insertions, 96 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 88cb6c54c..0519aad4f 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1072,6 +1072,40 @@ Entity *make_names_field_for_struct(Checker *c, Scope *scope) { return e; } +bool check_custom_align(Checker *c, AstNode *node, i64 *align_) { + GB_ASSERT(align_ != nullptr); + Operand o = {}; + check_expr(c, &o, node); + if (o.mode != Addressing_Constant) { + if (o.mode != Addressing_Invalid) { + error(node, "#align must be a constant"); + } + return false; + } + + Type *type = base_type(o.type); + if (is_type_untyped(type) || is_type_integer(type)) { + if (o.value.kind == ExactValue_Integer) { + i64 align = i128_to_i64(o.value.value_integer); + if (align < 1 || !gb_is_power_of_two(align)) { + error(node, "#align must be a power of 2, got %lld", align); + return false; + } + + // NOTE(bill): Success!!! + i64 custom_align = gb_clamp(align, 1, build_context.max_align); + if (custom_align < align) { + warning(node, "Custom alignment has been clamped to %lld from %lld", align, custom_align); + } + *align_ = custom_align; + return true; + } + } + + error(node, "#align must be an integer"); + return false; +} + void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Operand> *poly_operands) { GB_ASSERT(is_type_struct(struct_type)); ast_node(st, StructType, node); @@ -1294,40 +1328,11 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Opera syntax_error(st->align, "`#align` cannot be applied with `#packed`"); return; } - - Operand o = {}; - check_expr(c, &o, st->align); - if (o.mode != Addressing_Constant) { - if (o.mode != Addressing_Invalid) { - error(st->align, "#align must be a constant"); - } - return; - } - - Type *type = base_type(o.type); - if (is_type_untyped(type) || is_type_integer(type)) { - if (o.value.kind == ExactValue_Integer) { - i64 align = i128_to_i64(o.value.value_integer); - if (align < 1 || !gb_is_power_of_two(align)) { - error(st->align, "#align must be a power of 2, got %lld", align); - return; - } - - // NOTE(bill): Success!!! - i64 custom_align = gb_clamp(align, 1, build_context.max_align); - if (custom_align < align) { - warning(st->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align); - } - struct_type->Struct.custom_align = custom_align; - return; - } + i64 custom_align = 1; + if (check_custom_align(c, st->align, &custom_align)) { + struct_type->Struct.custom_align = custom_align; } - - error(st->align, "#align must be an integer"); - return; } - - } void check_union_type(Checker *c, Type *union_type, AstNode *node) { GB_ASSERT(is_type_union(union_type)); @@ -1376,43 +1381,15 @@ void check_union_type(Checker *c, Type *union_type, AstNode *node) { union_type->Union.variants = variants; if (ut->align != nullptr) { - Operand o = {}; - check_expr(c, &o, ut->align); - if (o.mode != Addressing_Constant) { - if (o.mode != Addressing_Invalid) { - error(ut->align, "#align must be a constant"); - } - return; - } - - Type *type = base_type(o.type); - if (is_type_untyped(type) || is_type_integer(type)) { - if (o.value.kind == ExactValue_Integer) { - i64 align = i128_to_i64(o.value.value_integer); - if (align < 1 || !gb_is_power_of_two(align)) { - error(ut->align, "#align must be a power of 2, got %lld", align); - return; - } - - // NOTE(bill): Success!!! - i64 custom_align = gb_clamp(align, 1, build_context.max_align); - if (custom_align < align) { - warning(ut->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align); - } - if (variants.count == 0) { - error(ut->align, "An empty union cannot have a custom alignment"); - } else { - union_type->Union.custom_align = custom_align; - } - return; + i64 custom_align = 1; + if (check_custom_align(c, ut->align, &custom_align)) { + if (variants.count == 0) { + error(ut->align, "An empty union cannot have a custom alignment"); + } else { + union_type->Union.custom_align = custom_align; } } - - error(ut->align, "#align must be an integer"); - return; } - - } // void check_raw_union_type(Checker *c, Type *union_type, AstNode *node) { @@ -1653,36 +1630,10 @@ void check_bit_field_type(Checker *c, Type *bit_field_type, AstNode *node) { if (bft->align != nullptr) { - Operand o = {}; - check_expr(c, &o, bft->align); - if (o.mode != Addressing_Constant) { - if (o.mode != Addressing_Invalid) { - error(bft->align, "#align must be a constant"); - } - return; - } - - Type *type = base_type(o.type); - if (is_type_untyped(type) || is_type_integer(type)) { - if (o.value.kind == ExactValue_Integer) { - i64 align = i128_to_i64(o.value.value_integer); - if (align < 1 || !gb_is_power_of_two(align)) { - error(bft->align, "#align must be a power of 2, got %lld", align); - return; - } - - // NOTE(bill): Success!!! - i64 custom_align = gb_clamp(align, 1, build_context.max_align); - if (custom_align < align) { - warning(bft->align, "Custom alignment has been clamped to %lld from %lld", align, custom_align); - } - bit_field_type->BitField.custom_align = custom_align; - return; - } + i64 custom_align = 1; + if (check_custom_align(c, bft->align, &custom_align)) { + bit_field_type->BitField.custom_align = custom_align; } - - error(bft->align, "#align must be an integer"); - return; } } diff --git a/src/gb/gb.h b/src/gb/gb.h index 3386d529b..48da7ae41 100644 --- a/src/gb/gb.h +++ b/src/gb/gb.h @@ -7991,7 +7991,7 @@ gb_inline b32 gb_file_move(char const *existing_filename, char const *new_filena b32 gb_file_remove(char const *filename) { #if defined(GB_SYSTEM_OSX) - return unlink(existing_filename) != -1; + return unlink(filename) != -1; #else return remove(filename) == 0; #endif |