summaryrefslogtreecommitdiff
path: root/Compiler.h
blob: 62097327c05bcb757a998c3c88d5cc846fbce8f5 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef _LLVM_NDK_CC_COMPILER_H
#define _LLVM_NDK_CC_COMPILER_H

#include <cstdio>
#include <map>
#include <string>
#include <vector>

#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/TargetOptions.h"

#include "clang/Frontend/CodeGenOptions.h"

#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"

namespace llvm {
  class tool_output_file;
  class raw_ostream;
}

namespace clang {
  class Diagnostic;
  class DiagnosticClient;
  class FileManager;
  class FileSystemOptions;
  class SourceManager;
  class Preprocessor;
  class TargetOptions;
  class ASTContext;
  class ASTConsumer;
  class Backend;
  class TargetInfo;
  class TextDiagnosticPrinter;
}

namespace ndkpc {

#define DEFAULT_TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi"

class Compiler {
public:
  typedef enum {
    OT_Dependency,
    OT_Assembly,
    OT_LLVMAssembly,
    OT_Bitcode,
    OT_Nothing,
    OT_Object,

    OT_Default = OT_Bitcode
  } OutputType;

  Compiler();
  ~Compiler();

  void init(const std::string &Triple, const std::string &CPU,
            const std::vector<std::string> &Features,
            bool isCXX);

  bool setInputSource(llvm::StringRef InputFile, const char *Text,
                      size_t TextLength);

  bool setInputSource(llvm::StringRef InputFile);

  inline const std::string &getInputFileName() const { return mInputFileName; }

  inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
    mIncludePaths = IncludePaths;
  }

  inline void setPreDefinedSymbol(const std::map<std::string, std::string>& M) {
    mPreDefinedSymbolMap = M;
  }

  inline void setOutputType(OutputType OT) { mOT = OT; }

  bool setOutput(const char *OutputFile);
  inline const std::string &getOutputFileName() const {
    return mOutputFileName;
  }

  int compile();

  // Reset the slang compiler state such that it can be reused to compile
  // another file
  void reset();

private:
  clang::LangOptions mLangOpts;
  clang::CodeGenOptions mCodeGenOpts;

  static void LLVMErrorHandler(void *UserData, const std::string &Message);

  bool mInitialized;

  // The diagnostics engine instance (for status reporting during compilation)
  llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> mDiagnostics;
  // The diagnostics id
  llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> mDiagIDs;
  // The clients of diagnostics engine. The ownership is taken by the
  // mDiagnostics after creation.
  clang::TextDiagnosticPrinter *mpDiagClient;
  void createDiagnostic();

  // The target being compiled for
  clang::TargetOptions mTargetOpts;
  llvm::OwningPtr<clang::TargetInfo> mTarget;
  void createTarget(const std::string &Triple, const std::string &CPU,
                    const std::vector<std::string> &Features);

  // Below is for parsing and code generation

  // The file manager (for prepocessor doing the job such as header file search)
  llvm::OwningPtr<clang::FileManager> mFileMgr;
  llvm::OwningPtr<clang::FileSystemOptions> mFileSysOpt;
  void createFileManager();

  // The source manager (responsible for the source code handling)
  llvm::OwningPtr<clang::SourceManager> mSourceMgr;
  void createSourceManager();

  // The preprocessor (source code preprocessor)
  llvm::OwningPtr<clang::Preprocessor> mPP;
  void createPreprocessor();

  // The AST context (the context to hold long-lived AST nodes)
  llvm::OwningPtr<clang::ASTContext> mASTContext;
  void createASTContext();

  // The AST consumer, responsible for code generation
  llvm::OwningPtr<clang::ASTConsumer> mBackend;

  // Input file name
  std::string mInputFileName;
  std::string mOutputFileName;

  OutputType mOT;

  // Output stream
  llvm::OwningPtr<llvm::tool_output_file> mOS;

  std::vector<std::string> mIncludePaths;

  std::map<std::string, std::string> mPreDefinedSymbolMap;
  void injectPreDefined();

  void initDiagnostic() {}
  void initPreprocessor() {}
  void initASTContext() {}

  clang::ASTConsumer *createBackend(const clang::CodeGenOptions& CodeGenOpts,
                                    llvm::raw_ostream *OS,
                                    OutputType OT);
};

}

#endif // _LLVM_NDK_CC_COMPILER_H