haskell-gi
Safe HaskellNone
LanguageHaskell2010

Data.GI.CodeGen.StructLayout

Description

Fix the memory layout of structs and unions.

When compiling the typelib, the typelib compiler (both gi-compile-repository in GLib and g-ir-compiler in gobject-introspection) makes two simplifications when computing the layout of structs and unions:

  • It ignores the width of bitfields: each bitfield is laid out as if it was a full member of its declared type.
  • It ignores unions and structs declared inline (such as union { ... } u;, or anonymous unions).

This leads to wrong struct sizes, and wrong offsets for the fields following the affected members. This affects the struct itself, and any struct or union embedding it. See https://github.com/haskell-gi/haskell-gi/issues/485.

The GIR files do contain the required information, so here we recompute the layout ourselves, following the rules of the C ABI. To make sure that we are not missing any information (fields that we could not parse, types of unknown size, ...), we first check that by laying out the struct in the same way that the typelib compiler does we reproduce the typelib data exactly. If this is not the case we keep the data from the typelib.

Synopsis

Documentation

data Aggregate Source #

The information about a named type that we need in order to compute layouts.

Constructors

AggStruct Struct 
AggUnion Union 
AggScalar Int

Enums and flags, with their storage size in bytes.

AggPointer

Types which are always stored as pointers in structs (objects, interfaces, callbacks).

fixupStructLayouts :: Map Name Aggregate -> Map Name Aggregate Source #

Given the structs, unions, and other named types in scope, return the structs and unions whose layout has been corrected. Bitfields in the corrected structs and unions will also have their fieldBitShift set.