blob: ea335673853ad79434f23bc1f770d07c9168f5c3 [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:
Zonr Chang641558f2010-10-12 21:07:06 +080074 bool mInitialized;
75
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 // The diagnostics engine instance (for status reporting during compilation)
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080077 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();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070082
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070083 // The target being compiled for
84 clang::TargetOptions mTargetOpts;
85 llvm::OwningPtr<clang::TargetInfo> mTarget;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070086 void createTarget(const std::string &Triple, const std::string &CPU,
87 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070088
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080089 // Below is for parsing and code generation
Shih-wei Liao462aefd2010-06-04 15:32:04 -070090
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070091 // The file manager (for prepocessor doing the job such as header file search)
92 llvm::OwningPtr<clang::FileManager> mFileMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080093 void createFileManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070094
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070095 // The source manager (responsible for the source code handling)
96 llvm::OwningPtr<clang::SourceManager> mSourceMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080097 void createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070098
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099 // The preprocessor (source code preprocessor)
100 llvm::OwningPtr<clang::Preprocessor> mPP;
101 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700102
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 // The AST context (the context to hold long-lived AST nodes)
104 llvm::OwningPtr<clang::ASTContext> mASTContext;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800105 void createASTContext();
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700106
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700107 // The AST consumer, responsible for code generation
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800108 llvm::OwningPtr<clang::ASTConsumer> mBackend;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700109
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700110 // Input file name
111 std::string mInputFileName;
112 std::string mOutputFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700113
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700114 std::string mDepOutputFileName;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700115 std::string mDepTargetBCFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700116 std::vector<std::string> mAdditionalDepTargets;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700117
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800118 OutputType mOT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700119
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700120 // Output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800121 llvm::OwningPtr<llvm::tool_output_file> mOS;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700122 // Dependency output stream
Zonr Change8c263a2010-10-12 00:35:29 +0800123 llvm::OwningPtr<llvm::tool_output_file> mDOS;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700124
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700125 std::vector<std::string> mIncludePaths;
Ying Wange2e522f2010-09-01 13:24:01 -0700126
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800127 protected:
128 PragmaList mPragmas;
129
130 inline clang::Diagnostic &getDiagnostics() { return *mDiagnostics; }
131 inline const clang::TargetInfo &getTargetInfo() const { return *mTarget; }
132 inline clang::FileManager &getFileManager() { return *mFileMgr; }
133 inline clang::SourceManager &getSourceManager() { return *mSourceMgr; }
134 inline clang::Preprocessor &getPreprocessor() { return *mPP; }
135 inline clang::ASTContext &getASTContext() { return *mASTContext; }
136
137 inline const clang::TargetOptions &getTargetOptions() const
138 { return mTargetOpts; }
139
140 virtual void initDiagnostic() {}
141 virtual void initPreprocessor() {}
142 virtual void initASTContext() {}
143
144 virtual clang::ASTConsumer
145 *createBackend(const clang::CodeGenOptions& CodeGenOpts,
146 llvm::raw_ostream *OS,
147 OutputType OT);
148
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700149 public:
150 static const std::string TargetDescription;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700151
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700152 static const llvm::StringRef PragmaMetadataName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700153
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700154 static void GlobalInitialization();
155
Zonr Chang641558f2010-10-12 21:07:06 +0800156 Slang();
157
158 void init(const std::string &Triple, const std::string &CPU,
159 const std::vector<std::string> &Features);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700160
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800161 bool setInputSource(llvm::StringRef InputFile, const char *Text,
162 size_t TextLength);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700163
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800164 bool setInputSource(llvm::StringRef InputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700165
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800166 inline const std::string &getInputFileName() const { return mInputFileName; }
Ying Wange2e522f2010-09-01 13:24:01 -0700167
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700168 inline void setIncludePaths(const std::vector<std::string> &IncludePaths) {
169 mIncludePaths = IncludePaths;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700170 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700171
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800172 inline void setOutputType(OutputType OT) { mOT = OT; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700173
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700174 bool setOutput(const char *OutputFile);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800175 inline const std::string &getOutputFileName() const {
176 return mOutputFileName;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700177 }
Kirk Stewart1fd85792010-07-07 09:51:23 -0700178
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700179 bool setDepOutput(const char *OutputFile);
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700180 inline void setDepTargetBC(const char *TargetBCFile) {
181 mDepTargetBCFileName = TargetBCFile;
182 }
183 inline void setAdditionalDepTargets(
184 const std::vector<std::string> &AdditionalDepTargets) {
185 mAdditionalDepTargets = AdditionalDepTargets;
186 }
Stephen Hinescc0efad2010-10-04 16:13:02 -0700187
188 int generateDepFile();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700189 int compile();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700190
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800191 inline const char *getErrorMessage() { return mDiagClient->str().c_str(); }
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700192
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700193 // Reset the slang compiler state such that it can be reused to compile
194 // another file
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800195 virtual void reset();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700196
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800197 virtual ~Slang();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700198};
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700199}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700200
zonr6315f762010-10-05 15:35:14 +0800201#endif // _SLANG_COMPILER_SLANG_H