blob: 7883ebbf63ff8f0611f7f09f314b84ddafd983eb [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 */
25#include "clang/Basic/FileManager.h" /* for class clang::FileManager and class clang::FileEntry */
26#include "clang/Basic/TargetOptions.h" /* for class clang::TargetOptions */
27
28namespace llvm {
29
30class Twine;
31class TargetInfo;
32
33} /* namespace llvm */
34
35namespace clang {
36
37class LangOptions;
38class CodeGenOptions;
39
40} /* namespace clang */
41
42namespace slang {
43
44using namespace clang;
45
46class Slang {
47 static LangOptions LangOpts;
48 static CodeGenOptions CodeGenOpts;
49
50 static bool GlobalInitialized;
51
52 static void GlobalInitialization();
53
54 static void LLVMErrorHandler(void *UserData, const std::string &Message);
55
56private:
57 PragmaList mPragmas;
58
59 /* The diagnostics engine instance (for status reporting during compilation) */
60 llvm::OwningPtr<Diagnostic> mDiagnostics;
61
62 llvm::OwningPtr<DiagnosticBuffer> mDiagClient;
63 inline void createDiagnostic() {
64 mDiagClient.reset(new DiagnosticBuffer());
65 mDiagnostics.reset(new Diagnostic(mDiagClient.get()));
Kirk Stewart6322c932010-06-10 16:34:34 -070066 bool optionNotFound = mDiagnostics->setDiagnosticGroupMapping("implicit-function-declaration", clang::diag::MAP_ERROR);
67 assert(!optionNotFound && "Unable find option group implicit-function-declaration");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070068 return;
69 }
70
71 /* The target being compiled for */
72 TargetOptions mTargetOpts;
73 llvm::OwningPtr<TargetInfo> mTarget;
74 void createTarget(const char* Triple, const char* CPU, const char** Features);
75
76 /**** Below is for parsing ****/
77
78 /* The file manager (for prepocessor doing the job such as header file search) */
79 llvm::OwningPtr<FileManager> mFileMgr;
80 inline void createFileManager() { mFileMgr.reset(new FileManager()); return; }
81
82 /* The source manager (responsible for the source code handling) */
83 llvm::OwningPtr<SourceManager> mSourceMgr; /* The source manager */
84 inline void createSourceManager() { mSourceMgr.reset(new SourceManager(*mDiagnostics)); return; }
85
86 /* The preprocessor (source code preprocessor) */
87 llvm::OwningPtr<Preprocessor> mPP;
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -070088 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070089
Shih-wei Liao462aefd2010-06-04 15:32:04 -070090 /* The AST context (the context to hold long-lived AST nodes) */
91 llvm::OwningPtr<ASTContext> mASTContext;
92 inline void createASTContext() {
93 mASTContext.reset(new ASTContext(LangOpts,
94 *mSourceMgr,
95 *mTarget,
96 mPP->getIdentifierTable(),
97 mPP->getSelectorTable(),
98 mPP->getBuiltinInfo()));
99 return;
100 }
101
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700102 /* Context for RenderScript */
103 llvm::OwningPtr<RSContext> mRSContext;
104 inline void createRSContext() {
105 mRSContext.reset(new RSContext(mPP.get(),
106 mASTContext.get(),
107 mTarget.get()));
108 return;
109 }
110
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700111 /* The AST consumer, responsible for code generation */
112 llvm::OwningPtr<Backend> mBackend;
113 inline void createBackend() {
114 mBackend.reset(new Backend(*mDiagnostics,
115 CodeGenOpts,
116 mTargetOpts,
117 mPragmas,
118 mOS.take(),
Kirk Stewart6b226742010-06-11 10:51:12 -0700119 mOutputType,
Kirk Stewart1fd85792010-07-07 09:51:23 -0700120 *mSourceMgr,
121 mAllowRSPrefix));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700122
123 return;
124 }
125
126 inline void createRSBackend() {
127 mBackend.reset(new RSBackend(mRSContext.get(),
128 *mDiagnostics,
129 CodeGenOpts,
130 mTargetOpts,
131 mPragmas,
132 mOS.take(),
Kirk Stewart6b226742010-06-11 10:51:12 -0700133 mOutputType,
Kirk Stewart1fd85792010-07-07 09:51:23 -0700134 *mSourceMgr,
135 mAllowRSPrefix));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700136
137 return;
138 }
139
140 /* Input file name */
141 std::string mInputFileName;
142 std::string mOutputFileName;
143
144 SlangCompilerOutputTy mOutputType;
145
146 /* Output stream */
147 llvm::OwningPtr<llvm::raw_ostream> mOS;
148
Kirk Stewart1fd85792010-07-07 09:51:23 -0700149 bool mAllowRSPrefix;
150
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700151public:
152 static const std::string TargetDescription;
153
154 static const llvm::Twine PragmaMetadataName;
155
156 Slang(const char* Triple, const char* CPU, const char** Features);
157
158 bool setInputSource(llvm::StringRef inputFile, const char* text, size_t textLength);
159
160 bool setInputSource(llvm::StringRef inputFile);
161
162 void setOutputType(SlangCompilerOutputTy outputType);
163
164 inline bool setOutput(FILE* stream) {
165 if(stream == NULL)
166 return false;
167
168 mOS.reset( new llvm::raw_fd_ostream(fileno(stream), /* shouldClose */false) );
169 return true;
170 }
171
172 bool setOutput(const char* outputFile);
173
Kirk Stewart1fd85792010-07-07 09:51:23 -0700174 inline void allowRSPrefix() {
175 mAllowRSPrefix = true;
176 }
177
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700178 int compile();
179
180 bool reflectToJava(const char* outputPackageName);
Shih-wei Liao6de89272010-07-15 15:26:20 -0700181 bool reflectToJavaPath(const char* outputPathName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700182
183 inline const char* getErrorMessage() {
184 return mDiagClient->str().c_str();
185 }
186
187 void getPragmas(size_t* actualStringCount, size_t maxStringCount, char** strings);
188
189 /* Reset the slang compiler state such that it can be reused to compile another file */
190 inline void reset() {
191 /* Seems there's no way to clear the diagnostics. We just re-create it. */
192 createDiagnostic();
193 mOutputType = SlangCompilerOutput_Default;
194 return;
195 }
196
197 ~Slang();
198}; /* class Slang */
199
200} /* namespace slang */
201
202#endif /* _SLANG_COMPILER_SLANG_HPP */