blob: 9f7c46ab086a86860800efb1756c8ffb5c98708b [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;
Loganbe274822011-02-16 22:02:54 +080079 // The diagnostics id
80 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> mDiagIDs;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080081 // The clients of diagnostics engine. The ownership is taken by the
82 // mDiagnostics after creation.
83 DiagnosticBuffer *mDiagClient;
84 void createDiagnostic();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070085
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070086 // The target being compiled for
87 clang::TargetOptions mTargetOpts;
88 llvm::OwningPtr<clang::TargetInfo> mTarget;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070089 void createTarget(const std::string &Triple, const std::string &CPU,
90 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070091
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080092 // Below is for parsing and code generation
Shih-wei Liao462aefd2010-06-04 15:32:04 -070093
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070094 // The file manager (for prepocessor doing the job such as header file search)
95 llvm::OwningPtr<clang::FileManager> mFileMgr;
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -080096 llvm::OwningPtr<clang::FileSystemOptions> mFileSysOpt;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080097 void createFileManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070098
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099 // The source manager (responsible for the source code handling)
100 llvm::OwningPtr<clang::SourceManager> mSourceMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800101 void createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700102
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 // The preprocessor (source code preprocessor)
104 llvm::OwningPtr<clang::Preprocessor> mPP;
105 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700106
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700107 // The AST context (the context to hold long-lived AST nodes)
108 llvm::OwningPtr<clang::ASTContext> mASTContext;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800109 void createASTContext();
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700110
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111 // The AST consumer, responsible for code generation
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800112 llvm::OwningPtr<clang::ASTConsumer> mBackend;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700113
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700114 // Input file name
115 std::string mInputFileName;
116 std::string mOutputFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700117
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700118 std::string mDepOutputFileName;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700119 std::string mDepTargetBCFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700120 std::vector<std::string> mAdditionalDepTargets;
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800121 std::vector<std::string> mGeneratedFileNames;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700122
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800123 OutputType mOT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700124
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700125 // Output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800126 llvm::OwningPtr<llvm::tool_output_file> mOS;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700127 // Dependency output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800128 llvm::OwningPtr<llvm::tool_output_file> mDOS;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700129
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700130 std::vector<std::string> mIncludePaths;
Ying Wange2e522f2010-09-01 13:24:01 -0700131
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800132 protected:
133 PragmaList mPragmas;
134
135 inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; }
136 inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; }
137 inline clang::FileManager &getFileManager() { return *mFileMgr; }
138 inline clang::SourceManager &getSourceManager() { return *mSourceMgr; }
139 inline clang::Preprocessor &getPreprocessor() { return *mPP; }
140 inline clang::ASTContext &getASTContext() { return *mASTContext; }
141
142 inline const clang::TargetOptions &getTargetOptions() const
143 { return mTargetOpts; }
144
145 virtual void initDiagnostic() {}
146 virtual void initPreprocessor() {}
147 virtual void initASTContext() {}
148
149 virtual clang::ASTConsumer
150 *createBackend(const clang::CodeGenOptions& CodeGenOpts,
151 llvm::raw_ostream *OS,
152 OutputType OT);
153
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700154 public:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700155 static const llvm::StringRef PragmaMetadataName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700157 static void GlobalInitialization();
158
Zonr Chang641558f2010-10-12 21:07:06 +0800159 Slang();
160
161 void init(const std::string &Triple, const std::string &CPU,
162 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700163
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800164 bool setInputSource(llvm::StringRef InputFile, const char *Text,
165 size_t TextLength);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700166
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800167 bool setInputSource(llvm::StringRef InputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700168
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800169 inline const std::string &getInputFileName() const { return mInputFileName; }
Ying Wange2e522f2010-09-01 13:24:01 -0700170
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700171 inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
172 mIncludePaths = IncludePaths;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700174
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800175 inline void setOutputType(OutputType OT) { mOT = OT; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700176
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700177 bool setOutput(const char *OutputFile);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800178 inline const std::string &getOutputFileName() const {
179 return mOutputFileName;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700180 }
Kirk Stewart1fd85792010-07-07 09:51:23 -0700181
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700182 bool setDepOutput(const char *OutputFile);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700183 inline void setDepTargetBC(const char *TargetBCFile) {
184 mDepTargetBCFileName = TargetBCFile;
185 }
186 inline void setAdditionalDepTargets(
187 const std::vector<std::string> &AdditionalDepTargets) {
188 mAdditionalDepTargets = AdditionalDepTargets;
189 }
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800190 inline void appendGeneratedFileName(
191 const std::string &GeneratedFileName) {
192 mGeneratedFileNames.push_back(GeneratedFileName);
193 }
Stephen Hinescc0efad2010-10-04 16:13:02 -0700194
195 int generateDepFile();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700196 int compile();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700197
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800198 inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700199
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700200 // Reset the slang compiler state such that it can be reused to compile
201 // another file
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800202 virtual void reset();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700203
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800204 virtual ~Slang();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700205};
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700206
Stephen Hinese639eb52010-11-08 19:27:20 -0800207} // namespace slang
208
209#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ NOLINT