blob: 8b6385a3fadaacf604246d18c3387f6afd752568 [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "Scope.h"
2
Andreas Huberc9410c72016-07-28 12:18:40 -07003#include "Formatter.h"
Andreas Hubera2723d22016-07-29 15:36:07 -07004#include "Interface.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07005
Andreas Huber2831d512016-08-15 09:33:47 -07006#include <android-base/logging.h>
7
Andreas Huberc9410c72016-07-28 12:18:40 -07008namespace android {
9
Andreas Huber31629bc2016-08-03 09:06:40 -070010Scope::Scope() {}
Andreas Huberc9410c72016-07-28 12:18:40 -070011
Andreas Huber0d0f9a22016-08-17 10:26:11 -070012bool Scope::addType(
13 const char *localName, NamedType *type, std::string *errorMsg) {
Andreas Huber31629bc2016-08-03 09:06:40 -070014 if (mTypeIndexByName.indexOfKey(localName) >= 0) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070015 *errorMsg = "A type named '";
16 (*errorMsg) += localName;
17 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070018
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
Andreas Huberc9410c72016-07-28 12:18:40 -070029Type *Scope::lookupType(const char *name) const {
30 ssize_t index = mTypeIndexByName.indexOfKey(name);
31
32 if (index >= 0) {
33 return mTypes[mTypeIndexByName.valueAt(index)];
34 }
35
36 return NULL;
37}
38
Andreas Huber5345ec22016-07-29 13:33:27 -070039bool Scope::isScope() const {
40 return true;
41}
42
Andreas Huber881227d2016-08-02 14:20:21 -070043Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070044 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070045 return static_cast<Interface *>(mTypes[0]);
46 }
47
48 return NULL;
49}
50
51bool Scope::containsSingleInterface(std::string *ifaceName) const {
52 Interface *iface = getInterface();
53
54 if (iface != NULL) {
Andreas Huber0e00de42016-08-03 09:56:02 -070055 *ifaceName = iface->localName();
Andreas Hubera2723d22016-07-29 15:36:07 -070056 return true;
57 }
58
59 return false;
60}
61
Andreas Huberc7dfef32016-08-16 10:57:14 -070062std::string Scope::pickUniqueAnonymousName() const {
63 static size_t sNextID = 0;
64
65 for (;;) {
66 std::string anonName = "_hidl_Anon_" + std::to_string(sNextID++);
67
68 if (mTypeIndexByName.indexOfKey(anonName) < 0) {
69 return anonName;
70 }
71 }
72}
73
Andreas Huber2831d512016-08-15 09:33:47 -070074std::string Scope::getJavaType() const {
75 CHECK(!"Should not be here");
76 return std::string();
77}
78
Andreas Huber881227d2016-08-02 14:20:21 -070079status_t Scope::emitTypeDeclarations(Formatter &out) const {
80 for (size_t i = 0; i < mTypes.size(); ++i) {
81 status_t err = mTypes[i]->emitTypeDeclarations(out);
82
83 if (err != OK) {
84 return err;
85 }
86 }
87
88 return OK;
89}
90
Andreas Huber2831d512016-08-15 09:33:47 -070091status_t Scope::emitJavaTypeDeclarations(Formatter &out) const {
92 for (size_t i = 0; i < mTypes.size(); ++i) {
93 status_t err = mTypes[i]->emitJavaTypeDeclarations(out);
94
95 if (err != OK) {
96 return err;
97 }
98 }
99
100 return OK;
101}
102
Andreas Huber881227d2016-08-02 14:20:21 -0700103status_t Scope::emitTypeDefinitions(
104 Formatter &out, const std::string prefix) const {
105 for (size_t i = 0; i < mTypes.size(); ++i) {
106 status_t err = mTypes[i]->emitTypeDefinitions(out, prefix);
107
108 if (err != OK) {
109 return err;
110 }
111 }
112
113 return OK;
114}
115
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700116
Andreas Huber70a59e12016-08-16 12:57:01 -0700117const std::vector<Type *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700118 return mTypes;
119}
120
121status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
122 for (size_t i = 0; i < mTypes.size(); ++i) {
123 status_t status = mTypes[i]->emitVtsTypeDeclarations(out);
124 if (status != OK) {
125 return status;
126 }
127 }
128 return OK;
129}
130
Andreas Huber70a59e12016-08-16 12:57:01 -0700131bool Scope::isJavaCompatible() const {
132 for (const auto &type : mTypes) {
133 if (!type->isJavaCompatible()) {
134 return false;
135 }
136 }
137
138 return true;
139}
140
Andreas Huberc9410c72016-07-28 12:18:40 -0700141} // namespace android
142