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> |
| 25 | #include <vector> |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 26 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 29 | Scope::Scope(const char* localName, const Location& location, Scope* parent) |
| 30 | : NamedType(localName, location, parent) {} |
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 | |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 98 | bool Scope::containsInterfaces() const { |
| 99 | for (const NamedType *type : mTypes) { |
| 100 | if (type->isInterface()) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return false; |
| 106 | } |
| 107 | |
Timur Iskhakov | e9ccfa2 | 2017-08-14 15:07:03 -0700 | [diff] [blame] | 108 | const std::vector<Annotation*>& Scope::annotations() const { |
| 109 | return mAnnotations; |
| 110 | } |
| 111 | |
| 112 | void Scope::setAnnotations(std::vector<Annotation*>* annotations) { |
| 113 | CHECK(mAnnotations.empty()); |
| 114 | CHECK(annotations != nullptr); |
| 115 | mAnnotations = *annotations; |
| 116 | } |
| 117 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame^] | 118 | std::vector<const Type*> Scope::getDefinedTypes() const { |
| 119 | std::vector<const Type*> ret; |
| 120 | ret.insert(ret.end(), mTypes.begin(), mTypes.end()); |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 121 | return ret; |
| 122 | } |
| 123 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame^] | 124 | std::vector<const ConstantExpression*> Scope::getConstantExpressions() const { |
| 125 | std::vector<const ConstantExpression*> ret; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 126 | for (const auto* annotation : mAnnotations) { |
| 127 | const auto& retAnnotation = annotation->getConstantExpressions(); |
| 128 | ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end()); |
| 129 | } |
| 130 | return ret; |
| 131 | } |
| 132 | |
Chih-Hung Hsieh | 8c90cc5 | 2017-08-03 14:51:13 -0700 | [diff] [blame] | 133 | status_t Scope::forEachType(const std::function<status_t(Type *)> &func) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 134 | for (size_t i = 0; i < mTypes.size(); ++i) { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 135 | status_t err = func(mTypes[i]); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 136 | |
| 137 | if (err != OK) { |
| 138 | return err; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return OK; |
| 143 | } |
| 144 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 145 | status_t Scope::emitTypeDeclarations(Formatter &out) const { |
| 146 | return forEachType([&](Type *type) { |
| 147 | return type->emitTypeDeclarations(out); |
| 148 | }); |
| 149 | } |
| 150 | |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 151 | status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 152 | return forEachType([&](Type *type) { |
| 153 | return type->emitGlobalTypeDeclarations(out); |
| 154 | }); |
| 155 | } |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 156 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 157 | status_t Scope::emitGlobalHwDeclarations(Formatter &out) const { |
| 158 | return forEachType([&](Type *type) { |
| 159 | return type->emitGlobalHwDeclarations(out); |
| 160 | }); |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 163 | status_t Scope::emitJavaTypeDeclarations( |
| 164 | Formatter &out, bool atTopLevel) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 165 | return forEachType([&](Type *type) { |
| 166 | return type->emitJavaTypeDeclarations(out, atTopLevel); |
| 167 | }); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Chih-Hung Hsieh | 8c90cc5 | 2017-08-03 14:51:13 -0700 | [diff] [blame] | 170 | status_t Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 171 | return forEachType([&](Type *type) { |
| 172 | return type->emitTypeDefinitions(out, prefix); |
| 173 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 176 | const std::vector<NamedType *> &Scope::getSubTypes() const { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 177 | return mTypes; |
| 178 | } |
| 179 | |
| 180 | status_t Scope::emitVtsTypeDeclarations(Formatter &out) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 181 | return forEachType([&](Type *type) { |
| 182 | return type->emitVtsTypeDeclarations(out); |
| 183 | }); |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 186 | bool Scope::isJavaCompatible() const { |
| 187 | for (const auto &type : mTypes) { |
| 188 | if (!type->isJavaCompatible()) { |
| 189 | return false; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
Andreas Huber | 60d3b22 | 2017-03-30 09:10:56 -0700 | [diff] [blame] | 196 | bool Scope::containsPointer() const { |
| 197 | for (const auto &type : mTypes) { |
| 198 | if (type->containsPointer()) { |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return false; |
| 204 | } |
| 205 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 206 | void Scope::appendToExportedTypesVector( |
| 207 | std::vector<const Type *> *exportedTypes) const { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 208 | forEachType([&](Type *type) { |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 209 | type->appendToExportedTypesVector(exportedTypes); |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 210 | return OK; |
| 211 | }); |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 214 | //////////////////////////////////////// |
| 215 | |
Steven Moreland | 0ecc7b8 | 2017-07-19 12:59:23 -0700 | [diff] [blame] | 216 | RootScope::RootScope(const char* localName, const Location& location, Scope* parent) |
| 217 | : Scope(localName, location, parent) {} |
| 218 | RootScope::~RootScope() {} |
| 219 | |
| 220 | std::string RootScope::typeName() const { |
| 221 | return "(root scope)"; |
| 222 | } |
| 223 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 224 | status_t RootScope::validate() const { |
| 225 | CHECK(annotations().empty()); |
| 226 | return Scope::validate(); |
| 227 | } |
| 228 | |
| 229 | //////////////////////////////////////// |
| 230 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 231 | LocalIdentifier::LocalIdentifier(){} |
| 232 | LocalIdentifier::~LocalIdentifier(){} |
| 233 | |
| 234 | bool LocalIdentifier::isEnumValue() const { |
| 235 | return false; |
| 236 | } |
| 237 | |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 238 | ConstantExpression* LocalIdentifier::constExpr() const { |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 242 | } // namespace android |
| 243 | |