1 //===--- LayoutOverrideSource.h --Override Record Layouts -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_FRONTEND_LAYOUTOVERRIDESOURCE_H
10 #define LLVM_CLANG_FRONTEND_LAYOUTOVERRIDESOURCE_H
11 
12 #include "clang/AST/ExternalASTSource.h"
13 #include "clang/Basic/LLVM.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/StringRef.h"
16 
17 namespace clang {
18   /// An external AST source that overrides the layout of
19   /// a specified set of record types.
20   ///
21   /// This class is used only for testing the ability of external AST sources
22   /// to override the layout of record types. Its input is the output format
23   /// of the command-line argument -fdump-record-layouts.
24   class LayoutOverrideSource : public ExternalASTSource {
25     /// The layout of a given record.
26     struct Layout {
27       /// The size of the record.
28       uint64_t Size;
29 
30       /// The alignment of the record.
31       uint64_t Align;
32 
33       /// The offsets of non-virtual base classes in the record.
34       SmallVector<CharUnits, 8> BaseOffsets;
35 
36       /// The offsets of virtual base classes in the record.
37       SmallVector<CharUnits, 8> VBaseOffsets;
38 
39       /// The offsets of the fields, in source order.
40       SmallVector<uint64_t, 8> FieldOffsets;
41     };
42 
43     /// The set of layouts that will be overridden.
44     llvm::StringMap<Layout> Layouts;
45 
46   public:
47     /// Create a new AST source that overrides the layout of some
48     /// set of record types.
49     ///
50     /// The file is the result of passing -fdump-record-layouts to a file.
51     explicit LayoutOverrideSource(StringRef Filename);
52 
53     /// If this particular record type has an overridden layout,
54     /// return that layout.
55     bool
56     layoutRecordType(const RecordDecl *Record,
57        uint64_t &Size, uint64_t &Alignment,
58        llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
59        llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
60        llvm::DenseMap<const CXXRecordDecl *,
61                       CharUnits> &VirtualBaseOffsets) override;
62 
63     /// Dump the overridden layouts.
64     void dump();
65   };
66 }
67 
68 #endif
69