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