123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969 |
- #include <stdio.h>
- #include <cctype>
- #include <cstdlib>
- #include <fstream>
- #include <functional>
- #include <iostream>
- #include <memory>
- #include <sstream>
- #include <strings.h>
- #include "Generator.h"
- #include "Scanner.h"
- #include "Specification.h"
- #include "Utilities.h"
- using namespace std;
- const unsigned int MIN_API_LEVEL = 9;
- const NumericalType TYPES[] = {
- {"f16", "FLOAT_16", "half", "short", FLOATING_POINT, 11, 5},
- {"f32", "FLOAT_32", "float", "float", FLOATING_POINT, 24, 8},
- {"f64", "FLOAT_64", "double", "double", FLOATING_POINT, 53, 11},
- {"i8", "SIGNED_8", "char", "byte", SIGNED_INTEGER, 7, 0},
- {"u8", "UNSIGNED_8", "uchar", "byte", UNSIGNED_INTEGER, 8, 0},
- {"i16", "SIGNED_16", "short", "short", SIGNED_INTEGER, 15, 0},
- {"u16", "UNSIGNED_16", "ushort", "short", UNSIGNED_INTEGER, 16, 0},
- {"i32", "SIGNED_32", "int", "int", SIGNED_INTEGER, 31, 0},
- {"u32", "UNSIGNED_32", "uint", "int", UNSIGNED_INTEGER, 32, 0},
- {"i64", "SIGNED_64", "long", "long", SIGNED_INTEGER, 63, 0},
- {"u64", "UNSIGNED_64", "ulong", "long", UNSIGNED_INTEGER, 64, 0},
- };
- const int NUM_TYPES = sizeof(TYPES) / sizeof(TYPES[0]);
- static const char kTagUnreleased[] = "UNRELEASED";
- static const string kCTypePatterns[] = {"#1", "#2", "#3", "#4"};
- static const string kRSTypePatterns[] = {"#RST_1", "#RST_2", "#RST_3", "#RST_4"};
- SystemSpecification systemSpecification;
- static int findCType(const string& cType) {
- for (int i = 0; i < NUM_TYPES; i++) {
- if (cType == TYPES[i].cType) {
- return i;
- }
- }
- return -1;
- }
- static vector<string> convertToTypeVector(const string& input) {
-
- vector<string> entries;
- stringstream stream(input);
- string entry;
- while (getline(stream, entry, ',')) {
- trimSpaces(&entry);
- entries.push_back(entry);
- }
-
- vector<string> result;
- for (auto t : TYPES) {
- for (auto i = entries.begin(); i != entries.end(); ++i) {
- if (*i == t.specType) {
- result.push_back(t.cType);
- entries.erase(i);
- break;
- }
- }
- }
-
- for (auto s : entries) {
- result.push_back(s);
- }
- return result;
- }
- static bool isRSTValid(const vector<string> &typeVector) {
- for (auto type: typeVector) {
- if (findCType(type) == -1)
- return false;
- }
- return true;
- }
- void getVectorSizeAndBaseType(const string& type, string& vectorSize, string& baseType) {
- vectorSize = "1";
- baseType = type;
-
- const int last = type.size() - 1;
- const char lastChar = type[last];
- if (lastChar >= '0' && lastChar <= '9') {
- const string trimmed = type.substr(0, last);
- int i = findCType(trimmed);
- if (i >= 0) {
- baseType = trimmed;
- vectorSize = lastChar;
- }
- }
- }
- void ParameterDefinition::parseParameterDefinition(const string& type, const string& name,
- const string& testOption, int lineNumber,
- bool isReturn, Scanner* scanner) {
- rsType = type;
- specName = name;
-
- isOutParameter = isReturn || charRemoved('*', &rsType);
- getVectorSizeAndBaseType(rsType, mVectorSize, rsBaseType);
- typeIndex = findCType(rsBaseType);
- if (mVectorSize == "3") {
- vectorWidth = "4";
- } else {
- vectorWidth = mVectorSize;
- }
-
- if (isOutParameter) {
- variableName = "out";
- if (!specName.empty()) {
- variableName += capitalize(specName);
- } else if (!isReturn) {
- scanner->error(lineNumber) << "Should have a name.\n";
- }
- doubleVariableName = variableName + "Double";
- } else {
- variableName = "in";
- if (specName.empty()) {
- scanner->error(lineNumber) << "Should have a name.\n";
- }
- variableName += capitalize(specName);
- doubleVariableName = variableName + "Double";
- }
- rsAllocName = "gAlloc" + capitalize(variableName);
- javaAllocName = variableName;
- javaArrayName = "array" + capitalize(javaAllocName);
-
- undefinedIfOutIsNan = false;
- compatibleTypeIndex = -1;
- if (!testOption.empty()) {
- if (testOption.compare(0, 6, "range(") == 0) {
- size_t pComma = testOption.find(',');
- size_t pParen = testOption.find(')');
- if (pComma == string::npos || pParen == string::npos) {
- scanner->error(lineNumber) << "Incorrect range " << testOption << "\n";
- } else {
- minValue = testOption.substr(6, pComma - 6);
- maxValue = testOption.substr(pComma + 1, pParen - pComma - 1);
- }
- } else if (testOption.compare(0, 6, "above(") == 0) {
- size_t pParen = testOption.find(')');
- if (pParen == string::npos) {
- scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
- } else {
- smallerParameter = testOption.substr(6, pParen - 6);
- }
- } else if (testOption.compare(0, 11, "compatible(") == 0) {
- size_t pParen = testOption.find(')');
- if (pParen == string::npos) {
- scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
- } else {
- compatibleTypeIndex = findCType(testOption.substr(11, pParen - 11));
- }
- } else if (testOption.compare(0, 11, "conditional") == 0) {
- undefinedIfOutIsNan = true;
- } else {
- scanner->error(lineNumber) << "Unrecognized testOption " << testOption << "\n";
- }
- }
- isFloatType = false;
- if (typeIndex >= 0) {
- javaBaseType = TYPES[typeIndex].javaType;
- specType = TYPES[typeIndex].specType;
- isFloatType = TYPES[typeIndex].exponentBits > 0;
- }
- if (!minValue.empty()) {
- if (typeIndex < 0 || TYPES[typeIndex].kind != FLOATING_POINT) {
- scanner->error(lineNumber) << "range(,) is only supported for floating point\n";
- }
- }
- }
- bool VersionInfo::scan(Scanner* scanner, unsigned int maxApiLevel) {
- if (scanner->findOptionalTag("version:")) {
- const string s = scanner->getValue();
- if (s.compare(0, sizeof(kTagUnreleased), kTagUnreleased) == 0) {
-
-
- minVersion = maxVersion = kUnreleasedVersion;
- } else {
- sscanf(s.c_str(), "%u %u", &minVersion, &maxVersion);
- if (minVersion && minVersion < MIN_API_LEVEL) {
- scanner->error() << "Minimum version must >= 9\n";
- }
- if (minVersion == MIN_API_LEVEL) {
- minVersion = 0;
- }
- if (maxVersion && maxVersion < MIN_API_LEVEL) {
- scanner->error() << "Maximum version must >= 9\n";
- }
- }
- }
- if (scanner->findOptionalTag("size:")) {
- sscanf(scanner->getValue().c_str(), "%i", &intSize);
- }
- if (maxVersion > maxApiLevel) {
- maxVersion = maxApiLevel;
- }
- return minVersion == 0 || minVersion <= maxApiLevel;
- }
- Definition::Definition(const std::string& name)
- : mName(name), mDeprecatedApiLevel(0), mHidden(false), mFinalVersion(-1) {
- }
- void Definition::updateFinalVersion(const VersionInfo& info) {
-
- if (mFinalVersion < 0 || info.maxVersion == 0 ||
- (mFinalVersion > 0 &&
- static_cast<int>(info.maxVersion) > mFinalVersion)) {
- mFinalVersion = info.maxVersion;
- }
- }
- void Definition::scanDocumentationTags(Scanner* scanner, bool firstOccurence,
- const SpecFile* specFile) {
- if (scanner->findOptionalTag("hidden:")) {
- scanner->checkNoValue();
- mHidden = true;
- }
- if (scanner->findOptionalTag("deprecated:")) {
- string value = scanner->getValue();
- size_t pComma = value.find(", ");
- if (pComma != string::npos) {
- mDeprecatedMessage = value.substr(pComma + 2);
- value.erase(pComma);
- }
- sscanf(value.c_str(), "%i", &mDeprecatedApiLevel);
- if (mDeprecatedApiLevel <= 0) {
- scanner->error() << "deprecated entries should have a level > 0\n";
- }
- }
- if (firstOccurence) {
- if (scanner->findTag("summary:")) {
- mSummary = scanner->getValue();
- }
- if (scanner->findTag("description:")) {
- scanner->checkNoValue();
- while (scanner->findOptionalTag("")) {
- mDescription.push_back(scanner->getValue());
- }
- }
- mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + mName;
- } else if (scanner->findOptionalTag("summary:")) {
- scanner->error() << "Only the first specification should have a summary.\n";
- }
- }
- Constant::~Constant() {
- for (auto i : mSpecifications) {
- delete i;
- }
- }
- Type::~Type() {
- for (auto i : mSpecifications) {
- delete i;
- }
- }
- Function::Function(const string& name) : Definition(name) {
- mCapitalizedName = capitalize(mName);
- }
- Function::~Function() {
- for (auto i : mSpecifications) {
- delete i;
- }
- }
- bool Function::someParametersAreDocumented() const {
- for (auto p : mParameters) {
- if (!p->documentation.empty()) {
- return true;
- }
- }
- return false;
- }
- void Function::addParameter(ParameterEntry* entry, Scanner* scanner) {
- for (auto i : mParameters) {
- if (i->name == entry->name) {
-
- if (!entry->documentation.empty()) {
- scanner->error(entry->lineNumber)
- << "Only the first occurence of an arg should have the "
- "documentation.\n";
- }
- return;
- }
- }
- mParameters.push_back(entry);
- }
- void Function::addReturn(ParameterEntry* entry, Scanner* scanner) {
- if (entry->documentation.empty()) {
- return;
- }
- if (!mReturnDocumentation.empty()) {
- scanner->error() << "ret: should be documented only for the first variant\n";
- }
- mReturnDocumentation = entry->documentation;
- }
- void ConstantSpecification::scanConstantSpecification(Scanner* scanner, SpecFile* specFile,
- unsigned int maxApiLevel) {
- string name = scanner->getValue();
- VersionInfo info;
- if (!info.scan(scanner, maxApiLevel)) {
- cout << "Skipping some " << name << " definitions.\n";
- scanner->skipUntilTag("end:");
- return;
- }
- bool created = false;
- Constant* constant = systemSpecification.findOrCreateConstant(name, &created);
- ConstantSpecification* spec = new ConstantSpecification(constant);
- constant->addSpecification(spec);
- constant->updateFinalVersion(info);
- specFile->addConstantSpecification(spec, created);
- spec->mVersionInfo = info;
- if (scanner->findTag("value:")) {
- spec->mValue = scanner->getValue();
- }
- if (scanner->findTag("type:")) {
- spec->mType = scanner->getValue();
- }
- constant->scanDocumentationTags(scanner, created, specFile);
- scanner->findTag("end:");
- }
- void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile,
- unsigned int maxApiLevel) {
- string name = scanner->getValue();
- VersionInfo info;
- if (!info.scan(scanner, maxApiLevel)) {
- cout << "Skipping some " << name << " definitions.\n";
- scanner->skipUntilTag("end:");
- return;
- }
- bool created = false;
- Type* type = systemSpecification.findOrCreateType(name, &created);
- TypeSpecification* spec = new TypeSpecification(type);
- type->addSpecification(spec);
- type->updateFinalVersion(info);
- specFile->addTypeSpecification(spec, created);
- spec->mVersionInfo = info;
- if (scanner->findOptionalTag("simple:")) {
- spec->mKind = SIMPLE;
- spec->mSimpleType = scanner->getValue();
- }
- if (scanner->findOptionalTag("rs_object:")) {
- spec->mKind = RS_OBJECT;
- }
- if (scanner->findOptionalTag("struct:")) {
- spec->mKind = STRUCT;
- spec->mStructName = scanner->getValue();
- while (scanner->findOptionalTag("field:")) {
- string s = scanner->getValue();
- string comment;
- scanner->parseDocumentation(&s, &comment);
- spec->mFields.push_back(s);
- spec->mFieldComments.push_back(comment);
- }
- }
- if (scanner->findOptionalTag("enum:")) {
- spec->mKind = ENUM;
- spec->mEnumName = scanner->getValue();
- while (scanner->findOptionalTag("value:")) {
- string s = scanner->getValue();
- string comment;
- scanner->parseDocumentation(&s, &comment);
- spec->mValues.push_back(s);
- spec->mValueComments.push_back(comment);
- }
- }
- if (scanner->findOptionalTag("attrib:")) {
- spec->mAttribute = scanner->getValue();
- }
- type->scanDocumentationTags(scanner, created, specFile);
- scanner->findTag("end:");
- }
- FunctionSpecification::~FunctionSpecification() {
- for (auto i : mParameters) {
- delete i;
- }
- delete mReturn;
- for (auto i : mPermutations) {
- delete i;
- }
- }
- string FunctionSpecification::expandRSTypeInString(const string &s,
- const string &pattern,
- const string &cTypeStr) const {
-
-
-
- int typeIdx = findCType(cTypeStr);
- if (typeIdx == -1) {
- return s;
- }
-
- return stringReplace(s, pattern, TYPES[typeIdx].rsDataType);
- }
- string FunctionSpecification::expandString(string s,
- int replacementIndexes[MAX_REPLACEABLES]) const {
- for (unsigned idx = 0; idx < mReplaceables.size(); idx ++) {
- string toString = mReplaceables[idx][replacementIndexes[idx]];
-
- s = expandRSTypeInString(s, kRSTypePatterns[idx], toString);
-
- s = stringReplace(s, kCTypePatterns[idx], toString);
- }
- return s;
- }
- void FunctionSpecification::expandStringVector(const vector<string>& in,
- int replacementIndexes[MAX_REPLACEABLES],
- vector<string>* out) const {
- out->clear();
- for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
- out->push_back(expandString(*iter, replacementIndexes));
- }
- }
- void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) {
- int start[MAX_REPLACEABLES];
- int end[MAX_REPLACEABLES];
- for (int i = 0; i < MAX_REPLACEABLES; i++) {
- if (i < (int)mReplaceables.size()) {
- start[i] = 0;
- end[i] = mReplaceables[i].size();
- } else {
- start[i] = -1;
- end[i] = 0;
- }
- }
- int replacementIndexes[MAX_REPLACEABLES];
-
- for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3];
- replacementIndexes[3]++) {
- for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2];
- replacementIndexes[2]++) {
- for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1];
- replacementIndexes[1]++) {
- for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0];
- replacementIndexes[0]++) {
- auto p = new FunctionPermutation(function, this, replacementIndexes, scanner);
- mPermutations.push_back(p);
- }
- }
- }
- }
- }
- string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const {
- return expandString(mUnexpandedName, replacementIndexes);
- }
- void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES],
- std::string* retType, int* lineNumber) const {
- *retType = expandString(mReturn->type, replacementIndexes);
- *lineNumber = mReturn->lineNumber;
- }
- void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES],
- std::string* type, std::string* name, std::string* testOption,
- int* lineNumber) const {
- ParameterEntry* p = mParameters[index];
- *type = expandString(p->type, replacementIndexes);
- *name = p->name;
- *testOption = expandString(p->testOption, replacementIndexes);
- *lineNumber = p->lineNumber;
- }
- void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES],
- std::vector<std::string>* inlines) const {
- expandStringVector(mInline, replacementIndexes, inlines);
- }
- void FunctionSpecification::parseTest(Scanner* scanner) {
- const string value = scanner->getValue();
- if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" ||
- value == "none") {
- mTest = value;
- } else if (value.compare(0, 7, "limited") == 0) {
- mTest = "limited";
- if (value.compare(7, 1, "(") == 0) {
- size_t pParen = value.find(')');
- if (pParen == string::npos) {
- scanner->error() << "Incorrect test: \"" << value << "\"\n";
- } else {
- mPrecisionLimit = value.substr(8, pParen - 8);
- }
- }
- } else {
- scanner->error() << "Unrecognized test option: \"" << value << "\"\n";
- }
- }
- bool FunctionSpecification::hasTests(unsigned int versionOfTestFiles) const {
- if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) {
- return false;
- }
- if (mTest == "none") {
- return false;
- }
- return true;
- }
- void FunctionSpecification::checkRSTPatternValidity(const string &inlineStr, bool allow,
- Scanner *scanner) {
- for (int i = 0; i < MAX_REPLACEABLES; i ++) {
- bool patternFound = inlineStr.find(kRSTypePatterns[i]) != string::npos;
- if (patternFound) {
- if (!allow) {
- scanner->error() << "RST_i pattern not allowed here\n";
- }
- else if (mIsRSTAllowed[i] == false) {
- scanner->error() << "Found pattern \"" << kRSTypePatterns[i]
- << "\" in spec. But some entry in the corresponding"
- << " parameter list cannot be translated to an RS type\n";
- }
- }
- }
- }
- void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile,
- unsigned int maxApiLevel) {
-
- const string& unexpandedName = scanner->getValue();
- string name = unexpandedName;
- size_t p = name.find('#');
- if (p != string::npos) {
- if (p > 0 && name[p - 1] == '_') {
- p--;
- }
- name.erase(p);
- }
- VersionInfo info;
- if (!info.scan(scanner, maxApiLevel)) {
- cout << "Skipping some " << name << " definitions.\n";
- scanner->skipUntilTag("end:");
- return;
- }
- bool created = false;
- Function* function = systemSpecification.findOrCreateFunction(name, &created);
- FunctionSpecification* spec = new FunctionSpecification(function);
- function->addSpecification(spec);
- function->updateFinalVersion(info);
- specFile->addFunctionSpecification(spec, created);
- spec->mUnexpandedName = unexpandedName;
- spec->mTest = "scalar";
- spec->mVersionInfo = info;
- if (scanner->findOptionalTag("internal:")) {
- spec->mInternal = (scanner->getValue() == "true");
- }
- if (scanner->findOptionalTag("intrinsic:")) {
- spec->mIntrinsic = (scanner->getValue() == "true");
- }
- if (scanner->findOptionalTag("attrib:")) {
- spec->mAttribute = scanner->getValue();
- }
- if (scanner->findOptionalTag("w:")) {
- vector<string> t;
- if (scanner->getValue().find("1") != string::npos) {
- t.push_back("");
- }
- if (scanner->getValue().find("2") != string::npos) {
- t.push_back("2");
- }
- if (scanner->getValue().find("3") != string::npos) {
- t.push_back("3");
- }
- if (scanner->getValue().find("4") != string::npos) {
- t.push_back("4");
- }
- spec->mReplaceables.push_back(t);
-
- spec->mIsRSTAllowed.push_back(false);
- }
- while (scanner->findOptionalTag("t:")) {
- spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue()));
- spec->mIsRSTAllowed.push_back(isRSTValid(spec->mReplaceables.back()));
- }
-
-
- spec->checkRSTPatternValidity(unexpandedName, false, scanner);
- if (scanner->findTag("ret:")) {
- ParameterEntry* p = scanner->parseArgString(true);
- function->addReturn(p, scanner);
- spec->mReturn = p;
-
- spec->checkRSTPatternValidity(p->type, false, scanner);
- }
- while (scanner->findOptionalTag("arg:")) {
- ParameterEntry* p = scanner->parseArgString(false);
- function->addParameter(p, scanner);
- spec->mParameters.push_back(p);
-
- spec->checkRSTPatternValidity(p->type, false, scanner);
- spec->checkRSTPatternValidity(p->testOption, false, scanner);
- }
- function->scanDocumentationTags(scanner, created, specFile);
- if (scanner->findOptionalTag("inline:")) {
- scanner->checkNoValue();
- while (scanner->findOptionalTag("")) {
- spec->mInline.push_back(scanner->getValue());
-
- spec->checkRSTPatternValidity(spec->mInline.back(), true, scanner);
- }
- }
- if (scanner->findOptionalTag("test:")) {
- spec->parseTest(scanner);
- }
- scanner->findTag("end:");
- spec->createPermutations(function, scanner);
- }
- FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec,
- int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner)
- : mReturn(nullptr), mInputCount(0), mOutputCount(0) {
-
-
-
- mName = spec->getName(replacementIndexes);
- mNameTrunk = func->getName();
- mTest = spec->getTest();
- mPrecisionLimit = spec->getPrecisionLimit();
- spec->getInlines(replacementIndexes, &mInline);
- mHasFloatAnswers = false;
- for (size_t i = 0; i < spec->getNumberOfParams(); i++) {
- string type, name, testOption;
- int lineNumber = 0;
- spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber);
- ParameterDefinition* def = new ParameterDefinition();
- def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner);
- if (def->isOutParameter) {
- mOutputCount++;
- } else {
- mInputCount++;
- }
- if (def->typeIndex < 0 && mTest != "none") {
- scanner->error(lineNumber)
- << "Could not find " << def->rsBaseType
- << " while generating automated tests. Use test: none if not needed.\n";
- }
- if (def->isOutParameter && def->isFloatType) {
- mHasFloatAnswers = true;
- }
- mParams.push_back(def);
- }
- string retType;
- int lineNumber = 0;
- spec->getReturn(replacementIndexes, &retType, &lineNumber);
- if (!retType.empty()) {
- mReturn = new ParameterDefinition();
- mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner);
- if (mReturn->isFloatType) {
- mHasFloatAnswers = true;
- }
- mOutputCount++;
- }
- }
- FunctionPermutation::~FunctionPermutation() {
- for (auto i : mParams) {
- delete i;
- }
- delete mReturn;
- }
- SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) {
- string core = mSpecFileName;
-
- size_t l = core.length();
- const char SPEC[] = ".spec";
- const int SPEC_SIZE = sizeof(SPEC) - 1;
- const int start = l - SPEC_SIZE;
- if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) {
- core.erase(start);
- }
-
- mHeaderFileName = core + ".rsh";
- mDetailedDocumentationUrl = core + ".html";
- }
- void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) {
- mConstantSpecificationsList.push_back(spec);
- if (hasDocumentation) {
- Constant* constant = spec->getConstant();
- mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant));
- }
- }
- void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) {
- mTypeSpecificationsList.push_back(spec);
- if (hasDocumentation) {
- Type* type = spec->getType();
- mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type));
- }
- }
- void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) {
- mFunctionSpecificationsList.push_back(spec);
- if (hasDocumentation) {
- Function* function = spec->getFunction();
- mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function));
- }
- }
- bool SpecFile::readSpecFile(unsigned int maxApiLevel) {
- FILE* specFile = fopen(mSpecFileName.c_str(), "rte");
- if (!specFile) {
- cerr << "Error opening input file: " << mSpecFileName << "\n";
- return false;
- }
- Scanner scanner(mSpecFileName, specFile);
-
- scanner.skipBlankEntries();
- if (scanner.findTag("header:")) {
- if (scanner.findTag("summary:")) {
- mBriefDescription = scanner.getValue();
- }
- if (scanner.findTag("description:")) {
- scanner.checkNoValue();
- while (scanner.findOptionalTag("")) {
- mFullDescription.push_back(scanner.getValue());
- }
- }
- if (scanner.findOptionalTag("include:")) {
- scanner.checkNoValue();
- while (scanner.findOptionalTag("")) {
- mVerbatimInclude.push_back(scanner.getValue());
- }
- }
- scanner.findTag("end:");
- }
- while (1) {
- scanner.skipBlankEntries();
- if (scanner.atEnd()) {
- break;
- }
- const string tag = scanner.getNextTag();
- if (tag == "function:") {
- FunctionSpecification::scanFunctionSpecification(&scanner, this, maxApiLevel);
- } else if (tag == "type:") {
- TypeSpecification::scanTypeSpecification(&scanner, this, maxApiLevel);
- } else if (tag == "constant:") {
- ConstantSpecification::scanConstantSpecification(&scanner, this, maxApiLevel);
- } else {
- scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
- return false;
- }
- }
- fclose(specFile);
- return scanner.getErrorCount() == 0;
- }
- SystemSpecification::~SystemSpecification() {
- for (auto i : mConstants) {
- delete i.second;
- }
- for (auto i : mTypes) {
- delete i.second;
- }
- for (auto i : mFunctions) {
- delete i.second;
- }
- for (auto i : mSpecFiles) {
- delete i;
- }
- }
- template <class T>
- T* findOrCreate(const string& name, map<string, T*>* map, bool* created) {
- auto iter = map->find(name);
- if (iter != map->end()) {
- *created = false;
- return iter->second;
- }
- *created = true;
- T* f = new T(name);
- map->insert(pair<string, T*>(name, f));
- return f;
- }
- Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) {
- return findOrCreate<Constant>(name, &mConstants, created);
- }
- Type* SystemSpecification::findOrCreateType(const string& name, bool* created) {
- return findOrCreate<Type>(name, &mTypes, created);
- }
- Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) {
- return findOrCreate<Function>(name, &mFunctions, created);
- }
- bool SystemSpecification::readSpecFile(const string& fileName, unsigned int maxApiLevel) {
- SpecFile* spec = new SpecFile(fileName);
- if (!spec->readSpecFile(maxApiLevel)) {
- cerr << fileName << ": Failed to parse.\n";
- return false;
- }
- mSpecFiles.push_back(spec);
- return true;
- }
- static void updateMaxApiLevel(const VersionInfo& info, unsigned int* maxApiLevel) {
- if (info.minVersion == VersionInfo::kUnreleasedVersion) {
-
- return;
- }
- *maxApiLevel = max(*maxApiLevel, max(info.minVersion, info.maxVersion));
- }
- unsigned int SystemSpecification::getMaximumApiLevel() {
- unsigned int maxApiLevel = 0;
- for (auto i : mConstants) {
- for (auto j: i.second->getSpecifications()) {
- updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
- }
- }
- for (auto i : mTypes) {
- for (auto j: i.second->getSpecifications()) {
- updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
- }
- }
- for (auto i : mFunctions) {
- for (auto j: i.second->getSpecifications()) {
- updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
- }
- }
- return maxApiLevel;
- }
- bool SystemSpecification::generateFiles(unsigned int maxApiLevel) const {
- bool success = generateHeaderFiles("include") &&
- generateDocumentation("docs") &&
- generateTestFiles("test", maxApiLevel) &&
- generateStubsWhiteList("slangtest", maxApiLevel);
- if (success) {
- cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
- << " constants, and " << mFunctions.size() << " functions.\n";
- }
- return success;
- }
- string SystemSpecification::getHtmlAnchor(const string& name) const {
- Definition* d = nullptr;
- auto c = mConstants.find(name);
- if (c != mConstants.end()) {
- d = c->second;
- } else {
- auto t = mTypes.find(name);
- if (t != mTypes.end()) {
- d = t->second;
- } else {
- auto f = mFunctions.find(name);
- if (f != mFunctions.end()) {
- d = f->second;
- } else {
- return string();
- }
- }
- }
- ostringstream stream;
- stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
- return stream.str();
- }
|