blob: db3ba9c2af4175d0ecc96e73968d0c882cbc90a1 [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
Devang Patel159e3302007-11-07 01:57:13 +000079 class BitFieldInfo {
80 public:
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +000081 explicit BitFieldInfo(unsigned B, unsigned S)
82 : Begin(B), Size(S) {}
83
Devang Patel159e3302007-11-07 01:57:13 +000084 unsigned Begin;
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +000085 unsigned Size;
Devang Patel159e3302007-11-07 01:57:13 +000086 };
87 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
88
Devang Patelb1e39892007-10-23 23:26:46 +000089 /// RecordTypesToResolve - This keeps track of record types that are not
90 /// yet incomplete. One llvm::OpaqueType is associated with each incomplete
91 /// record.
92 llvm::DenseMap<const RecordDecl *, llvm::Type *> RecordTypesToResolve;
93
Devang Patel30ec9972007-10-25 18:32:36 +000094 /// TypeHolderMap - This map keeps cache of llvm::Types (through PATypeHolder)
95 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
96 /// used instead of llvm::Type because it allows us to bypass potential
97 /// dangling type pointers due to type refinement on llvm side.
Devang Patel0ffe89a2007-10-30 20:46:47 +000098 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeHolderMap;
Devang Patel5825ac22007-10-25 21:40:12 +000099
100 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
101 /// method directly because it does not do any type caching. This method
102 /// is available only for ConvertType(). CovertType() is preferred
103 /// interface to convert type T into a llvm::Type.
104 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000105public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000106 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000107 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000108
Devang Pateld9e9ede2007-10-31 20:08:22 +0000109 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000111 ASTContext &getContext() const { return Context; }
Devang Patel5825ac22007-10-25 21:40:12 +0000112
113 /// ConvertType - Convert type T into a llvm::Type. Maintain and use
Chris Lattner19009e62008-01-09 18:47:25 +0000114 /// type cache through TypeHolderMap.
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 const llvm::Type *ConvertType(QualType T);
Chris Lattner19009e62008-01-09 18:47:25 +0000116
117 /// ConvertTypeForMem - Convert type T into a llvm::Type. Maintain and use
118 /// type cache through TypeHolderMap. This differs from ConvertType in that
119 /// it is used to convert to the memory representation for a type. For
120 /// example, the scalar representation for _Bool is i1, but the memory
121 /// representation is usually i8 or i32, depending on the target.
122 const llvm::Type *ConvertTypeForMem(QualType T);
123
124
Devang Patel88a981b2007-11-01 19:11:01 +0000125 const CGRecordLayout *getCGRecordLayout(const llvm::Type*) const;
Devang Patelb84a06e2007-10-23 02:10:49 +0000126
127 /// getLLVMFieldNo - Return llvm::StructType element number
128 /// that corresponds to the field FD.
129 unsigned getLLVMFieldNo(const FieldDecl *FD);
Chris Lattner19009e62008-01-09 18:47:25 +0000130
131public: // These are internal details of CGT that shouldn't be used externally.
132 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
133 std::vector<const llvm::Type*> &ArgTys);
Devang Patelb84a06e2007-10-23 02:10:49 +0000134
135 /// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000136 void addFieldInfo(const FieldDecl *FD, unsigned No);
137
138 /// addBitFieldInfo - Assign a start bit and a size to field FD.
139 void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
140
141 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field FD.
142 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143};
Chris Lattnera7674d82007-07-13 22:13:22 +0000144
Reid Spencer5f016e22007-07-11 17:01:13 +0000145} // end namespace CodeGen
146} // end namespace clang
147
148#endif