slang.h revision 8f4d972ef6c6796ffdde603e456979a7004d1e20
1/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_H_  // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_H_
19
20#include <cstdio>
21#include <string>
22#include <vector>
23
24#include "clang/Basic/TargetOptions.h"
25#include "clang/Lex/ModuleLoader.h"
26
27#include "llvm/ADT/IntrusiveRefCntPtr.h"
28#include "llvm/ADT/OwningPtr.h"
29#include "llvm/ADT/StringRef.h"
30
31#include "slang_diagnostic_buffer.h"
32#include "slang_pragma_recorder.h"
33
34namespace llvm {
35  class tool_output_file;
36}
37
38namespace clang {
39  class ASTConsumer;
40  class ASTContext;
41  class Backend;
42  class CodeGenOptions;
43  class Diagnostic;
44  class DiagnosticsEngine;
45  class FileManager;
46  class FileSystemOptions;
47  class LangOptions;
48  class Preprocessor;
49  class SourceManager;
50  class TargetInfo;
51  class TargetOptions;
52}
53
54namespace slang {
55
56class Slang : public clang::ModuleLoader {
57  static clang::LangOptions LangOpts;
58  static clang::CodeGenOptions CodeGenOpts;
59
60  static bool GlobalInitialized;
61
62  static void LLVMErrorHandler(void *UserData, const std::string &Message);
63
64 public:
65  enum OutputType {
66    OT_Dependency,
67    OT_Assembly,
68    OT_LLVMAssembly,
69    OT_Bitcode,
70    OT_Nothing,
71    OT_Object,
72
73    OT_Default = OT_Bitcode
74  };
75
76 private:
77  bool mInitialized;
78
79  // Diagnostics Mediator (An interface for both Producer and Consumer)
80  llvm::OwningPtr<clang::Diagnostic> mDiag;
81
82  // Diagnostics Engine (Producer and Diagnostics Reporter)
83  clang::DiagnosticsEngine *mDiagEngine;
84
85  // Diagnostics Consumer
86  // NOTE: The ownership is taken by mDiagEngine after creation.
87  DiagnosticBuffer *mDiagClient;
88
89  // The target being compiled for
90  clang::TargetOptions mTargetOpts;
91  llvm::OwningPtr<clang::TargetInfo> mTarget;
92  void createTarget(std::string const &Triple, std::string const &CPU,
93                    std::vector<std::string> const &Features);
94
95
96  // File manager (for prepocessor doing the job such as header file search)
97  llvm::OwningPtr<clang::FileManager> mFileMgr;
98  llvm::OwningPtr<clang::FileSystemOptions> mFileSysOpt;
99  void createFileManager();
100
101
102  // Source manager (responsible for the source code handling)
103  llvm::OwningPtr<clang::SourceManager> mSourceMgr;
104  void createSourceManager();
105
106
107  // Preprocessor (source code preprocessor)
108  llvm::OwningPtr<clang::Preprocessor> mPP;
109  void createPreprocessor();
110
111
112  // AST context (the context to hold long-lived AST nodes)
113  llvm::OwningPtr<clang::ASTContext> mASTContext;
114  void createASTContext();
115
116
117  // AST consumer, responsible for code generation
118  llvm::OwningPtr<clang::ASTConsumer> mBackend;
119
120
121  // File names
122  std::string mInputFileName;
123  std::string mOutputFileName;
124
125  std::string mDepOutputFileName;
126  std::string mDepTargetBCFileName;
127  std::vector<std::string> mAdditionalDepTargets;
128  std::vector<std::string> mGeneratedFileNames;
129
130  OutputType mOT;
131
132  // Output stream
133  llvm::OwningPtr<llvm::tool_output_file> mOS;
134
135  // Dependency output stream
136  llvm::OwningPtr<llvm::tool_output_file> mDOS;
137
138  std::vector<std::string> mIncludePaths;
139
140 protected:
141  PragmaList mPragmas;
142
143  clang::DiagnosticsEngine &getDiagnostics() { return *mDiagEngine; }
144  clang::TargetInfo const &getTargetInfo() const { return *mTarget; }
145  clang::FileManager &getFileManager() { return *mFileMgr; }
146  clang::SourceManager &getSourceManager() { return *mSourceMgr; }
147  clang::Preprocessor &getPreprocessor() { return *mPP; }
148  clang::ASTContext &getASTContext() { return *mASTContext; }
149
150  inline clang::TargetOptions const &getTargetOptions() const
151    { return mTargetOpts; }
152
153  virtual void initDiagnostic() {}
154  virtual void initPreprocessor() {}
155  virtual void initASTContext() {}
156
157  virtual clang::ASTConsumer *
158    createBackend(const clang::CodeGenOptions& CodeGenOpts,
159                  llvm::raw_ostream *OS,
160                  OutputType OT);
161
162 public:
163  static const llvm::StringRef PragmaMetadataName;
164
165  static void GlobalInitialization();
166
167  Slang();
168
169  void init(const std::string &Triple, const std::string &CPU,
170            const std::vector<std::string> &Features,
171            clang::DiagnosticsEngine *DiagEngine,
172            DiagnosticBuffer *DiagClient);
173
174  virtual clang::ModuleKey loadModule(clang::SourceLocation ImportLoc,
175                                      clang::IdentifierInfo &ModuleName,
176                                      clang::SourceLocation ModuleNameLoc);
177
178  bool setInputSource(llvm::StringRef InputFile, const char *Text,
179                      size_t TextLength);
180
181  bool setInputSource(llvm::StringRef InputFile);
182
183  std::string const &getInputFileName() const { return mInputFileName; }
184
185  void setIncludePaths(const std::vector<std::string> &IncludePaths) {
186    mIncludePaths = IncludePaths;
187  }
188
189  void setOutputType(OutputType OT) { mOT = OT; }
190
191  bool setOutput(const char *OutputFile);
192
193  std::string const &getOutputFileName() const {
194    return mOutputFileName;
195  }
196
197  bool setDepOutput(const char *OutputFile);
198
199  void setDepTargetBC(const char *TargetBCFile) {
200    mDepTargetBCFileName = TargetBCFile;
201  }
202
203  void setAdditionalDepTargets(
204      std::vector<std::string> const &AdditionalDepTargets) {
205    mAdditionalDepTargets = AdditionalDepTargets;
206  }
207
208  void appendGeneratedFileName(std::string const &GeneratedFileName) {
209    mGeneratedFileNames.push_back(GeneratedFileName);
210  }
211
212  int generateDepFile();
213
214  int compile();
215
216  char const *getErrorMessage() { return mDiagClient->str().c_str(); }
217
218  // Reset the slang compiler state such that it can be reused to compile
219  // another file
220  virtual void reset();
221
222  virtual ~Slang();
223};
224
225}  // namespace slang
226
227#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_  NOLINT
228