blob: e7a48c6d2be00837b93fb2296de91689e2856d42 [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 Hubera2723d22016-07-29 15:36:07 -070019#include "Interface.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070020
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070021#include <hidl-util/Formatter.h>
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
Steven Morelandd537ab02016-09-12 10:32:01 -070030bool Scope::addType(NamedType *type, std::string *errorMsg) {
31 const std::string &localName = type->localName();
32
33 auto it = mTypeIndexByName.find(localName);
34
35 if (it != mTypeIndexByName.end()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070036 *errorMsg = "A type named '";
37 (*errorMsg) += localName;
38 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070039
Andreas Huberc9410c72016-07-28 12:18:40 -070040 return false;
41 }
42
43 size_t index = mTypes.size();
44 mTypes.push_back(type);
Steven Morelandd537ab02016-09-12 10:32:01 -070045 mTypeIndexByName[localName] = index;
Andreas Huberc9410c72016-07-28 12:18:40 -070046
47 return true;
48}
49
Steven Morelandd537ab02016-09-12 10:32:01 -070050NamedType *Scope::lookupType(const char *name) const {
51 auto it = mTypeIndexByName.find(name);
Andreas Huberc9410c72016-07-28 12:18:40 -070052
Steven Morelandd537ab02016-09-12 10:32:01 -070053 if (it != mTypeIndexByName.end()) {
54 return mTypes[it->second];
Andreas Huberc9410c72016-07-28 12:18:40 -070055 }
56
57 return NULL;
58}
59
Andreas Huber5345ec22016-07-29 13:33:27 -070060bool Scope::isScope() const {
61 return true;
62}
63
Andreas Huber881227d2016-08-02 14:20:21 -070064Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070065 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070066 return static_cast<Interface *>(mTypes[0]);
67 }
68
69 return NULL;
70}
71
72bool Scope::containsSingleInterface(std::string *ifaceName) const {
73 Interface *iface = getInterface();
74
75 if (iface != NULL) {
Andreas Huber0e00de42016-08-03 09:56:02 -070076 *ifaceName = iface->localName();
Andreas Hubera2723d22016-07-29 15:36:07 -070077 return true;
78 }
79
80 return false;
81}
82
Andreas Huberc7dfef32016-08-16 10:57:14 -070083std::string Scope::pickUniqueAnonymousName() const {
84 static size_t sNextID = 0;
85
86 for (;;) {
87 std::string anonName = "_hidl_Anon_" + std::to_string(sNextID++);
88
Steven Morelandd537ab02016-09-12 10:32:01 -070089 auto it = mTypeIndexByName.find(anonName);
90
91 if (it == mTypeIndexByName.end()) {
Andreas Huberc7dfef32016-08-16 10:57:14 -070092 return anonName;
93 }
94 }
95}
96
Andreas Huber881227d2016-08-02 14:20:21 -070097status_t Scope::emitTypeDeclarations(Formatter &out) const {
98 for (size_t i = 0; i < mTypes.size(); ++i) {
99 status_t err = mTypes[i]->emitTypeDeclarations(out);
100
101 if (err != OK) {
102 return err;
103 }
104 }
105
106 return OK;
107}
108
Andreas Huber85eabdb2016-08-25 11:24:49 -0700109status_t Scope::emitJavaTypeDeclarations(
110 Formatter &out, bool atTopLevel) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700111 for (size_t i = 0; i < mTypes.size(); ++i) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700112 status_t err = mTypes[i]->emitJavaTypeDeclarations(out, atTopLevel);
Andreas Huber2831d512016-08-15 09:33:47 -0700113
114 if (err != OK) {
115 return err;
116 }
117 }
118
119 return OK;
120}
121
Andreas Huber881227d2016-08-02 14:20:21 -0700122status_t Scope::emitTypeDefinitions(
123 Formatter &out, const std::string prefix) const {
124 for (size_t i = 0; i < mTypes.size(); ++i) {
125 status_t err = mTypes[i]->emitTypeDefinitions(out, prefix);
126
127 if (err != OK) {
128 return err;
129 }
130 }
131
132 return OK;
133}
134
Steven Morelandd537ab02016-09-12 10:32:01 -0700135const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700136 return mTypes;
137}
138
139status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
140 for (size_t i = 0; i < mTypes.size(); ++i) {
141 status_t status = mTypes[i]->emitVtsTypeDeclarations(out);
142 if (status != OK) {
143 return status;
144 }
145 }
146 return OK;
147}
148
Andreas Huber70a59e12016-08-16 12:57:01 -0700149bool Scope::isJavaCompatible() const {
150 for (const auto &type : mTypes) {
151 if (!type->isJavaCompatible()) {
152 return false;
153 }
154 }
155
156 return true;
157}
158
Andreas Huberc9410c72016-07-28 12:18:40 -0700159} // namespace android
160