• 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 <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 getAdapterName() const;
88     std::string getProxyName() const;
89     std::string getStubName() const;
90     std::string getPassthroughName() const;
91     std::string getHwName() const;
92     FQName getProxyFqName() const;
93     FQName getStubFqName() const;
94     FQName getPassthroughFqName() const;
95 
96     std::string getCppType(
97             StorageMode mode,
98             bool specifyNamespaces) const override;
99 
100     std::string getJavaType(bool forInitializer) const override;
101     std::string getVtsType() const override;
102 
103     std::vector<const Reference<Type>*> getReferences() const override;
104     std::vector<const Reference<Type>*> getStrongReferences() const override;
105 
106     status_t resolveInheritance() override;
107     status_t validate() const override;
108     status_t validateUniqueNames() const;
109     status_t validateAnnotations() const override;
110 
111     void emitReaderWriter(
112             Formatter &out,
113             const std::string &name,
114             const std::string &parcelObj,
115             bool parcelObjIsPointer,
116             bool isReader,
117             ErrorMode mode) const override;
118 
119     void emitHidlDefinition(Formatter& out) const override;
120 
121     void emitPackageTypeDeclarations(Formatter& out) const override;
122     void emitPackageTypeHeaderDefinitions(Formatter& out) const override;
123     void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
124 
125     void getAlignmentAndSize(size_t* align, size_t* size) const override;
126     void emitJavaReaderWriter(
127             Formatter &out,
128             const std::string &parcelObj,
129             const std::string &argName,
130             bool isReader) const override;
131 
132     void emitVtsAttributeType(Formatter& out) const override;
133 
134     void emitVtsAttributeDeclaration(Formatter& out) const;
135     void emitVtsMethodDeclaration(Formatter& out, bool isInherited) const;
136 
137     bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
138 
139     bool isNeverStrongReference() const override;
140 
141    private:
142     Reference<Type> mSuperType;
143 
144     std::vector<Method*> mUserMethods;
145     std::vector<Method*> mReservedMethods;
146 
147     const Hash* mFileHash;
148 
149     bool fillPingMethod(Method* method) const;
150     bool fillDescriptorChainMethod(Method* method) const;
151     bool fillGetDescriptorMethod(Method* method) const;
152     bool fillHashChainMethod(Method* method) const;
153     bool fillSyspropsChangedMethod(Method* method) const;
154     bool fillLinkToDeathMethod(Method* method) const;
155     bool fillUnlinkToDeathMethod(Method* method) const;
156     bool fillSetHALInstrumentationMethod(Method* method) const;
157     bool fillGetDebugInfoMethod(Method* method) const;
158     bool fillDebugMethod(Method* method) const;
159 
160     void emitDigestChain(
161         Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
162         std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const;
163 
164     DISALLOW_COPY_AND_ASSIGN(Interface);
165 };
166 
167 // An interface / method tuple.
168 struct InterfaceAndMethod {
InterfaceAndMethodInterfaceAndMethod169     InterfaceAndMethod(const Interface *iface, Method *method)
170         : mInterface(iface),
171           mMethod(method) {}
methodInterfaceAndMethod172     Method *method() const { return mMethod; }
interfaceInterfaceAndMethod173     const Interface *interface() const { return mInterface; }
174 
175    private:
176     // do not own these objects.
177     const Interface *mInterface;
178     Method *mMethod;
179 };
180 
181 }  // namespace android
182 
183 #endif  // INTERFACE_H_
184 
185