refactor: remove AidlConstantReference::SetRefType
A constant reference is composed of <type>.<field>. But when <type> is
missing, <field> is looked up from the defining type (the current
scope).
e.g. enum Enum { A = 0, B = A }; // the second "A" refers to "Enum.A"
Previously, when <type> is missing, we create AidlTypeSpecifer for the
current scope type and set it as AidlConstantReference's type.
However, the purpose of AidlTypeSpecifier is to resolve it to a
AidlDefinedType to look up <field> from it.
In the above example, AidlTypeSpecifier("Enum") is created and resolved
to "Enum" type.
Because we already know the scope type, creating AidlTypeSpecifier and
resolve it again is meaningless.
When traversing AST, these manually created nodes should be skipped with
additional flags. (e.g. user_defined_xxx). Let's avoid creating AST
nodes manually.
Bug: none
Test: m aidl_unittests
Change-Id: I3965e571875c6b6a1c7c237f0b4628d362958d46
diff --git a/aidl_const_expressions.cpp b/aidl_const_expressions.cpp
index 0b7fc15..726da1d 100644
--- a/aidl_const_expressions.cpp
+++ b/aidl_const_expressions.cpp
@@ -746,28 +746,33 @@
}
}
-const AidlConstantValue* AidlConstantReference::Resolve() {
+const AidlConstantValue* AidlConstantReference::Resolve(const AidlDefinedType* scope) const {
if (resolved_) return resolved_;
- if (!GetRefType() || !GetRefType()->GetDefinedType()) {
+
+ const AidlDefinedType* defined_type;
+ if (ref_type_) {
+ defined_type = ref_type_->GetDefinedType();
+ } else {
+ defined_type = scope;
+ }
+
+ if (!defined_type) {
// This can happen when "const reference" is used in an unsupported way,
// but missed in checks there. It works as a safety net.
AIDL_ERROR(*this) << "Can't resolve the reference (" << value_ << ")";
return nullptr;
}
- auto defined_type = GetRefType()->GetDefinedType();
if (auto enum_decl = defined_type->AsEnumDeclaration(); enum_decl) {
for (const auto& e : enum_decl->GetEnumerators()) {
if (e->GetName() == field_name_) {
- resolved_ = e->GetValue();
- return resolved_;
+ return resolved_ = e->GetValue();
}
}
} else {
for (const auto& c : defined_type->GetConstantDeclarations()) {
if (c->GetName() == field_name_) {
- resolved_ = &c->GetValue();
- return resolved_;
+ return resolved_ = &c->GetValue();
}
}
}