blob: 02a77bbedfe3ab0f7dba676a4aa59506f4709f70 [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#include "slang.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070019#include <stdlib.h>
20
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070021#include <cstring>
22#include <list>
23#include <sstream>
Stephen Hinese639eb52010-11-08 19:27:20 -080024#include <string>
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070025#include <utility>
Stephen Hinese639eb52010-11-08 19:27:20 -080026#include <vector>
27
28#include "clang/AST/ASTConsumer.h"
29#include "clang/AST/ASTContext.h"
30
Loganbe274822011-02-16 22:02:54 +080031#include "clang/Basic/DiagnosticIDs.h"
Stephen Hines23c43582013-01-09 20:02:04 -080032#include "clang/Basic/DiagnosticOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080033#include "clang/Basic/FileManager.h"
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -080034#include "clang/Basic/FileSystemOptions.h"
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070035#include "clang/Basic/SourceLocation.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080036#include "clang/Basic/SourceManager.h"
37#include "clang/Basic/TargetInfo.h"
38#include "clang/Basic/TargetOptions.h"
39
Stephen Hinese639eb52010-11-08 19:27:20 -080040#include "clang/Frontend/DependencyOutputOptions.h"
41#include "clang/Frontend/FrontendDiagnostic.h"
Stephen Hines0e4ee652014-06-10 14:32:26 -070042#include "clang/Frontend/FrontendOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080043#include "clang/Frontend/TextDiagnosticPrinter.h"
44#include "clang/Frontend/Utils.h"
45
46#include "clang/Lex/Preprocessor.h"
Stephen Hines23c43582013-01-09 20:02:04 -080047#include "clang/Lex/PreprocessorOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080048#include "clang/Lex/HeaderSearch.h"
Stephen Hines23c43582013-01-09 20:02:04 -080049#include "clang/Lex/HeaderSearchOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080050
51#include "clang/Parse/ParseAST.h"
52
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070053#include "clang/Sema/SemaDiagnostic.h"
54
Loganbe274822011-02-16 22:02:54 +080055#include "llvm/ADT/IntrusiveRefCntPtr.h"
56
Stephen Hinese639eb52010-11-08 19:27:20 -080057#include "llvm/Bitcode/ReaderWriter.h"
58
59// More force linking
Tim Murrayee4016d2014-04-10 15:49:08 -070060#include "llvm/Linker/Linker.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080061
Zonr Chang08df36e2010-10-07 18:50:42 +080062// Force linking all passes/vmcore stuffs to libslang.so
Stephen Hines0da7f6c2013-03-05 15:19:02 -080063#include "llvm/LinkAllIR.h"
Zonr Chang08df36e2010-10-07 18:50:42 +080064#include "llvm/LinkAllPasses.h"
Zonr Chang08df36e2010-10-07 18:50:42 +080065
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080066#include "llvm/Support/raw_ostream.h"
zonr6315f762010-10-05 15:35:14 +080067#include "llvm/Support/MemoryBuffer.h"
68#include "llvm/Support/ErrorHandling.h"
69#include "llvm/Support/ManagedStatic.h"
Loganbe274822011-02-16 22:02:54 +080070#include "llvm/Support/Path.h"
Logan Chien9207a2e2011-10-21 15:39:28 +080071#include "llvm/Support/TargetSelect.h"
72#include "llvm/Support/ToolOutputFile.h"
Stephen Hinescc0efad2010-10-04 16:13:02 -070073
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070074#include "os_sep.h"
75#include "rs_cc_options.h"
Stephen Hines6e6578a2011-02-07 18:05:48 -080076#include "slang_assert.h"
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080077#include "slang_backend.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070078
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070079#include "slang_rs_context.h"
80#include "slang_rs_export_type.h"
81
82#include "slang_rs_reflection.h"
83#include "slang_rs_reflection_cpp.h"
84
85
Zonr Chang08df36e2010-10-07 18:50:42 +080086namespace {
Zonr Chang08df36e2010-10-07 18:50:42 +080087
Stephen Hines9ae18b22014-06-10 23:53:00 -070088static const char *kRSTriple32 = "armv7-none-linux-gnueabi";
89static const char *kRSTriple64 = "aarch64-none-linux-gnueabi";
90
Stephen Hinese639eb52010-11-08 19:27:20 -080091} // namespace
92
93namespace slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070094
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070095
96#define FS_SUFFIX "fs"
97
98#define RS_HEADER_SUFFIX "rsh"
99
100/* RS_HEADER_ENTRY(name) */
101#define ENUM_RS_HEADER() \
102 RS_HEADER_ENTRY(rs_allocation_data) \
103 RS_HEADER_ENTRY(rs_atomic) \
104 RS_HEADER_ENTRY(rs_convert) \
105 RS_HEADER_ENTRY(rs_core) \
106 RS_HEADER_ENTRY(rs_debug) \
107 RS_HEADER_ENTRY(rs_for_each) \
108 RS_HEADER_ENTRY(rs_graphics) \
109 RS_HEADER_ENTRY(rs_graphics_types) \
110 RS_HEADER_ENTRY(rs_io) \
111 RS_HEADER_ENTRY(rs_math) \
112 RS_HEADER_ENTRY(rs_matrix) \
113 RS_HEADER_ENTRY(rs_object_info) \
114 RS_HEADER_ENTRY(rs_object_types) \
115 RS_HEADER_ENTRY(rs_quaternion) \
116 RS_HEADER_ENTRY(rs_time) \
117 RS_HEADER_ENTRY(rs_value_types) \
118 RS_HEADER_ENTRY(rs_vector_math) \
119
120
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700121// The named of metadata node that pragma resides (should be synced with
122// bcc.cpp)
Shih-wei Liaof52a6202010-09-10 17:40:53 -0700123const llvm::StringRef Slang::PragmaMetadataName = "#pragma";
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700124
Logan Chien9207a2e2011-10-21 15:39:28 +0800125static inline llvm::tool_output_file *
126OpenOutputFile(const char *OutputFile,
Stephen Hinesa1f95ee2013-08-09 01:26:08 -0700127 llvm::sys::fs::OpenFlags Flags,
Stephen Hines3eb819a2014-11-24 18:16:52 -0800128 std::error_code &EC,
Stephen Hinese67239d2012-02-24 15:08:36 -0800129 clang::DiagnosticsEngine *DiagEngine) {
Stephen Hines3eb819a2014-11-24 18:16:52 -0800130 slangAssert((OutputFile != nullptr) &&
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700131 (DiagEngine != nullptr) && "Invalid parameter!");
Zonr Change8c263a2010-10-12 00:35:29 +0800132
Stephen Hines3eb819a2014-11-24 18:16:52 -0800133 EC = llvm::sys::fs::create_directories(
134 llvm::sys::path::parent_path(OutputFile));
135 if (!EC) {
Zonr Change8c263a2010-10-12 00:35:29 +0800136 llvm::tool_output_file *F =
Stephen Hines3eb819a2014-11-24 18:16:52 -0800137 new llvm::tool_output_file(OutputFile, EC, Flags);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700138 if (F != nullptr)
Zonr Change8c263a2010-10-12 00:35:29 +0800139 return F;
140 }
141
142 // Report error here.
Logan Chien9207a2e2011-10-21 15:39:28 +0800143 DiagEngine->Report(clang::diag::err_fe_error_opening)
Stephen Hines3eb819a2014-11-24 18:16:52 -0800144 << OutputFile << EC.message();
Zonr Change8c263a2010-10-12 00:35:29 +0800145
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700146 return nullptr;
Zonr Change8c263a2010-10-12 00:35:29 +0800147}
148
Stephen Hines9ae18b22014-06-10 23:53:00 -0700149void Slang::createTarget(uint32_t BitWidth) {
150 std::vector<std::string> features;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700151
Stephen Hines9ae18b22014-06-10 23:53:00 -0700152 if (BitWidth == 64) {
153 mTargetOpts->Triple = kRSTriple64;
154 } else {
155 mTargetOpts->Triple = kRSTriple32;
156 // Treat long as a 64-bit type for our 32-bit RS code.
157 features.push_back("+long64");
158 mTargetOpts->FeaturesAsWritten = features;
159 }
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700160
Logan Chien9207a2e2011-10-21 15:39:28 +0800161 mTarget.reset(clang::TargetInfo::CreateTargetInfo(*mDiagEngine,
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700162 mTargetOpts));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700163}
164
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800165void Slang::createFileManager() {
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -0800166 mFileSysOpt.reset(new clang::FileSystemOptions());
Loganbe274822011-02-16 22:02:54 +0800167 mFileMgr.reset(new clang::FileManager(*mFileSysOpt));
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800168}
169
170void Slang::createSourceManager() {
Logan Chien9207a2e2011-10-21 15:39:28 +0800171 mSourceMgr.reset(new clang::SourceManager(*mDiagEngine, *mFileMgr));
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800172}
173
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700174void Slang::createPreprocessor() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700175 // Default only search header file in current dir
Stephen Hines23c43582013-01-09 20:02:04 -0800176 llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> HSOpts =
177 new clang::HeaderSearchOptions();
178 clang::HeaderSearch *HeaderInfo = new clang::HeaderSearch(HSOpts,
Stephen Hineseeb251e2014-02-11 02:08:05 -0800179 *mSourceMgr,
Stephen Hines0444de02012-03-02 23:19:06 -0800180 *mDiagEngine,
181 LangOpts,
182 mTarget.get());
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700183
Stephen Hines23c43582013-01-09 20:02:04 -0800184 llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions> PPOpts =
185 new clang::PreprocessorOptions();
186 mPP.reset(new clang::Preprocessor(PPOpts,
187 *mDiagEngine,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700188 LangOpts,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700189 *mSourceMgr,
Logan Chien9207a2e2011-10-21 15:39:28 +0800190 *HeaderInfo,
191 *this,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700192 nullptr,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800193 /* OwnsHeaderSearch = */true));
Stephen Hines3fd0a942011-01-18 12:27:39 -0800194 // Initialize the preprocessor
Stephen Hines75d47182014-05-28 09:43:18 -0700195 mPP->Initialize(getTargetInfo());
Stephen Hines0e4ee652014-06-10 14:32:26 -0700196 clang::FrontendOptions FEOpts;
Stephen Hines7ac9d0d2014-07-15 16:50:03 -0700197 clang::InitializePreprocessor(*mPP, *PPOpts, FEOpts);
Stephen Hines0e4ee652014-06-10 14:32:26 -0700198
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700199 mPragmas.clear();
Stephen Hines3fd0a942011-01-18 12:27:39 -0800200 mPP->AddPragmaHandler(new PragmaRecorder(&mPragmas));
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700201
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700202 std::vector<clang::DirectoryLookup> SearchList;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800203 for (unsigned i = 0, e = mIncludePaths.size(); i != e; i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700204 if (const clang::DirectoryEntry *DE =
Loganbe274822011-02-16 22:02:54 +0800205 mFileMgr->getDirectory(mIncludePaths[i])) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700206 SearchList.push_back(clang::DirectoryLookup(DE,
207 clang::SrcMgr::C_System,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700208 false));
209 }
Shih-wei Liao90898282010-07-19 18:38:57 -0700210 }
211
Logan Chien9207a2e2011-10-21 15:39:28 +0800212 HeaderInfo->SetSearchPaths(SearchList,
213 /* angledDirIdx = */1,
214 /* systemDixIdx = */1,
215 /* noCurDirSearch = */false);
Shih-wei Liao90898282010-07-19 18:38:57 -0700216
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800217 initPreprocessor();
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700218}
219
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800220void Slang::createASTContext() {
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700221 mASTContext.reset(
222 new clang::ASTContext(LangOpts, *mSourceMgr, mPP->getIdentifierTable(),
223 mPP->getSelectorTable(), mPP->getBuiltinInfo()));
Stephen Hines75d47182014-05-28 09:43:18 -0700224 mASTContext->InitBuiltinTypes(getTargetInfo());
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800225 initASTContext();
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800226}
227
Logan Chien9207a2e2011-10-21 15:39:28 +0800228clang::ASTConsumer *
David Gross2770d0e2015-08-03 14:58:59 -0700229Slang::createBackend(const RSCCOptions &Opts, const clang::CodeGenOptions &CodeGenOpts,
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700230 llvm::raw_ostream *OS, OutputType OT) {
David Gross2770d0e2015-08-03 14:58:59 -0700231 return new Backend(mRSContext, &getDiagnostics(), Opts, CodeGenOpts,
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700232 getTargetOptions(), &mPragmas, OS, OT, getSourceManager(),
233 mAllowRSPrefix, mIsFilterscript);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800234}
235
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700236Slang::Slang(uint32_t BitWidth, clang::DiagnosticsEngine *DiagEngine,
237 DiagnosticBuffer *DiagClient)
238 : mDiagEngine(DiagEngine), mDiagClient(DiagClient),
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700239 mTargetOpts(new clang::TargetOptions()), mOT(OT_Default),
240 mRSContext(nullptr), mAllowRSPrefix(false), mTargetAPI(0),
241 mVerbose(false), mIsFilterscript(false) {
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700242 // Please refer to include/clang/Basic/LangOptions.h to setup
243 // the options.
244 LangOpts.RTTI = 0; // Turn off the RTTI information support
245 LangOpts.LineComment = 1;
246 LangOpts.C99 = 1;
247 LangOpts.Renderscript = 1;
248 LangOpts.LaxVectorConversions = 0; // Do not bitcast vectors!
249 LangOpts.CharIsSigned = 1; // Signed char is our default.
250
251 CodeGenOpts.OptimizationLevel = 3;
252
253 createTarget(BitWidth);
254 createFileManager();
255 createSourceManager();
Zonr Chang641558f2010-10-12 21:07:06 +0800256}
257
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700258Slang::~Slang() {
259 delete mRSContext;
260 for (ReflectedDefinitionListTy::iterator I = ReflectedDefinitions.begin(),
261 E = ReflectedDefinitions.end();
262 I != E; I++) {
263 delete I->getValue().first;
264 }
265}
266
Stephen Hines23c43582013-01-09 20:02:04 -0800267clang::ModuleLoadResult Slang::loadModule(
268 clang::SourceLocation ImportLoc,
269 clang::ModuleIdPath Path,
270 clang::Module::NameVisibilityKind Visibility,
271 bool IsInclusionDirective) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800272 slangAssert(0 && "Not implemented");
Stephen Hines23c43582013-01-09 20:02:04 -0800273 return clang::ModuleLoadResult();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700274}
275
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800276bool Slang::setInputSource(llvm::StringRef InputFile) {
277 mInputFileName = InputFile.str();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700278
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700279 mSourceMgr->clearIDTables();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700280
Loganbe274822011-02-16 22:02:54 +0800281 const clang::FileEntry *File = mFileMgr->getFile(InputFile);
Stephen Hines75d47182014-05-28 09:43:18 -0700282 if (File) {
283 mSourceMgr->setMainFileID(mSourceMgr->createFileID(File,
284 clang::SourceLocation(), clang::SrcMgr::C_User));
285 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700286
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700287 if (mSourceMgr->getMainFileID().isInvalid()) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800288 mDiagEngine->Report(clang::diag::err_fe_error_reading) << InputFile;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700289 return false;
290 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700291
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700292 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700293}
294
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800295bool Slang::setOutput(const char *OutputFile) {
Stephen Hines3eb819a2014-11-24 18:16:52 -0800296 std::error_code EC;
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700297 llvm::tool_output_file *OS = nullptr;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700298
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800299 switch (mOT) {
Stephen Hinescc0efad2010-10-04 16:13:02 -0700300 case OT_Dependency:
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800301 case OT_Assembly:
302 case OT_LLVMAssembly: {
Stephen Hines3eb819a2014-11-24 18:16:52 -0800303 OS = OpenOutputFile(OutputFile, llvm::sys::fs::F_Text, EC, mDiagEngine);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700304 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700305 }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800306 case OT_Nothing: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700307 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700308 }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800309 case OT_Object:
Stephen Hinescc0efad2010-10-04 16:13:02 -0700310 case OT_Bitcode: {
Stephen Hines3eb819a2014-11-24 18:16:52 -0800311 OS = OpenOutputFile(OutputFile, llvm::sys::fs::F_None, EC, mDiagEngine);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700312 break;
313 }
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800314 default: {
Stephen Hinescc0efad2010-10-04 16:13:02 -0700315 llvm_unreachable("Unknown compiler output type");
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800316 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700317 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700318
Stephen Hines3eb819a2014-11-24 18:16:52 -0800319 if (EC)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700320 return false;
Zonr Change8c263a2010-10-12 00:35:29 +0800321
322 mOS.reset(OS);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700323
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800324 mOutputFileName = OutputFile;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700325
326 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700327}
328
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700329bool Slang::setDepOutput(const char *OutputFile) {
Stephen Hines3eb819a2014-11-24 18:16:52 -0800330 std::error_code EC;
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800331
Stephen Hinesa1f95ee2013-08-09 01:26:08 -0700332 mDOS.reset(
Stephen Hines3eb819a2014-11-24 18:16:52 -0800333 OpenOutputFile(OutputFile, llvm::sys::fs::F_Text, EC, mDiagEngine));
334 if (EC || (mDOS.get() == nullptr))
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700335 return false;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700336
337 mDepOutputFileName = OutputFile;
338
339 return true;
340}
341
Ying Wang21c94c92015-07-22 16:27:14 -0700342int Slang::generateDepFile(bool PhonyTarget) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800343 if (mDiagEngine->hasErrorOccurred())
Loganbe274822011-02-16 22:02:54 +0800344 return 1;
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700345 if (mDOS.get() == nullptr)
Stephen Hinesf7de8522010-10-06 11:46:18 -0700346 return 1;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700347
Zonr Change8c263a2010-10-12 00:35:29 +0800348 // Initialize options for generating dependency file
Stephen Hinescc0efad2010-10-04 16:13:02 -0700349 clang::DependencyOutputOptions DepOpts;
350 DepOpts.IncludeSystemHeaders = 1;
Ying Wang21c94c92015-07-22 16:27:14 -0700351 if (PhonyTarget)
352 DepOpts.UsePhonyTargets = 1;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700353 DepOpts.OutputFile = mDepOutputFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700354 DepOpts.Targets = mAdditionalDepTargets;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700355 DepOpts.Targets.push_back(mDepTargetBCFileName);
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800356 for (std::vector<std::string>::const_iterator
357 I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end();
358 I != E;
359 I++) {
360 DepOpts.Targets.push_back(*I);
361 }
362 mGeneratedFileNames.clear();
Stephen Hinescc0efad2010-10-04 16:13:02 -0700363
Zonr Change8c263a2010-10-12 00:35:29 +0800364 // Per-compilation needed initialization
Stephen Hinescc0efad2010-10-04 16:13:02 -0700365 createPreprocessor();
Tim Murrayee4016d2014-04-10 15:49:08 -0700366 clang::DependencyFileGenerator::CreateAndAttachToPreprocessor(*mPP.get(), DepOpts);
Stephen Hinescc0efad2010-10-04 16:13:02 -0700367
Zonr Change8c263a2010-10-12 00:35:29 +0800368 // Inform the diagnostic client we are processing a source file
Stephen Hinescc0efad2010-10-04 16:13:02 -0700369 mDiagClient->BeginSourceFile(LangOpts, mPP.get());
370
Zonr Change8c263a2010-10-12 00:35:29 +0800371 // Go through the source file (no operations necessary)
Stephen Hinescc0efad2010-10-04 16:13:02 -0700372 clang::Token Tok;
373 mPP->EnterMainSourceFile();
374 do {
375 mPP->Lex(Tok);
376 } while (Tok.isNot(clang::tok::eof));
377
378 mPP->EndSourceFile();
379
Zonr Change8c263a2010-10-12 00:35:29 +0800380 // Declare success if no error
Logan Chien9207a2e2011-10-21 15:39:28 +0800381 if (!mDiagEngine->hasErrorOccurred())
Zonr Change8c263a2010-10-12 00:35:29 +0800382 mDOS->keep();
383
384 // Clean up after compilation
Stephen Hinescc0efad2010-10-04 16:13:02 -0700385 mPP.reset();
Zonr Change8c263a2010-10-12 00:35:29 +0800386 mDOS.reset();
Stephen Hinescc0efad2010-10-04 16:13:02 -0700387
Logan Chien9207a2e2011-10-21 15:39:28 +0800388 return mDiagEngine->hasErrorOccurred() ? 1 : 0;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700389}
390
David Gross2770d0e2015-08-03 14:58:59 -0700391int Slang::compile(const RSCCOptions &Opts) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800392 if (mDiagEngine->hasErrorOccurred())
Loganbe274822011-02-16 22:02:54 +0800393 return 1;
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700394 if (mOS.get() == nullptr)
Stephen Hinesf7de8522010-10-06 11:46:18 -0700395 return 1;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700396
397 // Here is per-compilation needed initialization
398 createPreprocessor();
399 createASTContext();
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800400
David Gross2770d0e2015-08-03 14:58:59 -0700401 mBackend.reset(createBackend(Opts, CodeGenOpts, &mOS->os(), mOT));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700402
403 // Inform the diagnostic client we are processing a source file
404 mDiagClient->BeginSourceFile(LangOpts, mPP.get());
405
406 // The core of the slang compiler
407 ParseAST(*mPP, mBackend.get(), *mASTContext);
408
Zonr Change8c263a2010-10-12 00:35:29 +0800409 // Inform the diagnostic client we are done with previous source file
410 mDiagClient->EndSourceFile();
411
412 // Declare success if no error
Logan Chien9207a2e2011-10-21 15:39:28 +0800413 if (!mDiagEngine->hasErrorOccurred())
Zonr Change8c263a2010-10-12 00:35:29 +0800414 mOS->keep();
415
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800416 // The compilation ended, clear
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700417 mBackend.reset();
Zonr Change8c263a2010-10-12 00:35:29 +0800418 mOS.reset();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700419
Logan Chien9207a2e2011-10-21 15:39:28 +0800420 return mDiagEngine->hasErrorOccurred() ? 1 : 0;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700421}
422
mkopec1c460b372012-01-09 11:21:50 -0500423void Slang::setDebugMetadataEmission(bool EmitDebug) {
Shih-wei Liao43730fe2012-08-02 23:06:18 -0700424 if (EmitDebug)
Stephen Hines23c43582013-01-09 20:02:04 -0800425 CodeGenOpts.setDebugInfo(clang::CodeGenOptions::FullDebugInfo);
Shih-wei Liao43730fe2012-08-02 23:06:18 -0700426 else
Stephen Hines23c43582013-01-09 20:02:04 -0800427 CodeGenOpts.setDebugInfo(clang::CodeGenOptions::NoDebugInfo);
mkopec1c460b372012-01-09 11:21:50 -0500428}
429
430void Slang::setOptimizationLevel(llvm::CodeGenOpt::Level OptimizationLevel) {
431 CodeGenOpts.OptimizationLevel = OptimizationLevel;
432}
433
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700434bool Slang::isFilterscript(const char *Filename) {
435 const char *c = strrchr(Filename, '.');
436 if (c && !strncmp(FS_SUFFIX, c + 1, strlen(FS_SUFFIX) + 1)) {
437 return true;
438 } else {
439 return false;
440 }
441}
442
443bool Slang::generateJavaBitcodeAccessor(const std::string &OutputPathBase,
444 const std::string &PackageName,
445 const std::string *LicenseNote) {
446 RSSlangReflectUtils::BitCodeAccessorContext BCAccessorContext;
447
448 BCAccessorContext.rsFileName = getInputFileName().c_str();
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700449 BCAccessorContext.bc32FileName = mOutput32FileName.c_str();
450 BCAccessorContext.bc64FileName = mOutputFileName.c_str();
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700451 BCAccessorContext.reflectPath = OutputPathBase.c_str();
452 BCAccessorContext.packageName = PackageName.c_str();
453 BCAccessorContext.licenseNote = LicenseNote;
454 BCAccessorContext.bcStorage = BCST_JAVA_CODE; // Must be BCST_JAVA_CODE
455 BCAccessorContext.verbose = false;
456
457 return RSSlangReflectUtils::GenerateJavaBitCodeAccessor(BCAccessorContext);
458}
459
460bool Slang::checkODR(const char *CurInputFile) {
461 for (RSContext::ExportableList::iterator I = mRSContext->exportable_begin(),
462 E = mRSContext->exportable_end();
463 I != E;
464 I++) {
465 RSExportable *RSE = *I;
466 if (RSE->getKind() != RSExportable::EX_TYPE)
467 continue;
468
469 RSExportType *ET = static_cast<RSExportType *>(RSE);
470 if (ET->getClass() != RSExportType::ExportClassRecord)
471 continue;
472
473 RSExportRecordType *ERT = static_cast<RSExportRecordType *>(ET);
474
475 // Artificial record types (create by us not by user in the source) always
476 // conforms the ODR.
477 if (ERT->isArtificial())
478 continue;
479
480 // Key to lookup ERT in ReflectedDefinitions
481 llvm::StringRef RDKey(ERT->getName());
482 ReflectedDefinitionListTy::const_iterator RD =
483 ReflectedDefinitions.find(RDKey);
484
485 if (RD != ReflectedDefinitions.end()) {
486 const RSExportRecordType *Reflected = RD->getValue().first;
487 // There's a record (struct) with the same name reflected before. Enforce
488 // ODR checking - the Reflected must hold *exactly* the same "definition"
489 // as the one defined previously. We say two record types A and B have the
490 // same definition iff:
491 //
492 // struct A { struct B {
493 // Type(a1) a1, Type(b1) b1,
494 // Type(a2) a2, Type(b1) b2,
495 // ... ...
496 // Type(aN) aN Type(b3) b3,
497 // }; }
498 // Cond. #1. They have same number of fields, i.e., N = M;
499 // Cond. #2. for (i := 1 to N)
500 // Type(ai) = Type(bi) must hold;
501 // Cond. #3. for (i := 1 to N)
502 // Name(ai) = Name(bi) must hold;
503 //
504 // where,
505 // Type(F) = the type of field F and
506 // Name(F) = the field name.
507
508 bool PassODR = false;
509 // Cond. #1 and Cond. #2
510 if (Reflected->equals(ERT)) {
511 // Cond #3.
512 RSExportRecordType::const_field_iterator AI = Reflected->fields_begin(),
513 BI = ERT->fields_begin();
514
515 for (unsigned i = 0, e = Reflected->getFields().size(); i != e; i++) {
516 if ((*AI)->getName() != (*BI)->getName())
517 break;
518 AI++;
519 BI++;
520 }
521 PassODR = (AI == (Reflected->fields_end()));
522 }
523
524 if (!PassODR) {
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700525 unsigned DiagID = mDiagEngine->getCustomDiagID(
526 clang::DiagnosticsEngine::Error,
527 "type '%0' in different translation unit (%1 v.s. %2) "
528 "has incompatible type definition");
529 getDiagnostics().Report(DiagID) << Reflected->getName()
530 << getInputFileName()
531 << RD->getValue().second;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700532 return false;
533 }
534 } else {
535 llvm::StringMapEntry<ReflectedDefinitionTy> *ME =
536 llvm::StringMapEntry<ReflectedDefinitionTy>::Create(RDKey);
537 ME->setValue(std::make_pair(ERT, CurInputFile));
538
Matt Wala3f45a252015-06-30 11:24:19 -0700539 if (!ReflectedDefinitions.insert(ME)) {
540 slangAssert(false && "Type shouldn't be in map yet!");
541 }
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700542
543 // Take the ownership of ERT such that it won't be freed in ~RSContext().
544 ERT->keep();
545 }
546 }
547 return true;
548}
549
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700550void Slang::initPreprocessor() {
551 clang::Preprocessor &PP = getPreprocessor();
552
553 std::stringstream RSH;
554 RSH << PP.getPredefines();
555 RSH << "#define RS_VERSION " << mTargetAPI << "\n";
556 RSH << "#include \"rs_core." RS_HEADER_SUFFIX "\"\n";
557 PP.setPredefines(RSH.str());
558}
559
560void Slang::initASTContext() {
561 mRSContext = new RSContext(getPreprocessor(),
562 getASTContext(),
563 getTargetInfo(),
564 &mPragmas,
565 mTargetAPI,
566 mVerbose);
567}
568
569bool Slang::IsRSHeaderFile(const char *File) {
570#define RS_HEADER_ENTRY(name) \
571 if (::strcmp(File, #name "." RS_HEADER_SUFFIX) == 0) \
572 return true;
573ENUM_RS_HEADER()
574#undef RS_HEADER_ENTRY
575 return false;
576}
577
578bool Slang::IsLocInRSHeaderFile(const clang::SourceLocation &Loc,
579 const clang::SourceManager &SourceMgr) {
580 clang::FullSourceLoc FSL(Loc, SourceMgr);
581 clang::PresumedLoc PLoc = SourceMgr.getPresumedLoc(FSL);
582
583 const char *Filename = PLoc.getFilename();
584 if (!Filename) {
585 return false;
586 } else {
587 return IsRSHeaderFile(llvm::sys::path::filename(Filename).data());
588 }
589}
590
591bool Slang::compile(
592 const std::list<std::pair<const char*, const char*> > &IOFiles64,
593 const std::list<std::pair<const char*, const char*> > &IOFiles32,
594 const std::list<std::pair<const char*, const char*> > &DepFiles,
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700595 const RSCCOptions &Opts,
596 clang::DiagnosticOptions &DiagOpts) {
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700597 if (IOFiles32.empty())
598 return true;
599
600 if (Opts.mEmitDependency && (DepFiles.size() != IOFiles32.size())) {
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700601 unsigned DiagID = mDiagEngine->getCustomDiagID(
602 clang::DiagnosticsEngine::Error,
603 "invalid parameter for output dependencies files.");
604 getDiagnostics().Report(DiagID);
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700605 return false;
606 }
607
608 if (Opts.mEmit3264 && (IOFiles64.size() != IOFiles32.size())) {
609 slangAssert(false && "Should have equal number of 32/64-bit files");
610 return false;
611 }
612
613 std::string RealPackageName;
614
615 const char *InputFile, *Output64File, *Output32File, *BCOutputFile,
616 *DepOutputFile;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700617
618 setIncludePaths(Opts.mIncludePaths);
619 setOutputType(Opts.mOutputType);
620 if (Opts.mEmitDependency) {
621 setAdditionalDepTargets(Opts.mAdditionalDepTargets);
622 }
623
624 setDebugMetadataEmission(Opts.mDebugEmission);
625
626 setOptimizationLevel(Opts.mOptimizationLevel);
627
628 mAllowRSPrefix = Opts.mAllowRSPrefix;
629
630 mTargetAPI = Opts.mTargetAPI;
631 if (mTargetAPI != SLANG_DEVELOPMENT_TARGET_API &&
632 (mTargetAPI < SLANG_MINIMUM_TARGET_API ||
633 mTargetAPI > SLANG_MAXIMUM_TARGET_API)) {
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700634 unsigned DiagID = mDiagEngine->getCustomDiagID(
635 clang::DiagnosticsEngine::Error,
636 "target API level '%0' is out of range ('%1' - '%2')");
637 getDiagnostics().Report(DiagID) << mTargetAPI << SLANG_MINIMUM_TARGET_API
638 << SLANG_MAXIMUM_TARGET_API;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700639 return false;
640 }
641
Stephen Hinesbd0a7dd2015-07-01 19:24:00 -0700642 if (mTargetAPI >= SLANG_M_TARGET_API) {
Pirama Arumuga Nainare4dd17d2015-04-07 11:09:14 -0700643 LangOpts.NativeHalfType = 1;
644 LangOpts.HalfArgsAndReturns = 1;
645 }
646
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700647 mVerbose = Opts.mVerbose;
648
649 // Skip generation of warnings a second time if we are doing more than just
650 // a single pass over the input file.
651 bool SuppressAllWarnings = (Opts.mOutputType != Slang::OT_Dependency);
652
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700653 std::list<std::pair<const char*, const char*> >::const_iterator
654 IOFile64Iter = IOFiles64.begin(),
655 IOFile32Iter = IOFiles32.begin(),
656 DepFileIter = DepFiles.begin();
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700657
658 for (unsigned i = 0, e = IOFiles32.size(); i != e; i++) {
659 InputFile = IOFile64Iter->first;
660 Output64File = IOFile64Iter->second;
661 Output32File = IOFile32Iter->second;
662
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700663 if (!setInputSource(InputFile))
664 return false;
665
666 if (!setOutput(Output64File))
667 return false;
668
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700669 // For use with 64-bit compilation/reflection. This only sets the filename of
670 // the 32-bit bitcode file, and doesn't actually verify it already exists.
671 mOutput32FileName = Output32File;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700672
673 mIsFilterscript = isFilterscript(InputFile);
674
Stephen McGroarty68e562f2015-06-05 16:57:24 +0100675 CodeGenOpts.MainFileName = mInputFileName;
676
David Gross2770d0e2015-08-03 14:58:59 -0700677 if (Slang::compile(Opts) > 0)
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700678 return false;
679
680 if (!Opts.mJavaReflectionPackageName.empty()) {
681 mRSContext->setReflectJavaPackageName(Opts.mJavaReflectionPackageName);
682 }
683 const std::string &RealPackageName =
684 mRSContext->getReflectJavaPackageName();
685
686 bool doReflection = true;
687 if (Opts.mEmit3264 && (Opts.mBitWidth == 32)) {
688 // Skip reflection on the 32-bit path if we are going to emit it on the
689 // 64-bit path.
690 doReflection = false;
691 }
692 if (Opts.mOutputType != Slang::OT_Dependency && doReflection) {
693
694 if (Opts.mBitcodeStorage == BCST_CPP_CODE) {
695 const std::string &outputFileName = (Opts.mBitWidth == 64) ?
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700696 mOutputFileName : mOutput32FileName;
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700697 RSReflectionCpp R(mRSContext, Opts.mJavaReflectionPathBase,
698 getInputFileName(), outputFileName);
699 if (!R.reflect()) {
700 return false;
701 }
702 } else {
703 if (!Opts.mRSPackageName.empty()) {
704 mRSContext->setRSPackageName(Opts.mRSPackageName);
705 }
706
Jean-Luc Brouilletff5abc62015-05-05 21:16:44 -0700707 std::vector<std::string> generatedFileNames;
708 RSReflectionJava R(mRSContext, &generatedFileNames,
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700709 Opts.mJavaReflectionPathBase, getInputFileName(),
Jean-Luc Brouillet5309b0c2015-05-01 16:13:11 -0700710 mOutputFileName,
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700711 Opts.mBitcodeStorage == BCST_JAVA_CODE);
712 if (!R.reflect()) {
713 // TODO Is this needed or will the error message have been printed
714 // already? and why not for the C++ case?
715 fprintf(stderr, "RSContext::reflectToJava : failed to do reflection "
716 "(%s)\n",
717 R.getLastError());
718 return false;
719 }
720
721 for (std::vector<std::string>::const_iterator
Jean-Luc Brouilletff5abc62015-05-05 21:16:44 -0700722 I = generatedFileNames.begin(), E = generatedFileNames.end();
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700723 I != E;
724 I++) {
725 std::string ReflectedName = RSSlangReflectUtils::ComputePackagedPath(
726 Opts.mJavaReflectionPathBase.c_str(),
727 (RealPackageName + OS_PATH_SEPARATOR_STR + *I).c_str());
728 appendGeneratedFileName(ReflectedName + ".java");
729 }
730
731 if ((Opts.mOutputType == Slang::OT_Bitcode) &&
732 (Opts.mBitcodeStorage == BCST_JAVA_CODE) &&
733 !generateJavaBitcodeAccessor(Opts.mJavaReflectionPathBase,
734 RealPackageName.c_str(),
735 mRSContext->getLicenseNote())) {
736 return false;
737 }
738 }
739 }
740
741 if (Opts.mEmitDependency) {
742 BCOutputFile = DepFileIter->first;
743 DepOutputFile = DepFileIter->second;
744
745 setDepTargetBC(BCOutputFile);
746
747 if (!setDepOutput(DepOutputFile))
748 return false;
749
750 if (SuppressAllWarnings) {
751 getDiagnostics().setSuppressAllDiagnostics(true);
752 }
Ying Wang21c94c92015-07-22 16:27:14 -0700753 if (generateDepFile(Opts.mEmitPhonyDependency) > 0)
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700754 return false;
755 if (SuppressAllWarnings) {
756 getDiagnostics().setSuppressAllDiagnostics(false);
757 }
758
759 DepFileIter++;
760 }
761
762 if (!checkODR(InputFile))
763 return false;
764
765 IOFile64Iter++;
766 IOFile32Iter++;
767 }
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700768 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700769}
Stephen Hinese639eb52010-11-08 19:27:20 -0800770
771} // namespace slang