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 FQNAME_H_
18 
19 #define FQNAME_H_
20 
21 #include <android-base/macros.h>
22 #include <string>
23 #include <vector>
24 
25 namespace android {
26 
27 struct FQName {
28     explicit FQName();
29     explicit FQName(const std::string &s);
30 
31     FQName(const std::string &package,
32            const std::string &version,
33            const std::string &name,
34            const std::string &valueName = "");
35 
36     // a synonym to FQName(names.join("."))
37     FQName(const std::vector<std::string> &names);
38 
39     FQName(const FQName& other);
40 
41     bool isValid() const;
42     bool isIdentifier() const;
43     bool setTo(const std::string &s);
44 
45     void applyDefaults(
46             const std::string &defaultPackage,
47             const std::string &defaultVersion);
48 
49     std::string package() const;
50     // Return version in the form "@1.0" if it is present, otherwise empty string.
51     std::string atVersion() const;
52     // Return version in the form "1.0" if it is present, otherwise empty string.
53     std::string version() const;
54     // Return version in the form "V1_0" if it is present, otherwise empty string.
55     std::string sanitizedVersion() const;
56     // Return true only if version is present.
57     bool hasVersion() const;
58 
59     // The next two methods return the name part of the FQName, that is, the
60     // part after the version field.  For example:
61     //
62     // package android.hardware.tests.foo@1.0;
63     // interface IFoo {
64     //    struct bar {
65     //        struct baz {
66     //            ...
67     //        };
68     //    };
69     // };
70     //
71     // package android.hardware.tests.bar@1.0;
72     // import android.hardware.tests.foo@1.0;
73     // interface {
74     //    struct boo {
75     //        IFoo.bar.baz base;
76     //    };
77     // }
78     //
79     // The FQName for base is android.hardware.tests.foo@1.0::IFoo.bar.baz; so
80     // FQName::name() will return "IFoo.bar.baz". FQName::names() will return
81     // std::vector<std::string>{"IFoo","bar","baz"}
82 
83     std::string name() const;
84     std::vector<std::string> names() const;
85 
86     // The next two methods returns two parts of the FQName, that is,
87     // the first part package + version + name, the second part valueName.
88     FQName typeName() const;
89     std::string valueName() const;
90 
91     bool isFullyQualified() const;
92 
93     // true if:
94     // 1. (package)?(version)?(name):(valueName)
95     // 2. (valueName), aka a single identifier
96     bool isValidValueName() const;
97 
98     void print() const;
99     std::string string() const;
100 
101     bool operator<(const FQName &other) const;
102     bool operator==(const FQName &other) const;
103     bool operator!=(const FQName &other) const;
104 
105     // Must be called on an interface
106     // android.hardware.foo@1.0::IBar
107     // -> Bar
108     std::string getInterfaceBaseName() const;
109 
110     // Must be called on an interface
111     // android.hardware.foo@1.0::IBar
112     // -> IBar
113     std::string getInterfaceName() const;
114 
115     // Must be called on an interface
116     // android.hardware.foo@1.0::IBar
117     // -> IHwBar
118     std::string getInterfaceHwName() const;
119 
120     // Must be called on an interface
121     // android.hardware.foo@1.0::IBar
122     // -> BpBar
123     std::string getInterfaceProxyName() const;
124 
125     // Must be called on an interface
126     // android.hardware.foo@1.0::IBar
127     // -> BnBar
128     std::string getInterfaceStubName() const;
129 
130     // Must be called on an interface
131     // android.hardware.foo@1.0::IBar
132     // -> BsBar
133     std::string getInterfacePassthroughName() const;
134 
135     // Must be called on an interface
136     // android.hardware.foo@1.0::IBar
137     // -> android.hardware.foo@1.0::BpBar
138     FQName getInterfaceProxyFqName() const;
139 
140     // Must be called on an interface
141     // android.hardware.foo@1.0::IBar
142     // -> android.hardware.foo@1.0::BnBar
143     FQName getInterfaceStubFqName() const;
144 
145     // Must be called on an interface
146     // android.hardware.foo@1.0::IBar
147     // -> android.hardware.foo@1.0::BsBar
148     FQName getInterfacePassthroughFqName() const;
149 
150     // Replace whatever after :: with "types"
151     // android.hardware.foo@1.0::Abc.Type:VALUE
152     // -> android.hardware.foo@1.0::types
153     FQName getTypesForPackage() const;
154 
155     // android.hardware.foo@1.0::Abc.Type:VALUE
156     // -> android.hardware.foo@1.0
157     FQName getPackageAndVersion() const;
158 
159     // the following comments all assume that the FQName
160     // is android.hardware.foo@1.0::IBar.Baz.Bam
161 
162     // returns highest type in the hidl namespace, i.e.
163     // android.hardware.foo@1.0::IBar
164     FQName getTopLevelType() const;
165 
166     // returns an unambiguous fully qualified name which can be
167     // baked into a token, i.e.
168     // android_hardware_Foo_V1_0_IBar_Baz
169     std::string tokenName() const;
170 
171     // Returns an absolute C++ namespace prefix, i.e.
172     // ::android::hardware::Foo::V1_0.
173     std::string cppNamespace() const;
174 
175     // Returns a name qualified assuming we are in cppNamespace, i.e.
176     // IBar::Baz.
177     std::string cppLocalName() const;
178 
179     // Returns a fully qualified absolute C++ type name, i.e.
180     // ::android::hardware::Foo::V1_0::IBar::Baz.
181     std::string cppName() const;
182 
183     // Returns the java package name, i.e. "android.hardware.Foo.V1_0".
184     std::string javaPackage() const;
185 
186     // Returns the fully qualified java type name,
187     // i.e. "android.hardware.Foo.V1_0.IBar.Baz"
188     std::string javaName() const;
189 
190     bool endsWith(const FQName &other) const;
191 
192     // If this is android.hardware@1.0::IFoo
193     // package = "and" -> false
194     // package = "android" -> true
195     // package = "android.hardware@1.0" -> false
196     bool inPackage(const std::string &package) const;
197 
198     void getPackageComponents(std::vector<std::string> *components) const;
199 
200     void getPackageAndVersionComponents(
201             std::vector<std::string> *components,
202             bool cpp_compatible) const;
203 
204     // return major and minor version if they exist, else abort program.
205     // Existence of version can be checked via hasVersion().
206     size_t getPackageMajorVersion() const;
207     size_t getPackageMinorVersion() const;
208 
209     // minor-- if result doesn't underflow, else abort.
210     FQName downRev() const;
211 
212 private:
213     bool mValid;
214     bool mIsIdentifier;
215     std::string mPackage;
216     // mMajor == 0 means empty.
217     size_t mMajor = 0;
218     size_t mMinor = 0;
219     std::string mName;
220     std::string mValueName;
221 
222     void setVersion(const std::string &v);
223     void clearVersion();
224     void parseVersion(const std::string &majorStr, const std::string &minorStr);
225 };
226 
227 static const FQName gIBaseFqName = FQName{"android.hidl.base@1.0::IBase"};
228 static const FQName gIBasePackageFqName = FQName{"android.hidl.base@1.0"};
229 
230 }  // namespace android
231 
232 #endif  // FQNAME_H_
233