aboutsummaryrefslogtreecommitdiff
path: root/CodeGen/CodeGenTypes.h
blob: 998175537b391471fb633a2615a47d6528f48594 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is the code that handles AST -> LLVM type lowering. 
//
//===----------------------------------------------------------------------===//

#ifndef CODEGEN_CODEGENTYPES_H
#define CODEGEN_CODEGENTYPES_H

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include <vector>

namespace llvm {
  class Module;
  class Type;
}

namespace clang {
  class ASTContext;
  class TagDecl;
  class TargetInfo;
  class QualType;
  class FunctionTypeProto;
 class FieldDecl;

namespace CodeGen {
  class CodeGenTypes;

  /// RecordOrganizer - This helper class, used by RecordLayoutInfo, layouts 
  /// structs and unions. It manages transient information used during layout.
  /// FIXME : At the moment assume 
  ///    - one to one mapping between AST FieldDecls and 
  ///      llvm::StructType elements.
  ///    - Ignore bit fields
  ///    - Ignore field aligments
  ///    - Ignore packed structs
  class RecordOrganizer {
  public:
    RecordOrganizer() : STy(NULL) {}
    
    /// addField - Add new field.
    void addField(const FieldDecl *FD);

    /// layoutFields - Do the actual work and lay out all fields. Create
    /// corresponding llvm struct type.  This should be invoked only after
    /// all fields are added.
    void layoutFields(CodeGenTypes &CGT);

    /// getLLVMType - Return associated llvm struct type. This may be NULL
    /// if fields are not laid out.
    llvm::Type *getLLVMType() {
      return STy;
    }

  private:
    llvm::Type *STy;
    llvm::SmallVector<const FieldDecl *, 8> FieldDecls;
  };

  /// RecordLayoutInfo - This class handles struct and union layout info while 
  /// lowering AST types to LLVM types.
  class RecordLayoutInfo {
    RecordLayoutInfo(); // DO NOT IMPLEMENT
  public:
    RecordLayoutInfo(RecordOrganizer *RO);

    /// getLLVMType - Return llvm type associated with this record.
    llvm::Type *getLLVMType() {
      return STy;
    }

  private:
    llvm::Type *STy;
  };
  
/// CodeGenTypes - This class organizes the cross-module state that is used
/// while lowering AST types to LLVM types.
class CodeGenTypes {
  ASTContext &Context;
  TargetInfo &Target;
  llvm::Module& TheModule;
  
  llvm::DenseMap<const TagDecl*, llvm::Type*> TagDeclTypes;

  /// RecordLayouts - This maps llvm struct type with corresponding 
  /// record layout info. 
  llvm::DenseMap<const llvm::Type*, RecordLayoutInfo *> RecordLayouts;

  /// FieldInfo - This maps struct field with corresponding llvm struct type
  /// field no. This info is populated by record organizer.
  llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;

public:
  CodeGenTypes(ASTContext &Ctx, llvm::Module &M);
  ~CodeGenTypes();
  
  TargetInfo &getTarget() const { return Target; }
  
  const llvm::Type *ConvertType(QualType T);
  void DecodeArgumentTypes(const FunctionTypeProto &FTP, 
                           std::vector<const llvm::Type*> &ArgTys);

  RecordLayoutInfo *getRecordLayoutInfo(const llvm::Type*);
  
  /// getLLVMFieldNo - Return llvm::StructType element number
  /// that corresponds to the field FD.
  unsigned getLLVMFieldNo(const FieldDecl *FD);

  /// addFieldInfo - Assign field number to field FD.
  void addFieldInfo(const FieldDecl *FD, unsigned No);
};

}  // end namespace CodeGen
}  // end namespace clang

#endif