blob: b514fe72c1b076ef50cd6b4eb701a5e870addb19 [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"
Andreas Huber84f89de2016-07-28 15:39:51 -070020#include "FQName.h"
Andreas Hubereb1081f2016-07-28 13:13:24 -070021#include "HandleType.h"
Andreas Huberbfd76212016-08-09 11:12:16 -070022#include "Interface.h"
Andreas Huber4b2cf352016-08-31 13:58:19 -070023#include "PredefinedType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070024#include "Scope.h"
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070025#include "TypeDef.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070026
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070027#include <hidl-util/Formatter.h>
Andreas Hubereb1081f2016-07-28 13:13:24 -070028#include <android-base/logging.h>
Andreas Huber39fa7182016-08-19 14:27:33 -070029#include <iostream>
Andreas Huberc9410c72016-07-28 12:18:40 -070030#include <stdlib.h>
31
Andreas Huberc9410c72016-07-28 12:18:40 -070032namespace android {
33
Andreas Huber0d0f9a22016-08-17 10:26:11 -070034AST::AST(Coordinator *coordinator, const std::string &path)
Andreas Huber5345ec22016-07-29 13:33:27 -070035 : mCoordinator(coordinator),
Andreas Huber0d0f9a22016-08-17 10:26:11 -070036 mPath(path),
Andreas Huber5345ec22016-07-29 13:33:27 -070037 mScanner(NULL),
Andreas Huber9ed827c2016-08-22 12:31:13 -070038 mRootScope(new Scope("" /* localName */)) {
Andreas Huberc9410c72016-07-28 12:18:40 -070039 enterScope(mRootScope);
40}
41
42AST::~AST() {
Andreas Huberc9410c72016-07-28 12:18:40 -070043 delete mRootScope;
44 mRootScope = NULL;
45
Andreas Hubereb1081f2016-07-28 13:13:24 -070046 CHECK(mScanner == NULL);
Andreas Huberc9410c72016-07-28 12:18:40 -070047
Andreas Huber5345ec22016-07-29 13:33:27 -070048 // Ownership of "coordinator" was NOT transferred.
Andreas Huberc9410c72016-07-28 12:18:40 -070049}
50
51void *AST::scanner() {
52 return mScanner;
53}
54
55void AST::setScanner(void *scanner) {
56 mScanner = scanner;
57}
58
Andreas Huber0d0f9a22016-08-17 10:26:11 -070059const std::string &AST::getFilename() const {
60 return mPath;
61}
62
Andreas Huber84f89de2016-07-28 15:39:51 -070063bool AST::setPackage(const char *package) {
Andreas Huberda51b8e2016-07-28 16:00:57 -070064 mPackage.setTo(package);
65 CHECK(mPackage.isValid());
Andreas Huber84f89de2016-07-28 15:39:51 -070066
Andreas Huberda51b8e2016-07-28 16:00:57 -070067 if (mPackage.package().empty()
68 || mPackage.version().empty()
69 || !mPackage.name().empty()) {
Andreas Huber84f89de2016-07-28 15:39:51 -070070 return false;
71 }
72
Andreas Huber84f89de2016-07-28 15:39:51 -070073 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070074}
75
Andreas Hubera2723d22016-07-29 15:36:07 -070076FQName AST::package() const {
77 return mPackage;
78}
79
80bool AST::isInterface(std::string *ifaceName) const {
81 return mRootScope->containsSingleInterface(ifaceName);
82}
83
Andreas Huber5345ec22016-07-29 13:33:27 -070084bool AST::addImport(const char *import) {
85 FQName fqName(import);
86 CHECK(fqName.isValid());
Andreas Hubereb1081f2016-07-28 13:13:24 -070087
Andreas Huber5345ec22016-07-29 13:33:27 -070088 fqName.applyDefaults(mPackage.package(), mPackage.version());
89
Andreas Huber68f24592016-07-29 14:53:48 -070090 // LOG(INFO) << "importing " << fqName.string();
Andreas Huber5345ec22016-07-29 13:33:27 -070091
92 if (fqName.name().empty()) {
Andreas Huberd2943e12016-08-05 11:59:31 -070093 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -070094
Andreas Huberd2943e12016-08-05 11:59:31 -070095 status_t err =
Iliyan Malchev5bb14022016-08-09 15:04:39 -070096 mCoordinator->appendPackageInterfacesToSet(fqName,
97 &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -070098
99 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700100 return false;
101 }
102
Andreas Huberd2943e12016-08-05 11:59:31 -0700103 for (const auto &subFQName : packageInterfaces) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700104 AST *ast = mCoordinator->parse(subFQName, &mImportedASTs);
105 if (ast == NULL) {
Andreas Huber68f24592016-07-29 14:53:48 -0700106 return false;
107 }
108 }
109
110 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700111 }
112
Andreas Huber39fa7182016-08-19 14:27:33 -0700113 AST *importAST = mCoordinator->parse(fqName, &mImportedASTs);
Andreas Huber5345ec22016-07-29 13:33:27 -0700114
Andreas Huber68f24592016-07-29 14:53:48 -0700115 if (importAST == NULL) {
116 return false;
117 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700118
119 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700120}
121
Andreas Huber39fa7182016-08-19 14:27:33 -0700122void AST::addImportedAST(AST *ast) {
123 mImportedASTs.insert(ast);
124}
125
Andreas Huberc9410c72016-07-28 12:18:40 -0700126void AST::enterScope(Scope *container) {
127 mScopePath.push_back(container);
128}
129
130void AST::leaveScope() {
131 mScopePath.pop();
132}
133
134Scope *AST::scope() {
Andreas Hubereb1081f2016-07-28 13:13:24 -0700135 CHECK(!mScopePath.empty());
Andreas Huberc9410c72016-07-28 12:18:40 -0700136 return mScopePath.top();
137}
138
Andreas Huber39fa7182016-08-19 14:27:33 -0700139bool AST::addTypeDef(
140 const char *localName, Type *type, std::string *errorMsg) {
141 // The reason we wrap the given type in a TypeDef is simply to suppress
142 // emitting any type definitions later on, since this is just an alias
143 // to a type defined elsewhere.
144 return addScopedTypeInternal(
145 localName, new TypeDef(type), errorMsg, true /* isTypeDef */);
146}
147
Andreas Huber9ed827c2016-08-22 12:31:13 -0700148bool AST::addScopedType(NamedType *type, std::string *errorMsg) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700149 return addScopedTypeInternal(
Andreas Huber9ed827c2016-08-22 12:31:13 -0700150 type->localName().c_str(), type, errorMsg, false /* isTypeDef */);
Andreas Huber39fa7182016-08-19 14:27:33 -0700151}
152
153bool AST::addScopedTypeInternal(
154 const char *localName,
155 Type *type,
156 std::string *errorMsg,
157 bool isTypeDef) {
158 if (!isTypeDef) {
159 // Resolve typeDefs to the target type.
160 while (type->isTypeDef()) {
161 type = static_cast<TypeDef *>(type)->referencedType();
162 }
163 }
164
Andreas Huber31629bc2016-08-03 09:06:40 -0700165 // LOG(INFO) << "adding scoped type '" << localName << "'";
166
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700167 bool success = scope()->addType(localName, type, errorMsg);
Andreas Huber5a545442016-08-03 10:44:56 -0700168 if (!success) {
169 return false;
170 }
171
Andreas Huber31629bc2016-08-03 09:06:40 -0700172 std::string path;
173 for (size_t i = 1; i < mScopePath.size(); ++i) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700174 path.append(mScopePath[i]->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700175 path.append(".");
176 }
177 path.append(localName);
178
Andreas Huber31629bc2016-08-03 09:06:40 -0700179 FQName fqName(mPackage.package(), mPackage.version(), path);
Andreas Huber39fa7182016-08-19 14:27:33 -0700180
181 if (!isTypeDef) {
182 CHECK(type->isNamedType());
183
184 NamedType *namedType = static_cast<NamedType *>(type);
Andreas Huber39fa7182016-08-19 14:27:33 -0700185 namedType->setFullName(fqName);
186 }
187
188 mDefinedTypesByFullName.add(fqName, type);
Andreas Huber31629bc2016-08-03 09:06:40 -0700189
Andreas Huber5a545442016-08-03 10:44:56 -0700190 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700191}
192
Andreas Huberfd4afab2016-08-03 13:02:57 -0700193Type *AST::lookupType(const char *name) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700194 FQName fqName(name);
195 CHECK(fqName.isValid());
196
Andreas Huberda51b8e2016-07-28 16:00:57 -0700197 if (fqName.name().empty()) {
198 // Given a package and version???
199 return NULL;
200 }
201
Andreas Huber84f89de2016-07-28 15:39:51 -0700202 if (fqName.package().empty() && fqName.version().empty()) {
203 // This is just a plain identifier, resolve locally first if possible.
204
205 for (size_t i = mScopePath.size(); i-- > 0;) {
206 Type *type = mScopePath[i]->lookupType(name);
207
208 if (type != NULL) {
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700209 // Resolve typeDefs to the target type.
210 while (type->isTypeDef()) {
211 type = static_cast<TypeDef *>(type)->referencedType();
212 }
213
Andreas Huberfd4afab2016-08-03 13:02:57 -0700214 return type->ref();
Andreas Huber84f89de2016-07-28 15:39:51 -0700215 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700216 }
217 }
218
Andreas Huber39fa7182016-08-19 14:27:33 -0700219 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700220 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700221 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700222
Andreas Huber39fa7182016-08-19 14:27:33 -0700223 for (const auto &importedAST : mImportedASTs) {
224 FQName matchingName;
225 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700226
Andreas Huber39fa7182016-08-19 14:27:33 -0700227 if (match != nullptr) {
228 if (resolvedType != nullptr) {
229 std::cerr << "ERROR: Unable to resolve type name '"
230 << fqName.string()
231 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700232
Andreas Huber39fa7182016-08-19 14:27:33 -0700233 std::cerr << " " << resolvedName.string() << "\n";
234 std::cerr << " " << matchingName.string() << "\n";
235
236 return NULL;
237 }
238
239 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700240 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700241 resolvedName = matchingName;
242
243 // Keep going even after finding a match.
244 }
245 }
246
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700247 if (resolvedType == nullptr
248 && fqName.package().empty()
249 && fqName.version().empty()
250 && fqName.name() == "MQDescriptor") {
251 return new PredefinedType("::android::hardware::MQDescriptor");
252 }
253
Andreas Huber39fa7182016-08-19 14:27:33 -0700254 if (resolvedType) {
255#if 0
256 LOG(INFO) << "found '"
257 << resolvedName.string()
258 << "' after looking for '"
259 << fqName.string()
260 << "'.";
261#endif
262
263 // Resolve typeDefs to the target type.
264 while (resolvedType->isTypeDef()) {
265 resolvedType =
266 static_cast<TypeDef *>(resolvedType)->referencedType();
267 }
268
Iliyan Malchev800273d2016-09-02 15:25:07 -0700269 returnedType = resolvedType;
270
271 // If the resolved type is not an interface, we need to determine
272 // whether it is defined in types.hal, or in some other interface. In
273 // the latter case, we need to emit a dependency for the interface in
274 // which the type is defined.
275 //
276 // Consider the following:
277 // android.hardware.tests.foo@1.0::Record
278 // android.hardware.tests.foo@1.0::IFoo.Folder
279 // android.hardware.tests.foo@1.0::Folder
280 //
281 // If Record is an interface, then we keep track of it for the purpose
282 // of emitting dependencies in the target language (for example #include
283 // in C++). If Record is a UDT, then we assume it is defined in
284 // types.hal in android.hardware.tests.foo@1.0.
285 //
286 // In the case of IFoo.Folder, the same applies. If IFoo is an
287 // interface, we need to track this for the purpose of emitting
288 // dependencies. If not, then it must have been defined in types.hal.
289 //
290 // In the case of just specifying Folder, the resolved type is
291 // android.hardware.tests.foo@1.0::IFoo.Folder, and the same logic as
292 // above applies.
293
294 if (!resolvedType->isInterface()) {
295 FQName ifc(resolvedName.package(),
296 resolvedName.version(),
297 resolvedName.names().at(0));
298 for (const auto &importedAST : mImportedASTs) {
299 FQName matchingName;
300 Type *match = importedAST->findDefinedType(ifc, &matchingName);
301 if (match != nullptr && match->isInterface()) {
302 resolvedType = match;
303 }
304 }
305 }
306
Andreas Huber39fa7182016-08-19 14:27:33 -0700307 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700308 // Non-interface types are declared in the associated types header.
Andreas Huber39fa7182016-08-19 14:27:33 -0700309 FQName typesName(
310 resolvedName.package(), resolvedName.version(), "types");
311
Andreas Huber0e00de42016-08-03 09:56:02 -0700312 mImportedNames.insert(typesName);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700313
314 if (resolvedType->isNamedType()) {
315 mImportedNamesForJava.insert(
316 static_cast<NamedType *>(resolvedType)->fqName());
317 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700318 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700319 // Do _not_ use fqName, i.e. the name we used to look up the type,
320 // but instead use the name of the interface we found.
321 // This is necessary because if fqName pointed to a typedef which
322 // in turn referenced the found interface we'd mistakenly use the
323 // name of the typedef instead of the proper name of the interface.
324
325 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700326 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber85eabdb2016-08-25 11:24:49 -0700327
328 mImportedNamesForJava.insert(
329 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700330 }
Andreas Huber737080b2016-08-02 15:38:04 -0700331 }
332
Iliyan Malchev800273d2016-09-02 15:25:07 -0700333 return returnedType->ref();
Andreas Huber5345ec22016-07-29 13:33:27 -0700334}
335
Andreas Huber39fa7182016-08-19 14:27:33 -0700336Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
337 for (size_t i = 0; i < mDefinedTypesByFullName.size(); ++i) {
338 const FQName &key = mDefinedTypesByFullName.keyAt(i);
Andreas Huber5345ec22016-07-29 13:33:27 -0700339
Andreas Huber39fa7182016-08-19 14:27:33 -0700340 if (key.endsWith(fqName)) {
341 *matchingName = key;
342 return mDefinedTypesByFullName.valueAt(i);
Andreas Huber5345ec22016-07-29 13:33:27 -0700343 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700344 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700345
346 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700347}
348
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700349void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700350 for (const auto &fqName : mImportedNames) {
351 FQName packageName(fqName.package(), fqName.version(), "");
352
353 if (packageName == mPackage) {
354 // We only care about external imports, not our own package.
355 continue;
356 }
357
358 importSet->insert(packageName);
359 }
360}
361
Andreas Huber0fa9e392016-08-31 09:05:44 -0700362bool AST::isJavaCompatible() const {
363 std::string ifaceName;
364 if (!AST::isInterface(&ifaceName)) {
365 for (size_t i = 0; i < mRootScope->countTypes(); ++i) {
366 std::string typeName;
367 const Type *type = mRootScope->typeAt(i, &typeName);
368
369 if (!type->isJavaCompatible()) {
370 return false;
371 }
372 }
373
374 return true;
375 }
376
377 const Interface *iface = mRootScope->getInterface();
378 return iface->isJavaCompatible();
379}
380
Andreas Huberc9410c72016-07-28 12:18:40 -0700381} // namespace android;