blob: df8651ba9072baef623c59e979501c758f66208c [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
Andreas Huber2831d512016-08-15 09:33:47 -070021#include <android-base/logging.h>
Yifan Hong327cfe12016-10-03 10:29:42 -070022#include <hidl-util/Formatter.h>
23#include <vector>
Andreas Huber2831d512016-08-15 09:33:47 -070024
Andreas Huberc9410c72016-07-28 12:18:40 -070025namespace android {
26
Yifan Honga4b53d02016-10-31 17:29:10 -070027Scope::Scope(const char *localName,
28 const Location &location)
29 : NamedType(localName, location) {
Andreas Huber9ed827c2016-08-22 12:31:13 -070030}
Yifan Hongf24fa852016-09-23 11:03:15 -070031Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070032
Steven Morelandd537ab02016-09-12 10:32:01 -070033bool Scope::addType(NamedType *type, std::string *errorMsg) {
34 const std::string &localName = type->localName();
35
36 auto it = mTypeIndexByName.find(localName);
37
38 if (it != mTypeIndexByName.end()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070039 *errorMsg = "A type named '";
40 (*errorMsg) += localName;
41 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070042
Andreas Huberc9410c72016-07-28 12:18:40 -070043 return false;
44 }
45
46 size_t index = mTypes.size();
47 mTypes.push_back(type);
Steven Morelandd537ab02016-09-12 10:32:01 -070048 mTypeIndexByName[localName] = index;
Andreas Huberc9410c72016-07-28 12:18:40 -070049
50 return true;
51}
52
Yifan Hongae16eed2016-09-23 13:25:25 -070053NamedType *Scope::lookupType(const FQName &fqName) const {
Yifan Hong327cfe12016-10-03 10:29:42 -070054 CHECK(fqName.package().empty() && fqName.version().empty());
55 if (!fqName.valueName().empty()) {
56 LOG(WARNING) << fqName.string() << " does not refer to a type.";
57 return nullptr;
58 }
59 std::vector<std::string> names = fqName.names();
60 CHECK_GT(names.size(), 0u);
61 auto it = mTypeIndexByName.find(names[0]);
Andreas Huberc9410c72016-07-28 12:18:40 -070062
Yifan Hong327cfe12016-10-03 10:29:42 -070063 if (it == mTypeIndexByName.end()) {
64 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -070065 }
66
Yifan Hong327cfe12016-10-03 10:29:42 -070067 NamedType *outerType = mTypes[it->second];
68 if (names.size() == 1) {
69 return outerType;
70 }
71 if (!outerType->isScope()) {
72 // more than one names, but the first name is not a scope
73 return nullptr;
74 }
75 Scope *outerScope = static_cast<Scope *>(outerType);
76 // *slowly* pop first element
77 names.erase(names.begin());
78 FQName innerName(names);
79 return outerScope->lookupType(innerName);
Andreas Huberc9410c72016-07-28 12:18:40 -070080}
81
Yifan Hongf24fa852016-09-23 11:03:15 -070082LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
83 return NULL;
84}
85
Andreas Huber5345ec22016-07-29 13:33:27 -070086bool Scope::isScope() const {
87 return true;
88}
89
Andreas Huber881227d2016-08-02 14:20:21 -070090Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070091 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070092 return static_cast<Interface *>(mTypes[0]);
93 }
94
95 return NULL;
96}
97
98bool Scope::containsSingleInterface(std::string *ifaceName) const {
99 Interface *iface = getInterface();
100
101 if (iface != NULL) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700102 *ifaceName = iface->localName();
Andreas Hubera2723d22016-07-29 15:36:07 -0700103 return true;
104 }
105
106 return false;
107}
108
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700109bool Scope::containsInterfaces() const {
110 for (const NamedType *type : mTypes) {
111 if (type->isInterface()) {
112 return true;
113 }
114 }
115
116 return false;
117}
118
Yifan Hong244e82d2016-11-11 11:13:57 -0800119status_t Scope::forEachType(std::function<status_t(Type *)> func) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700120 for (size_t i = 0; i < mTypes.size(); ++i) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800121 status_t err = func(mTypes[i]);
Andreas Huber881227d2016-08-02 14:20:21 -0700122
123 if (err != OK) {
124 return err;
125 }
126 }
127
128 return OK;
129}
130
Yifan Hong244e82d2016-11-11 11:13:57 -0800131status_t Scope::emitTypeDeclarations(Formatter &out) const {
132 return forEachType([&](Type *type) {
133 return type->emitTypeDeclarations(out);
134 });
135}
136
Andreas Hubere3f769a2016-10-10 10:54:44 -0700137status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800138 return forEachType([&](Type *type) {
139 return type->emitGlobalTypeDeclarations(out);
140 });
141}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700142
Yifan Hong244e82d2016-11-11 11:13:57 -0800143status_t Scope::emitGlobalHwDeclarations(Formatter &out) const {
144 return forEachType([&](Type *type) {
145 return type->emitGlobalHwDeclarations(out);
146 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700147}
148
Andreas Huber85eabdb2016-08-25 11:24:49 -0700149status_t Scope::emitJavaTypeDeclarations(
150 Formatter &out, bool atTopLevel) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800151 return forEachType([&](Type *type) {
152 return type->emitJavaTypeDeclarations(out, atTopLevel);
153 });
Andreas Huber2831d512016-08-15 09:33:47 -0700154}
155
Andreas Huber881227d2016-08-02 14:20:21 -0700156status_t Scope::emitTypeDefinitions(
157 Formatter &out, const std::string prefix) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800158 return forEachType([&](Type *type) {
159 return type->emitTypeDefinitions(out, prefix);
160 });
Andreas Huber881227d2016-08-02 14:20:21 -0700161}
162
Steven Morelandd537ab02016-09-12 10:32:01 -0700163const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700164 return mTypes;
165}
166
167status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800168 return forEachType([&](Type *type) {
169 return type->emitVtsTypeDeclarations(out);
170 });
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700171}
172
Andreas Huber70a59e12016-08-16 12:57:01 -0700173bool Scope::isJavaCompatible() const {
174 for (const auto &type : mTypes) {
175 if (!type->isJavaCompatible()) {
176 return false;
177 }
178 }
179
180 return true;
181}
182
Andreas Huber019d21d2016-10-03 12:59:47 -0700183void Scope::appendToExportedTypesVector(
184 std::vector<const Type *> *exportedTypes) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800185 forEachType([&](Type *type) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700186 type->appendToExportedTypesVector(exportedTypes);
Yifan Hong244e82d2016-11-11 11:13:57 -0800187 return OK;
188 });
Andreas Huber019d21d2016-10-03 12:59:47 -0700189}
190
Yifan Hongf24fa852016-09-23 11:03:15 -0700191LocalIdentifier::LocalIdentifier(){}
192LocalIdentifier::~LocalIdentifier(){}
193
194bool LocalIdentifier::isEnumValue() const {
195 return false;
196}
197
Andreas Huberc9410c72016-07-28 12:18:40 -0700198} // namespace android
199