blob: c4cc58714466ac3c0586cd4d05287f51c4578701 [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}
Yifan Hongf24fa852016-09-23 11:03:15 -070029Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070030
Steven Morelandd537ab02016-09-12 10:32:01 -070031bool Scope::addType(NamedType *type, std::string *errorMsg) {
32 const std::string &localName = type->localName();
33
34 auto it = mTypeIndexByName.find(localName);
35
36 if (it != mTypeIndexByName.end()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070037 *errorMsg = "A type named '";
38 (*errorMsg) += localName;
39 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070040
Andreas Huberc9410c72016-07-28 12:18:40 -070041 return false;
42 }
43
44 size_t index = mTypes.size();
45 mTypes.push_back(type);
Steven Morelandd537ab02016-09-12 10:32:01 -070046 mTypeIndexByName[localName] = index;
Andreas Huberc9410c72016-07-28 12:18:40 -070047
48 return true;
49}
50
Yifan Hongae16eed2016-09-23 13:25:25 -070051NamedType *Scope::lookupType(const FQName &fqName) const {
52 auto it = mTypeIndexByName.find(fqName.string().c_str());
Andreas Huberc9410c72016-07-28 12:18:40 -070053
Steven Morelandd537ab02016-09-12 10:32:01 -070054 if (it != mTypeIndexByName.end()) {
55 return mTypes[it->second];
Andreas Huberc9410c72016-07-28 12:18:40 -070056 }
57
58 return NULL;
59}
60
Yifan Hongf24fa852016-09-23 11:03:15 -070061LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
62 return NULL;
63}
64
Andreas Huber5345ec22016-07-29 13:33:27 -070065bool Scope::isScope() const {
66 return true;
67}
68
Andreas Huber881227d2016-08-02 14:20:21 -070069Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070070 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070071 return static_cast<Interface *>(mTypes[0]);
72 }
73
74 return NULL;
75}
76
77bool Scope::containsSingleInterface(std::string *ifaceName) const {
78 Interface *iface = getInterface();
79
80 if (iface != NULL) {
Andreas Huber0e00de42016-08-03 09:56:02 -070081 *ifaceName = iface->localName();
Andreas Hubera2723d22016-07-29 15:36:07 -070082 return true;
83 }
84
85 return false;
86}
87
Andreas Huberc7dfef32016-08-16 10:57:14 -070088std::string Scope::pickUniqueAnonymousName() const {
89 static size_t sNextID = 0;
90
91 for (;;) {
92 std::string anonName = "_hidl_Anon_" + std::to_string(sNextID++);
93
Steven Morelandd537ab02016-09-12 10:32:01 -070094 auto it = mTypeIndexByName.find(anonName);
95
96 if (it == mTypeIndexByName.end()) {
Andreas Huberc7dfef32016-08-16 10:57:14 -070097 return anonName;
98 }
99 }
100}
101
Andreas Huber881227d2016-08-02 14:20:21 -0700102status_t Scope::emitTypeDeclarations(Formatter &out) const {
103 for (size_t i = 0; i < mTypes.size(); ++i) {
104 status_t err = mTypes[i]->emitTypeDeclarations(out);
105
106 if (err != OK) {
107 return err;
108 }
109 }
110
111 return OK;
112}
113
Andreas Huber85eabdb2016-08-25 11:24:49 -0700114status_t Scope::emitJavaTypeDeclarations(
115 Formatter &out, bool atTopLevel) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700116 for (size_t i = 0; i < mTypes.size(); ++i) {
Andreas Huber85eabdb2016-08-25 11:24:49 -0700117 status_t err = mTypes[i]->emitJavaTypeDeclarations(out, atTopLevel);
Andreas Huber2831d512016-08-15 09:33:47 -0700118
119 if (err != OK) {
120 return err;
121 }
122 }
123
124 return OK;
125}
126
Andreas Huber881227d2016-08-02 14:20:21 -0700127status_t Scope::emitTypeDefinitions(
128 Formatter &out, const std::string prefix) const {
129 for (size_t i = 0; i < mTypes.size(); ++i) {
130 status_t err = mTypes[i]->emitTypeDefinitions(out, prefix);
131
132 if (err != OK) {
133 return err;
134 }
135 }
136
137 return OK;
138}
139
Steven Morelandd537ab02016-09-12 10:32:01 -0700140const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700141 return mTypes;
142}
143
144status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
145 for (size_t i = 0; i < mTypes.size(); ++i) {
146 status_t status = mTypes[i]->emitVtsTypeDeclarations(out);
147 if (status != OK) {
148 return status;
149 }
150 }
151 return OK;
152}
153
Andreas Huber70a59e12016-08-16 12:57:01 -0700154bool Scope::isJavaCompatible() const {
155 for (const auto &type : mTypes) {
156 if (!type->isJavaCompatible()) {
157 return false;
158 }
159 }
160
161 return true;
162}
163
Yifan Hongf24fa852016-09-23 11:03:15 -0700164LocalIdentifier::LocalIdentifier(){}
165LocalIdentifier::~LocalIdentifier(){}
166
167bool LocalIdentifier::isEnumValue() const {
168 return false;
169}
170
Andreas Huberc9410c72016-07-28 12:18:40 -0700171} // namespace android
172