Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
| 20 | namespace llvm { |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 21 | class Module; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | class Type; |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 23 | class PATypeHolder; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 24 | class TargetData; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | namespace clang { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 28 | class ASTContext; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 29 | class TagDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | class TargetInfo; |
| 31 | class QualType; |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 32 | class Type; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | class FunctionTypeProto; |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 34 | class FieldDecl; |
| 35 | class RecordDecl; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 36 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | namespace CodeGen { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 38 | class CodeGenTypes; |
| 39 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 40 | /// CGRecordLayout - This class handles struct and union layout info while |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 41 | /// lowering AST types to LLVM types. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 42 | class CGRecordLayout { |
| 43 | CGRecordLayout(); // DO NOT IMPLEMENT |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 44 | public: |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 45 | CGRecordLayout(llvm::Type *T) : STy(T) { |
Devang Patel | 057afdd | 2007-10-24 20:38:06 +0000 | [diff] [blame] | 46 | // FIXME : Collect info about fields that requires adjustments |
| 47 | // (i.e. fields that do not directly map to llvm struct fields.) |
| 48 | } |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 49 | |
| 50 | /// getLLVMType - Return llvm type associated with this record. |
Devang Patel | c4c429a | 2007-10-24 00:56:23 +0000 | [diff] [blame] | 51 | llvm::Type *getLLVMType() const { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 52 | return STy; |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | llvm::Type *STy; |
| 57 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | |
| 59 | /// CodeGenTypes - This class organizes the cross-module state that is used |
| 60 | /// while lowering AST types to LLVM types. |
| 61 | class CodeGenTypes { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 62 | ASTContext &Context; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | TargetInfo &Target; |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 64 | llvm::Module& TheModule; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 65 | const llvm::TargetData& TheTargetData; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 66 | |
| 67 | llvm::DenseMap<const TagDecl*, llvm::Type*> TagDeclTypes; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 68 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 69 | /// CGRecordLayouts - This maps llvm struct type with corresponding |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 70 | /// record layout info. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 71 | /// FIXME : If CGRecordLayout is less than 16 bytes then use |
Devang Patel | 71bcb09 | 2007-10-24 00:32:16 +0000 | [diff] [blame] | 72 | /// inline it in the map. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 73 | llvm::DenseMap<const llvm::Type*, CGRecordLayout *> CGRecordLayouts; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 74 | |
| 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 Patel | 159e330 | 2007-11-07 01:57:13 +0000 | [diff] [blame^] | 79 | class BitFieldInfo { |
| 80 | public: |
| 81 | explicit BitFieldInfo(unsigned N, unsigned B, unsigned E) |
| 82 | : No(N), Begin(B), End(E) {} |
| 83 | private: |
| 84 | unsigned No; |
| 85 | unsigned Begin; |
| 86 | unsigned End; |
| 87 | }; |
| 88 | llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields; |
| 89 | |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 90 | /// RecordTypesToResolve - This keeps track of record types that are not |
| 91 | /// yet incomplete. One llvm::OpaqueType is associated with each incomplete |
| 92 | /// record. |
| 93 | llvm::DenseMap<const RecordDecl *, llvm::Type *> RecordTypesToResolve; |
| 94 | |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 95 | /// TypeHolderMap - This map keeps cache of llvm::Types (through PATypeHolder) |
| 96 | /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is |
| 97 | /// used instead of llvm::Type because it allows us to bypass potential |
| 98 | /// dangling type pointers due to type refinement on llvm side. |
Devang Patel | 0ffe89a | 2007-10-30 20:46:47 +0000 | [diff] [blame] | 99 | llvm::DenseMap<Type *, llvm::PATypeHolder> TypeHolderMap; |
Devang Patel | 5825ac2 | 2007-10-25 21:40:12 +0000 | [diff] [blame] | 100 | |
| 101 | /// ConvertNewType - Convert type T into a llvm::Type. Do not use this |
| 102 | /// method directly because it does not do any type caching. This method |
| 103 | /// is available only for ConvertType(). CovertType() is preferred |
| 104 | /// interface to convert type T into a llvm::Type. |
| 105 | const llvm::Type *ConvertNewType(QualType T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | public: |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 107 | CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 108 | ~CodeGenTypes(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | |
Devang Patel | d9e9ede | 2007-10-31 20:08:22 +0000 | [diff] [blame] | 110 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | TargetInfo &getTarget() const { return Target; } |
Devang Patel | 86522b9 | 2007-10-29 20:50:19 +0000 | [diff] [blame] | 112 | ASTContext &getContext() const { return Context; } |
Devang Patel | 5825ac2 | 2007-10-25 21:40:12 +0000 | [diff] [blame] | 113 | |
| 114 | /// ConvertType - Convert type T into a llvm::Type. Maintain and use |
| 115 | /// type cache through TypeHOlderMap. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | const llvm::Type *ConvertType(QualType T); |
| 117 | void DecodeArgumentTypes(const FunctionTypeProto &FTP, |
| 118 | std::vector<const llvm::Type*> &ArgTys); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 119 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 120 | const CGRecordLayout *getCGRecordLayout(const llvm::Type*) const; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 121 | |
| 122 | /// getLLVMFieldNo - Return llvm::StructType element number |
| 123 | /// that corresponds to the field FD. |
| 124 | unsigned getLLVMFieldNo(const FieldDecl *FD); |
| 125 | |
| 126 | /// addFieldInfo - Assign field number to field FD. |
Devang Patel | 159e330 | 2007-11-07 01:57:13 +0000 | [diff] [blame^] | 127 | void addFieldInfo(const FieldDecl *FD, unsigned No, unsigned Begin, |
| 128 | unsigned End); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | }; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 130 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | } // end namespace CodeGen |
| 132 | } // end namespace clang |
| 133 | |
| 134 | #endif |