blob: 5e8e6870e6d698e082e20dff6712a385d8063fb8 [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "Scope.h"
2
3#include "Constant.h"
4#include "Formatter.h"
Andreas Hubera2723d22016-07-29 15:36:07 -07005#include "Interface.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07006
7namespace android {
8
Andreas Huber31629bc2016-08-03 09:06:40 -07009Scope::Scope() {}
Andreas Huberc9410c72016-07-28 12:18:40 -070010
Andreas Huber31629bc2016-08-03 09:06:40 -070011bool Scope::addType(const char *localName, NamedType *type) {
12 if (mTypeIndexByName.indexOfKey(localName) >= 0) {
Andreas Huberc9410c72016-07-28 12:18:40 -070013 return false;
14 }
15
16 size_t index = mTypes.size();
17 mTypes.push_back(type);
Andreas Huber31629bc2016-08-03 09:06:40 -070018 mTypeIndexByName.add(localName, index);
Andreas Huberc9410c72016-07-28 12:18:40 -070019
20 return true;
21}
22
23bool Scope::addConstant(Constant *constant) {
24 ssize_t index = mConstants.indexOfKey(constant->name());
25
26 if (index >= 0) {
27 return false;
28 }
29
30 mConstants.add(constant->name(), constant);
31
32 return true;
33}
34
35Type *Scope::lookupType(const char *name) const {
36 ssize_t index = mTypeIndexByName.indexOfKey(name);
37
38 if (index >= 0) {
39 return mTypes[mTypeIndexByName.valueAt(index)];
40 }
41
42 return NULL;
43}
44
Andreas Huber5345ec22016-07-29 13:33:27 -070045bool Scope::isScope() const {
46 return true;
47}
48
Andreas Huber881227d2016-08-02 14:20:21 -070049Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070050 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070051 return static_cast<Interface *>(mTypes[0]);
52 }
53
54 return NULL;
55}
56
57bool Scope::containsSingleInterface(std::string *ifaceName) const {
58 Interface *iface = getInterface();
59
60 if (iface != NULL) {
Andreas Huber0e00de42016-08-03 09:56:02 -070061 *ifaceName = iface->localName();
Andreas Hubera2723d22016-07-29 15:36:07 -070062 return true;
63 }
64
65 return false;
66}
67
Andreas Huber881227d2016-08-02 14:20:21 -070068status_t Scope::emitTypeDeclarations(Formatter &out) const {
69 for (size_t i = 0; i < mTypes.size(); ++i) {
70 status_t err = mTypes[i]->emitTypeDeclarations(out);
71
72 if (err != OK) {
73 return err;
74 }
75 }
76
77 return OK;
78}
79
80status_t Scope::emitTypeDefinitions(
81 Formatter &out, const std::string prefix) const {
82 for (size_t i = 0; i < mTypes.size(); ++i) {
83 status_t err = mTypes[i]->emitTypeDefinitions(out, prefix);
84
85 if (err != OK) {
86 return err;
87 }
88 }
89
90 return OK;
91}
92
Andreas Huberc9410c72016-07-28 12:18:40 -070093} // namespace android
94