blob: b2414b36d59cd23382300fb0407f304aa7fddfa1 [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
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070098bool Scope::containsInterfaces() const {
99 for (const NamedType *type : mTypes) {
100 if (type->isInterface()) {
101 return true;
102 }
103 }
104
105 return false;
106}
107
Yifan Hong244e82d2016-11-11 11:13:57 -0800108status_t Scope::forEachType(std::function<status_t(Type *)> func) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700109 for (size_t i = 0; i < mTypes.size(); ++i) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800110 status_t err = func(mTypes[i]);
Andreas Huber881227d2016-08-02 14:20:21 -0700111
112 if (err != OK) {
113 return err;
114 }
115 }
116
117 return OK;
118}
119
Yifan Hong244e82d2016-11-11 11:13:57 -0800120status_t Scope::emitTypeDeclarations(Formatter &out) const {
121 return forEachType([&](Type *type) {
122 return type->emitTypeDeclarations(out);
123 });
124}
125
Andreas Hubere3f769a2016-10-10 10:54:44 -0700126status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800127 return forEachType([&](Type *type) {
128 return type->emitGlobalTypeDeclarations(out);
129 });
130}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700131
Yifan Hong244e82d2016-11-11 11:13:57 -0800132status_t Scope::emitGlobalHwDeclarations(Formatter &out) const {
133 return forEachType([&](Type *type) {
134 return type->emitGlobalHwDeclarations(out);
135 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700136}
137
Andreas Huber85eabdb2016-08-25 11:24:49 -0700138status_t Scope::emitJavaTypeDeclarations(
139 Formatter &out, bool atTopLevel) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800140 return forEachType([&](Type *type) {
141 return type->emitJavaTypeDeclarations(out, atTopLevel);
142 });
Andreas Huber2831d512016-08-15 09:33:47 -0700143}
144
Andreas Huber881227d2016-08-02 14:20:21 -0700145status_t Scope::emitTypeDefinitions(
146 Formatter &out, const std::string prefix) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800147 return forEachType([&](Type *type) {
148 return type->emitTypeDefinitions(out, prefix);
149 });
Andreas Huber881227d2016-08-02 14:20:21 -0700150}
151
Steven Morelandd537ab02016-09-12 10:32:01 -0700152const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700153 return mTypes;
154}
155
156status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800157 return forEachType([&](Type *type) {
158 return type->emitVtsTypeDeclarations(out);
159 });
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700160}
161
Andreas Huber70a59e12016-08-16 12:57:01 -0700162bool Scope::isJavaCompatible() const {
163 for (const auto &type : mTypes) {
164 if (!type->isJavaCompatible()) {
165 return false;
166 }
167 }
168
169 return true;
170}
171
Andreas Huber60d3b222017-03-30 09:10:56 -0700172bool Scope::containsPointer() const {
173 for (const auto &type : mTypes) {
174 if (type->containsPointer()) {
175 return true;
176 }
177 }
178
179 return false;
180}
181
Andreas Huber019d21d2016-10-03 12:59:47 -0700182void Scope::appendToExportedTypesVector(
183 std::vector<const Type *> *exportedTypes) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800184 forEachType([&](Type *type) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700185 type->appendToExportedTypesVector(exportedTypes);
Yifan Hong244e82d2016-11-11 11:13:57 -0800186 return OK;
187 });
Andreas Huber019d21d2016-10-03 12:59:47 -0700188}
189
Yifan Hongf24fa852016-09-23 11:03:15 -0700190LocalIdentifier::LocalIdentifier(){}
191LocalIdentifier::~LocalIdentifier(){}
192
193bool LocalIdentifier::isEnumValue() const {
194 return false;
195}
196
Andreas Huberc9410c72016-07-28 12:18:40 -0700197} // namespace android
198