1#ifndef _LLVM_NDK_CC_COMPILER_H
2#define _LLVM_NDK_CC_COMPILER_H
3
4#include <cstdio>
5#include <map>
6#include <string>
7#include <vector>
8
9#include "clang/Basic/DiagnosticIDs.h"
10#include "clang/Basic/LangOptions.h"
11#include "clang/Basic/TargetOptions.h"
12
13#include "clang/Frontend/CodeGenOptions.h"
14
15#include "llvm/ADT/IntrusiveRefCntPtr.h"
16#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/StringRef.h"
18
19namespace llvm {
20  class tool_output_file;
21  class raw_ostream;
22}
23
24namespace clang {
25  class Diagnostic;
26  class DiagnosticClient;
27  class FileManager;
28  class FileSystemOptions;
29  class SourceManager;
30  class Preprocessor;
31  class TargetOptions;
32  class ASTContext;
33  class ASTConsumer;
34  class Backend;
35  class TargetInfo;
36  class TextDiagnosticPrinter;
37}
38
39namespace ndkpc {
40
41#define DEFAULT_TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi"
42
43class Compiler {
44public:
45  typedef enum {
46    OT_Dependency,
47    OT_Assembly,
48    OT_LLVMAssembly,
49    OT_Bitcode,
50    OT_Nothing,
51    OT_Object,
52
53    OT_Default = OT_Bitcode
54  } OutputType;
55
56  Compiler();
57  ~Compiler();
58
59  void init(const std::string &Triple, const std::string &CPU,
60            const std::vector<std::string> &Features,
61            bool isCXX);
62
63  bool setInputSource(llvm::StringRef InputFile, const char *Text,
64                      size_t TextLength);
65
66  bool setInputSource(llvm::StringRef InputFile);
67
68  inline const std::string &getInputFileName() const { return mInputFileName; }
69
70  inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
71    mIncludePaths = IncludePaths;
72  }
73
74  inline void setPreDefinedSymbol(const std::map<std::string, std::string>& M) {
75    mPreDefinedSymbolMap = M;
76  }
77
78  inline void setOutputType(OutputType OT) { mOT = OT; }
79
80  bool setOutput(const char *OutputFile);
81  inline const std::string &getOutputFileName() const {
82    return mOutputFileName;
83  }
84
85  int compile();
86
87  // Reset the slang compiler state such that it can be reused to compile
88  // another file
89  void reset();
90
91private:
92  clang::LangOptions mLangOpts;
93  clang::CodeGenOptions mCodeGenOpts;
94
95  static void LLVMErrorHandler(void *UserData, const std::string &Message);
96
97  bool mInitialized;
98
99  // The diagnostics engine instance (for status reporting during compilation)
100  llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> mDiagnostics;
101  // The diagnostics id
102  llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> mDiagIDs;
103  // The clients of diagnostics engine. The ownership is taken by the
104  // mDiagnostics after creation.
105  clang::TextDiagnosticPrinter *mpDiagClient;
106  void createDiagnostic();
107
108  // The target being compiled for
109  clang::TargetOptions mTargetOpts;
110  llvm::OwningPtr<clang::TargetInfo> mTarget;
111  void createTarget(const std::string &Triple, const std::string &CPU,
112                    const std::vector<std::string> &Features);
113
114  // Below is for parsing and code generation
115
116  // The file manager (for prepocessor doing the job such as header file search)
117  llvm::OwningPtr<clang::FileManager> mFileMgr;
118  llvm::OwningPtr<clang::FileSystemOptions> mFileSysOpt;
119  void createFileManager();
120
121  // The source manager (responsible for the source code handling)
122  llvm::OwningPtr<clang::SourceManager> mSourceMgr;
123  void createSourceManager();
124
125  // The preprocessor (source code preprocessor)
126  llvm::OwningPtr<clang::Preprocessor> mPP;
127  void createPreprocessor();
128
129  // The AST context (the context to hold long-lived AST nodes)
130  llvm::OwningPtr<clang::ASTContext> mASTContext;
131  void createASTContext();
132
133  // The AST consumer, responsible for code generation
134  llvm::OwningPtr<clang::ASTConsumer> mBackend;
135
136  // Input file name
137  std::string mInputFileName;
138  std::string mOutputFileName;
139
140  OutputType mOT;
141
142  // Output stream
143  llvm::OwningPtr<llvm::tool_output_file> mOS;
144
145  std::vector<std::string> mIncludePaths;
146
147  std::map<std::string, std::string> mPreDefinedSymbolMap;
148  void injectPreDefined();
149
150  void initDiagnostic() {}
151  void initPreprocessor() {}
152  void initASTContext() {}
153
154  clang::ASTConsumer *createBackend(const clang::CodeGenOptions& CodeGenOpts,
155                                    llvm::raw_ostream *OS,
156                                    OutputType OT);
157};
158
159}
160
161#endif // _LLVM_NDK_CC_COMPILER_H
162