Path: blob/main/contrib/llvm-project/lld/MachO/Layout.h
34870 views
//===- Layout.h -----------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78// Convenience macros for obtaining offsets of members in structs.9//10// Usage:11//12// #define FOR_EACH_FOO_FIELD(DO) \13// DO(Ptr, bar) \14// DO(uint32_t, baz) \15// CREATE_LAYOUT_CLASS(Foo, FOR_EACH_FOO_FIELD)16// #undef FOR_EACH_FOO_FIELD17//18// This will generate19//20// struct FooLayout {21// uint32_t barOffset;22// uint32_t bazOffset;23// uint32_t totalSize;24//25// FooLayout(size_t wordSize) {26// if (wordSize == 8)27// init<uint64_t>();28// else {29// assert(wordSize == 4);30// init<uint32_t>();31// }32// }33//34// private:35// template <class Ptr> void init() {36// FOR_EACH_FIELD(_INIT_OFFSET);37// barOffset = offsetof(Layout<Ptr>, bar);38// bazOffset = offsetof(Layout<Ptr>, baz);39// totalSize = sizeof(Layout<Ptr>);40// }41// template <class Ptr> struct Layout {42// Ptr bar;43// uint32_t baz;44// };45// };4647#define _OFFSET_FOR_FIELD(_, name) uint32_t name##Offset;48#define _INIT_OFFSET(type, name) name##Offset = offsetof(Layout<Ptr>, name);49#define _LAYOUT_ENTRY(type, name) type name;5051#define CREATE_LAYOUT_CLASS(className, FOR_EACH_FIELD) \52struct className##Layout { \53FOR_EACH_FIELD(_OFFSET_FOR_FIELD) \54uint32_t totalSize; \55\56className##Layout(size_t wordSize) { \57if (wordSize == 8) \58init<uint64_t>(); \59else { \60assert(wordSize == 4); \61init<uint32_t>(); \62} \63} \64\65private: \66template <class Ptr> void init() { \67FOR_EACH_FIELD(_INIT_OFFSET); \68totalSize = sizeof(Layout<Ptr>); \69} \70template <class Ptr> struct Layout { \71FOR_EACH_FIELD(_LAYOUT_ENTRY) \72}; \73}747576