blob: ac2f53c5082f9f58c4bbbd057c0da93879b2eb1e [file] [log] [blame]
Anders Carlsson307846f2009-07-23 03:17:50 +00001//===--- CGRecordLayoutBuilder.h - Record builder helper --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This is a helper class used to build CGRecordLayout objects and LLVM types.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CGRECORDLAYOUTBUILDER_H
15#define CLANG_CODEGEN_CGRECORDLAYOUTBUILDER_H
16
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/Support/DataTypes.h"
19#include <vector>
20
21namespace llvm {
22 class Type;
23}
24
25namespace clang {
26 class FieldDecl;
27 class RecordDecl;
28
29namespace CodeGen {
30 class CGRecordLayout;
31 class CodeGenTypes;
32
33class CGRecordLayoutBuilder {
34 CodeGenTypes &Types;
35
36 /// StructPacking - Will be 0 if this struct is not packed. If it is packed,
37 /// it will have the packing alignment in bits.
38 ///
39 unsigned StructPacking;
40
41 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
42 /// LLVM types.
43 unsigned AlignmentAsLLVMStruct;
44
45 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
46 /// this will have the number of bits still available in the field.
47 char BitsAvailableInLastField;
48
49 /// FieldTypes - Holds the LLVM types that the struct is created from.
50 std::vector<const llvm::Type *> FieldTypes;
51
52 /// FieldInfo - Holds size and offset information about a field.
53 /// FIXME: I think we can get rid of this.
54 struct FieldInfo {
55 FieldInfo(uint64_t OffsetInBytes, uint64_t SizeInBytes)
56 : OffsetInBytes(OffsetInBytes), SizeInBytes(SizeInBytes) { }
57
58 const uint64_t OffsetInBytes;
59 const uint64_t SizeInBytes;
60 };
61 llvm::SmallVector<FieldInfo, 16> FieldInfos;
62
63 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
64 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
65 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
66
67 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
68 struct LLVMBitFieldInfo {
69 LLVMBitFieldInfo(const FieldDecl *FD, unsigned Start, unsigned Size)
70 : FD(FD), Start(Start), Size(Size) { }
71
72 const FieldDecl *FD;
73
74 unsigned Start;
75 unsigned Size;
76 };
77 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
78
79 CGRecordLayoutBuilder(CodeGenTypes &Types)
80 : Types(Types), StructPacking(0), AlignmentAsLLVMStruct(1)
81 , BitsAvailableInLastField(0) { }
82
83 /// Layout - Will layout a RecordDecl.
84 void Layout(const RecordDecl *D);
85
86 /// LayoutField - try to layout all fields in the record decl.
87 /// Returns false if the operation failed because the struct is not packed.
88 bool LayoutFields(const RecordDecl *D);
89
90 /// LayoutField - layout a single field. Returns false if the operation failed
91 /// because the current struct is not packed.
92 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
93
94 /// LayoutBitField - layout a single bit field.
95 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
96
97 /// AppendField - Appends a field with the given offset size and type.
98 void AppendField(uint64_t FieldOffsetInBytes, uint64_t FieldSizeInBytes,
99 const llvm::Type *FieldTy);
100
101 /// AppendPadding - Appends enough padding bytes so that the total struct
102 /// size matches the alignment of the passed in type.
103 void AppendPadding(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
104
105 /// AppendPadding - Appends enough padding bytes so that the total
106 /// struct size is a multiple of the field alignment.
107 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
108
109 /// AppendBytes - Append a given number of bytes to the record.
110 void AppendBytes(uint64_t NumBytes);
111
112 /// getNextFieldOffsetInBytes - returns where the next field offset is.
113 uint64_t getNextFieldOffsetInBytes() const;
114
115 unsigned getTypeAlignment(const llvm::Type *Ty) const;
116 uint64_t getTypeSizeInBytes(const llvm::Type *Ty) const;
117
118public:
119 /// ComputeLayout - Return the right record layout for a given record decl.
120 static CGRecordLayout *ComputeLayout(CodeGenTypes &Types,
121 const RecordDecl *D);
122};
123
124} // end namespace CodeGen
125} // end namespace clang
126
127
128#endif