blob: a528a6caa2acc3e4d8fe89cc89c35d3dacc6cba6 [file] [log] [blame]
zonr6315f762010-10-05 15:35:14 +08001#ifndef _SLANG_COMPILER_SLANG_H
2#define _SLANG_COMPILER_SLANG_H
Shih-wei Liao462aefd2010-06-04 15:32:04 -07003
zonr6315f762010-10-05 15:35:14 +08004#include <cstdio>
5#include <string>
Ying Wange2e522f2010-09-01 13:24:01 -07006#include <vector>
Shih-wei Liao462aefd2010-06-04 15:32:04 -07007
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07008#include "llvm/ADT/OwningPtr.h"
9#include "llvm/ADT/StringRef.h"
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080010#include "llvm/ADT/IntrusiveRefCntPtr.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070011
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070012#include "clang/Basic/TargetOptions.h"
13
zonr6315f762010-10-05 15:35:14 +080014#include "slang_pragma_recorder.h"
15#include "slang_diagnostic_buffer.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070016
17namespace llvm {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080018 class raw_ostream;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070019}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070020
21namespace clang {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080022 class Diagnostic;
23 class FileManager;
24 class SourceManager;
zonr6315f762010-10-05 15:35:14 +080025 class LangOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080026 class Preprocessor;
zonr6315f762010-10-05 15:35:14 +080027 class TargetOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080028 class CodeGenOptions;
29 class ASTContext;
30 class ASTConsumer;
31 class Backend;
32 class TargetInfo;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070033}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070034
35namespace slang {
36
Shih-wei Liao462aefd2010-06-04 15:32:04 -070037class Slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070038 static clang::LangOptions LangOpts;
39 static clang::CodeGenOptions CodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070040
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070041 static bool GlobalInitialized;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070042
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070043 static void GlobalInitialization();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070044
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045 static void LLVMErrorHandler(void *UserData, const std::string &Message);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080047 public:
48 typedef enum {
Stephen Hinescc0efad2010-10-04 16:13:02 -070049 OT_Dependency,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080050 OT_Assembly,
51 OT_LLVMAssembly,
52 OT_Bitcode,
53 OT_Nothing,
54 OT_Object,
55
56 OT_Default = OT_Bitcode
57 } OutputType;
58
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 // The diagnostics engine instance (for status reporting during compilation)
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080061 llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics;
62 // The clients of diagnostics engine. The ownership is taken by the
63 // mDiagnostics after creation.
64 DiagnosticBuffer *mDiagClient;
65 void createDiagnostic();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070066
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070067 // The target being compiled for
68 clang::TargetOptions mTargetOpts;
69 llvm::OwningPtr<clang::TargetInfo> mTarget;
70 void createTarget(const char *Triple, const char *CPU, const char **Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080072 // Below is for parsing and code generation
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 // The file manager (for prepocessor doing the job such as header file search)
75 llvm::OwningPtr<clang::FileManager> mFileMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080076 void createFileManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070077
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 // The source manager (responsible for the source code handling)
79 llvm::OwningPtr<clang::SourceManager> mSourceMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080080 void createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070081
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070082 // The preprocessor (source code preprocessor)
83 llvm::OwningPtr<clang::Preprocessor> mPP;
84 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070085
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070086 // The AST context (the context to hold long-lived AST nodes)
87 llvm::OwningPtr<clang::ASTContext> mASTContext;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080088 void createASTContext();
Shih-wei Liao001fb6d2010-06-21 11:17:11 -070089
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070090 // The AST consumer, responsible for code generation
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080091 llvm::OwningPtr<clang::ASTConsumer> mBackend;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070092
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070093 // Input file name
94 std::string mInputFileName;
95 std::string mOutputFileName;
Stephen Hinescc0efad2010-10-04 16:13:02 -070096 std::string mDepTargetBCFileName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070097
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080098 OutputType mOT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070099
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700100 // Output stream
101 llvm::OwningPtr<llvm::raw_ostream> mOS;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700102
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 std::vector<std::string> mIncludePaths;
Ying Wange2e522f2010-09-01 13:24:01 -0700104
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800105 protected:
106 PragmaList mPragmas;
107
108 inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; }
109 inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; }
110 inline clang::FileManager &getFileManager() { return *mFileMgr; }
111 inline clang::SourceManager &getSourceManager() { return *mSourceMgr; }
112 inline clang::Preprocessor &getPreprocessor() { return *mPP; }
113 inline clang::ASTContext &getASTContext() { return *mASTContext; }
114
115 inline const clang::TargetOptions &getTargetOptions() const
116 { return mTargetOpts; }
117
118 virtual void initDiagnostic() {}
119 virtual void initPreprocessor() {}
120 virtual void initASTContext() {}
121
122 virtual clang::ASTConsumer
123 *createBackend(const clang::CodeGenOptions& CodeGenOpts,
124 llvm::raw_ostream *OS,
125 OutputType OT);
126
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700127 public:
128 static const std::string TargetDescription;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700129
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700130 static const llvm::StringRef PragmaMetadataName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700131
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700132 Slang(const char *Triple, const char *CPU, const char **Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700133
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800134 bool setInputSource(llvm::StringRef InputFile, const char *Text,
135 size_t TextLength);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700136
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800137 bool setInputSource(llvm::StringRef InputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700138
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800139 inline const std::string &getInputFileName() const { return mInputFileName; }
Ying Wange2e522f2010-09-01 13:24:01 -0700140
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800141 inline void addIncludePath(const char *Path) {
142 mIncludePaths.push_back(Path);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700143 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700144
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800145 inline void setOutputType(OutputType OT) { mOT = OT; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700146
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800147 bool setOutput(const char *outputFile);
148 inline const std::string &getOutputFileName() const {
149 return mOutputFileName;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700150 }
Kirk Stewart1fd85792010-07-07 09:51:23 -0700151
Stephen Hinescc0efad2010-10-04 16:13:02 -0700152 bool setDepTargetBC(const char *targetBCFile);
153
154 int generateDepFile();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700155 int compile();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800157 inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700158
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700159 // Reset the slang compiler state such that it can be reused to compile
160 // another file
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800161 virtual void reset();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700162
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800163 virtual ~Slang();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700164};
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700165}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700166
zonr6315f762010-10-05 15:35:14 +0800167#endif // _SLANG_COMPILER_SLANG_H