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