blob: 86c57d10c4ac3613fd3e631494e59bce89f832f6 [file] [log] [blame]
Daniel Dunbar072d0bb2010-03-30 22:26:10 +00001//===--- CGRecordLayout.h - LLVM Record Layout Information ------*- 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#ifndef CLANG_CODEGEN_CGRECORDLAYOUT_H
11#define CLANG_CODEGEN_CGRECORDLAYOUT_H
12
Daniel Dunbar034299e2010-03-31 01:09:11 +000013#include "llvm/ADT/DenseMap.h"
14#include "clang/AST/Decl.h"
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000015namespace llvm {
16 class Type;
17}
18
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000019namespace clang {
20namespace CodeGen {
21
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000022class CGBitFieldInfo {
23public:
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +000024 CGBitFieldInfo(const llvm::Type *FieldTy, unsigned FieldNo,
25 unsigned Start, unsigned Size, bool IsSigned)
26 : FieldTy(FieldTy), FieldNo(FieldNo),
27 Start(Start), Size(Size), IsSigned(IsSigned) {}
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000028
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +000029 const llvm::Type *FieldTy;
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000030 unsigned FieldNo;
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +000031
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000032 unsigned Start;
33 unsigned Size;
Daniel Dunbar196ea442010-04-06 01:07:44 +000034 bool IsSigned : 1;
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000035};
36
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000037/// CGRecordLayout - This class handles struct and union layout info while
38/// lowering AST types to LLVM types.
Daniel Dunbar034299e2010-03-31 01:09:11 +000039///
40/// These layout objects are only created on demand as IR generation requires.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000041class CGRecordLayout {
Daniel Dunbar034299e2010-03-31 01:09:11 +000042 friend class CodeGenTypes;
43
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000044 CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT
45 void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT
46
Daniel Dunbar034299e2010-03-31 01:09:11 +000047private:
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000048 /// The LLVMType corresponding to this record layout.
49 const llvm::Type *LLVMType;
50
Daniel Dunbar034299e2010-03-31 01:09:11 +000051 /// Map from (non-bit-field) struct field to the corresponding llvm struct
52 /// type field no. This info is populated by record builder.
53 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
54
55 /// Map from (bit-field) struct field to the corresponding llvm struct type
56 /// field no. This info is populated by record builder.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000057 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar034299e2010-03-31 01:09:11 +000058
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000059 /// Whether one of the fields in this record layout is a pointer to data
60 /// member, or a struct that contains pointer to data member.
Daniel Dunbar034299e2010-03-31 01:09:11 +000061 bool ContainsPointerToDataMember : 1;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000062
63public:
64 CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember)
65 : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) {}
66
Daniel Dunbar034299e2010-03-31 01:09:11 +000067 /// \brief Return the LLVM type associated with this record.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000068 const llvm::Type *getLLVMType() const {
69 return LLVMType;
70 }
71
Daniel Dunbar034299e2010-03-31 01:09:11 +000072 /// \brief Check whether this struct contains pointers to data members.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000073 bool containsPointerToDataMember() const {
74 return ContainsPointerToDataMember;
75 }
Daniel Dunbar034299e2010-03-31 01:09:11 +000076
77 /// \brief Return the BitFieldInfo that corresponds to the field FD.
78 unsigned getLLVMFieldNo(const FieldDecl *FD) const {
79 assert(!FD->isBitField() && "Invalid call for bit-field decl!");
80 assert(FieldInfo.count(FD) && "Invalid field for record!");
81 return FieldInfo.lookup(FD);
82 }
83
84 /// \brief Return llvm::StructType element number that corresponds to the
85 /// field FD.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000086 const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
Daniel Dunbar034299e2010-03-31 01:09:11 +000087 assert(FD->isBitField() && "Invalid call for non bit-field decl!");
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000088 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
Daniel Dunbar034299e2010-03-31 01:09:11 +000089 it = BitFields.find(FD);
90 assert(it != BitFields.end() && "Unable to find bitfield info");
91 return it->second;
92 }
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000093};
94
95} // end namespace CodeGen
96} // end namespace clang
97
98#endif