slang_rs.cpp revision 7ac9d0de26d325071ad3f26f8d34514efca2d3d6
1/*
2 * Copyright 2010-2012, 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
17#include "slang_rs.h"
18
19#include <cstring>
20#include <list>
21#include <sstream>
22#include <string>
23#include <utility>
24#include <vector>
25
26#include "clang/Basic/SourceLocation.h"
27
28#include "clang/Frontend/FrontendDiagnostic.h"
29
30#include "clang/Sema/SemaDiagnostic.h"
31
32#include "llvm/Support/Path.h"
33
34#include "os_sep.h"
35#include "rs_cc_options.h"
36#include "slang_rs_backend.h"
37#include "slang_rs_context.h"
38#include "slang_rs_export_type.h"
39
40#include "slang_rs_reflection.h"
41#include "slang_rs_reflection_cpp.h"
42
43namespace slang {
44
45#define FS_SUFFIX  "fs"
46
47#define RS_HEADER_SUFFIX  "rsh"
48
49/* RS_HEADER_ENTRY(name) */
50#define ENUM_RS_HEADER()  \
51  RS_HEADER_ENTRY(rs_allocation) \
52  RS_HEADER_ENTRY(rs_atomic) \
53  RS_HEADER_ENTRY(rs_cl) \
54  RS_HEADER_ENTRY(rs_core) \
55  RS_HEADER_ENTRY(rs_core_math) \
56  RS_HEADER_ENTRY(rs_debug) \
57  RS_HEADER_ENTRY(rs_element) \
58  RS_HEADER_ENTRY(rs_graphics) \
59  RS_HEADER_ENTRY(rs_math) \
60  RS_HEADER_ENTRY(rs_mesh) \
61  RS_HEADER_ENTRY(rs_matrix) \
62  RS_HEADER_ENTRY(rs_object) \
63  RS_HEADER_ENTRY(rs_program) \
64  RS_HEADER_ENTRY(rs_quaternion) \
65  RS_HEADER_ENTRY(rs_sampler) \
66  RS_HEADER_ENTRY(rs_time) \
67  RS_HEADER_ENTRY(rs_types) \
68
69// Returns true if \p Filename ends in ".fs".
70bool SlangRS::isFilterscript(const char *Filename) {
71  const char *c = strrchr(Filename, '.');
72  if (c && !strncmp(FS_SUFFIX, c + 1, strlen(FS_SUFFIX) + 1)) {
73    return true;
74  } else {
75    return false;
76  }
77}
78
79bool SlangRS::generateJavaBitcodeAccessor(const std::string &OutputPathBase,
80                                          const std::string &PackageName,
81                                          const std::string *LicenseNote) {
82  RSSlangReflectUtils::BitCodeAccessorContext BCAccessorContext;
83
84  BCAccessorContext.rsFileName = getInputFileName().c_str();
85  BCAccessorContext.bc32FileName = getOutput32FileName().c_str();
86  BCAccessorContext.bc64FileName = getOutputFileName().c_str();
87  BCAccessorContext.reflectPath = OutputPathBase.c_str();
88  BCAccessorContext.packageName = PackageName.c_str();
89  BCAccessorContext.licenseNote = LicenseNote;
90  BCAccessorContext.bcStorage = BCST_JAVA_CODE;   // Must be BCST_JAVA_CODE
91  BCAccessorContext.verbose = false;
92
93  return RSSlangReflectUtils::GenerateJavaBitCodeAccessor(BCAccessorContext);
94}
95
96bool SlangRS::checkODR(const char *CurInputFile) {
97  for (RSContext::ExportableList::iterator I = mRSContext->exportable_begin(),
98          E = mRSContext->exportable_end();
99       I != E;
100       I++) {
101    RSExportable *RSE = *I;
102    if (RSE->getKind() != RSExportable::EX_TYPE)
103      continue;
104
105    RSExportType *ET = static_cast<RSExportType *>(RSE);
106    if (ET->getClass() != RSExportType::ExportClassRecord)
107      continue;
108
109    RSExportRecordType *ERT = static_cast<RSExportRecordType *>(ET);
110
111    // Artificial record types (create by us not by user in the source) always
112    // conforms the ODR.
113    if (ERT->isArtificial())
114      continue;
115
116    // Key to lookup ERT in ReflectedDefinitions
117    llvm::StringRef RDKey(ERT->getName());
118    ReflectedDefinitionListTy::const_iterator RD =
119        ReflectedDefinitions.find(RDKey);
120
121    if (RD != ReflectedDefinitions.end()) {
122      const RSExportRecordType *Reflected = RD->getValue().first;
123      // There's a record (struct) with the same name reflected before. Enforce
124      // ODR checking - the Reflected must hold *exactly* the same "definition"
125      // as the one defined previously. We say two record types A and B have the
126      // same definition iff:
127      //
128      //  struct A {              struct B {
129      //    Type(a1) a1,            Type(b1) b1,
130      //    Type(a2) a2,            Type(b1) b2,
131      //    ...                     ...
132      //    Type(aN) aN             Type(b3) b3,
133      //  };                      }
134      //  Cond. #1. They have same number of fields, i.e., N = M;
135      //  Cond. #2. for (i := 1 to N)
136      //              Type(ai) = Type(bi) must hold;
137      //  Cond. #3. for (i := 1 to N)
138      //              Name(ai) = Name(bi) must hold;
139      //
140      // where,
141      //  Type(F) = the type of field F and
142      //  Name(F) = the field name.
143
144      bool PassODR = false;
145      // Cond. #1 and Cond. #2
146      if (Reflected->equals(ERT)) {
147        // Cond #3.
148        RSExportRecordType::const_field_iterator AI = Reflected->fields_begin(),
149                                                 BI = ERT->fields_begin();
150
151        for (unsigned i = 0, e = Reflected->getFields().size(); i != e; i++) {
152          if ((*AI)->getName() != (*BI)->getName())
153            break;
154          AI++;
155          BI++;
156        }
157        PassODR = (AI == (Reflected->fields_end()));
158      }
159
160      if (!PassODR) {
161        getDiagnostics().Report(mDiagErrorODR) << Reflected->getName()
162                                               << getInputFileName()
163                                               << RD->getValue().second;
164        return false;
165      }
166    } else {
167      llvm::StringMapEntry<ReflectedDefinitionTy> *ME =
168          llvm::StringMapEntry<ReflectedDefinitionTy>::Create(RDKey);
169      ME->setValue(std::make_pair(ERT, CurInputFile));
170
171      if (!ReflectedDefinitions.insert(ME))
172        delete ME;
173
174      // Take the ownership of ERT such that it won't be freed in ~RSContext().
175      ERT->keep();
176    }
177  }
178  return true;
179}
180
181void SlangRS::initDiagnostic() {
182  clang::DiagnosticsEngine &DiagEngine = getDiagnostics();
183
184  if (DiagEngine.setSeverityForGroup("implicit-function-declaration",
185                                     clang::diag::Severity::Error))
186    DiagEngine.Report(clang::diag::warn_unknown_warning_option)
187      << "implicit-function-declaration";
188
189  DiagEngine.setSeverity(
190    clang::diag::ext_typecheck_convert_discards_qualifiers,
191    clang::diag::Severity::Error,
192    clang::SourceLocation());
193
194  mDiagErrorInvalidOutputDepParameter =
195    DiagEngine.getCustomDiagID(
196      clang::DiagnosticsEngine::Error,
197      "invalid parameter for output dependencies files.");
198
199  mDiagErrorODR =
200    DiagEngine.getCustomDiagID(
201      clang::DiagnosticsEngine::Error,
202      "type '%0' in different translation unit (%1 v.s. %2) "
203      "has incompatible type definition");
204
205  mDiagErrorTargetAPIRange =
206    DiagEngine.getCustomDiagID(
207      clang::DiagnosticsEngine::Error,
208      "target API level '%0' is out of range ('%1' - '%2')");
209}
210
211void SlangRS::initPreprocessor() {
212  clang::Preprocessor &PP = getPreprocessor();
213
214  std::stringstream RSH;
215  RSH << PP.getPredefines();
216  RSH << "#define RS_VERSION " << mTargetAPI << "\n";
217  RSH << "#include \"rs_core." RS_HEADER_SUFFIX "\"\n";
218  PP.setPredefines(RSH.str());
219}
220
221void SlangRS::initASTContext() {
222  mRSContext = new RSContext(getPreprocessor(),
223                             getASTContext(),
224                             getTargetInfo(),
225                             &mPragmas,
226                             mTargetAPI,
227                             mVerbose);
228}
229
230clang::ASTConsumer
231*SlangRS::createBackend(const clang::CodeGenOptions& CodeGenOpts,
232                        llvm::raw_ostream *OS,
233                        Slang::OutputType OT) {
234    return new RSBackend(mRSContext,
235                         &getDiagnostics(),
236                         CodeGenOpts,
237                         getTargetOptions(),
238                         &mPragmas,
239                         OS,
240                         OT,
241                         getSourceManager(),
242                         mAllowRSPrefix,
243                         mIsFilterscript);
244}
245
246bool SlangRS::IsRSHeaderFile(const char *File) {
247#define RS_HEADER_ENTRY(name)  \
248  if (::strcmp(File, #name "." RS_HEADER_SUFFIX) == 0)  \
249    return true;
250ENUM_RS_HEADER()
251#undef RS_HEADER_ENTRY
252  return false;
253}
254
255bool SlangRS::IsLocInRSHeaderFile(const clang::SourceLocation &Loc,
256                                  const clang::SourceManager &SourceMgr) {
257  clang::FullSourceLoc FSL(Loc, SourceMgr);
258  clang::PresumedLoc PLoc = SourceMgr.getPresumedLoc(FSL);
259
260  const char *Filename = PLoc.getFilename();
261  if (!Filename) {
262    return false;
263  } else {
264    return IsRSHeaderFile(llvm::sys::path::filename(Filename).data());
265  }
266}
267
268SlangRS::SlangRS()
269  : Slang(), mRSContext(NULL), mAllowRSPrefix(false), mTargetAPI(0),
270    mVerbose(false), mIsFilterscript(false) {
271}
272
273bool SlangRS::compile(
274    const std::list<std::pair<const char*, const char*> > &IOFiles64,
275    const std::list<std::pair<const char*, const char*> > &IOFiles32,
276    const std::list<std::pair<const char*, const char*> > &DepFiles,
277    const RSCCOptions &Opts) {
278  if (IOFiles32.empty())
279    return true;
280
281  if (Opts.mEmitDependency && (DepFiles.size() != IOFiles32.size())) {
282    getDiagnostics().Report(mDiagErrorInvalidOutputDepParameter);
283    return false;
284  }
285
286  if (Opts.mEmit3264 && (IOFiles64.size() != IOFiles32.size())) {
287    slangAssert(false && "Should have equal number of 32/64-bit files");
288    return false;
289  }
290
291  std::string RealPackageName;
292
293  const char *InputFile, *Output64File, *Output32File, *BCOutputFile,
294             *DepOutputFile;
295  std::list<std::pair<const char*, const char*> >::const_iterator
296      IOFile64Iter = IOFiles64.begin(),
297      IOFile32Iter = IOFiles32.begin(),
298      DepFileIter = DepFiles.begin();
299
300  setIncludePaths(Opts.mIncludePaths);
301  setOutputType(Opts.mOutputType);
302  if (Opts.mEmitDependency) {
303    setAdditionalDepTargets(Opts.mAdditionalDepTargets);
304  }
305
306  setDebugMetadataEmission(Opts.mDebugEmission);
307
308  setOptimizationLevel(Opts.mOptimizationLevel);
309
310  mAllowRSPrefix = Opts.mAllowRSPrefix;
311
312  mTargetAPI = Opts.mTargetAPI;
313  if (mTargetAPI != SLANG_DEVELOPMENT_TARGET_API &&
314      (mTargetAPI < SLANG_MINIMUM_TARGET_API ||
315       mTargetAPI > SLANG_MAXIMUM_TARGET_API)) {
316    getDiagnostics().Report(mDiagErrorTargetAPIRange) << mTargetAPI
317        << SLANG_MINIMUM_TARGET_API << SLANG_MAXIMUM_TARGET_API;
318    return false;
319  }
320
321  mVerbose = Opts.mVerbose;
322
323  // Skip generation of warnings a second time if we are doing more than just
324  // a single pass over the input file.
325  bool SuppressAllWarnings = (Opts.mOutputType != Slang::OT_Dependency);
326
327  for (unsigned i = 0, e = IOFiles32.size(); i != e; i++) {
328    InputFile = IOFile64Iter->first;
329    Output64File = IOFile64Iter->second;
330    Output32File = IOFile32Iter->second;
331
332    reset();
333
334    if (!setInputSource(InputFile))
335      return false;
336
337    if (!setOutput(Output64File))
338      return false;
339
340    setOutput32(Output32File);
341
342    mIsFilterscript = isFilterscript(InputFile);
343
344    if (Slang::compile() > 0)
345      return false;
346
347    if (!Opts.mJavaReflectionPackageName.empty()) {
348      mRSContext->setReflectJavaPackageName(Opts.mJavaReflectionPackageName);
349    }
350    const std::string &RealPackageName =
351        mRSContext->getReflectJavaPackageName();
352
353    bool doReflection = true;
354    if (Opts.mEmit3264 && (Opts.mBitWidth == 32)) {
355      // Skip reflection on the 32-bit path if we are going to emit it on the
356      // 64-bit path.
357      doReflection = false;
358    }
359    if (Opts.mOutputType != Slang::OT_Dependency && doReflection) {
360
361      if (Opts.mBitcodeStorage == BCST_CPP_CODE) {
362          RSReflectionCpp R(mRSContext, Opts.mJavaReflectionPathBase,
363                            getInputFileName(), getOutputFileName());
364          if (!R.reflect()) {
365            return false;
366          }
367      } else {
368        if (!Opts.mRSPackageName.empty()) {
369          mRSContext->setRSPackageName(Opts.mRSPackageName);
370        }
371
372        RSReflectionJava R(mRSContext, &mGeneratedFileNames,
373                           Opts.mJavaReflectionPathBase, getInputFileName(),
374                           getOutputFileName(),
375                           Opts.mBitcodeStorage == BCST_JAVA_CODE);
376        if (!R.reflect()) {
377          // TODO Is this needed or will the error message have been printed
378          // already? and why not for the C++ case?
379          fprintf(stderr, "RSContext::reflectToJava : failed to do reflection "
380                          "(%s)\n",
381                  R.getLastError());
382          return false;
383        }
384
385        for (std::vector<std::string>::const_iterator
386                 I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end();
387             I != E;
388             I++) {
389          std::string ReflectedName = RSSlangReflectUtils::ComputePackagedPath(
390              Opts.mJavaReflectionPathBase.c_str(),
391              (RealPackageName + OS_PATH_SEPARATOR_STR + *I).c_str());
392          appendGeneratedFileName(ReflectedName + ".java");
393        }
394
395        if ((Opts.mOutputType == Slang::OT_Bitcode) &&
396            (Opts.mBitcodeStorage == BCST_JAVA_CODE) &&
397            !generateJavaBitcodeAccessor(Opts.mJavaReflectionPathBase,
398                                         RealPackageName.c_str(),
399                                         mRSContext->getLicenseNote())) {
400          return false;
401        }
402      }
403    }
404
405    if (Opts.mEmitDependency) {
406      BCOutputFile = DepFileIter->first;
407      DepOutputFile = DepFileIter->second;
408
409      setDepTargetBC(BCOutputFile);
410
411      if (!setDepOutput(DepOutputFile))
412        return false;
413
414      if (SuppressAllWarnings) {
415        getDiagnostics().setSuppressAllDiagnostics(true);
416      }
417      if (generateDepFile() > 0)
418        return false;
419      if (SuppressAllWarnings) {
420        getDiagnostics().setSuppressAllDiagnostics(false);
421      }
422
423      DepFileIter++;
424    }
425
426    if (!checkODR(InputFile))
427      return false;
428
429    IOFile64Iter++;
430    IOFile32Iter++;
431  }
432
433  return true;
434}
435
436void SlangRS::reset() {
437  delete mRSContext;
438  mRSContext = NULL;
439  mGeneratedFileNames.clear();
440  Slang::reset();
441}
442
443SlangRS::~SlangRS() {
444  delete mRSContext;
445  for (ReflectedDefinitionListTy::iterator I = ReflectedDefinitions.begin(),
446          E = ReflectedDefinitions.end();
447       I != E;
448       I++) {
449    delete I->getValue().first;
450  }
451}
452
453}  // namespace slang
454