blob: f02e42901b669c64ac70429ba3257b77689f423b [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[] = {
Jean-Luc Brouillet6119da92015-04-10 09:36:15 -070038 {"f16", "FLOAT_16", "half", "float", FLOATING_POINT, 11, 5},
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070039 {"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)
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700228 : mName(name), mDeprecatedApiLevel(0), mHidden(false), mFinalVersion(-1) {
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700229}
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:")) {
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700250 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 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700260 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700261 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 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700271 mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + mName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700272 } else if (scanner->findOptionalTag("summary:")) {
273 scanner->error() << "Only the first specification should have a summary.\n";
274 }
275}
276
277Constant::~Constant() {
278 for (auto i : mSpecifications) {
279 delete i;
280 }
281}
282
283Type::~Type() {
284 for (auto i : mSpecifications) {
285 delete i;
286 }
287}
288
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700289Function::Function(const string& name) : Definition(name) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700290 mCapitalizedName = capitalize(mName);
291}
292
293Function::~Function() {
294 for (auto i : mSpecifications) {
295 delete i;
296 }
297}
298
299bool Function::someParametersAreDocumented() const {
300 for (auto p : mParameters) {
301 if (!p->documentation.empty()) {
302 return true;
303 }
304 }
305 return false;
306}
307
308void 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
323void 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
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700333void ConstantSpecification::scanConstantSpecification(Scanner* scanner, SpecFile* specFile,
334 int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700335 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700336 VersionInfo info;
337 if (!info.scan(scanner, maxApiLevel)) {
338 cout << "Skipping some " << name << " definitions.\n";
339 scanner->skipUntilTag("end:");
340 return;
341 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700342
343 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700344 Constant* constant = systemSpecification.findOrCreateConstant(name, &created);
345 ConstantSpecification* spec = new ConstantSpecification(constant);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700346 constant->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700347 constant->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700348 specFile->addConstantSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700349 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700350
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700351 if (scanner->findTag("value:")) {
352 spec->mValue = scanner->getValue();
353 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700354 constant->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700355
356 scanner->findTag("end:");
357}
358
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700359void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile,
360 int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700361 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700362 VersionInfo info;
363 if (!info.scan(scanner, maxApiLevel)) {
364 cout << "Skipping some " << name << " definitions.\n";
365 scanner->skipUntilTag("end:");
366 return;
367 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700368
369 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700370 Type* type = systemSpecification.findOrCreateType(name, &created);
371 TypeSpecification* spec = new TypeSpecification(type);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700372 type->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700373 type->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700374 specFile->addTypeSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700375 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700376
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700377 if (scanner->findOptionalTag("simple:")) {
378 spec->mKind = SIMPLE;
379 spec->mSimpleType = scanner->getValue();
380 }
Stephen Hinesca51c782015-08-25 23:43:34 -0700381 if (scanner->findOptionalTag("rs_object:")) {
382 spec->mKind = RS_OBJECT;
383 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700384 if (scanner->findOptionalTag("struct:")) {
385 spec->mKind = STRUCT;
386 spec->mStructName = scanner->getValue();
387 while (scanner->findOptionalTag("field:")) {
388 string s = scanner->getValue();
389 string comment;
390 scanner->parseDocumentation(&s, &comment);
391 spec->mFields.push_back(s);
392 spec->mFieldComments.push_back(comment);
393 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700394 }
395 if (scanner->findOptionalTag("enum:")) {
396 spec->mKind = ENUM;
397 spec->mEnumName = scanner->getValue();
398 while (scanner->findOptionalTag("value:")) {
399 string s = scanner->getValue();
400 string comment;
401 scanner->parseDocumentation(&s, &comment);
402 spec->mValues.push_back(s);
403 spec->mValueComments.push_back(comment);
404 }
405 }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700406 if (scanner->findOptionalTag("attrib:")) {
407 spec->mAttribute = scanner->getValue();
408 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700409 type->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700410
411 scanner->findTag("end:");
412}
413
414FunctionSpecification::~FunctionSpecification() {
415 for (auto i : mParameters) {
416 delete i;
417 }
418 delete mReturn;
419 for (auto i : mPermutations) {
420 delete i;
421 }
422}
423
424string FunctionSpecification::expandString(string s,
425 int replacementIndexes[MAX_REPLACEABLES]) const {
426 if (mReplaceables.size() > 0) {
427 s = stringReplace(s, "#1", mReplaceables[0][replacementIndexes[0]]);
428 }
429 if (mReplaceables.size() > 1) {
430 s = stringReplace(s, "#2", mReplaceables[1][replacementIndexes[1]]);
431 }
432 if (mReplaceables.size() > 2) {
433 s = stringReplace(s, "#3", mReplaceables[2][replacementIndexes[2]]);
434 }
435 if (mReplaceables.size() > 3) {
436 s = stringReplace(s, "#4", mReplaceables[3][replacementIndexes[3]]);
437 }
438 return s;
439}
440
441void FunctionSpecification::expandStringVector(const vector<string>& in,
442 int replacementIndexes[MAX_REPLACEABLES],
443 vector<string>* out) const {
444 out->clear();
445 for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
446 out->push_back(expandString(*iter, replacementIndexes));
447 }
448}
449
450void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) {
451 int start[MAX_REPLACEABLES];
452 int end[MAX_REPLACEABLES];
453 for (int i = 0; i < MAX_REPLACEABLES; i++) {
454 if (i < (int)mReplaceables.size()) {
455 start[i] = 0;
456 end[i] = mReplaceables[i].size();
457 } else {
458 start[i] = -1;
459 end[i] = 0;
460 }
461 }
462 int replacementIndexes[MAX_REPLACEABLES];
463 // TODO: These loops assume that MAX_REPLACEABLES is 4.
464 for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3];
465 replacementIndexes[3]++) {
466 for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2];
467 replacementIndexes[2]++) {
468 for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1];
469 replacementIndexes[1]++) {
470 for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0];
471 replacementIndexes[0]++) {
472 auto p = new FunctionPermutation(function, this, replacementIndexes, scanner);
473 mPermutations.push_back(p);
474 }
475 }
476 }
477 }
478}
479
480string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const {
481 return expandString(mUnexpandedName, replacementIndexes);
482}
483
484void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES],
485 std::string* retType, int* lineNumber) const {
486 *retType = expandString(mReturn->type, replacementIndexes);
487 *lineNumber = mReturn->lineNumber;
488}
489
490void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES],
491 std::string* type, std::string* name, std::string* testOption,
492 int* lineNumber) const {
493 ParameterEntry* p = mParameters[index];
494 *type = expandString(p->type, replacementIndexes);
495 *name = p->name;
496 *testOption = expandString(p->testOption, replacementIndexes);
497 *lineNumber = p->lineNumber;
498}
499
500void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES],
501 std::vector<std::string>* inlines) const {
502 expandStringVector(mInline, replacementIndexes, inlines);
503}
504
505void FunctionSpecification::parseTest(Scanner* scanner) {
506 const string value = scanner->getValue();
507 if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" ||
508 value == "none") {
509 mTest = value;
510 } else if (value.compare(0, 7, "limited") == 0) {
511 mTest = "limited";
512 if (value.compare(7, 1, "(") == 0) {
513 size_t pParen = value.find(')');
514 if (pParen == string::npos) {
515 scanner->error() << "Incorrect test: \"" << value << "\"\n";
516 } else {
517 mPrecisionLimit = value.substr(8, pParen - 8);
518 }
519 }
520 } else {
521 scanner->error() << "Unrecognized test option: \"" << value << "\"\n";
522 }
523}
524
525bool FunctionSpecification::hasTests(int versionOfTestFiles) const {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700526 if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) {
527 return false;
528 }
529 if (mTest == "none") {
530 return false;
531 }
532 return true;
533}
534
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700535void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile,
536 int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700537 // Some functions like convert have # part of the name. Truncate at that point.
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700538 const string& unexpandedName = scanner->getValue();
539 string name = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700540 size_t p = name.find('#');
541 if (p != string::npos) {
542 if (p > 0 && name[p - 1] == '_') {
543 p--;
544 }
545 name.erase(p);
546 }
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700547 VersionInfo info;
548 if (!info.scan(scanner, maxApiLevel)) {
549 cout << "Skipping some " << name << " definitions.\n";
550 scanner->skipUntilTag("end:");
551 return;
552 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700553
554 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700555 Function* function = systemSpecification.findOrCreateFunction(name, &created);
556 FunctionSpecification* spec = new FunctionSpecification(function);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700557 function->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700558 function->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700559 specFile->addFunctionSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700560
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700561 spec->mUnexpandedName = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700562 spec->mTest = "scalar"; // default
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700563 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700564
565 if (scanner->findOptionalTag("attrib:")) {
566 spec->mAttribute = scanner->getValue();
567 }
568 if (scanner->findOptionalTag("w:")) {
569 vector<string> t;
570 if (scanner->getValue().find("1") != string::npos) {
571 t.push_back("");
572 }
573 if (scanner->getValue().find("2") != string::npos) {
574 t.push_back("2");
575 }
576 if (scanner->getValue().find("3") != string::npos) {
577 t.push_back("3");
578 }
579 if (scanner->getValue().find("4") != string::npos) {
580 t.push_back("4");
581 }
582 spec->mReplaceables.push_back(t);
583 }
584
585 while (scanner->findOptionalTag("t:")) {
586 spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue()));
587 }
588
589 if (scanner->findTag("ret:")) {
590 ParameterEntry* p = scanner->parseArgString(true);
591 function->addReturn(p, scanner);
592 spec->mReturn = p;
593 }
594 while (scanner->findOptionalTag("arg:")) {
595 ParameterEntry* p = scanner->parseArgString(false);
596 function->addParameter(p, scanner);
597 spec->mParameters.push_back(p);
598 }
599
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700600 function->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700601
602 if (scanner->findOptionalTag("inline:")) {
603 scanner->checkNoValue();
604 while (scanner->findOptionalTag("")) {
605 spec->mInline.push_back(scanner->getValue());
606 }
607 }
608 if (scanner->findOptionalTag("test:")) {
609 spec->parseTest(scanner);
610 }
611
612 scanner->findTag("end:");
613
614 spec->createPermutations(function, scanner);
615}
616
617FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec,
618 int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner)
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700619 : mReturn(nullptr), mInputCount(0), mOutputCount(0) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700620 // We expand the strings now to make capitalization easier. The previous code preserved
621 // the #n
622 // markers just before emitting, which made capitalization difficult.
623 mName = spec->getName(replacementIndexes);
624 mNameTrunk = func->getName();
625 mTest = spec->getTest();
626 mPrecisionLimit = spec->getPrecisionLimit();
627 spec->getInlines(replacementIndexes, &mInline);
628
629 mHasFloatAnswers = false;
630 for (size_t i = 0; i < spec->getNumberOfParams(); i++) {
631 string type, name, testOption;
632 int lineNumber = 0;
633 spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber);
634 ParameterDefinition* def = new ParameterDefinition();
635 def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner);
636 if (def->isOutParameter) {
637 mOutputCount++;
638 } else {
639 mInputCount++;
640 }
641
642 if (def->typeIndex < 0 && mTest != "none") {
643 scanner->error(lineNumber)
644 << "Could not find " << def->rsBaseType
645 << " while generating automated tests. Use test: none if not needed.\n";
646 }
647 if (def->isOutParameter && def->isFloatType) {
648 mHasFloatAnswers = true;
649 }
650 mParams.push_back(def);
651 }
652
653 string retType;
654 int lineNumber = 0;
655 spec->getReturn(replacementIndexes, &retType, &lineNumber);
656 if (!retType.empty()) {
657 mReturn = new ParameterDefinition();
658 mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner);
659 if (mReturn->isFloatType) {
660 mHasFloatAnswers = true;
661 }
662 mOutputCount++;
663 }
664}
665
666FunctionPermutation::~FunctionPermutation() {
667 for (auto i : mParams) {
668 delete i;
669 }
670 delete mReturn;
671}
672
673SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) {
674 string core = mSpecFileName;
675 // Remove .spec
676 size_t l = core.length();
677 const char SPEC[] = ".spec";
678 const int SPEC_SIZE = sizeof(SPEC) - 1;
679 const int start = l - SPEC_SIZE;
680 if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) {
681 core.erase(start);
682 }
683
684 // The header file name should have the same base but with a ".rsh" extension.
685 mHeaderFileName = core + ".rsh";
Jean-Luc Brouilletd9935ee2015-04-03 17:27:02 -0700686 mDetailedDocumentationUrl = core + ".html";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700687}
688
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700689void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) {
690 mConstantSpecificationsList.push_back(spec);
691 if (hasDocumentation) {
692 Constant* constant = spec->getConstant();
693 mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700694 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700695}
696
697void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) {
698 mTypeSpecificationsList.push_back(spec);
699 if (hasDocumentation) {
700 Type* type = spec->getType();
701 mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700702 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700703}
704
705void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) {
706 mFunctionSpecificationsList.push_back(spec);
707 if (hasDocumentation) {
708 Function* function = spec->getFunction();
709 mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700710 }
711}
712
713// Read the specification, adding the definitions to the global functions map.
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700714bool SpecFile::readSpecFile(int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700715 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
716 if (!specFile) {
717 cerr << "Error opening input file: " << mSpecFileName << "\n";
718 return false;
719 }
720
721 Scanner scanner(mSpecFileName, specFile);
722
723 // Scan the header that should start the file.
724 scanner.skipBlankEntries();
725 if (scanner.findTag("header:")) {
726 if (scanner.findTag("summary:")) {
727 mBriefDescription = scanner.getValue();
728 }
729 if (scanner.findTag("description:")) {
730 scanner.checkNoValue();
731 while (scanner.findOptionalTag("")) {
732 mFullDescription.push_back(scanner.getValue());
733 }
734 }
735 if (scanner.findOptionalTag("include:")) {
736 scanner.checkNoValue();
737 while (scanner.findOptionalTag("")) {
738 mVerbatimInclude.push_back(scanner.getValue());
739 }
740 }
741 scanner.findTag("end:");
742 }
743
744 while (1) {
745 scanner.skipBlankEntries();
746 if (scanner.atEnd()) {
747 break;
748 }
749 const string tag = scanner.getNextTag();
750 if (tag == "function:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700751 FunctionSpecification::scanFunctionSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700752 } else if (tag == "type:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700753 TypeSpecification::scanTypeSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700754 } else if (tag == "constant:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700755 ConstantSpecification::scanConstantSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700756 } else {
757 scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
758 return false;
759 }
760 }
761
762 fclose(specFile);
763 return scanner.getErrorCount() == 0;
764}
765
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700766SystemSpecification::~SystemSpecification() {
767 for (auto i : mConstants) {
768 delete i.second;
769 }
770 for (auto i : mTypes) {
771 delete i.second;
772 }
773 for (auto i : mFunctions) {
774 delete i.second;
775 }
776 for (auto i : mSpecFiles) {
777 delete i;
778 }
779}
780
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700781// Returns the named entry in the map. Creates it if it's not there.
782template <class T>
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700783T* findOrCreate(const string& name, map<string, T*>* map, bool* created) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700784 auto iter = map->find(name);
785 if (iter != map->end()) {
786 *created = false;
787 return iter->second;
788 }
789 *created = true;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700790 T* f = new T(name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700791 map->insert(pair<string, T*>(name, f));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700792 return f;
793}
794
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700795Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) {
796 return findOrCreate<Constant>(name, &mConstants, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700797}
798
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700799Type* SystemSpecification::findOrCreateType(const string& name, bool* created) {
800 return findOrCreate<Type>(name, &mTypes, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700801}
802
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700803Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) {
804 return findOrCreate<Function>(name, &mFunctions, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700805}
806
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700807bool SystemSpecification::readSpecFile(const string& fileName, int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700808 SpecFile* spec = new SpecFile(fileName);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700809 if (!spec->readSpecFile(maxApiLevel)) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700810 cerr << fileName << ": Failed to parse.\n";
811 return false;
812 }
813 mSpecFiles.push_back(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700814 return true;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700815}
816
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700817
818static void updateMaxApiLevel(const VersionInfo& info, int* maxApiLevel) {
819 *maxApiLevel = max(*maxApiLevel, max(info.minVersion, info.maxVersion));
820}
821
822int SystemSpecification::getMaximumApiLevel() {
823 int maxApiLevel = 0;
824 for (auto i : mConstants) {
825 for (auto j: i.second->getSpecifications()) {
826 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
827 }
828 }
829 for (auto i : mTypes) {
830 for (auto j: i.second->getSpecifications()) {
831 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
832 }
833 }
834 for (auto i : mFunctions) {
835 for (auto j: i.second->getSpecifications()) {
836 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
837 }
838 }
839 return maxApiLevel;
840}
841
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700842bool SystemSpecification::generateFiles(bool forVerification, int maxApiLevel) const {
843 bool success = generateHeaderFiles("scriptc") &&
844 generateDocumentation("docs", forVerification) &&
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700845 generateTestFiles("test", maxApiLevel) &&
846 generateStubsWhiteList("slangtest", maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700847 if (success) {
848 cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
849 << " constants, and " << mFunctions.size() << " functions.\n";
850 }
851 return success;
852}
853
854string SystemSpecification::getHtmlAnchor(const string& name) const {
855 Definition* d = nullptr;
856 auto c = mConstants.find(name);
857 if (c != mConstants.end()) {
858 d = c->second;
859 } else {
860 auto t = mTypes.find(name);
861 if (t != mTypes.end()) {
862 d = t->second;
863 } else {
864 auto f = mFunctions.find(name);
865 if (f != mFunctions.end()) {
866 d = f->second;
867 } else {
868 return string();
869 }
870 }
871 }
872 ostringstream stream;
873 stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
874 return stream.str();
875}