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 INTERFACE_H_ 18 19 #define INTERFACE_H_ 20 21 #include <string> 22 #include <vector> 23 24 #include <hidl-hash/Hash.h> 25 26 #include "ConstantExpression.h" 27 #include "Reference.h" 28 #include "Scope.h" 29 30 namespace android { 31 32 struct Method; 33 struct InterfaceAndMethod; 34 35 extern const FQName gIBaseFqName; 36 extern const FQName gIManagerFqName; 37 38 struct Interface : public Scope { 39 const static std::unique_ptr<ConstantExpression> FLAG_ONE_WAY; 40 const static std::unique_ptr<ConstantExpression> FLAG_CLEAR_BUF; 41 42 Interface(const std::string& localName, const FQName& fullName, const Location& location, 43 Scope* parent, const Reference<Type>& superType, const Hash* fileHash); 44 45 const Hash* getFileHash() const; 46 47 void addUserDefinedMethod(Method* method); 48 bool addAllReservedMethods(const std::map<std::string, Method*>& allReservedMethods); 49 50 bool isElidableType() const override; 51 bool isInterface() const override; isIBaseInterface52 bool isIBase() const { return fqName() == gIBaseFqName; } 53 std::string typeName() const override; 54 55 bool hasSensitiveDataAnnotation() const; 56 57 const Interface* superType() const; 58 59 // Super type chain to root type. 60 // First element is superType(). 61 std::vector<const Interface *> superTypeChain() const; 62 // Super type chain to root type, including myself. 63 // First element is this. 64 std::vector<const Interface *> typeChain() const; 65 66 // user defined methods (explicit definition in HAL files) 67 const std::vector<Method *> &userDefinedMethods() const; 68 // HIDL reserved methods (every interface has these implicitly defined) 69 const std::vector<Method *> &hidlReservedMethods() const; 70 // the sum of userDefinedMethods() and hidlReservedMethods(). 71 std::vector<Method *> methods() const; 72 73 // userDefinedMethods() for all super type + methods() 74 // The order will be as follows (in the transaction code order): 75 // great-great-...-great-grand parent->userDefinedMethods() 76 // ... 77 // parent->userDefinedMethods() 78 // this->userDefinedMethods() 79 // this->hidlReservedMethods() 80 std::vector<InterfaceAndMethod> allMethodsFromRoot() const; 81 82 // allMethodsFromRoot for parent 83 std::vector<InterfaceAndMethod> allSuperMethodsFromRoot() const; 84 85 // aliases for corresponding methods in this->fqName() 86 std::string getBaseName() const; 87 std::string getProxyName() const; 88 std::string getStubName() const; 89 std::string getPassthroughName() const; 90 std::string getHwName() const; 91 FQName getProxyFqName() const; 92 FQName getStubFqName() const; 93 FQName getPassthroughFqName() const; 94 95 std::string getCppType( 96 StorageMode mode, 97 bool specifyNamespaces) const override; 98 99 std::string getJavaType(bool forInitializer) const override; 100 std::string getVtsType() const override; 101 102 std::vector<const Reference<Type>*> getReferences() const override; 103 std::vector<const Reference<Type>*> getStrongReferences() const override; 104 105 status_t resolveInheritance() override; 106 status_t validate() const override; 107 status_t validateUniqueNames() const; 108 status_t validateAnnotations() const override; 109 110 void emitReaderWriter( 111 Formatter &out, 112 const std::string &name, 113 const std::string &parcelObj, 114 bool parcelObjIsPointer, 115 bool isReader, 116 ErrorMode mode) const override; 117 118 void emitHidlDefinition(Formatter& out) const override; 119 120 void emitPackageTypeDeclarations(Formatter& out) const override; 121 void emitPackageTypeHeaderDefinitions(Formatter& out) const override; 122 void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override; 123 124 void getAlignmentAndSize(size_t* align, size_t* size) const override; 125 void emitJavaReaderWriter( 126 Formatter &out, 127 const std::string &parcelObj, 128 const std::string &argName, 129 bool isReader) const override; 130 131 void emitVtsAttributeType(Formatter& out) const override; 132 133 void emitVtsAttributeDeclaration(Formatter& out) const; 134 void emitVtsMethodDeclaration(Formatter& out, bool isInherited) const; 135 136 bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override; 137 138 bool isNeverStrongReference() const override; 139 140 private: 141 Reference<Type> mSuperType; 142 143 std::vector<Method*> mUserMethods; 144 std::vector<Method*> mReservedMethods; 145 146 const Hash* mFileHash; 147 148 bool fillPingMethod(Method* method) const; 149 bool fillDescriptorChainMethod(Method* method) const; 150 bool fillGetDescriptorMethod(Method* method) const; 151 bool fillHashChainMethod(Method* method) const; 152 bool fillSyspropsChangedMethod(Method* method) const; 153 bool fillLinkToDeathMethod(Method* method) const; 154 bool fillUnlinkToDeathMethod(Method* method) const; 155 bool fillSetHALInstrumentationMethod(Method* method) const; 156 bool fillGetDebugInfoMethod(Method* method) const; 157 bool fillDebugMethod(Method* method) const; 158 159 void emitDigestChain( 160 Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain, 161 std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const; 162 163 DISALLOW_COPY_AND_ASSIGN(Interface); 164 }; 165 166 // An interface / method tuple. 167 struct InterfaceAndMethod { InterfaceAndMethodInterfaceAndMethod168 InterfaceAndMethod(const Interface *iface, Method *method) 169 : mInterface(iface), 170 mMethod(method) {} methodInterfaceAndMethod171 Method *method() const { return mMethod; } interfaceInterfaceAndMethod172 const Interface *interface() const { return mInterface; } 173 174 private: 175 // do not own these objects. 176 const Interface *mInterface; 177 Method *mMethod; 178 }; 179 180 } // namespace android 181 182 #endif // INTERFACE_H_ 183 184