slang.h revision e639eb5caa2c386b4a60659a4929e8a6141a2cbe
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
26#include "llvm/ADT/IntrusiveRefCntPtr.h"
27#include "llvm/ADT/OwningPtr.h"
28#include "llvm/ADT/StringRef.h"
29
30#include "slang_diagnostic_buffer.h"
31#include "slang_pragma_recorder.h"
32
33namespace llvm {
34  class tool_output_file;
35}
36
37namespace clang {
38  class Diagnostic;
39  class FileManager;
40  class SourceManager;
41  class LangOptions;
42  class Preprocessor;
43  class TargetOptions;
44  class CodeGenOptions;
45  class ASTContext;
46  class ASTConsumer;
47  class Backend;
48  class TargetInfo;
49}
50
51namespace slang {
52
53class Slang {
54  static clang::LangOptions LangOpts;
55  static clang::CodeGenOptions CodeGenOpts;
56
57  static bool GlobalInitialized;
58
59  static void LLVMErrorHandler(void *UserData, const std::string &Message);
60
61 public:
62  typedef enum {
63    OT_Dependency,
64    OT_Assembly,
65    OT_LLVMAssembly,
66    OT_Bitcode,
67    OT_Nothing,
68    OT_Object,
69
70    OT_Default = OT_Bitcode
71  } OutputType;
72
73 private:
74  bool mInitialized;
75
76  // The diagnostics engine instance (for status reporting during compilation)
77  llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics;
78  // The clients of diagnostics engine. The ownership is taken by the
79  // mDiagnostics after creation.
80  DiagnosticBuffer *mDiagClient;
81  void createDiagnostic();
82
83  // The target being compiled for
84  clang::TargetOptions mTargetOpts;
85  llvm::OwningPtr<clang::TargetInfo> mTarget;
86  void createTarget(const std::string &Triple, const std::string &CPU,
87                    const std::vector<std::string> &Features);
88
89  // Below is for parsing and code generation
90
91  // The file manager (for prepocessor doing the job such as header file search)
92  llvm::OwningPtr<clang::FileManager> mFileMgr;
93  void createFileManager();
94
95  // The source manager (responsible for the source code handling)
96  llvm::OwningPtr<clang::SourceManager> mSourceMgr;
97  void createSourceManager();
98
99  // The preprocessor (source code preprocessor)
100  llvm::OwningPtr<clang::Preprocessor> mPP;
101  void createPreprocessor();
102
103  // The AST context (the context to hold long-lived AST nodes)
104  llvm::OwningPtr<clang::ASTContext> mASTContext;
105  void createASTContext();
106
107  // The AST consumer, responsible for code generation
108  llvm::OwningPtr<clang::ASTConsumer> mBackend;
109
110  // Input file name
111  std::string mInputFileName;
112  std::string mOutputFileName;
113
114  std::string mDepOutputFileName;
115  std::string mDepTargetBCFileName;
116  std::vector<std::string> mAdditionalDepTargets;
117
118  OutputType mOT;
119
120  // Output stream
121  llvm::OwningPtr<llvm::tool_output_file> mOS;
122  // Dependency output stream
123  llvm::OwningPtr<llvm::tool_output_file> mDOS;
124
125  std::vector<std::string> mIncludePaths;
126
127 protected:
128  PragmaList mPragmas;
129
130  inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; }
131  inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; }
132  inline clang::FileManager &getFileManager() { return *mFileMgr; }
133  inline clang::SourceManager &getSourceManager() { return *mSourceMgr; }
134  inline clang::Preprocessor &getPreprocessor() { return *mPP; }
135  inline clang::ASTContext &getASTContext() { return *mASTContext; }
136
137  inline const clang::TargetOptions &getTargetOptions() const
138    { return mTargetOpts; }
139
140  virtual void initDiagnostic() {}
141  virtual void initPreprocessor() {}
142  virtual void initASTContext() {}
143
144  virtual clang::ASTConsumer
145  *createBackend(const clang::CodeGenOptions& CodeGenOpts,
146                 llvm::raw_ostream *OS,
147                 OutputType OT);
148
149 public:
150  static const llvm::StringRef PragmaMetadataName;
151
152  static void GlobalInitialization();
153
154  Slang();
155
156  void init(const std::string &Triple, const std::string &CPU,
157            const std::vector<std::string> &Features);
158
159  bool setInputSource(llvm::StringRef InputFile, const char *Text,
160                      size_t TextLength);
161
162  bool setInputSource(llvm::StringRef InputFile);
163
164  inline const std::string &getInputFileName() const { return mInputFileName; }
165
166  inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
167    mIncludePaths = IncludePaths;
168  }
169
170  inline void setOutputType(OutputType OT) { mOT = OT; }
171
172  bool setOutput(const char *OutputFile);
173  inline const std::string &getOutputFileName() const {
174    return mOutputFileName;
175  }
176
177  bool setDepOutput(const char *OutputFile);
178  inline void setDepTargetBC(const char *TargetBCFile) {
179    mDepTargetBCFileName = TargetBCFile;
180  }
181  inline void setAdditionalDepTargets(
182      const std::vector<std::string> &AdditionalDepTargets) {
183    mAdditionalDepTargets = AdditionalDepTargets;
184  }
185
186  int generateDepFile();
187  int compile();
188
189  inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
190
191  // Reset the slang compiler state such that it can be reused to compile
192  // another file
193  virtual void reset();
194
195  virtual ~Slang();
196};
197
198}  // namespace slang
199
200#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_  NOLINT
201