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 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 19 | #include "Interface.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 20 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 22 | #include <hidl-util/Formatter.h> |
| 23 | #include <vector> |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 24 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 27 | Scope::Scope(const char* localName, const Location& location, Scope* parent) |
| 28 | : NamedType(localName, location, parent) {} |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 29 | Scope::~Scope(){} |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 31 | bool Scope::addType(NamedType *type, std::string *errorMsg) { |
| 32 | const std::string &localName = type->localName(); |
| 33 | |
| 34 | auto it = mTypeIndexByName.find(localName); |
| 35 | |
| 36 | if (it != mTypeIndexByName.end()) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 37 | *errorMsg = "A type named '"; |
| 38 | (*errorMsg) += localName; |
| 39 | (*errorMsg) += "' is already declared in the current scope."; |
Andreas Huber | c7dfef3 | 2016-08-16 10:57:14 -0700 | [diff] [blame] | 40 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | |
| 44 | size_t index = mTypes.size(); |
| 45 | mTypes.push_back(type); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 46 | mTypeIndexByName[localName] = index; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 51 | NamedType *Scope::lookupType(const FQName &fqName) const { |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 52 | CHECK(fqName.package().empty() && fqName.version().empty()); |
| 53 | if (!fqName.valueName().empty()) { |
| 54 | LOG(WARNING) << fqName.string() << " does not refer to a type."; |
| 55 | return nullptr; |
| 56 | } |
| 57 | std::vector<std::string> names = fqName.names(); |
| 58 | CHECK_GT(names.size(), 0u); |
| 59 | auto it = mTypeIndexByName.find(names[0]); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 60 | |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 61 | if (it == mTypeIndexByName.end()) { |
| 62 | return nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Yifan Hong | 327cfe1 | 2016-10-03 10:29:42 -0700 | [diff] [blame] | 65 | NamedType *outerType = mTypes[it->second]; |
| 66 | if (names.size() == 1) { |
| 67 | return outerType; |
| 68 | } |
| 69 | if (!outerType->isScope()) { |
| 70 | // more than one names, but the first name is not a scope |
| 71 | return nullptr; |
| 72 | } |
| 73 | Scope *outerScope = static_cast<Scope *>(outerType); |
| 74 | // *slowly* pop first element |
| 75 | names.erase(names.begin()); |
| 76 | FQName innerName(names); |
| 77 | return outerScope->lookupType(innerName); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 80 | LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const { |
| 81 | return NULL; |
| 82 | } |
| 83 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 84 | bool Scope::isScope() const { |
| 85 | return true; |
| 86 | } |
| 87 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 88 | Interface *Scope::getInterface() const { |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 89 | if (mTypes.size() == 1 && mTypes[0]->isInterface()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 90 | return static_cast<Interface *>(mTypes[0]); |
| 91 | } |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 96 | bool Scope::containsInterfaces() const { |
| 97 | for (const NamedType *type : mTypes) { |
| 98 | if (type->isInterface()) { |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return false; |
| 104 | } |
| 105 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 106 | status_t Scope::forEachType(std::function<status_t(Type *)> func) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 107 | for (size_t i = 0; i < mTypes.size(); ++i) { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 108 | status_t err = func(mTypes[i]); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 109 | |
| 110 | if (err != OK) { |
| 111 | return err; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return OK; |
| 116 | } |
| 117 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 118 | status_t Scope::emitTypeDeclarations(Formatter &out) const { |
| 119 | return forEachType([&](Type *type) { |
| 120 | return type->emitTypeDeclarations(out); |
| 121 | }); |
| 122 | } |
| 123 | |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 124 | status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 125 | return forEachType([&](Type *type) { |
| 126 | return type->emitGlobalTypeDeclarations(out); |
| 127 | }); |
| 128 | } |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 129 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 130 | status_t Scope::emitGlobalHwDeclarations(Formatter &out) const { |
| 131 | return forEachType([&](Type *type) { |
| 132 | return type->emitGlobalHwDeclarations(out); |
| 133 | }); |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 136 | status_t Scope::emitJavaTypeDeclarations( |
| 137 | Formatter &out, bool atTopLevel) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 138 | return forEachType([&](Type *type) { |
| 139 | return type->emitJavaTypeDeclarations(out, atTopLevel); |
| 140 | }); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 143 | status_t Scope::emitTypeDefinitions( |
| 144 | Formatter &out, const std::string prefix) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 145 | return forEachType([&](Type *type) { |
| 146 | return type->emitTypeDefinitions(out, prefix); |
| 147 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 150 | const std::vector<NamedType *> &Scope::getSubTypes() const { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 151 | return mTypes; |
| 152 | } |
| 153 | |
| 154 | status_t Scope::emitVtsTypeDeclarations(Formatter &out) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 155 | return forEachType([&](Type *type) { |
| 156 | return type->emitVtsTypeDeclarations(out); |
| 157 | }); |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 160 | bool Scope::isJavaCompatible() const { |
| 161 | for (const auto &type : mTypes) { |
| 162 | if (!type->isJavaCompatible()) { |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
Andreas Huber | 60d3b22 | 2017-03-30 09:10:56 -0700 | [diff] [blame] | 170 | bool Scope::containsPointer() const { |
| 171 | for (const auto &type : mTypes) { |
| 172 | if (type->containsPointer()) { |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return false; |
| 178 | } |
| 179 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 180 | void Scope::appendToExportedTypesVector( |
| 181 | std::vector<const Type *> *exportedTypes) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 182 | forEachType([&](Type *type) { |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 183 | type->appendToExportedTypesVector(exportedTypes); |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 184 | return OK; |
| 185 | }); |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Steven Moreland | 0ecc7b8 | 2017-07-19 12:59:23 -0700 | [diff] [blame] | 188 | RootScope::RootScope(const char* localName, const Location& location, Scope* parent) |
| 189 | : Scope(localName, location, parent) {} |
| 190 | RootScope::~RootScope() {} |
| 191 | |
| 192 | std::string RootScope::typeName() const { |
| 193 | return "(root scope)"; |
| 194 | } |
| 195 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 196 | LocalIdentifier::LocalIdentifier(){} |
| 197 | LocalIdentifier::~LocalIdentifier(){} |
| 198 | |
| 199 | bool LocalIdentifier::isEnumValue() const { |
| 200 | return false; |
| 201 | } |
| 202 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 203 | } // namespace android |
| 204 | |