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 | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 19 | #include "Formatter.h" |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 20 | #include "Interface.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 21 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 26 | Scope::Scope(const char *localName) |
| 27 | : NamedType(localName) { |
| 28 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 29 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 30 | bool Scope::addType(const char *localName, Type *type, std::string *errorMsg) { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 31 | if (mTypeIndexByName.indexOfKey(localName) >= 0) { |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 32 | *errorMsg = "A type named '"; |
| 33 | (*errorMsg) += localName; |
| 34 | (*errorMsg) += "' is already declared in the current scope."; |
Andreas Huber | c7dfef3 | 2016-08-16 10:57:14 -0700 | [diff] [blame] | 35 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 36 | return false; |
| 37 | } |
| 38 | |
| 39 | size_t index = mTypes.size(); |
| 40 | mTypes.push_back(type); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 41 | mTypeIndexByName.add(localName, index); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 46 | Type *Scope::lookupType(const char *name) const { |
| 47 | ssize_t index = mTypeIndexByName.indexOfKey(name); |
| 48 | |
| 49 | if (index >= 0) { |
| 50 | return mTypes[mTypeIndexByName.valueAt(index)]; |
| 51 | } |
| 52 | |
| 53 | return NULL; |
| 54 | } |
| 55 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 56 | bool Scope::isScope() const { |
| 57 | return true; |
| 58 | } |
| 59 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 60 | Interface *Scope::getInterface() const { |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 61 | if (mTypes.size() == 1 && mTypes[0]->isInterface()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 62 | return static_cast<Interface *>(mTypes[0]); |
| 63 | } |
| 64 | |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | bool Scope::containsSingleInterface(std::string *ifaceName) const { |
| 69 | Interface *iface = getInterface(); |
| 70 | |
| 71 | if (iface != NULL) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 72 | *ifaceName = iface->localName(); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
Andreas Huber | c7dfef3 | 2016-08-16 10:57:14 -0700 | [diff] [blame] | 79 | std::string Scope::pickUniqueAnonymousName() const { |
| 80 | static size_t sNextID = 0; |
| 81 | |
| 82 | for (;;) { |
| 83 | std::string anonName = "_hidl_Anon_" + std::to_string(sNextID++); |
| 84 | |
| 85 | if (mTypeIndexByName.indexOfKey(anonName) < 0) { |
| 86 | return anonName; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 91 | std::string Scope::getJavaType() const { |
| 92 | CHECK(!"Should not be here"); |
| 93 | return std::string(); |
| 94 | } |
| 95 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 96 | status_t Scope::emitTypeDeclarations(Formatter &out) const { |
| 97 | for (size_t i = 0; i < mTypes.size(); ++i) { |
| 98 | status_t err = mTypes[i]->emitTypeDeclarations(out); |
| 99 | |
| 100 | if (err != OK) { |
| 101 | return err; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return OK; |
| 106 | } |
| 107 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 108 | status_t Scope::emitJavaTypeDeclarations( |
| 109 | Formatter &out, bool atTopLevel) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 110 | for (size_t i = 0; i < mTypes.size(); ++i) { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 111 | status_t err = mTypes[i]->emitJavaTypeDeclarations(out, atTopLevel); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 112 | |
| 113 | if (err != OK) { |
| 114 | return err; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return OK; |
| 119 | } |
| 120 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 121 | status_t Scope::emitTypeDefinitions( |
| 122 | Formatter &out, const std::string prefix) const { |
| 123 | for (size_t i = 0; i < mTypes.size(); ++i) { |
| 124 | status_t err = mTypes[i]->emitTypeDefinitions(out, prefix); |
| 125 | |
| 126 | if (err != OK) { |
| 127 | return err; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return OK; |
| 132 | } |
| 133 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 134 | const std::vector<Type *> &Scope::getSubTypes() const { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 135 | return mTypes; |
| 136 | } |
| 137 | |
| 138 | status_t Scope::emitVtsTypeDeclarations(Formatter &out) const { |
| 139 | for (size_t i = 0; i < mTypes.size(); ++i) { |
| 140 | status_t status = mTypes[i]->emitVtsTypeDeclarations(out); |
| 141 | if (status != OK) { |
| 142 | return status; |
| 143 | } |
| 144 | } |
| 145 | return OK; |
| 146 | } |
| 147 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 148 | bool Scope::isJavaCompatible() const { |
| 149 | for (const auto &type : mTypes) { |
| 150 | if (!type->isJavaCompatible()) { |
| 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 158 | size_t Scope::countTypes() const { |
| 159 | return mTypeIndexByName.size(); |
| 160 | } |
| 161 | |
| 162 | const Type *Scope::typeAt(size_t index, std::string *name) const { |
| 163 | *name = mTypeIndexByName.keyAt(index); |
| 164 | return mTypes[mTypeIndexByName.valueAt(index)]; |
| 165 | } |
| 166 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 167 | } // namespace android |
| 168 | |