blob: 5e2abb1bb38fc21e3f1afeeac0e74e0b96643c1a [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:
24 CGBitFieldInfo(unsigned FieldNo, unsigned Start, unsigned Size)
25 : FieldNo(FieldNo), Start(Start), Size(Size) {}
26
27 unsigned FieldNo;
28 unsigned Start;
29 unsigned Size;
30};
31
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000032/// CGRecordLayout - This class handles struct and union layout info while
33/// lowering AST types to LLVM types.
Daniel Dunbar034299e2010-03-31 01:09:11 +000034///
35/// These layout objects are only created on demand as IR generation requires.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000036class CGRecordLayout {
Daniel Dunbar034299e2010-03-31 01:09:11 +000037 friend class CodeGenTypes;
38
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000039 CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT
40 void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT
41
Daniel Dunbar034299e2010-03-31 01:09:11 +000042private:
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000043 /// The LLVMType corresponding to this record layout.
44 const llvm::Type *LLVMType;
45
Daniel Dunbar034299e2010-03-31 01:09:11 +000046 /// Map from (non-bit-field) struct field to the corresponding llvm struct
47 /// type field no. This info is populated by record builder.
48 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
49
50 /// Map from (bit-field) struct field to the corresponding llvm struct type
51 /// field no. This info is populated by record builder.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000052 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar034299e2010-03-31 01:09:11 +000053
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000054 /// Whether one of the fields in this record layout is a pointer to data
55 /// member, or a struct that contains pointer to data member.
Daniel Dunbar034299e2010-03-31 01:09:11 +000056 bool ContainsPointerToDataMember : 1;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000057
58public:
59 CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember)
60 : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) {}
61
Daniel Dunbar034299e2010-03-31 01:09:11 +000062 /// \brief Return the LLVM type associated with this record.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000063 const llvm::Type *getLLVMType() const {
64 return LLVMType;
65 }
66
Daniel Dunbar034299e2010-03-31 01:09:11 +000067 /// \brief Check whether this struct contains pointers to data members.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000068 bool containsPointerToDataMember() const {
69 return ContainsPointerToDataMember;
70 }
Daniel Dunbar034299e2010-03-31 01:09:11 +000071
72 /// \brief Return the BitFieldInfo that corresponds to the field FD.
73 unsigned getLLVMFieldNo(const FieldDecl *FD) const {
74 assert(!FD->isBitField() && "Invalid call for bit-field decl!");
75 assert(FieldInfo.count(FD) && "Invalid field for record!");
76 return FieldInfo.lookup(FD);
77 }
78
79 /// \brief Return llvm::StructType element number that corresponds to the
80 /// field FD.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000081 const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
Daniel Dunbar034299e2010-03-31 01:09:11 +000082 assert(FD->isBitField() && "Invalid call for non bit-field decl!");
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000083 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
Daniel Dunbar034299e2010-03-31 01:09:11 +000084 it = BitFields.find(FD);
85 assert(it != BitFields.end() && "Unable to find bitfield info");
86 return it->second;
87 }
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000088};
89
90} // end namespace CodeGen
91} // end namespace clang
92
93#endif