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