blob: 8fb5ef0556d9878fd70f55cbb884cd6653f46aa3 [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 Huberc9410c72016-07-28 12:18:40 -070020#include "Formatter.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
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
247 if (resolvedType) {
248#if 0
249 LOG(INFO) << "found '"
250 << resolvedName.string()
251 << "' after looking for '"
252 << fqName.string()
253 << "'.";
254#endif
255
256 // Resolve typeDefs to the target type.
257 while (resolvedType->isTypeDef()) {
258 resolvedType =
259 static_cast<TypeDef *>(resolvedType)->referencedType();
260 }
261
Iliyan Malchev800273d2016-09-02 15:25:07 -0700262 returnedType = resolvedType;
263
264 // If the resolved type is not an interface, we need to determine
265 // whether it is defined in types.hal, or in some other interface. In
266 // the latter case, we need to emit a dependency for the interface in
267 // which the type is defined.
268 //
269 // Consider the following:
270 // android.hardware.tests.foo@1.0::Record
271 // android.hardware.tests.foo@1.0::IFoo.Folder
272 // android.hardware.tests.foo@1.0::Folder
273 //
274 // If Record is an interface, then we keep track of it for the purpose
275 // of emitting dependencies in the target language (for example #include
276 // in C++). If Record is a UDT, then we assume it is defined in
277 // types.hal in android.hardware.tests.foo@1.0.
278 //
279 // In the case of IFoo.Folder, the same applies. If IFoo is an
280 // interface, we need to track this for the purpose of emitting
281 // dependencies. If not, then it must have been defined in types.hal.
282 //
283 // In the case of just specifying Folder, the resolved type is
284 // android.hardware.tests.foo@1.0::IFoo.Folder, and the same logic as
285 // above applies.
286
287 if (!resolvedType->isInterface()) {
288 FQName ifc(resolvedName.package(),
289 resolvedName.version(),
290 resolvedName.names().at(0));
291 for (const auto &importedAST : mImportedASTs) {
292 FQName matchingName;
293 Type *match = importedAST->findDefinedType(ifc, &matchingName);
294 if (match != nullptr && match->isInterface()) {
295 resolvedType = match;
296 }
297 }
298 }
299
Andreas Huber39fa7182016-08-19 14:27:33 -0700300 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700301 // Non-interface types are declared in the associated types header.
Andreas Huber39fa7182016-08-19 14:27:33 -0700302 FQName typesName(
303 resolvedName.package(), resolvedName.version(), "types");
304
Andreas Huber0e00de42016-08-03 09:56:02 -0700305 mImportedNames.insert(typesName);
Andreas Huber85eabdb2016-08-25 11:24:49 -0700306
307 if (resolvedType->isNamedType()) {
308 mImportedNamesForJava.insert(
309 static_cast<NamedType *>(resolvedType)->fqName());
310 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700311 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700312 // Do _not_ use fqName, i.e. the name we used to look up the type,
313 // but instead use the name of the interface we found.
314 // This is necessary because if fqName pointed to a typedef which
315 // in turn referenced the found interface we'd mistakenly use the
316 // name of the typedef instead of the proper name of the interface.
317
318 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700319 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber85eabdb2016-08-25 11:24:49 -0700320
321 mImportedNamesForJava.insert(
322 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700323 }
Andreas Huber737080b2016-08-02 15:38:04 -0700324 }
325
Iliyan Malchev800273d2016-09-02 15:25:07 -0700326 return returnedType->ref();
Andreas Huber5345ec22016-07-29 13:33:27 -0700327}
328
Andreas Huber39fa7182016-08-19 14:27:33 -0700329Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
330 for (size_t i = 0; i < mDefinedTypesByFullName.size(); ++i) {
331 const FQName &key = mDefinedTypesByFullName.keyAt(i);
Andreas Huber5345ec22016-07-29 13:33:27 -0700332
Andreas Huber39fa7182016-08-19 14:27:33 -0700333 if (key.endsWith(fqName)) {
334 *matchingName = key;
335 return mDefinedTypesByFullName.valueAt(i);
Andreas Huber5345ec22016-07-29 13:33:27 -0700336 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700337 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700338
339 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700340}
341
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700342void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700343 for (const auto &fqName : mImportedNames) {
344 FQName packageName(fqName.package(), fqName.version(), "");
345
346 if (packageName == mPackage) {
347 // We only care about external imports, not our own package.
348 continue;
349 }
350
351 importSet->insert(packageName);
352 }
353}
354
Andreas Huber0fa9e392016-08-31 09:05:44 -0700355bool AST::isJavaCompatible() const {
356 std::string ifaceName;
357 if (!AST::isInterface(&ifaceName)) {
358 for (size_t i = 0; i < mRootScope->countTypes(); ++i) {
359 std::string typeName;
360 const Type *type = mRootScope->typeAt(i, &typeName);
361
362 if (!type->isJavaCompatible()) {
363 return false;
364 }
365 }
366
367 return true;
368 }
369
370 const Interface *iface = mRootScope->getInterface();
371 return iface->isJavaCompatible();
372}
373
Andreas Huberc9410c72016-07-28 12:18:40 -0700374} // namespace android;