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