blob: f52fc6522987cd5efe1700f80ac67afd3d22c6df [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huberc9410c72016-07-28 12:18:40 -070017#include "Scope.h"
18
Andreas Huberc9410c72016-07-28 12:18:40 -070019#include "Formatter.h"
Andreas Hubera2723d22016-07-29 15:36:07 -070020#include "Interface.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070021
Andreas Huber2831d512016-08-15 09:33:47 -070022#include <android-base/logging.h>
23
Andreas Huberc9410c72016-07-28 12:18:40 -070024namespace android {
25
Andreas Huber9ed827c2016-08-22 12:31:13 -070026Scope::Scope(const char *localName)
27 : NamedType(localName) {
28}
Andreas Huberc9410c72016-07-28 12:18:40 -070029
Andreas Huber39fa7182016-08-19 14:27:33 -070030bool Scope::addType(const char *localName, Type *type, std::string *errorMsg) {
Andreas Huber31629bc2016-08-03 09:06:40 -070031 if (mTypeIndexByName.indexOfKey(localName) >= 0) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070032 *errorMsg = "A type named '";
33 (*errorMsg) += localName;
34 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070035
Andreas Huberc9410c72016-07-28 12:18:40 -070036 return false;
37 }
38
39 size_t index = mTypes.size();
40 mTypes.push_back(type);
Andreas Huber31629bc2016-08-03 09:06:40 -070041 mTypeIndexByName.add(localName, index);
Andreas Huberc9410c72016-07-28 12:18:40 -070042
43 return true;
44}
45
Andreas Huberc9410c72016-07-28 12:18:40 -070046Type *Scope::lookupType(const char *name) const {
47 ssize_t index = mTypeIndexByName.indexOfKey(name);
48
49 if (index >= 0) {
50 return mTypes[mTypeIndexByName.valueAt(index)];
51 }
52
53 return NULL;
54}
55
Andreas Huber5345ec22016-07-29 13:33:27 -070056bool Scope::isScope() const {
57 return true;
58}
59
Andreas Huber881227d2016-08-02 14:20:21 -070060Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070061 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070062 return static_cast<Interface *>(mTypes[0]);
63 }
64
65 return NULL;
66}
67
68bool Scope::containsSingleInterface(std::string *ifaceName) const {
69 Interface *iface = getInterface();
70
71 if (iface != NULL) {
Andreas Huber0e00de42016-08-03 09:56:02 -070072 *ifaceName = iface->localName();
Andreas Hubera2723d22016-07-29 15:36:07 -070073 return true;
74 }
75
76 return false;
77}
78
Andreas Huberc7dfef32016-08-16 10:57:14 -070079std::string Scope::pickUniqueAnonymousName() const {
80 static size_t sNextID = 0;
81
82 for (;;) {
83 std::string anonName = "_hidl_Anon_" + std::to_string(sNextID++);
84
85 if (mTypeIndexByName.indexOfKey(anonName) < 0) {
86 return anonName;
87 }
88 }
89}
90
Andreas Huber2831d512016-08-15 09:33:47 -070091std::string Scope::getJavaType() const {
92 CHECK(!"Should not be here");
93 return std::string();
94}
95
Andreas Huber881227d2016-08-02 14:20:21 -070096status_t Scope::emitTypeDeclarations(Formatter &out) const {
97 for (size_t i = 0; i < mTypes.size(); ++i) {
98 status_t err = mTypes[i]->emitTypeDeclarations(out);
99
100 if (err != OK) {
101 return err;
102 }
103 }
104
105 return OK;
106}
107
Andreas Huber85eabdb2016-08-25 11:24:49 -0700108status_t Scope::emitJavaTypeDeclarations(
109 Formatter &out, bool atTopLevel) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700110 for (size_t i = 0; i < mTypes.size(); ++i) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700111 status_t err = mTypes[i]->emitJavaTypeDeclarations(out, atTopLevel);
Andreas Huber2831d512016-08-15 09:33:47 -0700112
113 if (err != OK) {
114 return err;
115 }
116 }
117
118 return OK;
119}
120
Andreas Huber881227d2016-08-02 14:20:21 -0700121status_t Scope::emitTypeDefinitions(
122 Formatter &out, const std::string prefix) const {
123 for (size_t i = 0; i < mTypes.size(); ++i) {
124 status_t err = mTypes[i]->emitTypeDefinitions(out, prefix);
125
126 if (err != OK) {
127 return err;
128 }
129 }
130
131 return OK;
132}
133
Andreas Huber70a59e12016-08-16 12:57:01 -0700134const std::vector<Type *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700135 return mTypes;
136}
137
138status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
139 for (size_t i = 0; i < mTypes.size(); ++i) {
140 status_t status = mTypes[i]->emitVtsTypeDeclarations(out);
141 if (status != OK) {
142 return status;
143 }
144 }
145 return OK;
146}
147
Andreas Huber70a59e12016-08-16 12:57:01 -0700148bool Scope::isJavaCompatible() const {
149 for (const auto &type : mTypes) {
150 if (!type->isJavaCompatible()) {
151 return false;
152 }
153 }
154
155 return true;
156}
157
Andreas Huber85eabdb2016-08-25 11:24:49 -0700158size_t Scope::countTypes() const {
159 return mTypeIndexByName.size();
160}
161
162const Type *Scope::typeAt(size_t index, std::string *name) const {
163 *name = mTypeIndexByName.keyAt(index);
164 return mTypes[mTypeIndexByName.valueAt(index)];
165}
166
Andreas Huberc9410c72016-07-28 12:18:40 -0700167} // namespace android
168