slang_rs.cpp revision 2e35b136cc2434080fcd682d2f95e53a87675dd4
1/*
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
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 "slang_rs_backend.h"
36#include "slang_rs_context.h"
37#include "slang_rs_export_type.h"
38
39namespace slang {
40
41#define RS_HEADER_SUFFIX  "rsh"
42
43/* RS_HEADER_ENTRY(name, default_included) */
44#define ENUM_RS_HEADER()  \
45  RS_HEADER_ENTRY(rs_types,     1) \
46  RS_HEADER_ENTRY(rs_cl,        1) \
47  RS_HEADER_ENTRY(rs_core,      1) \
48  RS_HEADER_ENTRY(rs_math,      1) \
49  RS_HEADER_ENTRY(rs_time,      1) \
50  RS_HEADER_ENTRY(rs_graphics,  0)
51
52bool SlangRS::reflectToJava(const std::string &OutputPathBase,
53                            const std::string &OutputPackageName,
54                            std::string *RealPackageName) {
55  return mRSContext->reflectToJava(OutputPathBase,
56                                   OutputPackageName,
57                                   getInputFileName(),
58                                   getOutputFileName(),
59                                   RealPackageName);
60}
61
62bool SlangRS::generateBitcodeAccessor(const std::string &OutputPathBase,
63                                      const std::string &PackageName) {
64  RSSlangReflectUtils::BitCodeAccessorContext BCAccessorContext;
65
66  BCAccessorContext.rsFileName = getInputFileName().c_str();
67  BCAccessorContext.bcFileName = getOutputFileName().c_str();
68  BCAccessorContext.reflectPath = OutputPathBase.c_str();
69  BCAccessorContext.packageName = PackageName.c_str();
70  BCAccessorContext.bcStorage = BCST_JAVA_CODE;   // Must be BCST_JAVA_CODE
71
72  return RSSlangReflectUtils::GenerateBitCodeAccessor(BCAccessorContext);
73}
74
75bool SlangRS::checkODR(const char *CurInputFile) {
76  for (RSContext::ExportableList::iterator I = mRSContext->exportable_begin(),
77          E = mRSContext->exportable_end();
78       I != E;
79       I++) {
80    RSExportable *RSE = *I;
81    if (RSE->getKind() != RSExportable::EX_TYPE)
82      continue;
83
84    RSExportType *ET = static_cast<RSExportType *>(RSE);
85    if (ET->getClass() != RSExportType::ExportClassRecord)
86      continue;
87
88    RSExportRecordType *ERT = static_cast<RSExportRecordType *>(ET);
89
90    // Artificial record types (create by us not by user in the source) always
91    // conforms the ODR.
92    if (ERT->isArtificial())
93      continue;
94
95    // Key to lookup ERT in ReflectedDefinitions
96    llvm::StringRef RDKey(ERT->getName());
97    ReflectedDefinitionListTy::const_iterator RD =
98        ReflectedDefinitions.find(RDKey);
99
100    if (RD != ReflectedDefinitions.end()) {
101      const RSExportRecordType *Reflected = RD->getValue().first;
102      // There's a record (struct) with the same name reflected before. Enforce
103      // ODR checking - the Reflected must hold *exactly* the same "definition"
104      // as the one defined previously. We say two record types A and B have the
105      // same definition iff:
106      //
107      //  struct A {              struct B {
108      //    Type(a1) a1,            Type(b1) b1,
109      //    Type(a2) a2,            Type(b1) b2,
110      //    ...                     ...
111      //    Type(aN) aN             Type(b3) b3,
112      //  };                      }
113      //  Cond. #1. They have same number of fields, i.e., N = M;
114      //  Cond. #2. for (i := 1 to N)
115      //              Type(ai) = Type(bi) must hold;
116      //  Cond. #3. for (i := 1 to N)
117      //              Name(ai) = Name(bi) must hold;
118      //
119      // where,
120      //  Type(F) = the type of field F and
121      //  Name(F) = the field name.
122
123      bool PassODR = false;
124      // Cond. #1 and Cond. #2
125      if (Reflected->equals(ERT)) {
126        // Cond #3.
127        RSExportRecordType::const_field_iterator AI = Reflected->fields_begin(),
128                                                 BI = ERT->fields_begin();
129
130        for (unsigned i = 0, e = Reflected->getFields().size(); i != e; i++) {
131          if ((*AI)->getName() != (*BI)->getName())
132            break;
133          AI++;
134          BI++;
135        }
136        PassODR = (AI == (Reflected->fields_end()));
137      }
138
139      if (!PassODR) {
140        getDiagnostics().Report(mDiagErrorODR) << Reflected->getName()
141                                               << getInputFileName()
142                                               << RD->getValue().second;
143        return false;
144      }
145    } else {
146      llvm::StringMapEntry<ReflectedDefinitionTy> *ME =
147          llvm::StringMapEntry<ReflectedDefinitionTy>::Create(RDKey.begin(),
148                                                              RDKey.end());
149      ME->setValue(std::make_pair(ERT, CurInputFile));
150
151      if (!ReflectedDefinitions.insert(ME))
152        delete ME;
153
154      // Take the ownership of ERT such that it won't be freed in ~RSContext().
155      ERT->keep();
156    }
157  }
158  return true;
159}
160
161void SlangRS::initDiagnostic() {
162  clang::Diagnostic &Diag = getDiagnostics();
163  if (Diag.setDiagnosticGroupMapping("implicit-function-declaration",
164                                     clang::diag::MAP_ERROR))
165    Diag.Report(clang::diag::warn_unknown_warning_option)
166        << "implicit-function-declaration";
167
168  Diag.setDiagnosticMapping(
169      clang::diag::ext_typecheck_convert_discards_qualifiers,
170      clang::diag::MAP_ERROR,
171      clang::SourceLocation());
172
173  mDiagErrorInvalidOutputDepParameter =
174      Diag.getCustomDiagID(clang::Diagnostic::Error,
175                           "invalid parameter for output dependencies files.");
176
177  mDiagErrorODR =
178      Diag.getCustomDiagID(clang::Diagnostic::Error,
179                           "type '%0' in different translation unit (%1 v.s. "
180                           "%2) has incompatible type definition");
181
182  mDiagErrorTargetAPIRange = Diag.getCustomDiagID(clang::Diagnostic::Error,
183      "target API level '%0' is out of range ('%1' - '%2')");
184
185  return;
186}
187
188void SlangRS::initPreprocessor() {
189  clang::Preprocessor &PP = getPreprocessor();
190
191  std::stringstream RSH;
192  RSH << "#define RS_VERSION " << mTargetAPI << std::endl;
193#define RS_HEADER_ENTRY(name, default_included)  \
194  if (default_included) \
195    RSH << "#include \"" #name "." RS_HEADER_SUFFIX "\"" << std::endl;
196  ENUM_RS_HEADER()
197#undef RS_HEADER_ENTRY
198  PP.setPredefines(RSH.str());
199
200  return;
201}
202
203void SlangRS::initASTContext() {
204  mRSContext = new RSContext(getPreprocessor(),
205                             getASTContext(),
206                             getTargetInfo(),
207                             &mPragmas,
208                             &mGeneratedFileNames);
209  return;
210}
211
212clang::ASTConsumer
213*SlangRS::createBackend(const clang::CodeGenOptions& CodeGenOpts,
214                        llvm::raw_ostream *OS,
215                        Slang::OutputType OT) {
216    return new RSBackend(mRSContext,
217                         &getDiagnostics(),
218                         CodeGenOpts,
219                         getTargetOptions(),
220                         &mPragmas,
221                         OS,
222                         OT,
223                         getSourceManager(),
224                         mAllowRSPrefix,
225                         mTargetAPI);
226}
227
228bool SlangRS::IsRSHeaderFile(const char *File) {
229#define RS_HEADER_ENTRY(name, default_included)  \
230  if (::strcmp(File, #name "."RS_HEADER_SUFFIX) == 0)  \
231    return true;
232ENUM_RS_HEADER()
233#undef RS_HEADER_ENTRY
234  return false;
235}
236
237bool SlangRS::IsFunctionInRSHeaderFile(const clang::FunctionDecl *FD,
238                                       const clang::SourceManager &SourceMgr) {
239  clang::FullSourceLoc FSL(FD->getLocStart(), SourceMgr);
240  clang::PresumedLoc PLoc = SourceMgr.getPresumedLoc(FSL);
241
242  return IsRSHeaderFile(llvm::sys::path::filename(PLoc.getFilename()).data());
243}
244
245SlangRS::SlangRS() : Slang(), mRSContext(NULL), mAllowRSPrefix(false),
246    mTargetAPI(0) {
247  return;
248}
249
250bool SlangRS::compile(
251    const std::list<std::pair<const char*, const char*> > &IOFiles,
252    const std::list<std::pair<const char*, const char*> > &DepFiles,
253    const std::vector<std::string> &IncludePaths,
254    const std::vector<std::string> &AdditionalDepTargets,
255    Slang::OutputType OutputType, BitCodeStorageType BitcodeStorage,
256    bool AllowRSPrefix, bool OutputDep,
257    unsigned int TargetAPI,
258    const std::string &JavaReflectionPathBase,
259    const std::string &JavaReflectionPackageName) {
260  if (IOFiles.empty())
261    return true;
262
263  if (OutputDep && (DepFiles.size() != IOFiles.size())) {
264    getDiagnostics().Report(mDiagErrorInvalidOutputDepParameter);
265    return false;
266  }
267
268  std::string RealPackageName;
269
270  const char *InputFile, *OutputFile, *BCOutputFile, *DepOutputFile;
271  std::list<std::pair<const char*, const char*> >::const_iterator
272      IOFileIter = IOFiles.begin(), DepFileIter = DepFiles.begin();
273
274  setIncludePaths(IncludePaths);
275  setOutputType(OutputType);
276  if (OutputDep) {
277    setAdditionalDepTargets(AdditionalDepTargets);
278  }
279
280  mAllowRSPrefix = AllowRSPrefix;
281
282  mTargetAPI = TargetAPI;
283  if (mTargetAPI < RS_MINIMUM_TARGET_API ||
284      mTargetAPI > RS_MAXIMUM_TARGET_API) {
285    getDiagnostics().Report(mDiagErrorTargetAPIRange) << mTargetAPI
286                                                      << RS_MINIMUM_TARGET_API
287                                                      << RS_MAXIMUM_TARGET_API;
288    return false;
289  }
290
291  for (unsigned i = 0, e = IOFiles.size(); i != e; i++) {
292    InputFile = IOFileIter->first;
293    OutputFile = IOFileIter->second;
294
295    reset();
296
297    if (!setInputSource(InputFile))
298      return false;
299
300    if (!setOutput(OutputFile))
301      return false;
302
303    if (Slang::compile() > 0)
304      return false;
305
306    if (OutputType != Slang::OT_Dependency) {
307      if (!reflectToJava(JavaReflectionPathBase,
308                         JavaReflectionPackageName,
309                         &RealPackageName))
310        return false;
311
312      for (std::vector<std::string>::const_iterator
313               I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end();
314           I != E;
315           I++) {
316        std::string ReflectedName = RSSlangReflectUtils::ComputePackagedPath(
317            JavaReflectionPathBase.c_str(),
318            (RealPackageName + OS_PATH_SEPARATOR_STR + *I).c_str());
319        appendGeneratedFileName(ReflectedName + ".java");
320      }
321
322      if ((OutputType == Slang::OT_Bitcode) &&
323          (BitcodeStorage == BCST_JAVA_CODE) &&
324          !generateBitcodeAccessor(JavaReflectionPathBase,
325                                     RealPackageName.c_str()))
326          return false;
327    }
328
329    if (OutputDep) {
330      BCOutputFile = DepFileIter->first;
331      DepOutputFile = DepFileIter->second;
332
333      setDepTargetBC(BCOutputFile);
334
335      if (!setDepOutput(DepOutputFile))
336        return false;
337
338      if (generateDepFile() > 0)
339        return false;
340
341      DepFileIter++;
342    }
343
344    if (!checkODR(InputFile))
345      return false;
346
347    IOFileIter++;
348  }
349
350  return true;
351}
352
353void SlangRS::reset() {
354  delete mRSContext;
355  mRSContext = NULL;
356  mGeneratedFileNames.clear();
357  Slang::reset();
358  return;
359}
360
361SlangRS::~SlangRS() {
362  delete mRSContext;
363  for (ReflectedDefinitionListTy::iterator I = ReflectedDefinitions.begin(),
364          E = ReflectedDefinitions.end();
365       I != E;
366       I++) {
367    delete I->getValue().first;
368  }
369  return;
370}
371
372}  // namespace slang
373