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 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 21 | #include <hidl-util/Formatter.h> |
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 | } |
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 { |
| 52 | auto it = mTypeIndexByName.find(fqName.string().c_str()); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 53 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 54 | if (it != mTypeIndexByName.end()) { |
| 55 | return mTypes[it->second]; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return NULL; |
| 59 | } |
| 60 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 61 | LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const { |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 65 | bool Scope::isScope() const { |
| 66 | return true; |
| 67 | } |
| 68 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 69 | Interface *Scope::getInterface() const { |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 70 | if (mTypes.size() == 1 && mTypes[0]->isInterface()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 71 | return static_cast<Interface *>(mTypes[0]); |
| 72 | } |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | bool Scope::containsSingleInterface(std::string *ifaceName) const { |
| 78 | Interface *iface = getInterface(); |
| 79 | |
| 80 | if (iface != NULL) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 81 | *ifaceName = iface->localName(); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
Andreas Huber | c7dfef3 | 2016-08-16 10:57:14 -0700 | [diff] [blame] | 88 | std::string Scope::pickUniqueAnonymousName() const { |
| 89 | static size_t sNextID = 0; |
| 90 | |
| 91 | for (;;) { |
| 92 | std::string anonName = "_hidl_Anon_" + std::to_string(sNextID++); |
| 93 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 94 | auto it = mTypeIndexByName.find(anonName); |
| 95 | |
| 96 | if (it == mTypeIndexByName.end()) { |
Andreas Huber | c7dfef3 | 2016-08-16 10:57:14 -0700 | [diff] [blame] | 97 | return anonName; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 102 | status_t Scope::emitTypeDeclarations(Formatter &out) const { |
| 103 | for (size_t i = 0; i < mTypes.size(); ++i) { |
| 104 | status_t err = mTypes[i]->emitTypeDeclarations(out); |
| 105 | |
| 106 | if (err != OK) { |
| 107 | return err; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return OK; |
| 112 | } |
| 113 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 114 | status_t Scope::emitJavaTypeDeclarations( |
| 115 | Formatter &out, bool atTopLevel) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 116 | for (size_t i = 0; i < mTypes.size(); ++i) { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 117 | status_t err = mTypes[i]->emitJavaTypeDeclarations(out, atTopLevel); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 118 | |
| 119 | if (err != OK) { |
| 120 | return err; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return OK; |
| 125 | } |
| 126 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 127 | status_t Scope::emitTypeDefinitions( |
| 128 | Formatter &out, const std::string prefix) const { |
| 129 | for (size_t i = 0; i < mTypes.size(); ++i) { |
| 130 | status_t err = mTypes[i]->emitTypeDefinitions(out, prefix); |
| 131 | |
| 132 | if (err != OK) { |
| 133 | return err; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return OK; |
| 138 | } |
| 139 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 140 | const std::vector<NamedType *> &Scope::getSubTypes() const { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 141 | return mTypes; |
| 142 | } |
| 143 | |
| 144 | status_t Scope::emitVtsTypeDeclarations(Formatter &out) const { |
| 145 | for (size_t i = 0; i < mTypes.size(); ++i) { |
| 146 | status_t status = mTypes[i]->emitVtsTypeDeclarations(out); |
| 147 | if (status != OK) { |
| 148 | return status; |
| 149 | } |
| 150 | } |
| 151 | return OK; |
| 152 | } |
| 153 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 154 | bool Scope::isJavaCompatible() const { |
| 155 | for (const auto &type : mTypes) { |
| 156 | if (!type->isJavaCompatible()) { |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 164 | LocalIdentifier::LocalIdentifier(){} |
| 165 | LocalIdentifier::~LocalIdentifier(){} |
| 166 | |
| 167 | bool LocalIdentifier::isEnumValue() const { |
| 168 | return false; |
| 169 | } |
| 170 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 171 | } // namespace android |
| 172 | |