blob: ced1266d28321bd2101a0e6a3c612113cc2be598 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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
Stephen Hinese639eb52010-11-08 19:27:20 -080017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_H_
Shih-wei Liao462aefd2010-06-04 15:32:04 -070019
zonr6315f762010-10-05 15:35:14 +080020#include <cstdio>
21#include <string>
Ying Wange2e522f2010-09-01 13:24:01 -070022#include <vector>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070023
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070024#include "clang/Basic/TargetOptions.h"
25
Stephen Hinese639eb52010-11-08 19:27:20 -080026#include "llvm/ADT/IntrusiveRefCntPtr.h"
27#include "llvm/ADT/OwningPtr.h"
28#include "llvm/ADT/StringRef.h"
29
zonr6315f762010-10-05 15:35:14 +080030#include "slang_diagnostic_buffer.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080031#include "slang_pragma_recorder.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070032
33namespace llvm {
Zonr Change8c263a2010-10-12 00:35:29 +080034 class tool_output_file;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070036
37namespace clang {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080038 class Diagnostic;
39 class FileManager;
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -080040 class FileSystemOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080041 class SourceManager;
zonr6315f762010-10-05 15:35:14 +080042 class LangOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080043 class Preprocessor;
zonr6315f762010-10-05 15:35:14 +080044 class TargetOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080045 class CodeGenOptions;
46 class ASTContext;
47 class ASTConsumer;
48 class Backend;
49 class TargetInfo;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070051
52namespace slang {
53
Shih-wei Liao462aefd2010-06-04 15:32:04 -070054class Slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070055 static clang::LangOptions LangOpts;
56 static clang::CodeGenOptions CodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070057
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 static bool GlobalInitialized;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070059
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 static void LLVMErrorHandler(void *UserData, const std::string &Message);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070061
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080062 public:
63 typedef enum {
Stephen Hinescc0efad2010-10-04 16:13:02 -070064 OT_Dependency,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080065 OT_Assembly,
66 OT_LLVMAssembly,
67 OT_Bitcode,
68 OT_Nothing,
69 OT_Object,
70
71 OT_Default = OT_Bitcode
72 } OutputType;
73
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 private:
Zonr Chang641558f2010-10-12 21:07:06 +080075 bool mInitialized;
76
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070077 // The diagnostics engine instance (for status reporting during compilation)
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080078 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();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070083
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070084 // The target being compiled for
85 clang::TargetOptions mTargetOpts;
86 llvm::OwningPtr<clang::TargetInfo> mTarget;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070087 void createTarget(const std::string &Triple, const std::string &CPU,
88 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070089
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080090 // Below is for parsing and code generation
Shih-wei Liao462aefd2010-06-04 15:32:04 -070091
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070092 // The file manager (for prepocessor doing the job such as header file search)
93 llvm::OwningPtr<clang::FileManager> mFileMgr;
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -080094 llvm::OwningPtr<clang::FileSystemOptions> mFileSysOpt;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080095 void createFileManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070096
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 // The source manager (responsible for the source code handling)
98 llvm::OwningPtr<clang::SourceManager> mSourceMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080099 void createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700100
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 // The preprocessor (source code preprocessor)
102 llvm::OwningPtr<clang::Preprocessor> mPP;
103 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700104
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 // The AST context (the context to hold long-lived AST nodes)
106 llvm::OwningPtr<clang::ASTContext> mASTContext;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800107 void createASTContext();
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700108
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700109 // The AST consumer, responsible for code generation
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800110 llvm::OwningPtr<clang::ASTConsumer> mBackend;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700111
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700112 // Input file name
113 std::string mInputFileName;
114 std::string mOutputFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700115
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700116 std::string mDepOutputFileName;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700117 std::string mDepTargetBCFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700118 std::vector<std::string> mAdditionalDepTargets;
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800119 std::vector<std::string> mGeneratedFileNames;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700120
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800121 OutputType mOT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700122
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700123 // Output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800124 llvm::OwningPtr<llvm::tool_output_file> mOS;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700125 // Dependency output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800126 llvm::OwningPtr<llvm::tool_output_file> mDOS;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700127
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700128 std::vector<std::string> mIncludePaths;
Ying Wange2e522f2010-09-01 13:24:01 -0700129
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800130 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
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700152 public:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700153 static const llvm::StringRef PragmaMetadataName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700154
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700155 static void GlobalInitialization();
156
Zonr Chang641558f2010-10-12 21:07:06 +0800157 Slang();
158
159 void init(const std::string &Triple, const std::string &CPU,
160 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700161
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800162 bool setInputSource(llvm::StringRef InputFile, const char *Text,
163 size_t TextLength);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700164
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800165 bool setInputSource(llvm::StringRef InputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700166
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800167 inline const std::string &getInputFileName() const { return mInputFileName; }
Ying Wange2e522f2010-09-01 13:24:01 -0700168
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700169 inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
170 mIncludePaths = IncludePaths;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700171 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700172
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800173 inline void setOutputType(OutputType OT) { mOT = OT; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700174
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700175 bool setOutput(const char *OutputFile);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800176 inline const std::string &getOutputFileName() const {
177 return mOutputFileName;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700178 }
Kirk Stewart1fd85792010-07-07 09:51:23 -0700179
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700180 bool setDepOutput(const char *OutputFile);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700181 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 }
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800188 inline void appendGeneratedFileName(
189 const std::string &GeneratedFileName) {
190 mGeneratedFileNames.push_back(GeneratedFileName);
191 }
Stephen Hinescc0efad2010-10-04 16:13:02 -0700192
193 int generateDepFile();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194 int compile();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700195
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800196 inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700197
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700198 // Reset the slang compiler state such that it can be reused to compile
199 // another file
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800200 virtual void reset();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700201
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800202 virtual ~Slang();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700203};
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700204
Stephen Hinese639eb52010-11-08 19:27:20 -0800205} // namespace slang
206
207#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ NOLINT