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" |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | namespace llvm { |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 23 | class Module; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | class Type; |
| 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; |
| 32 | class FunctionTypeProto; |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 33 | class FieldDecl; |
| 34 | class RecordDecl; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 35 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | namespace CodeGen { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 37 | class CodeGenTypes; |
| 38 | |
| 39 | /// RecordOrganizer - This helper class, used by RecordLayoutInfo, layouts |
| 40 | /// structs and unions. It manages transient information used during layout. |
| 41 | /// FIXME : At the moment assume |
| 42 | /// - one to one mapping between AST FieldDecls and |
| 43 | /// llvm::StructType elements. |
| 44 | /// - Ignore bit fields |
| 45 | /// - Ignore field aligments |
| 46 | /// - Ignore packed structs |
| 47 | class RecordOrganizer { |
| 48 | public: |
| 49 | RecordOrganizer() : STy(NULL) {} |
| 50 | |
| 51 | /// addField - Add new field. |
| 52 | void addField(const FieldDecl *FD); |
| 53 | |
| 54 | /// layoutFields - Do the actual work and lay out all fields. Create |
| 55 | /// corresponding llvm struct type. This should be invoked only after |
| 56 | /// all fields are added. |
| 57 | void layoutFields(CodeGenTypes &CGT); |
| 58 | |
| 59 | /// getLLVMType - Return associated llvm struct type. This may be NULL |
| 60 | /// if fields are not laid out. |
| 61 | llvm::Type *getLLVMType() { |
| 62 | return STy; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | llvm::Type *STy; |
| 67 | llvm::SmallVector<const FieldDecl *, 8> FieldDecls; |
| 68 | }; |
| 69 | |
| 70 | /// RecordLayoutInfo - This class handles struct and union layout info while |
| 71 | /// lowering AST types to LLVM types. |
| 72 | class RecordLayoutInfo { |
| 73 | RecordLayoutInfo(); // DO NOT IMPLEMENT |
| 74 | public: |
| 75 | RecordLayoutInfo(RecordOrganizer *RO); |
| 76 | |
| 77 | /// getLLVMType - Return llvm type associated with this record. |
| 78 | llvm::Type *getLLVMType() { |
| 79 | return STy; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | llvm::Type *STy; |
| 84 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | |
| 86 | /// CodeGenTypes - This class organizes the cross-module state that is used |
| 87 | /// while lowering AST types to LLVM types. |
| 88 | class CodeGenTypes { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 89 | ASTContext &Context; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | TargetInfo &Target; |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 91 | llvm::Module& TheModule; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 92 | |
| 93 | llvm::DenseMap<const TagDecl*, llvm::Type*> TagDeclTypes; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 94 | |
| 95 | /// RecordLayouts - This maps llvm struct type with corresponding |
| 96 | /// record layout info. |
Devang Patel | 71bcb09 | 2007-10-24 00:32:16 +0000 | [diff] [blame] | 97 | /// FIXME : If RecordLayoutInfo is less than 16 bytes then use |
| 98 | /// inline it in the map. |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 99 | llvm::DenseMap<const llvm::Type*, RecordLayoutInfo *> RecordLayouts; |
| 100 | |
| 101 | /// FieldInfo - This maps struct field with corresponding llvm struct type |
| 102 | /// field no. This info is populated by record organizer. |
| 103 | llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; |
| 104 | |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 105 | /// RecordTypesToResolve - This keeps track of record types that are not |
| 106 | /// yet incomplete. One llvm::OpaqueType is associated with each incomplete |
| 107 | /// record. |
| 108 | llvm::DenseMap<const RecordDecl *, llvm::Type *> RecordTypesToResolve; |
| 109 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | public: |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 111 | CodeGenTypes(ASTContext &Ctx, llvm::Module &M); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 112 | ~CodeGenTypes(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | |
| 114 | TargetInfo &getTarget() const { return Target; } |
| 115 | |
| 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 | |
| 120 | RecordLayoutInfo *getRecordLayoutInfo(const llvm::Type*); |
| 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. |
| 127 | void addFieldInfo(const FieldDecl *FD, unsigned No); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | }; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | } // end namespace CodeGen |
| 131 | } // end namespace clang |
| 132 | |
| 133 | #endif |