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