blob: 36f406c09d8ac057d971cbcbea299adcdfb07c0c [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
217Definition::Definition(const std::string& name, SpecFile* specFile) : mName(name), mHidden(false) {
218 mSpecFileName = specFile->getSpecFileName();
219 mUrl = specFile->getDetailedDocumentationUrl() + "#android_rs:" + name;
220}
221
222void Definition::scanDocumentationTags(Scanner* scanner, bool firstOccurence) {
223 if (scanner->findOptionalTag("hidden:")) {
224 scanner->checkNoValue();
225 mHidden = true;
226 }
227 if (firstOccurence) {
228 if (scanner->findTag("summary:")) {
229 mSummary = scanner->getValue();
230 }
231 if (scanner->findTag("description:")) {
232 scanner->checkNoValue();
233 while (scanner->findOptionalTag("")) {
234 mDescription.push_back(scanner->getValue());
235 }
236 }
237 } 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
254Function::Function(const string& name, SpecFile* specFile) : Definition(name, specFile) {
255 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;
306 Constant* constant = specFile->findOrCreateConstant(name, &created);
307
308 ConstantSpecification* spec = new ConstantSpecification();
309 constant->addSpecification(spec);
310
311 spec->scanVersionInfo(scanner);
312 if (scanner->findTag("value:")) {
313 spec->mValue = scanner->getValue();
314 }
315 constant->scanDocumentationTags(scanner, created);
316
317 scanner->findTag("end:");
318}
319
320void TypeSpecification::scanTypeSpecification(Scanner* scanner, SpecFile* specFile) {
321 string name = scanner->getValue();
322
323 bool created = false;
324 Type* type = specFile->findOrCreateType(name, &created);
325
326 TypeSpecification* spec = new TypeSpecification();
327 type->addSpecification(spec);
328
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 }
359 type->scanDocumentationTags(scanner, created);
360
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;
500 Function* function = specFile->findOrCreateFunction(name, &created);
501
502 FunctionSpecification* spec = new FunctionSpecification();
503 function->addSpecification(spec);
504
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
545 function->scanDocumentationTags(scanner, created);
546
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
636SpecFile::~SpecFile() {
637 for (auto i : mConstantsList) {
638 delete i;
639 }
640 for (auto i : mTypesList) {
641 delete i;
642 }
643 for (auto i : mFunctionsList) {
644 delete i;
645 }
646}
647
648// Read the specification, adding the definitions to the global functions map.
649bool SpecFile::readSpecFile() {
650 FILE* specFile = fopen(mSpecFileName.c_str(), "rt");
651 if (!specFile) {
652 cerr << "Error opening input file: " << mSpecFileName << "\n";
653 return false;
654 }
655
656 Scanner scanner(mSpecFileName, specFile);
657
658 // Scan the header that should start the file.
659 scanner.skipBlankEntries();
660 if (scanner.findTag("header:")) {
661 if (scanner.findTag("summary:")) {
662 mBriefDescription = scanner.getValue();
663 }
664 if (scanner.findTag("description:")) {
665 scanner.checkNoValue();
666 while (scanner.findOptionalTag("")) {
667 mFullDescription.push_back(scanner.getValue());
668 }
669 }
670 if (scanner.findOptionalTag("include:")) {
671 scanner.checkNoValue();
672 while (scanner.findOptionalTag("")) {
673 mVerbatimInclude.push_back(scanner.getValue());
674 }
675 }
676 scanner.findTag("end:");
677 }
678
679 while (1) {
680 scanner.skipBlankEntries();
681 if (scanner.atEnd()) {
682 break;
683 }
684 const string tag = scanner.getNextTag();
685 if (tag == "function:") {
686 FunctionSpecification::scanFunctionSpecification(&scanner, this);
687 } else if (tag == "type:") {
688 TypeSpecification::scanTypeSpecification(&scanner, this);
689 } else if (tag == "constant:") {
690 ConstantSpecification::scanConstantSpecification(&scanner, this);
691 } else {
692 scanner.error() << "Expected function:, type:, or constant:. Found: " << tag << "\n";
693 return false;
694 }
695 }
696
697 fclose(specFile);
698 return scanner.getErrorCount() == 0;
699}
700
701// Returns the named entry in the map. Creates it if it's not there.
702template <class T>
703T* findOrCreate(const string& name, list<T*>* list, map<string, T*>* map, bool* created,
704 SpecFile* specFile) {
705 auto iter = map->find(name);
706 if (iter != map->end()) {
707 *created = false;
708 return iter->second;
709 }
710 *created = true;
711 T* f = new T(name, specFile);
712 map->insert(pair<string, T*>(name, f));
713 list->push_back(f);
714 return f;
715}
716
717Constant* SpecFile::findOrCreateConstant(const string& name, bool* created) {
718 return findOrCreate<Constant>(name, &mConstantsList, &mConstantsMap, created, this);
719}
720
721Type* SpecFile::findOrCreateType(const string& name, bool* created) {
722 return findOrCreate<Type>(name, &mTypesList, &mTypesMap, created, this);
723}
724
725Function* SpecFile::findOrCreateFunction(const string& name, bool* created) {
726 return findOrCreate<Function>(name, &mFunctionsList, &mFunctionsMap, created, this);
727}
728
729SystemSpecification::~SystemSpecification() {
730 for (auto i : mSpecFiles) {
731 delete i;
732 }
733}
734
735template <class T>
736static bool addDefinitionToMap(map<string, T*>* map, T* object) {
737 const string& name = object->getName();
738 auto i = map->find(name);
739 if (i != map->end()) {
740 T* existing = i->second;
741 cerr << object->getSpecFileName() << ": Error. " << name << " has already been defined in "
742 << existing->getSpecFileName() << "\n";
743 return false;
744 }
745 (*map)[name] = object;
746 return true;
747}
748
749bool SystemSpecification::readSpecFile(const string& fileName) {
750 SpecFile* spec = new SpecFile(fileName);
751 if (!spec->readSpecFile()) {
752 cerr << fileName << ": Failed to parse.\n";
753 return false;
754 }
755 mSpecFiles.push_back(spec);
756
757 // Store links to the definitions in a global table.
758 bool success = true;
759 for (auto i : spec->getConstantsMap()) {
760 if (!addDefinitionToMap(&mConstants, i.second)) {
761 success = false;
762 }
763 }
764 for (auto i : spec->getTypesMap()) {
765 if (!addDefinitionToMap(&mTypes, i.second)) {
766 success = false;
767 }
768 }
769 for (auto i : spec->getFunctionsMap()) {
770 if (!addDefinitionToMap(&mFunctions, i.second)) {
771 success = false;
772 }
773 }
774
775 return success;
776}
777
778bool SystemSpecification::generateFiles(int versionOfTestFiles) const {
779 bool success = GenerateHeaderFiles() && generateHtmlDocumentation() &&
780 GenerateTestFiles(versionOfTestFiles);
781 if (success) {
782 cout << "Successfully processed " << mTypes.size() << " types, " << mConstants.size()
783 << " constants, and " << mFunctions.size() << " functions.\n";
784 }
785 return success;
786}
787
788string SystemSpecification::getHtmlAnchor(const string& name) const {
789 Definition* d = nullptr;
790 auto c = mConstants.find(name);
791 if (c != mConstants.end()) {
792 d = c->second;
793 } else {
794 auto t = mTypes.find(name);
795 if (t != mTypes.end()) {
796 d = t->second;
797 } else {
798 auto f = mFunctions.find(name);
799 if (f != mFunctions.end()) {
800 d = f->second;
801 } else {
802 return string();
803 }
804 }
805 }
806 ostringstream stream;
807 stream << "<a href='" << d->getUrl() << "'>" << name << "</a>";
808 return stream.str();
809}