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