]> granicus.if.org Git - clang/blob - CodeGen/CodeGenTypes.h
166d10300b2ce141936aa192b7cad305fd239746
[clang] / CodeGen / CodeGenTypes.h
1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- 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 the code that handles AST -> LLVM type lowering. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef CODEGEN_CODEGENTYPES_H
15 #define CODEGEN_CODEGENTYPES_H
16
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include <vector>
20
21 namespace llvm {
22   class Module;
23   class Type;
24   class PATypeHolder;
25   class TargetData;
26 }
27
28 namespace clang {
29   class ASTContext;
30   class TagDecl;
31   class TargetInfo;
32   class QualType;
33   class Type;
34   class FunctionTypeProto;
35   class FieldDecl;
36   class RecordDecl;
37
38 namespace CodeGen {
39   class CodeGenTypes;
40
41   /// CGRecordLayout - This class handles struct and union layout info while 
42   /// lowering AST types to LLVM types.
43   class CGRecordLayout {
44     CGRecordLayout(); // DO NOT IMPLEMENT
45   public:
46     CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF) 
47       : STy(T), PaddingFields(PF) {
48       // FIXME : Collect info about fields that requires adjustments 
49       // (i.e. fields that do not directly map to llvm struct fields.)
50     }
51
52     /// getLLVMType - Return llvm type associated with this record.
53     llvm::Type *getLLVMType() const {
54       return STy;
55     }
56
57     bool isPaddingField(unsigned No) const {
58       return PaddingFields.count(No) != 0;
59     }
60
61     unsigned getNumPaddingFields() {
62       return PaddingFields.size();
63     }
64
65   private:
66     llvm::Type *STy;
67     llvm::SmallSet<unsigned, 8> PaddingFields;
68   };
69   
70 /// CodeGenTypes - This class organizes the cross-module state that is used
71 /// while lowering AST types to LLVM types.
72 class CodeGenTypes {
73   ASTContext &Context;
74   TargetInfo &Target;
75   llvm::Module& TheModule;
76   const llvm::TargetData& TheTargetData;
77   
78   llvm::DenseMap<const TagDecl*, llvm::PATypeHolder> TagDeclTypes;
79
80   /// CGRecordLayouts - This maps llvm struct type with corresponding 
81   /// record layout info. 
82   /// FIXME : If CGRecordLayout is less than 16 bytes then use 
83   /// inline it in the map.
84   llvm::DenseMap<const TagDecl*, CGRecordLayout *> CGRecordLayouts;
85
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
90 public:
91   class BitFieldInfo {
92   public:
93     explicit BitFieldInfo(unsigned short B, unsigned short S)
94       : Begin(B), Size(S) {}
95
96     unsigned short Begin;
97     unsigned short Size;
98   };
99
100 private:
101   llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
102
103   /// 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.
107   llvm::DenseMap<Type *, llvm::PATypeHolder> TypeHolderMap;
108
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);
114 public:
115   CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
116   ~CodeGenTypes();
117   
118   const llvm::TargetData &getTargetData() const { return TheTargetData; }
119   TargetInfo &getTarget() const { return Target; }
120   ASTContext &getContext() const { return Context; }
121
122   /// ConvertType - Convert type T into a llvm::Type. Maintain and use
123   /// type cache through TypeHolderMap.
124   const llvm::Type *ConvertType(QualType T);
125   
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   
134   const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
135   
136   /// getLLVMFieldNo - Return llvm::StructType element number
137   /// that corresponds to the field FD.
138   unsigned getLLVMFieldNo(const FieldDecl *FD);
139     
140   
141   /// UpdateCompletedType - When we find the full definition for a TagDecl,
142   /// replace the 'opaque' type we previously made for it if applicable.
143   void UpdateCompletedType(const TagDecl *TD);
144   
145 public:  // These are internal details of CGT that shouldn't be used externally.
146   void DecodeArgumentTypes(const FunctionTypeProto &FTP, 
147                            std::vector<const llvm::Type*> &ArgTys);
148
149   /// addFieldInfo - Assign field number to field FD.
150   void addFieldInfo(const FieldDecl *FD, unsigned No);
151
152   /// addBitFieldInfo - Assign a start bit and a size to field FD.
153   void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
154
155   /// getBitFieldInfo - Return the BitFieldInfo  that corresponds to the field
156   /// FD.
157   BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
158
159   /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
160   /// enum.
161   const llvm::Type *ConvertTagDeclType(QualType T, const TagDecl *TD);
162 };
163
164 }  // end namespace CodeGen
165 }  // end namespace clang
166
167 #endif