Extract the common base class: AidlCommentable

For now it is inherited by AidlMember & AidlDefinedType.

Bug: 174514415
Test: aidl_unittests
Change-Id: Idf8e298f1b029c49030878aa338231e1a545abea
diff --git a/aidl_language.cpp b/aidl_language.cpp
index 1016d76..532cbec 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -655,7 +655,7 @@
 AidlVariableDeclaration::AidlVariableDeclaration(const AidlLocation& location,
                                                  AidlTypeSpecifier* type, const std::string& name,
                                                  AidlConstantValue* default_value)
-    : AidlMember(location),
+    : AidlMember(location, type->GetComments()),
       type_(type),
       name_(name),
       default_user_specified_(true),
@@ -763,12 +763,21 @@
   }
 }
 
-AidlMember::AidlMember(const AidlLocation& location) : AidlNode(location) {}
+bool AidlCommentable::IsHidden() const {
+  return HasHideComment(GetComments());
+}
+
+bool AidlCommentable::IsDeprecated() const {
+  return HasDeprecatedComment(GetComments());
+}
+
+AidlMember::AidlMember(const AidlLocation& location, const std::string& comments)
+    : AidlNode(location), AidlCommentable(comments) {}
 
 AidlConstantDeclaration::AidlConstantDeclaration(const AidlLocation& location,
                                                  AidlTypeSpecifier* type, const std::string& name,
                                                  AidlConstantValue* value)
-    : AidlMember(location), type_(type), name_(name), value_(value) {}
+    : AidlMember(location, type->GetComments()), type_(type), name_(name), value_(value) {}
 
 bool AidlConstantDeclaration::CheckValid(const AidlTypenames& typenames) const {
   bool valid = true;
@@ -804,9 +813,8 @@
 AidlMethod::AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type,
                        const std::string& name, std::vector<std::unique_ptr<AidlArgument>>* args,
                        const std::string& comments, int id, bool is_user_defined)
-    : AidlMember(location),
+    : AidlMember(location, comments),
       oneway_(oneway),
-      comments_(comments),
       type_(type),
       name_(name),
       arguments_(std::move(*args)),
@@ -820,14 +828,6 @@
   }
 }
 
-bool AidlMember::IsHidden() const {
-  return HasHideComment(GetComments());
-}
-
-bool AidlMember::IsDeprecated() const {
-  return HasDeprecatedComment(GetComments());
-}
-
 string AidlMethod::Signature() const {
   vector<string> arg_signatures;
   for (const auto& arg : GetArguments()) {
@@ -853,8 +853,8 @@
                                  const std::string& comments, const std::string& package,
                                  std::vector<std::unique_ptr<AidlMember>>* members)
     : AidlAnnotatable(location),
+      AidlCommentable(comments),
       name_(name),
-      comments_(comments),
       package_(package),
       split_package_(package.empty() ? std::vector<std::string>()
                                      : android::base::Split(package, ".")) {
@@ -885,14 +885,6 @@
   return true;
 }
 
-bool AidlDefinedType::IsHidden() const {
-  return HasHideComment(GetComments());
-}
-
-bool AidlDefinedType::IsDeprecated() const {
-  return HasDeprecatedComment(GetComments());
-}
-
 std::string AidlDefinedType::GetCanonicalName() const {
   if (package_.empty()) {
     return GetName();
diff --git a/aidl_language.h b/aidl_language.h
index b438aa5..54b6fc0 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -426,9 +426,23 @@
 // Returns the universal value unaltered.
 std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
 
-class AidlMember : public AidlNode {
+class AidlCommentable {
  public:
-  AidlMember(const AidlLocation& location);
+  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;
+
+ private:
+  std::string comments_;
+};
+
+class AidlMember : public AidlNode, public AidlCommentable {
+ public:
+  AidlMember(const AidlLocation& location, const std::string& comments);
   virtual ~AidlMember() = default;
 
   // non-copyable, non-movable
@@ -452,10 +466,6 @@
     return const_cast<AidlVariableDeclaration*>(
         const_cast<const AidlMember*>(this)->AsVariableDeclaration());
   }
-
-  virtual const std::string& GetComments() const = 0;
-  bool IsHidden() const;
-  bool IsDeprecated() const;
 };
 
 // TODO: This class is used for method arguments and also parcelable fields,
@@ -476,7 +486,6 @@
   AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
 
   const AidlVariableDeclaration* AsVariableDeclaration() const override { return this; }
-  const std::string& GetComments() const override { return GetType().GetComments(); }
 
   std::string GetName() const { return name_; }
   std::string GetCapitalizedName() const;
@@ -791,7 +800,6 @@
   }
 
   const AidlConstantDeclaration* AsConstantDeclaration() const override { return this; }
-  const std::string& GetComments() const override { return GetType().GetComments(); }
 
   void TraverseChildren(std::function<void(const AidlNode&)> traverse) const override {
     traverse(GetType());
@@ -821,7 +829,6 @@
   AidlMethod& operator=(AidlMethod&&) = delete;
 
   const AidlMethod* AsMethod() const override { return this; }
-  const string& GetComments() const override { return comments_; }
   const AidlTypeSpecifier& GetType() const { return *type_; }
   AidlTypeSpecifier* GetMutableType() { return type_.get(); }
 
@@ -883,7 +890,7 @@
 
 // AidlDefinedType represents either an interface, parcelable, or enum that is
 // defined in the source file.
-class AidlDefinedType : public AidlAnnotatable {
+class AidlDefinedType : public AidlAnnotatable, public AidlCommentable {
  public:
   AidlDefinedType(const AidlLocation& location, const std::string& name,
                   const std::string& comments, const std::string& package,
@@ -897,10 +904,6 @@
   AidlDefinedType& operator=(AidlDefinedType&&) = delete;
 
   const std::string& GetName() const { return name_; };
-  bool IsHidden() const;
-  bool IsDeprecated() const;
-  const std::string& GetComments() const { return comments_; }
-  void SetComments(const std::string comments) { comments_ = comments; }
 
   /* dot joined package, example: "android.package.foo" */
   std::string GetPackage() const { return package_; }