diff options
Diffstat (limited to 'cparser/PackedStructs.ml')
-rw-r--r-- | cparser/PackedStructs.ml | 44 |
1 files changed, 15 insertions, 29 deletions
diff --git a/cparser/PackedStructs.ml b/cparser/PackedStructs.ml index e1287eb8..a2c91c0a 100644 --- a/cparser/PackedStructs.ml +++ b/cparser/PackedStructs.ml @@ -81,47 +81,33 @@ let transf_field_decl mfa swapped loc env struct_id f = (* Rewriting struct declarations *) let transf_struct_decl mfa msa swapped loc env struct_id attrs ml = + let attrs' = + remove_custom_attributes ["packed";"__packed__"] attrs in let ml' = List.map (transf_field_decl mfa swapped loc env struct_id) ml in - if msa = 0 then (attrs, ml') else begin - let al' = (* natural alignment of the transformed struct *) - List.fold_left - (fun a f' -> max a (safe_alignof loc env f'.fld_typ)) - 1 ml' in - (set_alignas_attr (max msa al') attrs, ml') + if msa = 0 then (attrs', ml') else begin + (* [Cutil.composite_info_def] takes packing parameters into account. + Hence the alignment it returns is the correct alignment for + the transformed struct. *) + let ci = Cutil.composite_info_def env Struct attrs ml in + match ci.ci_alignof with + | None -> error loc "incomplete struct"; (attrs', ml') + | Some al -> (set_alignas_attr al attrs', ml') end (* Rewriting composite declarations *) -let is_pow2 n = n > 0 && n land (n - 1) = 0 - -let packed_param_value loc n = - let m = Int64.to_int n in - if n <> Int64.of_int m then - (error loc "__packed__ parameter `%Ld' is too large" n; 0) - else if m = 0 || is_pow2 m then - m - else - (error loc "__packed__ parameter `%Ld' must be a power of 2" n; 0) - let transf_composite loc env su id attrs ml = match su with | Union -> (attrs, ml) | Struct -> let (mfa, msa, swapped) = match find_custom_attributes ["packed";"__packed__"] attrs with - | [] -> (0L, 0L, false) - | [[]] -> (1L, 0L, false) - | [[AInt n]] -> (n, 0L, false) - | [[AInt n; AInt p]] -> (n, p, false) - | [[AInt n; AInt p; AInt q]] -> (n, p, q <> 0L) - | _ -> - error loc "ill-formed or ambiguous __packed__ attribute"; - (0L, 0L, false) in - let mfa = packed_param_value loc mfa in - let msa = packed_param_value loc msa in - let attrs' = remove_custom_attributes ["packed";"__packed__"] attrs in - transf_struct_decl mfa msa swapped loc env id attrs' ml + | [] -> (0, 0, false) + | [_] -> Cutil.packing_parameters attrs + | _ -> error loc "multiple __packed__ attributes"; + (0, 0, false) in + transf_struct_decl mfa msa swapped loc env id attrs ml (* Accessor functions *) |