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