blob: a1fab37baec0623de5f7afcdd12746b75c390fde [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 Huber7c5ddfb2016-09-29 13:45:22 -070085bool AST::containsInterfaces() const {
86 return mRootScope->containsInterfaces();
87}
88
Andreas Huber5345ec22016-07-29 13:33:27 -070089bool AST::addImport(const char *import) {
90 FQName fqName(import);
91 CHECK(fqName.isValid());
Andreas Hubereb1081f2016-07-28 13:13:24 -070092
Andreas Huber5345ec22016-07-29 13:33:27 -070093 fqName.applyDefaults(mPackage.package(), mPackage.version());
94
Andreas Huber68f24592016-07-29 14:53:48 -070095 // LOG(INFO) << "importing " << fqName.string();
Andreas Huber5345ec22016-07-29 13:33:27 -070096
97 if (fqName.name().empty()) {
Andreas Huberd2943e12016-08-05 11:59:31 -070098 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -070099
Andreas Huberd2943e12016-08-05 11:59:31 -0700100 status_t err =
Steven Morelandaa186832016-09-26 13:51:43 -0700101 mCoordinator->appendPackageInterfacesToVector(fqName,
102 &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -0700103
104 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700105 return false;
106 }
107
Andreas Huberd2943e12016-08-05 11:59:31 -0700108 for (const auto &subFQName : packageInterfaces) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700109 AST *ast = mCoordinator->parse(subFQName, &mImportedASTs);
110 if (ast == NULL) {
Andreas Huber68f24592016-07-29 14:53:48 -0700111 return false;
112 }
113 }
114
115 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700116 }
117
Andreas Huber39fa7182016-08-19 14:27:33 -0700118 AST *importAST = mCoordinator->parse(fqName, &mImportedASTs);
Andreas Huber5345ec22016-07-29 13:33:27 -0700119
Andreas Huber68f24592016-07-29 14:53:48 -0700120 if (importAST == NULL) {
121 return false;
122 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700123
124 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700125}
126
Andreas Huber39fa7182016-08-19 14:27:33 -0700127void AST::addImportedAST(AST *ast) {
128 mImportedASTs.insert(ast);
129}
130
Andreas Huberc9410c72016-07-28 12:18:40 -0700131void AST::enterScope(Scope *container) {
132 mScopePath.push_back(container);
133}
134
135void AST::leaveScope() {
Steven Morelandd537ab02016-09-12 10:32:01 -0700136 mScopePath.pop_back();
Andreas Huberc9410c72016-07-28 12:18:40 -0700137}
138
139Scope *AST::scope() {
Andreas Hubereb1081f2016-07-28 13:13:24 -0700140 CHECK(!mScopePath.empty());
Steven Morelandd537ab02016-09-12 10:32:01 -0700141 return mScopePath.back();
Andreas Huberc9410c72016-07-28 12:18:40 -0700142}
143
Andreas Huber39fa7182016-08-19 14:27:33 -0700144bool AST::addTypeDef(
145 const char *localName, Type *type, std::string *errorMsg) {
146 // The reason we wrap the given type in a TypeDef is simply to suppress
147 // emitting any type definitions later on, since this is just an alias
148 // to a type defined elsewhere.
149 return addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700150 new TypeDef(localName, type), errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700151}
152
Andreas Huber9ed827c2016-08-22 12:31:13 -0700153bool AST::addScopedType(NamedType *type, std::string *errorMsg) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700154 return addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700155 type, errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700156}
157
158bool AST::addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700159 NamedType *type,
160 std::string *errorMsg) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700161
Steven Morelandd537ab02016-09-12 10:32:01 -0700162 bool success = scope()->addType(type, errorMsg);
Andreas Huber5a545442016-08-03 10:44:56 -0700163 if (!success) {
164 return false;
165 }
166
Andreas Huber31629bc2016-08-03 09:06:40 -0700167 std::string path;
168 for (size_t i = 1; i < mScopePath.size(); ++i) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700169 path.append(mScopePath[i]->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700170 path.append(".");
171 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700172 path.append(type->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700173
Andreas Huber31629bc2016-08-03 09:06:40 -0700174 FQName fqName(mPackage.package(), mPackage.version(), path);
Andreas Huber39fa7182016-08-19 14:27:33 -0700175
Steven Morelandd537ab02016-09-12 10:32:01 -0700176 type->setFullName(fqName);
Andreas Huber39fa7182016-08-19 14:27:33 -0700177
Steven Morelandd537ab02016-09-12 10:32:01 -0700178 mDefinedTypesByFullName[fqName] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700179
Andreas Huber5a545442016-08-03 10:44:56 -0700180 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700181}
182
Yifan Hongf24fa852016-09-23 11:03:15 -0700183EnumValue *AST::lookupEnumValue(const FQName &fqName, std::string *errorMsg) {
184
185 FQName enumTypeName = fqName.typeName();
186 std::string enumValueName = fqName.valueName();
187
188 CHECK(enumTypeName.isValid());
189 CHECK(!enumValueName.empty());
190
191 Type *type = lookupType(enumTypeName);
192 if(type == nullptr) {
193 *errorMsg = "Cannot find type " + enumTypeName.string();
194 return nullptr;
195 }
196 if(!type->isEnum()) {
197 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
198 return nullptr;
199 }
200
201 EnumType *enumType = static_cast<EnumType *>(type);
202 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
203 if(v == nullptr) {
204 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
205 return nullptr;
206 }
207 return v;
208}
209
Yifan Hongae16eed2016-09-23 13:25:25 -0700210Type *AST::lookupType(const FQName &fqName) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700211 CHECK(fqName.isValid());
212
Andreas Huberda51b8e2016-07-28 16:00:57 -0700213 if (fqName.name().empty()) {
214 // Given a package and version???
215 return NULL;
216 }
217
Andreas Huber84f89de2016-07-28 15:39:51 -0700218 if (fqName.package().empty() && fqName.version().empty()) {
219 // This is just a plain identifier, resolve locally first if possible.
220
221 for (size_t i = mScopePath.size(); i-- > 0;) {
Yifan Hongae16eed2016-09-23 13:25:25 -0700222 Type *type = mScopePath[i]->lookupType(fqName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700223
224 if (type != NULL) {
Andreas Huber8d3ac0c2016-08-04 14:49:23 -0700225 // Resolve typeDefs to the target type.
226 while (type->isTypeDef()) {
227 type = static_cast<TypeDef *>(type)->referencedType();
228 }
229
Andreas Huberfd4afab2016-08-03 13:02:57 -0700230 return type->ref();
Andreas Huber84f89de2016-07-28 15:39:51 -0700231 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700232 }
233 }
234
Andreas Huber39fa7182016-08-19 14:27:33 -0700235 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700236 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700237 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700238
Andreas Huber39fa7182016-08-19 14:27:33 -0700239 for (const auto &importedAST : mImportedASTs) {
240 FQName matchingName;
241 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700242
Andreas Huber39fa7182016-08-19 14:27:33 -0700243 if (match != nullptr) {
244 if (resolvedType != nullptr) {
245 std::cerr << "ERROR: Unable to resolve type name '"
246 << fqName.string()
247 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700248
Andreas Huber39fa7182016-08-19 14:27:33 -0700249 std::cerr << " " << resolvedName.string() << "\n";
250 std::cerr << " " << matchingName.string() << "\n";
251
252 return NULL;
253 }
254
255 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700256 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700257 resolvedName = matchingName;
258
259 // Keep going even after finding a match.
260 }
261 }
262
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700263 if (resolvedType == nullptr
264 && fqName.package().empty()
265 && fqName.version().empty()
Hridya Valsarajub77944a2016-09-22 09:34:20 -0700266 && fqName.name() == "MQDescriptorSync") {
267 return new PredefinedType(
268 "::android::hardware::MQDescriptorSync");
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700269 }
270
Andreas Huber39fa7182016-08-19 14:27:33 -0700271 if (resolvedType) {
272#if 0
273 LOG(INFO) << "found '"
274 << resolvedName.string()
275 << "' after looking for '"
276 << fqName.string()
277 << "'.";
278#endif
279
280 // Resolve typeDefs to the target type.
281 while (resolvedType->isTypeDef()) {
282 resolvedType =
283 static_cast<TypeDef *>(resolvedType)->referencedType();
284 }
285
Iliyan Malchev800273d2016-09-02 15:25:07 -0700286 returnedType = resolvedType;
287
288 // If the resolved type is not an interface, we need to determine
289 // whether it is defined in types.hal, or in some other interface. In
290 // the latter case, we need to emit a dependency for the interface in
291 // which the type is defined.
292 //
293 // Consider the following:
294 // android.hardware.tests.foo@1.0::Record
295 // android.hardware.tests.foo@1.0::IFoo.Folder
296 // android.hardware.tests.foo@1.0::Folder
297 //
298 // If Record is an interface, then we keep track of it for the purpose
299 // of emitting dependencies in the target language (for example #include
300 // in C++). If Record is a UDT, then we assume it is defined in
301 // types.hal in android.hardware.tests.foo@1.0.
302 //
303 // In the case of IFoo.Folder, the same applies. If IFoo is an
304 // interface, we need to track this for the purpose of emitting
305 // dependencies. If not, then it must have been defined in types.hal.
306 //
307 // In the case of just specifying Folder, the resolved type is
308 // android.hardware.tests.foo@1.0::IFoo.Folder, and the same logic as
309 // above applies.
310
311 if (!resolvedType->isInterface()) {
312 FQName ifc(resolvedName.package(),
313 resolvedName.version(),
314 resolvedName.names().at(0));
315 for (const auto &importedAST : mImportedASTs) {
316 FQName matchingName;
317 Type *match = importedAST->findDefinedType(ifc, &matchingName);
318 if (match != nullptr && match->isInterface()) {
319 resolvedType = match;
320 }
321 }
322 }
323
Andreas Huber39fa7182016-08-19 14:27:33 -0700324 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700325 // Non-interface types are declared in the associated types header.
Andreas Huber39fa7182016-08-19 14:27:33 -0700326 FQName typesName(
327 resolvedName.package(), resolvedName.version(), "types");
328
Andreas Huber0e00de42016-08-03 09:56:02 -0700329 mImportedNames.insert(typesName);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700330
Steven Morelandd537ab02016-09-12 10:32:01 -0700331 if (resolvedType->isNamedType() && !resolvedType->isTypeDef()) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700332 mImportedNamesForJava.insert(
333 static_cast<NamedType *>(resolvedType)->fqName());
334 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700335 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700336 // Do _not_ use fqName, i.e. the name we used to look up the type,
337 // but instead use the name of the interface we found.
338 // This is necessary because if fqName pointed to a typedef which
339 // in turn referenced the found interface we'd mistakenly use the
340 // name of the typedef instead of the proper name of the interface.
341
342 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700343 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber85eabdb2016-08-25 11:24:49 -0700344
345 mImportedNamesForJava.insert(
346 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700347 }
Andreas Huber737080b2016-08-02 15:38:04 -0700348 }
349
Iliyan Malchev800273d2016-09-02 15:25:07 -0700350 return returnedType->ref();
Andreas Huber5345ec22016-07-29 13:33:27 -0700351}
352
Andreas Huber39fa7182016-08-19 14:27:33 -0700353Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700354 for (const auto &pair : mDefinedTypesByFullName) {
355 const FQName &key = pair.first;
356 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700357
Andreas Huber39fa7182016-08-19 14:27:33 -0700358 if (key.endsWith(fqName)) {
359 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700360 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700361 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700362 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700363
364 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700365}
366
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700367void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700368 for (const auto &fqName : mImportedNames) {
369 FQName packageName(fqName.package(), fqName.version(), "");
370
371 if (packageName == mPackage) {
372 // We only care about external imports, not our own package.
373 continue;
374 }
375
376 importSet->insert(packageName);
377 }
378}
379
Andreas Huber0fa9e392016-08-31 09:05:44 -0700380bool AST::isJavaCompatible() const {
381 std::string ifaceName;
382 if (!AST::isInterface(&ifaceName)) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700383 for (const auto *type : mRootScope->getSubTypes()) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700384 if (!type->isJavaCompatible()) {
385 return false;
386 }
387 }
388
389 return true;
390 }
391
392 const Interface *iface = mRootScope->getInterface();
393 return iface->isJavaCompatible();
394}
395
Andreas Huber019d21d2016-10-03 12:59:47 -0700396void AST::appendToExportedTypesVector(
397 std::vector<const Type *> *exportedTypes) const {
398 mRootScope->appendToExportedTypesVector(exportedTypes);
399}
400
Andreas Huberc9410c72016-07-28 12:18:40 -0700401} // namespace android;