blob: a82fd1b1a2caf0dc081090621ecd7019e5d853a0 [file] [log] [blame]
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07001/*
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
32using namespace std;
33
34// API level when RenderScript was added.
35const int MIN_API_LEVEL = 9;
36
37const NumericalType TYPES[] = {
38 {"f16", "FLOAT_16", "half", "half", FLOATING_POINT, 11, 5},
39 {"f32", "FLOAT_32", "float", "float", FLOATING_POINT, 24, 8},
40 {"f64", "FLOAT_64", "double", "double", FLOATING_POINT, 53, 11},
41 {"i8", "SIGNED_8", "char", "byte", SIGNED_INTEGER, 7, 0},
42 {"u8", "UNSIGNED_8", "uchar", "byte", UNSIGNED_INTEGER, 8, 0},
43 {"i16", "SIGNED_16", "short", "short", SIGNED_INTEGER, 15, 0},
44 {"u16", "UNSIGNED_16", "ushort", "short", UNSIGNED_INTEGER, 16, 0},
45 {"i32", "SIGNED_32", "int", "int", SIGNED_INTEGER, 31, 0},
46 {"u32", "UNSIGNED_32", "uint", "int", UNSIGNED_INTEGER, 32, 0},
47 {"i64", "SIGNED_64", "long", "long", SIGNED_INTEGER, 63, 0},
48 {"u64", "UNSIGNED_64", "ulong", "long", UNSIGNED_INTEGER, 64, 0},
49};
50
51const int NUM_TYPES = sizeof(TYPES) / sizeof(TYPES[0]);
52
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070053// The singleton of the collected information of all the spec files.
54SystemSpecification systemSpecification;
55
56// Returns the index in TYPES for the provided cType
57static 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 */
69static 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
102void 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
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700111 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];
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700120 if (lastChar >= '0' && lastChar <= '9') {
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700121 const string trimmed = rsType.substr(0, last);
122 int i = findCType(trimmed);
123 if (i >= 0) {
124 rsBaseType = trimmed;
125 mVectorSize = lastChar;
126 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700127 }
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700128 typeIndex = findCType(rsBaseType);
129
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700130 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
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700191 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
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700204bool VersionInfo::scan(Scanner* scanner, int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700205 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 }
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700221 if (maxVersion > maxApiLevel) {
222 maxVersion = maxApiLevel;
223 }
224 return minVersion == 0 || minVersion <= maxApiLevel;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700225}
226
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700227Definition::Definition(const std::string& name)
228 : mName(name), mDeprecated(false), mHidden(false), mFinalVersion(-1) {
229}
230
231void 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 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700241}
242
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700243void Definition::scanDocumentationTags(Scanner* scanner, bool firstOccurence,
244 const SpecFile* specFile) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700245 if (scanner->findOptionalTag("hidden:")) {
246 scanner->checkNoValue();
247 mHidden = true;
248 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700249 if (scanner->findOptionalTag("deprecated:")) {
250 mDeprecated = true;
251 mDeprecatedMessage = scanner->getValue();
252 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700253 if (firstOccurence) {
254 if (scanner->findTag("summary:")) {
255 mSummary = scanner->getValue();
256 }
257 if (scanner->findTag("description:")) {
258 scanner->checkNoValue();
259 while (scanner->findOptionalTag("")) {
260 mDescription.push_back(scanner->getValue());
261 }
262 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700263 mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + mName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700264 } else if (scanner->findOptionalTag("summary:")) {
265 scanner->error() << "Only the first specification should have a summary.\n";
266 }
267}
268
269Constant::~Constant() {
270 for (auto i : mSpecifications) {
271 delete i;
272 }
273}
274
275Type::~Type() {
276 for (auto i : mSpecifications) {
277 delete i;
278 }
279}
280
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700281Function::Function(const string& name) : Definition(name) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700282 mCapitalizedName = capitalize(mName);
283}
284
285Function::~Function() {
286 for (auto i : mSpecifications) {
287 delete i;
288 }
289}
290
291bool Function::someParametersAreDocumented() const {
292 for (auto p : mParameters) {
293 if (!p->documentation.empty()) {
294 return true;
295 }
296 }
297 return false;
298}
299
300void Function::addParameter(ParameterEntry* entry, Scanner* scanner) {
301 for (auto i : mParameters) {
302 if (i->name == entry->name) {
303 // It's a duplicate.
304 if (!entry->documentation.empty()) {
305 scanner->error(entry->lineNumber)
306 << "Only the first occurence of an arg should have the "
307 "documentation.\n";
308 }
309 return;
310 }
311 }
312 mParameters.push_back(entry);
313}
314
315void Function::addReturn(ParameterEntry* entry, Scanner* scanner) {
316 if (entry->documentation.empty()) {
317 return;
318 }
319 if (!mReturnDocumentation.empty()) {
320 scanner->error() << "ret: should be documented only for the first variant\n";
321 }
322 mReturnDocumentation = entry->documentation;
323}
324
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700325void ConstantSpecification::scanConstantSpecification(Scanner* scanner, SpecFile* specFile,
326 int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700327 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700328 VersionInfo info;
329 if (!info.scan(scanner, maxApiLevel)) {
330 cout << "Skipping some " << name << " definitions.\n";
331 scanner->skipUntilTag("end:");
332 return;
333 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700334
335 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700336 Constant* constant = systemSpecification.findOrCreateConstant(name, &created);
337 ConstantSpecification* spec = new ConstantSpecification(constant);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700338 constant->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700339 constant->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700340 specFile->addConstantSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700341 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700342
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700343 if (scanner->findTag("value:")) {
344 spec->mValue = scanner->getValue();
345 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700346 constant->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700347
348 scanner->findTag("end:");
349}
350
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700351void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile,
352 int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700353 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700354 VersionInfo info;
355 if (!info.scan(scanner, maxApiLevel)) {
356 cout << "Skipping some " << name << " definitions.\n";
357 scanner->skipUntilTag("end:");
358 return;
359 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700360
361 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700362 Type* type = systemSpecification.findOrCreateType(name, &created);
363 TypeSpecification* spec = new TypeSpecification(type);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700364 type->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700365 type->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700366 specFile->addTypeSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700367 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700368
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700369 if (scanner->findOptionalTag("simple:")) {
370 spec->mKind = SIMPLE;
371 spec->mSimpleType = scanner->getValue();
372 }
373 if (scanner->findOptionalTag("struct:")) {
374 spec->mKind = STRUCT;
375 spec->mStructName = scanner->getValue();
376 while (scanner->findOptionalTag("field:")) {
377 string s = scanner->getValue();
378 string comment;
379 scanner->parseDocumentation(&s, &comment);
380 spec->mFields.push_back(s);
381 spec->mFieldComments.push_back(comment);
382 }
383 if (scanner->findOptionalTag("attrib:")) {
384 spec->mAttrib = scanner->getValue();
385 }
386 }
387 if (scanner->findOptionalTag("enum:")) {
388 spec->mKind = ENUM;
389 spec->mEnumName = scanner->getValue();
390 while (scanner->findOptionalTag("value:")) {
391 string s = scanner->getValue();
392 string comment;
393 scanner->parseDocumentation(&s, &comment);
394 spec->mValues.push_back(s);
395 spec->mValueComments.push_back(comment);
396 }
397 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700398 type->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700399
400 scanner->findTag("end:");
401}
402
403FunctionSpecification::~FunctionSpecification() {
404 for (auto i : mParameters) {
405 delete i;
406 }
407 delete mReturn;
408 for (auto i : mPermutations) {
409 delete i;
410 }
411}
412
413string FunctionSpecification::expandString(string s,
414 int replacementIndexes[MAX_REPLACEABLES]) const {
415 if (mReplaceables.size() > 0) {
416 s = stringReplace(s, "#1", mReplaceables[0][replacementIndexes[0]]);
417 }
418 if (mReplaceables.size() > 1) {
419 s = stringReplace(s, "#2", mReplaceables[1][replacementIndexes[1]]);
420 }
421 if (mReplaceables.size() > 2) {
422 s = stringReplace(s, "#3", mReplaceables[2][replacementIndexes[2]]);
423 }
424 if (mReplaceables.size() > 3) {
425 s = stringReplace(s, "#4", mReplaceables[3][replacementIndexes[3]]);
426 }
427 return s;
428}
429
430void FunctionSpecification::expandStringVector(const vector<string>& in,
431 int replacementIndexes[MAX_REPLACEABLES],
432 vector<string>* out) const {
433 out->clear();
434 for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
435 out->push_back(expandString(*iter, replacementIndexes));
436 }
437}
438
439void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) {
440 int start[MAX_REPLACEABLES];
441 int end[MAX_REPLACEABLES];
442 for (int i = 0; i < MAX_REPLACEABLES; i++) {
443 if (i < (int)mReplaceables.size()) {
444 start[i] = 0;
445 end[i] = mReplaceables[i].size();
446 } else {
447 start[i] = -1;
448 end[i] = 0;
449 }
450 }
451 int replacementIndexes[MAX_REPLACEABLES];
452 // TODO: These loops assume that MAX_REPLACEABLES is 4.
453 for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3];
454 replacementIndexes[3]++) {
455 for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2];
456 replacementIndexes[2]++) {
457 for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1];
458 replacementIndexes[1]++) {
459 for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0];
460 replacementIndexes[0]++) {
461 auto p = new FunctionPermutation(function, this, replacementIndexes, scanner);
462 mPermutations.push_back(p);
463 }
464 }
465 }
466 }
467}
468
469string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const {
470 return expandString(mUnexpandedName, replacementIndexes);
471}
472
473void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES],
474 std::string* retType, int* lineNumber) const {
475 *retType = expandString(mReturn->type, replacementIndexes);
476 *lineNumber = mReturn->lineNumber;
477}
478
479void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES],
480 std::string* type, std::string* name, std::string* testOption,
481 int* lineNumber) const {
482 ParameterEntry* p = mParameters[index];
483 *type = expandString(p->type, replacementIndexes);
484 *name = p->name;
485 *testOption = expandString(p->testOption, replacementIndexes);
486 *lineNumber = p->lineNumber;
487}
488
489void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES],
490 std::vector<std::string>* inlines) const {
491 expandStringVector(mInline, replacementIndexes, inlines);
492}
493
494void FunctionSpecification::parseTest(Scanner* scanner) {
495 const string value = scanner->getValue();
496 if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" ||
497 value == "none") {
498 mTest = value;
499 } else if (value.compare(0, 7, "limited") == 0) {
500 mTest = "limited";
501 if (value.compare(7, 1, "(") == 0) {
502 size_t pParen = value.find(')');
503 if (pParen == string::npos) {
504 scanner->error() << "Incorrect test: \"" << value << "\"\n";
505 } else {
506 mPrecisionLimit = value.substr(8, pParen - 8);
507 }
508 }
509 } else {
510 scanner->error() << "Unrecognized test option: \"" << value << "\"\n";
511 }
512}
513
514bool FunctionSpecification::hasTests(int versionOfTestFiles) const {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700515 if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) {
516 return false;
517 }
518 if (mTest == "none") {
519 return false;
520 }
521 return true;
522}
523
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700524void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile,
525 int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700526 // Some functions like convert have # part of the name. Truncate at that point.
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700527 const string& unexpandedName = scanner->getValue();
528 string name = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700529 size_t p = name.find('#');
530 if (p != string::npos) {
531 if (p > 0 && name[p - 1] == '_') {
532 p--;
533 }
534 name.erase(p);
535 }
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700536 VersionInfo info;
537 if (!info.scan(scanner, maxApiLevel)) {
538 cout << "Skipping some " << name << " definitions.\n";
539 scanner->skipUntilTag("end:");
540 return;
541 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700542
543 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700544 Function* function = systemSpecification.findOrCreateFunction(name, &created);
545 FunctionSpecification* spec = new FunctionSpecification(function);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700546 function->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700547 function->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700548 specFile->addFunctionSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700549
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700550 spec->mUnexpandedName = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700551 spec->mTest = "scalar"; // default
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700552 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700553
554 if (scanner->findOptionalTag("attrib:")) {
555 spec->mAttribute = scanner->getValue();
556 }
557 if (scanner->findOptionalTag("w:")) {
558 vector<string> t;
559 if (scanner->getValue().find("1") != string::npos) {
560 t.push_back("");
561 }
562 if (scanner->getValue().find("2") != string::npos) {
563 t.push_back("2");
564 }
565 if (scanner->getValue().find("3") != string::npos) {
566 t.push_back("3");
567 }
568 if (scanner->getValue().find("4") != string::npos) {
569 t.push_back("4");
570 }
571 spec->mReplaceables.push_back(t);
572 }
573
574 while (scanner->findOptionalTag("t:")) {
575 spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue()));
576 }
577
578 if (scanner->findTag("ret:")) {
579 ParameterEntry* p = scanner->parseArgString(true);
580 function->addReturn(p, scanner);
581 spec->mReturn = p;
582 }
583 while (scanner->findOptionalTag("arg:")) {
584 ParameterEntry* p = scanner->parseArgString(false);
585 function->addParameter(p, scanner);
586 spec->mParameters.push_back(p);
587 }
588
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700589 function->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700590
591 if (scanner->findOptionalTag("inline:")) {
592 scanner->checkNoValue();
593 while (scanner->findOptionalTag("")) {
594 spec->mInline.push_back(scanner->getValue());
595 }
596 }
597 if (scanner->findOptionalTag("test:")) {
598 spec->parseTest(scanner);
599 }
600
601 scanner->findTag("end:");
602
603 spec->createPermutations(function, scanner);
604}
605
606FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec,
607 int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner)
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700608 : mReturn(nullptr), mInputCount(0), mOutputCount(0) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700609 // We expand the strings now to make capitalization easier. The previous code preserved
610 // the #n
611 // markers just before emitting, which made capitalization difficult.
612 mName = spec->getName(replacementIndexes);
613 mNameTrunk = func->getName();
614 mTest = spec->getTest();
615 mPrecisionLimit = spec->getPrecisionLimit();
616 spec->getInlines(replacementIndexes, &mInline);
617
618 mHasFloatAnswers = false;
619 for (size_t i = 0; i < spec->getNumberOfParams(); i++) {
620 string type, name, testOption;
621 int lineNumber = 0;
622 spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber);
623 ParameterDefinition* def = new ParameterDefinition();
624 def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner);
625 if (def->isOutParameter) {
626 mOutputCount++;
627 } else {
628 mInputCount++;
629 }
630
631 if (def->typeIndex < 0 && mTest != "none") {
632 scanner->error(lineNumber)
633 << "Could not find " << def->rsBaseType
634 << " while generating automated tests. Use test: none if not needed.\n";
635 }
636 if (def->isOutParameter && def->isFloatType) {
637 mHasFloatAnswers = true;
638 }
639 mParams.push_back(def);
640 }
641
642 string retType;
643 int lineNumber = 0;
644 spec->getReturn(replacementIndexes, &retType, &lineNumber);
645 if (!retType.empty()) {
646 mReturn = new ParameterDefinition();
647 mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner);
648 if (mReturn->isFloatType) {
649 mHasFloatAnswers = true;
650 }
651 mOutputCount++;
652 }
653}
654
655FunctionPermutation::~FunctionPermutation() {
656 for (auto i : mParams) {
657 delete i;
658 }
659 delete mReturn;
660}
661
662SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) {
663 string core = mSpecFileName;
664 // Remove .spec
665 size_t l = core.length();
666 const char SPEC[] = ".spec";
667 const int SPEC_SIZE = sizeof(SPEC) - 1;
668 const int start = l - SPEC_SIZE;
669 if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) {
670 core.erase(start);
671 }
672
673 // The header file name should have the same base but with a ".rsh" extension.
674 mHeaderFileName = core + ".rsh";
Jean-Luc Brouilletd9935ee2015-04-03 17:27:02 -0700675 mDetailedDocumentationUrl = core + ".html";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700676}
677
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700678void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) {
679 mConstantSpecificationsList.push_back(spec);
680 if (hasDocumentation) {
681 Constant* constant = spec->getConstant();
682 mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700683 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700684}
685
686void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) {
687 mTypeSpecificationsList.push_back(spec);
688 if (hasDocumentation) {
689 Type* type = spec->getType();
690 mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700691 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700692}
693
694void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) {
695 mFunctionSpecificationsList.push_back(spec);
696 if (hasDocumentation) {
697 Function* function = spec->getFunction();
698 mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700699 }
700}
701
702// Read the specification, adding the definitions to the global functions map.
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700703bool SpecFile::readSpecFile(int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700704 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
705 if (!specFile) {
706 cerr << "Error opening input file: " << mSpecFileName << "\n";
707 return false;
708 }
709
710 Scanner scanner(mSpecFileName, specFile);
711
712 // Scan the header that should start the file.
713 scanner.skipBlankEntries();
714 if (scanner.findTag("header:")) {
715 if (scanner.findTag("summary:")) {
716 mBriefDescription = scanner.getValue();
717 }
718 if (scanner.findTag("description:")) {
719 scanner.checkNoValue();
720 while (scanner.findOptionalTag("")) {
721 mFullDescription.push_back(scanner.getValue());
722 }
723 }
724 if (scanner.findOptionalTag("include:")) {
725 scanner.checkNoValue();
726 while (scanner.findOptionalTag("")) {
727 mVerbatimInclude.push_back(scanner.getValue());
728 }
729 }
730 scanner.findTag("end:");
731 }
732
733 while (1) {
734 scanner.skipBlankEntries();
735 if (scanner.atEnd()) {
736 break;
737 }
738 const string tag = scanner.getNextTag();
739 if (tag == "function:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700740 FunctionSpecification::scanFunctionSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700741 } else if (tag == "type:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700742 TypeSpecification::scanTypeSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700743 } else if (tag == "constant:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700744 ConstantSpecification::scanConstantSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700745 } else {
746 scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
747 return false;
748 }
749 }
750
751 fclose(specFile);
752 return scanner.getErrorCount() == 0;
753}
754
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700755SystemSpecification::~SystemSpecification() {
756 for (auto i : mConstants) {
757 delete i.second;
758 }
759 for (auto i : mTypes) {
760 delete i.second;
761 }
762 for (auto i : mFunctions) {
763 delete i.second;
764 }
765 for (auto i : mSpecFiles) {
766 delete i;
767 }
768}
769
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700770// Returns the named entry in the map. Creates it if it's not there.
771template <class T>
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700772T* findOrCreate(const string& name, map<string, T*>* map, bool* created) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700773 auto iter = map->find(name);
774 if (iter != map->end()) {
775 *created = false;
776 return iter->second;
777 }
778 *created = true;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700779 T* f = new T(name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700780 map->insert(pair<string, T*>(name, f));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700781 return f;
782}
783
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700784Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) {
785 return findOrCreate<Constant>(name, &mConstants, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700786}
787
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700788Type* SystemSpecification::findOrCreateType(const string& name, bool* created) {
789 return findOrCreate<Type>(name, &mTypes, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700790}
791
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700792Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) {
793 return findOrCreate<Function>(name, &mFunctions, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700794}
795
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700796bool SystemSpecification::readSpecFile(const string& fileName, int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700797 SpecFile* spec = new SpecFile(fileName);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700798 if (!spec->readSpecFile(maxApiLevel)) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700799 cerr << fileName << ": Failed to parse.\n";
800 return false;
801 }
802 mSpecFiles.push_back(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700803 return true;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700804}
805
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700806bool SystemSpecification::generateFiles(bool forVerification, int maxApiLevel) const {
807 bool success = generateHeaderFiles("scriptc") &&
808 generateDocumentation("docs", forVerification) &&
809 generateTestFiles("test", maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700810 if (success) {
811 cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
812 << " constants, and " << mFunctions.size() << " functions.\n";
813 }
814 return success;
815}
816
817string SystemSpecification::getHtmlAnchor(const string& name) const {
818 Definition* d = nullptr;
819 auto c = mConstants.find(name);
820 if (c != mConstants.end()) {
821 d = c->second;
822 } else {
823 auto t = mTypes.find(name);
824 if (t != mTypes.end()) {
825 d = t->second;
826 } else {
827 auto f = mFunctions.find(name);
828 if (f != mFunctions.end()) {
829 d = f->second;
830 } else {
831 return string();
832 }
833 }
834 }
835 ostringstream stream;
836 stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
837 return stream.str();
838}