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_language.h b/aidl_language.h
index 08cd5b9..0ea5ce4 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -549,10 +549,10 @@
struct Visitor {
virtual ~Visitor() {}
- virtual void Visit(AidlConstantValue&) = 0;
- virtual void Visit(AidlConstantReference&) = 0;
- virtual void Visit(AidlUnaryConstExpression&) = 0;
- virtual void Visit(AidlBinaryConstExpression&) = 0;
+ virtual void Visit(const AidlConstantValue&) = 0;
+ virtual void Visit(const AidlConstantReference&) = 0;
+ virtual void Visit(const AidlUnaryConstExpression&) = 0;
+ virtual void Visit(const AidlBinaryConstExpression&) = 0;
};
// Returns the evaluated value. T> should match to the actual type.
@@ -668,13 +668,12 @@
const std::string& comments);
const std::unique_ptr<AidlTypeSpecifier>& GetRefType() const { return ref_type_; }
- void SetRefType(std::unique_ptr<AidlTypeSpecifier> type) { ref_type_ = std::move(type); }
const std::string& GetFieldName() const { return field_name_; }
const std::string& GetComments() const { return comments_; }
bool CheckValid() const override;
void Accept(Visitor& visitor) override { visitor.Visit(*this); }
- const AidlConstantValue* Resolve();
+ const AidlConstantValue* Resolve(const AidlDefinedType* scope) const;
private:
bool evaluate() const override;
@@ -682,7 +681,7 @@
std::unique_ptr<AidlTypeSpecifier> ref_type_;
std::string field_name_;
const std::string comments_;
- const AidlConstantValue* resolved_ = nullptr;
+ mutable const AidlConstantValue* resolved_ = nullptr;
};
class AidlUnaryConstExpression : public AidlConstantValue {