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