Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 <stdio.h> |
| 18 | #include <cctype> |
| 19 | #include <cstdlib> |
| 20 | #include <fstream> |
| 21 | #include <functional> |
| 22 | #include <iostream> |
| 23 | #include <memory> |
| 24 | #include <sstream> |
| 25 | #include <strings.h> |
| 26 | |
| 27 | #include "Generator.h" |
| 28 | #include "Scanner.h" |
| 29 | #include "Specification.h" |
| 30 | #include "Utilities.h" |
| 31 | |
| 32 | using namespace std; |
| 33 | |
| 34 | // API level when RenderScript was added. |
| 35 | const int MIN_API_LEVEL = 9; |
| 36 | |
| 37 | const NumericalType TYPES[] = { |
Jean-Luc Brouillet | 6119da9 | 2015-04-10 09:36:15 -0700 | [diff] [blame^] | 38 | {"f16", "FLOAT_16", "half", "float", FLOATING_POINT, 11, 5}, |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 39 | {"f32", "FLOAT_32", "float", "float", FLOATING_POINT, 24, 8}, |
| 40 | {"f64", "FLOAT_64", "double", "double", FLOATING_POINT, 53, 11}, |
| 41 | {"i8", "SIGNED_8", "char", "byte", SIGNED_INTEGER, 7, 0}, |
| 42 | {"u8", "UNSIGNED_8", "uchar", "byte", UNSIGNED_INTEGER, 8, 0}, |
| 43 | {"i16", "SIGNED_16", "short", "short", SIGNED_INTEGER, 15, 0}, |
| 44 | {"u16", "UNSIGNED_16", "ushort", "short", UNSIGNED_INTEGER, 16, 0}, |
| 45 | {"i32", "SIGNED_32", "int", "int", SIGNED_INTEGER, 31, 0}, |
| 46 | {"u32", "UNSIGNED_32", "uint", "int", UNSIGNED_INTEGER, 32, 0}, |
| 47 | {"i64", "SIGNED_64", "long", "long", SIGNED_INTEGER, 63, 0}, |
| 48 | {"u64", "UNSIGNED_64", "ulong", "long", UNSIGNED_INTEGER, 64, 0}, |
| 49 | }; |
| 50 | |
| 51 | const int NUM_TYPES = sizeof(TYPES) / sizeof(TYPES[0]); |
| 52 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 53 | // The singleton of the collected information of all the spec files. |
| 54 | SystemSpecification systemSpecification; |
| 55 | |
| 56 | // Returns the index in TYPES for the provided cType |
| 57 | static int findCType(const string& cType) { |
| 58 | for (int i = 0; i < NUM_TYPES; i++) { |
| 59 | if (cType == TYPES[i].cType) { |
| 60 | return i; |
| 61 | } |
| 62 | } |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | /* Converts a string like "u8, u16" to a vector of "ushort", "uint". |
| 67 | * For non-numerical types, we don't need to convert the abbreviation. |
| 68 | */ |
| 69 | static vector<string> convertToTypeVector(const string& input) { |
| 70 | // First convert the string to an array of strings. |
| 71 | vector<string> entries; |
| 72 | stringstream stream(input); |
| 73 | string entry; |
| 74 | while (getline(stream, entry, ',')) { |
| 75 | trimSpaces(&entry); |
| 76 | entries.push_back(entry); |
| 77 | } |
| 78 | |
| 79 | /* Second, we look for present numerical types. We do it this way |
| 80 | * so the order of numerical types is always the same, no matter |
| 81 | * how specified in the spec file. |
| 82 | */ |
| 83 | vector<string> result; |
| 84 | for (auto t : TYPES) { |
| 85 | for (auto i = entries.begin(); i != entries.end(); ++i) { |
| 86 | if (*i == t.specType) { |
| 87 | result.push_back(t.cType); |
| 88 | entries.erase(i); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Add the remaining; they are not numerical types. |
| 95 | for (auto s : entries) { |
| 96 | result.push_back(s); |
| 97 | } |
| 98 | |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | void ParameterDefinition::parseParameterDefinition(const string& type, const string& name, |
| 103 | const string& testOption, int lineNumber, |
| 104 | bool isReturn, Scanner* scanner) { |
| 105 | rsType = type; |
| 106 | specName = name; |
| 107 | |
| 108 | // Determine if this is an output. |
| 109 | isOutParameter = isReturn || charRemoved('*', &rsType); |
| 110 | |
Jean-Luc Brouillet | 66fea24 | 2015-04-09 16:47:59 -0700 | [diff] [blame] | 111 | rsBaseType = rsType; |
| 112 | mVectorSize = "1"; |
| 113 | /* If it's a vector type, we need to split the base type from the size. |
| 114 | * We know that's it's a vector type if the last character is a digit and |
| 115 | * the rest is an actual base type. We used to only verify the first part, |
| 116 | * which created a problem with rs_matrix2x2. |
| 117 | */ |
| 118 | const int last = rsType.size() - 1; |
| 119 | const char lastChar = rsType[last]; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 120 | if (lastChar >= '0' && lastChar <= '9') { |
Jean-Luc Brouillet | 66fea24 | 2015-04-09 16:47:59 -0700 | [diff] [blame] | 121 | const string trimmed = rsType.substr(0, last); |
| 122 | int i = findCType(trimmed); |
| 123 | if (i >= 0) { |
| 124 | rsBaseType = trimmed; |
| 125 | mVectorSize = lastChar; |
| 126 | } |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 127 | } |
Jean-Luc Brouillet | 66fea24 | 2015-04-09 16:47:59 -0700 | [diff] [blame] | 128 | typeIndex = findCType(rsBaseType); |
| 129 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 130 | if (mVectorSize == "3") { |
| 131 | vectorWidth = "4"; |
| 132 | } else { |
| 133 | vectorWidth = mVectorSize; |
| 134 | } |
| 135 | |
| 136 | /* Create variable names to be used in the java and .rs files. Because x and |
| 137 | * y are reserved in .rs files, we prefix variable names with "in" or "out". |
| 138 | */ |
| 139 | if (isOutParameter) { |
| 140 | variableName = "out"; |
| 141 | if (!specName.empty()) { |
| 142 | variableName += capitalize(specName); |
| 143 | } else if (!isReturn) { |
| 144 | scanner->error(lineNumber) << "Should have a name.\n"; |
| 145 | } |
| 146 | } else { |
| 147 | variableName = "in"; |
| 148 | if (specName.empty()) { |
| 149 | scanner->error(lineNumber) << "Should have a name.\n"; |
| 150 | } |
| 151 | variableName += capitalize(specName); |
| 152 | } |
| 153 | rsAllocName = "gAlloc" + capitalize(variableName); |
| 154 | javaAllocName = variableName; |
| 155 | javaArrayName = "array" + capitalize(javaAllocName); |
| 156 | |
| 157 | // Process the option. |
| 158 | undefinedIfOutIsNan = false; |
| 159 | compatibleTypeIndex = -1; |
| 160 | if (!testOption.empty()) { |
| 161 | if (testOption.compare(0, 6, "range(") == 0) { |
| 162 | size_t pComma = testOption.find(','); |
| 163 | size_t pParen = testOption.find(')'); |
| 164 | if (pComma == string::npos || pParen == string::npos) { |
| 165 | scanner->error(lineNumber) << "Incorrect range " << testOption << "\n"; |
| 166 | } else { |
| 167 | minValue = testOption.substr(6, pComma - 6); |
| 168 | maxValue = testOption.substr(pComma + 1, pParen - pComma - 1); |
| 169 | } |
| 170 | } else if (testOption.compare(0, 6, "above(") == 0) { |
| 171 | size_t pParen = testOption.find(')'); |
| 172 | if (pParen == string::npos) { |
| 173 | scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n"; |
| 174 | } else { |
| 175 | smallerParameter = testOption.substr(6, pParen - 6); |
| 176 | } |
| 177 | } else if (testOption.compare(0, 11, "compatible(") == 0) { |
| 178 | size_t pParen = testOption.find(')'); |
| 179 | if (pParen == string::npos) { |
| 180 | scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n"; |
| 181 | } else { |
| 182 | compatibleTypeIndex = findCType(testOption.substr(11, pParen - 11)); |
| 183 | } |
| 184 | } else if (testOption.compare(0, 11, "conditional") == 0) { |
| 185 | undefinedIfOutIsNan = true; |
| 186 | } else { |
| 187 | scanner->error(lineNumber) << "Unrecognized testOption " << testOption << "\n"; |
| 188 | } |
| 189 | } |
| 190 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 191 | isFloatType = false; |
| 192 | if (typeIndex >= 0) { |
| 193 | javaBaseType = TYPES[typeIndex].javaType; |
| 194 | specType = TYPES[typeIndex].specType; |
| 195 | isFloatType = TYPES[typeIndex].exponentBits > 0; |
| 196 | } |
| 197 | if (!minValue.empty()) { |
| 198 | if (typeIndex < 0 || TYPES[typeIndex].kind != FLOATING_POINT) { |
| 199 | scanner->error(lineNumber) << "range(,) is only supported for floating point\n"; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 204 | bool VersionInfo::scan(Scanner* scanner, int maxApiLevel) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 205 | if (scanner->findOptionalTag("version:")) { |
| 206 | const string s = scanner->getValue(); |
| 207 | sscanf(s.c_str(), "%i %i", &minVersion, &maxVersion); |
| 208 | if (minVersion && minVersion < MIN_API_LEVEL) { |
| 209 | scanner->error() << "Minimum version must >= 9\n"; |
| 210 | } |
| 211 | if (minVersion == MIN_API_LEVEL) { |
| 212 | minVersion = 0; |
| 213 | } |
| 214 | if (maxVersion && maxVersion < MIN_API_LEVEL) { |
| 215 | scanner->error() << "Maximum version must >= 9\n"; |
| 216 | } |
| 217 | } |
| 218 | if (scanner->findOptionalTag("size:")) { |
| 219 | sscanf(scanner->getValue().c_str(), "%i", &intSize); |
| 220 | } |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 221 | if (maxVersion > maxApiLevel) { |
| 222 | maxVersion = maxApiLevel; |
| 223 | } |
| 224 | return minVersion == 0 || minVersion <= maxApiLevel; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Jean-Luc Brouillet | 67923a9 | 2015-05-12 15:38:27 -0700 | [diff] [blame] | 227 | Definition::Definition(const std::string& name) |
Jean-Luc Brouillet | 36e2be5 | 2015-04-30 14:41:24 -0700 | [diff] [blame] | 228 | : mName(name), mDeprecatedApiLevel(0), mHidden(false), mFinalVersion(-1) { |
Jean-Luc Brouillet | 67923a9 | 2015-05-12 15:38:27 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void Definition::updateFinalVersion(const VersionInfo& info) { |
| 232 | /* We set it if: |
| 233 | * - We have never set mFinalVersion before, or |
| 234 | * - The max version is 0, which means we have not expired this API, or |
| 235 | * - We have a max that's later than what we currently have. |
| 236 | */ |
| 237 | if (mFinalVersion < 0 || info.maxVersion == 0 || |
| 238 | (mFinalVersion > 0 && info.maxVersion > mFinalVersion)) { |
| 239 | mFinalVersion = info.maxVersion; |
| 240 | } |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 243 | void Definition::scanDocumentationTags(Scanner* scanner, bool firstOccurence, |
| 244 | const SpecFile* specFile) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 245 | if (scanner->findOptionalTag("hidden:")) { |
| 246 | scanner->checkNoValue(); |
| 247 | mHidden = true; |
| 248 | } |
Jean-Luc Brouillet | 4a73004 | 2015-04-02 16:15:25 -0700 | [diff] [blame] | 249 | if (scanner->findOptionalTag("deprecated:")) { |
Jean-Luc Brouillet | 36e2be5 | 2015-04-30 14:41:24 -0700 | [diff] [blame] | 250 | string value = scanner->getValue(); |
| 251 | size_t pComma = value.find(", "); |
| 252 | if (pComma != string::npos) { |
| 253 | mDeprecatedMessage = value.substr(pComma + 2); |
| 254 | value.erase(pComma); |
| 255 | } |
| 256 | sscanf(value.c_str(), "%i", &mDeprecatedApiLevel); |
| 257 | if (mDeprecatedApiLevel <= 0) { |
| 258 | scanner->error() << "deprecated entries should have a level > 0\n"; |
| 259 | } |
Jean-Luc Brouillet | 4a73004 | 2015-04-02 16:15:25 -0700 | [diff] [blame] | 260 | } |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 261 | if (firstOccurence) { |
| 262 | if (scanner->findTag("summary:")) { |
| 263 | mSummary = scanner->getValue(); |
| 264 | } |
| 265 | if (scanner->findTag("description:")) { |
| 266 | scanner->checkNoValue(); |
| 267 | while (scanner->findOptionalTag("")) { |
| 268 | mDescription.push_back(scanner->getValue()); |
| 269 | } |
| 270 | } |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 271 | mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + mName; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 272 | } else if (scanner->findOptionalTag("summary:")) { |
| 273 | scanner->error() << "Only the first specification should have a summary.\n"; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | Constant::~Constant() { |
| 278 | for (auto i : mSpecifications) { |
| 279 | delete i; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | Type::~Type() { |
| 284 | for (auto i : mSpecifications) { |
| 285 | delete i; |
| 286 | } |
| 287 | } |
| 288 | |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 289 | Function::Function(const string& name) : Definition(name) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 290 | mCapitalizedName = capitalize(mName); |
| 291 | } |
| 292 | |
| 293 | Function::~Function() { |
| 294 | for (auto i : mSpecifications) { |
| 295 | delete i; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | bool Function::someParametersAreDocumented() const { |
| 300 | for (auto p : mParameters) { |
| 301 | if (!p->documentation.empty()) { |
| 302 | return true; |
| 303 | } |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | void Function::addParameter(ParameterEntry* entry, Scanner* scanner) { |
| 309 | for (auto i : mParameters) { |
| 310 | if (i->name == entry->name) { |
| 311 | // It's a duplicate. |
| 312 | if (!entry->documentation.empty()) { |
| 313 | scanner->error(entry->lineNumber) |
| 314 | << "Only the first occurence of an arg should have the " |
| 315 | "documentation.\n"; |
| 316 | } |
| 317 | return; |
| 318 | } |
| 319 | } |
| 320 | mParameters.push_back(entry); |
| 321 | } |
| 322 | |
| 323 | void Function::addReturn(ParameterEntry* entry, Scanner* scanner) { |
| 324 | if (entry->documentation.empty()) { |
| 325 | return; |
| 326 | } |
| 327 | if (!mReturnDocumentation.empty()) { |
| 328 | scanner->error() << "ret: should be documented only for the first variant\n"; |
| 329 | } |
| 330 | mReturnDocumentation = entry->documentation; |
| 331 | } |
| 332 | |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 333 | void ConstantSpecification::scanConstantSpecification(Scanner* scanner, SpecFile* specFile, |
| 334 | int maxApiLevel) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 335 | string name = scanner->getValue(); |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 336 | VersionInfo info; |
| 337 | if (!info.scan(scanner, maxApiLevel)) { |
| 338 | cout << "Skipping some " << name << " definitions.\n"; |
| 339 | scanner->skipUntilTag("end:"); |
| 340 | return; |
| 341 | } |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 342 | |
| 343 | bool created = false; |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 344 | Constant* constant = systemSpecification.findOrCreateConstant(name, &created); |
| 345 | ConstantSpecification* spec = new ConstantSpecification(constant); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 346 | constant->addSpecification(spec); |
Jean-Luc Brouillet | 67923a9 | 2015-05-12 15:38:27 -0700 | [diff] [blame] | 347 | constant->updateFinalVersion(info); |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 348 | specFile->addConstantSpecification(spec, created); |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 349 | spec->mVersionInfo = info; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 350 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 351 | if (scanner->findTag("value:")) { |
| 352 | spec->mValue = scanner->getValue(); |
| 353 | } |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 354 | constant->scanDocumentationTags(scanner, created, specFile); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 355 | |
| 356 | scanner->findTag("end:"); |
| 357 | } |
| 358 | |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 359 | void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile, |
| 360 | int maxApiLevel) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 361 | string name = scanner->getValue(); |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 362 | VersionInfo info; |
| 363 | if (!info.scan(scanner, maxApiLevel)) { |
| 364 | cout << "Skipping some " << name << " definitions.\n"; |
| 365 | scanner->skipUntilTag("end:"); |
| 366 | return; |
| 367 | } |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 368 | |
| 369 | bool created = false; |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 370 | Type* type = systemSpecification.findOrCreateType(name, &created); |
| 371 | TypeSpecification* spec = new TypeSpecification(type); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 372 | type->addSpecification(spec); |
Jean-Luc Brouillet | 67923a9 | 2015-05-12 15:38:27 -0700 | [diff] [blame] | 373 | type->updateFinalVersion(info); |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 374 | specFile->addTypeSpecification(spec, created); |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 375 | spec->mVersionInfo = info; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 376 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 377 | if (scanner->findOptionalTag("simple:")) { |
| 378 | spec->mKind = SIMPLE; |
| 379 | spec->mSimpleType = scanner->getValue(); |
| 380 | } |
| 381 | if (scanner->findOptionalTag("struct:")) { |
| 382 | spec->mKind = STRUCT; |
| 383 | spec->mStructName = scanner->getValue(); |
| 384 | while (scanner->findOptionalTag("field:")) { |
| 385 | string s = scanner->getValue(); |
| 386 | string comment; |
| 387 | scanner->parseDocumentation(&s, &comment); |
| 388 | spec->mFields.push_back(s); |
| 389 | spec->mFieldComments.push_back(comment); |
| 390 | } |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 391 | } |
| 392 | if (scanner->findOptionalTag("enum:")) { |
| 393 | spec->mKind = ENUM; |
| 394 | spec->mEnumName = scanner->getValue(); |
| 395 | while (scanner->findOptionalTag("value:")) { |
| 396 | string s = scanner->getValue(); |
| 397 | string comment; |
| 398 | scanner->parseDocumentation(&s, &comment); |
| 399 | spec->mValues.push_back(s); |
| 400 | spec->mValueComments.push_back(comment); |
| 401 | } |
| 402 | } |
Jean-Luc Brouillet | 36e2be5 | 2015-04-30 14:41:24 -0700 | [diff] [blame] | 403 | if (scanner->findOptionalTag("attrib:")) { |
| 404 | spec->mAttribute = scanner->getValue(); |
| 405 | } |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 406 | type->scanDocumentationTags(scanner, created, specFile); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 407 | |
| 408 | scanner->findTag("end:"); |
| 409 | } |
| 410 | |
| 411 | FunctionSpecification::~FunctionSpecification() { |
| 412 | for (auto i : mParameters) { |
| 413 | delete i; |
| 414 | } |
| 415 | delete mReturn; |
| 416 | for (auto i : mPermutations) { |
| 417 | delete i; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | string FunctionSpecification::expandString(string s, |
| 422 | int replacementIndexes[MAX_REPLACEABLES]) const { |
| 423 | if (mReplaceables.size() > 0) { |
| 424 | s = stringReplace(s, "#1", mReplaceables[0][replacementIndexes[0]]); |
| 425 | } |
| 426 | if (mReplaceables.size() > 1) { |
| 427 | s = stringReplace(s, "#2", mReplaceables[1][replacementIndexes[1]]); |
| 428 | } |
| 429 | if (mReplaceables.size() > 2) { |
| 430 | s = stringReplace(s, "#3", mReplaceables[2][replacementIndexes[2]]); |
| 431 | } |
| 432 | if (mReplaceables.size() > 3) { |
| 433 | s = stringReplace(s, "#4", mReplaceables[3][replacementIndexes[3]]); |
| 434 | } |
| 435 | return s; |
| 436 | } |
| 437 | |
| 438 | void FunctionSpecification::expandStringVector(const vector<string>& in, |
| 439 | int replacementIndexes[MAX_REPLACEABLES], |
| 440 | vector<string>* out) const { |
| 441 | out->clear(); |
| 442 | for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) { |
| 443 | out->push_back(expandString(*iter, replacementIndexes)); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) { |
| 448 | int start[MAX_REPLACEABLES]; |
| 449 | int end[MAX_REPLACEABLES]; |
| 450 | for (int i = 0; i < MAX_REPLACEABLES; i++) { |
| 451 | if (i < (int)mReplaceables.size()) { |
| 452 | start[i] = 0; |
| 453 | end[i] = mReplaceables[i].size(); |
| 454 | } else { |
| 455 | start[i] = -1; |
| 456 | end[i] = 0; |
| 457 | } |
| 458 | } |
| 459 | int replacementIndexes[MAX_REPLACEABLES]; |
| 460 | // TODO: These loops assume that MAX_REPLACEABLES is 4. |
| 461 | for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3]; |
| 462 | replacementIndexes[3]++) { |
| 463 | for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2]; |
| 464 | replacementIndexes[2]++) { |
| 465 | for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1]; |
| 466 | replacementIndexes[1]++) { |
| 467 | for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0]; |
| 468 | replacementIndexes[0]++) { |
| 469 | auto p = new FunctionPermutation(function, this, replacementIndexes, scanner); |
| 470 | mPermutations.push_back(p); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const { |
| 478 | return expandString(mUnexpandedName, replacementIndexes); |
| 479 | } |
| 480 | |
| 481 | void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES], |
| 482 | std::string* retType, int* lineNumber) const { |
| 483 | *retType = expandString(mReturn->type, replacementIndexes); |
| 484 | *lineNumber = mReturn->lineNumber; |
| 485 | } |
| 486 | |
| 487 | void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES], |
| 488 | std::string* type, std::string* name, std::string* testOption, |
| 489 | int* lineNumber) const { |
| 490 | ParameterEntry* p = mParameters[index]; |
| 491 | *type = expandString(p->type, replacementIndexes); |
| 492 | *name = p->name; |
| 493 | *testOption = expandString(p->testOption, replacementIndexes); |
| 494 | *lineNumber = p->lineNumber; |
| 495 | } |
| 496 | |
| 497 | void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES], |
| 498 | std::vector<std::string>* inlines) const { |
| 499 | expandStringVector(mInline, replacementIndexes, inlines); |
| 500 | } |
| 501 | |
| 502 | void FunctionSpecification::parseTest(Scanner* scanner) { |
| 503 | const string value = scanner->getValue(); |
| 504 | if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" || |
| 505 | value == "none") { |
| 506 | mTest = value; |
| 507 | } else if (value.compare(0, 7, "limited") == 0) { |
| 508 | mTest = "limited"; |
| 509 | if (value.compare(7, 1, "(") == 0) { |
| 510 | size_t pParen = value.find(')'); |
| 511 | if (pParen == string::npos) { |
| 512 | scanner->error() << "Incorrect test: \"" << value << "\"\n"; |
| 513 | } else { |
| 514 | mPrecisionLimit = value.substr(8, pParen - 8); |
| 515 | } |
| 516 | } |
| 517 | } else { |
| 518 | scanner->error() << "Unrecognized test option: \"" << value << "\"\n"; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | bool FunctionSpecification::hasTests(int versionOfTestFiles) const { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 523 | if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) { |
| 524 | return false; |
| 525 | } |
| 526 | if (mTest == "none") { |
| 527 | return false; |
| 528 | } |
| 529 | return true; |
| 530 | } |
| 531 | |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 532 | void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile, |
| 533 | int maxApiLevel) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 534 | // Some functions like convert have # part of the name. Truncate at that point. |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 535 | const string& unexpandedName = scanner->getValue(); |
| 536 | string name = unexpandedName; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 537 | size_t p = name.find('#'); |
| 538 | if (p != string::npos) { |
| 539 | if (p > 0 && name[p - 1] == '_') { |
| 540 | p--; |
| 541 | } |
| 542 | name.erase(p); |
| 543 | } |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 544 | VersionInfo info; |
| 545 | if (!info.scan(scanner, maxApiLevel)) { |
| 546 | cout << "Skipping some " << name << " definitions.\n"; |
| 547 | scanner->skipUntilTag("end:"); |
| 548 | return; |
| 549 | } |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 550 | |
| 551 | bool created = false; |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 552 | Function* function = systemSpecification.findOrCreateFunction(name, &created); |
| 553 | FunctionSpecification* spec = new FunctionSpecification(function); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 554 | function->addSpecification(spec); |
Jean-Luc Brouillet | 67923a9 | 2015-05-12 15:38:27 -0700 | [diff] [blame] | 555 | function->updateFinalVersion(info); |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 556 | specFile->addFunctionSpecification(spec, created); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 557 | |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 558 | spec->mUnexpandedName = unexpandedName; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 559 | spec->mTest = "scalar"; // default |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 560 | spec->mVersionInfo = info; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 561 | |
| 562 | if (scanner->findOptionalTag("attrib:")) { |
| 563 | spec->mAttribute = scanner->getValue(); |
| 564 | } |
| 565 | if (scanner->findOptionalTag("w:")) { |
| 566 | vector<string> t; |
| 567 | if (scanner->getValue().find("1") != string::npos) { |
| 568 | t.push_back(""); |
| 569 | } |
| 570 | if (scanner->getValue().find("2") != string::npos) { |
| 571 | t.push_back("2"); |
| 572 | } |
| 573 | if (scanner->getValue().find("3") != string::npos) { |
| 574 | t.push_back("3"); |
| 575 | } |
| 576 | if (scanner->getValue().find("4") != string::npos) { |
| 577 | t.push_back("4"); |
| 578 | } |
| 579 | spec->mReplaceables.push_back(t); |
| 580 | } |
| 581 | |
| 582 | while (scanner->findOptionalTag("t:")) { |
| 583 | spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue())); |
| 584 | } |
| 585 | |
| 586 | if (scanner->findTag("ret:")) { |
| 587 | ParameterEntry* p = scanner->parseArgString(true); |
| 588 | function->addReturn(p, scanner); |
| 589 | spec->mReturn = p; |
| 590 | } |
| 591 | while (scanner->findOptionalTag("arg:")) { |
| 592 | ParameterEntry* p = scanner->parseArgString(false); |
| 593 | function->addParameter(p, scanner); |
| 594 | spec->mParameters.push_back(p); |
| 595 | } |
| 596 | |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 597 | function->scanDocumentationTags(scanner, created, specFile); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 598 | |
| 599 | if (scanner->findOptionalTag("inline:")) { |
| 600 | scanner->checkNoValue(); |
| 601 | while (scanner->findOptionalTag("")) { |
| 602 | spec->mInline.push_back(scanner->getValue()); |
| 603 | } |
| 604 | } |
| 605 | if (scanner->findOptionalTag("test:")) { |
| 606 | spec->parseTest(scanner); |
| 607 | } |
| 608 | |
| 609 | scanner->findTag("end:"); |
| 610 | |
| 611 | spec->createPermutations(function, scanner); |
| 612 | } |
| 613 | |
| 614 | FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec, |
| 615 | int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner) |
Jean-Luc Brouillet | 4a73004 | 2015-04-02 16:15:25 -0700 | [diff] [blame] | 616 | : mReturn(nullptr), mInputCount(0), mOutputCount(0) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 617 | // We expand the strings now to make capitalization easier. The previous code preserved |
| 618 | // the #n |
| 619 | // markers just before emitting, which made capitalization difficult. |
| 620 | mName = spec->getName(replacementIndexes); |
| 621 | mNameTrunk = func->getName(); |
| 622 | mTest = spec->getTest(); |
| 623 | mPrecisionLimit = spec->getPrecisionLimit(); |
| 624 | spec->getInlines(replacementIndexes, &mInline); |
| 625 | |
| 626 | mHasFloatAnswers = false; |
| 627 | for (size_t i = 0; i < spec->getNumberOfParams(); i++) { |
| 628 | string type, name, testOption; |
| 629 | int lineNumber = 0; |
| 630 | spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber); |
| 631 | ParameterDefinition* def = new ParameterDefinition(); |
| 632 | def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner); |
| 633 | if (def->isOutParameter) { |
| 634 | mOutputCount++; |
| 635 | } else { |
| 636 | mInputCount++; |
| 637 | } |
| 638 | |
| 639 | if (def->typeIndex < 0 && mTest != "none") { |
| 640 | scanner->error(lineNumber) |
| 641 | << "Could not find " << def->rsBaseType |
| 642 | << " while generating automated tests. Use test: none if not needed.\n"; |
| 643 | } |
| 644 | if (def->isOutParameter && def->isFloatType) { |
| 645 | mHasFloatAnswers = true; |
| 646 | } |
| 647 | mParams.push_back(def); |
| 648 | } |
| 649 | |
| 650 | string retType; |
| 651 | int lineNumber = 0; |
| 652 | spec->getReturn(replacementIndexes, &retType, &lineNumber); |
| 653 | if (!retType.empty()) { |
| 654 | mReturn = new ParameterDefinition(); |
| 655 | mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner); |
| 656 | if (mReturn->isFloatType) { |
| 657 | mHasFloatAnswers = true; |
| 658 | } |
| 659 | mOutputCount++; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | FunctionPermutation::~FunctionPermutation() { |
| 664 | for (auto i : mParams) { |
| 665 | delete i; |
| 666 | } |
| 667 | delete mReturn; |
| 668 | } |
| 669 | |
| 670 | SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) { |
| 671 | string core = mSpecFileName; |
| 672 | // Remove .spec |
| 673 | size_t l = core.length(); |
| 674 | const char SPEC[] = ".spec"; |
| 675 | const int SPEC_SIZE = sizeof(SPEC) - 1; |
| 676 | const int start = l - SPEC_SIZE; |
| 677 | if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) { |
| 678 | core.erase(start); |
| 679 | } |
| 680 | |
| 681 | // The header file name should have the same base but with a ".rsh" extension. |
| 682 | mHeaderFileName = core + ".rsh"; |
Jean-Luc Brouillet | d9935ee | 2015-04-03 17:27:02 -0700 | [diff] [blame] | 683 | mDetailedDocumentationUrl = core + ".html"; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 686 | void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) { |
| 687 | mConstantSpecificationsList.push_back(spec); |
| 688 | if (hasDocumentation) { |
| 689 | Constant* constant = spec->getConstant(); |
| 690 | mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant)); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 691 | } |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) { |
| 695 | mTypeSpecificationsList.push_back(spec); |
| 696 | if (hasDocumentation) { |
| 697 | Type* type = spec->getType(); |
| 698 | mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type)); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 699 | } |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) { |
| 703 | mFunctionSpecificationsList.push_back(spec); |
| 704 | if (hasDocumentation) { |
| 705 | Function* function = spec->getFunction(); |
| 706 | mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function)); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | // Read the specification, adding the definitions to the global functions map. |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 711 | bool SpecFile::readSpecFile(int maxApiLevel) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 712 | FILE* specFile = fopen(mSpecFileName.c_str(), "rt"); |
| 713 | if (!specFile) { |
| 714 | cerr << "Error opening input file: " << mSpecFileName << "\n"; |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | Scanner scanner(mSpecFileName, specFile); |
| 719 | |
| 720 | // Scan the header that should start the file. |
| 721 | scanner.skipBlankEntries(); |
| 722 | if (scanner.findTag("header:")) { |
| 723 | if (scanner.findTag("summary:")) { |
| 724 | mBriefDescription = scanner.getValue(); |
| 725 | } |
| 726 | if (scanner.findTag("description:")) { |
| 727 | scanner.checkNoValue(); |
| 728 | while (scanner.findOptionalTag("")) { |
| 729 | mFullDescription.push_back(scanner.getValue()); |
| 730 | } |
| 731 | } |
| 732 | if (scanner.findOptionalTag("include:")) { |
| 733 | scanner.checkNoValue(); |
| 734 | while (scanner.findOptionalTag("")) { |
| 735 | mVerbatimInclude.push_back(scanner.getValue()); |
| 736 | } |
| 737 | } |
| 738 | scanner.findTag("end:"); |
| 739 | } |
| 740 | |
| 741 | while (1) { |
| 742 | scanner.skipBlankEntries(); |
| 743 | if (scanner.atEnd()) { |
| 744 | break; |
| 745 | } |
| 746 | const string tag = scanner.getNextTag(); |
| 747 | if (tag == "function:") { |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 748 | FunctionSpecification::scanFunctionSpecification(&scanner, this, maxApiLevel); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 749 | } else if (tag == "type:") { |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 750 | TypeSpecification::scanTypeSpecification(&scanner, this, maxApiLevel); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 751 | } else if (tag == "constant:") { |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 752 | ConstantSpecification::scanConstantSpecification(&scanner, this, maxApiLevel); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 753 | } else { |
| 754 | scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n"; |
| 755 | return false; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | fclose(specFile); |
| 760 | return scanner.getErrorCount() == 0; |
| 761 | } |
| 762 | |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 763 | SystemSpecification::~SystemSpecification() { |
| 764 | for (auto i : mConstants) { |
| 765 | delete i.second; |
| 766 | } |
| 767 | for (auto i : mTypes) { |
| 768 | delete i.second; |
| 769 | } |
| 770 | for (auto i : mFunctions) { |
| 771 | delete i.second; |
| 772 | } |
| 773 | for (auto i : mSpecFiles) { |
| 774 | delete i; |
| 775 | } |
| 776 | } |
| 777 | |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 778 | // Returns the named entry in the map. Creates it if it's not there. |
| 779 | template <class T> |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 780 | T* findOrCreate(const string& name, map<string, T*>* map, bool* created) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 781 | auto iter = map->find(name); |
| 782 | if (iter != map->end()) { |
| 783 | *created = false; |
| 784 | return iter->second; |
| 785 | } |
| 786 | *created = true; |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 787 | T* f = new T(name); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 788 | map->insert(pair<string, T*>(name, f)); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 789 | return f; |
| 790 | } |
| 791 | |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 792 | Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) { |
| 793 | return findOrCreate<Constant>(name, &mConstants, created); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 796 | Type* SystemSpecification::findOrCreateType(const string& name, bool* created) { |
| 797 | return findOrCreate<Type>(name, &mTypes, created); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 800 | Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) { |
| 801 | return findOrCreate<Function>(name, &mFunctions, created); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 804 | bool SystemSpecification::readSpecFile(const string& fileName, int maxApiLevel) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 805 | SpecFile* spec = new SpecFile(fileName); |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 806 | if (!spec->readSpecFile(maxApiLevel)) { |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 807 | cerr << fileName << ": Failed to parse.\n"; |
| 808 | return false; |
| 809 | } |
| 810 | mSpecFiles.push_back(spec); |
Jean-Luc Brouillet | 7c07854 | 2015-03-23 16:16:08 -0700 | [diff] [blame] | 811 | return true; |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Jean-Luc Brouillet | 2217eb7 | 2015-04-24 14:41:48 -0700 | [diff] [blame] | 814 | bool SystemSpecification::generateFiles(bool forVerification, int maxApiLevel) const { |
| 815 | bool success = generateHeaderFiles("scriptc") && |
| 816 | generateDocumentation("docs", forVerification) && |
| 817 | generateTestFiles("test", maxApiLevel); |
Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame] | 818 | if (success) { |
| 819 | cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size() |
| 820 | << " constants, and " << mFunctions.size() << " functions.\n"; |
| 821 | } |
| 822 | return success; |
| 823 | } |
| 824 | |
| 825 | string SystemSpecification::getHtmlAnchor(const string& name) const { |
| 826 | Definition* d = nullptr; |
| 827 | auto c = mConstants.find(name); |
| 828 | if (c != mConstants.end()) { |
| 829 | d = c->second; |
| 830 | } else { |
| 831 | auto t = mTypes.find(name); |
| 832 | if (t != mTypes.end()) { |
| 833 | d = t->second; |
| 834 | } else { |
| 835 | auto f = mFunctions.find(name); |
| 836 | if (f != mFunctions.end()) { |
| 837 | d = f->second; |
| 838 | } else { |
| 839 | return string(); |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | ostringstream stream; |
| 844 | stream << "<a href='" << d->getUrl() << "'>" << name << "</a>"; |
| 845 | return stream.str(); |
| 846 | } |