blob: 0e21c8eef419a1deda9681792b5f12cbeb251112 [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "Scope.h"
2
3#include "Constant.h"
4#include "Formatter.h"
Andreas Hubera2723d22016-07-29 15:36:07 -07005#include "Interface.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07006
Andreas Huber2831d512016-08-15 09:33:47 -07007#include <android-base/logging.h>
8
Andreas Huberc9410c72016-07-28 12:18:40 -07009namespace android {
10
Andreas Huber31629bc2016-08-03 09:06:40 -070011Scope::Scope() {}
Andreas Huberc9410c72016-07-28 12:18:40 -070012
Andreas Huber31629bc2016-08-03 09:06:40 -070013bool Scope::addType(const char *localName, NamedType *type) {
14 if (mTypeIndexByName.indexOfKey(localName) >= 0) {
Andreas Huberc7dfef32016-08-16 10:57:14 -070015 fprintf(stderr,
16 "A type named '%s' is already declared in the current scope.\n",
17 localName);
18
Andreas Huberc9410c72016-07-28 12:18:40 -070019 return false;
20 }
21
22 size_t index = mTypes.size();
23 mTypes.push_back(type);
Andreas Huber31629bc2016-08-03 09:06:40 -070024 mTypeIndexByName.add(localName, index);
Andreas Huberc9410c72016-07-28 12:18:40 -070025
26 return true;
27}
28
29bool 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
41Type *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 Huber5345ec22016-07-29 13:33:27 -070051bool Scope::isScope() const {
52 return true;
53}
54
Andreas Huber881227d2016-08-02 14:20:21 -070055Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070056 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070057 return static_cast<Interface *>(mTypes[0]);
58 }
59
60 return NULL;
61}
62
63bool Scope::containsSingleInterface(std::string *ifaceName) const {
64 Interface *iface = getInterface();
65
66 if (iface != NULL) {
Andreas Huber0e00de42016-08-03 09:56:02 -070067 *ifaceName = iface->localName();
Andreas Hubera2723d22016-07-29 15:36:07 -070068 return true;
69 }
70
71 return false;
72}
73
Andreas Huberc7dfef32016-08-16 10:57:14 -070074std::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 Huber2831d512016-08-15 09:33:47 -070086std::string Scope::getJavaType() const {
87 CHECK(!"Should not be here");
88 return std::string();
89}
90
Andreas Huber881227d2016-08-02 14:20:21 -070091status_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 Huber2831d512016-08-15 09:33:47 -0700103status_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 Huber881227d2016-08-02 14:20:21 -0700115status_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 Zhang5158db42016-08-10 10:25:20 -0700128
129Vector<Type *> Scope::getSubTypes() const {
130 return mTypes;
131}
132
133status_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 Huberc9410c72016-07-28 12:18:40 -0700143} // namespace android
144