blob: 4ebf4e88decb1047f151f8ea797a4be5736c3ed3 [file] [log] [blame]
Anders Carlsson45372a62009-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"
Chandler Carruth9f8eb202009-10-26 01:37:10 +000018#include "llvm/System/DataTypes.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000019#include <vector>
20
21namespace llvm {
22 class Type;
23}
24
25namespace clang {
26 class FieldDecl;
27 class RecordDecl;
Mike Stump1eb44332009-09-09 15:08:12 +000028
Anders Carlsson45372a62009-07-23 03:17:50 +000029namespace CodeGen {
30 class CGRecordLayout;
31 class CodeGenTypes;
32
Mike Stump1eb44332009-09-09 15:08:12 +000033class CGRecordLayoutBuilder {
Anders Carlsson45372a62009-07-23 03:17:50 +000034 CodeGenTypes &Types;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Anders Carlsson4b5584b2009-07-23 17:24:40 +000036 /// Packed - Whether the resulting LLVM struct will be packed or not.
37 bool Packed;
Anders Carlsson45372a62009-07-23 03:17:50 +000038
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000039 /// ContainsMemberPointer - Whether one of the fields is a member pointer
40 /// or is a struct that contains a member pointer.
41 bool ContainsMemberPointer;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Anders Carlssona5dd7222009-08-08 19:38:24 +000043 /// Alignment - Contains the alignment of the RecordDecl.
44 unsigned Alignment;
45
Anders Carlsson45372a62009-07-23 03:17:50 +000046 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
47 /// LLVM types.
48 unsigned AlignmentAsLLVMStruct;
Mike Stump1eb44332009-09-09 15:08:12 +000049
Anders Carlsson45372a62009-07-23 03:17:50 +000050 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
51 /// this will have the number of bits still available in the field.
52 char BitsAvailableInLastField;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +000053
54 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
55 uint64_t NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +000056
Anders Carlsson45372a62009-07-23 03:17:50 +000057 /// FieldTypes - Holds the LLVM types that the struct is created from.
58 std::vector<const llvm::Type *> FieldTypes;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Anders Carlsson45372a62009-07-23 03:17:50 +000060 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
61 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
62 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
63
64 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
65 struct LLVMBitFieldInfo {
Mike Stump1eb44332009-09-09 15:08:12 +000066 LLVMBitFieldInfo(const FieldDecl *FD, unsigned FieldNo, unsigned Start,
Anders Carlsson8330cee2009-07-23 17:01:21 +000067 unsigned Size)
68 : FD(FD), FieldNo(FieldNo), Start(Start), Size(Size) { }
Mike Stump1eb44332009-09-09 15:08:12 +000069
Anders Carlsson45372a62009-07-23 03:17:50 +000070 const FieldDecl *FD;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Anders Carlsson8330cee2009-07-23 17:01:21 +000072 unsigned FieldNo;
Anders Carlsson45372a62009-07-23 03:17:50 +000073 unsigned Start;
74 unsigned Size;
75 };
76 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
Mike Stump1eb44332009-09-09 15:08:12 +000077
78 CGRecordLayoutBuilder(CodeGenTypes &Types)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000079 : Types(Types), Packed(false), ContainsMemberPointer(false)
80 , Alignment(0), AlignmentAsLLVMStruct(1)
Anders Carlssonc2cc1d52009-07-28 17:56:36 +000081 , BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
Anders Carlsson45372a62009-07-23 03:17:50 +000082
83 /// Layout - Will layout a RecordDecl.
84 void Layout(const RecordDecl *D);
85
Anders Carlsson5a6e3982009-07-23 03:43:54 +000086 /// LayoutUnion - Will layout a union RecordDecl.
87 void LayoutUnion(const RecordDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000088
Anders Carlsson45372a62009-07-23 03:17:50 +000089 /// LayoutField - try to layout all fields in the record decl.
90 /// Returns false if the operation failed because the struct is not packed.
91 bool LayoutFields(const RecordDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000092
Anders Carlsson45372a62009-07-23 03:17:50 +000093 /// LayoutField - layout a single field. Returns false if the operation failed
94 /// because the current struct is not packed.
95 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
Mike Stump1eb44332009-09-09 15:08:12 +000096
Anders Carlsson45372a62009-07-23 03:17:50 +000097 /// LayoutBitField - layout a single bit field.
98 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
99
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000100 /// AppendField - Appends a field with the given offset and type.
101 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000102
103 /// AppendPadding - Appends enough padding bytes so that the total struct
104 /// size matches the alignment of the passed in type.
105 void AppendPadding(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
106
107 /// AppendPadding - Appends enough padding bytes so that the total
108 /// struct size is a multiple of the field alignment.
109 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Anders Carlsson45372a62009-07-23 03:17:50 +0000111 /// AppendBytes - Append a given number of bytes to the record.
112 void AppendBytes(uint64_t NumBytes);
113
Anders Carlssonc1efe362009-07-27 14:55:54 +0000114 /// AppendTailPadding - Append enough tail padding so that the type will have
115 /// the passed size.
116 void AppendTailPadding(uint64_t RecordSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Anders Carlsson45372a62009-07-23 03:17:50 +0000118 unsigned getTypeAlignment(const llvm::Type *Ty) const;
119 uint64_t getTypeSizeInBytes(const llvm::Type *Ty) const;
120
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000121 /// CheckForMemberPointer - Check if the field contains a member pointer.
122 void CheckForMemberPointer(const FieldDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Anders Carlsson45372a62009-07-23 03:17:50 +0000124public:
125 /// ComputeLayout - Return the right record layout for a given record decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000126 static CGRecordLayout *ComputeLayout(CodeGenTypes &Types,
Anders Carlsson45372a62009-07-23 03:17:50 +0000127 const RecordDecl *D);
128};
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Anders Carlsson45372a62009-07-23 03:17:50 +0000130} // end namespace CodeGen
131} // end namespace clang
Anders Carlsson45372a62009-07-23 03:17:50 +0000132
Mike Stump1eb44332009-09-09 15:08:12 +0000133
134#endif