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