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