blob: 2fb8191cf2dcb1aadd6968181af7bfb101020a3a [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 {
Daniel Dunbarb97bff92010-04-12 18:14:18 +000016 class raw_ostream;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000017 class Type;
18}
19
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000020namespace clang {
21namespace CodeGen {
22
Daniel Dunbarb97bff92010-04-12 18:14:18 +000023/// Helper object for describing how to generate the code for access to a
24/// bit-field.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000025class CGBitFieldInfo {
26public:
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +000027 CGBitFieldInfo(const llvm::Type *FieldTy, unsigned FieldNo,
28 unsigned Start, unsigned Size, bool IsSigned)
29 : FieldTy(FieldTy), FieldNo(FieldNo),
30 Start(Start), Size(Size), IsSigned(IsSigned) {}
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000031
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +000032 const llvm::Type *FieldTy;
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000033 unsigned FieldNo;
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +000034
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000035 unsigned Start;
36 unsigned Size;
Daniel Dunbar196ea442010-04-06 01:07:44 +000037 bool IsSigned : 1;
Daniel Dunbarb97bff92010-04-12 18:14:18 +000038
39 void print(llvm::raw_ostream &OS) const;
40 void dump() const;
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000041};
42
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000043/// CGRecordLayout - This class handles struct and union layout info while
44/// lowering AST types to LLVM types.
Daniel Dunbar034299e2010-03-31 01:09:11 +000045///
46/// These layout objects are only created on demand as IR generation requires.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000047class CGRecordLayout {
Daniel Dunbar034299e2010-03-31 01:09:11 +000048 friend class CodeGenTypes;
49
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000050 CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT
51 void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT
52
Daniel Dunbar034299e2010-03-31 01:09:11 +000053private:
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000054 /// The LLVMType corresponding to this record layout.
55 const llvm::Type *LLVMType;
56
Daniel Dunbar034299e2010-03-31 01:09:11 +000057 /// Map from (non-bit-field) struct field to the corresponding llvm struct
58 /// type field no. This info is populated by record builder.
59 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
60
61 /// Map from (bit-field) struct field to the corresponding llvm struct type
62 /// field no. This info is populated by record builder.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000063 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar034299e2010-03-31 01:09:11 +000064
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000065 /// Whether one of the fields in this record layout is a pointer to data
66 /// member, or a struct that contains pointer to data member.
Daniel Dunbar034299e2010-03-31 01:09:11 +000067 bool ContainsPointerToDataMember : 1;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000068
69public:
70 CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember)
71 : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) {}
72
Daniel Dunbar034299e2010-03-31 01:09:11 +000073 /// \brief Return the LLVM type associated with this record.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000074 const llvm::Type *getLLVMType() const {
75 return LLVMType;
76 }
77
Daniel Dunbar034299e2010-03-31 01:09:11 +000078 /// \brief Check whether this struct contains pointers to data members.
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000079 bool containsPointerToDataMember() const {
80 return ContainsPointerToDataMember;
81 }
Daniel Dunbar034299e2010-03-31 01:09:11 +000082
83 /// \brief Return the BitFieldInfo that corresponds to the field FD.
84 unsigned getLLVMFieldNo(const FieldDecl *FD) const {
85 assert(!FD->isBitField() && "Invalid call for bit-field decl!");
86 assert(FieldInfo.count(FD) && "Invalid field for record!");
87 return FieldInfo.lookup(FD);
88 }
89
90 /// \brief Return llvm::StructType element number that corresponds to the
91 /// field FD.
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000092 const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const {
Daniel Dunbar034299e2010-03-31 01:09:11 +000093 assert(FD->isBitField() && "Invalid call for non bit-field decl!");
Daniel Dunbarcd3d5e72010-04-05 16:20:44 +000094 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator
Daniel Dunbar034299e2010-03-31 01:09:11 +000095 it = BitFields.find(FD);
96 assert(it != BitFields.end() && "Unable to find bitfield info");
97 return it->second;
98 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +000099
100 void print(llvm::raw_ostream &OS) const;
101 void dump() const;
Daniel Dunbar072d0bb2010-03-30 22:26:10 +0000102};
103
104} // end namespace CodeGen
105} // end namespace clang
106
107#endif