blob: 92d14d612533be7058345e29779ec0bc1f8d703c [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
53const char BASE_URL[] = "http://developer.android.com/reference/android/graphics/drawable/";
54
55// The singleton of the collected information of all the spec files.
56SystemSpecification systemSpecification;
57
58// Returns the index in TYPES for the provided cType
59static int findCType(const string& cType) {
60 for (int i = 0; i < NUM_TYPES; i++) {
61 if (cType == TYPES[i].cType) {
62 return i;
63 }
64 }
65 return -1;
66}
67
68/* Converts a string like "u8, u16" to a vector of "ushort", "uint".
69 * For non-numerical types, we don't need to convert the abbreviation.
70 */
71static vector<string> convertToTypeVector(const string& input) {
72 // First convert the string to an array of strings.
73 vector<string> entries;
74 stringstream stream(input);
75 string entry;
76 while (getline(stream, entry, ',')) {
77 trimSpaces(&entry);
78 entries.push_back(entry);
79 }
80
81 /* Second, we look for present numerical types. We do it this way
82 * so the order of numerical types is always the same, no matter
83 * how specified in the spec file.
84 */
85 vector<string> result;
86 for (auto t : TYPES) {
87 for (auto i = entries.begin(); i != entries.end(); ++i) {
88 if (*i == t.specType) {
89 result.push_back(t.cType);
90 entries.erase(i);
91 break;
92 }
93 }
94 }
95
96 // Add the remaining; they are not numerical types.
97 for (auto s : entries) {
98 result.push_back(s);
99 }
100
101 return result;
102}
103
104void ParameterDefinition::parseParameterDefinition(const string& type, const string& name,
105 const string& testOption, int lineNumber,
106 bool isReturn, Scanner* scanner) {
107 rsType = type;
108 specName = name;
109
110 // Determine if this is an output.
111 isOutParameter = isReturn || charRemoved('*', &rsType);
112
113 // Extract the vector size out of the type.
114 int last = rsType.size() - 1;
115 char lastChar = rsType[last];
116 if (lastChar >= '0' && lastChar <= '9') {
117 rsBaseType = rsType.substr(0, last);
118 mVectorSize = lastChar;
119 } else {
120 rsBaseType = rsType;
121 mVectorSize = "1";
122 }
123 if (mVectorSize == "3") {
124 vectorWidth = "4";
125 } else {
126 vectorWidth = mVectorSize;
127 }
128
129 /* Create variable names to be used in the java and .rs files. Because x and
130 * y are reserved in .rs files, we prefix variable names with "in" or "out".
131 */
132 if (isOutParameter) {
133 variableName = "out";
134 if (!specName.empty()) {
135 variableName += capitalize(specName);
136 } else if (!isReturn) {
137 scanner->error(lineNumber) << "Should have a name.\n";
138 }
139 } else {
140 variableName = "in";
141 if (specName.empty()) {
142 scanner->error(lineNumber) << "Should have a name.\n";
143 }
144 variableName += capitalize(specName);
145 }
146 rsAllocName = "gAlloc" + capitalize(variableName);
147 javaAllocName = variableName;
148 javaArrayName = "array" + capitalize(javaAllocName);
149
150 // Process the option.
151 undefinedIfOutIsNan = false;
152 compatibleTypeIndex = -1;
153 if (!testOption.empty()) {
154 if (testOption.compare(0, 6, "range(") == 0) {
155 size_t pComma = testOption.find(',');
156 size_t pParen = testOption.find(')');
157 if (pComma == string::npos || pParen == string::npos) {
158 scanner->error(lineNumber) << "Incorrect range " << testOption << "\n";
159 } else {
160 minValue = testOption.substr(6, pComma - 6);
161 maxValue = testOption.substr(pComma + 1, pParen - pComma - 1);
162 }
163 } else if (testOption.compare(0, 6, "above(") == 0) {
164 size_t pParen = testOption.find(')');
165 if (pParen == string::npos) {
166 scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
167 } else {
168 smallerParameter = testOption.substr(6, pParen - 6);
169 }
170 } else if (testOption.compare(0, 11, "compatible(") == 0) {
171 size_t pParen = testOption.find(')');
172 if (pParen == string::npos) {
173 scanner->error(lineNumber) << "Incorrect testOption " << testOption << "\n";
174 } else {
175 compatibleTypeIndex = findCType(testOption.substr(11, pParen - 11));
176 }
177 } else if (testOption.compare(0, 11, "conditional") == 0) {
178 undefinedIfOutIsNan = true;
179 } else {
180 scanner->error(lineNumber) << "Unrecognized testOption " << testOption << "\n";
181 }
182 }
183
184 typeIndex = findCType(rsBaseType);
185 isFloatType = false;
186 if (typeIndex >= 0) {
187 javaBaseType = TYPES[typeIndex].javaType;
188 specType = TYPES[typeIndex].specType;
189 isFloatType = TYPES[typeIndex].exponentBits > 0;
190 }
191 if (!minValue.empty()) {
192 if (typeIndex < 0 || TYPES[typeIndex].kind != FLOATING_POINT) {
193 scanner->error(lineNumber) << "range(,) is only supported for floating point\n";
194 }
195 }
196}
197
198void VersionInfo::scan(Scanner* scanner) {
199 if (scanner->findOptionalTag("version:")) {
200 const string s = scanner->getValue();
201 sscanf(s.c_str(), "%i %i", &minVersion, &maxVersion);
202 if (minVersion && minVersion < MIN_API_LEVEL) {
203 scanner->error() << "Minimum version must >= 9\n";
204 }
205 if (minVersion == MIN_API_LEVEL) {
206 minVersion = 0;
207 }
208 if (maxVersion && maxVersion < MIN_API_LEVEL) {
209 scanner->error() << "Maximum version must >= 9\n";
210 }
211 }
212 if (scanner->findOptionalTag("size:")) {
213 sscanf(scanner->getValue().c_str(), "%i", &intSize);
214 }
215}
216
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700217Definition::Definition(const std::string& name) : mName(name), mHidden(false) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700218}
219
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700220void Definition::scanDocumentationTags(Scanner* scanner, bool firstOccurence,
221 const SpecFile* specFile) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700222 if (scanner->findOptionalTag("hidden:")) {
223 scanner->checkNoValue();
224 mHidden = true;
225 }
226 if (firstOccurence) {
227 if (scanner->findTag("summary:")) {
228 mSummary = scanner->getValue();
229 }
230 if (scanner->findTag("description:")) {
231 scanner->checkNoValue();
232 while (scanner->findOptionalTag("")) {
233 mDescription.push_back(scanner->getValue());
234 }
235 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700236 mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + mName;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700237 } else if (scanner->findOptionalTag("summary:")) {
238 scanner->error() << "Only the first specification should have a summary.\n";
239 }
240}
241
242Constant::~Constant() {
243 for (auto i : mSpecifications) {
244 delete i;
245 }
246}
247
248Type::~Type() {
249 for (auto i : mSpecifications) {
250 delete i;
251 }
252}
253
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700254Function::Function(const string& name) : Definition(name) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700255 mCapitalizedName = capitalize(mName);
256}
257
258Function::~Function() {
259 for (auto i : mSpecifications) {
260 delete i;
261 }
262}
263
264bool Function::someParametersAreDocumented() const {
265 for (auto p : mParameters) {
266 if (!p->documentation.empty()) {
267 return true;
268 }
269 }
270 return false;
271}
272
273void Function::addParameter(ParameterEntry* entry, Scanner* scanner) {
274 for (auto i : mParameters) {
275 if (i->name == entry->name) {
276 // It's a duplicate.
277 if (!entry->documentation.empty()) {
278 scanner->error(entry->lineNumber)
279 << "Only the first occurence of an arg should have the "
280 "documentation.\n";
281 }
282 return;
283 }
284 }
285 mParameters.push_back(entry);
286}
287
288void Function::addReturn(ParameterEntry* entry, Scanner* scanner) {
289 if (entry->documentation.empty()) {
290 return;
291 }
292 if (!mReturnDocumentation.empty()) {
293 scanner->error() << "ret: should be documented only for the first variant\n";
294 }
295 mReturnDocumentation = entry->documentation;
296}
297
298void Specification::scanVersionInfo(Scanner* scanner) {
299 mVersionInfo.scan(scanner);
300}
301
302void ConstantSpecification::scanConstantSpecification(Scanner* scanner, SpecFile* specFile) {
303 string name = scanner->getValue();
304
305 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700306 Constant* constant = systemSpecification.findOrCreateConstant(name, &created);
307 ConstantSpecification* spec = new ConstantSpecification(constant);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700308 constant->addSpecification(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700309 specFile->addConstantSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700310
311 spec->scanVersionInfo(scanner);
312 if (scanner->findTag("value:")) {
313 spec->mValue = scanner->getValue();
314 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700315 constant->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700316
317 scanner->findTag("end:");
318}
319
320void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile) {
321 string name = scanner->getValue();
322
323 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700324 Type* type = systemSpecification.findOrCreateType(name, &created);
325 TypeSpecification* spec = new TypeSpecification(type);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700326 type->addSpecification(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700327 specFile->addTypeSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700328
329 spec->scanVersionInfo(scanner);
330 if (scanner->findOptionalTag("simple:")) {
331 spec->mKind = SIMPLE;
332 spec->mSimpleType = scanner->getValue();
333 }
334 if (scanner->findOptionalTag("struct:")) {
335 spec->mKind = STRUCT;
336 spec->mStructName = scanner->getValue();
337 while (scanner->findOptionalTag("field:")) {
338 string s = scanner->getValue();
339 string comment;
340 scanner->parseDocumentation(&s, &comment);
341 spec->mFields.push_back(s);
342 spec->mFieldComments.push_back(comment);
343 }
344 if (scanner->findOptionalTag("attrib:")) {
345 spec->mAttrib = scanner->getValue();
346 }
347 }
348 if (scanner->findOptionalTag("enum:")) {
349 spec->mKind = ENUM;
350 spec->mEnumName = scanner->getValue();
351 while (scanner->findOptionalTag("value:")) {
352 string s = scanner->getValue();
353 string comment;
354 scanner->parseDocumentation(&s, &comment);
355 spec->mValues.push_back(s);
356 spec->mValueComments.push_back(comment);
357 }
358 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700359 type->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700360
361 scanner->findTag("end:");
362}
363
364FunctionSpecification::~FunctionSpecification() {
365 for (auto i : mParameters) {
366 delete i;
367 }
368 delete mReturn;
369 for (auto i : mPermutations) {
370 delete i;
371 }
372}
373
374string FunctionSpecification::expandString(string s,
375 int replacementIndexes[MAX_REPLACEABLES]) const {
376 if (mReplaceables.size() > 0) {
377 s = stringReplace(s, "#1", mReplaceables[0][replacementIndexes[0]]);
378 }
379 if (mReplaceables.size() > 1) {
380 s = stringReplace(s, "#2", mReplaceables[1][replacementIndexes[1]]);
381 }
382 if (mReplaceables.size() > 2) {
383 s = stringReplace(s, "#3", mReplaceables[2][replacementIndexes[2]]);
384 }
385 if (mReplaceables.size() > 3) {
386 s = stringReplace(s, "#4", mReplaceables[3][replacementIndexes[3]]);
387 }
388 return s;
389}
390
391void FunctionSpecification::expandStringVector(const vector<string>& in,
392 int replacementIndexes[MAX_REPLACEABLES],
393 vector<string>* out) const {
394 out->clear();
395 for (vector<string>::const_iterator iter = in.begin(); iter != in.end(); iter++) {
396 out->push_back(expandString(*iter, replacementIndexes));
397 }
398}
399
400void FunctionSpecification::createPermutations(Function* function, Scanner* scanner) {
401 int start[MAX_REPLACEABLES];
402 int end[MAX_REPLACEABLES];
403 for (int i = 0; i < MAX_REPLACEABLES; i++) {
404 if (i < (int)mReplaceables.size()) {
405 start[i] = 0;
406 end[i] = mReplaceables[i].size();
407 } else {
408 start[i] = -1;
409 end[i] = 0;
410 }
411 }
412 int replacementIndexes[MAX_REPLACEABLES];
413 // TODO: These loops assume that MAX_REPLACEABLES is 4.
414 for (replacementIndexes[3] = start[3]; replacementIndexes[3] < end[3];
415 replacementIndexes[3]++) {
416 for (replacementIndexes[2] = start[2]; replacementIndexes[2] < end[2];
417 replacementIndexes[2]++) {
418 for (replacementIndexes[1] = start[1]; replacementIndexes[1] < end[1];
419 replacementIndexes[1]++) {
420 for (replacementIndexes[0] = start[0]; replacementIndexes[0] < end[0];
421 replacementIndexes[0]++) {
422 auto p = new FunctionPermutation(function, this, replacementIndexes, scanner);
423 mPermutations.push_back(p);
424 }
425 }
426 }
427 }
428}
429
430string FunctionSpecification::getName(int replacementIndexes[MAX_REPLACEABLES]) const {
431 return expandString(mUnexpandedName, replacementIndexes);
432}
433
434void FunctionSpecification::getReturn(int replacementIndexes[MAX_REPLACEABLES],
435 std::string* retType, int* lineNumber) const {
436 *retType = expandString(mReturn->type, replacementIndexes);
437 *lineNumber = mReturn->lineNumber;
438}
439
440void FunctionSpecification::getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES],
441 std::string* type, std::string* name, std::string* testOption,
442 int* lineNumber) const {
443 ParameterEntry* p = mParameters[index];
444 *type = expandString(p->type, replacementIndexes);
445 *name = p->name;
446 *testOption = expandString(p->testOption, replacementIndexes);
447 *lineNumber = p->lineNumber;
448}
449
450void FunctionSpecification::getInlines(int replacementIndexes[MAX_REPLACEABLES],
451 std::vector<std::string>* inlines) const {
452 expandStringVector(mInline, replacementIndexes, inlines);
453}
454
455void FunctionSpecification::parseTest(Scanner* scanner) {
456 const string value = scanner->getValue();
457 if (value == "scalar" || value == "vector" || value == "noverify" || value == "custom" ||
458 value == "none") {
459 mTest = value;
460 } else if (value.compare(0, 7, "limited") == 0) {
461 mTest = "limited";
462 if (value.compare(7, 1, "(") == 0) {
463 size_t pParen = value.find(')');
464 if (pParen == string::npos) {
465 scanner->error() << "Incorrect test: \"" << value << "\"\n";
466 } else {
467 mPrecisionLimit = value.substr(8, pParen - 8);
468 }
469 }
470 } else {
471 scanner->error() << "Unrecognized test option: \"" << value << "\"\n";
472 }
473}
474
475bool FunctionSpecification::hasTests(int versionOfTestFiles) const {
476 if (mVersionInfo.minVersion != 0 && mVersionInfo.minVersion > versionOfTestFiles) {
477 return false;
478 }
479 if (mVersionInfo.maxVersion != 0 && mVersionInfo.maxVersion < versionOfTestFiles) {
480 return false;
481 }
482 if (mTest == "none") {
483 return false;
484 }
485 return true;
486}
487
488void FunctionSpecification::scanFunctionSpecification(Scanner* scanner, SpecFile* specFile) {
489 // Some functions like convert have # part of the name. Truncate at that point.
490 string name = scanner->getValue();
491 size_t p = name.find('#');
492 if (p != string::npos) {
493 if (p > 0 && name[p - 1] == '_') {
494 p--;
495 }
496 name.erase(p);
497 }
498
499 bool created = false;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700500 Function* function = systemSpecification.findOrCreateFunction(name, &created);
501 FunctionSpecification* spec = new FunctionSpecification(function);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700502 function->addSpecification(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700503 specFile->addFunctionSpecification(spec, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700504
505 spec->mUnexpandedName = scanner->getValue();
506 spec->mTest = "scalar"; // default
507
508 spec->scanVersionInfo(scanner);
509
510 if (scanner->findOptionalTag("attrib:")) {
511 spec->mAttribute = scanner->getValue();
512 }
513 if (scanner->findOptionalTag("w:")) {
514 vector<string> t;
515 if (scanner->getValue().find("1") != string::npos) {
516 t.push_back("");
517 }
518 if (scanner->getValue().find("2") != string::npos) {
519 t.push_back("2");
520 }
521 if (scanner->getValue().find("3") != string::npos) {
522 t.push_back("3");
523 }
524 if (scanner->getValue().find("4") != string::npos) {
525 t.push_back("4");
526 }
527 spec->mReplaceables.push_back(t);
528 }
529
530 while (scanner->findOptionalTag("t:")) {
531 spec->mReplaceables.push_back(convertToTypeVector(scanner->getValue()));
532 }
533
534 if (scanner->findTag("ret:")) {
535 ParameterEntry* p = scanner->parseArgString(true);
536 function->addReturn(p, scanner);
537 spec->mReturn = p;
538 }
539 while (scanner->findOptionalTag("arg:")) {
540 ParameterEntry* p = scanner->parseArgString(false);
541 function->addParameter(p, scanner);
542 spec->mParameters.push_back(p);
543 }
544
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700545 function->scanDocumentationTags(scanner, created, specFile);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700546
547 if (scanner->findOptionalTag("inline:")) {
548 scanner->checkNoValue();
549 while (scanner->findOptionalTag("")) {
550 spec->mInline.push_back(scanner->getValue());
551 }
552 }
553 if (scanner->findOptionalTag("test:")) {
554 spec->parseTest(scanner);
555 }
556
557 scanner->findTag("end:");
558
559 spec->createPermutations(function, scanner);
560}
561
562FunctionPermutation::FunctionPermutation(Function* func, FunctionSpecification* spec,
563 int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner)
564 : mFunction(func), mReturn(nullptr), mInputCount(0), mOutputCount(0) {
565 // We expand the strings now to make capitalization easier. The previous code preserved
566 // the #n
567 // markers just before emitting, which made capitalization difficult.
568 mName = spec->getName(replacementIndexes);
569 mNameTrunk = func->getName();
570 mTest = spec->getTest();
571 mPrecisionLimit = spec->getPrecisionLimit();
572 spec->getInlines(replacementIndexes, &mInline);
573
574 mHasFloatAnswers = false;
575 for (size_t i = 0; i < spec->getNumberOfParams(); i++) {
576 string type, name, testOption;
577 int lineNumber = 0;
578 spec->getParam(i, replacementIndexes, &type, &name, &testOption, &lineNumber);
579 ParameterDefinition* def = new ParameterDefinition();
580 def->parseParameterDefinition(type, name, testOption, lineNumber, false, scanner);
581 if (def->isOutParameter) {
582 mOutputCount++;
583 } else {
584 mInputCount++;
585 }
586
587 if (def->typeIndex < 0 && mTest != "none") {
588 scanner->error(lineNumber)
589 << "Could not find " << def->rsBaseType
590 << " while generating automated tests. Use test: none if not needed.\n";
591 }
592 if (def->isOutParameter && def->isFloatType) {
593 mHasFloatAnswers = true;
594 }
595 mParams.push_back(def);
596 }
597
598 string retType;
599 int lineNumber = 0;
600 spec->getReturn(replacementIndexes, &retType, &lineNumber);
601 if (!retType.empty()) {
602 mReturn = new ParameterDefinition();
603 mReturn->parseParameterDefinition(retType, "", "", lineNumber, true, scanner);
604 if (mReturn->isFloatType) {
605 mHasFloatAnswers = true;
606 }
607 mOutputCount++;
608 }
609}
610
611FunctionPermutation::~FunctionPermutation() {
612 for (auto i : mParams) {
613 delete i;
614 }
615 delete mReturn;
616}
617
618SpecFile::SpecFile(const string& specFileName) : mSpecFileName(specFileName) {
619 string core = mSpecFileName;
620 // Remove .spec
621 size_t l = core.length();
622 const char SPEC[] = ".spec";
623 const int SPEC_SIZE = sizeof(SPEC) - 1;
624 const int start = l - SPEC_SIZE;
625 if (start >= 0 && core.compare(start, SPEC_SIZE, SPEC) == 0) {
626 core.erase(start);
627 }
628
629 // The header file name should have the same base but with a ".rsh" extension.
630 mHeaderFileName = core + ".rsh";
631
632 mDetailedDocumentationUrl = BASE_URL;
633 mDetailedDocumentationUrl += core + ".html";
634}
635
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700636void SpecFile::addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation) {
637 mConstantSpecificationsList.push_back(spec);
638 if (hasDocumentation) {
639 Constant* constant = spec->getConstant();
640 mDocumentedConstants.insert(pair<string, Constant*>(constant->getName(), constant));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700641 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700642}
643
644void SpecFile::addTypeSpecification(TypeSpecification* spec, bool hasDocumentation) {
645 mTypeSpecificationsList.push_back(spec);
646 if (hasDocumentation) {
647 Type* type = spec->getType();
648 mDocumentedTypes.insert(pair<string, Type*>(type->getName(), type));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700649 }
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700650}
651
652void SpecFile::addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation) {
653 mFunctionSpecificationsList.push_back(spec);
654 if (hasDocumentation) {
655 Function* function = spec->getFunction();
656 mDocumentedFunctions.insert(pair<string, Function*>(function->getName(), function));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700657 }
658}
659
660// Read the specification, adding the definitions to the global functions map.
661bool SpecFile::readSpecFile() {
662 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
663 if (!specFile) {
664 cerr << "Error opening input file: " << mSpecFileName << "\n";
665 return false;
666 }
667
668 Scanner scanner(mSpecFileName, specFile);
669
670 // Scan the header that should start the file.
671 scanner.skipBlankEntries();
672 if (scanner.findTag("header:")) {
673 if (scanner.findTag("summary:")) {
674 mBriefDescription = scanner.getValue();
675 }
676 if (scanner.findTag("description:")) {
677 scanner.checkNoValue();
678 while (scanner.findOptionalTag("")) {
679 mFullDescription.push_back(scanner.getValue());
680 }
681 }
682 if (scanner.findOptionalTag("include:")) {
683 scanner.checkNoValue();
684 while (scanner.findOptionalTag("")) {
685 mVerbatimInclude.push_back(scanner.getValue());
686 }
687 }
688 scanner.findTag("end:");
689 }
690
691 while (1) {
692 scanner.skipBlankEntries();
693 if (scanner.atEnd()) {
694 break;
695 }
696 const string tag = scanner.getNextTag();
697 if (tag == "function:") {
698 FunctionSpecification::scanFunctionSpecification(&scanner, this);
699 } else if (tag == "type:") {
700 TypeSpecification::scanTypeSpecification(&scanner, this);
701 } else if (tag == "constant:") {
702 ConstantSpecification::scanConstantSpecification(&scanner, this);
703 } else {
704 scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
705 return false;
706 }
707 }
708
709 fclose(specFile);
710 return scanner.getErrorCount() == 0;
711}
712
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700713SystemSpecification::~SystemSpecification() {
714 for (auto i : mConstants) {
715 delete i.second;
716 }
717 for (auto i : mTypes) {
718 delete i.second;
719 }
720 for (auto i : mFunctions) {
721 delete i.second;
722 }
723 for (auto i : mSpecFiles) {
724 delete i;
725 }
726}
727
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700728// Returns the named entry in the map. Creates it if it's not there.
729template <class T>
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700730T* findOrCreate(const string& name, map<string, T*>* map, bool* created) {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700731 auto iter = map->find(name);
732 if (iter != map->end()) {
733 *created = false;
734 return iter->second;
735 }
736 *created = true;
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700737 T* f = new T(name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700738 map->insert(pair<string, T*>(name, f));
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700739 return f;
740}
741
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700742Constant* SystemSpecification::findOrCreateConstant(const string& name, bool* created) {
743 return findOrCreate<Constant>(name, &mConstants, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700744}
745
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700746Type* SystemSpecification::findOrCreateType(const string& name, bool* created) {
747 return findOrCreate<Type>(name, &mTypes, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700748}
749
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700750Function* SystemSpecification::findOrCreateFunction(const string& name, bool* created) {
751 return findOrCreate<Function>(name, &mFunctions, created);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700752}
753
754bool SystemSpecification::readSpecFile(const string& fileName) {
755 SpecFile* spec = new SpecFile(fileName);
756 if (!spec->readSpecFile()) {
757 cerr << fileName << ": Failed to parse.\n";
758 return false;
759 }
760 mSpecFiles.push_back(spec);
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700761 return true;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700762}
763
764bool SystemSpecification::generateFiles(int versionOfTestFiles) const {
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -0700765 bool success = GenerateHeaderFiles("scriptc") && generateHtmlDocumentation("html") &&
766 GenerateTestFiles("test", versionOfTestFiles);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700767 if (success) {
768 cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
769 << " constants, and " << mFunctions.size() << " functions.\n";
770 }
771 return success;
772}
773
774string SystemSpecification::getHtmlAnchor(const string& name) const {
775 Definition* d = nullptr;
776 auto c = mConstants.find(name);
777 if (c != mConstants.end()) {
778 d = c->second;
779 } else {
780 auto t = mTypes.find(name);
781 if (t != mTypes.end()) {
782 d = t->second;
783 } else {
784 auto f = mFunctions.find(name);
785 if (f != mFunctions.end()) {
786 d = f->second;
787 } else {
788 return string();
789 }
790 }
791 }
792 ostringstream stream;
793 stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
794 return stream.str();
795}