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 COMPOUND_TYPE_H_ 18 19 #define COMPOUND_TYPE_H_ 20 21 #include "Reference.h" 22 #include "Scope.h" 23 24 #include <vector> 25 26 namespace android { 27 28 struct CompoundType : public Scope { 29 enum Style { 30 STYLE_STRUCT, 31 STYLE_UNION, 32 STYLE_SAFE_UNION, 33 }; 34 35 CompoundType(Style style, const char* localName, const FQName& fullName, 36 const Location& location, Scope* parent); 37 38 Style style() const; 39 40 void setFields(std::vector<NamedReference<Type>*>* fields); 41 42 bool isCompoundType() const override; 43 44 bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override; 45 46 std::string typeName() const override; 47 48 std::vector<const Reference<Type>*> getReferences() const override; 49 50 status_t validate() const override; 51 status_t validateUniqueNames() const; 52 status_t validateSubTypeNames() const; 53 54 std::string getCppType(StorageMode mode, 55 bool specifyNamespaces) const override; 56 57 std::string getJavaType(bool forInitializer) const override; 58 59 std::string getVtsType() const override; 60 61 void emitReaderWriter( 62 Formatter &out, 63 const std::string &name, 64 const std::string &parcelObj, 65 bool parcelObjIsPointer, 66 bool isReader, 67 ErrorMode mode) const override; 68 69 void emitReaderWriterEmbedded( 70 Formatter &out, 71 size_t depth, 72 const std::string &name, 73 const std::string &sanitizedName, 74 bool nameIsPointer, 75 const std::string &parcelObj, 76 bool parcelObjIsPointer, 77 bool isReader, 78 ErrorMode mode, 79 const std::string &parentName, 80 const std::string &offsetText) const override; 81 82 void emitResolveReferences( 83 Formatter &out, 84 const std::string &name, 85 bool nameIsPointer, 86 const std::string &parcelObj, 87 bool parcelObjIsPointer, 88 bool isReader, 89 ErrorMode mode) const override; 90 91 void emitResolveReferencesEmbedded( 92 Formatter &out, 93 size_t depth, 94 const std::string &name, 95 const std::string &sanitizedName, 96 bool nameIsPointer, 97 const std::string &parcelObj, 98 bool parcelObjIsPointer, 99 bool isReader, 100 ErrorMode mode, 101 const std::string &parentName, 102 const std::string &offsetText) const override; 103 104 void emitJavaReaderWriter( 105 Formatter &out, 106 const std::string &parcelObj, 107 const std::string &argName, 108 bool isReader) const override; 109 110 void emitJavaFieldInitializer( 111 Formatter &out, const std::string &fieldName) const override; 112 113 void emitJavaFieldDefaultInitialValue( 114 Formatter &out, const std::string &declaredFieldName) const override; 115 116 void emitJavaFieldReaderWriter( 117 Formatter &out, 118 size_t depth, 119 const std::string &parcelName, 120 const std::string &blobName, 121 const std::string &fieldName, 122 const std::string &offset, 123 bool isReader) const override; 124 125 void emitTypeDeclarations(Formatter& out) const override; 126 void emitTypeForwardDeclaration(Formatter& out) const override; 127 void emitPackageTypeDeclarations(Formatter& out) const override; 128 void emitPackageTypeHeaderDefinitions(Formatter& out) const override; 129 void emitPackageHwDeclarations(Formatter& out) const override; 130 131 void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override; 132 133 void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override; 134 135 bool needsEmbeddedReadWrite() const override; 136 bool deepNeedsResolveReferences(std::unordered_set<const Type*>* visited) const override; 137 bool resultNeedsDeref() const override; 138 139 void emitVtsTypeDeclarations(Formatter& out) const override; 140 void emitVtsAttributeType(Formatter& out) const override; 141 142 bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override; 143 bool deepContainsPointer(std::unordered_set<const Type*>* visited) const override; 144 145 void getAlignmentAndSize(size_t *align, size_t *size) const override; 146 147 bool containsInterface() const; 148 private: 149 150 struct Layout { 151 size_t offset; 152 size_t align; 153 size_t size; 154 LayoutCompoundType::Layout155 Layout() : offset(0), align(1), size(0) {} 156 static size_t getPad(size_t offset, size_t align); 157 }; 158 159 struct CompoundLayout { 160 // Layout of this entire object including metadata. 161 // For struct/union, this is the same as innerStruct. 162 Layout overall; 163 // Layout of user-specified data 164 Layout innerStruct; 165 // Layout of discriminator for safe union (otherwise zero) 166 Layout discriminator; 167 }; 168 169 Style mStyle; 170 std::vector<NamedReference<Type>*>* mFields; 171 172 void emitLayoutAsserts(Formatter& out, const Layout& localLayout, 173 const std::string& localLayoutName) const; 174 175 void emitInvalidSubTypeNamesError(const std::string& subTypeName, 176 const Location& location) const; 177 178 void emitSafeUnionTypeDefinitions(Formatter& out) const; 179 void emitSafeUnionTypeConstructors(Formatter& out) const; 180 void emitSafeUnionTypeDeclarations(Formatter& out) const; 181 std::unique_ptr<ScalarType> getUnionDiscriminatorType() const; 182 183 void emitSafeUnionUnknownDiscriminatorError(Formatter& out, const std::string& value, 184 bool fatal) const; 185 186 void emitSafeUnionCopyAndAssignDefinition(Formatter& out, 187 const std::string& parameterName, 188 bool isCopyConstructor, 189 bool usesMoveSemantics) const; 190 191 struct SafeUnionEnumElement { 192 std::string fieldName; 193 std::string fieldTypeName; 194 }; 195 196 std::vector<SafeUnionEnumElement> getSafeUnionEnumElements(bool useCppTypeName) const; 197 198 CompoundLayout getCompoundAlignmentAndSize() const; 199 void emitPaddingZero(Formatter& out, size_t offset, size_t size) const; 200 201 void emitSafeUnionReaderWriterForInterfaces( 202 Formatter &out, 203 const std::string &name, 204 const std::string &parcelObj, 205 bool parcelObjIsPointer, 206 bool isReader, 207 ErrorMode mode) const; 208 209 void emitStructReaderWriter( 210 Formatter &out, const std::string &prefix, bool isReader) const; 211 void emitResolveReferenceDef(Formatter& out, const std::string& prefix, bool isReader) const; 212 213 DISALLOW_COPY_AND_ASSIGN(CompoundType); 214 }; 215 216 } // namespace android 217 218 #endif // COMPOUND_TYPE_H_ 219 220