blob: b60b9bab3a2cddbed95446523e2d2a6621c43b12 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the code that handles AST -> LLVM type lowering.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CODEGEN_CODEGENTYPES_H
15#define CODEGEN_CODEGENTYPES_H
16
Anders Carlssonc9e20912007-08-21 00:21:21 +000017#include "llvm/ADT/DenseMap.h"
Devang Pateleae15602008-02-05 02:39:50 +000018#include "llvm/ADT/SmallSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include <vector>
20
21namespace llvm {
Anders Carlsson4e533282007-08-17 22:00:32 +000022 class Module;
Reid Spencer5f016e22007-07-11 17:01:13 +000023 class Type;
Devang Patel30ec9972007-10-25 18:32:36 +000024 class PATypeHolder;
Devang Patel7a4718e2007-10-31 20:01:01 +000025 class TargetData;
Reid Spencer5f016e22007-07-11 17:01:13 +000026}
27
28namespace clang {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000029 class ASTContext;
Anders Carlssonc9e20912007-08-21 00:21:21 +000030 class TagDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000031 class TargetInfo;
32 class QualType;
Devang Patel30ec9972007-10-25 18:32:36 +000033 class Type;
Reid Spencer5f016e22007-07-11 17:01:13 +000034 class FunctionTypeProto;
Devang Patelb1e39892007-10-23 23:26:46 +000035 class FieldDecl;
36 class RecordDecl;
Devang Patelb84a06e2007-10-23 02:10:49 +000037
Reid Spencer5f016e22007-07-11 17:01:13 +000038namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000039 class CodeGenTypes;
40
Devang Patel88a981b2007-11-01 19:11:01 +000041 /// CGRecordLayout - This class handles struct and union layout info while
Devang Patelb84a06e2007-10-23 02:10:49 +000042 /// lowering AST types to LLVM types.
Devang Patel88a981b2007-11-01 19:11:01 +000043 class CGRecordLayout {
44 CGRecordLayout(); // DO NOT IMPLEMENT
Devang Patelb84a06e2007-10-23 02:10:49 +000045 public:
Devang Pateleae15602008-02-05 02:39:50 +000046 CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF)
47 : STy(T), PaddingFields(PF) {
Devang Patel057afdd2007-10-24 20:38:06 +000048 // FIXME : Collect info about fields that requires adjustments
49 // (i.e. fields that do not directly map to llvm struct fields.)
50 }
Devang Patelb84a06e2007-10-23 02:10:49 +000051
52 /// getLLVMType - Return llvm type associated with this record.
Devang Patelc4c429a2007-10-24 00:56:23 +000053 llvm::Type *getLLVMType() const {
Devang Patelb84a06e2007-10-23 02:10:49 +000054 return STy;
55 }
56
Devang Pateleae15602008-02-05 02:39:50 +000057 bool isPaddingField(unsigned No) const {
58 return PaddingFields.count(No) != 0;
59 }
60
61 unsigned getNumPaddingFields() {
62 return PaddingFields.size();
63 }
64
Devang Patelb84a06e2007-10-23 02:10:49 +000065 private:
66 llvm::Type *STy;
Devang Pateleae15602008-02-05 02:39:50 +000067 llvm::SmallSet<unsigned, 8> PaddingFields;
Devang Patelb84a06e2007-10-23 02:10:49 +000068 };
Reid Spencer5f016e22007-07-11 17:01:13 +000069
70/// CodeGenTypes - This class organizes the cross-module state that is used
71/// while lowering AST types to LLVM types.
72class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000073 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000075 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000076 const llvm::TargetData& TheTargetData;
Anders Carlssonc9e20912007-08-21 00:21:21 +000077
Chris Lattnerd86e6bc2008-02-05 08:06:13 +000078 llvm::DenseMap<const TagDecl*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000079
Devang Patel88a981b2007-11-01 19:11:01 +000080 /// CGRecordLayouts - This maps llvm struct type with corresponding
Devang Patelb84a06e2007-10-23 02:10:49 +000081 /// record layout info.
Devang Patel88a981b2007-11-01 19:11:01 +000082 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +000083 /// inline it in the map.
Chris Lattneraf319132008-02-05 06:55:31 +000084 llvm::DenseMap<const TagDecl*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000085
86 /// FieldInfo - This maps struct field with corresponding llvm struct type
87 /// field no. This info is populated by record organizer.
88 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
89
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000090public:
Devang Patel159e3302007-11-07 01:57:13 +000091 class BitFieldInfo {
92 public:
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000093 explicit BitFieldInfo(unsigned short B, unsigned short S)
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +000094 : Begin(B), Size(S) {}
95
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000096 unsigned short Begin;
97 unsigned short Size;
Devang Patel159e3302007-11-07 01:57:13 +000098 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000099
100private:
Devang Patel159e3302007-11-07 01:57:13 +0000101 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
102
Devang Patel30ec9972007-10-25 18:32:36 +0000103 /// TypeHolderMap - This map keeps cache of llvm::Types (through PATypeHolder)
104 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
105 /// used instead of llvm::Type because it allows us to bypass potential
106 /// dangling type pointers due to type refinement on llvm side.
Devang Patel0ffe89a2007-10-30 20:46:47 +0000107 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeHolderMap;
Devang Patel5825ac22007-10-25 21:40:12 +0000108
109 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
110 /// method directly because it does not do any type caching. This method
111 /// is available only for ConvertType(). CovertType() is preferred
112 /// interface to convert type T into a llvm::Type.
113 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000114public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000115 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000116 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000117
Devang Pateld9e9ede2007-10-31 20:08:22 +0000118 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000120 ASTContext &getContext() const { return Context; }
Devang Patel5825ac22007-10-25 21:40:12 +0000121
122 /// ConvertType - Convert type T into a llvm::Type. Maintain and use
Chris Lattner19009e62008-01-09 18:47:25 +0000123 /// type cache through TypeHolderMap.
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 const llvm::Type *ConvertType(QualType T);
Chris Lattner19009e62008-01-09 18:47:25 +0000125
126 /// ConvertTypeForMem - Convert type T into a llvm::Type. Maintain and use
127 /// type cache through TypeHolderMap. This differs from ConvertType in that
128 /// it is used to convert to the memory representation for a type. For
129 /// example, the scalar representation for _Bool is i1, but the memory
130 /// representation is usually i8 or i32, depending on the target.
131 const llvm::Type *ConvertTypeForMem(QualType T);
132
133
Chris Lattneraf319132008-02-05 06:55:31 +0000134 const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
Devang Patelb84a06e2007-10-23 02:10:49 +0000135
136 /// getLLVMFieldNo - Return llvm::StructType element number
137 /// that corresponds to the field FD.
138 unsigned getLLVMFieldNo(const FieldDecl *FD);
Chris Lattner19009e62008-01-09 18:47:25 +0000139
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000140
141 /// ForceTypeCompilation - When we find the definition for a type, we require
142 /// it to be recompiled, to update the lazy understanding of what it is in our
143 /// maps.
144 void ForceTypeCompilation(QualType T);
145
Chris Lattner19009e62008-01-09 18:47:25 +0000146public: // These are internal details of CGT that shouldn't be used externally.
147 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
148 std::vector<const llvm::Type*> &ArgTys);
Devang Patelb84a06e2007-10-23 02:10:49 +0000149
150 /// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000151 void addFieldInfo(const FieldDecl *FD, unsigned No);
152
153 /// addBitFieldInfo - Assign a start bit and a size to field FD.
154 void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
155
156 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field FD.
157 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158};
Chris Lattnera7674d82007-07-13 22:13:22 +0000159
Reid Spencer5f016e22007-07-11 17:01:13 +0000160} // end namespace CodeGen
161} // end namespace clang
162
163#endif