blob: 0dbaa73614234e3d642ed915d00cf12e2b0a7701 [file] [log] [blame]
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <cctype>
19#include <cstdlib>
20#include <fstream>
21#include <functional>
22#include <iostream>
23#include <memory>
24#include <sstream>
25#include <strings.h>
26
27#include "Generator.h"
28#include "Scanner.h"
29#include "Specification.h"
30#include "Utilities.h"
31
32using namespace std;
33
34// API level when RenderScript was added.
35const int MIN_API_LEVEL = 9;
36
37const NumericalType TYPES[] = {
38 {"f16", "FLOAT_16", "half", "half", FLOATING_POINT, 11, 5},
39 {"f32", "FLOAT_32", "float", "float", FLOATING_POINT, 24, 8},
40 {"f64", "FLOAT_64", "double", "double", FLOATING_POINT, 53, 11},
41 {"i8", "SIGNED_8", "char", "byte", SIGNED_INTEGER, 7, 0},
42 {"u8", "UNSIGNED_8", "uchar", "byte", UNSIGNED_INTEGER, 8, 0},
43 {"i16", "SIGNED_16", "short", "short", SIGNED_INTEGER, 15, 0},
44 {"u16", "UNSIGNED_16", "ushort", "short", UNSIGNED_INTEGER, 16, 0},
45 {"i32", "SIGNED_32", "int", "int", SIGNED_INTEGER, 31, 0},
46 {"u32", "UNSIGNED_32", "uint", "int", UNSIGNED_INTEGER, 32, 0},
47 {"i64", "SIGNED_64", "long", "long", SIGNED_INTEGER, 63, 0},
48 {"u64", "UNSIGNED_64", "ulong", "long", UNSIGNED_INTEGER, 64, 0},
49};
50
51const int NUM_TYPES = sizeof(TYPES) / sizeof(TYPES[0]);
52
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070053// The singleton of the collected information of all the spec files.
54SystemSpecification systemSpecification;
55
56// Returns the index in TYPES for the provided cType
57static int findCType(const string& cType) {
58 for (int i = 0; i < NUM_TYPES; i++) {
59 if (cType == TYPES[i].cType) {
60 return i;
61 }
62 }
63 return -1;
64}
65
66/* Converts a string like "u8, u16" to a vector of "ushort", "uint".
67 * For non-numerical types, we don't need to convert the abbreviation.
68 */
69static vector<string> convertToTypeVector(const string& input) {
70 // First convert the string to an array of strings.
71 vector<string> entries;
72 stringstream stream(input);
73 string entry;
74 while (getline(stream, entry, ',')) {
75 trimSpaces(&entry);
76 entries.push_back(entry);
77 }
78
79 /* Second, we look for present numerical types. We do it this way
80 * so the order of numerical types is always the same, no matter
81 * how specified in the spec file.
82 */
83 vector<string> result;
84 for (auto t : TYPES) {
85 for (auto i = entries.begin(); i != entries.end(); ++i) {
86 if (*i == t.specType) {
87 result.push_back(t.cType);
88 entries.erase(i);
89 break;
90 }
91 }
92 }
93
94 // Add the remaining; they are not numerical types.
95 for (auto s : entries) {
96 result.push_back(s);
97 }
98
99 return result;
100}
101
102void ParameterDefinition::parseParameterDefinition(const string& type, const string& name,
103 const string& testOption, int lineNumber,
104 bool isReturn, Scanner* scanner) {
105 rsType = type;
106 specName = name;
107
108 // Determine if this is an output.
109 isOutParameter = isReturn || charRemoved('*', &rsType);
110
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700111 rsBaseType = rsType;
112 mVectorSize = "1";
113 /* If it's a vector type, we need to split the base type from the size.
114 * We know that's it's a vector type if the last character is a digit and
115 * the rest is an actual base type. We used to only verify the first part,
116 * which created a problem with rs_matrix2x2.
117 */
118 const int last = rsType.size() - 1;
119 const char lastChar = rsType[last];
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700120 if (lastChar >= '0' && lastChar <= '9') {
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700121 const string trimmed = rsType.substr(0, last);
122 int i = findCType(trimmed);
123 if (i >= 0) {
124 rsBaseType = trimmed;
125 mVectorSize = lastChar;
126 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700127 }
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700128 typeIndex = findCType(rsBaseType);
129
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700130 if (mVectorSize == "3") {
131 vectorWidth = "4";
132 } else {
133 vectorWidth = mVectorSize;
134 }
135
136 /* Create variable names to be used in the java and .rs files. Because x and
137 * y are reserved in .rs files, we prefix variable names with "in" or "out".
138 */
139 if (isOutParameter) {
140 variableName = "out";
141 if (!specName.empty()) {
142 variableName += capitalize(specName);
143 } else if (!isReturn) {
144 scanner->error(lineNumber) << "Should have a name.\n";
145 }
146 } else {
147 variableName = "in";
148 if (specName.empty()) {
149 scanner->error(lineNumber) << "Should have a name.\n";
150 }
151 variableName += capitalize(specName);
152 }
153 rsAllocName = "gAlloc" + capitalize(variableName);
154 javaAllocName = variableName;
155 javaArrayName = "array" + capitalize(javaAllocName);
156
157 // Process the option.
158 undefinedIfOutIsNan = false;
159 compatibleTypeIndex = -1;
160 if (!testOption.empty()) {
161 if (testOption.compare(0, 6, "range(") == 0) {
162 size_t pComma = testOption.find(',');
163 size_t pParen = testOption.find(')');
164 if (pComma == string::npos || pParen == string::npos) {
165 scanner->error(lineNumber) << "Incorrect range " << testOption << "\n";
166 } else {
167 minValue = testOption.substr(6, pComma - 6);
168 maxValue = testOption.substr(pComma + 1, pParen - pComma - 1);
169 }
170 } else if (testOption.compare(0, 6, "above(") == 0) {
171 size_t pParen = testOption.find(')');
172 if (pParen == string::npos) {
173 scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
174 } else {
175 smallerParameter = testOption.substr(6, pParen - 6);
176 }
177 } else if (testOption.compare(0, 11, "compatible(") == 0) {
178 size_t pParen = testOption.find(')');
179 if (pParen == string::npos) {
180 scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
181 } else {
182 compatibleTypeIndex = findCType(testOption.substr(11, pParen - 11));
183 }
184 } else if (testOption.compare(0, 11, "conditional") == 0) {
185 undefinedIfOutIsNan = true;
186 } else {
187 scanner->error(lineNumber) << "Unrecognized testOption " << testOption << "\n";
188 }
189 }
190
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700191 isFloatType = false;
192 if (typeIndex >= 0) {
193 javaBaseType = TYPES[typeIndex].javaType;
194 specType = TYPES[typeIndex].specType;
195 isFloatType = TYPES[typeIndex].exponentBits > 0;
196 }
197 if (!minValue.empty()) {
198 if (typeIndex < 0 || TYPES[typeIndex].kind != FLOATING_POINT) {
199 scanner->error(lineNumber) << "range(,) is only supported for floating point\n";
200 }
201 }
202}
203
204void VersionInfo::scan(Scanner* scanner) {
205 if (scanner->findOptionalTag("version:")) {
206 const string s = scanner->getValue();
207 sscanf(s.c_str(), "%i %i", &minVersion, &maxVersion);
208 if (minVersion && minVersion < MIN_API_LEVEL) {
209 scanner->error() << "Minimum version must >= 9\n";
210 }
211 if (minVersion == MIN_API_LEVEL) {
212 minVersion = 0;
213 }
214 if (maxVersion && maxVersion < MIN_API_LEVEL) {
215 scanner->error() << "Maximum version must >= 9\n";
216 }
217 }
218 if (scanner->findOptionalTag("size:")) {
219 sscanf(scanner->getValue().c_str(), "%i", &intSize);
220 }
221}
222
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700223Definition::Definition(const std::string& name) : mName(name), mDeprecated(false), mHidden(false) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700224}
225
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700226void Definition::scanDocumentationTags(Scanner* scanner, bool firstOccurence,
227 const SpecFile* specFile) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700228 if (scanner->findOptionalTag("hidden:")) {
229 scanner->checkNoValue();
230 mHidden = true;
231 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700232 if (scanner->findOptionalTag("deprecated:")) {
233 mDeprecated = true;
234 mDeprecatedMessage = scanner->getValue();
235 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700236 if (firstOccurence) {
237 if (scanner->findTag("summary:")) {
238 mSummary = scanner->getValue();
239 }
240 if (scanner->findTag("description:")) {
241 scanner->checkNoValue();
242 while (scanner->findOptionalTag("")) {
243 mDescription.push_back(scanner->getValue());
244 }
245 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700246 mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + mName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700247 } else if (scanner->findOptionalTag("summary:")) {
248 scanner->error() << "Only the first specification should have a summary.\n";
249 }
250}
251
252Constant::~Constant() {
253 for (auto i : mSpecifications) {
254 delete i;
255 }
256}
257
258Type::~Type() {
259 for (auto i : mSpecifications) {
260 delete i;
261 }
262}
263
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700264Function::Function(const string& name) : Definition(name) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700265 mCapitalizedName = capitalize(mName);
266}
267
268Function::~Function() {
269 for (auto i : mSpecifications) {
270 delete i;
271 }
272}
273
274bool Function::someParametersAreDocumented() const {
275 for (auto p : mParameters) {
276 if (!p->documentation.empty()) {
277 return true;
278 }
279 }
280 return false;
281}
282
283void Function::addParameter(ParameterEntry* entry, Scanner* scanner) {
284 for (auto i : mParameters) {
285 if (i->name == entry->name) {
286 // It's a duplicate.
287 if (!entry->documentation.empty()) {
288 scanner->error(entry->lineNumber)
289 << "Only the first occurence of an arg should have the "
290 "documentation.\n";
291 }
292 return;
293 }
294 }
295 mParameters.push_back(entry);
296}
297
298void Function::addReturn(ParameterEntry* entry, Scanner* scanner) {
299 if (entry->documentation.empty()) {
300 return;
301 }
302 if (!mReturnDocumentation.empty()) {
303 scanner->error() << "ret: should be documented only for the first variant\n";
304 }
305 mReturnDocumentation = entry->documentation;
306}
307
308void Specification::scanVersionInfo(Scanner* scanner) {
309 mVersionInfo.scan(scanner);
310}
311
312void ConstantSpecification::scanConstantSpecification(Scanner* scanner, SpecFile* specFile) {
313 string name = scanner->getValue();
314
315 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700316 Constant* constant = systemSpecification.findOrCreateConstant(name, &created);
317 ConstantSpecification* spec = new ConstantSpecification(constant);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700318 constant->addSpecification(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700319 specFile->addConstantSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700320
321 spec->scanVersionInfo(scanner);
322 if (scanner->findTag("value:")) {
323 spec->mValue = scanner->getValue();
324 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700325 constant->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700326
327 scanner->findTag("end:");
328}
329
330void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile) {
331 string name = scanner->getValue();
332
333 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700334 Type* type = systemSpecification.findOrCreateType(name, &created);
335 TypeSpecification* spec = new TypeSpecification(type);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700336 type->addSpecification(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700337 specFile->addTypeSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700338
339 spec->scanVersionInfo(scanner);
340 if (scanner->findOptionalTag("simple:")) {
341 spec->mKind = SIMPLE;
342 spec->mSimpleType = scanner->getValue();
343 }
344 if (scanner->findOptionalTag("struct:")) {
345 spec->mKind = STRUCT;
346 spec->mStructName = scanner->getValue();
347 while (scanner->findOptionalTag("field:")) {
348 string s = scanner->getValue();
349 string comment;
350 scanner->parseDocumentation(&s, &comment);
351 spec->mFields.push_back(s);
352 spec->mFieldComments.push_back(comment);
353 }
354 if (scanner->findOptionalTag("attrib:")) {
355 spec->mAttrib = scanner->getValue();
356 }
357 }
358 if (scanner->findOptionalTag("enum:")) {
359 spec->mKind = ENUM;
360 spec->mEnumName = scanner->getValue();
361 while (scanner->findOptionalTag("value:")) {
362 string s = scanner->getValue();
363 string comment;
364 scanner->parseDocumentation(&s, &comment);
365 spec->mValues.push_back(s);
366 spec->mValueComments.push_back(comment);
367 }
368 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700369 type->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700370
371 scanner->findTag("end:");
372}
373
374FunctionSpecification::~FunctionSpecification() {
375 for (auto i : mParameters) {
376 delete i;
377 }
378 delete mReturn;
379 for (auto i : mPermutations) {
380 delete i;
381 }
382}
383
384string FunctionSpecification::expandString(string s,
385 int replacementIndexes[MAX_REPLACEABLES]) const {
386 if (mReplaceables.size() > 0) {
387 s = stringReplace(s, "#1", mReplaceables[0][replacementIndexes[0]]);
388 }
389 if (mReplaceables.size() > 1) {
390 s = stringReplace(s, "#2", mReplaceables[1][replacementIndexes[1]]);
391 }
392 if (mReplaceables.size() > 2) {
393 s = stringReplace(s, "#3", mReplaceables[2][replacementIndexes[2]]);
394 }
395 if (mReplaceables.size() > 3) {
396 s = stringReplace(s, "#4", mReplaceables[3][replacementIndexes[3]]);
397 }
398 return s;
399}
400
401void FunctionSpecification::expandStringVector(const vector<string>& in,
402 int replacementIndexes[MAX_REPLACEABLES],
403 vector<string>* out) const {
404 out->clear();
405 for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
406 out->push_back(expandString(*iter, replacementIndexes));
407 }
408}
409
410void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) {
411 int start[MAX_REPLACEABLES];
412 int end[MAX_REPLACEABLES];
413 for (int i = 0; i < MAX_REPLACEABLES; i++) {
414 if (i < (int)mReplaceables.size()) {
415 start[i] = 0;
416 end[i] = mReplaceables[i].size();
417 } else {
418 start[i] = -1;
419 end[i] = 0;
420 }
421 }
422 int replacementIndexes[MAX_REPLACEABLES];
423 // TODO: These loops assume that MAX_REPLACEABLES is 4.
424 for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3];
425 replacementIndexes[3]++) {
426 for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2];
427 replacementIndexes[2]++) {
428 for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1];
429 replacementIndexes[1]++) {
430 for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0];
431 replacementIndexes[0]++) {
432 auto p = new FunctionPermutation(function, this, replacementIndexes, scanner);
433 mPermutations.push_back(p);
434 }
435 }
436 }
437 }
438}
439
440string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const {
441 return expandString(mUnexpandedName, replacementIndexes);
442}
443
444void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES],
445 std::string* retType, int* lineNumber) const {
446 *retType = expandString(mReturn->type, replacementIndexes);
447 *lineNumber = mReturn->lineNumber;
448}
449
450void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES],
451 std::string* type, std::string* name, std::string* testOption,
452 int* lineNumber) const {
453 ParameterEntry* p = mParameters[index];
454 *type = expandString(p->type, replacementIndexes);
455 *name = p->name;
456 *testOption = expandString(p->testOption, replacementIndexes);
457 *lineNumber = p->lineNumber;
458}
459
460void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES],
461 std::vector<std::string>* inlines) const {
462 expandStringVector(mInline, replacementIndexes, inlines);
463}
464
465void FunctionSpecification::parseTest(Scanner* scanner) {
466 const string value = scanner->getValue();
467 if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" ||
468 value == "none") {
469 mTest = value;
470 } else if (value.compare(0, 7, "limited") == 0) {
471 mTest = "limited";
472 if (value.compare(7, 1, "(") == 0) {
473 size_t pParen = value.find(')');
474 if (pParen == string::npos) {
475 scanner->error() << "Incorrect test: \"" << value << "\"\n";
476 } else {
477 mPrecisionLimit = value.substr(8, pParen - 8);
478 }
479 }
480 } else {
481 scanner->error() << "Unrecognized test option: \"" << value << "\"\n";
482 }
483}
484
485bool FunctionSpecification::hasTests(int versionOfTestFiles) const {
486 if (mVersionInfo.minVersion != 0 && mVersionInfo.minVersion > versionOfTestFiles) {
487 return false;
488 }
489 if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) {
490 return false;
491 }
492 if (mTest == "none") {
493 return false;
494 }
495 return true;
496}
497
498void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile) {
499 // Some functions like convert have # part of the name. Truncate at that point.
500 string name = scanner->getValue();
501 size_t p = name.find('#');
502 if (p != string::npos) {
503 if (p > 0 && name[p - 1] == '_') {
504 p--;
505 }
506 name.erase(p);
507 }
508
509 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700510 Function* function = systemSpecification.findOrCreateFunction(name, &created);
511 FunctionSpecification* spec = new FunctionSpecification(function);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700512 function->addSpecification(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700513 specFile->addFunctionSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700514
515 spec->mUnexpandedName = scanner->getValue();
516 spec->mTest = "scalar"; // default
517
518 spec->scanVersionInfo(scanner);
519
520 if (scanner->findOptionalTag("attrib:")) {
521 spec->mAttribute = scanner->getValue();
522 }
523 if (scanner->findOptionalTag("w:")) {
524 vector<string> t;
525 if (scanner->getValue().find("1") != string::npos) {
526 t.push_back("");
527 }
528 if (scanner->getValue().find("2") != string::npos) {
529 t.push_back("2");
530 }
531 if (scanner->getValue().find("3") != string::npos) {
532 t.push_back("3");
533 }
534 if (scanner->getValue().find("4") != string::npos) {
535 t.push_back("4");
536 }
537 spec->mReplaceables.push_back(t);
538 }
539
540 while (scanner->findOptionalTag("t:")) {
541 spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue()));
542 }
543
544 if (scanner->findTag("ret:")) {
545 ParameterEntry* p = scanner->parseArgString(true);
546 function->addReturn(p, scanner);
547 spec->mReturn = p;
548 }
549 while (scanner->findOptionalTag("arg:")) {
550 ParameterEntry* p = scanner->parseArgString(false);
551 function->addParameter(p, scanner);
552 spec->mParameters.push_back(p);
553 }
554
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700555 function->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700556
557 if (scanner->findOptionalTag("inline:")) {
558 scanner->checkNoValue();
559 while (scanner->findOptionalTag("")) {
560 spec->mInline.push_back(scanner->getValue());
561 }
562 }
563 if (scanner->findOptionalTag("test:")) {
564 spec->parseTest(scanner);
565 }
566
567 scanner->findTag("end:");
568
569 spec->createPermutations(function, scanner);
570}
571
572FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec,
573 int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner)
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700574 : mReturn(nullptr), mInputCount(0), mOutputCount(0) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700575 // We expand the strings now to make capitalization easier. The previous code preserved
576 // the #n
577 // markers just before emitting, which made capitalization difficult.
578 mName = spec->getName(replacementIndexes);
579 mNameTrunk = func->getName();
580 mTest = spec->getTest();
581 mPrecisionLimit = spec->getPrecisionLimit();
582 spec->getInlines(replacementIndexes, &mInline);
583
584 mHasFloatAnswers = false;
585 for (size_t i = 0; i < spec->getNumberOfParams(); i++) {
586 string type, name, testOption;
587 int lineNumber = 0;
588 spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber);
589 ParameterDefinition* def = new ParameterDefinition();
590 def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner);
591 if (def->isOutParameter) {
592 mOutputCount++;
593 } else {
594 mInputCount++;
595 }
596
597 if (def->typeIndex < 0 && mTest != "none") {
598 scanner->error(lineNumber)
599 << "Could not find " << def->rsBaseType
600 << " while generating automated tests. Use test: none if not needed.\n";
601 }
602 if (def->isOutParameter && def->isFloatType) {
603 mHasFloatAnswers = true;
604 }
605 mParams.push_back(def);
606 }
607
608 string retType;
609 int lineNumber = 0;
610 spec->getReturn(replacementIndexes, &retType, &lineNumber);
611 if (!retType.empty()) {
612 mReturn = new ParameterDefinition();
613 mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner);
614 if (mReturn->isFloatType) {
615 mHasFloatAnswers = true;
616 }
617 mOutputCount++;
618 }
619}
620
621FunctionPermutation::~FunctionPermutation() {
622 for (auto i : mParams) {
623 delete i;
624 }
625 delete mReturn;
626}
627
628SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) {
629 string core = mSpecFileName;
630 // Remove .spec
631 size_t l = core.length();
632 const char SPEC[] = ".spec";
633 const int SPEC_SIZE = sizeof(SPEC) - 1;
634 const int start = l - SPEC_SIZE;
635 if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) {
636 core.erase(start);
637 }
638
639 // The header file name should have the same base but with a ".rsh" extension.
640 mHeaderFileName = core + ".rsh";
Jean-Luc Brouilletd9935ee2015-04-03 17:27:02 -0700641 mDetailedDocumentationUrl = core + ".html";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700642}
643
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700644void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) {
645 mConstantSpecificationsList.push_back(spec);
646 if (hasDocumentation) {
647 Constant* constant = spec->getConstant();
648 mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700649 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700650}
651
652void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) {
653 mTypeSpecificationsList.push_back(spec);
654 if (hasDocumentation) {
655 Type* type = spec->getType();
656 mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700657 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700658}
659
660void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) {
661 mFunctionSpecificationsList.push_back(spec);
662 if (hasDocumentation) {
663 Function* function = spec->getFunction();
664 mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700665 }
666}
667
668// Read the specification, adding the definitions to the global functions map.
669bool SpecFile::readSpecFile() {
670 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
671 if (!specFile) {
672 cerr << "Error opening input file: " << mSpecFileName << "\n";
673 return false;
674 }
675
676 Scanner scanner(mSpecFileName, specFile);
677
678 // Scan the header that should start the file.
679 scanner.skipBlankEntries();
680 if (scanner.findTag("header:")) {
681 if (scanner.findTag("summary:")) {
682 mBriefDescription = scanner.getValue();
683 }
684 if (scanner.findTag("description:")) {
685 scanner.checkNoValue();
686 while (scanner.findOptionalTag("")) {
687 mFullDescription.push_back(scanner.getValue());
688 }
689 }
690 if (scanner.findOptionalTag("include:")) {
691 scanner.checkNoValue();
692 while (scanner.findOptionalTag("")) {
693 mVerbatimInclude.push_back(scanner.getValue());
694 }
695 }
696 scanner.findTag("end:");
697 }
698
699 while (1) {
700 scanner.skipBlankEntries();
701 if (scanner.atEnd()) {
702 break;
703 }
704 const string tag = scanner.getNextTag();
705 if (tag == "function:") {
706 FunctionSpecification::scanFunctionSpecification(&scanner, this);
707 } else if (tag == "type:") {
708 TypeSpecification::scanTypeSpecification(&scanner, this);
709 } else if (tag == "constant:") {
710 ConstantSpecification::scanConstantSpecification(&scanner, this);
711 } else {
712 scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
713 return false;
714 }
715 }
716
717 fclose(specFile);
718 return scanner.getErrorCount() == 0;
719}
720
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700721SystemSpecification::~SystemSpecification() {
722 for (auto i : mConstants) {
723 delete i.second;
724 }
725 for (auto i : mTypes) {
726 delete i.second;
727 }
728 for (auto i : mFunctions) {
729 delete i.second;
730 }
731 for (auto i : mSpecFiles) {
732 delete i;
733 }
734}
735
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700736// Returns the named entry in the map. Creates it if it's not there.
737template <class T>
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700738T* findOrCreate(const string& name, map<string, T*>* map, bool* created) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700739 auto iter = map->find(name);
740 if (iter != map->end()) {
741 *created = false;
742 return iter->second;
743 }
744 *created = true;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700745 T* f = new T(name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700746 map->insert(pair<string, T*>(name, f));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700747 return f;
748}
749
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700750Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) {
751 return findOrCreate<Constant>(name, &mConstants, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700752}
753
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700754Type* SystemSpecification::findOrCreateType(const string& name, bool* created) {
755 return findOrCreate<Type>(name, &mTypes, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700756}
757
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700758Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) {
759 return findOrCreate<Function>(name, &mFunctions, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700760}
761
762bool SystemSpecification::readSpecFile(const string& fileName) {
763 SpecFile* spec = new SpecFile(fileName);
764 if (!spec->readSpecFile()) {
765 cerr << fileName << ": Failed to parse.\n";
766 return false;
767 }
768 mSpecFiles.push_back(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700769 return true;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700770}
771
772bool SystemSpecification::generateFiles(int versionOfTestFiles) const {
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700773 bool success = generateHeaderFiles("scriptc") && generateHtmlDocumentation("html") &&
774 generateTestFiles("test", versionOfTestFiles);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700775 if (success) {
776 cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
777 << " constants, and " << mFunctions.size() << " functions.\n";
778 }
779 return success;
780}
781
782string SystemSpecification::getHtmlAnchor(const string& name) const {
783 Definition* d = nullptr;
784 auto c = mConstants.find(name);
785 if (c != mConstants.end()) {
786 d = c->second;
787 } else {
788 auto t = mTypes.find(name);
789 if (t != mTypes.end()) {
790 d = t->second;
791 } else {
792 auto f = mFunctions.find(name);
793 if (f != mFunctions.end()) {
794 d = f->second;
795 } else {
796 return string();
797 }
798 }
799 }
800 ostringstream stream;
801 stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
802 return stream.str();
803}