Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #include "Scope.h" |
| 18 | |
Timur Iskhakov | e9ccfa2 | 2017-08-14 15:07:03 -0700 | [diff] [blame] | 19 | #include "Annotation.h" |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 20 | #include "ConstantExpression.h" |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 21 | #include "Interface.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 22 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 24 | #include <hidl-util/Formatter.h> |
Steven Moreland | 169a2d2 | 2018-01-25 10:05:31 -0800 | [diff] [blame] | 25 | #include <hidl-util/StringHelper.h> |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 26 | #include <algorithm> |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 27 | #include <iostream> |
Neel Mehta | 69920a6 | 2019-07-22 16:22:13 -0700 | [diff] [blame^] | 28 | #include <string> |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 29 | #include <vector> |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 30 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Neel Mehta | 69920a6 | 2019-07-22 16:22:13 -0700 | [diff] [blame^] | 33 | Scope::Scope(const std::string& localName, const FQName& fullName, const Location& location, |
| 34 | Scope* parent) |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 35 | : NamedType(localName, fullName, location, parent) {} |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 36 | Scope::~Scope(){} |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 37 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 38 | void Scope::addType(NamedType* type) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 39 | size_t index = mTypes.size(); |
| 40 | mTypes.push_back(type); |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 41 | mTypeIndexByName[type->localName()] = index; |
| 42 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 43 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 44 | status_t Scope::validateUniqueNames() const { |
| 45 | for (const auto* type : mTypes) { |
| 46 | if (mTypes[mTypeIndexByName.at(type->localName())] != type) { |
| 47 | std::cerr << "ERROR: A type named '" << type->localName() |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 48 | << "' is already declared in the scope at " << type->location() << std::endl; |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 49 | return UNKNOWN_ERROR; |
| 50 | } |
| 51 | } |
| 52 | return OK; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 55 | NamedType *Scope::lookupType(const FQName &fqName) const { |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 56 | CHECK(fqName.package().empty() && fqName.version().empty()); |
| 57 | if (!fqName.valueName().empty()) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 58 | std::cerr << "ERROR: " << fqName.string() << " does not refer to a type." << std::endl; |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 59 | return nullptr; |
| 60 | } |
| 61 | std::vector<std::string> names = fqName.names(); |
| 62 | CHECK_GT(names.size(), 0u); |
| 63 | auto it = mTypeIndexByName.find(names[0]); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 64 | |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 65 | if (it == mTypeIndexByName.end()) { |
| 66 | return nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 69 | NamedType *outerType = mTypes[it->second]; |
| 70 | if (names.size() == 1) { |
| 71 | return outerType; |
| 72 | } |
| 73 | if (!outerType->isScope()) { |
| 74 | // more than one names, but the first name is not a scope |
| 75 | return nullptr; |
| 76 | } |
| 77 | Scope *outerScope = static_cast<Scope *>(outerType); |
| 78 | // *slowly* pop first element |
| 79 | names.erase(names.begin()); |
Steven Moreland | e1b157e | 2018-03-06 14:18:32 -0800 | [diff] [blame] | 80 | FQName innerName; |
| 81 | CHECK(FQName::parse(StringHelper::JoinStrings(names, "."), &innerName)); |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 82 | return outerScope->lookupType(innerName); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 85 | LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const { |
Yi Kong | d7f8ab3 | 2018-07-24 11:27:02 -0700 | [diff] [blame] | 86 | return nullptr; |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 89 | bool Scope::isScope() const { |
| 90 | return true; |
| 91 | } |
| 92 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 93 | Interface *Scope::getInterface() const { |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 94 | if (mTypes.size() == 1 && mTypes[0]->isInterface()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 95 | return static_cast<Interface *>(mTypes[0]); |
| 96 | } |
| 97 | |
Yi Kong | d7f8ab3 | 2018-07-24 11:27:02 -0700 | [diff] [blame] | 98 | return nullptr; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Steven Moreland | b47a262 | 2018-07-11 09:04:25 -0700 | [diff] [blame] | 101 | bool Scope::definesInterfaces() const { |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 102 | for (const NamedType *type : mTypes) { |
| 103 | if (type->isInterface()) { |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
Timur Iskhakov | e9ccfa2 | 2017-08-14 15:07:03 -0700 | [diff] [blame] | 111 | const std::vector<Annotation*>& Scope::annotations() const { |
| 112 | return mAnnotations; |
| 113 | } |
| 114 | |
| 115 | void Scope::setAnnotations(std::vector<Annotation*>* annotations) { |
| 116 | CHECK(mAnnotations.empty()); |
| 117 | CHECK(annotations != nullptr); |
| 118 | mAnnotations = *annotations; |
| 119 | } |
| 120 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 121 | std::vector<const Type*> Scope::getDefinedTypes() const { |
| 122 | std::vector<const Type*> ret; |
| 123 | ret.insert(ret.end(), mTypes.begin(), mTypes.end()); |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 124 | return ret; |
| 125 | } |
| 126 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 127 | std::vector<const NamedType*> Scope::getSortedDefinedTypes() const { |
| 128 | std::vector<const NamedType*> ret; |
| 129 | ret.insert(ret.end(), mTypes.begin(), mTypes.end()); |
| 130 | |
| 131 | std::sort(ret.begin(), ret.end(), [](const NamedType* lhs, const NamedType* rhs) -> bool { |
| 132 | return lhs->location() < rhs->location(); |
| 133 | }); |
| 134 | return ret; |
| 135 | } |
| 136 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 137 | std::vector<const ConstantExpression*> Scope::getConstantExpressions() const { |
| 138 | std::vector<const ConstantExpression*> ret; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 139 | for (const auto* annotation : mAnnotations) { |
| 140 | const auto& retAnnotation = annotation->getConstantExpressions(); |
| 141 | ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end()); |
| 142 | } |
| 143 | return ret; |
| 144 | } |
| 145 | |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 146 | void Scope::topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder) { |
| 147 | auto less = [&](const Type* lhs, const Type* rhs) { |
| 148 | return reversedOrder.at(lhs) < reversedOrder.at(rhs); |
| 149 | }; |
| 150 | |
| 151 | if (std::is_sorted(mTypes.begin(), mTypes.end(), less)) return; |
| 152 | |
| 153 | mTypeOrderChanged = true; |
| 154 | std::sort(mTypes.begin(), mTypes.end(), less); |
| 155 | |
| 156 | for (size_t i = 0; i != mTypes.size(); ++i) { |
| 157 | mTypeIndexByName.at(mTypes[i]->localName()) = i; |
| 158 | } |
| 159 | } |
| 160 | |
Neel Mehta | 3b414a8 | 2019-07-02 15:47:48 -0700 | [diff] [blame] | 161 | void Scope::emitHidlDefinition(Formatter& out) const { |
| 162 | const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes(); |
| 163 | out.join(definedTypes.begin(), definedTypes.end(), "\n", |
| 164 | [&](auto t) { t->emitHidlDefinition(out); }); |
| 165 | } |
| 166 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 167 | void Scope::emitTypeDeclarations(Formatter& out) const { |
| 168 | if (mTypes.empty()) return; |
Timur Iskhakov | 99072c3 | 2017-09-13 16:34:21 -0700 | [diff] [blame] | 169 | |
| 170 | out << "// Forward declaration for forward reference support:\n"; |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 171 | for (const Type* type : mTypes) { |
Timur Iskhakov | fd3f250 | 2017-09-05 16:25:02 -0700 | [diff] [blame] | 172 | type->emitTypeForwardDeclaration(out); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 173 | } |
Timur Iskhakov | 99072c3 | 2017-09-13 16:34:21 -0700 | [diff] [blame] | 174 | out << "\n"; |
Timur Iskhakov | fd3f250 | 2017-09-05 16:25:02 -0700 | [diff] [blame] | 175 | |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 176 | if (mTypeOrderChanged) { |
| 177 | out << "// Order of inner types was changed for forward reference support.\n\n"; |
| 178 | } |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 179 | |
| 180 | for (const Type* type : mTypes) { |
Steven Moreland | 073269e | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 181 | type->emitDocComment(out); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 182 | type->emitTypeDeclarations(out); |
| 183 | } |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Steven Moreland | 8e61c5a | 2017-11-17 15:55:28 -0800 | [diff] [blame] | 186 | void Scope::emitGlobalTypeDeclarations(Formatter& out) const { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 187 | for (const Type* type : mTypes) { |
Steven Moreland | 8e61c5a | 2017-11-17 15:55:28 -0800 | [diff] [blame] | 188 | type->emitGlobalTypeDeclarations(out); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 189 | } |
Steven Moreland | 8e61c5a | 2017-11-17 15:55:28 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 192 | void Scope::emitPackageTypeDeclarations(Formatter& out) const { |
| 193 | for (const Type* type : mTypes) { |
| 194 | type->emitPackageTypeDeclarations(out); |
| 195 | } |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 196 | } |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 197 | |
Steven Moreland | 09c6ebe | 2018-10-09 10:15:48 -0700 | [diff] [blame] | 198 | void Scope::emitPackageTypeHeaderDefinitions(Formatter& out) const { |
| 199 | for (const Type* type : mTypes) { |
| 200 | type->emitPackageTypeHeaderDefinitions(out); |
| 201 | } |
| 202 | } |
| 203 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 204 | void Scope::emitPackageHwDeclarations(Formatter& out) const { |
| 205 | for (const Type* type : mTypes) { |
| 206 | type->emitPackageHwDeclarations(out); |
| 207 | } |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 210 | void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const { |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 211 | if (mTypeOrderChanged) { |
| 212 | out << "// Order of inner types was changed for forward reference support.\n\n"; |
| 213 | } |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 214 | |
| 215 | for (const Type* type : mTypes) { |
Steven Moreland | 073269e | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 216 | type->emitDocComment(out); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 217 | type->emitJavaTypeDeclarations(out, atTopLevel); |
| 218 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 221 | void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const { |
| 222 | for (const Type* type : mTypes) { |
| 223 | type->emitTypeDefinitions(out, prefix); |
| 224 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 227 | const std::vector<NamedType *> &Scope::getSubTypes() const { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 228 | return mTypes; |
| 229 | } |
| 230 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 231 | void Scope::emitVtsTypeDeclarations(Formatter& out) const { |
| 232 | for (const Type* type : mTypes) { |
| 233 | type->emitVtsTypeDeclarations(out); |
| 234 | } |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 237 | bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 238 | for (const Type* type : mTypes) { |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 239 | if (!type->isJavaCompatible(visited)) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | } |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 243 | return Type::deepIsJavaCompatible(visited); |
Andreas Huber | 60d3b22 | 2017-03-30 09:10:56 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 246 | void Scope::appendToExportedTypesVector( |
| 247 | std::vector<const Type *> *exportedTypes) const { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 248 | for (const Type* type : mTypes) { |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 249 | type->appendToExportedTypesVector(exportedTypes); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 250 | } |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 253 | //////////////////////////////////////// |
| 254 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 255 | RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location, |
| 256 | Scope* parent) |
| 257 | : Scope(localName, fullName, location, parent) {} |
Steven Moreland | 0ecc7b8 | 2017-07-19 12:59:23 -0700 | [diff] [blame] | 258 | RootScope::~RootScope() {} |
| 259 | |
| 260 | std::string RootScope::typeName() const { |
| 261 | return "(root scope)"; |
| 262 | } |
| 263 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 264 | status_t RootScope::validate() const { |
| 265 | CHECK(annotations().empty()); |
| 266 | return Scope::validate(); |
| 267 | } |
| 268 | |
| 269 | //////////////////////////////////////// |
| 270 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 271 | LocalIdentifier::LocalIdentifier(){} |
| 272 | LocalIdentifier::~LocalIdentifier(){} |
| 273 | |
| 274 | bool LocalIdentifier::isEnumValue() const { |
| 275 | return false; |
| 276 | } |
| 277 | |
Timur Iskhakov | dbaed33 | 2017-08-31 16:33:41 -0700 | [diff] [blame] | 278 | const LocalIdentifier* LocalIdentifier::resolve() const { |
| 279 | return this; |
| 280 | } |
| 281 | |
| 282 | LocalIdentifier* LocalIdentifier::resolve() { |
| 283 | return this; |
| 284 | } |
| 285 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 286 | ConstantExpression* LocalIdentifier::constExpr() const { |
| 287 | return nullptr; |
| 288 | } |
| 289 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 290 | } // namespace android |
| 291 | |