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