blob: 96a1390d3a84f56a74da0e7f1119d09ce7c07482 [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
zonr6315f762010-10-05 15:35:14 +080017#ifndef _SLANG_COMPILER_SLANG_H
18#define _SLANG_COMPILER_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 "llvm/ADT/OwningPtr.h"
25#include "llvm/ADT/StringRef.h"
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080026#include "llvm/ADT/IntrusiveRefCntPtr.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070027
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070028#include "clang/Basic/TargetOptions.h"
29
zonr6315f762010-10-05 15:35:14 +080030#include "slang_pragma_recorder.h"
31#include "slang_diagnostic_buffer.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;
40 class SourceManager;
zonr6315f762010-10-05 15:35:14 +080041 class LangOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080042 class Preprocessor;
zonr6315f762010-10-05 15:35:14 +080043 class TargetOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080044 class CodeGenOptions;
45 class ASTContext;
46 class ASTConsumer;
47 class Backend;
48 class TargetInfo;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070050
51namespace slang {
52
Shih-wei Liao462aefd2010-06-04 15:32:04 -070053class Slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 static clang::LangOptions LangOpts;
55 static clang::CodeGenOptions CodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070056
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070057 static bool GlobalInitialized;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070058
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 static void LLVMErrorHandler(void *UserData, const std::string &Message);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070060
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080061 public:
62 typedef enum {
Stephen Hinescc0efad2010-10-04 16:13:02 -070063 OT_Dependency,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080064 OT_Assembly,
65 OT_LLVMAssembly,
66 OT_Bitcode,
67 OT_Nothing,
68 OT_Object,
69
70 OT_Default = OT_Bitcode
71 } OutputType;
72
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 // The diagnostics engine instance (for status reporting during compilation)
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080075 llvm::IntrusiveRefCntPtr<clang::Diagnostic> mDiagnostics;
76 // The clients of diagnostics engine. The ownership is taken by the
77 // mDiagnostics after creation.
78 DiagnosticBuffer *mDiagClient;
79 void createDiagnostic();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070080
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 // The target being compiled for
82 clang::TargetOptions mTargetOpts;
83 llvm::OwningPtr<clang::TargetInfo> mTarget;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070084 void createTarget(const std::string &Triple, const std::string &CPU,
85 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080087 // Below is for parsing and code generation
Shih-wei Liao462aefd2010-06-04 15:32:04 -070088
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070089 // The file manager (for prepocessor doing the job such as header file search)
90 llvm::OwningPtr<clang::FileManager> mFileMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080091 void createFileManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070092
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070093 // The source manager (responsible for the source code handling)
94 llvm::OwningPtr<clang::SourceManager> mSourceMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080095 void createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070096
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 // The preprocessor (source code preprocessor)
98 llvm::OwningPtr<clang::Preprocessor> mPP;
99 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700100
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 // The AST context (the context to hold long-lived AST nodes)
102 llvm::OwningPtr<clang::ASTContext> mASTContext;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800103 void createASTContext();
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700104
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 // The AST consumer, responsible for code generation
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800106 llvm::OwningPtr<clang::ASTConsumer> mBackend;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700107
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108 // Input file name
109 std::string mInputFileName;
110 std::string mOutputFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700111
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700112 std::string mDepOutputFileName;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700113 std::string mDepTargetBCFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700114 std::vector<std::string> mAdditionalDepTargets;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700115
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800116 OutputType mOT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700117
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700118 // Output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800119 llvm::OwningPtr<llvm::tool_output_file> mOS;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700120 // Dependency output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800121 llvm::OwningPtr<llvm::tool_output_file> mDOS;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700122
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700123 std::vector<std::string> mIncludePaths;
Ying Wange2e522f2010-09-01 13:24:01 -0700124
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800125 protected:
126 PragmaList mPragmas;
127
128 inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; }
129 inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; }
130 inline clang::FileManager &getFileManager() { return *mFileMgr; }
131 inline clang::SourceManager &getSourceManager() { return *mSourceMgr; }
132 inline clang::Preprocessor &getPreprocessor() { return *mPP; }
133 inline clang::ASTContext &getASTContext() { return *mASTContext; }
134
135 inline const clang::TargetOptions &getTargetOptions() const
136 { return mTargetOpts; }
137
138 virtual void initDiagnostic() {}
139 virtual void initPreprocessor() {}
140 virtual void initASTContext() {}
141
142 virtual clang::ASTConsumer
143 *createBackend(const clang::CodeGenOptions& CodeGenOpts,
144 llvm::raw_ostream *OS,
145 OutputType OT);
146
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700147 public:
148 static const std::string TargetDescription;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700149
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700150 static const llvm::StringRef PragmaMetadataName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700151
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700152 static void GlobalInitialization();
153
154 Slang(const std::string &Triple, const std::string &CPU,
155 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800157 bool setInputSource(llvm::StringRef InputFile, const char *Text,
158 size_t TextLength);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700159
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800160 bool setInputSource(llvm::StringRef InputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700161
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800162 inline const std::string &getInputFileName() const { return mInputFileName; }
Ying Wange2e522f2010-09-01 13:24:01 -0700163
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700164 inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
165 mIncludePaths = IncludePaths;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700166 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700167
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800168 inline void setOutputType(OutputType OT) { mOT = OT; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700169
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700170 bool setOutput(const char *OutputFile);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800171 inline const std::string &getOutputFileName() const {
172 return mOutputFileName;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173 }
Kirk Stewart1fd85792010-07-07 09:51:23 -0700174
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700175 bool setDepOutput(const char *OutputFile);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700176 inline void setDepTargetBC(const char *TargetBCFile) {
177 mDepTargetBCFileName = TargetBCFile;
178 }
179 inline void setAdditionalDepTargets(
180 const std::vector<std::string> &AdditionalDepTargets) {
181 mAdditionalDepTargets = AdditionalDepTargets;
182 }
Stephen Hinescc0efad2010-10-04 16:13:02 -0700183
184 int generateDepFile();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700185 int compile();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700186
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800187 inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700188
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700189 // Reset the slang compiler state such that it can be reused to compile
190 // another file
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800191 virtual void reset();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700192
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800193 virtual ~Slang();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194};
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700195}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700196
zonr6315f762010-10-05 15:35:14 +0800197#endif // _SLANG_COMPILER_SLANG_H