blob: d3fbad531f0cb89a1f26253402881294bd300bc3 [file] [log] [blame]
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07001/*
2 * Copyright (C) 2015 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#ifndef ANDROID_RS_API_GENERATOR_SPECIFICATION_H
18#define ANDROID_RS_API_GENERATOR_SPECIFICATION_H
19
20// See Generator.cpp for documentation of the .spec file format.
21
Yang Ni12398d82015-09-18 14:57:07 -070022#include <climits>
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070023#include <fstream>
24#include <list>
25#include <map>
26#include <string>
27#include <vector>
28
29class Constant;
30class ConstantSpecification;
31class Function;
32class FunctionPermutation;
33class FunctionSpecification;
34class SpecFile;
35class Specification;
36class Scanner;
37class SystemSpecification;
38class Type;
39class TypeSpecification;
40
41enum NumberKind { SIGNED_INTEGER, UNSIGNED_INTEGER, FLOATING_POINT };
42
43// Table of type equivalences.
44struct NumericalType {
45 const char* specType; // Name found in the .spec file
46 const char* rsDataType; // RS data type
47 const char* cType; // Type in a C file
48 const char* javaType; // Type in a Java file
49 NumberKind kind;
50 /* For integers, number of bits of the number, excluding the sign bit.
51 * For floats, number of implied bits of the mantissa.
52 */
53 int significantBits;
54 // For floats, number of bits of the exponent. 0 for integer types.
55 int exponentBits;
56};
57
58/* Corresponds to one parameter line in a .spec file. These will be parsed when
59 * we instantiate the FunctionPermutation(s) that correspond to one FunctionSpecification.
60 */
61struct ParameterEntry {
62 std::string type;
63 std::string name;
64 /* Optional information on how to generate test values for this parameter. Can be:
65 * - range(low, high): Generates values between these two limits only.
66 * - above(other_parameter): The values must be greater than those of the named parameter.
67 * Used for clamp.
68 * - compatible(type): The values must also be fully representable in the specified type.
69 * - conditional: Don't verify this value the function return NaN.
70 */
71 std::string testOption;
72 std::string documentation;
73 int lineNumber;
74};
75
76/* Information about a parameter to a function. The values of all the fields should only be set by
77 * parseParameterDefinition.
78 */
79struct ParameterDefinition {
80 std::string rsType; // The Renderscript type, e.g. "uint3"
81 std::string rsBaseType; // As above but without the number, e.g. "uint"
82 std::string javaBaseType; // The type we need to declare in Java, e.g. "unsigned int"
83 std::string specType; // The type found in the spec, e.g. "f16"
84 bool isFloatType; // True if it's a floating point value
85 /* The number of entries in the vector. It should be either "1", "2", "3", or "4". It's also
86 * "1" for scalars.
87 */
88 std::string mVectorSize;
89 /* The space the vector takes in an array. It's the same as the vector size, except for size
90 * "3", where the width is "4".
91 */
92 std::string vectorWidth;
93
94 std::string specName; // e.g. x, as found in the spec file
95 std::string variableName; // e.g. inX, used both in .rs and .java
96 std::string rsAllocName; // e.g. gAllocInX
97 std::string javaAllocName; // e.g. inX
98 std::string javaArrayName; // e.g. arrayInX
99
100 // If non empty, the mininum and maximum values to be used when generating the test data.
101 std::string minValue;
102 std::string maxValue;
103 /* If non empty, contains the name of another parameter that should be smaller or equal to this
104 * parameter, i.e. value(smallerParameter) <= value(this). This is used when testing clamp.
105 */
106 std::string smallerParameter;
107
108 bool isOutParameter; // True if this parameter returns data from the script.
109 bool undefinedIfOutIsNan; // If true, we don't validate if 'out' is NaN.
110
111 int typeIndex; // Index in the TYPES array. Negative if not found in the array.
112 int compatibleTypeIndex; // Index in TYPES for which the test data must also fit.
113
114 /* Fill this object from the type, name, and testOption.
115 * isReturn is true if we're processing the "return:"
116 */
117 void parseParameterDefinition(const std::string& type, const std::string& name,
118 const std::string& testOption, int lineNumber, bool isReturn,
119 Scanner* scanner);
120};
121
122struct VersionInfo {
123 /* The range of versions a specification applies to. Zero if there's no restriction,
124 * so an API that became available at 12 and is still valid would have min:12 max:0.
125 * If non zero, both versions should be at least 9, the API level that introduced
126 * RenderScript.
127 */
Yang Ni12398d82015-09-18 14:57:07 -0700128 unsigned int minVersion;
129 unsigned int maxVersion;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700130 // Either 0, 32 or 64. If 0, this definition is valid for both 32 and 64 bits.
131 int intSize;
132
133 VersionInfo() : minVersion(0), maxVersion(0), intSize(0) {}
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700134 /* Scan the version info from the spec file. maxApiLevel specifies the maximum level
135 * we are interested in. This may alter maxVersion. This method returns false if the
136 * minVersion is greater than the maxApiLevel.
137 */
Yang Ni12398d82015-09-18 14:57:07 -0700138 bool scan(Scanner* scanner, unsigned int maxApiLevel);
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700139 /* Return true if the target can be found whitin the range. */
140 bool includesVersion(int target) const {
141 return (minVersion == 0 || target >= minVersion) &&
142 (maxVersion == 0 || target <= maxVersion);
143 }
Yang Ni12398d82015-09-18 14:57:07 -0700144
145 static constexpr unsigned int kUnreleasedVersion = UINT_MAX;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700146};
147
148// We have three type of definitions
149class Definition {
150protected:
151 std::string mName;
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700152 /* If greater than 0, this definition is deprecated. It's the API level at which
153 * we added the deprecation warning.
154 */
155 int mDeprecatedApiLevel;
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700156 std::string mDeprecatedMessage; // Optional specific warning if the API is deprecated
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700157 bool mHidden; // True if it should not be documented
158 std::string mSummary; // A one-line description
159 std::vector<std::string> mDescription; // The comments to be included in the header
160 std::string mUrl; // The URL of the detailed documentation
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700161 int mFinalVersion; // API level at which this API was removed, 0 if API is still valid
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700162
163public:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700164 Definition(const std::string& name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700165
166 std::string getName() const { return mName; }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700167 bool deprecated() const { return mDeprecatedApiLevel > 0; }
168 int getDeprecatedApiLevel() const { return mDeprecatedApiLevel; }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700169 std::string getDeprecatedMessage() const { return mDeprecatedMessage; }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700170 bool hidden() const { return mHidden; }
171 std::string getSummary() const { return mSummary; }
172 const std::vector<std::string>& getDescription() const { return mDescription; }
173 std::string getUrl() const { return mUrl; }
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700174 int getFinalVersion() const { return mFinalVersion; }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700175
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700176 void scanDocumentationTags(Scanner* scanner, bool firstOccurence, const SpecFile* specFile);
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700177 // Keep track of the final version of this API, if any.
178 void updateFinalVersion(const VersionInfo& info);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700179};
180
181/* Represents a constant, like M_PI. This is a grouping of the version specific specifications.
182 * We'll only have one instance of Constant for each name.
183 */
184class Constant : public Definition {
185private:
186 std::vector<ConstantSpecification*> mSpecifications; // Owned
187
188public:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700189 Constant(const std::string& name) : Definition(name) {}
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700190 ~Constant();
191
192 const std::vector<ConstantSpecification*> getSpecifications() const { return mSpecifications; }
193 // This method should only be called by the scanning code.
194 void addSpecification(ConstantSpecification* spec) { mSpecifications.push_back(spec); }
195};
196
197/* Represents a type, like "float4". This is a grouping of the version specific specifications.
198 * We'll only have one instance of Type for each name.
199 */
200class Type : public Definition {
201private:
202 std::vector<TypeSpecification*> mSpecifications; // Owned
203
204public:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700205 Type(const std::string& name) : Definition(name) {}
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700206 ~Type();
207
208 const std::vector<TypeSpecification*> getSpecifications() const { return mSpecifications; }
209 // This method should only be called by the scanning code.
210 void addSpecification(TypeSpecification* spec) { mSpecifications.push_back(spec); }
211};
212
213/* Represents a function, like "clamp". Even though the spec file contains many entries for clamp,
214 * we'll only have one clamp instance.
215 */
216class Function : public Definition {
217private:
218 // mName in the base class contains the lower case name, e.g. native_log
219 std::string mCapitalizedName; // The capitalized name, e.g. NativeLog
220
221 // The unique parameters between all the specifications. NOT OWNED.
222 std::vector<ParameterEntry*> mParameters;
223 std::string mReturnDocumentation;
224
225 std::vector<FunctionSpecification*> mSpecifications; // Owned
226
227public:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700228 Function(const std::string& name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700229 ~Function();
230
231 std::string getCapitalizedName() const { return mCapitalizedName; }
232 const std::vector<ParameterEntry*>& getParameters() const { return mParameters; }
233 std::string getReturnDocumentation() const { return mReturnDocumentation; }
234 const std::vector<FunctionSpecification*> getSpecifications() const { return mSpecifications; }
235
236 bool someParametersAreDocumented() const;
237
238 // The following methods should only be called by the scanning code.
239 void addParameter(ParameterEntry* entry, Scanner* scanner);
240 void addReturn(ParameterEntry* entry, Scanner* scanner);
241 void addSpecification(FunctionSpecification* spec) { mSpecifications.push_back(spec); }
242};
243
244/* Base class for TypeSpecification, ConstantSpecification, and FunctionSpecification.
245 * A specification can be specific to a range of RenderScript version or 32bits vs 64 bits.
246 * This base class contains code to parse and store this version information.
247 */
248class Specification {
249protected:
250 VersionInfo mVersionInfo;
251 void scanVersionInfo(Scanner* scanner);
252
253public:
254 VersionInfo getVersionInfo() const { return mVersionInfo; }
255};
256
257/* Defines one of the many variations of a constant. There's a one to one correspondance between
258 * ConstantSpecification objects and entries in the spec file.
259 */
260class ConstantSpecification : public Specification {
261private:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700262 Constant* mConstant; // Not owned
263
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700264 std::string mValue; // E.g. "3.1415"
265public:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700266 ConstantSpecification(Constant* constant) : mConstant(constant) {}
267
268 Constant* getConstant() const { return mConstant; }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700269 std::string getValue() const { return mValue; }
270
271 // Parse a constant specification and add it to specFile.
Yang Ni12398d82015-09-18 14:57:07 -0700272 static void scanConstantSpecification(Scanner* scanner, SpecFile* specFile, unsigned int maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700273};
274
275enum TypeKind {
276 SIMPLE,
Stephen Hinesca51c782015-08-25 23:43:34 -0700277 RS_OBJECT,
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700278 STRUCT,
279 ENUM,
280};
281
282/* Defines one of the many variations of a type. There's a one to one correspondance between
283 * TypeSpecification objects and entries in the spec file.
284 */
285class TypeSpecification : public Specification {
286private:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700287 Type* mType; // Not owned
288
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700289 TypeKind mKind; // The kind of type specification
290
291 // If mKind is SIMPLE:
292 std::string mSimpleType; // The definition of the type
293
294 // If mKind is STRUCT:
295 std::string mStructName; // The name found after the struct keyword
296 std::vector<std::string> mFields; // One entry per struct field
297 std::vector<std::string> mFieldComments; // One entry per struct field
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700298 std::string mAttribute; // Some structures may have attributes
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700299
300 // If mKind is ENUM:
301 std::string mEnumName; // The name found after the enum keyword
302 std::vector<std::string> mValues; // One entry per enum value
303 std::vector<std::string> mValueComments; // One entry per enum value
304public:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700305 TypeSpecification(Type* type) : mType(type) {}
306
307 Type* getType() const { return mType; }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700308 TypeKind getKind() const { return mKind; }
309 std::string getSimpleType() const { return mSimpleType; }
310 std::string getStructName() const { return mStructName; }
311 const std::vector<std::string>& getFields() const { return mFields; }
312 const std::vector<std::string>& getFieldComments() const { return mFieldComments; }
Jean-Luc Brouillet36e2be52015-04-30 14:41:24 -0700313 std::string getAttribute() const { return mAttribute; }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700314 std::string getEnumName() const { return mEnumName; }
315 const std::vector<std::string>& getValues() const { return mValues; }
316 const std::vector<std::string>& getValueComments() const { return mValueComments; }
317
318 // Parse a type specification and add it to specFile.
Yang Ni12398d82015-09-18 14:57:07 -0700319 static void scanTypeSpecification(Scanner* scanner, SpecFile* specFile, unsigned int maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700320};
321
322// Maximum number of placeholders (like #1, #2) in function specifications.
323const int MAX_REPLACEABLES = 4;
324
325/* Defines one of the many variations of the function. There's a one to one correspondance between
326 * FunctionSpecification objects and entries in the spec file. Some of the strings that are parts
327 * of a FunctionSpecification can include placeholders, which are "#1", "#2", "#3", and "#4". We'll
328 * replace these by values before generating the files.
329 */
330class FunctionSpecification : public Specification {
331private:
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700332 Function* mFunction; // Not owned
333
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700334 /* How to test. One of:
335 * "scalar": Generate test code that checks entries of each vector indepently. E.g. for
336 * sin(float3), the test code will call the CoreMathVerfier.computeSin 3 times.
337 * "limited": Like "scalar" but we don't generate extreme values. This is not currently
338 * enabled as we were generating to many errors.
339 * "custom": Like "scalar" but instead of calling CoreMathVerifier.computeXXX() to compute
340 * the expected value, we call instead CoreMathVerifier.verifyXXX(). This method
341 * returns a string that contains the error message, null if there's no error.
342 * "vector": Generate test code that calls the CoreMathVerifier only once for each vector.
343 * This is useful for APIs like dot() or length().
344 * "noverify": Generate test code that calls the API but don't verify the returned value.
345 * This can discover unresolved references.
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700346 * "": Don't test. This is the default.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700347 */
348 std::string mTest;
Yang Ni12398d82015-09-18 14:57:07 -0700349 bool mInternal; // Internal. Not visible to users. (Default: false)
350 bool mIntrinsic; // Compiler intrinsic that is lowered to an internal API.
351 // (Default: false)
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700352 std::string mAttribute; // Function attributes.
353 std::string mPrecisionLimit; // Maximum precision required when checking output of this
354 // function.
355
356 // The vectors of values with which we'll replace #1, #2, ...
357 std::vector<std::vector<std::string> > mReplaceables;
358
359 /* The collection of permutations for this specification, i.e. this class instantianted
360 * for specific values of #1, #2, etc. Owned.
361 */
362 std::vector<FunctionPermutation*> mPermutations;
363
364 // The following fields may contain placeholders that will be replaced using the mReplaceables.
365
366 /* As of this writing, convert_... is the only function with #1 in its name.
367 * The related Function object contains the name of the function without #n, e.g. convert.
368 * This is the name with the #, e.g. convert_#1_#2
369 */
370 std::string mUnexpandedName;
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700371 ParameterEntry* mReturn; // The return type. The name should be empty. Owned.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700372 std::vector<ParameterEntry*> mParameters; // The parameters. Owned.
373 std::vector<std::string> mInline; // The inline code to be included in the header
374
375 /* Substitute the placeholders in the strings (e.g. #1, #2, ...) by the corresponding
376 * entries in mReplaceables. indexOfReplaceable1 selects with value to use for #1,
377 * same for 2, 3, and 4.
378 */
379 std::string expandString(std::string s, int indexOfReplaceable[MAX_REPLACEABLES]) const;
380 void expandStringVector(const std::vector<std::string>& in,
381 int replacementIndexes[MAX_REPLACEABLES],
382 std::vector<std::string>* out) const;
383
384 // Fill the mPermutations field.
385 void createPermutations(Function* function, Scanner* scanner);
386
387public:
Yang Ni12398d82015-09-18 14:57:07 -0700388 FunctionSpecification(Function* function) : mFunction(function), mInternal(false),
389 mIntrinsic(false), mReturn(nullptr) {}
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700390 ~FunctionSpecification();
391
Jean-Luc Brouillet67923a92015-05-12 15:38:27 -0700392 Function* getFunction() const { return mFunction; }
Yang Ni12398d82015-09-18 14:57:07 -0700393 bool isInternal() const { return mInternal; }
394 bool isIntrinsic() const { return mIntrinsic; }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700395 std::string getAttribute() const { return mAttribute; }
396 std::string getTest() const { return mTest; }
397 std::string getPrecisionLimit() const { return mPrecisionLimit; }
398
399 const std::vector<FunctionPermutation*>& getPermutations() const { return mPermutations; }
400
401 std::string getName(int replacementIndexes[MAX_REPLACEABLES]) const;
402 void getReturn(int replacementIndexes[MAX_REPLACEABLES], std::string* retType,
403 int* lineNumber) const;
404 size_t getNumberOfParams() const { return mParameters.size(); }
405 void getParam(size_t index, int replacementIndexes[MAX_REPLACEABLES], std::string* type,
406 std::string* name, std::string* testOption, int* lineNumber) const;
407 void getInlines(int replacementIndexes[MAX_REPLACEABLES],
408 std::vector<std::string>* inlines) const;
409
410 // Parse the "test:" line.
411 void parseTest(Scanner* scanner);
412
413 // Return true if we need to generate tests for this function.
Yang Ni12398d82015-09-18 14:57:07 -0700414 bool hasTests(unsigned int versionOfTestFiles) const;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700415
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700416 bool hasInline() const { return mInline.size() > 0; }
417
418 /* Return true if this function can be overloaded. This is added by default to all
419 * specifications, so except for the very few exceptions that start the attributes
420 * with an '=' to avoid this, we'll return true.
421 */
422 bool isOverloadable() const {
423 return mAttribute.empty() || mAttribute[0] != '=';
424 }
425
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700426 // Parse a function specification and add it to specFile.
Yang Ni12398d82015-09-18 14:57:07 -0700427 static void scanFunctionSpecification(Scanner* scanner, SpecFile* specFile, unsigned int maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700428};
429
430/* A concrete version of a function specification, where all placeholders have been replaced by
431 * actual values.
432 */
433class FunctionPermutation {
434private:
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700435 // These are the expanded version of those found on FunctionSpecification
436 std::string mName;
437 std::string mNameTrunk; // The name without any expansion, e.g. convert
438 std::string mTest; // How to test. One of "scalar", "vector", "noverify", "limited", and
439 // "none".
440 std::string mPrecisionLimit; // Maximum precision required when checking output of this
441 // function.
442
443 // The parameters of the function. This does not include the return type. Owned.
444 std::vector<ParameterDefinition*> mParams;
445 // The return type. nullptr if a void function. Owned.
446 ParameterDefinition* mReturn;
447
448 // The number of input and output parameters. mOutputCount counts the return type.
449 int mInputCount;
450 int mOutputCount;
451
452 // Whether one of the output parameters is a float.
453 bool mHasFloatAnswers;
454
455 // The inline code that implements this function. Will be empty if not an inline.
456 std::vector<std::string> mInline;
457
458public:
459 FunctionPermutation(Function* function, FunctionSpecification* specification,
460 int replacementIndexes[MAX_REPLACEABLES], Scanner* scanner);
461 ~FunctionPermutation();
462
463 std::string getName() const { return mName; }
464 std::string getNameTrunk() const { return mNameTrunk; }
465 std::string getTest() const { return mTest; }
466 std::string getPrecisionLimit() const { return mPrecisionLimit; }
467
Jean-Luc Brouillet66fea242015-04-09 16:47:59 -0700468 const std::vector<std::string>& getInline() const { return mInline; }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700469 const ParameterDefinition* getReturn() const { return mReturn; }
470 int getInputCount() const { return mInputCount; }
471 int getOutputCount() const { return mOutputCount; }
472 bool hasFloatAnswers() const { return mHasFloatAnswers; }
473
474 const std::vector<ParameterDefinition*> getParams() const { return mParams; }
475};
476
477// An entire spec file and the methods to process it.
478class SpecFile {
479private:
480 std::string mSpecFileName;
481 std::string mHeaderFileName;
482 std::string mDetailedDocumentationUrl;
483 std::string mBriefDescription;
484 std::vector<std::string> mFullDescription;
485 // Text to insert as-is in the generated header.
486 std::vector<std::string> mVerbatimInclude;
487
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700488 /* The constants, types, and functions specifications declared in this
489 * file, in the order they are found in the file. This matters for
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700490 * header generation, as some types and inline functions depend
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700491 * on each other. Pointers not owned.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700492 */
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700493 std::list<ConstantSpecification*> mConstantSpecificationsList;
494 std::list<TypeSpecification*> mTypeSpecificationsList;
495 std::list<FunctionSpecification*> mFunctionSpecificationsList;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700496
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700497 /* The constants, types, and functions that are documented in this file.
498 * In very rare cases, specifications for an API are split across multiple
499 * files, e.g. currently for ClearObject(). The documentation for
500 * that function must be found in the first spec file encountered, so the
501 * order of the files on the command line matters.
502 */
503 std::map<std::string, Constant*> mDocumentedConstants;
504 std::map<std::string, Type*> mDocumentedTypes;
505 std::map<std::string, Function*> mDocumentedFunctions;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700506
507public:
508 explicit SpecFile(const std::string& specFileName);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700509
510 std::string getSpecFileName() const { return mSpecFileName; }
511 std::string getHeaderFileName() const { return mHeaderFileName; }
512 std::string getDetailedDocumentationUrl() const { return mDetailedDocumentationUrl; }
513 const std::string getBriefDescription() const { return mBriefDescription; }
514 const std::vector<std::string>& getFullDescription() const { return mFullDescription; }
515 const std::vector<std::string>& getVerbatimInclude() const { return mVerbatimInclude; }
516
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700517 const std::list<ConstantSpecification*>& getConstantSpecifications() const {
518 return mConstantSpecificationsList;
519 }
520 const std::list<TypeSpecification*>& getTypeSpecifications() const {
521 return mTypeSpecificationsList;
522 }
523 const std::list<FunctionSpecification*>& getFunctionSpecifications() const {
524 return mFunctionSpecificationsList;
525 }
526 const std::map<std::string, Constant*>& getDocumentedConstants() const {
527 return mDocumentedConstants;
528 }
529 const std::map<std::string, Type*>& getDocumentedTypes() const { return mDocumentedTypes; }
530 const std::map<std::string, Function*>& getDocumentedFunctions() const {
531 return mDocumentedFunctions;
532 }
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700533
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700534 bool hasSpecifications() const {
535 return !mDocumentedConstants.empty() || !mDocumentedTypes.empty() ||
536 !mDocumentedFunctions.empty();
537 }
538
Yang Ni12398d82015-09-18 14:57:07 -0700539 bool readSpecFile(unsigned int maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700540
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700541 /* These are called by the parser to keep track of the specifications defined in this file.
542 * hasDocumentation is true if this specification containes the documentation.
543 */
544 void addConstantSpecification(ConstantSpecification* spec, bool hasDocumentation);
545 void addTypeSpecification(TypeSpecification* spec, bool hasDocumentation);
546 void addFunctionSpecification(FunctionSpecification* spec, bool hasDocumentation);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700547};
548
549// The collection of all the spec files.
550class SystemSpecification {
551private:
552 std::vector<SpecFile*> mSpecFiles;
553
554 /* Entries in the table of contents. We accumulate them in a map to sort them.
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700555 * Pointers are owned.
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700556 */
557 std::map<std::string, Constant*> mConstants;
558 std::map<std::string, Type*> mTypes;
559 std::map<std::string, Function*> mFunctions;
560
561public:
562 ~SystemSpecification();
Jean-Luc Brouillet7c078542015-03-23 16:16:08 -0700563
564 /* These are called the parser to create unique instances per name. Set *created to true
565 * if the named specification did not already exist.
566 */
567 Constant* findOrCreateConstant(const std::string& name, bool* created);
568 Type* findOrCreateType(const std::string& name, bool* created);
569 Function* findOrCreateFunction(const std::string& name, bool* created);
570
Jean-Luc Brouillet2217eb72015-04-24 14:41:48 -0700571 /* Parse the spec file and create the object hierarchy, adding a pointer to mSpecFiles.
572 * We won't include information passed the specified level.
573 */
Yang Ni12398d82015-09-18 14:57:07 -0700574 bool readSpecFile(const std::string& fileName, unsigned int maxApiLevel);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700575 // Generate all the files.
Yang Ni12398d82015-09-18 14:57:07 -0700576 bool generateFiles(bool forVerification, unsigned int maxApiLevel) const;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700577
578 const std::vector<SpecFile*>& getSpecFiles() const { return mSpecFiles; }
579 const std::map<std::string, Constant*>& getConstants() const { return mConstants; }
580 const std::map<std::string, Type*>& getTypes() const { return mTypes; }
581 const std::map<std::string, Function*>& getFunctions() const { return mFunctions; }
582
583 // Returns "<a href='...'> for the named specification, or empty if not found.
584 std::string getHtmlAnchor(const std::string& name) const;
Jean-Luc Brouillet36090672015-04-07 15:15:53 -0700585
586 // Returns the maximum API level specified in any spec file.
Yang Ni12398d82015-09-18 14:57:07 -0700587 unsigned int getMaximumApiLevel();
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700588};
589
590// Singleton that represents the collection of all the specs we're processing.
591extern SystemSpecification systemSpecification;
592
593// Table of equivalences of numerical types.
594extern const NumericalType TYPES[];
595extern const int NUM_TYPES;
596
597#endif // ANDROID_RS_API_GENERATOR_SPECIFICATION_H