blob: 785f358ba53394639ed7d0bcea87ddd5544c830b [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
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700118void ParameterDefinition::parseParameterDefinition(const string& type, const string& name,
119 const string& testOption, int lineNumber,
120 bool isReturn, Scanner* scanner) {
121 rsType = type;
122 specName = name;
123
124 // Determine if this is an output.
125 isOutParameter = isReturn || charRemoved('*', &rsType);
126
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700127 rsBaseType = rsType;
128 mVectorSize = "1";
129 /* If it's a vector type, we need to split the base type from the size.
130 * We know that's it's a vector type if the last character is a digit and
131 * the rest is an actual base type. We used to only verify the first part,
132 * which created a problem with rs_matrix2x2.
133 */
134 const int last = rsType.size() - 1;
135 const char lastChar = rsType[last];
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700136 if (lastChar >= '0' && lastChar <= '9') {
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700137 const string trimmed = rsType.substr(0, last);
138 int i = findCType(trimmed);
139 if (i >= 0) {
140 rsBaseType = trimmed;
141 mVectorSize = lastChar;
142 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700143 }
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700144 typeIndex = findCType(rsBaseType);
145
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700146 if (mVectorSize == "3") {
147 vectorWidth = "4";
148 } else {
149 vectorWidth = mVectorSize;
150 }
151
152 /* Create variable names to be used in the java and .rs files. Because x and
153 * y are reserved in .rs files, we prefix variable names with "in" or "out".
154 */
155 if (isOutParameter) {
156 variableName = "out";
157 if (!specName.empty()) {
158 variableName += capitalize(specName);
159 } else if (!isReturn) {
160 scanner->error(lineNumber) << "Should have a name.\n";
161 }
162 } else {
163 variableName = "in";
164 if (specName.empty()) {
165 scanner->error(lineNumber) << "Should have a name.\n";
166 }
167 variableName += capitalize(specName);
168 }
169 rsAllocName = "gAlloc" + capitalize(variableName);
170 javaAllocName = variableName;
171 javaArrayName = "array" + capitalize(javaAllocName);
172
173 // Process the option.
174 undefinedIfOutIsNan = false;
175 compatibleTypeIndex = -1;
176 if (!testOption.empty()) {
177 if (testOption.compare(0, 6, "range(") == 0) {
178 size_t pComma = testOption.find(',');
179 size_t pParen = testOption.find(')');
180 if (pComma == string::npos || pParen == string::npos) {
181 scanner->error(lineNumber) << "Incorrect range " << testOption << "\n";
182 } else {
183 minValue = testOption.substr(6, pComma - 6);
184 maxValue = testOption.substr(pComma + 1, pParen - pComma - 1);
185 }
186 } else if (testOption.compare(0, 6, "above(") == 0) {
187 size_t pParen = testOption.find(')');
188 if (pParen == string::npos) {
189 scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
190 } else {
191 smallerParameter = testOption.substr(6, pParen - 6);
192 }
193 } else if (testOption.compare(0, 11, "compatible(") == 0) {
194 size_t pParen = testOption.find(')');
195 if (pParen == string::npos) {
196 scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
197 } else {
198 compatibleTypeIndex = findCType(testOption.substr(11, pParen - 11));
199 }
200 } else if (testOption.compare(0, 11, "conditional") == 0) {
201 undefinedIfOutIsNan = true;
202 } else {
203 scanner->error(lineNumber) << "Unrecognized testOption " << testOption << "\n";
204 }
205 }
206
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700207 isFloatType = false;
208 if (typeIndex >= 0) {
209 javaBaseType = TYPES[typeIndex].javaType;
210 specType = TYPES[typeIndex].specType;
211 isFloatType = TYPES[typeIndex].exponentBits > 0;
212 }
213 if (!minValue.empty()) {
214 if (typeIndex < 0 || TYPES[typeIndex].kind != FLOATING_POINT) {
215 scanner->error(lineNumber) << "range(,) is only supported for floating point\n";
216 }
217 }
218}
219
Yang Ni12398d82015-09-18 14:57:07 -0700220bool VersionInfo::scan(Scanner* scanner, unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700221 if (scanner->findOptionalTag("version:")) {
222 const string s = scanner->getValue();
Yang Ni12398d82015-09-18 14:57:07 -0700223 if (s.compare(0, sizeof(kTagUnreleased), kTagUnreleased) == 0) {
224 // The API is still under development and does not have
225 // an official version number.
226 minVersion = maxVersion = kUnreleasedVersion;
227 } else {
228 sscanf(s.c_str(), "%u %u", &minVersion, &maxVersion);
229 if (minVersion && minVersion < MIN_API_LEVEL) {
230 scanner->error() << "Minimum version must >= 9\n";
231 }
232 if (minVersion == MIN_API_LEVEL) {
233 minVersion = 0;
234 }
235 if (maxVersion && maxVersion < MIN_API_LEVEL) {
236 scanner->error() << "Maximum version must >= 9\n";
237 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700238 }
239 }
240 if (scanner->findOptionalTag("size:")) {
241 sscanf(scanner->getValue().c_str(), "%i", &intSize);
242 }
Yang Ni12398d82015-09-18 14:57:07 -0700243
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700244 if (maxVersion > maxApiLevel) {
245 maxVersion = maxApiLevel;
246 }
Yang Ni12398d82015-09-18 14:57:07 -0700247
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700248 return minVersion == 0 || minVersion <= maxApiLevel;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700249}
250
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700251Definition::Definition(const std::string& name)
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700252 : mName(name), mDeprecatedApiLevel(0), mHidden(false), mFinalVersion(-1) {
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700253}
254
255void Definition::updateFinalVersion(const VersionInfo& info) {
256 /* We set it if:
257 * - We have never set mFinalVersion before, or
258 * - The max version is 0, which means we have not expired this API, or
259 * - We have a max that's later than what we currently have.
260 */
261 if (mFinalVersion < 0 || info.maxVersion == 0 ||
262 (mFinalVersion > 0 && info.maxVersion > mFinalVersion)) {
263 mFinalVersion = info.maxVersion;
264 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700265}
266
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700267void Definition::scanDocumentationTags(Scanner* scanner, bool firstOccurence,
268 const SpecFile* specFile) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700269 if (scanner->findOptionalTag("hidden:")) {
270 scanner->checkNoValue();
271 mHidden = true;
272 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700273 if (scanner->findOptionalTag("deprecated:")) {
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700274 string value = scanner->getValue();
275 size_t pComma = value.find(", ");
276 if (pComma != string::npos) {
277 mDeprecatedMessage = value.substr(pComma + 2);
278 value.erase(pComma);
279 }
280 sscanf(value.c_str(), "%i", &mDeprecatedApiLevel);
281 if (mDeprecatedApiLevel <= 0) {
282 scanner->error() << "deprecated entries should have a level > 0\n";
283 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700284 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700285 if (firstOccurence) {
286 if (scanner->findTag("summary:")) {
287 mSummary = scanner->getValue();
288 }
289 if (scanner->findTag("description:")) {
290 scanner->checkNoValue();
291 while (scanner->findOptionalTag("")) {
292 mDescription.push_back(scanner->getValue());
293 }
294 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700295 mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + mName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700296 } else if (scanner->findOptionalTag("summary:")) {
297 scanner->error() << "Only the first specification should have a summary.\n";
298 }
299}
300
301Constant::~Constant() {
302 for (auto i : mSpecifications) {
303 delete i;
304 }
305}
306
307Type::~Type() {
308 for (auto i : mSpecifications) {
309 delete i;
310 }
311}
312
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700313Function::Function(const string& name) : Definition(name) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700314 mCapitalizedName = capitalize(mName);
315}
316
317Function::~Function() {
318 for (auto i : mSpecifications) {
319 delete i;
320 }
321}
322
323bool Function::someParametersAreDocumented() const {
324 for (auto p : mParameters) {
325 if (!p->documentation.empty()) {
326 return true;
327 }
328 }
329 return false;
330}
331
332void Function::addParameter(ParameterEntry* entry, Scanner* scanner) {
333 for (auto i : mParameters) {
334 if (i->name == entry->name) {
335 // It's a duplicate.
336 if (!entry->documentation.empty()) {
337 scanner->error(entry->lineNumber)
338 << "Only the first occurence of an arg should have the "
339 "documentation.\n";
340 }
341 return;
342 }
343 }
344 mParameters.push_back(entry);
345}
346
347void Function::addReturn(ParameterEntry* entry, Scanner* scanner) {
348 if (entry->documentation.empty()) {
349 return;
350 }
351 if (!mReturnDocumentation.empty()) {
352 scanner->error() << "ret: should be documented only for the first variant\n";
353 }
354 mReturnDocumentation = entry->documentation;
355}
356
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700357void ConstantSpecification::scanConstantSpecification(Scanner* scanner, SpecFile* specFile,
Yang Ni12398d82015-09-18 14:57:07 -0700358 unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700359 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700360 VersionInfo info;
361 if (!info.scan(scanner, maxApiLevel)) {
362 cout << "Skipping some " << name << " definitions.\n";
363 scanner->skipUntilTag("end:");
364 return;
365 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700366
367 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700368 Constant* constant = systemSpecification.findOrCreateConstant(name, &created);
369 ConstantSpecification* spec = new ConstantSpecification(constant);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700370 constant->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700371 constant->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700372 specFile->addConstantSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700373 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700374
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700375 if (scanner->findTag("value:")) {
376 spec->mValue = scanner->getValue();
377 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700378 constant->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700379
380 scanner->findTag("end:");
381}
382
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700383void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile,
Yang Ni12398d82015-09-18 14:57:07 -0700384 unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700385 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700386 VersionInfo info;
387 if (!info.scan(scanner, maxApiLevel)) {
388 cout << "Skipping some " << name << " definitions.\n";
389 scanner->skipUntilTag("end:");
390 return;
391 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700392
393 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700394 Type* type = systemSpecification.findOrCreateType(name, &created);
395 TypeSpecification* spec = new TypeSpecification(type);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700396 type->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700397 type->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700398 specFile->addTypeSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700399 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700400
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700401 if (scanner->findOptionalTag("simple:")) {
402 spec->mKind = SIMPLE;
403 spec->mSimpleType = scanner->getValue();
404 }
Stephen Hinesca51c782015-08-25 23:43:34 -0700405 if (scanner->findOptionalTag("rs_object:")) {
406 spec->mKind = RS_OBJECT;
407 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700408 if (scanner->findOptionalTag("struct:")) {
409 spec->mKind = STRUCT;
410 spec->mStructName = scanner->getValue();
411 while (scanner->findOptionalTag("field:")) {
412 string s = scanner->getValue();
413 string comment;
414 scanner->parseDocumentation(&s, &comment);
415 spec->mFields.push_back(s);
416 spec->mFieldComments.push_back(comment);
417 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700418 }
419 if (scanner->findOptionalTag("enum:")) {
420 spec->mKind = ENUM;
421 spec->mEnumName = scanner->getValue();
422 while (scanner->findOptionalTag("value:")) {
423 string s = scanner->getValue();
424 string comment;
425 scanner->parseDocumentation(&s, &comment);
426 spec->mValues.push_back(s);
427 spec->mValueComments.push_back(comment);
428 }
429 }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700430 if (scanner->findOptionalTag("attrib:")) {
431 spec->mAttribute = scanner->getValue();
432 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700433 type->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700434
435 scanner->findTag("end:");
436}
437
438FunctionSpecification::~FunctionSpecification() {
439 for (auto i : mParameters) {
440 delete i;
441 }
442 delete mReturn;
443 for (auto i : mPermutations) {
444 delete i;
445 }
446}
447
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800448string FunctionSpecification::expandRSTypeInString(const string &s,
449 const string &pattern,
450 const string &cTypeStr) const {
451 // Find index of numerical type corresponding to cTypeStr. The case where
452 // pattern is found in s but cTypeStr is not a numerical type is checked in
453 // checkRSTPatternValidity.
454 int typeIdx = findCType(cTypeStr);
455 if (typeIdx == -1) {
456 return s;
457 }
458 // If index exists, perform replacement.
459 return stringReplace(s, pattern, TYPES[typeIdx].rsDataType);
460}
461
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700462string FunctionSpecification::expandString(string s,
463 int replacementIndexes[MAX_REPLACEABLES]) const {
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800464
465
466 for (unsigned idx = 0; idx < mReplaceables.size(); idx ++) {
467 string toString = mReplaceables[idx][replacementIndexes[idx]];
468
469 // replace #RST_i patterns with RS datatype corresponding to toString
470 s = expandRSTypeInString(s, kRSTypePatterns[idx], toString);
471
472 // replace #i patterns with C type from mReplaceables
473 s = stringReplace(s, kCTypePatterns[idx], toString);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700474 }
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800475
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700476 return s;
477}
478
479void FunctionSpecification::expandStringVector(const vector<string>& in,
480 int replacementIndexes[MAX_REPLACEABLES],
481 vector<string>* out) const {
482 out->clear();
483 for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
484 out->push_back(expandString(*iter, replacementIndexes));
485 }
486}
487
488void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) {
489 int start[MAX_REPLACEABLES];
490 int end[MAX_REPLACEABLES];
491 for (int i = 0; i < MAX_REPLACEABLES; i++) {
492 if (i < (int)mReplaceables.size()) {
493 start[i] = 0;
494 end[i] = mReplaceables[i].size();
495 } else {
496 start[i] = -1;
497 end[i] = 0;
498 }
499 }
500 int replacementIndexes[MAX_REPLACEABLES];
501 // TODO: These loops assume that MAX_REPLACEABLES is 4.
502 for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3];
503 replacementIndexes[3]++) {
504 for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2];
505 replacementIndexes[2]++) {
506 for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1];
507 replacementIndexes[1]++) {
508 for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0];
509 replacementIndexes[0]++) {
510 auto p = new FunctionPermutation(function, this, replacementIndexes, scanner);
511 mPermutations.push_back(p);
512 }
513 }
514 }
515 }
516}
517
518string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const {
519 return expandString(mUnexpandedName, replacementIndexes);
520}
521
522void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES],
523 std::string* retType, int* lineNumber) const {
524 *retType = expandString(mReturn->type, replacementIndexes);
525 *lineNumber = mReturn->lineNumber;
526}
527
528void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES],
529 std::string* type, std::string* name, std::string* testOption,
530 int* lineNumber) const {
531 ParameterEntry* p = mParameters[index];
532 *type = expandString(p->type, replacementIndexes);
533 *name = p->name;
534 *testOption = expandString(p->testOption, replacementIndexes);
535 *lineNumber = p->lineNumber;
536}
537
538void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES],
539 std::vector<std::string>* inlines) const {
540 expandStringVector(mInline, replacementIndexes, inlines);
541}
542
543void FunctionSpecification::parseTest(Scanner* scanner) {
544 const string value = scanner->getValue();
545 if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" ||
546 value == "none") {
547 mTest = value;
548 } else if (value.compare(0, 7, "limited") == 0) {
549 mTest = "limited";
550 if (value.compare(7, 1, "(") == 0) {
551 size_t pParen = value.find(')');
552 if (pParen == string::npos) {
553 scanner->error() << "Incorrect test: \"" << value << "\"\n";
554 } else {
555 mPrecisionLimit = value.substr(8, pParen - 8);
556 }
557 }
558 } else {
559 scanner->error() << "Unrecognized test option: \"" << value << "\"\n";
560 }
561}
562
Yang Ni12398d82015-09-18 14:57:07 -0700563bool FunctionSpecification::hasTests(unsigned int versionOfTestFiles) const {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700564 if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) {
565 return false;
566 }
567 if (mTest == "none") {
568 return false;
569 }
570 return true;
571}
572
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800573void FunctionSpecification::checkRSTPatternValidity(const string &inlineStr, bool allow,
574 Scanner *scanner) {
575 for (int i = 0; i < MAX_REPLACEABLES; i ++) {
576 bool patternFound = inlineStr.find(kRSTypePatterns[i]) != string::npos;
577
578 if (patternFound) {
579 if (!allow) {
580 scanner->error() << "RST_i pattern not allowed here\n";
581 }
582 else if (mIsRSTAllowed[i] == false) {
583 scanner->error() << "Found pattern \"" << kRSTypePatterns[i]
584 << "\" in spec. But some entry in the corresponding"
585 << " parameter list cannot be translated to an RS type\n";
586 }
587 }
588 }
589}
590
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700591void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile,
Yang Ni12398d82015-09-18 14:57:07 -0700592 unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700593 // Some functions like convert have # part of the name. Truncate at that point.
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700594 const string& unexpandedName = scanner->getValue();
595 string name = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700596 size_t p = name.find('#');
597 if (p != string::npos) {
598 if (p > 0 && name[p - 1] == '_') {
599 p--;
600 }
601 name.erase(p);
602 }
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700603 VersionInfo info;
604 if (!info.scan(scanner, maxApiLevel)) {
605 cout << "Skipping some " << name << " definitions.\n";
606 scanner->skipUntilTag("end:");
607 return;
608 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700609
610 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700611 Function* function = systemSpecification.findOrCreateFunction(name, &created);
612 FunctionSpecification* spec = new FunctionSpecification(function);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700613 function->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700614 function->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700615 specFile->addFunctionSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700616
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700617 spec->mUnexpandedName = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700618 spec->mTest = "scalar"; // default
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700619 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700620
Yang Ni12398d82015-09-18 14:57:07 -0700621 if (scanner->findOptionalTag("internal:")) {
622 spec->mInternal = (scanner->getValue() == "true");
623 }
624 if (scanner->findOptionalTag("intrinsic:")) {
625 spec->mIntrinsic = (scanner->getValue() == "true");
626 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700627 if (scanner->findOptionalTag("attrib:")) {
628 spec->mAttribute = scanner->getValue();
629 }
630 if (scanner->findOptionalTag("w:")) {
631 vector<string> t;
632 if (scanner->getValue().find("1") != string::npos) {
633 t.push_back("");
634 }
635 if (scanner->getValue().find("2") != string::npos) {
636 t.push_back("2");
637 }
638 if (scanner->getValue().find("3") != string::npos) {
639 t.push_back("3");
640 }
641 if (scanner->getValue().find("4") != string::npos) {
642 t.push_back("4");
643 }
644 spec->mReplaceables.push_back(t);
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800645 // RST_i pattern not applicable for width.
646 spec->mIsRSTAllowed.push_back(false);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700647 }
648
649 while (scanner->findOptionalTag("t:")) {
650 spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue()));
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800651 spec->mIsRSTAllowed.push_back(isRSTValid(spec->mReplaceables.back()));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700652 }
653
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800654 // Disallow RST_* pattern in function name
655 // FIXME the line number for this error would be wrong
656 spec->checkRSTPatternValidity(unexpandedName, false, scanner);
657
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700658 if (scanner->findTag("ret:")) {
659 ParameterEntry* p = scanner->parseArgString(true);
660 function->addReturn(p, scanner);
661 spec->mReturn = p;
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800662
663 // Disallow RST_* pattern in return type
664 spec->checkRSTPatternValidity(p->type, false, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700665 }
666 while (scanner->findOptionalTag("arg:")) {
667 ParameterEntry* p = scanner->parseArgString(false);
668 function->addParameter(p, scanner);
669 spec->mParameters.push_back(p);
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800670
671 // Disallow RST_* pattern in parameter type or testOption
672 spec->checkRSTPatternValidity(p->type, false, scanner);
673 spec->checkRSTPatternValidity(p->testOption, false, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700674 }
675
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700676 function->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700677
678 if (scanner->findOptionalTag("inline:")) {
679 scanner->checkNoValue();
680 while (scanner->findOptionalTag("")) {
681 spec->mInline.push_back(scanner->getValue());
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800682
683 // Allow RST_* pattern in inline definitions
684 spec->checkRSTPatternValidity(spec->mInline.back(), true, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700685 }
686 }
687 if (scanner->findOptionalTag("test:")) {
688 spec->parseTest(scanner);
689 }
690
691 scanner->findTag("end:");
692
693 spec->createPermutations(function, scanner);
694}
695
696FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec,
697 int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner)
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700698 : mReturn(nullptr), mInputCount(0), mOutputCount(0) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700699 // We expand the strings now to make capitalization easier. The previous code preserved
700 // the #n
701 // markers just before emitting, which made capitalization difficult.
702 mName = spec->getName(replacementIndexes);
703 mNameTrunk = func->getName();
704 mTest = spec->getTest();
705 mPrecisionLimit = spec->getPrecisionLimit();
706 spec->getInlines(replacementIndexes, &mInline);
707
708 mHasFloatAnswers = false;
709 for (size_t i = 0; i < spec->getNumberOfParams(); i++) {
710 string type, name, testOption;
711 int lineNumber = 0;
712 spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber);
713 ParameterDefinition* def = new ParameterDefinition();
714 def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner);
715 if (def->isOutParameter) {
716 mOutputCount++;
717 } else {
718 mInputCount++;
719 }
720
721 if (def->typeIndex < 0 && mTest != "none") {
722 scanner->error(lineNumber)
723 << "Could not find " << def->rsBaseType
724 << " while generating automated tests. Use test: none if not needed.\n";
725 }
726 if (def->isOutParameter && def->isFloatType) {
727 mHasFloatAnswers = true;
728 }
729 mParams.push_back(def);
730 }
731
732 string retType;
733 int lineNumber = 0;
734 spec->getReturn(replacementIndexes, &retType, &lineNumber);
735 if (!retType.empty()) {
736 mReturn = new ParameterDefinition();
737 mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner);
738 if (mReturn->isFloatType) {
739 mHasFloatAnswers = true;
740 }
741 mOutputCount++;
742 }
743}
744
745FunctionPermutation::~FunctionPermutation() {
746 for (auto i : mParams) {
747 delete i;
748 }
749 delete mReturn;
750}
751
752SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) {
753 string core = mSpecFileName;
754 // Remove .spec
755 size_t l = core.length();
756 const char SPEC[] = ".spec";
757 const int SPEC_SIZE = sizeof(SPEC) - 1;
758 const int start = l - SPEC_SIZE;
759 if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) {
760 core.erase(start);
761 }
762
763 // The header file name should have the same base but with a ".rsh" extension.
764 mHeaderFileName = core + ".rsh";
Jean-Luc Brouilletd9935ee2015-04-03 17:27:02 -0700765 mDetailedDocumentationUrl = core + ".html";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700766}
767
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700768void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) {
769 mConstantSpecificationsList.push_back(spec);
770 if (hasDocumentation) {
771 Constant* constant = spec->getConstant();
772 mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700773 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700774}
775
776void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) {
777 mTypeSpecificationsList.push_back(spec);
778 if (hasDocumentation) {
779 Type* type = spec->getType();
780 mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700781 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700782}
783
784void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) {
785 mFunctionSpecificationsList.push_back(spec);
786 if (hasDocumentation) {
787 Function* function = spec->getFunction();
788 mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700789 }
790}
791
792// Read the specification, adding the definitions to the global functions map.
Yang Ni12398d82015-09-18 14:57:07 -0700793bool SpecFile::readSpecFile(unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700794 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
795 if (!specFile) {
796 cerr << "Error opening input file: " << mSpecFileName << "\n";
797 return false;
798 }
799
800 Scanner scanner(mSpecFileName, specFile);
801
802 // Scan the header that should start the file.
803 scanner.skipBlankEntries();
804 if (scanner.findTag("header:")) {
805 if (scanner.findTag("summary:")) {
806 mBriefDescription = scanner.getValue();
807 }
808 if (scanner.findTag("description:")) {
809 scanner.checkNoValue();
810 while (scanner.findOptionalTag("")) {
811 mFullDescription.push_back(scanner.getValue());
812 }
813 }
814 if (scanner.findOptionalTag("include:")) {
815 scanner.checkNoValue();
816 while (scanner.findOptionalTag("")) {
817 mVerbatimInclude.push_back(scanner.getValue());
818 }
819 }
820 scanner.findTag("end:");
821 }
822
823 while (1) {
824 scanner.skipBlankEntries();
825 if (scanner.atEnd()) {
826 break;
827 }
828 const string tag = scanner.getNextTag();
829 if (tag == "function:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700830 FunctionSpecification::scanFunctionSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700831 } else if (tag == "type:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700832 TypeSpecification::scanTypeSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700833 } else if (tag == "constant:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700834 ConstantSpecification::scanConstantSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700835 } else {
836 scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
837 return false;
838 }
839 }
840
841 fclose(specFile);
842 return scanner.getErrorCount() == 0;
843}
844
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700845SystemSpecification::~SystemSpecification() {
846 for (auto i : mConstants) {
847 delete i.second;
848 }
849 for (auto i : mTypes) {
850 delete i.second;
851 }
852 for (auto i : mFunctions) {
853 delete i.second;
854 }
855 for (auto i : mSpecFiles) {
856 delete i;
857 }
858}
859
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700860// Returns the named entry in the map. Creates it if it's not there.
861template <class T>
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700862T* findOrCreate(const string& name, map<string, T*>* map, bool* created) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700863 auto iter = map->find(name);
864 if (iter != map->end()) {
865 *created = false;
866 return iter->second;
867 }
868 *created = true;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700869 T* f = new T(name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700870 map->insert(pair<string, T*>(name, f));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700871 return f;
872}
873
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700874Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) {
875 return findOrCreate<Constant>(name, &mConstants, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700876}
877
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700878Type* SystemSpecification::findOrCreateType(const string& name, bool* created) {
879 return findOrCreate<Type>(name, &mTypes, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700880}
881
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700882Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) {
883 return findOrCreate<Function>(name, &mFunctions, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700884}
885
Yang Ni12398d82015-09-18 14:57:07 -0700886bool SystemSpecification::readSpecFile(const string& fileName, unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700887 SpecFile* spec = new SpecFile(fileName);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700888 if (!spec->readSpecFile(maxApiLevel)) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700889 cerr << fileName << ": Failed to parse.\n";
890 return false;
891 }
892 mSpecFiles.push_back(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700893 return true;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700894}
895
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700896
Yang Ni12398d82015-09-18 14:57:07 -0700897static void updateMaxApiLevel(const VersionInfo& info, unsigned int* maxApiLevel) {
898 if (info.minVersion == VersionInfo::kUnreleasedVersion) {
899 // Ignore development API level in consideration of max API level.
900 return;
901 }
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700902 *maxApiLevel = max(*maxApiLevel, max(info.minVersion, info.maxVersion));
903}
904
Yang Ni12398d82015-09-18 14:57:07 -0700905unsigned int SystemSpecification::getMaximumApiLevel() {
906 unsigned int maxApiLevel = 0;
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700907 for (auto i : mConstants) {
908 for (auto j: i.second->getSpecifications()) {
909 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
910 }
911 }
912 for (auto i : mTypes) {
913 for (auto j: i.second->getSpecifications()) {
914 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
915 }
916 }
917 for (auto i : mFunctions) {
918 for (auto j: i.second->getSpecifications()) {
919 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
920 }
921 }
922 return maxApiLevel;
923}
924
Yang Ni12398d82015-09-18 14:57:07 -0700925bool SystemSpecification::generateFiles(bool forVerification, unsigned int maxApiLevel) const {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700926 bool success = generateHeaderFiles("scriptc") &&
927 generateDocumentation("docs", forVerification) &&
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700928 generateTestFiles("test", maxApiLevel) &&
929 generateStubsWhiteList("slangtest", maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700930 if (success) {
931 cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
932 << " constants, and " << mFunctions.size() << " functions.\n";
933 }
934 return success;
935}
936
937string SystemSpecification::getHtmlAnchor(const string& name) const {
938 Definition* d = nullptr;
939 auto c = mConstants.find(name);
940 if (c != mConstants.end()) {
941 d = c->second;
942 } else {
943 auto t = mTypes.find(name);
944 if (t != mTypes.end()) {
945 d = t->second;
946 } else {
947 auto f = mFunctions.find(name);
948 if (f != mFunctions.end()) {
949 d = f->second;
950 } else {
951 return string();
952 }
953 }
954 }
955 ostringstream stream;
956 stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
957 return stream.str();
958}