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