Move annotations from Type to Scope

Test: hidl_test
Change-Id: I0b570096c38e27cb818930bd508d31aa47fe5d4d
diff --git a/Scope.cpp b/Scope.cpp
index 1cee4f8..9223b78 100644
--- a/Scope.cpp
+++ b/Scope.cpp
@@ -16,6 +16,7 @@
 
 #include "Scope.h"
 
+#include "Annotation.h"
 #include "Interface.h"
 
 #include <android-base/logging.h>
@@ -103,6 +104,16 @@
     return false;
 }
 
+const std::vector<Annotation*>& Scope::annotations() const {
+    return mAnnotations;
+}
+
+void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
+    CHECK(mAnnotations.empty());
+    CHECK(annotations != nullptr);
+    mAnnotations = *annotations;
+}
+
 status_t Scope::forEachType(std::function<status_t(Type *)> func) const {
     for (size_t i = 0; i < mTypes.size(); ++i) {
         status_t err = func(mTypes[i]);
diff --git a/Scope.h b/Scope.h
index 6a34a59..14f51f9 100644
--- a/Scope.h
+++ b/Scope.h
@@ -25,6 +25,7 @@
 
 namespace android {
 
+struct Annotation;
 struct ConstantExpression;
 struct Formatter;
 struct Interface;
@@ -49,6 +50,10 @@
 
     bool containsInterfaces() const;
 
+    const std::vector<Annotation*>& annotations() const;
+
+    void setAnnotations(std::vector<Annotation*>* annotations);
+
     status_t emitTypeDeclarations(Formatter &out) const override;
     status_t emitGlobalTypeDeclarations(Formatter &out) const override;
     status_t emitGlobalHwDeclarations(Formatter &out) const override;
@@ -72,6 +77,7 @@
 private:
     std::vector<NamedType *> mTypes;
     std::map<std::string, size_t> mTypeIndexByName;
+    std::vector<Annotation*> mAnnotations;
 
     status_t forEachType(std::function<status_t(Type *)> func) const;
 
diff --git a/Type.cpp b/Type.cpp
index 6512fd7..3d32180 100644
--- a/Type.cpp
+++ b/Type.cpp
@@ -16,7 +16,6 @@
 
 #include "Type.h"
 
-#include "Annotation.h"
 #include "ScalarType.h"
 
 #include <hidl-util/Formatter.h>
@@ -24,20 +23,10 @@
 
 namespace android {
 
-Type::Type()
-    : mAnnotations(nullptr) {
-}
+Type::Type() {}
 
 Type::~Type() {}
 
-void Type::setAnnotations(std::vector<Annotation *> *annotations) {
-    mAnnotations = annotations;
-}
-
-const std::vector<Annotation *> &Type::annotations() const {
-    return *mAnnotations;
-}
-
 bool Type::isScope() const {
     return false;
 }
diff --git a/Type.h b/Type.h
index f4769ba..a489e5c 100644
--- a/Type.h
+++ b/Type.h
@@ -28,7 +28,6 @@
 
 namespace android {
 
-struct Annotation;
 struct Formatter;
 struct ScalarType;
 struct FQName;
@@ -210,9 +209,6 @@
     virtual bool containsPointer() const;
     virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
 
-    void setAnnotations(std::vector<Annotation *> *annotations);
-    const std::vector<Annotation *> &annotations() const;
-
     virtual void appendToExportedTypesVector(
             std::vector<const Type *> *exportedTypes) const;
 
@@ -250,8 +246,6 @@
             const std::string &name) const;
 
 private:
-    std::vector<Annotation *> *mAnnotations;
-
     DISALLOW_COPY_AND_ASSIGN(Type);
 };
 
diff --git a/hidl-gen_y.yy b/hidl-gen_y.yy
index b7b6176..603cddb 100644
--- a/hidl-gen_y.yy
+++ b/hidl-gen_y.yy
@@ -624,7 +624,8 @@
     : opt_annotations type_declaration_body
       {
           if (!$2->isTypeDef()) {
-              $2->setAnnotations($1);
+              CHECK($2->isScope());
+              static_cast<Scope*>($2)->setAnnotations($1);
           } else if (!$1->empty()) {
               // Since typedefs are always resolved to their target it makes
               // little sense to annotate them and have their annotations
@@ -937,7 +938,8 @@
 annotated_compound_declaration
     : opt_annotations compound_declaration
       {
-          $2->setAnnotations($1);
+          CHECK($2->isScope());
+          static_cast<Scope*>($2)->setAnnotations($1);
           $$ = $2;
       }
     ;