blob: fa6cfbd74b82531923d89388e9066ec9e12c893f [file] [log] [blame]
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001#ifndef _SLANG_COMPILER_SLANG_HPP
2# define _SLANG_COMPILER_SLANG_HPP
3
4#include "slang_backend.hpp"
5#include "slang_rs_context.hpp"
6#include "slang_rs_backend.hpp"
7#include "slang_pragma_recorder.hpp"
8#include "slang_diagnostic_buffer.hpp"
9
10#include <cstdio>
11#include <string>
Ying Wange2e522f2010-09-01 13:24:01 -070012#include <vector>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070013
14#include "llvm/Support/raw_ostream.h" /* for class llvm::raw_ostream */
15
16#include "llvm/ADT/OwningPtr.h" /* for class llvm::OwningPtr */
17#include "llvm/ADT/StringRef.h" /* for class llvm::StringRef */
18
19#include "clang/AST/ASTConsumer.h" /* for class clang::ASTConsumer */
20#include "clang/AST/ASTContext.h" /* for class clang::ASTContext */
21
22#include "clang/Lex/Preprocessor.h" /* for class clang::Preprocessor */
23#include "clang/Lex/HeaderSearch.h" /* for class clang::HeaderSearch */
24
25#include "clang/Basic/Diagnostic.h" /* for class clang::Diagnostic, class clang::DiagnosticClient, class clang::DiagnosticInfo */
Kirk Stewartb0cadb32010-07-20 17:26:06 -070026#include "clang/Sema/SemaDiagnostic.h" /* for members of clang::diag */
Shih-wei Liao462aefd2010-06-04 15:32:04 -070027#include "clang/Basic/FileManager.h" /* for class clang::FileManager and class clang::FileEntry */
28#include "clang/Basic/TargetOptions.h" /* for class clang::TargetOptions */
29
30namespace llvm {
31
32class Twine;
33class TargetInfo;
34
35} /* namespace llvm */
36
37namespace clang {
38
39class LangOptions;
40class CodeGenOptions;
41
42} /* namespace clang */
43
44namespace slang {
45
46using namespace clang;
47
48class Slang {
49 static LangOptions LangOpts;
50 static CodeGenOptions CodeGenOpts;
51
52 static bool GlobalInitialized;
53
54 static void GlobalInitialization();
55
56 static void LLVMErrorHandler(void *UserData, const std::string &Message);
57
58private:
59 PragmaList mPragmas;
60
61 /* The diagnostics engine instance (for status reporting during compilation) */
62 llvm::OwningPtr<Diagnostic> mDiagnostics;
63
64 llvm::OwningPtr<DiagnosticBuffer> mDiagClient;
65 inline void createDiagnostic() {
66 mDiagClient.reset(new DiagnosticBuffer());
67 mDiagnostics.reset(new Diagnostic(mDiagClient.get()));
Kirk Stewart6322c932010-06-10 16:34:34 -070068 bool optionNotFound = mDiagnostics->setDiagnosticGroupMapping("implicit-function-declaration", clang::diag::MAP_ERROR);
69 assert(!optionNotFound && "Unable find option group implicit-function-declaration");
Kirk Stewartb0cadb32010-07-20 17:26:06 -070070 mDiagnostics->setDiagnosticMapping(clang::diag::ext_typecheck_convert_discards_qualifiers, clang::diag::MAP_ERROR);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071 return;
72 }
73
74 /* The target being compiled for */
75 TargetOptions mTargetOpts;
76 llvm::OwningPtr<TargetInfo> mTarget;
77 void createTarget(const char* Triple, const char* CPU, const char** Features);
78
79 /**** Below is for parsing ****/
80
81 /* The file manager (for prepocessor doing the job such as header file search) */
82 llvm::OwningPtr<FileManager> mFileMgr;
83 inline void createFileManager() { mFileMgr.reset(new FileManager()); return; }
84
85 /* The source manager (responsible for the source code handling) */
86 llvm::OwningPtr<SourceManager> mSourceMgr; /* The source manager */
87 inline void createSourceManager() { mSourceMgr.reset(new SourceManager(*mDiagnostics)); return; }
88
89 /* The preprocessor (source code preprocessor) */
90 llvm::OwningPtr<Preprocessor> mPP;
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -070091 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070092
Shih-wei Liao462aefd2010-06-04 15:32:04 -070093 /* The AST context (the context to hold long-lived AST nodes) */
94 llvm::OwningPtr<ASTContext> mASTContext;
95 inline void createASTContext() {
96 mASTContext.reset(new ASTContext(LangOpts,
97 *mSourceMgr,
98 *mTarget,
99 mPP->getIdentifierTable(),
100 mPP->getSelectorTable(),
101 mPP->getBuiltinInfo()));
102 return;
103 }
104
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700105 /* Context for RenderScript */
106 llvm::OwningPtr<RSContext> mRSContext;
107 inline void createRSContext() {
108 mRSContext.reset(new RSContext(mPP.get(),
109 mASTContext.get(),
110 mTarget.get()));
111 return;
112 }
113
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700114 /* The AST consumer, responsible for code generation */
115 llvm::OwningPtr<Backend> mBackend;
116 inline void createBackend() {
117 mBackend.reset(new Backend(*mDiagnostics,
118 CodeGenOpts,
119 mTargetOpts,
120 mPragmas,
121 mOS.take(),
Kirk Stewart6b226742010-06-11 10:51:12 -0700122 mOutputType,
Kirk Stewart1fd85792010-07-07 09:51:23 -0700123 *mSourceMgr,
124 mAllowRSPrefix));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700125
126 return;
127 }
128
129 inline void createRSBackend() {
130 mBackend.reset(new RSBackend(mRSContext.get(),
131 *mDiagnostics,
132 CodeGenOpts,
133 mTargetOpts,
134 mPragmas,
135 mOS.take(),
Kirk Stewart6b226742010-06-11 10:51:12 -0700136 mOutputType,
Kirk Stewart1fd85792010-07-07 09:51:23 -0700137 *mSourceMgr,
138 mAllowRSPrefix));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700139
140 return;
141 }
142
143 /* Input file name */
144 std::string mInputFileName;
145 std::string mOutputFileName;
146
147 SlangCompilerOutputTy mOutputType;
148
149 /* Output stream */
150 llvm::OwningPtr<llvm::raw_ostream> mOS;
151
Kirk Stewart1fd85792010-07-07 09:51:23 -0700152 bool mAllowRSPrefix;
153
Ying Wange2e522f2010-09-01 13:24:01 -0700154 std::vector<std::string> mIncludePaths;
155
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156public:
157 static const std::string TargetDescription;
158
159 static const llvm::Twine PragmaMetadataName;
160
161 Slang(const char* Triple, const char* CPU, const char** Features);
162
163 bool setInputSource(llvm::StringRef inputFile, const char* text, size_t textLength);
164
165 bool setInputSource(llvm::StringRef inputFile);
166
Ying Wange2e522f2010-09-01 13:24:01 -0700167 void addIncludePath(const char* path);
168
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700169 void setOutputType(SlangCompilerOutputTy outputType);
170
171 inline bool setOutput(FILE* stream) {
172 if(stream == NULL)
173 return false;
174
175 mOS.reset( new llvm::raw_fd_ostream(fileno(stream), /* shouldClose */false) );
176 return true;
177 }
178
179 bool setOutput(const char* outputFile);
180
Kirk Stewart1fd85792010-07-07 09:51:23 -0700181 inline void allowRSPrefix() {
182 mAllowRSPrefix = true;
183 }
184
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700185 int compile();
186
187 bool reflectToJava(const char* outputPackageName);
Shih-wei Liao6de89272010-07-15 15:26:20 -0700188 bool reflectToJavaPath(const char* outputPathName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700189
190 inline const char* getErrorMessage() {
191 return mDiagClient->str().c_str();
192 }
193
194 void getPragmas(size_t* actualStringCount, size_t maxStringCount, char** strings);
195
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700196 const char* exportFuncs();
197
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700198 /* Reset the slang compiler state such that it can be reused to compile another file */
199 inline void reset() {
200 /* Seems there's no way to clear the diagnostics. We just re-create it. */
201 createDiagnostic();
202 mOutputType = SlangCompilerOutput_Default;
203 return;
204 }
205
Shih-wei Liao0a3f20e2010-08-10 13:09:49 -0700206 // smallvector list_of_filename;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700207 ~Slang();
208}; /* class Slang */
209
210} /* namespace slang */
211
212#endif /* _SLANG_COMPILER_SLANG_HPP */