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