blob: 99e9f9adf039e74134f3a1602e0169bcaacb9f67 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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
Andreas Huber84f89de2016-07-28 15:39:51 -070017#include "FQName.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <hidl-util/StringHelper.h>
Andreas Huber84f89de2016-07-28 15:39:51 -070020#include <android-base/logging.h>
Iliyan Malchev800273d2016-09-02 15:25:07 -070021#include <iostream>
Andreas Huber84f89de2016-07-28 15:39:51 -070022#include <regex>
Iliyan Malchev800273d2016-09-02 15:25:07 -070023#include <sstream>
Andreas Huber84f89de2016-07-28 15:39:51 -070024
25#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
Yifan Hong95a47bb2016-09-29 10:08:10 -070026#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
Yifan Hong90ea87f2016-11-01 14:25:47 -070027#define RE_MAJOR "[0-9]+"
28#define RE_MINOR "[0-9]+"
Andreas Huber84f89de2016-07-28 15:39:51 -070029
Yifan Hong333a6d22017-01-12 12:28:02 -080030// android.hardware.foo@1.0::IFoo.Type
31static const std::regex kRE1("(" RE_PATH ")@(" RE_MAJOR ")[.](" RE_MINOR ")::(" RE_PATH ")");
32// @1.0::IFoo.Type
Yifan Hong90ea87f2016-11-01 14:25:47 -070033static const std::regex kRE2("@(" RE_MAJOR ")[.](" RE_MINOR ")::(" RE_PATH ")");
Yifan Hong333a6d22017-01-12 12:28:02 -080034// android.hardware.foo@1.0 (for package declaration and whole package import)
Yifan Hong90ea87f2016-11-01 14:25:47 -070035static const std::regex kRE3("(" RE_PATH ")@(" RE_MAJOR ")[.](" RE_MINOR ")");
Yifan Hong333a6d22017-01-12 12:28:02 -080036// IFoo.Type
Yifan Hongb44a6c82016-09-22 15:50:18 -070037static const std::regex kRE4("(" RE_COMPONENT ")([.]" RE_COMPONENT ")+");
Yifan Hong333a6d22017-01-12 12:28:02 -080038// Type (a plain identifier)
Yifan Hongb44a6c82016-09-22 15:50:18 -070039static const std::regex kRE5("(" RE_COMPONENT ")");
40
Yifan Hong333a6d22017-01-12 12:28:02 -080041// android.hardware.foo@1.0::IFoo.Type:MY_ENUM_VALUE
42static const std::regex kRE6("(" RE_PATH ")@(" RE_MAJOR ")[.](" RE_MINOR ")::(" RE_PATH "):(" RE_COMPONENT ")");
43// @1.0::IFoo.Type:MY_ENUM_VALUE
Yifan Hong90ea87f2016-11-01 14:25:47 -070044static const std::regex kRE7("@(" RE_MAJOR ")[.](" RE_MINOR ")::(" RE_PATH "):(" RE_COMPONENT ")");
Yifan Hong333a6d22017-01-12 12:28:02 -080045// IFoo.Type:MY_ENUM_VALUE
Yifan Hongb44a6c82016-09-22 15:50:18 -070046static const std::regex kRE8("(" RE_PATH "):(" RE_COMPONENT ")");
Andreas Huber84f89de2016-07-28 15:39:51 -070047
Yifan Hong333a6d22017-01-12 12:28:02 -080048// 1.0
Yifan Hong90ea87f2016-11-01 14:25:47 -070049static const std::regex kREVer("(" RE_MAJOR ")[.](" RE_MINOR ")");
50
Andreas Huber84f89de2016-07-28 15:39:51 -070051namespace android {
52
Andreas Huberda51b8e2016-07-28 16:00:57 -070053FQName::FQName()
Yifan Hong327cfe12016-10-03 10:29:42 -070054 : mValid(false),
55 mIsIdentifier(false) {
Andreas Huberda51b8e2016-07-28 16:00:57 -070056}
57
Andreas Huber84f89de2016-07-28 15:39:51 -070058FQName::FQName(const std::string &s)
Yifan Hong327cfe12016-10-03 10:29:42 -070059 : mValid(false),
60 mIsIdentifier(false) {
Andreas Huber84f89de2016-07-28 15:39:51 -070061 setTo(s);
62}
63
Andreas Huber68f24592016-07-29 14:53:48 -070064FQName::FQName(
65 const std::string &package,
66 const std::string &version,
Yifan Hongb44a6c82016-09-22 15:50:18 -070067 const std::string &name,
68 const std::string &valueName)
Andreas Huber68f24592016-07-29 14:53:48 -070069 : mValid(true),
Yifan Hongb44a6c82016-09-22 15:50:18 -070070 mIsIdentifier(false),
Andreas Huber68f24592016-07-29 14:53:48 -070071 mPackage(package),
Yifan Hongb44a6c82016-09-22 15:50:18 -070072 mName(name),
73 mValueName(valueName) {
Yifan Hong90ea87f2016-11-01 14:25:47 -070074 setVersion(version);
Andreas Huber68f24592016-07-29 14:53:48 -070075}
76
Yifan Hongf24fa852016-09-23 11:03:15 -070077FQName::FQName(const FQName& other)
78 : mValid(other.mValid),
79 mIsIdentifier(other.mIsIdentifier),
80 mPackage(other.mPackage),
Yifan Hong90ea87f2016-11-01 14:25:47 -070081 mMajor(other.mMajor),
82 mMinor(other.mMinor),
Yifan Hongf24fa852016-09-23 11:03:15 -070083 mName(other.mName),
84 mValueName(other.mValueName) {
85}
86
Yifan Hong327cfe12016-10-03 10:29:42 -070087FQName::FQName(const std::vector<std::string> &names)
88 : mValid(false),
89 mIsIdentifier(false) {
90 setTo(StringHelper::JoinStrings(names, "."));
91}
92
Andreas Huber84f89de2016-07-28 15:39:51 -070093bool FQName::isValid() const {
94 return mValid;
95}
96
Yifan Hongb44a6c82016-09-22 15:50:18 -070097bool FQName::isIdentifier() const {
98 return mIsIdentifier;
99}
100
Andreas Huber68f24592016-07-29 14:53:48 -0700101bool FQName::isFullyQualified() const {
Yifan Hong90ea87f2016-11-01 14:25:47 -0700102 return !mPackage.empty() && !version().empty() && !mName.empty();
Andreas Huber68f24592016-07-29 14:53:48 -0700103}
104
Yifan Hongb44a6c82016-09-22 15:50:18 -0700105bool FQName::isValidValueName() const {
106 return mIsIdentifier
107 || (!mName.empty() && !mValueName.empty());
108}
109
Andreas Huber84f89de2016-07-28 15:39:51 -0700110bool FQName::setTo(const std::string &s) {
111 mPackage.clear();
Yifan Hong90ea87f2016-11-01 14:25:47 -0700112 mMajor.clear();
113 mMinor.clear();
Andreas Huber84f89de2016-07-28 15:39:51 -0700114 mName.clear();
115
116 mValid = true;
117
118 std::smatch match;
119 if (std::regex_match(s, match, kRE1)) {
Yifan Hong90ea87f2016-11-01 14:25:47 -0700120 CHECK_EQ(match.size(), 5u);
121
122 mPackage = match.str(1);
123 mMajor = match.str(2);
124 mMinor = match.str(3);
125 mName = match.str(4);
126 } else if (std::regex_match(s, match, kRE2)) {
127 CHECK_EQ(match.size(), 4u);
128
129 mMajor = match.str(1);
130 mMinor = match.str(2);
131 mName = match.str(3);
132 } else if (std::regex_match(s, match, kRE3)) {
Yifan Hong95a47bb2016-09-29 10:08:10 -0700133 CHECK_EQ(match.size(), 4u);
Andreas Huber84f89de2016-07-28 15:39:51 -0700134
135 mPackage = match.str(1);
Yifan Hong90ea87f2016-11-01 14:25:47 -0700136 mMajor = match.str(2);
137 mMinor = match.str(3);
Andreas Huberda51b8e2016-07-28 16:00:57 -0700138 } else if (std::regex_match(s, match, kRE4)) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700139 mName = match.str(0);
Yifan Hongb44a6c82016-09-22 15:50:18 -0700140 } else if (std::regex_match(s, match, kRE5)) {
141 mIsIdentifier = true;
142 mName = match.str(0);
143 } else if (std::regex_match(s, match, kRE6)) {
Yifan Hong90ea87f2016-11-01 14:25:47 -0700144 CHECK_EQ(match.size(), 6u);
Yifan Hongb44a6c82016-09-22 15:50:18 -0700145
146 mPackage = match.str(1);
Yifan Hong90ea87f2016-11-01 14:25:47 -0700147 mMajor = match.str(2);
148 mMinor = match.str(3);
149 mName = match.str(4);
150 mValueName = match.str(5);
151 } else if (std::regex_match(s, match, kRE7)) {
152 CHECK_EQ(match.size(), 5u);
153
154 mMajor = match.str(1);
155 mMinor = match.str(2);
Yifan Hong95a47bb2016-09-29 10:08:10 -0700156 mName = match.str(3);
157 mValueName = match.str(4);
Yifan Hongb44a6c82016-09-22 15:50:18 -0700158 } else if (std::regex_match(s, match, kRE8)) {
Yifan Hong95a47bb2016-09-29 10:08:10 -0700159 CHECK_EQ(match.size(), 3u);
Yifan Hongb44a6c82016-09-22 15:50:18 -0700160
161 mName = match.str(1);
Yifan Hong95a47bb2016-09-29 10:08:10 -0700162 mValueName = match.str(2);
Andreas Huber84f89de2016-07-28 15:39:51 -0700163 } else {
164 mValid = false;
165 }
166
Yifan Hongb44a6c82016-09-22 15:50:18 -0700167 // mValueName must go with mName.
Yifan Hong333a6d22017-01-12 12:28:02 -0800168 CHECK(mValueName.empty() || !mName.empty());
169
170 // package without version is not allowed.
171 CHECK(mPackage.empty() || !version().empty());
Yifan Hongb44a6c82016-09-22 15:50:18 -0700172
Andreas Huber84f89de2016-07-28 15:39:51 -0700173 return isValid();
174}
175
176std::string FQName::package() const {
177 return mPackage;
178}
179
180std::string FQName::version() const {
Yifan Hong90ea87f2016-11-01 14:25:47 -0700181 CHECK(mMajor.empty() == mMinor.empty());
182 if (mMajor.empty() && mMinor.empty()) {
183 return "";
184 }
185 return mMajor + "." + mMinor;
186}
187
Yifan Hong97288ac2016-12-12 16:03:51 -0800188std::string FQName::sanitizedVersion() const {
189 CHECK(mMajor.empty() == mMinor.empty());
190 if (mMajor.empty() && mMinor.empty()) {
191 return "";
192 }
193 return "V" + mMajor + "_" + mMinor;
194}
195
Yifan Hong90ea87f2016-11-01 14:25:47 -0700196std::string FQName::atVersion() const {
197 std::string v = version();
198 return v.empty() ? "" : ("@" + v);
199}
200
201void FQName::setVersion(const std::string &v) {
202 if (v.empty()) {
203 mMajor.clear();
204 mMinor.clear();
205 return;
206 }
207 std::smatch match;
208 if (std::regex_match(v, match, kREVer)) {
209 CHECK_EQ(match.size(), 3u);
210
211 mMajor = match.str(1);
212 mMinor = match.str(2);
213 } else {
214 mValid = false;
215 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700216}
217
218std::string FQName::name() const {
219 return mName;
220}
221
Iliyan Malchev800273d2016-09-02 15:25:07 -0700222std::vector<std::string> FQName::names() const {
223 std::vector<std::string> res {};
224 std::istringstream ss(name());
225 std::string s;
226 while (std::getline(ss, s, '.')) {
227 res.push_back(s);
228 }
229 return res;
230}
231
Yifan Hongb44a6c82016-09-22 15:50:18 -0700232std::string FQName::valueName() const {
233 return mValueName;
234}
235
236FQName FQName::typeName() const {
Yifan Hong90ea87f2016-11-01 14:25:47 -0700237 return FQName(mPackage, version(), mName);
Yifan Hongb44a6c82016-09-22 15:50:18 -0700238}
239
Andreas Huber84f89de2016-07-28 15:39:51 -0700240void FQName::applyDefaults(
241 const std::string &defaultPackage,
242 const std::string &defaultVersion) {
Yifan Hong333a6d22017-01-12 12:28:02 -0800243
244 // package without version is not allowed.
245 CHECK(mPackage.empty() || !version().empty());
246
Andreas Huber84f89de2016-07-28 15:39:51 -0700247 if (mPackage.empty()) {
248 mPackage = defaultPackage;
249 }
250
Yifan Hong90ea87f2016-11-01 14:25:47 -0700251 if (version().empty()) {
252 setVersion(defaultVersion);
Andreas Huber84f89de2016-07-28 15:39:51 -0700253 }
254}
255
Andreas Huber68f24592016-07-29 14:53:48 -0700256std::string FQName::string() const {
Andreas Huber84f89de2016-07-28 15:39:51 -0700257 CHECK(mValid);
258
Andreas Huber5345ec22016-07-29 13:33:27 -0700259 std::string out;
Andreas Huber84f89de2016-07-28 15:39:51 -0700260 out.append(mPackage);
Yifan Hong90ea87f2016-11-01 14:25:47 -0700261 out.append(atVersion());
Andreas Huberda51b8e2016-07-28 16:00:57 -0700262 if (!mName.empty()) {
Yifan Hong90ea87f2016-11-01 14:25:47 -0700263 if (!mPackage.empty() || !version().empty()) {
Andreas Huberda51b8e2016-07-28 16:00:57 -0700264 out.append("::");
265 }
266 out.append(mName);
Yifan Hongb44a6c82016-09-22 15:50:18 -0700267
268 if (!mValueName.empty()) {
269 out.append(":");
270 out.append(mValueName);
271 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700272 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700273
274 return out;
275}
276
Andreas Huberda51b8e2016-07-28 16:00:57 -0700277void FQName::print() const {
278 if (!mValid) {
279 LOG(INFO) << "INVALID";
280 return;
281 }
282
Andreas Huber68f24592016-07-29 14:53:48 -0700283 LOG(INFO) << string();
284}
285
286bool FQName::operator<(const FQName &other) const {
287 return string() < other.string();
Andreas Huberda51b8e2016-07-28 16:00:57 -0700288}
289
Andreas Huberd2943e12016-08-05 11:59:31 -0700290bool FQName::operator==(const FQName &other) const {
291 return string() == other.string();
292}
293
Yifan Hongc8934042016-11-17 17:10:52 -0800294bool FQName::operator!=(const FQName &other) const {
295 return !(*this == other);
296}
297
Yifan Hongeefe4f22017-01-04 15:32:42 -0800298std::string FQName::getInterfaceName() const {
Steven Moreland197d56c2016-09-09 10:03:58 -0700299 CHECK(names().size() == 1) << "Must be a top level type";
300 CHECK(!mName.empty() && mName[0] == 'I') << mName;
301
Yifan Hongeefe4f22017-01-04 15:32:42 -0800302 return mName;
303}
304
305std::string FQName::getInterfaceBaseName() const {
Steven Moreland197d56c2016-09-09 10:03:58 -0700306 // cut off the leading 'I'.
Yifan Hongeefe4f22017-01-04 15:32:42 -0800307 return getInterfaceName().substr(1);
308}
309
310std::string FQName::getInterfaceHwName() const {
311 return "IHw" + getInterfaceBaseName();
312}
313
314std::string FQName::getInterfaceProxyName() const {
Yifan Hong01e7cde2017-01-09 17:45:45 -0800315 return "BpHw" + getInterfaceBaseName();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800316}
317
318std::string FQName::getInterfaceStubName() const {
Yifan Hong01e7cde2017-01-09 17:45:45 -0800319 return "BnHw" + getInterfaceBaseName();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800320}
321
322std::string FQName::getInterfacePassthroughName() const {
323 return "Bs" + getInterfaceBaseName();
324}
325
326FQName FQName::getInterfaceProxyFqName() const {
327 return FQName(package(), version(), getInterfaceProxyName());
328}
329
330FQName FQName::getInterfaceStubFqName() const {
331 return FQName(package(), version(), getInterfaceStubName());
332}
333
334FQName FQName::getInterfacePassthroughFqName() const {
335 return FQName(package(), version(), getInterfacePassthroughName());
Steven Moreland197d56c2016-09-09 10:03:58 -0700336}
337
Yifan Hong1977ea32016-10-05 12:49:08 -0700338FQName FQName::getTypesForPackage() const {
339 return FQName(package(), version(), "types");
340}
341
Steven Moreland9c387612016-09-07 09:54:26 -0700342const FQName FQName::getTopLevelType() const {
343 auto idx = mName.find('.');
344
345 if (idx == std::string::npos) {
346 return *this;
347 }
348
Yifan Hong90ea87f2016-11-01 14:25:47 -0700349 return FQName(mPackage, version(), mName.substr(0, idx));
Steven Moreland9c387612016-09-07 09:54:26 -0700350}
351
352std::string FQName::tokenName() const {
353 std::vector<std::string> components;
354 getPackageAndVersionComponents(&components, true /* cpp_compatible */);
355
Steven Moreland6f596c82016-11-29 18:45:24 -0800356 if (!mName.empty()) {
357 std::vector<std::string> nameComponents;
358 StringHelper::SplitString(mName, '.', &nameComponents);
Steven Moreland9c387612016-09-07 09:54:26 -0700359
Steven Moreland6f596c82016-11-29 18:45:24 -0800360 components.insert(components.end(), nameComponents.begin(), nameComponents.end());
361 }
Steven Moreland9c387612016-09-07 09:54:26 -0700362
Steven Morelandaf440142016-09-07 10:09:11 -0700363 return StringHelper::JoinStrings(components, "_");
Steven Moreland9c387612016-09-07 09:54:26 -0700364}
365
Andreas Huber0e00de42016-08-03 09:56:02 -0700366std::string FQName::cppNamespace() const {
367 std::vector<std::string> components;
368 getPackageAndVersionComponents(&components, true /* cpp_compatible */);
369
370 std::string out = "::";
Steven Morelandaf440142016-09-07 10:09:11 -0700371 out += StringHelper::JoinStrings(components, "::");
Andreas Huber0e00de42016-08-03 09:56:02 -0700372
373 return out;
374}
375
Steven Moreland979e0992016-09-07 09:18:08 -0700376std::string FQName::cppLocalName() const {
377 std::vector<std::string> components;
Steven Morelandaf440142016-09-07 10:09:11 -0700378 StringHelper::SplitString(mName, '.', &components);
Steven Moreland979e0992016-09-07 09:18:08 -0700379
Yifan Hongb44a6c82016-09-22 15:50:18 -0700380 return StringHelper::JoinStrings(components, "::")
381 + (mValueName.empty() ? "" : ("::" + mValueName));
Steven Moreland979e0992016-09-07 09:18:08 -0700382}
383
Andreas Huber0e00de42016-08-03 09:56:02 -0700384std::string FQName::cppName() const {
385 std::string out = cppNamespace();
386
387 std::vector<std::string> components;
Steven Morelandaf440142016-09-07 10:09:11 -0700388 StringHelper::SplitString(name(), '.', &components);
Andreas Huber0e00de42016-08-03 09:56:02 -0700389 out += "::";
Steven Morelandaf440142016-09-07 10:09:11 -0700390 out += StringHelper::JoinStrings(components, "::");
Yifan Hongb44a6c82016-09-22 15:50:18 -0700391 if (!mValueName.empty()) {
392 out += "::" + mValueName;
393 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700394
395 return out;
396}
397
Andreas Huber2831d512016-08-15 09:33:47 -0700398std::string FQName::javaPackage() const {
399 std::vector<std::string> components;
400 getPackageAndVersionComponents(&components, true /* cpp_compatible */);
401
Steven Morelandaf440142016-09-07 10:09:11 -0700402 return StringHelper::JoinStrings(components, ".");
Andreas Huber2831d512016-08-15 09:33:47 -0700403}
404
405std::string FQName::javaName() const {
Yifan Hongb44a6c82016-09-22 15:50:18 -0700406 return javaPackage() + "." + name()
407 + (mValueName.empty() ? "" : ("." + mValueName));
Andreas Huber2831d512016-08-15 09:33:47 -0700408}
409
Andreas Huber0e00de42016-08-03 09:56:02 -0700410void FQName::getPackageComponents(std::vector<std::string> *components) const {
Steven Morelandaf440142016-09-07 10:09:11 -0700411 StringHelper::SplitString(package(), '.', components);
Andreas Huber0e00de42016-08-03 09:56:02 -0700412}
413
414void FQName::getPackageAndVersionComponents(
415 std::vector<std::string> *components,
416 bool cpp_compatible) const {
417 getPackageComponents(components);
418
Andreas Huber0e00de42016-08-03 09:56:02 -0700419 if (!cpp_compatible) {
Martijn Coenena21f1492016-09-08 15:55:14 +0200420 components->push_back(getPackageMajorVersion() +
421 "." + getPackageMinorVersion());
Andreas Huber0e00de42016-08-03 09:56:02 -0700422 return;
423 }
424
Yifan Hong97288ac2016-12-12 16:03:51 -0800425 components->push_back(sanitizedVersion());
Andreas Huber0e00de42016-08-03 09:56:02 -0700426}
427
Martijn Coenena21f1492016-09-08 15:55:14 +0200428std::string FQName::getPackageMajorVersion() const {
Yifan Hong90ea87f2016-11-01 14:25:47 -0700429 return mMajor;
Martijn Coenena21f1492016-09-08 15:55:14 +0200430}
431
432std::string FQName::getPackageMinorVersion() const {
Yifan Hong90ea87f2016-11-01 14:25:47 -0700433 return mMinor;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700434}
435
Andreas Huber39fa7182016-08-19 14:27:33 -0700436bool FQName::endsWith(const FQName &other) const {
437 std::string s1 = string();
438 std::string s2 = other.string();
439
440 size_t pos = s1.rfind(s2);
441 if (pos == std::string::npos || pos + s2.size() != s1.size()) {
442 return false;
443 }
444
Yifan Hongd9f22f72016-10-05 15:17:33 -0700445 // A match is only a match if it is preceded by a "boundary", i.e.
446 // we perform a component-wise match from the end.
447 // "az" is not a match for "android.hardware.foo@1.0::IFoo.bar.baz",
448 // "baz", "bar.baz", "IFoo.bar.baz", "@1.0::IFoo.bar.baz" are.
449 if (pos == 0) {
450 // matches "android.hardware.foo@1.0::IFoo.bar.baz"
451 return true;
Andreas Huber39fa7182016-08-19 14:27:33 -0700452 }
453
Yifan Hongd9f22f72016-10-05 15:17:33 -0700454 if (s1[pos - 1] == '.') {
455 // matches "baz" and "bar.baz"
456 return true;
457 }
458
459 if (s1[pos - 1] == ':') {
460 // matches "IFoo.bar.baz"
461 return true;
462 }
463
464 if (s1[pos] == '@') {
465 // matches "@1.0::IFoo.bar.baz"
466 return true;
467 }
468
469 return false;
Andreas Huber39fa7182016-08-19 14:27:33 -0700470}
471
Andreas Huber84f89de2016-07-28 15:39:51 -0700472} // namespace android
473