blob: c72c72a65b1faea7a1b4930d1964a68171045655 [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 Huber9ed827c2016-08-22 12:31:13 -070010Scope::Scope(const char *localName)
11 : NamedType(localName) {
12}
Andreas Huberc9410c72016-07-28 12:18:40 -070013
Andreas Huber39fa7182016-08-19 14:27:33 -070014bool Scope::addType(const char *localName, Type *type, std::string *errorMsg) {
Andreas Huber31629bc2016-08-03 09:06:40 -070015 if (mTypeIndexByName.indexOfKey(localName) >= 0) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070016 *errorMsg = "A type named '";
17 (*errorMsg) += localName;
18 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070019
Andreas Huberc9410c72016-07-28 12:18:40 -070020 return false;
21 }
22
23 size_t index = mTypes.size();
24 mTypes.push_back(type);
Andreas Huber31629bc2016-08-03 09:06:40 -070025 mTypeIndexByName.add(localName, index);
Andreas Huberc9410c72016-07-28 12:18:40 -070026
27 return true;
28}
29
Andreas Huberc9410c72016-07-28 12:18:40 -070030Type *Scope::lookupType(const char *name) const {
31 ssize_t index = mTypeIndexByName.indexOfKey(name);
32
33 if (index >= 0) {
34 return mTypes[mTypeIndexByName.valueAt(index)];
35 }
36
37 return NULL;
38}
39
Andreas Huber5345ec22016-07-29 13:33:27 -070040bool Scope::isScope() const {
41 return true;
42}
43
Andreas Huber881227d2016-08-02 14:20:21 -070044Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070045 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070046 return static_cast<Interface *>(mTypes[0]);
47 }
48
49 return NULL;
50}
51
52bool Scope::containsSingleInterface(std::string *ifaceName) const {
53 Interface *iface = getInterface();
54
55 if (iface != NULL) {
Andreas Huber0e00de42016-08-03 09:56:02 -070056 *ifaceName = iface->localName();
Andreas Hubera2723d22016-07-29 15:36:07 -070057 return true;
58 }
59
60 return false;
61}
62
Andreas Huberc7dfef32016-08-16 10:57:14 -070063std::string Scope::pickUniqueAnonymousName() const {
64 static size_t sNextID = 0;
65
66 for (;;) {
67 std::string anonName = "_hidl_Anon_" + std::to_string(sNextID++);
68
69 if (mTypeIndexByName.indexOfKey(anonName) < 0) {
70 return anonName;
71 }
72 }
73}
74
Andreas Huber2831d512016-08-15 09:33:47 -070075std::string Scope::getJavaType() const {
76 CHECK(!"Should not be here");
77 return std::string();
78}
79
Andreas Huber881227d2016-08-02 14:20:21 -070080status_t Scope::emitTypeDeclarations(Formatter &out) const {
81 for (size_t i = 0; i < mTypes.size(); ++i) {
82 status_t err = mTypes[i]->emitTypeDeclarations(out);
83
84 if (err != OK) {
85 return err;
86 }
87 }
88
89 return OK;
90}
91
Andreas Huber2831d512016-08-15 09:33:47 -070092status_t Scope::emitJavaTypeDeclarations(Formatter &out) const {
93 for (size_t i = 0; i < mTypes.size(); ++i) {
94 status_t err = mTypes[i]->emitJavaTypeDeclarations(out);
95
96 if (err != OK) {
97 return err;
98 }
99 }
100
101 return OK;
102}
103
Andreas Huber881227d2016-08-02 14:20:21 -0700104status_t Scope::emitTypeDefinitions(
105 Formatter &out, const std::string prefix) const {
106 for (size_t i = 0; i < mTypes.size(); ++i) {
107 status_t err = mTypes[i]->emitTypeDefinitions(out, prefix);
108
109 if (err != OK) {
110 return err;
111 }
112 }
113
114 return OK;
115}
116
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