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