blob: 0848451f9112a2e195cc7e1ae1cf4409ddfcb94a [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>
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -070021#include <list>
zonr6315f762010-10-05 15:35:14 +080022#include <string>
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -070023#include <utility>
Ying Wange2e522f2010-09-01 13:24:01 -070024#include <vector>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070025
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -070026#include "llvm/ADT/StringMap.h"
27
28#include "slang_rs_reflect_utils.h"
29#include "slang_version.h"
30
Stephen Hines23c43582013-01-09 20:02:04 -080031// Terrible workaround for TargetOptions.h not using llvm::RefCountedBase!
32#include "llvm/ADT/IntrusiveRefCntPtr.h"
33using llvm::RefCountedBase;
34
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -070035#include "clang/Basic/LangOptions.h"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070036#include "clang/Basic/TargetOptions.h"
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -070037#include "clang/Frontend/CodeGenOptions.h"
Logan Chien9207a2e2011-10-21 15:39:28 +080038#include "clang/Lex/ModuleLoader.h"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070039
Stephen Hinese639eb52010-11-08 19:27:20 -080040#include "llvm/ADT/StringRef.h"
41
mkopec1c460b372012-01-09 11:21:50 -050042#include "llvm/Target/TargetMachine.h"
43
zonr6315f762010-10-05 15:35:14 +080044#include "slang_diagnostic_buffer.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080045#include "slang_pragma_recorder.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
47namespace llvm {
Zonr Change8c263a2010-10-12 00:35:29 +080048 class tool_output_file;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070049}
Shih-wei Liao462aefd2010-06-04 15:32:04 -070050
51namespace clang {
Logan Chien9207a2e2011-10-21 15:39:28 +080052 class ASTConsumer;
53 class ASTContext;
54 class Backend;
55 class CodeGenOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080056 class Diagnostic;
Logan Chien9207a2e2011-10-21 15:39:28 +080057 class DiagnosticsEngine;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080058 class FileManager;
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -080059 class FileSystemOptions;
zonr6315f762010-10-05 15:35:14 +080060 class LangOptions;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080061 class Preprocessor;
Logan Chien9207a2e2011-10-21 15:39:28 +080062 class SourceManager;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080063 class TargetInfo;
Stephen Hinese67239d2012-02-24 15:08:36 -080064} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070065
66namespace slang {
67
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -070068class RSCCOptions;
69class RSContext;
70class RSExportRecordType;
71
Logan Chien9207a2e2011-10-21 15:39:28 +080072class Slang : public clang::ModuleLoader {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080073 public:
Logan Chien9207a2e2011-10-21 15:39:28 +080074 enum OutputType {
Stephen Hinescc0efad2010-10-04 16:13:02 -070075 OT_Dependency,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080076 OT_Assembly,
77 OT_LLVMAssembly,
78 OT_Bitcode,
79 OT_Nothing,
80 OT_Object,
81
82 OT_Default = OT_Bitcode
Logan Chien9207a2e2011-10-21 15:39:28 +080083 };
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080084
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 private:
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -070086 // Language options (define the language feature for compiler such as C99)
87 clang::LangOptions LangOpts;
88 // Code generation options for the compiler
89 clang::CodeGenOptions CodeGenOpts;
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -070090
91 // Returns true if this is a Filterscript file.
92 static bool isFilterscript(const char *Filename);
93
Logan Chien9207a2e2011-10-21 15:39:28 +080094 // Diagnostics Engine (Producer and Diagnostics Reporter)
Stephen Hines8f4d9722011-12-05 14:14:42 -080095 clang::DiagnosticsEngine *mDiagEngine;
Logan Chien9207a2e2011-10-21 15:39:28 +080096
97 // Diagnostics Consumer
98 // NOTE: The ownership is taken by mDiagEngine after creation.
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080099 DiagnosticBuffer *mDiagClient;
Logan Chien9207a2e2011-10-21 15:39:28 +0800100
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 // The target being compiled for
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700102 std::shared_ptr<clang::TargetOptions> mTargetOpts;
103 std::unique_ptr<clang::TargetInfo> mTarget;
Stephen Hines9ae18b22014-06-10 23:53:00 -0700104 void createTarget(uint32_t BitWidth);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700105
Logan Chien9207a2e2011-10-21 15:39:28 +0800106 // File manager (for prepocessor doing the job such as header file search)
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700107 std::unique_ptr<clang::FileManager> mFileMgr;
108 std::unique_ptr<clang::FileSystemOptions> mFileSysOpt;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800109 void createFileManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700110
Logan Chien9207a2e2011-10-21 15:39:28 +0800111 // Source manager (responsible for the source code handling)
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700112 std::unique_ptr<clang::SourceManager> mSourceMgr;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800113 void createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700114
Logan Chien9207a2e2011-10-21 15:39:28 +0800115 // Preprocessor (source code preprocessor)
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700116 std::unique_ptr<clang::Preprocessor> mPP;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700117 void createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700118
Logan Chien9207a2e2011-10-21 15:39:28 +0800119 // AST context (the context to hold long-lived AST nodes)
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700120 std::unique_ptr<clang::ASTContext> mASTContext;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800121 void createASTContext();
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700122
Logan Chien9207a2e2011-10-21 15:39:28 +0800123 // AST consumer, responsible for code generation
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700124 std::unique_ptr<clang::ASTConsumer> mBackend;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700125
Logan Chien9207a2e2011-10-21 15:39:28 +0800126 // File names
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700127 std::string mInputFileName;
128 std::string mOutputFileName;
Stephen Hines9ae18b22014-06-10 23:53:00 -0700129 std::string mOutput32FileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700130
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700131 std::string mDepOutputFileName;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700132 std::string mDepTargetBCFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700133 std::vector<std::string> mAdditionalDepTargets;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700134
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800135 OutputType mOT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700136
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700137 // Output stream
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700138 std::unique_ptr<llvm::tool_output_file> mOS;
Logan Chien9207a2e2011-10-21 15:39:28 +0800139
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700140 // Dependency output stream
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700141 std::unique_ptr<llvm::tool_output_file> mDOS;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700142
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700143 std::vector<std::string> mIncludePaths;
Ying Wange2e522f2010-09-01 13:24:01 -0700144
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700145 // Context for Renderscript
146 RSContext *mRSContext;
147
148 bool mAllowRSPrefix;
149
150 unsigned int mTargetAPI;
151
152 bool mVerbose;
153
154 bool mIsFilterscript;
155
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700156 // Collect generated filenames (without the .java) for dependency generation
157 std::vector<std::string> mGeneratedFileNames;
158
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800159 PragmaList mPragmas;
160
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700161 // FIXME: Should be std::list<RSExportable *> here. But currently we only
162 // check ODR on record type.
163 //
164 // ReflectedDefinitions maps record type name to a pair:
165 // <its RSExportRecordType instance,
166 // the first file contains this record type definition>
167 typedef std::pair<RSExportRecordType*, const char*> ReflectedDefinitionTy;
168 typedef llvm::StringMap<ReflectedDefinitionTy> ReflectedDefinitionListTy;
169 ReflectedDefinitionListTy ReflectedDefinitions;
170
171 bool generateJavaBitcodeAccessor(const std::string &OutputPathBase,
172 const std::string &PackageName,
173 const std::string *LicenseNote);
174
175 // CurInputFile is the pointer to a char array holding the input filename
176 // and is valid before compile() ends.
177 bool checkODR(const char *CurInputFile);
178
Logan Chien9207a2e2011-10-21 15:39:28 +0800179 clang::DiagnosticsEngine &getDiagnostics() { return *mDiagEngine; }
180 clang::TargetInfo const &getTargetInfo() const { return *mTarget; }
181 clang::FileManager &getFileManager() { return *mFileMgr; }
182 clang::SourceManager &getSourceManager() { return *mSourceMgr; }
183 clang::Preprocessor &getPreprocessor() { return *mPP; }
184 clang::ASTContext &getASTContext() { return *mASTContext; }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800185
Logan Chien9207a2e2011-10-21 15:39:28 +0800186 inline clang::TargetOptions const &getTargetOptions() const
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700187 { return *mTargetOpts.get(); }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800188
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700189 void initPreprocessor();
190 void initASTContext();
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800191
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700192 clang::ASTConsumer *createBackend(const clang::CodeGenOptions &CodeGenOpts,
193 llvm::raw_ostream *OS,
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700194 OutputType OT);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800195
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700196 public:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700197 static const llvm::StringRef PragmaMetadataName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700198
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700199 static void GlobalInitialization();
200
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700201 static bool IsRSHeaderFile(const char *File);
202 // FIXME: Determine whether a location is in RS header (i.e., one of the RS
203 // built-in APIs) should only need its names (we need a "list" of RS
204 // built-in APIs).
205 static bool IsLocInRSHeaderFile(const clang::SourceLocation &Loc,
206 const clang::SourceManager &SourceMgr);
207
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700208 Slang(uint32_t BitWidth, clang::DiagnosticsEngine *DiagEngine,
209 DiagnosticBuffer *DiagClient);
Zonr Chang641558f2010-10-12 21:07:06 +0800210
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700211 virtual ~Slang();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700212
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800213 bool setInputSource(llvm::StringRef InputFile);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700214
Logan Chien9207a2e2011-10-21 15:39:28 +0800215 std::string const &getInputFileName() const { return mInputFileName; }
Ying Wange2e522f2010-09-01 13:24:01 -0700216
Logan Chien9207a2e2011-10-21 15:39:28 +0800217 void setIncludePaths(const std::vector<std::string> &IncludePaths) {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700218 mIncludePaths = IncludePaths;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700219 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700220
Logan Chien9207a2e2011-10-21 15:39:28 +0800221 void setOutputType(OutputType OT) { mOT = OT; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700222
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700223 bool setOutput(const char *OutputFile);
Logan Chien9207a2e2011-10-21 15:39:28 +0800224
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700225 bool setDepOutput(const char *OutputFile);
Logan Chien9207a2e2011-10-21 15:39:28 +0800226
227 void setDepTargetBC(const char *TargetBCFile) {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700228 mDepTargetBCFileName = TargetBCFile;
229 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800230
231 void setAdditionalDepTargets(
232 std::vector<std::string> const &AdditionalDepTargets) {
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700233 mAdditionalDepTargets = AdditionalDepTargets;
234 }
Logan Chien9207a2e2011-10-21 15:39:28 +0800235
236 void appendGeneratedFileName(std::string const &GeneratedFileName) {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800237 mGeneratedFileNames.push_back(GeneratedFileName);
238 }
Stephen Hinescc0efad2010-10-04 16:13:02 -0700239
240 int generateDepFile();
Logan Chien9207a2e2011-10-21 15:39:28 +0800241
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700242 int compile();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700243
Logan Chien9207a2e2011-10-21 15:39:28 +0800244 char const *getErrorMessage() { return mDiagClient->str().c_str(); }
Shih-wei Liao4c9f7422010-08-05 04:30:02 -0700245
mkopec1c460b372012-01-09 11:21:50 -0500246 void setDebugMetadataEmission(bool EmitDebug);
247
248 void setOptimizationLevel(llvm::CodeGenOpt::Level OptimizationLevel);
249
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700250 // Compile bunch of RS files given in the llvm-rs-cc arguments. Return true if
251 // all given input files are successfully compiled without errors.
252 //
253 // @IOFiles - List of pairs of <input file path, output file path>.
254 //
255 // @DepFiles - List of pairs of <output dep. file path, dependent bitcode
256 // target>. If @OutputDep is true, this parameter must be given
257 // with the same number of pairs given in @IOFiles.
258 //
259 // @Opts - Selection of options defined from invoking llvm-rs-cc
260 bool
261 compile(const std::list<std::pair<const char *, const char *>> &IOFiles64,
262 const std::list<std::pair<const char *, const char *>> &IOFiles32,
263 const std::list<std::pair<const char *, const char *>> &DepFiles,
Jean-Luc Brouillet666b0a72015-05-01 16:13:11 -0700264 const RSCCOptions &Opts,
265 clang::DiagnosticOptions &DiagOpts);
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700266
267 clang::ModuleLoadResult loadModule(clang::SourceLocation ImportLoc,
268 clang::ModuleIdPath Path,
269 clang::Module::NameVisibilityKind VK,
270 bool IsInclusionDirective) override;
271
272 void makeModuleVisible(clang::Module *Mod,
273 clang::Module::NameVisibilityKind Visibility,
274 clang::SourceLocation ImportLoc,
275 bool Complain = false) override {}
276
277 clang::GlobalModuleIndex *
Stephen Hines73320392015-06-10 17:08:16 -0700278 loadGlobalModuleIndex(clang::SourceLocation TriggerLoc) override {
279 // We don't support C++ modules for RenderScript.
280 return nullptr;
281 }
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700282
283 bool lookupMissingImports(llvm::StringRef Name,
Stephen Hines73320392015-06-10 17:08:16 -0700284 clang::SourceLocation TriggerLoc) override {
285 // We don't support C++ modules for RenderScript.
286 return false;
287 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700288};
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700289
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -0700290} // namespace slang
Stephen Hinese639eb52010-11-08 19:27:20 -0800291
292#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_H_ NOLINT