blob: 67284d6e9e1eade9a76024138ab3dc2e1ef885af [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
Ying Wange2e522f2010-09-01 13:24:01 -070010#include <vector>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070011
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070012#include "llvm/Support/raw_ostream.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070013
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070014#include "llvm/ADT/OwningPtr.h"
15#include "llvm/ADT/StringRef.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070016
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070017#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/ASTContext.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070019
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070020#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/HeaderSearch.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070022
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070023#include "clang/Basic/Diagnostic.h"
24#include "clang/Sema/SemaDiagnostic.h"
25#include "clang/Basic/FileManager.h"
26#include "clang/Basic/TargetOptions.h"
27
28#include <cstdio>
29#include <string>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070030
31namespace llvm {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070032class TargetInfo;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070033}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070034
35namespace clang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070036class LangOptions;
37class CodeGenOptions;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070038class TargetOptions;
39}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070040
41namespace slang {
42
Shih-wei Liao462aefd2010-06-04 15:32:04 -070043class Slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070044 static clang::LangOptions LangOpts;
45 static clang::CodeGenOptions CodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047 static bool GlobalInitialized;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049 static void GlobalInitialization();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070050
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051 static void LLVMErrorHandler(void *UserData, const std::string &Message);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070053 private:
54 PragmaList mPragmas;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070055
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070056 // The diagnostics engine instance (for status reporting during compilation)
57 llvm::OwningPtr<clang::Diagnostic> mDiagnostics;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070058
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 llvm::OwningPtr<DiagnosticBuffer> mDiagClient;
60 inline void createDiagnostic() {
61 mDiagClient.reset(new DiagnosticBuffer());
62 mDiagnostics.reset(new clang::Diagnostic(mDiagClient.get()));
63 if (!mDiagnostics->setDiagnosticGroupMapping(
64 "implicit-function-declaration",
65 clang::diag::MAP_ERROR))
66 assert("Unable find option group implicit-function-declaration");
67 mDiagnostics->setDiagnosticMapping(
68 clang::diag::ext_typecheck_convert_discards_qualifiers,
69 clang::diag::MAP_ERROR);
70 return;
71 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070072
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 // The target being compiled for
74 clang::TargetOptions mTargetOpts;
75 llvm::OwningPtr<clang::TargetInfo> mTarget;
76 void createTarget(const char *Triple, const char *CPU, const char **Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070077
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 // Below is for parsing
Shih-wei Liao462aefd2010-06-04 15:32:04 -070079
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070080 // The file manager (for prepocessor doing the job such as header file search)
81 llvm::OwningPtr<clang::FileManager> mFileMgr;
82 inline void createFileManager() {
83 mFileMgr.reset(new clang::FileManager());
84 return;
85 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070087 // The source manager (responsible for the source code handling)
88 llvm::OwningPtr<clang::SourceManager> mSourceMgr;
89 inline void createSourceManager() {
90 mSourceMgr.reset(new clang::SourceManager(*mDiagnostics));
91 return;
92 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070093
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070094 // The preprocessor (source code preprocessor)
95 llvm::OwningPtr<clang::Preprocessor> mPP;
96 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070097
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070098 // The AST context (the context to hold long-lived AST nodes)
99 llvm::OwningPtr<clang::ASTContext> mASTContext;
100 inline void createASTContext() {
101 mASTContext.reset(new clang::ASTContext(LangOpts,
102 *mSourceMgr,
103 *mTarget,
104 mPP->getIdentifierTable(),
105 mPP->getSelectorTable(),
106 mPP->getBuiltinInfo(),
107 /* size_reserve */0));
108 return;
109 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700110
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111 // Context for RenderScript
112 llvm::OwningPtr<RSContext> mRSContext;
113 inline void createRSContext() {
114 mRSContext.reset(new RSContext(mPP.get(),
115 mASTContext.get(),
116 mTarget.get()));
117 return;
118 }
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700119
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700120 // The AST consumer, responsible for code generation
121 llvm::OwningPtr<Backend> mBackend;
122 inline void createBackend() {
123 mBackend.reset(new Backend(*mDiagnostics,
124 CodeGenOpts,
125 mTargetOpts,
126 mPragmas,
127 mOS.take(),
128 mOutputType,
129 *mSourceMgr,
130 mAllowRSPrefix));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700131
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700132 return;
133 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700134
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700135 inline void createRSBackend() {
136 mBackend.reset(new RSBackend(mRSContext.get(),
137 *mDiagnostics,
138 CodeGenOpts,
139 mTargetOpts,
140 mPragmas,
141 mOS.take(),
142 mOutputType,
143 *mSourceMgr,
144 mAllowRSPrefix));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700145
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700146 return;
147 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700148
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700149 // Input file name
150 std::string mInputFileName;
151 std::string mOutputFileName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700152
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700153 SlangCompilerOutputTy mOutputType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700154
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700155 // Output stream
156 llvm::OwningPtr<llvm::raw_ostream> mOS;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700157
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700158 bool mAllowRSPrefix;
Kirk Stewart1fd85792010-07-07 09:51:23 -0700159
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700160 std::vector<std::string> mIncludePaths;
Ying Wange2e522f2010-09-01 13:24:01 -0700161
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700162 public:
163 static const std::string TargetDescription;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700164
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700165 static const llvm::StringRef PragmaMetadataName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700166
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700167 Slang(const char *Triple, const char *CPU, const char **Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700168
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700169 bool setInputSource(llvm::StringRef inputFile, const char *text,
170 size_t textLength);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700171
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700172 bool setInputSource(llvm::StringRef inputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700173
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700174 void addIncludePath(const char *path);
Ying Wange2e522f2010-09-01 13:24:01 -0700175
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700176 void setOutputType(SlangCompilerOutputTy outputType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700177
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700178 inline bool setOutput(FILE *stream) {
179 if(stream == NULL)
180 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700181
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700182 mOS.reset(
183 new llvm::raw_fd_ostream(fileno(stream), /* shouldClose */false));
184 return true;
185 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700186
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700187 bool setOutput(const char *outputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700188
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700189 inline void allowRSPrefix() {
190 mAllowRSPrefix = true;
191 }
Kirk Stewart1fd85792010-07-07 09:51:23 -0700192
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700193 int compile();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700194
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700195 // The package name that's really applied will be filled in realPackageName.
196 // bSize is the buffer realPackageName size.
197 bool reflectToJava(const char *outputPackageName,
198 char *realPackageName, int bSize);
199 bool reflectToJavaPath(const char *outputPathName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700200
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700201 inline const char *getErrorMessage() {
202 return mDiagClient->str().c_str();
203 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700204
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700205 void getPragmas(size_t *actualStringCount, size_t maxStringCount,
206 char **strings);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700207
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700208 const char *exportFuncs();
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700209
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700210 // Reset the slang compiler state such that it can be reused to compile
211 // another file
212 inline void reset() {
213 // Seems there's no way to clear the diagnostics. We just re-create it.
214 createDiagnostic();
215 mOutputType = SlangCompilerOutput_Default;
216 return;
217 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700218
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700219 ~Slang();
220};
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700221
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700222}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700223
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700224#endif // _SLANG_COMPILER_SLANG_HPP