diff options
Diffstat (limited to 'src/check_expr.cpp')
| -rw-r--r-- | src/check_expr.cpp | 141 |
1 files changed, 46 insertions, 95 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; } } |