blob: 5ae49553ab588a3d46b8e5cb9542946c16c9cb79 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include <vector>
19
20namespace llvm {
Anders Carlsson4e533282007-08-17 22:00:32 +000021 class Module;
Reid Spencer5f016e22007-07-11 17:01:13 +000022 class Type;
Devang Patel30ec9972007-10-25 18:32:36 +000023 class PATypeHolder;
Devang Patel7a4718e2007-10-31 20:01:01 +000024 class TargetData;
Reid Spencer5f016e22007-07-11 17:01:13 +000025}
26
27namespace clang {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000028 class ASTContext;
Anders Carlssonc9e20912007-08-21 00:21:21 +000029 class TagDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000030 class TargetInfo;
31 class QualType;
Devang Patel30ec9972007-10-25 18:32:36 +000032 class Type;
Reid Spencer5f016e22007-07-11 17:01:13 +000033 class FunctionTypeProto;
Devang Patelb1e39892007-10-23 23:26:46 +000034 class FieldDecl;
35 class RecordDecl;
Devang Patelb84a06e2007-10-23 02:10:49 +000036
Reid Spencer5f016e22007-07-11 17:01:13 +000037namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000038 class CodeGenTypes;
39
Devang Patel88a981b2007-11-01 19:11:01 +000040 /// CGRecordLayout - This class handles struct and union layout info while
Devang Patelb84a06e2007-10-23 02:10:49 +000041 /// lowering AST types to LLVM types.
Devang Patel88a981b2007-11-01 19:11:01 +000042 class CGRecordLayout {
43 CGRecordLayout(); // DO NOT IMPLEMENT
Devang Patelb84a06e2007-10-23 02:10:49 +000044 public:
Devang Patel88a981b2007-11-01 19:11:01 +000045 CGRecordLayout(llvm::Type *T) : STy(T) {
Devang Patel057afdd2007-10-24 20:38:06 +000046 // FIXME : Collect info about fields that requires adjustments
47 // (i.e. fields that do not directly map to llvm struct fields.)
48 }
Devang Patelb84a06e2007-10-23 02:10:49 +000049
50 /// getLLVMType - Return llvm type associated with this record.
Devang Patelc4c429a2007-10-24 00:56:23 +000051 llvm::Type *getLLVMType() const {
Devang Patelb84a06e2007-10-23 02:10:49 +000052 return STy;
53 }
54
55 private:
56 llvm::Type *STy;
57 };
Reid Spencer5f016e22007-07-11 17:01:13 +000058
59/// CodeGenTypes - This class organizes the cross-module state that is used
60/// while lowering AST types to LLVM types.
61class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000062 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000064 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000065 const llvm::TargetData& TheTargetData;
Anders Carlssonc9e20912007-08-21 00:21:21 +000066
67 llvm::DenseMap<const TagDecl*, llvm::Type*> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000068
Devang Patel88a981b2007-11-01 19:11:01 +000069 /// CGRecordLayouts - This maps llvm struct type with corresponding
Devang Patelb84a06e2007-10-23 02:10:49 +000070 /// record layout info.
Devang Patel88a981b2007-11-01 19:11:01 +000071 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +000072 /// inline it in the map.
Devang Patel88a981b2007-11-01 19:11:01 +000073 llvm::DenseMap<const llvm::Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000074
75 /// FieldInfo - This maps struct field with corresponding llvm struct type
76 /// field no. This info is populated by record organizer.
77 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
78
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000079public:
Devang Patel159e3302007-11-07 01:57:13 +000080 class BitFieldInfo {
81 public:
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000082 explicit BitFieldInfo(unsigned short B, unsigned short S)
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +000083 : Begin(B), Size(S) {}
84
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000085 unsigned short Begin;
86 unsigned short Size;
Devang Patel159e3302007-11-07 01:57:13 +000087 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000088
89private:
Devang Patel159e3302007-11-07 01:57:13 +000090 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
91
Devang Patelb1e39892007-10-23 23:26:46 +000092 /// RecordTypesToResolve - This keeps track of record types that are not
93 /// yet incomplete. One llvm::OpaqueType is associated with each incomplete
94 /// record.
95 llvm::DenseMap<const RecordDecl *, llvm::Type *> RecordTypesToResolve;
96
Devang Patel30ec9972007-10-25 18:32:36 +000097 /// TypeHolderMap - This map keeps cache of llvm::Types (through PATypeHolder)
98 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
99 /// used instead of llvm::Type because it allows us to bypass potential
100 /// dangling type pointers due to type refinement on llvm side.
Devang Patel0ffe89a2007-10-30 20:46:47 +0000101 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeHolderMap;
Devang Patel5825ac22007-10-25 21:40:12 +0000102
103 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
104 /// method directly because it does not do any type caching. This method
105 /// is available only for ConvertType(). CovertType() is preferred
106 /// interface to convert type T into a llvm::Type.
107 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000108public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000109 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000110 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000111
Devang Pateld9e9ede2007-10-31 20:08:22 +0000112 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000114 ASTContext &getContext() const { return Context; }
Devang Patel5825ac22007-10-25 21:40:12 +0000115
116 /// ConvertType - Convert type T into a llvm::Type. Maintain and use
Chris Lattner19009e62008-01-09 18:47:25 +0000117 /// type cache through TypeHolderMap.
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 const llvm::Type *ConvertType(QualType T);
Chris Lattner19009e62008-01-09 18:47:25 +0000119
120 /// ConvertTypeForMem - Convert type T into a llvm::Type. Maintain and use
121 /// type cache through TypeHolderMap. This differs from ConvertType in that
122 /// it is used to convert to the memory representation for a type. For
123 /// example, the scalar representation for _Bool is i1, but the memory
124 /// representation is usually i8 or i32, depending on the target.
125 const llvm::Type *ConvertTypeForMem(QualType T);
126
127
Devang Patel88a981b2007-11-01 19:11:01 +0000128 const CGRecordLayout *getCGRecordLayout(const llvm::Type*) const;
Devang Patelb84a06e2007-10-23 02:10:49 +0000129
130 /// getLLVMFieldNo - Return llvm::StructType element number
131 /// that corresponds to the field FD.
132 unsigned getLLVMFieldNo(const FieldDecl *FD);
Chris Lattner19009e62008-01-09 18:47:25 +0000133
134public: // These are internal details of CGT that shouldn't be used externally.
135 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
136 std::vector<const llvm::Type*> &ArgTys);
Devang Patelb84a06e2007-10-23 02:10:49 +0000137
138 /// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000139 void addFieldInfo(const FieldDecl *FD, unsigned No);
140
141 /// addBitFieldInfo - Assign a start bit and a size to field FD.
142 void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
143
144 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field FD.
145 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146};
Chris Lattnera7674d82007-07-13 22:13:22 +0000147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148} // end namespace CodeGen
149} // end namespace clang
150
151#endif