Kill comments token in AidlMethod

Change-Id: I3df6629eaa5b56093d2453269d4b138987bee7ce
Test: unit tests
Bug: 24410295
Signed-off-by: Casey Dahlin <sadmac@google.com>
diff --git a/aidl.cpp b/aidl.cpp
index b84fcdf..b63c869 100644
--- a/aidl.cpp
+++ b/aidl.cpp
@@ -206,14 +206,14 @@
   int err = 0;
   set<string> method_names;
   for (const auto& m : *c->methods) {
-    if (!types->AddContainerType(m->type->type.data) ||
+    if (!types->AddContainerType(m->type->GetName()) ||
         !types->IsValidReturnType(*m->type, filename)) {
       err = 1;  // return type is invalid
     }
 
     int index = 1;
-    for (const std::unique_ptr<AidlArgument>& arg : *m->args) {
-      if (!types->AddContainerType(arg->GetType().type.data) ||
+    for (const auto& arg : *m->args) {
+      if (!types->AddContainerType(arg->GetType().GetName()) ||
           !types->IsValidArg(*arg, index, filename)) {
         err = 1;
       }
diff --git a/aidl_language.cpp b/aidl_language.cpp
index eeed677..ab87107 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -43,14 +43,25 @@
   yylex_destroy(scanner_);
 }
 
+AidlType::AidlType(const std::string& name, unsigned line,
+                   const std::string& comments, unsigned dimension)
+    : name_(name),
+      line_(line),
+      dimension_(dimension),
+      comments_(comments) {}
+
+AidlType::AidlType(const std::string& name, unsigned line,
+                   const std::string& comments)
+    : AidlType(name, line, comments, 0) {}
+
 string AidlType::ToString() const {
-  return string(type.data) + Brackets();
+  return name_ + Brackets();
 }
 
 std::string AidlType::Brackets() const {
   std::string result;
 
-  for (int i = 0; i < dimension; i++)
+  for (unsigned i = 0; i < dimension_; i++)
     result += "[]";
 
   return result;
@@ -95,6 +106,9 @@
   return ret;
 }
 
+AidlMethod::AidlMethod(std::string comments)
+    : comments_(comments) {}
+
 string Parser::FileName() {
   return filename_;
 }
diff --git a/aidl_language.h b/aidl_language.h
index 3de29ec..c870111 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -48,16 +48,26 @@
 
 class AidlType : public AidlNode {
  public:
-  AidlType() = default;
+  AidlType(const std::string& name, unsigned line,
+           const std::string& comments, unsigned dimension);
+  AidlType(const std::string& name, unsigned line,
+           const std::string& comments);
   virtual ~AidlType() = default;
 
-  buffer_type type;
-  int dimension;
+  const std::string& GetName() const { return name_; }
+  unsigned GetLine() const { return line_; }
+  unsigned GetDimension() const { return dimension_; }
+  const std::string& GetComments() const { return comments_; }
 
   std::string ToString() const;
   std::string Brackets() const;
 
  private:
+  std::string name_;
+  unsigned line_;
+  unsigned dimension_;
+  std::string comments_;
+
   DISALLOW_COPY_AND_ASSIGN(AidlType);
 };
 
@@ -65,7 +75,8 @@
  public:
   enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
 
-  AidlArgument(AidlArgument::Direction direction, AidlType* type, buffer_type name);
+  AidlArgument(AidlArgument::Direction direction, AidlType* type,
+               buffer_type name);
   AidlArgument(AidlType *type, buffer_type name);
   virtual ~AidlArgument() = default;
 
@@ -89,9 +100,11 @@
 
 class AidlMethod {
  public:
-  AidlMethod() = default;
+  AidlMethod(std::string comments);
   virtual ~AidlMethod() = default;
 
+  std::string GetComments() const { return comments_; }
+
   AidlType* type;
   bool oneway;
   buffer_type oneway_token;
@@ -100,10 +113,11 @@
   bool hasId;
   buffer_type id;
   buffer_type semicolon_token;
-  buffer_type* comments_token; // points into this structure, DO NOT DELETE
   int assigned_id;
 
  private:
+  std::string comments_;
+
   DISALLOW_COPY_AND_ASSIGN(AidlMethod);
 };
 
diff --git a/aidl_language_y.y b/aidl_language_y.y
index aee02db..28c86c2 100644
--- a/aidl_language_y.y
+++ b/aidl_language_y.y
@@ -189,7 +189,7 @@
 
 method_decl:
         type IDENTIFIER '(' arg_list ')' ';'  {
-                                                        AidlMethod *method = new AidlMethod();
+                                                        AidlMethod *method = new AidlMethod($1->GetComments());
                                                         method->oneway = false;
                                                         method->type = $1;
                                                         memset(&method->oneway_token, 0, sizeof(buffer_type));
@@ -198,11 +198,11 @@
                                                         method->hasId = false;
                                                         memset(&method->id, 0, sizeof(buffer_type));
                                                         method->semicolon_token = $6;
-                                                        method->comments_token = &method->type->type;
                                                         $$ = method;
                                                     }
     |   ONEWAY type IDENTIFIER '(' arg_list ')' ';'  {
-                                                        AidlMethod *method = new AidlMethod();
+                                                        AidlMethod *method =
+                                                            new AidlMethod(android::aidl::gather_comments($1.extra));
                                                         method->oneway = true;
                                                         method->oneway_token = $1;
                                                         method->type = $2;
@@ -211,11 +211,10 @@
                                                         method->hasId = false;
                                                         memset(&method->id, 0, sizeof(buffer_type));
                                                         method->semicolon_token = $7;
-                                                        method->comments_token = &method->oneway_token;
                                                         $$ = method;
                                                     }
     |    type IDENTIFIER '(' arg_list ')' '=' IDVALUE ';'  {
-                                                        AidlMethod *method = new AidlMethod();
+                                                        AidlMethod *method = new AidlMethod($1->GetComments());
                                                         method->oneway = false;
                                                         memset(&method->oneway_token, 0, sizeof(buffer_type));
                                                         method->type = $1;
@@ -224,11 +223,11 @@
                                                         method->hasId = true;
                                                         method->id = $7;
                                                         method->semicolon_token = $8;
-                                                        method->comments_token = &method->type->type;
                                                         $$ = method;
                                                     }
     |   ONEWAY type IDENTIFIER '(' arg_list ')' '=' IDVALUE ';'  {
-                                                        AidlMethod *method = new AidlMethod();
+                                                        AidlMethod *method =
+                                                            new AidlMethod(android::aidl::gather_comments($1.extra));
                                                         method->oneway = true;
                                                         method->oneway_token = $1;
                                                         method->type = $2;
@@ -237,7 +236,6 @@
                                                         method->hasId = true;
                                                         method->id = $8;
                                                         method->semicolon_token = $9;
-                                                        method->comments_token = &method->oneway_token;
                                                         $$ = method;
                                                     }
     ;
@@ -265,23 +263,20 @@
  | type IDENTIFIER
   { $$ = new AidlArgument($1, $2); };
 
-type:
-        IDENTIFIER              {
-				    $$ = new AidlType();
-                                    $$->type = $1;
-                                    $$->dimension = 0;
-                                }
-    |   IDENTIFIER ARRAY        {
-				    $$ = new AidlType();
-                                    $$->type = $1;
-                                    $$->dimension = count_brackets($2.data);
-                                }
-    |   GENERIC                 {
-				    $$ = new AidlType();
-                                    $$->type = $1;
-                                    $$->dimension = 0;
-                                }
-    ;
+type
+ : IDENTIFIER {
+    $$ = new AidlType($1.data, @1.begin.line,
+                      android::aidl::gather_comments($1.extra));
+  }
+ | IDENTIFIER ARRAY {
+    $$ = new AidlType($1.data,
+                      @1.begin.line, android::aidl::gather_comments($1.extra),
+                      count_brackets($2.data));
+  }
+ | GENERIC {
+    $$ = new AidlType($1.data, @1.begin.line,
+                      android::aidl::gather_comments($1.extra));
+  };
 
 direction
  : IN
diff --git a/generate_cpp.cpp b/generate_cpp.cpp
index 5f14a61..0df7ae9 100644
--- a/generate_cpp.cpp
+++ b/generate_cpp.cpp
@@ -53,7 +53,7 @@
 
 string GetCPPVarDec(const TypeNamespace& types, const AidlType& type,
                     const string& var_name, bool use_pointer) {
-  const Type* cpp_type = types.Find(type.type.data);
+  const Type* cpp_type = types.Find(type.GetName());
   if (cpp_type == nullptr) {
     // We should have caught this in type resolution.
     LOG(FATAL) << "internal error";
diff --git a/generate_java.cpp b/generate_java.cpp
index 736385e..3d780d7 100644
--- a/generate_java.cpp
+++ b/generate_java.cpp
@@ -38,24 +38,6 @@
 
 // =================================================
 string
-gather_comments(extra_text_type* extra)
-{
-    string s;
-    while (extra) {
-        if (extra->which == SHORT_COMMENT) {
-            s += extra->data;
-        }
-        else if (extra->which == LONG_COMMENT) {
-            s += "/*";
-            s += extra->data;
-            s += "*/";
-        }
-        extra = extra->next;
-    }
-    return s;
-}
-
-string
 append(const char* a, const char* b)
 {
     string s = a;
diff --git a/generate_java.h b/generate_java.h
index 7980319..5affbf1 100644
--- a/generate_java.h
+++ b/generate_java.h
@@ -24,7 +24,6 @@
 
 }  // namespace java
 
-string gather_comments(extra_text_type* extra);
 string append(const char* a, const char* b);
 
 class VariableFactory
diff --git a/generate_java_binder.cpp b/generate_java_binder.cpp
index b8fded0..2150d48 100644
--- a/generate_java_binder.cpp
+++ b/generate_java_binder.cpp
@@ -286,16 +286,16 @@
 
     // == the declaration in the interface ===================================
     Method* decl = new Method;
-        decl->comment = gather_comments(method.comments_token->extra);
+        decl->comment = method.GetComments();
         decl->modifiers = PUBLIC;
-        decl->returnType = types->Find(method.type->type.data);
-        decl->returnTypeDimension = method.type->dimension;
+        decl->returnType = types->Find(method.type->GetName());
+        decl->returnTypeDimension = method.type->GetDimension();
         decl->name = method.name.data;
 
     for (const std::unique_ptr<AidlArgument>& arg : *method.args) {
         decl->parameters.push_back(new Variable(
-                            types->Find(arg->GetType().type.data), arg->GetName(),
-                            arg->GetType().dimension));
+                            types->Find(arg->GetType().GetName()), arg->GetName(),
+                            arg->GetType().GetDimension()));
     }
 
     decl->exceptions.push_back(types->RemoteExceptionType());
@@ -316,9 +316,9 @@
     Variable* cl = NULL;
     VariableFactory stubArgs("_arg");
     for (const std::unique_ptr<AidlArgument>& arg : *method.args) {
-        const Type* t = types->Find(arg->GetType().type.data);
+        const Type* t = types->Find(arg->GetType().GetName());
         Variable* v = stubArgs.Get(t);
-        v->dimension = arg->GetType().dimension;
+        v->dimension = arg->GetType().GetDimension();
 
         c->statements->Add(new VariableDeclaration(v));
 
@@ -326,10 +326,10 @@
             generate_create_from_parcel(t, c->statements, v,
                     stubClass->transact_data, &cl);
         } else {
-            if (arg->GetType().dimension == 0) {
+            if (arg->GetType().GetDimension() == 0) {
                 c->statements->Add(new Assignment(v, new NewExpression(v->type)));
             }
-            else if (arg->GetType().dimension == 1) {
+            else if (arg->GetType().GetDimension() == 1) {
                 generate_new_array(v->type, c->statements, v,
                         stubClass->transact_data, types);
             }
@@ -344,7 +344,7 @@
 
     // the real call
     Variable* _result = NULL;
-    if (0 == strcmp(method.type->type.data, "void")) {
+    if (method.type->GetName() == "void") {
         c->statements->Add(realCall);
 
         if (!oneway) {
@@ -374,7 +374,7 @@
     // out parameters
     i = 0;
     for (const std::unique_ptr<AidlArgument>& arg : *method.args) {
-        const Type* t = types->Find(arg->GetType().type.data);
+        const Type* t = types->Find(arg->GetType().GetName());
         Variable* v = stubArgs.Get(i++);
 
         if (arg->GetDirection() & AidlArgument::OUT_DIR) {
@@ -391,16 +391,16 @@
 
     // == the proxy method ===================================================
     Method* proxy = new Method;
-        proxy->comment = gather_comments(method.comments_token->extra);
+        proxy->comment = method.GetComments();
         proxy->modifiers = PUBLIC | OVERRIDE;
-        proxy->returnType = types->Find(method.type->type.data);
-        proxy->returnTypeDimension = method.type->dimension;
+        proxy->returnType = types->Find(method.type->GetName());
+        proxy->returnTypeDimension = method.type->GetDimension();
         proxy->name = method.name.data;
         proxy->statements = new StatementBlock;
         for (const std::unique_ptr<AidlArgument>& arg : *method.args) {
             proxy->parameters.push_back(new Variable(
-                            types->Find(arg->GetType().type.data), arg->GetName(),
-                            arg->GetType().dimension));
+                            types->Find(arg->GetType().GetName()), arg->GetName(),
+                            arg->GetType().GetDimension()));
         }
         proxy->exceptions.push_back(types->RemoteExceptionType());
     proxyClass->elements.push_back(proxy);
@@ -420,9 +420,9 @@
 
     // the return value
     _result = NULL;
-    if (0 != strcmp(method.type->type.data, "void")) {
+    if (method.type->GetName() != "void") {
         _result = new Variable(proxy->returnType, "_result",
-                method.type->dimension);
+                method.type->GetDimension());
         proxy->statements->Add(new VariableDeclaration(_result));
     }
 
@@ -438,10 +438,10 @@
 
     // the parameters
     for (const std::unique_ptr<AidlArgument>& arg : *method.args) {
-        const Type* t = types->Find(arg->GetType().type.data);
-        Variable* v = new Variable(t, arg->GetName(), arg->GetType().dimension);
+        const Type* t = types->Find(arg->GetType().GetName());
+        Variable* v = new Variable(t, arg->GetName(), arg->GetType().GetDimension());
         AidlArgument::Direction dir = arg->GetDirection();
-        if (dir == AidlArgument::OUT_DIR && arg->GetType().dimension != 0) {
+        if (dir == AidlArgument::OUT_DIR && arg->GetType().GetDimension() != 0) {
             IfStatement* checklen = new IfStatement();
             checklen->expression = new Comparison(v, "==", NULL_VALUE);
             checklen->statements->Add(new MethodCall(_data, "writeInt", 1,
@@ -479,8 +479,8 @@
 
         // the out/inout parameters
         for (const std::unique_ptr<AidlArgument>& arg : *method.args) {
-            const Type* t = types->Find(arg->GetType().type.data);
-            Variable* v = new Variable(t, arg->GetName(), arg->GetType().dimension);
+            const Type* t = types->Find(arg->GetType().GetName());
+            Variable* v = new Variable(t, arg->GetName(), arg->GetType().GetDimension());
             if (arg->GetDirection() & AidlArgument::OUT_DIR) {
                 generate_read_from_parcel(t, tryStatement->statements,
                                             v, _reply, &cl);
diff --git a/parse_helpers.cpp b/parse_helpers.cpp
index cca2021..c2f63e4 100644
--- a/parse_helpers.cpp
+++ b/parse_helpers.cpp
@@ -79,5 +79,20 @@
   return out;
 }
 
+std::string gather_comments(extra_text_type* extra) {
+  std::string s;
+  for (; extra; extra = extra->next) {
+    if (extra->which == SHORT_COMMENT) {
+      s += extra->data;
+    }
+    else if (extra->which == LONG_COMMENT) {
+      s += "/*";
+      s += extra->data;
+      s += "*/";
+    }
+  }
+  return s;
+}
+
 }  // namespace android
 }  // namespace aidl
diff --git a/parse_helpers.h b/parse_helpers.h
index 2e77624..07ecd2c 100644
--- a/parse_helpers.h
+++ b/parse_helpers.h
@@ -17,6 +17,10 @@
 #ifndef AIDL_PARSE_HELPERS_H_
 #define AIDL_PARSE_HELPERS_H_
 
+#include <string>
+
+#include "aidl_language.h"
+
 namespace android {
 namespace aidl {
 
@@ -29,6 +33,8 @@
 
 char* cpp_strdup(const char* in);
 
+std::string gather_comments(extra_text_type* extra);
+
 }  // namespace android
 }  // namespace aidl
 
diff --git a/type_namespace.cpp b/type_namespace.cpp
index 51368b7..7283473 100644
--- a/type_namespace.cpp
+++ b/type_namespace.cpp
@@ -40,10 +40,10 @@
                                       const string& filename) const {
   const string error_prefix = StringPrintf(
       "In file %s line %d return type %s%s:\n    ",
-      filename.c_str(), raw_type.type.lineno, raw_type.type.data,
+      filename.c_str(), raw_type.GetLine(), raw_type.GetName().c_str(),
       raw_type.Brackets().c_str());
 
-  const ValidatableType* return_type = GetValidatableType(raw_type.type.data);
+  const ValidatableType* return_type = GetValidatableType(raw_type.GetName());
   if (return_type == nullptr) {
     cerr << error_prefix << "unknown return type" << endl;
     return false;
@@ -54,12 +54,12 @@
     return false;
   }
 
-  if (raw_type.dimension > 0 && !return_type->CanBeArray()) {
+  if (raw_type.GetDimension() > 0 && !return_type->CanBeArray()) {
     cerr << error_prefix << "return type cannot be an array" << endl;
     return false;
   }
 
-  if (raw_type.dimension > 1) {
+  if (raw_type.GetDimension() > 1) {
     cerr << error_prefix << "only one dimensional arrays are supported" << endl;
     return false;
   }
@@ -74,44 +74,44 @@
       filename.c_str(), a.GetLine(), a.GetName().c_str(), arg_index);
 
   // check the arg type
-  const ValidatableType* t = GetValidatableType(a.GetType().type.data);
+  const ValidatableType* t = GetValidatableType(a.GetType().GetName());
   if (t == nullptr) {
-    cerr << error_prefix << "unknown type " << a.GetType().type.data << endl;
+    cerr << error_prefix << "unknown type " << a.GetType().GetName().c_str() << endl;
     return false;
   }
 
   if (!t->CanWriteToParcel()) {
     cerr << error_prefix
          << StringPrintf("'%s %s' can't be marshalled.",
-                         a.GetType().type.data, a.GetName().c_str()) << endl;
+                         a.GetType().GetName().c_str(), a.GetName().c_str()) << endl;
     return false;
   }
 
   if (!a.DirectionWasSpecified() &&
-      (a.GetType().dimension != 0 || t->CanBeOutParameter())) {
+      (a.GetType().GetDimension() != 0 || t->CanBeOutParameter())) {
     cerr << error_prefix << StringPrintf(
         "'%s %s' can be an out parameter, so you must declare it as in,"
-        " out or inout.", a.GetType().type.data, a.GetName().c_str()) << endl;
+        " out or inout.", a.GetType().GetName().c_str(), a.GetName().c_str()) << endl;
     return false;
   }
 
   if (a.GetDirection() != AidlArgument::IN_DIR &&
       !t->CanBeOutParameter() &&
-      a.GetType().dimension == 0) {
+      a.GetType().GetDimension() == 0) {
     cerr << error_prefix << StringPrintf(
         "'%s' can only be an in parameter.",
         a.ToString().c_str()) << endl;
     return false;
   }
 
-  if (a.GetType().dimension > 0 && !t->CanBeArray()) {
+  if (a.GetType().GetDimension() > 0 && !t->CanBeArray()) {
     cerr << error_prefix << StringPrintf(
         "'%s' cannot be an array.",
         a.ToString().c_str()) << endl;
     return false;
   }
 
-  if (a.GetType().dimension > 1) {
+  if (a.GetType().GetDimension() > 1) {
     cerr << error_prefix << "Only one dimensional arrays are supported."
          << endl;
     return false;