refactor: AidlAnnotation/AidlTypeSpecifier are commentable
These nodes are not syntactically commentable, but they hold comments
for parent nodes while parsing.
For example,
// comments
@Annotation int foo;
The comment is first attached to the annotation and then transfered to
the type('int') and then finally passed to the variable declaration.
While processing AST, we only care about variable 'foo's comments.
Bug: none
Test: aidl_unittests
Change-Id: I11f19dd9e5f8b5fe6a8343f862bbe34393ad8a4d
diff --git a/aidl_language.h b/aidl_language.h
index 8604c34..80d321e 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -173,15 +173,27 @@
template <>
bool AidlParameterizable<std::string>::CheckValid() const;
-class AidlConstantValue;
-class AidlConstantDeclaration;
+class AidlCommentable {
+ public:
+ AidlCommentable(const std::string& comments) : comments_(comments) {}
+ virtual ~AidlCommentable() = default;
+
+ const std::string& GetComments() const { return comments_; }
+ void SetComments(const std::string comments) { comments_ = comments; }
+ bool IsHidden() const;
+ bool IsDeprecated() const;
+ void Dump(CodeWriter& out) const;
+
+ private:
+ std::string comments_;
+};
// Transforms a value string into a language specific form. Raw value as produced by
// AidlConstantValue.
using ConstantValueDecorator =
std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
-class AidlAnnotation : public AidlNode {
+class AidlAnnotation : public AidlNode, public AidlCommentable {
public:
enum class Type {
BACKING = 1,
@@ -222,7 +234,8 @@
static AidlAnnotation* Parse(
const AidlLocation& location, const string& name,
- std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
+ std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list,
+ const std::string& comments);
AidlAnnotation(const AidlAnnotation&) = default;
AidlAnnotation(AidlAnnotation&&) = default;
@@ -243,8 +256,6 @@
std::map<std::string, std::string> AnnotationParams(
const ConstantValueDecorator& decorator) const;
- const string& GetComments() const { return comments_; }
- void SetComments(const string& comments) { comments_ = comments; }
void TraverseChildren(std::function<void(const AidlNode&)> traverse) const override;
void DispatchVisit(AidlVisitor& v) const override { v.Visit(*this); }
@@ -275,10 +286,10 @@
static const std::vector<Schema>& AllSchemas();
AidlAnnotation(const AidlLocation& location, const Schema& schema,
- std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
+ std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters,
+ const std::string& comments);
const Schema& schema_;
- string comments_;
std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
};
@@ -340,6 +351,7 @@
// AidlTypeSpecifier represents a reference to either a built-in type,
// a defined type, or a variant (e.g., array of generic) of a type.
class AidlTypeSpecifier final : public AidlAnnotatable,
+ public AidlCommentable,
public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
public:
AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
@@ -376,12 +388,8 @@
const string& GetUnresolvedName() const { return unresolved_name_; }
- const string& GetComments() const { return comments_; }
-
const std::vector<std::string> GetSplitName() const { return split_name_; }
- void SetComments(const string& comment) { comments_ = comment; }
-
bool IsResolved() const { return fully_qualified_name_ != ""; }
bool IsArray() const { return is_array_; }
@@ -417,7 +425,6 @@
const string unresolved_name_;
string fully_qualified_name_;
bool is_array_;
- string comments_;
vector<string> split_name_;
const AidlDefinedType* defined_type_ = nullptr; // set when Resolve() for defined types
mutable shared_ptr<AidlTypeSpecifier> array_base_;
@@ -426,21 +433,6 @@
// Returns the universal value unaltered.
std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
-class AidlCommentable {
- public:
- AidlCommentable(const std::string& comments) : comments_(comments) {}
- virtual ~AidlCommentable() = default;
-
- const std::string& GetComments() const { return comments_; }
- void SetComments(const std::string comments) { comments_ = comments; }
- bool IsHidden() const;
- bool IsDeprecated() const;
- void Dump(CodeWriter& out) const;
-
- private:
- std::string comments_;
-};
-
class AidlMember : public AidlNode, public AidlCommentable {
public:
AidlMember(const AidlLocation& location, const std::string& comments);
@@ -878,7 +870,6 @@
private:
bool oneway_;
- std::string comments_;
std::unique_ptr<AidlTypeSpecifier> type_;
std::string name_;
const std::vector<std::unique_ptr<AidlArgument>> arguments_;
@@ -985,7 +976,6 @@
bool CheckValidWithMembers(const AidlTypenames& typenames) const;
std::string name_;
- std::string comments_;
const std::string package_;
const std::vector<std::string> split_package_;
std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;