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