slang.h revision 4cc67fce91f43215d61b2695746eab102a3db516
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  std::vector<std::string> mGeneratedFileNames;
118
119  OutputType mOT;
120
121  // Output stream
122  llvm::OwningPtr<llvm::tool_output_file> mOS;
123  // Dependency output stream
124  llvm::OwningPtr<llvm::tool_output_file> mDOS;
125
126  std::vector<std::string> mIncludePaths;
127
128 protected:
129  PragmaList mPragmas;
130
131  inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; }
132  inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; }
133  inline clang::FileManager &getFileManager() { return *mFileMgr; }
134  inline clang::SourceManager &getSourceManager() { return *mSourceMgr; }
135  inline clang::Preprocessor &getPreprocessor() { return *mPP; }
136  inline clang::ASTContext &getASTContext() { return *mASTContext; }
137
138  inline const clang::TargetOptions &getTargetOptions() const
139    { return mTargetOpts; }
140
141  virtual void initDiagnostic() {}
142  virtual void initPreprocessor() {}
143  virtual void initASTContext() {}
144
145  virtual clang::ASTConsumer
146  *createBackend(const clang::CodeGenOptions& CodeGenOpts,
147                 llvm::raw_ostream *OS,
148                 OutputType OT);
149
150 public:
151  static const llvm::StringRef PragmaMetadataName;
152
153  static void GlobalInitialization();
154
155  Slang();
156
157  void init(const std::string &Triple, const std::string &CPU,
158            const std::vector<std::string> &Features);
159
160  bool setInputSource(llvm::StringRef InputFile, const char *Text,
161                      size_t TextLength);
162
163  bool setInputSource(llvm::StringRef InputFile);
164
165  inline const std::string &getInputFileName() const { return mInputFileName; }
166
167  inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
168    mIncludePaths = IncludePaths;
169  }
170
171  inline void setOutputType(OutputType OT) { mOT = OT; }
172
173  bool setOutput(const char *OutputFile);
174  inline const std::string &getOutputFileName() const {
175    return mOutputFileName;
176  }
177
178  bool setDepOutput(const char *OutputFile);
179  inline void setDepTargetBC(const char *TargetBCFile) {
180    mDepTargetBCFileName = TargetBCFile;
181  }
182  inline void setAdditionalDepTargets(
183      const std::vector<std::string> &AdditionalDepTargets) {
184    mAdditionalDepTargets = AdditionalDepTargets;
185  }
186  inline void appendGeneratedFileName(
187      const std::string &GeneratedFileName) {
188    mGeneratedFileNames.push_back(GeneratedFileName);
189  }
190
191  int generateDepFile();
192  int compile();
193
194  inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
195
196  // Reset the slang compiler state such that it can be reused to compile
197  // another file
198  virtual void reset();
199
200  virtual ~Slang();
201};
202
203}  // namespace slang
204
205#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_  NOLINT
206