blob: a614135292e2c8f1d6d1cd5ce80ebdce04b05e35 [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 Change8c263a2010-10-12 00:35:29 +080018 class tool_output_file;
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 LLVMErrorHandler(void *UserData, const std::string &Message);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070044
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080045 public:
46 typedef enum {
Stephen Hinescc0efad2010-10-04 16:13:02 -070047 OT_Dependency,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080048 OT_Assembly,
49 OT_LLVMAssembly,
50 OT_Bitcode,
51 OT_Nothing,
52 OT_Object,
53
54 OT_Default = OT_Bitcode
55 } OutputType;
56
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070057 private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 // The diagnostics engine instance (for status reporting during compilation)
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080059 llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics;
60 // The clients of diagnostics engine. The ownership is taken by the
61 // mDiagnostics after creation.
62 DiagnosticBuffer *mDiagClient;
63 void createDiagnostic();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 // The target being compiled for
66 clang::TargetOptions mTargetOpts;
67 llvm::OwningPtr<clang::TargetInfo> mTarget;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070068 void createTarget(const std::string &Triple, const std::string &CPU,
69 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070070
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080071 // Below is for parsing and code generation
Shih-wei Liao462aefd2010-06-04 15:32:04 -070072
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 // The file manager (for prepocessor doing the job such as header file search)
74 llvm::OwningPtr<clang::FileManager> mFileMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080075 void createFileManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070076
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070077 // The source manager (responsible for the source code handling)
78 llvm::OwningPtr<clang::SourceManager> mSourceMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080079 void createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070080
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 // The preprocessor (source code preprocessor)
82 llvm::OwningPtr<clang::Preprocessor> mPP;
83 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070084
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 // The AST context (the context to hold long-lived AST nodes)
86 llvm::OwningPtr<clang::ASTContext> mASTContext;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080087 void createASTContext();
Shih-wei Liao001fb6d2010-06-21 11:17:11 -070088
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070089 // The AST consumer, responsible for code generation
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080090 llvm::OwningPtr<clang::ASTConsumer> mBackend;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070091
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070092 // Input file name
93 std::string mInputFileName;
94 std::string mOutputFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070095
Stephen Hines0b7ef1a2010-10-07 16:36:59 -070096 std::string mDepOutputFileName;
Stephen Hinescc0efad2010-10-04 16:13:02 -070097 std::string mDepTargetBCFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070098 std::vector<std::string> mAdditionalDepTargets;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070099
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800100 OutputType mOT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700101
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700102 // Output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800103 llvm::OwningPtr<llvm::tool_output_file> mOS;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700104 // Dependency output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800105 llvm::OwningPtr<llvm::tool_output_file> mDOS;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700106
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700107 std::vector<std::string> mIncludePaths;
Ying Wange2e522f2010-09-01 13:24:01 -0700108
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800109 protected:
110 PragmaList mPragmas;
111
112 inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; }
113 inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; }
114 inline clang::FileManager &getFileManager() { return *mFileMgr; }
115 inline clang::SourceManager &getSourceManager() { return *mSourceMgr; }
116 inline clang::Preprocessor &getPreprocessor() { return *mPP; }
117 inline clang::ASTContext &getASTContext() { return *mASTContext; }
118
119 inline const clang::TargetOptions &getTargetOptions() const
120 { return mTargetOpts; }
121
122 virtual void initDiagnostic() {}
123 virtual void initPreprocessor() {}
124 virtual void initASTContext() {}
125
126 virtual clang::ASTConsumer
127 *createBackend(const clang::CodeGenOptions& CodeGenOpts,
128 llvm::raw_ostream *OS,
129 OutputType OT);
130
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700131 public:
132 static const std::string TargetDescription;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700133
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700134 static const llvm::StringRef PragmaMetadataName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700135
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700136 static void GlobalInitialization();
137
138 Slang(const std::string &Triple, const std::string &CPU,
139 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700140
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800141 bool setInputSource(llvm::StringRef InputFile, const char *Text,
142 size_t TextLength);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700143
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800144 bool setInputSource(llvm::StringRef InputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700145
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800146 inline const std::string &getInputFileName() const { return mInputFileName; }
Ying Wange2e522f2010-09-01 13:24:01 -0700147
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700148 inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
149 mIncludePaths = IncludePaths;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700150 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700151
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800152 inline void setOutputType(OutputType OT) { mOT = OT; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700153
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700154 bool setOutput(const char *OutputFile);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800155 inline const std::string &getOutputFileName() const {
156 return mOutputFileName;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700157 }
Kirk Stewart1fd85792010-07-07 09:51:23 -0700158
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700159 bool setDepOutput(const char *OutputFile);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700160 inline void setDepTargetBC(const char *TargetBCFile) {
161 mDepTargetBCFileName = TargetBCFile;
162 }
163 inline void setAdditionalDepTargets(
164 const std::vector<std::string> &AdditionalDepTargets) {
165 mAdditionalDepTargets = AdditionalDepTargets;
166 }
Stephen Hinescc0efad2010-10-04 16:13:02 -0700167
168 int generateDepFile();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700169 int compile();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700170
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800171 inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700172
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173 // Reset the slang compiler state such that it can be reused to compile
174 // another file
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800175 virtual void reset();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700176
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800177 virtual ~Slang();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700178};
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700179}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700180
zonr6315f762010-10-05 15:35:14 +0800181#endif // _SLANG_COMPILER_SLANG_H