blob: 998175537b391471fb633a2615a47d6528f48594 [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"
Devang Patelb84a06e2007-10-23 02:10:49 +000018#include "llvm/ADT/SmallVector.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include <vector>
20
21namespace llvm {
Anders Carlsson4e533282007-08-17 22:00:32 +000022 class Module;
Reid Spencer5f016e22007-07-11 17:01:13 +000023 class Type;
24}
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;
31 class FunctionTypeProto;
Devang Patelb84a06e2007-10-23 02:10:49 +000032 class FieldDecl;
33
Reid Spencer5f016e22007-07-11 17:01:13 +000034namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000035 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 Spencer5f016e22007-07-11 17:01:13 +000083
84/// CodeGenTypes - This class organizes the cross-module state that is used
85/// while lowering AST types to LLVM types.
86class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000087 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000088 TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000089 llvm::Module& TheModule;
Anders Carlssonc9e20912007-08-21 00:21:21 +000090
91 llvm::DenseMap<const TagDecl*, llvm::Type*> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000092
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 Spencer5f016e22007-07-11 17:01:13 +0000101public:
Anders Carlsson4e533282007-08-17 22:00:32 +0000102 CodeGenTypes(ASTContext &Ctx, llvm::Module &M);
Devang Patelb84a06e2007-10-23 02:10:49 +0000103 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000104
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 Patelb84a06e2007-10-23 02:10:49 +0000110
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 Spencer5f016e22007-07-11 17:01:13 +0000119};
Chris Lattnera7674d82007-07-13 22:13:22 +0000120
Reid Spencer5f016e22007-07-11 17:01:13 +0000121} // end namespace CodeGen
122} // end namespace clang
123
124#endif