1 /* 2 * Copyright (C) 2016 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 ENUM_TYPE_H_ 18 19 #define ENUM_TYPE_H_ 20 21 #include "ConstantExpression.h" 22 #include "Reference.h" 23 #include "Scope.h" 24 25 #include <vector> 26 27 namespace android { 28 29 struct EnumValue; 30 struct BitFieldType; 31 32 struct EnumType : public Scope { 33 EnumType(const char* localName, const FQName& fullName, const Location& location, 34 const Reference<Type>& storageType, Scope* parent); 35 36 const Type *storageType() const; 37 const std::vector<EnumValue *> &values() const; 38 void addValue(EnumValue *value); 39 40 void forEachValueFromRoot(const std::function<void(EnumValue*)> f) const; 41 42 LocalIdentifier *lookupIdentifier(const std::string &name) const override; 43 44 bool isElidableType() const override; 45 const ScalarType *resolveToScalarType() const override; 46 47 std::string typeName() const override; 48 bool isEnum() const override; 49 bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override; 50 51 std::string getCppType(StorageMode mode, 52 bool specifyNamespaces) const override; 53 54 std::string getJavaType(bool forInitializer) const override; 55 56 std::string getJavaSuffix() const override; 57 58 std::string getJavaWrapperType() const override; 59 60 std::string getVtsType() const override; 61 62 std::string getBitfieldCppType(StorageMode mode, bool specifyNamespaces = true) const; 63 std::string getBitfieldJavaType(bool forInitializer = false) const; 64 std::string getBitfieldJavaWrapperType() const; 65 66 // Return the type that corresponds to bitfield<T>. 67 const BitFieldType* getBitfieldType() const; 68 69 std::vector<const Reference<Type>*> getReferences() const override; 70 std::vector<const ConstantExpression*> getConstantExpressions() const override; 71 72 status_t resolveInheritance() override; 73 status_t validate() const override; 74 status_t validateUniqueNames() const; 75 76 void emitReaderWriter( 77 Formatter &out, 78 const std::string &name, 79 const std::string &parcelObj, 80 bool parcelObjIsPointer, 81 bool isReader, 82 ErrorMode mode) const override; 83 84 void emitJavaFieldReaderWriter( 85 Formatter &out, 86 size_t depth, 87 const std::string &parcelName, 88 const std::string &blobName, 89 const std::string &fieldName, 90 const std::string &offset, 91 bool isReader) const override; 92 93 void emitTypeDeclarations(Formatter& out) const override; 94 void emitTypeForwardDeclaration(Formatter& out) const override; 95 void emitGlobalTypeDeclarations(Formatter& out) const override; 96 void emitPackageTypeDeclarations(Formatter& out) const override; 97 98 void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override; 99 100 void emitVtsTypeDeclarations(Formatter& out) const override; 101 void emitVtsAttributeType(Formatter& out) const override; 102 103 void emitJavaDump( 104 Formatter &out, 105 const std::string &streamName, 106 const std::string &name) const override; 107 108 void getAlignmentAndSize(size_t *align, size_t *size) const override; 109 110 void appendToExportedTypesVector( 111 std::vector<const Type *> *exportedTypes) const override; 112 113 void emitExportedHeader(Formatter& out, bool forJava) const override; 114 115 private: 116 std::vector<const EnumType*> typeChain() const; 117 std::vector<const EnumType*> superTypeChain() const; 118 119 const Annotation *findExportAnnotation() const; 120 121 void emitIteratorDeclaration(Formatter& out) const; 122 void emitIteratorDefinitions(Formatter& out) const; 123 124 void emitEnumBitwiseOperator( 125 Formatter &out, 126 bool lhsIsEnum, 127 bool rhsIsEnum, 128 const std::string &op) const; 129 130 void emitBitFieldBitwiseAssignmentOperator( 131 Formatter &out, 132 const std::string &op) const; 133 134 std::vector<EnumValue *> mValues; 135 Reference<Type> mStorageType; 136 137 DISALLOW_COPY_AND_ASSIGN(EnumType); 138 }; 139 140 struct EnumValue : public LocalIdentifier, DocCommentable { 141 EnumValue(const char* name, ConstantExpression* value, const Location& location); 142 143 std::string name() const; 144 std::string value(ScalarType::Kind castKind) const; 145 std::string cppValue(ScalarType::Kind castKind) const; 146 std::string javaValue(ScalarType::Kind castKind) const; 147 std::string comment() const; 148 void autofill(const EnumType* prevType, EnumValue* prevValue, const ScalarType* type); 149 ConstantExpression* constExpr() const override; 150 151 bool isAutoFill() const; 152 bool isEnumValue() const override; 153 154 const Location& location() const; 155 156 private: 157 std::string mName; 158 ConstantExpression* mValue; 159 const Location mLocation; 160 bool mIsAutoFill; 161 162 DISALLOW_COPY_AND_ASSIGN(EnumValue); 163 }; 164 165 struct BitFieldType : public TemplatedType { 166 BitFieldType(Scope* parent); 167 168 std::string templatedTypeName() const override; 169 170 const EnumType* getElementEnumType() const; 171 172 bool isBitField() const override; 173 174 bool isCompatibleElementType(const Type* elementType) const override; 175 176 bool isElidableType() const override; 177 178 bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override; 179 180 const ScalarType *resolveToScalarType() const override; 181 182 std::string getCppType(StorageMode mode, 183 bool specifyNamespaces) const override; 184 185 std::string getJavaType(bool forInitializer) const override; 186 187 std::string getJavaSuffix() const override; 188 189 std::string getJavaWrapperType() const override; 190 191 std::string getVtsType() const override; 192 193 const EnumType* getEnumType() const; 194 195 void emitVtsAttributeType(Formatter& out) const override; 196 197 void getAlignmentAndSize(size_t *align, size_t *size) const override; 198 199 void emitReaderWriter( 200 Formatter &out, 201 const std::string &name, 202 const std::string &parcelObj, 203 bool parcelObjIsPointer, 204 bool isReader, 205 ErrorMode mode) const override; 206 207 void emitDump( 208 Formatter &out, 209 const std::string &streamName, 210 const std::string &name) const override; 211 212 void emitJavaDump( 213 Formatter &out, 214 const std::string &streamName, 215 const std::string &name) const override; 216 217 void emitJavaFieldReaderWriter( 218 Formatter &out, 219 size_t depth, 220 const std::string &parcelName, 221 const std::string &blobName, 222 const std::string &fieldName, 223 const std::string &offset, 224 bool isReader) const override; 225 }; 226 227 } // namespace android 228 229 #endif // ENUM_TYPE_H_ 230 231