blob: 10d54129cede6ef1383edeed82a29cc5f59a8695 [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>
12
13#include "llvm/Support/raw_ostream.h" /* for class llvm::raw_ostream */
14
15#include "llvm/ADT/OwningPtr.h" /* for class llvm::OwningPtr */
16#include "llvm/ADT/StringRef.h" /* for class llvm::StringRef */
17
18#include "clang/AST/ASTConsumer.h" /* for class clang::ASTConsumer */
19#include "clang/AST/ASTContext.h" /* for class clang::ASTContext */
20
21#include "clang/Lex/Preprocessor.h" /* for class clang::Preprocessor */
22#include "clang/Lex/HeaderSearch.h" /* for class clang::HeaderSearch */
23
24#include "clang/Basic/Diagnostic.h" /* for class clang::Diagnostic, class clang::DiagnosticClient, class clang::DiagnosticInfo */
Kirk Stewartb0cadb32010-07-20 17:26:06 -070025#include "clang/Sema/SemaDiagnostic.h" /* for members of clang::diag */
Shih-wei Liao462aefd2010-06-04 15:32:04 -070026#include "clang/Basic/FileManager.h" /* for class clang::FileManager and class clang::FileEntry */
27#include "clang/Basic/TargetOptions.h" /* for class clang::TargetOptions */
28
29namespace llvm {
30
31class Twine;
32class TargetInfo;
33
34} /* namespace llvm */
35
36namespace clang {
37
38class LangOptions;
39class CodeGenOptions;
40
41} /* namespace clang */
42
43namespace slang {
44
45using namespace clang;
46
47class Slang {
48 static LangOptions LangOpts;
49 static CodeGenOptions CodeGenOpts;
50
51 static bool GlobalInitialized;
52
53 static void GlobalInitialization();
54
55 static void LLVMErrorHandler(void *UserData, const std::string &Message);
56
57private:
58 PragmaList mPragmas;
59
60 /* The diagnostics engine instance (for status reporting during compilation) */
61 llvm::OwningPtr<Diagnostic> mDiagnostics;
62
63 llvm::OwningPtr<DiagnosticBuffer> mDiagClient;
64 inline void createDiagnostic() {
65 mDiagClient.reset(new DiagnosticBuffer());
66 mDiagnostics.reset(new Diagnostic(mDiagClient.get()));
Kirk Stewart6322c932010-06-10 16:34:34 -070067 bool optionNotFound = mDiagnostics->setDiagnosticGroupMapping("implicit-function-declaration", clang::diag::MAP_ERROR);
68 assert(!optionNotFound && "Unable find option group implicit-function-declaration");
Kirk Stewartb0cadb32010-07-20 17:26:06 -070069 mDiagnostics->setDiagnosticMapping(clang::diag::ext_typecheck_convert_discards_qualifiers, clang::diag::MAP_ERROR);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070070 return;
71 }
72
73 /* The target being compiled for */
74 TargetOptions mTargetOpts;
75 llvm::OwningPtr<TargetInfo> mTarget;
76 void createTarget(const char* Triple, const char* CPU, const char** Features);
77
78 /**** Below is for parsing ****/
79
80 /* The file manager (for prepocessor doing the job such as header file search) */
81 llvm::OwningPtr<FileManager> mFileMgr;
82 inline void createFileManager() { mFileMgr.reset(new FileManager()); return; }
83
84 /* The source manager (responsible for the source code handling) */
85 llvm::OwningPtr<SourceManager> mSourceMgr; /* The source manager */
86 inline void createSourceManager() { mSourceMgr.reset(new SourceManager(*mDiagnostics)); return; }
87
88 /* The preprocessor (source code preprocessor) */
89 llvm::OwningPtr<Preprocessor> mPP;
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -070090 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070091
Shih-wei Liao462aefd2010-06-04 15:32:04 -070092 /* The AST context (the context to hold long-lived AST nodes) */
93 llvm::OwningPtr<ASTContext> mASTContext;
94 inline void createASTContext() {
95 mASTContext.reset(new ASTContext(LangOpts,
96 *mSourceMgr,
97 *mTarget,
98 mPP->getIdentifierTable(),
99 mPP->getSelectorTable(),
100 mPP->getBuiltinInfo()));
101 return;
102 }
103
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700104 /* Context for RenderScript */
105 llvm::OwningPtr<RSContext> mRSContext;
106 inline void createRSContext() {
107 mRSContext.reset(new RSContext(mPP.get(),
108 mASTContext.get(),
109 mTarget.get()));
110 return;
111 }
112
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700113 /* The AST consumer, responsible for code generation */
114 llvm::OwningPtr<Backend> mBackend;
115 inline void createBackend() {
116 mBackend.reset(new Backend(*mDiagnostics,
117 CodeGenOpts,
118 mTargetOpts,
119 mPragmas,
120 mOS.take(),
Kirk Stewart6b226742010-06-11 10:51:12 -0700121 mOutputType,
Kirk Stewart1fd85792010-07-07 09:51:23 -0700122 *mSourceMgr,
123 mAllowRSPrefix));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700124
125 return;
126 }
127
128 inline void createRSBackend() {
129 mBackend.reset(new RSBackend(mRSContext.get(),
130 *mDiagnostics,
131 CodeGenOpts,
132 mTargetOpts,
133 mPragmas,
134 mOS.take(),
Kirk Stewart6b226742010-06-11 10:51:12 -0700135 mOutputType,
Kirk Stewart1fd85792010-07-07 09:51:23 -0700136 *mSourceMgr,
137 mAllowRSPrefix));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700138
139 return;
140 }
141
142 /* Input file name */
143 std::string mInputFileName;
144 std::string mOutputFileName;
145
146 SlangCompilerOutputTy mOutputType;
147
148 /* Output stream */
149 llvm::OwningPtr<llvm::raw_ostream> mOS;
150
Kirk Stewart1fd85792010-07-07 09:51:23 -0700151 bool mAllowRSPrefix;
152
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700153public:
154 static const std::string TargetDescription;
155
156 static const llvm::Twine PragmaMetadataName;
157
158 Slang(const char* Triple, const char* CPU, const char** Features);
159
160 bool setInputSource(llvm::StringRef inputFile, const char* text, size_t textLength);
161
162 bool setInputSource(llvm::StringRef inputFile);
163
164 void setOutputType(SlangCompilerOutputTy outputType);
165
166 inline bool setOutput(FILE* stream) {
167 if(stream == NULL)
168 return false;
169
170 mOS.reset( new llvm::raw_fd_ostream(fileno(stream), /* shouldClose */false) );
171 return true;
172 }
173
174 bool setOutput(const char* outputFile);
175
Kirk Stewart1fd85792010-07-07 09:51:23 -0700176 inline void allowRSPrefix() {
177 mAllowRSPrefix = true;
178 }
179
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700180 int compile();
181
182 bool reflectToJava(const char* outputPackageName);
Shih-wei Liao6de89272010-07-15 15:26:20 -0700183 bool reflectToJavaPath(const char* outputPathName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700184
185 inline const char* getErrorMessage() {
186 return mDiagClient->str().c_str();
187 }
188
189 void getPragmas(size_t* actualStringCount, size_t maxStringCount, char** strings);
190
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700191 const char* exportFuncs();
192
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700193 /* Reset the slang compiler state such that it can be reused to compile another file */
194 inline void reset() {
195 /* Seems there's no way to clear the diagnostics. We just re-create it. */
196 createDiagnostic();
197 mOutputType = SlangCompilerOutput_Default;
198 return;
199 }
200
201 ~Slang();
202}; /* class Slang */
203
204} /* namespace slang */
205
206#endif /* _SLANG_COMPILER_SLANG_HPP */