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