blob: 7afeea462d044b4df1a3d6b1366f43cd8fa3e239 [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 Leo9309a062015-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 Leo9309a062015-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 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700383 constant->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700384
385 scanner->findTag("end:");
386}
387
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700388void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile,
Yang Ni12398d82015-09-18 14:57:07 -0700389 unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700390 string name = scanner->getValue();
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700391 VersionInfo info;
392 if (!info.scan(scanner, maxApiLevel)) {
393 cout << "Skipping some " << name << " definitions.\n";
394 scanner->skipUntilTag("end:");
395 return;
396 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700397
398 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700399 Type* type = systemSpecification.findOrCreateType(name, &created);
400 TypeSpecification* spec = new TypeSpecification(type);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700401 type->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700402 type->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700403 specFile->addTypeSpecification(spec, created);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700404 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700405
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700406 if (scanner->findOptionalTag("simple:")) {
407 spec->mKind = SIMPLE;
408 spec->mSimpleType = scanner->getValue();
409 }
Stephen Hinesca51c782015-08-25 23:43:34 -0700410 if (scanner->findOptionalTag("rs_object:")) {
411 spec->mKind = RS_OBJECT;
412 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700413 if (scanner->findOptionalTag("struct:")) {
414 spec->mKind = STRUCT;
415 spec->mStructName = scanner->getValue();
416 while (scanner->findOptionalTag("field:")) {
417 string s = scanner->getValue();
418 string comment;
419 scanner->parseDocumentation(&s, &comment);
420 spec->mFields.push_back(s);
421 spec->mFieldComments.push_back(comment);
422 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700423 }
424 if (scanner->findOptionalTag("enum:")) {
425 spec->mKind = ENUM;
426 spec->mEnumName = scanner->getValue();
427 while (scanner->findOptionalTag("value:")) {
428 string s = scanner->getValue();
429 string comment;
430 scanner->parseDocumentation(&s, &comment);
431 spec->mValues.push_back(s);
432 spec->mValueComments.push_back(comment);
433 }
434 }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700435 if (scanner->findOptionalTag("attrib:")) {
436 spec->mAttribute = scanner->getValue();
437 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700438 type->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700439
440 scanner->findTag("end:");
441}
442
443FunctionSpecification::~FunctionSpecification() {
444 for (auto i : mParameters) {
445 delete i;
446 }
447 delete mReturn;
448 for (auto i : mPermutations) {
449 delete i;
450 }
451}
452
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800453string FunctionSpecification::expandRSTypeInString(const string &s,
454 const string &pattern,
455 const string &cTypeStr) const {
456 // Find index of numerical type corresponding to cTypeStr. The case where
457 // pattern is found in s but cTypeStr is not a numerical type is checked in
458 // checkRSTPatternValidity.
459 int typeIdx = findCType(cTypeStr);
460 if (typeIdx == -1) {
461 return s;
462 }
463 // If index exists, perform replacement.
464 return stringReplace(s, pattern, TYPES[typeIdx].rsDataType);
465}
466
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700467string FunctionSpecification::expandString(string s,
468 int replacementIndexes[MAX_REPLACEABLES]) const {
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800469
470
471 for (unsigned idx = 0; idx < mReplaceables.size(); idx ++) {
472 string toString = mReplaceables[idx][replacementIndexes[idx]];
473
474 // replace #RST_i patterns with RS datatype corresponding to toString
475 s = expandRSTypeInString(s, kRSTypePatterns[idx], toString);
476
477 // replace #i patterns with C type from mReplaceables
478 s = stringReplace(s, kCTypePatterns[idx], toString);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700479 }
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800480
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700481 return s;
482}
483
484void FunctionSpecification::expandStringVector(const vector<string>& in,
485 int replacementIndexes[MAX_REPLACEABLES],
486 vector<string>* out) const {
487 out->clear();
488 for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
489 out->push_back(expandString(*iter, replacementIndexes));
490 }
491}
492
493void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) {
494 int start[MAX_REPLACEABLES];
495 int end[MAX_REPLACEABLES];
496 for (int i = 0; i < MAX_REPLACEABLES; i++) {
497 if (i < (int)mReplaceables.size()) {
498 start[i] = 0;
499 end[i] = mReplaceables[i].size();
500 } else {
501 start[i] = -1;
502 end[i] = 0;
503 }
504 }
505 int replacementIndexes[MAX_REPLACEABLES];
506 // TODO: These loops assume that MAX_REPLACEABLES is 4.
507 for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3];
508 replacementIndexes[3]++) {
509 for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2];
510 replacementIndexes[2]++) {
511 for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1];
512 replacementIndexes[1]++) {
513 for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0];
514 replacementIndexes[0]++) {
515 auto p = new FunctionPermutation(function, this, replacementIndexes, scanner);
516 mPermutations.push_back(p);
517 }
518 }
519 }
520 }
521}
522
523string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const {
524 return expandString(mUnexpandedName, replacementIndexes);
525}
526
527void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES],
528 std::string* retType, int* lineNumber) const {
529 *retType = expandString(mReturn->type, replacementIndexes);
530 *lineNumber = mReturn->lineNumber;
531}
532
533void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES],
534 std::string* type, std::string* name, std::string* testOption,
535 int* lineNumber) const {
536 ParameterEntry* p = mParameters[index];
537 *type = expandString(p->type, replacementIndexes);
538 *name = p->name;
539 *testOption = expandString(p->testOption, replacementIndexes);
540 *lineNumber = p->lineNumber;
541}
542
543void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES],
544 std::vector<std::string>* inlines) const {
545 expandStringVector(mInline, replacementIndexes, inlines);
546}
547
548void FunctionSpecification::parseTest(Scanner* scanner) {
549 const string value = scanner->getValue();
550 if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" ||
551 value == "none") {
552 mTest = value;
553 } else if (value.compare(0, 7, "limited") == 0) {
554 mTest = "limited";
555 if (value.compare(7, 1, "(") == 0) {
556 size_t pParen = value.find(')');
557 if (pParen == string::npos) {
558 scanner->error() << "Incorrect test: \"" << value << "\"\n";
559 } else {
560 mPrecisionLimit = value.substr(8, pParen - 8);
561 }
562 }
563 } else {
564 scanner->error() << "Unrecognized test option: \"" << value << "\"\n";
565 }
566}
567
Yang Ni12398d82015-09-18 14:57:07 -0700568bool FunctionSpecification::hasTests(unsigned int versionOfTestFiles) const {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700569 if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) {
570 return false;
571 }
572 if (mTest == "none") {
573 return false;
574 }
575 return true;
576}
577
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800578void FunctionSpecification::checkRSTPatternValidity(const string &inlineStr, bool allow,
579 Scanner *scanner) {
580 for (int i = 0; i < MAX_REPLACEABLES; i ++) {
581 bool patternFound = inlineStr.find(kRSTypePatterns[i]) != string::npos;
582
583 if (patternFound) {
584 if (!allow) {
585 scanner->error() << "RST_i pattern not allowed here\n";
586 }
587 else if (mIsRSTAllowed[i] == false) {
588 scanner->error() << "Found pattern \"" << kRSTypePatterns[i]
589 << "\" in spec. But some entry in the corresponding"
590 << " parameter list cannot be translated to an RS type\n";
591 }
592 }
593 }
594}
595
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700596void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile,
Yang Ni12398d82015-09-18 14:57:07 -0700597 unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700598 // Some functions like convert have # part of the name. Truncate at that point.
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700599 const string& unexpandedName = scanner->getValue();
600 string name = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700601 size_t p = name.find('#');
602 if (p != string::npos) {
603 if (p > 0 && name[p - 1] == '_') {
604 p--;
605 }
606 name.erase(p);
607 }
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700608 VersionInfo info;
609 if (!info.scan(scanner, maxApiLevel)) {
610 cout << "Skipping some " << name << " definitions.\n";
611 scanner->skipUntilTag("end:");
612 return;
613 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700614
615 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700616 Function* function = systemSpecification.findOrCreateFunction(name, &created);
617 FunctionSpecification* spec = new FunctionSpecification(function);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700618 function->addSpecification(spec);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700619 function->updateFinalVersion(info);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700620 specFile->addFunctionSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700621
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700622 spec->mUnexpandedName = unexpandedName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700623 spec->mTest = "scalar"; // default
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700624 spec->mVersionInfo = info;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700625
Yang Ni12398d82015-09-18 14:57:07 -0700626 if (scanner->findOptionalTag("internal:")) {
627 spec->mInternal = (scanner->getValue() == "true");
628 }
629 if (scanner->findOptionalTag("intrinsic:")) {
630 spec->mIntrinsic = (scanner->getValue() == "true");
631 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700632 if (scanner->findOptionalTag("attrib:")) {
633 spec->mAttribute = scanner->getValue();
634 }
635 if (scanner->findOptionalTag("w:")) {
636 vector<string> t;
637 if (scanner->getValue().find("1") != string::npos) {
638 t.push_back("");
639 }
640 if (scanner->getValue().find("2") != string::npos) {
641 t.push_back("2");
642 }
643 if (scanner->getValue().find("3") != string::npos) {
644 t.push_back("3");
645 }
646 if (scanner->getValue().find("4") != string::npos) {
647 t.push_back("4");
648 }
649 spec->mReplaceables.push_back(t);
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800650 // RST_i pattern not applicable for width.
651 spec->mIsRSTAllowed.push_back(false);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700652 }
653
654 while (scanner->findOptionalTag("t:")) {
655 spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue()));
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800656 spec->mIsRSTAllowed.push_back(isRSTValid(spec->mReplaceables.back()));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700657 }
658
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800659 // Disallow RST_* pattern in function name
660 // FIXME the line number for this error would be wrong
661 spec->checkRSTPatternValidity(unexpandedName, false, scanner);
662
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700663 if (scanner->findTag("ret:")) {
664 ParameterEntry* p = scanner->parseArgString(true);
665 function->addReturn(p, scanner);
666 spec->mReturn = p;
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800667
668 // Disallow RST_* pattern in return type
669 spec->checkRSTPatternValidity(p->type, false, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700670 }
671 while (scanner->findOptionalTag("arg:")) {
672 ParameterEntry* p = scanner->parseArgString(false);
673 function->addParameter(p, scanner);
674 spec->mParameters.push_back(p);
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800675
676 // Disallow RST_* pattern in parameter type or testOption
677 spec->checkRSTPatternValidity(p->type, false, scanner);
678 spec->checkRSTPatternValidity(p->testOption, false, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700679 }
680
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700681 function->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700682
683 if (scanner->findOptionalTag("inline:")) {
684 scanner->checkNoValue();
685 while (scanner->findOptionalTag("")) {
686 spec->mInline.push_back(scanner->getValue());
Pirama Arumuga Nainar43d758c2015-11-13 12:54:42 -0800687
688 // Allow RST_* pattern in inline definitions
689 spec->checkRSTPatternValidity(spec->mInline.back(), true, scanner);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700690 }
691 }
692 if (scanner->findOptionalTag("test:")) {
693 spec->parseTest(scanner);
694 }
695
696 scanner->findTag("end:");
697
698 spec->createPermutations(function, scanner);
699}
700
701FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec,
702 int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner)
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700703 : mReturn(nullptr), mInputCount(0), mOutputCount(0) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700704 // We expand the strings now to make capitalization easier. The previous code preserved
705 // the #n
706 // markers just before emitting, which made capitalization difficult.
707 mName = spec->getName(replacementIndexes);
708 mNameTrunk = func->getName();
709 mTest = spec->getTest();
710 mPrecisionLimit = spec->getPrecisionLimit();
711 spec->getInlines(replacementIndexes, &mInline);
712
713 mHasFloatAnswers = false;
714 for (size_t i = 0; i < spec->getNumberOfParams(); i++) {
715 string type, name, testOption;
716 int lineNumber = 0;
717 spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber);
718 ParameterDefinition* def = new ParameterDefinition();
719 def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner);
720 if (def->isOutParameter) {
721 mOutputCount++;
722 } else {
723 mInputCount++;
724 }
725
726 if (def->typeIndex < 0 && mTest != "none") {
727 scanner->error(lineNumber)
728 << "Could not find " << def->rsBaseType
729 << " while generating automated tests. Use test: none if not needed.\n";
730 }
731 if (def->isOutParameter && def->isFloatType) {
732 mHasFloatAnswers = true;
733 }
734 mParams.push_back(def);
735 }
736
737 string retType;
738 int lineNumber = 0;
739 spec->getReturn(replacementIndexes, &retType, &lineNumber);
740 if (!retType.empty()) {
741 mReturn = new ParameterDefinition();
742 mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner);
743 if (mReturn->isFloatType) {
744 mHasFloatAnswers = true;
745 }
746 mOutputCount++;
747 }
748}
749
750FunctionPermutation::~FunctionPermutation() {
751 for (auto i : mParams) {
752 delete i;
753 }
754 delete mReturn;
755}
756
757SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) {
758 string core = mSpecFileName;
759 // Remove .spec
760 size_t l = core.length();
761 const char SPEC[] = ".spec";
762 const int SPEC_SIZE = sizeof(SPEC) - 1;
763 const int start = l - SPEC_SIZE;
764 if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) {
765 core.erase(start);
766 }
767
768 // The header file name should have the same base but with a ".rsh" extension.
769 mHeaderFileName = core + ".rsh";
Jean-Luc Brouilletd9935ee2015-04-03 17:27:02 -0700770 mDetailedDocumentationUrl = core + ".html";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700771}
772
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700773void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) {
774 mConstantSpecificationsList.push_back(spec);
775 if (hasDocumentation) {
776 Constant* constant = spec->getConstant();
777 mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700778 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700779}
780
781void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) {
782 mTypeSpecificationsList.push_back(spec);
783 if (hasDocumentation) {
784 Type* type = spec->getType();
785 mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700786 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700787}
788
789void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) {
790 mFunctionSpecificationsList.push_back(spec);
791 if (hasDocumentation) {
792 Function* function = spec->getFunction();
793 mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700794 }
795}
796
797// Read the specification, adding the definitions to the global functions map.
Yang Ni12398d82015-09-18 14:57:07 -0700798bool SpecFile::readSpecFile(unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700799 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
800 if (!specFile) {
801 cerr << "Error opening input file: " << mSpecFileName << "\n";
802 return false;
803 }
804
805 Scanner scanner(mSpecFileName, specFile);
806
807 // Scan the header that should start the file.
808 scanner.skipBlankEntries();
809 if (scanner.findTag("header:")) {
810 if (scanner.findTag("summary:")) {
811 mBriefDescription = scanner.getValue();
812 }
813 if (scanner.findTag("description:")) {
814 scanner.checkNoValue();
815 while (scanner.findOptionalTag("")) {
816 mFullDescription.push_back(scanner.getValue());
817 }
818 }
819 if (scanner.findOptionalTag("include:")) {
820 scanner.checkNoValue();
821 while (scanner.findOptionalTag("")) {
822 mVerbatimInclude.push_back(scanner.getValue());
823 }
824 }
825 scanner.findTag("end:");
826 }
827
828 while (1) {
829 scanner.skipBlankEntries();
830 if (scanner.atEnd()) {
831 break;
832 }
833 const string tag = scanner.getNextTag();
834 if (tag == "function:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700835 FunctionSpecification::scanFunctionSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700836 } else if (tag == "type:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700837 TypeSpecification::scanTypeSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700838 } else if (tag == "constant:") {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700839 ConstantSpecification::scanConstantSpecification(&scanner, this, maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700840 } else {
841 scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
842 return false;
843 }
844 }
845
846 fclose(specFile);
847 return scanner.getErrorCount() == 0;
848}
849
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700850SystemSpecification::~SystemSpecification() {
851 for (auto i : mConstants) {
852 delete i.second;
853 }
854 for (auto i : mTypes) {
855 delete i.second;
856 }
857 for (auto i : mFunctions) {
858 delete i.second;
859 }
860 for (auto i : mSpecFiles) {
861 delete i;
862 }
863}
864
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700865// Returns the named entry in the map. Creates it if it's not there.
866template <class T>
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700867T* findOrCreate(const string& name, map<string, T*>* map, bool* created) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700868 auto iter = map->find(name);
869 if (iter != map->end()) {
870 *created = false;
871 return iter->second;
872 }
873 *created = true;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700874 T* f = new T(name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700875 map->insert(pair<string, T*>(name, f));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700876 return f;
877}
878
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700879Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) {
880 return findOrCreate<Constant>(name, &mConstants, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700881}
882
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700883Type* SystemSpecification::findOrCreateType(const string& name, bool* created) {
884 return findOrCreate<Type>(name, &mTypes, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700885}
886
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700887Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) {
888 return findOrCreate<Function>(name, &mFunctions, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700889}
890
Yang Ni12398d82015-09-18 14:57:07 -0700891bool SystemSpecification::readSpecFile(const string& fileName, unsigned int maxApiLevel) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700892 SpecFile* spec = new SpecFile(fileName);
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700893 if (!spec->readSpecFile(maxApiLevel)) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700894 cerr << fileName << ": Failed to parse.\n";
895 return false;
896 }
897 mSpecFiles.push_back(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700898 return true;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700899}
900
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700901
Yang Ni12398d82015-09-18 14:57:07 -0700902static void updateMaxApiLevel(const VersionInfo& info, unsigned int* maxApiLevel) {
903 if (info.minVersion == VersionInfo::kUnreleasedVersion) {
904 // Ignore development API level in consideration of max API level.
905 return;
906 }
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700907 *maxApiLevel = max(*maxApiLevel, max(info.minVersion, info.maxVersion));
908}
909
Yang Ni12398d82015-09-18 14:57:07 -0700910unsigned int SystemSpecification::getMaximumApiLevel() {
911 unsigned int maxApiLevel = 0;
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700912 for (auto i : mConstants) {
913 for (auto j: i.second->getSpecifications()) {
914 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
915 }
916 }
917 for (auto i : mTypes) {
918 for (auto j: i.second->getSpecifications()) {
919 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
920 }
921 }
922 for (auto i : mFunctions) {
923 for (auto j: i.second->getSpecifications()) {
924 updateMaxApiLevel(j->getVersionInfo(), &maxApiLevel);
925 }
926 }
927 return maxApiLevel;
928}
929
Yang Ni12398d82015-09-18 14:57:07 -0700930bool SystemSpecification::generateFiles(bool forVerification, unsigned int maxApiLevel) const {
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700931 bool success = generateHeaderFiles("scriptc") &&
932 generateDocumentation("docs", forVerification) &&
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700933 generateTestFiles("test", maxApiLevel) &&
934 generateStubsWhiteList("slangtest", maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700935 if (success) {
936 cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
937 << " constants, and " << mFunctions.size() << " functions.\n";
938 }
939 return success;
940}
941
942string SystemSpecification::getHtmlAnchor(const string& name) const {
943 Definition* d = nullptr;
944 auto c = mConstants.find(name);
945 if (c != mConstants.end()) {
946 d = c->second;
947 } else {
948 auto t = mTypes.find(name);
949 if (t != mTypes.end()) {
950 d = t->second;
951 } else {
952 auto f = mFunctions.find(name);
953 if (f != mFunctions.end()) {
954 d = f->second;
955 } else {
956 return string();
957 }
958 }
959 }
960 ostringstream stream;
961 stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
962 return stream.str();
963}