slang.h revision cc0efad052e944f64bb71cfdaa8a825e30192e78
1#ifndef _SLANG_COMPILER_SLANG_H
2#define _SLANG_COMPILER_SLANG_H
3
4#include <cstdio>
5#include <string>
6#include <vector>
7
8#include "llvm/ADT/OwningPtr.h"
9#include "llvm/ADT/StringRef.h"
10#include "llvm/ADT/IntrusiveRefCntPtr.h"
11
12#include "clang/Basic/TargetOptions.h"
13
14#include "slang_pragma_recorder.h"
15#include "slang_diagnostic_buffer.h"
16
17namespace llvm {
18  class raw_ostream;
19}
20
21namespace clang {
22  class Diagnostic;
23  class FileManager;
24  class SourceManager;
25  class LangOptions;
26  class Preprocessor;
27  class TargetOptions;
28  class CodeGenOptions;
29  class ASTContext;
30  class ASTConsumer;
31  class Backend;
32  class TargetInfo;
33}
34
35namespace slang {
36
37class Slang {
38  static clang::LangOptions LangOpts;
39  static clang::CodeGenOptions CodeGenOpts;
40
41  static bool GlobalInitialized;
42
43  static void GlobalInitialization();
44
45  static void LLVMErrorHandler(void *UserData, const std::string &Message);
46
47 public:
48  typedef enum {
49    OT_Dependency,
50    OT_Assembly,
51    OT_LLVMAssembly,
52    OT_Bitcode,
53    OT_Nothing,
54    OT_Object,
55
56    OT_Default = OT_Bitcode
57  } OutputType;
58
59 private:
60  // The diagnostics engine instance (for status reporting during compilation)
61  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();
66
67  // 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);
71
72  // Below is for parsing and code generation
73
74  // The file manager (for prepocessor doing the job such as header file search)
75  llvm::OwningPtr<clang::FileManager> mFileMgr;
76  void createFileManager();
77
78  // The source manager (responsible for the source code handling)
79  llvm::OwningPtr<clang::SourceManager> mSourceMgr;
80  void createSourceManager();
81
82  // The preprocessor (source code preprocessor)
83  llvm::OwningPtr<clang::Preprocessor> mPP;
84  void createPreprocessor();
85
86  // The AST context (the context to hold long-lived AST nodes)
87  llvm::OwningPtr<clang::ASTContext> mASTContext;
88  void createASTContext();
89
90  // The AST consumer, responsible for code generation
91  llvm::OwningPtr<clang::ASTConsumer> mBackend;
92
93  // Input file name
94  std::string mInputFileName;
95  std::string mOutputFileName;
96  std::string mDepTargetBCFileName;
97
98  OutputType mOT;
99
100  // Output stream
101  llvm::OwningPtr<llvm::raw_ostream> mOS;
102
103  std::vector<std::string> mIncludePaths;
104
105 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
127 public:
128  static const std::string TargetDescription;
129
130  static const llvm::StringRef PragmaMetadataName;
131
132  Slang(const char *Triple, const char *CPU, const char **Features);
133
134  bool setInputSource(llvm::StringRef InputFile, const char *Text,
135                      size_t TextLength);
136
137  bool setInputSource(llvm::StringRef InputFile);
138
139  inline const std::string &getInputFileName() const { return mInputFileName; }
140
141  inline void addIncludePath(const char *Path) {
142    mIncludePaths.push_back(Path);
143  }
144
145  inline void setOutputType(OutputType OT) { mOT = OT; }
146
147  bool setOutput(const char *outputFile);
148  inline const std::string &getOutputFileName() const {
149    return mOutputFileName;
150  }
151
152  bool setDepTargetBC(const char *targetBCFile);
153
154  int generateDepFile();
155  int compile();
156
157  inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
158
159  // Reset the slang compiler state such that it can be reused to compile
160  // another file
161  virtual void reset();
162
163  virtual ~Slang();
164};
165}
166
167#endif  // _SLANG_COMPILER_SLANG_H
168