slang.h revision c460b37ffb50819a32c2a8967754b6f784b28263
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 "llvm/Target/TargetMachine.h"
32
33#include "slang_diagnostic_buffer.h"
34#include "slang_pragma_recorder.h"
35
36namespace llvm {
37  class tool_output_file;
38}
39
40namespace clang {
41  class ASTConsumer;
42  class ASTContext;
43  class Backend;
44  class CodeGenOptions;
45  class Diagnostic;
46  class DiagnosticsEngine;
47  class FileManager;
48  class FileSystemOptions;
49  class LangOptions;
50  class Preprocessor;
51  class SourceManager;
52  class TargetInfo;
53  class TargetOptions;
54}
55
56namespace slang {
57
58class Slang : public clang::ModuleLoader {
59  static clang::LangOptions LangOpts;
60  static clang::CodeGenOptions CodeGenOpts;
61
62  static bool GlobalInitialized;
63
64  static void LLVMErrorHandler(void *UserData, const std::string &Message);
65
66 public:
67  enum OutputType {
68    OT_Dependency,
69    OT_Assembly,
70    OT_LLVMAssembly,
71    OT_Bitcode,
72    OT_Nothing,
73    OT_Object,
74
75    OT_Default = OT_Bitcode
76  };
77
78 private:
79  bool mInitialized;
80
81  // Diagnostics Mediator (An interface for both Producer and Consumer)
82  llvm::OwningPtr<clang::Diagnostic> mDiag;
83
84  // Diagnostics ID
85  llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> mDiagIDs;
86
87  // Diagnostics Engine (Producer and Diagnostics Reporter)
88  llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> mDiagEngine;
89
90  // Diagnostics Consumer
91  // NOTE: The ownership is taken by mDiagEngine after creation.
92  DiagnosticBuffer *mDiagClient;
93
94  void createDiagnostic();
95
96
97  // The target being compiled for
98  clang::TargetOptions mTargetOpts;
99  llvm::OwningPtr<clang::TargetInfo> mTarget;
100  void createTarget(std::string const &Triple, std::string const &CPU,
101                    std::vector<std::string> const &Features);
102
103
104  // File manager (for prepocessor doing the job such as header file search)
105  llvm::OwningPtr<clang::FileManager> mFileMgr;
106  llvm::OwningPtr<clang::FileSystemOptions> mFileSysOpt;
107  void createFileManager();
108
109
110  // Source manager (responsible for the source code handling)
111  llvm::OwningPtr<clang::SourceManager> mSourceMgr;
112  void createSourceManager();
113
114
115  // Preprocessor (source code preprocessor)
116  llvm::OwningPtr<clang::Preprocessor> mPP;
117  void createPreprocessor();
118
119
120  // AST context (the context to hold long-lived AST nodes)
121  llvm::OwningPtr<clang::ASTContext> mASTContext;
122  void createASTContext();
123
124
125  // AST consumer, responsible for code generation
126  llvm::OwningPtr<clang::ASTConsumer> mBackend;
127
128
129  // File names
130  std::string mInputFileName;
131  std::string mOutputFileName;
132
133  std::string mDepOutputFileName;
134  std::string mDepTargetBCFileName;
135  std::vector<std::string> mAdditionalDepTargets;
136  std::vector<std::string> mGeneratedFileNames;
137
138  OutputType mOT;
139
140  // Output stream
141  llvm::OwningPtr<llvm::tool_output_file> mOS;
142
143  // Dependency output stream
144  llvm::OwningPtr<llvm::tool_output_file> mDOS;
145
146  std::vector<std::string> mIncludePaths;
147
148 protected:
149  PragmaList mPragmas;
150
151  clang::DiagnosticsEngine &getDiagnostics() { return *mDiagEngine; }
152  clang::TargetInfo const &getTargetInfo() const { return *mTarget; }
153  clang::FileManager &getFileManager() { return *mFileMgr; }
154  clang::SourceManager &getSourceManager() { return *mSourceMgr; }
155  clang::Preprocessor &getPreprocessor() { return *mPP; }
156  clang::ASTContext &getASTContext() { return *mASTContext; }
157
158  inline clang::TargetOptions const &getTargetOptions() const
159    { return mTargetOpts; }
160
161  virtual void initDiagnostic() {}
162  virtual void initPreprocessor() {}
163  virtual void initASTContext() {}
164
165  virtual clang::ASTConsumer *
166    createBackend(const clang::CodeGenOptions& CodeGenOpts,
167                  llvm::raw_ostream *OS,
168                  OutputType OT);
169
170 public:
171  static const llvm::StringRef PragmaMetadataName;
172
173  static void GlobalInitialization();
174
175  Slang();
176
177  void init(const std::string &Triple, const std::string &CPU,
178            const std::vector<std::string> &Features);
179
180  virtual clang::ModuleKey loadModule(clang::SourceLocation ImportLoc,
181                                      clang::IdentifierInfo &ModuleName,
182                                      clang::SourceLocation ModuleNameLoc);
183
184  bool setInputSource(llvm::StringRef InputFile, const char *Text,
185                      size_t TextLength);
186
187  bool setInputSource(llvm::StringRef InputFile);
188
189  std::string const &getInputFileName() const { return mInputFileName; }
190
191  void setIncludePaths(const std::vector<std::string> &IncludePaths) {
192    mIncludePaths = IncludePaths;
193  }
194
195  void setOutputType(OutputType OT) { mOT = OT; }
196
197  bool setOutput(const char *OutputFile);
198
199  std::string const &getOutputFileName() const {
200    return mOutputFileName;
201  }
202
203  bool setDepOutput(const char *OutputFile);
204
205  void setDepTargetBC(const char *TargetBCFile) {
206    mDepTargetBCFileName = TargetBCFile;
207  }
208
209  void setAdditionalDepTargets(
210      std::vector<std::string> const &AdditionalDepTargets) {
211    mAdditionalDepTargets = AdditionalDepTargets;
212  }
213
214  void appendGeneratedFileName(std::string const &GeneratedFileName) {
215    mGeneratedFileNames.push_back(GeneratedFileName);
216  }
217
218  int generateDepFile();
219
220  int compile();
221
222  char const *getErrorMessage() { return mDiagClient->str().c_str(); }
223
224  void setDebugMetadataEmission(bool EmitDebug);
225
226  void setOptimizationLevel(llvm::CodeGenOpt::Level OptimizationLevel);
227
228  // Reset the slang compiler state such that it can be reused to compile
229  // another file
230  virtual void reset();
231
232  virtual ~Slang();
233};
234
235}  // namespace slang
236
237#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_  NOLINT
238