blob: af86e78c3ab53fb2d37d4c521751d237d8485eed [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hines0a813a32012-08-03 16:52:40 -07002 * Copyright 2010-2012, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
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
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080017#include "slang_rs.h"
18
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070019#include <cstring>
Stephen Hinese639eb52010-11-08 19:27:20 -080020#include <list>
Stephen Hines2e35b132011-07-22 02:50:19 -070021#include <sstream>
Stephen Hinese639eb52010-11-08 19:27:20 -080022#include <string>
23#include <utility>
24#include <vector>
Stephen Hinesfcda2352010-10-19 16:49:32 -070025
Loganbe274822011-02-16 22:02:54 +080026#include "clang/Basic/SourceLocation.h"
27
Zonr Changcf6af6a2010-10-12 12:38:51 +080028#include "clang/Frontend/FrontendDiagnostic.h"
29
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080030#include "clang/Sema/SemaDiagnostic.h"
31
Loganbe274822011-02-16 22:02:54 +080032#include "llvm/Support/Path.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080033
Raphael8d5a2f62011-02-08 00:15:05 -080034#include "os_sep.h"
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080035#include "slang_rs_backend.h"
36#include "slang_rs_context.h"
Zonr Chang641558f2010-10-12 21:07:06 +080037#include "slang_rs_export_type.h"
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080038
Jason Sams1b6a0882012-03-12 15:07:58 -070039#include "slang_rs_reflection_cpp.h"
40
Stephen Hinese639eb52010-11-08 19:27:20 -080041namespace slang {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080042
Zonr Chang3c250c52010-10-07 12:19:23 +080043#define RS_HEADER_SUFFIX "rsh"
44
Stephen Hines2d35edd2011-08-09 11:33:27 -070045/* RS_HEADER_ENTRY(name) */
Zonr Chang3c250c52010-10-07 12:19:23 +080046#define ENUM_RS_HEADER() \
Stephen Hines2d35edd2011-08-09 11:33:27 -070047 RS_HEADER_ENTRY(rs_allocation) \
48 RS_HEADER_ENTRY(rs_atomic) \
49 RS_HEADER_ENTRY(rs_cl) \
50 RS_HEADER_ENTRY(rs_core) \
51 RS_HEADER_ENTRY(rs_debug) \
Alex Sakhartchouk4eb4b382012-03-21 09:58:39 -070052 RS_HEADER_ENTRY(rs_element) \
Stephen Hines2d35edd2011-08-09 11:33:27 -070053 RS_HEADER_ENTRY(rs_graphics) \
54 RS_HEADER_ENTRY(rs_math) \
Alex Sakhartchouk4eb4b382012-03-21 09:58:39 -070055 RS_HEADER_ENTRY(rs_mesh) \
Stephen Hines2d35edd2011-08-09 11:33:27 -070056 RS_HEADER_ENTRY(rs_matrix) \
57 RS_HEADER_ENTRY(rs_object) \
Alex Sakhartchouk4eb4b382012-03-21 09:58:39 -070058 RS_HEADER_ENTRY(rs_program) \
Stephen Hines2d35edd2011-08-09 11:33:27 -070059 RS_HEADER_ENTRY(rs_quaternion) \
Alex Sakhartchouk4eb4b382012-03-21 09:58:39 -070060 RS_HEADER_ENTRY(rs_sampler) \
Stephen Hines2d35edd2011-08-09 11:33:27 -070061 RS_HEADER_ENTRY(rs_time) \
62 RS_HEADER_ENTRY(rs_types) \
63
Zonr Chang3c250c52010-10-07 12:19:23 +080064
Zonr Changcf6af6a2010-10-12 12:38:51 +080065bool SlangRS::reflectToJava(const std::string &OutputPathBase,
66 const std::string &OutputPackageName,
Stephen Hines0a813a32012-08-03 16:52:40 -070067 const std::string &RSPackageName,
Zonr Changcf6af6a2010-10-12 12:38:51 +080068 std::string *RealPackageName) {
69 return mRSContext->reflectToJava(OutputPathBase,
70 OutputPackageName,
Stephen Hines0a813a32012-08-03 16:52:40 -070071 RSPackageName,
Zonr Changcf6af6a2010-10-12 12:38:51 +080072 getInputFileName(),
73 getOutputFileName(),
74 RealPackageName);
75}
76
77bool SlangRS::generateBitcodeAccessor(const std::string &OutputPathBase,
78 const std::string &PackageName) {
79 RSSlangReflectUtils::BitCodeAccessorContext BCAccessorContext;
80
81 BCAccessorContext.rsFileName = getInputFileName().c_str();
82 BCAccessorContext.bcFileName = getOutputFileName().c_str();
83 BCAccessorContext.reflectPath = OutputPathBase.c_str();
84 BCAccessorContext.packageName = PackageName.c_str();
85 BCAccessorContext.bcStorage = BCST_JAVA_CODE; // Must be BCST_JAVA_CODE
86
87 return RSSlangReflectUtils::GenerateBitCodeAccessor(BCAccessorContext);
88}
89
Zonr Change86245a2010-10-12 21:42:13 +080090bool SlangRS::checkODR(const char *CurInputFile) {
Zonr Chang641558f2010-10-12 21:07:06 +080091 for (RSContext::ExportableList::iterator I = mRSContext->exportable_begin(),
92 E = mRSContext->exportable_end();
93 I != E;
94 I++) {
Stephen Hinesa858cb62011-01-17 12:17:51 -080095 RSExportable *RSE = *I;
96 if (RSE->getKind() != RSExportable::EX_TYPE)
Zonr Chang641558f2010-10-12 21:07:06 +080097 continue;
98
Stephen Hinesa858cb62011-01-17 12:17:51 -080099 RSExportType *ET = static_cast<RSExportType *>(RSE);
Zonr Chang641558f2010-10-12 21:07:06 +0800100 if (ET->getClass() != RSExportType::ExportClassRecord)
101 continue;
102
103 RSExportRecordType *ERT = static_cast<RSExportRecordType *>(ET);
104
105 // Artificial record types (create by us not by user in the source) always
106 // conforms the ODR.
107 if (ERT->isArtificial())
108 continue;
109
110 // Key to lookup ERT in ReflectedDefinitions
111 llvm::StringRef RDKey(ERT->getName());
112 ReflectedDefinitionListTy::const_iterator RD =
113 ReflectedDefinitions.find(RDKey);
114
115 if (RD != ReflectedDefinitions.end()) {
116 const RSExportRecordType *Reflected = RD->getValue().first;
117 // There's a record (struct) with the same name reflected before. Enforce
118 // ODR checking - the Reflected must hold *exactly* the same "definition"
119 // as the one defined previously. We say two record types A and B have the
120 // same definition iff:
121 //
122 // struct A { struct B {
123 // Type(a1) a1, Type(b1) b1,
124 // Type(a2) a2, Type(b1) b2,
125 // ... ...
126 // Type(aN) aN Type(b3) b3,
127 // }; }
128 // Cond. #1. They have same number of fields, i.e., N = M;
129 // Cond. #2. for (i := 1 to N)
130 // Type(ai) = Type(bi) must hold;
131 // Cond. #3. for (i := 1 to N)
132 // Name(ai) = Name(bi) must hold;
133 //
134 // where,
135 // Type(F) = the type of field F and
136 // Name(F) = the field name.
137
138 bool PassODR = false;
139 // Cond. #1 and Cond. #2
140 if (Reflected->equals(ERT)) {
141 // Cond #3.
142 RSExportRecordType::const_field_iterator AI = Reflected->fields_begin(),
143 BI = ERT->fields_begin();
144
145 for (unsigned i = 0, e = Reflected->getFields().size(); i != e; i++) {
146 if ((*AI)->getName() != (*BI)->getName())
147 break;
148 AI++;
149 BI++;
150 }
151 PassODR = (AI == (Reflected->fields_end()));
152 }
153
154 if (!PassODR) {
155 getDiagnostics().Report(mDiagErrorODR) << Reflected->getName()
156 << getInputFileName()
157 << RD->getValue().second;
158 return false;
159 }
160 } else {
161 llvm::StringMapEntry<ReflectedDefinitionTy> *ME =
162 llvm::StringMapEntry<ReflectedDefinitionTy>::Create(RDKey.begin(),
163 RDKey.end());
Zonr Change86245a2010-10-12 21:42:13 +0800164 ME->setValue(std::make_pair(ERT, CurInputFile));
Zonr Chang641558f2010-10-12 21:07:06 +0800165
166 if (!ReflectedDefinitions.insert(ME))
167 delete ME;
168
169 // Take the ownership of ERT such that it won't be freed in ~RSContext().
170 ERT->keep();
171 }
Zonr Chang641558f2010-10-12 21:07:06 +0800172 }
173 return true;
174}
Zonr Changcf6af6a2010-10-12 12:38:51 +0800175
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800176void SlangRS::initDiagnostic() {
Logan Chien9207a2e2011-10-21 15:39:28 +0800177 clang::DiagnosticsEngine &DiagEngine = getDiagnostics();
Zonr Changcf6af6a2010-10-12 12:38:51 +0800178
Logan Chien9207a2e2011-10-21 15:39:28 +0800179 if (DiagEngine.setDiagnosticGroupMapping("implicit-function-declaration",
180 clang::diag::MAP_ERROR))
181 DiagEngine.Report(clang::diag::warn_unknown_warning_option)
182 << "implicit-function-declaration";
183
184 DiagEngine.setDiagnosticMapping(
185 clang::diag::ext_typecheck_convert_discards_qualifiers,
186 clang::diag::MAP_ERROR,
187 clang::SourceLocation());
Zonr Chang641558f2010-10-12 21:07:06 +0800188
189 mDiagErrorInvalidOutputDepParameter =
Logan Chien9207a2e2011-10-21 15:39:28 +0800190 DiagEngine.getCustomDiagID(
191 clang::DiagnosticsEngine::Error,
192 "invalid parameter for output dependencies files.");
Zonr Chang641558f2010-10-12 21:07:06 +0800193
194 mDiagErrorODR =
Logan Chien9207a2e2011-10-21 15:39:28 +0800195 DiagEngine.getCustomDiagID(
196 clang::DiagnosticsEngine::Error,
197 "type '%0' in different translation unit (%1 v.s. %2) "
198 "has incompatible type definition");
Zonr Chang641558f2010-10-12 21:07:06 +0800199
Logan Chien9207a2e2011-10-21 15:39:28 +0800200 mDiagErrorTargetAPIRange =
201 DiagEngine.getCustomDiagID(
202 clang::DiagnosticsEngine::Error,
Stephen Hines2e35b132011-07-22 02:50:19 -0700203 "target API level '%0' is out of range ('%1' - '%2')");
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800204}
205
206void SlangRS::initPreprocessor() {
207 clang::Preprocessor &PP = getPreprocessor();
208
Stephen Hines2e35b132011-07-22 02:50:19 -0700209 std::stringstream RSH;
210 RSH << "#define RS_VERSION " << mTargetAPI << std::endl;
Stephen Hines2d35edd2011-08-09 11:33:27 -0700211 RSH << "#include \"rs_core." RS_HEADER_SUFFIX "\"" << std::endl;
Stephen Hines2e35b132011-07-22 02:50:19 -0700212 PP.setPredefines(RSH.str());
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800213}
214
215void SlangRS::initASTContext() {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700216 mRSContext = new RSContext(getPreprocessor(),
217 getASTContext(),
Stephen Hines3fd0a942011-01-18 12:27:39 -0800218 getTargetInfo(),
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800219 &mPragmas,
Stephen Hines4a4bf922011-08-18 17:20:33 -0700220 mTargetAPI,
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800221 &mGeneratedFileNames);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800222}
223
224clang::ASTConsumer
225*SlangRS::createBackend(const clang::CodeGenOptions& CodeGenOpts,
226 llvm::raw_ostream *OS,
227 Slang::OutputType OT) {
228 return new RSBackend(mRSContext,
Stephen Hinese639eb52010-11-08 19:27:20 -0800229 &getDiagnostics(),
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800230 CodeGenOpts,
231 getTargetOptions(),
Stephen Hines3fd0a942011-01-18 12:27:39 -0800232 &mPragmas,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800233 OS,
234 OT,
235 getSourceManager(),
Stephen Hines4a4bf922011-08-18 17:20:33 -0700236 mAllowRSPrefix);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800237}
238
Zonr Chang592a9542010-10-07 20:03:58 +0800239bool SlangRS::IsRSHeaderFile(const char *File) {
Stephen Hines2d35edd2011-08-09 11:33:27 -0700240#define RS_HEADER_ENTRY(name) \
Zonr Chang3ab021f2010-10-18 22:32:15 +0800241 if (::strcmp(File, #name "."RS_HEADER_SUFFIX) == 0) \
Zonr Chang592a9542010-10-07 20:03:58 +0800242 return true;
243ENUM_RS_HEADER()
244#undef RS_HEADER_ENTRY
Zonr Chang592a9542010-10-07 20:03:58 +0800245 return false;
246}
247
Stephen Hinesfcda2352010-10-19 16:49:32 -0700248bool SlangRS::IsFunctionInRSHeaderFile(const clang::FunctionDecl *FD,
249 const clang::SourceManager &SourceMgr) {
250 clang::FullSourceLoc FSL(FD->getLocStart(), SourceMgr);
251 clang::PresumedLoc PLoc = SourceMgr.getPresumedLoc(FSL);
Stephen Hinesfcda2352010-10-19 16:49:32 -0700252
Stephen Hines688e64b2011-08-23 16:01:25 -0700253 const char *Filename = PLoc.getFilename();
254 if (!Filename) {
255 return false;
256 } else {
257 return IsRSHeaderFile(llvm::sys::path::filename(Filename).data());
258 }
Stephen Hinesfcda2352010-10-19 16:49:32 -0700259}
260
Logan Chien9207a2e2011-10-21 15:39:28 +0800261SlangRS::SlangRS()
262 : Slang(), mRSContext(NULL), mAllowRSPrefix(false), mTargetAPI(0) {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800263}
264
Zonr Changcf6af6a2010-10-12 12:38:51 +0800265bool SlangRS::compile(
266 const std::list<std::pair<const char*, const char*> > &IOFiles,
267 const std::list<std::pair<const char*, const char*> > &DepFiles,
268 const std::vector<std::string> &IncludePaths,
269 const std::vector<std::string> &AdditionalDepTargets,
270 Slang::OutputType OutputType, BitCodeStorageType BitcodeStorage,
271 bool AllowRSPrefix, bool OutputDep,
mkopec1c460b372012-01-09 11:21:50 -0500272 unsigned int TargetAPI, bool EmitDebug,
273 llvm::CodeGenOpt::Level OptimizationLevel,
Zonr Changcf6af6a2010-10-12 12:38:51 +0800274 const std::string &JavaReflectionPathBase,
Stephen Hines0a813a32012-08-03 16:52:40 -0700275 const std::string &JavaReflectionPackageName,
276 const std::string &RSPackageName) {
Zonr Changcf6af6a2010-10-12 12:38:51 +0800277 if (IOFiles.empty())
278 return true;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800279
Zonr Changcf6af6a2010-10-12 12:38:51 +0800280 if (OutputDep && (DepFiles.size() != IOFiles.size())) {
Zonr Chang641558f2010-10-12 21:07:06 +0800281 getDiagnostics().Report(mDiagErrorInvalidOutputDepParameter);
Zonr Changcf6af6a2010-10-12 12:38:51 +0800282 return false;
283 }
284
285 std::string RealPackageName;
286
287 const char *InputFile, *OutputFile, *BCOutputFile, *DepOutputFile;
288 std::list<std::pair<const char*, const char*> >::const_iterator
289 IOFileIter = IOFiles.begin(), DepFileIter = DepFiles.begin();
290
291 setIncludePaths(IncludePaths);
292 setOutputType(OutputType);
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800293 if (OutputDep) {
Zonr Changcf6af6a2010-10-12 12:38:51 +0800294 setAdditionalDepTargets(AdditionalDepTargets);
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800295 }
Zonr Changcf6af6a2010-10-12 12:38:51 +0800296
mkopec1c460b372012-01-09 11:21:50 -0500297 setDebugMetadataEmission(EmitDebug);
298
299 setOptimizationLevel(OptimizationLevel);
300
Zonr Changcf6af6a2010-10-12 12:38:51 +0800301 mAllowRSPrefix = AllowRSPrefix;
302
Stephen Hines2e35b132011-07-22 02:50:19 -0700303 mTargetAPI = TargetAPI;
Stephen Hines4cc499d2011-08-24 19:06:17 -0700304 if (mTargetAPI < SLANG_MINIMUM_TARGET_API ||
305 mTargetAPI > SLANG_MAXIMUM_TARGET_API) {
Stephen Hines2e35b132011-07-22 02:50:19 -0700306 getDiagnostics().Report(mDiagErrorTargetAPIRange) << mTargetAPI
Stephen Hines4cc499d2011-08-24 19:06:17 -0700307 << SLANG_MINIMUM_TARGET_API << SLANG_MAXIMUM_TARGET_API;
Stephen Hines2e35b132011-07-22 02:50:19 -0700308 return false;
309 }
310
Stephen Hinesc632be22011-09-23 15:53:16 -0700311 // Skip generation of warnings a second time if we are doing more than just
312 // a single pass over the input file.
313 bool SuppressAllWarnings = (OutputType != Slang::OT_Dependency);
314
Zonr Changcf6af6a2010-10-12 12:38:51 +0800315 for (unsigned i = 0, e = IOFiles.size(); i != e; i++) {
316 InputFile = IOFileIter->first;
317 OutputFile = IOFileIter->second;
318
319 reset();
320
321 if (!setInputSource(InputFile))
322 return false;
323
324 if (!setOutput(OutputFile))
325 return false;
326
Stephen Hinescf9a73a2012-09-20 10:38:02 -0700327 if (!JavaReflectionPackageName.empty()) {
328 mRSContext->setReflectJavaPackageName(
329 JavaReflectionPackageName);
330 }
331
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800332 if (Slang::compile() > 0)
333 return false;
334
335 if (OutputType != Slang::OT_Dependency) {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800336
Stephen Hines5c25c512012-02-27 12:58:17 -0800337 if (BitcodeStorage == BCST_CPP_CODE) {
Jason Sams1b6a0882012-03-12 15:07:58 -0700338 RSReflectionCpp R(mRSContext);
339 bool ret = R.reflect(JavaReflectionPathBase, getInputFileName(), getOutputFileName());
340 if (!ret) {
341 return false;
342 }
Stephen Hines5c25c512012-02-27 12:58:17 -0800343 } else {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800344
Stephen Hines5c25c512012-02-27 12:58:17 -0800345 if (!reflectToJava(JavaReflectionPathBase,
346 JavaReflectionPackageName,
Stephen Hines0a813a32012-08-03 16:52:40 -0700347 RSPackageName,
Stephen Hines5c25c512012-02-27 12:58:17 -0800348 &RealPackageName)) {
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800349 return false;
Stephen Hines5c25c512012-02-27 12:58:17 -0800350 }
351
352 for (std::vector<std::string>::const_iterator
353 I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end();
354 I != E;
355 I++) {
356 std::string ReflectedName = RSSlangReflectUtils::ComputePackagedPath(
357 JavaReflectionPathBase.c_str(),
358 (RealPackageName + OS_PATH_SEPARATOR_STR + *I).c_str());
359 appendGeneratedFileName(ReflectedName + ".java");
360 }
361
362 if ((OutputType == Slang::OT_Bitcode) &&
363 (BitcodeStorage == BCST_JAVA_CODE) &&
364 !generateBitcodeAccessor(JavaReflectionPathBase,
365 RealPackageName.c_str())) {
366 return false;
367 }
368 }
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800369 }
370
Zonr Changcf6af6a2010-10-12 12:38:51 +0800371 if (OutputDep) {
372 BCOutputFile = DepFileIter->first;
373 DepOutputFile = DepFileIter->second;
374
375 setDepTargetBC(BCOutputFile);
376
377 if (!setDepOutput(DepOutputFile))
378 return false;
379
Stephen Hinesc632be22011-09-23 15:53:16 -0700380 if (SuppressAllWarnings) {
381 getDiagnostics().setSuppressAllDiagnostics(true);
382 }
Zonr Changcf6af6a2010-10-12 12:38:51 +0800383 if (generateDepFile() > 0)
384 return false;
Stephen Hinesc632be22011-09-23 15:53:16 -0700385 if (SuppressAllWarnings) {
386 getDiagnostics().setSuppressAllDiagnostics(false);
387 }
Zonr Changcf6af6a2010-10-12 12:38:51 +0800388
389 DepFileIter++;
390 }
391
Zonr Change86245a2010-10-12 21:42:13 +0800392 if (!checkODR(InputFile))
Zonr Chang641558f2010-10-12 21:07:06 +0800393 return false;
394
Zonr Changcf6af6a2010-10-12 12:38:51 +0800395 IOFileIter++;
396 }
397
398 return true;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800399}
400
Zonr Chang641558f2010-10-12 21:07:06 +0800401void SlangRS::reset() {
402 delete mRSContext;
403 mRSContext = NULL;
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800404 mGeneratedFileNames.clear();
Zonr Chang641558f2010-10-12 21:07:06 +0800405 Slang::reset();
406 return;
407}
408
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800409SlangRS::~SlangRS() {
410 delete mRSContext;
Zonr Chang641558f2010-10-12 21:07:06 +0800411 for (ReflectedDefinitionListTy::iterator I = ReflectedDefinitions.begin(),
412 E = ReflectedDefinitions.end();
413 I != E;
414 I++) {
415 delete I->getValue().first;
416 }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800417 return;
418}
Stephen Hinese639eb52010-11-08 19:27:20 -0800419
420} // namespace slang