blob: c2bfc68dbb866b253e86d6b0205e3ce92e5c7ae6 [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 Huberc9410c72016-07-28 12:18:40 -070017#include "AST.h"
18
Andreas Huber5345ec22016-07-29 13:33:27 -070019#include "Coordinator.h"
Yifan Hongf24fa852016-09-23 11:03:15 -070020#include "EnumType.h"
Andreas Huber84f89de2016-07-28 15:39:51 -070021#include "FQName.h"
Andreas Hubereb1081f2016-07-28 13:13:24 -070022#include "HandleType.h"
Andreas Huberbfd76212016-08-09 11:12:16 -070023#include "Interface.h"
Andreas Huber4b2cf352016-08-31 13:58:19 -070024#include "PredefinedType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070025#include "Scope.h"
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070026#include "TypeDef.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070027
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Andreas Hubereb1081f2016-07-28 13:13:24 -070029#include <android-base/logging.h>
Andreas Huber39fa7182016-08-19 14:27:33 -070030#include <iostream>
Andreas Huberc9410c72016-07-28 12:18:40 -070031#include <stdlib.h>
32
Andreas Huberc9410c72016-07-28 12:18:40 -070033namespace android {
34
Andreas Huber0d0f9a22016-08-17 10:26:11 -070035AST::AST(Coordinator *coordinator, const std::string &path)
Andreas Huber5345ec22016-07-29 13:33:27 -070036 : mCoordinator(coordinator),
Andreas Huber0d0f9a22016-08-17 10:26:11 -070037 mPath(path),
Andreas Huber5345ec22016-07-29 13:33:27 -070038 mScanner(NULL),
Andreas Huber9ed827c2016-08-22 12:31:13 -070039 mRootScope(new Scope("" /* localName */)) {
Andreas Huberc9410c72016-07-28 12:18:40 -070040 enterScope(mRootScope);
41}
42
43AST::~AST() {
Andreas Huberc9410c72016-07-28 12:18:40 -070044 delete mRootScope;
45 mRootScope = NULL;
46
Andreas Hubereb1081f2016-07-28 13:13:24 -070047 CHECK(mScanner == NULL);
Andreas Huberc9410c72016-07-28 12:18:40 -070048
Andreas Huber5345ec22016-07-29 13:33:27 -070049 // Ownership of "coordinator" was NOT transferred.
Andreas Huberc9410c72016-07-28 12:18:40 -070050}
51
52void *AST::scanner() {
53 return mScanner;
54}
55
56void AST::setScanner(void *scanner) {
57 mScanner = scanner;
58}
59
Andreas Huber0d0f9a22016-08-17 10:26:11 -070060const std::string &AST::getFilename() const {
61 return mPath;
62}
63
Andreas Huber84f89de2016-07-28 15:39:51 -070064bool AST::setPackage(const char *package) {
Andreas Huberda51b8e2016-07-28 16:00:57 -070065 mPackage.setTo(package);
66 CHECK(mPackage.isValid());
Andreas Huber84f89de2016-07-28 15:39:51 -070067
Andreas Huberda51b8e2016-07-28 16:00:57 -070068 if (mPackage.package().empty()
69 || mPackage.version().empty()
70 || !mPackage.name().empty()) {
Andreas Huber84f89de2016-07-28 15:39:51 -070071 return false;
72 }
73
Andreas Huber84f89de2016-07-28 15:39:51 -070074 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070075}
76
Andreas Hubera2723d22016-07-29 15:36:07 -070077FQName AST::package() const {
78 return mPackage;
79}
80
81bool AST::isInterface(std::string *ifaceName) const {
82 return mRootScope->containsSingleInterface(ifaceName);
83}
84
Andreas Huber5345ec22016-07-29 13:33:27 -070085bool AST::addImport(const char *import) {
86 FQName fqName(import);
87 CHECK(fqName.isValid());
Andreas Hubereb1081f2016-07-28 13:13:24 -070088
Andreas Huber5345ec22016-07-29 13:33:27 -070089 fqName.applyDefaults(mPackage.package(), mPackage.version());
90
Andreas Huber68f24592016-07-29 14:53:48 -070091 // LOG(INFO) << "importing " << fqName.string();
Andreas Huber5345ec22016-07-29 13:33:27 -070092
93 if (fqName.name().empty()) {
Andreas Huberd2943e12016-08-05 11:59:31 -070094 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -070095
Andreas Huberd2943e12016-08-05 11:59:31 -070096 status_t err =
Steven Morelandaa186832016-09-26 13:51:43 -070097 mCoordinator->appendPackageInterfacesToVector(fqName,
98 &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -070099
100 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700101 return false;
102 }
103
Andreas Huberd2943e12016-08-05 11:59:31 -0700104 for (const auto &subFQName : packageInterfaces) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700105 AST *ast = mCoordinator->parse(subFQName, &mImportedASTs);
106 if (ast == NULL) {
Andreas Huber68f24592016-07-29 14:53:48 -0700107 return false;
108 }
109 }
110
111 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700112 }
113
Andreas Huber39fa7182016-08-19 14:27:33 -0700114 AST *importAST = mCoordinator->parse(fqName, &mImportedASTs);
Andreas Huber5345ec22016-07-29 13:33:27 -0700115
Andreas Huber68f24592016-07-29 14:53:48 -0700116 if (importAST == NULL) {
117 return false;
118 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700119
120 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700121}
122
Andreas Huber39fa7182016-08-19 14:27:33 -0700123void AST::addImportedAST(AST *ast) {
124 mImportedASTs.insert(ast);
125}
126
Andreas Huberc9410c72016-07-28 12:18:40 -0700127void AST::enterScope(Scope *container) {
128 mScopePath.push_back(container);
129}
130
131void AST::leaveScope() {
Steven Morelandd537ab02016-09-12 10:32:01 -0700132 mScopePath.pop_back();
Andreas Huberc9410c72016-07-28 12:18:40 -0700133}
134
135Scope *AST::scope() {
Andreas Hubereb1081f2016-07-28 13:13:24 -0700136 CHECK(!mScopePath.empty());
Steven Morelandd537ab02016-09-12 10:32:01 -0700137 return mScopePath.back();
Andreas Huberc9410c72016-07-28 12:18:40 -0700138}
139
Andreas Huber39fa7182016-08-19 14:27:33 -0700140bool AST::addTypeDef(
141 const char *localName, Type *type, std::string *errorMsg) {
142 // The reason we wrap the given type in a TypeDef is simply to suppress
143 // emitting any type definitions later on, since this is just an alias
144 // to a type defined elsewhere.
145 return addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700146 new TypeDef(localName, type), errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700147}
148
Andreas Huber9ed827c2016-08-22 12:31:13 -0700149bool AST::addScopedType(NamedType *type, std::string *errorMsg) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700150 return addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700151 type, errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700152}
153
154bool AST::addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700155 NamedType *type,
156 std::string *errorMsg) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700157
Steven Morelandd537ab02016-09-12 10:32:01 -0700158 bool success = scope()->addType(type, errorMsg);
Andreas Huber5a545442016-08-03 10:44:56 -0700159 if (!success) {
160 return false;
161 }
162
Andreas Huber31629bc2016-08-03 09:06:40 -0700163 std::string path;
164 for (size_t i = 1; i < mScopePath.size(); ++i) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700165 path.append(mScopePath[i]->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700166 path.append(".");
167 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700168 path.append(type->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700169
Andreas Huber31629bc2016-08-03 09:06:40 -0700170 FQName fqName(mPackage.package(), mPackage.version(), path);
Andreas Huber39fa7182016-08-19 14:27:33 -0700171
Steven Morelandd537ab02016-09-12 10:32:01 -0700172 type->setFullName(fqName);
Andreas Huber39fa7182016-08-19 14:27:33 -0700173
Steven Morelandd537ab02016-09-12 10:32:01 -0700174 mDefinedTypesByFullName[fqName] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700175
Andreas Huber5a545442016-08-03 10:44:56 -0700176 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700177}
178
Yifan Hongf24fa852016-09-23 11:03:15 -0700179EnumValue *AST::lookupEnumValue(const FQName &fqName, std::string *errorMsg) {
180
181 FQName enumTypeName = fqName.typeName();
182 std::string enumValueName = fqName.valueName();
183
184 CHECK(enumTypeName.isValid());
185 CHECK(!enumValueName.empty());
186
187 Type *type = lookupType(enumTypeName);
188 if(type == nullptr) {
189 *errorMsg = "Cannot find type " + enumTypeName.string();
190 return nullptr;
191 }
192 if(!type->isEnum()) {
193 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
194 return nullptr;
195 }
196
197 EnumType *enumType = static_cast<EnumType *>(type);
198 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
199 if(v == nullptr) {
200 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
201 return nullptr;
202 }
203 return v;
204}
205
Yifan Hongae16eed2016-09-23 13:25:25 -0700206Type *AST::lookupType(const FQName &fqName) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700207 CHECK(fqName.isValid());
208
Andreas Huberda51b8e2016-07-28 16:00:57 -0700209 if (fqName.name().empty()) {
210 // Given a package and version???
211 return NULL;
212 }
213
Andreas Huber84f89de2016-07-28 15:39:51 -0700214 if (fqName.package().empty() && fqName.version().empty()) {
215 // This is just a plain identifier, resolve locally first if possible.
216
217 for (size_t i = mScopePath.size(); i-- > 0;) {
Yifan Hongae16eed2016-09-23 13:25:25 -0700218 Type *type = mScopePath[i]->lookupType(fqName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700219
220 if (type != NULL) {
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700221 // Resolve typeDefs to the target type.
222 while (type->isTypeDef()) {
223 type = static_cast<TypeDef *>(type)->referencedType();
224 }
225
Andreas Huberfd4afab2016-08-03 13:02:57 -0700226 return type->ref();
Andreas Huber84f89de2016-07-28 15:39:51 -0700227 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700228 }
229 }
230
Andreas Huber39fa7182016-08-19 14:27:33 -0700231 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700232 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700233 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700234
Andreas Huber39fa7182016-08-19 14:27:33 -0700235 for (const auto &importedAST : mImportedASTs) {
236 FQName matchingName;
237 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700238
Andreas Huber39fa7182016-08-19 14:27:33 -0700239 if (match != nullptr) {
240 if (resolvedType != nullptr) {
241 std::cerr << "ERROR: Unable to resolve type name '"
242 << fqName.string()
243 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700244
Andreas Huber39fa7182016-08-19 14:27:33 -0700245 std::cerr << " " << resolvedName.string() << "\n";
246 std::cerr << " " << matchingName.string() << "\n";
247
248 return NULL;
249 }
250
251 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700252 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700253 resolvedName = matchingName;
254
255 // Keep going even after finding a match.
256 }
257 }
258
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700259 if (resolvedType == nullptr
260 && fqName.package().empty()
261 && fqName.version().empty()
Hridya Valsarajub77944a2016-09-22 09:34:20 -0700262 && fqName.name() == "MQDescriptorSync") {
263 return new PredefinedType(
264 "::android::hardware::MQDescriptorSync");
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700265 }
266
Andreas Huber39fa7182016-08-19 14:27:33 -0700267 if (resolvedType) {
268#if 0
269 LOG(INFO) << "found '"
270 << resolvedName.string()
271 << "' after looking for '"
272 << fqName.string()
273 << "'.";
274#endif
275
276 // Resolve typeDefs to the target type.
277 while (resolvedType->isTypeDef()) {
278 resolvedType =
279 static_cast<TypeDef *>(resolvedType)->referencedType();
280 }
281
Iliyan Malchev800273d2016-09-02 15:25:07 -0700282 returnedType = resolvedType;
283
284 // If the resolved type is not an interface, we need to determine
285 // whether it is defined in types.hal, or in some other interface. In
286 // the latter case, we need to emit a dependency for the interface in
287 // which the type is defined.
288 //
289 // Consider the following:
290 // android.hardware.tests.foo@1.0::Record
291 // android.hardware.tests.foo@1.0::IFoo.Folder
292 // android.hardware.tests.foo@1.0::Folder
293 //
294 // If Record is an interface, then we keep track of it for the purpose
295 // of emitting dependencies in the target language (for example #include
296 // in C++). If Record is a UDT, then we assume it is defined in
297 // types.hal in android.hardware.tests.foo@1.0.
298 //
299 // In the case of IFoo.Folder, the same applies. If IFoo is an
300 // interface, we need to track this for the purpose of emitting
301 // dependencies. If not, then it must have been defined in types.hal.
302 //
303 // In the case of just specifying Folder, the resolved type is
304 // android.hardware.tests.foo@1.0::IFoo.Folder, and the same logic as
305 // above applies.
306
307 if (!resolvedType->isInterface()) {
308 FQName ifc(resolvedName.package(),
309 resolvedName.version(),
310 resolvedName.names().at(0));
311 for (const auto &importedAST : mImportedASTs) {
312 FQName matchingName;
313 Type *match = importedAST->findDefinedType(ifc, &matchingName);
314 if (match != nullptr && match->isInterface()) {
315 resolvedType = match;
316 }
317 }
318 }
319
Andreas Huber39fa7182016-08-19 14:27:33 -0700320 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700321 // Non-interface types are declared in the associated types header.
Andreas Huber39fa7182016-08-19 14:27:33 -0700322 FQName typesName(
323 resolvedName.package(), resolvedName.version(), "types");
324
Andreas Huber0e00de42016-08-03 09:56:02 -0700325 mImportedNames.insert(typesName);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700326
Steven Morelandd537ab02016-09-12 10:32:01 -0700327 if (resolvedType->isNamedType() && !resolvedType->isTypeDef()) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700328 mImportedNamesForJava.insert(
329 static_cast<NamedType *>(resolvedType)->fqName());
330 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700331 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700332 // Do _not_ use fqName, i.e. the name we used to look up the type,
333 // but instead use the name of the interface we found.
334 // This is necessary because if fqName pointed to a typedef which
335 // in turn referenced the found interface we'd mistakenly use the
336 // name of the typedef instead of the proper name of the interface.
337
338 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700339 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber85eabdb2016-08-25 11:24:49 -0700340
341 mImportedNamesForJava.insert(
342 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700343 }
Andreas Huber737080b2016-08-02 15:38:04 -0700344 }
345
Iliyan Malchev800273d2016-09-02 15:25:07 -0700346 return returnedType->ref();
Andreas Huber5345ec22016-07-29 13:33:27 -0700347}
348
Andreas Huber39fa7182016-08-19 14:27:33 -0700349Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700350 for (const auto &pair : mDefinedTypesByFullName) {
351 const FQName &key = pair.first;
352 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700353
Andreas Huber39fa7182016-08-19 14:27:33 -0700354 if (key.endsWith(fqName)) {
355 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700356 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700357 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700358 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700359
360 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700361}
362
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700363void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700364 for (const auto &fqName : mImportedNames) {
365 FQName packageName(fqName.package(), fqName.version(), "");
366
367 if (packageName == mPackage) {
368 // We only care about external imports, not our own package.
369 continue;
370 }
371
372 importSet->insert(packageName);
373 }
374}
375
Andreas Huber0fa9e392016-08-31 09:05:44 -0700376bool AST::isJavaCompatible() const {
377 std::string ifaceName;
378 if (!AST::isInterface(&ifaceName)) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700379 for (const auto *type : mRootScope->getSubTypes()) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700380 if (!type->isJavaCompatible()) {
381 return false;
382 }
383 }
384
385 return true;
386 }
387
388 const Interface *iface = mRootScope->getInterface();
389 return iface->isJavaCompatible();
390}
391
Andreas Huberc9410c72016-07-28 12:18:40 -0700392} // namespace android;