blob: 95aea85c8eb3541f2461ffbc03ad6f23ad231df8 [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//
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 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;
Reid Spencer5f016e22007-07-11 17:01:13 +000024}
25
26namespace clang {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000027 class ASTContext;
Anders Carlssonc9e20912007-08-21 00:21:21 +000028 class TagDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000029 class TargetInfo;
30 class QualType;
Devang Patel30ec9972007-10-25 18:32:36 +000031 class Type;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 class FunctionTypeProto;
Devang Patelb1e39892007-10-23 23:26:46 +000033 class FieldDecl;
34 class RecordDecl;
Devang Patelb84a06e2007-10-23 02:10:49 +000035
Reid Spencer5f016e22007-07-11 17:01:13 +000036namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000037 class CodeGenTypes;
38
Devang Patelb84a06e2007-10-23 02:10:49 +000039 /// RecordLayoutInfo - This class handles struct and union layout info while
40 /// lowering AST types to LLVM types.
41 class RecordLayoutInfo {
42 RecordLayoutInfo(); // DO NOT IMPLEMENT
43 public:
Devang Patel057afdd2007-10-24 20:38:06 +000044 RecordLayoutInfo(llvm::Type *T) : STy(T) {
45 // FIXME : Collect info about fields that requires adjustments
46 // (i.e. fields that do not directly map to llvm struct fields.)
47 }
Devang Patelb84a06e2007-10-23 02:10:49 +000048
49 /// getLLVMType - Return llvm type associated with this record.
Devang Patelc4c429a2007-10-24 00:56:23 +000050 llvm::Type *getLLVMType() const {
Devang Patelb84a06e2007-10-23 02:10:49 +000051 return STy;
52 }
53
54 private:
55 llvm::Type *STy;
56 };
Reid Spencer5f016e22007-07-11 17:01:13 +000057
58/// CodeGenTypes - This class organizes the cross-module state that is used
59/// while lowering AST types to LLVM types.
60class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000061 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000062 TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000063 llvm::Module& TheModule;
Anders Carlssonc9e20912007-08-21 00:21:21 +000064
65 llvm::DenseMap<const TagDecl*, llvm::Type*> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000066
67 /// RecordLayouts - This maps llvm struct type with corresponding
68 /// record layout info.
Devang Patel71bcb092007-10-24 00:32:16 +000069 /// FIXME : If RecordLayoutInfo is less than 16 bytes then use
70 /// inline it in the map.
Devang Patelb84a06e2007-10-23 02:10:49 +000071 llvm::DenseMap<const llvm::Type*, RecordLayoutInfo *> RecordLayouts;
72
73 /// FieldInfo - This maps struct field with corresponding llvm struct type
74 /// field no. This info is populated by record organizer.
75 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
76
Devang Patelb1e39892007-10-23 23:26:46 +000077 /// RecordTypesToResolve - This keeps track of record types that are not
78 /// yet incomplete. One llvm::OpaqueType is associated with each incomplete
79 /// record.
80 llvm::DenseMap<const RecordDecl *, llvm::Type *> RecordTypesToResolve;
81
Devang Patel30ec9972007-10-25 18:32:36 +000082 /// TypeHolderMap - This map keeps cache of llvm::Types (through PATypeHolder)
83 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
84 /// used instead of llvm::Type because it allows us to bypass potential
85 /// dangling type pointers due to type refinement on llvm side.
Devang Patel0ffe89a2007-10-30 20:46:47 +000086 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeHolderMap;
Devang Patel5825ac22007-10-25 21:40:12 +000087
88 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
89 /// method directly because it does not do any type caching. This method
90 /// is available only for ConvertType(). CovertType() is preferred
91 /// interface to convert type T into a llvm::Type.
92 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +000093public:
Anders Carlsson4e533282007-08-17 22:00:32 +000094 CodeGenTypes(ASTContext &Ctx, llvm::Module &M);
Devang Patelb84a06e2007-10-23 02:10:49 +000095 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +000096
97 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +000098 ASTContext &getContext() const { return Context; }
Devang Patel5825ac22007-10-25 21:40:12 +000099
100 /// ConvertType - Convert type T into a llvm::Type. Maintain and use
101 /// type cache through TypeHOlderMap.
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 const llvm::Type *ConvertType(QualType T);
103 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
104 std::vector<const llvm::Type*> &ArgTys);
Devang Patelb84a06e2007-10-23 02:10:49 +0000105
Devang Patelc4c429a2007-10-24 00:56:23 +0000106 const RecordLayoutInfo *getRecordLayoutInfo(const llvm::Type*) const;
Devang Patelb84a06e2007-10-23 02:10:49 +0000107
108 /// getLLVMFieldNo - Return llvm::StructType element number
109 /// that corresponds to the field FD.
110 unsigned getLLVMFieldNo(const FieldDecl *FD);
111
112 /// addFieldInfo - Assign field number to field FD.
113 void addFieldInfo(const FieldDecl *FD, unsigned No);
Reid Spencer5f016e22007-07-11 17:01:13 +0000114};
Chris Lattnera7674d82007-07-13 22:13:22 +0000115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116} // end namespace CodeGen
117} // end namespace clang
118
119#endif