blob: 024b8b6ec8ec0899ae80b1419c92e4fbcff76851 [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.
Yang Ni12398d82015-09-18 14:57:07 -070035const unsigned int MIN_API_LEVEL = 9;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070036
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
Yang Ni12398d82015-09-18 14:57:07 -070053static const char kTagUnreleased[] = "UNRELEASED";
54
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -080055// Patterns that get substituted with C type or RS Data type names in function
56// names, arguments, return types, and inlines.
57static const string kCTypePatterns[] = {"#1", "#2", "#3", "#4"};
58static const string kRSTypePatterns[] = {"#RST_1", "#RST_2", "#RST_3", "#RST_4"};
59
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070060// The singleton of the collected information of all the spec files.
61SystemSpecification systemSpecification;
62
63// Returns the index in TYPES for the provided cType
64static 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 */
76static 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
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800109// Returns true if each entry in typeVector is an RS numerical type
110static 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
Dean De Leo1f088012015-11-25 13:37:05 +0000118void 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
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700139void 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
Dean De Leo1f088012015-11-25 13:37:05 +0000148 getVectorSizeAndBaseType(rsType, mVectorSize, rsBaseType);
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700149 typeIndex = findCType(rsBaseType);
150
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700151 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 } else {
168 variableName = "in";
169 if (specName.empty()) {
170 scanner->error(lineNumber) << "Should have a name.\n";
171 }
172 variableName += capitalize(specName);
173 }
174 rsAllocName = "gAlloc" + capitalize(variableName);
175 javaAllocName = variableName;
176 javaArrayName = "array" + capitalize(javaAllocName);
177
178 // Process the option.
179 undefinedIfOutIsNan = false;
180 compatibleTypeIndex = -1;
181 if (!testOption.empty()) {
182 if (testOption.compare(0, 6, "range(") == 0) {
183 size_t pComma = testOption.find(',');
184 size_t pParen = testOption.find(')');
185 if (pComma == string::npos || pParen == string::npos) {
186 scanner->error(lineNumber) << "Incorrect range " << testOption << "\n";
187 } else {
188 minValue = testOption.substr(6, pComma - 6);
189 maxValue = testOption.substr(pComma + 1, pParen - pComma - 1);
190 }
191 } else if (testOption.compare(0, 6, "above(") == 0) {
192 size_t pParen = testOption.find(')');
193 if (pParen == string::npos) {
194 scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
195 } else {
196 smallerParameter = testOption.substr(6, pParen - 6);
197 }
198 } else if (testOption.compare(0, 11, "compatible(") == 0) {
199 size_t pParen = testOption.find(')');
200 if (pParen == string::npos) {
201 scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
202 } else {
203 compatibleTypeIndex = findCType(testOption.substr(11, pParen - 11));
204 }
205 } else if (testOption.compare(0, 11, "conditional") == 0) {
206 undefinedIfOutIsNan = true;
207 } else {
208 scanner->error(lineNumber) << "Unrecognized testOption " << testOption << "\n";
209 }
210 }
211
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700212 isFloatType = false;
213 if (typeIndex >= 0) {
214 javaBaseType = TYPES[typeIndex].javaType;
215 specType = TYPES[typeIndex].specType;
216 isFloatType = TYPES[typeIndex].exponentBits > 0;
217 }
218 if (!minValue.empty()) {
219 if (typeIndex < 0 || TYPES[typeIndex].kind != FLOATING_POINT) {
220 scanner->error(lineNumber) << "range(,) is only supported for floating point\n";
221 }
222 }
223}
224
Yang Ni12398d82015-09-18 14:57:07 -0700225bool VersionInfo::scan(Scanner* scanner, unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700226 if (scanner->findOptionalTag("version:")) {
227 const string s = scanner->getValue();
Yang Ni12398d82015-09-18 14:57:07 -0700228 if (s.compare(0, sizeof(kTagUnreleased), kTagUnreleased) == 0) {
229 // The API is still under development and does not have
230 // an official version number.
231 minVersion = maxVersion = kUnreleasedVersion;
232 } else {
233 sscanf(s.c_str(), "%u %u", &minVersion, &maxVersion);
234 if (minVersion && minVersion < MIN_API_LEVEL) {
235 scanner->error() << "Minimum version must >= 9\n";
236 }
237 if (minVersion == MIN_API_LEVEL) {
238 minVersion = 0;
239 }
240 if (maxVersion && maxVersion < MIN_API_LEVEL) {
241 scanner->error() << "Maximum version must >= 9\n";
242 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700243 }
244 }
245 if (scanner->findOptionalTag("size:")) {
246 sscanf(scanner->getValue().c_str(), "%i", &intSize);
247 }
Yang Ni12398d82015-09-18 14:57:07 -0700248
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700249 if (maxVersion > maxApiLevel) {
250 maxVersion = maxApiLevel;
251 }
Yang Ni12398d82015-09-18 14:57:07 -0700252
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700253 return minVersion == 0 || minVersion <= maxApiLevel;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700254}
255
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700256Definition::Definition(const std::string& name)
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700257 : mName(name), mDeprecatedApiLevel(0), mHidden(false), mFinalVersion(-1) {
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700258}
259
260void Definition::updateFinalVersion(const VersionInfo& info) {
261 /* We set it if:
262 * - We have never set mFinalVersion before, or
263 * - The max version is 0, which means we have not expired this API, or
264 * - We have a max that's later than what we currently have.
265 */
266 if (mFinalVersion < 0 || info.maxVersion == 0 ||
267 (mFinalVersion > 0 && info.maxVersion > mFinalVersion)) {
268 mFinalVersion = info.maxVersion;
269 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700270}
271
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700272void Definition::scanDocumentationTags(Scanner* scanner, bool firstOccurence,
273 const SpecFile* specFile) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700274 if (scanner->findOptionalTag("hidden:")) {
275 scanner->checkNoValue();
276 mHidden = true;
277 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700278 if (scanner->findOptionalTag("deprecated:")) {
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700279 string value = scanner->getValue();
280 size_t pComma = value.find(", ");
281 if (pComma != string::npos) {
282 mDeprecatedMessage = value.substr(pComma + 2);
283 value.erase(pComma);
284 }
285 sscanf(value.c_str(), "%i", &mDeprecatedApiLevel);
286 if (mDeprecatedApiLevel <= 0) {
287 scanner->error() << "deprecated entries should have a level > 0\n";
288 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700289 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700290 if (firstOccurence) {
291 if (scanner->findTag("summary:")) {
292 mSummary = scanner->getValue();
293 }
294 if (scanner->findTag("description:")) {
295 scanner->checkNoValue();
296 while (scanner->findOptionalTag("")) {
297 mDescription.push_back(scanner->getValue());
298 }
299 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700300 mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + mName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700301 } else if (scanner->findOptionalTag("summary:")) {
302 scanner->error() << "Only the first specification should have a summary.\n";
303 }
304}
305
306Constant::~Constant() {
307 for (auto i : mSpecifications) {
308 delete i;
309 }
310}
311
312Type::~Type() {
313 for (auto i : mSpecifications) {
314 delete i;
315 }
316}
317
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700318Function::Function(const string& name) : Definition(name) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700319 mCapitalizedName = capitalize(mName);
320}
321
322Function::~Function() {
323 for (auto i : mSpecifications) {
324 delete i;
325 }
326}
327
328bool Function::someParametersAreDocumented() const {
329 for (auto p : mParameters) {
330 if (!p->documentation.empty()) {
331 return true;
332 }
333 }
334 return false;
335}
336
337void Function::addParameter(ParameterEntry* entry, Scanner* scanner) {
338 for (auto i : mParameters) {
339 if (i->name == entry->name) {
340 // It's a duplicate.
341 if (!entry->documentation.empty()) {
342 scanner->error(entry->lineNumber)
343 << "Only the first occurence of an arg should have the "
344 "documentation.\n";
345 }
346 return;
347 }
348 }
349 mParameters.push_back(entry);
350}
351
352void Function::addReturn(ParameterEntry* entry, Scanner* scanner) {
353 if (entry->documentation.empty()) {
354 return;
355 }
356 if (!mReturnDocumentation.empty()) {
357 scanner->error() << "ret: should be documented only for the first variant\n";
358 }
359 mReturnDocumentation = entry->documentation;
360}
361
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700362void ConstantSpecification::scanConstantSpecification(Scanner* scanner, SpecFile* specFile,
Yang Ni12398d82015-09-18 14:57:07 -0700363 unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700364 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700365 VersionInfo info;
366 if (!info.scan(scanner, maxApiLevel)) {
367 cout << "Skipping some " << name << " definitions.\n";
368 scanner->skipUntilTag("end:");
369 return;
370 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700371
372 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700373 Constant* constant = systemSpecification.findOrCreateConstant(name, &created);
374 ConstantSpecification* spec = new ConstantSpecification(constant);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700375 constant->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700376 constant->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700377 specFile->addConstantSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700378 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700379
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700380 if (scanner->findTag("value:")) {
381 spec->mValue = scanner->getValue();
382 }
Verena Beckhamef0c4552016-02-19 18:54:43 +0000383 if (scanner->findTag("type:")) {
384 spec->mType = scanner->getValue();
385 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700386 constant->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700387
388 scanner->findTag("end:");
389}
390
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700391void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile,
Yang Ni12398d82015-09-18 14:57:07 -0700392 unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700393 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700394 VersionInfo info;
395 if (!info.scan(scanner, maxApiLevel)) {
396 cout << "Skipping some " << name << " definitions.\n";
397 scanner->skipUntilTag("end:");
398 return;
399 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700400
401 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700402 Type* type = systemSpecification.findOrCreateType(name, &created);
403 TypeSpecification* spec = new TypeSpecification(type);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700404 type->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700405 type->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700406 specFile->addTypeSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700407 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700408
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700409 if (scanner->findOptionalTag("simple:")) {
410 spec->mKind = SIMPLE;
411 spec->mSimpleType = scanner->getValue();
412 }
Stephen Hinesca51c782015-08-25 23:43:34 -0700413 if (scanner->findOptionalTag("rs_object:")) {
414 spec->mKind = RS_OBJECT;
415 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700416 if (scanner->findOptionalTag("struct:")) {
417 spec->mKind = STRUCT;
418 spec->mStructName = scanner->getValue();
419 while (scanner->findOptionalTag("field:")) {
420 string s = scanner->getValue();
421 string comment;
422 scanner->parseDocumentation(&s, &comment);
423 spec->mFields.push_back(s);
424 spec->mFieldComments.push_back(comment);
425 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700426 }
427 if (scanner->findOptionalTag("enum:")) {
428 spec->mKind = ENUM;
429 spec->mEnumName = scanner->getValue();
430 while (scanner->findOptionalTag("value:")) {
431 string s = scanner->getValue();
432 string comment;
433 scanner->parseDocumentation(&s, &comment);
434 spec->mValues.push_back(s);
435 spec->mValueComments.push_back(comment);
436 }
437 }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700438 if (scanner->findOptionalTag("attrib:")) {
439 spec->mAttribute = scanner->getValue();
440 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700441 type->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700442
443 scanner->findTag("end:");
444}
445
446FunctionSpecification::~FunctionSpecification() {
447 for (auto i : mParameters) {
448 delete i;
449 }
450 delete mReturn;
451 for (auto i : mPermutations) {
452 delete i;
453 }
454}
455
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800456string FunctionSpecification::expandRSTypeInString(const string &s,
457 const string &pattern,
458 const string &cTypeStr) const {
459 // Find index of numerical type corresponding to cTypeStr. The case where
460 // pattern is found in s but cTypeStr is not a numerical type is checked in
461 // checkRSTPatternValidity.
462 int typeIdx = findCType(cTypeStr);
463 if (typeIdx == -1) {
464 return s;
465 }
466 // If index exists, perform replacement.
467 return stringReplace(s, pattern, TYPES[typeIdx].rsDataType);
468}
469
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700470string FunctionSpecification::expandString(string s,
471 int replacementIndexes[MAX_REPLACEABLES]) const {
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800472
473
474 for (unsigned idx = 0; idx < mReplaceables.size(); idx ++) {
475 string toString = mReplaceables[idx][replacementIndexes[idx]];
476
477 // replace #RST_i patterns with RS datatype corresponding to toString
478 s = expandRSTypeInString(s, kRSTypePatterns[idx], toString);
479
480 // replace #i patterns with C type from mReplaceables
481 s = stringReplace(s, kCTypePatterns[idx], toString);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700482 }
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800483
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700484 return s;
485}
486
487void FunctionSpecification::expandStringVector(const vector<string>& in,
488 int replacementIndexes[MAX_REPLACEABLES],
489 vector<string>* out) const {
490 out->clear();
491 for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
492 out->push_back(expandString(*iter, replacementIndexes));
493 }
494}
495
496void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) {
497 int start[MAX_REPLACEABLES];
498 int end[MAX_REPLACEABLES];
499 for (int i = 0; i < MAX_REPLACEABLES; i++) {
500 if (i < (int)mReplaceables.size()) {
501 start[i] = 0;
502 end[i] = mReplaceables[i].size();
503 } else {
504 start[i] = -1;
505 end[i] = 0;
506 }
507 }
508 int replacementIndexes[MAX_REPLACEABLES];
509 // TODO: These loops assume that MAX_REPLACEABLES is 4.
510 for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3];
511 replacementIndexes[3]++) {
512 for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2];
513 replacementIndexes[2]++) {
514 for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1];
515 replacementIndexes[1]++) {
516 for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0];
517 replacementIndexes[0]++) {
518 auto p = new FunctionPermutation(function, this, replacementIndexes, scanner);
519 mPermutations.push_back(p);
520 }
521 }
522 }
523 }
524}
525
526string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const {
527 return expandString(mUnexpandedName, replacementIndexes);
528}
529
530void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES],
531 std::string* retType, int* lineNumber) const {
532 *retType = expandString(mReturn->type, replacementIndexes);
533 *lineNumber = mReturn->lineNumber;
534}
535
536void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES],
537 std::string* type, std::string* name, std::string* testOption,
538 int* lineNumber) const {
539 ParameterEntry* p = mParameters[index];
540 *type = expandString(p->type, replacementIndexes);
541 *name = p->name;
542 *testOption = expandString(p->testOption, replacementIndexes);
543 *lineNumber = p->lineNumber;
544}
545
546void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES],
547 std::vector<std::string>* inlines) const {
548 expandStringVector(mInline, replacementIndexes, inlines);
549}
550
551void FunctionSpecification::parseTest(Scanner* scanner) {
552 const string value = scanner->getValue();
553 if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" ||
554 value == "none") {
555 mTest = value;
556 } else if (value.compare(0, 7, "limited") == 0) {
557 mTest = "limited";
558 if (value.compare(7, 1, "(") == 0) {
559 size_t pParen = value.find(')');
560 if (pParen == string::npos) {
561 scanner->error() << "Incorrect test: \"" << value << "\"\n";
562 } else {
563 mPrecisionLimit = value.substr(8, pParen - 8);
564 }
565 }
566 } else {
567 scanner->error() << "Unrecognized test option: \"" << value << "\"\n";
568 }
569}
570
Yang Ni12398d82015-09-18 14:57:07 -0700571bool FunctionSpecification::hasTests(unsigned int versionOfTestFiles) const {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700572 if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) {
573 return false;
574 }
575 if (mTest == "none") {
576 return false;
577 }
578 return true;
579}
580
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800581void FunctionSpecification::checkRSTPatternValidity(const string &inlineStr, bool allow,
582 Scanner *scanner) {
583 for (int i = 0; i < MAX_REPLACEABLES; i ++) {
584 bool patternFound = inlineStr.find(kRSTypePatterns[i]) != string::npos;
585
586 if (patternFound) {
587 if (!allow) {
588 scanner->error() << "RST_i pattern not allowed here\n";
589 }
590 else if (mIsRSTAllowed[i] == false) {
591 scanner->error() << "Found pattern \"" << kRSTypePatterns[i]
592 << "\" in spec. But some entry in the corresponding"
593 << " parameter list cannot be translated to an RS type\n";
594 }
595 }
596 }
597}
598
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700599void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile,
Yang Ni12398d82015-09-18 14:57:07 -0700600 unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700601 // Some functions like convert have # part of the name. Truncate at that point.
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700602 const string& unexpandedName = scanner->getValue();
603 string name = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700604 size_t p = name.find('#');
605 if (p != string::npos) {
606 if (p > 0 && name[p - 1] == '_') {
607 p--;
608 }
609 name.erase(p);
610 }
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700611 VersionInfo info;
612 if (!info.scan(scanner, maxApiLevel)) {
613 cout << "Skipping some " << name << " definitions.\n";
614 scanner->skipUntilTag("end:");
615 return;
616 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700617
618 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700619 Function* function = systemSpecification.findOrCreateFunction(name, &created);
620 FunctionSpecification* spec = new FunctionSpecification(function);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700621 function->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700622 function->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700623 specFile->addFunctionSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700624
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700625 spec->mUnexpandedName = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700626 spec->mTest = "scalar"; // default
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700627 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700628
Yang Ni12398d82015-09-18 14:57:07 -0700629 if (scanner->findOptionalTag("internal:")) {
630 spec->mInternal = (scanner->getValue() == "true");
631 }
632 if (scanner->findOptionalTag("intrinsic:")) {
633 spec->mIntrinsic = (scanner->getValue() == "true");
634 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700635 if (scanner->findOptionalTag("attrib:")) {
636 spec->mAttribute = scanner->getValue();
637 }
638 if (scanner->findOptionalTag("w:")) {
639 vector<string> t;
640 if (scanner->getValue().find("1") != string::npos) {
641 t.push_back("");
642 }
643 if (scanner->getValue().find("2") != string::npos) {
644 t.push_back("2");
645 }
646 if (scanner->getValue().find("3") != string::npos) {
647 t.push_back("3");
648 }
649 if (scanner->getValue().find("4") != string::npos) {
650 t.push_back("4");
651 }
652 spec->mReplaceables.push_back(t);
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800653 // RST_i pattern not applicable for width.
654 spec->mIsRSTAllowed.push_back(false);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700655 }
656
657 while (scanner->findOptionalTag("t:")) {
658 spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue()));
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800659 spec->mIsRSTAllowed.push_back(isRSTValid(spec->mReplaceables.back()));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700660 }
661
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800662 // Disallow RST_* pattern in function name
663 // FIXME the line number for this error would be wrong
664 spec->checkRSTPatternValidity(unexpandedName, false, scanner);
665
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700666 if (scanner->findTag("ret:")) {
667 ParameterEntry* p = scanner->parseArgString(true);
668 function->addReturn(p, scanner);
669 spec->mReturn = p;
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800670
671 // Disallow RST_* pattern in return type
672 spec->checkRSTPatternValidity(p->type, false, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700673 }
674 while (scanner->findOptionalTag("arg:")) {
675 ParameterEntry* p = scanner->parseArgString(false);
676 function->addParameter(p, scanner);
677 spec->mParameters.push_back(p);
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800678
679 // Disallow RST_* pattern in parameter type or testOption
680 spec->checkRSTPatternValidity(p->type, false, scanner);
681 spec->checkRSTPatternValidity(p->testOption, false, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700682 }
683
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700684 function->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700685
686 if (scanner->findOptionalTag("inline:")) {
687 scanner->checkNoValue();
688 while (scanner->findOptionalTag("")) {
689 spec->mInline.push_back(scanner->getValue());
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800690
691 // Allow RST_* pattern in inline definitions
692 spec->checkRSTPatternValidity(spec->mInline.back(), true, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700693 }
694 }
695 if (scanner->findOptionalTag("test:")) {
696 spec->parseTest(scanner);
697 }
698
699 scanner->findTag("end:");
700
701 spec->createPermutations(function, scanner);
702}
703
704FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec,
705 int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner)
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700706 : mReturn(nullptr), mInputCount(0), mOutputCount(0) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700707 // We expand the strings now to make capitalization easier. The previous code preserved
708 // the #n
709 // markers just before emitting, which made capitalization difficult.
710 mName = spec->getName(replacementIndexes);
711 mNameTrunk = func->getName();
712 mTest = spec->getTest();
713 mPrecisionLimit = spec->getPrecisionLimit();
714 spec->getInlines(replacementIndexes, &mInline);
715
716 mHasFloatAnswers = false;
717 for (size_t i = 0; i < spec->getNumberOfParams(); i++) {
718 string type, name, testOption;
719 int lineNumber = 0;
720 spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber);
721 ParameterDefinition* def = new ParameterDefinition();
722 def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner);
723 if (def->isOutParameter) {
724 mOutputCount++;
725 } else {
726 mInputCount++;
727 }
728
729 if (def->typeIndex < 0 && mTest != "none") {
730 scanner->error(lineNumber)
731 << "Could not find " << def->rsBaseType
732 << " while generating automated tests. Use test: none if not needed.\n";
733 }
734 if (def->isOutParameter && def->isFloatType) {
735 mHasFloatAnswers = true;
736 }
737 mParams.push_back(def);
738 }
739
740 string retType;
741 int lineNumber = 0;
742 spec->getReturn(replacementIndexes, &retType, &lineNumber);
743 if (!retType.empty()) {
744 mReturn = new ParameterDefinition();
745 mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner);
746 if (mReturn->isFloatType) {
747 mHasFloatAnswers = true;
748 }
749 mOutputCount++;
750 }
751}
752
753FunctionPermutation::~FunctionPermutation() {
754 for (auto i : mParams) {
755 delete i;
756 }
757 delete mReturn;
758}
759
760SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) {
761 string core = mSpecFileName;
762 // Remove .spec
763 size_t l = core.length();
764 const char SPEC[] = ".spec";
765 const int SPEC_SIZE = sizeof(SPEC) - 1;
766 const int start = l - SPEC_SIZE;
767 if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) {
768 core.erase(start);
769 }
770
771 // The header file name should have the same base but with a ".rsh" extension.
772 mHeaderFileName = core + ".rsh";
Jean-Luc Brouilletd9935ee2015-04-03 17:27:02 -0700773 mDetailedDocumentationUrl = core + ".html";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700774}
775
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700776void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) {
777 mConstantSpecificationsList.push_back(spec);
778 if (hasDocumentation) {
779 Constant* constant = spec->getConstant();
780 mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700781 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700782}
783
784void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) {
785 mTypeSpecificationsList.push_back(spec);
786 if (hasDocumentation) {
787 Type* type = spec->getType();
788 mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700789 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700790}
791
792void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) {
793 mFunctionSpecificationsList.push_back(spec);
794 if (hasDocumentation) {
795 Function* function = spec->getFunction();
796 mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700797 }
798}
799
800// Read the specification, adding the definitions to the global functions map.
Yang Ni12398d82015-09-18 14:57:07 -0700801bool SpecFile::readSpecFile(unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700802 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
803 if (!specFile) {
804 cerr << "Error opening input file: " << mSpecFileName << "\n";
805 return false;
806 }
807
808 Scanner scanner(mSpecFileName, specFile);
809
810 // Scan the header that should start the file.
811 scanner.skipBlankEntries();
812 if (scanner.findTag("header:")) {
813 if (scanner.findTag("summary:")) {
814 mBriefDescription = scanner.getValue();
815 }
816 if (scanner.findTag("description:")) {
817 scanner.checkNoValue();
818 while (scanner.findOptionalTag("")) {
819 mFullDescription.push_back(scanner.getValue());
820 }
821 }
822 if (scanner.findOptionalTag("include:")) {
823 scanner.checkNoValue();
824 while (scanner.findOptionalTag("")) {
825 mVerbatimInclude.push_back(scanner.getValue());
826 }
827 }
828 scanner.findTag("end:");
829 }
830
831 while (1) {
832 scanner.skipBlankEntries();
833 if (scanner.atEnd()) {
834 break;
835 }
836 const string tag = scanner.getNextTag();
837 if (tag == "function:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700838 FunctionSpecification::scanFunctionSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700839 } else if (tag == "type:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700840 TypeSpecification::scanTypeSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700841 } else if (tag == "constant:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700842 ConstantSpecification::scanConstantSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700843 } else {
844 scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
845 return false;
846 }
847 }
848
849 fclose(specFile);
850 return scanner.getErrorCount() == 0;
851}
852
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700853SystemSpecification::~SystemSpecification() {
854 for (auto i : mConstants) {
855 delete i.second;
856 }
857 for (auto i : mTypes) {
858 delete i.second;
859 }
860 for (auto i : mFunctions) {
861 delete i.second;
862 }
863 for (auto i : mSpecFiles) {
864 delete i;
865 }
866}
867
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700868// Returns the named entry in the map. Creates it if it's not there.
869template <class T>
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700870T* findOrCreate(const string& name, map<string, T*>* map, bool* created) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700871 auto iter = map->find(name);
872 if (iter != map->end()) {
873 *created = false;
874 return iter->second;
875 }
876 *created = true;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700877 T* f = new T(name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700878 map->insert(pair<string, T*>(name, f));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700879 return f;
880}
881
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700882Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) {
883 return findOrCreate<Constant>(name, &mConstants, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700884}
885
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700886Type* SystemSpecification::findOrCreateType(const string& name, bool* created) {
887 return findOrCreate<Type>(name, &mTypes, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700888}
889
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700890Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) {
891 return findOrCreate<Function>(name, &mFunctions, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700892}
893
Yang Ni12398d82015-09-18 14:57:07 -0700894bool SystemSpecification::readSpecFile(const string& fileName, unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700895 SpecFile* spec = new SpecFile(fileName);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700896 if (!spec->readSpecFile(maxApiLevel)) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700897 cerr << fileName << ": Failed to parse.\n";
898 return false;
899 }
900 mSpecFiles.push_back(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700901 return true;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700902}
903
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700904
Yang Ni12398d82015-09-18 14:57:07 -0700905static void updateMaxApiLevel(const VersionInfo& info, unsigned int* maxApiLevel) {
906 if (info.minVersion == VersionInfo::kUnreleasedVersion) {
907 // Ignore development API level in consideration of max API level.
908 return;
909 }
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700910 *maxApiLevel = max(*maxApiLevel, max(info.minVersion, info.maxVersion));
911}
912
Yang Ni12398d82015-09-18 14:57:07 -0700913unsigned int SystemSpecification::getMaximumApiLevel() {
914 unsigned int maxApiLevel = 0;
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700915 for (auto i : mConstants) {
916 for (auto j: i.second->getSpecifications()) {
917 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
918 }
919 }
920 for (auto i : mTypes) {
921 for (auto j: i.second->getSpecifications()) {
922 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
923 }
924 }
925 for (auto i : mFunctions) {
926 for (auto j: i.second->getSpecifications()) {
927 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
928 }
929 }
930 return maxApiLevel;
931}
932
Yang Ni12398d82015-09-18 14:57:07 -0700933bool SystemSpecification::generateFiles(bool forVerification, unsigned int maxApiLevel) const {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700934 bool success = generateHeaderFiles("scriptc") &&
935 generateDocumentation("docs", forVerification) &&
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700936 generateTestFiles("test", maxApiLevel) &&
937 generateStubsWhiteList("slangtest", maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700938 if (success) {
939 cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
940 << " constants, and " << mFunctions.size() << " functions.\n";
941 }
942 return success;
943}
944
945string SystemSpecification::getHtmlAnchor(const string& name) const {
946 Definition* d = nullptr;
947 auto c = mConstants.find(name);
948 if (c != mConstants.end()) {
949 d = c->second;
950 } else {
951 auto t = mTypes.find(name);
952 if (t != mTypes.end()) {
953 d = t->second;
954 } else {
955 auto f = mFunctions.find(name);
956 if (f != mFunctions.end()) {
957 d = f->second;
958 } else {
959 return string();
960 }
961 }
962 }
963 ostringstream stream;
964 stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
965 return stream.str();
966}